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