Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
base.h File Reference

Go to the source code of this file.

Data Structures

struct  lzma_allocator
 Custom functions for memory handling. More...
 
struct  lzma_stream
 Passing data to and from liblzma. More...
 

Macros

#define LZMA_STREAM_INIT
 Initialization for lzma_stream.
 

Typedefs

typedef unsigned char lzma_bool
 Boolean.
 
typedef struct lzma_internal_s lzma_internal
 Internal data structure.
 

Enumerations

enum  lzma_reserved_enum { LZMA_RESERVED_ENUM = 0 }
 Type of reserved enumeration variable in structures. More...
 
enum  lzma_ret {
  LZMA_OK = 0 , LZMA_STREAM_END = 1 , LZMA_NO_CHECK = 2 , LZMA_UNSUPPORTED_CHECK = 3 ,
  LZMA_GET_CHECK = 4 , LZMA_MEM_ERROR = 5 , LZMA_MEMLIMIT_ERROR = 6 , LZMA_FORMAT_ERROR = 7 ,
  LZMA_OPTIONS_ERROR = 8 , LZMA_DATA_ERROR = 9 , LZMA_BUF_ERROR = 10 , LZMA_PROG_ERROR = 11
}
 Return values used by several functions in liblzma. More...
 
enum  lzma_action {
  LZMA_RUN = 0 , LZMA_SYNC_FLUSH = 1 , LZMA_FULL_FLUSH = 2 , LZMA_FULL_BARRIER = 4 ,
  LZMA_FINISH = 3
}
 The ‘action’ argument for lzma_code() More...
 

