]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/libtiff/tif_read.c
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
28 * Scanline-oriented Read Support
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);
39 TIFFReadRawStrip1(TIFF
* tif
, uint32 strip
, void* buf
, tmsize_t size
,const char* module);
41 #define NOSTRIP ((uint32)(-1)) /* undefined state */
42 #define NOTILE ((uint32)(-1)) /* undefined state */
45 TIFFFillStripPartial( TIFF
*tif
, int strip
, tmsize_t read_ahead
, int restart
)
47 static const char module[] = "TIFFFillStripPartial";
48 register TIFFDirectory
*td
= &tif
->tif_dir
;
52 /* tmsize_t bytecountm; */
54 if (!_TIFFFillStriles( tif
) || !tif
->tif_dir
.td_stripbytecount
)
58 * Expand raw data buffer, if needed, to hold data
59 * strip coming from file (perhaps should set upper
60 * bound on the size of a buffer we'll use?).
63 /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */
64 if (read_ahead
*2 > tif
->tif_rawdatasize
) {
67 tif
->tif_curstrip
= NOSTRIP
;
68 if ((tif
->tif_flags
& TIFF_MYBUFFER
) == 0) {
69 TIFFErrorExt(tif
->tif_clientdata
, module,
70 "Data buffer too small to hold part of strip %lu",
71 (unsigned long) strip
);
74 if (!TIFFReadBufferSetup(tif
, 0, read_ahead
*2))
80 tif
->tif_rawdataloaded
= 0;
81 tif
->tif_rawdataoff
= 0;
85 ** If we are reading more data, move any unused data to the
86 ** start of the buffer.
88 if( tif
->tif_rawdataloaded
> 0 )
89 unused_data
= tif
->tif_rawdataloaded
- (tif
->tif_rawcp
- tif
->tif_rawdata
);
95 assert((tif
->tif_flags
&TIFF_BUFFERMMAP
)==0);
96 memmove( tif
->tif_rawdata
, tif
->tif_rawcp
, unused_data
);
100 ** Seek to the point in the file where more data should be read.
102 read_offset
= td
->td_stripoffset
[strip
]
103 + tif
->tif_rawdataoff
+ tif
->tif_rawdataloaded
;
105 if (!SeekOK(tif
, read_offset
)) {
106 TIFFErrorExt(tif
->tif_clientdata
, module,
107 "Seek error at scanline %lu, strip %lu",
108 (unsigned long) tif
->tif_row
, (unsigned long) strip
);
113 ** How much do we want to read?
115 to_read
= tif
->tif_rawdatasize
- unused_data
;
116 if( (uint64
) to_read
> td
->td_stripbytecount
[strip
]
117 - tif
->tif_rawdataoff
- tif
->tif_rawdataloaded
)
119 to_read
= (tmsize_t
)td
->td_stripbytecount
[strip
]
120 - tif
->tif_rawdataoff
- tif
->tif_rawdataloaded
;
123 assert((tif
->tif_flags
&TIFF_BUFFERMMAP
)==0);
124 cc
= TIFFReadFile(tif
, tif
->tif_rawdata
+ unused_data
, to_read
);
127 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
128 TIFFErrorExt(tif
->tif_clientdata
, module,
129 "Read error at scanline %lu; got %I64u bytes, expected %I64u",
130 (unsigned long) tif
->tif_row
,
131 (unsigned __int64
) cc
,
132 (unsigned __int64
) to_read
);
134 TIFFErrorExt(tif
->tif_clientdata
, module,
135 "Read error at scanline %lu; got %llu bytes, expected %llu",
136 (unsigned long) tif
->tif_row
,
137 (unsigned long long) cc
,
138 (unsigned long long) to_read
);
143 tif
->tif_rawdataoff
= tif
->tif_rawdataoff
+ tif
->tif_rawdataloaded
- unused_data
;
144 tif
->tif_rawdataloaded
= unused_data
+ to_read
;
146 tif
->tif_rawcp
= tif
->tif_rawdata
;
148 if (!isFillOrder(tif
, td
->td_fillorder
) &&
149 (tif
->tif_flags
& TIFF_NOBITREV
) == 0) {
150 assert((tif
->tif_flags
&TIFF_BUFFERMMAP
)==0);
151 TIFFReverseBits(tif
->tif_rawdata
+ unused_data
, to_read
);
155 ** When starting a strip from the beginning we need to
156 ** restart the decoder.
159 return TIFFStartStrip(tif
, strip
);
165 * Seek to a random row+sample in a file.
167 * Only used by TIFFReadScanline, and is only used on
168 * strip organized files. We do some tricky stuff to try
169 * and avoid reading the whole compressed raw data for big
173 TIFFSeek(TIFF
* tif
, uint32 row
, uint16 sample
)
175 register TIFFDirectory
*td
= &tif
->tif_dir
;
178 tmsize_t read_ahead
= 0;
181 ** Establish what strip we are working from.
183 if (row
>= td
->td_imagelength
) { /* out of range */
184 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
185 "%lu: Row out of range, max %lu",
187 (unsigned long) td
->td_imagelength
);
190 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
) {
191 if (sample
>= td
->td_samplesperpixel
) {
192 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
193 "%lu: Sample out of range, max %lu",
194 (unsigned long) sample
, (unsigned long) td
->td_samplesperpixel
);
197 strip
= (uint32
)sample
*td
->td_stripsperimage
+ row
/td
->td_rowsperstrip
;
199 strip
= row
/ td
->td_rowsperstrip
;
202 * Do we want to treat this strip as one whole chunk or
203 * read it a few lines at a time?
205 #if defined(CHUNKY_STRIP_READ_SUPPORT)
206 if (!_TIFFFillStriles( tif
) || !tif
->tif_dir
.td_stripbytecount
)
208 whole_strip
= tif
->tif_dir
.td_stripbytecount
[strip
] < 10
216 read_ahead
= tif
->tif_scanlinesize
* 16 + 5000;
220 * If we haven't loaded this strip, do so now, possibly
221 * only reading the first part.
223 if (strip
!= tif
->tif_curstrip
) { /* different strip, refill */
227 if (!TIFFFillStrip(tif
, strip
))
232 if( !TIFFFillStripPartial(tif
,strip
,read_ahead
,1) )
238 ** If we already have some data loaded, do we need to read some more?
240 else if( !whole_strip
)
242 if( ((tif
->tif_rawdata
+ tif
->tif_rawdataloaded
) - tif
->tif_rawcp
) < read_ahead
243 && (uint64
) tif
->tif_rawdataoff
+tif
->tif_rawdataloaded
< td
->td_stripbytecount
[strip
] )
245 if( !TIFFFillStripPartial(tif
,strip
,read_ahead
,0) )
250 if (row
< tif
->tif_row
) {
252 * Moving backwards within the same strip: backup
253 * to the start and then decode forward (below).
255 * NB: If you're planning on lots of random access within a
256 * strip, it's better to just read and decode the entire
257 * strip, and then access the decoded data in a random fashion.
260 if( tif
->tif_rawdataoff
!= 0 )
262 if( !TIFFFillStripPartial(tif
,strip
,read_ahead
,1) )
267 if (!TIFFStartStrip(tif
, strip
))
272 if (row
!= tif
->tif_row
) {
274 * Seek forward to the desired row.
277 /* TODO: Will this really work with partial buffers? */
279 if (!(*tif
->tif_seek
)(tif
, row
- tif
->tif_row
))
288 TIFFReadScanline(TIFF
* tif
, void* buf
, uint32 row
, uint16 sample
)
292 if (!TIFFCheckRead(tif
, 0))
294 if( (e
= TIFFSeek(tif
, row
, sample
)) != 0) {
296 * Decompress desired row into user buffer.
298 e
= (*tif
->tif_decoderow
)
299 (tif
, (uint8
*) buf
, tif
->tif_scanlinesize
, sample
);
301 /* we are now poised at the beginning of the next row */
302 tif
->tif_row
= row
+ 1;
305 (*tif
->tif_postdecode
)(tif
, (uint8
*) buf
,
306 tif
->tif_scanlinesize
);
308 return (e
> 0 ? 1 : -1);
312 * Read a strip of data and decompress the specified
313 * amount into the user-supplied buffer.
316 TIFFReadEncodedStrip(TIFF
* tif
, uint32 strip
, void* buf
, tmsize_t size
)
318 static const char module[] = "TIFFReadEncodedStrip";
319 TIFFDirectory
*td
= &tif
->tif_dir
;
321 uint32 stripsperplane
;
326 if (!TIFFCheckRead(tif
,0))
327 return((tmsize_t
)(-1));
328 if (strip
>=td
->td_nstrips
)
330 TIFFErrorExt(tif
->tif_clientdata
,module,
331 "%lu: Strip out of range, max %lu",(unsigned long)strip
,
332 (unsigned long)td
->td_nstrips
);
333 return((tmsize_t
)(-1));
336 * Calculate the strip size according to the number of
337 * rows in the strip (check for truncated last strip on any
338 * of the separations).
340 rowsperstrip
=td
->td_rowsperstrip
;
341 if (rowsperstrip
>td
->td_imagelength
)
342 rowsperstrip
=td
->td_imagelength
;
343 stripsperplane
=((td
->td_imagelength
+rowsperstrip
-1)/rowsperstrip
);
344 stripinplane
=(strip%stripsperplane
);
345 plane
=(strip
/stripsperplane
);
346 rows
=td
->td_imagelength
-stripinplane
*rowsperstrip
;
347 if (rows
>rowsperstrip
)
349 stripsize
=TIFFVStripSize(tif
,rows
);
351 return((tmsize_t
)(-1));
352 if ((size
!=(tmsize_t
)(-1))&&(size
<stripsize
))
354 if (!TIFFFillStrip(tif
,strip
))
355 return((tmsize_t
)(-1));
356 if ((*tif
->tif_decodestrip
)(tif
,buf
,stripsize
,plane
)<=0)
357 return((tmsize_t
)(-1));
358 (*tif
->tif_postdecode
)(tif
,buf
,stripsize
);
363 TIFFReadRawStrip1(TIFF
* tif
, uint32 strip
, void* buf
, tmsize_t size
,
366 TIFFDirectory
*td
= &tif
->tif_dir
;
368 if (!_TIFFFillStriles( tif
))
369 return ((tmsize_t
)(-1));
371 assert((tif
->tif_flags
&TIFF_NOREADRAW
)==0);
372 if (!isMapped(tif
)) {
375 if (!SeekOK(tif
, td
->td_stripoffset
[strip
])) {
376 TIFFErrorExt(tif
->tif_clientdata
, module,
377 "Seek error at scanline %lu, strip %lu",
378 (unsigned long) tif
->tif_row
, (unsigned long) strip
);
379 return ((tmsize_t
)(-1));
381 cc
= TIFFReadFile(tif
, buf
, size
);
383 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
384 TIFFErrorExt(tif
->tif_clientdata
, module,
385 "Read error at scanline %lu; got %I64u bytes, expected %I64u",
386 (unsigned long) tif
->tif_row
,
387 (unsigned __int64
) cc
,
388 (unsigned __int64
) size
);
390 TIFFErrorExt(tif
->tif_clientdata
, module,
391 "Read error at scanline %lu; got %llu bytes, expected %llu",
392 (unsigned long) tif
->tif_row
,
393 (unsigned long long) cc
,
394 (unsigned long long) size
);
396 return ((tmsize_t
)(-1));
401 ma
=(tmsize_t
)td
->td_stripoffset
[strip
];
403 if (((uint64
)ma
!=td
->td_stripoffset
[strip
])||(ma
>tif
->tif_size
))
405 else if ((mb
<ma
)||(mb
<size
)||(mb
>tif
->tif_size
))
410 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
411 TIFFErrorExt(tif
->tif_clientdata
, module,
412 "Read error at scanline %lu, strip %lu; got %I64u bytes, expected %I64u",
413 (unsigned long) tif
->tif_row
,
414 (unsigned long) strip
,
415 (unsigned __int64
) n
,
416 (unsigned __int64
) size
);
418 TIFFErrorExt(tif
->tif_clientdata
, module,
419 "Read error at scanline %lu, strip %lu; got %llu bytes, expected %llu",
420 (unsigned long) tif
->tif_row
,
421 (unsigned long) strip
,
422 (unsigned long long) n
,
423 (unsigned long long) size
);
425 return ((tmsize_t
)(-1));
427 _TIFFmemcpy(buf
, tif
->tif_base
+ ma
,
434 * Read a strip of data from the file.
437 TIFFReadRawStrip(TIFF
* tif
, uint32 strip
, void* buf
, tmsize_t size
)
439 static const char module[] = "TIFFReadRawStrip";
440 TIFFDirectory
*td
= &tif
->tif_dir
;
444 if (!TIFFCheckRead(tif
, 0))
445 return ((tmsize_t
)(-1));
446 if (strip
>= td
->td_nstrips
) {
447 TIFFErrorExt(tif
->tif_clientdata
, module,
448 "%lu: Strip out of range, max %lu",
449 (unsigned long) strip
,
450 (unsigned long) td
->td_nstrips
);
451 return ((tmsize_t
)(-1));
453 if (tif
->tif_flags
&TIFF_NOREADRAW
)
455 TIFFErrorExt(tif
->tif_clientdata
, module,
456 "Compression scheme does not support access to raw uncompressed data");
457 return ((tmsize_t
)(-1));
459 bytecount
= td
->td_stripbytecount
[strip
];
460 if (bytecount
<= 0) {
461 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
462 TIFFErrorExt(tif
->tif_clientdata
, module,
463 "%I64u: Invalid strip byte count, strip %lu",
464 (unsigned __int64
) bytecount
,
465 (unsigned long) strip
);
467 TIFFErrorExt(tif
->tif_clientdata
, module,
468 "%llu: Invalid strip byte count, strip %lu",
469 (unsigned long long) bytecount
,
470 (unsigned long) strip
);
472 return ((tmsize_t
)(-1));
474 bytecountm
= (tmsize_t
)bytecount
;
475 if ((uint64
)bytecountm
!=bytecount
) {
476 TIFFErrorExt(tif
->tif_clientdata
, module, "Integer overflow");
477 return ((tmsize_t
)(-1));
479 if (size
!= (tmsize_t
)(-1) && size
< bytecountm
)
481 return (TIFFReadRawStrip1(tif
, strip
, buf
, bytecountm
, module));
485 * Read the specified strip and setup for decoding. The data buffer is
486 * expanded, as necessary, to hold the strip's data.
489 TIFFFillStrip(TIFF
* tif
, uint32 strip
)
491 static const char module[] = "TIFFFillStrip";
492 TIFFDirectory
*td
= &tif
->tif_dir
;
494 if (!_TIFFFillStriles( tif
) || !tif
->tif_dir
.td_stripbytecount
)
497 if ((tif
->tif_flags
&TIFF_NOREADRAW
)==0)
499 uint64 bytecount
= td
->td_stripbytecount
[strip
];
500 if (bytecount
<= 0) {
501 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
502 TIFFErrorExt(tif
->tif_clientdata
, module,
503 "Invalid strip byte count %I64u, strip %lu",
504 (unsigned __int64
) bytecount
,
505 (unsigned long) strip
);
507 TIFFErrorExt(tif
->tif_clientdata
, module,
508 "Invalid strip byte count %llu, strip %lu",
509 (unsigned long long) bytecount
,
510 (unsigned long) strip
);
515 (isFillOrder(tif
, td
->td_fillorder
)
516 || (tif
->tif_flags
& TIFF_NOBITREV
))) {
518 * The image is mapped into memory and we either don't
519 * need to flip bits or the compression routine is
520 * going to handle this operation itself. In this
521 * case, avoid copying the raw data and instead just
522 * reference the data from the memory mapped file
523 * image. This assumes that the decompression
524 * routines do not modify the contents of the raw data
525 * buffer (if they try to, the application will get a
526 * fault since the file is mapped read-only).
528 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) {
529 _TIFFfree(tif
->tif_rawdata
);
530 tif
->tif_rawdata
= NULL
;
531 tif
->tif_rawdatasize
= 0;
533 tif
->tif_flags
&= ~TIFF_MYBUFFER
;
535 * We must check for overflow, potentially causing
536 * an OOB read. Instead of simple
538 * td->td_stripoffset[strip]+bytecount > tif->tif_size
540 * comparison (which can overflow) we do the following
543 if (bytecount
> (uint64
)tif
->tif_size
||
544 td
->td_stripoffset
[strip
] > (uint64
)tif
->tif_size
- bytecount
) {
546 * This error message might seem strange, but
547 * it's what would happen if a read were done
550 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
551 TIFFErrorExt(tif
->tif_clientdata
, module,
553 "Read error on strip %lu; "
554 "got %I64u bytes, expected %I64u",
555 (unsigned long) strip
,
556 (unsigned __int64
) tif
->tif_size
- td
->td_stripoffset
[strip
],
557 (unsigned __int64
) bytecount
);
559 TIFFErrorExt(tif
->tif_clientdata
, module,
561 "Read error on strip %lu; "
562 "got %llu bytes, expected %llu",
563 (unsigned long) strip
,
564 (unsigned long long) tif
->tif_size
- td
->td_stripoffset
[strip
],
565 (unsigned long long) bytecount
);
567 tif
->tif_curstrip
= NOSTRIP
;
570 tif
->tif_rawdatasize
= (tmsize_t
)bytecount
;
571 tif
->tif_rawdata
= tif
->tif_base
+ (tmsize_t
)td
->td_stripoffset
[strip
];
572 tif
->tif_rawdataoff
= 0;
573 tif
->tif_rawdataloaded
= (tmsize_t
) bytecount
;
576 * When we have tif_rawdata reference directly into the memory mapped file
577 * we need to be pretty careful about how we use the rawdata. It is not
578 * a general purpose working buffer as it normally otherwise is. So we
579 * keep track of this fact to avoid using it improperly.
581 tif
->tif_flags
|= TIFF_BUFFERMMAP
;
584 * Expand raw data buffer, if needed, to hold data
585 * strip coming from file (perhaps should set upper
586 * bound on the size of a buffer we'll use?).
589 bytecountm
=(tmsize_t
)bytecount
;
590 if ((uint64
)bytecountm
!=bytecount
)
592 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
595 if (bytecountm
> tif
->tif_rawdatasize
) {
596 tif
->tif_curstrip
= NOSTRIP
;
597 if ((tif
->tif_flags
& TIFF_MYBUFFER
) == 0) {
598 TIFFErrorExt(tif
->tif_clientdata
, module,
599 "Data buffer too small to hold strip %lu",
600 (unsigned long) strip
);
603 if (!TIFFReadBufferSetup(tif
, 0, bytecountm
))
606 if (tif
->tif_flags
&TIFF_BUFFERMMAP
) {
607 tif
->tif_curstrip
= NOSTRIP
;
608 if (!TIFFReadBufferSetup(tif
, 0, bytecountm
))
611 if (TIFFReadRawStrip1(tif
, strip
, tif
->tif_rawdata
,
612 bytecountm
, module) != bytecountm
)
615 tif
->tif_rawdataoff
= 0;
616 tif
->tif_rawdataloaded
= bytecountm
;
618 if (!isFillOrder(tif
, td
->td_fillorder
) &&
619 (tif
->tif_flags
& TIFF_NOBITREV
) == 0)
620 TIFFReverseBits(tif
->tif_rawdata
, bytecountm
);
623 return (TIFFStartStrip(tif
, strip
));
627 * Tile-oriented Read Support
628 * Contributed by Nancy Cam (Silicon Graphics).
632 * Read and decompress a tile of data. The
633 * tile is selected by the (x,y,z,s) coordinates.
636 TIFFReadTile(TIFF
* tif
, void* buf
, uint32 x
, uint32 y
, uint32 z
, uint16 s
)
638 if (!TIFFCheckRead(tif
, 1) || !TIFFCheckTile(tif
, x
, y
, z
, s
))
639 return ((tmsize_t
)(-1));
640 return (TIFFReadEncodedTile(tif
,
641 TIFFComputeTile(tif
, x
, y
, z
, s
), buf
, (tmsize_t
)(-1)));
645 * Read a tile of data and decompress the specified
646 * amount into the user-supplied buffer.
649 TIFFReadEncodedTile(TIFF
* tif
, uint32 tile
, void* buf
, tmsize_t size
)
651 static const char module[] = "TIFFReadEncodedTile";
652 TIFFDirectory
*td
= &tif
->tif_dir
;
653 tmsize_t tilesize
= tif
->tif_tilesize
;
655 if (!TIFFCheckRead(tif
, 1))
656 return ((tmsize_t
)(-1));
657 if (tile
>= td
->td_nstrips
) {
658 TIFFErrorExt(tif
->tif_clientdata
, module,
659 "%lu: Tile out of range, max %lu",
660 (unsigned long) tile
, (unsigned long) td
->td_nstrips
);
661 return ((tmsize_t
)(-1));
663 if (size
== (tmsize_t
)(-1))
665 else if (size
> tilesize
)
667 if (TIFFFillTile(tif
, tile
) && (*tif
->tif_decodetile
)(tif
,
668 (uint8
*) buf
, size
, (uint16
)(tile
/td
->td_stripsperimage
))) {
669 (*tif
->tif_postdecode
)(tif
, (uint8
*) buf
, size
);
672 return ((tmsize_t
)(-1));
676 TIFFReadRawTile1(TIFF
* tif
, uint32 tile
, void* buf
, tmsize_t size
, const char* module)
678 TIFFDirectory
*td
= &tif
->tif_dir
;
680 if (!_TIFFFillStriles( tif
))
681 return ((tmsize_t
)(-1));
683 assert((tif
->tif_flags
&TIFF_NOREADRAW
)==0);
684 if (!isMapped(tif
)) {
687 if (!SeekOK(tif
, td
->td_stripoffset
[tile
])) {
688 TIFFErrorExt(tif
->tif_clientdata
, module,
689 "Seek error at row %lu, col %lu, tile %lu",
690 (unsigned long) tif
->tif_row
,
691 (unsigned long) tif
->tif_col
,
692 (unsigned long) tile
);
693 return ((tmsize_t
)(-1));
695 cc
= TIFFReadFile(tif
, buf
, size
);
697 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
698 TIFFErrorExt(tif
->tif_clientdata
, module,
699 "Read error at row %lu, col %lu; got %I64u bytes, expected %I64u",
700 (unsigned long) tif
->tif_row
,
701 (unsigned long) tif
->tif_col
,
702 (unsigned __int64
) cc
,
703 (unsigned __int64
) size
);
705 TIFFErrorExt(tif
->tif_clientdata
, module,
706 "Read error at row %lu, col %lu; got %llu bytes, expected %llu",
707 (unsigned long) tif
->tif_row
,
708 (unsigned long) tif
->tif_col
,
709 (unsigned long long) cc
,
710 (unsigned long long) size
);
712 return ((tmsize_t
)(-1));
717 ma
=(tmsize_t
)td
->td_stripoffset
[tile
];
719 if (((uint64
)ma
!=td
->td_stripoffset
[tile
])||(ma
>tif
->tif_size
))
721 else if ((mb
<ma
)||(mb
<size
)||(mb
>tif
->tif_size
))
726 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
727 TIFFErrorExt(tif
->tif_clientdata
, module,
728 "Read error at row %lu, col %lu, tile %lu; got %I64u bytes, expected %I64u",
729 (unsigned long) tif
->tif_row
,
730 (unsigned long) tif
->tif_col
,
731 (unsigned long) tile
,
732 (unsigned __int64
) n
,
733 (unsigned __int64
) size
);
735 TIFFErrorExt(tif
->tif_clientdata
, module,
736 "Read error at row %lu, col %lu, tile %lu; got %llu bytes, expected %llu",
737 (unsigned long) tif
->tif_row
,
738 (unsigned long) tif
->tif_col
,
739 (unsigned long) tile
,
740 (unsigned long long) n
,
741 (unsigned long long) size
);
743 return ((tmsize_t
)(-1));
745 _TIFFmemcpy(buf
, tif
->tif_base
+ ma
, size
);
751 * Read a tile of data from the file.
754 TIFFReadRawTile(TIFF
* tif
, uint32 tile
, void* buf
, tmsize_t size
)
756 static const char module[] = "TIFFReadRawTile";
757 TIFFDirectory
*td
= &tif
->tif_dir
;
761 if (!TIFFCheckRead(tif
, 1))
762 return ((tmsize_t
)(-1));
763 if (tile
>= td
->td_nstrips
) {
764 TIFFErrorExt(tif
->tif_clientdata
, module,
765 "%lu: Tile out of range, max %lu",
766 (unsigned long) tile
, (unsigned long) td
->td_nstrips
);
767 return ((tmsize_t
)(-1));
769 if (tif
->tif_flags
&TIFF_NOREADRAW
)
771 TIFFErrorExt(tif
->tif_clientdata
, module,
772 "Compression scheme does not support access to raw uncompressed data");
773 return ((tmsize_t
)(-1));
775 bytecount64
= td
->td_stripbytecount
[tile
];
776 if (size
!= (tmsize_t
)(-1) && (uint64
)size
< bytecount64
)
777 bytecount64
= (uint64
)size
;
778 bytecountm
= (tmsize_t
)bytecount64
;
779 if ((uint64
)bytecountm
!=bytecount64
)
781 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
782 return ((tmsize_t
)(-1));
784 return (TIFFReadRawTile1(tif
, tile
, buf
, bytecountm
, module));
788 * Read the specified tile and setup for decoding. The data buffer is
789 * expanded, as necessary, to hold the tile's data.
792 TIFFFillTile(TIFF
* tif
, uint32 tile
)
794 static const char module[] = "TIFFFillTile";
795 TIFFDirectory
*td
= &tif
->tif_dir
;
797 if (!_TIFFFillStriles( tif
) || !tif
->tif_dir
.td_stripbytecount
)
800 if ((tif
->tif_flags
&TIFF_NOREADRAW
)==0)
802 uint64 bytecount
= td
->td_stripbytecount
[tile
];
803 if (bytecount
<= 0) {
804 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
805 TIFFErrorExt(tif
->tif_clientdata
, module,
806 "%I64u: Invalid tile byte count, tile %lu",
807 (unsigned __int64
) bytecount
,
808 (unsigned long) tile
);
810 TIFFErrorExt(tif
->tif_clientdata
, module,
811 "%llu: Invalid tile byte count, tile %lu",
812 (unsigned long long) bytecount
,
813 (unsigned long) tile
);
818 (isFillOrder(tif
, td
->td_fillorder
)
819 || (tif
->tif_flags
& TIFF_NOBITREV
))) {
821 * The image is mapped into memory and we either don't
822 * need to flip bits or the compression routine is
823 * going to handle this operation itself. In this
824 * case, avoid copying the raw data and instead just
825 * reference the data from the memory mapped file
826 * image. This assumes that the decompression
827 * routines do not modify the contents of the raw data
828 * buffer (if they try to, the application will get a
829 * fault since the file is mapped read-only).
831 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) {
832 _TIFFfree(tif
->tif_rawdata
);
833 tif
->tif_rawdata
= NULL
;
834 tif
->tif_rawdatasize
= 0;
836 tif
->tif_flags
&= ~TIFF_MYBUFFER
;
838 * We must check for overflow, potentially causing
839 * an OOB read. Instead of simple
841 * td->td_stripoffset[tile]+bytecount > tif->tif_size
843 * comparison (which can overflow) we do the following
846 if (bytecount
> (uint64
)tif
->tif_size
||
847 td
->td_stripoffset
[tile
] > (uint64
)tif
->tif_size
- bytecount
) {
848 tif
->tif_curtile
= NOTILE
;
851 tif
->tif_rawdatasize
= (tmsize_t
)bytecount
;
853 tif
->tif_base
+ (tmsize_t
)td
->td_stripoffset
[tile
];
854 tif
->tif_rawdataoff
= 0;
855 tif
->tif_rawdataloaded
= (tmsize_t
) bytecount
;
856 tif
->tif_flags
|= TIFF_BUFFERMMAP
;
859 * Expand raw data buffer, if needed, to hold data
860 * tile coming from file (perhaps should set upper
861 * bound on the size of a buffer we'll use?).
864 bytecountm
=(tmsize_t
)bytecount
;
865 if ((uint64
)bytecountm
!=bytecount
)
867 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
870 if (bytecountm
> tif
->tif_rawdatasize
) {
871 tif
->tif_curtile
= NOTILE
;
872 if ((tif
->tif_flags
& TIFF_MYBUFFER
) == 0) {
873 TIFFErrorExt(tif
->tif_clientdata
, module,
874 "Data buffer too small to hold tile %lu",
875 (unsigned long) tile
);
878 if (!TIFFReadBufferSetup(tif
, 0, bytecountm
))
881 if (tif
->tif_flags
&TIFF_BUFFERMMAP
) {
882 tif
->tif_curtile
= NOTILE
;
883 if (!TIFFReadBufferSetup(tif
, 0, bytecountm
))
887 if (TIFFReadRawTile1(tif
, tile
, tif
->tif_rawdata
,
888 bytecountm
, module) != bytecountm
)
891 tif
->tif_rawdataoff
= 0;
892 tif
->tif_rawdataloaded
= bytecountm
;
894 if (!isFillOrder(tif
, td
->td_fillorder
) &&
895 (tif
->tif_flags
& TIFF_NOBITREV
) == 0)
896 TIFFReverseBits(tif
->tif_rawdata
,
897 tif
->tif_rawdataloaded
);
900 return (TIFFStartTile(tif
, tile
));
904 * Setup the raw data buffer in preparation for
905 * reading a strip of raw data. If the buffer
906 * is specified as zero, then a buffer of appropriate
907 * size is allocated by the library. Otherwise,
908 * the client must guarantee that the buffer is
909 * large enough to hold any individual strip of
913 TIFFReadBufferSetup(TIFF
* tif
, void* bp
, tmsize_t size
)
915 static const char module[] = "TIFFReadBufferSetup";
917 assert((tif
->tif_flags
&TIFF_NOREADRAW
)==0);
918 tif
->tif_flags
&= ~TIFF_BUFFERMMAP
;
920 if (tif
->tif_rawdata
) {
921 if (tif
->tif_flags
& TIFF_MYBUFFER
)
922 _TIFFfree(tif
->tif_rawdata
);
923 tif
->tif_rawdata
= NULL
;
924 tif
->tif_rawdatasize
= 0;
927 tif
->tif_rawdatasize
= size
;
928 tif
->tif_rawdata
= (uint8
*) bp
;
929 tif
->tif_flags
&= ~TIFF_MYBUFFER
;
931 tif
->tif_rawdatasize
= (tmsize_t
)TIFFroundup_64((uint64
)size
, 1024);
932 if (tif
->tif_rawdatasize
==0)
933 tif
->tif_rawdatasize
=(tmsize_t
)(-1);
934 tif
->tif_rawdata
= (uint8
*) _TIFFmalloc(tif
->tif_rawdatasize
);
935 tif
->tif_flags
|= TIFF_MYBUFFER
;
937 if (tif
->tif_rawdata
== NULL
) {
938 TIFFErrorExt(tif
->tif_clientdata
, module,
939 "No space for data buffer at scanline %lu",
940 (unsigned long) tif
->tif_row
);
941 tif
->tif_rawdatasize
= 0;
948 * Set state to appear as if a
949 * strip has just been read in.
952 TIFFStartStrip(TIFF
* tif
, uint32 strip
)
954 TIFFDirectory
*td
= &tif
->tif_dir
;
956 if (!_TIFFFillStriles( tif
) || !tif
->tif_dir
.td_stripbytecount
)
959 if ((tif
->tif_flags
& TIFF_CODERSETUP
) == 0) {
960 if (!(*tif
->tif_setupdecode
)(tif
))
962 tif
->tif_flags
|= TIFF_CODERSETUP
;
964 tif
->tif_curstrip
= strip
;
965 tif
->tif_row
= (strip
% td
->td_stripsperimage
) * td
->td_rowsperstrip
;
966 tif
->tif_flags
&= ~TIFF_BUF4WRITE
;
968 if (tif
->tif_flags
&TIFF_NOREADRAW
)
970 tif
->tif_rawcp
= NULL
;
975 tif
->tif_rawcp
= tif
->tif_rawdata
;
976 tif
->tif_rawcc
= (tmsize_t
)td
->td_stripbytecount
[strip
];
978 return ((*tif
->tif_predecode
)(tif
,
979 (uint16
)(strip
/ td
->td_stripsperimage
)));
983 * Set state to appear as if a
984 * tile has just been read in.
987 TIFFStartTile(TIFF
* tif
, uint32 tile
)
989 TIFFDirectory
*td
= &tif
->tif_dir
;
991 if (!_TIFFFillStriles( tif
) || !tif
->tif_dir
.td_stripbytecount
)
994 if ((tif
->tif_flags
& TIFF_CODERSETUP
) == 0) {
995 if (!(*tif
->tif_setupdecode
)(tif
))
997 tif
->tif_flags
|= TIFF_CODERSETUP
;
999 tif
->tif_curtile
= tile
;
1001 (tile
% TIFFhowmany_32(td
->td_imagewidth
, td
->td_tilewidth
)) *
1004 (tile
% TIFFhowmany_32(td
->td_imagelength
, td
->td_tilelength
)) *
1006 tif
->tif_flags
&= ~TIFF_BUF4WRITE
;
1007 if (tif
->tif_flags
&TIFF_NOREADRAW
)
1009 tif
->tif_rawcp
= NULL
;
1014 tif
->tif_rawcp
= tif
->tif_rawdata
;
1015 tif
->tif_rawcc
= (tmsize_t
)td
->td_stripbytecount
[tile
];
1017 return ((*tif
->tif_predecode
)(tif
,
1018 (uint16
)(tile
/td
->td_stripsperimage
)));
1022 TIFFCheckRead(TIFF
* tif
, int tiles
)
1024 if (tif
->tif_mode
== O_WRONLY
) {
1025 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, "File not open for reading");
1028 if (tiles
^ isTiled(tif
)) {
1029 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
, tiles
?
1030 "Can not read tiles from a stripped image" :
1031 "Can not read scanlines from a tiled image");
1038 _TIFFNoPostDecode(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
1040 (void) tif
; (void) buf
; (void) cc
;
1044 _TIFFSwab16BitData(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
1047 assert((cc
& 1) == 0);
1048 TIFFSwabArrayOfShort((uint16
*) buf
, cc
/2);
1052 _TIFFSwab24BitData(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
1055 assert((cc
% 3) == 0);
1056 TIFFSwabArrayOfTriples((uint8
*) buf
, cc
/3);
1060 _TIFFSwab32BitData(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
1063 assert((cc
& 3) == 0);
1064 TIFFSwabArrayOfLong((uint32
*) buf
, cc
/4);
1068 _TIFFSwab64BitData(TIFF
* tif
, uint8
* buf
, tmsize_t cc
)
1071 assert((cc
& 7) == 0);
1072 TIFFSwabArrayOfDouble((double*) buf
, cc
/8);
1075 /* vim: set ts=8 sts=8 sw=8 noet: */