1 /* $Id: tif_read.c,v 1.45 2015-06-07 22:35:40 bfriesen Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 * Scanline-oriented Read Support
34 int TIFFFillStrip(TIFF* tif, uint32 strip);
35 int TIFFFillTile(TIFF* tif, uint32 tile);
36 static int TIFFStartStrip(TIFF* tif, uint32 strip);
37 static int TIFFStartTile(TIFF* tif, uint32 tile);
38 static int TIFFCheckRead(TIFF*, int);
40 TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
42 #define NOSTRIP ((uint32)(-1)) /* undefined state */
43 #define NOTILE ((uint32)(-1)) /* undefined state */
46 TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
48 static const char module[] = "TIFFFillStripPartial";
49 register TIFFDirectory *td = &tif->tif_dir;
53 /* tmsize_t bytecountm; */
55 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
59 * Expand raw data buffer, if needed, to hold data
60 * strip coming from file (perhaps should set upper
61 * bound on the size of a buffer we'll use?).
64 /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */
65 if (read_ahead*2 > tif->tif_rawdatasize) {
68 tif->tif_curstrip = NOSTRIP;
69 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
70 TIFFErrorExt(tif->tif_clientdata, module,
71 "Data buffer too small to hold part of strip %lu",
72 (unsigned long) strip);
75 if (!TIFFReadBufferSetup(tif, 0, read_ahead*2))
81 tif->tif_rawdataloaded = 0;
82 tif->tif_rawdataoff = 0;
86 ** If we are reading more data, move any unused data to the
87 ** start of the buffer.
89 if( tif->tif_rawdataloaded > 0 )
90 unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
96 assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
97 memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
101 ** Seek to the point in the file where more data should be read.
103 read_offset = td->td_stripoffset[strip]
104 + tif->tif_rawdataoff + tif->tif_rawdataloaded;
106 if (!SeekOK(tif, read_offset)) {
107 TIFFErrorExt(tif->tif_clientdata, module,
108 "Seek error at scanline %lu, strip %lu",
109 (unsigned long) tif->tif_row, (unsigned long) strip);
114 ** How much do we want to read?
116 to_read = tif->tif_rawdatasize - unused_data;
117 if( (uint64) to_read > td->td_stripbytecount[strip]
118 - tif->tif_rawdataoff - tif->tif_rawdataloaded )
120 to_read = (tmsize_t) td->td_stripbytecount[strip]
121 - tif->tif_rawdataoff - tif->tif_rawdataloaded;
124 assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
125 cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read);
128 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
129 TIFFErrorExt(tif->tif_clientdata, module,
130 "Read error at scanline %lu; got %I64u bytes, expected %I64u",
131 (unsigned long) tif->tif_row,
132 (unsigned __int64) cc,
133 (unsigned __int64) to_read);
135 TIFFErrorExt(tif->tif_clientdata, module,
136 "Read error at scanline %lu; got %llu bytes, expected %llu",
137 (unsigned long) tif->tif_row,
138 (unsigned long long) cc,
139 (unsigned long long) to_read);
144 tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
145 tif->tif_rawdataloaded = unused_data + to_read;
147 tif->tif_rawcp = tif->tif_rawdata;
149 if (!isFillOrder(tif, td->td_fillorder) &&
150 (tif->tif_flags & TIFF_NOBITREV) == 0) {
151 assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
152 TIFFReverseBits(tif->tif_rawdata + unused_data, to_read );
156 ** When starting a strip from the beginning we need to
157 ** restart the decoder.
160 return TIFFStartStrip(tif, strip);
166 * Seek to a random row+sample in a file.
168 * Only used by TIFFReadScanline, and is only used on
169 * strip organized files. We do some tricky stuff to try
170 * and avoid reading the whole compressed raw data for big
174 TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
176 register TIFFDirectory *td = &tif->tif_dir;
179 tmsize_t read_ahead = 0;
182 ** Establish what strip we are working from.
184 if (row >= td->td_imagelength) { /* out of range */
185 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
186 "%lu: Row out of range, max %lu",
188 (unsigned long) td->td_imagelength);
191 if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
192 if (sample >= td->td_samplesperpixel) {
193 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
194 "%lu: Sample out of range, max %lu",
195 (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
198 strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
200 strip = row / td->td_rowsperstrip;
203 * Do we want to treat this strip as one whole chunk or
204 * read it a few lines at a time?
206 #if defined(CHUNKY_STRIP_READ_SUPPORT)
207 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
209 whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
217 read_ahead = tif->tif_scanlinesize * 16 + 5000;
221 * If we haven't loaded this strip, do so now, possibly
222 * only reading the first part.
224 if (strip != tif->tif_curstrip) { /* different strip, refill */
228 if (!TIFFFillStrip(tif, strip))
233 if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
239 ** If we already have some data loaded, do we need to read some more?
241 else if( !whole_strip )
243 if( ((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < read_ahead
244 && (uint64) tif->tif_rawdataoff+tif->tif_rawdataloaded < td->td_stripbytecount[strip] )
246 if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
251 if (row < tif->tif_row) {
253 * Moving backwards within the same strip: backup
254 * to the start and then decode forward (below).
256 * NB: If you're planning on lots of random access within a
257 * strip, it's better to just read and decode the entire
258 * strip, and then access the decoded data in a random fashion.
261 if( tif->tif_rawdataoff != 0 )
263 if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
268 if (!TIFFStartStrip(tif, strip))
273 if (row != tif->tif_row) {
275 * Seek forward to the desired row.
278 /* TODO: Will this really work with partial buffers? */
280 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
289 TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
293 if (!TIFFCheckRead(tif, 0))
295 if( (e = TIFFSeek(tif, row, sample)) != 0) {
297 * Decompress desired row into user buffer.
299 e = (*tif->tif_decoderow)
300 (tif, (uint8*) buf, tif->tif_scanlinesize, sample);
302 /* we are now poised at the beginning of the next row */
303 tif->tif_row = row + 1;
306 (*tif->tif_postdecode)(tif, (uint8*) buf,
307 tif->tif_scanlinesize);
309 return (e > 0 ? 1 : -1);
313 * Read a strip of data and decompress the specified
314 * amount into the user-supplied buffer.
317 TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
319 static const char module[] = "TIFFReadEncodedStrip";
320 TIFFDirectory *td = &tif->tif_dir;
322 uint32 stripsperplane;
327 if (!TIFFCheckRead(tif,0))
328 return((tmsize_t)(-1));
329 if (strip>=td->td_nstrips)
331 TIFFErrorExt(tif->tif_clientdata,module,
332 "%lu: Strip out of range, max %lu",(unsigned long)strip,
333 (unsigned long)td->td_nstrips);
334 return((tmsize_t)(-1));
337 * Calculate the strip size according to the number of
338 * rows in the strip (check for truncated last strip on any
339 * of the separations).
341 rowsperstrip=td->td_rowsperstrip;
342 if (rowsperstrip>td->td_imagelength)
343 rowsperstrip=td->td_imagelength;
344 stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);
345 stripinplane=(strip%stripsperplane);
346 plane=(strip/stripsperplane);
347 rows=td->td_imagelength-stripinplane*rowsperstrip;
348 if (rows>rowsperstrip)
350 stripsize=TIFFVStripSize(tif,rows);
352 return((tmsize_t)(-1));
353 if ((size!=(tmsize_t)(-1))&&(size<stripsize))
355 if (!TIFFFillStrip(tif,strip))
356 return((tmsize_t)(-1));
357 if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
358 return((tmsize_t)(-1));
359 (*tif->tif_postdecode)(tif,buf,stripsize);
364 TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
367 TIFFDirectory *td = &tif->tif_dir;
369 if (!_TIFFFillStriles( tif ))
370 return ((tmsize_t)(-1));
372 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
373 if (!isMapped(tif)) {
376 if (!SeekOK(tif, td->td_stripoffset[strip])) {
377 TIFFErrorExt(tif->tif_clientdata, module,
378 "Seek error at scanline %lu, strip %lu",
379 (unsigned long) tif->tif_row, (unsigned long) strip);
380 return ((tmsize_t)(-1));
382 cc = TIFFReadFile(tif, buf, size);
384 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
385 TIFFErrorExt(tif->tif_clientdata, module,
386 "Read error at scanline %lu; got %I64u bytes, expected %I64u",
387 (unsigned long) tif->tif_row,
388 (unsigned __int64) cc,
389 (unsigned __int64) size);
391 TIFFErrorExt(tif->tif_clientdata, module,
392 "Read error at scanline %lu; got %llu bytes, expected %llu",
393 (unsigned long) tif->tif_row,
394 (unsigned long long) cc,
395 (unsigned long long) size);
397 return ((tmsize_t)(-1));
402 ma=(tmsize_t)td->td_stripoffset[strip];
404 if (((uint64)ma!=td->td_stripoffset[strip])||(ma>tif->tif_size))
406 else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
411 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
412 TIFFErrorExt(tif->tif_clientdata, module,
413 "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
414 (unsigned long) tif->tif_row,
415 (unsigned long) strip,
416 (unsigned __int64) n,
417 (unsigned __int64) size);
419 TIFFErrorExt(tif->tif_clientdata, module,
420 "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
421 (unsigned long) tif->tif_row,
422 (unsigned long) strip,
423 (unsigned long long) n,
424 (unsigned long long) size);
426 return ((tmsize_t)(-1));
428 _TIFFmemcpy(buf, tif->tif_base + ma,
435 * Read a strip of data from the file.
438 TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
440 static const char module[] = "TIFFReadRawStrip";
441 TIFFDirectory *td = &tif->tif_dir;
445 if (!TIFFCheckRead(tif, 0))
446 return ((tmsize_t)(-1));
447 if (strip >= td->td_nstrips) {
448 TIFFErrorExt(tif->tif_clientdata, module,
449 "%lu: Strip out of range, max %lu",
450 (unsigned long) strip,
451 (unsigned long) td->td_nstrips);
452 return ((tmsize_t)(-1));
454 if (tif->tif_flags&TIFF_NOREADRAW)
456 TIFFErrorExt(tif->tif_clientdata, module,
457 "Compression scheme does not support access to raw uncompressed data");
458 return ((tmsize_t)(-1));
460 bytecount = td->td_stripbytecount[strip];
461 if ((int64)bytecount <= 0) {
462 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
463 TIFFErrorExt(tif->tif_clientdata, module,
464 "%I64u: Invalid strip byte count, strip %lu",
465 (unsigned __int64) bytecount,
466 (unsigned long) strip);
468 TIFFErrorExt(tif->tif_clientdata, module,
469 "%llu: Invalid strip byte count, strip %lu",
470 (unsigned long long) bytecount,
471 (unsigned long) strip);
473 return ((tmsize_t)(-1));
475 bytecountm = (tmsize_t)bytecount;
476 if ((uint64)bytecountm!=bytecount) {
477 TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
478 return ((tmsize_t)(-1));
480 if (size != (tmsize_t)(-1) && size < bytecountm)
482 return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
486 * Read the specified strip and setup for decoding. The data buffer is
487 * expanded, as necessary, to hold the strip's data.
490 TIFFFillStrip(TIFF* tif, uint32 strip)
492 static const char module[] = "TIFFFillStrip";
493 TIFFDirectory *td = &tif->tif_dir;
495 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
498 if ((tif->tif_flags&TIFF_NOREADRAW)==0)
500 uint64 bytecount = td->td_stripbytecount[strip];
501 if ((int64)bytecount <= 0) {
502 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
503 TIFFErrorExt(tif->tif_clientdata, module,
504 "Invalid strip byte count %I64u, strip %lu",
505 (unsigned __int64) bytecount,
506 (unsigned long) strip);
508 TIFFErrorExt(tif->tif_clientdata, module,
509 "Invalid strip byte count %llu, strip %lu",
510 (unsigned long long) bytecount,
511 (unsigned long) strip);
516 (isFillOrder(tif, td->td_fillorder)
517 || (tif->tif_flags & TIFF_NOBITREV))) {
519 * The image is mapped into memory and we either don't
520 * need to flip bits or the compression routine is
521 * going to handle this operation itself. In this
522 * case, avoid copying the raw data and instead just
523 * reference the data from the memory mapped file
524 * image. This assumes that the decompression
525 * routines do not modify the contents of the raw data
526 * buffer (if they try to, the application will get a
527 * fault since the file is mapped read-only).
529 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
530 _TIFFfree(tif->tif_rawdata);
531 tif->tif_rawdata = NULL;
532 tif->tif_rawdatasize = 0;
534 tif->tif_flags &= ~TIFF_MYBUFFER;
536 * We must check for overflow, potentially causing
537 * an OOB read. Instead of simple
539 * td->td_stripoffset[strip]+bytecount > tif->tif_size
541 * comparison (which can overflow) we do the following
544 if (bytecount > (uint64)tif->tif_size ||
545 td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
547 * This error message might seem strange, but
548 * it's what would happen if a read were done
551 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
552 TIFFErrorExt(tif->tif_clientdata, module,
554 "Read error on strip %lu; "
555 "got %I64u bytes, expected %I64u",
556 (unsigned long) strip,
557 (unsigned __int64) tif->tif_size - td->td_stripoffset[strip],
558 (unsigned __int64) bytecount);
560 TIFFErrorExt(tif->tif_clientdata, module,
562 "Read error on strip %lu; "
563 "got %llu bytes, expected %llu",
564 (unsigned long) strip,
565 (unsigned long long) tif->tif_size - td->td_stripoffset[strip],
566 (unsigned long long) bytecount);
568 tif->tif_curstrip = NOSTRIP;
571 tif->tif_rawdatasize = (tmsize_t)bytecount;
572 tif->tif_rawdata = tif->tif_base + (tmsize_t)td->td_stripoffset[strip];
573 tif->tif_rawdataoff = 0;
574 tif->tif_rawdataloaded = (tmsize_t) bytecount;
577 * When we have tif_rawdata reference directly into the memory mapped file
578 * we need to be pretty careful about how we use the rawdata. It is not
579 * a general purpose working buffer as it normally otherwise is. So we
580 * keep track of this fact to avoid using it improperly.
582 tif->tif_flags |= TIFF_BUFFERMMAP;
585 * Expand raw data buffer, if needed, to hold data
586 * strip coming from file (perhaps should set upper
587 * bound on the size of a buffer we'll use?).
590 bytecountm=(tmsize_t)bytecount;
591 if ((uint64)bytecountm!=bytecount)
593 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
596 if (bytecountm > tif->tif_rawdatasize) {
597 tif->tif_curstrip = NOSTRIP;
598 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
599 TIFFErrorExt(tif->tif_clientdata, module,
600 "Data buffer too small to hold strip %lu",
601 (unsigned long) strip);
604 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
607 if (tif->tif_flags&TIFF_BUFFERMMAP) {
608 tif->tif_curstrip = NOSTRIP;
609 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
612 if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
613 bytecountm, module) != bytecountm)
616 tif->tif_rawdataoff = 0;
617 tif->tif_rawdataloaded = bytecountm;
619 if (!isFillOrder(tif, td->td_fillorder) &&
620 (tif->tif_flags & TIFF_NOBITREV) == 0)
621 TIFFReverseBits(tif->tif_rawdata, bytecountm);
624 return (TIFFStartStrip(tif, strip));
628 * Tile-oriented Read Support
629 * Contributed by Nancy Cam (Silicon Graphics).
633 * Read and decompress a tile of data. The
634 * tile is selected by the (x,y,z,s) coordinates.
637 TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
639 if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s))
640 return ((tmsize_t)(-1));
641 return (TIFFReadEncodedTile(tif,
642 TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
646 * Read a tile of data and decompress the specified
647 * amount into the user-supplied buffer.
650 TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
652 static const char module[] = "TIFFReadEncodedTile";
653 TIFFDirectory *td = &tif->tif_dir;
654 tmsize_t tilesize = tif->tif_tilesize;
656 if (!TIFFCheckRead(tif, 1))
657 return ((tmsize_t)(-1));
658 if (tile >= td->td_nstrips) {
659 TIFFErrorExt(tif->tif_clientdata, module,
660 "%lu: Tile out of range, max %lu",
661 (unsigned long) tile, (unsigned long) td->td_nstrips);
662 return ((tmsize_t)(-1));
664 if (size == (tmsize_t)(-1))
666 else if (size > tilesize)
668 if (TIFFFillTile(tif, tile) && (*tif->tif_decodetile)(tif,
669 (uint8*) buf, size, (uint16)(tile/td->td_stripsperimage))) {
670 (*tif->tif_postdecode)(tif, (uint8*) buf, size);
673 return ((tmsize_t)(-1));
677 TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
679 TIFFDirectory *td = &tif->tif_dir;
681 if (!_TIFFFillStriles( tif ))
682 return ((tmsize_t)(-1));
684 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
685 if (!isMapped(tif)) {
688 if (!SeekOK(tif, td->td_stripoffset[tile])) {
689 TIFFErrorExt(tif->tif_clientdata, module,
690 "Seek error at row %lu, col %lu, tile %lu",
691 (unsigned long) tif->tif_row,
692 (unsigned long) tif->tif_col,
693 (unsigned long) tile);
694 return ((tmsize_t)(-1));
696 cc = TIFFReadFile(tif, buf, size);
698 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
699 TIFFErrorExt(tif->tif_clientdata, module,
700 "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
701 (unsigned long) tif->tif_row,
702 (unsigned long) tif->tif_col,
703 (unsigned __int64) cc,
704 (unsigned __int64) size);
706 TIFFErrorExt(tif->tif_clientdata, module,
707 "Read error at row %lu, col %lu; got %llu bytes, expected %llu",
708 (unsigned long) tif->tif_row,
709 (unsigned long) tif->tif_col,
710 (unsigned long long) cc,
711 (unsigned long long) size);
713 return ((tmsize_t)(-1));
718 ma=(tmsize_t)td->td_stripoffset[tile];
720 if (((uint64)ma!=td->td_stripoffset[tile])||(ma>tif->tif_size))
722 else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
727 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
728 TIFFErrorExt(tif->tif_clientdata, module,
729 "Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
730 (unsigned long) tif->tif_row,
731 (unsigned long) tif->tif_col,
732 (unsigned long) tile,
733 (unsigned __int64) n,
734 (unsigned __int64) size);
736 TIFFErrorExt(tif->tif_clientdata, module,
737 "Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
738 (unsigned long) tif->tif_row,
739 (unsigned long) tif->tif_col,
740 (unsigned long) tile,
741 (unsigned long long) n,
742 (unsigned long long) size);
744 return ((tmsize_t)(-1));
746 _TIFFmemcpy(buf, tif->tif_base + ma, size);
752 * Read a tile of data from the file.
755 TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
757 static const char module[] = "TIFFReadRawTile";
758 TIFFDirectory *td = &tif->tif_dir;
762 if (!TIFFCheckRead(tif, 1))
763 return ((tmsize_t)(-1));
764 if (tile >= td->td_nstrips) {
765 TIFFErrorExt(tif->tif_clientdata, module,
766 "%lu: Tile out of range, max %lu",
767 (unsigned long) tile, (unsigned long) td->td_nstrips);
768 return ((tmsize_t)(-1));
770 if (tif->tif_flags&TIFF_NOREADRAW)
772 TIFFErrorExt(tif->tif_clientdata, module,
773 "Compression scheme does not support access to raw uncompressed data");
774 return ((tmsize_t)(-1));
776 bytecount64 = td->td_stripbytecount[tile];
777 if (size != (tmsize_t)(-1) && (uint64)size < bytecount64)
778 bytecount64 = (uint64)size;
779 bytecountm = (tmsize_t)bytecount64;
780 if ((uint64)bytecountm!=bytecount64)
782 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
783 return ((tmsize_t)(-1));
785 return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
789 * Read the specified tile and setup for decoding. The data buffer is
790 * expanded, as necessary, to hold the tile's data.
793 TIFFFillTile(TIFF* tif, uint32 tile)
795 static const char module[] = "TIFFFillTile";
796 TIFFDirectory *td = &tif->tif_dir;
798 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
801 if ((tif->tif_flags&TIFF_NOREADRAW)==0)
803 uint64 bytecount = td->td_stripbytecount[tile];
804 if ((int64)bytecount <= 0) {
805 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
806 TIFFErrorExt(tif->tif_clientdata, module,
807 "%I64u: Invalid tile byte count, tile %lu",
808 (unsigned __int64) bytecount,
809 (unsigned long) tile);
811 TIFFErrorExt(tif->tif_clientdata, module,
812 "%llu: Invalid tile byte count, tile %lu",
813 (unsigned long long) bytecount,
814 (unsigned long) tile);
819 (isFillOrder(tif, td->td_fillorder)
820 || (tif->tif_flags & TIFF_NOBITREV))) {
822 * The image is mapped into memory and we either don't
823 * need to flip bits or the compression routine is
824 * going to handle this operation itself. In this
825 * case, avoid copying the raw data and instead just
826 * reference the data from the memory mapped file
827 * image. This assumes that the decompression
828 * routines do not modify the contents of the raw data
829 * buffer (if they try to, the application will get a
830 * fault since the file is mapped read-only).
832 if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
833 _TIFFfree(tif->tif_rawdata);
834 tif->tif_rawdata = NULL;
835 tif->tif_rawdatasize = 0;
837 tif->tif_flags &= ~TIFF_MYBUFFER;
839 * We must check for overflow, potentially causing
840 * an OOB read. Instead of simple
842 * td->td_stripoffset[tile]+bytecount > tif->tif_size
844 * comparison (which can overflow) we do the following
847 if (bytecount > (uint64)tif->tif_size ||
848 td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
849 tif->tif_curtile = NOTILE;
852 tif->tif_rawdatasize = (tmsize_t)bytecount;
854 tif->tif_base + (tmsize_t)td->td_stripoffset[tile];
855 tif->tif_rawdataoff = 0;
856 tif->tif_rawdataloaded = (tmsize_t) bytecount;
857 tif->tif_flags |= TIFF_BUFFERMMAP;
860 * Expand raw data buffer, if needed, to hold data
861 * tile coming from file (perhaps should set upper
862 * bound on the size of a buffer we'll use?).
865 bytecountm=(tmsize_t)bytecount;
866 if ((uint64)bytecountm!=bytecount)
868 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
871 if (bytecountm > tif->tif_rawdatasize) {
872 tif->tif_curtile = NOTILE;
873 if ((tif->tif_flags & TIFF_MYBUFFER) == 0) {
874 TIFFErrorExt(tif->tif_clientdata, module,
875 "Data buffer too small to hold tile %lu",
876 (unsigned long) tile);
879 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
882 if (tif->tif_flags&TIFF_BUFFERMMAP) {
883 tif->tif_curtile = NOTILE;
884 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
888 if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
889 bytecountm, module) != bytecountm)
892 tif->tif_rawdataoff = 0;
893 tif->tif_rawdataloaded = bytecountm;
895 if (!isFillOrder(tif, td->td_fillorder) &&
896 (tif->tif_flags & TIFF_NOBITREV) == 0)
897 TIFFReverseBits(tif->tif_rawdata,
898 tif->tif_rawdataloaded);
901 return (TIFFStartTile(tif, tile));
905 * Setup the raw data buffer in preparation for
906 * reading a strip of raw data. If the buffer
907 * is specified as zero, then a buffer of appropriate
908 * size is allocated by the library. Otherwise,
909 * the client must guarantee that the buffer is
910 * large enough to hold any individual strip of
914 TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
916 static const char module[] = "TIFFReadBufferSetup";
918 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
919 tif->tif_flags &= ~TIFF_BUFFERMMAP;
921 if (tif->tif_rawdata) {
922 if (tif->tif_flags & TIFF_MYBUFFER)
923 _TIFFfree(tif->tif_rawdata);
924 tif->tif_rawdata = NULL;
925 tif->tif_rawdatasize = 0;
928 tif->tif_rawdatasize = size;
929 tif->tif_rawdata = (uint8*) bp;
930 tif->tif_flags &= ~TIFF_MYBUFFER;
932 tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64)size, 1024);
933 if (tif->tif_rawdatasize==0) {
934 TIFFErrorExt(tif->tif_clientdata, module,
935 "Invalid buffer size");
938 tif->tif_rawdata = (uint8*) _TIFFmalloc(tif->tif_rawdatasize);
939 tif->tif_flags |= TIFF_MYBUFFER;
941 if (tif->tif_rawdata == NULL) {
942 TIFFErrorExt(tif->tif_clientdata, module,
943 "No space for data buffer at scanline %lu",
944 (unsigned long) tif->tif_row);
945 tif->tif_rawdatasize = 0;
952 * Set state to appear as if a
953 * strip has just been read in.
956 TIFFStartStrip(TIFF* tif, uint32 strip)
958 TIFFDirectory *td = &tif->tif_dir;
960 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
963 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
964 if (!(*tif->tif_setupdecode)(tif))
966 tif->tif_flags |= TIFF_CODERSETUP;
968 tif->tif_curstrip = strip;
969 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
970 tif->tif_flags &= ~TIFF_BUF4WRITE;
972 if (tif->tif_flags&TIFF_NOREADRAW)
974 tif->tif_rawcp = NULL;
979 tif->tif_rawcp = tif->tif_rawdata;
980 tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
982 return ((*tif->tif_predecode)(tif,
983 (uint16)(strip / td->td_stripsperimage)));
987 * Set state to appear as if a
988 * tile has just been read in.
991 TIFFStartTile(TIFF* tif, uint32 tile)
993 static const char module[] = "TIFFStartTile";
994 TIFFDirectory *td = &tif->tif_dir;
997 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
1000 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
1001 if (!(*tif->tif_setupdecode)(tif))
1003 tif->tif_flags |= TIFF_CODERSETUP;
1005 tif->tif_curtile = tile;
1006 howmany32=TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth);
1007 if (howmany32 == 0) {
1008 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
1011 tif->tif_row = (tile % howmany32) * td->td_tilelength;
1012 howmany32=TIFFhowmany_32(td->td_imagelength, td->td_tilelength);
1013 if (howmany32 == 0) {
1014 TIFFErrorExt(tif->tif_clientdata,module,"Zero tiles");
1017 tif->tif_col = (tile % howmany32) * td->td_tilewidth;
1018 tif->tif_flags &= ~TIFF_BUF4WRITE;
1019 if (tif->tif_flags&TIFF_NOREADRAW)
1021 tif->tif_rawcp = NULL;
1026 tif->tif_rawcp = tif->tif_rawdata;
1027 tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
1029 return ((*tif->tif_predecode)(tif,
1030 (uint16)(tile/td->td_stripsperimage)));
1034 TIFFCheckRead(TIFF* tif, int tiles)
1036 if (tif->tif_mode == O_WRONLY) {
1037 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
1040 if (tiles ^ isTiled(tif)) {
1041 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, tiles ?
1042 "Can not read tiles from a stripped image" :
1043 "Can not read scanlines from a tiled image");
1050 _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
1052 (void) tif; (void) buf; (void) cc;
1056 _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1059 assert((cc & 1) == 0);
1060 TIFFSwabArrayOfShort((uint16*) buf, cc/2);
1064 _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1067 assert((cc % 3) == 0);
1068 TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
1072 _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1075 assert((cc & 3) == 0);
1076 TIFFSwabArrayOfLong((uint32*) buf, cc/4);
1080 _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1083 assert((cc & 7) == 0);
1084 TIFFSwabArrayOfDouble((double*) buf, cc/8);
1087 /* vim: set ts=8 sts=8 sw=8 noet: */