PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/FreeImage/Source/LibTIFF/tif_dirread.c

https://bitbucket.org/cabalistic/ogredeps/
C | 2117 lines | 1537 code | 121 blank | 459 comment | 455 complexity | 59d28fb22a8c0615026dfdfaf9d54058 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /* $Id: tif_dirread.c,v 1.38 2011/04/10 17:14:09 drolon 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 Read Support Routines.
  29. */
  30. #include "tiffiop.h"
  31. #define IGNORE 0 /* tag placeholder used below */
  32. #ifdef HAVE_IEEEFP
  33. # define TIFFCvtIEEEFloatToNative(tif, n, fp)
  34. # define TIFFCvtIEEEDoubleToNative(tif, n, dp)
  35. #else
  36. extern void TIFFCvtIEEEFloatToNative(TIFF*, uint32, float*);
  37. extern void TIFFCvtIEEEDoubleToNative(TIFF*, uint32, double*);
  38. #endif
  39. static TIFFDirEntry* TIFFReadDirectoryFind(TIFFDirEntry* dir,
  40. uint16 dircount, uint16 tagid);
  41. static int EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
  42. static void MissingRequired(TIFF*, const char*);
  43. static int TIFFCheckDirOffset(TIFF*, toff_t);
  44. static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
  45. static uint16 TIFFFetchDirectory(TIFF*, toff_t, TIFFDirEntry**, toff_t *);
  46. static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
  47. static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);
  48. static float TIFFFetchRational(TIFF*, TIFFDirEntry*);
  49. static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*);
  50. static int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, uint16*);
  51. static int TIFFFetchPerSampleLongs(TIFF*, TIFFDirEntry*, uint32*);
  52. static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*, double*);
  53. static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
  54. static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
  55. static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
  56. static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
  57. static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);
  58. static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);
  59. static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
  60. static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);
  61. static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
  62. static void ChopUpSingleUncompressedStrip(TIFF*);
  63. /*
  64. * Read the next TIFF directory from a file and convert it to the internal
  65. * format. We read directories sequentially.
  66. */
  67. int
  68. TIFFReadDirectory(TIFF* tif)
  69. {
  70. static const char module[] = "TIFFReadDirectory";
  71. int n;
  72. TIFFDirectory* td;
  73. TIFFDirEntry *dp, *dir = NULL;
  74. uint16 iv;
  75. uint32 v;
  76. const TIFFFieldInfo* fip;
  77. size_t fix;
  78. uint16 dircount;
  79. uint16 previous_tag = 0;
  80. int diroutoforderwarning = 0, compressionknown = 0;
  81. int haveunknowntags = 0;
  82. tif->tif_diroff = tif->tif_nextdiroff;
  83. /*
  84. * Check whether we have the last offset or bad offset (IFD looping).
  85. */
  86. if (!TIFFCheckDirOffset(tif, tif->tif_nextdiroff))
  87. return 0;
  88. /*
  89. * Cleanup any previous compression state.
  90. */
  91. (*tif->tif_cleanup)(tif);
  92. tif->tif_curdir++;
  93. dircount = TIFFFetchDirectory(tif, tif->tif_nextdiroff,
  94. &dir, &tif->tif_nextdiroff);
  95. if (!dircount) {
  96. TIFFErrorExt(tif->tif_clientdata, module,
  97. "%s: Failed to read directory at offset %u",
  98. tif->tif_name, tif->tif_nextdiroff);
  99. return 0;
  100. }
  101. {
  102. TIFFDirEntry* ma;
  103. uint16 mb;
  104. for (ma=dir, mb=0; mb<dircount; ma++, mb++)
  105. {
  106. TIFFDirEntry* na;
  107. uint16 nb;
  108. for (na=ma+1, nb=mb+1; nb<dircount; na++, nb++)
  109. {
  110. if (ma->tdir_tag==na->tdir_tag)
  111. na->tdir_tag=IGNORE;
  112. }
  113. }
  114. }
  115. tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
  116. /*
  117. * Setup default value and then make a pass over
  118. * the fields to check type and tag information,
  119. * and to extract info required to size data
  120. * structures. A second pass is made afterwards
  121. * to read in everthing not taken in the first pass.
  122. */
  123. td = &tif->tif_dir;
  124. /* free any old stuff and reinit */
  125. TIFFFreeDirectory(tif);
  126. TIFFDefaultDirectory(tif);
  127. /*
  128. * Electronic Arts writes gray-scale TIFF files
  129. * without a PlanarConfiguration directory entry.
  130. * Thus we setup a default value here, even though
  131. * the TIFF spec says there is no default value.
  132. */
  133. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  134. /*
  135. * Sigh, we must make a separate pass through the
  136. * directory for the following reason:
  137. *
  138. * We must process the Compression tag in the first pass
  139. * in order to merge in codec-private tag definitions (otherwise
  140. * we may get complaints about unknown tags). However, the
  141. * Compression tag may be dependent on the SamplesPerPixel
  142. * tag value because older TIFF specs permited Compression
  143. * to be written as a SamplesPerPixel-count tag entry.
  144. * Thus if we don't first figure out the correct SamplesPerPixel
  145. * tag value then we may end up ignoring the Compression tag
  146. * value because it has an incorrect count value (if the
  147. * true value of SamplesPerPixel is not 1).
  148. *
  149. * It sure would have been nice if Aldus had really thought
  150. * this stuff through carefully.
  151. */
  152. for (dp = dir, n = dircount; n > 0; n--, dp++) {
  153. if (tif->tif_flags & TIFF_SWAB) {
  154. TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
  155. TIFFSwabArrayOfLong(&dp->tdir_count, 2);
  156. }
  157. if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) {
  158. if (!TIFFFetchNormalTag(tif, dp))
  159. goto bad;
  160. dp->tdir_tag = IGNORE;
  161. }
  162. }
  163. /*
  164. * First real pass over the directory.
  165. */
  166. fix = 0;
  167. for (dp = dir, n = dircount; n > 0; n--, dp++) {
  168. if (dp->tdir_tag == IGNORE)
  169. continue;
  170. /*
  171. * Silicon Beach (at least) writes unordered
  172. * directory tags (violating the spec). Handle
  173. * it here, but be obnoxious (maybe they'll fix it?).
  174. */
  175. if (dp->tdir_tag < previous_tag) {
  176. if (!diroutoforderwarning) {
  177. TIFFWarningExt(tif->tif_clientdata, module,
  178. "%s: invalid TIFF directory; tags are not sorted in ascending order",
  179. tif->tif_name);
  180. diroutoforderwarning = 1;
  181. }
  182. }
  183. previous_tag = dp->tdir_tag;
  184. if (fix >= tif->tif_nfields ||
  185. dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag)
  186. fix = 0; /* O(n^2) */
  187. while (fix < tif->tif_nfields &&
  188. tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
  189. fix++;
  190. if (fix >= tif->tif_nfields ||
  191. tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
  192. /* Unknown tag ... we'll deal with it below */
  193. haveunknowntags = 1;
  194. continue;
  195. }
  196. /*
  197. * Null out old tags that we ignore.
  198. */
  199. if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
  200. ignore:
  201. dp->tdir_tag = IGNORE;
  202. continue;
  203. }
  204. /*
  205. * Check data type.
  206. */
  207. fip = tif->tif_fieldinfo[fix];
  208. while (dp->tdir_type != (unsigned short) fip->field_type
  209. && fix < tif->tif_nfields) {
  210. if (fip->field_type == TIFF_ANY) /* wildcard */
  211. break;
  212. fip = tif->tif_fieldinfo[++fix];
  213. if (fix >= tif->tif_nfields ||
  214. fip->field_tag != dp->tdir_tag) {
  215. TIFFWarningExt(tif->tif_clientdata, module,
  216. "%s: wrong data type %d for \"%s\"; tag ignored",
  217. tif->tif_name, dp->tdir_type,
  218. tif->tif_fieldinfo[fix-1]->field_name);
  219. goto ignore;
  220. }
  221. }
  222. /*
  223. * Check count if known in advance.
  224. */
  225. if (fip->field_readcount != TIFF_VARIABLE
  226. && fip->field_readcount != TIFF_VARIABLE2) {
  227. uint32 expected = (fip->field_readcount == TIFF_SPP) ?
  228. (uint32) td->td_samplesperpixel :
  229. (uint32) fip->field_readcount;
  230. if (!CheckDirCount(tif, dp, expected))
  231. goto ignore;
  232. }
  233. switch (dp->tdir_tag) {
  234. case TIFFTAG_COMPRESSION:
  235. /*
  236. * The 5.0 spec says the Compression tag has
  237. * one value, while earlier specs say it has
  238. * one value per sample. Because of this, we
  239. * accept the tag if one value is supplied.
  240. */
  241. if (dp->tdir_count == 1) {
  242. v = TIFFExtractData(tif,
  243. dp->tdir_type, dp->tdir_offset);
  244. if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
  245. goto bad;
  246. else
  247. compressionknown = 1;
  248. break;
  249. /* XXX: workaround for broken TIFFs */
  250. } else if (dp->tdir_type == TIFF_LONG) {
  251. if (!TIFFFetchPerSampleLongs(tif, dp, &v) ||
  252. !TIFFSetField(tif, dp->tdir_tag, (uint16)v))
  253. goto bad;
  254. } else {
  255. if (!TIFFFetchPerSampleShorts(tif, dp, &iv)
  256. || !TIFFSetField(tif, dp->tdir_tag, iv))
  257. goto bad;
  258. }
  259. dp->tdir_tag = IGNORE;
  260. break;
  261. case TIFFTAG_STRIPOFFSETS:
  262. case TIFFTAG_STRIPBYTECOUNTS:
  263. case TIFFTAG_TILEOFFSETS:
  264. case TIFFTAG_TILEBYTECOUNTS:
  265. TIFFSetFieldBit(tif, fip->field_bit);
  266. break;
  267. case TIFFTAG_IMAGEWIDTH:
  268. case TIFFTAG_IMAGELENGTH:
  269. case TIFFTAG_IMAGEDEPTH:
  270. case TIFFTAG_TILELENGTH:
  271. case TIFFTAG_TILEWIDTH:
  272. case TIFFTAG_TILEDEPTH:
  273. case TIFFTAG_PLANARCONFIG:
  274. case TIFFTAG_ROWSPERSTRIP:
  275. case TIFFTAG_EXTRASAMPLES:
  276. if (!TIFFFetchNormalTag(tif, dp))
  277. goto bad;
  278. dp->tdir_tag = IGNORE;
  279. break;
  280. }
  281. }
  282. /*
  283. * If we saw any unknown tags, make an extra pass over the directory
  284. * to deal with them. This must be done separately because the tags
  285. * could have become known when we registered a codec after finding
  286. * the Compression tag. In a correctly-sorted directory there's
  287. * no problem because Compression will come before any codec-private
  288. * tags, but if the sorting is wrong that might not hold.
  289. */
  290. if (haveunknowntags) {
  291. fix = 0;
  292. for (dp = dir, n = dircount; n > 0; n--, dp++) {
  293. if (dp->tdir_tag == IGNORE)
  294. continue;
  295. if (fix >= tif->tif_nfields ||
  296. dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag)
  297. fix = 0; /* O(n^2) */
  298. while (fix < tif->tif_nfields &&
  299. tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
  300. fix++;
  301. if (fix >= tif->tif_nfields ||
  302. tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
  303. TIFFWarningExt(tif->tif_clientdata,
  304. module,
  305. "%s: unknown field with tag %d (0x%x) encountered",
  306. tif->tif_name,
  307. dp->tdir_tag,
  308. dp->tdir_tag);
  309. if (!_TIFFMergeFieldInfo(tif,
  310. _TIFFCreateAnonFieldInfo(tif,
  311. dp->tdir_tag,
  312. (TIFFDataType) dp->tdir_type),
  313. 1))
  314. {
  315. TIFFWarningExt(tif->tif_clientdata,
  316. module,
  317. "Registering anonymous field with tag %d (0x%x) failed",
  318. dp->tdir_tag,
  319. dp->tdir_tag);
  320. dp->tdir_tag = IGNORE;
  321. continue;
  322. }
  323. fix = 0;
  324. while (fix < tif->tif_nfields &&
  325. tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
  326. fix++;
  327. }
  328. /*
  329. * Check data type.
  330. */
  331. fip = tif->tif_fieldinfo[fix];
  332. while (dp->tdir_type != (unsigned short) fip->field_type
  333. && fix < tif->tif_nfields) {
  334. if (fip->field_type == TIFF_ANY) /* wildcard */
  335. break;
  336. fip = tif->tif_fieldinfo[++fix];
  337. if (fix >= tif->tif_nfields ||
  338. fip->field_tag != dp->tdir_tag) {
  339. TIFFWarningExt(tif->tif_clientdata, module,
  340. "%s: wrong data type %d for \"%s\"; tag ignored",
  341. tif->tif_name, dp->tdir_type,
  342. tif->tif_fieldinfo[fix-1]->field_name);
  343. dp->tdir_tag = IGNORE;
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. /*
  350. * XXX: OJPEG hack.
  351. * If a) compression is OJPEG, b) planarconfig tag says it's separate,
  352. * c) strip offsets/bytecounts tag are both present and
  353. * d) both contain exactly one value, then we consistently find
  354. * that the buggy implementation of the buggy compression scheme
  355. * matches contig planarconfig best. So we 'fix-up' the tag here
  356. */
  357. if ((td->td_compression==COMPRESSION_OJPEG) &&
  358. (td->td_planarconfig==PLANARCONFIG_SEPARATE)) {
  359. dp = TIFFReadDirectoryFind(dir,dircount,TIFFTAG_STRIPOFFSETS);
  360. if ((dp!=0) && (dp->tdir_count==1)) {
  361. dp = TIFFReadDirectoryFind(dir, dircount,
  362. TIFFTAG_STRIPBYTECOUNTS);
  363. if ((dp!=0) && (dp->tdir_count==1)) {
  364. td->td_planarconfig=PLANARCONFIG_CONTIG;
  365. TIFFWarningExt(tif->tif_clientdata,
  366. "TIFFReadDirectory",
  367. "Planarconfig tag value assumed incorrect, "
  368. "assuming data is contig instead of chunky");
  369. }
  370. }
  371. }
  372. /*
  373. * Allocate directory structure and setup defaults.
  374. */
  375. if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
  376. MissingRequired(tif, "ImageLength");
  377. goto bad;
  378. }
  379. /*
  380. * Setup appropriate structures (by strip or by tile)
  381. */
  382. if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
  383. td->td_nstrips = TIFFNumberOfStrips(tif);
  384. td->td_tilewidth = td->td_imagewidth;
  385. td->td_tilelength = td->td_rowsperstrip;
  386. td->td_tiledepth = td->td_imagedepth;
  387. tif->tif_flags &= ~TIFF_ISTILED;
  388. } else {
  389. td->td_nstrips = TIFFNumberOfTiles(tif);
  390. tif->tif_flags |= TIFF_ISTILED;
  391. }
  392. if (!td->td_nstrips) {
  393. TIFFErrorExt(tif->tif_clientdata, module,
  394. "%s: cannot handle zero number of %s",
  395. tif->tif_name, isTiled(tif) ? "tiles" : "strips");
  396. goto bad;
  397. }
  398. td->td_stripsperimage = td->td_nstrips;
  399. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  400. td->td_stripsperimage /= td->td_samplesperpixel;
  401. if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
  402. if ((td->td_compression==COMPRESSION_OJPEG) &&
  403. (isTiled(tif)==0) &&
  404. (td->td_nstrips==1)) {
  405. /*
  406. * XXX: OJPEG hack.
  407. * If a) compression is OJPEG, b) it's not a tiled TIFF,
  408. * and c) the number of strips is 1,
  409. * then we tolerate the absence of stripoffsets tag,
  410. * because, presumably, all required data is in the
  411. * JpegInterchangeFormat stream.
  412. */
  413. TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
  414. } else {
  415. MissingRequired(tif,
  416. isTiled(tif) ? "TileOffsets" : "StripOffsets");
  417. goto bad;
  418. }
  419. }
  420. /*
  421. * Second pass: extract other information.
  422. */
  423. for (dp = dir, n = dircount; n > 0; n--, dp++) {
  424. if (dp->tdir_tag == IGNORE)
  425. continue;
  426. switch (dp->tdir_tag) {
  427. case TIFFTAG_MINSAMPLEVALUE:
  428. case TIFFTAG_MAXSAMPLEVALUE:
  429. case TIFFTAG_BITSPERSAMPLE:
  430. case TIFFTAG_DATATYPE:
  431. case TIFFTAG_SAMPLEFORMAT:
  432. /*
  433. * The 5.0 spec says the Compression tag has
  434. * one value, while earlier specs say it has
  435. * one value per sample. Because of this, we
  436. * accept the tag if one value is supplied.
  437. *
  438. * The MinSampleValue, MaxSampleValue, BitsPerSample
  439. * DataType and SampleFormat tags are supposed to be
  440. * written as one value/sample, but some vendors
  441. * incorrectly write one value only -- so we accept
  442. * that as well (yech). Other vendors write correct
  443. * value for NumberOfSamples, but incorrect one for
  444. * BitsPerSample and friends, and we will read this
  445. * too.
  446. */
  447. if (dp->tdir_count == 1) {
  448. v = TIFFExtractData(tif,
  449. dp->tdir_type, dp->tdir_offset);
  450. if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
  451. goto bad;
  452. /* XXX: workaround for broken TIFFs */
  453. } else if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE
  454. && dp->tdir_type == TIFF_LONG) {
  455. if (!TIFFFetchPerSampleLongs(tif, dp, &v) ||
  456. !TIFFSetField(tif, dp->tdir_tag, (uint16)v))
  457. goto bad;
  458. } else {
  459. if (!TIFFFetchPerSampleShorts(tif, dp, &iv) ||
  460. !TIFFSetField(tif, dp->tdir_tag, iv))
  461. goto bad;
  462. }
  463. break;
  464. case TIFFTAG_SMINSAMPLEVALUE:
  465. {
  466. double minv = 0.0, maxv = 0.0;
  467. if (!TIFFFetchPerSampleAnys(tif, dp, &minv, &maxv) ||
  468. !TIFFSetField(tif, dp->tdir_tag, minv))
  469. goto bad;
  470. }
  471. break;
  472. case TIFFTAG_SMAXSAMPLEVALUE:
  473. {
  474. double minv = 0.0, maxv = 0.0;
  475. if (!TIFFFetchPerSampleAnys(tif, dp, &minv, &maxv) ||
  476. !TIFFSetField(tif, dp->tdir_tag, maxv))
  477. goto bad;
  478. }
  479. break;
  480. case TIFFTAG_STRIPOFFSETS:
  481. case TIFFTAG_TILEOFFSETS:
  482. if (!TIFFFetchStripThing(tif, dp,
  483. td->td_nstrips, &td->td_stripoffset))
  484. goto bad;
  485. break;
  486. case TIFFTAG_STRIPBYTECOUNTS:
  487. case TIFFTAG_TILEBYTECOUNTS:
  488. if (!TIFFFetchStripThing(tif, dp,
  489. td->td_nstrips, &td->td_stripbytecount))
  490. goto bad;
  491. break;
  492. case TIFFTAG_COLORMAP:
  493. case TIFFTAG_TRANSFERFUNCTION:
  494. {
  495. char* cp;
  496. /*
  497. * TransferFunction can have either 1x or 3x
  498. * data values; Colormap can have only 3x
  499. * items.
  500. */
  501. v = 1L<<td->td_bitspersample;
  502. if (dp->tdir_tag == TIFFTAG_COLORMAP ||
  503. dp->tdir_count != v) {
  504. if (!CheckDirCount(tif, dp, 3 * v))
  505. break;
  506. }
  507. v *= sizeof(uint16);
  508. cp = (char *)_TIFFCheckMalloc(tif,
  509. dp->tdir_count,
  510. sizeof (uint16),
  511. "to read \"TransferFunction\" tag");
  512. if (cp != NULL) {
  513. if (TIFFFetchData(tif, dp, cp)) {
  514. /*
  515. * This deals with there being
  516. * only one array to apply to
  517. * all samples.
  518. */
  519. uint32 c = 1L << td->td_bitspersample;
  520. if (dp->tdir_count == c)
  521. v = 0L;
  522. TIFFSetField(tif, dp->tdir_tag,
  523. cp, cp+v, cp+2*v);
  524. }
  525. _TIFFfree(cp);
  526. }
  527. break;
  528. }
  529. case TIFFTAG_PAGENUMBER:
  530. case TIFFTAG_HALFTONEHINTS:
  531. case TIFFTAG_YCBCRSUBSAMPLING:
  532. case TIFFTAG_DOTRANGE:
  533. (void) TIFFFetchShortPair(tif, dp);
  534. break;
  535. case TIFFTAG_REFERENCEBLACKWHITE:
  536. (void) TIFFFetchRefBlackWhite(tif, dp);
  537. break;
  538. /* BEGIN REV 4.0 COMPATIBILITY */
  539. case TIFFTAG_OSUBFILETYPE:
  540. v = 0L;
  541. switch (TIFFExtractData(tif, dp->tdir_type,
  542. dp->tdir_offset)) {
  543. case OFILETYPE_REDUCEDIMAGE:
  544. v = FILETYPE_REDUCEDIMAGE;
  545. break;
  546. case OFILETYPE_PAGE:
  547. v = FILETYPE_PAGE;
  548. break;
  549. }
  550. if (v)
  551. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, v);
  552. break;
  553. /* END REV 4.0 COMPATIBILITY */
  554. default:
  555. (void) TIFFFetchNormalTag(tif, dp);
  556. break;
  557. }
  558. }
  559. /*
  560. * OJPEG hack:
  561. * - If a) compression is OJPEG, and b) photometric tag is missing,
  562. * then we consistently find that photometric should be YCbCr
  563. * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
  564. * then we consistently find that the buggy implementation of the
  565. * buggy compression scheme matches photometric YCbCr instead.
  566. * - If a) compression is OJPEG, and b) bitspersample tag is missing,
  567. * then we consistently find bitspersample should be 8.
  568. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  569. * and c) photometric is RGB or YCbCr, then we consistently find
  570. * samplesperpixel should be 3
  571. * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
  572. * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
  573. * find samplesperpixel should be 3
  574. */
  575. if (td->td_compression==COMPRESSION_OJPEG)
  576. {
  577. if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
  578. {
  579. TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
  580. "Photometric tag is missing, assuming data is YCbCr");
  581. if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
  582. goto bad;
  583. }
  584. else if (td->td_photometric==PHOTOMETRIC_RGB)
  585. {
  586. td->td_photometric=PHOTOMETRIC_YCBCR;
  587. TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
  588. "Photometric tag value assumed incorrect, "
  589. "assuming data is YCbCr instead of RGB");
  590. }
  591. if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
  592. {
  593. TIFFWarningExt(tif->tif_clientdata,"TIFFReadDirectory",
  594. "BitsPerSample tag is missing, assuming 8 bits per sample");
  595. if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
  596. goto bad;
  597. }
  598. if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
  599. {
  600. if (td->td_photometric==PHOTOMETRIC_RGB)
  601. {
  602. TIFFWarningExt(tif->tif_clientdata,
  603. "TIFFReadDirectory",
  604. "SamplesPerPixel tag is missing, "
  605. "assuming correct SamplesPerPixel value is 3");
  606. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
  607. goto bad;
  608. }
  609. if (td->td_photometric==PHOTOMETRIC_YCBCR)
  610. {
  611. TIFFWarningExt(tif->tif_clientdata,
  612. "TIFFReadDirectory",
  613. "SamplesPerPixel tag is missing, "
  614. "applying correct SamplesPerPixel value of 3");
  615. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
  616. goto bad;
  617. }
  618. else if ((td->td_photometric==PHOTOMETRIC_MINISWHITE)
  619. || (td->td_photometric==PHOTOMETRIC_MINISBLACK))
  620. {
  621. /*
  622. * SamplesPerPixel tag is missing, but is not required
  623. * by spec. Assume correct SamplesPerPixel value of 1.
  624. */
  625. if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
  626. goto bad;
  627. }
  628. }
  629. }
  630. /*
  631. * Verify Palette image has a Colormap.
  632. */
  633. if (td->td_photometric == PHOTOMETRIC_PALETTE &&
  634. !TIFFFieldSet(tif, FIELD_COLORMAP)) {
  635. if ( tif->tif_dir.td_bitspersample>=8 && tif->tif_dir.td_samplesperpixel==3)
  636. tif->tif_dir.td_photometric = PHOTOMETRIC_RGB;
  637. else if (tif->tif_dir.td_bitspersample>=8)
  638. tif->tif_dir.td_photometric = PHOTOMETRIC_MINISBLACK;
  639. else {
  640. MissingRequired(tif, "Colormap");
  641. goto bad;
  642. }
  643. }
  644. /*
  645. * OJPEG hack:
  646. * We do no further messing with strip/tile offsets/bytecounts in OJPEG
  647. * TIFFs
  648. */
  649. if (td->td_compression!=COMPRESSION_OJPEG)
  650. {
  651. /*
  652. * Attempt to deal with a missing StripByteCounts tag.
  653. */
  654. if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
  655. /*
  656. * Some manufacturers violate the spec by not giving
  657. * the size of the strips. In this case, assume there
  658. * is one uncompressed strip of data.
  659. */
  660. if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
  661. td->td_nstrips > 1) ||
  662. (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  663. td->td_nstrips != td->td_samplesperpixel)) {
  664. MissingRequired(tif, "StripByteCounts");
  665. goto bad;
  666. }
  667. TIFFWarningExt(tif->tif_clientdata, module,
  668. "%s: TIFF directory is missing required "
  669. "\"%s\" field, calculating from imagelength",
  670. tif->tif_name,
  671. _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
  672. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  673. goto bad;
  674. /*
  675. * Assume we have wrong StripByteCount value (in case
  676. * of single strip) in following cases:
  677. * - it is equal to zero along with StripOffset;
  678. * - it is larger than file itself (in case of uncompressed
  679. * image);
  680. * - it is smaller than the size of the bytes per row
  681. * multiplied on the number of rows. The last case should
  682. * not be checked in the case of writing new image,
  683. * because we may do not know the exact strip size
  684. * until the whole image will be written and directory
  685. * dumped out.
  686. */
  687. #define BYTECOUNTLOOKSBAD \
  688. ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
  689. (td->td_compression == COMPRESSION_NONE && \
  690. td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
  691. (tif->tif_mode == O_RDONLY && \
  692. td->td_compression == COMPRESSION_NONE && \
  693. td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
  694. } else if (td->td_nstrips == 1
  695. && td->td_stripoffset[0] != 0
  696. && BYTECOUNTLOOKSBAD) {
  697. /*
  698. * XXX: Plexus (and others) sometimes give a value of
  699. * zero for a tag when they don't know what the
  700. * correct value is! Try and handle the simple case
  701. * of estimating the size of a one strip image.
  702. */
  703. TIFFWarningExt(tif->tif_clientdata, module,
  704. "%s: Bogus \"%s\" field, ignoring and calculating from imagelength",
  705. tif->tif_name,
  706. _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
  707. if(EstimateStripByteCounts(tif, dir, dircount) < 0)
  708. goto bad;
  709. } else if (td->td_planarconfig == PLANARCONFIG_CONTIG
  710. && td->td_nstrips > 2
  711. && td->td_compression == COMPRESSION_NONE
  712. && td->td_stripbytecount[0] != td->td_stripbytecount[1]
  713. && td->td_stripbytecount[0] != 0
  714. && td->td_stripbytecount[1] != 0 ) {
  715. /*
  716. * XXX: Some vendors fill StripByteCount array with
  717. * absolutely wrong values (it can be equal to
  718. * StripOffset array, for example). Catch this case
  719. * here.
  720. */
  721. TIFFWarningExt(tif->tif_clientdata, module,
  722. "%s: Wrong \"%s\" field, ignoring and calculating from imagelength",
  723. tif->tif_name,
  724. _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
  725. if (EstimateStripByteCounts(tif, dir, dircount) < 0)
  726. goto bad;
  727. }
  728. }
  729. if (dir) {
  730. _TIFFfree((char *)dir);
  731. dir = NULL;
  732. }
  733. if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
  734. td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1);
  735. /*
  736. * Setup default compression scheme.
  737. */
  738. /*
  739. * XXX: We can optimize checking for the strip bounds using the sorted
  740. * bytecounts array. See also comments for TIFFAppendToStrip()
  741. * function in tif_write.c.
  742. */
  743. if (td->td_nstrips > 1) {
  744. tstrip_t strip;
  745. td->td_stripbytecountsorted = 1;
  746. for (strip = 1; strip < td->td_nstrips; strip++) {
  747. if (td->td_stripoffset[strip - 1] >
  748. td->td_stripoffset[strip]) {
  749. td->td_stripbytecountsorted = 0;
  750. break;
  751. }
  752. }
  753. }
  754. if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
  755. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  756. /*
  757. * Some manufacturers make life difficult by writing
  758. * large amounts of uncompressed data as a single strip.
  759. * This is contrary to the recommendations of the spec.
  760. * The following makes an attempt at breaking such images
  761. * into strips closer to the recommended 8k bytes. A
  762. * side effect, however, is that the RowsPerStrip tag
  763. * value may be changed.
  764. */
  765. if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&
  766. (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)
  767. ChopUpSingleUncompressedStrip(tif);
  768. /*
  769. * Reinitialize i/o since we are starting on a new directory.
  770. */
  771. tif->tif_row = (uint32) -1;
  772. tif->tif_curstrip = (tstrip_t) -1;
  773. tif->tif_col = (uint32) -1;
  774. tif->tif_curtile = (ttile_t) -1;
  775. tif->tif_tilesize = (tsize_t) -1;
  776. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  777. if (!tif->tif_scanlinesize) {
  778. TIFFErrorExt(tif->tif_clientdata, module,
  779. "%s: cannot handle zero scanline size",
  780. tif->tif_name);
  781. return (0);
  782. }
  783. if (isTiled(tif)) {
  784. tif->tif_tilesize = TIFFTileSize(tif);
  785. if (!tif->tif_tilesize) {
  786. TIFFErrorExt(tif->tif_clientdata, module,
  787. "%s: cannot handle zero tile size",
  788. tif->tif_name);
  789. return (0);
  790. }
  791. } else {
  792. if (!TIFFStripSize(tif)) {
  793. TIFFErrorExt(tif->tif_clientdata, module,
  794. "%s: cannot handle zero strip size",
  795. tif->tif_name);
  796. return (0);
  797. }
  798. }
  799. return (1);
  800. bad:
  801. if (dir)
  802. _TIFFfree(dir);
  803. return (0);
  804. }
  805. static TIFFDirEntry*
  806. TIFFReadDirectoryFind(TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
  807. {
  808. TIFFDirEntry* m;
  809. uint16 n;
  810. for (m=dir, n=0; n<dircount; m++, n++)
  811. {
  812. if (m->tdir_tag==tagid)
  813. return(m);
  814. }
  815. return(0);
  816. }
  817. /*
  818. * Read custom directory from the arbitarry offset.
  819. * The code is very similar to TIFFReadDirectory().
  820. */
  821. int
  822. TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
  823. const TIFFFieldInfo info[], size_t n)
  824. {
  825. static const char module[] = "TIFFReadCustomDirectory";
  826. TIFFDirectory* td = &tif->tif_dir;
  827. TIFFDirEntry *dp, *dir = NULL;
  828. const TIFFFieldInfo* fip;
  829. size_t fix;
  830. uint16 i, dircount;
  831. _TIFFSetupFieldInfo(tif, info, n);
  832. dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
  833. if (!dircount) {
  834. TIFFErrorExt(tif->tif_clientdata, module,
  835. "%s: Failed to read custom directory at offset %u",
  836. tif->tif_name, diroff);
  837. return 0;
  838. }
  839. TIFFFreeDirectory(tif);
  840. _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
  841. fix = 0;
  842. for (dp = dir, i = dircount; i > 0; i--, dp++) {
  843. if (tif->tif_flags & TIFF_SWAB) {
  844. TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
  845. TIFFSwabArrayOfLong(&dp->tdir_count, 2);
  846. }
  847. if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
  848. continue;
  849. while (fix < tif->tif_nfields &&
  850. tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
  851. fix++;
  852. if (fix >= tif->tif_nfields ||
  853. tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
  854. TIFFWarningExt(tif->tif_clientdata, module,
  855. "%s: unknown field with tag %d (0x%x) encountered",
  856. tif->tif_name, dp->tdir_tag, dp->tdir_tag);
  857. if (!_TIFFMergeFieldInfo(tif,
  858. _TIFFCreateAnonFieldInfo(tif,
  859. dp->tdir_tag,
  860. (TIFFDataType) dp->tdir_type),
  861. 1))
  862. {
  863. TIFFWarningExt(tif->tif_clientdata, module,
  864. "Registering anonymous field with tag %d (0x%x) failed",
  865. dp->tdir_tag, dp->tdir_tag);
  866. goto ignore;
  867. }
  868. fix = 0;
  869. while (fix < tif->tif_nfields &&
  870. tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
  871. fix++;
  872. }
  873. /*
  874. * Null out old tags that we ignore.
  875. */
  876. if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
  877. ignore:
  878. dp->tdir_tag = IGNORE;
  879. continue;
  880. }
  881. /*
  882. * Check data type.
  883. */
  884. fip = tif->tif_fieldinfo[fix];
  885. while (dp->tdir_type != (unsigned short) fip->field_type
  886. && fix < tif->tif_nfields) {
  887. if (fip->field_type == TIFF_ANY) /* wildcard */
  888. break;
  889. fip = tif->tif_fieldinfo[++fix];
  890. if (fix >= tif->tif_nfields ||
  891. fip->field_tag != dp->tdir_tag) {
  892. TIFFWarningExt(tif->tif_clientdata, module,
  893. "%s: wrong data type %d for \"%s\"; tag ignored",
  894. tif->tif_name, dp->tdir_type,
  895. tif->tif_fieldinfo[fix-1]->field_name);
  896. goto ignore;
  897. }
  898. }
  899. /*
  900. * Check count if known in advance.
  901. */
  902. if (fip->field_readcount != TIFF_VARIABLE
  903. && fip->field_readcount != TIFF_VARIABLE2) {
  904. uint32 expected = (fip->field_readcount == TIFF_SPP) ?
  905. (uint32) td->td_samplesperpixel :
  906. (uint32) fip->field_readcount;
  907. if (!CheckDirCount(tif, dp, expected))
  908. goto ignore;
  909. }
  910. /*
  911. * EXIF tags which need to be specifically processed.
  912. */
  913. switch (dp->tdir_tag) {
  914. case EXIFTAG_SUBJECTDISTANCE:
  915. (void) TIFFFetchSubjectDistance(tif, dp);
  916. break;
  917. default:
  918. (void) TIFFFetchNormalTag(tif, dp);
  919. break;
  920. }
  921. }
  922. if (dir)
  923. _TIFFfree(dir);
  924. return 1;
  925. }
  926. /*
  927. * EXIF is important special case of custom IFD, so we have a special
  928. * function to read it.
  929. */
  930. int
  931. TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
  932. {
  933. size_t exifFieldInfoCount;
  934. const TIFFFieldInfo *exifFieldInfo =
  935. _TIFFGetExifFieldInfo(&exifFieldInfoCount);
  936. return TIFFReadCustomDirectory(tif, diroff, exifFieldInfo,
  937. exifFieldInfoCount);
  938. }
  939. static int
  940. EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
  941. {
  942. static const char module[] = "EstimateStripByteCounts";
  943. TIFFDirEntry *dp;
  944. TIFFDirectory *td = &tif->tif_dir;
  945. uint32 strip;
  946. if (td->td_stripbytecount)
  947. _TIFFfree(td->td_stripbytecount);
  948. td->td_stripbytecount = (uint32*)
  949. _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint32),
  950. "for \"StripByteCounts\" array");
  951. if( td->td_stripbytecount == NULL )
  952. return -1;
  953. if (td->td_compression != COMPRESSION_NONE) {
  954. uint32 space = (uint32)(sizeof (TIFFHeader)
  955. + sizeof (uint16)
  956. + (dircount * sizeof (TIFFDirEntry))
  957. + sizeof (uint32));
  958. toff_t filesize = TIFFGetFileSize(tif);
  959. uint16 n;
  960. /* calculate amount of space used by indirect values */
  961. for (dp = dir, n = dircount; n > 0; n--, dp++)
  962. {
  963. uint32 cc = TIFFDataWidth((TIFFDataType) dp->tdir_type);
  964. if (cc == 0) {
  965. TIFFErrorExt(tif->tif_clientdata, module,
  966. "%s: Cannot determine size of unknown tag type %d",
  967. tif->tif_name, dp->tdir_type);
  968. return -1;
  969. }
  970. cc = cc * dp->tdir_count;
  971. if (cc > sizeof (uint32))
  972. space += cc;
  973. }
  974. space = filesize - space;
  975. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  976. space /= td->td_samplesperpixel;
  977. for (strip = 0; strip < td->td_nstrips; strip++)
  978. td->td_stripbytecount[strip] = space;
  979. /*
  980. * This gross hack handles the case were the offset to
  981. * the last strip is past the place where we think the strip
  982. * should begin. Since a strip of data must be contiguous,
  983. * it's safe to assume that we've overestimated the amount
  984. * of data in the strip and trim this number back accordingly.
  985. */
  986. strip--;
  987. if (((toff_t)(td->td_stripoffset[strip]+
  988. td->td_stripbytecount[strip])) > filesize)
  989. td->td_stripbytecount[strip] =
  990. filesize - td->td_stripoffset[strip];
  991. } else if (isTiled(tif)) {
  992. uint32 bytespertile = TIFFTileSize(tif);
  993. for (strip = 0; strip < td->td_nstrips; strip++)
  994. td->td_stripbytecount[strip] = bytespertile;
  995. } else {
  996. uint32 rowbytes = TIFFScanlineSize(tif);
  997. uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
  998. for (strip = 0; strip < td->td_nstrips; strip++)
  999. td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
  1000. }
  1001. TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
  1002. if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
  1003. td->td_rowsperstrip = td->td_imagelength;
  1004. return 1;
  1005. }
  1006. static void
  1007. MissingRequired(TIFF* tif, const char* tagname)
  1008. {
  1009. static const char module[] = "MissingRequired";
  1010. TIFFErrorExt(tif->tif_clientdata, module,
  1011. "%s: TIFF directory is missing required \"%s\" field",
  1012. tif->tif_name, tagname);
  1013. }
  1014. /*
  1015. * Check the directory offset against the list of already seen directory
  1016. * offsets. This is a trick to prevent IFD looping. The one can create TIFF
  1017. * file with looped directory pointers. We will maintain a list of already
  1018. * seen directories and check every IFD offset against that list.
  1019. */
  1020. static int
  1021. TIFFCheckDirOffset(TIFF* tif, toff_t diroff)
  1022. {
  1023. uint16 n;
  1024. if (diroff == 0) /* no more directories */
  1025. return 0;
  1026. for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
  1027. if (tif->tif_dirlist[n] == diroff)
  1028. return 0;
  1029. }
  1030. tif->tif_dirnumber++;
  1031. if (tif->tif_dirnumber > tif->tif_dirlistsize) {
  1032. toff_t* new_dirlist;
  1033. /*
  1034. * XXX: Reduce memory allocation granularity of the dirlist
  1035. * array.
  1036. */
  1037. new_dirlist = (toff_t *)_TIFFCheckRealloc(tif,
  1038. tif->tif_dirlist,
  1039. tif->tif_dirnumber,
  1040. 2 * sizeof(toff_t),
  1041. "for IFD list");
  1042. if (!new_dirlist)
  1043. return 0;
  1044. tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
  1045. tif->tif_dirlist = new_dirlist;
  1046. }
  1047. tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
  1048. return 1;
  1049. }
  1050. /*
  1051. * Check the count field of a directory entry against a known value. The
  1052. * caller is expected to skip/ignore the tag if there is a mismatch.
  1053. */
  1054. static int
  1055. CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
  1056. {
  1057. if (count > dir->tdir_count) {
  1058. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  1059. "incorrect count for field \"%s\" (%u, expecting %u); tag ignored",
  1060. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
  1061. dir->tdir_count, count);
  1062. return (0);
  1063. } else if (count < dir->tdir_count) {
  1064. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  1065. "incorrect count for field \"%s\" (%u, expecting %u); tag trimmed",
  1066. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
  1067. dir->tdir_count, count);
  1068. dir->tdir_count = count;
  1069. return (1);
  1070. }
  1071. return (1);
  1072. }
  1073. /*
  1074. * Read IFD structure from the specified offset. If the pointer to
  1075. * nextdiroff variable has been specified, read it too. Function returns a
  1076. * number of fields in the directory or 0 if failed.
  1077. */
  1078. static uint16
  1079. TIFFFetchDirectory(TIFF* tif, toff_t diroff, TIFFDirEntry **pdir,
  1080. toff_t *nextdiroff)
  1081. {
  1082. static const char module[] = "TIFFFetchDirectory";
  1083. TIFFDirEntry *dir;
  1084. uint16 dircount;
  1085. assert(pdir);
  1086. tif->tif_diroff = diroff;
  1087. if (nextdiroff)
  1088. *nextdiroff = 0;
  1089. if (!isMapped(tif)) {
  1090. if (!SeekOK(tif, tif->tif_diroff)) {
  1091. TIFFErrorExt(tif->tif_clientdata, module,
  1092. "%s: Seek error accessing TIFF directory",
  1093. tif->tif_name);
  1094. return 0;
  1095. }
  1096. if (!ReadOK(tif, &dircount, sizeof (uint16))) {
  1097. TIFFErrorExt(tif->tif_clientdata, module,
  1098. "%s: Can not read TIFF directory count",
  1099. tif->tif_name);
  1100. return 0;
  1101. }
  1102. if (tif->tif_flags & TIFF_SWAB)
  1103. TIFFSwabShort(&dircount);
  1104. dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
  1105. sizeof (TIFFDirEntry),
  1106. "to read TIFF directory");
  1107. if (dir == NULL)
  1108. return 0;
  1109. if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
  1110. TIFFErrorExt(tif->tif_clientdata, module,
  1111. "%.100s: Can not read TIFF directory",
  1112. tif->tif_name);
  1113. _TIFFfree(dir);
  1114. return 0;
  1115. }
  1116. /*
  1117. * Read offset to next directory for sequential scans if
  1118. * needed.
  1119. */
  1120. if (nextdiroff)
  1121. (void) ReadOK(tif, nextdiroff, sizeof(uint32));
  1122. } else {
  1123. toff_t off = tif->tif_diroff;
  1124. /*
  1125. * Check for integer overflow when validating the dir_off,
  1126. * otherwise a very high offset may cause an OOB read and
  1127. * crash the client. Make two comparisons instead of
  1128. *
  1129. * off + sizeof(uint16) > tif->tif_size
  1130. *
  1131. * to avoid overflow.
  1132. */
  1133. if (tif->tif_size < sizeof (uint16) ||
  1134. off > tif->tif_size - sizeof(uint16)) {
  1135. TIFFErrorExt(tif->tif_clientdata, module,
  1136. "%s: Can not read TIFF directory count",
  1137. tif->tif_name);
  1138. return 0;
  1139. } else {
  1140. _TIFFmemcpy(&dircount, tif->tif_base + off,
  1141. sizeof(uint16));
  1142. }
  1143. off += sizeof (uint16);
  1144. if (tif->tif_flags & TIFF_SWAB)
  1145. TIFFSwabShort(&dircount);
  1146. dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
  1147. sizeof(TIFFDirEntry),
  1148. "to read TIFF directory");
  1149. if (dir == NULL)
  1150. return 0;
  1151. if (off + dircount * sizeof (TIFFDirEntry) > tif->tif_size) {
  1152. TIFFErrorExt(tif->tif_clientdata, module,
  1153. "%s: Can not read TIFF directory",
  1154. tif->tif_name);
  1155. _TIFFfree(dir);
  1156. return 0;
  1157. } else {
  1158. _TIFFmemcpy(dir, tif->tif_base + off,
  1159. dircount * sizeof(TIFFDirEntry));
  1160. }
  1161. if (nextdiroff) {
  1162. off += dircount * sizeof (TIFFDirEntry);
  1163. if (off + sizeof (uint32) <= tif->tif_size) {
  1164. _TIFFmemcpy(nextdiroff, tif->tif_base + off,
  1165. sizeof (uint32));
  1166. }
  1167. }
  1168. }
  1169. if (nextdiroff && tif->tif_flags & TIFF_SWAB)
  1170. TIFFSwabLong(nextdiroff);
  1171. *pdir = dir;
  1172. return dircount;
  1173. }
  1174. /*
  1175. * Fetch a contiguous directory item.
  1176. */
  1177. static tsize_t
  1178. TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
  1179. {
  1180. uint32 w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
  1181. /*
  1182. * FIXME: butecount should have tsize_t type, but for now libtiff
  1183. * defines tsize_t as a signed 32-bit integer and we are losing
  1184. * ability to read arrays larger than 2^31 bytes. So we are using
  1185. * uint32 instead of tsize_t here.
  1186. */
  1187. uint32 cc = dir->tdir_count * w;
  1188. /* Check for overflow. */
  1189. if (!dir->tdir_count || !w || cc / w != dir->tdir_count)
  1190. goto bad;
  1191. if (!isMapped(tif)) {
  1192. if (!SeekOK(tif, dir->tdir_offset))
  1193. goto bad;
  1194. if (!ReadOK(tif, cp, cc))
  1195. goto bad;
  1196. } else {
  1197. /* Check for overflow. */
  1198. if (dir->tdir_offset + cc < dir->tdir_offset
  1199. || dir->tdir_offset + cc < cc
  1200. || dir->tdir_offset + cc > tif->tif_size)
  1201. goto bad;
  1202. _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
  1203. }
  1204. if (tif->tif_flags & TIFF_SWAB) {
  1205. switch (dir->tdir_type) {
  1206. case TIFF_SHORT:
  1207. case TIFF_SSHORT:
  1208. TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
  1209. break;
  1210. case TIFF_LONG:
  1211. case TIFF_SLONG:
  1212. case TIFF_FLOAT:
  1213. TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
  1214. break;
  1215. case TIFF_RATIONAL:
  1216. case TIFF_SRATIONAL:
  1217. TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
  1218. break;
  1219. case TIFF_DOUBLE:
  1220. TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
  1221. break;
  1222. }
  1223. }
  1224. return (cc);
  1225. bad:
  1226. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1227. "Error fetching data for field \"%s\"",
  1228. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  1229. return (tsize_t) 0;
  1230. }
  1231. /*
  1232. * Fetch an ASCII item from the file.
  1233. */
  1234. static tsize_t
  1235. TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp)
  1236. {
  1237. if (dir->tdir_count <= 4) {
  1238. uint32 l = dir->tdir_offset;
  1239. if (tif->tif_flags & TIFF_SWAB)
  1240. TIFFSwabLong(&l);
  1241. _TIFFmemcpy(cp, &l, dir->tdir_count);
  1242. return (1);
  1243. }
  1244. return (TIFFFetchData(tif, dir, cp));
  1245. }
  1246. /*
  1247. * Convert numerator+denominator to float.
  1248. */
  1249. static int
  1250. cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
  1251. {
  1252. if (denom == 0) {
  1253. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1254. "%s: Rational with zero denominator (num = %u)",
  1255. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
  1256. return (0);
  1257. } else {
  1258. if (dir->tdir_type == TIFF_RATIONAL)
  1259. *rv = ((float)num / (float)denom);
  1260. else
  1261. *rv = ((float)(int32)num / (float)(int32)denom);
  1262. return (1);
  1263. }
  1264. }
  1265. /*
  1266. * Fetch a rational item from the file at offset off and return the value as a
  1267. * floating point number.
  1268. */
  1269. static float
  1270. TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
  1271. {
  1272. uint32 l[2];
  1273. float v;
  1274. return (!TIFFFetchData(tif, dir, (char *)l) ||
  1275. !cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v);
  1276. }
  1277. /*
  1278. * Fetch a single floating point value from the offset field and return it as
  1279. * a native float.
  1280. */
  1281. static float
  1282. TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
  1283. {
  1284. float v;
  1285. int32 l = TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
  1286. _TIFFmemcpy(&v, &l, sizeof(float));
  1287. TIFFCvtIEEEFloatToNative(tif, 1, &v);
  1288. return (v);
  1289. }
  1290. /*
  1291. * Fetch an array of BYTE or SBYTE values.
  1292. */
  1293. static int
  1294. TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint8* v)
  1295. {
  1296. if (dir->tdir_count <= 4) {
  1297. /*
  1298. * Extract data from offset field.
  1299. */
  1300. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  1301. if (dir->tdir_type == TIFF_SBYTE)
  1302. switch (dir->tdir_count) {
  1303. case 4: v[3] = dir->tdir_offset & 0xff;
  1304. case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
  1305. case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
  1306. case 1: v[0] = dir->tdir_offset >> 24;
  1307. }
  1308. else
  1309. switch (dir->tdir_count) {
  1310. case 4: v[3] = dir->tdir_offset & 0xff;
  1311. case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
  1312. case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
  1313. case 1: v[0] = dir->tdir_offset >> 24;
  1314. }
  1315. } else {
  1316. if (dir->tdir_type == TIFF_SBYTE)
  1317. switch (dir->tdir_count) {
  1318. case 4: v[3] = dir->tdir_offset >> 24;
  1319. case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
  1320. case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
  1321. case 1: v[0] = dir->tdir_offset & 0xff;
  1322. }
  1323. else
  1324. switch (dir->tdir_count) {
  1325. case 4: v[3] = dir->tdir_offset >> 24;
  1326. case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
  1327. case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
  1328. case 1: v[0] = dir->tdir_offset & 0xff;
  1329. }
  1330. }
  1331. return (1);
  1332. } else
  1333. return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */
  1334. }
  1335. /*
  1336. * Fetch an array of SHORT or SSHORT values.
  1337. */
  1338. static int
  1339. TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
  1340. {
  1341. if (dir->tdir_count <= 2) {
  1342. if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
  1343. switch (dir->tdir_count) {
  1344. case 2: v[1] = (uint16) (dir->tdir_offset & 0xffff);
  1345. case 1: v[0] = (uint16) (dir->tdir_offset >> 16);
  1346. }
  1347. } else {
  1348. switch (dir->tdir_count) {
  1349. case 2: v[1] = (uint16) (dir->tdir_offset >> 16);
  1350. case 1: v[0] = (uint16) (dir->tdir_offset & 0xffff);
  1351. }
  1352. }
  1353. return (1);
  1354. } else
  1355. return (TIFFFetchData(tif, dir, (char *)v) != 0);
  1356. }
  1357. /*
  1358. * Fetch a pair of SHORT or BYTE values. Some tags may have either BYTE
  1359. * or SHORT type and this function works with both ones.
  1360. */
  1361. static int
  1362. TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
  1363. {
  1364. /*
  1365. * Prevent overflowing the v stack arrays below by performing a sanity
  1366. * check on tdir_count, this should never be greater than two.
  1367. */
  1368. if (dir->tdir_count > 2) {
  1369. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  1370. "unexpected count for field \"%s\", %u, expected 2; ignored",
  1371. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
  1372. dir->tdir_count);
  1373. return 0;
  1374. }
  1375. switch (dir->tdir_type) {
  1376. case TIFF_BYTE:
  1377. case TIFF_SBYTE:
  1378. {
  1379. uint8 v[4];
  1380. return TIFFFetchByteArray(tif, dir, v)
  1381. && TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
  1382. }
  1383. case TIFF_SHORT:
  1384. case TIFF_SSHORT:
  1385. {
  1386. uint16 v[2];
  1387. return TIFFFetchShortArray(tif, dir, v)
  1388. && TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
  1389. }
  1390. default:
  1391. return 0;
  1392. }
  1393. }
  1394. /*
  1395. * Fetch an array of LONG or SLONG values.
  1396. */
  1397. static int
  1398. TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
  1399. {
  1400. if (dir->tdir_count == 1) {
  1401. v[0] = dir->tdir_offset;
  1402. return (1);
  1403. } else
  1404. return (TIFFFetchData(tif, dir, (char*) v) != 0);
  1405. }
  1406. /*
  1407. * Fetch an array of RATIONAL or SRATIONAL values.
  1408. */
  1409. static int
  1410. TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
  1411. {
  1412. int ok = 0;
  1413. uint32* l;
  1414. l = (uint32*)_TIFFCheckMalloc(tif,
  1415. dir->tdir_count, TIFFDataWidth((TIFFDataType) dir->tdir_type),
  1416. "to fetch array of rationals");
  1417. if (l) {
  1418. if (TIFFFetchData(tif, dir, (char *)l)) {
  1419. uint32 i;
  1420. for (i = 0; i < dir->tdir_count; i++) {
  1421. ok = cvtRational(tif, dir,
  1422. l[2*i+0], l[2*i+1], &v[i]);
  1423. if (!ok)
  1424. break;
  1425. }
  1426. }
  1427. _TIFFfree((char *)l);
  1428. }
  1429. return (ok);
  1430. }
  1431. /*
  1432. * Fetch an array of FLOAT values.
  1433. */
  1434. static int
  1435. TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
  1436. {
  1437. if (dir->tdir_count == 1) {
  1438. union
  1439. {
  1440. float f;
  1441. uint32 i;
  1442. } float_union;
  1443. float_union.i=dir->tdir_offset;
  1444. v[0]=float_union.f;
  1445. TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
  1446. return (1);
  1447. } else if (TIFFFetchData(tif, dir, (char*) v)) {
  1448. TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
  1449. return (1);
  1450. } else
  1451. return (0);
  1452. }
  1453. /*
  1454. * Fetch an array of DOUBLE values.
  1455. */
  1456. static int
  1457. TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
  1458. {
  1459. if (TIFFFetchData(tif, dir, (char*) v)) {
  1460. TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v);
  1461. return (1);
  1462. } else
  1463. return (0);
  1464. }
  1465. /*
  1466. * Fetch an array of ANY values. The actual values are returned as doubles
  1467. * which should be able hold all the types. Yes, there really should be an
  1468. * tany_t to avoid this potential non-portability ... Note in particular that
  1469. * we assume that the double return value vector is large enough to read in
  1470. * any fundamental type. We use that vector as a buffer to read in the base
  1471. * type vector and then convert it in place to double (from end to front of
  1472. * course).
  1473. */
  1474. static int
  1475. TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
  1476. {
  1477. int i;
  1478. switch (dir->tdir_type) {
  1479. case TIFF_BYTE:
  1480. case TIFF_SBYTE:
  1481. if (!TIFFFetchByteArray(tif, dir, (uint8*) v))
  1482. return (0);
  1483. if (dir->tdir_type == TIFF_BYTE) {
  1484. uint8* vp = (uint8*) v;
  1485. for (i = dir->tdir_count-1; i >= 0; i--)
  1486. v[i] = vp[i];
  1487. } else {
  1488. int8* vp = (int8*) v;
  1489. for (i = dir->tdir_count-1; i >= 0; i--)
  1490. v[i] = vp[i];
  1491. }
  1492. break;
  1493. case TIFF_SHORT:
  1494. case TIFF_SSHORT:
  1495. if (!TIFFFetchShortArray(tif, dir, (uint16*) v))
  1496. return (0);
  1497. if (dir->tdir_type == TIFF_SHORT) {
  1498. uint16* vp = (uint16*) v;
  1499. for (i = dir->tdir_count-1; i >= 0; i--)
  1500. v[i] = vp[i];
  1501. } else {
  1502. int16* vp = (int16*) v;
  1503. for (i = dir->tdir_count-1; i >= 0; i--)
  1504. v[i] = vp[i];
  1505. }
  1506. break;
  1507. case TIFF_LONG:
  1508. case TIFF_SLONG:
  1509. if (!TIFFFetchLongArray(tif, dir, (uint32*) v))
  1510. return (0);
  1511. if (dir->tdir_type == TIFF_LONG) {
  1512. uint32* vp = (uint32*) v;
  1513. for (i = dir->tdir_count-1; i >= 0; i--)
  1514. v[i] = vp[i];
  1515. } else {
  1516. int32* vp = (int32*) v;
  1517. for (i = dir->tdir_count-1; i >= 0; i--)
  1518. v[i] = vp[i];
  1519. }
  1520. break;
  1521. case TIFF_RATIONAL:
  1522. case TIFF_SRATIONAL:
  1523. if (!TIFFFetchRationalArray(tif, dir, (float*) v))
  1524. return (0);
  1525. { float* vp = (float*) v;
  1526. for (i = dir->tdir_count-1; i >= 0; i--)
  1527. v[i] = vp[i];
  1528. }
  1529. break;
  1530. case TIFF_FLOAT:
  1531. if (!TIFFFetchFloatArray(tif, dir, (float*) v))
  1532. return (0);
  1533. { float* vp = (float*) v;
  1534. for (i = dir->tdir_count-1; i >= 0; i--)
  1535. v[i] = vp[i];
  1536. }
  1537. break;
  1538. case TIFF_DOUBLE:
  1539. return (TIFFFetchDoubleArray(tif, dir, (double*) v));
  1540. default:
  1541. /* TIFF_NOTYPE */
  1542. /* TIFF_ASCII */
  1543. /* TIFF_UNDEFINED */
  1544. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1545. "cannot read TIFF_ANY type %d for field \"%s\"",
  1546. dir->tdir_type,
  1547. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  1548. return (0);
  1549. }
  1550. return (1);
  1551. }
  1552. /*
  1553. * Fetch a tag that is not handled by special case code.
  1554. */
  1555. static int
  1556. TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
  1557. {
  1558. static const char mesg[] = "to fetch tag value";
  1559. int ok = 0;
  1560. const TIFFFieldInfo* fip = _TIFFFieldWithTag(tif, dp->tdir_tag);
  1561. if (dp->tdir_count > 1) { /* array of values */
  1562. char* cp = NULL;
  1563. switch (dp->tdir_type) {
  1564. case TIFF_BYTE:
  1565. case TIFF_SBYTE:
  1566. cp = (char *)_TIFFCheckMalloc(tif,
  1567. dp->tdir_count, sizeof (uint8), mesg);
  1568. ok = cp && TIFFFetchByteArray(tif, dp, (uint8*) cp);
  1569. break;
  1570. case TIFF_SHORT:
  1571. case TIFF_SSHORT:
  1572. cp = (char *)_TIFFCheckMalloc(tif,
  1573. dp->tdir_count, sizeof (uint16), mesg);
  1574. ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp);
  1575. break;
  1576. case TIFF_LONG:
  1577. case TIFF_SLONG:
  1578. cp = (char *)_TIFFCheckMalloc(tif,
  1579. dp->tdir_count, sizeof (uint32), mesg);
  1580. ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp);
  1581. break;
  1582. case TIFF_RATIONAL:
  1583. case TIFF_SRATIONAL:
  1584. cp = (char *)_TIFFCheckMalloc(tif,
  1585. dp->tdir_count, sizeof (float), mesg);
  1586. ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp);
  1587. break;
  1588. case TIFF_FLOAT:
  1589. cp = (char *)_TIFFCheckMalloc(tif,
  1590. dp->tdir_count, sizeof (float), mesg);
  1591. ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp);
  1592. break;
  1593. case TIFF_DOUBLE:
  1594. cp = (char *)_TIFFCheckMalloc(tif,
  1595. dp->tdir_count, sizeof (double), mesg);
  1596. ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp);
  1597. break;
  1598. case TIFF_ASCII:
  1599. case TIFF_UNDEFINED: /* bit of a cheat... */
  1600. /*
  1601. * Some vendors write strings w/o the trailing
  1602. * NULL byte, so always append one just in case.
  1603. */
  1604. cp = (char *)_TIFFCheckMalloc(tif, dp->tdir_count + 1,
  1605. 1, mesg);
  1606. if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 )
  1607. cp[dp->tdir_count] = '\0'; /* XXX */
  1608. break;
  1609. }
  1610. if (ok) {
  1611. ok = (fip->field_passcount ?
  1612. TIFFSetField(tif, dp->tdir_tag, dp->tdir_count, cp)
  1613. : TIFFSetField(tif, dp->tdir_tag, cp));
  1614. }
  1615. if (cp != NULL)
  1616. _TIFFfree(cp);
  1617. } else if (CheckDirCount(tif, dp, 1)) { /* singleton value */
  1618. switch (dp->tdir_type) {
  1619. case TIFF_BYTE:
  1620. case TIFF_SBYTE:
  1621. case TIFF_SHORT:
  1622. case TIFF_SSHORT:
  1623. /*
  1624. * If the tag is also acceptable as a LONG or SLONG
  1625. * then TIFFSetField will expect an uint32 parameter
  1626. * passed to it (through varargs). Thus, for machines
  1627. * where sizeof (int) != sizeof (uint32) we must do
  1628. * a careful check here. It's hard to say if this
  1629. * is worth optimizing.
  1630. *
  1631. * NB: We use TIFFFieldWithTag here knowing that
  1632. * it returns us the first entry in the table
  1633. * for the tag and that that entry is for the
  1634. * widest potential data type the tag may have.
  1635. */
  1636. { TIFFDataType type = fip->field_type;
  1637. if (type != TIFF_LONG && type != TIFF_SLONG) {
  1638. uint16 v = (uint16)
  1639. TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
  1640. ok = (fip->field_passcount ?
  1641. TIFFSetField(tif, dp->tdir_tag, 1, &v)
  1642. : TIFFSetField(tif, dp->tdir_tag, v));
  1643. break;
  1644. }
  1645. }
  1646. /* fall thru... */
  1647. case TIFF_LONG:
  1648. case TIFF_SLONG:
  1649. { uint32 v32 =
  1650. TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
  1651. ok = (fip->field_passcount ?
  1652. TIFFSetField(tif, dp->tdir_tag, 1, &v32)
  1653. : TIFFSetField(tif, dp->tdir_tag, v32));
  1654. }
  1655. break;
  1656. case TIFF_RATIONAL:
  1657. case TIFF_SRATIONAL:
  1658. case TIFF_FLOAT:
  1659. { float v = (dp->tdir_type == TIFF_FLOAT ?
  1660. TIFFFetchFloat(tif, dp)
  1661. : TIFFFetchRational(tif, dp));
  1662. ok = (fip->field_passcount ?
  1663. TIFFSetField(tif, dp->tdir_tag, 1, &v)
  1664. : TIFFSetField(tif, dp->tdir_tag, v));
  1665. }
  1666. break;
  1667. case TIFF_DOUBLE:
  1668. { double v;
  1669. ok = (TIFFFetchDoubleArray(tif, dp, &v) &&
  1670. (fip->field_passcount ?
  1671. TIFFSetField(tif, dp->tdir_tag, 1, &v)
  1672. : TIFFSetField(tif, dp->tdir_tag, v))
  1673. );
  1674. }
  1675. break;
  1676. case TIFF_ASCII:
  1677. case TIFF_UNDEFINED: /* bit of a cheat... */
  1678. { char c[2];
  1679. if( (ok = (TIFFFetchString(tif, dp, c) != 0)) != 0 ) {
  1680. c[1] = '\0'; /* XXX paranoid */
  1681. ok = (fip->field_passcount ?
  1682. TIFFSetField(tif, dp->tdir_tag, 1, c)
  1683. : TIFFSetField(tif, dp->tdir_tag, c));
  1684. }
  1685. }
  1686. break;
  1687. }
  1688. }
  1689. return (ok);
  1690. }
  1691. #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
  1692. /*
  1693. * Fetch samples/pixel short values for
  1694. * the specified tag and verify that
  1695. * all values are the same.
  1696. */
  1697. static int
  1698. TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
  1699. {
  1700. uint16 samples = tif->tif_dir.td_samplesperpixel;
  1701. int status = 0;
  1702. if (CheckDirCount(tif, dir, (uint32) samples)) {
  1703. uint16 buf[10];
  1704. uint16* v = buf;
  1705. if (dir->tdir_count > NITEMS(buf))
  1706. v = (uint16*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof(uint16),
  1707. "to fetch per-sample values");
  1708. if (v && TIFFFetchShortArray(tif, dir, v)) {
  1709. uint16 i;
  1710. int check_count = dir->tdir_count;
  1711. if( samples < check_count )
  1712. check_count = samples;
  1713. for (i = 1; i < check_count; i++)
  1714. if (v[i] != v[0]) {
  1715. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1716. "Cannot handle different per-sample values for field \"%s\"",
  1717. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  1718. goto bad;
  1719. }
  1720. *pl = v[0];
  1721. status = 1;
  1722. }
  1723. bad:
  1724. if (v && v != buf)
  1725. _TIFFfree(v);
  1726. }
  1727. return (status);
  1728. }
  1729. /*
  1730. * Fetch samples/pixel long values for
  1731. * the specified tag and verify that
  1732. * all values are the same.
  1733. */
  1734. static int
  1735. TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
  1736. {
  1737. uint16 samples = tif->tif_dir.td_samplesperpixel;
  1738. int status = 0;
  1739. if (CheckDirCount(tif, dir, (uint32) samples)) {
  1740. uint32 buf[10];
  1741. uint32* v = buf;
  1742. if (dir->tdir_count > NITEMS(buf))
  1743. v = (uint32*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof(uint32),
  1744. "to fetch per-sample values");
  1745. if (v && TIFFFetchLongArray(tif, dir, v)) {
  1746. uint16 i;
  1747. int check_count = dir->tdir_count;
  1748. if( samples < check_count )
  1749. check_count = samples;
  1750. for (i = 1; i < check_count; i++)
  1751. if (v[i] != v[0]) {
  1752. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  1753. "Cannot handle different per-sample values for field \"%s\"",
  1754. _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
  1755. goto bad;
  1756. }
  1757. *pl = v[0];
  1758. status = 1;
  1759. }
  1760. bad:
  1761. if (v && v != buf)
  1762. _TIFFfree(v);
  1763. }
  1764. return (status);
  1765. }
  1766. /*
  1767. * Fetch samples/pixel ANY values for the specified tag and returns their min
  1768. * and max.
  1769. */
  1770. static int
  1771. TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* minv, double* maxv)
  1772. {
  1773. uint16 samples = tif->tif_dir.td_samplesperpixel;
  1774. int status = 0;
  1775. if (CheckDirCount(tif, dir, (uint32) samples)) {
  1776. double buf[10];
  1777. double* v = buf;
  1778. if (dir->tdir_count > NITEMS(buf))
  1779. v = (double*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof (double),
  1780. "to fetch per-sample values");
  1781. if (v && TIFFFetchAnyArray(tif, dir, v)) {
  1782. uint16 i;
  1783. int check_count = dir->tdir_count;
  1784. if( samples < check_count )
  1785. check_count = samples;
  1786. *minv = *maxv = v[0];
  1787. for (i = 1; i < check_count; i++)
  1788. {
  1789. if (v[i] < *minv)
  1790. *minv = v[i];
  1791. if (v[i] > *maxv)
  1792. *maxv = v[i];
  1793. }
  1794. status = 1;
  1795. }
  1796. if (v && v != buf)
  1797. _TIFFfree(v);
  1798. }
  1799. return (status);
  1800. }
  1801. #undef NITEMS
  1802. /*
  1803. * Fetch a set of offsets or lengths.
  1804. * While this routine says "strips", in fact it's also used for tiles.
  1805. */
  1806. static int
  1807. TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
  1808. {
  1809. register uint32* lp;
  1810. int status;
  1811. CheckDirCount(tif, dir, (uint32) nstrips);
  1812. /*
  1813. * Allocate space for strip information.
  1814. */
  1815. if (*lpp == NULL &&
  1816. (*lpp = (uint32 *)_TIFFCheckMalloc(tif,
  1817. nstrips, sizeof (uint32), "for strip array")) == NULL)
  1818. return (0);
  1819. lp = *lpp;
  1820. _TIFFmemset( lp, 0, sizeof(uint32) * nstrips );
  1821. if (dir->tdir_type == (int)TIFF_SHORT) {
  1822. /*
  1823. * Handle uint16->uint32 expansion.
  1824. */
  1825. uint16* dp = (uint16*) _TIFFCheckMalloc(tif,
  1826. dir->tdir_count, sizeof (uint16), "to fetch strip tag");
  1827. if (dp == NULL)
  1828. return (0);
  1829. if( (status = TIFFFetchShortArray(tif, dir, dp)) != 0 ) {
  1830. int i;
  1831. for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
  1832. {
  1833. lp[i] = dp[i];
  1834. }
  1835. }
  1836. _TIFFfree((char*) dp);
  1837. } else if( nstrips != (int) dir->tdir_count ) {
  1838. /* Special case to correct length */
  1839. uint32* dp = (uint32*) _TIFFCheckMalloc(tif,
  1840. dir->tdir_count, sizeof (uint32), "to fetch strip tag");
  1841. if (dp == NULL)
  1842. return (0);
  1843. status = TIFFFetchLongArray(tif, dir, dp);
  1844. if( status != 0 ) {
  1845. int i;
  1846. for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
  1847. {
  1848. lp[i] = dp[i];
  1849. }
  1850. }
  1851. _TIFFfree( (char *) dp );
  1852. } else
  1853. status = TIFFFetchLongArray(tif, dir, lp);
  1854. return (status);
  1855. }
  1856. /*
  1857. * Fetch and set the RefBlackWhite tag.
  1858. */
  1859. static int
  1860. TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
  1861. {
  1862. static const char mesg[] = "for \"ReferenceBlackWhite\" array";
  1863. char* cp;
  1864. int ok;
  1865. if (dir->tdir_type == TIFF_RATIONAL)
  1866. return (TIFFFetchNormalTag(tif, dir));
  1867. /*
  1868. * Handle LONG's for backward compatibility.
  1869. */
  1870. cp = (char *)_TIFFCheckMalloc(tif, dir->tdir_count,
  1871. sizeof (uint32), mesg);
  1872. if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) {
  1873. float* fp = (float*)
  1874. _TIFFCheckMalloc(tif, dir->tdir_count, sizeof (float), mesg);
  1875. if( (ok = (fp != NULL)) != 0 ) {
  1876. uint32 i;
  1877. for (i = 0; i < dir->tdir_count; i++)
  1878. fp[i] = (float)((uint32*) cp)[i];
  1879. ok = TIFFSetField(tif, dir->tdir_tag, fp);
  1880. _TIFFfree((char*) fp);
  1881. }
  1882. }
  1883. if (cp)
  1884. _TIFFfree(cp);
  1885. return (ok);
  1886. }
  1887. /*
  1888. * Fetch and set the SubjectDistance EXIF tag.
  1889. */
  1890. static int
  1891. TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
  1892. {
  1893. uint32 l[2];
  1894. float v;
  1895. int ok = 0;
  1896. if( dir->tdir_count != 1 || dir->tdir_type != TIFF_RATIONAL )
  1897. {
  1898. TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
  1899. "incorrect count or type for SubjectDistance, tag ignored" );
  1900. return (0);
  1901. }
  1902. if (TIFFFetchData(tif, dir, (char *)l)
  1903. && cvtRational(tif, dir, l[0], l[1], &v)) {
  1904. /*
  1905. * XXX: Numerator 0xFFFFFFFF means that we have infinite
  1906. * distance. Indicate that with a negative floating point
  1907. * SubjectDistance value.
  1908. */
  1909. ok = TIFFSetField(tif, dir->tdir_tag,
  1910. (l[0] != 0xFFFFFFFF) ? v : -v);
  1911. }
  1912. return ok;
  1913. }
  1914. /*
  1915. * Replace a single strip (tile) of uncompressed data by multiple strips
  1916. * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
  1917. * dealing with large images or for dealing with machines with a limited
  1918. * amount memory.
  1919. */
  1920. static void
  1921. ChopUpSingleUncompressedStrip(TIFF* tif)
  1922. {
  1923. register TIFFDirectory *td = &tif->tif_dir;
  1924. uint32 bytecount = td->td_stripbytecount[0];
  1925. uint32 offset = td->td_stripoffset[0];
  1926. tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes;
  1927. tstrip_t strip, nstrips, rowsperstrip;
  1928. uint32* newcounts;
  1929. uint32* newoffsets;
  1930. /*
  1931. * Make the rows hold at least one scanline, but fill specified amount
  1932. * of data if possible.
  1933. */
  1934. if (rowbytes > STRIP_SIZE_DEFAULT) {
  1935. stripbytes = rowbytes;
  1936. rowsperstrip = 1;
  1937. } else if (rowbytes > 0 ) {
  1938. rowsperstrip = STRIP_SIZE_DEFAULT / rowbytes;
  1939. stripbytes = rowbytes * rowsperstrip;
  1940. }
  1941. else
  1942. return;
  1943. /*
  1944. * never increase the number of strips in an image
  1945. */
  1946. if (rowsperstrip >= td->td_rowsperstrip)
  1947. return;
  1948. nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes);
  1949. if( nstrips == 0 ) /* something is wonky, do nothing. */
  1950. return;
  1951. newcounts = (uint32*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint32),
  1952. "for chopped \"StripByteCounts\" array");
  1953. newoffsets = (uint32*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint32),
  1954. "for chopped \"StripOffsets\" array");
  1955. if (newcounts == NULL || newoffsets == NULL) {
  1956. /*
  1957. * Unable to allocate new strip information, give up and use
  1958. * the original one strip information.
  1959. */
  1960. if (newcounts != NULL)
  1961. _TIFFfree(newcounts);
  1962. if (newoffsets != NULL)
  1963. _TIFFfree(newoffsets);
  1964. return;
  1965. }
  1966. /*
  1967. * Fill the strip information arrays with new bytecounts and offsets
  1968. * that reflect the broken-up format.
  1969. */
  1970. for (strip = 0; strip < nstrips; strip++) {
  1971. if ((uint32)stripbytes > bytecount)
  1972. stripbytes = bytecount;
  1973. newcounts[strip] = stripbytes;
  1974. newoffsets[strip] = offset;
  1975. offset += stripbytes;
  1976. bytecount -= stripbytes;
  1977. }
  1978. /*
  1979. * Replace old single strip info with multi-strip info.
  1980. */
  1981. td->td_stripsperimage = td->td_nstrips = nstrips;
  1982. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
  1983. _TIFFfree(td->td_stripbytecount);
  1984. _TIFFfree(td->td_stripoffset);
  1985. td->td_stripbytecount = newcounts;
  1986. td->td_stripoffset = newoffsets;
  1987. td->td_stripbytecountsorted = 1;
  1988. }
  1989. /* vim: set ts=8 sts=8 sw=8 noet: */
  1990. /*
  1991. * Local Variables:
  1992. * mode: c
  1993. * c-basic-offset: 8
  1994. * fill-column: 78
  1995. * End:
  1996. */