LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_zip.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1995-1997 Sam Leffler
3 * Copyright (c) 1995-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
30#include "tiffiop.h"
31#ifdef ZIP_SUPPORT
32/*
33 * TIFF Library.
34 *
35 * ZIP (aka Deflate) Compression Support
36 *
37 * This file is an interface to the zlib library written by
38 * Jean-loup Gailly and Mark Adler. You must use version 1.0 or later
39 * of the library.
40 *
41 * Optionally, libdeflate (https://github.com/ebiggers/libdeflate) may be used
42 * to do the compression and decompression, but only for whole strips and tiles.
43 * For scanline access, zlib will be sued as a fallback.
44 */
45#include "tif_predict.h"
46#include "zlib.h"
47
48#if LIBDEFLATE_SUPPORT
49#include "libdeflate.h"
50#endif
51#define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
52
53#include <stdio.h>
54
55/*
56 * Sigh, ZLIB_VERSION is defined as a string so there's no
57 * way to do a proper check here. Instead we guess based
58 * on the presence of #defines that were added between the
59 * 0.95 and 1.0 distributions.
60 */
61#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
62#error "Antiquated ZLIB software; you must use version 1.0 or later"
63#endif
64
65#define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
66
67/*
68 * State block for each open TIFF
69 * file using ZIP compression/decompression.
70 */
79typedef struct
80{
87 z_stream stream;
96 int state;
102#if LIBDEFLATE_SUPPORT
107 int libdeflate_state;
112 struct libdeflate_decompressor *libdeflate_dec;
117 struct libdeflate_compressor *libdeflate_enc;
118#endif
119#define ZSTATE_INIT_DECODE 0x01
120#define ZSTATE_INIT_ENCODE 0x02
121
126} ZIPState;
127
128#define GetZIPState(tif) ((ZIPState *)(tif)->tif_data)
129#define ZIPDecoderState(tif) GetZIPState(tif)
130#define ZIPEncoderState(tif) GetZIPState(tif)
131
132static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
133static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
134
144static int ZIPFixupTags(TIFF *tif)
145{
146 (void)tif;
147 return (1);
148}
149
163static int ZIPSetupDecode(TIFF *tif)
164{
165 static const char module[] = "ZIPSetupDecode";
166 ZIPState *sp = ZIPDecoderState(tif);
167
168 assert(sp != NULL);
169
170 /* if we were last encoding, terminate this mode */
171 if (sp->state & ZSTATE_INIT_ENCODE)
172 {
173 deflateEnd(&sp->stream);
174 sp->state = 0;
175 }
176
177 /* This function can possibly be called several times by */
178 /* PredictorSetupDecode() if this function succeeds but */
179 /* PredictorSetup() fails */
180 if ((sp->state & ZSTATE_INIT_DECODE) == 0 &&
181 inflateInit(&sp->stream) != Z_OK)
182 {
183 TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
184 return (0);
185 }
186 else
187 {
188 sp->state |= ZSTATE_INIT_DECODE;
189 return (1);
190 }
191}
192
204static inline uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b)
205{
206 return a < b ? a : b;
207}
208
219static inline uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v)
220{
221 return (uInt)TIFF_MIN_UINT64(v, INT32_MAX);
222}
223
224/*
225 * Setup state for decoding a strip.
226 */
243static int ZIPPreDecode(TIFF *tif, uint16_t s)
244{
245 ZIPState *sp = ZIPDecoderState(tif);
246
247 (void)s;
248 assert(sp != NULL);
249
250 if ((sp->state & ZSTATE_INIT_DECODE) == 0)
251 tif->tif_setupdecode(tif);
252
253#if LIBDEFLATE_SUPPORT
254 sp->libdeflate_state = -1;
255#endif
256 sp->stream.next_in = tif->tif_rawdata;
257 assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
258 we need to simplify this code to reflect a ZLib that is likely updated
259 to deal with 8byte memory sizes, though this code will respond
260 appropriately even before we simplify it */
261 sp->stream.avail_in =
263 if (inflateReset(&sp->stream) == Z_OK)
264 {
265 sp->read_error = 0;
266 return 1;
267 }
268 return 0;
269}
270
299static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
300{
301 static const char module[] = "ZIPDecode";
302 ZIPState *sp = ZIPDecoderState(tif);
303
304 (void)s;
305 assert(sp != NULL);
306 assert(sp->state == ZSTATE_INIT_DECODE);
307
308 if (sp->read_error)
309 {
310 memset(op, 0, (size_t)occ);
311 TIFFErrorExtR(tif, module,
312 "ZIPDecode: Scanline %" PRIu32 " cannot be read due to "
313 "previous error",
314 tif->tif_dir.td_row);
315 return 0;
316 }
317
318#if LIBDEFLATE_SUPPORT
319 if (sp->libdeflate_state == 1)
320 return 0;
321
322 /* If we have libdeflate support and we are asked to read a whole */
323 /* strip/tile, then go for using it */
324 do
325 {
326 TIFFDirectory *td = &tif->tif_dir;
327
328 if (sp->libdeflate_state == 0)
329 break;
330 if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
331 break;
332
333 /* Check if we are in the situation where we can use libdeflate */
334 if (isTiled(tif))
335 {
336 if (TIFFTileSize64(tif) != (uint64_t)occ)
337 break;
338 }
339 else
340 {
341 uint32_t strip_height = td->td_imagelength - tif->tif_dir.td_row;
342 if (strip_height > td->td_rowsperstrip)
343 strip_height = td->td_rowsperstrip;
344 if (TIFFVStripSize64(tif, strip_height) != (uint64_t)occ)
345 break;
346 }
347
348 /* Check for overflow */
349 if ((size_t)tif->tif_rawcc != (uint64_t)tif->tif_rawcc)
350 break;
351 if ((size_t)occ != (uint64_t)occ)
352 break;
353
354 /* Go for decompression using libdeflate */
355 {
356 enum libdeflate_result res;
357 if (sp->libdeflate_dec == NULL)
358 {
359 sp->libdeflate_dec = libdeflate_alloc_decompressor();
360 if (sp->libdeflate_dec == NULL)
361 {
362 break;
363 }
364 }
365
366 sp->libdeflate_state = 1;
367
368 res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->tif_rawcp,
369 (size_t)tif->tif_rawcc, op,
370 (size_t)occ, NULL);
371
372 if (res == LIBDEFLATE_SUCCESS)
373 {
374 tif->tif_rawcp += tif->tif_rawcc;
375 tif->tif_rawcc = 0;
376 return 1;
377 }
378
379 /* There are odd files in the wild where the last strip, when it
380 * is smaller in height than td_rowsperstrip, actually contains
381 * data for td_rowsperstrip lines. libdeflate reports them as
382 * LIBDEFLATE_INSUFFICIENT_SPACE and leaves the output buffer
383 * undefined (stored blocks are not copied at all), so decode them
384 * again with zlib, which stops at occ and ignores the extra data.
385 */
386 if (res == LIBDEFLATE_INSUFFICIENT_SPACE)
387 break;
388
389 memset(op, 0, (size_t)occ);
390 TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
391 (unsigned long)tif->tif_dir.td_row);
392 sp->read_error = 1;
393 return 0;
394 }
395 } while (0);
396 sp->libdeflate_state = 0;
397#endif /* LIBDEFLATE_SUPPORT */
398
399 sp->stream.next_in = tif->tif_rawcp;
400
401 sp->stream.next_out = op;
402 assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
403 we need to simplify this code to reflect a ZLib that is likely updated
404 to deal with 8byte memory sizes, though this code will respond
405 appropriately even before we simplify it */
406 do
407 {
408 int state;
409 uInt avail_in_before =
411 uInt avail_out_before = TIFF_CLAMP_UINT64_TO_INT32_MAX((uint64_t)occ);
412 sp->stream.avail_in = avail_in_before;
413 sp->stream.avail_out = avail_out_before;
414 state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
415 tif->tif_rawcc -= (avail_in_before - sp->stream.avail_in);
416 occ -= (avail_out_before - sp->stream.avail_out);
417 if (state == Z_STREAM_END)
418 break;
419 if (state == Z_DATA_ERROR)
420 {
421 memset(sp->stream.next_out, 0, (size_t)occ);
422 TIFFErrorExtR(tif, module, "Decoding error at scanline %lu, %s",
423 (unsigned long)tif->tif_dir.td_row, SAFE_MSG(sp));
424 sp->read_error = 1;
425 return (0);
426 }
427 if (state != Z_OK)
428 {
429 memset(sp->stream.next_out, 0, (size_t)occ);
430 TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
431 sp->read_error = 1;
432 return (0);
433 }
434 } while (occ > 0);
435 if (occ != 0)
436 {
437 TIFFErrorExtR(tif, module,
438 "Not enough data at scanline %lu (short %" PRIu64
439 " bytes)",
440 (unsigned long)tif->tif_dir.td_row, (uint64_t)occ);
441 memset(sp->stream.next_out, 0, (size_t)occ);
442 sp->read_error = 1;
443 return (0);
444 }
445
446 tif->tif_rawcp = sp->stream.next_in;
447
448 return (1);
449}
450
464static int ZIPSetupEncode(TIFF *tif)
465{
466 static const char module[] = "ZIPSetupEncode";
467 ZIPState *sp = ZIPEncoderState(tif);
468 int cappedQuality;
469
470 assert(sp != NULL);
471 if (sp->state & ZSTATE_INIT_DECODE)
472 {
473 inflateEnd(&sp->stream);
474 sp->state = 0;
475 }
476
477 cappedQuality = sp->zipquality;
478 if (cappedQuality > Z_BEST_COMPRESSION)
479 cappedQuality = Z_BEST_COMPRESSION;
480
481 if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
482 {
483 TIFFErrorExtR(tif, module, "%s", SAFE_MSG(sp));
484 return (0);
485 }
486 else
487 {
488 sp->state |= ZSTATE_INIT_ENCODE;
489 return (1);
490 }
491}
492
493/*
494 * Reset encoding state at the start of a strip.
495 */
510static int ZIPPreEncode(TIFF *tif, uint16_t s)
511{
512 ZIPState *sp = ZIPEncoderState(tif);
513
514 (void)s;
515 assert(sp != NULL);
516 if (sp->state != ZSTATE_INIT_ENCODE)
517 tif->tif_setupencode(tif);
518
519#if LIBDEFLATE_SUPPORT
520 sp->libdeflate_state = -1;
521#endif
522 sp->stream.next_out = tif->tif_rawdata;
523 assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
524 we need to simplify this code to reflect a ZLib that is likely updated
525 to deal with 8byte memory sizes, though this code will respond
526 appropriately even before we simplify it */
527 sp->stream.avail_out = (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
528 ? (uInt)tif->tif_rawdatasize
529 : 0xFFFFFFFFU;
530 return (deflateReset(&sp->stream) == Z_OK);
531}
532
533/*
534 * Encode a chunk of pixels.
535 */
562static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
563{
564 static const char module[] = "ZIPEncode";
565 ZIPState *sp = ZIPEncoderState(tif);
566
567 assert(sp != NULL);
568 if (sp->state != ZSTATE_INIT_ENCODE)
569 {
570 TIFFErrorExtR(tif, module,
571 "ZIP codec state is not initialized for encoding");
572 return 0;
573 }
574
575 (void)s;
576
577#if LIBDEFLATE_SUPPORT
578 if (sp->libdeflate_state == 1)
579 return 0;
580
581 /* If we have libdeflate support and we are asked to write a whole */
582 /* strip/tile, then go for using it */
583 do
584 {
585 TIFFDirectory *td = &tif->tif_dir;
586
587 if (sp->libdeflate_state == 0)
588 break;
589 if (sp->subcodec == DEFLATE_SUBCODEC_ZLIB)
590 break;
591
592 /* Libdeflate does not support the 0-compression level */
593 if (sp->zipquality == Z_NO_COMPRESSION)
594 break;
595
596 /* Check if we are in the situation where we can use libdeflate */
597 if (isTiled(tif))
598 {
599 if (TIFFTileSize64(tif) != (uint64_t)cc)
600 break;
601 }
602 else
603 {
604 uint32_t strip_height = td->td_imagelength - tif->tif_dir.td_row;
605 if (strip_height > td->td_rowsperstrip)
606 strip_height = td->td_rowsperstrip;
607 if (TIFFVStripSize64(tif, strip_height) != (uint64_t)cc)
608 break;
609 }
610
611 /* Check for overflow */
612 if ((size_t)tif->tif_rawdatasize != (uint64_t)tif->tif_rawdatasize)
613 break;
614 if ((size_t)cc != (uint64_t)cc)
615 break;
616
617 /* Go for compression using libdeflate */
618 {
619 size_t nCompressedBytes;
620 if (sp->libdeflate_enc == NULL)
621 {
622 /* To get results as good as zlib, we asked for an extra */
623 /* level of compression */
624 sp->libdeflate_enc = libdeflate_alloc_compressor(
625 sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
626 : sp->zipquality >= 6 && sp->zipquality <= 9
627 ? sp->zipquality + 1
628 : sp->zipquality);
629 if (sp->libdeflate_enc == NULL)
630 {
631 TIFFErrorExtR(tif, module, "Cannot allocate compressor");
632 break;
633 }
634 }
635
636 /* Make sure the output buffer is large enough for the worse case.
637 */
638 /* In TIFFWriteBufferSetup(), when libtiff allocates the buffer */
639 /* we've taken a 10% margin over the uncompressed size, which should
640 */
641 /* be large enough even for the the worse case scenario. */
642 if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (size_t)cc) >
643 (size_t)tif->tif_rawdatasize)
644 {
645 break;
646 }
647
648 sp->libdeflate_state = 1;
649 nCompressedBytes = libdeflate_zlib_compress(
650 sp->libdeflate_enc, bp, (size_t)cc, tif->tif_rawdata,
651 (size_t)tif->tif_rawdatasize);
652
653 if (nCompressedBytes == 0)
654 {
655 TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
656 (unsigned long)tif->tif_dir.td_row);
657 return 0;
658 }
659
660 tif->tif_rawcc = (tmsize_t)nCompressedBytes;
661
662 if (!TIFFFlushData1(tif))
663 return 0;
664
665 return 1;
666 }
667 } while (0);
668 sp->libdeflate_state = 0;
669#endif /* LIBDEFLATE_SUPPORT */
670
671 sp->stream.next_in = bp;
672 assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
673 we need to simplify this code to reflect a ZLib that is likely updated
674 to deal with 8byte memory sizes, though this code will respond
675 appropriately even before we simplify it */
676 do
677 {
678 uInt avail_in_before = TIFF_CLAMP_UINT64_TO_INT32_MAX((uint64_t)cc);
679 sp->stream.avail_in = avail_in_before;
680 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
681 {
682 TIFFErrorExtR(tif, module, "Encoder error: %s", SAFE_MSG(sp));
683 return (0);
684 }
685 if (sp->stream.avail_out == 0)
686 {
687 tif->tif_rawcc = tif->tif_rawdatasize;
688 if (!TIFFFlushData1(tif))
689 return 0;
690 sp->stream.next_out = tif->tif_rawdata;
691 sp->stream.avail_out =
693 }
694 cc -= (avail_in_before - sp->stream.avail_in);
695 } while (cc > 0);
696 return (1);
697}
698
699/*
700 * Finish off an encoded strip by flushing the last
701 * string and tacking on an End Of Information code.
702 */
719static int ZIPPostEncode(TIFF *tif)
720{
721 static const char module[] = "ZIPPostEncode";
722 ZIPState *sp = ZIPEncoderState(tif);
723 int state;
724
725 if (sp->state != ZSTATE_INIT_ENCODE)
726 {
727 TIFFErrorExtR(tif, module,
728 "ZIP codec state is not initialized for encoding");
729 return 0;
730 }
731
732#if LIBDEFLATE_SUPPORT
733 if (sp->libdeflate_state == 1)
734 return 1;
735#endif
736
737 sp->stream.avail_in = 0;
738 do
739 {
740 state = deflate(&sp->stream, Z_FINISH);
741 switch (state)
742 {
743 case Z_STREAM_END:
744 case Z_OK:
745 if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
746 {
747 tif->tif_rawcc =
748 tif->tif_rawdatasize - sp->stream.avail_out;
749 if (!TIFFFlushData1(tif))
750 return 0;
751 sp->stream.next_out = tif->tif_rawdata;
752 sp->stream.avail_out =
753 (uint64_t)tif->tif_rawdatasize <= 0xFFFFFFFFU
754 ? (uInt)tif->tif_rawdatasize
755 : 0xFFFFFFFFU;
756 }
757 break;
758 default:
759 TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
760 return (0);
761 }
762 } while (state != Z_STREAM_END);
763 return (1);
764}
765
779static void ZIPCleanup(TIFF *tif)
780{
781 ZIPState *sp = GetZIPState(tif);
782
783 assert(sp != 0);
784
785 (void)TIFFPredictorCleanup(tif);
786
789
790 if (sp->state & ZSTATE_INIT_ENCODE)
791 {
792 deflateEnd(&sp->stream);
793 sp->state = 0;
794 }
795 else if (sp->state & ZSTATE_INIT_DECODE)
796 {
797 inflateEnd(&sp->stream);
798 sp->state = 0;
799 }
800
801#if LIBDEFLATE_SUPPORT
802 if (sp->libdeflate_dec)
803 libdeflate_free_decompressor(sp->libdeflate_dec);
804 if (sp->libdeflate_enc)
805 libdeflate_free_compressor(sp->libdeflate_enc);
806#endif
807
808 _TIFFfreeExt(tif, sp);
809 tif->tif_data = NULL;
810
812}
813
834static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
835{
836 static const char module[] = "ZIPVSetField";
837 ZIPState *sp = GetZIPState(tif);
838
839 switch (tag)
840 {
842 sp->zipquality = (int)va_arg(ap, int);
843 if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
844 sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
845 {
847 tif, module,
848 "Invalid ZipQuality value. Should be in [-1,%d] range",
850 return 0;
851 }
852
853 if (sp->state & ZSTATE_INIT_ENCODE)
854 {
855 int cappedQuality = sp->zipquality;
856 if (cappedQuality > Z_BEST_COMPRESSION)
857 cappedQuality = Z_BEST_COMPRESSION;
858 if (deflateParams(&sp->stream, cappedQuality,
859 Z_DEFAULT_STRATEGY) != Z_OK)
860 {
861 TIFFErrorExtR(tif, module, "ZLib error: %s", SAFE_MSG(sp));
862 return (0);
863 }
864 }
865
866#if LIBDEFLATE_SUPPORT
867 if (sp->libdeflate_enc)
868 {
869 libdeflate_free_compressor(sp->libdeflate_enc);
870 sp->libdeflate_enc = NULL;
871 }
872#endif
873
874 return (1);
875
877 sp->subcodec = (int)va_arg(ap, int);
878 if (sp->subcodec != DEFLATE_SUBCODEC_ZLIB &&
879 sp->subcodec != DEFLATE_SUBCODEC_LIBDEFLATE)
880 {
881 TIFFErrorExtR(tif, module, "Invalid DeflateCodec value.");
882 return 0;
883 }
884#if !LIBDEFLATE_SUPPORT
885 if (sp->subcodec == DEFLATE_SUBCODEC_LIBDEFLATE)
886 {
887 TIFFErrorExtR(tif, module,
888 "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE "
889 "unsupported in this build");
890 return 0;
891 }
892#endif
893 return 1;
894
895 default:
896 return (*sp->vsetparent)(tif, tag, ap);
897 }
898 /*NOTREACHED*/
899}
900
916static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
917{
918 ZIPState *sp = GetZIPState(tif);
919
920 switch (tag)
921 {
923 *va_arg(ap, int *) = sp->zipquality;
924 break;
925
927 *va_arg(ap, int *) = sp->subcodec;
928 break;
929
930 default:
931 return (*sp->vgetparent)(tif, tag, ap);
932 }
933 return (1);
934}
935
943static const TIFFField zipFields[] = {
945 FALSE, "", NULL},
947 TRUE, FALSE, "", NULL},
948};
949
970static void *TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
971{
972 static const char module[] = "TIFF_zalloc";
973 TIFF *tif = (TIFF *)opaque;
974
975 if (items > ~(size_t)0 / size)
976 {
977 TIFFErrorExtR(tif, module, "Overflow");
978 return NULL;
979 }
980
981 return _TIFFmallocExt(tif, items * size);
982}
983
996static void TIFF_zfree(void *opaque, void *ptr)
997{
998 _TIFFfreeExt((TIFF *)opaque, ptr);
999}
1000
1011static uint64_t ZIPGetMaxCompressionRatio(TIFF *tif)
1012{
1013 (void)tif;
1014 /* cf https://zlib.net/zlib_tech.html */
1015 return 1032;
1016}
1017
1018int TIFFInitZIP(TIFF *tif, int scheme)
1019{
1020 static const char module[] = "TIFFInitZIP";
1021 ZIPState *sp;
1022
1023 assert((scheme == COMPRESSION_DEFLATE) ||
1024 (scheme == COMPRESSION_ADOBE_DEFLATE));
1025#ifdef NDEBUG
1026 (void)scheme;
1027#endif
1028
1029 /*
1030 * Merge codec-specific tag information.
1031 */
1033 {
1034 TIFFErrorExtR(tif, module,
1035 "Merging Deflate codec-specific tags failed");
1036 return 0;
1037 }
1038
1039 /*
1040 * Allocate state block so tag methods have storage to record values.
1041 */
1042 tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, 1, sizeof(ZIPState));
1043 if (tif->tif_data == NULL)
1044 goto bad;
1045 sp = GetZIPState(tif);
1046 sp->stream.zalloc = TIFF_zalloc;
1047 sp->stream.zfree = TIFF_zfree;
1048 sp->stream.opaque = tif;
1049 sp->stream.data_type = Z_BINARY;
1050
1051 /*
1052 * Override parent get/set field methods.
1053 */
1054 sp->vgetparent = tif->tif_tagmethods.vgetfield;
1055 tif->tif_tagmethods.vgetfield = ZIPVGetField; /* hook for codec tags */
1056 sp->vsetparent = tif->tif_tagmethods.vsetfield;
1057 tif->tif_tagmethods.vsetfield = ZIPVSetField; /* hook for codec tags */
1058
1059 /* Default values for codec-specific fields */
1060 sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
1061 sp->state = 0;
1062#if LIBDEFLATE_SUPPORT
1063 sp->subcodec = DEFLATE_SUBCODEC_LIBDEFLATE;
1064#else
1065 sp->subcodec = DEFLATE_SUBCODEC_ZLIB;
1066#endif
1067
1068 /*
1069 * Install codec methods.
1070 */
1074 tif->tif_decoderow = ZIPDecode;
1080 tif->tif_encoderow = ZIPEncode;
1083 tif->tif_cleanup = ZIPCleanup;
1085 /*
1086 * Setup predictor setup.
1087 */
1088 (void)TIFFPredictorInit(tif);
1089 return (1);
1090bad:
1091 TIFFErrorExtR(tif, module, "No space for ZIP state block");
1092 return (0);
1093}
1094#endif /* ZIP_SUPPORT */
1095
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define ZSTATE_INIT_DECODE
Bit 0x01 of the codec state field: decoding has been set up.
Definition tif_zip.c:119
static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve Deflate quality or backend or delegate a tag query.
Definition tif_zip.c:916
static void * TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
Allocate zlib internal storage through handle Ext allocation.
Definition tif_zip.c:970
#define GetZIPState(tif)
Cast tif->tif_data to the ZIPState.
Definition tif_zip.c:128
static uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v)
Bound a zlib chunk to the signed 32-bit maximum.
Definition tif_zip.c:219
static uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b)
Select the smaller of two unsigned values.
Definition tif_zip.c:204
#define ZIPEncoderState(tif)
Alias of GetZIPState(), returning the ZIPState for encoding.
Definition tif_zip.c:130
static int ZIPFixupTags(TIFF *tif)
Accept Deflate tag fixup without changes.
Definition tif_zip.c:144
static const TIFFField zipFields[]
Define compression quality and backend pseudo-tags.
Definition tif_zip.c:943
static uint64_t ZIPGetMaxCompressionRatio(TIFF *tif)
Report the Deflate expansion allocation heuristic.
Definition tif_zip.c:1011
static int ZIPPreEncode(TIFF *tif, uint16_t s)
Reset zlib encoding and per-strile backend selection.
Definition tif_zip.c:510
static int ZIPPostEncode(TIFF *tif)
Finish zlib output or accept an already written libdeflate frame.
Definition tif_zip.c:719
static int ZIPSetupEncode(TIFF *tif)
Initialise zlib encoding at a capped compression quality.
Definition tif_zip.c:464
static void TIFF_zfree(void *opaque, void *ptr)
Release zlib storage through the matching handle Ext allocator.
Definition tif_zip.c:996
int TIFFInitZIP(TIFF *tif, int scheme)
Install predictor-aware Deflate codec and allocator hooks.
Definition tif_zip.c:1018
static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
Set Deflate quality or backend or delegate another tag.
Definition tif_zip.c:834
#define SAFE_MSG(sp)
Borrow the zlib stream error text, or an empty string if none.
Definition tif_zip.c:65
static int ZIPSetupDecode(TIFF *tif)
Initialise zlib decode mode and end an active encoder.
Definition tif_zip.c:163
#define ZSTATE_INIT_ENCODE
Bit 0x02 of the codec state field: encoding has been set up.
Definition tif_zip.c:120
static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Compress input with streaming zlib or eligible whole-strile libdeflate.
Definition tif_zip.c:562
#define ZIPDecoderState(tif)
Alias of GetZIPState(), returning the ZIPState for decoding.
Definition tif_zip.c:129
static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Expand zlib or eligible whole-strile libdeflate input.
Definition tif_zip.c:299
static int ZIPPreDecode(TIFF *tif, uint16_t s)
Reset zlib decoding and per-strile backend selection.
Definition tif_zip.c:243
#define LIBDEFLATE_MAX_COMPRESSION_LEVEL
Highest accepted TIFFTAG_ZIPQUALITY value: 12.
Definition tif_zip.c:51
static void ZIPCleanup(TIFF *tif)
End Deflate dependency objects and release codec state.
Definition tif_zip.c:779
int _TIFFMergeFields(TIFF *tif, const TIFFField info[], uint32_t n)
Merge borrowed field definitions into the lookup table.
#define FIELD_PSEUDO
Share bit zero with FIELD_IGNORE for pseudo-tags.
Definition tif_dir.h:620
@ TIFF_SETGET_INT
Promoted int scalar representation.
Definition tif_dir.h:762
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
Definition tif_open.c:301
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
Definition tif_open.c:195
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array under a handle's allocation policy.
Definition tif_open.c:224
int TIFFPredictorCleanup(TIFF *tif)
Restore saved Predictor tag print and setup hooks.
int TIFFPredictorInit(TIFF *tif)
Install Predictor tag and setup hooks around a codec.
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFFArrayCount(a)
Compute the element count of an actual array.
Definition tiffiop.h:1239
#define TIFF_ANY
Match any data type when searching for a field descriptor.
Definition tiffio.h:1289
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested strip height.
Definition tif_strip.c:177
uint64_t TIFFTileSize64(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:303
#define DEFLATE_SUBCODEC_LIBDEFLATE
Select the libdeflate Deflate implementation.
Definition tiff.h:1680
#define TIFFTAG_DEFLATE_SUBCODEC
Pseudo-tag: Select the zlib or libdeflate Deflate implementation.
Definition tiff.h:1676
#define TIFFTAG_ZIPQUALITY
Pseudo-tag: Deflate compression level control.
Definition tiff.h:1610
#define DEFLATE_SUBCODEC_ZLIB
Select the zlib Deflate implementation.
Definition tiff.h:1678
int(* TIFFVGetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag getter.
Definition tiffio.h:1540
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
Definition tiffio.h:1521
#define COMPRESSION_DEFLATE
Deflate compression, legacy tag.
Definition tiff.h:349
#define COMPRESSION_ADOBE_DEFLATE
Deflate compression, as recognized by Adobe.
Definition tiff.h:351
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_rowsperstrip
Rows per strip; UINT32_MAX represents one whole-image strip.
Definition tif_dir.h:184
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
uint32_t td_imagelength
Image length in rows.
Definition tif_dir.h:156
Prefix codec state with Predictor method chains and row geometry.
Definition tif_predict.h:76
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
Definition tiffio.h:1577
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Definition tiffio.h:1575
Own Deflate dependency streams predictor prefix and backend selection.
Definition tif_zip.c:80
TIFFVGetMethod vgetparent
Borrowed parent getter restored on cleanup.
Definition tif_zip.c:123
z_stream stream
Zlib stream with internal allocations owned until inflateEnd/deflateEnd.
Definition tif_zip.c:87
int state
Active zlib mode flags.
Definition tif_zip.c:96
TIFFVSetMethod vsetparent
Borrowed parent setter restored on cleanup.
Definition tif_zip.c:125
int read_error
Same-strile decode error latch reset by successful predecode.
Definition tif_zip.c:89
int subcodec
Requested zlib or libdeflate backend, validated by the setter after assignment.
Definition tif_zip.c:101
int zipquality
Stored quality, including rejected attempted assignments; zlib uses a cap of nine.
Definition tif_zip.c:94
TIFFPredictorState predict
Predictor prefix, first in the codec state.
Definition tif_zip.c:82
Describe a field's file type and internal argument contract.
Definition tif_dir.h:993
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
Definition tiffiop.h:649
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
Definition tiffiop.h:653
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
Definition tiffiop.h:742
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
Definition tiffiop.h:639
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
Definition tiffiop.h:626
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
Definition tiffiop.h:630
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
Definition tiffiop.h:670
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
Definition tiffiop.h:632
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
Definition tiffiop.h:645
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
Definition tiffiop.h:661
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
Definition tiffiop.h:675
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
Definition tiffiop.h:681
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
Definition tiffiop.h:686
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Definition tiffiop.h:651
Define the predictor prefix and method-chain contracts.
Declare private library state, callbacks, and implementation helpers.