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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
/*
* Copyright (c) 1995-1997 Sam Leffler
* Copyright (c) 1995-1997 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include "tiffiop.h"
#ifdef ZIP_SUPPORT
/*
* TIFF Library.
*
* ZIP (aka Deflate) Compression Support
*
* This file is an interface to the zlib library written by
* Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
* of the library.
*
* Optionally, libdeflate (https://github.com/ebiggers/libdeflate) may be used
* to do the compression and decompression, but only for whole strips and tiles.
* For scanline access, zlib will be sued as a fallback.
*/
#include "tif_predict.h"
#include "zlib.h"
#if LIBDEFLATE_SUPPORT
#include "libdeflate.h"
#endif
#define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
#include <stdio.h>
/*
* Sigh, ZLIB_VERSION is defined as a string so there's no
* way to do a proper check here. Instead we guess based
* on the presence of #defines that were added between the
* 0.95 and 1.0 distributions.
*/
#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
#error "Antiquated ZLIB software; you must use version 1.0 or later"
#endif
#define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
/*
* State block for each open TIFF
* file using ZIP compression/decompression.
*/
typedef struct
{
TIFFPredictorState predict;
z_stream stream;
int read_error; /* whether a read error has occurred, and which should cause
further reads in the same strip/tile to be aborted */
int zipquality; /* compression level */
int state; /* state flags */
int subcodec; /* DEFLATE_SUBCODEC_ZLIB or DEFLATE_SUBCODEC_LIBDEFLATE */
#if LIBDEFLATE_SUPPORT
int libdeflate_state; /* -1 = until first time ZIPEncode() / ZIPDecode() is
called, 0 = use zlib, 1 = use libdeflate */
struct libdeflate_decompressor *libdeflate_dec;
struct libdeflate_compressor *libdeflate_enc;
#endif
#define ZSTATE_INIT_DECODE 0x01
#define ZSTATE_INIT_ENCODE 0x02
TIFFVGetMethod vgetparent; /* super-class method */
TIFFVSetMethod vsetparent; /* super-class method */
} ZIPState;
#define GetZIPState(tif) ((ZIPState *)(tif)->tif_data)
#define ZIPDecoderState(tif) GetZIPState(tif)
#define ZIPEncoderState(tif) GetZIPState(tif)
static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
static int ZIPFixupTags(TIFF *tif)
{
(void)tif;
return (1);
}
static int ZIPSetupDecode(TIFF *tif)
{
static const char module[] = "ZIPSetupDecode";
ZIPState *sp = ZIPDecoderState(tif);
assert(sp != NULL);
/* if we were last encoding, terminate this mode */
if (sp->state & ZSTATE_INIT_ENCODE)
{
deflateEnd(&sp->stream);
sp->state = 0;
}
/* This function can possibly be called several times by */
/* PredictorSetupDecode() if this function succeeds but */
/* PredictorSetup() fails */
if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
inflateInit(&sp->stream) != Z_OK)
{
TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
return (0);
}
else
{
sp->state |= ZSTATE_INIT_DECODE;
return (1);
}
}
/*
* Setup state for decoding a strip.
*/
static int ZIPPreDecode(TIFF *tif, uint16_t s)
{
ZIPState *sp = ZIPDecoderState(tif);
(void)s;
assert(sp != NULL);
if ((sp->state & ZSTATE_INIT_DECODE) == 0)
tif->tif_setupdecode(tif);
#if LIBDEFLATE_SUPPORT
sp->libdeflate_state = -1;
#endif
sp->stream.next_in = tif->tif_rawdata;
assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
appropriately even before we simplify it */
sp->stream.avail_in = (uint64_t)tif->tif_rawcc < 0xFFFFFFFFU
? (uInt)tif->tif_rawcc
: 0xFFFFFFFFU;
if (inflateReset(&sp->stream) == Z_OK)
{
sp->read_error = 0;
return 1;
}
return 0;
}
static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
{
static const char module[] = "ZIPDecode";
ZIPState *sp = ZIPDecoderState(tif);
(void)s;
assert(sp != NULL);
assert(sp->state == ZSTATE_INIT_DECODE);
if (sp->read_error)
{
memset(op, 0, (size_t)occ);
TIFFErrorExtR(tif, module,
"ZIPDecode: Scanline %" PRIu32 " cannot be read due to "
"previous error",
tif->tif_row);
return 0;
}
#if LIBDEFLATE_SUPPORT
if (sp->libdeflate_state == 1)
return 0;
/* If we have libdeflate support and we are asked to read a whole */
/* strip/tile, then go for using it */
do
{
TIFFDirectory *td = &tif->tif_dir;
if (sp->libdeflate_state == 0)
break;
if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
break;
/* Check if we are in the situation where we can use libdeflate */
if (isTiled(tif))
{
if (TIFFTileSize64(tif) != (uint64_t)occ)
break;
}
else
{
uint32_t strip_height = td->td_imagelength - tif->tif_row;
if (strip_height > td->td_rowsperstrip)
strip_height = td->td_rowsperstrip;
if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ)
break;
}
/* Check for overflow */
if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc)
break;
if ((size_t)occ != (uint64_t)occ)
break;
/* Go for decompression using libdeflate */
{
enum libdeflate_result res;
if (sp->libdeflate_dec == NULL)
{
sp->libdeflate_dec = libdeflate_alloc_decompressor();
if (sp->libdeflate_dec == NULL)
{
break;
}
}
sp->libdeflate_state = 1;
res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp,
(size_t)tif->tif_rawcc, op,
(size_t)occ, NULL);
tif->tif_rawcp += tif->tif_rawcc;
tif->tif_rawcc = 0;
/* We accept LIBDEFLATE_INSUFFICIENT_SPACE has a return */
/* There are odd files in the wild where the last strip, when */
/* it is smaller in height than td_rowsperstrip, actually contains
*/
/* data for td_rowsperstrip lines. Just ignore that silently. */
if (res != LIBDEFLATE_SUCCESS &&
res != LIBDEFLATE_INSUFFICIENT_SPACE)
{
memset(op, 0, (size_t)occ);
TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
(unsigned long)tif->tif_row);
sp->read_error = 1;
return 0;
}
return 1;
}
} while (0);
sp->libdeflate_state = 0;
#endif /* LIBDEFLATE_SUPPORT */
sp->stream.next_in = tif->tif_rawcp;
sp->stream.next_out = op;
assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
appropriately even before we simplify it */
do
{
int state;
uInt avail_in_before = (uint64_t)tif->tif_rawcc <= 0xFFFFFFFFU
? (uInt)tif->tif_rawcc
: 0xFFFFFFFFU;
uInt avail_out_before =
(uint64_t)occ < 0xFFFFFFFFU ? (uInt)occ : 0xFFFFFFFFU;
sp->stream.avail_in = avail_in_before;
sp->stream.avail_out = avail_out_before;
/* coverity[overrun-buffer-arg] */
state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
occ -= (avail_out_before - sp->stream.avail_out);
if (state == Z_STREAM_END)
break;
if (state == Z_DATA_ERROR)
{
/* coverity[overrun-buffer-arg:SUPPRESS] */
memset(sp->stream.next_out, 0, sp->stream.avail_out);
TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s",
(unsigned long)tif->tif_row, SAFE_MSG(sp));
sp->read_error = 1;
return (0);
}
if (state != Z_OK)
{
memset(sp->stream.next_out, 0, sp->stream.avail_out);
TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
sp->read_error = 1;
return (0);
}
} while (occ > 0);
if (occ != 0)
{
TIFFErrorExtR(tif, module,
"Not enough data at scanline %lu (short %" PRIu64
" bytes)",
(unsigned long)tif->tif_row, (uint64_t)occ);
memset(sp->stream.next_out, 0, sp->stream.avail_out);
sp->read_error = 1;
return (0);
}
tif->tif_rawcp = sp->stream.next_in;
return (1);
}
static int ZIPSetupEncode(TIFF *tif)
{
static const char module[] = "ZIPSetupEncode";
ZIPState *sp = ZIPEncoderState(tif);
int cappedQuality;
assert(sp != NULL);
if (sp->state & ZSTATE_INIT_DECODE)
{
inflateEnd(&sp->stream);
sp->state = 0;
}
cappedQuality = sp->zipquality;
if (cappedQuality > Z_BEST_COMPRESSION)
cappedQuality = Z_BEST_COMPRESSION;
if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
{
TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
return (0);
}
else
{
sp->state |= ZSTATE_INIT_ENCODE;
return (1);
}
}
/*
* Reset encoding state at the start of a strip.
*/
static int ZIPPreEncode(TIFF *tif, uint16_t s)
{
ZIPState *sp = ZIPEncoderState(tif);
(void)s;
assert(sp != NULL);
if (sp->state != ZSTATE_INIT_ENCODE)
tif->tif_setupencode(tif);
#if LIBDEFLATE_SUPPORT
sp->libdeflate_state = -1;
#endif
sp->stream.next_out = tif->tif_rawdata;
assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
appropriately even before we simplify it */
sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
? (uInt)tif->tif_rawdatasize
: 0xFFFFFFFFU;
return (deflateReset(&sp->stream) == Z_OK);
}
/*
* Encode a chunk of pixels.
*/
static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
{
static const char module[] = "ZIPEncode";
ZIPState *sp = ZIPEncoderState(tif);
assert(sp != NULL);
assert(sp->state == ZSTATE_INIT_ENCODE);
(void)s;
#if LIBDEFLATE_SUPPORT
if (sp->libdeflate_state == 1)
return 0;
/* If we have libdeflate support and we are asked to write a whole */
/* strip/tile, then go for using it */
do
{
TIFFDirectory *td = &tif->tif_dir;
if (sp->libdeflate_state == 0)
break;
if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
break;
/* Libdeflate does not support the 0-compression level */
if (sp->zipquality == Z_NO_COMPRESSION)
break;
/* Check if we are in the situation where we can use libdeflate */
if (isTiled(tif))
{
if (TIFFTileSize64(tif) != (uint64_t)cc)
break;
}
else
{
uint32_t strip_height = td->td_imagelength - tif->tif_row;
if (strip_height > td->td_rowsperstrip)
strip_height = td->td_rowsperstrip;
if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc)
break;
}
/* Check for overflow */
if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize)
break;
if ((size_t)cc != (uint64_t)cc)
break;
/* Go for compression using libdeflate */
{
size_t nCompressedBytes;
if (sp->libdeflate_enc == NULL)
{
/* To get results as good as zlib, we asked for an extra */
/* level of compression */
sp->libdeflate_enc = libdeflate_alloc_compressor(
sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
: sp->zipquality >= 6 && sp->zipquality <= 9
? sp->zipquality + 1
: sp->zipquality);
if (sp->libdeflate_enc == NULL)
{
TIFFErrorExtR(tif, module, "Cannot allocate compressor");
break;
}
}
/* Make sure the output buffer is large enough for the worse case.
*/
/* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
/* we've taken a 10% margin over the uncompressed size, which should
*/
/* be large enough even for the the worse case scenario. */
if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
(size_t)tif->tif_rawdatasize)
{
break;
}
sp->libdeflate_state = 1;
nCompressedBytes = libdeflate_zlib_compress(
sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata,
(size_t)tif->tif_rawdatasize);
if (nCompressedBytes == 0)
{
TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
(unsigned long)tif->tif_row);
return 0;
}
tif->tif_rawcc = nCompressedBytes;
if (!TIFFFlushData1(tif))
return 0;
return 1;
}
} while (0);
sp->libdeflate_state = 0;
#endif /* LIBDEFLATE_SUPPORT */
sp->stream.next_in = bp;
assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
we need to simplify this code to reflect a ZLib that is likely updated
to deal with 8byte memory sizes, though this code will respond
appropriately even before we simplify it */
do
{
uInt avail_in_before =
(uint64_t)cc <= 0xFFFFFFFFU ? (uInt)cc : 0xFFFFFFFFU;
sp->stream.avail_in = avail_in_before;
/* coverity[overrun-buffer-arg] */
if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
{
TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp));
return (0);
}
if (sp->stream.avail_out == 0)
{
tif->tif_rawcc = tif->tif_rawdatasize;
if (!TIFFFlushData1(tif))
return 0;
sp->stream.next_out = tif->tif_rawdata;
sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
? (uInt)tif->tif_rawdatasize
: 0xFFFFFFFFU;
}
cc -= (avail_in_before - sp->stream.avail_in);
} while (cc > 0);
return (1);
}
/*
* Finish off an encoded strip by flushing the last
* string and tacking on an End Of Information code.
*/
static int ZIPPostEncode(TIFF *tif)
{
static const char module[] = "ZIPPostEncode";
ZIPState *sp = ZIPEncoderState(tif);
int state;
#if LIBDEFLATE_SUPPORT
if (sp->libdeflate_state == 1)
return 1;
#endif
sp->stream.avail_in = 0;
do
{
state = deflate(&sp->stream, Z_FINISH);
switch (state)
{
case Z_STREAM_END:
case Z_OK:
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
{
tif->tif_rawcc =
tif->tif_rawdatasize - sp->stream.avail_out;
if (!TIFFFlushData1(tif))
return 0;
sp->stream.next_out = tif->tif_rawdata;
sp->stream.avail_out =
(uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
? (uInt)tif->tif_rawdatasize
: 0xFFFFFFFFU;
}
break;
default:
TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
return (0);
}
} while (state != Z_STREAM_END);
return (1);
}
static void ZIPCleanup(TIFF *tif)
{
ZIPState *sp = GetZIPState(tif);
assert(sp != 0);
(void)TIFFPredictorCleanup(tif);
tif->tif_tagmethods.vgetfield = sp->vgetparent;
tif->tif_tagmethods.vsetfield = sp->vsetparent;
if (sp->state & ZSTATE_INIT_ENCODE)
{
deflateEnd(&sp->stream);
sp->state = 0;
}
else if (sp->state & ZSTATE_INIT_DECODE)
{
inflateEnd(&sp->stream);
sp->state = 0;
}
#if LIBDEFLATE_SUPPORT
if (sp->libdeflate_dec)
libdeflate_free_decompressor(sp->libdeflate_dec);
if (sp->libdeflate_enc)
libdeflate_free_compressor(sp->libdeflate_enc);
#endif
_TIFFfreeExt(tif, sp);
tif->tif_data = NULL;
_TIFFSetDefaultCompressionState(tif);
}
static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
{
static const char module[] = "ZIPVSetField";
ZIPState *sp = GetZIPState(tif);
switch (tag)
{
case TIFFTAG_ZIPQUALITY:
sp->zipquality = (int)va_arg(ap, int);
if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
{
TIFFErrorExtR(
tif, module,
"Invalid ZipQuality value. Should be in [-1,%d] range",
LIBDEFLATE_MAX_COMPRESSION_LEVEL);
return 0;
}
if (sp->state & ZSTATE_INIT_ENCODE)
{
int cappedQuality = sp->zipquality;
if (cappedQuality > Z_BEST_COMPRESSION)
cappedQuality = Z_BEST_COMPRESSION;
if (deflateParams(&sp->stream, cappedQuality,
Z_DEFAULT_STRATEGY) != Z_OK)
{
TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
return (0);
}
}
#if LIBDEFLATE_SUPPORT
if (sp->libdeflate_enc)
{
libdeflate_free_compressor(sp->libdeflate_enc);
sp->libdeflate_enc = NULL;
}
#endif
return (1);
case TIFFTAG_DEFLATE_SUBCODEC:
sp->subcodec = (int)va_arg(ap, int);
if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE)
{
TIFFErrorExtR(tif, module, "Invalid DeflateCodec value.");
return 0;
}
#if !LIBDEFLATE_SUPPORT
if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE)
{
TIFFErrorExtR(tif, module,
"DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE "
"unsupported in this build");
return 0;
}
#endif
return 1;
default:
return (*sp->vsetparent)(tif, tag, ap);
}
/*NOTREACHED*/
}
static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
{
ZIPState *sp = GetZIPState(tif);
switch (tag)
{
case TIFFTAG_ZIPQUALITY:
*va_arg(ap, int *) = sp->zipquality;
break;
case TIFFTAG_DEFLATE_SUBCODEC:
*va_arg(ap, int *) = sp->subcodec;
break;
default:
return (*sp->vgetparent)(tif, tag, ap);
}
return (1);
}
static const TIFFField zipFields[] = {
{TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE,
FALSE, "", NULL},
{TIFFTAG_DEFLATE_SUBCODEC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO,
TRUE, FALSE, "", NULL},
};
static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
{
static const char module[] = "TIFF_zalloc";
TIFF *tif = opaque;
if (items > ~(size_t)0 / size)
{
TIFFErrorExtR(tif, module, "Overflow");
return NULL;
}
return _TIFFmallocExt(tif, items * size);
}
static void TIFF_zfree(void *opaque, void *ptr)
{
_TIFFfreeExt((TIFF *)opaque, ptr);
}
int TIFFInitZIP(TIFF *tif, int scheme)
{
static const char module[] = "TIFFInitZIP";
ZIPState *sp;
assert((scheme == COMPRESSION_DEFLATE) ||
(scheme == COMPRESSION_ADOBE_DEFLATE));
#ifdef NDEBUG
(void)scheme;
#endif
/*
* Merge codec-specific tag information.
*/
if (!_TIFFMergeFields(tif, zipFields, TIFFArrayCount(zipFields)))
{
TIFFErrorExtR(tif, module,
"Merging Deflate codec-specific tags failed");
return 0;
}
/*
* Allocate state block so tag methods have storage to record values.
*/
tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, sizeof(ZIPState), 1);
if (tif->tif_data == NULL)
goto bad;
sp = GetZIPState(tif);
sp->stream.zalloc = TIFF_zalloc;
sp->stream.zfree = TIFF_zfree;
sp->stream.opaque = tif;
sp->stream.data_type = Z_BINARY;
/*
* Override parent get/set field methods.
*/
sp->vgetparent = tif->tif_tagmethods.vgetfield;
tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
sp->vsetparent = tif->tif_tagmethods.vsetfield;
tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
/* Default values for codec-specific fields */
sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
sp->state = 0;
#if LIBDEFLATE_SUPPORT
sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
#else
sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
#endif
/*
* Install codec methods.
*/
tif->tif_fixuptags = ZIPFixupTags;
tif->tif_setupdecode = ZIPSetupDecode;
tif->tif_predecode = ZIPPreDecode;
tif->tif_decoderow = ZIPDecode;
tif->tif_decodestrip = ZIPDecode;
tif->tif_decodetile = ZIPDecode;
tif->tif_setupencode = ZIPSetupEncode;
tif->tif_preencode = ZIPPreEncode;
tif->tif_postencode = ZIPPostEncode;
tif->tif_encoderow = ZIPEncode;
tif->tif_encodestrip = ZIPEncode;
tif->tif_encodetile = ZIPEncode;
tif->tif_cleanup = ZIPCleanup;
/*
* Setup predictor setup.
*/
(void)TIFFPredictorInit(tif);
return (1);
bad:
TIFFErrorExtR(tif, module, "No space for ZIP state block");
return (0);
}
#endif /* ZIP_SUPPORT */
|