tif_read.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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 =
  101. (uint8_t *)_TIFFrealloc(tif->tif_rawdata, 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. return (e > 0 ? 1 : -1);
  424. }
  425. /*
  426. * Calculate the strip size according to the number of
  427. * rows in the strip (check for truncated last strip on any
  428. * of the separations).
  429. */
  430. static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip,
  431. uint16_t *pplane)
  432. {
  433. static const char module[] = "TIFFReadEncodedStrip";
  434. TIFFDirectory *td = &tif->tif_dir;
  435. uint32_t rowsperstrip;
  436. uint32_t stripsperplane;
  437. uint32_t stripinplane;
  438. uint32_t rows;
  439. tmsize_t stripsize;
  440. if (!TIFFCheckRead(tif, 0))
  441. return ((tmsize_t)(-1));
  442. if (strip >= td->td_nstrips)
  443. {
  444. TIFFErrorExtR(tif, module,
  445. "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
  446. td->td_nstrips);
  447. return ((tmsize_t)(-1));
  448. }
  449. rowsperstrip = td->td_rowsperstrip;
  450. if (rowsperstrip > td->td_imagelength)
  451. rowsperstrip = td->td_imagelength;
  452. stripsperplane =
  453. TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
  454. stripinplane = (strip % stripsperplane);
  455. if (pplane)
  456. *pplane = (uint16_t)(strip / stripsperplane);
  457. rows = td->td_imagelength - stripinplane * rowsperstrip;
  458. if (rows > rowsperstrip)
  459. rows = rowsperstrip;
  460. stripsize = TIFFVStripSize(tif, rows);
  461. if (stripsize == 0)
  462. return ((tmsize_t)(-1));
  463. return stripsize;
  464. }
  465. /*
  466. * Read a strip of data and decompress the specified
  467. * amount into the user-supplied buffer.
  468. */
  469. tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf,
  470. tmsize_t size)
  471. {
  472. static const char module[] = "TIFFReadEncodedStrip";
  473. TIFFDirectory *td = &tif->tif_dir;
  474. tmsize_t stripsize;
  475. uint16_t plane;
  476. stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
  477. if (stripsize == ((tmsize_t)(-1)))
  478. return ((tmsize_t)(-1));
  479. /* shortcut to avoid an extra memcpy() */
  480. if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
  481. size >= stripsize && !isMapped(tif) &&
  482. ((tif->tif_flags & TIFF_NOREADRAW) == 0))
  483. {
  484. if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
  485. return ((tmsize_t)(-1));
  486. if (!isFillOrder(tif, td->td_fillorder) &&
  487. (tif->tif_flags & TIFF_NOBITREV) == 0)
  488. TIFFReverseBits(buf, stripsize);
  489. (*tif->tif_postdecode)(tif, buf, stripsize);
  490. return (stripsize);
  491. }
  492. if ((size != (tmsize_t)(-1)) && (size < stripsize))
  493. stripsize = size;
  494. if (!TIFFFillStrip(tif, strip))
  495. return ((tmsize_t)(-1));
  496. if ((*tif->tif_decodestrip)(tif, buf, stripsize, plane) <= 0)
  497. return ((tmsize_t)(-1));
  498. (*tif->tif_postdecode)(tif, buf, stripsize);
  499. return (stripsize);
  500. }
  501. /* Variant of TIFFReadEncodedStrip() that does
  502. * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
  503. * TIFFFillStrip() has succeeded. This avoid excessive memory allocation in case
  504. * of truncated file.
  505. * * calls regular TIFFReadEncodedStrip() if *buf != NULL
  506. */
  507. tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip,
  508. void **buf,
  509. tmsize_t bufsizetoalloc,
  510. tmsize_t size_to_read)
  511. {
  512. tmsize_t this_stripsize;
  513. uint16_t plane;
  514. if (*buf != NULL)
  515. {
  516. return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read);
  517. }
  518. this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane);
  519. if (this_stripsize == ((tmsize_t)(-1)))
  520. return ((tmsize_t)(-1));
  521. if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize))
  522. this_stripsize = size_to_read;
  523. if (!TIFFFillStrip(tif, strip))
  524. return ((tmsize_t)(-1));
  525. *buf = _TIFFmallocExt(tif, bufsizetoalloc);
  526. if (*buf == NULL)
  527. {
  528. TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer");
  529. return ((tmsize_t)(-1));
  530. }
  531. _TIFFmemset(*buf, 0, bufsizetoalloc);
  532. if ((*tif->tif_decodestrip)(tif, *buf, this_stripsize, plane) <= 0)
  533. return ((tmsize_t)(-1));
  534. (*tif->tif_postdecode)(tif, *buf, this_stripsize);
  535. return (this_stripsize);
  536. }
  537. static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf,
  538. tmsize_t size, const char *module)
  539. {
  540. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  541. if (!isMapped(tif))
  542. {
  543. tmsize_t cc;
  544. if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip)))
  545. {
  546. TIFFErrorExtR(tif, module,
  547. "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
  548. tif->tif_row, strip);
  549. return ((tmsize_t)(-1));
  550. }
  551. cc = TIFFReadFile(tif, buf, size);
  552. if (cc != size)
  553. {
  554. TIFFErrorExtR(tif, module,
  555. "Read error at scanline %" PRIu32
  556. "; got %" TIFF_SSIZE_FORMAT
  557. " bytes, expected %" TIFF_SSIZE_FORMAT,
  558. tif->tif_row, cc, size);
  559. return ((tmsize_t)(-1));
  560. }
  561. }
  562. else
  563. {
  564. tmsize_t ma = 0;
  565. tmsize_t n;
  566. if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
  567. ((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size))
  568. {
  569. n = 0;
  570. }
  571. else if (ma > TIFF_TMSIZE_T_MAX - size)
  572. {
  573. n = 0;
  574. }
  575. else
  576. {
  577. tmsize_t mb = ma + size;
  578. if (mb > tif->tif_size)
  579. n = tif->tif_size - ma;
  580. else
  581. n = size;
  582. }
  583. if (n != size)
  584. {
  585. TIFFErrorExtR(tif, module,
  586. "Read error at scanline %" PRIu32 ", strip %" PRIu32
  587. "; got %" TIFF_SSIZE_FORMAT
  588. " bytes, expected %" TIFF_SSIZE_FORMAT,
  589. tif->tif_row, strip, n, size);
  590. return ((tmsize_t)(-1));
  591. }
  592. _TIFFmemcpy(buf, tif->tif_base + ma, size);
  593. }
  594. return (size);
  595. }
  596. static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile,
  597. int is_strip, tmsize_t size,
  598. const char *module)
  599. {
  600. assert(!isMapped(tif));
  601. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  602. if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile)))
  603. {
  604. if (is_strip)
  605. {
  606. TIFFErrorExtR(tif, module,
  607. "Seek error at scanline %" PRIu32 ", strip %" PRIu32,
  608. tif->tif_row, strip_or_tile);
  609. }
  610. else
  611. {
  612. TIFFErrorExtR(tif, module,
  613. "Seek error at row %" PRIu32 ", col %" PRIu32
  614. ", tile %" PRIu32,
  615. tif->tif_row, tif->tif_col, strip_or_tile);
  616. }
  617. return ((tmsize_t)(-1));
  618. }
  619. if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module))
  620. {
  621. return ((tmsize_t)(-1));
  622. }
  623. return (size);
  624. }
  625. /*
  626. * Read a strip of data from the file.
  627. */
  628. tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size)
  629. {
  630. static const char module[] = "TIFFReadRawStrip";
  631. TIFFDirectory *td = &tif->tif_dir;
  632. uint64_t bytecount64;
  633. tmsize_t bytecountm;
  634. if (!TIFFCheckRead(tif, 0))
  635. return ((tmsize_t)(-1));
  636. if (strip >= td->td_nstrips)
  637. {
  638. TIFFErrorExtR(tif, module,
  639. "%" PRIu32 ": Strip out of range, max %" PRIu32, strip,
  640. td->td_nstrips);
  641. return ((tmsize_t)(-1));
  642. }
  643. if (tif->tif_flags & TIFF_NOREADRAW)
  644. {
  645. TIFFErrorExtR(tif, module,
  646. "Compression scheme does not support access to raw "
  647. "uncompressed data");
  648. return ((tmsize_t)(-1));
  649. }
  650. bytecount64 = TIFFGetStrileByteCount(tif, strip);
  651. if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
  652. bytecountm = size;
  653. else
  654. bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
  655. if (bytecountm == 0)
  656. {
  657. return ((tmsize_t)(-1));
  658. }
  659. return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
  660. }
  661. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  662. static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; }
  663. /*
  664. * Read the specified strip and setup for decoding. The data buffer is
  665. * expanded, as necessary, to hold the strip's data.
  666. */
  667. int TIFFFillStrip(TIFF *tif, uint32_t strip)
  668. {
  669. static const char module[] = "TIFFFillStrip";
  670. TIFFDirectory *td = &tif->tif_dir;
  671. if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
  672. {
  673. uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
  674. if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
  675. {
  676. TIFFErrorExtR(tif, module,
  677. "Invalid strip byte count %" PRIu64
  678. ", strip %" PRIu32,
  679. bytecount, strip);
  680. return (0);
  681. }
  682. /* To avoid excessive memory allocations: */
  683. /* Byte count should normally not be larger than a number of */
  684. /* times the uncompressed size plus some margin */
  685. if (bytecount > 1024 * 1024)
  686. {
  687. /* 10 and 4096 are just values that could be adjusted. */
  688. /* Hopefully they are safe enough for all codecs */
  689. tmsize_t stripsize = TIFFStripSize(tif);
  690. if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
  691. {
  692. uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
  693. TIFFErrorExtR(tif, module,
  694. "Too large strip byte count %" PRIu64
  695. ", strip %" PRIu32 ". Limiting to %" PRIu64,
  696. bytecount, strip, newbytecount);
  697. bytecount = newbytecount;
  698. }
  699. }
  700. if (isMapped(tif))
  701. {
  702. /*
  703. * We must check for overflow, potentially causing
  704. * an OOB read. Instead of simple
  705. *
  706. * TIFFGetStrileOffset(tif, strip)+bytecount > tif->tif_size
  707. *
  708. * comparison (which can overflow) we do the following
  709. * two comparisons:
  710. */
  711. if (bytecount > (uint64_t)tif->tif_size ||
  712. TIFFGetStrileOffset(tif, strip) >
  713. (uint64_t)tif->tif_size - bytecount)
  714. {
  715. /*
  716. * This error message might seem strange, but
  717. * it's what would happen if a read were done
  718. * instead.
  719. */
  720. TIFFErrorExtR(
  721. tif, module,
  722. "Read error on strip %" PRIu32 "; "
  723. "got %" PRIu64 " bytes, expected %" PRIu64,
  724. strip,
  725. NoSanitizeSubUInt64(tif->tif_size,
  726. TIFFGetStrileOffset(tif, strip)),
  727. bytecount);
  728. tif->tif_curstrip = NOSTRIP;
  729. return (0);
  730. }
  731. }
  732. if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
  733. (tif->tif_flags & TIFF_NOBITREV)))
  734. {
  735. /*
  736. * The image is mapped into memory and we either don't
  737. * need to flip bits or the compression routine is
  738. * going to handle this operation itself. In this
  739. * case, avoid copying the raw data and instead just
  740. * reference the data from the memory mapped file
  741. * image. This assumes that the decompression
  742. * routines do not modify the contents of the raw data
  743. * buffer (if they try to, the application will get a
  744. * fault since the file is mapped read-only).
  745. */
  746. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  747. {
  748. _TIFFfreeExt(tif, tif->tif_rawdata);
  749. tif->tif_rawdata = NULL;
  750. tif->tif_rawdatasize = 0;
  751. }
  752. tif->tif_flags &= ~TIFF_MYBUFFER;
  753. tif->tif_rawdatasize = (tmsize_t)bytecount;
  754. tif->tif_rawdata =
  755. tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip);
  756. tif->tif_rawdataoff = 0;
  757. tif->tif_rawdataloaded = (tmsize_t)bytecount;
  758. /*
  759. * When we have tif_rawdata reference directly into the memory
  760. * mapped file we need to be pretty careful about how we use the
  761. * rawdata. It is not a general purpose working buffer as it
  762. * normally otherwise is. So we keep track of this fact to avoid
  763. * using it improperly.
  764. */
  765. tif->tif_flags |= TIFF_BUFFERMMAP;
  766. }
  767. else
  768. {
  769. /*
  770. * Expand raw data buffer, if needed, to hold data
  771. * strip coming from file (perhaps should set upper
  772. * bound on the size of a buffer we'll use?).
  773. */
  774. tmsize_t bytecountm;
  775. bytecountm = (tmsize_t)bytecount;
  776. if ((uint64_t)bytecountm != bytecount)
  777. {
  778. TIFFErrorExtR(tif, module, "Integer overflow");
  779. return (0);
  780. }
  781. if (bytecountm > tif->tif_rawdatasize)
  782. {
  783. tif->tif_curstrip = NOSTRIP;
  784. if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
  785. {
  786. TIFFErrorExtR(
  787. tif, module,
  788. "Data buffer too small to hold strip %" PRIu32, strip);
  789. return (0);
  790. }
  791. }
  792. if (tif->tif_flags & TIFF_BUFFERMMAP)
  793. {
  794. tif->tif_curstrip = NOSTRIP;
  795. tif->tif_rawdata = NULL;
  796. tif->tif_rawdatasize = 0;
  797. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  798. }
  799. if (isMapped(tif))
  800. {
  801. if (bytecountm > tif->tif_rawdatasize &&
  802. !TIFFReadBufferSetup(tif, 0, bytecountm))
  803. {
  804. return (0);
  805. }
  806. if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm,
  807. module) != bytecountm)
  808. {
  809. return (0);
  810. }
  811. }
  812. else
  813. {
  814. if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm,
  815. module) != bytecountm)
  816. {
  817. return (0);
  818. }
  819. }
  820. tif->tif_rawdataoff = 0;
  821. tif->tif_rawdataloaded = bytecountm;
  822. if (!isFillOrder(tif, td->td_fillorder) &&
  823. (tif->tif_flags & TIFF_NOBITREV) == 0)
  824. TIFFReverseBits(tif->tif_rawdata, bytecountm);
  825. }
  826. }
  827. return (TIFFStartStrip(tif, strip));
  828. }
  829. /*
  830. * Tile-oriented Read Support
  831. * Contributed by Nancy Cam (Silicon Graphics).
  832. */
  833. /*
  834. * Read and decompress a tile of data. The
  835. * tile is selected by the (x,y,z,s) coordinates.
  836. */
  837. tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z,
  838. uint16_t s)
  839. {
  840. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  841. return ((tmsize_t)(-1));
  842. return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf,
  843. (tmsize_t)(-1)));
  844. }
  845. /*
  846. * Read a tile of data and decompress the specified
  847. * amount into the user-supplied buffer.
  848. */
  849. tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
  850. {
  851. static const char module[] = "TIFFReadEncodedTile";
  852. TIFFDirectory *td = &tif->tif_dir;
  853. tmsize_t tilesize = tif->tif_tilesize;
  854. if (!TIFFCheckRead(tif, 1))
  855. return ((tmsize_t)(-1));
  856. if (tile >= td->td_nstrips)
  857. {
  858. TIFFErrorExtR(tif, module,
  859. "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
  860. td->td_nstrips);
  861. return ((tmsize_t)(-1));
  862. }
  863. /* shortcut to avoid an extra memcpy() */
  864. if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) &&
  865. size >= tilesize && !isMapped(tif) &&
  866. ((tif->tif_flags & TIFF_NOREADRAW) == 0))
  867. {
  868. if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
  869. return ((tmsize_t)(-1));
  870. if (!isFillOrder(tif, td->td_fillorder) &&
  871. (tif->tif_flags & TIFF_NOBITREV) == 0)
  872. TIFFReverseBits(buf, tilesize);
  873. (*tif->tif_postdecode)(tif, buf, tilesize);
  874. return (tilesize);
  875. }
  876. if (size == (tmsize_t)(-1))
  877. size = tilesize;
  878. else if (size > tilesize)
  879. size = tilesize;
  880. if (TIFFFillTile(tif, tile) &&
  881. (*tif->tif_decodetile)(tif, (uint8_t *)buf, size,
  882. (uint16_t)(tile / td->td_stripsperimage)))
  883. {
  884. (*tif->tif_postdecode)(tif, (uint8_t *)buf, size);
  885. return (size);
  886. }
  887. else
  888. return ((tmsize_t)(-1));
  889. }
  890. /* Variant of TIFFReadTile() that does
  891. * * if *buf == NULL, *buf = _TIFFmallocExt(tif, bufsizetoalloc) only after
  892. * TIFFFillTile() has succeeded. This avoid excessive memory allocation in case
  893. * of truncated file.
  894. * * calls regular TIFFReadEncodedTile() if *buf != NULL
  895. */
  896. tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf,
  897. tmsize_t bufsizetoalloc, uint32_t x,
  898. uint32_t y, uint32_t z, uint16_t s)
  899. {
  900. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  901. return ((tmsize_t)(-1));
  902. return (_TIFFReadEncodedTileAndAllocBuffer(
  903. tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc,
  904. (tmsize_t)(-1)));
  905. }
  906. /* Variant of TIFFReadEncodedTile() 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 _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile,
  913. void **buf, tmsize_t bufsizetoalloc,
  914. tmsize_t size_to_read)
  915. {
  916. static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer";
  917. TIFFDirectory *td = &tif->tif_dir;
  918. tmsize_t tilesize = tif->tif_tilesize;
  919. if (*buf != NULL)
  920. {
  921. return TIFFReadEncodedTile(tif, tile, *buf, size_to_read);
  922. }
  923. if (!TIFFCheckRead(tif, 1))
  924. return ((tmsize_t)(-1));
  925. if (tile >= td->td_nstrips)
  926. {
  927. TIFFErrorExtR(tif, module,
  928. "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
  929. td->td_nstrips);
  930. return ((tmsize_t)(-1));
  931. }
  932. if (!TIFFFillTile(tif, tile))
  933. return ((tmsize_t)(-1));
  934. /* Sanity checks to avoid excessive memory allocation */
  935. /* Cf https://gitlab.com/libtiff/libtiff/-/issues/479 */
  936. if (td->td_compression == COMPRESSION_NONE)
  937. {
  938. if (tif->tif_rawdatasize != tilesize)
  939. {
  940. TIFFErrorExtR(tif, TIFFFileName(tif),
  941. "Invalid tile byte count for tile %u. "
  942. "Expected %" PRIu64 ", got %" PRIu64,
  943. tile, (uint64_t)tilesize,
  944. (uint64_t)tif->tif_rawdatasize);
  945. return ((tmsize_t)(-1));
  946. }
  947. }
  948. else
  949. {
  950. /* Max compression ratio experimentally determined. Might be fragile...
  951. * Only apply this heuristics to situations where the memory allocation
  952. * would be big, to avoid breaking nominal use cases.
  953. */
  954. const int maxCompressionRatio =
  955. td->td_compression == COMPRESSION_ZSTD ? 33000
  956. : td->td_compression == COMPRESSION_JXL
  957. ?
  958. /* Evaluated on a 8000x8000 tile */
  959. 25000 * (td->td_planarconfig == PLANARCONFIG_CONTIG
  960. ? td->td_samplesperpixel
  961. : 1)
  962. : td->td_compression == COMPRESSION_LZMA ? 7000 : 1000;
  963. if (bufsizetoalloc > 100 * 1000 * 1000 &&
  964. tif->tif_rawdatasize < tilesize / maxCompressionRatio)
  965. {
  966. TIFFErrorExtR(tif, TIFFFileName(tif),
  967. "Likely invalid tile byte count for tile %u. "
  968. "Uncompressed tile size is %" PRIu64 ", "
  969. "compressed one is %" PRIu64,
  970. tile, (uint64_t)tilesize,
  971. (uint64_t)tif->tif_rawdatasize);
  972. return ((tmsize_t)(-1));
  973. }
  974. }
  975. *buf = _TIFFmallocExt(tif, bufsizetoalloc);
  976. if (*buf == NULL)
  977. {
  978. TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer");
  979. return ((tmsize_t)(-1));
  980. }
  981. _TIFFmemset(*buf, 0, bufsizetoalloc);
  982. if (size_to_read == (tmsize_t)(-1))
  983. size_to_read = tilesize;
  984. else if (size_to_read > tilesize)
  985. size_to_read = tilesize;
  986. if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read,
  987. (uint16_t)(tile / td->td_stripsperimage)))
  988. {
  989. (*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read);
  990. return (size_to_read);
  991. }
  992. else
  993. return ((tmsize_t)(-1));
  994. }
  995. static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf,
  996. tmsize_t size, const char *module)
  997. {
  998. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  999. if (!isMapped(tif))
  1000. {
  1001. tmsize_t cc;
  1002. if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile)))
  1003. {
  1004. TIFFErrorExtR(tif, module,
  1005. "Seek error at row %" PRIu32 ", col %" PRIu32
  1006. ", tile %" PRIu32,
  1007. tif->tif_row, tif->tif_col, tile);
  1008. return ((tmsize_t)(-1));
  1009. }
  1010. cc = TIFFReadFile(tif, buf, size);
  1011. if (cc != size)
  1012. {
  1013. TIFFErrorExtR(tif, module,
  1014. "Read error at row %" PRIu32 ", col %" PRIu32
  1015. "; got %" TIFF_SSIZE_FORMAT
  1016. " bytes, expected %" TIFF_SSIZE_FORMAT,
  1017. tif->tif_row, tif->tif_col, cc, size);
  1018. return ((tmsize_t)(-1));
  1019. }
  1020. }
  1021. else
  1022. {
  1023. tmsize_t ma, mb;
  1024. tmsize_t n;
  1025. ma = (tmsize_t)TIFFGetStrileOffset(tif, tile);
  1026. mb = ma + size;
  1027. if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) ||
  1028. (ma > tif->tif_size))
  1029. n = 0;
  1030. else if ((mb < ma) || (mb < size) || (mb > tif->tif_size))
  1031. n = tif->tif_size - ma;
  1032. else
  1033. n = size;
  1034. if (n != size)
  1035. {
  1036. TIFFErrorExtR(tif, module,
  1037. "Read error at row %" PRIu32 ", col %" PRIu32
  1038. ", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT
  1039. " bytes, expected %" TIFF_SSIZE_FORMAT,
  1040. tif->tif_row, tif->tif_col, tile, n, size);
  1041. return ((tmsize_t)(-1));
  1042. }
  1043. _TIFFmemcpy(buf, tif->tif_base + ma, size);
  1044. }
  1045. return (size);
  1046. }
  1047. /*
  1048. * Read a tile of data from the file.
  1049. */
  1050. tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size)
  1051. {
  1052. static const char module[] = "TIFFReadRawTile";
  1053. TIFFDirectory *td = &tif->tif_dir;
  1054. uint64_t bytecount64;
  1055. tmsize_t bytecountm;
  1056. if (!TIFFCheckRead(tif, 1))
  1057. return ((tmsize_t)(-1));
  1058. if (tile >= td->td_nstrips)
  1059. {
  1060. TIFFErrorExtR(tif, module,
  1061. "%" PRIu32 ": Tile out of range, max %" PRIu32, tile,
  1062. td->td_nstrips);
  1063. return ((tmsize_t)(-1));
  1064. }
  1065. if (tif->tif_flags & TIFF_NOREADRAW)
  1066. {
  1067. TIFFErrorExtR(tif, module,
  1068. "Compression scheme does not support access to raw "
  1069. "uncompressed data");
  1070. return ((tmsize_t)(-1));
  1071. }
  1072. bytecount64 = TIFFGetStrileByteCount(tif, tile);
  1073. if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64)
  1074. bytecountm = size;
  1075. else
  1076. bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module);
  1077. if (bytecountm == 0)
  1078. {
  1079. return ((tmsize_t)(-1));
  1080. }
  1081. return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
  1082. }
  1083. /*
  1084. * Read the specified tile and setup for decoding. The data buffer is
  1085. * expanded, as necessary, to hold the tile's data.
  1086. */
  1087. int TIFFFillTile(TIFF *tif, uint32_t tile)
  1088. {
  1089. static const char module[] = "TIFFFillTile";
  1090. TIFFDirectory *td = &tif->tif_dir;
  1091. if ((tif->tif_flags & TIFF_NOREADRAW) == 0)
  1092. {
  1093. uint64_t bytecount = TIFFGetStrileByteCount(tif, tile);
  1094. if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX)
  1095. {
  1096. TIFFErrorExtR(tif, module,
  1097. "%" PRIu64 ": Invalid tile byte count, tile %" PRIu32,
  1098. bytecount, tile);
  1099. return (0);
  1100. }
  1101. /* To avoid excessive memory allocations: */
  1102. /* Byte count should normally not be larger than a number of */
  1103. /* times the uncompressed size plus some margin */
  1104. if (bytecount > 1024 * 1024)
  1105. {
  1106. /* 10 and 4096 are just values that could be adjusted. */
  1107. /* Hopefully they are safe enough for all codecs */
  1108. tmsize_t stripsize = TIFFTileSize(tif);
  1109. if (stripsize != 0 && (bytecount - 4096) / 10 > (uint64_t)stripsize)
  1110. {
  1111. uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096;
  1112. TIFFErrorExtR(tif, module,
  1113. "Too large tile byte count %" PRIu64
  1114. ", tile %" PRIu32 ". Limiting to %" PRIu64,
  1115. bytecount, tile, newbytecount);
  1116. bytecount = newbytecount;
  1117. }
  1118. }
  1119. if (isMapped(tif))
  1120. {
  1121. /*
  1122. * We must check for overflow, potentially causing
  1123. * an OOB read. Instead of simple
  1124. *
  1125. * TIFFGetStrileOffset(tif, tile)+bytecount > tif->tif_size
  1126. *
  1127. * comparison (which can overflow) we do the following
  1128. * two comparisons:
  1129. */
  1130. if (bytecount > (uint64_t)tif->tif_size ||
  1131. TIFFGetStrileOffset(tif, tile) >
  1132. (uint64_t)tif->tif_size - bytecount)
  1133. {
  1134. tif->tif_curtile = NOTILE;
  1135. return (0);
  1136. }
  1137. }
  1138. if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) ||
  1139. (tif->tif_flags & TIFF_NOBITREV)))
  1140. {
  1141. /*
  1142. * The image is mapped into memory and we either don't
  1143. * need to flip bits or the compression routine is
  1144. * going to handle this operation itself. In this
  1145. * case, avoid copying the raw data and instead just
  1146. * reference the data from the memory mapped file
  1147. * image. This assumes that the decompression
  1148. * routines do not modify the contents of the raw data
  1149. * buffer (if they try to, the application will get a
  1150. * fault since the file is mapped read-only).
  1151. */
  1152. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
  1153. {
  1154. _TIFFfreeExt(tif, tif->tif_rawdata);
  1155. tif->tif_rawdata = NULL;
  1156. tif->tif_rawdatasize = 0;
  1157. }
  1158. tif->tif_flags &= ~TIFF_MYBUFFER;
  1159. tif->tif_rawdatasize = (tmsize_t)bytecount;
  1160. tif->tif_rawdata =
  1161. tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile);
  1162. tif->tif_rawdataoff = 0;
  1163. tif->tif_rawdataloaded = (tmsize_t)bytecount;
  1164. tif->tif_flags |= TIFF_BUFFERMMAP;
  1165. }
  1166. else
  1167. {
  1168. /*
  1169. * Expand raw data buffer, if needed, to hold data
  1170. * tile coming from file (perhaps should set upper
  1171. * bound on the size of a buffer we'll use?).
  1172. */
  1173. tmsize_t bytecountm;
  1174. bytecountm = (tmsize_t)bytecount;
  1175. if ((uint64_t)bytecountm != bytecount)
  1176. {
  1177. TIFFErrorExtR(tif, module, "Integer overflow");
  1178. return (0);
  1179. }
  1180. if (bytecountm > tif->tif_rawdatasize)
  1181. {
  1182. tif->tif_curtile = NOTILE;
  1183. if ((tif->tif_flags & TIFF_MYBUFFER) == 0)
  1184. {
  1185. TIFFErrorExtR(tif, module,
  1186. "Data buffer too small to hold tile %" PRIu32,
  1187. tile);
  1188. return (0);
  1189. }
  1190. }
  1191. if (tif->tif_flags & TIFF_BUFFERMMAP)
  1192. {
  1193. tif->tif_curtile = NOTILE;
  1194. tif->tif_rawdata = NULL;
  1195. tif->tif_rawdatasize = 0;
  1196. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  1197. }
  1198. if (isMapped(tif))
  1199. {
  1200. if (bytecountm > tif->tif_rawdatasize &&
  1201. !TIFFReadBufferSetup(tif, 0, bytecountm))
  1202. {
  1203. return (0);
  1204. }
  1205. if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm,
  1206. module) != bytecountm)
  1207. {
  1208. return (0);
  1209. }
  1210. }
  1211. else
  1212. {
  1213. if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) !=
  1214. bytecountm)
  1215. {
  1216. return (0);
  1217. }
  1218. }
  1219. tif->tif_rawdataoff = 0;
  1220. tif->tif_rawdataloaded = bytecountm;
  1221. if (tif->tif_rawdata != NULL &&
  1222. !isFillOrder(tif, td->td_fillorder) &&
  1223. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1224. TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded);
  1225. }
  1226. }
  1227. return (TIFFStartTile(tif, tile));
  1228. }
  1229. /*
  1230. * Setup the raw data buffer in preparation for
  1231. * reading a strip of raw data. If the buffer
  1232. * is specified as zero, then a buffer of appropriate
  1233. * size is allocated by the library. Otherwise,
  1234. * the client must guarantee that the buffer is
  1235. * large enough to hold any individual strip of
  1236. * raw data.
  1237. */
  1238. int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size)
  1239. {
  1240. static const char module[] = "TIFFReadBufferSetup";
  1241. assert((tif->tif_flags & TIFF_NOREADRAW) == 0);
  1242. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  1243. if (tif->tif_rawdata)
  1244. {
  1245. if (tif->tif_flags & TIFF_MYBUFFER)
  1246. _TIFFfreeExt(tif, tif->tif_rawdata);
  1247. tif->tif_rawdata = NULL;
  1248. tif->tif_rawdatasize = 0;
  1249. }
  1250. if (bp)
  1251. {
  1252. tif->tif_rawdatasize = size;
  1253. tif->tif_rawdata = (uint8_t *)bp;
  1254. tif->tif_flags &= ~TIFF_MYBUFFER;
  1255. }
  1256. else
  1257. {
  1258. tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024);
  1259. if (tif->tif_rawdatasize == 0)
  1260. {
  1261. TIFFErrorExtR(tif, module, "Invalid buffer size");
  1262. return (0);
  1263. }
  1264. /* Initialize to zero to avoid uninitialized buffers in case of */
  1265. /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
  1266. tif->tif_rawdata =
  1267. (uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize);
  1268. tif->tif_flags |= TIFF_MYBUFFER;
  1269. }
  1270. if (tif->tif_rawdata == NULL)
  1271. {
  1272. TIFFErrorExtR(tif, module,
  1273. "No space for data buffer at scanline %" PRIu32,
  1274. tif->tif_row);
  1275. tif->tif_rawdatasize = 0;
  1276. return (0);
  1277. }
  1278. return (1);
  1279. }
  1280. /*
  1281. * Set state to appear as if a
  1282. * strip has just been read in.
  1283. */
  1284. static int TIFFStartStrip(TIFF *tif, uint32_t strip)
  1285. {
  1286. TIFFDirectory *td = &tif->tif_dir;
  1287. if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
  1288. {
  1289. if (!(*tif->tif_setupdecode)(tif))
  1290. return (0);
  1291. tif->tif_flags |= TIFF_CODERSETUP;
  1292. }
  1293. tif->tif_curstrip = strip;
  1294. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  1295. tif->tif_flags &= ~TIFF_BUF4WRITE;
  1296. if (tif->tif_flags & TIFF_NOREADRAW)
  1297. {
  1298. tif->tif_rawcp = NULL;
  1299. tif->tif_rawcc = 0;
  1300. }
  1301. else
  1302. {
  1303. tif->tif_rawcp = tif->tif_rawdata;
  1304. if (tif->tif_rawdataloaded > 0)
  1305. tif->tif_rawcc = tif->tif_rawdataloaded;
  1306. else
  1307. tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip);
  1308. }
  1309. if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) ==
  1310. 0)
  1311. {
  1312. /* Needed for example for scanline access, if tif_predecode */
  1313. /* fails, and we try to read the same strip again. Without invalidating
  1314. */
  1315. /* tif_curstrip, we'd call tif_decoderow() on a possibly invalid */
  1316. /* codec state. */
  1317. tif->tif_curstrip = NOSTRIP;
  1318. return 0;
  1319. }
  1320. return 1;
  1321. }
  1322. /*
  1323. * Set state to appear as if a
  1324. * tile has just been read in.
  1325. */
  1326. static int TIFFStartTile(TIFF *tif, uint32_t tile)
  1327. {
  1328. static const char module[] = "TIFFStartTile";
  1329. TIFFDirectory *td = &tif->tif_dir;
  1330. uint32_t howmany32;
  1331. if ((tif->tif_flags & TIFF_CODERSETUP) == 0)
  1332. {
  1333. if (!(*tif->tif_setupdecode)(tif))
  1334. return (0);
  1335. tif->tif_flags |= TIFF_CODERSETUP;
  1336. }
  1337. tif->tif_curtile = tile;
  1338. howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
  1339. if (howmany32 == 0)
  1340. {
  1341. TIFFErrorExtR(tif, module, "Zero tiles");
  1342. return 0;
  1343. }
  1344. tif->tif_row = (tile % howmany32) * td->td_tilelength;
  1345. howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
  1346. if (howmany32 == 0)
  1347. {
  1348. TIFFErrorExtR(tif, module, "Zero tiles");
  1349. return 0;
  1350. }
  1351. tif->tif_col = (tile % howmany32) * td->td_tilewidth;
  1352. tif->tif_flags &= ~TIFF_BUF4WRITE;
  1353. if (tif->tif_flags & TIFF_NOREADRAW)
  1354. {
  1355. tif->tif_rawcp = NULL;
  1356. tif->tif_rawcc = 0;
  1357. }
  1358. else
  1359. {
  1360. tif->tif_rawcp = tif->tif_rawdata;
  1361. if (tif->tif_rawdataloaded > 0)
  1362. tif->tif_rawcc = tif->tif_rawdataloaded;
  1363. else
  1364. tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile);
  1365. }
  1366. return (
  1367. (*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage)));
  1368. }
  1369. static int TIFFCheckRead(TIFF *tif, int tiles)
  1370. {
  1371. if (tif->tif_mode == O_WRONLY)
  1372. {
  1373. TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
  1374. return (0);
  1375. }
  1376. if (tiles ^ isTiled(tif))
  1377. {
  1378. TIFFErrorExtR(tif, tif->tif_name,
  1379. tiles ? "Can not read tiles from a striped image"
  1380. : "Can not read scanlines from a tiled image");
  1381. return (0);
  1382. }
  1383. return (1);
  1384. }
  1385. /* Use the provided input buffer (inbuf, insize) and decompress it into
  1386. * (outbuf, outsize).
  1387. * This function replaces the use of
  1388. * TIFFReadEncodedStrip()/TIFFReadEncodedTile() when the user can provide the
  1389. * buffer for the input data, for example when he wants to avoid libtiff to read
  1390. * the strile offset/count values from the [Strip|Tile][Offsets/ByteCounts]
  1391. * array. inbuf content must be writable (if bit reversal is needed) Returns 1
  1392. * in case of success, 0 otherwise.
  1393. */
  1394. int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf,
  1395. tmsize_t insize, void *outbuf, tmsize_t outsize)
  1396. {
  1397. static const char module[] = "TIFFReadFromUserBuffer";
  1398. TIFFDirectory *td = &tif->tif_dir;
  1399. int ret = 1;
  1400. uint32_t old_tif_flags = tif->tif_flags;
  1401. tmsize_t old_rawdatasize = tif->tif_rawdatasize;
  1402. void *old_rawdata = tif->tif_rawdata;
  1403. if (tif->tif_mode == O_WRONLY)
  1404. {
  1405. TIFFErrorExtR(tif, tif->tif_name, "File not open for reading");
  1406. return 0;
  1407. }
  1408. if (tif->tif_flags & TIFF_NOREADRAW)
  1409. {
  1410. TIFFErrorExtR(tif, module,
  1411. "Compression scheme does not support access to raw "
  1412. "uncompressed data");
  1413. return 0;
  1414. }
  1415. tif->tif_flags &= ~TIFF_MYBUFFER;
  1416. tif->tif_flags |= TIFF_BUFFERMMAP;
  1417. tif->tif_rawdatasize = insize;
  1418. tif->tif_rawdata = inbuf;
  1419. tif->tif_rawdataoff = 0;
  1420. tif->tif_rawdataloaded = insize;
  1421. if (!isFillOrder(tif, td->td_fillorder) &&
  1422. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1423. {
  1424. TIFFReverseBits(inbuf, insize);
  1425. }
  1426. if (TIFFIsTiled(tif))
  1427. {
  1428. if (!TIFFStartTile(tif, strile) ||
  1429. !(*tif->tif_decodetile)(tif, (uint8_t *)outbuf, outsize,
  1430. (uint16_t)(strile / td->td_stripsperimage)))
  1431. {
  1432. ret = 0;
  1433. }
  1434. }
  1435. else
  1436. {
  1437. uint32_t rowsperstrip = td->td_rowsperstrip;
  1438. uint32_t stripsperplane;
  1439. if (rowsperstrip > td->td_imagelength)
  1440. rowsperstrip = td->td_imagelength;
  1441. stripsperplane =
  1442. TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
  1443. if (!TIFFStartStrip(tif, strile) ||
  1444. !(*tif->tif_decodestrip)(tif, (uint8_t *)outbuf, outsize,
  1445. (uint16_t)(strile / stripsperplane)))
  1446. {
  1447. ret = 0;
  1448. }
  1449. }
  1450. if (ret)
  1451. {
  1452. (*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize);
  1453. }
  1454. if (!isFillOrder(tif, td->td_fillorder) &&
  1455. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1456. {
  1457. TIFFReverseBits(inbuf, insize);
  1458. }
  1459. tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) |
  1460. (tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP));
  1461. tif->tif_rawdatasize = old_rawdatasize;
  1462. tif->tif_rawdata = old_rawdata;
  1463. tif->tif_rawdataoff = 0;
  1464. tif->tif_rawdataloaded = 0;
  1465. return ret;
  1466. }
  1467. void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1468. {
  1469. (void)tif;
  1470. (void)buf;
  1471. (void)cc;
  1472. }
  1473. void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1474. {
  1475. (void)tif;
  1476. assert((cc & 1) == 0);
  1477. TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2);
  1478. }
  1479. void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1480. {
  1481. (void)tif;
  1482. assert((cc % 3) == 0);
  1483. TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3);
  1484. }
  1485. void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1486. {
  1487. (void)tif;
  1488. assert((cc & 3) == 0);
  1489. TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4);
  1490. }
  1491. void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc)
  1492. {
  1493. (void)tif;
  1494. assert((cc & 7) == 0);
  1495. TIFFSwabArrayOfDouble((double *)buf, cc / 8);
  1496. }