| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#include "tiffiop.h" |
| 30 |
#include <limits.h> |
| 31 |
#include <stdio.h> |
| 32 |
|
| 33 |
int TIFFFillStrip(TIFF *tif, uint32_t strip); |
| 34 |
int TIFFFillTile(TIFF *tif, uint32_t tile); |
| 35 |
static int TIFFStartStrip(TIFF *tif, uint32_t strip); |
| 36 |
static int TIFFStartTile(TIFF *tif, uint32_t tile); |
| 37 |
static int TIFFCheckRead(TIFF *, int); |
| 38 |
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf, |
| 39 |
tmsize_t size, const char *module); |
| 40 |
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf, |
| 41 |
tmsize_t size, const char *module); |
| 42 |
|
| 43 |
#define INITIAL_THRESHOLD (1024 * 1024) |
| 44 |
#define THRESHOLD_MULTIPLIER 10 |
| 45 |
#define MAX_THRESHOLD \ |
| 46 |
(THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * THRESHOLD_MULTIPLIER * \ |
| 47 |
INITIAL_THRESHOLD) |
| 48 |
|
| 49 |
#define TIFF_INT64_MAX ((((int64_t)0x7FFFFFFF) << 32) | 0xFFFFFFFF) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
static int TIFFReadAndRealloc(TIFF *tif, tmsize_t size, tmsize_t rawdata_offset, |
| 54 |
int is_strip, uint32_t strip_or_tile, |
| 55 |
const char *module) |
| 56 |
{ |
| 57 |
#if SIZEOF_SIZE_T == 8 |
| 58 |
tmsize_t threshold = INITIAL_THRESHOLD; |
| 59 |
#endif |
| 60 |
tmsize_t already_read = 0; |
| 61 |
|
| 62 |
#if SIZEOF_SIZE_T != 8 |
| 63 |
|
| 64 |
|
| 65 |
if (size > 1000 * 1000 * 1000) |
| 66 |
{ |
| 67 |
uint64_t filesize = TIFFGetFileSize(tif); |
| 68 |
if ((uint64_t)size >= filesize) |
| 69 |
{ |
| 70 |
TIFFErrorExtR(tif, module, |
| 71 |
"Chunk size requested is larger than file size."); |
| 72 |
return 0; |
| 73 |
} |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
while (already_read < size) |
| 84 |
{ |
| 85 |
tmsize_t bytes_read; |
| 86 |
tmsize_t to_read = size - already_read; |
| 87 |
#if SIZEOF_SIZE_T == 8 |
| 88 |
if (to_read >= threshold && threshold < MAX_THRESHOLD && |
| 89 |
already_read + to_read + rawdata_offset > tif->tif_rawdatasize) |
| 90 |
{ |
| 91 |
to_read = threshold; |
| 92 |
threshold *= THRESHOLD_MULTIPLIER; |
| 93 |
} |
| 94 |
#endif |
| 95 |
if (already_read + to_read + rawdata_offset > tif->tif_rawdatasize) |
| 96 |
{ |
| 97 |
uint8_t *new_rawdata; |
| 98 |
assert((tif->tif_flags & TIFF_MYBUFFER) != 0); |
| 99 |
tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64( |
| 100 |
(uint64_t)already_read + (uint64_t)to_read + |
| 101 |
(uint64_t)rawdata_offset, |
| 102 |
1024); |
| 103 |
if (tif->tif_rawdatasize == 0) |
| 104 |
{ |
| 105 |
TIFFErrorExtR(tif, module, "Invalid buffer size"); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
new_rawdata = (uint8_t *)_TIFFreallocExt(tif, tif->tif_rawdata, |
| 109 |
tif->tif_rawdatasize); |
| 110 |
if (new_rawdata == 0) |
| 111 |
{ |
| 112 |
TIFFErrorExtR(tif, module, |
| 113 |
"No space for data buffer at scanline %" PRIu32, |
| 114 |
tif->tif_dir.td_row); |
| 115 |
_TIFFfreeExt(tif, tif->tif_rawdata); |
| 116 |
tif->tif_rawdata = 0; |
| 117 |
tif->tif_rawdatasize = 0; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
tif->tif_rawdata = new_rawdata; |
| 121 |
} |
| 122 |
if (tif->tif_rawdata == NULL) |
| 123 |
{ |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
bytes_read = TIFFReadFile( |
| 129 |
tif, tif->tif_rawdata + rawdata_offset + already_read, to_read); |
| 130 |
if (bytes_read < 0) |
| 131 |
|
| 132 |
bytes_read = 0; |
| 133 |
already_read += bytes_read; |
| 134 |
if (bytes_read != to_read) |
| 135 |
{ |
| 136 |
memset( |
| 137 |
tif->tif_rawdata + rawdata_offset + already_read, 0, |
| 138 |
(size_t)(tif->tif_rawdatasize - rawdata_offset - already_read)); |
| 139 |
if (is_strip) |
| 140 |
{ |
| 141 |
TIFFErrorExtR(tif, module, |
| 142 |
"Read error at scanline %" PRIu32 |
| 143 |
"; got %" TIFF_SSIZE_FORMAT " bytes, " |
| 144 |
"expected %" TIFF_SSIZE_FORMAT, |
| 145 |
tif->tif_dir.td_row, already_read, size); |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
TIFFErrorExtR(tif, module, |
| 150 |
"Read error at row %" PRIu32 ", col %" PRIu32 |
| 151 |
", tile %" PRIu32 "; " |
| 152 |
"got %" TIFF_SSIZE_FORMAT |
| 153 |
" bytes, expected %" TIFF_SSIZE_FORMAT "", |
| 154 |
tif->tif_dir.td_row, tif->tif_dir.td_col, |
| 155 |
strip_or_tile, already_read, size); |
| 156 |
} |
| 157 |
return 0; |
| 158 |
} |
| 159 |
} |
| 160 |
return 1; |
| 161 |
} |
| 162 |
|
| 163 |
static int TIFFFillStripPartial(TIFF *tif, int strip, tmsize_t read_ahead, |
| 164 |
int restart) |
| 165 |
{ |
| 166 |
static const char module[] = "TIFFFillStripPartial"; |
| 167 |
TIFFDirectory *td = &tif->tif_dir; |
| 168 |
tmsize_t unused_data; |
| 169 |
uint64_t read_offset; |
| 170 |
tmsize_t to_read; |
| 171 |
tmsize_t read_ahead_mod; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if (read_ahead < TIFF_TMSIZE_T_MAX / 2) |
| 185 |
read_ahead_mod = read_ahead * 2; |
| 186 |
else |
| 187 |
read_ahead_mod = read_ahead; |
| 188 |
if (read_ahead_mod > tif->tif_rawdatasize) |
| 189 |
{ |
| 190 |
assert(restart); |
| 191 |
|
| 192 |
tif->tif_dir.td_curstrip = NOSTRIP; |
| 193 |
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) |
| 194 |
{ |
| 195 |
TIFFErrorExtR(tif, module, |
| 196 |
"Data buffer too small to hold part of strip %d", |
| 197 |
strip); |
| 198 |
return (0); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (restart) |
| 203 |
{ |
| 204 |
tif->tif_rawdataloaded = 0; |
| 205 |
tif->tif_rawdataoff = 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if (tif->tif_rawdataloaded > 0) |
| 213 |
unused_data = |
| 214 |
tif->tif_rawdataloaded - (tif->tif_rawcp - tif->tif_rawdata); |
| 215 |
else |
| 216 |
unused_data = 0; |
| 217 |
|
| 218 |
if (unused_data > 0) |
| 219 |
{ |
| 220 |
assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0); |
| 221 |
memmove(tif->tif_rawdata, tif->tif_rawcp, (size_t)unused_data); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
read_offset = TIFFGetStrileOffset(tif, (uint32_t)strip); |
| 228 |
if (read_offset > UINT64_MAX - (uint64_t)tif->tif_rawdataoff || |
| 229 |
read_offset + (uint64_t)tif->tif_rawdataoff > |
| 230 |
UINT64_MAX - (uint64_t)tif->tif_rawdataloaded) |
| 231 |
{ |
| 232 |
TIFFErrorExtR(tif, module, |
| 233 |
"Seek error at scanline %" PRIu32 ", strip %d", |
| 234 |
tif->tif_dir.td_row, strip); |
| 235 |
return 0; |
| 236 |
} |
| 237 |
read_offset += |
| 238 |
(uint64_t)tif->tif_rawdataoff + (uint64_t)tif->tif_rawdataloaded; |
| 239 |
|
| 240 |
if (!SeekOK(tif, read_offset)) |
| 241 |
{ |
| 242 |
TIFFErrorExtR(tif, module, |
| 243 |
"Seek error at scanline %" PRIu32 ", strip %d", |
| 244 |
tif->tif_dir.td_row, strip); |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (read_ahead_mod > tif->tif_rawdatasize) |
| 252 |
to_read = read_ahead_mod - unused_data; |
| 253 |
else |
| 254 |
to_read = tif->tif_rawdatasize - unused_data; |
| 255 |
if ((uint64_t)to_read > TIFFGetStrileByteCount(tif, (uint32_t)strip) - |
| 256 |
(uint64_t)tif->tif_rawdataoff - |
| 257 |
(uint64_t)tif->tif_rawdataloaded) |
| 258 |
{ |
| 259 |
to_read = (tmsize_t)(TIFFGetStrileByteCount(tif, (uint32_t)strip) - |
| 260 |
(uint64_t)tif->tif_rawdataoff - |
| 261 |
(uint64_t)tif->tif_rawdataloaded); |
| 262 |
} |
| 263 |
|
| 264 |
assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0); |
| 265 |
if (!TIFFReadAndRealloc(tif, to_read, unused_data, 1, |
| 266 |
0, |
| 267 |
module)) |
| 268 |
{ |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
tif->tif_rawdataoff = |
| 273 |
tif->tif_rawdataoff + tif->tif_rawdataloaded - unused_data; |
| 274 |
tif->tif_rawdataloaded = unused_data + to_read; |
| 275 |
|
| 276 |
tif->tif_rawcc = tif->tif_rawdataloaded; |
| 277 |
tif->tif_rawcp = tif->tif_rawdata; |
| 278 |
|
| 279 |
if (!isFillOrder(tif, td->td_fillorder) && |
| 280 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 281 |
{ |
| 282 |
assert((tif->tif_flags & TIFF_BUFFERMMAP) == 0); |
| 283 |
TIFFReverseBits(tif->tif_rawdata + unused_data, to_read); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if (restart) |
| 291 |
{ |
| 292 |
|
| 293 |
#ifdef JPEG_SUPPORT |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
if (tif->tif_dir.td_compression == COMPRESSION_JPEG && |
| 300 |
(uint64_t)tif->tif_rawcc < |
| 301 |
TIFFGetStrileByteCount(tif, (uint32_t)strip)) |
| 302 |
{ |
| 303 |
if (TIFFJPEGIsFullStripRequired(tif)) |
| 304 |
{ |
| 305 |
return TIFFFillStrip(tif, (uint32_t)strip); |
| 306 |
} |
| 307 |
} |
| 308 |
#endif |
| 309 |
|
| 310 |
return TIFFStartStrip(tif, (uint32_t)strip); |
| 311 |
} |
| 312 |
else |
| 313 |
{ |
| 314 |
return 1; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
static int TIFFSeek(TIFF *tif, uint32_t row, uint16_t sample) |
| 327 |
{ |
| 328 |
TIFFDirectory *td = &tif->tif_dir; |
| 329 |
uint32_t strip; |
| 330 |
int whole_strip; |
| 331 |
tmsize_t read_ahead = 0; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
if (row >= td->td_imagelength) |
| 337 |
{ |
| 338 |
TIFFErrorExtR(tif, tif->tif_name, |
| 339 |
"%" PRIu32 ": Row out of range, max %" PRIu32 "", row, |
| 340 |
td->td_imagelength); |
| 341 |
return (0); |
| 342 |
} |
| 343 |
if (td->td_rowsperstrip == 0) |
| 344 |
{ |
| 345 |
TIFFErrorExtR(tif, tif->tif_name, |
| 346 |
"Cannot compute strip: RowsPerStrip is zero"); |
| 347 |
return (0); |
| 348 |
} |
| 349 |
if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
| 350 |
{ |
| 351 |
uint64_t sample_offset; |
| 352 |
uint64_t strip64; |
| 353 |
if (sample >= td->td_samplesperpixel) |
| 354 |
{ |
| 355 |
TIFFErrorExtR(tif, tif->tif_name, |
| 356 |
"%" PRIu16 ": Sample out of range, max %" PRIu16 "", |
| 357 |
sample, td->td_samplesperpixel); |
| 358 |
return (0); |
| 359 |
} |
| 360 |
sample_offset = |
| 361 |
_TIFFMultiply64(tif, sample, td->td_stripsperimage, "TIFFSeek"); |
| 362 |
if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0) |
| 363 |
return (0); |
| 364 |
strip64 = _TIFFAdd64(tif, sample_offset, row / td->td_rowsperstrip, |
| 365 |
"TIFFSeek"); |
| 366 |
if (strip64 == 0 && |
| 367 |
(sample_offset != 0 || (row / td->td_rowsperstrip) != 0)) |
| 368 |
return (0); |
| 369 |
strip = _TIFFCastUInt64ToUInt32(tif, strip64, "TIFFSeek"); |
| 370 |
if (strip == 0 && strip64 != 0) |
| 371 |
return (0); |
| 372 |
} |
| 373 |
else |
| 374 |
strip = row / td->td_rowsperstrip; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
#if defined(CHUNKY_STRIP_READ_SUPPORT) |
| 381 |
whole_strip = TIFFGetStrileByteCount(tif, strip) < 10 || isMapped(tif); |
| 382 |
if (td->td_compression == COMPRESSION_LERC || |
| 383 |
td->td_compression == COMPRESSION_JBIG) |
| 384 |
{ |
| 385 |
|
| 386 |
|
| 387 |
whole_strip = 1; |
| 388 |
} |
| 389 |
|
| 390 |
if (!whole_strip) |
| 391 |
{ |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
if (tif->tif_dir.td_scanlinesize < TIFF_TMSIZE_T_MAX / 16 && |
| 398 |
tif->tif_dir.td_scanlinesize * 16 < TIFF_TMSIZE_T_MAX - 5000) |
| 399 |
{ |
| 400 |
read_ahead = tif->tif_dir.td_scanlinesize * 16 + 5000; |
| 401 |
} |
| 402 |
else |
| 403 |
{ |
| 404 |
read_ahead = tif->tif_dir.td_scanlinesize; |
| 405 |
} |
| 406 |
} |
| 407 |
#else |
| 408 |
whole_strip = 1; |
| 409 |
#endif |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if (strip != tif->tif_dir.td_curstrip) |
| 416 |
{ |
| 417 |
|
| 418 |
if (whole_strip) |
| 419 |
{ |
| 420 |
if (!TIFFFillStrip(tif, strip)) |
| 421 |
return (0); |
| 422 |
} |
| 423 |
#if defined(CHUNKY_STRIP_READ_SUPPORT) |
| 424 |
else |
| 425 |
{ |
| 426 |
if (!TIFFFillStripPartial(tif, strip, read_ahead, 1)) |
| 427 |
return 0; |
| 428 |
} |
| 429 |
#endif |
| 430 |
} |
| 431 |
|
| 432 |
#if defined(CHUNKY_STRIP_READ_SUPPORT) |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
else if (!whole_strip) |
| 437 |
{ |
| 438 |
|
| 439 |
if (((tif->tif_rawdata + tif->tif_rawdataloaded) - tif->tif_rawcp) < |
| 440 |
read_ahead && |
| 441 |
(uint64_t)tif->tif_rawdataoff + tif->tif_rawdataloaded < |
| 442 |
TIFFGetStrileByteCount(tif, strip)) |
| 443 |
{ |
| 444 |
if (!TIFFFillStripPartial(tif, strip, read_ahead, 0)) |
| 445 |
return 0; |
| 446 |
} |
| 447 |
} |
| 448 |
#endif |
| 449 |
|
| 450 |
if (row < tif->tif_dir.td_row) |
| 451 |
{ |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (tif->tif_rawdataoff != 0) |
| 462 |
{ |
| 463 |
if (!TIFFFillStripPartial(tif, (int)strip, read_ahead, 1)) |
| 464 |
return 0; |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
if (!TIFFStartStrip(tif, strip)) |
| 469 |
return (0); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
if (row != tif->tif_dir.td_row) |
| 474 |
{ |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
if (!(*tif->tif_seek)(tif, row - tif->tif_dir.td_row)) |
| 482 |
return (0); |
| 483 |
tif->tif_dir.td_row = row; |
| 484 |
} |
| 485 |
|
| 486 |
return (1); |
| 487 |
} |
| 488 |
|
| 489 |
int TIFFReadScanline(TIFF *tif, void *buf, uint32_t row, uint16_t sample) |
| 490 |
{ |
| 491 |
int e; |
| 492 |
|
| 493 |
if (!TIFFCheckRead(tif, 0)) |
| 494 |
return (-1); |
| 495 |
if ((e = TIFFSeek(tif, row, sample)) != 0) |
| 496 |
{ |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
e = (*tif->tif_decoderow)(tif, (uint8_t *)buf, |
| 501 |
tif->tif_dir.td_scanlinesize, sample); |
| 502 |
|
| 503 |
|
| 504 |
tif->tif_dir.td_row = row + 1; |
| 505 |
|
| 506 |
if (e) |
| 507 |
(*tif->tif_postdecode)(tif, (uint8_t *)buf, |
| 508 |
tif->tif_dir.td_scanlinesize); |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
|
| 513 |
if (buf) |
| 514 |
memset(buf, 0, (size_t)tif->tif_dir.td_scanlinesize); |
| 515 |
} |
| 516 |
return (e > 0 ? 1 : -1); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
static tmsize_t TIFFReadEncodedStripGetStripSize(TIFF *tif, uint32_t strip, |
| 525 |
uint16_t *pplane) |
| 526 |
{ |
| 527 |
static const char module[] = "TIFFReadEncodedStrip"; |
| 528 |
TIFFDirectory *td = &tif->tif_dir; |
| 529 |
uint32_t rowsperstrip; |
| 530 |
uint32_t stripsperplane; |
| 531 |
uint32_t stripinplane; |
| 532 |
uint32_t rows; |
| 533 |
tmsize_t stripsize; |
| 534 |
if (!TIFFCheckRead(tif, 0)) |
| 535 |
return ((tmsize_t)(-1)); |
| 536 |
if (strip >= td->td_nstrips) |
| 537 |
{ |
| 538 |
TIFFErrorExtR(tif, module, |
| 539 |
"%" PRIu32 ": Strip out of range, max %" PRIu32, strip, |
| 540 |
td->td_nstrips); |
| 541 |
return ((tmsize_t)(-1)); |
| 542 |
} |
| 543 |
|
| 544 |
rowsperstrip = td->td_rowsperstrip; |
| 545 |
if (rowsperstrip > td->td_imagelength) |
| 546 |
rowsperstrip = td->td_imagelength; |
| 547 |
if (rowsperstrip == 0) |
| 548 |
{ |
| 549 |
TIFFErrorExtR(tif, module, "rowsperstrip is zero"); |
| 550 |
return ((tmsize_t)(-1)); |
| 551 |
} |
| 552 |
stripsperplane = |
| 553 |
TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip); |
| 554 |
stripinplane = (strip % stripsperplane); |
| 555 |
if (pplane) |
| 556 |
*pplane = (uint16_t)(strip / stripsperplane); |
| 557 |
rows = td->td_imagelength - stripinplane * rowsperstrip; |
| 558 |
if (rows > rowsperstrip) |
| 559 |
rows = rowsperstrip; |
| 560 |
stripsize = TIFFVStripSize(tif, rows); |
| 561 |
if (stripsize == 0) |
| 562 |
return ((tmsize_t)(-1)); |
| 563 |
return stripsize; |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
tmsize_t TIFFReadEncodedStrip(TIFF *tif, uint32_t strip, void *buf, |
| 571 |
tmsize_t size) |
| 572 |
{ |
| 573 |
static const char module[] = "TIFFReadEncodedStrip"; |
| 574 |
TIFFDirectory *td = &tif->tif_dir; |
| 575 |
tmsize_t stripsize; |
| 576 |
uint16_t plane; |
| 577 |
|
| 578 |
stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane); |
| 579 |
if (stripsize == ((tmsize_t)(-1))) |
| 580 |
return ((tmsize_t)(-1)); |
| 581 |
|
| 582 |
|
| 583 |
if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) && |
| 584 |
size >= stripsize && !isMapped(tif) && |
| 585 |
((tif->tif_flags & TIFF_NOREADRAW) == 0)) |
| 586 |
{ |
| 587 |
if (TIFFReadRawStrip1(tif, strip, buf, stripsize, module) != stripsize) |
| 588 |
return ((tmsize_t)(-1)); |
| 589 |
|
| 590 |
if (!isFillOrder(tif, td->td_fillorder) && |
| 591 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 592 |
TIFFReverseBits((uint8_t *)buf, stripsize); |
| 593 |
|
| 594 |
(*tif->tif_postdecode)(tif, (uint8_t *)buf, stripsize); |
| 595 |
return (stripsize); |
| 596 |
} |
| 597 |
|
| 598 |
if ((size != (tmsize_t)(-1)) && (size < stripsize)) |
| 599 |
stripsize = size; |
| 600 |
if (!TIFFFillStrip(tif, strip)) |
| 601 |
{ |
| 602 |
|
| 603 |
|
| 604 |
if (buf) |
| 605 |
memset(buf, 0, (size_t)stripsize); |
| 606 |
return ((tmsize_t)(-1)); |
| 607 |
} |
| 608 |
if ((*tif->tif_decodestrip)(tif, (uint8_t *)buf, stripsize, plane) <= 0) |
| 609 |
return ((tmsize_t)(-1)); |
| 610 |
(*tif->tif_postdecode)(tif, (uint8_t *)buf, stripsize); |
| 611 |
return (stripsize); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
tmsize_t _TIFFReadEncodedStripAndAllocBuffer(TIFF *tif, uint32_t strip, |
| 621 |
void **buf, |
| 622 |
tmsize_t bufsizetoalloc, |
| 623 |
tmsize_t size_to_read) |
| 624 |
{ |
| 625 |
tmsize_t this_stripsize; |
| 626 |
uint16_t plane; |
| 627 |
|
| 628 |
if (*buf != NULL) |
| 629 |
{ |
| 630 |
return TIFFReadEncodedStrip(tif, strip, *buf, size_to_read); |
| 631 |
} |
| 632 |
|
| 633 |
this_stripsize = TIFFReadEncodedStripGetStripSize(tif, strip, &plane); |
| 634 |
if (this_stripsize == ((tmsize_t)(-1))) |
| 635 |
return ((tmsize_t)(-1)); |
| 636 |
|
| 637 |
if ((size_to_read != (tmsize_t)(-1)) && (size_to_read < this_stripsize)) |
| 638 |
this_stripsize = size_to_read; |
| 639 |
if (!TIFFFillStrip(tif, strip)) |
| 640 |
return ((tmsize_t)(-1)); |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
if (bufsizetoalloc > 100 * 1024 * 1024) |
| 648 |
{ |
| 649 |
const uint64_t maxCompressionRatio = TIFFGetMaxCompressionRatio(tif); |
| 650 |
if (maxCompressionRatio > 0 && |
| 651 |
(uint64_t)tif->tif_rawdatasize < |
| 652 |
(uint64_t)this_stripsize / maxCompressionRatio) |
| 653 |
{ |
| 654 |
TIFFErrorExtR(tif, TIFFFileName(tif), |
| 655 |
"Likely invalid strip byte count for strip %u. " |
| 656 |
"Uncompressed strip size is %" PRIu64 ", " |
| 657 |
"compressed one is %" PRIu64, |
| 658 |
strip, (uint64_t)this_stripsize, |
| 659 |
(uint64_t)tif->tif_rawdatasize); |
| 660 |
return ((tmsize_t)(-1)); |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
*buf = _TIFFcallocExt(tif, 1, bufsizetoalloc); |
| 665 |
if (*buf == NULL) |
| 666 |
{ |
| 667 |
TIFFErrorExtR(tif, TIFFFileName(tif), "No space for strip buffer"); |
| 668 |
return ((tmsize_t)(-1)); |
| 669 |
} |
| 670 |
|
| 671 |
if ((*tif->tif_decodestrip)(tif, (uint8_t *)*buf, this_stripsize, plane) <= |
| 672 |
0) |
| 673 |
return ((tmsize_t)(-1)); |
| 674 |
(*tif->tif_postdecode)(tif, (uint8_t *)*buf, this_stripsize); |
| 675 |
return (this_stripsize); |
| 676 |
} |
| 677 |
|
| 678 |
static tmsize_t TIFFReadRawStrip1(TIFF *tif, uint32_t strip, void *buf, |
| 679 |
tmsize_t size, const char *module) |
| 680 |
{ |
| 681 |
assert((tif->tif_flags & TIFF_NOREADRAW) == 0); |
| 682 |
if (!isMapped(tif)) |
| 683 |
{ |
| 684 |
tmsize_t cc; |
| 685 |
|
| 686 |
if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip))) |
| 687 |
{ |
| 688 |
TIFFErrorExtR(tif, module, |
| 689 |
"Seek error at scanline %" PRIu32 ", strip %" PRIu32, |
| 690 |
tif->tif_dir.td_row, strip); |
| 691 |
return ((tmsize_t)(-1)); |
| 692 |
} |
| 693 |
cc = TIFFReadFile(tif, buf, size); |
| 694 |
if (cc != size) |
| 695 |
{ |
| 696 |
TIFFErrorExtR(tif, module, |
| 697 |
"Read error at scanline %" PRIu32 |
| 698 |
"; got %" TIFF_SSIZE_FORMAT |
| 699 |
" bytes, expected %" TIFF_SSIZE_FORMAT, |
| 700 |
tif->tif_dir.td_row, cc, size); |
| 701 |
return ((tmsize_t)(-1)); |
| 702 |
} |
| 703 |
} |
| 704 |
else |
| 705 |
{ |
| 706 |
tmsize_t ma = 0; |
| 707 |
tmsize_t n; |
| 708 |
if ((TIFFGetStrileOffset(tif, strip) > (uint64_t)TIFF_TMSIZE_T_MAX) || |
| 709 |
((ma = (tmsize_t)TIFFGetStrileOffset(tif, strip)) > tif->tif_size)) |
| 710 |
{ |
| 711 |
n = 0; |
| 712 |
} |
| 713 |
else if (ma > TIFF_TMSIZE_T_MAX - size) |
| 714 |
{ |
| 715 |
n = 0; |
| 716 |
} |
| 717 |
else |
| 718 |
{ |
| 719 |
tmsize_t mb = ma + size; |
| 720 |
if (mb > tif->tif_size) |
| 721 |
n = tif->tif_size - ma; |
| 722 |
else |
| 723 |
n = size; |
| 724 |
} |
| 725 |
if (n != size) |
| 726 |
{ |
| 727 |
TIFFErrorExtR(tif, module, |
| 728 |
"Read error at scanline %" PRIu32 ", strip %" PRIu32 |
| 729 |
"; got %" TIFF_SSIZE_FORMAT |
| 730 |
" bytes, expected %" TIFF_SSIZE_FORMAT, |
| 731 |
tif->tif_dir.td_row, strip, n, size); |
| 732 |
return ((tmsize_t)(-1)); |
| 733 |
} |
| 734 |
_TIFFmemcpy(buf, tif->tif_base + ma, size); |
| 735 |
} |
| 736 |
return (size); |
| 737 |
} |
| 738 |
|
| 739 |
static tmsize_t TIFFReadRawStripOrTile2(TIFF *tif, uint32_t strip_or_tile, |
| 740 |
int is_strip, tmsize_t size, |
| 741 |
const char *module) |
| 742 |
{ |
| 743 |
assert(!isMapped(tif)); |
| 744 |
assert((tif->tif_flags & TIFF_NOREADRAW) == 0); |
| 745 |
|
| 746 |
if (!SeekOK(tif, TIFFGetStrileOffset(tif, strip_or_tile))) |
| 747 |
{ |
| 748 |
if (is_strip) |
| 749 |
{ |
| 750 |
TIFFErrorExtR(tif, module, |
| 751 |
"Seek error at scanline %" PRIu32 ", strip %" PRIu32, |
| 752 |
tif->tif_dir.td_row, strip_or_tile); |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
TIFFErrorExtR( |
| 757 |
tif, module, |
| 758 |
"Seek error at row %" PRIu32 ", col %" PRIu32 ", tile %" PRIu32, |
| 759 |
tif->tif_dir.td_row, tif->tif_dir.td_col, strip_or_tile); |
| 760 |
} |
| 761 |
return ((tmsize_t)(-1)); |
| 762 |
} |
| 763 |
|
| 764 |
if (!TIFFReadAndRealloc(tif, size, 0, is_strip, strip_or_tile, module)) |
| 765 |
{ |
| 766 |
return ((tmsize_t)(-1)); |
| 767 |
} |
| 768 |
|
| 769 |
return (size); |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
tmsize_t TIFFReadRawStrip(TIFF *tif, uint32_t strip, void *buf, tmsize_t size) |
| 776 |
{ |
| 777 |
static const char module[] = "TIFFReadRawStrip"; |
| 778 |
TIFFDirectory *td = &tif->tif_dir; |
| 779 |
uint64_t bytecount64; |
| 780 |
tmsize_t bytecountm; |
| 781 |
|
| 782 |
if (!TIFFCheckRead(tif, 0)) |
| 783 |
return ((tmsize_t)(-1)); |
| 784 |
if (strip >= td->td_nstrips) |
| 785 |
{ |
| 786 |
TIFFErrorExtR(tif, module, |
| 787 |
"%" PRIu32 ": Strip out of range, max %" PRIu32, strip, |
| 788 |
td->td_nstrips); |
| 789 |
return ((tmsize_t)(-1)); |
| 790 |
} |
| 791 |
if (tif->tif_flags & TIFF_NOREADRAW) |
| 792 |
{ |
| 793 |
TIFFErrorExtR(tif, module, |
| 794 |
"Compression scheme does not support access to raw " |
| 795 |
"uncompressed data"); |
| 796 |
return ((tmsize_t)(-1)); |
| 797 |
} |
| 798 |
bytecount64 = TIFFGetStrileByteCount(tif, strip); |
| 799 |
if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64) |
| 800 |
bytecountm = size; |
| 801 |
else |
| 802 |
bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module); |
| 803 |
if (bytecountm == 0) |
| 804 |
{ |
| 805 |
return ((tmsize_t)(-1)); |
| 806 |
} |
| 807 |
return (TIFFReadRawStrip1(tif, strip, buf, bytecountm, module)); |
| 808 |
} |
| 809 |
|
| 810 |
TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW |
| 811 |
static uint64_t NoSanitizeSubUInt64(uint64_t a, uint64_t b) { return a - b; } |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
int TIFFFillStrip(TIFF *tif, uint32_t strip) |
| 818 |
{ |
| 819 |
static const char module[] = "TIFFFillStrip"; |
| 820 |
TIFFDirectory *td = &tif->tif_dir; |
| 821 |
|
| 822 |
if ((tif->tif_flags & TIFF_NOREADRAW) == 0) |
| 823 |
{ |
| 824 |
uint64_t bytecount = TIFFGetStrileByteCount(tif, strip); |
| 825 |
if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX) |
| 826 |
{ |
| 827 |
TIFFErrorExtR(tif, module, |
| 828 |
"Invalid strip byte count %" PRIu64 |
| 829 |
", strip %" PRIu32, |
| 830 |
bytecount, strip); |
| 831 |
return (0); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
const tmsize_t stripsize = TIFFStripSize(tif); |
| 836 |
if (stripsize > 0) |
| 837 |
{ |
| 838 |
if (bytecount > 1024 * 1024 && |
| 839 |
(bytecount - 4096) / 10 > (uint64_t)stripsize) |
| 840 |
{ |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
uint64_t newbytecount = (uint64_t)stripsize * 10 + 4096; |
| 852 |
TIFFWarningExtR(tif, module, |
| 853 |
"Too large strip byte count %" PRIu64 |
| 854 |
", strip %" PRIu32 ". Limiting to %" PRIu64, |
| 855 |
bytecount, strip, newbytecount); |
| 856 |
bytecount = newbytecount; |
| 857 |
} |
| 858 |
else if (stripsize > 100 * 1024 * 1024) |
| 859 |
{ |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
const uint64_t maxCompressionRatio = |
| 866 |
TIFFGetMaxCompressionRatio(tif); |
| 867 |
if (maxCompressionRatio > 0 && |
| 868 |
bytecount < (uint64_t)stripsize / maxCompressionRatio) |
| 869 |
{ |
| 870 |
TIFFErrorExtR( |
| 871 |
tif, module, |
| 872 |
"Likely invalid strip byte count for strip %u. " |
| 873 |
"Uncompressed strip size is %" PRIu64 ", " |
| 874 |
"compressed one is %" PRIu64, |
| 875 |
strip, (uint64_t)stripsize, bytecount); |
| 876 |
return 0; |
| 877 |
} |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
if (isMapped(tif)) |
| 882 |
{ |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
if (bytecount > (uint64_t)tif->tif_size || |
| 893 |
TIFFGetStrileOffset(tif, strip) > |
| 894 |
(uint64_t)tif->tif_size - bytecount) |
| 895 |
{ |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
TIFFErrorExtR( |
| 902 |
tif, module, |
| 903 |
|
| 904 |
"Read error on strip %" PRIu32 "; " |
| 905 |
"got %" PRIu64 " bytes, expected %" PRIu64, |
| 906 |
strip, |
| 907 |
NoSanitizeSubUInt64((uint64_t)tif->tif_size, |
| 908 |
TIFFGetStrileOffset(tif, strip)), |
| 909 |
bytecount); |
| 910 |
tif->tif_dir.td_curstrip = NOSTRIP; |
| 911 |
return (0); |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) || |
| 916 |
(tif->tif_flags & TIFF_NOBITREV))) |
| 917 |
{ |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) |
| 930 |
{ |
| 931 |
_TIFFfreeExt(tif, tif->tif_rawdata); |
| 932 |
tif->tif_rawdata = NULL; |
| 933 |
tif->tif_rawdatasize = 0; |
| 934 |
} |
| 935 |
tif->tif_flags &= ~TIFF_MYBUFFER; |
| 936 |
tif->tif_rawdatasize = (tmsize_t)bytecount; |
| 937 |
tif->tif_rawdata = |
| 938 |
tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, strip); |
| 939 |
tif->tif_rawdataoff = 0; |
| 940 |
tif->tif_rawdataloaded = (tmsize_t)bytecount; |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
tif->tif_flags |= TIFF_BUFFERMMAP; |
| 950 |
} |
| 951 |
else |
| 952 |
{ |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
tmsize_t bytecountm; |
| 959 |
bytecountm = (tmsize_t)bytecount; |
| 960 |
if ((uint64_t)bytecountm != bytecount) |
| 961 |
{ |
| 962 |
TIFFErrorExtR(tif, module, "Integer overflow"); |
| 963 |
return (0); |
| 964 |
} |
| 965 |
if (bytecountm > tif->tif_rawdatasize) |
| 966 |
{ |
| 967 |
tif->tif_dir.td_curstrip = NOSTRIP; |
| 968 |
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) |
| 969 |
{ |
| 970 |
TIFFErrorExtR( |
| 971 |
tif, module, |
| 972 |
"Data buffer too small to hold strip %" PRIu32, strip); |
| 973 |
return (0); |
| 974 |
} |
| 975 |
} |
| 976 |
if (tif->tif_flags & TIFF_BUFFERMMAP) |
| 977 |
{ |
| 978 |
tif->tif_dir.td_curstrip = NOSTRIP; |
| 979 |
tif->tif_rawdata = NULL; |
| 980 |
tif->tif_rawdatasize = 0; |
| 981 |
tif->tif_flags &= ~TIFF_BUFFERMMAP; |
| 982 |
} |
| 983 |
|
| 984 |
if (isMapped(tif)) |
| 985 |
{ |
| 986 |
if (bytecountm > tif->tif_rawdatasize && |
| 987 |
!TIFFReadBufferSetup(tif, 0, bytecountm)) |
| 988 |
{ |
| 989 |
return (0); |
| 990 |
} |
| 991 |
if (TIFFReadRawStrip1(tif, strip, tif->tif_rawdata, bytecountm, |
| 992 |
module) != bytecountm) |
| 993 |
{ |
| 994 |
return (0); |
| 995 |
} |
| 996 |
} |
| 997 |
else |
| 998 |
{ |
| 999 |
if (TIFFReadRawStripOrTile2(tif, strip, 1, bytecountm, |
| 1000 |
module) != bytecountm) |
| 1001 |
{ |
| 1002 |
return (0); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
tif->tif_rawdataoff = 0; |
| 1007 |
tif->tif_rawdataloaded = bytecountm; |
| 1008 |
|
| 1009 |
if (!isFillOrder(tif, td->td_fillorder) && |
| 1010 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 1011 |
TIFFReverseBits(tif->tif_rawdata, bytecountm); |
| 1012 |
} |
| 1013 |
} |
| 1014 |
return (TIFFStartStrip(tif, strip)); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
tmsize_t TIFFReadTile(TIFF *tif, void *buf, uint32_t x, uint32_t y, uint32_t z, |
| 1027 |
uint16_t s) |
| 1028 |
{ |
| 1029 |
if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s)) |
| 1030 |
return ((tmsize_t)(-1)); |
| 1031 |
return (TIFFReadEncodedTile(tif, TIFFComputeTile(tif, x, y, z, s), buf, |
| 1032 |
(tmsize_t)(-1))); |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
tmsize_t TIFFReadEncodedTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size) |
| 1040 |
{ |
| 1041 |
static const char module[] = "TIFFReadEncodedTile"; |
| 1042 |
TIFFDirectory *td = &tif->tif_dir; |
| 1043 |
tmsize_t tilesize = tif->tif_dir.td_tilesize; |
| 1044 |
|
| 1045 |
if (!TIFFCheckRead(tif, 1)) |
| 1046 |
return ((tmsize_t)(-1)); |
| 1047 |
if (tile >= td->td_nstrips) |
| 1048 |
{ |
| 1049 |
TIFFErrorExtR(tif, module, |
| 1050 |
"%" PRIu32 ": Tile out of range, max %" PRIu32, tile, |
| 1051 |
td->td_nstrips); |
| 1052 |
return ((tmsize_t)(-1)); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
if (td->td_compression == COMPRESSION_NONE && size != (tmsize_t)(-1) && |
| 1057 |
size >= tilesize && !isMapped(tif) && |
| 1058 |
((tif->tif_flags & TIFF_NOREADRAW) == 0)) |
| 1059 |
{ |
| 1060 |
if (TIFFReadRawTile1(tif, tile, buf, tilesize, module) != tilesize) |
| 1061 |
return ((tmsize_t)(-1)); |
| 1062 |
|
| 1063 |
if (!isFillOrder(tif, td->td_fillorder) && |
| 1064 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 1065 |
TIFFReverseBits((uint8_t *)buf, tilesize); |
| 1066 |
|
| 1067 |
(*tif->tif_postdecode)(tif, (uint8_t *)buf, tilesize); |
| 1068 |
return (tilesize); |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (size == (tmsize_t)(-1)) |
| 1072 |
size = tilesize; |
| 1073 |
else if (size > tilesize) |
| 1074 |
size = tilesize; |
| 1075 |
if (!TIFFFillTile(tif, tile)) |
| 1076 |
{ |
| 1077 |
|
| 1078 |
if (buf) |
| 1079 |
memset(buf, 0, (size_t)size); |
| 1080 |
return ((tmsize_t)(-1)); |
| 1081 |
} |
| 1082 |
else if ((*tif->tif_decodetile)(tif, (uint8_t *)buf, size, |
| 1083 |
(uint16_t)(tile / td->td_stripsperimage))) |
| 1084 |
{ |
| 1085 |
(*tif->tif_postdecode)(tif, (uint8_t *)buf, size); |
| 1086 |
return (size); |
| 1087 |
} |
| 1088 |
else |
| 1089 |
return ((tmsize_t)(-1)); |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
tmsize_t _TIFFReadTileAndAllocBuffer(TIFF *tif, void **buf, |
| 1099 |
tmsize_t bufsizetoalloc, uint32_t x, |
| 1100 |
uint32_t y, uint32_t z, uint16_t s) |
| 1101 |
{ |
| 1102 |
if (!TIFFCheckRead(tif, 1) || !TIFFCheckTile(tif, x, y, z, s)) |
| 1103 |
return ((tmsize_t)(-1)); |
| 1104 |
return (_TIFFReadEncodedTileAndAllocBuffer( |
| 1105 |
tif, TIFFComputeTile(tif, x, y, z, s), buf, bufsizetoalloc, |
| 1106 |
(tmsize_t)(-1))); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
tmsize_t _TIFFReadEncodedTileAndAllocBuffer(TIFF *tif, uint32_t tile, |
| 1116 |
void **buf, tmsize_t bufsizetoalloc, |
| 1117 |
tmsize_t size_to_read) |
| 1118 |
{ |
| 1119 |
static const char module[] = "_TIFFReadEncodedTileAndAllocBuffer"; |
| 1120 |
TIFFDirectory *td = &tif->tif_dir; |
| 1121 |
tmsize_t tilesize = tif->tif_dir.td_tilesize; |
| 1122 |
|
| 1123 |
if (*buf != NULL) |
| 1124 |
{ |
| 1125 |
return TIFFReadEncodedTile(tif, tile, *buf, size_to_read); |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (!TIFFCheckRead(tif, 1)) |
| 1129 |
return ((tmsize_t)(-1)); |
| 1130 |
if (tile >= td->td_nstrips) |
| 1131 |
{ |
| 1132 |
TIFFErrorExtR(tif, module, |
| 1133 |
"%" PRIu32 ": Tile out of range, max %" PRIu32, tile, |
| 1134 |
td->td_nstrips); |
| 1135 |
return ((tmsize_t)(-1)); |
| 1136 |
} |
| 1137 |
|
| 1138 |
if (!TIFFFillTile(tif, tile)) |
| 1139 |
return ((tmsize_t)(-1)); |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
if (td->td_compression == COMPRESSION_NONE) |
| 1144 |
{ |
| 1145 |
if (tif->tif_rawdatasize != tilesize) |
| 1146 |
{ |
| 1147 |
TIFFErrorExtR(tif, TIFFFileName(tif), |
| 1148 |
"Invalid tile byte count for tile %u. " |
| 1149 |
"Expected %" PRIu64 ", got %" PRIu64, |
| 1150 |
tile, (uint64_t)tilesize, |
| 1151 |
(uint64_t)tif->tif_rawdatasize); |
| 1152 |
return ((tmsize_t)(-1)); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
else |
| 1156 |
{ |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
if (bufsizetoalloc > 100 * 1024 * 1024) |
| 1162 |
{ |
| 1163 |
const uint64_t maxCompressionRatio = |
| 1164 |
TIFFGetMaxCompressionRatio(tif); |
| 1165 |
if (maxCompressionRatio > 0 && |
| 1166 |
(uint64_t)tif->tif_rawdatasize < |
| 1167 |
(uint64_t)tilesize / maxCompressionRatio) |
| 1168 |
{ |
| 1169 |
TIFFErrorExtR(tif, TIFFFileName(tif), |
| 1170 |
"Likely invalid tile byte count for tile %u. " |
| 1171 |
"Uncompressed tile size is %" PRIu64 ", " |
| 1172 |
"compressed one is %" PRIu64, |
| 1173 |
tile, (uint64_t)tilesize, |
| 1174 |
(uint64_t)tif->tif_rawdatasize); |
| 1175 |
return ((tmsize_t)(-1)); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
*buf = _TIFFmallocExt(tif, bufsizetoalloc); |
| 1181 |
if (*buf == NULL) |
| 1182 |
{ |
| 1183 |
TIFFErrorExtR(tif, TIFFFileName(tif), "No space for tile buffer"); |
| 1184 |
return ((tmsize_t)(-1)); |
| 1185 |
} |
| 1186 |
_TIFFmemset(*buf, 0, bufsizetoalloc); |
| 1187 |
|
| 1188 |
if (size_to_read == (tmsize_t)(-1)) |
| 1189 |
size_to_read = tilesize; |
| 1190 |
else if (size_to_read > tilesize) |
| 1191 |
size_to_read = tilesize; |
| 1192 |
if ((*tif->tif_decodetile)(tif, (uint8_t *)*buf, size_to_read, |
| 1193 |
(uint16_t)(tile / td->td_stripsperimage))) |
| 1194 |
{ |
| 1195 |
(*tif->tif_postdecode)(tif, (uint8_t *)*buf, size_to_read); |
| 1196 |
return (size_to_read); |
| 1197 |
} |
| 1198 |
else |
| 1199 |
return ((tmsize_t)(-1)); |
| 1200 |
} |
| 1201 |
|
| 1202 |
static tmsize_t TIFFReadRawTile1(TIFF *tif, uint32_t tile, void *buf, |
| 1203 |
tmsize_t size, const char *module) |
| 1204 |
{ |
| 1205 |
assert((tif->tif_flags & TIFF_NOREADRAW) == 0); |
| 1206 |
if (!isMapped(tif)) |
| 1207 |
{ |
| 1208 |
tmsize_t cc; |
| 1209 |
|
| 1210 |
if (!SeekOK(tif, TIFFGetStrileOffset(tif, tile))) |
| 1211 |
{ |
| 1212 |
TIFFErrorExtR(tif, module, |
| 1213 |
"Seek error at row %" PRIu32 ", col %" PRIu32 |
| 1214 |
", tile %" PRIu32, |
| 1215 |
tif->tif_dir.td_row, tif->tif_dir.td_col, tile); |
| 1216 |
return ((tmsize_t)(-1)); |
| 1217 |
} |
| 1218 |
cc = TIFFReadFile(tif, buf, size); |
| 1219 |
if (cc != size) |
| 1220 |
{ |
| 1221 |
TIFFErrorExtR(tif, module, |
| 1222 |
"Read error at row %" PRIu32 ", col %" PRIu32 |
| 1223 |
"; got %" TIFF_SSIZE_FORMAT |
| 1224 |
" bytes, expected %" TIFF_SSIZE_FORMAT, |
| 1225 |
tif->tif_dir.td_row, tif->tif_dir.td_col, cc, size); |
| 1226 |
return ((tmsize_t)(-1)); |
| 1227 |
} |
| 1228 |
} |
| 1229 |
else |
| 1230 |
{ |
| 1231 |
tmsize_t ma, mb; |
| 1232 |
tmsize_t n; |
| 1233 |
ma = (tmsize_t)TIFFGetStrileOffset(tif, tile); |
| 1234 |
mb = ma + size; |
| 1235 |
if ((TIFFGetStrileOffset(tif, tile) > (uint64_t)TIFF_TMSIZE_T_MAX) || |
| 1236 |
(ma > tif->tif_size)) |
| 1237 |
n = 0; |
| 1238 |
else if ((mb < ma) || (mb < size) || (mb > tif->tif_size)) |
| 1239 |
n = tif->tif_size - ma; |
| 1240 |
else |
| 1241 |
n = size; |
| 1242 |
if (n != size) |
| 1243 |
{ |
| 1244 |
TIFFErrorExtR(tif, module, |
| 1245 |
"Read error at row %" PRIu32 ", col %" PRIu32 |
| 1246 |
", tile %" PRIu32 "; got %" TIFF_SSIZE_FORMAT |
| 1247 |
" bytes, expected %" TIFF_SSIZE_FORMAT, |
| 1248 |
tif->tif_dir.td_row, tif->tif_dir.td_col, tile, n, |
| 1249 |
size); |
| 1250 |
return ((tmsize_t)(-1)); |
| 1251 |
} |
| 1252 |
_TIFFmemcpy(buf, tif->tif_base + ma, size); |
| 1253 |
} |
| 1254 |
return (size); |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
tmsize_t TIFFReadRawTile(TIFF *tif, uint32_t tile, void *buf, tmsize_t size) |
| 1261 |
{ |
| 1262 |
static const char module[] = "TIFFReadRawTile"; |
| 1263 |
TIFFDirectory *td = &tif->tif_dir; |
| 1264 |
uint64_t bytecount64; |
| 1265 |
tmsize_t bytecountm; |
| 1266 |
|
| 1267 |
if (!TIFFCheckRead(tif, 1)) |
| 1268 |
return ((tmsize_t)(-1)); |
| 1269 |
if (tile >= td->td_nstrips) |
| 1270 |
{ |
| 1271 |
TIFFErrorExtR(tif, module, |
| 1272 |
"%" PRIu32 ": Tile out of range, max %" PRIu32, tile, |
| 1273 |
td->td_nstrips); |
| 1274 |
return ((tmsize_t)(-1)); |
| 1275 |
} |
| 1276 |
if (tif->tif_flags & TIFF_NOREADRAW) |
| 1277 |
{ |
| 1278 |
TIFFErrorExtR(tif, module, |
| 1279 |
"Compression scheme does not support access to raw " |
| 1280 |
"uncompressed data"); |
| 1281 |
return ((tmsize_t)(-1)); |
| 1282 |
} |
| 1283 |
bytecount64 = TIFFGetStrileByteCount(tif, tile); |
| 1284 |
if (size != (tmsize_t)(-1) && (uint64_t)size <= bytecount64) |
| 1285 |
bytecountm = size; |
| 1286 |
else |
| 1287 |
bytecountm = _TIFFCastUInt64ToSSize(tif, bytecount64, module); |
| 1288 |
if (bytecountm == 0) |
| 1289 |
{ |
| 1290 |
return ((tmsize_t)(-1)); |
| 1291 |
} |
| 1292 |
return (TIFFReadRawTile1(tif, tile, buf, bytecountm, module)); |
| 1293 |
} |
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
int TIFFFillTile(TIFF *tif, uint32_t tile) |
| 1300 |
{ |
| 1301 |
static const char module[] = "TIFFFillTile"; |
| 1302 |
TIFFDirectory *td = &tif->tif_dir; |
| 1303 |
|
| 1304 |
if ((tif->tif_flags & TIFF_NOREADRAW) == 0) |
| 1305 |
{ |
| 1306 |
uint64_t bytecount = TIFFGetStrileByteCount(tif, tile); |
| 1307 |
if (bytecount == 0 || bytecount > (uint64_t)TIFF_INT64_MAX) |
| 1308 |
{ |
| 1309 |
TIFFErrorExtR(tif, module, |
| 1310 |
"%" PRIu64 ": Invalid tile byte count, tile %" PRIu32, |
| 1311 |
bytecount, tile); |
| 1312 |
return (0); |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
const tmsize_t tilesize = TIFFTileSize(tif); |
| 1317 |
if (tilesize > 0) |
| 1318 |
{ |
| 1319 |
if (bytecount > 1024 * 1024 && |
| 1320 |
(bytecount - 4096) / 10 > (uint64_t)tilesize) |
| 1321 |
{ |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
uint64_t newbytecount = (uint64_t)tilesize * 10 + 4096; |
| 1333 |
TIFFWarningExtR(tif, module, |
| 1334 |
"Too large tile byte count %" PRIu64 |
| 1335 |
", tile %" PRIu32 ". Limiting to %" PRIu64, |
| 1336 |
bytecount, tile, newbytecount); |
| 1337 |
bytecount = newbytecount; |
| 1338 |
} |
| 1339 |
else if (tilesize > 100 * 1024 * 1024) |
| 1340 |
{ |
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
const uint64_t maxCompressionRatio = |
| 1347 |
TIFFGetMaxCompressionRatio(tif); |
| 1348 |
if (maxCompressionRatio > 0 && |
| 1349 |
bytecount < (uint64_t)tilesize / maxCompressionRatio) |
| 1350 |
{ |
| 1351 |
TIFFErrorExtR(tif, module, |
| 1352 |
"Likely invalid tile byte count for tile %u. " |
| 1353 |
"Uncompressed tile size is %" PRIu64 ", " |
| 1354 |
"compressed one is %" PRIu64, |
| 1355 |
tile, (uint64_t)tilesize, bytecount); |
| 1356 |
return 0; |
| 1357 |
} |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
if (isMapped(tif)) |
| 1362 |
{ |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
if (bytecount > (uint64_t)tif->tif_size || |
| 1373 |
TIFFGetStrileOffset(tif, tile) > |
| 1374 |
(uint64_t)tif->tif_size - bytecount) |
| 1375 |
{ |
| 1376 |
tif->tif_dir.td_curtile = NOTILE; |
| 1377 |
return (0); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
if (isMapped(tif) && (isFillOrder(tif, td->td_fillorder) || |
| 1382 |
(tif->tif_flags & TIFF_NOBITREV))) |
| 1383 |
{ |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) |
| 1396 |
{ |
| 1397 |
_TIFFfreeExt(tif, tif->tif_rawdata); |
| 1398 |
tif->tif_rawdata = NULL; |
| 1399 |
tif->tif_rawdatasize = 0; |
| 1400 |
} |
| 1401 |
tif->tif_flags &= ~TIFF_MYBUFFER; |
| 1402 |
|
| 1403 |
tif->tif_rawdatasize = (tmsize_t)bytecount; |
| 1404 |
tif->tif_rawdata = |
| 1405 |
tif->tif_base + (tmsize_t)TIFFGetStrileOffset(tif, tile); |
| 1406 |
tif->tif_rawdataoff = 0; |
| 1407 |
tif->tif_rawdataloaded = (tmsize_t)bytecount; |
| 1408 |
tif->tif_flags |= TIFF_BUFFERMMAP; |
| 1409 |
} |
| 1410 |
else |
| 1411 |
{ |
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
tmsize_t bytecountm; |
| 1418 |
bytecountm = (tmsize_t)bytecount; |
| 1419 |
if ((uint64_t)bytecountm != bytecount) |
| 1420 |
{ |
| 1421 |
TIFFErrorExtR(tif, module, "Integer overflow"); |
| 1422 |
return (0); |
| 1423 |
} |
| 1424 |
if (bytecountm > tif->tif_rawdatasize) |
| 1425 |
{ |
| 1426 |
tif->tif_dir.td_curtile = NOTILE; |
| 1427 |
if ((tif->tif_flags & TIFF_MYBUFFER) == 0) |
| 1428 |
{ |
| 1429 |
TIFFErrorExtR(tif, module, |
| 1430 |
"Data buffer too small to hold tile %" PRIu32, |
| 1431 |
tile); |
| 1432 |
return (0); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
if (tif->tif_flags & TIFF_BUFFERMMAP) |
| 1436 |
{ |
| 1437 |
tif->tif_dir.td_curtile = NOTILE; |
| 1438 |
tif->tif_rawdata = NULL; |
| 1439 |
tif->tif_rawdatasize = 0; |
| 1440 |
tif->tif_flags &= ~TIFF_BUFFERMMAP; |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (isMapped(tif)) |
| 1444 |
{ |
| 1445 |
if (bytecountm > tif->tif_rawdatasize && |
| 1446 |
!TIFFReadBufferSetup(tif, 0, bytecountm)) |
| 1447 |
{ |
| 1448 |
return (0); |
| 1449 |
} |
| 1450 |
if (TIFFReadRawTile1(tif, tile, tif->tif_rawdata, bytecountm, |
| 1451 |
module) != bytecountm) |
| 1452 |
{ |
| 1453 |
return (0); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
else |
| 1457 |
{ |
| 1458 |
if (TIFFReadRawStripOrTile2(tif, tile, 0, bytecountm, module) != |
| 1459 |
bytecountm) |
| 1460 |
{ |
| 1461 |
return (0); |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
tif->tif_rawdataoff = 0; |
| 1466 |
tif->tif_rawdataloaded = bytecountm; |
| 1467 |
|
| 1468 |
if (tif->tif_rawdata != NULL && |
| 1469 |
!isFillOrder(tif, td->td_fillorder) && |
| 1470 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 1471 |
TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdataloaded); |
| 1472 |
} |
| 1473 |
} |
| 1474 |
return (TIFFStartTile(tif, tile)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
int TIFFReadBufferSetup(TIFF *tif, void *bp, tmsize_t size) |
| 1487 |
{ |
| 1488 |
static const char module[] = "TIFFReadBufferSetup"; |
| 1489 |
|
| 1490 |
assert((tif->tif_flags & TIFF_NOREADRAW) == 0); |
| 1491 |
tif->tif_flags &= ~TIFF_BUFFERMMAP; |
| 1492 |
|
| 1493 |
if (tif->tif_rawdata) |
| 1494 |
{ |
| 1495 |
if (tif->tif_flags & TIFF_MYBUFFER) |
| 1496 |
_TIFFfreeExt(tif, tif->tif_rawdata); |
| 1497 |
tif->tif_rawdata = NULL; |
| 1498 |
tif->tif_rawdatasize = 0; |
| 1499 |
} |
| 1500 |
if (bp) |
| 1501 |
{ |
| 1502 |
tif->tif_rawdatasize = size; |
| 1503 |
tif->tif_rawdata = (uint8_t *)bp; |
| 1504 |
tif->tif_flags &= ~TIFF_MYBUFFER; |
| 1505 |
} |
| 1506 |
else |
| 1507 |
{ |
| 1508 |
tif->tif_rawdatasize = (tmsize_t)TIFFroundup_64((uint64_t)size, 1024); |
| 1509 |
if (tif->tif_rawdatasize == 0) |
| 1510 |
{ |
| 1511 |
TIFFErrorExtR(tif, module, "Invalid buffer size"); |
| 1512 |
return (0); |
| 1513 |
} |
| 1514 |
|
| 1515 |
|
| 1516 |
tif->tif_rawdata = |
| 1517 |
(uint8_t *)_TIFFcallocExt(tif, 1, tif->tif_rawdatasize); |
| 1518 |
tif->tif_flags |= TIFF_MYBUFFER; |
| 1519 |
} |
| 1520 |
if (tif->tif_rawdata == NULL) |
| 1521 |
{ |
| 1522 |
TIFFErrorExtR(tif, module, |
| 1523 |
"No space for data buffer at scanline %" PRIu32, |
| 1524 |
tif->tif_dir.td_row); |
| 1525 |
tif->tif_rawdatasize = 0; |
| 1526 |
return (0); |
| 1527 |
} |
| 1528 |
return (1); |
| 1529 |
} |
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
static int TIFFStartStrip(TIFF *tif, uint32_t strip) |
| 1536 |
{ |
| 1537 |
TIFFDirectory *td = &tif->tif_dir; |
| 1538 |
|
| 1539 |
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
| 1540 |
{ |
| 1541 |
if (!(*tif->tif_setupdecode)(tif)) |
| 1542 |
return (0); |
| 1543 |
tif->tif_flags |= TIFF_CODERSETUP; |
| 1544 |
} |
| 1545 |
if (td->td_stripsperimage == 0) |
| 1546 |
{ |
| 1547 |
TIFFErrorExtR(tif, "TIFFStartStrip", "Zero strips per image"); |
| 1548 |
return 0; |
| 1549 |
} |
| 1550 |
tif->tif_dir.td_curstrip = strip; |
| 1551 |
tif->tif_dir.td_row = (strip % td->td_stripsperimage) * td->td_rowsperstrip; |
| 1552 |
tif->tif_flags &= ~TIFF_BUF4WRITE; |
| 1553 |
|
| 1554 |
if (tif->tif_flags & TIFF_NOREADRAW) |
| 1555 |
{ |
| 1556 |
tif->tif_rawcp = NULL; |
| 1557 |
tif->tif_rawcc = 0; |
| 1558 |
} |
| 1559 |
else |
| 1560 |
{ |
| 1561 |
tif->tif_rawcp = tif->tif_rawdata; |
| 1562 |
if (tif->tif_rawdataloaded > 0) |
| 1563 |
tif->tif_rawcc = tif->tif_rawdataloaded; |
| 1564 |
else |
| 1565 |
tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, strip); |
| 1566 |
} |
| 1567 |
if ((*tif->tif_predecode)(tif, (uint16_t)(strip / td->td_stripsperimage)) == |
| 1568 |
0) |
| 1569 |
{ |
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
tif->tif_dir.td_curstrip = NOSTRIP; |
| 1576 |
return 0; |
| 1577 |
} |
| 1578 |
return 1; |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
static int TIFFStartTile(TIFF *tif, uint32_t tile) |
| 1586 |
{ |
| 1587 |
static const char module[] = "TIFFStartTile"; |
| 1588 |
TIFFDirectory *td = &tif->tif_dir; |
| 1589 |
uint32_t howmany32; |
| 1590 |
|
| 1591 |
if ((tif->tif_flags & TIFF_CODERSETUP) == 0) |
| 1592 |
{ |
| 1593 |
if (!(*tif->tif_setupdecode)(tif)) |
| 1594 |
return (0); |
| 1595 |
tif->tif_flags |= TIFF_CODERSETUP; |
| 1596 |
} |
| 1597 |
tif->tif_dir.td_curtile = tile; |
| 1598 |
if (td->td_tilewidth == 0) |
| 1599 |
{ |
| 1600 |
TIFFErrorExtR(tif, module, "Zero tilewidth"); |
| 1601 |
return 0; |
| 1602 |
} |
| 1603 |
howmany32 = TIFFhowmany_32(td->td_imagewidth, td->td_tilewidth); |
| 1604 |
if (howmany32 == 0) |
| 1605 |
{ |
| 1606 |
TIFFErrorExtR(tif, module, "Zero tiles"); |
| 1607 |
return 0; |
| 1608 |
} |
| 1609 |
tif->tif_dir.td_row = (tile % howmany32) * td->td_tilelength; |
| 1610 |
howmany32 = TIFFhowmany_32(td->td_imagelength, td->td_tilelength); |
| 1611 |
if (howmany32 == 0) |
| 1612 |
{ |
| 1613 |
TIFFErrorExtR(tif, module, "Zero tiles"); |
| 1614 |
return 0; |
| 1615 |
} |
| 1616 |
tif->tif_dir.td_col = (tile % howmany32) * td->td_tilewidth; |
| 1617 |
tif->tif_flags &= ~TIFF_BUF4WRITE; |
| 1618 |
if (tif->tif_flags & TIFF_NOREADRAW) |
| 1619 |
{ |
| 1620 |
tif->tif_rawcp = NULL; |
| 1621 |
tif->tif_rawcc = 0; |
| 1622 |
} |
| 1623 |
else |
| 1624 |
{ |
| 1625 |
tif->tif_rawcp = tif->tif_rawdata; |
| 1626 |
if (tif->tif_rawdataloaded > 0) |
| 1627 |
tif->tif_rawcc = tif->tif_rawdataloaded; |
| 1628 |
else |
| 1629 |
tif->tif_rawcc = (tmsize_t)TIFFGetStrileByteCount(tif, tile); |
| 1630 |
} |
| 1631 |
return ( |
| 1632 |
(*tif->tif_predecode)(tif, (uint16_t)(tile / td->td_stripsperimage))); |
| 1633 |
} |
| 1634 |
|
| 1635 |
static int TIFFCheckRead(TIFF *tif, int tiles) |
| 1636 |
{ |
| 1637 |
if (tif->tif_mode == O_WRONLY) |
| 1638 |
{ |
| 1639 |
TIFFErrorExtR(tif, tif->tif_name, "File not open for reading"); |
| 1640 |
return (0); |
| 1641 |
} |
| 1642 |
if (tiles ^ isTiled(tif)) |
| 1643 |
{ |
| 1644 |
TIFFErrorExtR(tif, tif->tif_name, |
| 1645 |
tiles ? "Can not read tiles from a striped image" |
| 1646 |
: "Can not read scanlines from a tiled image"); |
| 1647 |
return (0); |
| 1648 |
} |
| 1649 |
return (1); |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
int TIFFReadFromUserBuffer(TIFF *tif, uint32_t strile, void *inbuf, |
| 1662 |
tmsize_t insize, void *outbuf, tmsize_t outsize) |
| 1663 |
{ |
| 1664 |
static const char module[] = "TIFFReadFromUserBuffer"; |
| 1665 |
TIFFDirectory *td = &tif->tif_dir; |
| 1666 |
int ret = 1; |
| 1667 |
uint32_t old_tif_flags = tif->tif_flags; |
| 1668 |
tmsize_t old_rawdatasize = tif->tif_rawdatasize; |
| 1669 |
void *old_rawdata = tif->tif_rawdata; |
| 1670 |
|
| 1671 |
if (tif->tif_mode == O_WRONLY) |
| 1672 |
{ |
| 1673 |
TIFFErrorExtR(tif, tif->tif_name, "File not open for reading"); |
| 1674 |
return 0; |
| 1675 |
} |
| 1676 |
if (tif->tif_flags & TIFF_NOREADRAW) |
| 1677 |
{ |
| 1678 |
TIFFErrorExtR(tif, module, |
| 1679 |
"Compression scheme does not support access to raw " |
| 1680 |
"uncompressed data"); |
| 1681 |
return 0; |
| 1682 |
} |
| 1683 |
|
| 1684 |
tif->tif_flags &= ~TIFF_MYBUFFER; |
| 1685 |
tif->tif_flags |= TIFF_BUFFERMMAP; |
| 1686 |
tif->tif_rawdatasize = insize; |
| 1687 |
tif->tif_rawdata = (uint8_t *)inbuf; |
| 1688 |
tif->tif_rawdataoff = 0; |
| 1689 |
tif->tif_rawdataloaded = insize; |
| 1690 |
|
| 1691 |
if (!isFillOrder(tif, td->td_fillorder) && |
| 1692 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 1693 |
{ |
| 1694 |
TIFFReverseBits((uint8_t *)inbuf, insize); |
| 1695 |
} |
| 1696 |
|
| 1697 |
if (TIFFIsTiled(tif)) |
| 1698 |
{ |
| 1699 |
if (!TIFFStartTile(tif, strile)) |
| 1700 |
{ |
| 1701 |
ret = 0; |
| 1702 |
|
| 1703 |
if (outbuf) |
| 1704 |
memset(outbuf, 0, (size_t)outsize); |
| 1705 |
} |
| 1706 |
else if (!(*tif->tif_decodetile)( |
| 1707 |
tif, (uint8_t *)outbuf, outsize, |
| 1708 |
(uint16_t)(strile / td->td_stripsperimage))) |
| 1709 |
{ |
| 1710 |
ret = 0; |
| 1711 |
} |
| 1712 |
} |
| 1713 |
else |
| 1714 |
{ |
| 1715 |
uint32_t rowsperstrip = td->td_rowsperstrip; |
| 1716 |
uint32_t stripsperplane; |
| 1717 |
if (rowsperstrip > td->td_imagelength) |
| 1718 |
rowsperstrip = td->td_imagelength; |
| 1719 |
if (rowsperstrip == 0) |
| 1720 |
{ |
| 1721 |
TIFFErrorExtR(tif, module, "rowsperstrip is zero"); |
| 1722 |
ret = 0; |
| 1723 |
} |
| 1724 |
else |
| 1725 |
{ |
| 1726 |
stripsperplane = |
| 1727 |
TIFFhowmany_32_maxuint_compat(td->td_imagelength, rowsperstrip); |
| 1728 |
if (!TIFFStartStrip(tif, strile)) |
| 1729 |
{ |
| 1730 |
ret = 0; |
| 1731 |
|
| 1732 |
if (outbuf) |
| 1733 |
memset(outbuf, 0, (size_t)outsize); |
| 1734 |
} |
| 1735 |
else if (!(*tif->tif_decodestrip)( |
| 1736 |
tif, (uint8_t *)outbuf, outsize, |
| 1737 |
(uint16_t)(strile / stripsperplane))) |
| 1738 |
{ |
| 1739 |
ret = 0; |
| 1740 |
} |
| 1741 |
} |
| 1742 |
} |
| 1743 |
if (ret) |
| 1744 |
{ |
| 1745 |
(*tif->tif_postdecode)(tif, (uint8_t *)outbuf, outsize); |
| 1746 |
} |
| 1747 |
|
| 1748 |
if (!isFillOrder(tif, td->td_fillorder) && |
| 1749 |
(tif->tif_flags & TIFF_NOBITREV) == 0) |
| 1750 |
{ |
| 1751 |
TIFFReverseBits((uint8_t *)inbuf, insize); |
| 1752 |
} |
| 1753 |
|
| 1754 |
tif->tif_flags = (old_tif_flags & (TIFF_MYBUFFER | TIFF_BUFFERMMAP)) | |
| 1755 |
(tif->tif_flags & ~(TIFF_MYBUFFER | TIFF_BUFFERMMAP)); |
| 1756 |
tif->tif_rawdatasize = old_rawdatasize; |
| 1757 |
tif->tif_rawdata = (uint8_t *)old_rawdata; |
| 1758 |
tif->tif_rawdataoff = 0; |
| 1759 |
tif->tif_rawdataloaded = 0; |
| 1760 |
|
| 1761 |
return ret; |
| 1762 |
} |
| 1763 |
|
| 1764 |
void _TIFFNoPostDecode(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 1765 |
{ |
| 1766 |
(void)tif; |
| 1767 |
(void)buf; |
| 1768 |
(void)cc; |
| 1769 |
} |
| 1770 |
|
| 1771 |
void _TIFFSwab16BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 1772 |
{ |
| 1773 |
(void)tif; |
| 1774 |
assert((cc & 1) == 0); |
| 1775 |
TIFFSwabArrayOfShort((uint16_t *)buf, cc / 2); |
| 1776 |
} |
| 1777 |
|
| 1778 |
void _TIFFSwab24BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 1779 |
{ |
| 1780 |
(void)tif; |
| 1781 |
assert((cc % 3) == 0); |
| 1782 |
TIFFSwabArrayOfTriples((uint8_t *)buf, cc / 3); |
| 1783 |
} |
| 1784 |
|
| 1785 |
void _TIFFSwab32BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 1786 |
{ |
| 1787 |
(void)tif; |
| 1788 |
assert((cc & 3) == 0); |
| 1789 |
TIFFSwabArrayOfLong((uint32_t *)buf, cc / 4); |
| 1790 |
} |
| 1791 |
|
| 1792 |
void _TIFFSwab64BitData(TIFF *tif, uint8_t *buf, tmsize_t cc) |
| 1793 |
{ |
| 1794 |
(void)tif; |
| 1795 |
assert((cc & 7) == 0); |
| 1796 |
TIFFSwabArrayOfDouble((double *)buf, cc / 8); |
| 1797 |
} |