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