tif_read.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /* $Id: tif_read.c,v 1.40 2012-06-01 00:55:09 fwarmerdam 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. int TIFFFillStrip(TIFF* tif, uint32 strip);
  32. int TIFFFillTile(TIFF* tif, uint32 tile);
  33. static int TIFFStartStrip(TIFF* tif, uint32 strip);
  34. static int TIFFStartTile(TIFF* tif, uint32 tile);
  35. static int TIFFCheckRead(TIFF*, int);
  36. static tmsize_t
  37. TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
  38. #define NOSTRIP ((uint32)(-1)) /* undefined state */
  39. #define NOTILE ((uint32)(-1)) /* undefined state */
  40. static int
  41. TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
  42. {
  43. static const char module[] = "TIFFFillStripPartial";
  44. register TIFFDirectory *td = &tif->tif_dir;
  45. uint64 unused_data;
  46. uint64 read_offset;
  47. tmsize_t cc, to_read;
  48. tmsize_t bytecountm;
  49. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  50. return 0;
  51. /*
  52. * Expand raw data buffer, if needed, to hold data
  53. * strip coming from file (perhaps should set upper
  54. * bound on the size of a buffer we'll use?).
  55. */
  56. bytecountm=(tmsize_t) td->td_stripbytecount[strip];
  57. if (read_ahead*2 > tif->tif_rawdatasize) {
  58. assert( restart );
  59. tif->tif_curstrip = NOSTRIP;
  60. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  61. TIFFErrorExt(tif->tif_clientdata, module,
  62. "Data buffer too small to hold part of strip %lu",
  63. (unsigned long) strip);
  64. return (0);
  65. }
  66. if (!TIFFReadBufferSetup(tif, 0, read_ahead*2))
  67. return (0);
  68. }
  69. if( restart )
  70. {
  71. tif->tif_rawdataloaded = 0;
  72. tif->tif_rawdataoff = 0;
  73. }
  74. /*
  75. ** If we are reading more data, move any unused data to the
  76. ** start of the buffer.
  77. */
  78. if( tif->tif_rawdataloaded > 0 )
  79. unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
  80. else
  81. unused_data = 0;
  82. if( unused_data > 0 )
  83. {
  84. assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
  85. memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
  86. }
  87. /*
  88. ** Seek to the point in the file where more data should be read.
  89. */
  90. read_offset = td->td_stripoffset[strip]
  91. + tif->tif_rawdataoff + tif->tif_rawdataloaded;
  92. if (!SeekOK(tif, read_offset)) {
  93. TIFFErrorExt(tif->tif_clientdata, module,
  94. "Seek error at scanline %lu, strip %lu",
  95. (unsigned long) tif->tif_row, (unsigned long) strip);
  96. return 0;
  97. }
  98. /*
  99. ** How much do we want to read?
  100. */
  101. to_read = tif->tif_rawdatasize - unused_data;
  102. if( (uint64) to_read > td->td_stripbytecount[strip]
  103. - tif->tif_rawdataoff - tif->tif_rawdataloaded )
  104. {
  105. to_read = td->td_stripbytecount[strip]
  106. - tif->tif_rawdataoff - tif->tif_rawdataloaded;
  107. }
  108. assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
  109. cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read);
  110. if (cc != to_read) {
  111. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  112. TIFFErrorExt(tif->tif_clientdata, module,
  113. "Read error at scanline %lu; got %I64u bytes, expected %I64u",
  114. (unsigned long) tif->tif_row,
  115. (unsigned __int64) cc,
  116. (unsigned __int64) to_read);
  117. #else
  118. TIFFErrorExt(tif->tif_clientdata, module,
  119. "Read error at scanline %lu; got %llu bytes, expected %llu",
  120. (unsigned long) tif->tif_row,
  121. (unsigned long long) cc,
  122. (unsigned long long) to_read);
  123. #endif
  124. return 0;
  125. }
  126. tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
  127. tif->tif_rawdataloaded = unused_data + to_read;
  128. tif->tif_rawcp = tif->tif_rawdata;
  129. if (!isFillOrder(tif, td->td_fillorder) &&
  130. (tif->tif_flags & TIFF_NOBITREV) == 0) {
  131. assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
  132. TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
  133. }
  134. /*
  135. ** When starting a strip from the beginning we need to
  136. ** restart the decoder.
  137. */
  138. if( restart )
  139. return TIFFStartStrip(tif, strip);
  140. else
  141. return 1;
  142. }
  143. /*
  144. * Seek to a random row+sample in a file.
  145. *
  146. * Only used by TIFFReadScanline, and is only used on
  147. * strip organized files. We do some tricky stuff to try
  148. * and avoid reading the whole compressed raw data for big
  149. * strips.
  150. */
  151. static int
  152. TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
  153. {
  154. register TIFFDirectory *td = &tif->tif_dir;
  155. uint32 strip;
  156. int whole_strip;
  157. tmsize_t read_ahead = 0;
  158. /*
  159. ** Establish what strip we are working from.
  160. */
  161. if (row >= td->td_imagelength) { /* out of range */
  162. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  163. "%lu: Row out of range, max %lu",
  164. (unsigned long) row,
  165. (unsigned long) td->td_imagelength);
  166. return (0);
  167. }
  168. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  169. if (sample >= td->td_samplesperpixel) {
  170. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  171. "%lu: Sample out of range, max %lu",
  172. (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
  173. return (0);
  174. }
  175. strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
  176. } else
  177. strip = row / td->td_rowsperstrip;
  178. /*
  179. * Do we want to treat this strip as one whole chunk or
  180. * read it a few lines at a time?
  181. */
  182. #if defined(CHUNKY_STRIP_READ_SUPPORT)
  183. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  184. return 0;
  185. whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
  186. || isMapped(tif);
  187. #else
  188. whole_strip = 1;
  189. #endif
  190. if( !whole_strip )
  191. {
  192. read_ahead = tif->tif_scanlinesize * 16 + 5000;
  193. }
  194. /*
  195. * If we haven't loaded this strip, do so now, possibly
  196. * only reading the first part.
  197. */
  198. if (strip != tif->tif_curstrip) { /* different strip, refill */
  199. if( whole_strip )
  200. {
  201. if (!TIFFFillStrip(tif, strip))
  202. return (0);
  203. }
  204. else
  205. {
  206. if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
  207. return 0;
  208. }
  209. }
  210. /*
  211. ** If we already have some data loaded, do we need to read some more?
  212. */
  213. else if( !whole_strip )
  214. {
  215. if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead
  216. && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
  217. {
  218. if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
  219. return 0;
  220. }
  221. }
  222. if (row < tif->tif_row) {
  223. /*
  224. * Moving backwards within the same strip: backup
  225. * to the start and then decode forward (below).
  226. *
  227. * NB: If you're planning on lots of random access within a
  228. * strip, it's better to just read and decode the entire
  229. * strip, and then access the decoded data in a random fashion.
  230. */
  231. if( tif->tif_rawdataoff != 0 )
  232. {
  233. if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
  234. return 0;
  235. }
  236. else
  237. {
  238. if (!TIFFStartStrip(tif, strip))
  239. return (0);
  240. }
  241. }
  242. if (row != tif->tif_row) {
  243. /*
  244. * Seek forward to the desired row.
  245. */
  246. /* TODO: Will this really work with partial buffers? */
  247. if (!(*tif->tif_seek)(tif, row - tif->tif_row))
  248. return (0);
  249. tif->tif_row = row;
  250. }
  251. return (1);
  252. }
  253. int
  254. TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
  255. {
  256. int e;
  257. if (!TIFFCheckRead(tif, 0))
  258. return (-1);
  259. if( (e = TIFFSeek(tif, row, sample)) != 0) {
  260. /*
  261. * Decompress desired row into user buffer.
  262. */
  263. e = (*tif->tif_decoderow)
  264. (tif, (uint8*) buf, tif->tif_scanlinesize, sample);
  265. /* we are now poised at the beginning of the next row */
  266. tif->tif_row = row + 1;
  267. if (e)
  268. (*tif->tif_postdecode)(tif, (uint8*) buf,
  269. tif->tif_scanlinesize);
  270. }
  271. return (e > 0 ? 1 : -1);
  272. }
  273. /*
  274. * Read a strip of data and decompress the specified
  275. * amount into the user-supplied buffer.
  276. */
  277. tmsize_t
  278. TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
  279. {
  280. static const char module[] = "TIFFReadEncodedStrip";
  281. TIFFDirectory *td = &tif->tif_dir;
  282. uint32 rowsperstrip;
  283. uint32 stripsperplane;
  284. uint32 stripinplane;
  285. uint16 plane;
  286. uint32 rows;
  287. tmsize_t stripsize;
  288. if (!TIFFCheckRead(tif,0))
  289. return((tmsize_t)(-1));
  290. if (strip>=td->td_nstrips)
  291. {
  292. TIFFErrorExt(tif->tif_clientdata,module,
  293. "%lu: Strip out of range, max %lu",(unsigned long)strip,
  294. (unsigned long)td->td_nstrips);
  295. return((tmsize_t)(-1));
  296. }
  297. /*
  298. * Calculate the strip size according to the number of
  299. * rows in the strip (check for truncated last strip on any
  300. * of the separations).
  301. */
  302. rowsperstrip=td->td_rowsperstrip;
  303. if (rowsperstrip>td->td_imagelength)
  304. rowsperstrip=td->td_imagelength;
  305. stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);
  306. stripinplane=(strip%stripsperplane);
  307. plane=(strip/stripsperplane);
  308. rows=td->td_imagelength-stripinplane*rowsperstrip;
  309. if (rows>rowsperstrip)
  310. rows=rowsperstrip;
  311. stripsize=TIFFVStripSize(tif,rows);
  312. if (stripsize==0)
  313. return((tmsize_t)(-1));
  314. if ((size!=(tmsize_t)(-1))&&(size<stripsize))
  315. stripsize=size;
  316. if (!TIFFFillStrip(tif,strip))
  317. return((tmsize_t)(-1));
  318. if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
  319. return((tmsize_t)(-1));
  320. (*tif->tif_postdecode)(tif,buf,stripsize);
  321. return(stripsize);
  322. }
  323. static tmsize_t
  324. TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
  325. const char* module)
  326. {
  327. TIFFDirectory *td = &tif->tif_dir;
  328. if (!_TIFFFillStriles( tif ))
  329. return ((tmsize_t)(-1));
  330. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  331. if (!isMapped(tif)) {
  332. tmsize_t cc;
  333. if (!SeekOK(tif, td->td_stripoffset[strip])) {
  334. TIFFErrorExt(tif->tif_clientdata, module,
  335. "Seek error at scanline %lu, strip %lu",
  336. (unsigned long) tif->tif_row, (unsigned long) strip);
  337. return ((tmsize_t)(-1));
  338. }
  339. cc = TIFFReadFile(tif, buf, size);
  340. if (cc != size) {
  341. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  342. TIFFErrorExt(tif->tif_clientdata, module,
  343. "Read error at scanline %lu; got %I64u bytes, expected %I64u",
  344. (unsigned long) tif->tif_row,
  345. (unsigned __int64) cc,
  346. (unsigned __int64) size);
  347. #else
  348. TIFFErrorExt(tif->tif_clientdata, module,
  349. "Read error at scanline %lu; got %llu bytes, expected %llu",
  350. (unsigned long) tif->tif_row,
  351. (unsigned long long) cc,
  352. (unsigned long long) size);
  353. #endif
  354. return ((tmsize_t)(-1));
  355. }
  356. } else {
  357. tmsize_t ma,mb;
  358. tmsize_t n;
  359. ma=(tmsize_t)td->td_stripoffset[strip];
  360. mb=ma+size;
  361. if (((uint64)ma!=td->td_stripoffset[strip])||(ma>tif->tif_size))
  362. n=0;
  363. else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
  364. n=tif->tif_size-ma;
  365. else
  366. n=size;
  367. if (n!=size) {
  368. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  369. TIFFErrorExt(tif->tif_clientdata, module,
  370. "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
  371. (unsigned long) tif->tif_row,
  372. (unsigned long) strip,
  373. (unsigned __int64) n,
  374. (unsigned __int64) size);
  375. #else
  376. TIFFErrorExt(tif->tif_clientdata, module,
  377. "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
  378. (unsigned long) tif->tif_row,
  379. (unsigned long) strip,
  380. (unsigned long long) n,
  381. (unsigned long long) size);
  382. #endif
  383. return ((tmsize_t)(-1));
  384. }
  385. _TIFFmemcpy(buf, tif->tif_base + ma,
  386. size);
  387. }
  388. return (size);
  389. }
  390. /*
  391. * Read a strip of data from the file.
  392. */
  393. tmsize_t
  394. TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
  395. {
  396. static const char module[] = "TIFFReadRawStrip";
  397. TIFFDirectory *td = &tif->tif_dir;
  398. uint64 bytecount;
  399. tmsize_t bytecountm;
  400. if (!TIFFCheckRead(tif, 0))
  401. return ((tmsize_t)(-1));
  402. if (strip >= td->td_nstrips) {
  403. TIFFErrorExt(tif->tif_clientdata, module,
  404. "%lu: Strip out of range, max %lu",
  405. (unsigned long) strip,
  406. (unsigned long) td->td_nstrips);
  407. return ((tmsize_t)(-1));
  408. }
  409. if (tif->tif_flags&TIFF_NOREADRAW)
  410. {
  411. TIFFErrorExt(tif->tif_clientdata, module,
  412. "Compression scheme does not support access to raw uncompressed data");
  413. return ((tmsize_t)(-1));
  414. }
  415. bytecount = td->td_stripbytecount[strip];
  416. if (bytecount <= 0) {
  417. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  418. TIFFErrorExt(tif->tif_clientdata, module,
  419. "%I64u: Invalid strip byte count, strip %lu",
  420. (unsigned __int64) bytecount,
  421. (unsigned long) strip);
  422. #else
  423. TIFFErrorExt(tif->tif_clientdata, module,
  424. "%llu: Invalid strip byte count, strip %lu",
  425. (unsigned long long) bytecount,
  426. (unsigned long) strip);
  427. #endif
  428. return ((tmsize_t)(-1));
  429. }
  430. bytecountm = (tmsize_t)bytecount;
  431. if ((uint64)bytecountm!=bytecount) {
  432. TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
  433. return ((tmsize_t)(-1));
  434. }
  435. if (size != (tmsize_t)(-1) && size < bytecountm)
  436. bytecountm = size;
  437. return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
  438. }
  439. /*
  440. * Read the specified strip and setup for decoding. The data buffer is
  441. * expanded, as necessary, to hold the strip's data.
  442. */
  443. int
  444. TIFFFillStrip(TIFF* tif, uint32 strip)
  445. {
  446. static const char module[] = "TIFFFillStrip";
  447. TIFFDirectory *td = &tif->tif_dir;
  448. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  449. return 0;
  450. if ((tif->tif_flags&TIFF_NOREADRAW)==0)
  451. {
  452. uint64 bytecount = td->td_stripbytecount[strip];
  453. if (bytecount <= 0) {
  454. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  455. TIFFErrorExt(tif->tif_clientdata, module,
  456. "Invalid strip byte count %I64u, strip %lu",
  457. (unsigned __int64) bytecount,
  458. (unsigned long) strip);
  459. #else
  460. TIFFErrorExt(tif->tif_clientdata, module,
  461. "Invalid strip byte count %llu, strip %lu",
  462. (unsigned long long) bytecount,
  463. (unsigned long) strip);
  464. #endif
  465. return (0);
  466. }
  467. if (isMapped(tif) &&
  468. (isFillOrder(tif, td->td_fillorder)
  469. || (tif->tif_flags & TIFF_NOBITREV))) {
  470. /*
  471. * The image is mapped into memory and we either don't
  472. * need to flip bits or the compression routine is
  473. * going to handle this operation itself. In this
  474. * case, avoid copying the raw data and instead just
  475. * reference the data from the memory mapped file
  476. * image. This assumes that the decompression
  477. * routines do not modify the contents of the raw data
  478. * buffer (if they try to, the application will get a
  479. * fault since the file is mapped read-only).
  480. */
  481. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  482. _TIFFfree(tif->tif_rawdata);
  483. tif->tif_rawdata = NULL;
  484. tif->tif_rawdatasize = 0;
  485. }
  486. tif->tif_flags &= ~TIFF_MYBUFFER;
  487. /*
  488. * We must check for overflow, potentially causing
  489. * an OOB read. Instead of simple
  490. *
  491. * td->td_stripoffset[strip]+bytecount > tif->tif_size
  492. *
  493. * comparison (which can overflow) we do the following
  494. * two comparisons:
  495. */
  496. if (bytecount > (uint64)tif->tif_size ||
  497. td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
  498. /*
  499. * This error message might seem strange, but
  500. * it's what would happen if a read were done
  501. * instead.
  502. */
  503. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  504. TIFFErrorExt(tif->tif_clientdata, module,
  505. "Read error on strip %lu; "
  506. "got %I64u bytes, expected %I64u",
  507. (unsigned long) strip,
  508. (unsigned __int64) tif->tif_size - td->td_stripoffset[strip],
  509. (unsigned __int64) bytecount);
  510. #else
  511. TIFFErrorExt(tif->tif_clientdata, module,
  512. "Read error on strip %lu; "
  513. "got %llu bytes, expected %llu",
  514. (unsigned long) strip,
  515. (unsigned long long) tif->tif_size - td->td_stripoffset[strip],
  516. (unsigned long long) bytecount);
  517. #endif
  518. tif->tif_curstrip = NOSTRIP;
  519. return (0);
  520. }
  521. tif->tif_rawdatasize = (tmsize_t)bytecount;
  522. tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
  523. tif->tif_rawdataoff = 0;
  524. tif->tif_rawdataloaded = (tmsize_t) bytecount;
  525. /*
  526. * When we have tif_rawdata reference directly into the memory mapped file
  527. * we need to be pretty careful about how we use the rawdata. It is not
  528. * a general purpose working buffer as it normally otherwise is. So we
  529. * keep track of this fact to avoid using it improperly.
  530. */
  531. tif->tif_flags |= TIFF_BUFFERMMAP;
  532. } else {
  533. /*
  534. * Expand raw data buffer, if needed, to hold data
  535. * strip coming from file (perhaps should set upper
  536. * bound on the size of a buffer we'll use?).
  537. */
  538. tmsize_t bytecountm;
  539. bytecountm=(tmsize_t)bytecount;
  540. if ((uint64)bytecountm!=bytecount)
  541. {
  542. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  543. return(0);
  544. }
  545. if (bytecountm > tif->tif_rawdatasize) {
  546. tif->tif_curstrip = NOSTRIP;
  547. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  548. TIFFErrorExt(tif->tif_clientdata, module,
  549. "Data buffer too small to hold strip %lu",
  550. (unsigned long) strip);
  551. return (0);
  552. }
  553. if (!TIFFReadBufferSetup(tif, 0, bytecountm))
  554. return (0);
  555. }
  556. if (tif->tif_flags&TIFF_BUFFERMMAP) {
  557. tif->tif_curstrip = NOSTRIP;
  558. if (!TIFFReadBufferSetup(tif, 0, bytecountm))
  559. return (0);
  560. }
  561. if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
  562. bytecountm, module) != bytecountm)
  563. return (0);
  564. tif->tif_rawdataoff = 0;
  565. tif->tif_rawdataloaded = bytecountm;
  566. if (!isFillOrder(tif, td->td_fillorder) &&
  567. (tif->tif_flags & TIFF_NOBITREV) == 0)
  568. TIFFReverseBits(tif->tif_rawdata, bytecountm);
  569. }
  570. }
  571. return (TIFFStartStrip(tif, strip));
  572. }
  573. /*
  574. * Tile-oriented Read Support
  575. * Contributed by Nancy Cam (Silicon Graphics).
  576. */
  577. /*
  578. * Read and decompress a tile of data. The
  579. * tile is selected by the (x,y,z,s) coordinates.
  580. */
  581. tmsize_t
  582. TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
  583. {
  584. if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
  585. return ((tmsize_t)(-1));
  586. return (TIFFReadEncodedTile(tif,
  587. TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
  588. }
  589. /*
  590. * Read a tile of data and decompress the specified
  591. * amount into the user-supplied buffer.
  592. */
  593. tmsize_t
  594. TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
  595. {
  596. static const char module[] = "TIFFReadEncodedTile";
  597. TIFFDirectory *td = &tif->tif_dir;
  598. tmsize_t tilesize = tif->tif_tilesize;
  599. if (!TIFFCheckRead(tif, 1))
  600. return ((tmsize_t)(-1));
  601. if (tile >= td->td_nstrips) {
  602. TIFFErrorExt(tif->tif_clientdata, module,
  603. "%lu: Tile out of range, max %lu",
  604. (unsigned long) tile, (unsigned long) td->td_nstrips);
  605. return ((tmsize_t)(-1));
  606. }
  607. if (size == (tmsize_t)(-1))
  608. size = tilesize;
  609. else if (size > tilesize)
  610. size = tilesize;
  611. if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
  612. (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {
  613. (*tif->tif_postdecode)(tif, (uint8*) buf, size);
  614. return (size);
  615. } else
  616. return ((tmsize_t)(-1));
  617. }
  618. static tmsize_t
  619. TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
  620. {
  621. TIFFDirectory *td = &tif->tif_dir;
  622. if (!_TIFFFillStriles( tif ))
  623. return ((tmsize_t)(-1));
  624. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  625. if (!isMapped(tif)) {
  626. tmsize_t cc;
  627. if (!SeekOK(tif, td->td_stripoffset[tile])) {
  628. TIFFErrorExt(tif->tif_clientdata, module,
  629. "Seek error at row %lu, col %lu, tile %lu",
  630. (unsigned long) tif->tif_row,
  631. (unsigned long) tif->tif_col,
  632. (unsigned long) tile);
  633. return ((tmsize_t)(-1));
  634. }
  635. cc = TIFFReadFile(tif, buf, size);
  636. if (cc != size) {
  637. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  638. TIFFErrorExt(tif->tif_clientdata, module,
  639. "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
  640. (unsigned long) tif->tif_row,
  641. (unsigned long) tif->tif_col,
  642. (unsigned __int64) cc,
  643. (unsigned __int64) size);
  644. #else
  645. TIFFErrorExt(tif->tif_clientdata, module,
  646. "Read error at row %lu, col %lu; got %llu bytes, expected %llu",
  647. (unsigned long) tif->tif_row,
  648. (unsigned long) tif->tif_col,
  649. (unsigned long long) cc,
  650. (unsigned long long) size);
  651. #endif
  652. return ((tmsize_t)(-1));
  653. }
  654. } else {
  655. tmsize_t ma,mb;
  656. tmsize_t n;
  657. ma=(tmsize_t)td->td_stripoffset[tile];
  658. mb=ma+size;
  659. if (((uint64)ma!=td->td_stripoffset[tile])||(ma>tif->tif_size))
  660. n=0;
  661. else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
  662. n=tif->tif_size-ma;
  663. else
  664. n=size;
  665. if (n!=size) {
  666. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  667. TIFFErrorExt(tif->tif_clientdata, module,
  668. "Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
  669. (unsigned long) tif->tif_row,
  670. (unsigned long) tif->tif_col,
  671. (unsigned long) tile,
  672. (unsigned __int64) n,
  673. (unsigned __int64) size);
  674. #else
  675. TIFFErrorExt(tif->tif_clientdata, module,
  676. "Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
  677. (unsigned long) tif->tif_row,
  678. (unsigned long) tif->tif_col,
  679. (unsigned long) tile,
  680. (unsigned long long) n,
  681. (unsigned long long) size);
  682. #endif
  683. return ((tmsize_t)(-1));
  684. }
  685. _TIFFmemcpy(buf, tif->tif_base + ma, size);
  686. }
  687. return (size);
  688. }
  689. /*
  690. * Read a tile of data from the file.
  691. */
  692. tmsize_t
  693. TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
  694. {
  695. static const char module[] = "TIFFReadRawTile";
  696. TIFFDirectory *td = &tif->tif_dir;
  697. uint64 bytecount64;
  698. tmsize_t bytecountm;
  699. if (!TIFFCheckRead(tif, 1))
  700. return ((tmsize_t)(-1));
  701. if (tile >= td->td_nstrips) {
  702. TIFFErrorExt(tif->tif_clientdata, module,
  703. "%lu: Tile out of range, max %lu",
  704. (unsigned long) tile, (unsigned long) td->td_nstrips);
  705. return ((tmsize_t)(-1));
  706. }
  707. if (tif->tif_flags&TIFF_NOREADRAW)
  708. {
  709. TIFFErrorExt(tif->tif_clientdata, module,
  710. "Compression scheme does not support access to raw uncompressed data");
  711. return ((tmsize_t)(-1));
  712. }
  713. bytecount64 = td->td_stripbytecount[tile];
  714. if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
  715. bytecount64 = (uint64)size;
  716. bytecountm = (tmsize_t)bytecount64;
  717. if ((uint64)bytecountm!=bytecount64)
  718. {
  719. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  720. return ((tmsize_t)(-1));
  721. }
  722. return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
  723. }
  724. /*
  725. * Read the specified tile and setup for decoding. The data buffer is
  726. * expanded, as necessary, to hold the tile's data.
  727. */
  728. int
  729. TIFFFillTile(TIFF* tif, uint32 tile)
  730. {
  731. static const char module[] = "TIFFFillTile";
  732. TIFFDirectory *td = &tif->tif_dir;
  733. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  734. return 0;
  735. if ((tif->tif_flags&TIFF_NOREADRAW)==0)
  736. {
  737. uint64 bytecount = td->td_stripbytecount[tile];
  738. if (bytecount <= 0) {
  739. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  740. TIFFErrorExt(tif->tif_clientdata, module,
  741. "%I64u: Invalid tile byte count, tile %lu",
  742. (unsigned __int64) bytecount,
  743. (unsigned long) tile);
  744. #else
  745. TIFFErrorExt(tif->tif_clientdata, module,
  746. "%llu: Invalid tile byte count, tile %lu",
  747. (unsigned long long) bytecount,
  748. (unsigned long) tile);
  749. #endif
  750. return (0);
  751. }
  752. if (isMapped(tif) &&
  753. (isFillOrder(tif, td->td_fillorder)
  754. || (tif->tif_flags & TIFF_NOBITREV))) {
  755. /*
  756. * The image is mapped into memory and we either don't
  757. * need to flip bits or the compression routine is
  758. * going to handle this operation itself. In this
  759. * case, avoid copying the raw data and instead just
  760. * reference the data from the memory mapped file
  761. * image. This assumes that the decompression
  762. * routines do not modify the contents of the raw data
  763. * buffer (if they try to, the application will get a
  764. * fault since the file is mapped read-only).
  765. */
  766. if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
  767. _TIFFfree(tif->tif_rawdata);
  768. tif->tif_rawdata = NULL;
  769. tif->tif_rawdatasize = 0;
  770. }
  771. tif->tif_flags &= ~TIFF_MYBUFFER;
  772. /*
  773. * We must check for overflow, potentially causing
  774. * an OOB read. Instead of simple
  775. *
  776. * td->td_stripoffset[tile]+bytecount > tif->tif_size
  777. *
  778. * comparison (which can overflow) we do the following
  779. * two comparisons:
  780. */
  781. if (bytecount > (uint64)tif->tif_size ||
  782. td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
  783. tif->tif_curtile = NOTILE;
  784. return (0);
  785. }
  786. tif->tif_rawdatasize = (tmsize_t)bytecount;
  787. tif->tif_rawdata =
  788. tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
  789. tif->tif_rawdataoff = 0;
  790. tif->tif_rawdataloaded = (tmsize_t) bytecount;
  791. tif->tif_flags |= TIFF_BUFFERMMAP;
  792. } else {
  793. /*
  794. * Expand raw data buffer, if needed, to hold data
  795. * tile coming from file (perhaps should set upper
  796. * bound on the size of a buffer we'll use?).
  797. */
  798. tmsize_t bytecountm;
  799. bytecountm=(tmsize_t)bytecount;
  800. if ((uint64)bytecountm!=bytecount)
  801. {
  802. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  803. return(0);
  804. }
  805. if (bytecountm > tif->tif_rawdatasize) {
  806. tif->tif_curtile = NOTILE;
  807. if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
  808. TIFFErrorExt(tif->tif_clientdata, module,
  809. "Data buffer too small to hold tile %lu",
  810. (unsigned long) tile);
  811. return (0);
  812. }
  813. if (!TIFFReadBufferSetup(tif, 0, bytecountm))
  814. return (0);
  815. }
  816. if (tif->tif_flags&TIFF_BUFFERMMAP) {
  817. tif->tif_curtile = NOTILE;
  818. if (!TIFFReadBufferSetup(tif, 0, bytecountm))
  819. return (0);
  820. }
  821. if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
  822. bytecountm, module) != bytecountm)
  823. return (0);
  824. tif->tif_rawdataoff = 0;
  825. tif->tif_rawdataloaded = bytecountm;
  826. if (!isFillOrder(tif, td->td_fillorder) &&
  827. (tif->tif_flags & TIFF_NOBITREV) == 0)
  828. TIFFReverseBits(tif->tif_rawdata,
  829. tif->tif_rawdataloaded);
  830. }
  831. }
  832. return (TIFFStartTile(tif, tile));
  833. }
  834. /*
  835. * Setup the raw data buffer in preparation for
  836. * reading a strip of raw data. If the buffer
  837. * is specified as zero, then a buffer of appropriate
  838. * size is allocated by the library. Otherwise,
  839. * the client must guarantee that the buffer is
  840. * large enough to hold any individual strip of
  841. * raw data.
  842. */
  843. int
  844. TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
  845. {
  846. static const char module[] = "TIFFReadBufferSetup";
  847. assert((tif->tif_flags&TIFF_NOREADRAW)==0);
  848. tif->tif_flags &= ~TIFF_BUFFERMMAP;
  849. if (tif->tif_rawdata) {
  850. if (tif->tif_flags & TIFF_MYBUFFER)
  851. _TIFFfree(tif->tif_rawdata);
  852. tif->tif_rawdata = NULL;
  853. tif->tif_rawdatasize = 0;
  854. }
  855. if (bp) {
  856. tif->tif_rawdatasize = size;
  857. tif->tif_rawdata = (uint8*) bp;
  858. tif->tif_flags &= ~TIFF_MYBUFFER;
  859. } else {
  860. tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024);
  861. if (tif->tif_rawdatasize==0)
  862. tif->tif_rawdatasize=(tmsize_t)(-1);
  863. tif->tif_rawdata = (uint8*) _TIFFmalloc(tif->tif_rawdatasize);
  864. tif->tif_flags |= TIFF_MYBUFFER;
  865. }
  866. if (tif->tif_rawdata == NULL) {
  867. TIFFErrorExt(tif->tif_clientdata, module,
  868. "No space for data buffer at scanline %lu",
  869. (unsigned long) tif->tif_row);
  870. tif->tif_rawdatasize = 0;
  871. return (0);
  872. }
  873. return (1);
  874. }
  875. /*
  876. * Set state to appear as if a
  877. * strip has just been read in.
  878. */
  879. static int
  880. TIFFStartStrip(TIFF* tif, uint32 strip)
  881. {
  882. TIFFDirectory *td = &tif->tif_dir;
  883. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  884. return 0;
  885. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  886. if (!(*tif->tif_setupdecode)(tif))
  887. return (0);
  888. tif->tif_flags |= TIFF_CODERSETUP;
  889. }
  890. tif->tif_curstrip = strip;
  891. tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
  892. tif->tif_flags &= ~TIFF_BUF4WRITE;
  893. if (tif->tif_flags&TIFF_NOREADRAW)
  894. {
  895. tif->tif_rawcp = NULL;
  896. tif->tif_rawcc = 0;
  897. }
  898. else
  899. {
  900. tif->tif_rawcp = tif->tif_rawdata;
  901. tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
  902. }
  903. return ((*tif->tif_predecode)(tif,
  904. (uint16)(strip / td->td_stripsperimage)));
  905. }
  906. /*
  907. * Set state to appear as if a
  908. * tile has just been read in.
  909. */
  910. static int
  911. TIFFStartTile(TIFF* tif, uint32 tile)
  912. {
  913. TIFFDirectory *td = &tif->tif_dir;
  914. if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
  915. return 0;
  916. if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
  917. if (!(*tif->tif_setupdecode)(tif))
  918. return (0);
  919. tif->tif_flags |= TIFF_CODERSETUP;
  920. }
  921. tif->tif_curtile = tile;
  922. tif->tif_row =
  923. (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth)) *
  924. td->td_tilelength;
  925. tif->tif_col =
  926. (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength)) *
  927. td->td_tilewidth;
  928. tif->tif_flags &= ~TIFF_BUF4WRITE;
  929. if (tif->tif_flags&TIFF_NOREADRAW)
  930. {
  931. tif->tif_rawcp = NULL;
  932. tif->tif_rawcc = 0;
  933. }
  934. else
  935. {
  936. tif->tif_rawcp = tif->tif_rawdata;
  937. tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
  938. }
  939. return ((*tif->tif_predecode)(tif,
  940. (uint16)(tile/td->td_stripsperimage)));
  941. }
  942. static int
  943. TIFFCheckRead(TIFF* tif, int tiles)
  944. {
  945. if (tif->tif_mode == O_WRONLY) {
  946. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
  947. return (0);
  948. }
  949. if (tiles ^ isTiled(tif)) {
  950. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
  951. "Can not read tiles from a stripped image" :
  952. "Can not read scanlines from a tiled image");
  953. return (0);
  954. }
  955. return (1);
  956. }
  957. void
  958. _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
  959. {
  960. (void) tif; (void) buf; (void) cc;
  961. }
  962. void
  963. _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  964. {
  965. (void) tif;
  966. assert((cc & 1) == 0);
  967. TIFFSwabArrayOfShort((uint16*) buf, cc/2);
  968. }
  969. void
  970. _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  971. {
  972. (void) tif;
  973. assert((cc % 3) == 0);
  974. TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
  975. }
  976. void
  977. _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  978. {
  979. (void) tif;
  980. assert((cc & 3) == 0);
  981. TIFFSwabArrayOfLong((uint32*) buf, cc/4);
  982. }
  983. void
  984. _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
  985. {
  986. (void) tif;
  987. assert((cc & 7) == 0);
  988. TIFFSwabArrayOfDouble((double*) buf, cc/8);
  989. }
  990. /* vim: set ts=8 sts=8 sw=8 noet: */
  991. /*
  992. * Local Variables:
  993. * mode: c
  994. * c-basic-offset: 8
  995. * fill-column: 78
  996. * End:
  997. */