tif_read.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /* $Id: tif_read.c,v 1.59 2017-05-13 15:34:06 erouault Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. * Scanline-oriented Read Support
  28. */
  29. #include "tiffiop.h"
  30. #include <stdio.h>
  31. #define TIFF_SIZE_T_MAX ((size_t) ~ ((size_t)0))
  32. #define TIFF_TMSIZE_T_MAX (tmsize_t)(TIFF_SIZE_T_MAX >> 1)
  33. int TIFFFillStrip(TIFF* tif, uint32 strip);
  34. int TIFFFillTile(TIFF* tif, uint32 tile);
  35. static int TIFFStartStrip(TIFF* tif, uint32 strip);
  36. static int TIFFStartTile(TIFF* tif, uint32 tile);
  37. static int TIFFCheckRead(TIFF*, int);
  38. static tmsize_t
  39. TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
  40. static tmsize_t
  41. TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module);
  42. #define NOSTRIP ((uint32)(-1)) /* undefined state */
  43. #define NOTILE ((uint32)(-1)) /* undefined state */
  44. #define INITIAL_THRESHOLD (1024 * 1024)
  45. #define THRESHOLD_MULTIPLIER 10
  46. #define MAX_THRESHOLD (THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * INITIAL_THRESHOLD)
  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,
  50. tmsize_t rawdata_offset,
  51. int is_strip, uint32 strip_or_tile,
  52. const char* module )
  53. {
  54. #if X64 // SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8 ESENTHEL CHANGED
  55. tmsize_t threshold = INITIAL_THRESHOLD;
  56. #endif
  57. tmsize_t already_read = 0;
  58. /* On 64 bit processes, read first a maximum of 1 MB, then 10 MB, etc */
  59. /* so as to avoid allocating too much memory in case the file is too */
  60. /* short. We could ask for the file size, but this might be */
  61. /* expensive with some I/O layers (think of reading a gzipped file) */
  62. /* Restrict to 64 bit processes, so as to avoid reallocs() */
  63. /* on 32 bit processes where virtual memory is scarce. */
  64. while( already_read < size )
  65. {
  66. tmsize_t bytes_read;
  67. tmsize_t to_read = size - already_read;
  68. #if X64 // SIZEOF_VOIDP == 8 || SIZEOF_SIZE_T == 8 ESENTHEL CHANGED
  69. if( to_read >= threshold && threshold < MAX_THRESHOLD &&
  70. already_read + to_read + rawdata_offset > tif->tif_rawdatasize )
  71. {
  72. to_read = threshold;
  73. threshold *= THRESHOLD_MULTIPLIER;
  74. }
  75. #endif
  76. if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize) {
  77. uint8* new_rawdata;
  78. assert((tif->tif_flags & TIFF_MYBUFFER) != 0);
  79. tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64(
  80. (uint64)already_read + to_read + rawdata_offset, 1024);
  81. if (tif->tif_rawdatasize==0) {
  82. TIFFErrorExt(tif->tif_clientdata, module,
  83. "Invalid buffer size");
  84. return 0;
  85. }
  86. new_rawdata = (uint8*) _TIFFrealloc(
  87. tif->tif_rawdata, tif->tif_rawdatasize);
  88. if( new_rawdata == 0 )
  89. {
  90. TIFFErrorExt(tif->tif_clientdata, module,
  91. "No space for data buffer at scanline %lu",
  92. (unsigned long) tif->tif_row);
  93. _TIFFfree(tif->tif_rawdata);
  94. tif->tif_rawdata = 0;
  95. tif->tif_rawdatasize = 0;
  96. return 0;
  97. }
  98. tif->tif_rawdata = new_rawdata;
  99. }
  100. bytes_read = TIFFReadFile(tif,
  101. tif->tif_rawdata + rawdata_offset + already_read, to_read);
  102. already_read += bytes_read;
  103. if (bytes_read != to_read) {
  104. memset( tif->tif_rawdata + rawdata_offset + already_read, 0,
  105. tif->tif_rawdatasize - rawdata_offset - already_read );
  106. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  107. if( is_strip )
  108. {
  109. TIFFErrorExt(tif->tif_clientdata, module,
  110. "Read error at scanline %lu; got %I64u bytes, "
  111. "expected %I64u",
  112. (unsigned long) tif->tif_row,
  113. (unsigned __int64) already_read,
  114. (unsigned __int64) size);
  115. }
  116. else
  117. {
  118. TIFFErrorExt(tif->tif_clientdata, module,
  119. "Read error at row %lu, col %lu, tile %lu; "
  120. "got %I64u bytes, expected %I64u",
  121. (unsigned long) tif->tif_row,
  122. (unsigned long) tif->tif_col,
  123. (unsigned long) strip_or_tile,
  124. (unsigned __int64) already_read,
  125. (unsigned __int64) size);
  126. }
  127. #else
  128. if( is_strip )
  129. {
  130. TIFFErrorExt(tif->tif_clientdata, module,
  131. "Read error at scanline %lu; got %llu bytes, "
  132. "expected %llu",
  133. (unsigned long) tif->tif_row,
  134. (unsigned long long) already_read,
  135. (unsigned long long) size);
  136. }
  137. else
  138. {
  139. TIFFErrorExt(tif->tif_clientdata, module,
  140. "Read error at row %lu, col %lu, tile %lu; "
  141. "got %llu bytes, expected %llu",
  142. (unsigned long) tif->tif_row,
  143. (unsigned long) tif->tif_col,
  144. (unsigned long) strip_or_tile,
  145. (unsigned long long) already_read,
  146. (unsigned long long) size);
  147. }
  148. #endif
  149. return 0;
  150. }
  151. }
  152. return 1;
  153. }
  154. static int
  155. TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
  156. {
  157. static const char module[] = "TIFFFillStripPartial";
  158. register TIFFDirectory *td = &tif->tif_dir;
  159. tmsize_t unused_data;
  160. uint64 read_offset;
  161. tmsize_t to_read;
  162. tmsize_t read_ahead_mod;
  163. /* tmsize_t bytecountm; */
  164. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  165. return 0;
  166. /*
  167. * Expand raw data buffer, if needed, to hold data
  168. * strip coming from file (perhaps should set upper
  169. * bound on the size of a buffer we'll use?).
  170. */
  171. /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */
  172. /* Not completely sure where the * 2 comes from, but probably for */
  173. /* an exponentional growth strategy of tif_rawdatasize */
  174. if( read_ahead < TIFF_TMSIZE_T_MAX / 2 )
  175. read_ahead_mod = read_ahead * 2;
  176. else
  177. read_ahead_mod = read_ahead;
  178. if (read_ahead_mod > tif->tif_rawdatasize) {
  179. assert( restart );
  180. tif->tif_curstrip = NOSTRIP;
  181. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  182. TIFFErrorExt(tif->tif_clientdata, module,
  183. "Data buffer too small to hold part of strip %lu",
  184. (unsigned long) strip);
  185. return (0);
  186. }
  187. }
  188. if( restart )
  189. {
  190. tif->tif_rawdataloaded = 0;
  191. tif->tif_rawdataoff = 0;
  192. }
  193. /*
  194. ** If we are reading more data, move any unused data to the
  195. ** start of the buffer.
  196. */
  197. if( tif->tif_rawdataloaded > 0 )
  198. unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
  199. else
  200. unused_data = 0;
  201. if( unused_data > 0 )
  202. {
  203. assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
  204. memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
  205. }
  206. /*
  207. ** Seek to the point in the file where more data should be read.
  208. */
  209. read_offset = td->td_stripoffset[strip]
  210. + tif->tif_rawdataoff + tif->tif_rawdataloaded;
  211. if (!SeekOK(tif, read_offset)) {
  212. TIFFErrorExt(tif->tif_clientdata, module,
  213. "Seek error at scanline %lu, strip %lu",
  214. (unsigned long) tif->tif_row, (unsigned long) strip);
  215. return 0;
  216. }
  217. /*
  218. ** How much do we want to read?
  219. */
  220. if( read_ahead_mod > tif->tif_rawdatasize )
  221. to_read = read_ahead_mod - unused_data;
  222. else
  223. to_read = tif->tif_rawdatasize - unused_data;
  224. if( (uint64) to_read > td->td_stripbytecount[strip]
  225. - tif->tif_rawdataoff - tif->tif_rawdataloaded )
  226. {
  227. to_read = (tmsize_t) td->td_stripbytecount[strip]
  228. - tif->tif_rawdataoff - tif->tif_rawdataloaded;
  229. }
  230. assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
  231. if( !TIFFReadAndRealloc( tif, to_read, unused_data,
  232. 1, /* is_strip */
  233. 0, /* strip_or_tile */
  234. module) )
  235. {
  236. return 0;
  237. }
  238. tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
  239. tif->tif_rawdataloaded = unused_data + to_read;
  240. tif->tif_rawcp = tif->tif_rawdata;
  241. if (!isFillOrder(tif, td->td_fillorder) &&
  242. (tif->tif_flags & TIFF_NOBITREV) == 0) {
  243. assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
  244. TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
  245. }
  246. /*
  247. ** When starting a strip from the beginning we need to
  248. ** restart the decoder.
  249. */
  250. if( restart )
  251. return TIFFStartStrip(tif, strip);
  252. else
  253. {
  254. tif->tif_rawcc = tif->tif_rawdataloaded;
  255. return 1;
  256. }
  257. }
  258. /*
  259. * Seek to a random row+sample in a file.
  260. *
  261. * Only used by TIFFReadScanline, and is only used on
  262. * strip organized files. We do some tricky stuff to try
  263. * and avoid reading the whole compressed raw data for big
  264. * strips.
  265. */
  266. static int
  267. TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
  268. {
  269. register TIFFDirectory *td = &tif->tif_dir;
  270. uint32 strip;
  271. int whole_strip;
  272. tmsize_t read_ahead = 0;
  273. /*
  274. ** Establish what strip we are working from.
  275. */
  276. if (row >= td->td_imagelength) { /* out of range */
  277. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  278. "%lu: Row out of range, max %lu",
  279. (unsigned long) row,
  280. (unsigned long) td->td_imagelength);
  281. return (0);
  282. }
  283. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  284. if (sample >= td->td_samplesperpixel) {
  285. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  286. "%lu: Sample out of range, max %lu",
  287. (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
  288. return (0);
  289. }
  290. strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
  291. } else
  292. strip = row / td->td_rowsperstrip;
  293. /*
  294. * Do we want to treat this strip as one whole chunk or
  295. * read it a few lines at a time?
  296. */
  297. #if defined(CHUNKY_STRIP_READ_SUPPORT)
  298. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  299. return 0;
  300. whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
  301. || isMapped(tif);
  302. #else
  303. whole_strip = 1;
  304. #endif
  305. if( !whole_strip )
  306. {
  307. /* 16 is for YCbCr mode where we may need to read 16 */
  308. /* lines at a time to get a decompressed line, and 5000 */
  309. /* is some constant value, for example for JPEG tables */
  310. if( tif->tif_scanlinesize < TIFF_TMSIZE_T_MAX / 16 &&
  311. tif->tif_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000 )
  312. {
  313. read_ahead = tif->tif_scanlinesize * 16 + 5000;
  314. }
  315. else
  316. {
  317. read_ahead = tif->tif_scanlinesize;
  318. }
  319. }
  320. /*
  321. * If we haven't loaded this strip, do so now, possibly
  322. * only reading the first part.
  323. */
  324. if (strip != tif->tif_curstrip) { /* different strip, refill */
  325. if( whole_strip )
  326. {
  327. if (!TIFFFillStrip(tif, strip))
  328. return (0);
  329. }
  330. else
  331. {
  332. if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
  333. return 0;
  334. }
  335. }
  336. /*
  337. ** If we already have some data loaded, do we need to read some more?
  338. */
  339. else if( !whole_strip )
  340. {
  341. if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead
  342. && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
  343. {
  344. if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
  345. return 0;
  346. }
  347. }
  348. if (row < tif->tif_row) {
  349. /*
  350. * Moving backwards within the same strip: backup
  351. * to the start and then decode forward (below).
  352. *
  353. * NB: If you're planning on lots of random access within a
  354. * strip, it's better to just read and decode the entire
  355. * strip, and then access the decoded data in a random fashion.
  356. */
  357. if( tif->tif_rawdataoff != 0 )
  358. {
  359. if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
  360. return 0;
  361. }
  362. else
  363. {
  364. if (!TIFFStartStrip(tif, strip))
  365. return (0);
  366. }
  367. }
  368. if (row != tif->tif_row) {
  369. /*
  370. * Seek forward to the desired row.
  371. */
  372. /* TODO: Will this really work with partial buffers? */
  373. if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  374. return (0);
  375. tif->tif_row = row;
  376. }
  377. return (1);
  378. }
  379. int
  380. TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
  381. {
  382. int e;
  383. if (!TIFFCheckRead(tif, 0))
  384. return (-1);
  385. if( (e = TIFFSeek(tif, row, sample)) != 0) {
  386. /*
  387. * Decompress desired row into user buffer.
  388. */
  389. e = (*tif->tif_decoderow)
  390. (tif, (uint8*) buf, tif->tif_scanlinesize, sample);
  391. /* we are now poised at the beginning of the next row */
  392. tif->tif_row = row + 1;
  393. if (e)
  394. (*tif->tif_postdecode)(tif, (uint8*) buf,
  395. tif->tif_scanlinesize);
  396. }
  397. return (e > 0 ? 1 : -1);
  398. }
  399. /*
  400. * Read a strip of data and decompress the specified
  401. * amount into the user-supplied buffer.
  402. */
  403. tmsize_t
  404. TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
  405. {
  406. static const char module[] = "TIFFReadEncodedStrip";
  407. TIFFDirectory *td = &tif->tif_dir;
  408. uint32 rowsperstrip;
  409. uint32 stripsperplane;
  410. uint32 stripinplane;
  411. uint16 plane;
  412. uint32 rows;
  413. tmsize_t stripsize;
  414. if (!TIFFCheckRead(tif,0))
  415. return((tmsize_t)(-1));
  416. if (strip>=td->td_nstrips)
  417. {
  418. TIFFErrorExt(tif->tif_clientdata,module,
  419. "%lu: Strip out of range, max %lu",(unsigned long)strip,
  420. (unsigned long)td->td_nstrips);
  421. return((tmsize_t)(-1));
  422. }
  423. /*
  424. * Calculate the strip size according to the number of
  425. * rows in the strip (check for truncated last strip on any
  426. * of the separations).
  427. */
  428. rowsperstrip=td->td_rowsperstrip;
  429. if (rowsperstrip>td->td_imagelength)
  430. rowsperstrip=td->td_imagelength;
  431. stripsperplane= TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip);
  432. stripinplane=(strip%stripsperplane);
  433. plane=(uint16)(strip/stripsperplane);
  434. rows=td->td_imagelength-stripinplane*rowsperstrip;
  435. if (rows>rowsperstrip)
  436. rows=rowsperstrip;
  437. stripsize=TIFFVStripSize(tif,rows);
  438. if (stripsize==0)
  439. return((tmsize_t)(-1));
  440. /* shortcut to avoid an extra memcpy() */
  441. if( td->td_compression == COMPRESSION_NONE &&
  442. size!=(tmsize_t)(-1) && size >= stripsize &&
  443. !isMapped(tif) &&
  444. ((tif->tif_flags&TIFF_NOREADRAW)==0) )
  445. {
  446. if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize)
  447. return ((tmsize_t)(-1));
  448. if (!isFillOrder(tif, td->td_fillorder) &&
  449. (tif->tif_flags & TIFF_NOBITREV) == 0)
  450. TIFFReverseBits(buf,stripsize);
  451. (*tif->tif_postdecode)(tif,buf,stripsize);
  452. return (stripsize);
  453. }
  454. if ((size!=(tmsize_t)(-1))&&(size<stripsize))
  455. stripsize=size;
  456. if (!TIFFFillStrip(tif,strip))
  457. return((tmsize_t)(-1));
  458. if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
  459. return((tmsize_t)(-1));
  460. (*tif->tif_postdecode)(tif,buf,stripsize);
  461. return(stripsize);
  462. }
  463. static tmsize_t
  464. TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
  465. const char* module)
  466. {
  467. TIFFDirectory *td = &tif->tif_dir;
  468. if (!_TIFFFillStriles( tif ))
  469. return ((tmsize_t)(-1));
  470. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  471. if (!isMapped(tif)) {
  472. tmsize_t cc;
  473. if (!SeekOK(tif, td->td_stripoffset[strip])) {
  474. TIFFErrorExt(tif->tif_clientdata, module,
  475. "Seek error at scanline %lu, strip %lu",
  476. (unsigned long) tif->tif_row, (unsigned long) strip);
  477. return ((tmsize_t)(-1));
  478. }
  479. cc = TIFFReadFile(tif, buf, size);
  480. if (cc != size) {
  481. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  482. TIFFErrorExt(tif->tif_clientdata, module,
  483. "Read error at scanline %lu; got %I64u bytes, expected %I64u",
  484. (unsigned long) tif->tif_row,
  485. (unsigned __int64) cc,
  486. (unsigned __int64) size);
  487. #else
  488. TIFFErrorExt(tif->tif_clientdata, module,
  489. "Read error at scanline %lu; got %llu bytes, expected %llu",
  490. (unsigned long) tif->tif_row,
  491. (unsigned long long) cc,
  492. (unsigned long long) size);
  493. #endif
  494. return ((tmsize_t)(-1));
  495. }
  496. } else {
  497. tmsize_t ma = 0;
  498. tmsize_t n;
  499. if ((td->td_stripoffset[strip] > (uint64)TIFF_TMSIZE_T_MAX)||
  500. ((ma=(tmsize_t)td->td_stripoffset[strip])>tif->tif_size))
  501. {
  502. n=0;
  503. }
  504. else if( ma > TIFF_TMSIZE_T_MAX - size )
  505. {
  506. n=0;
  507. }
  508. else
  509. {
  510. tmsize_t mb=ma+size;
  511. if (mb>tif->tif_size)
  512. n=tif->tif_size-ma;
  513. else
  514. n=size;
  515. }
  516. if (n!=size) {
  517. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  518. TIFFErrorExt(tif->tif_clientdata, module,
  519. "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
  520. (unsigned long) tif->tif_row,
  521. (unsigned long) strip,
  522. (unsigned __int64) n,
  523. (unsigned __int64) size);
  524. #else
  525. TIFFErrorExt(tif->tif_clientdata, module,
  526. "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
  527. (unsigned long) tif->tif_row,
  528. (unsigned long) strip,
  529. (unsigned long long) n,
  530. (unsigned long long) size);
  531. #endif
  532. return ((tmsize_t)(-1));
  533. }
  534. _TIFFmemcpy(buf, tif->tif_base + ma,
  535. size);
  536. }
  537. return (size);
  538. }
  539. static tmsize_t
  540. TIFFReadRawStripOrTile2(TIFF* tif, uint32 strip_or_tile, int is_strip,
  541. tmsize_t size, const char* module)
  542. {
  543. TIFFDirectory *td = &tif->tif_dir;
  544. assert( !isMapped(tif) );
  545. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  546. if (!SeekOK(tif, td->td_stripoffset[strip_or_tile])) {
  547. if( is_strip )
  548. {
  549. TIFFErrorExt(tif->tif_clientdata, module,
  550. "Seek error at scanline %lu, strip %lu",
  551. (unsigned long) tif->tif_row,
  552. (unsigned long) strip_or_tile);
  553. }
  554. else
  555. {
  556. TIFFErrorExt(tif->tif_clientdata, module,
  557. "Seek error at row %lu, col %lu, tile %lu",
  558. (unsigned long) tif->tif_row,
  559. (unsigned long) tif->tif_col,
  560. (unsigned long) strip_or_tile);
  561. }
  562. return ((tmsize_t)(-1));
  563. }
  564. if( !TIFFReadAndRealloc( tif, size, 0, is_strip,
  565. strip_or_tile, module ) )
  566. {
  567. return ((tmsize_t)(-1));
  568. }
  569. return (size);
  570. }
  571. /*
  572. * Read a strip of data from the file.
  573. */
  574. tmsize_t
  575. TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
  576. {
  577. static const char module[] = "TIFFReadRawStrip";
  578. TIFFDirectory *td = &tif->tif_dir;
  579. uint64 bytecount;
  580. tmsize_t bytecountm;
  581. if (!TIFFCheckRead(tif, 0))
  582. return ((tmsize_t)(-1));
  583. if (strip >= td->td_nstrips) {
  584. TIFFErrorExt(tif->tif_clientdata, module,
  585. "%lu: Strip out of range, max %lu",
  586. (unsigned long) strip,
  587. (unsigned long) td->td_nstrips);
  588. return ((tmsize_t)(-1));
  589. }
  590. if (tif->tif_flags&TIFF_NOREADRAW)
  591. {
  592. TIFFErrorExt(tif->tif_clientdata, module,
  593. "Compression scheme does not support access to raw uncompressed data");
  594. return ((tmsize_t)(-1));
  595. }
  596. bytecount = td->td_stripbytecount[strip];
  597. if ((int64)bytecount <= 0) {
  598. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  599. TIFFErrorExt(tif->tif_clientdata, module,
  600. "%I64u: Invalid strip byte count, strip %lu",
  601. (unsigned __int64) bytecount,
  602. (unsigned long) strip);
  603. #else
  604. TIFFErrorExt(tif->tif_clientdata, module,
  605. "%llu: Invalid strip byte count, strip %lu",
  606. (unsigned long long) bytecount,
  607. (unsigned long) strip);
  608. #endif
  609. return ((tmsize_t)(-1));
  610. }
  611. bytecountm = (tmsize_t)bytecount;
  612. if ((uint64)bytecountm!=bytecount) {
  613. TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
  614. return ((tmsize_t)(-1));
  615. }
  616. if (size != (tmsize_t)(-1) && size < bytecountm)
  617. bytecountm = size;
  618. return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
  619. }
  620. /*
  621. * Read the specified strip and setup for decoding. The data buffer is
  622. * expanded, as necessary, to hold the strip's data.
  623. */
  624. int
  625. TIFFFillStrip(TIFF* tif, uint32 strip)
  626. {
  627. static const char module[] = "TIFFFillStrip";
  628. TIFFDirectory *td = &tif->tif_dir;
  629. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  630. return 0;
  631. if ((tif->tif_flags&TIFF_NOREADRAW)==0)
  632. {
  633. uint64 bytecount = td->td_stripbytecount[strip];
  634. if ((int64)bytecount <= 0) {
  635. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  636. TIFFErrorExt(tif->tif_clientdata, module,
  637. "Invalid strip byte count %I64u, strip %lu",
  638. (unsigned __int64) bytecount,
  639. (unsigned long) strip);
  640. #else
  641. TIFFErrorExt(tif->tif_clientdata, module,
  642. "Invalid strip byte count %llu, strip %lu",
  643. (unsigned long long) bytecount,
  644. (unsigned long) strip);
  645. #endif
  646. return (0);
  647. }
  648. /* To avoid excessive memory allocations: */
  649. /* Byte count should normally not be larger than a number of */
  650. /* times the uncompressed size plus some margin */
  651. if( bytecount > 1024 * 1024 )
  652. {
  653. /* 10 and 4096 are just values that could be adjusted. */
  654. /* Hopefully they are safe enough for all codecs */
  655. tmsize_t stripsize = TIFFStripSize(tif);
  656. if( stripsize != 0 &&
  657. (bytecount - 4096) / 10 > (uint64)stripsize )
  658. {
  659. uint64 newbytecount = (uint64)stripsize * 10 + 4096;
  660. if( (int64)newbytecount >= 0 )
  661. {
  662. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  663. TIFFWarningExt(tif->tif_clientdata, module,
  664. "Too large strip byte count %I64u, strip %lu. Limiting to %I64u",
  665. (unsigned __int64) bytecount,
  666. (unsigned long) strip,
  667. (unsigned __int64) newbytecount);
  668. #else
  669. TIFFErrorExt(tif->tif_clientdata, module,
  670. "Too large strip byte count %llu, strip %lu. Limiting to %llu",
  671. (unsigned long long) bytecount,
  672. (unsigned long) strip,
  673. (unsigned long long) newbytecount);
  674. #endif
  675. bytecount = newbytecount;
  676. }
  677. }
  678. }
  679. if (isMapped(tif) &&
  680. (isFillOrder(tif, td->td_fillorder)
  681. || (tif->tif_flags & TIFF_NOBITREV))) {
  682. /*
  683. * The image is mapped into memory and we either don't
  684. * need to flip bits or the compression routine is
  685. * going to handle this operation itself. In this
  686. * case, avoid copying the raw data and instead just
  687. * reference the data from the memory mapped file
  688. * image. This assumes that the decompression
  689. * routines do not modify the contents of the raw data
  690. * buffer (if they try to, the application will get a
  691. * fault since the file is mapped read-only).
  692. */
  693. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  694. _TIFFfree(tif->tif_rawdata);
  695. tif->tif_rawdata = NULL;
  696. tif->tif_rawdatasize = 0;
  697. }
  698. tif->tif_flags &= ~TIFF_MYBUFFER;
  699. /*
  700. * We must check for overflow, potentially causing
  701. * an OOB read. Instead of simple
  702. *
  703. * td->td_stripoffset[strip]+bytecount > tif->tif_size
  704. *
  705. * comparison (which can overflow) we do the following
  706. * two comparisons:
  707. */
  708. if (bytecount > (uint64)tif->tif_size ||
  709. td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
  710. /*
  711. * This error message might seem strange, but
  712. * it's what would happen if a read were done
  713. * instead.
  714. */
  715. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  716. TIFFErrorExt(tif->tif_clientdata, module,
  717. "Read error on strip %lu; "
  718. "got %I64u bytes, expected %I64u",
  719. (unsigned long) strip,
  720. (unsigned __int64) tif->tif_size - td->td_stripoffset[strip],
  721. (unsigned __int64) bytecount);
  722. #else
  723. TIFFErrorExt(tif->tif_clientdata, module,
  724. "Read error on strip %lu; "
  725. "got %llu bytes, expected %llu",
  726. (unsigned long) strip,
  727. (unsigned long long) tif->tif_size - td->td_stripoffset[strip],
  728. (unsigned long long) bytecount);
  729. #endif
  730. tif->tif_curstrip = NOSTRIP;
  731. return (0);
  732. }
  733. tif->tif_rawdatasize = (tmsize_t)bytecount;
  734. tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
  735. tif->tif_rawdataoff = 0;
  736. tif->tif_rawdataloaded = (tmsize_t) bytecount;
  737. /*
  738. * When we have tif_rawdata reference directly into the memory mapped file
  739. * we need to be pretty careful about how we use the rawdata. It is not
  740. * a general purpose working buffer as it normally otherwise is. So we
  741. * keep track of this fact to avoid using it improperly.
  742. */
  743. tif->tif_flags |= TIFF_BUFFERMMAP;
  744. } else {
  745. /*
  746. * Expand raw data buffer, if needed, to hold data
  747. * strip coming from file (perhaps should set upper
  748. * bound on the size of a buffer we'll use?).
  749. */
  750. tmsize_t bytecountm;
  751. bytecountm=(tmsize_t)bytecount;
  752. if ((uint64)bytecountm!=bytecount)
  753. {
  754. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  755. return(0);
  756. }
  757. if (bytecountm > tif->tif_rawdatasize) {
  758. tif->tif_curstrip = NOSTRIP;
  759. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  760. TIFFErrorExt(tif->tif_clientdata, module,
  761. "Data buffer too small to hold strip %lu",
  762. (unsigned long) strip);
  763. return (0);
  764. }
  765. }
  766. if (tif->tif_flags&TIFF_BUFFERMMAP) {
  767. tif->tif_curstrip = NOSTRIP;
  768. tif->tif_rawdata = NULL;
  769. tif->tif_rawdatasize = 0;
  770. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  771. }
  772. if( isMapped(tif) )
  773. {
  774. if (bytecountm > tif->tif_rawdatasize &&
  775. !TIFFReadBufferSetup(tif, 0, bytecountm))
  776. {
  777. return (0);
  778. }
  779. if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
  780. bytecountm, module) != bytecountm)
  781. {
  782. return (0);
  783. }
  784. }
  785. else
  786. {
  787. if (TIFFReadRawStripOrTile2(tif, strip, 1,
  788. bytecountm, module) != bytecountm)
  789. {
  790. return (0);
  791. }
  792. }
  793. tif->tif_rawdataoff = 0;
  794. tif->tif_rawdataloaded = bytecountm;
  795. if (!isFillOrder(tif, td->td_fillorder) &&
  796. (tif->tif_flags & TIFF_NOBITREV) == 0)
  797. TIFFReverseBits(tif->tif_rawdata, bytecountm);
  798. }
  799. }
  800. return (TIFFStartStrip(tif, strip));
  801. }
  802. /*
  803. * Tile-oriented Read Support
  804. * Contributed by Nancy Cam (Silicon Graphics).
  805. */
  806. /*
  807. * Read and decompress a tile of data. The
  808. * tile is selected by the (x,y,z,s) coordinates.
  809. */
  810. tmsize_t
  811. TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
  812. {
  813. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  814. return ((tmsize_t)(-1));
  815. return (TIFFReadEncodedTile(tif,
  816. TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
  817. }
  818. /*
  819. * Read a tile of data and decompress the specified
  820. * amount into the user-supplied buffer.
  821. */
  822. tmsize_t
  823. TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
  824. {
  825. static const char module[] = "TIFFReadEncodedTile";
  826. TIFFDirectory *td = &tif->tif_dir;
  827. tmsize_t tilesize = tif->tif_tilesize;
  828. if (!TIFFCheckRead(tif, 1))
  829. return ((tmsize_t)(-1));
  830. if (tile >= td->td_nstrips) {
  831. TIFFErrorExt(tif->tif_clientdata, module,
  832. "%lu: Tile out of range, max %lu",
  833. (unsigned long) tile, (unsigned long) td->td_nstrips);
  834. return ((tmsize_t)(-1));
  835. }
  836. /* shortcut to avoid an extra memcpy() */
  837. if( td->td_compression == COMPRESSION_NONE &&
  838. size!=(tmsize_t)(-1) && size >= tilesize &&
  839. !isMapped(tif) &&
  840. ((tif->tif_flags&TIFF_NOREADRAW)==0) )
  841. {
  842. if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize)
  843. return ((tmsize_t)(-1));
  844. if (!isFillOrder(tif, td->td_fillorder) &&
  845. (tif->tif_flags & TIFF_NOBITREV) == 0)
  846. TIFFReverseBits(buf,tilesize);
  847. (*tif->tif_postdecode)(tif,buf,tilesize);
  848. return (tilesize);
  849. }
  850. if (size == (tmsize_t)(-1))
  851. size = tilesize;
  852. else if (size > tilesize)
  853. size = tilesize;
  854. if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
  855. (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {
  856. (*tif->tif_postdecode)(tif, (uint8*) buf, size);
  857. return (size);
  858. } else
  859. return ((tmsize_t)(-1));
  860. }
  861. static tmsize_t
  862. TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
  863. {
  864. TIFFDirectory *td = &tif->tif_dir;
  865. if (!_TIFFFillStriles( tif ))
  866. return ((tmsize_t)(-1));
  867. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  868. if (!isMapped(tif)) {
  869. tmsize_t cc;
  870. if (!SeekOK(tif, td->td_stripoffset[tile])) {
  871. TIFFErrorExt(tif->tif_clientdata, module,
  872. "Seek error at row %lu, col %lu, tile %lu",
  873. (unsigned long) tif->tif_row,
  874. (unsigned long) tif->tif_col,
  875. (unsigned long) tile);
  876. return ((tmsize_t)(-1));
  877. }
  878. cc = TIFFReadFile(tif, buf, size);
  879. if (cc != size) {
  880. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  881. TIFFErrorExt(tif->tif_clientdata, module,
  882. "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
  883. (unsigned long) tif->tif_row,
  884. (unsigned long) tif->tif_col,
  885. (unsigned __int64) cc,
  886. (unsigned __int64) size);
  887. #else
  888. TIFFErrorExt(tif->tif_clientdata, module,
  889. "Read error at row %lu, col %lu; got %llu bytes, expected %llu",
  890. (unsigned long) tif->tif_row,
  891. (unsigned long) tif->tif_col,
  892. (unsigned long long) cc,
  893. (unsigned long long) size);
  894. #endif
  895. return ((tmsize_t)(-1));
  896. }
  897. } else {
  898. tmsize_t ma,mb;
  899. tmsize_t n;
  900. ma=(tmsize_t)td->td_stripoffset[tile];
  901. mb=ma+size;
  902. if ((td->td_stripoffset[tile] > (uint64)TIFF_TMSIZE_T_MAX)||(ma>tif->tif_size))
  903. n=0;
  904. else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
  905. n=tif->tif_size-ma;
  906. else
  907. n=size;
  908. if (n!=size) {
  909. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  910. TIFFErrorExt(tif->tif_clientdata, module,
  911. "Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
  912. (unsigned long) tif->tif_row,
  913. (unsigned long) tif->tif_col,
  914. (unsigned long) tile,
  915. (unsigned __int64) n,
  916. (unsigned __int64) size);
  917. #else
  918. TIFFErrorExt(tif->tif_clientdata, module,
  919. "Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
  920. (unsigned long) tif->tif_row,
  921. (unsigned long) tif->tif_col,
  922. (unsigned long) tile,
  923. (unsigned long long) n,
  924. (unsigned long long) size);
  925. #endif
  926. return ((tmsize_t)(-1));
  927. }
  928. _TIFFmemcpy(buf, tif->tif_base + ma, size);
  929. }
  930. return (size);
  931. }
  932. /*
  933. * Read a tile of data from the file.
  934. */
  935. tmsize_t
  936. TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
  937. {
  938. static const char module[] = "TIFFReadRawTile";
  939. TIFFDirectory *td = &tif->tif_dir;
  940. uint64 bytecount64;
  941. tmsize_t bytecountm;
  942. if (!TIFFCheckRead(tif, 1))
  943. return ((tmsize_t)(-1));
  944. if (tile >= td->td_nstrips) {
  945. TIFFErrorExt(tif->tif_clientdata, module,
  946. "%lu: Tile out of range, max %lu",
  947. (unsigned long) tile, (unsigned long) td->td_nstrips);
  948. return ((tmsize_t)(-1));
  949. }
  950. if (tif->tif_flags&TIFF_NOREADRAW)
  951. {
  952. TIFFErrorExt(tif->tif_clientdata, module,
  953. "Compression scheme does not support access to raw uncompressed data");
  954. return ((tmsize_t)(-1));
  955. }
  956. bytecount64 = td->td_stripbytecount[tile];
  957. if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
  958. bytecount64 = (uint64)size;
  959. bytecountm = (tmsize_t)bytecount64;
  960. if ((uint64)bytecountm!=bytecount64)
  961. {
  962. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  963. return ((tmsize_t)(-1));
  964. }
  965. return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
  966. }
  967. /*
  968. * Read the specified tile and setup for decoding. The data buffer is
  969. * expanded, as necessary, to hold the tile's data.
  970. */
  971. int
  972. TIFFFillTile(TIFF* tif, uint32 tile)
  973. {
  974. static const char module[] = "TIFFFillTile";
  975. TIFFDirectory *td = &tif->tif_dir;
  976. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  977. return 0;
  978. if ((tif->tif_flags&TIFF_NOREADRAW)==0)
  979. {
  980. uint64 bytecount = td->td_stripbytecount[tile];
  981. if ((int64)bytecount <= 0) {
  982. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  983. TIFFErrorExt(tif->tif_clientdata, module,
  984. "%I64u: Invalid tile byte count, tile %lu",
  985. (unsigned __int64) bytecount,
  986. (unsigned long) tile);
  987. #else
  988. TIFFErrorExt(tif->tif_clientdata, module,
  989. "%llu: Invalid tile byte count, tile %lu",
  990. (unsigned long long) bytecount,
  991. (unsigned long) tile);
  992. #endif
  993. return (0);
  994. }
  995. if (isMapped(tif) &&
  996. (isFillOrder(tif, td->td_fillorder)
  997. || (tif->tif_flags & TIFF_NOBITREV))) {
  998. /*
  999. * The image is mapped into memory and we either don't
  1000. * need to flip bits or the compression routine is
  1001. * going to handle this operation itself. In this
  1002. * case, avoid copying the raw data and instead just
  1003. * reference the data from the memory mapped file
  1004. * image. This assumes that the decompression
  1005. * routines do not modify the contents of the raw data
  1006. * buffer (if they try to, the application will get a
  1007. * fault since the file is mapped read-only).
  1008. */
  1009. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  1010. _TIFFfree(tif->tif_rawdata);
  1011. tif->tif_rawdata = NULL;
  1012. tif->tif_rawdatasize = 0;
  1013. }
  1014. tif->tif_flags &= ~TIFF_MYBUFFER;
  1015. /*
  1016. * We must check for overflow, potentially causing
  1017. * an OOB read. Instead of simple
  1018. *
  1019. * td->td_stripoffset[tile]+bytecount > tif->tif_size
  1020. *
  1021. * comparison (which can overflow) we do the following
  1022. * two comparisons:
  1023. */
  1024. if (bytecount > (uint64)tif->tif_size ||
  1025. td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
  1026. tif->tif_curtile = NOTILE;
  1027. return (0);
  1028. }
  1029. tif->tif_rawdatasize = (tmsize_t)bytecount;
  1030. tif->tif_rawdata =
  1031. tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
  1032. tif->tif_rawdataoff = 0;
  1033. tif->tif_rawdataloaded = (tmsize_t) bytecount;
  1034. tif->tif_flags |= TIFF_BUFFERMMAP;
  1035. } else {
  1036. /*
  1037. * Expand raw data buffer, if needed, to hold data
  1038. * tile coming from file (perhaps should set upper
  1039. * bound on the size of a buffer we'll use?).
  1040. */
  1041. tmsize_t bytecountm;
  1042. bytecountm=(tmsize_t)bytecount;
  1043. if ((uint64)bytecountm!=bytecount)
  1044. {
  1045. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  1046. return(0);
  1047. }
  1048. if (bytecountm > tif->tif_rawdatasize) {
  1049. tif->tif_curtile = NOTILE;
  1050. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  1051. TIFFErrorExt(tif->tif_clientdata, module,
  1052. "Data buffer too small to hold tile %lu",
  1053. (unsigned long) tile);
  1054. return (0);
  1055. }
  1056. }
  1057. if (tif->tif_flags&TIFF_BUFFERMMAP) {
  1058. tif->tif_curtile = NOTILE;
  1059. tif->tif_rawdata = NULL;
  1060. tif->tif_rawdatasize = 0;
  1061. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  1062. }
  1063. if( isMapped(tif) )
  1064. {
  1065. if (bytecountm > tif->tif_rawdatasize &&
  1066. !TIFFReadBufferSetup(tif, 0, bytecountm))
  1067. {
  1068. return (0);
  1069. }
  1070. if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
  1071. bytecountm, module) != bytecountm)
  1072. {
  1073. return (0);
  1074. }
  1075. }
  1076. else
  1077. {
  1078. if (TIFFReadRawStripOrTile2(tif, tile, 0,
  1079. bytecountm, module) != bytecountm)
  1080. {
  1081. return (0);
  1082. }
  1083. }
  1084. tif->tif_rawdataoff = 0;
  1085. tif->tif_rawdataloaded = bytecountm;
  1086. if (!isFillOrder(tif, td->td_fillorder) &&
  1087. (tif->tif_flags & TIFF_NOBITREV) == 0)
  1088. TIFFReverseBits(tif->tif_rawdata,
  1089. tif->tif_rawdataloaded);
  1090. }
  1091. }
  1092. return (TIFFStartTile(tif, tile));
  1093. }
  1094. /*
  1095. * Setup the raw data buffer in preparation for
  1096. * reading a strip of raw data. If the buffer
  1097. * is specified as zero, then a buffer of appropriate
  1098. * size is allocated by the library. Otherwise,
  1099. * the client must guarantee that the buffer is
  1100. * large enough to hold any individual strip of
  1101. * raw data.
  1102. */
  1103. int
  1104. TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
  1105. {
  1106. static const char module[] = "TIFFReadBufferSetup";
  1107. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  1108. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  1109. if (tif->tif_rawdata) {
  1110. if (tif->tif_flags & TIFF_MYBUFFER)
  1111. _TIFFfree(tif->tif_rawdata);
  1112. tif->tif_rawdata = NULL;
  1113. tif->tif_rawdatasize = 0;
  1114. }
  1115. if (bp) {
  1116. tif->tif_rawdatasize = size;
  1117. tif->tif_rawdata = (uint8*) bp;
  1118. tif->tif_flags &= ~TIFF_MYBUFFER;
  1119. } else {
  1120. tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024);
  1121. if (tif->tif_rawdatasize==0) {
  1122. TIFFErrorExt(tif->tif_clientdata, module,
  1123. "Invalid buffer size");
  1124. return (0);
  1125. }
  1126. /* Initialize to zero to avoid uninitialized buffers in case of */
  1127. /* short reads (http://bugzilla.maptools.org/show_bug.cgi?id=2651) */
  1128. tif->tif_rawdata = (uint8*) _TIFFcalloc(1, tif->tif_rawdatasize);
  1129. tif->tif_flags |= TIFF_MYBUFFER;
  1130. }
  1131. if (tif->tif_rawdata == NULL) {
  1132. TIFFErrorExt(tif->tif_clientdata, module,
  1133. "No space for data buffer at scanline %lu",
  1134. (unsigned long) tif->tif_row);
  1135. tif->tif_rawdatasize = 0;
  1136. return (0);
  1137. }
  1138. return (1);
  1139. }
  1140. /*
  1141. * Set state to appear as if a
  1142. * strip has just been read in.
  1143. */
  1144. static int
  1145. TIFFStartStrip(TIFF* tif, uint32 strip)
  1146. {
  1147. TIFFDirectory *td = &tif->tif_dir;
  1148. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  1149. return 0;
  1150. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  1151. if (!(*tif->tif_setupdecode)(tif))
  1152. return (0);
  1153. tif->tif_flags |= TIFF_CODERSETUP;
  1154. }
  1155. tif->tif_curstrip = strip;
  1156. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  1157. tif->tif_flags &= ~TIFF_BUF4WRITE;
  1158. if (tif->tif_flags&TIFF_NOREADRAW)
  1159. {
  1160. tif->tif_rawcp = NULL;
  1161. tif->tif_rawcc = 0;
  1162. }
  1163. else
  1164. {
  1165. tif->tif_rawcp = tif->tif_rawdata;
  1166. if( tif->tif_rawdataloaded > 0 )
  1167. tif->tif_rawcc = tif->tif_rawdataloaded;
  1168. else
  1169. tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
  1170. }
  1171. return ((*tif->tif_predecode)(tif,
  1172. (uint16)(strip / td->td_stripsperimage)));
  1173. }
  1174. /*
  1175. * Set state to appear as if a
  1176. * tile has just been read in.
  1177. */
  1178. static int
  1179. TIFFStartTile(TIFF* tif, uint32 tile)
  1180. {
  1181. static const char module[] = "TIFFStartTile";
  1182. TIFFDirectory *td = &tif->tif_dir;
  1183. uint32 howmany32;
  1184. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  1185. return 0;
  1186. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  1187. if (!(*tif->tif_setupdecode)(tif))
  1188. return (0);
  1189. tif->tif_flags |= TIFF_CODERSETUP;
  1190. }
  1191. tif->tif_curtile = tile;
  1192. howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
  1193. if (howmany32 == 0) {
  1194. TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
  1195. return 0;
  1196. }
  1197. tif->tif_row = (tile % howmany32) * td->td_tilelength;
  1198. howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
  1199. if (howmany32 == 0) {
  1200. TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
  1201. return 0;
  1202. }
  1203. tif->tif_col = (tile % howmany32) * td->td_tilewidth;
  1204. tif->tif_flags &= ~TIFF_BUF4WRITE;
  1205. if (tif->tif_flags&TIFF_NOREADRAW)
  1206. {
  1207. tif->tif_rawcp = NULL;
  1208. tif->tif_rawcc = 0;
  1209. }
  1210. else
  1211. {
  1212. tif->tif_rawcp = tif->tif_rawdata;
  1213. tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
  1214. }
  1215. return ((*tif->tif_predecode)(tif,
  1216. (uint16)(tile/td->td_stripsperimage)));
  1217. }
  1218. static int
  1219. TIFFCheckRead(TIFF* tif, int tiles)
  1220. {
  1221. if (tif->tif_mode == O_WRONLY) {
  1222. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
  1223. return (0);
  1224. }
  1225. if (tiles ^ isTiled(tif)) {
  1226. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
  1227. "Can not read tiles from a stripped image" :
  1228. "Can not read scanlines from a tiled image");
  1229. return (0);
  1230. }
  1231. return (1);
  1232. }
  1233. void
  1234. _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
  1235. {
  1236. (void) tif; (void) buf; (void) cc;
  1237. }
  1238. void
  1239. _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  1240. {
  1241. (void) tif;
  1242. assert((cc & 1) == 0);
  1243. TIFFSwabArrayOfShort((uint16*) buf, cc/2);
  1244. }
  1245. void
  1246. _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  1247. {
  1248. (void) tif;
  1249. assert((cc % 3) == 0);
  1250. TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
  1251. }
  1252. void
  1253. _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  1254. {
  1255. (void) tif;
  1256. assert((cc & 3) == 0);
  1257. TIFFSwabArrayOfLong((uint32*) buf, cc/4);
  1258. }
  1259. void
  1260. _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  1261. {
  1262. (void) tif;
  1263. assert((cc & 7) == 0);
  1264. TIFFSwabArrayOfDouble((double*) buf, cc/8);
  1265. }
  1266. /* vim: set ts=8 sts=8 sw=8 noet: */
  1267. /*
  1268. * Local Variables:
  1269. * mode: c
  1270. * c-basic-offset: 8
  1271. * fill-column: 78
  1272. * End:
  1273. */