tif_read.c 55 KB

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