Functions

 LZMA_API (lzma_ret) lzma_code(lzma_stream *strm
 Encode or decode data.
 
 LZMA_API (void) lzma_end(lzma_stream *strm) lzma_nothrow
 Free memory allocated for the coder data structures.
 
 LZMA_API (uint64_t) lzma_memusage(const lzma_stream *strm) lzma_nothrow lzma_attr_pure
 Get the memory usage of decoder filter chain.
 

Variables

lzma_action action lzma_nothrow lzma_attr_warn_unused_result
 
uint64_t * progress_in
 
uint64_t uint64_t *progress_out lzma_nothrow
 

Macro Definition Documentation

◆ LZMA_STREAM_INIT

#define LZMA_STREAM_INIT
Value:
{ NULL, 0, 0, NULL, 0, 0, NULL, NULL, \
NULL, NULL, NULL, NULL, 0, 0, 0, 0, \
@ LZMA_RESERVED_ENUM
Definition base.h:45
#define NULL
Definition getopt1.c:37

Initialization for lzma_stream.

When you declare an instance of lzma_stream, you can immediately initialize it so that initialization functions know that no memory has been allocated yet:

lzma_stream strm = LZMA_STREAM_INIT;

If you need to initialize a dynamically allocated lzma_stream, you can use memset(strm_pointer, 0, sizeof(lzma_stream)). Strictly speaking, this violates the C standard since NULL may have different internal representation than zero, but it should be portable enough in practice. Anyway, for maximum portability, you can use something like this:

lzma_stream tmp = LZMA_STREAM_INIT;
*strm = tmp;

Typedef Documentation

◆ lzma_bool

typedef unsigned char lzma_bool

Boolean.

This is here because C89 doesn't have stdbool.h. To set a value for variables having type lzma_bool, you can use

  • C99's ‘true’ and ‘false’ from stdbool.h;
  • C++'s internal ‘true’ and ‘false’; or
  • integers one (true) and zero (false).

This is here because C89 doesn't have stdbool.h. To set a value for variables having type lzma_bool, you can use

  • C99's 'true' and 'false' from stdbool.h;
  • C++'s internal 'true' and 'false'; or
  • integers one (true) and zero (false).

◆ lzma_internal

Internal data structure.

The contents of this structure is not visible outside the library.

Enumeration Type Documentation

◆ lzma_action

The ‘action’ argument for lzma_code()

After the first use of LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, LZMA_FULL_BARRIER, or LZMA_FINISH, the same ‘action’ must is used until lzma_code() returns LZMA_STREAM_END. Also, the amount of input (that is, strm->avail_in) must not be modified by the application until lzma_code() returns LZMA_STREAM_END. Changing the ‘action’ or modifying the amount of input will make lzma_code() return LZMA_PROG_ERROR.

Enumerator
LZMA_RUN 

Continue coding.

Encoder: Encode as much input as possible. Some internal buffering will probably be done (depends on the filter chain in use), which causes latency: the input used won't usually be decodeable from the output of the same lzma_code() call.

Decoder: Decode as much input as possible and produce as much output as possible.

LZMA_SYNC_FLUSH 

Make all the input available at output.

Normally the encoder introduces some latency. LZMA_SYNC_FLUSH forces all the buffered data to be available at output without resetting the internal state of the encoder. This way it is possible to use compressed stream for example for communication over network.

Only some filters support LZMA_SYNC_FLUSH. Trying to use LZMA_SYNC_FLUSH with filters that don't support it will make lzma_code() return LZMA_OPTIONS_ERROR. For example, LZMA1 doesn't support LZMA_SYNC_FLUSH but LZMA2 does.

Using LZMA_SYNC_FLUSH very often can dramatically reduce the compression ratio. With some filters (for example, LZMA2), fine-tuning the compression options may help mitigate this problem significantly (for example, match finder with LZMA2).

Decoders don't support LZMA_SYNC_FLUSH.

LZMA_FULL_FLUSH 

Finish encoding of the current Block.

All the input data going to the current Block must have been given to the encoder (the last bytes can still be pending in *next_in). Call lzma_code() with LZMA_FULL_FLUSH until it returns LZMA_STREAM_END. Then continue normally with LZMA_RUN or finish the Stream with LZMA_FINISH.

This action is currently supported only by Stream encoder and easy encoder (which uses Stream encoder). If there is no unfinished Block, no empty Block is created.

LZMA_FULL_BARRIER 

Finish encoding of the current Block.

This is like LZMA_FULL_FLUSH except that this doesn't necessarily wait until all the input has been made available via the output buffer. That is, lzma_code() might return LZMA_STREAM_END as soon as all the input has been consumed (avail_in == 0).

LZMA_FULL_BARRIER is useful with a threaded encoder if one wants to split the .xz Stream into Blocks at specific offsets but doesn't care if the output isn't flushed immediately. Using LZMA_FULL_BARRIER allows keeping the threads busy while LZMA_FULL_FLUSH would make lzma_code() wait until all the threads have finished until more data could be passed to the encoder.

With a lzma_stream initialized with the single-threaded lzma_stream_encoder() or lzma_easy_encoder(), LZMA_FULL_BARRIER is an alias for LZMA_FULL_FLUSH.

LZMA_FINISH 

Finish the coding operation.

All the input data must have been given to the encoder (the last bytes can still be pending in next_in). Call lzma_code() with LZMA_FINISH until it returns LZMA_STREAM_END. Once LZMA_FINISH has been used, the amount of input must no longer be changed by the application.

When decoding, using LZMA_FINISH is optional unless the LZMA_CONCATENATED flag was used when the decoder was initialized. When LZMA_CONCATENATED was not used, the only effect of LZMA_FINISH is that the amount of input must not be changed just like in the encoder.

◆ lzma_reserved_enum

Type of reserved enumeration variable in structures.

To avoid breaking library ABI when new features are added, several structures contain extra variables that may be used in future. Since sizeof(enum) can be different than sizeof(int), and sizeof(enum) may even vary depending on the range of enumeration constants, we specify a separate type to be used for reserved enumeration variables. All enumeration constants in liblzma API will be non-negative and less than 128, which should guarantee that the ABI won't break even when new constants are added to existing enumerations.

Enumerator
LZMA_RESERVED_ENUM 

◆ lzma_ret

enum lzma_ret

Return values used by several functions in liblzma.

Check the descriptions of specific functions to find out which return values they can return. With some functions the return values may have more specific meanings than described here; those differences are described per-function basis.

Enumerator
LZMA_OK 

Operation completed successfully.

LZMA_STREAM_END 

End of stream was reached.

In encoder, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, or LZMA_FINISH was finished. In decoder, this indicates that all the data was successfully decoded.

In all cases, when LZMA_STREAM_END is returned, the last output bytes should be picked from strm->next_out.

LZMA_NO_CHECK 

Input stream has no integrity check.

This return value can be returned only if the LZMA_TELL_NO_CHECK flag was used when initializing the decoder. LZMA_NO_CHECK is just a warning, and the decoding can be continued normally.

It is possible to call lzma_get_check() immediately after lzma_code has returned LZMA_NO_CHECK. The result will naturally be LZMA_CHECK_NONE, but the possibility to call lzma_get_check() may be convenient in some applications.

LZMA_UNSUPPORTED_CHECK 

Cannot calculate the integrity check.

The usage of this return value is different in encoders and decoders.

Encoders can return this value only from the initialization function. If initialization fails with this value, the encoding cannot be done, because there's no way to produce output with the correct integrity check.

Decoders can return this value only from lzma_code() and only if the LZMA_TELL_UNSUPPORTED_CHECK flag was used when initializing the decoder. The decoding can still be continued normally even if the check type is unsupported, but naturally the check will not be validated, and possible errors may go undetected.

With decoder, it is possible to call lzma_get_check() immediately after lzma_code() has returned LZMA_UNSUPPORTED_CHECK. This way it is possible to find out what the unsupported Check ID was.

LZMA_GET_CHECK 

Integrity check type is now available.

This value can be returned only by the lzma_code() function and only if the decoder was initialized with the LZMA_TELL_ANY_CHECK flag. LZMA_GET_CHECK tells the application that it may now call lzma_get_check() to find out the Check ID. This can be used, for example, to implement a decoder that accepts only files that have strong enough integrity check.

LZMA_MEM_ERROR 

Cannot allocate memory.

Memory allocation failed, or the size of the allocation would be greater than SIZE_MAX.

Due to internal implementation reasons, the coding cannot be continued even if more memory were made available after LZMA_MEM_ERROR.

LZMA_MEMLIMIT_ERROR 
LZMA_FORMAT_ERROR 

Memory usage limit was reached.

Decoder would need more memory than allowed by the specified memory usage limit. To continue decoding, the memory usage limit has to be increased with lzma_memlimit_set().

File format not recognized

The decoder did not recognize the input as supported file format. This error can occur, for example, when trying to decode .lzma format file with lzma_stream_decoder, because lzma_stream_decoder accepts only the .xz format.

LZMA_OPTIONS_ERROR 

Invalid or unsupported options.

Invalid or unsupported options, for example

  • unsupported filter(s) or filter options; or
  • reserved bits set in headers (decoder only).

Rebuilding liblzma with more features enabled, or upgrading to a newer version of liblzma may help.

LZMA_DATA_ERROR 

Data is corrupt.

The usage of this return value is different in encoders and decoders. In both encoder and decoder, the coding cannot continue after this error.

Encoders return this if size limits of the target file format would be exceeded. These limits are huge, thus getting this error from an encoder is mostly theoretical. For example, the maximum compressed and uncompressed size of a .xz Stream is roughly 8 EiB (2^63 bytes).

Decoders return this error if the input data is corrupt. This can mean, for example, invalid CRC32 in headers or invalid check of uncompressed data.

LZMA_BUF_ERROR 

No progress is possible.

This error code is returned when the coder cannot consume any new input and produce any new output. The most common reason for this error is that the input stream being decoded is truncated or corrupt.

This error is not fatal. Coding can be continued normally by providing more input and/or more output space, if possible.

Typically the first call to lzma_code() that can do no progress returns LZMA_OK instead of LZMA_BUF_ERROR. Only the second consecutive call doing no progress will return LZMA_BUF_ERROR. This is intentional.

With zlib, Z_BUF_ERROR may be returned even if the application is doing nothing wrong, so apps will need to handle Z_BUF_ERROR specially. The above hack guarantees that liblzma never returns LZMA_BUF_ERROR to properly written applications unless the input file is truncated or corrupt. This should simplify the applications a little.

LZMA_PROG_ERROR 

Programming error.

This indicates that the arguments given to the function are invalid or the internal state of the decoder is corrupt.

  • Function arguments are invalid or the structures pointed by the argument pointers are invalid e.g. if strm->next_out has been set to NULL and strm->avail_out > 0 when calling lzma_code().
  • lzma_* functions have been called in wrong order e.g. lzma_code() was called right after lzma_end().
  • If errors occur randomly, the reason might be flaky hardware.

If you think that your code is correct, this error code can be a sign of a bug in liblzma. See the documentation how to report bugs.

Function Documentation

◆ LZMA_API() [1/3]

LZMA_API ( lzma_ret )
extern

Encode or decode data.

Initialize a .xz file information decoder.

List available filters and/or their options (for help message)

Convert a filter chain to a string.

MicroLZMA decoder.

Initialize .lz (lzip) decoder (a foreign file format)

Decode .xz, .lzma, and .lz (lzip) files with autodetection.

Initialize multithreaded .xz Stream decoder.

MicroLZMA encoder.

Decode a variable-length integer.

Compare two lzma_stream_flags structures.

Decode Stream Footer.

Decode Stream Header.

Encode Stream Footer.

Decode and validate the Index field.

Single-call .xz Index decoder.

Single-call .xz Index encoder.

Initialize .xz Index decoder.

Initialize .xz Index encoder.

Concatenate lzma_indexes.

Set the amount of Stream Padding.

Set the Stream Flags.

Decode Filter Flags from given buffer.

Encode Filter Flags into given buffer.

Calculate encoded size of a Filter Flags field.

Decode the Filter Properties field.

Encode the Filter Properties field.

Get the size of the Filter Properties field.

Single-call raw decoder.

Single-call raw encoder.

Update the filter chain in the encoder.

Initialize raw decoder.

Initialize raw encoder.

Single-call .xz Stream decoder.

Initialize .lzma decoder (legacy file format)

Decode .xz Streams and .lzma files with autodetection.

Initialize .xz Stream decoder.

Single-call .xz Stream encoder.

Initialize .lzma encoder (legacy file format)

Initialize multithreaded .xz Stream encoder.

Initialize .xz Stream encoder using a custom filter chain.

Single-call .xz Stream encoding using a preset number.

Single-call .xz Block decoder.

Single-call uncompressed .xz Block encoder.

Single-call .xz Block encoder.

Initialize .xz Block decoder.

Initialize .xz Block encoder.

Validate and set Compressed Size according to Unpadded Size.

Decode Block Header.

Encode Block Header.

Set the memory usage limit.

Once the lzma_stream has been successfully initialized (e.g. with lzma_stream_encoder()), the actual encoding or decoding is done using this function. The application has to update strm->next_in, strm->avail_in, strm->next_out, and strm->avail_out to pass input to and get output from liblzma.

See the description of the coder-specific initialization function to find out what ‘action’ values are supported by the coder.

This function is supported only when *strm has been initialized with a function that takes a memlimit argument.

liblzma 5.2.3 and earlier has a bug where memlimit value of 0 causes this function to do nothing (leaving the limit unchanged) and still return LZMA_OK. Later versions treat 0 as if 1 had been specified (so lzma_memlimit_get() will return 1 even if you specify 0 here).

Returns
- LZMA_OK: New memory usage limit successfully set.
  • LZMA_MEMLIMIT_ERROR: The new limit is too small. The limit was not changed.
  • LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't support memory usage limit.

The caller must have calculated the size of the Block Header already with lzma_block_header_size(). If a value larger than the one calculated by lzma_block_header_size() is used, the Block Header will be padded to the specified size.

Parameters
outBeginning of the output buffer. This must be at least block->header_size bytes.
blockBlock options to be encoded.
Returns
- LZMA_OK: Encoding was successful. block->header_size bytes were written to output buffer.
  • LZMA_OPTIONS_ERROR: Invalid or unsupported options.
  • LZMA_PROG_ERROR: Invalid arguments, for example block->header_size is invalid or block->filters is NULL.

block->version should (usually) be set to the highest value supported by the application. If the application sets block->version to a value higher than supported by the current liblzma version, this function will downgrade block->version to the highest value supported by it. Thus one should check the value of block->version after calling this function if block->version was set to a non-zero value and the application doesn't otherwise know that the liblzma version being used is new enough to support the specified block->version.

The size of the Block Header must have already been decoded with lzma_block_header_size_decode() macro and stored to block->header_size.

The integrity check type from Stream Header must have been stored to block->check.

block->filters must have been allocated, but they don't need to be initialized (possible existing filter options are not freed).

Parameters
blockDestination for Block options.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() (and also free() if an error occurs).
inBeginning of the input buffer. This must be at least block->header_size bytes.
Returns
- LZMA_OK: Decoding was successful. block->header_size bytes were read from the input buffer.
  • LZMA_OPTIONS_ERROR: The Block Header specifies some unsupported options such as unsupported filters. This can happen also if block->version was set to a too low value compared to what would be required to properly represent the information stored in the Block Header.
  • LZMA_DATA_ERROR: Block Header is corrupt, for example, the CRC32 doesn't match.
  • LZMA_PROG_ERROR: Invalid arguments, for example block->header_size is invalid or block->filters is NULL.

Block Header stores Compressed Size, but Index has Unpadded Size. If the application has already parsed the Index and is now decoding Blocks, it can calculate Compressed Size from Unpadded Size. This function does exactly that with error checking:

  • Compressed Size calculated from Unpadded Size must be positive integer, that is, Unpadded Size must be big enough that after Block Header and Check fields there's still at least one byte for Compressed Size.
  • If Compressed Size was present in Block Header, the new value calculated from Unpadded Size is compared against the value from Block Header.
Note
This function must be called after decoding the Block Header field so that it can properly validate Compressed Size if it was present in Block Header.
Returns
- LZMA_OK: block->compressed_size was set successfully.
  • LZMA_DATA_ERROR: unpadded_size is too small compared to block->header_size and lzma_check_size(block->check).
  • LZMA_PROG_ERROR: Some values are invalid. For example, block->header_size must be a multiple of four and between 8 and 1024 inclusive.

Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the filter chain supports it), and LZMA_FINISH.

Returns
- LZMA_OK: All good, continue with lzma_code().
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID that is not supported by this build of liblzma. Initializing the encoder failed.
  • LZMA_PROG_ERROR

Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using LZMA_FINISH is not required. It is supported only for convenience.

Returns
- LZMA_OK: All good, continue with lzma_code().
  • LZMA_UNSUPPORTED_CHECK: Initialization was successful, but the given Check ID is not supported, thus Check will be ignored.
  • LZMA_PROG_ERROR
  • LZMA_MEM_ERROR

In contrast to the multi-call encoder initialized with lzma_block_encoder(), this function encodes also the Block Header. This is required to make it possible to write appropriate Block Header also in case the data isn't compressible, and different filter chain has to be used to encode the data in uncompressed form using uncompressed chunks of the LZMA2 filter.

When the data isn't compressible, header_size, compressed_size, and uncompressed_size are set just like when the data was compressible, but it is possible that header_size is too small to hold the filter chain specified in block->filters, because that isn't necessarily the filter chain that was actually used to encode the data. lzma_block_unpadded_size() still works normally, because it doesn't read the filters array.

Parameters
blockBlock options: block->version, block->check, and block->filters must have been initialized.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

This is like lzma_block_buffer_encode() except this doesn't try to compress the data and instead encodes the data using LZMA2 uncompressed chunks. The required output buffer size can be determined with lzma_block_buffer_bound().

Since the data won't be compressed, this function ignores block->filters. This function doesn't take lzma_allocator because this function doesn't allocate any memory from the heap.

This is single-call equivalent of lzma_block_decoder(), and requires that the caller has already decoded Block Header and checked its memory usage.

Parameters
blockBlock options just like with lzma_block_decoder().
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Decoding was successful.
  • LZMA_OPTIONS_ERROR
  • LZMA_DATA_ERROR
  • LZMA_MEM_ERROR
  • LZMA_BUF_ERROR: Output buffer was too small.
  • LZMA_PROG_ERROR

The maximum required output buffer size can be calculated with lzma_stream_buffer_bound().

Parameters
presetCompression preset to use. See the description in lzma_easy_encoder().
checkType of the integrity check to calculate from uncompressed data.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
filtersArray of filters. This must be terminated with filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for more information.
checkType of the integrity check to calculate from uncompressed data.
Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

This provides the functionality of lzma_easy_encoder() and lzma_stream_encoder() as a single function for multithreaded use.

The supported actions for lzma_code() are LZMA_RUN, LZMA_FULL_FLUSH, LZMA_FULL_BARRIER, and LZMA_FINISH. Support for LZMA_SYNC_FLUSH might be added in the future.

Parameters
strmPointer to properly prepared lzma_stream
optionsPointer to multithreaded compression options
Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

The .lzma format is sometimes called the LZMA_Alone format, which is the reason for the name of this function. The .lzma format supports only the LZMA1 filter. There is no support for integrity checks like CRC32.

Use this function if and only if you need to create files readable by legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format is strongly recommended.

The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH. No kind of flushing is supported, because the file format doesn't make it possible.

Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Parameters
filtersArray of filters. This must be terminated with filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for more information.
checkType of the integrity check to calculate from uncompressed data.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED
Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR

This decoder autodetects between the .xz and .lzma file formats, and calls lzma_stream_decoder() or lzma_alone_decoder() once the type of the input file has been detected.

Parameters
strmPointer to properly prepared lzma_stream
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
flagsBitwise-or of flags, or zero for no flags.
Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.

Valid ‘action’ arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. There is no need to use LZMA_FINISH, but it's allowed because it may simplify certain types of applications.

Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Parameters
memlimitPointer to how much memory the decoder is allowed to allocate. The value pointed by this pointer is modified if and only if LZMA_MEMLIMIT_ERROR is returned.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_CONCATENATED. Note that LZMA_TELL_ANY_CHECK is not allowed and will return LZMA_PROG_ERROR.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if decoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_DATA_ERROR
  • LZMA_NO_CHECK: This can be returned only if using the LZMA_TELL_NO_CHECK flag.
  • LZMA_UNSUPPORTED_CHECK: This can be returned only if using the LZMA_TELL_UNSUPPORTED_CHECK flag.
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. The minimum required memlimit value was stored to *memlimit.
  • LZMA_BUF_ERROR: Output buffer was too small.
  • LZMA_PROG_ERROR

This function may be useful when implementing custom file formats.

Parameters
strmPointer to properly prepared lzma_stream
filtersArray of lzma_filter structures. The end of the array must be marked with .id = LZMA_VLI_UNKNOWN.

The ‘action’ with lzma_code() can be LZMA_RUN, LZMA_SYNC_FLUSH (if the filter chain supports it), or LZMA_FINISH.

Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

The initialization of raw decoder goes similarly to raw encoder.

The ‘action’ with lzma_code() can be LZMA_RUN or LZMA_FINISH. Using LZMA_FINISH is not required, it is supported just for convenience.

Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

This function is for advanced users only. This function has two slightly different purposes:

  • After LZMA_FULL_FLUSH when using Stream encoder: Set a new filter chain, which will be used starting from the next Block.
  • After LZMA_SYNC_FLUSH using Raw, Block, or Stream encoder: Change the filter-specific options in the middle of encoding. The actual filters in the chain (Filter IDs) cannot be changed. In the future, it might become possible to change the filter options without using LZMA_SYNC_FLUSH.

While rarely useful, this function may be called also when no data has been compressed yet. In that case, this function will behave as if LZMA_FULL_FLUSH (Stream encoder) or LZMA_SYNC_FLUSH (Raw or Block encoder) had been used right before calling this function.

Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Parameters
filtersArray of lzma_filter structures. The end of the array must be marked with .id = LZMA_VLI_UNKNOWN.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR
Note
There is no function to calculate how big output buffer would surely be big enough. (lzma_stream_buffer_bound() works only for lzma_stream_buffer_encode(); raw encoder won't necessarily meet that bound.)
Parameters
filtersArray of lzma_filter structures. The end of the array must be marked with .id = LZMA_VLI_UNKNOWN.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].

This function may be useful when implementing custom file formats using the raw encoder and decoder.

Parameters
sizePointer to uint32_t to hold the size of the properties
filterFilter ID and options (the size of the properties may vary depending on the options)
Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Note
This function validates the Filter ID, but does not necessarily validate the options. Thus, it is possible that this returns LZMA_OK while the following call to lzma_properties_encode() returns LZMA_OPTIONS_ERROR.
Parameters
filterFilter ID and options
propsBuffer to hold the encoded options. The size of buffer must have been already determined with lzma_properties_size().
Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Note
Even this function won't validate more options than actually necessary. Thus, it is possible that encoding the properties succeeds but using the same options to initialize the encoder will fail.
If lzma_properties_size() indicated that the size of the Filter Properties field is zero, calling lzma_properties_encode() is not required, but it won't do any harm either.
Parameters
filterfilter->id must have been set to the correct Filter ID. filter->options doesn't need to be initialized (it's not freed by this function). The decoded options will be stored in filter->options; it's application's responsibility to free it when appropriate. filter->options is set to NULL if there are no properties or if an error occurs.
allocatorCustom memory allocator used to allocate the options. Set to NULL to use the default malloc(), and in case of an error, also free().
propsInput buffer containing the properties.
props_sizeSize of the properties. This must be the exact size; giving too much or too little input will return LZMA_OPTIONS_ERROR.
Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR

