Remove obsolete VisualAge-related files.
[wxWidgets.git] / src / tiff / libtiff / tif_read.c
1
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 /*
27 * TIFF Library.
28 * Scanline-oriented Read Support
29 */
30 #include "tiffiop.h"
31 #include <stdio.h>
32
33 int TIFFFillStrip(TIFF* tif, uint32 strip);
34 int TIFFFillTile(TIFF* tif, uint32 tile);
35 static int TIFFStartStrip(TIFF* tif, uint32 strip);
36 static int TIFFStartTile(TIFF* tif, uint32 tile);
37 static int TIFFCheckRead(TIFF*, int);
38 static tmsize_t
39 TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,const char* module);
40
41 #define NOSTRIP ((uint32)(-1)) /* undefined state */
42 #define NOTILE ((uint32)(-1)) /* undefined state */
43
44 static int
45 TIFFFillStripPartial( TIFF *tif, int strip, tmsize_t read_ahead, int restart )
46 {
47 static const char module[] = "TIFFFillStripPartial";
48 register TIFFDirectory *td = &tif->tif_dir;
49 tmsize_t unused_data;
50 uint64 read_offset;
51 tmsize_t cc, to_read;
52 /* tmsize_t bytecountm; */
53
54 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
55 return 0;
56
57 /*
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?).
61 */
62
63 /* bytecountm=(tmsize_t) td->td_stripbytecount[strip]; */
64 if (read_ahead*2 > tif->tif_rawdatasize) {
65 assert( restart );
66
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);
72 return (0);
73 }
74 if (!TIFFReadBufferSetup(tif, 0, read_ahead*2))
75 return (0);
76 }
77
78 if( restart )
79 {
80 tif->tif_rawdataloaded = 0;
81 tif->tif_rawdataoff = 0;
82 }
83
84 /*
85 ** If we are reading more data, move any unused data to the
86 ** start of the buffer.
87 */
88 if( tif->tif_rawdataloaded > 0 )
89 unused_data = tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata);
90 else
91 unused_data = 0;
92
93 if( unused_data > 0 )
94 {
95 assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
96 memmove( tif->tif_rawdata, tif->tif_rawcp, unused_data );
97 }
98
99 /*
100 ** Seek to the point in the file where more data should be read.
101 */
102 read_offset = td->td_stripoffset[strip]
103 + tif->tif_rawdataoff + tif->tif_rawdataloaded;
104
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);
109 return 0;
110 }
111
112 /*
113 ** How much do we want to read?
114 */
115 to_read = tif->tif_rawdatasize - unused_data;
116 if( (uint64) to_read > td->td_stripbytecount[strip]
117 - tif->tif_rawdataoff - tif->tif_rawdataloaded )
118 {
119 to_read = (tmsize_t)td->td_stripbytecount[strip]
120 - tif->tif_rawdataoff - tif->tif_rawdataloaded;
121 }
122
123 assert((tif->tif_flags&TIFF_BUFFERMMAP)==0);
124 cc = TIFFReadFile(tif, tif->tif_rawdata + unused_data, to_read);
125
126 if (cc != 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);
133 #else
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);
139 #endif
140 return 0;
141 }
142
143 tif->tif_rawdataoff = tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data ;
144 tif->tif_rawdataloaded = unused_data + to_read;
145
146 tif->tif_rawcp = tif->tif_rawdata;
147
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 );
152 }
153
154 /*
155 ** When starting a strip from the beginning we need to
156 ** restart the decoder.
157 */
158 if( restart )
159 return TIFFStartStrip(tif, strip);
160 else
161 return 1;
162 }
163
164 /*
165 * Seek to a random row+sample in a file.
166 *
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
170 * strips.
171 */
172 static int
173 TIFFSeek(TIFF* tif, uint32 row, uint16 sample )
174 {
175 register TIFFDirectory *td = &tif->tif_dir;
176 uint32 strip;
177 int whole_strip;
178 tmsize_t read_ahead = 0;
179
180 /*
181 ** Establish what strip we are working from.
182 */
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",
186 (unsigned long) row,
187 (unsigned long) td->td_imagelength);
188 return (0);
189 }
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);
195 return (0);
196 }
197 strip = (uint32)sample*td->td_stripsperimage + row/td->td_rowsperstrip;
198 } else
199 strip = row / td->td_rowsperstrip;
200
201 /*
202 * Do we want to treat this strip as one whole chunk or
203 * read it a few lines at a time?
204 */
205 #if defined(CHUNKY_STRIP_READ_SUPPORT)
206 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
207 return 0;
208 whole_strip = tif->tif_dir.td_stripbytecount[strip] < 10
209 || isMapped(tif);
210 #else
211 whole_strip = 1;
212 #endif
213
214 if( !whole_strip )
215 {
216 read_ahead = tif->tif_scanlinesize * 16 + 5000;
217 }
218
219 /*
220 * If we haven't loaded this strip, do so now, possibly
221 * only reading the first part.
222 */
223 if (strip != tif->tif_curstrip) { /* different strip, refill */
224
225 if( whole_strip )
226 {
227 if (!TIFFFillStrip(tif, strip))
228 return (0);
229 }
230 else
231 {
232 if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
233 return 0;
234 }
235 }
236
237 /*
238 ** If we already have some data loaded, do we need to read some more?
239 */
240 else if( !whole_strip )
241 {
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] )
244 {
245 if( !TIFFFillStripPartial(tif,strip,read_ahead,0) )
246 return 0;
247 }
248 }
249
250 if (row < tif->tif_row) {
251 /*
252 * Moving backwards within the same strip: backup
253 * to the start and then decode forward (below).
254 *
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.
258 */
259
260 if( tif->tif_rawdataoff != 0 )
261 {
262 if( !TIFFFillStripPartial(tif,strip,read_ahead,1) )
263 return 0;
264 }
265 else
266 {
267 if (!TIFFStartStrip(tif, strip))
268 return (0);
269 }
270 }
271
272 if (row != tif->tif_row) {
273 /*
274 * Seek forward to the desired row.
275 */
276
277 /* TODO: Will this really work with partial buffers? */
278
279 if (!(*tif->tif_seek)(tif, row - tif->tif_row))
280 return (0);
281 tif->tif_row = row;
282 }
283
284 return (1);
285 }
286
287 int
288 TIFFReadScanline(TIFF* tif, void* buf, uint32 row, uint16 sample)
289 {
290 int e;
291
292 if (!TIFFCheckRead(tif, 0))
293 return (-1);
294 if( (e = TIFFSeek(tif, row, sample)) != 0) {
295 /*
296 * Decompress desired row into user buffer.
297 */
298 e = (*tif->tif_decoderow)
299 (tif, (uint8*) buf, tif->tif_scanlinesize, sample);
300
301 /* we are now poised at the beginning of the next row */
302 tif->tif_row = row + 1;
303
304 if (e)
305 (*tif->tif_postdecode)(tif, (uint8*) buf,
306 tif->tif_scanlinesize);
307 }
308 return (e > 0 ? 1 : -1);
309 }
310
311 /*
312 * Read a strip of data and decompress the specified
313 * amount into the user-supplied buffer.
314 */
315 tmsize_t
316 TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
317 {
318 static const char module[] = "TIFFReadEncodedStrip";
319 TIFFDirectory *td = &tif->tif_dir;
320 uint32 rowsperstrip;
321 uint32 stripsperplane;
322 uint32 stripinplane;
323 uint16 plane;
324 uint32 rows;
325 tmsize_t stripsize;
326 if (!TIFFCheckRead(tif,0))
327 return((tmsize_t)(-1));
328 if (strip>=td->td_nstrips)
329 {
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));
334 }
335 /*
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).
339 */
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)
348 rows=rowsperstrip;
349 stripsize=TIFFVStripSize(tif,rows);
350 if (stripsize==0)
351 return((tmsize_t)(-1));
352 if ((size!=(tmsize_t)(-1))&&(size<stripsize))
353 stripsize=size;
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);
359 return(stripsize);
360 }
361
362 static tmsize_t
363 TIFFReadRawStrip1(TIFF* tif, uint32 strip, void* buf, tmsize_t size,
364 const char* module)
365 {
366 TIFFDirectory *td = &tif->tif_dir;
367
368 if (!_TIFFFillStriles( tif ))
369 return ((tmsize_t)(-1));
370
371 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
372 if (!isMapped(tif)) {
373 tmsize_t cc;
374
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));
380 }
381 cc = TIFFReadFile(tif, buf, size);
382 if (cc != 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);
389 #else
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);
395 #endif
396 return ((tmsize_t)(-1));
397 }
398 } else {
399 tmsize_t ma,mb;
400 tmsize_t n;
401 ma=(tmsize_t)td->td_stripoffset[strip];
402 mb=ma+size;
403 if (((uint64)ma!=td->td_stripoffset[strip])||(ma>tif->tif_size))
404 n=0;
405 else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
406 n=tif->tif_size-ma;
407 else
408 n=size;
409 if (n!=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);
417 #else
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);
424 #endif
425 return ((tmsize_t)(-1));
426 }
427 _TIFFmemcpy(buf, tif->tif_base + ma,
428 size);
429 }
430 return (size);
431 }
432
433 /*
434 * Read a strip of data from the file.
435 */
436 tmsize_t
437 TIFFReadRawStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
438 {
439 static const char module[] = "TIFFReadRawStrip";
440 TIFFDirectory *td = &tif->tif_dir;
441 uint64 bytecount;
442 tmsize_t bytecountm;
443
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));
452 }
453 if (tif->tif_flags&TIFF_NOREADRAW)
454 {
455 TIFFErrorExt(tif->tif_clientdata, module,
456 "Compression scheme does not support access to raw uncompressed data");
457 return ((tmsize_t)(-1));
458 }
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);
466 #else
467 TIFFErrorExt(tif->tif_clientdata, module,
468 "%llu: Invalid strip byte count, strip %lu",
469 (unsigned long long) bytecount,
470 (unsigned long) strip);
471 #endif
472 return ((tmsize_t)(-1));
473 }
474 bytecountm = (tmsize_t)bytecount;
475 if ((uint64)bytecountm!=bytecount) {
476 TIFFErrorExt(tif->tif_clientdata, module, "Integer overflow");
477 return ((tmsize_t)(-1));
478 }
479 if (size != (tmsize_t)(-1) && size < bytecountm)
480 bytecountm = size;
481 return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module));
482 }
483
484 /*
485 * Read the specified strip and setup for decoding. The data buffer is
486 * expanded, as necessary, to hold the strip's data.
487 */
488 int
489 TIFFFillStrip(TIFF* tif, uint32 strip)
490 {
491 static const char module[] = "TIFFFillStrip";
492 TIFFDirectory *td = &tif->tif_dir;
493
494 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
495 return 0;
496
497 if ((tif->tif_flags&TIFF_NOREADRAW)==0)
498 {
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);
506 #else
507 TIFFErrorExt(tif->tif_clientdata, module,
508 "Invalid strip byte count %llu, strip %lu",
509 (unsigned long long) bytecount,
510 (unsigned long) strip);
511 #endif
512 return (0);
513 }
514 if (isMapped(tif) &&
515 (isFillOrder(tif, td->td_fillorder)
516 || (tif->tif_flags & TIFF_NOBITREV))) {
517 /*
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).
527 */
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;
532 }
533 tif->tif_flags &= ~TIFF_MYBUFFER;
534 /*
535 * We must check for overflow, potentially causing
536 * an OOB read. Instead of simple
537 *
538 * td->td_stripoffset[strip]+bytecount > tif->tif_size
539 *
540 * comparison (which can overflow) we do the following
541 * two comparisons:
542 */
543 if (bytecount > (uint64)tif->tif_size ||
544 td->td_stripoffset[strip] > (uint64)tif->tif_size - bytecount) {
545 /*
546 * This error message might seem strange, but
547 * it's what would happen if a read were done
548 * instead.
549 */
550 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
551 TIFFErrorExt(tif->tif_clientdata, module,
552
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);
558 #else
559 TIFFErrorExt(tif->tif_clientdata, module,
560
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);
566 #endif
567 tif->tif_curstrip = NOSTRIP;
568 return (0);
569 }
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;
574
575 /*
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.
580 */
581 tif->tif_flags |= TIFF_BUFFERMMAP;
582 } else {
583 /*
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?).
587 */
588 tmsize_t bytecountm;
589 bytecountm=(tmsize_t)bytecount;
590 if ((uint64)bytecountm!=bytecount)
591 {
592 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
593 return(0);
594 }
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);
601 return (0);
602 }
603 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
604 return (0);
605 }
606 if (tif->tif_flags&TIFF_BUFFERMMAP) {
607 tif->tif_curstrip = NOSTRIP;
608 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
609 return (0);
610 }
611 if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata,
612 bytecountm, module) != bytecountm)
613 return (0);
614
615 tif->tif_rawdataoff = 0;
616 tif->tif_rawdataloaded = bytecountm;
617
618 if (!isFillOrder(tif, td->td_fillorder) &&
619 (tif->tif_flags & TIFF_NOBITREV) == 0)
620 TIFFReverseBits(tif->tif_rawdata, bytecountm);
621 }
622 }
623 return (TIFFStartStrip(tif, strip));
624 }
625
626 /*
627 * Tile-oriented Read Support
628 * Contributed by Nancy Cam (Silicon Graphics).
629 */
630
631 /*
632 * Read and decompress a tile of data. The
633 * tile is selected by the (x,y,z,s) coordinates.
634 */
635 tmsize_t
636 TIFFReadTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
637 {
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)));
642 }
643
644 /*
645 * Read a tile of data and decompress the specified
646 * amount into the user-supplied buffer.
647 */
648 tmsize_t
649 TIFFReadEncodedTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
650 {
651 static const char module[] = "TIFFReadEncodedTile";
652 TIFFDirectory *td = &tif->tif_dir;
653 tmsize_t tilesize = tif->tif_tilesize;
654
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));
662 }
663 if (size == (tmsize_t)(-1))
664 size = tilesize;
665 else if (size > tilesize)
666 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);
670 return (size);
671 } else
672 return ((tmsize_t)(-1));
673 }
674
675 static tmsize_t
676 TIFFReadRawTile1(TIFF* tif, uint32 tile, void* buf, tmsize_t size, const char* module)
677 {
678 TIFFDirectory *td = &tif->tif_dir;
679
680 if (!_TIFFFillStriles( tif ))
681 return ((tmsize_t)(-1));
682
683 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
684 if (!isMapped(tif)) {
685 tmsize_t cc;
686
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));
694 }
695 cc = TIFFReadFile(tif, buf, size);
696 if (cc != 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);
704 #else
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);
711 #endif
712 return ((tmsize_t)(-1));
713 }
714 } else {
715 tmsize_t ma,mb;
716 tmsize_t n;
717 ma=(tmsize_t)td->td_stripoffset[tile];
718 mb=ma+size;
719 if (((uint64)ma!=td->td_stripoffset[tile])||(ma>tif->tif_size))
720 n=0;
721 else if ((mb<ma)||(mb<size)||(mb>tif->tif_size))
722 n=tif->tif_size-ma;
723 else
724 n=size;
725 if (n!=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);
734 #else
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);
742 #endif
743 return ((tmsize_t)(-1));
744 }
745 _TIFFmemcpy(buf, tif->tif_base + ma, size);
746 }
747 return (size);
748 }
749
750 /*
751 * Read a tile of data from the file.
752 */
753 tmsize_t
754 TIFFReadRawTile(TIFF* tif, uint32 tile, void* buf, tmsize_t size)
755 {
756 static const char module[] = "TIFFReadRawTile";
757 TIFFDirectory *td = &tif->tif_dir;
758 uint64 bytecount64;
759 tmsize_t bytecountm;
760
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));
768 }
769 if (tif->tif_flags&TIFF_NOREADRAW)
770 {
771 TIFFErrorExt(tif->tif_clientdata, module,
772 "Compression scheme does not support access to raw uncompressed data");
773 return ((tmsize_t)(-1));
774 }
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)
780 {
781 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
782 return ((tmsize_t)(-1));
783 }
784 return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module));
785 }
786
787 /*
788 * Read the specified tile and setup for decoding. The data buffer is
789 * expanded, as necessary, to hold the tile's data.
790 */
791 int
792 TIFFFillTile(TIFF* tif, uint32 tile)
793 {
794 static const char module[] = "TIFFFillTile";
795 TIFFDirectory *td = &tif->tif_dir;
796
797 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
798 return 0;
799
800 if ((tif->tif_flags&TIFF_NOREADRAW)==0)
801 {
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);
809 #else
810 TIFFErrorExt(tif->tif_clientdata, module,
811 "%llu: Invalid tile byte count, tile %lu",
812 (unsigned long long) bytecount,
813 (unsigned long) tile);
814 #endif
815 return (0);
816 }
817 if (isMapped(tif) &&
818 (isFillOrder(tif, td->td_fillorder)
819 || (tif->tif_flags & TIFF_NOBITREV))) {
820 /*
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).
830 */
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;
835 }
836 tif->tif_flags &= ~TIFF_MYBUFFER;
837 /*
838 * We must check for overflow, potentially causing
839 * an OOB read. Instead of simple
840 *
841 * td->td_stripoffset[tile]+bytecount > tif->tif_size
842 *
843 * comparison (which can overflow) we do the following
844 * two comparisons:
845 */
846 if (bytecount > (uint64)tif->tif_size ||
847 td->td_stripoffset[tile] > (uint64)tif->tif_size - bytecount) {
848 tif->tif_curtile = NOTILE;
849 return (0);
850 }
851 tif->tif_rawdatasize = (tmsize_t)bytecount;
852 tif->tif_rawdata =
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;
857 } else {
858 /*
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?).
862 */
863 tmsize_t bytecountm;
864 bytecountm=(tmsize_t)bytecount;
865 if ((uint64)bytecountm!=bytecount)
866 {
867 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
868 return(0);
869 }
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);
876 return (0);
877 }
878 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
879 return (0);
880 }
881 if (tif->tif_flags&TIFF_BUFFERMMAP) {
882 tif->tif_curtile = NOTILE;
883 if (!TIFFReadBufferSetup(tif, 0, bytecountm))
884 return (0);
885 }
886
887 if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata,
888 bytecountm, module) != bytecountm)
889 return (0);
890
891 tif->tif_rawdataoff = 0;
892 tif->tif_rawdataloaded = bytecountm;
893
894 if (!isFillOrder(tif, td->td_fillorder) &&
895 (tif->tif_flags & TIFF_NOBITREV) == 0)
896 TIFFReverseBits(tif->tif_rawdata,
897 tif->tif_rawdataloaded);
898 }
899 }
900 return (TIFFStartTile(tif, tile));
901 }
902
903 /*
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
910 * raw data.
911 */
912 int
913 TIFFReadBufferSetup(TIFF* tif, void* bp, tmsize_t size)
914 {
915 static const char module[] = "TIFFReadBufferSetup";
916
917 assert((tif->tif_flags&TIFF_NOREADRAW)==0);
918 tif->tif_flags &= ~TIFF_BUFFERMMAP;
919
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;
925 }
926 if (bp) {
927 tif->tif_rawdatasize = size;
928 tif->tif_rawdata = (uint8*) bp;
929 tif->tif_flags &= ~TIFF_MYBUFFER;
930 } else {
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;
936 }
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;
942 return (0);
943 }
944 return (1);
945 }
946
947 /*
948 * Set state to appear as if a
949 * strip has just been read in.
950 */
951 static int
952 TIFFStartStrip(TIFF* tif, uint32 strip)
953 {
954 TIFFDirectory *td = &tif->tif_dir;
955
956 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
957 return 0;
958
959 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
960 if (!(*tif->tif_setupdecode)(tif))
961 return (0);
962 tif->tif_flags |= TIFF_CODERSETUP;
963 }
964 tif->tif_curstrip = strip;
965 tif->tif_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip;
966 tif->tif_flags &= ~TIFF_BUF4WRITE;
967
968 if (tif->tif_flags&TIFF_NOREADRAW)
969 {
970 tif->tif_rawcp = NULL;
971 tif->tif_rawcc = 0;
972 }
973 else
974 {
975 tif->tif_rawcp = tif->tif_rawdata;
976 tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[strip];
977 }
978 return ((*tif->tif_predecode)(tif,
979 (uint16)(strip / td->td_stripsperimage)));
980 }
981
982 /*
983 * Set state to appear as if a
984 * tile has just been read in.
985 */
986 static int
987 TIFFStartTile(TIFF* tif, uint32 tile)
988 {
989 TIFFDirectory *td = &tif->tif_dir;
990
991 if (!_TIFFFillStriles( tif ) || !tif->tif_dir.td_stripbytecount)
992 return 0;
993
994 if ((tif->tif_flags & TIFF_CODERSETUP) == 0) {
995 if (!(*tif->tif_setupdecode)(tif))
996 return (0);
997 tif->tif_flags |= TIFF_CODERSETUP;
998 }
999 tif->tif_curtile = tile;
1000 tif->tif_row =
1001 (tile % TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth)) *
1002 td->td_tilelength;
1003 tif->tif_col =
1004 (tile % TIFFhowmany_32(td->td_imagelength, td->td_tilelength)) *
1005 td->td_tilewidth;
1006 tif->tif_flags &= ~TIFF_BUF4WRITE;
1007 if (tif->tif_flags&TIFF_NOREADRAW)
1008 {
1009 tif->tif_rawcp = NULL;
1010 tif->tif_rawcc = 0;
1011 }
1012 else
1013 {
1014 tif->tif_rawcp = tif->tif_rawdata;
1015 tif->tif_rawcc = (tmsize_t)td->td_stripbytecount[tile];
1016 }
1017 return ((*tif->tif_predecode)(tif,
1018 (uint16)(tile/td->td_stripsperimage)));
1019 }
1020
1021 static int
1022 TIFFCheckRead(TIFF* tif, int tiles)
1023 {
1024 if (tif->tif_mode == O_WRONLY) {
1025 TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "File not open for reading");
1026 return (0);
1027 }
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");
1032 return (0);
1033 }
1034 return (1);
1035 }
1036
1037 void
1038 _TIFFNoPostDecode(TIFF* tif, uint8* buf, tmsize_t cc)
1039 {
1040 (void) tif; (void) buf; (void) cc;
1041 }
1042
1043 void
1044 _TIFFSwab16BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1045 {
1046 (void) tif;
1047 assert((cc & 1) == 0);
1048 TIFFSwabArrayOfShort((uint16*) buf, cc/2);
1049 }
1050
1051 void
1052 _TIFFSwab24BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1053 {
1054 (void) tif;
1055 assert((cc % 3) == 0);
1056 TIFFSwabArrayOfTriples((uint8*) buf, cc/3);
1057 }
1058
1059 void
1060 _TIFFSwab32BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1061 {
1062 (void) tif;
1063 assert((cc & 3) == 0);
1064 TIFFSwabArrayOfLong((uint32*) buf, cc/4);
1065 }
1066
1067 void
1068 _TIFFSwab64BitData(TIFF* tif, uint8* buf, tmsize_t cc)
1069 {
1070 (void) tif;
1071 assert((cc & 7) == 0);
1072 TIFFSwabArrayOfDouble((double*) buf, cc/8);
1073 }
1074
1075 /* vim: set ts=8 sts=8 sw=8 noet: */
1076 /*
1077 * Local Variables:
1078 * mode: c
1079 * c-basic-offset: 8
1080 * fill-column: 78
1081 * End:
1082 */