49#include "libdeflate.h"
51#define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
61#if !defined(Z_NO_COMPRESSION) || !defined(Z_DEFLATED)
62#error "Antiquated ZLIB software; you must use version 1.0 or later"
65#define SAFE_MSG(sp) ((sp)->stream.msg == NULL ? "" : (sp)->stream.msg)
102#if LIBDEFLATE_SUPPORT
107 int libdeflate_state;
112 struct libdeflate_decompressor *libdeflate_dec;
117 struct libdeflate_compressor *libdeflate_enc;
119#define ZSTATE_INIT_DECODE 0x01
120#define ZSTATE_INIT_ENCODE 0x02
128#define GetZIPState(tif) ((ZIPState *)(tif)->tif_data)
129#define ZIPDecoderState(tif) GetZIPState(tif)
130#define ZIPEncoderState(tif) GetZIPState(tif)
165 static const char module[] = "ZIPSetupDecode";
173 deflateEnd(&sp->stream);
181 inflateInit(&sp->stream) != Z_OK)
206 return a < b ? a : b;
253#if LIBDEFLATE_SUPPORT
254 sp->libdeflate_state = -1;
257 assert(
sizeof(sp->
stream.avail_in) == 4);
263 if (inflateReset(&sp->
stream) == Z_OK)
301 static const char module[] = "ZIPDecode";
310 memset(op, 0, (
size_t)occ);
312 "ZIPDecode: Scanline %" PRIu32
" cannot be read due to "
318#if LIBDEFLATE_SUPPORT
319 if (sp->libdeflate_state == 1)
328 if (sp->libdeflate_state == 0)
351 if ((
size_t)occ != (uint64_t)occ)
356 enum libdeflate_result res;
357 if (sp->libdeflate_dec == NULL)
359 sp->libdeflate_dec = libdeflate_alloc_decompressor();
360 if (sp->libdeflate_dec == NULL)
366 sp->libdeflate_state = 1;
368 res = libdeflate_zlib_decompress(sp->libdeflate_dec, tif->
tif_rawcp,
372 if (res == LIBDEFLATE_SUCCESS)
386 if (res == LIBDEFLATE_INSUFFICIENT_SPACE)
389 memset(op, 0, (
size_t)occ);
396 sp->libdeflate_state = 0;
401 sp->stream.next_out = op;
402 assert(
sizeof(sp->stream.avail_out) == 4);
409 uInt avail_in_before =
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)
419 if (state == Z_DATA_ERROR)
421 memset(sp->stream.next_out, 0, (
size_t)occ);
422 TIFFErrorExtR(tif, module,
"Decoding error at scanline %lu, %s",
429 memset(sp->stream.next_out, 0, (
size_t)occ);
438 "Not enough data at scanline %lu (short %" PRIu64
441 memset(sp->stream.next_out, 0, (
size_t)occ);
466 static const char module[] = "ZIPSetupEncode";
473 inflateEnd(&sp->stream);
477 cappedQuality = sp->zipquality;
478 if (cappedQuality > Z_BEST_COMPRESSION)
479 cappedQuality = Z_BEST_COMPRESSION;
481 if (deflateInit(&sp->stream, cappedQuality) != Z_OK)
519#if LIBDEFLATE_SUPPORT
520 sp->libdeflate_state = -1;
523 assert(
sizeof(sp->
stream.avail_out) == 4);
530 return (deflateReset(&sp->
stream) == Z_OK);
564 static const char module[] = "ZIPEncode";
571 "ZIP codec state is not initialized for encoding");
577#if LIBDEFLATE_SUPPORT
578 if (sp->libdeflate_state == 1)
587 if (sp->libdeflate_state == 0)
593 if (sp->zipquality == Z_NO_COMPRESSION)
614 if ((
size_t)cc != (uint64_t)cc)
619 size_t nCompressedBytes;
620 if (sp->libdeflate_enc == NULL)
624 sp->libdeflate_enc = libdeflate_alloc_compressor(
625 sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
626 : sp->zipquality >= 6 && sp->zipquality <= 9
629 if (sp->libdeflate_enc == NULL)
642 if (libdeflate_zlib_compress_bound(sp->libdeflate_enc, (
size_t)cc) >
648 sp->libdeflate_state = 1;
649 nCompressedBytes = libdeflate_zlib_compress(
650 sp->libdeflate_enc, bp, (
size_t)cc, tif->
tif_rawdata,
653 if (nCompressedBytes == 0)
668 sp->libdeflate_state = 0;
671 sp->stream.next_in = bp;
672 assert(
sizeof(sp->stream.avail_in) == 4);
679 sp->stream.avail_in = avail_in_before;
680 if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
685 if (sp->stream.avail_out == 0)
691 sp->stream.avail_out =
694 cc -= (avail_in_before - sp->stream.avail_in);
721 static const char module[] = "ZIPPostEncode";
728 "ZIP codec state is not initialized for encoding");
732#if LIBDEFLATE_SUPPORT
733 if (sp->libdeflate_state == 1)
737 sp->stream.avail_in = 0;
740 state = deflate(&sp->stream, Z_FINISH);
752 sp->stream.avail_out =
762 }
while (state != Z_STREAM_END);
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);
836 static const char module[] = "ZIPVSetField";
842 sp->zipquality = (int)va_arg(ap,
int);
843 if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
848 "Invalid ZipQuality value. Should be in [-1,%d] range",
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)
866#if LIBDEFLATE_SUPPORT
867 if (sp->libdeflate_enc)
869 libdeflate_free_compressor(sp->libdeflate_enc);
870 sp->libdeflate_enc = NULL;
877 sp->subcodec = (int)va_arg(ap,
int);
884#if !LIBDEFLATE_SUPPORT
888 "DeflateCodec = DEFLATE_SUBCODEC_LIBDEFLATE "
889 "unsupported in this build");
896 return (*sp->vsetparent)(tif, tag, ap);
970static void *
TIFF_zalloc(
void *opaque,
unsigned int items,
unsigned int size)
972 static const char module[] = "TIFF_zalloc";
975 if (items > ~(
size_t)0 / size)
1020 static const char module[] = "TIFFInitZIP";
1035 "Merging Deflate codec-specific tags failed");
1048 sp->stream.opaque = tif;
1049 sp->stream.data_type = Z_BINARY;
1060 sp->zipquality = Z_DEFAULT_COMPRESSION;
1062#if LIBDEFLATE_SUPPORT
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.
static int ZIPVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve Deflate quality or backend or delegate a tag query.
static void * TIFF_zalloc(void *opaque, unsigned int items, unsigned int size)
Allocate zlib internal storage through handle Ext allocation.
#define GetZIPState(tif)
Cast tif->tif_data to the ZIPState.
static uInt TIFF_CLAMP_UINT64_TO_INT32_MAX(uint64_t v)
Bound a zlib chunk to the signed 32-bit maximum.
static uint64_t TIFF_MIN_UINT64(uint64_t a, uint64_t b)
Select the smaller of two unsigned values.
#define ZIPEncoderState(tif)
Alias of GetZIPState(), returning the ZIPState for encoding.
static int ZIPFixupTags(TIFF *tif)
Accept Deflate tag fixup without changes.
static const TIFFField zipFields[]
Define compression quality and backend pseudo-tags.
static uint64_t ZIPGetMaxCompressionRatio(TIFF *tif)
Report the Deflate expansion allocation heuristic.
static int ZIPPreEncode(TIFF *tif, uint16_t s)
Reset zlib encoding and per-strile backend selection.
static int ZIPPostEncode(TIFF *tif)
Finish zlib output or accept an already written libdeflate frame.
static int ZIPSetupEncode(TIFF *tif)
Initialise zlib encoding at a capped compression quality.
static void TIFF_zfree(void *opaque, void *ptr)
Release zlib storage through the matching handle Ext allocator.
int TIFFInitZIP(TIFF *tif, int scheme)
Install predictor-aware Deflate codec and allocator hooks.
static int ZIPVSetField(TIFF *tif, uint32_t tag, va_list ap)
Set Deflate quality or backend or delegate another tag.
#define SAFE_MSG(sp)
Borrow the zlib stream error text, or an empty string if none.
static int ZIPSetupDecode(TIFF *tif)
Initialise zlib decode mode and end an active encoder.
#define ZSTATE_INIT_ENCODE
Bit 0x02 of the codec state field: encoding has been set up.
static int ZIPEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Compress input with streaming zlib or eligible whole-strile libdeflate.
#define ZIPDecoderState(tif)
Alias of GetZIPState(), returning the ZIPState for decoding.
static int ZIPDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Expand zlib or eligible whole-strile libdeflate input.
static int ZIPPreDecode(TIFF *tif, uint16_t s)
Reset zlib decoding and per-strile backend selection.
#define LIBDEFLATE_MAX_COMPRESSION_LEVEL
Highest accepted TIFFTAG_ZIPQUALITY value: 12.
static void ZIPCleanup(TIFF *tif)
End Deflate dependency objects and release codec state.
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.
@ TIFF_SETGET_INT
Promoted int scalar representation.
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
void _TIFFfreeExt(TIFF *tif, void *p)
Release a handle-aware allocation and its payload charge.
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Allocate uninitialised bytes under a handle's allocation policy.
void * _TIFFcallocExt(TIFF *tif, tmsize_t nmemb, tmsize_t siz)
Allocate a zero-filled array under a handle's allocation policy.
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.
#define TRUE
Represent the legacy internal true value.
#define FALSE
Represent the legacy internal false value.
#define TIFFArrayCount(a)
Compute the element count of an actual array.
#define TIFF_ANY
Match any data type when searching for a field descriptor.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested strip height.
uint64_t TIFFTileSize64(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
int(* TIFFVGetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag getter.
int(* TIFFVSetMethod)(TIFF *tif, uint32_t tag, va_list ap)
Implement a handle's variadic tag setter.
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Store the current directory values and image traversal state.
uint32_t td_rowsperstrip
Rows per strip; UINT32_MAX represents one whole-image strip.
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
uint32_t td_imagelength
Image length in rows.
Prefix codec state with Predictor method chains and row geometry.
TIFFVGetMethod vgetfield
Variadic tag-query implementation for the handle.
TIFFVSetMethod vsetfield
Variadic tag-setting implementation for the handle.
Own Deflate dependency streams predictor prefix and backend selection.
TIFFVGetMethod vgetparent
Borrowed parent getter restored on cleanup.
z_stream stream
Zlib stream with internal allocations owned until inflateEnd/deflateEnd.
int state
Active zlib mode flags.
TIFFVSetMethod vsetparent
Borrowed parent setter restored on cleanup.
int read_error
Same-strile decode error latch reset by successful predecode.
int subcodec
Requested zlib or libdeflate backend, validated by the setter after assignment.
int zipquality
Stored quality, including rejected attempted assignments; zlib uses a cap of nine.
TIFFPredictorState predict
Predictor prefix, first in the codec state.
Describe a field's file type and internal argument contract.
Hold private state for one open TIFF library instance.
TIFFCodeMethod tif_encodestrip
Codec callback encoding strip data.
TIFFCodeMethod tif_encodetile
Codec callback encoding tile data.
TIFFTagMethods tif_tagmethods
Inline table of directory field and print callbacks.
TIFFPreMethod tif_preencode
Codec callback preparing a row, strip, or tile encode.
TIFFBoolMethod tif_fixuptags
Codec callback for directory-dependent tag corrections.
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
TIFFPreMethod tif_predecode
Codec callback preparing a row, strip, or tile decode.
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
TIFFGetMaxCompressionRatioMethod tif_getmaxcompressionratio
Codec callback returning its maximum compression ratio.
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
TIFFBoolMethod tif_setupencode
Codec setup callback run before preencode.
TIFFDirectory tif_dir
Inline representation of the current image directory.
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
TIFFCodeMethod tif_encoderow
Codec callback encoding a scanline.
TIFFVoidMethod tif_cleanup
Codec callback releasing codec-private state.
uint8_t * tif_data
Codec-private storage; allocation and cleanup follow the selected codec.
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
uint8_t * tif_rawdata
Raw buffer; ownership/mapping follow TIFF_MYBUFFER and TIFF_BUFFERMMAP.
tmsize_t tif_rawdatasize
Raw-buffer capacity in bytes; kept in step with tif_rawdata, including zero whenever tif_rawdata is N...
TIFFCodeMethod tif_decodetile
Codec callback decoding tile data.
Define the predictor prefix and method-chain contracts.
Declare private library state, callbacks, and implementation helpers.