Knowing the size of Filter Flags is useful to know when allocating memory to hold the encoded Filter Flags.

Parameters
sizePointer to integer to hold the calculated size
filterFilter ID and associated options whose encoded size is to be calculated
Returns
- LZMA_OK: *size set successfully. Note that this doesn't guarantee that filter->options is valid, thus lzma_filter_flags_encode() may still fail.
  • LZMA_OPTIONS_ERROR: Unknown Filter ID or unsupported options.
  • LZMA_PROG_ERROR: Invalid options
Note
If you need to calculate size of List of Filter Flags, you need to loop over every lzma_filter entry.

In contrast to some functions, this doesn't allocate the needed buffer. This is due to how this function is used internally by liblzma.

Parameters
filterFilter ID and options to be encoded
outBeginning of the output buffer
out_posout[*out_pos] is the next write position. This is updated by the encoder.
out_sizeout[out_size] is the first byte to not write.
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_OPTIONS_ERROR: Invalid or unsupported options.
  • LZMA_PROG_ERROR: Invalid options or not enough output buffer space (you should have checked it with lzma_filter_flags_size()).

The decoded result is stored into *filter. The old value of filter->options is not free()d.

Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

Set the Stream Flags of the last (and typically the only) Stream in lzma_index. This can be useful when reading information from the lzma_index, because to decode Blocks, knowing the integrity check type is needed.

The given Stream Flags are copied into internal preallocated structure in the lzma_index, thus the caller doesn't need to keep the *stream_flags available after calling this function.

Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR: Unsupported stream_flags->version.
  • LZMA_PROG_ERROR

Set the amount of Stream Padding of the last (and typically the only) Stream in the lzma_index. This is needed when planning to do random-access reading within multiple concatenated Streams.

By default, the amount of Stream Padding is assumed to be zero bytes.

Returns
- LZMA_OK
  • LZMA_DATA_ERROR: The file size would grow too big.
  • LZMA_PROG_ERROR

Concatenating lzma_indexes is useful when doing random-access reading in multi-Stream .xz file, or when combining multiple Streams into single Stream.

Parameters
destlzma_index after which src is appended
srclzma_index to be appended after dest. If this function succeeds, the memory allocated for src is freed or moved to be part of dest, and all iterators pointing to src will become invalid.
allocatorCustom memory allocator; can be NULL to use malloc() and free().
Returns
- LZMA_OK: lzma_indexes were concatenated successfully. src is now a dangling pointer.
  • LZMA_DATA_ERROR: *dest would grow too big.
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
iPointer to lzma_index which should be encoded.

The valid ‘action’ values for lzma_code() are LZMA_RUN and LZMA_FINISH. It is enough to use only one of them (you can choose freely).

Returns
- LZMA_OK: Initialization succeeded, continue with lzma_code().
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
iThe decoded Index will be made available via this pointer. Initially this function will set *i to NULL (the old value is ignored). If decoding succeeds (lzma_code() returns LZMA_STREAM_END), *i will be set to point to a new lzma_index, which the application has to later free with lzma_index_end().
memlimitHow much memory the resulting lzma_index is allowed to require. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.

Valid ‘action’ arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. There is no need to use LZMA_FINISH, but it's allowed because it may simplify certain types of applications.

Returns
- LZMA_OK: Initialization succeeded, continue with lzma_code().
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

liblzma 5.2.3 and older list also LZMA_MEMLIMIT_ERROR here but that error code has never been possible from this initialization function.

Parameters
ilzma_index to be encoded
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Output buffer is too small. Use lzma_index_size() to find out how much output space is needed.
  • LZMA_PROG_ERROR
Note
This function doesn't take allocator argument since all the internal data is allocated on stack.
Parameters
iIf decoding succeeds, *i will point to a new lzma_index, which the application has to later free with lzma_index_end(). If an error occurs, *i will be NULL. The old value of *i is always ignored and thus doesn't need to be initialized by the caller.
memlimitPointer to how much memory the resulting lzma_index is allowed to require. The value pointed by this pointer is modified if and only if LZMA_MEMLIMIT_ERROR is returned.
allocatorPointer to lzma_allocator, or NULL to use malloc()
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
Returns
- LZMA_OK: Decoding was successful.
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. The minimum required memlimit value was stored to *memlimit.
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

After telling the sizes of all Blocks with lzma_index_hash_append(), the actual Index field is decoded with this function. Specifically, once decoding of the Index field has been started, no more Records can be added using lzma_index_hash_append().

This function doesn't use lzma_stream structure to pass the input data. Instead, the input buffer is specified using three arguments. This is because it matches better the internal APIs of liblzma.

Parameters
index_hashPointer to a lzma_index_hash structure
inPointer to the beginning of the input buffer
in_posin[*in_pos] is the next byte to process
in_sizein[in_size] is the first byte not to process
Returns
- LZMA_OK: So far good, but more input is needed.
  • LZMA_STREAM_END: Index decoded successfully and it matches the Records given with lzma_index_hash_append().
  • LZMA_DATA_ERROR: Index is corrupt or doesn't match the information given with lzma_index_hash_append().
  • LZMA_BUF_ERROR: Cannot progress because *in_pos >= in_size.
  • LZMA_PROG_ERROR
Parameters
optionsStream Footer options to be encoded.
outBeginning of the output buffer of LZMA_STREAM_HEADER_SIZE bytes.
Returns
- LZMA_OK: Encoding was successful.
  • LZMA_OPTIONS_ERROR: options->version is not supported by this liblzma version.
  • LZMA_PROG_ERROR: Invalid options.
Parameters
optionsTarget for the decoded Stream Header options.
inBeginning of the input buffer of LZMA_STREAM_HEADER_SIZE bytes.

options->backward_size is always set to LZMA_VLI_UNKNOWN. This is to help comparing Stream Flags from Stream Header and Stream Footer with lzma_stream_flags_compare().

Returns
- LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given buffer cannot be Stream Header.
  • LZMA_DATA_ERROR: CRC32 doesn't match, thus the header is corrupt.
  • LZMA_OPTIONS_ERROR: Unsupported options are present in the header.
Note
When decoding .xz files that contain multiple Streams, it may make sense to print "file format not recognized" only if decoding of the Stream Header of the first Stream gives LZMA_FORMAT_ERROR. If non-first Stream Header gives LZMA_FORMAT_ERROR, the message used for LZMA_DATA_ERROR is probably more appropriate.

For example, Stream decoder in liblzma uses LZMA_DATA_ERROR if LZMA_FORMAT_ERROR is returned by lzma_stream_header_decode() when decoding non-first Stream.

Parameters
optionsTarget for the decoded Stream Header options.
inBeginning of the input buffer of LZMA_STREAM_HEADER_SIZE bytes.
Returns
- LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given buffer cannot be Stream Footer.
  • LZMA_DATA_ERROR: CRC32 doesn't match, thus the Stream Footer is corrupt.
  • LZMA_OPTIONS_ERROR: Unsupported options are present in Stream Footer.
Note
If Stream Header was already decoded successfully, but decoding Stream Footer returns LZMA_FORMAT_ERROR, the application should probably report some other error message than "file format not recognized", since the file more likely is corrupt (possibly truncated). Stream decoder in liblzma uses LZMA_DATA_ERROR in this situation.

backward_size values are compared only if both are not LZMA_VLI_UNKNOWN.

Returns
- LZMA_OK: Both are equal. If either had backward_size set to LZMA_VLI_UNKNOWN, backward_size values were not compared or validated.
  • LZMA_DATA_ERROR: The structures differ.
  • LZMA_OPTIONS_ERROR: version in either structure is greater than the maximum supported version (currently zero).
  • LZMA_PROG_ERROR: Invalid value, e.g. invalid check or backward_size.

Like lzma_vli_encode(), this function has single-call and multi-call modes.

Parameters
vliPointer to decoded integer. The decoder will initialize it to zero when *vli_pos == 0, so application isn't required to initialize *vli.
vli_posHow many bytes have already been decoded. When starting to decode a new integer in multi-call mode, *vli_pos must be initialized to zero. To use single-call decoding, set vli_pos to NULL.
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos].
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
Returns
Slightly different return values are used in multi-call and single-call modes.

Single-call (vli_pos == NULL):

  • LZMA_OK: Integer successfully decoded.
  • LZMA_DATA_ERROR: Integer is corrupt. This includes hitting the end of the input buffer before the whole integer was decoded; providing no input at all will use LZMA_DATA_ERROR.
  • LZMA_PROG_ERROR: Arguments are not sane.

Multi-call (vli_pos != NULL):

  • LZMA_OK: So far all OK, but the integer is not completely decoded yet.
  • LZMA_STREAM_END: Integer successfully decoded.
  • LZMA_DATA_ERROR: Integer is corrupt.
  • LZMA_BUF_ERROR: No input was provided.
  • LZMA_PROG_ERROR: Arguments are not sane.

This function is supported only when *strm has been initialized with a function that takes a memlimit argument.

liblzma 5.2.3 and earlier has a bug where memlimit value of 0 causes this function to do nothing (leaving the limit unchanged) and still return LZMA_OK. Later versions treat 0 as if 1 had been specified (so lzma_memlimit_get() will return 1 even if you specify 0 here).

