tif_dir.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /*
  25. * TIFF Library.
  26. *
  27. * Directory Tag Get & Set Routines.
  28. * (and also some miscellaneous stuff)
  29. */
  30. #include "tiffiop.h"
  31. #include <float.h> /*--: for Rational2Double */
  32. /*
  33. * These are used in the backwards compatibility code...
  34. */
  35. #define DATATYPE_VOID 0 /* !untyped data */
  36. #define DATATYPE_INT 1 /* !signed integer data */
  37. #define DATATYPE_UINT 2 /* !unsigned integer data */
  38. #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
  39. static void
  40. setByteArray(void** vpp, void* vp, size_t nmemb, size_t elem_size)
  41. {
  42. if (*vpp) {
  43. _TIFFfree(*vpp);
  44. *vpp = 0;
  45. }
  46. if (vp) {
  47. tmsize_t bytes = _TIFFMultiplySSize(NULL, nmemb, elem_size, NULL);
  48. if (bytes)
  49. *vpp = (void*) _TIFFmalloc(bytes);
  50. if (*vpp)
  51. _TIFFmemcpy(*vpp, vp, bytes);
  52. }
  53. }
  54. void _TIFFsetByteArray(void** vpp, void* vp, uint32 n)
  55. { setByteArray(vpp, vp, n, 1); }
  56. void _TIFFsetString(char** cpp, char* cp)
  57. { setByteArray((void**) cpp, (void*) cp, strlen(cp)+1, 1); }
  58. static void _TIFFsetNString(char** cpp, char* cp, uint32 n)
  59. { setByteArray((void**) cpp, (void*) cp, n, 1); }
  60. void _TIFFsetShortArray(uint16** wpp, uint16* wp, uint32 n)
  61. { setByteArray((void**) wpp, (void*) wp, n, sizeof (uint16)); }
  62. void _TIFFsetLongArray(uint32** lpp, uint32* lp, uint32 n)
  63. { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint32)); }
  64. static void _TIFFsetLong8Array(uint64** lpp, uint64* lp, uint32 n)
  65. { setByteArray((void**) lpp, (void*) lp, n, sizeof (uint64)); }
  66. void _TIFFsetFloatArray(float** fpp, float* fp, uint32 n)
  67. { setByteArray((void**) fpp, (void*) fp, n, sizeof (float)); }
  68. void _TIFFsetDoubleArray(double** dpp, double* dp, uint32 n)
  69. { setByteArray((void**) dpp, (void*) dp, n, sizeof (double)); }
  70. static void
  71. setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
  72. {
  73. if (*vpp)
  74. _TIFFfree(*vpp);
  75. *vpp = _TIFFmalloc(nmemb*sizeof(double));
  76. if (*vpp)
  77. {
  78. while (nmemb--)
  79. ((double*)*vpp)[nmemb] = value;
  80. }
  81. }
  82. /*
  83. * Install extra samples information.
  84. */
  85. static int
  86. setExtraSamples(TIFF* tif, va_list ap, uint32* v)
  87. {
  88. /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
  89. #define EXTRASAMPLE_COREL_UNASSALPHA 999
  90. uint16* va;
  91. uint32 i;
  92. TIFFDirectory* td = &tif->tif_dir;
  93. static const char module[] = "setExtraSamples";
  94. *v = (uint16) va_arg(ap, uint16_vap);
  95. if ((uint16) *v > td->td_samplesperpixel)
  96. return 0;
  97. va = va_arg(ap, uint16*);
  98. if (*v > 0 && va == NULL) /* typically missing param */
  99. return 0;
  100. for (i = 0; i < *v; i++) {
  101. if (va[i] > EXTRASAMPLE_UNASSALPHA) {
  102. /*
  103. * XXX: Corel Draw is known to produce incorrect
  104. * ExtraSamples tags which must be patched here if we
  105. * want to be able to open some of the damaged TIFF
  106. * files:
  107. */
  108. if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
  109. va[i] = EXTRASAMPLE_UNASSALPHA;
  110. else
  111. return 0;
  112. }
  113. }
  114. if ( td->td_transferfunction[0] != NULL && (td->td_samplesperpixel - *v > 1) &&
  115. !(td->td_samplesperpixel - td->td_extrasamples > 1))
  116. {
  117. TIFFWarningExt(tif->tif_clientdata,module,
  118. "ExtraSamples tag value is changing, "
  119. "but TransferFunction was read with a different value. Canceling it");
  120. TIFFClrFieldBit(tif,FIELD_TRANSFERFUNCTION);
  121. _TIFFfree(td->td_transferfunction[0]);
  122. td->td_transferfunction[0] = NULL;
  123. }
  124. td->td_extrasamples = (uint16) *v;
  125. _TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
  126. return 1;
  127. #undef EXTRASAMPLE_COREL_UNASSALPHA
  128. }
  129. /*
  130. * Confirm we have "samplesperpixel" ink names separated by \0. Returns
  131. * zero if the ink names are not as expected.
  132. */
  133. static uint32
  134. checkInkNamesString(TIFF* tif, uint32 slen, const char* s)
  135. {
  136. TIFFDirectory* td = &tif->tif_dir;
  137. uint16 i = td->td_samplesperpixel;
  138. if (slen > 0) {
  139. const char* ep = s+slen;
  140. const char* cp = s;
  141. for (; i > 0; i--) {
  142. for (; cp < ep && *cp != '\0'; cp++) {}
  143. if (cp >= ep)
  144. goto bad;
  145. cp++; /* skip \0 */
  146. }
  147. return ((uint32)(cp-s));
  148. }
  149. bad:
  150. TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
  151. "%s: Invalid InkNames value; expecting %d names, found %d",
  152. tif->tif_name,
  153. td->td_samplesperpixel,
  154. td->td_samplesperpixel-i);
  155. return (0);
  156. }
  157. static int
  158. _TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
  159. {
  160. static const char module[] = "_TIFFVSetField";
  161. TIFFDirectory* td = &tif->tif_dir;
  162. int status = 1;
  163. uint32 v32, i, v;
  164. double dblval;
  165. char* s;
  166. const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
  167. uint32 standard_tag = tag;
  168. if( fip == NULL ) /* cannot happen since OkToChangeTag() already checks it */
  169. return 0;
  170. /*
  171. * We want to force the custom code to be used for custom
  172. * fields even if the tag happens to match a well known
  173. * one - important for reinterpreted handling of standard
  174. * tag values in custom directories (i.e. EXIF)
  175. */
  176. if (fip->field_bit == FIELD_CUSTOM) {
  177. standard_tag = 0;
  178. }
  179. switch (standard_tag) {
  180. case TIFFTAG_SUBFILETYPE:
  181. td->td_subfiletype = (uint32) va_arg(ap, uint32);
  182. break;
  183. case TIFFTAG_IMAGEWIDTH:
  184. td->td_imagewidth = (uint32) va_arg(ap, uint32);
  185. break;
  186. case TIFFTAG_IMAGELENGTH:
  187. td->td_imagelength = (uint32) va_arg(ap, uint32);
  188. break;
  189. case TIFFTAG_BITSPERSAMPLE:
  190. td->td_bitspersample = (uint16) va_arg(ap, uint16_vap);
  191. /*
  192. * If the data require post-decoding processing to byte-swap
  193. * samples, set it up here. Note that since tags are required
  194. * to be ordered, compression code can override this behavior
  195. * in the setup method if it wants to roll the post decoding
  196. * work in with its normal work.
  197. */
  198. if (tif->tif_flags & TIFF_SWAB) {
  199. if (td->td_bitspersample == 8)
  200. tif->tif_postdecode = _TIFFNoPostDecode;
  201. else if (td->td_bitspersample == 16)
  202. tif->tif_postdecode = _TIFFSwab16BitData;
  203. else if (td->td_bitspersample == 24)
  204. tif->tif_postdecode = _TIFFSwab24BitData;
  205. else if (td->td_bitspersample == 32)
  206. tif->tif_postdecode = _TIFFSwab32BitData;
  207. else if (td->td_bitspersample == 64)
  208. tif->tif_postdecode = _TIFFSwab64BitData;
  209. else if (td->td_bitspersample == 128) /* two 64's */
  210. tif->tif_postdecode = _TIFFSwab64BitData;
  211. }
  212. break;
  213. case TIFFTAG_COMPRESSION:
  214. v = (uint16) va_arg(ap, uint16_vap);
  215. /*
  216. * If we're changing the compression scheme, the notify the
  217. * previous module so that it can cleanup any state it's
  218. * setup.
  219. */
  220. if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
  221. if ((uint32)td->td_compression == v)
  222. break;
  223. (*tif->tif_cleanup)(tif);
  224. tif->tif_flags &= ~TIFF_CODERSETUP;
  225. }
  226. /*
  227. * Setup new compression routine state.
  228. */
  229. if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
  230. td->td_compression = (uint16) v;
  231. else
  232. status = 0;
  233. break;
  234. case TIFFTAG_PHOTOMETRIC:
  235. td->td_photometric = (uint16) va_arg(ap, uint16_vap);
  236. break;
  237. case TIFFTAG_THRESHHOLDING:
  238. td->td_threshholding = (uint16) va_arg(ap, uint16_vap);
  239. break;
  240. case TIFFTAG_FILLORDER:
  241. v = (uint16) va_arg(ap, uint16_vap);
  242. if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
  243. goto badvalue;
  244. td->td_fillorder = (uint16) v;
  245. break;
  246. case TIFFTAG_ORIENTATION:
  247. v = (uint16) va_arg(ap, uint16_vap);
  248. if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
  249. goto badvalue;
  250. else
  251. td->td_orientation = (uint16) v;
  252. break;
  253. case TIFFTAG_SAMPLESPERPIXEL:
  254. v = (uint16) va_arg(ap, uint16_vap);
  255. if (v == 0)
  256. goto badvalue;
  257. if( v != td->td_samplesperpixel )
  258. {
  259. /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
  260. if( td->td_sminsamplevalue != NULL )
  261. {
  262. TIFFWarningExt(tif->tif_clientdata,module,
  263. "SamplesPerPixel tag value is changing, "
  264. "but SMinSampleValue tag was read with a different value. Canceling it");
  265. TIFFClrFieldBit(tif,FIELD_SMINSAMPLEVALUE);
  266. _TIFFfree(td->td_sminsamplevalue);
  267. td->td_sminsamplevalue = NULL;
  268. }
  269. if( td->td_smaxsamplevalue != NULL )
  270. {
  271. TIFFWarningExt(tif->tif_clientdata,module,
  272. "SamplesPerPixel tag value is changing, "
  273. "but SMaxSampleValue tag was read with a different value. Canceling it");
  274. TIFFClrFieldBit(tif,FIELD_SMAXSAMPLEVALUE);
  275. _TIFFfree(td->td_smaxsamplevalue);
  276. td->td_smaxsamplevalue = NULL;
  277. }
  278. /* Test if 3 transfer functions instead of just one are now needed
  279. See http://bugzilla.maptools.org/show_bug.cgi?id=2820 */
  280. if( td->td_transferfunction[0] != NULL && (v - td->td_extrasamples > 1) &&
  281. !(td->td_samplesperpixel - td->td_extrasamples > 1))
  282. {
  283. TIFFWarningExt(tif->tif_clientdata,module,
  284. "SamplesPerPixel tag value is changing, "
  285. "but TransferFunction was read with a different value. Canceling it");
  286. TIFFClrFieldBit(tif,FIELD_TRANSFERFUNCTION);
  287. _TIFFfree(td->td_transferfunction[0]);
  288. td->td_transferfunction[0] = NULL;
  289. }
  290. }
  291. td->td_samplesperpixel = (uint16) v;
  292. break;
  293. case TIFFTAG_ROWSPERSTRIP:
  294. v32 = (uint32) va_arg(ap, uint32);
  295. if (v32 == 0)
  296. goto badvalue32;
  297. td->td_rowsperstrip = v32;
  298. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  299. td->td_tilelength = v32;
  300. td->td_tilewidth = td->td_imagewidth;
  301. }
  302. break;
  303. case TIFFTAG_MINSAMPLEVALUE:
  304. td->td_minsamplevalue = (uint16) va_arg(ap, uint16_vap);
  305. break;
  306. case TIFFTAG_MAXSAMPLEVALUE:
  307. td->td_maxsamplevalue = (uint16) va_arg(ap, uint16_vap);
  308. break;
  309. case TIFFTAG_SMINSAMPLEVALUE:
  310. if (tif->tif_flags & TIFF_PERSAMPLE)
  311. _TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
  312. else
  313. setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
  314. break;
  315. case TIFFTAG_SMAXSAMPLEVALUE:
  316. if (tif->tif_flags & TIFF_PERSAMPLE)
  317. _TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
  318. else
  319. setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
  320. break;
  321. case TIFFTAG_XRESOLUTION:
  322. dblval = va_arg(ap, double);
  323. if( dblval < 0 )
  324. goto badvaluedouble;
  325. td->td_xresolution = _TIFFClampDoubleToFloat( dblval );
  326. break;
  327. case TIFFTAG_YRESOLUTION:
  328. dblval = va_arg(ap, double);
  329. if( dblval < 0 )
  330. goto badvaluedouble;
  331. td->td_yresolution = _TIFFClampDoubleToFloat( dblval );
  332. break;
  333. case TIFFTAG_PLANARCONFIG:
  334. v = (uint16) va_arg(ap, uint16_vap);
  335. if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
  336. goto badvalue;
  337. td->td_planarconfig = (uint16) v;
  338. break;
  339. case TIFFTAG_XPOSITION:
  340. td->td_xposition = _TIFFClampDoubleToFloat( va_arg(ap, double) );
  341. break;
  342. case TIFFTAG_YPOSITION:
  343. td->td_yposition = _TIFFClampDoubleToFloat( va_arg(ap, double) );
  344. break;
  345. case TIFFTAG_RESOLUTIONUNIT:
  346. v = (uint16) va_arg(ap, uint16_vap);
  347. if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
  348. goto badvalue;
  349. td->td_resolutionunit = (uint16) v;
  350. break;
  351. case TIFFTAG_PAGENUMBER:
  352. td->td_pagenumber[0] = (uint16) va_arg(ap, uint16_vap);
  353. td->td_pagenumber[1] = (uint16) va_arg(ap, uint16_vap);
  354. break;
  355. case TIFFTAG_HALFTONEHINTS:
  356. td->td_halftonehints[0] = (uint16) va_arg(ap, uint16_vap);
  357. td->td_halftonehints[1] = (uint16) va_arg(ap, uint16_vap);
  358. break;
  359. case TIFFTAG_COLORMAP:
  360. v32 = (uint32)(1L<<td->td_bitspersample);
  361. _TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
  362. _TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
  363. _TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
  364. break;
  365. case TIFFTAG_EXTRASAMPLES:
  366. if (!setExtraSamples(tif, ap, &v))
  367. goto badvalue;
  368. break;
  369. case TIFFTAG_MATTEING:
  370. td->td_extrasamples = (((uint16) va_arg(ap, uint16_vap)) != 0);
  371. if (td->td_extrasamples) {
  372. uint16 sv = EXTRASAMPLE_ASSOCALPHA;
  373. _TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
  374. }
  375. break;
  376. case TIFFTAG_TILEWIDTH:
  377. v32 = (uint32) va_arg(ap, uint32);
  378. if (v32 % 16) {
  379. if (tif->tif_mode != O_RDONLY)
  380. goto badvalue32;
  381. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  382. "Nonstandard tile width %u, convert file", v32);
  383. }
  384. td->td_tilewidth = v32;
  385. tif->tif_flags |= TIFF_ISTILED;
  386. break;
  387. case TIFFTAG_TILELENGTH:
  388. v32 = (uint32) va_arg(ap, uint32);
  389. if (v32 % 16) {
  390. if (tif->tif_mode != O_RDONLY)
  391. goto badvalue32;
  392. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  393. "Nonstandard tile length %u, convert file", v32);
  394. }
  395. td->td_tilelength = v32;
  396. tif->tif_flags |= TIFF_ISTILED;
  397. break;
  398. case TIFFTAG_TILEDEPTH:
  399. v32 = (uint32) va_arg(ap, uint32);
  400. if (v32 == 0)
  401. goto badvalue32;
  402. td->td_tiledepth = v32;
  403. break;
  404. case TIFFTAG_DATATYPE:
  405. v = (uint16) va_arg(ap, uint16_vap);
  406. switch (v) {
  407. case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
  408. case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
  409. case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
  410. case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
  411. default: goto badvalue;
  412. }
  413. td->td_sampleformat = (uint16) v;
  414. break;
  415. case TIFFTAG_SAMPLEFORMAT:
  416. v = (uint16) va_arg(ap, uint16_vap);
  417. if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
  418. goto badvalue;
  419. td->td_sampleformat = (uint16) v;
  420. /* Try to fix up the SWAB function for complex data. */
  421. if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
  422. && td->td_bitspersample == 32
  423. && tif->tif_postdecode == _TIFFSwab32BitData )
  424. tif->tif_postdecode = _TIFFSwab16BitData;
  425. else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
  426. || td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
  427. && td->td_bitspersample == 64
  428. && tif->tif_postdecode == _TIFFSwab64BitData )
  429. tif->tif_postdecode = _TIFFSwab32BitData;
  430. break;
  431. case TIFFTAG_IMAGEDEPTH:
  432. td->td_imagedepth = (uint32) va_arg(ap, uint32);
  433. break;
  434. case TIFFTAG_SUBIFD:
  435. if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
  436. td->td_nsubifd = (uint16) va_arg(ap, uint16_vap);
  437. _TIFFsetLong8Array(&td->td_subifd, (uint64*) va_arg(ap, uint64*),
  438. (uint32) td->td_nsubifd);
  439. } else {
  440. TIFFErrorExt(tif->tif_clientdata, module,
  441. "%s: Sorry, cannot nest SubIFDs",
  442. tif->tif_name);
  443. status = 0;
  444. }
  445. break;
  446. case TIFFTAG_YCBCRPOSITIONING:
  447. td->td_ycbcrpositioning = (uint16) va_arg(ap, uint16_vap);
  448. break;
  449. case TIFFTAG_YCBCRSUBSAMPLING:
  450. td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, uint16_vap);
  451. td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, uint16_vap);
  452. break;
  453. case TIFFTAG_TRANSFERFUNCTION:
  454. v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
  455. for (i = 0; i < v; i++)
  456. _TIFFsetShortArray(&td->td_transferfunction[i],
  457. va_arg(ap, uint16*), 1U<<td->td_bitspersample);
  458. break;
  459. case TIFFTAG_REFERENCEBLACKWHITE:
  460. /* XXX should check for null range */
  461. _TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
  462. break;
  463. case TIFFTAG_INKNAMES:
  464. v = (uint16) va_arg(ap, uint16_vap);
  465. s = va_arg(ap, char*);
  466. v = checkInkNamesString(tif, v, s);
  467. status = v > 0;
  468. if( v > 0 ) {
  469. _TIFFsetNString(&td->td_inknames, s, v);
  470. td->td_inknameslen = v;
  471. }
  472. break;
  473. case TIFFTAG_PERSAMPLE:
  474. v = (uint16) va_arg(ap, uint16_vap);
  475. if( v == PERSAMPLE_MULTI )
  476. tif->tif_flags |= TIFF_PERSAMPLE;
  477. else
  478. tif->tif_flags &= ~TIFF_PERSAMPLE;
  479. break;
  480. default: {
  481. TIFFTagValue *tv;
  482. int tv_size, iCustom;
  483. /*
  484. * This can happen if multiple images are open with different
  485. * codecs which have private tags. The global tag information
  486. * table may then have tags that are valid for one file but not
  487. * the other. If the client tries to set a tag that is not valid
  488. * for the image's codec then we'll arrive here. This
  489. * happens, for example, when tiffcp is used to convert between
  490. * compression schemes and codec-specific tags are blindly copied.
  491. */
  492. if(fip->field_bit != FIELD_CUSTOM) {
  493. TIFFErrorExt(tif->tif_clientdata, module,
  494. "%s: Invalid %stag \"%s\" (not supported by codec)",
  495. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
  496. fip->field_name);
  497. status = 0;
  498. break;
  499. }
  500. /*
  501. * Find the existing entry for this custom value.
  502. */
  503. tv = NULL;
  504. for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++) {
  505. if (td->td_customValues[iCustom].info->field_tag == tag) {
  506. tv = td->td_customValues + iCustom;
  507. if (tv->value != NULL) {
  508. _TIFFfree(tv->value);
  509. tv->value = NULL;
  510. }
  511. break;
  512. }
  513. }
  514. /*
  515. * Grow the custom list if the entry was not found.
  516. */
  517. if(tv == NULL) {
  518. TIFFTagValue *new_customValues;
  519. td->td_customValueCount++;
  520. new_customValues = (TIFFTagValue *)
  521. _TIFFrealloc(td->td_customValues,
  522. sizeof(TIFFTagValue) * td->td_customValueCount);
  523. if (!new_customValues) {
  524. TIFFErrorExt(tif->tif_clientdata, module,
  525. "%s: Failed to allocate space for list of custom values",
  526. tif->tif_name);
  527. status = 0;
  528. goto end;
  529. }
  530. td->td_customValues = new_customValues;
  531. tv = td->td_customValues + (td->td_customValueCount - 1);
  532. tv->info = fip;
  533. tv->value = NULL;
  534. tv->count = 0;
  535. }
  536. /*
  537. * Set custom value ... save a copy of the custom tag value.
  538. */
  539. tv_size = _TIFFDataSize(fip->field_type);
  540. /*--: Rational2Double: For Rationals evaluate "set_field_type" to determine internal storage size. */
  541. if (fip->field_type == TIFF_RATIONAL || fip->field_type == TIFF_SRATIONAL) {
  542. tv_size = _TIFFSetGetFieldSize(fip->set_field_type);
  543. }
  544. if (tv_size == 0) {
  545. status = 0;
  546. TIFFErrorExt(tif->tif_clientdata, module,
  547. "%s: Bad field type %d for \"%s\"",
  548. tif->tif_name, fip->field_type,
  549. fip->field_name);
  550. goto end;
  551. }
  552. if (fip->field_type == TIFF_ASCII)
  553. {
  554. uint32 ma;
  555. char* mb;
  556. if (fip->field_passcount)
  557. {
  558. assert(fip->field_writecount==TIFF_VARIABLE2);
  559. ma=(uint32)va_arg(ap,uint32);
  560. mb=(char*)va_arg(ap,char*);
  561. }
  562. else
  563. {
  564. mb=(char*)va_arg(ap,char*);
  565. ma=(uint32)(strlen(mb)+1);
  566. }
  567. tv->count=ma;
  568. setByteArray(&tv->value,mb,ma,1);
  569. }
  570. else
  571. {
  572. if (fip->field_passcount) {
  573. if (fip->field_writecount == TIFF_VARIABLE2)
  574. tv->count = (uint32) va_arg(ap, uint32);
  575. else
  576. tv->count = (int) va_arg(ap, int);
  577. } else if (fip->field_writecount == TIFF_VARIABLE
  578. || fip->field_writecount == TIFF_VARIABLE2)
  579. tv->count = 1;
  580. else if (fip->field_writecount == TIFF_SPP)
  581. tv->count = td->td_samplesperpixel;
  582. else
  583. tv->count = fip->field_writecount;
  584. if (tv->count == 0) {
  585. status = 0;
  586. TIFFErrorExt(tif->tif_clientdata, module,
  587. "%s: Null count for \"%s\" (type "
  588. "%d, writecount %d, passcount %d)",
  589. tif->tif_name,
  590. fip->field_name,
  591. fip->field_type,
  592. fip->field_writecount,
  593. fip->field_passcount);
  594. goto end;
  595. }
  596. tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
  597. "custom tag binary object");
  598. if (!tv->value) {
  599. status = 0;
  600. goto end;
  601. }
  602. if (fip->field_tag == TIFFTAG_DOTRANGE
  603. && strcmp(fip->field_name,"DotRange") == 0) {
  604. /* TODO: This is an evil exception and should not have been
  605. handled this way ... likely best if we move it into
  606. the directory structure with an explicit field in
  607. libtiff 4.1 and assign it a FIELD_ value */
  608. uint16 v2[2];
  609. v2[0] = (uint16)va_arg(ap, int);
  610. v2[1] = (uint16)va_arg(ap, int);
  611. _TIFFmemcpy(tv->value, &v2, 4);
  612. }
  613. else if (fip->field_passcount
  614. || fip->field_writecount == TIFF_VARIABLE
  615. || fip->field_writecount == TIFF_VARIABLE2
  616. || fip->field_writecount == TIFF_SPP
  617. || tv->count > 1) {
  618. /*--: Rational2Double: For Rationals tv_size is set above to 4 or 8 according to fip->set_field_type! */
  619. _TIFFmemcpy(tv->value, va_arg(ap, void *),
  620. tv->count * tv_size);
  621. } else {
  622. char *val = (char *)tv->value;
  623. assert( tv->count == 1 );
  624. switch (fip->field_type) {
  625. case TIFF_BYTE:
  626. case TIFF_UNDEFINED:
  627. {
  628. uint8 v2 = (uint8)va_arg(ap, int);
  629. _TIFFmemcpy(val, &v2, tv_size);
  630. }
  631. break;
  632. case TIFF_SBYTE:
  633. {
  634. int8 v2 = (int8)va_arg(ap, int);
  635. _TIFFmemcpy(val, &v2, tv_size);
  636. }
  637. break;
  638. case TIFF_SHORT:
  639. {
  640. uint16 v2 = (uint16)va_arg(ap, int);
  641. _TIFFmemcpy(val, &v2, tv_size);
  642. }
  643. break;
  644. case TIFF_SSHORT:
  645. {
  646. int16 v2 = (int16)va_arg(ap, int);
  647. _TIFFmemcpy(val, &v2, tv_size);
  648. }
  649. break;
  650. case TIFF_LONG:
  651. case TIFF_IFD:
  652. {
  653. uint32 v2 = va_arg(ap, uint32);
  654. _TIFFmemcpy(val, &v2, tv_size);
  655. }
  656. break;
  657. case TIFF_SLONG:
  658. {
  659. int32 v2 = va_arg(ap, int32);
  660. _TIFFmemcpy(val, &v2, tv_size);
  661. }
  662. break;
  663. case TIFF_LONG8:
  664. case TIFF_IFD8:
  665. {
  666. uint64 v2 = va_arg(ap, uint64);
  667. _TIFFmemcpy(val, &v2, tv_size);
  668. }
  669. break;
  670. case TIFF_SLONG8:
  671. {
  672. int64 v2 = va_arg(ap, int64);
  673. _TIFFmemcpy(val, &v2, tv_size);
  674. }
  675. break;
  676. case TIFF_RATIONAL:
  677. case TIFF_SRATIONAL:
  678. /*-- Rational2Double: For Rationals tv_size is set above to 4 or 8 according to fip->set_field_type! */
  679. {
  680. if (tv_size == 8) {
  681. double v2 = va_arg(ap, double);
  682. _TIFFmemcpy(val, &v2, tv_size);
  683. } else {
  684. /*-- default should be tv_size == 4 */
  685. float v3 = (float)va_arg(ap, double);
  686. _TIFFmemcpy(val, &v3, tv_size);
  687. /*-- ToDo: After Testing, this should be removed and tv_size==4 should be set as default. */
  688. if (tv_size != 4) {
  689. TIFFErrorExt(0,"TIFFLib: _TIFFVSetField()", "Rational2Double: .set_field_type in not 4 but %d", tv_size);
  690. }
  691. }
  692. }
  693. break;
  694. case TIFF_FLOAT:
  695. {
  696. float v2 = _TIFFClampDoubleToFloat(va_arg(ap, double));
  697. _TIFFmemcpy(val, &v2, tv_size);
  698. }
  699. break;
  700. case TIFF_DOUBLE:
  701. {
  702. double v2 = va_arg(ap, double);
  703. _TIFFmemcpy(val, &v2, tv_size);
  704. }
  705. break;
  706. default:
  707. _TIFFmemset(val, 0, tv_size);
  708. status = 0;
  709. break;
  710. }
  711. }
  712. }
  713. }
  714. }
  715. if (status) {
  716. const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
  717. if (fip2)
  718. TIFFSetFieldBit(tif, fip2->field_bit);
  719. tif->tif_flags |= TIFF_DIRTYDIRECT;
  720. }
  721. end:
  722. va_end(ap);
  723. return (status);
  724. badvalue:
  725. {
  726. const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
  727. TIFFErrorExt(tif->tif_clientdata, module,
  728. "%s: Bad value %u for \"%s\" tag",
  729. tif->tif_name, v,
  730. fip2 ? fip2->field_name : "Unknown");
  731. va_end(ap);
  732. }
  733. return (0);
  734. badvalue32:
  735. {
  736. const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
  737. TIFFErrorExt(tif->tif_clientdata, module,
  738. "%s: Bad value %u for \"%s\" tag",
  739. tif->tif_name, v32,
  740. fip2 ? fip2->field_name : "Unknown");
  741. va_end(ap);
  742. }
  743. return (0);
  744. badvaluedouble:
  745. {
  746. const TIFFField* fip2=TIFFFieldWithTag(tif,tag);
  747. TIFFErrorExt(tif->tif_clientdata, module,
  748. "%s: Bad value %f for \"%s\" tag",
  749. tif->tif_name, dblval,
  750. fip2 ? fip2->field_name : "Unknown");
  751. va_end(ap);
  752. }
  753. return (0);
  754. }
  755. /*
  756. * Return 1/0 according to whether or not
  757. * it is permissible to set the tag's value.
  758. * Note that we allow ImageLength to be changed
  759. * so that we can append and extend to images.
  760. * Any other tag may not be altered once writing
  761. * has commenced, unless its value has no effect
  762. * on the format of the data that is written.
  763. */
  764. static int
  765. OkToChangeTag(TIFF* tif, uint32 tag)
  766. {
  767. const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
  768. if (!fip) { /* unknown tag */
  769. TIFFErrorExt(tif->tif_clientdata, "TIFFSetField", "%s: Unknown %stag %u",
  770. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
  771. return (0);
  772. }
  773. if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
  774. !fip->field_oktochange) {
  775. /*
  776. * Consult info table to see if tag can be changed
  777. * after we've started writing. We only allow changes
  778. * to those tags that don't/shouldn't affect the
  779. * compression and/or format of the data.
  780. */
  781. TIFFErrorExt(tif->tif_clientdata, "TIFFSetField",
  782. "%s: Cannot modify tag \"%s\" while writing",
  783. tif->tif_name, fip->field_name);
  784. return (0);
  785. }
  786. return (1);
  787. }
  788. /*
  789. * Record the value of a field in the
  790. * internal directory structure. The
  791. * field will be written to the file
  792. * when/if the directory structure is
  793. * updated.
  794. */
  795. int
  796. TIFFSetField(TIFF* tif, uint32 tag, ...)
  797. {
  798. va_list ap;
  799. int status;
  800. va_start(ap, tag);
  801. status = TIFFVSetField(tif, tag, ap);
  802. va_end(ap);
  803. return (status);
  804. }
  805. /*
  806. * Clear the contents of the field in the internal structure.
  807. */
  808. int
  809. TIFFUnsetField(TIFF* tif, uint32 tag)
  810. {
  811. const TIFFField *fip = TIFFFieldWithTag(tif, tag);
  812. TIFFDirectory* td = &tif->tif_dir;
  813. if( !fip )
  814. return 0;
  815. if( fip->field_bit != FIELD_CUSTOM )
  816. TIFFClrFieldBit(tif, fip->field_bit);
  817. else
  818. {
  819. TIFFTagValue *tv = NULL;
  820. int i;
  821. for (i = 0; i < td->td_customValueCount; i++) {
  822. tv = td->td_customValues + i;
  823. if( tv->info->field_tag == tag )
  824. break;
  825. }
  826. if( i < td->td_customValueCount )
  827. {
  828. _TIFFfree(tv->value);
  829. for( ; i < td->td_customValueCount-1; i++) {
  830. td->td_customValues[i] = td->td_customValues[i+1];
  831. }
  832. td->td_customValueCount--;
  833. }
  834. }
  835. tif->tif_flags |= TIFF_DIRTYDIRECT;
  836. return (1);
  837. }
  838. /*
  839. * Like TIFFSetField, but taking a varargs
  840. * parameter list. This routine is useful
  841. * for building higher-level interfaces on
  842. * top of the library.
  843. */
  844. int
  845. TIFFVSetField(TIFF* tif, uint32 tag, va_list ap)
  846. {
  847. return OkToChangeTag(tif, tag) ?
  848. (*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
  849. }
  850. static int
  851. _TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
  852. {
  853. TIFFDirectory* td = &tif->tif_dir;
  854. int ret_val = 1;
  855. uint32 standard_tag = tag;
  856. const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
  857. if( fip == NULL ) /* cannot happen since TIFFGetField() already checks it */
  858. return 0;
  859. /*
  860. * We want to force the custom code to be used for custom
  861. * fields even if the tag happens to match a well known
  862. * one - important for reinterpreted handling of standard
  863. * tag values in custom directories (i.e. EXIF)
  864. */
  865. if (fip->field_bit == FIELD_CUSTOM) {
  866. standard_tag = 0;
  867. }
  868. if( standard_tag == TIFFTAG_NUMBEROFINKS )
  869. {
  870. int i;
  871. for (i = 0; i < td->td_customValueCount; i++) {
  872. uint16 val;
  873. TIFFTagValue *tv = td->td_customValues + i;
  874. if (tv->info->field_tag != standard_tag)
  875. continue;
  876. if( tv->value == NULL )
  877. return 0;
  878. val = *(uint16 *)tv->value;
  879. /* Truncate to SamplesPerPixel, since the */
  880. /* setting code for INKNAMES assume that there are SamplesPerPixel */
  881. /* inknames. */
  882. /* Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599 */
  883. if( val > td->td_samplesperpixel )
  884. {
  885. TIFFWarningExt(tif->tif_clientdata,"_TIFFVGetField",
  886. "Truncating NumberOfInks from %u to %u",
  887. val, td->td_samplesperpixel);
  888. val = td->td_samplesperpixel;
  889. }
  890. *va_arg(ap, uint16*) = val;
  891. return 1;
  892. }
  893. return 0;
  894. }
  895. switch (standard_tag) {
  896. case TIFFTAG_SUBFILETYPE:
  897. *va_arg(ap, uint32*) = td->td_subfiletype;
  898. break;
  899. case TIFFTAG_IMAGEWIDTH:
  900. *va_arg(ap, uint32*) = td->td_imagewidth;
  901. break;
  902. case TIFFTAG_IMAGELENGTH:
  903. *va_arg(ap, uint32*) = td->td_imagelength;
  904. break;
  905. case TIFFTAG_BITSPERSAMPLE:
  906. *va_arg(ap, uint16*) = td->td_bitspersample;
  907. break;
  908. case TIFFTAG_COMPRESSION:
  909. *va_arg(ap, uint16*) = td->td_compression;
  910. break;
  911. case TIFFTAG_PHOTOMETRIC:
  912. *va_arg(ap, uint16*) = td->td_photometric;
  913. break;
  914. case TIFFTAG_THRESHHOLDING:
  915. *va_arg(ap, uint16*) = td->td_threshholding;
  916. break;
  917. case TIFFTAG_FILLORDER:
  918. *va_arg(ap, uint16*) = td->td_fillorder;
  919. break;
  920. case TIFFTAG_ORIENTATION:
  921. *va_arg(ap, uint16*) = td->td_orientation;
  922. break;
  923. case TIFFTAG_SAMPLESPERPIXEL:
  924. *va_arg(ap, uint16*) = td->td_samplesperpixel;
  925. break;
  926. case TIFFTAG_ROWSPERSTRIP:
  927. *va_arg(ap, uint32*) = td->td_rowsperstrip;
  928. break;
  929. case TIFFTAG_MINSAMPLEVALUE:
  930. *va_arg(ap, uint16*) = td->td_minsamplevalue;
  931. break;
  932. case TIFFTAG_MAXSAMPLEVALUE:
  933. *va_arg(ap, uint16*) = td->td_maxsamplevalue;
  934. break;
  935. case TIFFTAG_SMINSAMPLEVALUE:
  936. if (tif->tif_flags & TIFF_PERSAMPLE)
  937. *va_arg(ap, double**) = td->td_sminsamplevalue;
  938. else
  939. {
  940. /* libtiff historically treats this as a single value. */
  941. uint16 i;
  942. double v = td->td_sminsamplevalue[0];
  943. for (i=1; i < td->td_samplesperpixel; ++i)
  944. if( td->td_sminsamplevalue[i] < v )
  945. v = td->td_sminsamplevalue[i];
  946. *va_arg(ap, double*) = v;
  947. }
  948. break;
  949. case TIFFTAG_SMAXSAMPLEVALUE:
  950. if (tif->tif_flags & TIFF_PERSAMPLE)
  951. *va_arg(ap, double**) = td->td_smaxsamplevalue;
  952. else
  953. {
  954. /* libtiff historically treats this as a single value. */
  955. uint16 i;
  956. double v = td->td_smaxsamplevalue[0];
  957. for (i=1; i < td->td_samplesperpixel; ++i)
  958. if( td->td_smaxsamplevalue[i] > v )
  959. v = td->td_smaxsamplevalue[i];
  960. *va_arg(ap, double*) = v;
  961. }
  962. break;
  963. case TIFFTAG_XRESOLUTION:
  964. *va_arg(ap, float*) = td->td_xresolution;
  965. break;
  966. case TIFFTAG_YRESOLUTION:
  967. *va_arg(ap, float*) = td->td_yresolution;
  968. break;
  969. case TIFFTAG_PLANARCONFIG:
  970. *va_arg(ap, uint16*) = td->td_planarconfig;
  971. break;
  972. case TIFFTAG_XPOSITION:
  973. *va_arg(ap, float*) = td->td_xposition;
  974. break;
  975. case TIFFTAG_YPOSITION:
  976. *va_arg(ap, float*) = td->td_yposition;
  977. break;
  978. case TIFFTAG_RESOLUTIONUNIT:
  979. *va_arg(ap, uint16*) = td->td_resolutionunit;
  980. break;
  981. case TIFFTAG_PAGENUMBER:
  982. *va_arg(ap, uint16*) = td->td_pagenumber[0];
  983. *va_arg(ap, uint16*) = td->td_pagenumber[1];
  984. break;
  985. case TIFFTAG_HALFTONEHINTS:
  986. *va_arg(ap, uint16*) = td->td_halftonehints[0];
  987. *va_arg(ap, uint16*) = td->td_halftonehints[1];
  988. break;
  989. case TIFFTAG_COLORMAP:
  990. *va_arg(ap, const uint16**) = td->td_colormap[0];
  991. *va_arg(ap, const uint16**) = td->td_colormap[1];
  992. *va_arg(ap, const uint16**) = td->td_colormap[2];
  993. break;
  994. case TIFFTAG_STRIPOFFSETS:
  995. case TIFFTAG_TILEOFFSETS:
  996. _TIFFFillStriles( tif );
  997. *va_arg(ap, const uint64**) = td->td_stripoffset_p;
  998. break;
  999. case TIFFTAG_STRIPBYTECOUNTS:
  1000. case TIFFTAG_TILEBYTECOUNTS:
  1001. _TIFFFillStriles( tif );
  1002. *va_arg(ap, const uint64**) = td->td_stripbytecount_p;
  1003. break;
  1004. case TIFFTAG_MATTEING:
  1005. *va_arg(ap, uint16*) =
  1006. (td->td_extrasamples == 1 &&
  1007. td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  1008. break;
  1009. case TIFFTAG_EXTRASAMPLES:
  1010. *va_arg(ap, uint16*) = td->td_extrasamples;
  1011. *va_arg(ap, const uint16**) = td->td_sampleinfo;
  1012. break;
  1013. case TIFFTAG_TILEWIDTH:
  1014. *va_arg(ap, uint32*) = td->td_tilewidth;
  1015. break;
  1016. case TIFFTAG_TILELENGTH:
  1017. *va_arg(ap, uint32*) = td->td_tilelength;
  1018. break;
  1019. case TIFFTAG_TILEDEPTH:
  1020. *va_arg(ap, uint32*) = td->td_tiledepth;
  1021. break;
  1022. case TIFFTAG_DATATYPE:
  1023. switch (td->td_sampleformat) {
  1024. case SAMPLEFORMAT_UINT:
  1025. *va_arg(ap, uint16*) = DATATYPE_UINT;
  1026. break;
  1027. case SAMPLEFORMAT_INT:
  1028. *va_arg(ap, uint16*) = DATATYPE_INT;
  1029. break;
  1030. case SAMPLEFORMAT_IEEEFP:
  1031. *va_arg(ap, uint16*) = DATATYPE_IEEEFP;
  1032. break;
  1033. case SAMPLEFORMAT_VOID:
  1034. *va_arg(ap, uint16*) = DATATYPE_VOID;
  1035. break;
  1036. }
  1037. break;
  1038. case TIFFTAG_SAMPLEFORMAT:
  1039. *va_arg(ap, uint16*) = td->td_sampleformat;
  1040. break;
  1041. case TIFFTAG_IMAGEDEPTH:
  1042. *va_arg(ap, uint32*) = td->td_imagedepth;
  1043. break;
  1044. case TIFFTAG_SUBIFD:
  1045. *va_arg(ap, uint16*) = td->td_nsubifd;
  1046. *va_arg(ap, const uint64**) = td->td_subifd;
  1047. break;
  1048. case TIFFTAG_YCBCRPOSITIONING:
  1049. *va_arg(ap, uint16*) = td->td_ycbcrpositioning;
  1050. break;
  1051. case TIFFTAG_YCBCRSUBSAMPLING:
  1052. *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
  1053. *va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
  1054. break;
  1055. case TIFFTAG_TRANSFERFUNCTION:
  1056. *va_arg(ap, const uint16**) = td->td_transferfunction[0];
  1057. if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  1058. *va_arg(ap, const uint16**) = td->td_transferfunction[1];
  1059. *va_arg(ap, const uint16**) = td->td_transferfunction[2];
  1060. } else {
  1061. *va_arg(ap, const uint16**) = NULL;
  1062. *va_arg(ap, const uint16**) = NULL;
  1063. }
  1064. break;
  1065. case TIFFTAG_REFERENCEBLACKWHITE:
  1066. *va_arg(ap, const float**) = td->td_refblackwhite;
  1067. break;
  1068. case TIFFTAG_INKNAMES:
  1069. *va_arg(ap, const char**) = td->td_inknames;
  1070. break;
  1071. default:
  1072. {
  1073. int i;
  1074. /*
  1075. * This can happen if multiple images are open
  1076. * with different codecs which have private
  1077. * tags. The global tag information table may
  1078. * then have tags that are valid for one file
  1079. * but not the other. If the client tries to
  1080. * get a tag that is not valid for the image's
  1081. * codec then we'll arrive here.
  1082. */
  1083. if( fip->field_bit != FIELD_CUSTOM )
  1084. {
  1085. TIFFErrorExt(tif->tif_clientdata, "_TIFFVGetField",
  1086. "%s: Invalid %stag \"%s\" "
  1087. "(not supported by codec)",
  1088. tif->tif_name,
  1089. isPseudoTag(tag) ? "pseudo-" : "",
  1090. fip->field_name);
  1091. ret_val = 0;
  1092. break;
  1093. }
  1094. /*
  1095. * Do we have a custom value?
  1096. */
  1097. ret_val = 0;
  1098. for (i = 0; i < td->td_customValueCount; i++) {
  1099. TIFFTagValue *tv = td->td_customValues + i;
  1100. if (tv->info->field_tag != tag)
  1101. continue;
  1102. if (fip->field_passcount) {
  1103. if (fip->field_readcount == TIFF_VARIABLE2)
  1104. *va_arg(ap, uint32*) = (uint32)tv->count;
  1105. else /* Assume TIFF_VARIABLE */
  1106. *va_arg(ap, uint16*) = (uint16)tv->count;
  1107. *va_arg(ap, const void **) = tv->value;
  1108. ret_val = 1;
  1109. } else if (fip->field_tag == TIFFTAG_DOTRANGE
  1110. && strcmp(fip->field_name,"DotRange") == 0) {
  1111. /* TODO: This is an evil exception and should not have been
  1112. handled this way ... likely best if we move it into
  1113. the directory structure with an explicit field in
  1114. libtiff 4.1 and assign it a FIELD_ value */
  1115. *va_arg(ap, uint16*) = ((uint16 *)tv->value)[0];
  1116. *va_arg(ap, uint16*) = ((uint16 *)tv->value)[1];
  1117. ret_val = 1;
  1118. } else {
  1119. if (fip->field_type == TIFF_ASCII
  1120. || fip->field_readcount == TIFF_VARIABLE
  1121. || fip->field_readcount == TIFF_VARIABLE2
  1122. || fip->field_readcount == TIFF_SPP
  1123. || tv->count > 1) {
  1124. *va_arg(ap, void **) = tv->value;
  1125. ret_val = 1;
  1126. } else {
  1127. char *val = (char *)tv->value;
  1128. assert( tv->count == 1 );
  1129. switch (fip->field_type) {
  1130. case TIFF_BYTE:
  1131. case TIFF_UNDEFINED:
  1132. *va_arg(ap, uint8*) =
  1133. *(uint8 *)val;
  1134. ret_val = 1;
  1135. break;
  1136. case TIFF_SBYTE:
  1137. *va_arg(ap, int8*) =
  1138. *(int8 *)val;
  1139. ret_val = 1;
  1140. break;
  1141. case TIFF_SHORT:
  1142. *va_arg(ap, uint16*) =
  1143. *(uint16 *)val;
  1144. ret_val = 1;
  1145. break;
  1146. case TIFF_SSHORT:
  1147. *va_arg(ap, int16*) =
  1148. *(int16 *)val;
  1149. ret_val = 1;
  1150. break;
  1151. case TIFF_LONG:
  1152. case TIFF_IFD:
  1153. *va_arg(ap, uint32*) =
  1154. *(uint32 *)val;
  1155. ret_val = 1;
  1156. break;
  1157. case TIFF_SLONG:
  1158. *va_arg(ap, int32*) =
  1159. *(int32 *)val;
  1160. ret_val = 1;
  1161. break;
  1162. case TIFF_LONG8:
  1163. case TIFF_IFD8:
  1164. *va_arg(ap, uint64*) =
  1165. *(uint64 *)val;
  1166. ret_val = 1;
  1167. break;
  1168. case TIFF_SLONG8:
  1169. *va_arg(ap, int64*) =
  1170. *(int64 *)val;
  1171. ret_val = 1;
  1172. break;
  1173. case TIFF_RATIONAL:
  1174. case TIFF_SRATIONAL:
  1175. {
  1176. /*-- Rational2Double: For Rationals evaluate "set_field_type" to determine internal storage size and return value size. */
  1177. int tv_size = _TIFFSetGetFieldSize(fip->set_field_type);
  1178. if (tv_size == 8) {
  1179. *va_arg(ap, double*) = *(double *)val;
  1180. ret_val = 1;
  1181. } else {
  1182. /*-- default should be tv_size == 4 */
  1183. *va_arg(ap, float*) = *(float *)val;
  1184. ret_val = 1;
  1185. /*-- ToDo: After Testing, this should be removed and tv_size==4 should be set as default. */
  1186. if (tv_size != 4) {
  1187. TIFFErrorExt(0,"TIFFLib: _TIFFVGetField()", "Rational2Double: .set_field_type in not 4 but %d", tv_size);
  1188. }
  1189. }
  1190. }
  1191. break;
  1192. case TIFF_FLOAT:
  1193. *va_arg(ap, float*) =
  1194. *(float *)val;
  1195. ret_val = 1;
  1196. break;
  1197. case TIFF_DOUBLE:
  1198. *va_arg(ap, double*) =
  1199. *(double *)val;
  1200. ret_val = 1;
  1201. break;
  1202. default:
  1203. ret_val = 0;
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. break;
  1209. }
  1210. }
  1211. }
  1212. return(ret_val);
  1213. }
  1214. /*
  1215. * Return the value of a field in the
  1216. * internal directory structure.
  1217. */
  1218. int
  1219. TIFFGetField(TIFF* tif, uint32 tag, ...)
  1220. {
  1221. int status;
  1222. va_list ap;
  1223. va_start(ap, tag);
  1224. status = TIFFVGetField(tif, tag, ap);
  1225. va_end(ap);
  1226. return (status);
  1227. }
  1228. /*
  1229. * Like TIFFGetField, but taking a varargs
  1230. * parameter list. This routine is useful
  1231. * for building higher-level interfaces on
  1232. * top of the library.
  1233. */
  1234. int
  1235. TIFFVGetField(TIFF* tif, uint32 tag, va_list ap)
  1236. {
  1237. const TIFFField* fip = TIFFFindField(tif, tag, TIFF_ANY);
  1238. return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
  1239. (*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
  1240. }
  1241. #define CleanupField(member) { \
  1242. if (td->member) { \
  1243. _TIFFfree(td->member); \
  1244. td->member = 0; \
  1245. } \
  1246. }
  1247. /*
  1248. * Release storage associated with a directory.
  1249. */
  1250. void
  1251. TIFFFreeDirectory(TIFF* tif)
  1252. {
  1253. TIFFDirectory *td = &tif->tif_dir;
  1254. int i;
  1255. _TIFFmemset(td->td_fieldsset, 0, FIELD_SETLONGS);
  1256. CleanupField(td_sminsamplevalue);
  1257. CleanupField(td_smaxsamplevalue);
  1258. CleanupField(td_colormap[0]);
  1259. CleanupField(td_colormap[1]);
  1260. CleanupField(td_colormap[2]);
  1261. CleanupField(td_sampleinfo);
  1262. CleanupField(td_subifd);
  1263. CleanupField(td_inknames);
  1264. CleanupField(td_refblackwhite);
  1265. CleanupField(td_transferfunction[0]);
  1266. CleanupField(td_transferfunction[1]);
  1267. CleanupField(td_transferfunction[2]);
  1268. CleanupField(td_stripoffset_p);
  1269. CleanupField(td_stripbytecount_p);
  1270. td->td_stripoffsetbyteallocsize = 0;
  1271. TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
  1272. TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
  1273. /* Cleanup custom tag values */
  1274. for( i = 0; i < td->td_customValueCount; i++ ) {
  1275. if (td->td_customValues[i].value)
  1276. _TIFFfree(td->td_customValues[i].value);
  1277. }
  1278. td->td_customValueCount = 0;
  1279. CleanupField(td_customValues);
  1280. _TIFFmemset( &(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
  1281. _TIFFmemset( &(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
  1282. }
  1283. #undef CleanupField
  1284. /*
  1285. * Client Tag extension support (from Niles Ritter).
  1286. */
  1287. static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
  1288. TIFFExtendProc
  1289. TIFFSetTagExtender(TIFFExtendProc extender)
  1290. {
  1291. TIFFExtendProc prev = _TIFFextender;
  1292. _TIFFextender = extender;
  1293. return (prev);
  1294. }
  1295. /*
  1296. * Setup for a new directory. Should we automatically call
  1297. * TIFFWriteDirectory() if the current one is dirty?
  1298. *
  1299. * The newly created directory will not exist on the file till
  1300. * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
  1301. */
  1302. int
  1303. TIFFCreateDirectory(TIFF* tif)
  1304. {
  1305. TIFFDefaultDirectory(tif);
  1306. tif->tif_diroff = 0;
  1307. tif->tif_nextdiroff = 0;
  1308. tif->tif_curoff = 0;
  1309. tif->tif_row = (uint32) -1;
  1310. tif->tif_curstrip = (uint32) -1;
  1311. return 0;
  1312. }
  1313. int
  1314. TIFFCreateCustomDirectory(TIFF* tif, const TIFFFieldArray* infoarray)
  1315. {
  1316. TIFFDefaultDirectory(tif);
  1317. /*
  1318. * Reset the field definitions to match the application provided list.
  1319. * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
  1320. * based on it's assumption this is an image directory.
  1321. */
  1322. _TIFFSetupFields(tif, infoarray);
  1323. tif->tif_diroff = 0;
  1324. tif->tif_nextdiroff = 0;
  1325. tif->tif_curoff = 0;
  1326. tif->tif_row = (uint32) -1;
  1327. tif->tif_curstrip = (uint32) -1;
  1328. return 0;
  1329. }
  1330. int
  1331. TIFFCreateEXIFDirectory(TIFF* tif)
  1332. {
  1333. const TIFFFieldArray* exifFieldArray;
  1334. exifFieldArray = _TIFFGetExifFields();
  1335. return TIFFCreateCustomDirectory(tif, exifFieldArray);
  1336. }
  1337. /*
  1338. * Creates the EXIF GPS custom directory
  1339. */
  1340. int
  1341. TIFFCreateGPSDirectory(TIFF* tif)
  1342. {
  1343. const TIFFFieldArray* gpsFieldArray;
  1344. gpsFieldArray = _TIFFGetGpsFields();
  1345. return TIFFCreateCustomDirectory(tif, gpsFieldArray);
  1346. }
  1347. /*
  1348. * Setup a default directory structure.
  1349. */
  1350. int
  1351. TIFFDefaultDirectory(TIFF* tif)
  1352. {
  1353. register TIFFDirectory* td = &tif->tif_dir;
  1354. const TIFFFieldArray* tiffFieldArray;
  1355. tiffFieldArray = _TIFFGetFields();
  1356. _TIFFSetupFields(tif, tiffFieldArray);
  1357. _TIFFmemset(td, 0, sizeof (*td));
  1358. td->td_fillorder = FILLORDER_MSB2LSB;
  1359. td->td_bitspersample = 1;
  1360. td->td_threshholding = THRESHHOLD_BILEVEL;
  1361. td->td_orientation = ORIENTATION_TOPLEFT;
  1362. td->td_samplesperpixel = 1;
  1363. td->td_rowsperstrip = (uint32) -1;
  1364. td->td_tilewidth = 0;
  1365. td->td_tilelength = 0;
  1366. td->td_tiledepth = 1;
  1367. #ifdef STRIPBYTECOUNTSORTED_UNUSED
  1368. td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
  1369. #endif
  1370. td->td_resolutionunit = RESUNIT_INCH;
  1371. td->td_sampleformat = SAMPLEFORMAT_UINT;
  1372. td->td_imagedepth = 1;
  1373. td->td_ycbcrsubsampling[0] = 2;
  1374. td->td_ycbcrsubsampling[1] = 2;
  1375. td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
  1376. tif->tif_postdecode = _TIFFNoPostDecode;
  1377. tif->tif_foundfield = NULL;
  1378. tif->tif_tagmethods.vsetfield = _TIFFVSetField;
  1379. tif->tif_tagmethods.vgetfield = _TIFFVGetField;
  1380. tif->tif_tagmethods.printdir = NULL;
  1381. /*
  1382. * Give client code a chance to install their own
  1383. * tag extensions & methods, prior to compression overloads,
  1384. * but do some prior cleanup first. (http://trac.osgeo.org/gdal/ticket/5054)
  1385. */
  1386. if (tif->tif_nfieldscompat > 0) {
  1387. uint32 i;
  1388. for (i = 0; i < tif->tif_nfieldscompat; i++) {
  1389. if (tif->tif_fieldscompat[i].allocated_size)
  1390. _TIFFfree(tif->tif_fieldscompat[i].fields);
  1391. }
  1392. _TIFFfree(tif->tif_fieldscompat);
  1393. tif->tif_nfieldscompat = 0;
  1394. tif->tif_fieldscompat = NULL;
  1395. }
  1396. if (_TIFFextender)
  1397. (*_TIFFextender)(tif);
  1398. (void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  1399. /*
  1400. * NB: The directory is marked dirty as a result of setting
  1401. * up the default compression scheme. However, this really
  1402. * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
  1403. * if the user does something. We could just do the setup
  1404. * by hand, but it seems better to use the normal mechanism
  1405. * (i.e. TIFFSetField).
  1406. */
  1407. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  1408. /*
  1409. * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
  1410. * we clear the ISTILED flag when setting up a new directory.
  1411. * Should we also be clearing stuff like INSUBIFD?
  1412. */
  1413. tif->tif_flags &= ~TIFF_ISTILED;
  1414. return (1);
  1415. }
  1416. static int
  1417. TIFFAdvanceDirectory(TIFF* tif, uint64* nextdir, uint64* off)
  1418. {
  1419. static const char module[] = "TIFFAdvanceDirectory";
  1420. if (isMapped(tif))
  1421. {
  1422. uint64 poff=*nextdir;
  1423. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1424. {
  1425. tmsize_t poffa,poffb,poffc,poffd;
  1426. uint16 dircount;
  1427. uint32 nextdir32;
  1428. poffa=(tmsize_t)poff;
  1429. poffb=poffa+sizeof(uint16);
  1430. if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint16))||(poffb>tif->tif_size))
  1431. {
  1432. TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
  1433. *nextdir=0;
  1434. return(0);
  1435. }
  1436. _TIFFmemcpy(&dircount,tif->tif_base+poffa,sizeof(uint16));
  1437. if (tif->tif_flags&TIFF_SWAB)
  1438. TIFFSwabShort(&dircount);
  1439. poffc=poffb+dircount*12;
  1440. poffd=poffc+sizeof(uint32);
  1441. if ((poffc<poffb)||(poffc<dircount*12)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint32))||(poffd>tif->tif_size))
  1442. {
  1443. TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
  1444. return(0);
  1445. }
  1446. if (off!=NULL)
  1447. *off=(uint64)poffc;
  1448. _TIFFmemcpy(&nextdir32,tif->tif_base+poffc,sizeof(uint32));
  1449. if (tif->tif_flags&TIFF_SWAB)
  1450. TIFFSwabLong(&nextdir32);
  1451. *nextdir=nextdir32;
  1452. }
  1453. else
  1454. {
  1455. tmsize_t poffa,poffb,poffc,poffd;
  1456. uint64 dircount64;
  1457. uint16 dircount16;
  1458. poffa=(tmsize_t)poff;
  1459. poffb=poffa+sizeof(uint64);
  1460. if (((uint64)poffa!=poff)||(poffb<poffa)||(poffb<(tmsize_t)sizeof(uint64))||(poffb>tif->tif_size))
  1461. {
  1462. TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory count");
  1463. return(0);
  1464. }
  1465. _TIFFmemcpy(&dircount64,tif->tif_base+poffa,sizeof(uint64));
  1466. if (tif->tif_flags&TIFF_SWAB)
  1467. TIFFSwabLong8(&dircount64);
  1468. if (dircount64>0xFFFF)
  1469. {
  1470. TIFFErrorExt(tif->tif_clientdata,module,"Sanity check on directory count failed");
  1471. return(0);
  1472. }
  1473. dircount16=(uint16)dircount64;
  1474. poffc=poffb+dircount16*20;
  1475. poffd=poffc+sizeof(uint64);
  1476. if ((poffc<poffb)||(poffc<dircount16*20)||(poffd<poffc)||(poffd<(tmsize_t)sizeof(uint64))||(poffd>tif->tif_size))
  1477. {
  1478. TIFFErrorExt(tif->tif_clientdata,module,"Error fetching directory link");
  1479. return(0);
  1480. }
  1481. if (off!=NULL)
  1482. *off=(uint64)poffc;
  1483. _TIFFmemcpy(nextdir,tif->tif_base+poffc,sizeof(uint64));
  1484. if (tif->tif_flags&TIFF_SWAB)
  1485. TIFFSwabLong8(nextdir);
  1486. }
  1487. return(1);
  1488. }
  1489. else
  1490. {
  1491. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1492. {
  1493. uint16 dircount;
  1494. uint32 nextdir32;
  1495. if (!SeekOK(tif, *nextdir) ||
  1496. !ReadOK(tif, &dircount, sizeof (uint16))) {
  1497. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
  1498. tif->tif_name);
  1499. return (0);
  1500. }
  1501. if (tif->tif_flags & TIFF_SWAB)
  1502. TIFFSwabShort(&dircount);
  1503. if (off != NULL)
  1504. *off = TIFFSeekFile(tif,
  1505. dircount*12, SEEK_CUR);
  1506. else
  1507. (void) TIFFSeekFile(tif,
  1508. dircount*12, SEEK_CUR);
  1509. if (!ReadOK(tif, &nextdir32, sizeof (uint32))) {
  1510. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory link",
  1511. tif->tif_name);
  1512. return (0);
  1513. }
  1514. if (tif->tif_flags & TIFF_SWAB)
  1515. TIFFSwabLong(&nextdir32);
  1516. *nextdir=nextdir32;
  1517. }
  1518. else
  1519. {
  1520. uint64 dircount64;
  1521. uint16 dircount16;
  1522. if (!SeekOK(tif, *nextdir) ||
  1523. !ReadOK(tif, &dircount64, sizeof (uint64))) {
  1524. TIFFErrorExt(tif->tif_clientdata, module, "%s: Error fetching directory count",
  1525. tif->tif_name);
  1526. return (0);
  1527. }
  1528. if (tif->tif_flags & TIFF_SWAB)
  1529. TIFFSwabLong8(&dircount64);
  1530. if (dircount64>0xFFFF)
  1531. {
  1532. TIFFErrorExt(tif->tif_clientdata, module, "Error fetching directory count");
  1533. return(0);
  1534. }
  1535. dircount16 = (uint16)dircount64;
  1536. if (off != NULL)
  1537. *off = TIFFSeekFile(tif,
  1538. dircount16*20, SEEK_CUR);
  1539. else
  1540. (void) TIFFSeekFile(tif,
  1541. dircount16*20, SEEK_CUR);
  1542. if (!ReadOK(tif, nextdir, sizeof (uint64))) {
  1543. TIFFErrorExt(tif->tif_clientdata, module,
  1544. "%s: Error fetching directory link",
  1545. tif->tif_name);
  1546. return (0);
  1547. }
  1548. if (tif->tif_flags & TIFF_SWAB)
  1549. TIFFSwabLong8(nextdir);
  1550. }
  1551. return (1);
  1552. }
  1553. }
  1554. /*
  1555. * Count the number of directories in a file.
  1556. */
  1557. uint16
  1558. TIFFNumberOfDirectories(TIFF* tif)
  1559. {
  1560. static const char module[] = "TIFFNumberOfDirectories";
  1561. uint64 nextdir;
  1562. uint16 n;
  1563. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1564. nextdir = tif->tif_header.classic.tiff_diroff;
  1565. else
  1566. nextdir = tif->tif_header.big.tiff_diroff;
  1567. n = 0;
  1568. while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1569. {
  1570. if (n != 65535) {
  1571. ++n;
  1572. }
  1573. else
  1574. {
  1575. TIFFErrorExt(tif->tif_clientdata, module,
  1576. "Directory count exceeded 65535 limit,"
  1577. " giving up on counting.");
  1578. return (65535);
  1579. }
  1580. }
  1581. return (n);
  1582. }
  1583. /*
  1584. * Set the n-th directory as the current directory.
  1585. * NB: Directories are numbered starting at 0.
  1586. */
  1587. int
  1588. TIFFSetDirectory(TIFF* tif, uint16 dirn)
  1589. {
  1590. uint64 nextdir;
  1591. uint16 n;
  1592. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1593. nextdir = tif->tif_header.classic.tiff_diroff;
  1594. else
  1595. nextdir = tif->tif_header.big.tiff_diroff;
  1596. for (n = dirn; n > 0 && nextdir != 0; n--)
  1597. if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1598. return (0);
  1599. tif->tif_nextdiroff = nextdir;
  1600. /*
  1601. * Set curdir to the actual directory index. The
  1602. * -1 is because TIFFReadDirectory will increment
  1603. * tif_curdir after successfully reading the directory.
  1604. */
  1605. tif->tif_curdir = (dirn - n) - 1;
  1606. /*
  1607. * Reset tif_dirnumber counter and start new list of seen directories.
  1608. * We need this to prevent IFD loops.
  1609. */
  1610. tif->tif_dirnumber = 0;
  1611. return (TIFFReadDirectory(tif));
  1612. }
  1613. /*
  1614. * Set the current directory to be the directory
  1615. * located at the specified file offset. This interface
  1616. * is used mainly to access directories linked with
  1617. * the SubIFD tag (e.g. thumbnail images).
  1618. */
  1619. int
  1620. TIFFSetSubDirectory(TIFF* tif, uint64 diroff)
  1621. {
  1622. tif->tif_nextdiroff = diroff;
  1623. /*
  1624. * Reset tif_dirnumber counter and start new list of seen directories.
  1625. * We need this to prevent IFD loops.
  1626. */
  1627. tif->tif_dirnumber = 0;
  1628. return (TIFFReadDirectory(tif));
  1629. }
  1630. /*
  1631. * Return file offset of the current directory.
  1632. */
  1633. uint64
  1634. TIFFCurrentDirOffset(TIFF* tif)
  1635. {
  1636. return (tif->tif_diroff);
  1637. }
  1638. /*
  1639. * Return an indication of whether or not we are
  1640. * at the last directory in the file.
  1641. */
  1642. int
  1643. TIFFLastDirectory(TIFF* tif)
  1644. {
  1645. return (tif->tif_nextdiroff == 0);
  1646. }
  1647. /*
  1648. * Unlink the specified directory from the directory chain.
  1649. */
  1650. int
  1651. TIFFUnlinkDirectory(TIFF* tif, uint16 dirn)
  1652. {
  1653. static const char module[] = "TIFFUnlinkDirectory";
  1654. uint64 nextdir;
  1655. uint64 off;
  1656. uint16 n;
  1657. if (tif->tif_mode == O_RDONLY) {
  1658. TIFFErrorExt(tif->tif_clientdata, module,
  1659. "Can not unlink directory in read-only file");
  1660. return (0);
  1661. }
  1662. /*
  1663. * Go to the directory before the one we want
  1664. * to unlink and nab the offset of the link
  1665. * field we'll need to patch.
  1666. */
  1667. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1668. {
  1669. nextdir = tif->tif_header.classic.tiff_diroff;
  1670. off = 4;
  1671. }
  1672. else
  1673. {
  1674. nextdir = tif->tif_header.big.tiff_diroff;
  1675. off = 8;
  1676. }
  1677. for (n = dirn-1; n > 0; n--) {
  1678. if (nextdir == 0) {
  1679. TIFFErrorExt(tif->tif_clientdata, module, "Directory %d does not exist", dirn);
  1680. return (0);
  1681. }
  1682. if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
  1683. return (0);
  1684. }
  1685. /*
  1686. * Advance to the directory to be unlinked and fetch
  1687. * the offset of the directory that follows.
  1688. */
  1689. if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
  1690. return (0);
  1691. /*
  1692. * Go back and patch the link field of the preceding
  1693. * directory to point to the offset of the directory
  1694. * that follows.
  1695. */
  1696. (void) TIFFSeekFile(tif, off, SEEK_SET);
  1697. if (!(tif->tif_flags&TIFF_BIGTIFF))
  1698. {
  1699. uint32 nextdir32;
  1700. nextdir32=(uint32)nextdir;
  1701. assert((uint64)nextdir32==nextdir);
  1702. if (tif->tif_flags & TIFF_SWAB)
  1703. TIFFSwabLong(&nextdir32);
  1704. if (!WriteOK(tif, &nextdir32, sizeof (uint32))) {
  1705. TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
  1706. return (0);
  1707. }
  1708. }
  1709. else
  1710. {
  1711. if (tif->tif_flags & TIFF_SWAB)
  1712. TIFFSwabLong8(&nextdir);
  1713. if (!WriteOK(tif, &nextdir, sizeof (uint64))) {
  1714. TIFFErrorExt(tif->tif_clientdata, module, "Error writing directory link");
  1715. return (0);
  1716. }
  1717. }
  1718. /*
  1719. * Leave directory state setup safely. We don't have
  1720. * facilities for doing inserting and removing directories,
  1721. * so it's safest to just invalidate everything. This
  1722. * means that the caller can only append to the directory
  1723. * chain.
  1724. */
  1725. (*tif->tif_cleanup)(tif);
  1726. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  1727. _TIFFfree(tif->tif_rawdata);
  1728. tif->tif_rawdata = NULL;
  1729. tif->tif_rawcc = 0;
  1730. tif->tif_rawdataoff = 0;
  1731. tif->tif_rawdataloaded = 0;
  1732. }
  1733. tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE|TIFF_BUF4WRITE);
  1734. TIFFFreeDirectory(tif);
  1735. TIFFDefaultDirectory(tif);
  1736. tif->tif_diroff = 0; /* force link on next write */
  1737. tif->tif_nextdiroff = 0; /* next write must be at end */
  1738. tif->tif_curoff = 0;
  1739. tif->tif_row = (uint32) -1;
  1740. tif->tif_curstrip = (uint32) -1;
  1741. return (1);
  1742. }
  1743. /* vim: set ts=8 sts=8 sw=8 noet: */
  1744. /*
  1745. * Local Variables:
  1746. * mode: c
  1747. * c-basic-offset: 8
  1748. * fill-column: 78
  1749. * End:
  1750. */