| 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 |
#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS |
| 30 |
#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "tiffiop.h" |
| 34 |
#include <assert.h> |
| 35 |
#include <limits.h> |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
int _tiffDummyMapProc(thandle_t fd, void **pbase, toff_t *psize) |
| 41 |
{ |
| 42 |
(void)fd; |
| 43 |
(void)pbase; |
| 44 |
(void)psize; |
| 45 |
return (0); |
| 46 |
} |
| 47 |
|
| 48 |
void _tiffDummyUnmapProc(thandle_t fd, void *base, toff_t size) |
| 49 |
{ |
| 50 |
(void)fd; |
| 51 |
(void)base; |
| 52 |
(void)size; |
| 53 |
} |
| 54 |
|
| 55 |
int _TIFFgetMode(TIFFOpenOptions *opts, thandle_t clientdata, const char *mode, |
| 56 |
const char *module) |
| 57 |
{ |
| 58 |
int m = -1; |
| 59 |
|
| 60 |
switch (mode[0]) |
| 61 |
{ |
| 62 |
case 'r': |
| 63 |
m = O_RDONLY; |
| 64 |
if (mode[1] == '+') |
| 65 |
m = O_RDWR; |
| 66 |
break; |
| 67 |
case 'w': |
| 68 |
case 'a': |
| 69 |
m = O_RDWR | O_CREAT; |
| 70 |
if (mode[0] == 'w') |
| 71 |
m |= O_TRUNC; |
| 72 |
break; |
| 73 |
default: |
| 74 |
_TIFFErrorEarly(opts, clientdata, module, "\"%s\": Bad mode", mode); |
| 75 |
break; |
| 76 |
} |
| 77 |
return (m); |
| 78 |
} |
| 79 |
|
| 80 |
TIFFOpenOptions *TIFFOpenOptionsAlloc(void) |
| 81 |
{ |
| 82 |
TIFFOpenOptions *opts = |
| 83 |
(TIFFOpenOptions *)_TIFFcalloc(1, sizeof(TIFFOpenOptions)); |
| 84 |
return opts; |
| 85 |
} |
| 86 |
|
| 87 |
void TIFFOpenOptionsFree(TIFFOpenOptions *opts) { _TIFFfree(opts); } |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
void TIFFOpenOptionsSetMaxSingleMemAlloc(TIFFOpenOptions *opts, |
| 95 |
tmsize_t max_single_mem_alloc) |
| 96 |
{ |
| 97 |
opts->max_single_mem_alloc = max_single_mem_alloc; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void TIFFOpenOptionsSetMaxCumulatedMemAlloc(TIFFOpenOptions *opts, |
| 107 |
tmsize_t max_cumulated_mem_alloc) |
| 108 |
{ |
| 109 |
opts->max_cumulated_mem_alloc = max_cumulated_mem_alloc; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
void TIFFOpenOptionsSetWarnAboutUnknownTags(TIFFOpenOptions *opts, |
| 116 |
int warn_about_unknown_tags) |
| 117 |
{ |
| 118 |
opts->warn_about_unknown_tags = warn_about_unknown_tags; |
| 119 |
} |
| 120 |
|
| 121 |
void TIFFOpenOptionsSetErrorHandlerExtR(TIFFOpenOptions *opts, |
| 122 |
TIFFErrorHandlerExtR handler, |
| 123 |
void *errorhandler_user_data) |
| 124 |
{ |
| 125 |
opts->errorhandler = handler; |
| 126 |
opts->errorhandler_user_data = errorhandler_user_data; |
| 127 |
} |
| 128 |
|
| 129 |
void TIFFOpenOptionsSetWarningHandlerExtR(TIFFOpenOptions *opts, |
| 130 |
TIFFErrorHandlerExtR handler, |
| 131 |
void *warnhandler_user_data) |
| 132 |
{ |
| 133 |
opts->warnhandler = handler; |
| 134 |
opts->warnhandler_user_data = warnhandler_user_data; |
| 135 |
} |
| 136 |
|
| 137 |
static void _TIFFEmitErrorAboveMaxSingleMemAlloc(TIFF *tif, |
| 138 |
const char *pszFunction, |
| 139 |
tmsize_t s) |
| 140 |
{ |
| 141 |
TIFFErrorExtR(tif, pszFunction, |
| 142 |
"Memory allocation of %" PRIu64 |
| 143 |
" bytes is beyond the %" PRIu64 |
| 144 |
" byte limit defined in open options", |
| 145 |
(uint64_t)s, (uint64_t)tif->tif_max_single_mem_alloc); |
| 146 |
} |
| 147 |
|
| 148 |
static void _TIFFEmitErrorAboveMaxCumulatedMemAlloc(TIFF *tif, |
| 149 |
const char *pszFunction, |
| 150 |
tmsize_t s) |
| 151 |
{ |
| 152 |
TIFFErrorExtR(tif, pszFunction, |
| 153 |
"Cumulated memory allocation of %" PRIu64 " + %" PRIu64 |
| 154 |
" bytes is beyond the %" PRIu64 |
| 155 |
" cumulated byte limit defined in open options", |
| 156 |
(uint64_t)tif->tif_cur_cumulated_mem_alloc, (uint64_t)s, |
| 157 |
(uint64_t)tif->tif_max_cumulated_mem_alloc); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
#define LEADING_AREA_TO_STORE_ALLOC_SIZE (2 * SIZEOF_SIZE_T) |
| 171 |
|
| 172 |
|
| 173 |
void *_TIFFmallocExt(TIFF *tif, tmsize_t s) |
| 174 |
{ |
| 175 |
if (tif != NULL && tif->tif_max_single_mem_alloc > 0 && |
| 176 |
s > tif->tif_max_single_mem_alloc) |
| 177 |
{ |
| 178 |
_TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFmallocExt", s); |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) |
| 182 |
{ |
| 183 |
if (s > tif->tif_max_cumulated_mem_alloc - |
| 184 |
tif->tif_cur_cumulated_mem_alloc || |
| 185 |
s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE) |
| 186 |
{ |
| 187 |
_TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFmallocExt", s); |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
void *ptr = _TIFFmalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s); |
| 191 |
if (!ptr) |
| 192 |
return NULL; |
| 193 |
tif->tif_cur_cumulated_mem_alloc += s; |
| 194 |
memcpy(ptr, &s, sizeof(s)); |
| 195 |
return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE; |
| 196 |
} |
| 197 |
return _TIFFmalloc(s); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
void *_TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz) |
| 202 |
{ |
| 203 |
if (nmemb <= 0 || siz <= 0 || nmemb > TIFF_TMSIZE_T_MAX / siz) |
| 204 |
return NULL; |
| 205 |
if (tif != NULL && tif->tif_max_single_mem_alloc > 0) |
| 206 |
{ |
| 207 |
if (nmemb * siz > tif->tif_max_single_mem_alloc) |
| 208 |
{ |
| 209 |
_TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFcallocExt", |
| 210 |
nmemb * siz); |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
} |
| 214 |
if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) |
| 215 |
{ |
| 216 |
const tmsize_t s = nmemb * siz; |
| 217 |
if (s > tif->tif_max_cumulated_mem_alloc - |
| 218 |
tif->tif_cur_cumulated_mem_alloc || |
| 219 |
s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE) |
| 220 |
{ |
| 221 |
_TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFcallocExt", s); |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
void *ptr = _TIFFcalloc(LEADING_AREA_TO_STORE_ALLOC_SIZE + s, 1); |
| 225 |
if (!ptr) |
| 226 |
return NULL; |
| 227 |
tif->tif_cur_cumulated_mem_alloc += s; |
| 228 |
memcpy(ptr, &s, sizeof(s)); |
| 229 |
return (char *)ptr + LEADING_AREA_TO_STORE_ALLOC_SIZE; |
| 230 |
} |
| 231 |
return _TIFFcalloc(nmemb, siz); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
void *_TIFFreallocExt(TIFF *tif, void *p, tmsize_t s) |
| 236 |
{ |
| 237 |
if (tif != NULL && tif->tif_max_single_mem_alloc > 0 && |
| 238 |
s > tif->tif_max_single_mem_alloc) |
| 239 |
{ |
| 240 |
_TIFFEmitErrorAboveMaxSingleMemAlloc(tif, "_TIFFreallocExt", s); |
| 241 |
return NULL; |
| 242 |
} |
| 243 |
if (tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) |
| 244 |
{ |
| 245 |
void *oldPtr = p; |
| 246 |
tmsize_t oldSize = 0; |
| 247 |
if (p) |
| 248 |
{ |
| 249 |
oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE; |
| 250 |
memcpy(&oldSize, oldPtr, sizeof(oldSize)); |
| 251 |
assert(oldSize <= tif->tif_cur_cumulated_mem_alloc); |
| 252 |
} |
| 253 |
if (s > oldSize && |
| 254 |
(s > tif->tif_max_cumulated_mem_alloc - |
| 255 |
(tif->tif_cur_cumulated_mem_alloc - oldSize) || |
| 256 |
s > TIFF_TMSIZE_T_MAX - LEADING_AREA_TO_STORE_ALLOC_SIZE)) |
| 257 |
{ |
| 258 |
_TIFFEmitErrorAboveMaxCumulatedMemAlloc(tif, "_TIFFreallocExt", |
| 259 |
s - oldSize); |
| 260 |
return NULL; |
| 261 |
} |
| 262 |
void *newPtr = |
| 263 |
_TIFFrealloc(oldPtr, LEADING_AREA_TO_STORE_ALLOC_SIZE + s); |
| 264 |
if (newPtr == NULL) |
| 265 |
return NULL; |
| 266 |
tif->tif_cur_cumulated_mem_alloc -= oldSize; |
| 267 |
tif->tif_cur_cumulated_mem_alloc += s; |
| 268 |
memcpy(newPtr, &s, sizeof(s)); |
| 269 |
return (char *)newPtr + LEADING_AREA_TO_STORE_ALLOC_SIZE; |
| 270 |
} |
| 271 |
return _TIFFrealloc(p, s); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
void _TIFFfreeExt(TIFF *tif, void *p) |
| 276 |
{ |
| 277 |
if (p != NULL && tif != NULL && tif->tif_max_cumulated_mem_alloc > 0) |
| 278 |
{ |
| 279 |
void *oldPtr = (char *)p - LEADING_AREA_TO_STORE_ALLOC_SIZE; |
| 280 |
tmsize_t oldSize; |
| 281 |
memcpy(&oldSize, oldPtr, sizeof(oldSize)); |
| 282 |
assert(oldSize <= tif->tif_cur_cumulated_mem_alloc); |
| 283 |
tif->tif_cur_cumulated_mem_alloc -= oldSize; |
| 284 |
p = oldPtr; |
| 285 |
} |
| 286 |
_TIFFfree(p); |
| 287 |
} |
| 288 |
|
| 289 |
TIFF *TIFFClientOpen(const char *name, const char *mode, thandle_t clientdata, |
| 290 |
TIFFReadWriteProc readproc, TIFFReadWriteProc writeproc, |
| 291 |
TIFFSeekProc seekproc, TIFFCloseProc closeproc, |
| 292 |
TIFFSizeProc sizeproc, TIFFMapFileProc mapproc, |
| 293 |
TIFFUnmapFileProc unmapproc) |
| 294 |
{ |
| 295 |
return TIFFClientOpenExt(name, mode, clientdata, readproc, writeproc, |
| 296 |
seekproc, closeproc, sizeproc, mapproc, unmapproc, |
| 297 |
NULL); |
| 298 |
} |
| 299 |
|
| 300 |
TIFF *TIFFClientOpenExt(const char *name, const char *mode, |
| 301 |
thandle_t clientdata, TIFFReadWriteProc readproc, |
| 302 |
TIFFReadWriteProc writeproc, TIFFSeekProc seekproc, |
| 303 |
TIFFCloseProc closeproc, TIFFSizeProc sizeproc, |
| 304 |
TIFFMapFileProc mapproc, TIFFUnmapFileProc unmapproc, |
| 305 |
TIFFOpenOptions *opts) |
| 306 |
{ |
| 307 |
static const char module[] = "TIFFClientOpenExt"; |
| 308 |
TIFF *tif; |
| 309 |
int m; |
| 310 |
const char *cp; |
| 311 |
tmsize_t size_to_alloc; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
assert(sizeof(uint8_t) == 1); |
| 318 |
assert(sizeof(int8_t) == 1); |
| 319 |
assert(sizeof(uint16_t) == 2); |
| 320 |
assert(sizeof(int16_t) == 2); |
| 321 |
assert(sizeof(uint32_t) == 4); |
| 322 |
assert(sizeof(int32_t) == 4); |
| 323 |
assert(sizeof(uint64_t) == 8); |
| 324 |
assert(sizeof(int64_t) == 8); |
| 325 |
{ |
| 326 |
union |
| 327 |
{ |
| 328 |
uint8_t a8[2]; |
| 329 |
uint16_t a16; |
| 330 |
} n; |
| 331 |
n.a8[0] = 1; |
| 332 |
n.a8[1] = 0; |
| 333 |
(void)n; |
| 334 |
#if WORDS_BIGENDIAN |
| 335 |
assert(n.a16 == 256); |
| 336 |
#else |
| 337 |
assert(n.a16 == 1); |
| 338 |
#endif |
| 339 |
} |
| 340 |
|
| 341 |
m = _TIFFgetMode(opts, clientdata, mode, module); |
| 342 |
if (m == -1) |
| 343 |
goto bad2; |
| 344 |
size_to_alloc = (tmsize_t)(sizeof(TIFF) + strlen(name) + 1); |
| 345 |
if (opts && opts->max_single_mem_alloc > 0 && |
| 346 |
size_to_alloc > opts->max_single_mem_alloc) |
| 347 |
{ |
| 348 |
_TIFFErrorEarly(opts, clientdata, module, |
| 349 |
"%s: Memory allocation of %" PRIu64 |
| 350 |
" bytes is beyond the %" PRIu64 |
| 351 |
" byte limit defined in open options", |
| 352 |
name, (uint64_t)size_to_alloc, |
| 353 |
(uint64_t)opts->max_single_mem_alloc); |
| 354 |
goto bad2; |
| 355 |
} |
| 356 |
if (opts && opts->max_cumulated_mem_alloc > 0 && |
| 357 |
size_to_alloc > opts->max_cumulated_mem_alloc) |
| 358 |
{ |
| 359 |
_TIFFErrorEarly(opts, clientdata, module, |
| 360 |
"%s: Memory allocation of %" PRIu64 |
| 361 |
" bytes is beyond the %" PRIu64 |
| 362 |
" cumulated byte limit defined in open options", |
| 363 |
name, (uint64_t)size_to_alloc, |
| 364 |
(uint64_t)opts->max_cumulated_mem_alloc); |
| 365 |
goto bad2; |
| 366 |
} |
| 367 |
tif = (TIFF *)_TIFFmallocExt(NULL, size_to_alloc); |
| 368 |
if (tif == NULL) |
| 369 |
{ |
| 370 |
_TIFFErrorEarly(opts, clientdata, module, |
| 371 |
"%s: Out of memory (TIFF structure)", name); |
| 372 |
goto bad2; |
| 373 |
} |
| 374 |
_TIFFmemset(tif, 0, sizeof(*tif)); |
| 375 |
tif->tif_name = (char *)tif + sizeof(TIFF); |
| 376 |
strcpy(tif->tif_name, name); |
| 377 |
tif->tif_mode = m & ~(O_CREAT | O_TRUNC); |
| 378 |
tif->tif_curdir = TIFF_NON_EXISTENT_DIR_NUMBER; |
| 379 |
tif->tif_curdircount = TIFF_NON_EXISTENT_DIR_NUMBER; |
| 380 |
tif->tif_curoff = 0; |
| 381 |
tif->tif_clientdata = clientdata; |
| 382 |
tif->tif_readproc = readproc; |
| 383 |
tif->tif_writeproc = writeproc; |
| 384 |
tif->tif_seekproc = seekproc; |
| 385 |
tif->tif_closeproc = closeproc; |
| 386 |
tif->tif_sizeproc = sizeproc; |
| 387 |
tif->tif_mapproc = mapproc ? mapproc : _tiffDummyMapProc; |
| 388 |
tif->tif_unmapproc = unmapproc ? unmapproc : _tiffDummyUnmapProc; |
| 389 |
if (opts) |
| 390 |
{ |
| 391 |
tif->tif_errorhandler = opts->errorhandler; |
| 392 |
tif->tif_errorhandler_user_data = opts->errorhandler_user_data; |
| 393 |
tif->tif_warnhandler = opts->warnhandler; |
| 394 |
tif->tif_warnhandler_user_data = opts->warnhandler_user_data; |
| 395 |
tif->tif_max_single_mem_alloc = opts->max_single_mem_alloc; |
| 396 |
tif->tif_max_cumulated_mem_alloc = opts->max_cumulated_mem_alloc; |
| 397 |
tif->tif_warn_about_unknown_tags = opts->warn_about_unknown_tags; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
_TIFFResetTifDirAndInitStrileCounters(&tif->tif_dir); |
| 403 |
|
| 404 |
if (!readproc || !writeproc || !seekproc || !closeproc || !sizeproc) |
| 405 |
{ |
| 406 |
TIFFErrorExtR(tif, module, |
| 407 |
"One of the client procedures is NULL pointer."); |
| 408 |
_TIFFfreeExt(NULL, tif); |
| 409 |
goto bad2; |
| 410 |
} |
| 411 |
|
| 412 |
_TIFFSetDefaultCompressionState(tif); |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
tif->tif_flags = FILLORDER_MSB2LSB; |
| 419 |
if (m == O_RDONLY) |
| 420 |
tif->tif_flags |= TIFF_MAPPED; |
| 421 |
|
| 422 |
#ifdef STRIPCHOP_DEFAULT |
| 423 |
if (m == O_RDONLY || m == O_RDWR) |
| 424 |
tif->tif_flags |= STRIPCHOP_DEFAULT; |
| 425 |
#endif |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
for (cp = mode; *cp; cp++) |
| 483 |
switch (*cp) |
| 484 |
{ |
| 485 |
case 'b': |
| 486 |
#if !WORDS_BIGENDIAN |
| 487 |
if (m & O_CREAT) |
| 488 |
tif->tif_flags |= TIFF_SWAB; |
| 489 |
#endif |
| 490 |
break; |
| 491 |
case 'l': |
| 492 |
#if WORDS_BIGENDIAN |
| 493 |
if ((m & O_CREAT)) |
| 494 |
tif->tif_flags |= TIFF_SWAB; |
| 495 |
#endif |
| 496 |
break; |
| 497 |
case 'B': |
| 498 |
tif->tif_flags = |
| 499 |
(tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB; |
| 500 |
break; |
| 501 |
case 'L': |
| 502 |
tif->tif_flags = |
| 503 |
(tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_LSB2MSB; |
| 504 |
break; |
| 505 |
case 'H': |
| 506 |
TIFFWarningExtR(tif, name, |
| 507 |
"H(ost) mode is deprecated. Since " |
| 508 |
"libtiff 4.5.1, it is an alias of 'B' / " |
| 509 |
"FILLORDER_MSB2LSB."); |
| 510 |
tif->tif_flags = |
| 511 |
(tif->tif_flags & ~TIFF_FILLORDER) | FILLORDER_MSB2LSB; |
| 512 |
break; |
| 513 |
case 'M': |
| 514 |
if (m == O_RDONLY) |
| 515 |
tif->tif_flags |= TIFF_MAPPED; |
| 516 |
break; |
| 517 |
case 'm': |
| 518 |
if (m == O_RDONLY) |
| 519 |
tif->tif_flags &= ~TIFF_MAPPED; |
| 520 |
break; |
| 521 |
case 'C': |
| 522 |
if (m == O_RDONLY) |
| 523 |
tif->tif_flags |= TIFF_STRIPCHOP; |
| 524 |
break; |
| 525 |
case 'c': |
| 526 |
if (m == O_RDONLY) |
| 527 |
tif->tif_flags &= ~TIFF_STRIPCHOP; |
| 528 |
break; |
| 529 |
case 'h': |
| 530 |
tif->tif_flags |= TIFF_HEADERONLY; |
| 531 |
break; |
| 532 |
case '8': |
| 533 |
if (m & O_CREAT) |
| 534 |
tif->tif_flags |= TIFF_BIGTIFF; |
| 535 |
break; |
| 536 |
case 'D': |
| 537 |
tif->tif_flags |= TIFF_DEFERSTRILELOAD; |
| 538 |
break; |
| 539 |
case 'O': |
| 540 |
if (m == O_RDONLY) |
| 541 |
tif->tif_flags |= |
| 542 |
(TIFF_LAZYSTRILELOAD_ASKED | TIFF_DEFERSTRILELOAD); |
| 543 |
break; |
| 544 |
default: |
| 545 |
break; |
| 546 |
} |
| 547 |
|
| 548 |
#ifdef DEFER_STRILE_LOAD |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
tif->tif_flags |= TIFF_DEFERSTRILELOAD; |
| 553 |
#endif |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
if ((m & O_TRUNC) || |
| 559 |
!ReadOK(tif, &tif->tif_header, sizeof(TIFFHeaderClassic))) |
| 560 |
{ |
| 561 |
if (tif->tif_mode == O_RDONLY) |
| 562 |
{ |
| 563 |
TIFFErrorExtR(tif, name, "Cannot read TIFF header"); |
| 564 |
goto bad; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
#if WORDS_BIGENDIAN |
| 570 |
tif->tif_header.common.tiff_magic = |
| 571 |
(tif->tif_flags & TIFF_SWAB) ? TIFF_LITTLEENDIAN : TIFF_BIGENDIAN; |
| 572 |
#else |
| 573 |
tif->tif_header.common.tiff_magic = |
| 574 |
(tif->tif_flags & TIFF_SWAB) ? TIFF_BIGENDIAN : TIFF_LITTLEENDIAN; |
| 575 |
#endif |
| 576 |
TIFFHeaderUnion tif_header_swapped; |
| 577 |
if (!(tif->tif_flags & TIFF_BIGTIFF)) |
| 578 |
{ |
| 579 |
tif->tif_header.common.tiff_version = TIFF_VERSION_CLASSIC; |
| 580 |
tif->tif_header.classic.tiff_diroff = 0; |
| 581 |
tif->tif_header_size = sizeof(TIFFHeaderClassic); |
| 582 |
|
| 583 |
_TIFFmemcpy(&tif_header_swapped, &tif->tif_header, |
| 584 |
sizeof(TIFFHeaderUnion)); |
| 585 |
if (tif->tif_flags & TIFF_SWAB) |
| 586 |
TIFFSwabShort(&tif_header_swapped.common.tiff_version); |
| 587 |
} |
| 588 |
else |
| 589 |
{ |
| 590 |
tif->tif_header.common.tiff_version = TIFF_VERSION_BIG; |
| 591 |
tif->tif_header.big.tiff_offsetsize = 8; |
| 592 |
tif->tif_header.big.tiff_unused = 0; |
| 593 |
tif->tif_header.big.tiff_diroff = 0; |
| 594 |
tif->tif_header_size = sizeof(TIFFHeaderBig); |
| 595 |
|
| 596 |
_TIFFmemcpy(&tif_header_swapped, &tif->tif_header, |
| 597 |
sizeof(TIFFHeaderUnion)); |
| 598 |
if (tif->tif_flags & TIFF_SWAB) |
| 599 |
{ |
| 600 |
TIFFSwabShort(&tif_header_swapped.common.tiff_version); |
| 601 |
TIFFSwabShort(&tif_header_swapped.big.tiff_offsetsize); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
TIFFSeekFile(tif, 0, SEEK_SET); |
| 612 |
if (!WriteOK(tif, &tif_header_swapped, |
| 613 |
(tmsize_t)(tif->tif_header_size))) |
| 614 |
{ |
| 615 |
TIFFErrorExtR(tif, name, "Error writing TIFF header"); |
| 616 |
goto bad; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
if (!TIFFDefaultDirectory(tif)) |
| 622 |
goto bad; |
| 623 |
tif->tif_diroff = 0; |
| 624 |
tif->tif_lastdiroff = 0; |
| 625 |
tif->tif_setdirectory_force_absolute = FALSE; |
| 626 |
|
| 627 |
|
| 628 |
tif->tif_curdircount = 0; |
| 629 |
return (tif); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
if (tif->tif_header.common.tiff_magic != TIFF_BIGENDIAN && |
| 636 |
tif->tif_header.common.tiff_magic != TIFF_LITTLEENDIAN |
| 637 |
#if MDI_SUPPORT |
| 638 |
&& |
| 639 |
#if HOST_BIGENDIAN |
| 640 |
tif->tif_header.common.tiff_magic != MDI_BIGENDIAN |
| 641 |
#else |
| 642 |
tif->tif_header.common.tiff_magic != MDI_LITTLEENDIAN |
| 643 |
#endif |
| 644 |
) |
| 645 |
{ |
| 646 |
TIFFErrorExtR(tif, name, |
| 647 |
"Not a TIFF or MDI file, bad magic number %" PRIu16 |
| 648 |
" (0x%" PRIx16 ")", |
| 649 |
#else |
| 650 |
) |
| 651 |
{ |
| 652 |
TIFFErrorExtR(tif, name, |
| 653 |
"Not a TIFF file, bad magic number %" PRIu16 |
| 654 |
" (0x%" PRIx16 ")", |
| 655 |
#endif |
| 656 |
tif->tif_header.common.tiff_magic, |
| 657 |
tif->tif_header.common.tiff_magic); |
| 658 |
goto bad; |
| 659 |
} |
| 660 |
if (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN) |
| 661 |
{ |
| 662 |
#if !WORDS_BIGENDIAN |
| 663 |
tif->tif_flags |= TIFF_SWAB; |
| 664 |
#endif |
| 665 |
} |
| 666 |
else |
| 667 |
{ |
| 668 |
#if WORDS_BIGENDIAN |
| 669 |
tif->tif_flags |= TIFF_SWAB; |
| 670 |
#endif |
| 671 |
} |
| 672 |
if (tif->tif_flags & TIFF_SWAB) |
| 673 |
TIFFSwabShort(&tif->tif_header.common.tiff_version); |
| 674 |
if ((tif->tif_header.common.tiff_version != TIFF_VERSION_CLASSIC) && |
| 675 |
(tif->tif_header.common.tiff_version != TIFF_VERSION_BIG)) |
| 676 |
{ |
| 677 |
TIFFErrorExtR(tif, name, |
| 678 |
"Not a TIFF file, bad version number %" PRIu16 |
| 679 |
" (0x%" PRIx16 ")", |
| 680 |
tif->tif_header.common.tiff_version, |
| 681 |
tif->tif_header.common.tiff_version); |
| 682 |
goto bad; |
| 683 |
} |
| 684 |
if (tif->tif_header.common.tiff_version == TIFF_VERSION_CLASSIC) |
| 685 |
{ |
| 686 |
if (tif->tif_flags & TIFF_SWAB) |
| 687 |
TIFFSwabLong(&tif->tif_header.classic.tiff_diroff); |
| 688 |
tif->tif_header_size = sizeof(TIFFHeaderClassic); |
| 689 |
} |
| 690 |
else |
| 691 |
{ |
| 692 |
if (!ReadOK(tif, |
| 693 |
((uint8_t *)(&tif->tif_header) + sizeof(TIFFHeaderClassic)), |
| 694 |
(sizeof(TIFFHeaderBig) - sizeof(TIFFHeaderClassic)))) |
| 695 |
{ |
| 696 |
TIFFErrorExtR(tif, name, "Cannot read TIFF header"); |
| 697 |
goto bad; |
| 698 |
} |
| 699 |
if (tif->tif_flags & TIFF_SWAB) |
| 700 |
{ |
| 701 |
TIFFSwabShort(&tif->tif_header.big.tiff_offsetsize); |
| 702 |
TIFFSwabLong8(&tif->tif_header.big.tiff_diroff); |
| 703 |
} |
| 704 |
if (tif->tif_header.big.tiff_offsetsize != 8) |
| 705 |
{ |
| 706 |
TIFFErrorExtR(tif, name, |
| 707 |
"Not a TIFF file, bad BigTIFF offsetsize %" PRIu16 |
| 708 |
" (0x%" PRIx16 ")", |
| 709 |
tif->tif_header.big.tiff_offsetsize, |
| 710 |
tif->tif_header.big.tiff_offsetsize); |
| 711 |
goto bad; |
| 712 |
} |
| 713 |
if (tif->tif_header.big.tiff_unused != 0) |
| 714 |
{ |
| 715 |
TIFFErrorExtR(tif, name, |
| 716 |
"Not a TIFF file, bad BigTIFF unused %" PRIu16 |
| 717 |
" (0x%" PRIx16 ")", |
| 718 |
tif->tif_header.big.tiff_unused, |
| 719 |
tif->tif_header.big.tiff_unused); |
| 720 |
goto bad; |
| 721 |
} |
| 722 |
tif->tif_header_size = sizeof(TIFFHeaderBig); |
| 723 |
tif->tif_flags |= TIFF_BIGTIFF; |
| 724 |
} |
| 725 |
tif->tif_flags |= TIFF_MYBUFFER; |
| 726 |
tif->tif_rawcp = tif->tif_rawdata = 0; |
| 727 |
tif->tif_rawdatasize = 0; |
| 728 |
tif->tif_rawdataoff = 0; |
| 729 |
tif->tif_rawdataloaded = 0; |
| 730 |
|
| 731 |
switch (mode[0]) |
| 732 |
{ |
| 733 |
case 'r': |
| 734 |
if (!(tif->tif_flags & TIFF_BIGTIFF)) |
| 735 |
tif->tif_nextdiroff = tif->tif_header.classic.tiff_diroff; |
| 736 |
else |
| 737 |
tif->tif_nextdiroff = tif->tif_header.big.tiff_diroff; |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
if (tif->tif_flags & TIFF_MAPPED) |
| 744 |
{ |
| 745 |
toff_t n; |
| 746 |
if (TIFFMapFileContents(tif, (void **)(&tif->tif_base), &n)) |
| 747 |
{ |
| 748 |
tif->tif_size = (tmsize_t)n; |
| 749 |
assert((toff_t)tif->tif_size == n); |
| 750 |
} |
| 751 |
else |
| 752 |
tif->tif_flags &= ~TIFF_MAPPED; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
if (tif->tif_flags & TIFF_HEADERONLY) |
| 765 |
{ |
| 766 |
if (!TIFFDefaultDirectory(tif)) |
| 767 |
goto bad; |
| 768 |
return (tif); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
if (TIFFReadDirectory(tif)) |
| 775 |
{ |
| 776 |
return (tif); |
| 777 |
} |
| 778 |
break; |
| 779 |
case 'a': |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
if (!TIFFDefaultDirectory(tif)) |
| 786 |
goto bad; |
| 787 |
return (tif); |
| 788 |
default: |
| 789 |
break; |
| 790 |
} |
| 791 |
bad: |
| 792 |
tif->tif_mode = O_RDONLY; |
| 793 |
TIFFCleanup(tif); |
| 794 |
bad2: |
| 795 |
return ((TIFF *)0); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
const char *TIFFFileName(TIFF *tif) { return (tif->tif_name); } |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
const char *TIFFSetFileName(TIFF *tif, const char *name) |
| 811 |
{ |
| 812 |
const char *old_name = tif->tif_name; |
| 813 |
tif->tif_name = (char *)name; |
| 814 |
return (old_name); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
int TIFFFileno(TIFF *tif) { return (tif->tif_fd); } |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
int TIFFSetFileno(TIFF *tif, int fd) |
| 826 |
{ |
| 827 |
int old_fd = tif->tif_fd; |
| 828 |
tif->tif_fd = fd; |
| 829 |
return old_fd; |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
thandle_t TIFFClientdata(TIFF *tif) { return (tif->tif_clientdata); } |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
thandle_t TIFFSetClientdata(TIFF *tif, thandle_t newvalue) |
| 841 |
{ |
| 842 |
thandle_t m = tif->tif_clientdata; |
| 843 |
tif->tif_clientdata = newvalue; |
| 844 |
return m; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
int TIFFGetMode(TIFF *tif) { return (tif->tif_mode); } |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
int TIFFSetMode(TIFF *tif, int mode) |
| 856 |
{ |
| 857 |
int old_mode = tif->tif_mode; |
| 858 |
tif->tif_mode = mode; |
| 859 |
return (old_mode); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
int TIFFIsTiled(TIFF *tif) { return (isTiled(tif)); } |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
uint32_t TIFFCurrentRow(TIFF *tif) { return (tif->tif_dir.td_row); } |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
tdir_t TIFFCurrentDirectory(TIFF *tif) { return (tif->tif_curdir); } |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
uint32_t TIFFCurrentStrip(TIFF *tif) { return (tif->tif_dir.td_curstrip); } |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
uint32_t TIFFCurrentTile(TIFF *tif) { return (tif->tif_dir.td_curtile); } |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
int TIFFIsByteSwapped(TIFF *tif) { return ((tif->tif_flags & TIFF_SWAB) != 0); } |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
int TIFFIsUpSampled(TIFF *tif) { return (isUpSampled(tif)); } |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
int TIFFIsMSB2LSB(TIFF *tif) { return (isFillOrder(tif, FILLORDER_MSB2LSB)); } |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
int TIFFIsBigEndian(TIFF *tif) |
| 907 |
{ |
| 908 |
return (tif->tif_header.common.tiff_magic == TIFF_BIGENDIAN); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
int TIFFIsBigTIFF(TIFF *tif) { return ((tif->tif_flags & TIFF_BIGTIFF) != 0); } |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
TIFFReadWriteProc TIFFGetReadProc(TIFF *tif) { return (tif->tif_readproc); } |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
TIFFReadWriteProc TIFFGetWriteProc(TIFF *tif) { return (tif->tif_writeproc); } |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
TIFFSeekProc TIFFGetSeekProc(TIFF *tif) { return (tif->tif_seekproc); } |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
TIFFCloseProc TIFFGetCloseProc(TIFF *tif) { return (tif->tif_closeproc); } |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
TIFFSizeProc TIFFGetSizeProc(TIFF *tif) { return (tif->tif_sizeproc); } |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
TIFFMapFileProc TIFFGetMapFileProc(TIFF *tif) { return (tif->tif_mapproc); } |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
TIFFUnmapFileProc TIFFGetUnmapFileProc(TIFF *tif) |
| 950 |
{ |
| 951 |
return (tif->tif_unmapproc); |
| 952 |
} |