tif_read.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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. * Scanline-oriented Read Support
  27. */
  28. #include "tiffiop.h"
  29. #include <stdio.h>
  30. int TIFFFillStrip(TIFF *tif, uint32_t strip);
  31. int TIFFFillTile(TIFF *tif, uint32_t tile);
  32. static int TIFFStartStrip(TIFF *tif, uint32_t strip);
  33. static int TIFFStartTile(TIFF *tif, uint32_t tile);
  34. static int TIFFCheckRead(TIFF *, int);
  35. static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
  36. tmsize_t size, const char *module);
  37. static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
  38. tmsize_t size, const char *module);
  39. #define NOSTRIP ((uint32_t)(-1)) /* undefined state */
  40. #define NOTILE ((uint32_t)(-1)) /* undefined state */
  41. #define INITIAL_THRESHOLD (1024 * 1024)
  42. #define THRESHOLD_MULTIPLIER 10
  43. #define MAX_THRESHOLD \
  44. (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \
  45. INITIAL_THRESHOLD)
  46. #define TIFF_INT64_MAX ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF)
  47. /* Read 'size' bytes in tif_rawdata buffer starting at offset 'rawdata_offset'
  48. * Returns 1 in case of success, 0 otherwise. */
  49. static int TIFFReadAndRealloc(TIFF *tif, tmsize_t size, tmsize_t rawdata_offset,
  50. int is_strip, uint32_t strip_or_tile,
  51. const char *module)
  52. {
  53. #if SIZEOF_SIZE_T == 8
  54. tmsize_t threshold = INITIAL_THRESHOLD;
  55. #endif
  56. tmsize_t already_read = 0;
  57. #if SIZEOF_SIZE_T != 8
  58. /* On 32 bit processes, if the request is large enough, check against */
  59. /* file size */
  60. if (size > 1000 * 1000 * 1000)
  61. {
  62. uint64_t filesize = TIFFGetFileSize(tif);
  63. if ((uint64_t)size >= filesize)
  64. {
  65. TIFFErrorExtR(tif, module,
  66. "Chunk size requested is larger than file size.");
  67. return 0;
  68. }
  69. }
  70. #endif
  71. /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
  72. /* so as to avoid allocating too much memory in case the file is too */
  73. /* short. We could ask for the file size, but this might be */
  74. /* expensive with some I/O layers (think of reading a gzipped file) */
  75. /* Restrict to 64 bit processes, so as to avoid reallocs() */
  76. /* on 32 bit processes where virtual memory is scarce. */
  77. while (already_read < size)
  78. {
  79. tmsize_t bytes_read;
  80. tmsize_t to_read = size - already_read;
  81. #if SIZEOF_SIZE_T == 8
  82. if (to_read >= threshold && threshold < MAX_THRESHOLD &&
  83. already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
  84. {
  85. to_read = threshold;
  86. threshold *= THRESHOLD_MULTIPLIER;
  87. }
  88. #endif
  89. if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize)
  90. {
  91. uint8_t *new_rawdata;
  92. assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
  93. tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
  94. (uint64_t)already_read + to_read + rawdata_offset, 1024);
  95. if (tif->tif_rawdatasize == 0)
  96. {
  97. TIFFErrorExtR(tif, module, "Invalid buffer size");
  98. return 0;
  99. }
  100. new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata,
  101. tif->tif_rawdatasize);
  102. if (new_rawdata == 0)
  103. {
  104. TIFFErrorExtR(tif, module,
  105. "No space for data buffer at scanline %" PRIu32,
  106. tif->tif_row);
  107. _TIFFfreeExt(tif, tif->tif_rawdata);
  108. tif->tif_rawdata = 0;
  109. tif->tif_rawdatasize = 0;
  110. return 0;
  111. }
  112. tif->tif_rawdata = new_rawdata;
  113. }
  114. if (tif->tif_rawdata == NULL)
  115. {
  116. /* should not happen in practice but helps CoverityScan */
  117. return 0;
  118. }
  119. bytes_read = TIFFReadFile(
  120. tif, tif->tif_rawdata + rawdata_offset + already_read, to_read);
  121. already_read += bytes_read;
  122. if (bytes_read != to_read)
  123. {
  124. memset(tif->tif_rawdata + rawdata_offset + already_read, 0,
  125. tif->tif_rawdatasize - rawdata_offset - already_read);
  126. if (is_strip)
  127. {
  128. TIFFErrorExtR(tif, module,
  129. "Read error at scanline %" PRIu32
  130. "; got %" TIFF_SSIZE_FORMAT " bytes, "
  131. "expected %" TIFF_SSIZE_FORMAT,
  132. tif->tif_row, already_read, size);
  133. }
  134. else
  135. {
  136. TIFFErrorExtR(tif, module,
  137. "Read error at row %" PRIu32 ", col %" PRIu32
  138. ", tile %" PRIu32 "; "
  139. "got %" TIFF_SSIZE_FORMAT
  140. " bytes, expected %" TIFF_SSIZE_FORMAT "",
  141. tif->tif_row, tif->tif_col, strip_or_tile,
  142. already_read, size);
  143. }
  144. return 0;
  145. }
  146. }
  147. return 1;
  148. }
  149. static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead,
  150. int restart)
  151. {
  152. static const char module[] = "TIFFFillStripPartial";
  153. register TIFFDirectory *td = &tif->tif_dir;
  154. tmsize_t unused_data;
  155. uint64_t read_offset;
  156. tmsize_t to_read;
  157. tmsize_t read_ahead_mod;
  158. /* tmsize_t bytecountm; */
  159. /*
  160. * Expand raw data buffer, if needed, to hold data
  161. * strip coming from file (perhaps should set upper
  162. * bound on the size of a buffer we'll use?).
  163. */
  164. /* bytecountm=(tmsize_t) TIFFGetStrileByteCount(tif, strip); */
  165. /* Not completely sure where the * 2 comes from, but probably for */
  166. /* an exponentional growth strategy of tif_rawdatasize */
  167. if (read_ahead < TIFF_TMSIZE_T_MAX / 2)
  168. read_ahead_mod = read_ahead * 2;
  169. else
  170. read_ahead_mod = read_ahead;
  171. if (read_ahead_mod > tif->tif_rawdatasize)
  172. {
  173. assert(restart);
  174. tif->tif_curstrip = NOSTRIP;
  175. if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
  176. {
  177. TIFFErrorExtR(tif, module,
  178. "Data buffer too small to hold part of strip %d",
  179. strip);
  180. return (0);
  181. }
  182. }
  183. if (restart)
  184. {
  185. tif->tif_rawdataloaded = 0;
  186. tif->tif_rawdataoff = 0;
  187. }
  188. /*
  189. ** If we are reading more data, move any unused data to the
  190. ** start of the buffer.
  191. */
  192. if (tif->tif_rawdataloaded > 0)
  193. unused_data =
  194. tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
  195. else
  196. unused_data = 0;
  197. if (unused_data > 0)
  198. {
  199. assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
  200. memmove(tif->tif_rawdata, tif->tif_rawcp, unused_data);
  201. }
  202. /*
  203. ** Seek to the point in the file where more data should be read.
  204. */
  205. read_offset = TIFFGetStrileOffset(tif, strip) + tif->tif_rawdataoff +
  206. tif->tif_rawdataloaded;
  207. if (!SeekOK(tif, read_offset))
  208. {
  209. TIFFErrorExtR(tif, module,
  210. "Seek error at scanline %" PRIu32 ", strip %d",
  211. tif->tif_row, strip);
  212. return 0;
  213. }
  214. /*
  215. ** How much do we want to read?
  216. */
  217. if (read_ahead_mod > tif->tif_rawdatasize)
  218. to_read = read_ahead_mod - unused_data;
  219. else
  220. to_read = tif->tif_rawdatasize - unused_data;
  221. if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, strip) -
  222. tif->tif_rawdataoff - tif->tif_rawdataloaded)
  223. {
  224. to_read = (tmsize_t)TIFFGetStrileByteCount(tif, strip) -
  225. tif->tif_rawdataoff - tif->tif_rawdataloaded;
  226. }
  227. assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
  228. if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, /* is_strip */
  229. 0, /* strip_or_tile */
  230. module))
  231. {
  232. return 0;
  233. }
  234. tif->tif_rawdataoff =
  235. tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data;
  236. tif->tif_rawdataloaded = unused_data + to_read;
  237. tif->tif_rawcc = tif->tif_rawdataloaded;
  238. tif->tif_rawcp = tif->tif_rawdata;
  239. if (!isFillOrder(tif, td->td_fillorder) &&
  240. (tif->tif_flags & TIFF_NOBITREV) == 0)
  241. {
  242. assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0);
  243. TIFFReverseBits(tif->tif_rawdata + unused_data, to_read);
  244. }
  245. /*
  246. ** When starting a strip from the beginning we need to
  247. ** restart the decoder.
  248. */
  249. if (restart)
  250. {
  251. #ifdef JPEG_SUPPORT
  252. /* A bit messy since breaks the codec abstraction. Ultimately */
  253. /* there should be a function pointer for that, but it seems */
  254. /* only JPEG is affected. */
  255. /* For JPEG, if there are multiple scans (can generally be known */
  256. /* with the read_ahead used), we need to read the whole strip */
  257. if (tif->tif_dir.td_compression == COMPRESSION_JPEG &&
  258. (uint64_t)tif->tif_rawcc < TIFFGetStrileByteCount(tif, strip))
  259. {
  260. if (TIFFJPEGIsFullStripRequired(tif))
  261. {
  262. return TIFFFillStrip(tif, strip);
  263. }
  264. }
  265. #endif
  266. return TIFFStartStrip(tif, strip);
  267. }
  268. else
  269. {
  270. return 1;
  271. }
  272. }
  273. /*
  274. * Seek to a random row+sample in a file.
  275. *
  276. * Only used by TIFFReadScanline, and is only used on
  277. * strip organized files. We do some tricky stuff to try
  278. * and avoid reading the whole compressed raw data for big
  279. * strips.
  280. */
  281. static int TIFFSeek(TIFF *tif, uint32_t row, uint16_t sample)
  282. {
  283. register TIFFDirectory *td = &tif->tif_dir;
  284. uint32_t strip;
  285. int whole_strip;
  286. tmsize_t read_ahead = 0;
  287. /*
  288. ** Establish what strip we are working from.
  289. */
  290. if (row >= td->td_imagelength)
  291. { /* out of range */
  292. TIFFErrorExtR(tif, tif->tif_name,
  293. "%" PRIu32 ": Row out of range, max %" PRIu32 "", row,
  294. td->td_imagelength);
  295. return (0);
  296. }
  297. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  298. {
  299. if (sample >= td->td_samplesperpixel)
  300. {
  301. TIFFErrorExtR(tif, tif->tif_name,
  302. "%" PRIu16 ": Sample out of range, max %" PRIu16 "",
  303. sample, td->td_samplesperpixel);
  304. return (0);
  305. }
  306. strip = (uint32_t)sample * td->td_stripsperimage +
  307. row / td->td_rowsperstrip;
  308. }
  309. else
  310. strip = row / td->td_rowsperstrip;
  311. /*
  312. * Do we want to treat this strip as one whole chunk or
  313. * read it a few lines at a time?
  314. */
  315. #if defined(CHUNKY_STRIP_READ_SUPPORT)
  316. whole_strip = TIFFGetStrileByteCount(tif, strip) < 10 || isMapped(tif);
  317. if (td->td_compression == COMPRESSION_LERC ||
  318. td->td_compression == COMPRESSION_JBIG)
  319. {
  320. /* Ideally plugins should have a way to declare they don't support
  321. * chunk strip */
  322. whole_strip = 1;
  323. }
  324. #else
  325. whole_strip = 1;
  326. #endif
  327. if (!whole_strip)
  328. {
  329. /* 16 is for YCbCr mode where we may need to read 16 */
  330. /* lines at a time to get a decompressed line, and 5000 */
  331. /* is some constant value, for example for JPEG tables */
  332. if (tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
  333. tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000)
  334. {
  335. read_ahead = tif->tif_scanlinesize * 16 + 5000;
  336. }
  337. else
  338. {
  339. read_ahead = tif->tif_scanlinesize;
  340. }
  341. }
  342. /*
  343. * If we haven't loaded this strip, do so now, possibly
  344. * only reading the first part.
  345. */
  346. if (strip != tif->tif_curstrip)
  347. { /* different strip, refill */
  348. if (whole_strip)
  349. {
  350. if (!TIFFFillStrip(tif, strip))
  351. return (0);
  352. }
  353. else
  354. {
  355. if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
  356. return 0;
  357. }
  358. }
  359. /*
  360. ** If we already have some data loaded, do we need to read some more?
  361. */
  362. else if (!whole_strip)
  363. {
  364. if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) <
  365. read_ahead &&
  366. (uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded <
  367. TIFFGetStrileByteCount(tif, strip))
  368. {
  369. if (!TIFFFillStripPartial(tif, strip, read_ahead, 0))
  370. return 0;
  371. }
  372. }
  373. if (row < tif->tif_row)
  374. {
  375. /*
  376. * Moving backwards within the same strip: backup
  377. * to the start and then decode forward (below).
  378. *
  379. * NB: If you're planning on lots of random access within a
  380. * strip, it's better to just read and decode the entire
  381. * strip, and then access the decoded data in a random fashion.
  382. */
  383. if (tif->tif_rawdataoff != 0)
  384. {
  385. if (!TIFFFillStripPartial(tif, strip, read_ahead, 1))
  386. return 0;
  387. }
  388. else
  389. {
  390. if (!TIFFStartStrip(tif, strip))
  391. return (0);
  392. }
  393. }
  394. if (row != tif->tif_row)
  395. {
  396. /*
  397. * Seek forward to the desired row.
  398. */
  399. /* TODO: Will this really work with partial buffers? */
  400. if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  401. return (0);
  402. tif->tif_row = row;
  403. }
  404. return (1);
  405. }
  406. int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample)
  407. {
  408. int e;
  409. if (!TIFFCheckRead(tif, 0))
  410. return (-1);
  411. if ((e = TIFFSeek(tif, row, sample)) != 0)
  412. {
  413. /*
  414. * Decompress desired row into user buffer.
  415. */
  416. e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, tif->tif_scanlinesize,
  417. sample);
  418. /* we are now poised at the beginning of the next row */
  419. tif->tif_row = row + 1;
  420. if (e)
  421. (*tif->tif_postdecode)(tif, (uint8_t *)buf, tif->tif_scanlinesize);
  422. }
  423. else
  424. {
  425. memset(buf, 0, (size_t)tif->tif_scanlinesize);
  426. }
  427. return (e > 0 ? 1 : -1);
  428. }
  429. /*
  430. * Calculate the strip size according to the number of
  431. * rows in the strip (check for truncated last strip on any
  432. * of the separations).
  433. */
  434. static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip,
  435. uint16_t *pplane)
  436. {
  437. static const char module[] = "TIFFReadEncodedStrip";
  438. TIFFDirectory *td = &tif->tif_dir;
  439. uint32_t rowsperstrip;
  440. uint32_t stripsperplane;
  441. uint32_t stripinplane;
  442. uint32_t rows;
  443. tmsize_t stripsize;
  444. if (!TIFFCheckRead(tif, 0))
  445. return ((tmsize_t)(-1));
  446. if (strip >= td->td_nstrips)
  447. {
  448. TIFFErrorExtR(tif, module,
  449. "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
  450. td->td_nstrips);
  451. return ((tmsize_t)(-1));
  452. }
  453. rowsperstrip = td->td_rowsperstrip;
  454. if (rowsperstrip > td->td_imagelength)
  455. rowsperstrip = td->td_imagelength;
  456. if (rowsperstrip == 0)
  457. {
  458. TIFFErrorExtR(tif, module, "rowsperstrip is zero");
  459. return ((tmsize_t)(-1));
  460. }
  461. stripsperplane =
  462. TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
  463. stripinplane = (strip % stripsperplane);
  464. if (pplane)
  465. *pplane = (uint16_t)(strip / stripsperplane);
  466. rows = td->td_imagelength - stripinplane * rowsperstrip;
  467. if (rows > rowsperstrip)
  468. rows = rowsperstrip;
  469. stripsize = TIFFVStripSize(tif, rows);
  470. if (stripsize == 0)
  471. return ((tmsize_t)(-1));
  472. return stripsize;
  473. }
  474. /*
  475. * Read a strip of data and decompress the specified
  476. * amount into the user-supplied buffer.
  477. */
  478. tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf,
  479. tmsize_t size)
  480. {
  481. static const char module[] = "TIFFReadEncodedStrip";
  482. TIFFDirectory *td = &tif->tif_dir;
  483. tmsize_t stripsize;
  484. uint16_t plane;
  485. stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
  486. if (stripsize == ((tmsize_t)(-1)))
  487. return ((tmsize_t)(-1));
  488. /* shortcut to avoid an extra memcpy() */
  489. if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
  490. size >= stripsize && !isMapped(tif) &&
  491. ((tif->tif_flags & TIFF_NOREADRAW) == 0))
  492. {
  493. if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
  494. return ((tmsize_t)(-1));
  495. if (!isFillOrder(tif, td->td_fillorder) &&
  496. (tif->tif_flags & TIFF_NOBITREV) == 0)
  497. TIFFReverseBits(buf, stripsize);
  498. (*tif->tif_postdecode)(tif, buf, stripsize);
  499. return (stripsize);
  500. }
  501. if ((size != (tmsize_t)(-1)) && (size < stripsize))
  502. stripsize = size;
  503. if (!TIFFFillStrip(tif, strip))
  504. {
  505. memset(buf, 0, (size_t)stripsize);
  506. return ((tmsize_t)(-1));
  507. }
  508. if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
  509. return ((tmsize_t)(-1));
  510. (*tif->tif_postdecode)(tif, buf, stripsize);
  511. return (stripsize);
  512. }
  513. /* Variant of TIFFReadEncodedStrip() that does
  514. * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
  515. * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case
  516. * of truncated file.
  517. * * calls regular TIFFReadEncodedStrip() if *buf != NULL
  518. */
  519. tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip,
  520. void **buf,
  521. tmsize_t bufsizetoalloc,
  522. tmsize_t size_to_read)
  523. {
  524. tmsize_t this_stripsize;
  525. uint16_t plane;
  526. if (*buf != NULL)
  527. {
  528. return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
  529. }
  530. this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
  531. if (this_stripsize == ((tmsize_t)(-1)))
  532. return ((tmsize_t)(-1));
  533. if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
  534. this_stripsize = size_to_read;
  535. if (!TIFFFillStrip(tif, strip))
  536. return ((tmsize_t)(-1));
  537. *buf = _TIFFmallocExt(tif, bufsizetoalloc);
  538. if (*buf == NULL)
  539. {
  540. TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
  541. return ((tmsize_t)(-1));
  542. }
  543. _TIFFmemset(*buf, 0, bufsizetoalloc);
  544. if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
  545. return ((tmsize_t)(-1));
  546. (*tif->tif_postdecode)(tif, *buf, this_stripsize);
  547. return (this_stripsize);
  548. }
  549. static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
  550. tmsize_t size, const char *module)
  551. {
  552. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  553. if (!isMapped(tif))
  554. {
  555. tmsize_t cc;
  556. if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
  557. {
  558. TIFFErrorExtR(tif, module,
  559. "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
  560. tif->tif_row, strip);
  561. return ((tmsize_t)(-1));
  562. }
  563. cc = TIFFReadFile(tif, buf, size);
  564. if (cc != size)
  565. {
  566. TIFFErrorExtR(tif, module,
  567. "Read error at scanline %" PRIu32
  568. "; got %" TIFF_SSIZE_FORMAT
  569. " bytes, expected %" TIFF_SSIZE_FORMAT,
  570. tif->tif_row, cc, size);
  571. return ((tmsize_t)(-1));
  572. }
  573. }
  574. else
  575. {
  576. tmsize_t ma = 0;
  577. tmsize_t n;
  578. if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
  579. ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
  580. {
  581. n = 0;
  582. }
  583. else if (ma > TIFF_TMSIZE_T_MAX - size)
  584. {
  585. n = 0;
  586. }
  587. else
  588. {
  589. tmsize_t mb = ma + size;
  590. if (mb > tif->tif_size)
  591. n = tif->tif_size - ma;
  592. else
  593. n = size;
  594. }
  595. if (n != size)
  596. {
  597. TIFFErrorExtR(tif, module,
  598. "Read error at scanline %" PRIu32 ", strip %" PRIu32
  599. "; got %" TIFF_SSIZE_FORMAT
  600. " bytes, expected %" TIFF_SSIZE_FORMAT,
  601. tif->tif_row, strip, n, size);
  602. return ((tmsize_t)(-1));
  603. }
  604. _TIFFmemcpy(buf, tif->tif_base + ma, size);
  605. }
  606. return (size);
  607. }
  608. static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,
  609. int is_strip, tmsize_t size,
  610. const char *module)
  611. {
  612. assert(!isMapped(tif));
  613. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  614. if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
  615. {
  616. if (is_strip)
  617. {
  618. TIFFErrorExtR(tif, module,
  619. "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
  620. tif->tif_row, strip_or_tile);
  621. }
  622. else
  623. {
  624. TIFFErrorExtR(tif, module,
  625. "Seek error at row %" PRIu32 ", col %" PRIu32
  626. ", tile %" PRIu32,
  627. tif->tif_row, tif->tif_col, strip_or_tile);
  628. }
  629. return ((tmsize_t)(-1));
  630. }
  631. if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
  632. {
  633. return ((tmsize_t)(-1));
  634. }
  635. return (size);
  636. }
  637. /*
  638. * Read a strip of data from the file.
  639. */
  640. tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
  641. {
  642. static const char module[] = "TIFFReadRawStrip";
  643. TIFFDirectory *td = &tif->tif_dir;
  644. uint64_t bytecount64;
  645. tmsize_t bytecountm;
  646. if (!TIFFCheckRead(tif, 0))
  647. return ((tmsize_t)(-1));
  648. if (strip >= td->td_nstrips)
  649. {
  650. TIFFErrorExtR(tif, module,
  651. "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
  652. td->td_nstrips);
  653. return ((tmsize_t)(-1));
  654. }
  655. if (tif->tif_flags & TIFF_NOREADRAW)
  656. {
  657. TIFFErrorExtR(tif, module,
  658. "Compression scheme does not support access to raw "
  659. "uncompressed data");
  660. return ((tmsize_t)(-1));
  661. }
  662. bytecount64 = TIFFGetStrileByteCount(tif, strip);
  663. if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
  664. bytecountm = size;
  665. else
  666. bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
  667. if (bytecountm == 0)
  668. {
  669. return ((tmsize_t)(-1));
  670. }
  671. return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
  672. }
  673. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  674. static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; }
  675. /*
  676. * Read the specified strip and setup for decoding. The data buffer is
  677. * expanded, as necessary, to hold the strip's data.
  678. */
  679. int TIFFFillStrip(TIFF *tif, uint32_t strip)
  680. {
  681. static const char module[] = "TIFFFillStrip";
  682. TIFFDirectory *td = &tif->tif_dir;
  683. if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
  684. {
  685. uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
  686. if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
  687. {
  688. TIFFErrorExtR(tif, module,
  689. "Invalid strip byte count %" PRIu64
  690. ", strip %" PRIu32,
  691. bytecount, strip);
  692. return (0);
  693. }
  694. /* To avoid excessive memory allocations: */
  695. /* Byte count should normally not be larger than a number of */
  696. /* times the uncompressed size plus some margin */
  697. if (bytecount > 1024 * 1024)
  698. {
  699. /* 10 and 4096 are just values that could be adjusted. */
  700. /* Hopefully they are safe enough for all codecs */
  701. tmsize_t stripsize = TIFFStripSize(tif);
  702. if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
  703. {
  704. uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
  705. TIFFErrorExtR(tif, module,
  706. "Too large strip byte count %" PRIu64
  707. ", strip %" PRIu32 ". Limiting to %" PRIu64,
  708. bytecount, strip, newbytecount);
  709. bytecount = newbytecount;
  710. }
  711. }
  712. if (isMapped(tif))
  713. {
  714. /*
  715. * We must check for overflow, potentially causing
  716. * an OOB read. Instead of simple
  717. *
  718. * TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size
  719. *
  720. * comparison (which can overflow) we do the following
  721. * two comparisons:
  722. */
  723. if (bytecount > (uint64_t)tif->tif_size ||
  724. TIFFGetStrileOffset(tif, strip) >
  725. (uint64_t)tif->tif_size - bytecount)
  726. {
  727. /*
  728. * This error message might seem strange, but
  729. * it's what would happen if a read were done
  730. * instead.
  731. */
  732. TIFFErrorExtR(
  733. tif, module,
  734. "Read error on strip %" PRIu32 "; "
  735. "got %" PRIu64 " bytes, expected %" PRIu64,
  736. strip,
  737. NoSanitizeSubUInt64(tif->tif_size,
  738. TIFFGetStrileOffset(tif, strip)),
  739. bytecount);
  740. tif->tif_curstrip = NOSTRIP;
  741. return (0);
  742. }
  743. }
  744. if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
  745. (tif->tif_flags & TIFF_NOBITREV)))
  746. {
  747. /*
  748. * The image is mapped into memory and we either don't
  749. * need to flip bits or the compression routine is
  750. * going to handle this operation itself. In this
  751. * case, avoid copying the raw data and instead just
  752. * reference the data from the memory mapped file
  753. * image. This assumes that the decompression
  754. * routines do not modify the contents of the raw data
  755. * buffer (if they try to, the application will get a
  756. * fault since the file is mapped read-only).
  757. */
  758. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  759. {
  760. _TIFFfreeExt(tif, tif->tif_rawdata);
  761. tif->tif_rawdata = NULL;
  762. tif->tif_rawdatasize = 0;
  763. }
  764. tif->tif_flags &= ~TIFF_MYBUFFER;
  765. tif->tif_rawdatasize = (tmsize_t)bytecount;
  766. tif->tif_rawdata =
  767. tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);
  768. tif->tif_rawdataoff = 0;
  769. tif->tif_rawdataloaded = (tmsize_t)bytecount;
  770. /*
  771. * When we have tif_rawdata reference directly into the memory
  772. * mapped file we need to be pretty careful about how we use the
  773. * rawdata. It is not a general purpose working buffer as it
  774. * normally otherwise is. So we keep track of this fact to avoid
  775. * using it improperly.
  776. */
  777. tif->tif_flags |= TIFF_BUFFERMMAP;
  778. }
  779. else
  780. {
  781. /*
  782. * Expand raw data buffer, if needed, to hold data
  783. * strip coming from file (perhaps should set upper
  784. * bound on the size of a buffer we'll use?).
  785. */
  786. tmsize_t bytecountm;
  787. bytecountm = (tmsize_t)bytecount;
  788. if ((uint64_t)bytecountm != bytecount)
  789. {
  790. TIFFErrorExtR(tif, module, "Integer overflow");
  791. return (0);
  792. }
  793. if (bytecountm > tif->tif_rawdatasize)
  794. {
  795. tif->tif_curstrip = NOSTRIP;
  796. if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
  797. {
  798. TIFFErrorExtR(
  799. tif, module,
  800. "Data buffer too small to hold strip %" PRIu32, strip);
  801. return (0);
  802. }
  803. }
  804. if (tif->tif_flags & TIFF_BUFFERMMAP)
  805. {
  806. tif->tif_curstrip = NOSTRIP;
  807. tif->tif_rawdata = NULL;
  808. tif->tif_rawdatasize = 0;
  809. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  810. }
  811. if (isMapped(tif))
  812. {
  813. if (bytecountm > tif->tif_rawdatasize &&
  814. !TIFFReadBufferSetup(tif, 0, bytecountm))
  815. {
  816. return (0);
  817. }
  818. if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
  819. module) != bytecountm)
  820. {
  821. return (0);
  822. }
  823. }
  824. else
  825. {
  826. if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
  827. module) != bytecountm)
  828. {
  829. return (0);
  830. }
  831. }
  832. tif->tif_rawdataoff = 0;
  833. tif->tif_rawdataloaded = bytecountm;
  834. if (!isFillOrder(tif, td->td_fillorder) &&
  835. (tif->tif_flags & TIFF_NOBITREV) == 0)
  836. TIFFReverseBits(tif->tif_rawdata, bytecountm);
  837. }
  838. }
  839. return (TIFFStartStrip(tif, strip));
  840. }
  841. /*
  842. * Tile-oriented Read Support
  843. * Contributed by Nancy Cam (Silicon Graphics).
  844. */
  845. /*
  846. * Read and decompress a tile of data. The
  847. * tile is selected by the (x,y,z,s) coordinates.
  848. */
  849. tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,
  850. uint16_t s)
  851. {
  852. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  853. return ((tmsize_t)(-1));
  854. return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
  855. (tmsize_t)(-1)));
  856. }
  857. /*
  858. * Read a tile of data and decompress the specified
  859. * amount into the user-supplied buffer.
  860. */
  861. tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
  862. {
  863. static const char module[] = "TIFFReadEncodedTile";
  864. TIFFDirectory *td = &tif->tif_dir;
  865. tmsize_t tilesize = tif->tif_tilesize;
  866. if (!TIFFCheckRead(tif, 1))
  867. return ((tmsize_t)(-1));
  868. if (tile >= td->td_nstrips)
  869. {
  870. TIFFErrorExtR(tif, module,
  871. "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
  872. td->td_nstrips);
  873. return ((tmsize_t)(-1));
  874. }
  875. /* shortcut to avoid an extra memcpy() */
  876. if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
  877. size >= tilesize && !isMapped(tif) &&
  878. ((tif->tif_flags & TIFF_NOREADRAW) == 0))
  879. {
  880. if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
  881. return ((tmsize_t)(-1));
  882. if (!isFillOrder(tif, td->td_fillorder) &&
  883. (tif->tif_flags & TIFF_NOBITREV) == 0)
  884. TIFFReverseBits(buf, tilesize);
  885. (*tif->tif_postdecode)(tif, buf, tilesize);
  886. return (tilesize);
  887. }
  888. if (size == (tmsize_t)(-1))
  889. size = tilesize;
  890. else if (size > tilesize)
  891. size = tilesize;
  892. if (!TIFFFillTile(tif, tile))
  893. {
  894. memset(buf, 0, (size_t)size);
  895. return ((tmsize_t)(-1));
  896. }
  897. else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
  898. (uint16_t)(tile / td->td_stripsperimage)))
  899. {
  900. (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
  901. return (size);
  902. }
  903. else
  904. return ((tmsize_t)(-1));
  905. }
  906. /* Variant of TIFFReadTile() that does
  907. * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
  908. * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
  909. * of truncated file.
  910. * * calls regular TIFFReadEncodedTile() if *buf != NULL
  911. */
  912. tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf,
  913. tmsize_t bufsizetoalloc, uint32_t x,
  914. uint32_t y, uint32_t z, uint16_t s)
  915. {
  916. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  917. return ((tmsize_t)(-1));
  918. return (_TIFFReadEncodedTileAndAllocBuffer(
  919. tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
  920. (tmsize_t)(-1)));
  921. }
  922. /* Variant of TIFFReadEncodedTile() that does
  923. * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
  924. * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
  925. * of truncated file.
  926. * * calls regular TIFFReadEncodedTile() if *buf != NULL
  927. */
  928. tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile,
  929. void **buf, tmsize_t bufsizetoalloc,
  930. tmsize_t size_to_read)
  931. {
  932. static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
  933. TIFFDirectory *td = &tif->tif_dir;
  934. tmsize_t tilesize = tif->tif_tilesize;
  935. if (*buf != NULL)
  936. {
  937. return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
  938. }
  939. if (!TIFFCheckRead(tif, 1))
  940. return ((tmsize_t)(-1));
  941. if (tile >= td->td_nstrips)
  942. {
  943. TIFFErrorExtR(tif, module,
  944. "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
  945. td->td_nstrips);
  946. return ((tmsize_t)(-1));
  947. }
  948. if (!TIFFFillTile(tif, tile))
  949. return ((tmsize_t)(-1));
  950. /* Sanity checks to avoid excessive memory allocation */
  951. /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
  952. if (td->td_compression == COMPRESSION_NONE)
  953. {
  954. if (tif->tif_rawdatasize != tilesize)
  955. {
  956. TIFFErrorExtR(tif, TIFFFileName(tif),
  957. "Invalid tile byte count for tile %u. "
  958. "Expected %" PRIu64 ", got %" PRIu64,
  959. tile, (uint64_t)tilesize,
  960. (uint64_t)tif->tif_rawdatasize);
  961. return ((tmsize_t)(-1));
  962. }
  963. }
  964. else
  965. {
  966. /* Max compression ratio experimentally determined. Might be fragile...
  967. * Only apply this heuristics to situations where the memory allocation
  968. * would be big, to avoid breaking nominal use cases.
  969. */
  970. const int maxCompressionRatio =
  971. td->td_compression == COMPRESSION_ZSTD ? 33000
  972. : td->td_compression == COMPRESSION_JXL
  973. ?
  974. /* Evaluated on a 8000x8000 tile */
  975. 25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
  976. ? td->td_samplesperpixel
  977. : 1)
  978. : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
  979. if (bufsizetoalloc > 100 * 1000 * 1000 &&
  980. tif->tif_rawdatasize < tilesize / maxCompressionRatio)
  981. {
  982. TIFFErrorExtR(tif, TIFFFileName(tif),
  983. "Likely invalid tile byte count for tile %u. "
  984. "Uncompressed tile size is %" PRIu64 ", "
  985. "compressed one is %" PRIu64,
  986. tile, (uint64_t)tilesize,
  987. (uint64_t)tif->tif_rawdatasize);
  988. return ((tmsize_t)(-1));
  989. }
  990. }
  991. *buf = _TIFFmallocExt(tif, bufsizetoalloc);
  992. if (*buf == NULL)
  993. {
  994. TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
  995. return ((tmsize_t)(-1));
  996. }
  997. _TIFFmemset(*buf, 0, bufsizetoalloc);
  998. if (size_to_read == (tmsize_t)(-1))
  999. size_to_read = tilesize;
  1000. else if (size_to_read > tilesize)
  1001. size_to_read = tilesize;
  1002. if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
  1003. (uint16_t)(tile / td->td_stripsperimage)))
  1004. {
  1005. (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
  1006. return (size_to_read);
  1007. }
  1008. else
  1009. return ((tmsize_t)(-1));
  1010. }
  1011. static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
  1012. tmsize_t size, const char *module)
  1013. {
  1014. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  1015. if (!isMapped(tif))
  1016. {
  1017. tmsize_t cc;
  1018. if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))
  1019. {
  1020. TIFFErrorExtR(tif, module,
  1021. "Seek error at row %" PRIu32 ", col %" PRIu32
  1022. ", tile %" PRIu32,
  1023. tif->tif_row, tif->tif_col, tile);
  1024. return ((tmsize_t)(-1));
  1025. }
  1026. cc = TIFFReadFile(tif, buf, size);
  1027. if (cc != size)
  1028. {
  1029. TIFFErrorExtR(tif, module,
  1030. "Read error at row %" PRIu32 ", col %" PRIu32
  1031. "; got %" TIFF_SSIZE_FORMAT
  1032. " bytes, expected %" TIFF_SSIZE_FORMAT,
  1033. tif->tif_row, tif->tif_col, cc, size);
  1034. return ((tmsize_t)(-1));
  1035. }
  1036. }
  1037. else
  1038. {
  1039. tmsize_t ma, mb;
  1040. tmsize_t n;
  1041. ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);
  1042. mb = ma + size;
  1043. if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
  1044. (ma > tif->tif_size))
  1045. n = 0;
  1046. else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))
  1047. n = tif->tif_size - ma;
  1048. else
  1049. n = size;
  1050. if (n != size)
  1051. {
  1052. TIFFErrorExtR(tif, module,
  1053. "Read error at row %" PRIu32 ", col %" PRIu32
  1054. ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT
  1055. " bytes, expected %" TIFF_SSIZE_FORMAT,
  1056. tif->tif_row, tif->tif_col, tile, n, size);
  1057. return ((tmsize_t)(-1));
  1058. }
  1059. _TIFFmemcpy(buf, tif->tif_base + ma, size);
  1060. }
  1061. return (size);
  1062. }
  1063. /*
  1064. * Read a tile of data from the file.
  1065. */
  1066. tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
  1067. {
  1068. static const char module[] = "TIFFReadRawTile";
  1069. TIFFDirectory *td = &tif->tif_dir;
  1070. uint64_t bytecount64;
  1071. tmsize_t bytecountm;
  1072. if (!TIFFCheckRead(tif, 1))
  1073. return ((tmsize_t)(-1));
  1074. if (tile >= td->td_nstrips)
  1075. {
  1076. TIFFErrorExtR(tif, module,
  1077. "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
  1078. td->td_nstrips);
  1079. return ((tmsize_t)(-1));
  1080. }
  1081. if (tif->tif_flags & TIFF_NOREADRAW)
  1082. {
  1083. TIFFErrorExtR(tif, module,
  1084. "Compression scheme does not support access to raw "
  1085. "uncompressed data");
  1086. return ((tmsize_t)(-1));
  1087. }
  1088. bytecount64 = TIFFGetStrileByteCount(tif, tile);
  1089. if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
  1090. bytecountm = size;
  1091. else
  1092. bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
  1093. if (bytecountm == 0)
  1094. {
  1095. return ((tmsize_t)(-1));
  1096. }
  1097. return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
  1098. }
  1099. /*
  1100. * Read the specified tile and setup for decoding. The data buffer is
  1101. * expanded, as necessary, to hold the tile's data.
  1102. */
  1103. int TIFFFillTile(TIFF *tif, uint32_t tile)
  1104. {
  1105. static const char module[] = "TIFFFillTile";
  1106. TIFFDirectory *td = &tif->tif_dir;
  1107. if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
  1108. {
  1109. uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
  1110. if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
  1111. {
  1112. TIFFErrorExtR(tif, module,
  1113. "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
  1114. bytecount, tile);
  1115. return (0);
  1116. }
  1117. /* To avoid excessive memory allocations: */
  1118. /* Byte count should normally not be larger than a number of */
  1119. /* times the uncompressed size plus some margin */
  1120. if (bytecount > 1024 * 1024)
  1121. {
  1122. /* 10 and 4096 are just values that could be adjusted. */
  1123. /* Hopefully they are safe enough for all codecs */
  1124. tmsize_t stripsize = TIFFTileSize(tif);
  1125. if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
  1126. {
  1127. uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
  1128. TIFFErrorExtR(tif, module,
  1129. "Too large tile byte count %" PRIu64
  1130. ", tile %" PRIu32 ". Limiting to %" PRIu64,
  1131. bytecount, tile, newbytecount);
  1132. bytecount = newbytecount;
  1133. }
  1134. }
  1135. if (isMapped(tif))
  1136. {
  1137. /*
  1138. * We must check for overflow, potentially causing
  1139. * an OOB read. Instead of simple
  1140. *
  1141. * TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size
  1142. *
  1143. * comparison (which can overflow) we do the following
  1144. * two comparisons:
  1145. */
  1146. if (bytecount > (uint64_t)tif->tif_size ||
  1147. TIFFGetStrileOffset(tif, tile) >
  1148. (uint64_t)tif->tif_size - bytecount)
  1149. {
  1150. tif->tif_curtile = NOTILE;
  1151. return (0);
  1152. }
  1153. }
  1154. if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
  1155. (tif->tif_flags & TIFF_NOBITREV)))
  1156. {
  1157. /*
  1158. * The image is mapped into memory and we either don't
  1159. * need to flip bits or the compression routine is
  1160. * going to handle this operation itself. In this
  1161. * case, avoid copying the raw data and instead just
  1162. * reference the data from the memory mapped file
  1163. * image. This assumes that the decompression
  1164. * routines do not modify the contents of the raw data
  1165. * buffer (if they try to, the application will get a
  1166. * fault since the file is mapped read-only).
  1167. */
  1168. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  1169. {
  1170. _TIFFfreeExt(tif, tif->tif_rawdata);
  1171. tif->tif_rawdata = NULL;
  1172. tif->tif_rawdatasize = 0;
  1173. }
  1174. tif->tif_flags &= ~TIFF_MYBUFFER;
  1175. tif->tif_rawdatasize = (tmsize_t)bytecount;
  1176. tif->tif_rawdata =
  1177. tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
  1178. tif->tif_rawdataoff = 0;
  1179. tif->tif_rawdataloaded = (tmsize_t)bytecount;
  1180. tif->tif_flags |= TIFF_BUFFERMMAP;
  1181. }
  1182. else
  1183. {
  1184. /*
  1185. * Expand raw data buffer, if needed, to hold data
  1186. * tile coming from file (perhaps should set upper
  1187. * bound on the size of a buffer we'll use?).
  1188. */
  1189. tmsize_t bytecountm;
  1190. bytecountm = (tmsize_t)bytecount;
  1191. if ((uint64_t)bytecountm != bytecount)
  1192. {
  1193. TIFFErrorExtR(tif, module, "Integer overflow");
  1194. return (0);
  1195. }
  1196. if (bytecountm > tif->tif_rawdatasize)
  1197. {
  1198. tif->tif_curtile = NOTILE;
  1199. if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
  1200. {
  1201. TIFFErrorExtR(tif, module,
  1202. "Data buffer too small to hold tile %" PRIu32,
  1203. tile);
  1204. return (0);
  1205. }
  1206. }
  1207. if (tif->tif_flags & TIFF_BUFFERMMAP)
  1208. {
  1209. tif->tif_curtile = NOTILE;
  1210. tif->tif_rawdata = NULL;
  1211. tif->tif_rawdatasize = 0;
  1212. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  1213. }
  1214. if (isMapped(tif))
  1215. {
  1216. if (bytecountm > tif->tif_rawdatasize &&
  1217. !TIFFReadBufferSetup(tif, 0, bytecountm))
  1218. {
  1219. return (0);
  1220. }
  1221. if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
  1222. module) != bytecountm)
  1223. {
  1224. return (0);
  1225. }
  1226. }
  1227. else
  1228. {
  1229. if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
  1230. bytecountm)
  1231. {
  1232. return (0);
  1233. }
  1234. }
  1235. tif->tif_rawdataoff = 0;
  1236. tif->tif_rawdataloaded = bytecountm;
  1237. if (tif->tif_rawdata != NULL &&
  1238. !isFillOrder(tif, td->td_fillorder) &&
  1239. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1240. TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
  1241. }
  1242. }
  1243. return (TIFFStartTile(tif, tile));
  1244. }
  1245. /*
  1246. * Setup the raw data buffer in preparation for
  1247. * reading a strip of raw data. If the buffer
  1248. * is specified as zero, then a buffer of appropriate
  1249. * size is allocated by the library. Otherwise,
  1250. * the client must guarantee that the buffer is
  1251. * large enough to hold any individual strip of
  1252. * raw data.
  1253. */
  1254. int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)
  1255. {
  1256. static const char module[] = "TIFFReadBufferSetup";
  1257. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  1258. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  1259. if (tif->tif_rawdata)
  1260. {
  1261. if (tif->tif_flags & TIFF_MYBUFFER)
  1262. _TIFFfreeExt(tif, tif->tif_rawdata);
  1263. tif->tif_rawdata = NULL;
  1264. tif->tif_rawdatasize = 0;
  1265. }
  1266. if (bp)
  1267. {
  1268. tif->tif_rawdatasize = size;
  1269. tif->tif_rawdata = (uint8_t *)bp;
  1270. tif->tif_flags &= ~TIFF_MYBUFFER;
  1271. }
  1272. else
  1273. {
  1274. tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);
  1275. if (tif->tif_rawdatasize == 0)
  1276. {
  1277. TIFFErrorExtR(tif, module, "Invalid buffer size");
  1278. return (0);
  1279. }
  1280. /* Initialize to zero to avoid uninitialized buffers in case of */
  1281. /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
  1282. tif->tif_rawdata =
  1283. (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
  1284. tif->tif_flags |= TIFF_MYBUFFER;
  1285. }
  1286. if (tif->tif_rawdata == NULL)
  1287. {
  1288. TIFFErrorExtR(tif, module,
  1289. "No space for data buffer at scanline %" PRIu32,
  1290. tif->tif_row);
  1291. tif->tif_rawdatasize = 0;
  1292. return (0);
  1293. }
  1294. return (1);
  1295. }
  1296. /*
  1297. * Set state to appear as if a
  1298. * strip has just been read in.
  1299. */
  1300. static int TIFFStartStrip(TIFF *tif, uint32_t strip)
  1301. {
  1302. TIFFDirectory *td = &tif->tif_dir;
  1303. if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
  1304. {
  1305. if (!(*tif->tif_setupdecode)(tif))
  1306. return (0);
  1307. tif->tif_flags |= TIFF_CODERSETUP;
  1308. }
  1309. tif->tif_curstrip = strip;
  1310. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  1311. tif->tif_flags &= ~TIFF_BUF4WRITE;
  1312. if (tif->tif_flags & TIFF_NOREADRAW)
  1313. {
  1314. tif->tif_rawcp = NULL;
  1315. tif->tif_rawcc = 0;
  1316. }
  1317. else
  1318. {
  1319. tif->tif_rawcp = tif->tif_rawdata;
  1320. if (tif->tif_rawdataloaded > 0)
  1321. tif->tif_rawcc = tif->tif_rawdataloaded;
  1322. else
  1323. tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
  1324. }
  1325. if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
  1326. 0)
  1327. {
  1328. /* Needed for example for scanline access, if tif_predecode */
  1329. /* fails, and we try to read the same strip again. Without invalidating
  1330. */
  1331. /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */
  1332. /* codec state. */
  1333. tif->tif_curstrip = NOSTRIP;
  1334. return 0;
  1335. }
  1336. return 1;
  1337. }
  1338. /*
  1339. * Set state to appear as if a
  1340. * tile has just been read in.
  1341. */
  1342. static int TIFFStartTile(TIFF *tif, uint32_t tile)
  1343. {
  1344. static const char module[] = "TIFFStartTile";
  1345. TIFFDirectory *td = &tif->tif_dir;
  1346. uint32_t howmany32;
  1347. if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
  1348. {
  1349. if (!(*tif->tif_setupdecode)(tif))
  1350. return (0);
  1351. tif->tif_flags |= TIFF_CODERSETUP;
  1352. }
  1353. tif->tif_curtile = tile;
  1354. if (td->td_tilewidth == 0)
  1355. {
  1356. TIFFErrorExtR(tif, module, "Zero tilewidth");
  1357. return 0;
  1358. }
  1359. howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
  1360. if (howmany32 == 0)
  1361. {
  1362. TIFFErrorExtR(tif, module, "Zero tiles");
  1363. return 0;
  1364. }
  1365. tif->tif_row = (tile % howmany32) * td->td_tilelength;
  1366. howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
  1367. if (howmany32 == 0)
  1368. {
  1369. TIFFErrorExtR(tif, module, "Zero tiles");
  1370. return 0;
  1371. }
  1372. tif->tif_col = (tile % howmany32) * td->td_tilewidth;
  1373. tif->tif_flags &= ~TIFF_BUF4WRITE;
  1374. if (tif->tif_flags & TIFF_NOREADRAW)
  1375. {
  1376. tif->tif_rawcp = NULL;
  1377. tif->tif_rawcc = 0;
  1378. }
  1379. else
  1380. {
  1381. tif->tif_rawcp = tif->tif_rawdata;
  1382. if (tif->tif_rawdataloaded > 0)
  1383. tif->tif_rawcc = tif->tif_rawdataloaded;
  1384. else
  1385. tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
  1386. }
  1387. return (
  1388. (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
  1389. }
  1390. static int TIFFCheckRead(TIFF *tif, int tiles)
  1391. {
  1392. if (tif->tif_mode == O_WRONLY)
  1393. {
  1394. TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
  1395. return (0);
  1396. }
  1397. if (tiles ^ isTiled(tif))
  1398. {
  1399. TIFFErrorExtR(tif, tif->tif_name,
  1400. tiles ? "Can not read tiles from a striped image"
  1401. : "Can not read scanlines from a tiled image");
  1402. return (0);
  1403. }
  1404. return (1);
  1405. }
  1406. /* Use the provided input buffer (inbuf, insize) and decompress it into
  1407. * (outbuf, outsize).
  1408. * This function replaces the use of
  1409. * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the
  1410. * buffer for the input data, for example when he wants to avoid libtiff to read
  1411. * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts]
  1412. * array. inbuf content must be writable (if bit reversal is needed) Returns 1
  1413. * in case of success, 0 otherwise.
  1414. */
  1415. int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf,
  1416. tmsize_t insize, void *outbuf, tmsize_t outsize)
  1417. {
  1418. static const char module[] = "TIFFReadFromUserBuffer";
  1419. TIFFDirectory *td = &tif->tif_dir;
  1420. int ret = 1;
  1421. uint32_t old_tif_flags = tif->tif_flags;
  1422. tmsize_t old_rawdatasize = tif->tif_rawdatasize;
  1423. void *old_rawdata = tif->tif_rawdata;
  1424. if (tif->tif_mode == O_WRONLY)
  1425. {
  1426. TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
  1427. return 0;
  1428. }
  1429. if (tif->tif_flags & TIFF_NOREADRAW)
  1430. {
  1431. TIFFErrorExtR(tif, module,
  1432. "Compression scheme does not support access to raw "
  1433. "uncompressed data");
  1434. return 0;
  1435. }
  1436. tif->tif_flags &= ~TIFF_MYBUFFER;
  1437. tif->tif_flags |= TIFF_BUFFERMMAP;
  1438. tif->tif_rawdatasize = insize;
  1439. tif->tif_rawdata = inbuf;
  1440. tif->tif_rawdataoff = 0;
  1441. tif->tif_rawdataloaded = insize;
  1442. if (!isFillOrder(tif, td->td_fillorder) &&
  1443. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1444. {
  1445. TIFFReverseBits(inbuf, insize);
  1446. }
  1447. if (TIFFIsTiled(tif))
  1448. {
  1449. if (!TIFFStartTile(tif, strile))
  1450. {
  1451. ret = 0;
  1452. memset(outbuf, 0, (size_t)outsize);
  1453. }
  1454. else if (!(*tif->tif_decodetile)(
  1455. tif, (uint8_t *)outbuf, outsize,
  1456. (uint16_t)(strile / td->td_stripsperimage)))
  1457. {
  1458. ret = 0;
  1459. }
  1460. }
  1461. else
  1462. {
  1463. uint32_t rowsperstrip = td->td_rowsperstrip;
  1464. uint32_t stripsperplane;
  1465. if (rowsperstrip > td->td_imagelength)
  1466. rowsperstrip = td->td_imagelength;
  1467. if (rowsperstrip == 0)
  1468. {
  1469. TIFFErrorExtR(tif, module, "rowsperstrip is zero");
  1470. ret = 0;
  1471. }
  1472. else
  1473. {
  1474. stripsperplane =
  1475. TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
  1476. if (!TIFFStartStrip(tif, strile))
  1477. {
  1478. ret = 0;
  1479. memset(outbuf, 0, (size_t)outsize);
  1480. }
  1481. else if (!(*tif->tif_decodestrip)(
  1482. tif, (uint8_t *)outbuf, outsize,
  1483. (uint16_t)(strile / stripsperplane)))
  1484. {
  1485. ret = 0;
  1486. }
  1487. }
  1488. }
  1489. if (ret)
  1490. {
  1491. (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);
  1492. }
  1493. if (!isFillOrder(tif, td->td_fillorder) &&
  1494. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1495. {
  1496. TIFFReverseBits(inbuf, insize);
  1497. }
  1498. tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
  1499. (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));
  1500. tif->tif_rawdatasize = old_rawdatasize;
  1501. tif->tif_rawdata = old_rawdata;
  1502. tif->tif_rawdataoff = 0;
  1503. tif->tif_rawdataloaded = 0;
  1504. return ret;
  1505. }
  1506. void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1507. {
  1508. (void)tif;
  1509. (void)buf;
  1510. (void)cc;
  1511. }
  1512. void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1513. {
  1514. (void)tif;
  1515. assert((cc & 1) == 0);
  1516. TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
  1517. }
  1518. void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1519. {
  1520. (void)tif;
  1521. assert((cc % 3) == 0);
  1522. TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
  1523. }
  1524. void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1525. {
  1526. (void)tif;
  1527. assert((cc & 3) == 0);
  1528. TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
  1529. }
  1530. void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1531. {
  1532. (void)tif;
  1533. assert((cc & 7) == 0);
  1534. TIFFSwabArrayOfDouble((double *)buf, cc / 8);
  1535. }