tif_dir.c 49 KB

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