71 lzma_filter filters[LZMA_FILTERS_MAX + 1];
85#define LSTATE_INIT_DECODE 0x01
86#define LSTATE_INIT_ENCODE 0x02
94#define GetLZMAState(tif) ((LZMAState *)(tif)->tif_data)
95#define LZMADecoderState(tif) GetLZMAState(tif)
96#define LZMAEncoderState(tif) GetLZMAState(tif)
117 return "operation completed successfully";
118 case LZMA_STREAM_END:
119 return "end of stream was reached";
121 return "input stream has no integrity check";
122 case LZMA_UNSUPPORTED_CHECK:
123 return "cannot calculate the integrity check";
125 return "integrity check type is now available";
127 return "cannot allocate memory";
128 case LZMA_MEMLIMIT_ERROR:
129 return "memory usage limit was reached";
130 case LZMA_FORMAT_ERROR:
131 return "file format not recognized";
132 case LZMA_OPTIONS_ERROR:
133 return "invalid or unsupported options";
134 case LZMA_DATA_ERROR:
135 return "data is corrupt";
137 return "no progress is possible (stream is truncated or corrupt)";
138 case LZMA_PROG_ERROR:
139 return "programming error";
140#if LZMA_VERSION >= 50040000
141 case LZMA_SEEK_NEEDED:
142 case LZMA_RET_INTERNAL1:
143 case LZMA_RET_INTERNAL2:
144 case LZMA_RET_INTERNAL3:
145 case LZMA_RET_INTERNAL4:
146 case LZMA_RET_INTERNAL5:
147 case LZMA_RET_INTERNAL6:
148 case LZMA_RET_INTERNAL7:
149 case LZMA_RET_INTERNAL8:
152 return "unidentified liblzma error";
221 static const char module[] = "LZMAPreDecode";
232 sp->stream.avail_in = (size_t)tif->
tif_rawcc;
236 "Liblzma cannot deal with buffers this size");
244 ret = lzma_stream_decoder(&sp->stream, (uint64_t)-1, 0);
247 TIFFErrorExtR(tif, module,
"Error initializing the stream decoder, %s",
281 static const char module[] = "LZMADecode";
290 memset(op, 0, (
size_t)occ);
292 "LZMADecode: Scanline %" PRIu32
" cannot be read due to "
299 sp->stream.avail_in = (size_t)tif->
tif_rawcc;
301 sp->stream.next_out = op;
302 sp->stream.avail_out = (size_t)occ;
303 if ((
tmsize_t)sp->stream.avail_out != occ)
307 memset(op, 0, (
size_t)occ);
309 "Liblzma cannot deal with buffers this size");
319 const uint8_t *next_in = sp->stream.next_in;
320 size_t avail_in = sp->stream.avail_in;
322 lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
323 if (ret == LZMA_STREAM_END)
325 if (ret == LZMA_MEMLIMIT_ERROR)
328 lzma_stream_decoder(&sp->stream, lzma_memusage(&sp->stream), 0);
332 memset(op, 0, (
size_t)occ);
334 "Error initializing the stream decoder, %s",
338 sp->stream.next_in = next_in;
339 sp->stream.avail_in = avail_in;
345 "Decoding error at scanline %" PRIu32
", %s",
349 }
while (sp->stream.avail_out > 0);
350 if (sp->stream.avail_out != 0)
353 memset(sp->stream.next_out, 0, sp->stream.avail_out);
355 "Not enough data at scanline %" PRIu32
361 tif->
tif_rawcp = (uint8_t *)sp->stream.next_in;
414 static const char module[] = "LZMAPreEncode";
428 "Liblzma cannot deal with buffers this size");
431 ret = lzma_stream_encoder(&sp->stream, sp->filters, sp->check);
434 TIFFErrorExtR(tif, module,
"Error in lzma_stream_encoder(): %s",
466 static const char module[] = "LZMAEncode";
473 "LZMA codec state is not initialized for encoding");
478 sp->stream.next_in = bp;
479 sp->stream.avail_in = (size_t)cc;
480 if ((
tmsize_t)sp->stream.avail_in != cc)
483 "Liblzma cannot deal with buffers this size");
488 lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
492 "Encoding error at scanline %" PRIu32
", %s",
496 if (sp->stream.avail_out == 0)
502 sp->stream.avail_out =
507 }
while (sp->stream.avail_in > 0);
532 static const char module[] = "LZMAPostEncode";
539 "LZMA codec state is not initialized for encoding");
543 sp->stream.avail_in = 0;
546 ret = lzma_code(&sp->stream, LZMA_FINISH);
549 case LZMA_STREAM_END:
558 sp->stream.avail_out =
566 case LZMA_UNSUPPORTED_CHECK:
569 case LZMA_MEMLIMIT_ERROR:
570 case LZMA_FORMAT_ERROR:
571 case LZMA_OPTIONS_ERROR:
572 case LZMA_DATA_ERROR:
574 case LZMA_PROG_ERROR:
575#if LZMA_VERSION >= 50040000
576 case LZMA_SEEK_NEEDED:
577 case LZMA_RET_INTERNAL1:
578 case LZMA_RET_INTERNAL2:
579 case LZMA_RET_INTERNAL3:
580 case LZMA_RET_INTERNAL4:
581 case LZMA_RET_INTERNAL5:
582 case LZMA_RET_INTERNAL6:
583 case LZMA_RET_INTERNAL7:
584 case LZMA_RET_INTERNAL8:
591 }
while (ret != LZMA_STREAM_END);
649 static const char module[] = "LZMAVSetField";
655 sp->preset = (int)va_arg(ap,
int);
656 lzma_lzma_preset(&sp->opt_lzma, (uint32_t)sp->preset);
660 lzma_stream_encoder(&sp->stream, sp->filters, sp->check);
669 return (*sp->vsetparent)(tif, tag, ap);
696 *va_arg(ap,
int *) = sp->
preset;
713 FALSE,
"LZMA2 Compression Preset", NULL},
743 static const char module[] = "TIFFInitLZMA";
745 lzma_stream tmp_stream = LZMA_STREAM_INIT;
755 TIFFErrorExtR(tif, module,
"Merging LZMA2 codec-specific tags failed");
766 memcpy(&sp->stream, &tmp_stream,
sizeof(lzma_stream));
777 sp->preset = LZMA_PRESET_DEFAULT;
778 sp->check = LZMA_CHECK_NONE;
782 sp->opt_delta.type = LZMA_DELTA_TYPE_BYTE;
790 sp->filters[0].id = LZMA_FILTER_DELTA;
791 sp->filters[0].options = &sp->opt_delta;
793 lzma_lzma_preset(&sp->opt_lzma, (uint32_t)sp->preset);
794 sp->filters[1].id = LZMA_FILTER_LZMA2;
795 sp->filters[1].options = &sp->opt_lzma;
797 sp->filters[2].id = LZMA_VLI_UNKNOWN;
798 sp->filters[2].options = NULL;
824 TIFFErrorExtR(tif, module,
"No space for LZMA2 state block");
void _TIFFSetDefaultCompressionState(TIFF *tif)
Install fallback codec callbacks and reset raw-access flags.
#define TIFF_SIZE_FORMAT
Provide the printf conversion fragment for size_t, without a percent sign.
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.
#define LZMAEncoderState(tif)
Alias of GetLZMAState(), returning the LZMAState for encoding.
#define GetLZMAState(tif)
Cast tif->tif_data to the LZMAState.
static int LZMAVGetField(TIFF *tif, uint32_t tag, va_list ap)
Retrieve LZMA preset or delegate a tag query.
static void LZMACleanup(TIFF *tif)
End the dependency stream and release LZMA codec state.
static int LZMAPostEncode(TIFF *tif)
Finish the LZMA2 stream and flush produced output.
static int LZMAPreEncode(TIFF *tif, uint16_t s)
Initialise a filtered LZMA2 stream for one output strile.
#define LSTATE_INIT_ENCODE
Bit 0x02 of the codec state field: encoding has been set up.
static int LZMAPreDecode(TIFF *tif, uint16_t s)
Initialise the liblzma decoder for one strile.
static int LZMASetupDecode(TIFF *tif)
Switch the liblzma stream to decode mode.
#define LSTATE_INIT_DECODE
Bit 0x01 of the codec state field: decoding has been set up.
static int LZMAFixupTags(TIFF *tif)
Accept LZMA tag fixup without changes.
#define LZMADecoderState(tif)
Alias of GetLZMAState(), returning the LZMAState for decoding.
static uint64_t LZMAGetMaxCompressionRatio(TIFF *tif)
Report the empirical LZMA expansion heuristic.
static int LZMAEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
Consume input through the active liblzma encoder.
static const TIFFField lzmaFields[]
Define the LZMA compression-preset pseudo-tag.
static int LZMASetupEncode(TIFF *tif)
Switch the liblzma stream to encode mode.
static int LZMADecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
Expand cached liblzma input into caller output.
int TIFFInitLZMA(TIFF *tif, int scheme)
Allocate predictor-aware LZMA2 codec state and install callbacks.
static const char * LZMAStrerror(lzma_ret ret)
Describe a liblzma status using immutable text.
static int LZMAVSetField(TIFF *tif, uint32_t tag, va_list ap)
Store LZMA preset or delegate a tag assignment.
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.
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 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.
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.
Own a filtered liblzma stream and predictor-prefixed methods.
TIFFPredictorState predict
Predictor prefix, first in codec state.
TIFFVSetMethod vsetparent
Borrowed parent tag setter restored during cleanup.
lzma_check check
Dependency integrity check, default LZMA_CHECK_NONE.
int read_error
Same-strile decode failure latch reset after successful predecode.
int preset
Cached compression preset; setter stores even when option conversion fails.
lzma_options_delta opt_delta
Embedded byte-delta options referenced by filters.
TIFFVGetMethod vgetparent
Borrowed parent tag getter restored during cleanup.
int state
Initialised decode/encode mode bits.
lzma_options_lzma opt_lzma
Embedded LZMA2 options referenced by filters.
lzma_stream stream
Dependency stream owning internal allocations until lzma_end.
uint16_t td_bitspersample
Bits in each sample.
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
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.
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.