tif_dir.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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. #include <limits.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 setByteArray(TIFF *tif, void **vpp, const void *vp, size_t nmemb,
  41. size_t elem_size)
  42. {
  43. if (*vpp)
  44. {
  45. _TIFFfreeExt(tif, *vpp);
  46. *vpp = 0;
  47. }
  48. if (vp)
  49. {
  50. tmsize_t bytes = _TIFFMultiplySSize(NULL, nmemb, elem_size, NULL);
  51. if (bytes)
  52. *vpp = (void *)_TIFFmallocExt(tif, bytes);
  53. if (*vpp)
  54. _TIFFmemcpy(*vpp, vp, bytes);
  55. }
  56. }
  57. void _TIFFsetByteArray(void **vpp, const void *vp, uint32_t n)
  58. {
  59. setByteArray(NULL, vpp, vp, n, 1);
  60. }
  61. void _TIFFsetByteArrayExt(TIFF *tif, void **vpp, const void *vp, uint32_t n)
  62. {
  63. setByteArray(tif, vpp, vp, n, 1);
  64. }
  65. static void _TIFFsetNString(TIFF *tif, char **cpp, const char *cp, uint32_t n)
  66. {
  67. setByteArray(tif, (void **)cpp, cp, n, 1);
  68. }
  69. void _TIFFsetShortArray(uint16_t **wpp, const uint16_t *wp, uint32_t n)
  70. {
  71. setByteArray(NULL, (void **)wpp, wp, n, sizeof(uint16_t));
  72. }
  73. void _TIFFsetShortArrayExt(TIFF *tif, uint16_t **wpp, const uint16_t *wp,
  74. uint32_t n)
  75. {
  76. setByteArray(tif, (void **)wpp, wp, n, sizeof(uint16_t));
  77. }
  78. void _TIFFsetLongArray(uint32_t **lpp, const uint32_t *lp, uint32_t n)
  79. {
  80. setByteArray(NULL, (void **)lpp, lp, n, sizeof(uint32_t));
  81. }
  82. void _TIFFsetLongArrayExt(TIFF *tif, uint32_t **lpp, const uint32_t *lp,
  83. uint32_t n)
  84. {
  85. setByteArray(tif, (void **)lpp, lp, n, sizeof(uint32_t));
  86. }
  87. static void _TIFFsetLong8Array(TIFF *tif, uint64_t **lpp, const uint64_t *lp,
  88. uint32_t n)
  89. {
  90. setByteArray(tif, (void **)lpp, lp, n, sizeof(uint64_t));
  91. }
  92. void _TIFFsetFloatArray(float **fpp, const float *fp, uint32_t n)
  93. {
  94. setByteArray(NULL, (void **)fpp, fp, n, sizeof(float));
  95. }
  96. void _TIFFsetFloatArrayExt(TIFF *tif, float **fpp, const float *fp, uint32_t n)
  97. {
  98. setByteArray(tif, (void **)fpp, fp, n, sizeof(float));
  99. }
  100. void _TIFFsetDoubleArray(double **dpp, const double *dp, uint32_t n)
  101. {
  102. setByteArray(NULL, (void **)dpp, dp, n, sizeof(double));
  103. }
  104. void _TIFFsetDoubleArrayExt(TIFF *tif, double **dpp, const double *dp,
  105. uint32_t n)
  106. {
  107. setByteArray(tif, (void **)dpp, dp, n, sizeof(double));
  108. }
  109. static void setDoubleArrayOneValue(TIFF *tif, double **vpp, double value,
  110. size_t nmemb)
  111. {
  112. if (*vpp)
  113. _TIFFfreeExt(tif, *vpp);
  114. *vpp = _TIFFmallocExt(tif, nmemb * sizeof(double));
  115. if (*vpp)
  116. {
  117. while (nmemb--)
  118. ((double *)*vpp)[nmemb] = value;
  119. }
  120. }
  121. /*
  122. * Install extra samples information.
  123. */
  124. static int setExtraSamples(TIFF *tif, va_list ap, uint32_t *v)
  125. {
  126. /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
  127. #define EXTRASAMPLE_COREL_UNASSALPHA 999
  128. uint16_t *va;
  129. uint32_t i;
  130. TIFFDirectory *td = &tif->tif_dir;
  131. static const char module[] = "setExtraSamples";
  132. *v = (uint16_t)va_arg(ap, uint16_vap);
  133. if ((uint16_t)*v > td->td_samplesperpixel)
  134. return 0;
  135. va = va_arg(ap, uint16_t *);
  136. if (*v > 0 && va == NULL) /* typically missing param */
  137. return 0;
  138. for (i = 0; i < *v; i++)
  139. {
  140. if (va[i] > EXTRASAMPLE_UNASSALPHA)
  141. {
  142. /*
  143. * XXX: Corel Draw is known to produce incorrect
  144. * ExtraSamples tags which must be patched here if we
  145. * want to be able to open some of the damaged TIFF
  146. * files:
  147. */
  148. if (va[i] == EXTRASAMPLE_COREL_UNASSALPHA)
  149. va[i] = EXTRASAMPLE_UNASSALPHA;
  150. else
  151. return 0;
  152. }
  153. }
  154. if (td->td_transferfunction[0] != NULL &&
  155. (td->td_samplesperpixel - *v > 1) &&
  156. !(td->td_samplesperpixel - td->td_extrasamples > 1))
  157. {
  158. TIFFWarningExtR(tif, module,
  159. "ExtraSamples tag value is changing, "
  160. "but TransferFunction was read with a different value. "
  161. "Canceling it");
  162. TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION);
  163. _TIFFfreeExt(tif, td->td_transferfunction[0]);
  164. td->td_transferfunction[0] = NULL;
  165. }
  166. td->td_extrasamples = (uint16_t)*v;
  167. _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, va, td->td_extrasamples);
  168. return 1;
  169. #undef EXTRASAMPLE_COREL_UNASSALPHA
  170. }
  171. /*
  172. * Count ink names separated by \0. Returns
  173. * zero if the ink names are not as expected.
  174. */
  175. static uint16_t countInkNamesString(TIFF *tif, uint32_t slen, const char *s)
  176. {
  177. uint16_t i = 0;
  178. if (slen > 0)
  179. {
  180. const char *ep = s + slen;
  181. const char *cp = s;
  182. do
  183. {
  184. for (; cp < ep && *cp != '\0'; cp++)
  185. {
  186. }
  187. if (cp >= ep)
  188. goto bad;
  189. cp++; /* skip \0 */
  190. i++;
  191. } while (cp < ep);
  192. return (i);
  193. }
  194. bad:
  195. TIFFErrorExtR(tif, "TIFFSetField",
  196. "%s: Invalid InkNames value; no NUL at given buffer end "
  197. "location %" PRIu32 ", after %" PRIu16 " ink",
  198. tif->tif_name, slen, i);
  199. return (0);
  200. }
  201. static int _TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
  202. {
  203. static const char module[] = "_TIFFVSetField";
  204. TIFFDirectory *td = &tif->tif_dir;
  205. int status = 1;
  206. uint32_t v32, v;
  207. double dblval;
  208. char *s;
  209. const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
  210. uint32_t standard_tag = tag;
  211. if (fip == NULL) /* cannot happen since OkToChangeTag() already checks it */
  212. return 0;
  213. /*
  214. * We want to force the custom code to be used for custom
  215. * fields even if the tag happens to match a well known
  216. * one - important for reinterpreted handling of standard
  217. * tag values in custom directories (i.e. EXIF)
  218. */
  219. if (fip->field_bit == FIELD_CUSTOM)
  220. {
  221. standard_tag = 0;
  222. }
  223. switch (standard_tag)
  224. {
  225. case TIFFTAG_SUBFILETYPE:
  226. td->td_subfiletype = (uint32_t)va_arg(ap, uint32_t);
  227. break;
  228. case TIFFTAG_IMAGEWIDTH:
  229. td->td_imagewidth = (uint32_t)va_arg(ap, uint32_t);
  230. break;
  231. case TIFFTAG_IMAGELENGTH:
  232. td->td_imagelength = (uint32_t)va_arg(ap, uint32_t);
  233. break;
  234. case TIFFTAG_BITSPERSAMPLE:
  235. td->td_bitspersample = (uint16_t)va_arg(ap, uint16_vap);
  236. /*
  237. * If the data require post-decoding processing to byte-swap
  238. * samples, set it up here. Note that since tags are required
  239. * to be ordered, compression code can override this behavior
  240. * in the setup method if it wants to roll the post decoding
  241. * work in with its normal work.
  242. */
  243. if (tif->tif_flags & TIFF_SWAB)
  244. {
  245. if (td->td_bitspersample == 8)
  246. tif->tif_postdecode = _TIFFNoPostDecode;
  247. else if (td->td_bitspersample == 16)
  248. tif->tif_postdecode = _TIFFSwab16BitData;
  249. else if (td->td_bitspersample == 24)
  250. tif->tif_postdecode = _TIFFSwab24BitData;
  251. else if (td->td_bitspersample == 32)
  252. tif->tif_postdecode = _TIFFSwab32BitData;
  253. else if (td->td_bitspersample == 64)
  254. tif->tif_postdecode = _TIFFSwab64BitData;
  255. else if (td->td_bitspersample == 128) /* two 64's */
  256. tif->tif_postdecode = _TIFFSwab64BitData;
  257. }
  258. break;
  259. case TIFFTAG_COMPRESSION:
  260. v = (uint16_t)va_arg(ap, uint16_vap);
  261. /*
  262. * If we're changing the compression scheme, notify the
  263. * previous module so that it can cleanup any state it's
  264. * setup.
  265. */
  266. if (TIFFFieldSet(tif, FIELD_COMPRESSION))
  267. {
  268. if ((uint32_t)td->td_compression == v)
  269. break;
  270. (*tif->tif_cleanup)(tif);
  271. tif->tif_flags &= ~TIFF_CODERSETUP;
  272. }
  273. /*
  274. * Setup new compression routine state.
  275. */
  276. if ((status = TIFFSetCompressionScheme(tif, v)) != 0)
  277. td->td_compression = (uint16_t)v;
  278. else
  279. status = 0;
  280. break;
  281. case TIFFTAG_PHOTOMETRIC:
  282. td->td_photometric = (uint16_t)va_arg(ap, uint16_vap);
  283. break;
  284. case TIFFTAG_THRESHHOLDING:
  285. td->td_threshholding = (uint16_t)va_arg(ap, uint16_vap);
  286. break;
  287. case TIFFTAG_FILLORDER:
  288. v = (uint16_t)va_arg(ap, uint16_vap);
  289. if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
  290. goto badvalue;
  291. td->td_fillorder = (uint16_t)v;
  292. break;
  293. case TIFFTAG_ORIENTATION:
  294. v = (uint16_t)va_arg(ap, uint16_vap);
  295. if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v)
  296. goto badvalue;
  297. else
  298. td->td_orientation = (uint16_t)v;
  299. break;
  300. case TIFFTAG_SAMPLESPERPIXEL:
  301. v = (uint16_t)va_arg(ap, uint16_vap);
  302. if (v == 0)
  303. goto badvalue;
  304. if (v != td->td_samplesperpixel)
  305. {
  306. /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
  307. if (td->td_sminsamplevalue != NULL)
  308. {
  309. TIFFWarningExtR(tif, module,
  310. "SamplesPerPixel tag value is changing, "
  311. "but SMinSampleValue tag was read with a "
  312. "different value. Canceling it");
  313. TIFFClrFieldBit(tif, FIELD_SMINSAMPLEVALUE);
  314. _TIFFfreeExt(tif, td->td_sminsamplevalue);
  315. td->td_sminsamplevalue = NULL;
  316. }
  317. if (td->td_smaxsamplevalue != NULL)
  318. {
  319. TIFFWarningExtR(tif, module,
  320. "SamplesPerPixel tag value is changing, "
  321. "but SMaxSampleValue tag was read with a "
  322. "different value. Canceling it");
  323. TIFFClrFieldBit(tif, FIELD_SMAXSAMPLEVALUE);
  324. _TIFFfreeExt(tif, td->td_smaxsamplevalue);
  325. td->td_smaxsamplevalue = NULL;
  326. }
  327. /* Test if 3 transfer functions instead of just one are now
  328. needed See http://bugzilla.maptools.org/show_bug.cgi?id=2820
  329. */
  330. if (td->td_transferfunction[0] != NULL &&
  331. (v - td->td_extrasamples > 1) &&
  332. !(td->td_samplesperpixel - td->td_extrasamples > 1))
  333. {
  334. TIFFWarningExtR(tif, module,
  335. "SamplesPerPixel tag value is changing, "
  336. "but TransferFunction was read with a "
  337. "different value. Canceling it");
  338. TIFFClrFieldBit(tif, FIELD_TRANSFERFUNCTION);
  339. _TIFFfreeExt(tif, td->td_transferfunction[0]);
  340. td->td_transferfunction[0] = NULL;
  341. }
  342. }
  343. td->td_samplesperpixel = (uint16_t)v;
  344. break;
  345. case TIFFTAG_ROWSPERSTRIP:
  346. v32 = (uint32_t)va_arg(ap, uint32_t);
  347. if (v32 == 0)
  348. goto badvalue32;
  349. td->td_rowsperstrip = v32;
  350. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS))
  351. {
  352. td->td_tilelength = v32;
  353. td->td_tilewidth = td->td_imagewidth;
  354. }
  355. break;
  356. case TIFFTAG_MINSAMPLEVALUE:
  357. td->td_minsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
  358. break;
  359. case TIFFTAG_MAXSAMPLEVALUE:
  360. td->td_maxsamplevalue = (uint16_t)va_arg(ap, uint16_vap);
  361. break;
  362. case TIFFTAG_SMINSAMPLEVALUE:
  363. if (tif->tif_flags & TIFF_PERSAMPLE)
  364. _TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue,
  365. va_arg(ap, double *),
  366. td->td_samplesperpixel);
  367. else
  368. setDoubleArrayOneValue(tif, &td->td_sminsamplevalue,
  369. va_arg(ap, double),
  370. td->td_samplesperpixel);
  371. break;
  372. case TIFFTAG_SMAXSAMPLEVALUE:
  373. if (tif->tif_flags & TIFF_PERSAMPLE)
  374. _TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue,
  375. va_arg(ap, double *),
  376. td->td_samplesperpixel);
  377. else
  378. setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue,
  379. va_arg(ap, double),
  380. td->td_samplesperpixel);
  381. break;
  382. case TIFFTAG_XRESOLUTION:
  383. dblval = va_arg(ap, double);
  384. if (dblval != dblval || dblval < 0)
  385. goto badvaluedouble;
  386. td->td_xresolution = _TIFFClampDoubleToFloat(dblval);
  387. break;
  388. case TIFFTAG_YRESOLUTION:
  389. dblval = va_arg(ap, double);
  390. if (dblval != dblval || dblval < 0)
  391. goto badvaluedouble;
  392. td->td_yresolution = _TIFFClampDoubleToFloat(dblval);
  393. break;
  394. case TIFFTAG_PLANARCONFIG:
  395. v = (uint16_t)va_arg(ap, uint16_vap);
  396. if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
  397. goto badvalue;
  398. td->td_planarconfig = (uint16_t)v;
  399. break;
  400. case TIFFTAG_XPOSITION:
  401. td->td_xposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
  402. break;
  403. case TIFFTAG_YPOSITION:
  404. td->td_yposition = _TIFFClampDoubleToFloat(va_arg(ap, double));
  405. break;
  406. case TIFFTAG_RESOLUTIONUNIT:
  407. v = (uint16_t)va_arg(ap, uint16_vap);
  408. if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
  409. goto badvalue;
  410. td->td_resolutionunit = (uint16_t)v;
  411. break;
  412. case TIFFTAG_PAGENUMBER:
  413. td->td_pagenumber[0] = (uint16_t)va_arg(ap, uint16_vap);
  414. td->td_pagenumber[1] = (uint16_t)va_arg(ap, uint16_vap);
  415. break;
  416. case TIFFTAG_HALFTONEHINTS:
  417. td->td_halftonehints[0] = (uint16_t)va_arg(ap, uint16_vap);
  418. td->td_halftonehints[1] = (uint16_t)va_arg(ap, uint16_vap);
  419. break;
  420. case TIFFTAG_COLORMAP:
  421. v32 = (uint32_t)(1L << td->td_bitspersample);
  422. _TIFFsetShortArrayExt(tif, &td->td_colormap[0],
  423. va_arg(ap, uint16_t *), v32);
  424. _TIFFsetShortArrayExt(tif, &td->td_colormap[1],
  425. va_arg(ap, uint16_t *), v32);
  426. _TIFFsetShortArrayExt(tif, &td->td_colormap[2],
  427. va_arg(ap, uint16_t *), v32);
  428. break;
  429. case TIFFTAG_EXTRASAMPLES:
  430. if (!setExtraSamples(tif, ap, &v))
  431. goto badvalue;
  432. break;
  433. case TIFFTAG_MATTEING:
  434. td->td_extrasamples = (((uint16_t)va_arg(ap, uint16_vap)) != 0);
  435. if (td->td_extrasamples)
  436. {
  437. uint16_t sv = EXTRASAMPLE_ASSOCALPHA;
  438. _TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1);
  439. }
  440. break;
  441. case TIFFTAG_TILEWIDTH:
  442. v32 = (uint32_t)va_arg(ap, uint32_t);
  443. if (v32 % 16)
  444. {
  445. if (tif->tif_mode != O_RDONLY)
  446. goto badvalue32;
  447. TIFFWarningExtR(
  448. tif, tif->tif_name,
  449. "Nonstandard tile width %" PRIu32 ", convert file", v32);
  450. }
  451. td->td_tilewidth = v32;
  452. tif->tif_flags |= TIFF_ISTILED;
  453. break;
  454. case TIFFTAG_TILELENGTH:
  455. v32 = (uint32_t)va_arg(ap, uint32_t);
  456. if (v32 % 16)
  457. {
  458. if (tif->tif_mode != O_RDONLY)
  459. goto badvalue32;
  460. TIFFWarningExtR(
  461. tif, tif->tif_name,
  462. "Nonstandard tile length %" PRIu32 ", convert file", v32);
  463. }
  464. td->td_tilelength = v32;
  465. tif->tif_flags |= TIFF_ISTILED;
  466. break;
  467. case TIFFTAG_TILEDEPTH:
  468. v32 = (uint32_t)va_arg(ap, uint32_t);
  469. if (v32 == 0)
  470. goto badvalue32;
  471. td->td_tiledepth = v32;
  472. break;
  473. case TIFFTAG_DATATYPE:
  474. v = (uint16_t)va_arg(ap, uint16_vap);
  475. switch (v)
  476. {
  477. case DATATYPE_VOID:
  478. v = SAMPLEFORMAT_VOID;
  479. break;
  480. case DATATYPE_INT:
  481. v = SAMPLEFORMAT_INT;
  482. break;
  483. case DATATYPE_UINT:
  484. v = SAMPLEFORMAT_UINT;
  485. break;
  486. case DATATYPE_IEEEFP:
  487. v = SAMPLEFORMAT_IEEEFP;
  488. break;
  489. default:
  490. goto badvalue;
  491. }
  492. td->td_sampleformat = (uint16_t)v;
  493. break;
  494. case TIFFTAG_SAMPLEFORMAT:
  495. v = (uint16_t)va_arg(ap, uint16_vap);
  496. if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
  497. goto badvalue;
  498. td->td_sampleformat = (uint16_t)v;
  499. /* Try to fix up the SWAB function for complex data. */
  500. if (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT &&
  501. td->td_bitspersample == 32 &&
  502. tif->tif_postdecode == _TIFFSwab32BitData)
  503. tif->tif_postdecode = _TIFFSwab16BitData;
  504. else if ((td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT ||
  505. td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP) &&
  506. td->td_bitspersample == 64 &&
  507. tif->tif_postdecode == _TIFFSwab64BitData)
  508. tif->tif_postdecode = _TIFFSwab32BitData;
  509. break;
  510. case TIFFTAG_IMAGEDEPTH:
  511. td->td_imagedepth = (uint32_t)va_arg(ap, uint32_t);
  512. break;
  513. case TIFFTAG_SUBIFD:
  514. if ((tif->tif_flags & TIFF_INSUBIFD) == 0)
  515. {
  516. td->td_nsubifd = (uint16_t)va_arg(ap, uint16_vap);
  517. _TIFFsetLong8Array(tif, &td->td_subifd,
  518. (uint64_t *)va_arg(ap, uint64_t *),
  519. (uint32_t)td->td_nsubifd);
  520. }
  521. else
  522. {
  523. TIFFErrorExtR(tif, module, "%s: Sorry, cannot nest SubIFDs",
  524. tif->tif_name);
  525. status = 0;
  526. }
  527. break;
  528. case TIFFTAG_YCBCRPOSITIONING:
  529. td->td_ycbcrpositioning = (uint16_t)va_arg(ap, uint16_vap);
  530. break;
  531. case TIFFTAG_YCBCRSUBSAMPLING:
  532. td->td_ycbcrsubsampling[0] = (uint16_t)va_arg(ap, uint16_vap);
  533. td->td_ycbcrsubsampling[1] = (uint16_t)va_arg(ap, uint16_vap);
  534. break;
  535. case TIFFTAG_TRANSFERFUNCTION:
  536. {
  537. uint32_t i;
  538. v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
  539. for (i = 0; i < v; i++)
  540. _TIFFsetShortArrayExt(tif, &td->td_transferfunction[i],
  541. va_arg(ap, uint16_t *),
  542. 1U << td->td_bitspersample);
  543. break;
  544. }
  545. case TIFFTAG_REFERENCEBLACKWHITE:
  546. /* XXX should check for null range */
  547. _TIFFsetFloatArrayExt(tif, &td->td_refblackwhite,
  548. va_arg(ap, float *), 6);
  549. break;
  550. case TIFFTAG_INKNAMES:
  551. {
  552. v = (uint16_t)va_arg(ap, uint16_vap);
  553. s = va_arg(ap, char *);
  554. uint16_t ninksinstring;
  555. ninksinstring = countInkNamesString(tif, v, s);
  556. status = ninksinstring > 0;
  557. if (ninksinstring > 0)
  558. {
  559. _TIFFsetNString(tif, &td->td_inknames, s, v);
  560. td->td_inknameslen = v;
  561. /* Set NumberOfInks to the value ninksinstring */
  562. if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS))
  563. {
  564. if (td->td_numberofinks != ninksinstring)
  565. {
  566. TIFFErrorExtR(
  567. tif, module,
  568. "Warning %s; Tag %s:\n Value %" PRIu16
  569. " of NumberOfInks is different from the number of "
  570. "inks %" PRIu16
  571. ".\n -> NumberOfInks value adapted to %" PRIu16 "",
  572. tif->tif_name, fip->field_name, td->td_numberofinks,
  573. ninksinstring, ninksinstring);
  574. td->td_numberofinks = ninksinstring;
  575. }
  576. }
  577. else
  578. {
  579. td->td_numberofinks = ninksinstring;
  580. TIFFSetFieldBit(tif, FIELD_NUMBEROFINKS);
  581. }
  582. if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
  583. {
  584. if (td->td_numberofinks != td->td_samplesperpixel)
  585. {
  586. TIFFErrorExtR(tif, module,
  587. "Warning %s; Tag %s:\n Value %" PRIu16
  588. " of NumberOfInks is different from the "
  589. "SamplesPerPixel value %" PRIu16 "",
  590. tif->tif_name, fip->field_name,
  591. td->td_numberofinks,
  592. td->td_samplesperpixel);
  593. }
  594. }
  595. }
  596. }
  597. break;
  598. case TIFFTAG_NUMBEROFINKS:
  599. v = (uint16_t)va_arg(ap, uint16_vap);
  600. /* If InkNames already set also NumberOfInks is set accordingly and
  601. * should be equal */
  602. if (TIFFFieldSet(tif, FIELD_INKNAMES))
  603. {
  604. if (v != td->td_numberofinks)
  605. {
  606. TIFFErrorExtR(
  607. tif, module,
  608. "Error %s; Tag %s:\n It is not possible to set the "
  609. "value %" PRIu32
  610. " for NumberOfInks\n which is different from the "
  611. "number of inks in the InkNames tag (%" PRIu16 ")",
  612. tif->tif_name, fip->field_name, v, td->td_numberofinks);
  613. /* Do not set / overwrite number of inks already set by
  614. * InkNames case accordingly. */
  615. status = 0;
  616. }
  617. }
  618. else
  619. {
  620. td->td_numberofinks = (uint16_t)v;
  621. if (TIFFFieldSet(tif, FIELD_SAMPLESPERPIXEL))
  622. {
  623. if (td->td_numberofinks != td->td_samplesperpixel)
  624. {
  625. TIFFErrorExtR(tif, module,
  626. "Warning %s; Tag %s:\n Value %" PRIu32
  627. " of NumberOfInks is different from the "
  628. "SamplesPerPixel value %" PRIu16 "",
  629. tif->tif_name, fip->field_name, v,
  630. td->td_samplesperpixel);
  631. }
  632. }
  633. }
  634. break;
  635. case TIFFTAG_PERSAMPLE:
  636. v = (uint16_t)va_arg(ap, uint16_vap);
  637. if (v == PERSAMPLE_MULTI)
  638. tif->tif_flags |= TIFF_PERSAMPLE;
  639. else
  640. tif->tif_flags &= ~TIFF_PERSAMPLE;
  641. break;
  642. default:
  643. {
  644. TIFFTagValue *tv;
  645. int tv_size, iCustom;
  646. /*
  647. * This can happen if multiple images are open with different
  648. * codecs which have private tags. The global tag information
  649. * table may then have tags that are valid for one file but not
  650. * the other. If the client tries to set a tag that is not valid
  651. * for the image's codec then we'll arrive here. This
  652. * happens, for example, when tiffcp is used to convert between
  653. * compression schemes and codec-specific tags are blindly copied.
  654. *
  655. * This also happens when a FIELD_IGNORE tag is written.
  656. */
  657. if (fip->field_bit == FIELD_IGNORE)
  658. {
  659. TIFFErrorExtR(
  660. tif, module,
  661. "%s: Ignored %stag \"%s\" (not supported by libtiff)",
  662. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
  663. fip->field_name);
  664. status = 0;
  665. break;
  666. }
  667. if (fip->field_bit != FIELD_CUSTOM)
  668. {
  669. TIFFErrorExtR(
  670. tif, module,
  671. "%s: Invalid %stag \"%s\" (not supported by codec)",
  672. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
  673. fip->field_name);
  674. status = 0;
  675. break;
  676. }
  677. /*
  678. * Find the existing entry for this custom value.
  679. */
  680. tv = NULL;
  681. for (iCustom = 0; iCustom < td->td_customValueCount; iCustom++)
  682. {
  683. if (td->td_customValues[iCustom].info->field_tag == tag)
  684. {
  685. tv = td->td_customValues + iCustom;
  686. if (tv->value != NULL)
  687. {
  688. _TIFFfreeExt(tif, tv->value);
  689. tv->value = NULL;
  690. }
  691. break;
  692. }
  693. }
  694. /*
  695. * Grow the custom list if the entry was not found.
  696. */
  697. if (tv == NULL)
  698. {
  699. TIFFTagValue *new_customValues;
  700. td->td_customValueCount++;
  701. new_customValues = (TIFFTagValue *)_TIFFreallocExt(
  702. tif, td->td_customValues,
  703. sizeof(TIFFTagValue) * td->td_customValueCount);
  704. if (!new_customValues)
  705. {
  706. TIFFErrorExtR(tif, module,
  707. "%s: Failed to allocate space for list of "
  708. "custom values",
  709. tif->tif_name);
  710. status = 0;
  711. goto end;
  712. }
  713. td->td_customValues = new_customValues;
  714. tv = td->td_customValues + (td->td_customValueCount - 1);
  715. tv->info = fip;
  716. tv->value = NULL;
  717. tv->count = 0;
  718. }
  719. /*
  720. * Set custom value ... save a copy of the custom tag value.
  721. */
  722. /*--: Rational2Double: For Rationals evaluate "set_field_type" to
  723. * determine internal storage size. */
  724. tv_size = TIFFFieldSetGetSize(fip);
  725. if (tv_size == 0)
  726. {
  727. status = 0;
  728. TIFFErrorExtR(tif, module, "%s: Bad field type %d for \"%s\"",
  729. tif->tif_name, fip->field_type, fip->field_name);
  730. goto end;
  731. }
  732. if (fip->field_type == TIFF_ASCII)
  733. {
  734. uint32_t ma;
  735. const char *mb;
  736. if (fip->field_passcount)
  737. {
  738. assert(fip->field_writecount == TIFF_VARIABLE2);
  739. ma = (uint32_t)va_arg(ap, uint32_t);
  740. mb = (const char *)va_arg(ap, const char *);
  741. }
  742. else
  743. {
  744. mb = (const char *)va_arg(ap, const char *);
  745. size_t len = strlen(mb) + 1;
  746. if (len >= 0x80000000U)
  747. {
  748. status = 0;
  749. TIFFErrorExtR(tif, module,
  750. "%s: Too long string value for \"%s\". "
  751. "Maximum supported is 2147483647 bytes",
  752. tif->tif_name, fip->field_name);
  753. goto end;
  754. }
  755. ma = (uint32_t)len;
  756. }
  757. tv->count = ma;
  758. setByteArray(tif, &tv->value, mb, ma, 1);
  759. }
  760. else
  761. {
  762. if (fip->field_passcount)
  763. {
  764. if (fip->field_writecount == TIFF_VARIABLE2)
  765. tv->count = (uint32_t)va_arg(ap, uint32_t);
  766. else
  767. tv->count = (int)va_arg(ap, int);
  768. }
  769. else if (fip->field_writecount == TIFF_VARIABLE ||
  770. fip->field_writecount == TIFF_VARIABLE2)
  771. tv->count = 1;
  772. else if (fip->field_writecount == TIFF_SPP)
  773. tv->count = td->td_samplesperpixel;
  774. else
  775. tv->count = fip->field_writecount;
  776. if (tv->count == 0)
  777. {
  778. status = 0;
  779. TIFFWarningExtR(tif, module,
  780. "%s: Null count for \"%s\" (type "
  781. "%d, writecount %d, passcount %d)",
  782. tif->tif_name, fip->field_name,
  783. fip->field_type, fip->field_writecount,
  784. fip->field_passcount);
  785. goto end;
  786. }
  787. tv->value = _TIFFCheckMalloc(tif, tv->count, tv_size,
  788. "custom tag binary object");
  789. if (!tv->value)
  790. {
  791. status = 0;
  792. goto end;
  793. }
  794. if (fip->field_tag == TIFFTAG_DOTRANGE &&
  795. strcmp(fip->field_name, "DotRange") == 0)
  796. {
  797. /* TODO: This is an evil exception and should not have been
  798. handled this way ... likely best if we move it into
  799. the directory structure with an explicit field in
  800. libtiff 4.1 and assign it a FIELD_ value */
  801. uint16_t v2[2];
  802. v2[0] = (uint16_t)va_arg(ap, int);
  803. v2[1] = (uint16_t)va_arg(ap, int);
  804. _TIFFmemcpy(tv->value, &v2, 4);
  805. }
  806. else if (fip->field_passcount ||
  807. fip->field_writecount == TIFF_VARIABLE ||
  808. fip->field_writecount == TIFF_VARIABLE2 ||
  809. fip->field_writecount == TIFF_SPP || tv->count > 1)
  810. {
  811. /*--: Rational2Double: For Rationals tv_size is set above to
  812. * 4 or 8 according to fip->set_field_type! */
  813. _TIFFmemcpy(tv->value, va_arg(ap, void *),
  814. tv->count * tv_size);
  815. /* Test here for too big values for LONG8, SLONG8 in
  816. * ClassicTIFF and delete custom field from custom list */
  817. if (!(tif->tif_flags & TIFF_BIGTIFF))
  818. {
  819. if (tv->info->field_type == TIFF_LONG8)
  820. {
  821. uint64_t *pui64 = (uint64_t *)tv->value;
  822. for (int i = 0; i < tv->count; i++)
  823. {
  824. if (pui64[i] > 0xffffffffu)
  825. {
  826. TIFFErrorExtR(
  827. tif, module,
  828. "%s: Bad LONG8 value %" PRIu64
  829. " at %d. array position for \"%s\" tag "
  830. "%d in ClassicTIFF. Tag won't be "
  831. "written to file",
  832. tif->tif_name, pui64[i], i,
  833. fip->field_name, tag);
  834. goto badvalueifd8long8;
  835. }
  836. }
  837. }
  838. else if (tv->info->field_type == TIFF_SLONG8)
  839. {
  840. int64_t *pi64 = (int64_t *)tv->value;
  841. for (int i = 0; i < tv->count; i++)
  842. {
  843. if (pi64[i] > 2147483647 ||
  844. pi64[i] < (-2147483647 - 1))
  845. {
  846. TIFFErrorExtR(
  847. tif, module,
  848. "%s: Bad SLONG8 value %" PRIi64
  849. " at %d. array position for \"%s\" tag "
  850. "%d in ClassicTIFF. Tag won't be "
  851. "written to file",
  852. tif->tif_name, pi64[i], i,
  853. fip->field_name, tag);
  854. goto badvalueifd8long8;
  855. }
  856. }
  857. }
  858. }
  859. }
  860. else
  861. {
  862. char *val = (char *)tv->value;
  863. assert(tv->count == 1);
  864. switch (fip->field_type)
  865. {
  866. case TIFF_BYTE:
  867. case TIFF_UNDEFINED:
  868. {
  869. uint8_t v2 = (uint8_t)va_arg(ap, int);
  870. _TIFFmemcpy(val, &v2, tv_size);
  871. }
  872. break;
  873. case TIFF_SBYTE:
  874. {
  875. int8_t v2 = (int8_t)va_arg(ap, int);
  876. _TIFFmemcpy(val, &v2, tv_size);
  877. }
  878. break;
  879. case TIFF_SHORT:
  880. {
  881. uint16_t v2 = (uint16_t)va_arg(ap, int);
  882. _TIFFmemcpy(val, &v2, tv_size);
  883. }
  884. break;
  885. case TIFF_SSHORT:
  886. {
  887. int16_t v2 = (int16_t)va_arg(ap, int);
  888. _TIFFmemcpy(val, &v2, tv_size);
  889. }
  890. break;
  891. case TIFF_LONG:
  892. case TIFF_IFD:
  893. {
  894. uint32_t v2 = va_arg(ap, uint32_t);
  895. _TIFFmemcpy(val, &v2, tv_size);
  896. }
  897. break;
  898. case TIFF_SLONG:
  899. {
  900. int32_t v2 = va_arg(ap, int32_t);
  901. _TIFFmemcpy(val, &v2, tv_size);
  902. }
  903. break;
  904. case TIFF_LONG8:
  905. case TIFF_IFD8:
  906. {
  907. uint64_t v2 = va_arg(ap, uint64_t);
  908. _TIFFmemcpy(val, &v2, tv_size);
  909. /* Test here for too big values for ClassicTIFF and
  910. * delete custom field from custom list */
  911. if (!(tif->tif_flags & TIFF_BIGTIFF) &&
  912. (v2 > 0xffffffffu))
  913. {
  914. TIFFErrorExtR(
  915. tif, module,
  916. "%s: Bad LONG8 or IFD8 value %" PRIu64
  917. " for \"%s\" tag %d in ClassicTIFF. Tag "
  918. "won't be written to file",
  919. tif->tif_name, v2, fip->field_name, tag);
  920. goto badvalueifd8long8;
  921. }
  922. }
  923. break;
  924. case TIFF_SLONG8:
  925. {
  926. int64_t v2 = va_arg(ap, int64_t);
  927. _TIFFmemcpy(val, &v2, tv_size);
  928. /* Test here for too big values for ClassicTIFF and
  929. * delete custom field from custom list */
  930. if (!(tif->tif_flags & TIFF_BIGTIFF) &&
  931. ((v2 > 2147483647) || (v2 < (-2147483647 - 1))))
  932. {
  933. TIFFErrorExtR(
  934. tif, module,
  935. "%s: Bad SLONG8 value %" PRIi64
  936. " for \"%s\" tag %d in ClassicTIFF. Tag "
  937. "won't be written to file",
  938. tif->tif_name, v2, fip->field_name, tag);
  939. goto badvalueifd8long8;
  940. }
  941. }
  942. break;
  943. case TIFF_RATIONAL:
  944. case TIFF_SRATIONAL:
  945. /*-- Rational2Double: For Rationals tv_size is set
  946. * above to 4 or 8 according to fip->set_field_type!
  947. */
  948. {
  949. if (tv_size == 8)
  950. {
  951. double v2 = va_arg(ap, double);
  952. _TIFFmemcpy(val, &v2, tv_size);
  953. }
  954. else
  955. {
  956. /*-- default should be tv_size == 4 */
  957. float v3 = (float)va_arg(ap, double);
  958. _TIFFmemcpy(val, &v3, tv_size);
  959. /*-- ToDo: After Testing, this should be
  960. * removed and tv_size==4 should be set as
  961. * default. */
  962. if (tv_size != 4)
  963. {
  964. TIFFErrorExtR(
  965. tif, module,
  966. "Rational2Double: .set_field_type "
  967. "in not 4 but %d",
  968. tv_size);
  969. }
  970. }
  971. }
  972. break;
  973. case TIFF_FLOAT:
  974. {
  975. float v2 =
  976. _TIFFClampDoubleToFloat(va_arg(ap, double));
  977. _TIFFmemcpy(val, &v2, tv_size);
  978. }
  979. break;
  980. case TIFF_DOUBLE:
  981. {
  982. double v2 = va_arg(ap, double);
  983. _TIFFmemcpy(val, &v2, tv_size);
  984. }
  985. break;
  986. default:
  987. _TIFFmemset(val, 0, tv_size);
  988. status = 0;
  989. break;
  990. }
  991. }
  992. }
  993. }
  994. }
  995. if (status)
  996. {
  997. const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
  998. if (fip2)
  999. TIFFSetFieldBit(tif, fip2->field_bit);
  1000. tif->tif_flags |= TIFF_DIRTYDIRECT;
  1001. }
  1002. end:
  1003. va_end(ap);
  1004. return (status);
  1005. badvalue:
  1006. {
  1007. const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
  1008. TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
  1009. tif->tif_name, v, fip2 ? fip2->field_name : "Unknown");
  1010. va_end(ap);
  1011. }
  1012. return (0);
  1013. badvalue32:
  1014. {
  1015. const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
  1016. TIFFErrorExtR(tif, module, "%s: Bad value %" PRIu32 " for \"%s\" tag",
  1017. tif->tif_name, v32, fip2 ? fip2->field_name : "Unknown");
  1018. va_end(ap);
  1019. }
  1020. return (0);
  1021. badvaluedouble:
  1022. {
  1023. const TIFFField *fip2 = TIFFFieldWithTag(tif, tag);
  1024. TIFFErrorExtR(tif, module, "%s: Bad value %f for \"%s\" tag", tif->tif_name,
  1025. dblval, fip2 ? fip2->field_name : "Unknown");
  1026. va_end(ap);
  1027. }
  1028. return (0);
  1029. badvalueifd8long8:
  1030. {
  1031. /* Error message issued already above. */
  1032. TIFFTagValue *tv2 = NULL;
  1033. int iCustom2, iC2;
  1034. /* Find the existing entry for this custom value. */
  1035. for (iCustom2 = 0; iCustom2 < td->td_customValueCount; iCustom2++)
  1036. {
  1037. if (td->td_customValues[iCustom2].info->field_tag == tag)
  1038. {
  1039. tv2 = td->td_customValues + (iCustom2);
  1040. break;
  1041. }
  1042. }
  1043. if (tv2 != NULL)
  1044. {
  1045. /* Remove custom field from custom list */
  1046. if (tv2->value != NULL)
  1047. {
  1048. _TIFFfreeExt(tif, tv2->value);
  1049. tv2->value = NULL;
  1050. }
  1051. /* Shorten list and close gap in customValues list.
  1052. * Re-allocation of td_customValues not necessary here. */
  1053. td->td_customValueCount--;
  1054. for (iC2 = iCustom2; iC2 < td->td_customValueCount; iC2++)
  1055. {
  1056. td->td_customValues[iC2] = td->td_customValues[iC2 + 1];
  1057. }
  1058. }
  1059. else
  1060. {
  1061. assert(0);
  1062. }
  1063. va_end(ap);
  1064. }
  1065. return (0);
  1066. } /*-- _TIFFVSetField() --*/
  1067. /*
  1068. * Return 1/0 according to whether or not
  1069. * it is permissible to set the tag's value.
  1070. * Note that we allow ImageLength to be changed
  1071. * so that we can append and extend to images.
  1072. * Any other tag may not be altered once writing
  1073. * has commenced, unless its value has no effect
  1074. * on the format of the data that is written.
  1075. */
  1076. static int OkToChangeTag(TIFF *tif, uint32_t tag)
  1077. {
  1078. const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
  1079. if (!fip)
  1080. { /* unknown tag */
  1081. TIFFErrorExtR(tif, "TIFFSetField", "%s: Unknown %stag %" PRIu32,
  1082. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
  1083. return (0);
  1084. }
  1085. if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
  1086. !fip->field_oktochange)
  1087. {
  1088. /*
  1089. * Consult info table to see if tag can be changed
  1090. * after we've started writing. We only allow changes
  1091. * to those tags that don't/shouldn't affect the
  1092. * compression and/or format of the data.
  1093. */
  1094. TIFFErrorExtR(tif, "TIFFSetField",
  1095. "%s: Cannot modify tag \"%s\" while writing",
  1096. tif->tif_name, fip->field_name);
  1097. return (0);
  1098. }
  1099. return (1);
  1100. }
  1101. /*
  1102. * Record the value of a field in the
  1103. * internal directory structure. The
  1104. * field will be written to the file
  1105. * when/if the directory structure is
  1106. * updated.
  1107. */
  1108. int TIFFSetField(TIFF *tif, uint32_t tag, ...)
  1109. {
  1110. va_list ap;
  1111. int status;
  1112. va_start(ap, tag);
  1113. status = TIFFVSetField(tif, tag, ap);
  1114. va_end(ap);
  1115. return (status);
  1116. }
  1117. /*
  1118. * Clear the contents of the field in the internal structure.
  1119. */
  1120. int TIFFUnsetField(TIFF *tif, uint32_t tag)
  1121. {
  1122. const TIFFField *fip = TIFFFieldWithTag(tif, tag);
  1123. TIFFDirectory *td = &tif->tif_dir;
  1124. if (!fip)
  1125. return 0;
  1126. if (fip->field_bit != FIELD_CUSTOM)
  1127. TIFFClrFieldBit(tif, fip->field_bit);
  1128. else
  1129. {
  1130. TIFFTagValue *tv = NULL;
  1131. int i;
  1132. for (i = 0; i < td->td_customValueCount; i++)
  1133. {
  1134. tv = td->td_customValues + i;
  1135. if (tv->info->field_tag == tag)
  1136. break;
  1137. }
  1138. if (i < td->td_customValueCount)
  1139. {
  1140. _TIFFfreeExt(tif, tv->value);
  1141. for (; i < td->td_customValueCount - 1; i++)
  1142. {
  1143. td->td_customValues[i] = td->td_customValues[i + 1];
  1144. }
  1145. td->td_customValueCount--;
  1146. }
  1147. }
  1148. tif->tif_flags |= TIFF_DIRTYDIRECT;
  1149. return (1);
  1150. }
  1151. /*
  1152. * Like TIFFSetField, but taking a varargs
  1153. * parameter list. This routine is useful
  1154. * for building higher-level interfaces on
  1155. * top of the library.
  1156. */
  1157. int TIFFVSetField(TIFF *tif, uint32_t tag, va_list ap)
  1158. {
  1159. return OkToChangeTag(tif, tag)
  1160. ? (*tif->tif_tagmethods.vsetfield)(tif, tag, ap)
  1161. : 0;
  1162. }
  1163. static int _TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1164. {
  1165. TIFFDirectory *td = &tif->tif_dir;
  1166. int ret_val = 1;
  1167. uint32_t standard_tag = tag;
  1168. const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
  1169. if (fip == NULL) /* cannot happen since TIFFGetField() already checks it */
  1170. return 0;
  1171. /*
  1172. * We want to force the custom code to be used for custom
  1173. * fields even if the tag happens to match a well known
  1174. * one - important for reinterpreted handling of standard
  1175. * tag values in custom directories (i.e. EXIF)
  1176. */
  1177. if (fip->field_bit == FIELD_CUSTOM)
  1178. {
  1179. standard_tag = 0;
  1180. }
  1181. switch (standard_tag)
  1182. {
  1183. case TIFFTAG_SUBFILETYPE:
  1184. *va_arg(ap, uint32_t *) = td->td_subfiletype;
  1185. break;
  1186. case TIFFTAG_IMAGEWIDTH:
  1187. *va_arg(ap, uint32_t *) = td->td_imagewidth;
  1188. break;
  1189. case TIFFTAG_IMAGELENGTH:
  1190. *va_arg(ap, uint32_t *) = td->td_imagelength;
  1191. break;
  1192. case TIFFTAG_BITSPERSAMPLE:
  1193. *va_arg(ap, uint16_t *) = td->td_bitspersample;
  1194. break;
  1195. case TIFFTAG_COMPRESSION:
  1196. *va_arg(ap, uint16_t *) = td->td_compression;
  1197. break;
  1198. case TIFFTAG_PHOTOMETRIC:
  1199. *va_arg(ap, uint16_t *) = td->td_photometric;
  1200. break;
  1201. case TIFFTAG_THRESHHOLDING:
  1202. *va_arg(ap, uint16_t *) = td->td_threshholding;
  1203. break;
  1204. case TIFFTAG_FILLORDER:
  1205. *va_arg(ap, uint16_t *) = td->td_fillorder;
  1206. break;
  1207. case TIFFTAG_ORIENTATION:
  1208. *va_arg(ap, uint16_t *) = td->td_orientation;
  1209. break;
  1210. case TIFFTAG_SAMPLESPERPIXEL:
  1211. *va_arg(ap, uint16_t *) = td->td_samplesperpixel;
  1212. break;
  1213. case TIFFTAG_ROWSPERSTRIP:
  1214. *va_arg(ap, uint32_t *) = td->td_rowsperstrip;
  1215. break;
  1216. case TIFFTAG_MINSAMPLEVALUE:
  1217. *va_arg(ap, uint16_t *) = td->td_minsamplevalue;
  1218. break;
  1219. case TIFFTAG_MAXSAMPLEVALUE:
  1220. *va_arg(ap, uint16_t *) = td->td_maxsamplevalue;
  1221. break;
  1222. case TIFFTAG_SMINSAMPLEVALUE:
  1223. if (tif->tif_flags & TIFF_PERSAMPLE)
  1224. *va_arg(ap, double **) = td->td_sminsamplevalue;
  1225. else
  1226. {
  1227. /* libtiff historically treats this as a single value. */
  1228. uint16_t i;
  1229. double v = td->td_sminsamplevalue[0];
  1230. for (i = 1; i < td->td_samplesperpixel; ++i)
  1231. if (td->td_sminsamplevalue[i] < v)
  1232. v = td->td_sminsamplevalue[i];
  1233. *va_arg(ap, double *) = v;
  1234. }
  1235. break;
  1236. case TIFFTAG_SMAXSAMPLEVALUE:
  1237. if (tif->tif_flags & TIFF_PERSAMPLE)
  1238. *va_arg(ap, double **) = td->td_smaxsamplevalue;
  1239. else
  1240. {
  1241. /* libtiff historically treats this as a single value. */
  1242. uint16_t i;
  1243. double v = td->td_smaxsamplevalue[0];
  1244. for (i = 1; i < td->td_samplesperpixel; ++i)
  1245. if (td->td_smaxsamplevalue[i] > v)
  1246. v = td->td_smaxsamplevalue[i];
  1247. *va_arg(ap, double *) = v;
  1248. }
  1249. break;
  1250. case TIFFTAG_XRESOLUTION:
  1251. *va_arg(ap, float *) = td->td_xresolution;
  1252. break;
  1253. case TIFFTAG_YRESOLUTION:
  1254. *va_arg(ap, float *) = td->td_yresolution;
  1255. break;
  1256. case TIFFTAG_PLANARCONFIG:
  1257. *va_arg(ap, uint16_t *) = td->td_planarconfig;
  1258. break;
  1259. case TIFFTAG_XPOSITION:
  1260. *va_arg(ap, float *) = td->td_xposition;
  1261. break;
  1262. case TIFFTAG_YPOSITION:
  1263. *va_arg(ap, float *) = td->td_yposition;
  1264. break;
  1265. case TIFFTAG_RESOLUTIONUNIT:
  1266. *va_arg(ap, uint16_t *) = td->td_resolutionunit;
  1267. break;
  1268. case TIFFTAG_PAGENUMBER:
  1269. *va_arg(ap, uint16_t *) = td->td_pagenumber[0];
  1270. *va_arg(ap, uint16_t *) = td->td_pagenumber[1];
  1271. break;
  1272. case TIFFTAG_HALFTONEHINTS:
  1273. *va_arg(ap, uint16_t *) = td->td_halftonehints[0];
  1274. *va_arg(ap, uint16_t *) = td->td_halftonehints[1];
  1275. break;
  1276. case TIFFTAG_COLORMAP:
  1277. *va_arg(ap, const uint16_t **) = td->td_colormap[0];
  1278. *va_arg(ap, const uint16_t **) = td->td_colormap[1];
  1279. *va_arg(ap, const uint16_t **) = td->td_colormap[2];
  1280. break;
  1281. case TIFFTAG_STRIPOFFSETS:
  1282. case TIFFTAG_TILEOFFSETS:
  1283. _TIFFFillStriles(tif);
  1284. *va_arg(ap, const uint64_t **) = td->td_stripoffset_p;
  1285. if (td->td_stripoffset_p == NULL)
  1286. ret_val = 0;
  1287. break;
  1288. case TIFFTAG_STRIPBYTECOUNTS:
  1289. case TIFFTAG_TILEBYTECOUNTS:
  1290. _TIFFFillStriles(tif);
  1291. *va_arg(ap, const uint64_t **) = td->td_stripbytecount_p;
  1292. if (td->td_stripbytecount_p == NULL)
  1293. ret_val = 0;
  1294. break;
  1295. case TIFFTAG_MATTEING:
  1296. *va_arg(ap, uint16_t *) =
  1297. (td->td_extrasamples == 1 &&
  1298. td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  1299. break;
  1300. case TIFFTAG_EXTRASAMPLES:
  1301. *va_arg(ap, uint16_t *) = td->td_extrasamples;
  1302. *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
  1303. break;
  1304. case TIFFTAG_TILEWIDTH:
  1305. *va_arg(ap, uint32_t *) = td->td_tilewidth;
  1306. break;
  1307. case TIFFTAG_TILELENGTH:
  1308. *va_arg(ap, uint32_t *) = td->td_tilelength;
  1309. break;
  1310. case TIFFTAG_TILEDEPTH:
  1311. *va_arg(ap, uint32_t *) = td->td_tiledepth;
  1312. break;
  1313. case TIFFTAG_DATATYPE:
  1314. switch (td->td_sampleformat)
  1315. {
  1316. case SAMPLEFORMAT_UINT:
  1317. *va_arg(ap, uint16_t *) = DATATYPE_UINT;
  1318. break;
  1319. case SAMPLEFORMAT_INT:
  1320. *va_arg(ap, uint16_t *) = DATATYPE_INT;
  1321. break;
  1322. case SAMPLEFORMAT_IEEEFP:
  1323. *va_arg(ap, uint16_t *) = DATATYPE_IEEEFP;
  1324. break;
  1325. case SAMPLEFORMAT_VOID:
  1326. *va_arg(ap, uint16_t *) = DATATYPE_VOID;
  1327. break;
  1328. }
  1329. break;
  1330. case TIFFTAG_SAMPLEFORMAT:
  1331. *va_arg(ap, uint16_t *) = td->td_sampleformat;
  1332. break;
  1333. case TIFFTAG_IMAGEDEPTH:
  1334. *va_arg(ap, uint32_t *) = td->td_imagedepth;
  1335. break;
  1336. case TIFFTAG_SUBIFD:
  1337. *va_arg(ap, uint16_t *) = td->td_nsubifd;
  1338. *va_arg(ap, const uint64_t **) = td->td_subifd;
  1339. break;
  1340. case TIFFTAG_YCBCRPOSITIONING:
  1341. *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning;
  1342. break;
  1343. case TIFFTAG_YCBCRSUBSAMPLING:
  1344. *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0];
  1345. *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1];
  1346. break;
  1347. case TIFFTAG_TRANSFERFUNCTION:
  1348. *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
  1349. if (td->td_samplesperpixel - td->td_extrasamples > 1)
  1350. {
  1351. *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
  1352. *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
  1353. }
  1354. else
  1355. {
  1356. *va_arg(ap, const uint16_t **) = NULL;
  1357. *va_arg(ap, const uint16_t **) = NULL;
  1358. }
  1359. break;
  1360. case TIFFTAG_REFERENCEBLACKWHITE:
  1361. *va_arg(ap, const float **) = td->td_refblackwhite;
  1362. break;
  1363. case TIFFTAG_INKNAMES:
  1364. *va_arg(ap, const char **) = td->td_inknames;
  1365. break;
  1366. case TIFFTAG_NUMBEROFINKS:
  1367. *va_arg(ap, uint16_t *) = td->td_numberofinks;
  1368. break;
  1369. default:
  1370. {
  1371. int i;
  1372. /*
  1373. * This can happen if multiple images are open
  1374. * with different codecs which have private
  1375. * tags. The global tag information table may
  1376. * then have tags that are valid for one file
  1377. * but not the other. If the client tries to
  1378. * get a tag that is not valid for the image's
  1379. * codec then we'll arrive here.
  1380. */
  1381. if (fip->field_bit != FIELD_CUSTOM)
  1382. {
  1383. TIFFErrorExtR(tif, "_TIFFVGetField",
  1384. "%s: Invalid %stag \"%s\" "
  1385. "(not supported by codec)",
  1386. tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
  1387. fip->field_name);
  1388. ret_val = 0;
  1389. break;
  1390. }
  1391. /*
  1392. * Do we have a custom value?
  1393. */
  1394. ret_val = 0;
  1395. for (i = 0; i < td->td_customValueCount; i++)
  1396. {
  1397. TIFFTagValue *tv = td->td_customValues + i;
  1398. if (tv->info->field_tag != tag)
  1399. continue;
  1400. if (fip->field_passcount)
  1401. {
  1402. if (fip->field_readcount == TIFF_VARIABLE2)
  1403. *va_arg(ap, uint32_t *) = (uint32_t)tv->count;
  1404. else /* Assume TIFF_VARIABLE */
  1405. *va_arg(ap, uint16_t *) = (uint16_t)tv->count;
  1406. *va_arg(ap, const void **) = tv->value;
  1407. ret_val = 1;
  1408. }
  1409. else if (fip->field_tag == TIFFTAG_DOTRANGE &&
  1410. strcmp(fip->field_name, "DotRange") == 0)
  1411. {
  1412. /* TODO: This is an evil exception and should not have been
  1413. handled this way ... likely best if we move it into
  1414. the directory structure with an explicit field in
  1415. libtiff 4.1 and assign it a FIELD_ value */
  1416. *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[0];
  1417. *va_arg(ap, uint16_t *) = ((uint16_t *)tv->value)[1];
  1418. ret_val = 1;
  1419. }
  1420. else
  1421. {
  1422. if (fip->field_type == TIFF_ASCII ||
  1423. fip->field_readcount == TIFF_VARIABLE ||
  1424. fip->field_readcount == TIFF_VARIABLE2 ||
  1425. fip->field_readcount == TIFF_SPP || tv->count > 1)
  1426. {
  1427. *va_arg(ap, void **) = tv->value;
  1428. ret_val = 1;
  1429. }
  1430. else
  1431. {
  1432. char *val = (char *)tv->value;
  1433. assert(tv->count == 1);
  1434. switch (fip->field_type)
  1435. {
  1436. case TIFF_BYTE:
  1437. case TIFF_UNDEFINED:
  1438. *va_arg(ap, uint8_t *) = *(uint8_t *)val;
  1439. ret_val = 1;
  1440. break;
  1441. case TIFF_SBYTE:
  1442. *va_arg(ap, int8_t *) = *(int8_t *)val;
  1443. ret_val = 1;
  1444. break;
  1445. case TIFF_SHORT:
  1446. *va_arg(ap, uint16_t *) = *(uint16_t *)val;
  1447. ret_val = 1;
  1448. break;
  1449. case TIFF_SSHORT:
  1450. *va_arg(ap, int16_t *) = *(int16_t *)val;
  1451. ret_val = 1;
  1452. break;
  1453. case TIFF_LONG:
  1454. case TIFF_IFD:
  1455. *va_arg(ap, uint32_t *) = *(uint32_t *)val;
  1456. ret_val = 1;
  1457. break;
  1458. case TIFF_SLONG:
  1459. *va_arg(ap, int32_t *) = *(int32_t *)val;
  1460. ret_val = 1;
  1461. break;
  1462. case TIFF_LONG8:
  1463. case TIFF_IFD8:
  1464. *va_arg(ap, uint64_t *) = *(uint64_t *)val;
  1465. ret_val = 1;
  1466. break;
  1467. case TIFF_SLONG8:
  1468. *va_arg(ap, int64_t *) = *(int64_t *)val;
  1469. ret_val = 1;
  1470. break;
  1471. case TIFF_RATIONAL:
  1472. case TIFF_SRATIONAL:
  1473. {
  1474. /*-- Rational2Double: For Rationals evaluate
  1475. * "set_field_type" to determine internal
  1476. * storage size and return value size. */
  1477. int tv_size = TIFFFieldSetGetSize(fip);
  1478. if (tv_size == 8)
  1479. {
  1480. *va_arg(ap, double *) = *(double *)val;
  1481. ret_val = 1;
  1482. }
  1483. else
  1484. {
  1485. /*-- default should be tv_size == 4 */
  1486. *va_arg(ap, float *) = *(float *)val;
  1487. ret_val = 1;
  1488. /*-- ToDo: After Testing, this should be
  1489. * removed and tv_size==4 should be set as
  1490. * default. */
  1491. if (tv_size != 4)
  1492. {
  1493. TIFFErrorExtR(
  1494. tif, "_TIFFVGetField",
  1495. "Rational2Double: .set_field_type "
  1496. "in not 4 but %d",
  1497. tv_size);
  1498. }
  1499. }
  1500. }
  1501. break;
  1502. case TIFF_FLOAT:
  1503. *va_arg(ap, float *) = *(float *)val;
  1504. ret_val = 1;
  1505. break;
  1506. case TIFF_DOUBLE:
  1507. *va_arg(ap, double *) = *(double *)val;
  1508. ret_val = 1;
  1509. break;
  1510. default:
  1511. ret_val = 0;
  1512. break;
  1513. }
  1514. }
  1515. }
  1516. break;
  1517. }
  1518. }
  1519. }
  1520. return (ret_val);
  1521. }
  1522. /*
  1523. * Return the value of a field in the
  1524. * internal directory structure.
  1525. */
  1526. int TIFFGetField(TIFF *tif, uint32_t tag, ...)
  1527. {
  1528. int status;
  1529. va_list ap;
  1530. va_start(ap, tag);
  1531. status = TIFFVGetField(tif, tag, ap);
  1532. va_end(ap);
  1533. return (status);
  1534. }
  1535. /*
  1536. * Like TIFFGetField, but taking a varargs
  1537. * parameter list. This routine is useful
  1538. * for building higher-level interfaces on
  1539. * top of the library.
  1540. */
  1541. int TIFFVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1542. {
  1543. const TIFFField *fip = TIFFFindField(tif, tag, TIFF_ANY);
  1544. return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit))
  1545. ? (*tif->tif_tagmethods.vgetfield)(tif, tag, ap)
  1546. : 0);
  1547. }
  1548. #define CleanupField(member) \
  1549. { \
  1550. if (td->member) \
  1551. { \
  1552. _TIFFfreeExt(tif, td->member); \
  1553. td->member = 0; \
  1554. } \
  1555. }
  1556. /*
  1557. * Release storage associated with a directory.
  1558. */
  1559. void TIFFFreeDirectory(TIFF *tif)
  1560. {
  1561. TIFFDirectory *td = &tif->tif_dir;
  1562. int i;
  1563. _TIFFmemset(td->td_fieldsset, 0, sizeof(td->td_fieldsset));
  1564. CleanupField(td_sminsamplevalue);
  1565. CleanupField(td_smaxsamplevalue);
  1566. CleanupField(td_colormap[0]);
  1567. CleanupField(td_colormap[1]);
  1568. CleanupField(td_colormap[2]);
  1569. CleanupField(td_sampleinfo);
  1570. CleanupField(td_subifd);
  1571. CleanupField(td_inknames);
  1572. CleanupField(td_refblackwhite);
  1573. CleanupField(td_transferfunction[0]);
  1574. CleanupField(td_transferfunction[1]);
  1575. CleanupField(td_transferfunction[2]);
  1576. CleanupField(td_stripoffset_p);
  1577. CleanupField(td_stripbytecount_p);
  1578. td->td_stripoffsetbyteallocsize = 0;
  1579. TIFFClrFieldBit(tif, FIELD_YCBCRSUBSAMPLING);
  1580. TIFFClrFieldBit(tif, FIELD_YCBCRPOSITIONING);
  1581. /* Cleanup custom tag values */
  1582. for (i = 0; i < td->td_customValueCount; i++)
  1583. {
  1584. if (td->td_customValues[i].value)
  1585. _TIFFfreeExt(tif, td->td_customValues[i].value);
  1586. }
  1587. td->td_customValueCount = 0;
  1588. CleanupField(td_customValues);
  1589. _TIFFmemset(&(td->td_stripoffset_entry), 0, sizeof(TIFFDirEntry));
  1590. _TIFFmemset(&(td->td_stripbytecount_entry), 0, sizeof(TIFFDirEntry));
  1591. }
  1592. #undef CleanupField
  1593. /*
  1594. * Client Tag extension support (from Niles Ritter).
  1595. */
  1596. static TIFFExtendProc _TIFFextender = (TIFFExtendProc)NULL;
  1597. TIFFExtendProc TIFFSetTagExtender(TIFFExtendProc extender)
  1598. {
  1599. TIFFExtendProc prev = _TIFFextender;
  1600. _TIFFextender = extender;
  1601. return (prev);
  1602. }
  1603. /*
  1604. * Setup for a new directory. Should we automatically call
  1605. * TIFFWriteDirectory() if the current one is dirty?
  1606. *
  1607. * The newly created directory will not exist on the file till
  1608. * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
  1609. */
  1610. int TIFFCreateDirectory(TIFF *tif)
  1611. {
  1612. TIFFDefaultDirectory(tif);
  1613. tif->tif_diroff = 0;
  1614. tif->tif_nextdiroff = 0;
  1615. tif->tif_curoff = 0;
  1616. tif->tif_row = (uint32_t)-1;
  1617. tif->tif_curstrip = (uint32_t)-1;
  1618. return 0;
  1619. }
  1620. int TIFFCreateCustomDirectory(TIFF *tif, const TIFFFieldArray *infoarray)
  1621. {
  1622. TIFFDefaultDirectory(tif);
  1623. /*
  1624. * Reset the field definitions to match the application provided list.
  1625. * Hopefully TIFFDefaultDirectory() won't have done anything irreversible
  1626. * based on it's assumption this is an image directory.
  1627. */
  1628. _TIFFSetupFields(tif, infoarray);
  1629. tif->tif_diroff = 0;
  1630. tif->tif_nextdiroff = 0;
  1631. tif->tif_curoff = 0;
  1632. tif->tif_row = (uint32_t)-1;
  1633. tif->tif_curstrip = (uint32_t)-1;
  1634. /* invalidate directory index */
  1635. tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
  1636. /* invalidate IFD loop lists */
  1637. _TIFFCleanupIFDOffsetAndNumberMaps(tif);
  1638. /* To be able to return from SubIFD or custom-IFD to main-IFD */
  1639. tif->tif_setdirectory_force_absolute = TRUE;
  1640. return 0;
  1641. }
  1642. int TIFFCreateEXIFDirectory(TIFF *tif)
  1643. {
  1644. const TIFFFieldArray *exifFieldArray;
  1645. exifFieldArray = _TIFFGetExifFields();
  1646. return TIFFCreateCustomDirectory(tif, exifFieldArray);
  1647. }
  1648. /*
  1649. * Creates the EXIF GPS custom directory
  1650. */
  1651. int TIFFCreateGPSDirectory(TIFF *tif)
  1652. {
  1653. const TIFFFieldArray *gpsFieldArray;
  1654. gpsFieldArray = _TIFFGetGpsFields();
  1655. return TIFFCreateCustomDirectory(tif, gpsFieldArray);
  1656. }
  1657. /*
  1658. * Setup a default directory structure.
  1659. */
  1660. int TIFFDefaultDirectory(TIFF *tif)
  1661. {
  1662. register TIFFDirectory *td = &tif->tif_dir;
  1663. const TIFFFieldArray *tiffFieldArray;
  1664. tiffFieldArray = _TIFFGetFields();
  1665. _TIFFSetupFields(tif, tiffFieldArray);
  1666. _TIFFmemset(td, 0, sizeof(*td));
  1667. td->td_fillorder = FILLORDER_MSB2LSB;
  1668. td->td_bitspersample = 1;
  1669. td->td_threshholding = THRESHHOLD_BILEVEL;
  1670. td->td_orientation = ORIENTATION_TOPLEFT;
  1671. td->td_samplesperpixel = 1;
  1672. td->td_rowsperstrip = (uint32_t)-1;
  1673. td->td_tilewidth = 0;
  1674. td->td_tilelength = 0;
  1675. td->td_tiledepth = 1;
  1676. #ifdef STRIPBYTECOUNTSORTED_UNUSED
  1677. td->td_stripbytecountsorted = 1; /* Our own arrays always sorted. */
  1678. #endif
  1679. td->td_resolutionunit = RESUNIT_INCH;
  1680. td->td_sampleformat = SAMPLEFORMAT_UINT;
  1681. td->td_imagedepth = 1;
  1682. td->td_ycbcrsubsampling[0] = 2;
  1683. td->td_ycbcrsubsampling[1] = 2;
  1684. td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
  1685. tif->tif_postdecode = _TIFFNoPostDecode;
  1686. tif->tif_foundfield = NULL;
  1687. tif->tif_tagmethods.vsetfield = _TIFFVSetField;
  1688. tif->tif_tagmethods.vgetfield = _TIFFVGetField;
  1689. tif->tif_tagmethods.printdir = NULL;
  1690. /* additional default values */
  1691. td->td_planarconfig = PLANARCONFIG_CONTIG;
  1692. td->td_compression = COMPRESSION_NONE;
  1693. td->td_subfiletype = 0;
  1694. td->td_minsamplevalue = 0;
  1695. /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
  1696. * Therefore, td_maxsamplevalue has to be re-calculated in
  1697. * TIFFGetFieldDefaulted(). */
  1698. td->td_maxsamplevalue = 1; /* Default for td_bitspersample=1 */
  1699. td->td_extrasamples = 0;
  1700. td->td_sampleinfo = NULL;
  1701. /*
  1702. * Give client code a chance to install their own
  1703. * tag extensions & methods, prior to compression overloads,
  1704. * but do some prior cleanup first.
  1705. * (http://trac.osgeo.org/gdal/ticket/5054)
  1706. */
  1707. if (tif->tif_nfieldscompat > 0)
  1708. {
  1709. uint32_t i;
  1710. for (i = 0; i < tif->tif_nfieldscompat; i++)
  1711. {
  1712. if (tif->tif_fieldscompat[i].allocated_size)
  1713. _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
  1714. }
  1715. _TIFFfreeExt(tif, tif->tif_fieldscompat);
  1716. tif->tif_nfieldscompat = 0;
  1717. tif->tif_fieldscompat = NULL;
  1718. }
  1719. if (_TIFFextender)
  1720. (*_TIFFextender)(tif);
  1721. (void)TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  1722. /*
  1723. * NB: The directory is marked dirty as a result of setting
  1724. * up the default compression scheme. However, this really
  1725. * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
  1726. * if the user does something. We could just do the setup
  1727. * by hand, but it seems better to use the normal mechanism
  1728. * (i.e. TIFFSetField).
  1729. */
  1730. tif->tif_flags &= ~TIFF_DIRTYDIRECT;
  1731. /*
  1732. * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
  1733. * we clear the ISTILED flag when setting up a new directory.
  1734. * Should we also be clearing stuff like INSUBIFD?
  1735. */
  1736. tif->tif_flags &= ~TIFF_ISTILED;
  1737. return (1);
  1738. }
  1739. static int TIFFAdvanceDirectory(TIFF *tif, uint64_t *nextdiroff, uint64_t *off,
  1740. tdir_t *nextdirnum)
  1741. {
  1742. static const char module[] = "TIFFAdvanceDirectory";
  1743. /* Add this directory to the directory list, if not already in. */
  1744. if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
  1745. {
  1746. TIFFErrorExtR(tif, module,
  1747. "Starting directory %u at offset 0x%" PRIx64 " (%" PRIu64
  1748. ") might cause an IFD loop",
  1749. *nextdirnum, *nextdiroff, *nextdiroff);
  1750. *nextdiroff = 0;
  1751. *nextdirnum = 0;
  1752. return (0);
  1753. }
  1754. if (isMapped(tif))
  1755. {
  1756. uint64_t poff = *nextdiroff;
  1757. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1758. {
  1759. tmsize_t poffa, poffb, poffc, poffd;
  1760. uint16_t dircount;
  1761. uint32_t nextdir32;
  1762. poffa = (tmsize_t)poff;
  1763. poffb = poffa + sizeof(uint16_t);
  1764. if (((uint64_t)poffa != poff) || (poffb < poffa) ||
  1765. (poffb < (tmsize_t)sizeof(uint16_t)) || (poffb > tif->tif_size))
  1766. {
  1767. TIFFErrorExtR(tif, module, "Error fetching directory count");
  1768. *nextdiroff = 0;
  1769. return (0);
  1770. }
  1771. _TIFFmemcpy(&dircount, tif->tif_base + poffa, sizeof(uint16_t));
  1772. if (tif->tif_flags & TIFF_SWAB)
  1773. TIFFSwabShort(&dircount);
  1774. poffc = poffb + dircount * 12;
  1775. poffd = poffc + sizeof(uint32_t);
  1776. if ((poffc < poffb) || (poffc < dircount * 12) || (poffd < poffc) ||
  1777. (poffd < (tmsize_t)sizeof(uint32_t)) || (poffd > tif->tif_size))
  1778. {
  1779. TIFFErrorExtR(tif, module, "Error fetching directory link");
  1780. return (0);
  1781. }
  1782. if (off != NULL)
  1783. *off = (uint64_t)poffc;
  1784. _TIFFmemcpy(&nextdir32, tif->tif_base + poffc, sizeof(uint32_t));
  1785. if (tif->tif_flags & TIFF_SWAB)
  1786. TIFFSwabLong(&nextdir32);
  1787. *nextdiroff = nextdir32;
  1788. }
  1789. else
  1790. {
  1791. tmsize_t poffa, poffb, poffc, poffd;
  1792. uint64_t dircount64;
  1793. uint16_t dircount16;
  1794. if (poff > (uint64_t)TIFF_TMSIZE_T_MAX - sizeof(uint64_t))
  1795. {
  1796. TIFFErrorExtR(tif, module, "Error fetching directory count");
  1797. return (0);
  1798. }
  1799. poffa = (tmsize_t)poff;
  1800. poffb = poffa + sizeof(uint64_t);
  1801. if (poffb > tif->tif_size)
  1802. {
  1803. TIFFErrorExtR(tif, module, "Error fetching directory count");
  1804. return (0);
  1805. }
  1806. _TIFFmemcpy(&dircount64, tif->tif_base + poffa, sizeof(uint64_t));
  1807. if (tif->tif_flags & TIFF_SWAB)
  1808. TIFFSwabLong8(&dircount64);
  1809. if (dircount64 > 0xFFFF)
  1810. {
  1811. TIFFErrorExtR(tif, module,
  1812. "Sanity check on directory count failed");
  1813. return (0);
  1814. }
  1815. dircount16 = (uint16_t)dircount64;
  1816. if (poffb > TIFF_TMSIZE_T_MAX - (tmsize_t)(dircount16 * 20) -
  1817. (tmsize_t)sizeof(uint64_t))
  1818. {
  1819. TIFFErrorExtR(tif, module, "Error fetching directory link");
  1820. return (0);
  1821. }
  1822. poffc = poffb + dircount16 * 20;
  1823. poffd = poffc + sizeof(uint64_t);
  1824. if (poffd > tif->tif_size)
  1825. {
  1826. TIFFErrorExtR(tif, module, "Error fetching directory link");
  1827. return (0);
  1828. }
  1829. if (off != NULL)
  1830. *off = (uint64_t)poffc;
  1831. _TIFFmemcpy(nextdiroff, tif->tif_base + poffc, sizeof(uint64_t));
  1832. if (tif->tif_flags & TIFF_SWAB)
  1833. TIFFSwabLong8(nextdiroff);
  1834. }
  1835. }
  1836. else
  1837. {
  1838. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1839. {
  1840. uint16_t dircount;
  1841. uint32_t nextdir32;
  1842. if (!SeekOK(tif, *nextdiroff) ||
  1843. !ReadOK(tif, &dircount, sizeof(uint16_t)))
  1844. {
  1845. TIFFErrorExtR(tif, module, "%s: Error fetching directory count",
  1846. tif->tif_name);
  1847. return (0);
  1848. }
  1849. if (tif->tif_flags & TIFF_SWAB)
  1850. TIFFSwabShort(&dircount);
  1851. if (off != NULL)
  1852. *off = TIFFSeekFile(tif, dircount * 12, SEEK_CUR);
  1853. else
  1854. (void)TIFFSeekFile(tif, dircount * 12, SEEK_CUR);
  1855. if (!ReadOK(tif, &nextdir32, sizeof(uint32_t)))
  1856. {
  1857. TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
  1858. tif->tif_name);
  1859. return (0);
  1860. }
  1861. if (tif->tif_flags & TIFF_SWAB)
  1862. TIFFSwabLong(&nextdir32);
  1863. *nextdiroff = nextdir32;
  1864. }
  1865. else
  1866. {
  1867. uint64_t dircount64;
  1868. uint16_t dircount16;
  1869. if (!SeekOK(tif, *nextdiroff) ||
  1870. !ReadOK(tif, &dircount64, sizeof(uint64_t)))
  1871. {
  1872. TIFFErrorExtR(tif, module, "%s: Error fetching directory count",
  1873. tif->tif_name);
  1874. return (0);
  1875. }
  1876. if (tif->tif_flags & TIFF_SWAB)
  1877. TIFFSwabLong8(&dircount64);
  1878. if (dircount64 > 0xFFFF)
  1879. {
  1880. TIFFErrorExtR(tif, module, "Error fetching directory count");
  1881. return (0);
  1882. }
  1883. dircount16 = (uint16_t)dircount64;
  1884. if (off != NULL)
  1885. *off = TIFFSeekFile(tif, dircount16 * 20, SEEK_CUR);
  1886. else
  1887. (void)TIFFSeekFile(tif, dircount16 * 20, SEEK_CUR);
  1888. if (!ReadOK(tif, nextdiroff, sizeof(uint64_t)))
  1889. {
  1890. TIFFErrorExtR(tif, module, "%s: Error fetching directory link",
  1891. tif->tif_name);
  1892. return (0);
  1893. }
  1894. if (tif->tif_flags & TIFF_SWAB)
  1895. TIFFSwabLong8(nextdiroff);
  1896. }
  1897. }
  1898. if (*nextdiroff != 0)
  1899. {
  1900. (*nextdirnum)++;
  1901. /* Check next directory for IFD looping and if so, set it as last
  1902. * directory. */
  1903. if (!_TIFFCheckDirNumberAndOffset(tif, *nextdirnum, *nextdiroff))
  1904. {
  1905. TIFFWarningExtR(
  1906. tif, module,
  1907. "the next directory %u at offset 0x%" PRIx64 " (%" PRIu64
  1908. ") might be an IFD loop. Treating directory %d as "
  1909. "last directory",
  1910. *nextdirnum, *nextdiroff, *nextdiroff, (int)(*nextdirnum) - 1);
  1911. *nextdiroff = 0;
  1912. (*nextdirnum)--;
  1913. }
  1914. }
  1915. return (1);
  1916. }
  1917. /*
  1918. * Count the number of directories in a file.
  1919. */
  1920. tdir_t TIFFNumberOfDirectories(TIFF *tif)
  1921. {
  1922. uint64_t nextdiroff;
  1923. tdir_t nextdirnum;
  1924. tdir_t n;
  1925. if (!(tif->tif_flags & TIFF_BIGTIFF))
  1926. nextdiroff = tif->tif_header.classic.tiff_diroff;
  1927. else
  1928. nextdiroff = tif->tif_header.big.tiff_diroff;
  1929. nextdirnum = 0;
  1930. n = 0;
  1931. while (nextdiroff != 0 &&
  1932. TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
  1933. {
  1934. ++n;
  1935. }
  1936. return (n);
  1937. }
  1938. /*
  1939. * Set the n-th directory as the current directory.
  1940. * NB: Directories are numbered starting at 0.
  1941. */
  1942. int TIFFSetDirectory(TIFF *tif, tdir_t dirn)
  1943. {
  1944. uint64_t nextdiroff;
  1945. tdir_t nextdirnum = 0;
  1946. tdir_t n;
  1947. if (tif->tif_setdirectory_force_absolute)
  1948. {
  1949. /* tif_setdirectory_force_absolute=1 will force parsing the main IFD
  1950. * chain from the beginning, thus IFD directory list needs to be cleared
  1951. * from possible SubIFD offsets.
  1952. */
  1953. _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
  1954. }
  1955. /* Even faster path, if offset is available within IFD loop hash list. */
  1956. if (!tif->tif_setdirectory_force_absolute &&
  1957. _TIFFGetOffsetFromDirNumber(tif, dirn, &nextdiroff))
  1958. {
  1959. /* Set parameters for following TIFFReadDirectory() below. */
  1960. tif->tif_nextdiroff = nextdiroff;
  1961. tif->tif_curdir = dirn;
  1962. /* Reset to relative stepping */
  1963. tif->tif_setdirectory_force_absolute = FALSE;
  1964. }
  1965. else
  1966. {
  1967. /* Fast path when we just advance relative to the current directory:
  1968. * start at the current dir offset and continue to seek from there.
  1969. * Check special cases when relative is not allowed:
  1970. * - jump back from SubIFD or custom directory
  1971. * - right after TIFFWriteDirectory() jump back to that directory
  1972. * using TIFFSetDirectory() */
  1973. const int relative = (dirn >= tif->tif_curdir) &&
  1974. (tif->tif_diroff != 0) &&
  1975. !tif->tif_setdirectory_force_absolute;
  1976. if (relative)
  1977. {
  1978. nextdiroff = tif->tif_diroff;
  1979. dirn -= tif->tif_curdir;
  1980. nextdirnum = tif->tif_curdir;
  1981. }
  1982. else if (!(tif->tif_flags & TIFF_BIGTIFF))
  1983. nextdiroff = tif->tif_header.classic.tiff_diroff;
  1984. else
  1985. nextdiroff = tif->tif_header.big.tiff_diroff;
  1986. /* Reset to relative stepping */
  1987. tif->tif_setdirectory_force_absolute = FALSE;
  1988. for (n = dirn; n > 0 && nextdiroff != 0; n--)
  1989. if (!TIFFAdvanceDirectory(tif, &nextdiroff, NULL, &nextdirnum))
  1990. return (0);
  1991. /* If the n-th directory could not be reached (does not exist),
  1992. * return here without touching anything further. */
  1993. if (nextdiroff == 0 || n > 0)
  1994. return (0);
  1995. tif->tif_nextdiroff = nextdiroff;
  1996. /* Set curdir to the actual directory index. */
  1997. if (relative)
  1998. tif->tif_curdir += dirn - n;
  1999. else
  2000. tif->tif_curdir = dirn - n;
  2001. }
  2002. /* The -1 decrement is because TIFFReadDirectory will increment
  2003. * tif_curdir after successfully reading the directory. */
  2004. if (tif->tif_curdir == 0)
  2005. tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
  2006. else
  2007. tif->tif_curdir--;
  2008. return (TIFFReadDirectory(tif));
  2009. }
  2010. /*
  2011. * Set the current directory to be the directory
  2012. * located at the specified file offset. This interface
  2013. * is used mainly to access directories linked with
  2014. * the SubIFD tag (e.g. thumbnail images).
  2015. */
  2016. int TIFFSetSubDirectory(TIFF *tif, uint64_t diroff)
  2017. {
  2018. /* Match nextdiroff and curdir for consistent IFD-loop checking.
  2019. * Only with TIFFSetSubDirectory() the IFD list can be corrupted with
  2020. * invalid offsets within the main IFD tree. In the case of several subIFDs
  2021. * of a main image, there are two possibilities that are not even mutually
  2022. * exclusive. a.) The subIFD tag contains an array with all offsets of the
  2023. * subIFDs. b.) The SubIFDs are concatenated with their NextIFD parameters.
  2024. * (refer to
  2025. * https://www.awaresystems.be/imaging/tiff/specification/TIFFPM6.pdf.)
  2026. */
  2027. int retval;
  2028. uint32_t curdir = 0;
  2029. int8_t probablySubIFD = 0;
  2030. if (diroff == 0)
  2031. {
  2032. /* Special case to invalidate the tif_lastdiroff member. */
  2033. tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
  2034. }
  2035. else
  2036. {
  2037. if (!_TIFFGetDirNumberFromOffset(tif, diroff, &curdir))
  2038. {
  2039. /* Non-existing offsets might point to a SubIFD or invalid IFD.*/
  2040. probablySubIFD = 1;
  2041. }
  2042. /* -1 because TIFFReadDirectory() will increment tif_curdir. */
  2043. tif->tif_curdir =
  2044. curdir == 0 ? TIFF_NON_EXISTENT_DIR_NUMBER : curdir - 1;
  2045. }
  2046. tif->tif_nextdiroff = diroff;
  2047. retval = TIFFReadDirectory(tif);
  2048. /* If failed, curdir was not incremented in TIFFReadDirectory(), so set it
  2049. * back, but leave it for diroff==0. */
  2050. if (!retval && diroff != 0)
  2051. {
  2052. if (tif->tif_curdir == TIFF_NON_EXISTENT_DIR_NUMBER)
  2053. tif->tif_curdir = 0;
  2054. else
  2055. tif->tif_curdir++;
  2056. }
  2057. if (retval && probablySubIFD)
  2058. {
  2059. /* Reset IFD list to start new one for SubIFD chain and also start
  2060. * SubIFD chain with tif_curdir=0. */
  2061. _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
  2062. tif->tif_curdir = 0; /* first directory of new chain */
  2063. /* add this offset to new IFD list */
  2064. _TIFFCheckDirNumberAndOffset(tif, tif->tif_curdir, diroff);
  2065. /* To be able to return from SubIFD or custom-IFD to main-IFD */
  2066. tif->tif_setdirectory_force_absolute = TRUE;
  2067. }
  2068. return (retval);
  2069. }
  2070. /*
  2071. * Return file offset of the current directory.
  2072. */
  2073. uint64_t TIFFCurrentDirOffset(TIFF *tif) { return (tif->tif_diroff); }
  2074. /*
  2075. * Return an indication of whether or not we are
  2076. * at the last directory in the file.
  2077. */
  2078. int TIFFLastDirectory(TIFF *tif) { return (tif->tif_nextdiroff == 0); }
  2079. /*
  2080. * Unlink the specified directory from the directory chain.
  2081. * Note: First directory starts with number dirn=1.
  2082. * This is different to TIFFSetDirectory() where the first directory starts with
  2083. * zero.
  2084. */
  2085. int TIFFUnlinkDirectory(TIFF *tif, tdir_t dirn)
  2086. {
  2087. static const char module[] = "TIFFUnlinkDirectory";
  2088. uint64_t nextdir;
  2089. tdir_t nextdirnum;
  2090. uint64_t off;
  2091. tdir_t n;
  2092. if (tif->tif_mode == O_RDONLY)
  2093. {
  2094. TIFFErrorExtR(tif, module,
  2095. "Can not unlink directory in read-only file");
  2096. return (0);
  2097. }
  2098. if (dirn == 0)
  2099. {
  2100. TIFFErrorExtR(tif, module,
  2101. "For TIFFUnlinkDirectory() first directory starts with "
  2102. "number 1 and not 0");
  2103. return (0);
  2104. }
  2105. /*
  2106. * Go to the directory before the one we want
  2107. * to unlink and nab the offset of the link
  2108. * field we'll need to patch.
  2109. */
  2110. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2111. {
  2112. nextdir = tif->tif_header.classic.tiff_diroff;
  2113. off = 4;
  2114. }
  2115. else
  2116. {
  2117. nextdir = tif->tif_header.big.tiff_diroff;
  2118. off = 8;
  2119. }
  2120. nextdirnum = 0; /* First directory is dirn=0 */
  2121. for (n = dirn - 1; n > 0; n--)
  2122. {
  2123. if (nextdir == 0)
  2124. {
  2125. TIFFErrorExtR(tif, module, "Directory %u does not exist", dirn);
  2126. return (0);
  2127. }
  2128. if (!TIFFAdvanceDirectory(tif, &nextdir, &off, &nextdirnum))
  2129. return (0);
  2130. }
  2131. /*
  2132. * Advance to the directory to be unlinked and fetch
  2133. * the offset of the directory that follows.
  2134. */
  2135. if (!TIFFAdvanceDirectory(tif, &nextdir, NULL, &nextdirnum))
  2136. return (0);
  2137. /*
  2138. * Go back and patch the link field of the preceding
  2139. * directory to point to the offset of the directory
  2140. * that follows.
  2141. */
  2142. (void)TIFFSeekFile(tif, off, SEEK_SET);
  2143. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2144. {
  2145. uint32_t nextdir32;
  2146. nextdir32 = (uint32_t)nextdir;
  2147. assert((uint64_t)nextdir32 == nextdir);
  2148. if (tif->tif_flags & TIFF_SWAB)
  2149. TIFFSwabLong(&nextdir32);
  2150. if (!WriteOK(tif, &nextdir32, sizeof(uint32_t)))
  2151. {
  2152. TIFFErrorExtR(tif, module, "Error writing directory link");
  2153. return (0);
  2154. }
  2155. }
  2156. else
  2157. {
  2158. if (tif->tif_flags & TIFF_SWAB)
  2159. TIFFSwabLong8(&nextdir);
  2160. if (!WriteOK(tif, &nextdir, sizeof(uint64_t)))
  2161. {
  2162. TIFFErrorExtR(tif, module, "Error writing directory link");
  2163. return (0);
  2164. }
  2165. }
  2166. /* For dirn=1 (first directory) also update the libtiff internal
  2167. * base offset variables. */
  2168. if (dirn == 1)
  2169. {
  2170. if (!(tif->tif_flags & TIFF_BIGTIFF))
  2171. tif->tif_header.classic.tiff_diroff = (uint32_t)nextdir;
  2172. else
  2173. tif->tif_header.big.tiff_diroff = nextdir;
  2174. }
  2175. /*
  2176. * Leave directory state setup safely. We don't have
  2177. * facilities for doing inserting and removing directories,
  2178. * so it's safest to just invalidate everything. This
  2179. * means that the caller can only append to the directory
  2180. * chain.
  2181. */
  2182. (*tif->tif_cleanup)(tif);
  2183. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  2184. {
  2185. _TIFFfreeExt(tif, tif->tif_rawdata);
  2186. tif->tif_rawdata = NULL;
  2187. tif->tif_rawcc = 0;
  2188. tif->tif_rawdataoff = 0;
  2189. tif->tif_rawdataloaded = 0;
  2190. }
  2191. tif->tif_flags &= ~(TIFF_BEENWRITING | TIFF_BUFFERSETUP | TIFF_POSTENCODE |
  2192. TIFF_BUF4WRITE);
  2193. TIFFFreeDirectory(tif);
  2194. TIFFDefaultDirectory(tif);
  2195. tif->tif_diroff = 0; /* force link on next write */
  2196. tif->tif_nextdiroff = 0; /* next write must be at end */
  2197. tif->tif_lastdiroff = 0; /* will be updated on next link */
  2198. tif->tif_curoff = 0;
  2199. tif->tif_row = (uint32_t)-1;
  2200. tif->tif_curstrip = (uint32_t)-1;
  2201. tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER;
  2202. _TIFFCleanupIFDOffsetAndNumberMaps(tif); /* invalidate IFD loop lists */
  2203. return (1);
  2204. }