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