liblzma 5.2.6 and earlier had a bug in single-threaded .xz decoder (lzma_stream_decoder()) which made it impossible to continue decoding after LZMA_MEMLIMIT_ERROR even if the limit was increased using lzma_memlimit_set(). Other decoders worked correctly.

Returns
- LZMA_OK: New memory usage limit successfully set.
  • LZMA_MEMLIMIT_ERROR: The new limit is too small. The limit was not changed.
  • LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't support memory usage limit.

Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using LZMA_FINISH is not required. It is supported only for convenience.

Returns
- LZMA_OK: All good, continue with lzma_code().
  • LZMA_PROG_ERROR
  • LZMA_MEM_ERROR

The MicroLZMA format is a raw LZMA stream whose first byte (always 0x00) has been replaced with bitwise-negation of the LZMA properties (lc/lp/pb). This encoding ensures that the first byte of MicroLZMA stream is never 0x00. There is no end of payload marker and thus the uncompressed size must be stored separately. For the best error detection the dictionary size should be stored separately as well but alternatively one may use the uncompressed size as the dictionary size when decoding.

With the MicroLZMA encoder, lzma_code() behaves slightly unusually. The action argument must be LZMA_FINISH and the return value will never be LZMA_OK. Thus the encoding is always done with a single lzma_code() after the initialization. The benefit of the combination of initialization function and lzma_code() is that memory allocations can be re-used for better performance.

lzma_code() will try to encode as much input as is possible to fit into the given output buffer. If not all input can be encoded, the stream will be finished without encoding all the input. The caller must check both input and output buffer usage after lzma_code() (total_in and total_out in lzma_stream can be convenient). Often lzma_code() can fill the output buffer completely if there is a lot of input, but sometimes a few bytes may remain unused because the next LZMA symbol would require more space.

lzma_stream.avail_out must be at least 6. Otherwise LZMA_PROG_ERROR will be returned.

The LZMA dictionary should be reasonably low to speed up the encoder re-initialization. A good value is bigger than the resulting uncompressed size of most of the output chunks. For example, if output size is 4 KiB, dictionary size of 32 KiB or 64 KiB is good. If the data compresses extremely well, even 128 KiB may be useful.

The MicroLZMA format and this encoder variant were made with the EROFS file system in mind. This format may be convenient in other embedded uses too where many small streams are needed. XZ Embedded includes a decoder for this format.

Returns
- LZMA_STREAM_END: All good. Check the amounts of input used and output produced. Store the amount of input used (uncompressed size) as it needs to be known to decompress the data.
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR: In addition to the generic reasons for this error code, this may also be returned if there isn't enough output space (6 bytes) to create a valid MicroLZMA stream.
Parameters
strmPointer to properly prepared lzma_stream
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK, LZMA_CONCATENATED, LZMA_FAIL_FAST
Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
optionsPointer to multithreaded compression options

The decoder can decode multiple Blocks in parallel. This requires that each Block Header contains the Compressed Size and Uncompressed size fields which are added by the multi-threaded encoder, see lzma_stream_encoder_mt().

A Stream with one Block will only utilize one thread. A Stream with multiple Blocks but without size information in Block Headers will be processed in single-threaded mode in the same way as done by lzma_stream_decoder(). Concatenated Streams are processed one Stream at a time; no inter-Stream parallelization is done.

This function behaves like lzma_stream_decoder() when options->threads == 1 and options->memlimit_threading <= 1.

Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
  • LZMA_OPTIONS_ERROR: Unsupported flags.
  • LZMA_PROG_ERROR

This decoder autodetects between the .xz, .lzma, and .lz file formats, and calls lzma_stream_decoder(), lzma_alone_decoder(), or lzma_lzip_decoder() once the type of the input file has been detected.

Support for .lz was added in 5.4.0.

If the flag LZMA_CONCATENATED is used and the input is a .lzma file: For historical reasons concatenated .lzma files aren't supported. If there is trailing data after one .lzma stream, lzma_code() will return LZMA_DATA_ERROR. (lzma_alone_decoder() doesn't have such a check as it doesn't support any decoder flags. It will return LZMA_STREAM_END after one .lzma stream.)

Parameters
strmPointer to properly prepared lzma_stream
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK, LZMA_CONCATENATED, LZMA_FAIL_FAST
Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter.
flagsBitwise-or of flags, or zero for no flags. All decoder flags listed above are supported although only LZMA_CONCATENATED and (in very rare cases) LZMA_IGNORE_CHECK are actually useful. LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, and LZMA_FAIL_FAST do nothing. LZMA_TELL_ANY_CHECK is supported for consistency only as CRC32 is always used in the .lz format.

This decoder supports the .lz format version 0 and the unextended .lz format version 1:

  • Files in the format version 0 were produced by lzip 1.3 and older. Such files aren't common but may be found from file archives as a few source packages were released in this format. People might have old personal files in this format too. Decompression support for the format version 0 was removed in lzip 1.18.
  • lzip 1.3 added decompression support for .lz format version 1 files. Compression support was added in lzip 1.4. In lzip 1.6 the .lz format version 1 was extended to support the Sync Flush marker. This extension is not supported by liblzma. lzma_code() will return LZMA_DATA_ERROR at the location of the Sync Flush marker. In practice files with the Sync Flush marker are very rare and thus liblzma can decompress almost all .lz files.

Just like with lzma_stream_decoder() for .xz files, LZMA_CONCATENATED should be used when decompressing normal standalone .lz files.

The .lz format allows putting non-.lz data at the end of a file after at least one valid .lz member. That is, one can append custom data at the end of a .lz file and the decoder is required to ignore it. In liblzma this is relevant only when LZMA_CONCATENATED is used. In that case lzma_code() will return LZMA_STREAM_END and leave lzma_stream.next_in pointing to the first byte of the non-.lz data. An exception to this is if the first 1-3 bytes of the non-.lz data are identical to the .lz magic bytes (0x4C, 0x5A, 0x49, 0x50; "LZIP" in US-ASCII). In such a case the 1-3 bytes will have been ignored by lzma_code(). If one wishes to locate the non-.lz data reliably, one must ensure that the first byte isn't 0x4C. Actually one should ensure that none of the first four bytes of trailing data are equal to the magic bytes because lzip >= 1.20 requires it by default.

Returns
- LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR
Parameters
memlimitPointer to how much memory the decoder is allowed to allocate. The value pointed by this pointer is modified if and only if LZMA_MEMLIMIT_ERROR is returned.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_IGNORE_CHECK, LZMA_CONCATENATED, LZMA_FAIL_FAST. Note that LZMA_TELL_ANY_CHECK is not allowed and will return LZMA_PROG_ERROR.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
outBeginning of the output buffer
out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if decoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
- LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_DATA_ERROR
  • LZMA_NO_CHECK: This can be returned only if using the LZMA_TELL_NO_CHECK flag.
  • LZMA_UNSUPPORTED_CHECK: This can be returned only if using the LZMA_TELL_UNSUPPORTED_CHECK flag.
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. The minimum required memlimit value was stored to *memlimit.
  • LZMA_BUF_ERROR: Output buffer was too small.
  • LZMA_PROG_ERROR

See lzma_microlzma_decoder() for more information.

The lzma_code() usage with this decoder is completely normal. The special behavior of lzma_code() applies to lzma_microlzma_encoder() only.

Parameters
strmPointer to properly prepared lzma_stream
comp_sizeCompressed size of the MicroLZMA stream. The caller must somehow know this exactly.
uncomp_sizeUncompressed size of the MicroLZMA stream. If the exact uncompressed size isn't known, this can be set to a value that is at most as big as the exact uncompressed size would be, but then the next argument uncomp_size_is_exact must be false.
uncomp_size_is_exactIf true, uncomp_size must be exactly correct. This will improve error detection at the end of the stream. If the exact uncompressed size isn't known, this must be false. uncomp_size must still be at most as big as the exact uncompressed size is. Setting this to false when the exact size is known will work but error detection at the end of the stream will be weaker.
dict_sizeLZMA dictionary size that was used when compressing the data. It is OK to use a bigger value too but liblzma will then allocate more memory than would actually be required and error detection will be slightly worse. (Note that with the implementation in XZ Embedded it doesn't affect the memory usage if one specifies bigger dictionary than actually required.)

This function may be called after lzma_code() has returned LZMA_STREAM_END when LZMA_FULL_BARRIER, LZMA_FULL_FLUSH, or LZMA_SYNC_FLUSH was used:

  • After LZMA_FULL_BARRIER or LZMA_FULL_FLUSH: Single-threaded .xz Stream encoder (lzma_stream_encoder()) and (since liblzma 5.4.0) multi-threaded Stream encoder (lzma_stream_encoder_mt()) allow setting a new filter chain to be used for the next Block(s).
  • After LZMA_SYNC_FLUSH: Raw encoder (lzma_raw_encoder()), Block encocder (lzma_block_encoder()), and single-threaded .xz Stream encoder (lzma_stream_encoder()) allow changing certain filter-specific options in the middle of encoding. The actual filters in the chain (Filter IDs) must not be changed! Currently only the lc, lp, and pb options of LZMA2 (not LZMA1) can be changed this way.
  • In the future some filters might allow changing some of their options without any barrier or flushing but currently such filters don't exist.

This function may also be called when no data has been compressed yet although this is rarely useful. In that case, this function will behave as if LZMA_FULL_FLUSH (Stream encoders) or LZMA_SYNC_FLUSH (Raw or Block encoder) had been used right before calling this function.

Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

Use cases:

  • Verbose output showing the full encoder options to the user (use LZMA_STR_ENCODER in flags)
  • Showing the filters and options that are required to decode a file (use LZMA_STR_DECODER in flags)
  • Showing the filter names without any options in informational messages where the technical details aren't important (no flags). In this case the .options in the filters array are ignored and may be NULL even if a filter has a mandatory options structure.

Note that even if the filter chain was specified using a preset, the resulting filter chain isn't reversed to a preset. So if you specify "6" to lzma_str_to_filters() then lzma_str_from_filters() will produce a string containing "lzma2".

Parameters
strOn success *str will be set to point to an allocated string describing the given filter chain. Old value is ignored. On error *str is always set to NULL.
filtersArray of 1-4 filters and a terminating element with .id = LZMA_VLI_UNKNOWN.
flagsBitwise-or of zero or more of the flags LZMA_STR_ENCODER, LZMA_STR_DECODER, LZMA_STR_GETOPT_LONG, and LZMA_STR_NO_SPACES.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR: Empty filter chain (filters[0].id == LZMA_VLI_UNKNOWN) or the filter chain includes a Filter ID that is not supported by this function.
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

If a filter_id is given then only one line is created which contains the filter name. If LZMA_STR_ENCODER or LZMA_STR_DECODER is used then the options required for encoding or decoding are listed on the same line too.

If filter_id is LZMA_VLI_UNKNOWN then all supported .xz-compatible filters are listed:

  • If neither LZMA_STR_ENCODER nor LZMA_STR_DECODER is used then the supported filter names are listed on a single line separated by spaces.
  • If LZMA_STR_ENCODER or LZMA_STR_DECODER is used then filters and the supported options are listed one filter per line. There won't be a '
    ' after the last filter.
  • If LZMA_STR_ALL_FILTERS is used then the list will include also those filters that cannot be used in the .xz format (LZMA1).
Parameters
strOn success *str will be set to point to an allocated string listing the filters and options. Old value is ignored. On error *str is always set to NULL.
filter_idFilter ID or LZMA_VLI_UNKNOWN.
flagsBitwise-or of zero or more of the flags LZMA_STR_ALL_FILTERS, LZMA_STR_ENCODER, LZMA_STR_DECODER, and LZMA_STR_GETOPT_LONG.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
Returns
- LZMA_OK
  • LZMA_OPTIONS_ERROR: Unsupported filter_id or flags
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to a properly prepared lzma_stream
dest_indexPointer to a pointer where the decoder will put the decoded lzma_index. The old value of *dest_index is ignored (not freed).
memlimitHow much memory the resulting lzma_index is allowed to require. Use UINT64_MAX to effectively disable the limiter.
file_sizeSize of the input .xz file

This decoder decodes the Stream Header, Stream Footer, Index, and Stream Padding field(s) from the input .xz file and stores the resulting combined index in *dest_index. This information can be used to get the uncompressed file size with lzma_index_uncompressed_size(*dest_index) or, for example, to implement random access reading by locating the Blocks in the Streams.

To get the required information from the .xz file, lzma_code() may ask the application to seek in the input file by returning LZMA_SEEK_NEEDED and having the target file position specified in lzma_stream.seek_pos. The number of seeks required depends on the input file and how big buffers the application provides. When possible, the decoder will seek backward and forward in the given buffer to avoid useless seek requests. Thus, if the application provides the whole file at once, no external seeking will be required (that is, lzma_code() won't return LZMA_SEEK_NEEDED).

The value in lzma_stream.total_in can be used to estimate how much data liblzma had to read to get the file information. However, due to seeking and the way total_in is updated, the value of total_in will be somewhat inaccurate (a little too big). Thus, total_in is a good estimate but don't expect to see the same exact value for the same file if you change the input buffer size or switch to a different liblzma version.

Valid ‘action’ arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. You only need to use LZMA_RUN; LZMA_FINISH is only supported because it might be convenient for some applications. If you use LZMA_FINISH and if lzma_code() asks the application to seek, remember to reset ‘action’ back to LZMA_RUN unless you hit the end of the file again.

Possible return values from lzma_code():

  • LZMA_OK: All OK so far, more input needed
  • LZMA_SEEK_NEEDED: Provide more input starting from the absolute file position strm->seek_pos
  • LZMA_STREAM_END: Decoding was successful, *dest_index has been set
  • LZMA_FORMAT_ERROR: The input file is not in the .xz format (the expected magic bytes were not found from the beginning of the file)
  • LZMA_OPTIONS_ERROR: File looks valid but contains headers that aren't supported by this version of liblzma
  • LZMA_DATA_ERROR: File is corrupt
  • LZMA_BUF_ERROR
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR
  • LZMA_PROG_ERROR
Returns
- LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

This function is supported only when *strm has been initialized with a function that takes a memlimit argument.

liblzma 5.2.3 and earlier has a bug where memlimit value of 0 causes this function to do nothing (leaving the limit unchanged) and still return LZMA_OK. Later versions treat 0 as if 1 had been specified (so lzma_memlimit_get() will return 1 even if you specify 0 here).

liblzma 5.2.6 and earlier had a bug in single-threaded .xz decoder (lzma_stream_decoder()) which made it impossible to continue decoding after LZMA_MEMLIMIT_ERROR even if the limit was increased using lzma_memlimit_set(). Other decoders worked correctly.

Returns
Possible lzma_ret values:
  • LZMA_OK: New memory usage limit successfully set.
  • LZMA_MEMLIMIT_ERROR: The new limit is too small. The limit was not changed.
  • LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't support memory usage limit.

The caller must have calculated the size of the Block Header already with lzma_block_header_size(). If a value larger than the one calculated by lzma_block_header_size() is used, the Block Header will be padded to the specified size.

Parameters
blockBlock options to be encoded.
[out]outBeginning of the output buffer. This must be at least block->header_size bytes.
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful. block->header_size bytes were written to output buffer.
  • LZMA_OPTIONS_ERROR: Invalid or unsupported options.
  • LZMA_PROG_ERROR: Invalid arguments, for example block->header_size is invalid or block->filters is NULL.

block->version should (usually) be set to the highest value supported by the application. If the application sets block->version to a value higher than supported by the current liblzma version, this function will downgrade block->version to the highest value supported by it. Thus one should check the value of block->version after calling this function if block->version was set to a non-zero value and the application doesn't otherwise know that the liblzma version being used is new enough to support the specified block->version.

The size of the Block Header must have already been decoded with lzma_block_header_size_decode() macro and stored to block->header_size.

The integrity check type from Stream Header must have been stored to block->check.

block->filters must have been allocated, but they don't need to be initialized (possible existing filter options are not freed).

Parameters
[out]blockDestination for Block options
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() (and also free() if an error occurs).
inBeginning of the input buffer. This must be at least block->header_size bytes.
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful. block->header_size bytes were read from the input buffer.
  • LZMA_OPTIONS_ERROR: The Block Header specifies some unsupported options such as unsupported filters. This can happen also if block->version was set to a too low value compared to what would be required to properly represent the information stored in the Block Header.
  • LZMA_DATA_ERROR: Block Header is corrupt, for example, the CRC32 doesn't match.
  • LZMA_PROG_ERROR: Invalid arguments, for example block->header_size is invalid or block->filters is NULL.

Block Header stores Compressed Size, but Index has Unpadded Size. If the application has already parsed the Index and is now decoding Blocks, it can calculate Compressed Size from Unpadded Size. This function does exactly that with error checking:

  • Compressed Size calculated from Unpadded Size must be positive integer, that is, Unpadded Size must be big enough that after Block Header and Check fields there's still at least one byte for Compressed Size.
  • If Compressed Size was present in Block Header, the new value calculated from Unpadded Size is compared against the value from Block Header.
Note
This function must be called after decoding the Block Header field so that it can properly validate Compressed Size if it was present in Block Header.
Parameters
blockBlock options: block->header_size must already be set with lzma_block_header_size().
unpadded_sizeUnpadded Size from the Index field in bytes
Returns
Possible lzma_ret values:
  • LZMA_OK: block->compressed_size was set successfully.
  • LZMA_DATA_ERROR: unpadded_size is too small compared to block->header_size and lzma_check_size(block->check).
  • LZMA_PROG_ERROR: Some values are invalid. For example, block->header_size must be a multiple of four and between 8 and 1024 inclusive.

Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the filter chain supports it), and LZMA_FINISH.

The Block encoder encodes the Block Data, Block Padding, and Check value. It does NOT encode the Block Header which can be encoded with lzma_block_header_encode().

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
blockBlock options: block->version, block->check, and block->filters must have been initialized.
Returns
Possible lzma_ret values:
  • LZMA_OK: All good, continue with lzma_code().
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID that is not supported by this build of liblzma. Initializing the encoder failed.
  • LZMA_PROG_ERROR

Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using LZMA_FINISH is not required. It is supported only for convenience.

The Block decoder decodes the Block Data, Block Padding, and Check value. It does NOT decode the Block Header which can be decoded with lzma_block_header_decode().

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
blockBlock options
Returns
Possible lzma_ret values:
  • LZMA_OK: All good, continue with lzma_code().
  • LZMA_PROG_ERROR
  • LZMA_MEM_ERROR

In contrast to the multi-call encoder initialized with lzma_block_encoder(), this function encodes also the Block Header. This is required to make it possible to write appropriate Block Header also in case the data isn't compressible, and different filter chain has to be used to encode the data in uncompressed form using uncompressed chunks of the LZMA2 filter.

When the data isn't compressible, header_size, compressed_size, and uncompressed_size are set just like when the data was compressible, but it is possible that header_size is too small to hold the filter chain specified in block->filters, because that isn't necessarily the filter chain that was actually used to encode the data. lzma_block_unpadded_size() still works normally, because it doesn't read the filters array.

Parameters
blockBlock options: block->version, block->check, and block->filters must have been initialized.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

This is like lzma_block_buffer_encode() except this doesn't try to compress the data and instead encodes the data using LZMA2 uncompressed chunks. The required output buffer size can be determined with lzma_block_buffer_bound().

Since the data won't be compressed, this function ignores block->filters. This function doesn't take lzma_allocator because this function doesn't allocate any memory from the heap.

Parameters
blockBlock options: block->version, block->check, and block->filters must have been initialized.
inBeginning of the input buffer
in_sizeSize of the input buffer
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

This is single-call equivalent of lzma_block_decoder(), and requires that the caller has already decoded Block Header and checked its memory usage.

Parameters
blockBlock options
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful.
  • LZMA_OPTIONS_ERROR
  • LZMA_DATA_ERROR
  • LZMA_MEM_ERROR
  • LZMA_BUF_ERROR: Output buffer was too small.
  • LZMA_PROG_ERROR

The maximum required output buffer size can be calculated with lzma_stream_buffer_bound().

Parameters
presetCompression preset to use. See the description in lzma_easy_encoder().
checkType of the integrity check to calculate from uncompressed data.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN. See filters.h for more information.
checkType of the integrity check to calculate from uncompressed data.
Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

This provides the functionality of lzma_easy_encoder() and lzma_stream_encoder() as a single function for multithreaded use.

The supported actions for lzma_code() are LZMA_RUN, LZMA_FULL_FLUSH, LZMA_FULL_BARRIER, and LZMA_FINISH. Support for LZMA_SYNC_FLUSH might be added in the future.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
optionsPointer to multithreaded compression options
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

The .lzma format is sometimes called the LZMA_Alone format, which is the reason for the name of this function. The .lzma format supports only the LZMA1 filter. There is no support for integrity checks like CRC32.

Use this function if and only if you need to create files readable by legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format is strongly recommended.

The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH. No kind of flushing is supported, because the file format doesn't make it possible.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
optionsPointer to encoder options
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Parameters
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN. See filters.h for more information.
checkType of the integrity check to calculate from uncompressed data.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_UNSUPPORTED_CHECK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

The MicroLZMA format is a raw LZMA stream whose first byte (always 0x00) has been replaced with bitwise-negation of the LZMA properties (lc/lp/pb). This encoding ensures that the first byte of MicroLZMA stream is never 0x00. There is no end of payload marker and thus the uncompressed size must be stored separately. For the best error detection the dictionary size should be stored separately as well but alternatively one may use the uncompressed size as the dictionary size when decoding.

With the MicroLZMA encoder, lzma_code() behaves slightly unusually. The action argument must be LZMA_FINISH and the return value will never be LZMA_OK. Thus the encoding is always done with a single lzma_code() after the initialization. The benefit of the combination of initialization function and lzma_code() is that memory allocations can be re-used for better performance.

lzma_code() will try to encode as much input as is possible to fit into the given output buffer. If not all input can be encoded, the stream will be finished without encoding all the input. The caller must check both input and output buffer usage after lzma_code() (total_in and total_out in lzma_stream can be convenient). Often lzma_code() can fill the output buffer completely if there is a lot of input, but sometimes a few bytes may remain unused because the next LZMA symbol would require more space.

lzma_stream.avail_out must be at least 6. Otherwise LZMA_PROG_ERROR will be returned.

The LZMA dictionary should be reasonably low to speed up the encoder re-initialization. A good value is bigger than the resulting uncompressed size of most of the output chunks. For example, if output size is 4 KiB, dictionary size of 32 KiB or 64 KiB is good. If the data compresses extremely well, even 128 KiB may be useful.

The MicroLZMA format and this encoder variant were made with the EROFS file system in mind. This format may be convenient in other embedded uses too where many small streams are needed. XZ Embedded includes a decoder for this format.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
optionsPointer to encoder options
Returns
Possible lzma_ret values:
  • LZMA_STREAM_END: All good. Check the amounts of input used and output produced. Store the amount of input used (uncompressed size) as it needs to be known to decompress the data.
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR: In addition to the generic reasons for this error code, this may also be returned if there isn't enough output space (6 bytes) to create a valid MicroLZMA stream.
Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK, LZMA_CONCATENATED, LZMA_FAIL_FAST
Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR

The decoder can decode multiple Blocks in parallel. This requires that each Block Header contains the Compressed Size and Uncompressed size fields which are added by the multi-threaded encoder, see lzma_stream_encoder_mt().

A Stream with one Block will only utilize one thread. A Stream with multiple Blocks but without size information in Block Headers will be processed in single-threaded mode in the same way as done by lzma_stream_decoder(). Concatenated Streams are processed one Stream at a time; no inter-Stream parallelization is done.

This function behaves like lzma_stream_decoder() when options->threads == 1 and options->memlimit_threading <= 1.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
optionsPointer to multithreaded compression options
Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
  • LZMA_OPTIONS_ERROR: Unsupported flags.
  • LZMA_PROG_ERROR

This decoder autodetects between the .xz, .lzma, and .lz file formats, and calls lzma_stream_decoder(), lzma_alone_decoder(), or lzma_lzip_decoder() once the type of the input file has been detected.

Support for .lz was added in 5.4.0.

If the flag LZMA_CONCATENATED is used and the input is a .lzma file: For historical reasons concatenated .lzma files aren't supported. If there is trailing data after one .lzma stream, lzma_code() will return LZMA_DATA_ERROR. (lzma_alone_decoder() doesn't have such a check as it doesn't support any decoder flags. It will return LZMA_STREAM_END after one .lzma stream.)

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK, LZMA_CONCATENATED, LZMA_FAIL_FAST
Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR

Valid 'action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. There is no need to use LZMA_FINISH, but it's allowed because it may simplify certain types of applications.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

This decoder supports the .lz format version 0 and the unextended .lz format version 1:

  • Files in the format version 0 were produced by lzip 1.3 and older. Such files aren't common but may be found from file archives as a few source packages were released in this format. People might have old personal files in this format too. Decompression support for the format version 0 was removed in lzip 1.18.
  • lzip 1.3 added decompression support for .lz format version 1 files. Compression support was added in lzip 1.4. In lzip 1.6 the .lz format version 1 was extended to support the Sync Flush marker. This extension is not supported by liblzma. lzma_code() will return LZMA_DATA_ERROR at the location of the Sync Flush marker. In practice files with the Sync Flush marker are very rare and thus liblzma can decompress almost all .lz files.

Just like with lzma_stream_decoder() for .xz files, LZMA_CONCATENATED should be used when decompressing normal standalone .lz files.

The .lz format allows putting non-.lz data at the end of a file after at least one valid .lz member. That is, one can append custom data at the end of a .lz file and the decoder is required to ignore it. In liblzma this is relevant only when LZMA_CONCATENATED is used. In that case lzma_code() will return LZMA_STREAM_END and leave lzma_stream.next_in pointing to the first byte of the non-.lz data. An exception to this is if the first 1-3 bytes of the non-.lz data are identical to the .lz magic bytes (0x4C, 0x5A, 0x49, 0x50; "LZIP" in US-ASCII). In such a case the 1-3 bytes will have been ignored by lzma_code(). If one wishes to locate the non-.lz data reliably, one must ensure that the first byte isn't 0x4C. Actually one should ensure that none of the first four bytes of trailing data are equal to the magic bytes because lzip >= 1.20 requires it by default.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
memlimitMemory usage limit as bytes. Use UINT64_MAX to effectively disable the limiter.
flagsBitwise-or of flags, or zero for no flags. All decoder flags listed above are supported although only LZMA_CONCATENATED and (in very rare cases) LZMA_IGNORE_CHECK are actually useful. LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, and LZMA_FAIL_FAST do nothing. LZMA_TELL_ANY_CHECK is supported for consistency only as CRC32 is always used in the .lz format.
Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization was successful.
  • LZMA_MEM_ERROR: Cannot allocate memory.
  • LZMA_OPTIONS_ERROR: Unsupported flags
  • LZMA_PROG_ERROR
Parameters
memlimitPointer to how much memory the decoder is allowed to allocate. The value pointed by this pointer is modified if and only if LZMA_MEMLIMIT_ERROR is returned.
flagsBitwise-or of zero or more of the decoder flags: LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, LZMA_IGNORE_CHECK, LZMA_CONCATENATED, LZMA_FAIL_FAST. Note that LZMA_TELL_ANY_CHECK is not allowed and will return LZMA_PROG_ERROR.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if decoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_DATA_ERROR
  • LZMA_NO_CHECK: This can be returned only if using the LZMA_TELL_NO_CHECK flag.
  • LZMA_UNSUPPORTED_CHECK: This can be returned only if using the LZMA_TELL_UNSUPPORTED_CHECK flag.
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. The minimum required memlimit value was stored to *memlimit.
  • LZMA_BUF_ERROR: Output buffer was too small.
  • LZMA_PROG_ERROR

See lzma_microlzma_encoder() for more information.

The lzma_code() usage with this decoder is completely normal. The special behavior of lzma_code() applies to lzma_microlzma_encoder() only.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
comp_sizeCompressed size of the MicroLZMA stream. The caller must somehow know this exactly.
uncomp_sizeUncompressed size of the MicroLZMA stream. If the exact uncompressed size isn't known, this can be set to a value that is at most as big as the exact uncompressed size would be, but then the next argument uncomp_size_is_exact must be false.
uncomp_size_is_exactIf true, uncomp_size must be exactly correct. This will improve error detection at the end of the stream. If the exact uncompressed size isn't known, this must be false. uncomp_size must still be at most as big as the exact uncompressed size is. Setting this to false when the exact size is known will work but error detection at the end of the stream will be weaker.
dict_sizeLZMA dictionary size that was used when compressing the data. It is OK to use a bigger value too but liblzma will then allocate more memory than would actually be required and error detection will be slightly worse. (Note that with the implementation in XZ Embedded it doesn't affect the memory usage if one specifies bigger dictionary than actually required.)
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

This function may be useful when implementing custom file formats.

The 'action' with lzma_code() can be LZMA_RUN, LZMA_SYNC_FLUSH (if the filter chain supports it), or LZMA_FINISH.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

The initialization of raw decoder goes similarly to raw encoder.

The 'action' with lzma_code() can be LZMA_RUN or LZMA_FINISH. Using LZMA_FINISH is not required, it is supported just for convenience.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR

This function may be called after lzma_code() has returned LZMA_STREAM_END when LZMA_FULL_BARRIER, LZMA_FULL_FLUSH, or LZMA_SYNC_FLUSH was used:

  • After LZMA_FULL_BARRIER or LZMA_FULL_FLUSH: Single-threaded .xz Stream encoder (lzma_stream_encoder()) and (since liblzma 5.4.0) multi-threaded Stream encoder (lzma_stream_encoder_mt()) allow setting a new filter chain to be used for the next Block(s).
  • After LZMA_SYNC_FLUSH: Raw encoder (lzma_raw_encoder()), Block encoder (lzma_block_encoder()), and single-threaded .xz Stream encoder (lzma_stream_encoder()) allow changing certain filter-specific options in the middle of encoding. The actual filters in the chain (Filter IDs) must not be changed! Currently only the lc, lp, and pb options of LZMA2 (not LZMA1) can be changed this way.
  • In the future some filters might allow changing some of their options without any barrier or flushing but currently such filters don't exist.

This function may also be called when no data has been compressed yet although this is rarely useful. In that case, this function will behave as if LZMA_FULL_FLUSH (Stream encoders) or LZMA_SYNC_FLUSH (Raw or Block encoder) had been used right before calling this function.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Note
There is no function to calculate how big output buffer would surely be big enough. (lzma_stream_buffer_bound() works only for lzma_stream_buffer_encode(); raw encoder won't necessarily meet that bound.)
Parameters
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_sizeSize of the input buffer
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR
Parameters
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful.
  • LZMA_BUF_ERROR: Not enough output buffer space.
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

This function may be useful when implementing custom file formats using the raw encoder and decoder.

Note
This function validates the Filter ID, but does not necessarily validate the options. Thus, it is possible that this returns LZMA_OK while the following call to lzma_properties_encode() returns LZMA_OPTIONS_ERROR.
Parameters
[out]sizePointer to uint32_t to hold the size of the properties
filterFilter ID and options (the size of the properties may vary depending on the options)
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_PROG_ERROR
Note
Even this function won't validate more options than actually necessary. Thus, it is possible that encoding the properties succeeds but using the same options to initialize the encoder will fail.
If lzma_properties_size() indicated that the size of the Filter Properties field is zero, calling lzma_properties_encode() is not required, but it won't do any harm either.
Parameters
filterFilter ID and options
[out]propsBuffer to hold the encoded options. The size of the buffer must have been already determined with lzma_properties_size().
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_PROG_ERROR
Parameters
filterfilter->id must have been set to the correct Filter ID. filter->options doesn't need to be initialized (it's not freed by this function). The decoded options will be stored in filter->options; it's application's responsibility to free it when appropriate. filter->options is set to NULL if there are no properties or if an error occurs.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). and in case of an error, also free().
propsInput buffer containing the properties.
props_sizeSize of the properties. This must be the exact size; giving too much or too little input will return LZMA_OPTIONS_ERROR.
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR

Knowing the size of Filter Flags is useful to know when allocating memory to hold the encoded Filter Flags.

Note
If you need to calculate size of List of Filter Flags, you need to loop over every lzma_filter entry.
Parameters
[out]sizePointer to integer to hold the calculated size
filterFilter ID and associated options whose encoded size is to be calculated
Returns
Possible lzma_ret values:
  • LZMA_OK: *size set successfully. Note that this doesn't guarantee that filter->options is valid, thus lzma_filter_flags_encode() may still fail.
  • LZMA_OPTIONS_ERROR: Unknown Filter ID or unsupported options.
  • LZMA_PROG_ERROR: Invalid options

In contrast to some functions, this doesn't allocate the needed buffer. This is due to how this function is used internally by liblzma.

Parameters
filterFilter ID and options to be encoded
[out]outBeginning of the output buffer
[out]out_posout[*out_pos] is the next write position. This is updated by the encoder.
out_sizeout[out_size] is the first byte to not write.
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_OPTIONS_ERROR: Invalid or unsupported options.
  • LZMA_PROG_ERROR: Invalid options or not enough output buffer space (you should have checked it with lzma_filter_flags_size()).

The decoded result is stored into *filter. The old value of filter->options is not free()d. If anything other than LZMA_OK is returned, filter->options is set to NULL.

Parameters
[out]filterDestination filter. The decoded Filter ID will be stored in filter->id. If options are needed they will be allocated and the pointer will be stored in filter->options.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
[out]in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_OPTIONS_ERROR
  • LZMA_MEM_ERROR
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

Use cases:

  • Verbose output showing the full encoder options to the user (use LZMA_STR_ENCODER in flags)
  • Showing the filters and options that are required to decode a file (use LZMA_STR_DECODER in flags)
  • Showing the filter names without any options in informational messages where the technical details aren't important (no flags). In this case the .options in the filters array are ignored and may be NULL even if a filter has a mandatory options structure.

Note that even if the filter chain was specified using a preset, the resulting filter chain isn't reversed to a preset. So if you specify "6" to lzma_str_to_filters() then lzma_str_from_filters() will produce a string containing "lzma2".

Parameters
[out]strOn success *str will be set to point to an allocated string describing the given filter chain. Old value is ignored. On error *str is always set to NULL.
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
flagsBitwise-or of zero or more of the flags LZMA_STR_ENCODER, LZMA_STR_DECODER, LZMA_STR_GETOPT_LONG, and LZMA_STR_NO_SPACES.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_OPTIONS_ERROR: Empty filter chain (filters[0].id == LZMA_VLI_UNKNOWN) or the filter chain includes a Filter ID that is not supported by this function.
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

If a filter_id is given then only one line is created which contains the filter name. If LZMA_STR_ENCODER or LZMA_STR_DECODER is used then the options read by the encoder or decoder are printed on the same line.

If filter_id is LZMA_VLI_UNKNOWN then all supported .xz-compatible filters are listed:

  • If neither LZMA_STR_ENCODER nor LZMA_STR_DECODER is used then the supported filter names are listed on a single line separated by spaces.
  • If LZMA_STR_ENCODER or LZMA_STR_DECODER is used then filters and the supported options are listed one filter per line. There won't be a newline after the last filter.
  • If LZMA_STR_ALL_FILTERS is used then the list will include also those filters that cannot be used in the .xz format (LZMA1).
Parameters
strOn success *str will be set to point to an allocated string listing the filters and options. Old value is ignored. On error *str is always set to NULL.
filter_idFilter ID or LZMA_VLI_UNKNOWN.
flagsBitwise-or of zero or more of the flags LZMA_STR_ALL_FILTERS, LZMA_STR_ENCODER, LZMA_STR_DECODER, and LZMA_STR_GETOPT_LONG.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_OPTIONS_ERROR: Unsupported filter_id or flags
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

Set the Stream Flags of the last (and typically the only) Stream in lzma_index. This can be useful when reading information from the lzma_index, because to decode Blocks, knowing the integrity check type is needed.

Parameters
iPointer to lzma_index structure
stream_flagsPointer to lzma_stream_flags structure. This is copied into the internal preallocated structure, so the caller doesn't need to keep the flags' data available after calling this function.
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_OPTIONS_ERROR: Unsupported stream_flags->version.
  • LZMA_PROG_ERROR

Set the amount of Stream Padding of the last (and typically the only) Stream in the lzma_index. This is needed when planning to do random-access reading within multiple concatenated Streams.

By default, the amount of Stream Padding is assumed to be zero bytes.

Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_DATA_ERROR: The file size would grow too big.
  • LZMA_PROG_ERROR

Concatenating lzma_indexes is useful when doing random-access reading in multi-Stream .xz file, or when combining multiple Streams into single Stream.

Parameters
[out]destlzma_index after which src is appended
srclzma_index to be appended after dest. If this function succeeds, the memory allocated for src is freed or moved to be part of dest, and all iterators pointing to src will become invalid.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
Returns
Possible lzma_ret values:
  • LZMA_OK: lzma_indexes were concatenated successfully. src is now a dangling pointer.
  • LZMA_DATA_ERROR: *dest would grow too big.
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
iPointer to lzma_index which should be encoded.

The valid 'action' values for lzma_code() are LZMA_RUN and LZMA_FINISH. It is enough to use only one of them (you can choose freely).

Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization succeeded, continue with lzma_code().
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to properly prepared lzma_stream
[out]iThe decoded Index will be made available via this pointer. Initially this function will set *i to NULL (the old value is ignored). If decoding succeeds (lzma_code() returns LZMA_STREAM_END), *i will be set to point to a new lzma_index, which the application has to later free with lzma_index_end().
memlimitHow much memory the resulting lzma_index is allowed to require. liblzma 5.2.3 and earlier don't allow 0 here and return LZMA_PROG_ERROR; later versions treat 0 as if 1 had been specified.

Valid 'action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. There is no need to use LZMA_FINISH, but it's allowed because it may simplify certain types of applications.

Returns
Possible lzma_ret values:
  • LZMA_OK: Initialization succeeded, continue with lzma_code().
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR
Note
liblzma 5.2.3 and older list also LZMA_MEMLIMIT_ERROR here but that error code has never been possible from this initialization function.
This function doesn't take allocator argument since all the internal data is allocated on stack.
Parameters
ilzma_index to be encoded
[out]outBeginning of the output buffer
[out]out_posThe next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds.
out_sizeSize of the out buffer; the first byte into which no data is written to is out[out_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_BUF_ERROR: Output buffer is too small. Use lzma_index_size() to find out how much output space is needed.
  • LZMA_PROG_ERROR
Parameters
[out]iIf decoding succeeds, *i will point to a new lzma_index, which the application has to later free with lzma_index_end(). If an error occurs, *i will be NULL. The old value of *i is always ignored and thus doesn't need to be initialized by the caller.
[out]memlimitPointer to how much memory the resulting lzma_index is allowed to require. The value pointed by this pointer is modified if and only if LZMA_MEMLIMIT_ERROR is returned.
allocatorlzma_allocator for custom allocator functions. Set to NULL to use malloc() and free().
inBeginning of the input buffer
in_posThe next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds.
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful.
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. The minimum required memlimit value was stored to *memlimit.
  • LZMA_DATA_ERROR
  • LZMA_PROG_ERROR

This decoder decodes the Stream Header, Stream Footer, Index, and Stream Padding field(s) from the input .xz file and stores the resulting combined index in *dest_index. This information can be used to get the uncompressed file size with lzma_index_uncompressed_size(*dest_index) or, for example, to implement random access reading by locating the Blocks in the Streams.

To get the required information from the .xz file, lzma_code() may ask the application to seek in the input file by returning LZMA_SEEK_NEEDED and having the target file position specified in lzma_stream.seek_pos. The number of seeks required depends on the input file and how big buffers the application provides. When possible, the decoder will seek backward and forward in the given buffer to avoid useless seek requests. Thus, if the application provides the whole file at once, no external seeking will be required (that is, lzma_code() won't return LZMA_SEEK_NEEDED).

The value in lzma_stream.total_in can be used to estimate how much data liblzma had to read to get the file information. However, due to seeking and the way total_in is updated, the value of total_in will be somewhat inaccurate (a little too big). Thus, total_in is a good estimate but don't expect to see the same exact value for the same file if you change the input buffer size or switch to a different liblzma version.

Valid 'action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. You only need to use LZMA_RUN; LZMA_FINISH is only supported because it might be convenient for some applications. If you use LZMA_FINISH and if lzma_code() asks the application to seek, remember to reset 'action' back to LZMA_RUN unless you hit the end of the file again.

Possible return values from lzma_code():

  • LZMA_OK: All OK so far, more input needed
  • LZMA_SEEK_NEEDED: Provide more input starting from the absolute file position strm->seek_pos
  • LZMA_STREAM_END: Decoding was successful, *dest_index has been set
  • LZMA_FORMAT_ERROR: The input file is not in the .xz format (the expected magic bytes were not found from the beginning of the file)
  • LZMA_OPTIONS_ERROR: File looks valid but contains headers that aren't supported by this version of liblzma
  • LZMA_DATA_ERROR: File is corrupt
  • LZMA_BUF_ERROR
  • LZMA_MEM_ERROR
  • LZMA_MEMLIMIT_ERROR
  • LZMA_PROG_ERROR
Parameters
strmPointer to a properly prepared lzma_stream
[out]dest_indexPointer to a pointer where the decoder will put the decoded lzma_index. The old value of *dest_index is ignored (not freed).
memlimitHow much memory the resulting lzma_index is allowed to require. Use UINT64_MAX to effectively disable the limiter.
file_sizeSize of the input .xz file
Returns
Possible lzma_ret values:
  • LZMA_OK
  • LZMA_MEM_ERROR
  • LZMA_PROG_ERROR

After telling the sizes of all Blocks with lzma_index_hash_append(), the actual Index field is decoded with this function. Specifically, once decoding of the Index field has been started, no more Records can be added using lzma_index_hash_append().

This function doesn't use lzma_stream structure to pass the input data. Instead, the input buffer is specified using three arguments. This is because it matches better the internal APIs of liblzma.

Parameters
index_hashPointer to a lzma_index_hash structure
inPointer to the beginning of the input buffer
[out]in_posin[*in_pos] is the next byte to process
in_sizein[in_size] is the first byte not to process
Returns
Possible lzma_ret values:
  • LZMA_OK: So far good, but more input is needed.
  • LZMA_STREAM_END: Index decoded successfully and it matches the Records given with lzma_index_hash_append().
  • LZMA_DATA_ERROR: Index is corrupt or doesn't match the information given with lzma_index_hash_append().
  • LZMA_BUF_ERROR: Cannot progress because *in_pos >= in_size.
  • LZMA_PROG_ERROR
Parameters
optionsStream Footer options to be encoded.
[out]outBeginning of the output buffer of LZMA_STREAM_HEADER_SIZE bytes.
Returns
Possible lzma_ret values:
  • LZMA_OK: Encoding was successful.
  • LZMA_OPTIONS_ERROR: options->version is not supported by this liblzma version.
  • LZMA_PROG_ERROR: Invalid options.

options->backward_size is always set to LZMA_VLI_UNKNOWN. This is to help comparing Stream Flags from Stream Header and Stream Footer with lzma_stream_flags_compare().

Note
When decoding .xz files that contain multiple Streams, it may make sense to print "file format not recognized" only if decoding of the Stream Header of the first Stream gives LZMA_FORMAT_ERROR. If non-first Stream Header gives LZMA_FORMAT_ERROR, the message used for LZMA_DATA_ERROR is probably more appropriate. For example, the Stream decoder in liblzma uses LZMA_DATA_ERROR if LZMA_FORMAT_ERROR is returned by lzma_stream_header_decode() when decoding non-first Stream.
Parameters
[out]optionsTarget for the decoded Stream Header options.
inBeginning of the input buffer of LZMA_STREAM_HEADER_SIZE bytes.
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given buffer cannot be Stream Header.
  • LZMA_DATA_ERROR: CRC32 doesn't match, thus the header is corrupt.
  • LZMA_OPTIONS_ERROR: Unsupported options are present in the header.
Note
If Stream Header was already decoded successfully, but decoding Stream Footer returns LZMA_FORMAT_ERROR, the application should probably report some other error message than "file format not recognized". The file likely is corrupt (possibly truncated). The Stream decoder in liblzma uses LZMA_DATA_ERROR in this situation.
Parameters
[out]optionsTarget for the decoded Stream Footer options.
inBeginning of the input buffer of LZMA_STREAM_HEADER_SIZE bytes.
Returns
Possible lzma_ret values:
  • LZMA_OK: Decoding was successful.
  • LZMA_FORMAT_ERROR: Magic bytes don't match, thus the given buffer cannot be Stream Footer.
  • LZMA_DATA_ERROR: CRC32 doesn't match, thus the Stream Footer is corrupt.
  • LZMA_OPTIONS_ERROR: Unsupported options are present in Stream Footer.

backward_size values are compared only if both are not LZMA_VLI_UNKNOWN.

Parameters
aPointer to lzma_stream_flags structure
bPointer to lzma_stream_flags structure
Returns
Possible lzma_ret values:
  • LZMA_OK: Both are equal. If either had backward_size set to LZMA_VLI_UNKNOWN, backward_size values were not compared or validated.
  • LZMA_DATA_ERROR: The structures differ.
  • LZMA_OPTIONS_ERROR: version in either structure is greater than the maximum supported version (currently zero).
  • LZMA_PROG_ERROR: Invalid value, e.g. invalid check or backward_size.

Like lzma_vli_encode(), this function has single-call and multi-call modes.

Parameters
[out]vliPointer to decoded integer. The decoder will initialize it to zero when *vli_pos == 0, so application isn't required to initialize *vli.
[out]vli_posHow many bytes have already been decoded. When starting to decode a new integer in multi-call mode, *vli_pos must be initialized to zero. To use single-call decoding, set vli_pos to NULL.
inBeginning of the input buffer
[out]in_posThe next byte will be read from in[*in_pos].
in_sizeSize of the input buffer; the first byte that won't be read is in[in_size].
Returns
Slightly different return values are used in multi-call and single-call modes.

Single-call (vli_pos == NULL):

  • LZMA_OK: Integer successfully decoded.
  • LZMA_DATA_ERROR: Integer is corrupt. This includes hitting the end of the input buffer before the whole integer was decoded; providing no input at all will use LZMA_DATA_ERROR.
  • LZMA_PROG_ERROR: Arguments are not sane.

Multi-call (vli_pos != NULL):

  • LZMA_OK: So far all OK, but the integer is not completely decoded yet.
  • LZMA_STREAM_END: Integer successfully decoded.
  • LZMA_DATA_ERROR: Integer is corrupt.
  • LZMA_BUF_ERROR: No input was provided.
  • LZMA_PROG_ERROR: Arguments are not sane.

◆ LZMA_API() [2/3]

LZMA_API ( uint64_t ) const
extern

Get the memory usage of decoder filter chain.

Calculate recommended Block size for multithreaded .xz encoder.

Get the uncompressed size of the file.

Get the total size of the file.

Get the total size of the Blocks.

Get the total size of the Stream.

Get the size of the Index field as bytes.

Get the number of Blocks.

Get the number of Streams.

Calculate the memory usage of an existing lzma_index.

Calculate approximate memory requirements for raw decoder.

Calculate approximate memory usage of multithreaded .xz encoder.

Calculate approximate decoder memory usage of a preset.

Calculate the total encoded size of a Block.

Get the current memory usage limit.

This function is currently supported only when *strm has been initialized with a function that takes a memlimit argument. With other functions, you should use e.g. lzma_raw_encoder_memusage() or lzma_raw_decoder_memusage() to estimate the memory requirements.

This function is useful e.g. after LZMA_MEMLIMIT_ERROR to find out how big the memory usage limit should have been to decode the input. Note that this may give misleading information if decoding .xz Streams that have multiple Blocks, because each Block can have different memory requirements.

Returns
How much memory is currently allocated for the filter decoders. If no filter chain is currently allocated, some non-zero value is still returned, which is less than or equal to what any filter chain would indicate as its memory requirement.

If this function isn't supported by *strm or some other error occurs, zero is returned.

This function is supported only when *strm has been initialized with a function that takes a memlimit argument.

Returns
On success, the current memory usage limit is returned (always non-zero). On error, zero is returned.

This is equivalent to lzma_block_unpadded_size() except that the returned value includes the size of the Block Padding field.

Returns
On success, total encoded size of the Block. On error, zero is returned.

This function is a wrapper for lzma_raw_decoder_memusage().

Parameters
presetCompression preset (level and possible flags)
Returns
Number of bytes of memory required to decompress a file that was compressed using the given preset. If an error occurs, for example due to unsupported preset, UINT64_MAX is returned.

Since doing the encoding in threaded mode doesn't affect the memory requirements of single-threaded decompressor, you can use lzma_easy_decoder_memusage(options->preset) or lzma_raw_decoder_memusage(options->filters) to calculate the decompressor memory requirements.

Parameters
optionsCompression options
Returns
Number of bytes of memory required for encoding with the given options. If an error occurs, for example due to unsupported preset or filter chain, UINT64_MAX is returned.

This function can be used to calculate the memory requirements for Block and Stream decoders too because Block and Stream decoders don't need significantly more memory than raw decoder.

Parameters
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
Returns
Number of bytes of memory required for the given filter chain when decoding. If an error occurs, for example due to unsupported filter chain, UINT64_MAX is returned.

This is a shorthand for lzma_index_memusage(lzma_index_stream_count(i), lzma_index_block_count(i)).

This returns the total number of Blocks in lzma_index. To get number of Blocks in individual Streams, use lzma_index_iter.

This is needed to verify the Backward Size field in the Stream Footer.

If multiple lzma_indexes have been combined, this works as if the Blocks were in a single Stream. This is useful if you are going to combine Blocks from multiple Streams into a single new Stream.

This doesn't include the Stream Header, Stream Footer, Stream Padding, or Index fields.

When no lzma_indexes have been combined with lzma_index_cat() and there is no Stream Padding, this function is identical to lzma_index_stream_size(). If multiple lzma_indexes have been combined, this includes also the headers of each separate Stream and the possible Stream Padding fields.

This function is supported only when *strm has been initialized with a function that takes a memlimit argument.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
Returns
On success, the current memory usage limit is returned (always non-zero). On error, zero is returned.

This is equivalent to lzma_block_unpadded_size() except that the returned value includes the size of the Block Padding field.

Parameters
blockBlock options: block->header_size must already be set with lzma_block_header_size().
Returns
On success, total encoded size of the Block. On error, zero is returned.

This function is a wrapper for lzma_raw_decoder_memusage().

Parameters
presetCompression preset (level and possible flags)
Returns
Number of bytes of memory required to decompress a file that was compressed using the given preset or UINT64_MAX on error.

This calculates a recommended Block size for multithreaded encoding given a filter chain. This is used internally by lzma_stream_encoder_mt() to determine the Block size if the block_size member is not set to the special value of 0 in the lzma_mt options struct.

If one wishes to change the filters between Blocks, this function is helpful to set the block_size member of the lzma_mt struct before calling lzma_stream_encoder_mt(). Since the block_size member represents the maximum possible Block size for the multithreaded .xz encoder, one can use this function to find the maximum recommended Block size based on all planned filter chains. Otherwise, the multithreaded encoder will base its maximum Block size on the first filter chain used (if the block_size member is not set), which may unnecessarily limit the Block size for a later filter chain.

Parameters
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
Returns
Recommended Block size in bytes, or UINT64_MAX if an error occurred.

This function can be used to calculate the memory requirements for Block and Stream decoders too because Block and Stream decoders don't need significantly more memory than raw decoder.

Parameters
filtersArray of filters terminated with .id == LZMA_VLI_UNKNOWN.
Returns
Number of bytes of memory required for the given filter chain when decoding or UINT64_MAX on error.

This is a shorthand for lzma_index_memusage(lzma_index_stream_count(i), lzma_index_block_count(i)).

Parameters
iPointer to lzma_index structure
Returns
Approximate memory in bytes used by the lzma_index structure.
Parameters
iPointer to lzma_index structure
Returns
Number of Streams in the lzma_index

This returns the total number of Blocks in lzma_index. To get number of Blocks in individual Streams, use lzma_index_iter.

Parameters
iPointer to lzma_index structure
Returns
Number of blocks in the lzma_index

This is needed to verify the Backward Size field in the Stream Footer.

Parameters
iPointer to lzma_index structure
Returns
Size in bytes of the Index

If multiple lzma_indexes have been combined, this works as if the Blocks were in a single Stream. This is useful if you are going to combine Blocks from multiple Streams into a single new Stream.

Parameters
iPointer to lzma_index structure
Returns
Size in bytes of the Stream (if all Blocks are combined into one Stream).

This doesn't include the Stream Header, Stream Footer, Stream Padding, or Index fields.

Parameters
iPointer to lzma_index structure
Returns
Size in bytes of all Blocks in the Stream(s)

When no lzma_indexes have been combined with lzma_index_cat() and there is no Stream Padding, this function is identical to lzma_index_stream_size(). If multiple lzma_indexes have been combined, this includes also the headers of each separate Stream and the possible Stream Padding fields.

Parameters
iPointer to lzma_index structure
Returns
Total size of the .xz file in bytes
Parameters
iPointer to lzma_index structure
Returns
Size in bytes of the uncompressed data in the file

Get the memory usage of decoder filter chain.

Calculate CRC64 using the polynomial from the ECMA-182 standard.

This function is used similarly to lzma_crc32(). See its documentation.

Get the memory usage of decoder filter chain.

Calculate CRC64 using the polynomial from the ECMA-182 standard.

This function is used similarly to lzma_crc32().

Parameters
bufPointer to the input buffer
sizeSize of the input buffer
crcPreviously returned CRC value. This is used to calculate the CRC of a big buffer in smaller chunks. Set to zero when starting a new calculation.
Returns
Updated CRC value, which can be passed to this function again to continue CRC calculation.

◆ LZMA_API() [3/3]

LZMA_API ( void )
extern

Free memory allocated for the coder data structures.

Rewind the iterator.

Initialize an iterator.

Get progress information.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.

After lzma_end(strm), strm->internal is guaranteed to be NULL. No other members of the lzma_stream structure are touched.

Note
zlib indicates an error if application end()s unfinished stream structure. liblzma doesn't do this, and assumes that application knows what it is doing.

In single-threaded mode, applications can get progress information from strm->total_in and strm->total_out. In multi-threaded mode this is less useful because a significant amount of both input and output data gets buffered internally by liblzma. This makes total_in and total_out give misleading information and also makes the progress indicator updates non-smooth.

This function gives realistic progress information also in multi-threaded mode by taking into account the progress made by each thread. In single-threaded mode *progress_in and *progress_out are set to strm->total_in and strm->total_out, respectively.

Parameters
iterPointer to a lzma_index_iter structure
ilzma_index to which the iterator will be associated

This function associates the iterator with the given lzma_index, and calls lzma_index_iter_rewind() on the iterator.

This function doesn't allocate any memory, thus there is no lzma_index_iter_end(). The iterator is valid as long as the associated lzma_index is valid, that is, until lzma_index_end() or using it as source in lzma_index_cat(). Specifically, lzma_index doesn't become invalid if new Blocks are added to it with lzma_index_append() or if it is used as the destination in lzma_index_cat().

It is safe to make copies of an initialized lzma_index_iter, for example, to easily restart reading at some particular position.

Rewind the iterator so that next call to lzma_index_iter_next() will return the first Block or Stream.

In single-threaded mode, applications can get progress information from strm->total_in and strm->total_out. In multi-threaded mode this is less useful because a significant amount of both input and output data gets buffered internally by liblzma. This makes total_in and total_out give misleading information and also makes the progress indicator updates non-smooth.

This function gives realistic progress information also in multi-threaded mode by taking into account the progress made by each thread. In single-threaded mode *progress_in and *progress_out are set to strm->total_in and strm->total_out, respectively.

Parameters
strmPointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT.
[out]progress_inPointer to the number of input bytes processed.
[out]progress_outPointer to the number of output bytes processed.

This function associates the iterator with the given lzma_index, and calls lzma_index_iter_rewind() on the iterator.

This function doesn't allocate any memory, thus there is no lzma_index_iter_end(). The iterator is valid as long as the associated lzma_index is valid, that is, until lzma_index_end() or using it as source in lzma_index_cat(). Specifically, lzma_index doesn't become invalid if new Blocks are added to it with lzma_index_append() or if it is used as the destination in lzma_index_cat().

It is safe to make copies of an initialized lzma_index_iter, for example, to easily restart reading at some particular position.

Parameters
iterPointer to a lzma_index_iter structure
ilzma_index to which the iterator will be associated

Rewind the iterator so that next call to lzma_index_iter_next() will return the first Block or Stream.

Parameters
iterPointer to a lzma_index_iter structure

Variable Documentation

◆ lzma_attr_warn_unused_result

lzma_action action lzma_nothrow lzma_attr_warn_unused_result

◆ lzma_nothrow

uint64_t memlimit lzma_nothrow

◆ progress_in

uint64_t* progress_in