liblzma (XZ Utils) 5.4.4
|
Common filter related types and functions. More...
Data Structures | |
struct | lzma_filter |
Filter options. More... | |
Macros | |
#define | LZMA_FILTERS_MAX 4 |
Maximum number of filters in a chain. | |
#define | LZMA_STR_ALL_FILTERS UINT32_C(0x01) |
Allow or show all filters. | |
#define | LZMA_STR_NO_VALIDATION UINT32_C(0x02) |
Do not validate the filter chain in lzma_str_to_filters() | |
#define | LZMA_STR_ENCODER UINT32_C(0x10) |
Stringify encoder options. | |
#define | LZMA_STR_DECODER UINT32_C(0x20) |
Stringify decoder options. | |
#define | LZMA_STR_GETOPT_LONG UINT32_C(0x40) |
Produce xz-compatible getopt_long() syntax. | |
#define | LZMA_STR_NO_SPACES UINT32_C(0x80) |
Use two dashes "--" instead of a space to separate filters. | |
Functions | |
lzma_bool | lzma_filter_encoder_is_supported (lzma_vli id) lzma_nothrow lzma_attr_const |
Test if the given Filter ID is supported for encoding. | |
lzma_bool | lzma_filter_decoder_is_supported (lzma_vli id) lzma_nothrow lzma_attr_const |
Test if the given Filter ID is supported for decoding. | |
lzma_ret | lzma_filters_copy (const lzma_filter *src, lzma_filter *dest, const lzma_allocator *allocator) lzma_nothrow lzma_attr_warn_unused_result |
Copy the filters array. | |
void | lzma_filters_free (lzma_filter *filters, const lzma_allocator *allocator) lzma_nothrow |
Free the options in the array of lzma_filter structures. | |
uint64_t | lzma_raw_encoder_memusage (const lzma_filter *filters) lzma_nothrow lzma_attr_pure |
Calculate approximate memory requirements for raw encoder. | |
uint64_t | lzma_raw_decoder_memusage (const lzma_filter *filters) lzma_nothrow lzma_attr_pure |
Calculate approximate memory requirements for raw decoder. | |
lzma_ret | lzma_raw_encoder (lzma_stream *strm, const lzma_filter *filters) lzma_nothrow lzma_attr_warn_unused_result |
Initialize raw encoder. | |
lzma_ret | lzma_raw_decoder (lzma_stream *strm, const lzma_filter *filters) lzma_nothrow lzma_attr_warn_unused_result |
Initialize raw decoder. | |
lzma_ret | lzma_filters_update (lzma_stream *strm, const lzma_filter *filters) lzma_nothrow |
Update the filter chain in the encoder. | |
lzma_ret | lzma_raw_buffer_encode (const lzma_filter *filters, const lzma_allocator *allocator, const uint8_t *in, size_t in_size, uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow |
Single-call raw encoder. | |
lzma_ret | lzma_raw_buffer_decode (const lzma_filter *filters, const lzma_allocator *allocator, const uint8_t *in, size_t *in_pos, size_t in_size, uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow |
Single-call raw decoder. | |
lzma_ret | lzma_properties_size (uint32_t *size, const lzma_filter *filter) lzma_nothrow |
Get the size of the Filter Properties field. | |
lzma_ret | lzma_properties_encode (const lzma_filter *filter, uint8_t *props) lzma_nothrow |
Encode the Filter Properties field. | |
lzma_ret | lzma_properties_decode (lzma_filter *filter, const lzma_allocator *allocator, const uint8_t *props, size_t props_size) lzma_nothrow |
Decode the Filter Properties field. | |
lzma_ret | lzma_filter_flags_size (uint32_t *size, const lzma_filter *filter) lzma_nothrow lzma_attr_warn_unused_result |
Calculate encoded size of a Filter Flags field. | |
lzma_ret | lzma_filter_flags_encode (const lzma_filter *filter, uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow lzma_attr_warn_unused_result |
Encode Filter Flags into given buffer. | |
lzma_ret | lzma_filter_flags_decode (lzma_filter *filter, const lzma_allocator *allocator, const uint8_t *in, size_t *in_pos, size_t in_size) lzma_nothrow lzma_attr_warn_unused_result |
Decode Filter Flags from given buffer. | |
const char * | lzma_str_to_filters (const char *str, int *error_pos, lzma_filter *filters, uint32_t flags, const lzma_allocator *allocator) lzma_nothrow lzma_attr_warn_unused_result |
Convert a string to a filter chain. | |
lzma_ret | lzma_str_from_filters (char **str, const lzma_filter *filters, uint32_t flags, const lzma_allocator *allocator) lzma_nothrow lzma_attr_warn_unused_result |
Convert a filter chain to a string. | |
lzma_ret | lzma_str_list_filters (char **str, lzma_vli filter_id, uint32_t flags, const lzma_allocator *allocator) lzma_nothrow lzma_attr_warn_unused_result |
List available filters and/or their options (for help message) | |
Common filter related types and functions.
#define LZMA_FILTERS_MAX 4 |
Maximum number of filters in a chain.
A filter chain can have 1-4 filters, of which three are allowed to change the size of the data. Usually only one or two filters are needed.
#define LZMA_STR_ALL_FILTERS UINT32_C(0x01) |
Allow or show all filters.
By default only the filters supported in the .xz format are accept by lzma_str_to_filters() or shown by lzma_str_list_filters().
#define LZMA_STR_NO_VALIDATION UINT32_C(0x02) |
Do not validate the filter chain in lzma_str_to_filters()
By default lzma_str_to_filters() can return an error if the filter chain as a whole isn't usable in the .xz format or in the raw encoder or decoder. With this flag, this validation is skipped. This flag doesn't affect the handling of the individual filter options. To allow non-.xz filters also LZMA_STR_ALL_FILTERS is needed.
#define LZMA_STR_ENCODER UINT32_C(0x10) |
Stringify encoder options.
Show the filter-specific options that the encoder will use. This may be useful for verbose diagnostic messages.
Note that if options were decoded from .xz headers then the encoder options may be undefined. This flag shouldn't be used in such a situation.
#define LZMA_STR_DECODER UINT32_C(0x20) |
Stringify decoder options.
Show the filter-specific options that the decoder will use. This may be useful for showing what filter options were decoded from file headers.
#define LZMA_STR_GETOPT_LONG UINT32_C(0x40) |
Produce xz-compatible getopt_long() syntax.
That is, "delta:dist=2 lzma2:dict=4MiB,pb=1,lp=1" becomes "--delta=dist=2 --lzma2=dict=4MiB,pb=1,lp=1".
This syntax is compatible with xz 5.0.0 as long as the filters and their options are supported too.
#define LZMA_STR_NO_SPACES UINT32_C(0x80) |
Use two dashes "--" instead of a space to separate filters.
That is, "delta:dist=2 lzma2:pb=1,lp=1" becomes "delta:dist=2--lzma2:pb=1,lp=1". This looks slightly odd but this kind of strings should be usable on the command line without quoting. However, it is possible that future versions with new filter options might produce strings that require shell quoting anyway as the exact set of possible characters isn't frozen for now.
It is guaranteed that the single quote (') will never be used in filter chain strings (even if LZMA_STR_NO_SPACES isn't used).
Test if the given Filter ID is supported for encoding.
id | Filter ID |
Test if the given Filter ID is supported for decoding.
id | Filter ID |
lzma_ret lzma_filters_copy | ( | const lzma_filter * | src, |
lzma_filter * | dest, | ||
const lzma_allocator * | allocator | ||
) |
Copy the filters array.
Copy the Filter IDs and filter-specific options from src to dest. Up to LZMA_FILTERS_MAX filters are copied, plus the terminating .id == LZMA_VLI_UNKNOWN. Thus, dest should have at least LZMA_FILTERS_MAX + 1 elements space unless the caller knows that src is smaller than that.
Unless the filter-specific options is NULL, the Filter ID has to be supported by liblzma, because liblzma needs to know the size of every filter-specific options structure. The filter-specific options are not validated. If options is NULL, any unsupported Filter IDs are copied without returning an error.
Old filter-specific options in dest are not freed, so dest doesn't need to be initialized by the caller in any way.
If an error occurs, memory possibly already allocated by this function is always freed. liblzma versions older than 5.2.7 may modify the dest array and leave its contents in an undefined state if an error occurs. liblzma 5.2.7 and newer only modify the dest array when returning LZMA_OK.
src | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. | |
[out] | dest | Destination filter array |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). |
void lzma_filters_free | ( | lzma_filter * | filters, |
const lzma_allocator * | allocator | ||
) |
Free the options in the array of lzma_filter structures.
This frees the filter chain options. The filters array itself is not freed.
The filters array must have at most LZMA_FILTERS_MAX + 1 elements including the terminating element which must have .id = LZMA_VLI_UNKNOWN. For all elements before the terminating element:
If filters is NULL, this does nothing. Again, this never frees the filters array itself.
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). |
uint64_t lzma_raw_encoder_memusage | ( | const lzma_filter * | filters | ) |
Calculate approximate memory requirements for raw encoder.
This function can be used to calculate the memory requirements for Block and Stream encoders too because Block and Stream encoders don't need significantly more memory than raw encoder.
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. |
uint64_t lzma_raw_decoder_memusage | ( | const lzma_filter * | filters | ) |
Calculate approximate memory requirements for raw decoder.
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.
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. |
lzma_ret lzma_raw_encoder | ( | lzma_stream * | strm, |
const lzma_filter * | filters | ||
) |
Initialize raw encoder.
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.
strm | Pointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT. |
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. |
lzma_ret lzma_raw_decoder | ( | lzma_stream * | strm, |
const lzma_filter * | filters | ||
) |
Initialize raw decoder.
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.
strm | Pointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT. |
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. |
lzma_ret lzma_filters_update | ( | lzma_stream * | strm, |
const lzma_filter * | filters | ||
) |
Update the filter chain in the encoder.
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:
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.
strm | Pointer to lzma_stream that is at least initialized with LZMA_STREAM_INIT. |
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. |
lzma_ret lzma_raw_buffer_encode | ( | const lzma_filter * | filters, |
const lzma_allocator * | allocator, | ||
const uint8_t * | in, | ||
size_t | in_size, | ||
uint8_t * | out, | ||
size_t * | out_pos, | ||
size_t | out_size | ||
) |
Single-call raw encoder.
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. | |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). | |
in | Beginning of the input buffer | |
in_size | Size of the input buffer | |
[out] | out | Beginning of the output buffer |
[out] | out_pos | The next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds. |
out_size | Size of the out buffer; the first byte into which no data is written to is out[out_size]. |
lzma_ret lzma_raw_buffer_decode | ( | const lzma_filter * | filters, |
const lzma_allocator * | allocator, | ||
const uint8_t * | in, | ||
size_t * | in_pos, | ||
size_t | in_size, | ||
uint8_t * | out, | ||
size_t * | out_pos, | ||
size_t | out_size | ||
) |
Single-call raw decoder.
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. | |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). | |
in | Beginning of the input buffer | |
in_pos | The next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds. | |
in_size | Size of the input buffer; the first byte that won't be read is in[in_size]. | |
[out] | out | Beginning of the output buffer |
[out] | out_pos | The next byte will be written to out[*out_pos]. *out_pos is updated only if encoding succeeds. |
out_size | Size of the out buffer; the first byte into which no data is written to is out[out_size]. |
lzma_ret lzma_properties_size | ( | uint32_t * | size, |
const lzma_filter * | filter | ||
) |
Get the size of the Filter Properties field.
This function may be useful when implementing custom file formats using the raw encoder and decoder.
[out] | size | Pointer to uint32_t to hold the size of the properties |
filter | Filter ID and options (the size of the properties may vary depending on the options) |
lzma_ret lzma_properties_encode | ( | const lzma_filter * | filter, |
uint8_t * | props | ||
) |
Encode the Filter Properties field.
filter | Filter ID and options | |
[out] | props | Buffer to hold the encoded options. The size of the buffer must have been already determined with lzma_properties_size(). |
lzma_ret lzma_properties_decode | ( | lzma_filter * | filter, |
const lzma_allocator * | allocator, | ||
const uint8_t * | props, | ||
size_t | props_size | ||
) |
Decode the Filter Properties field.
filter | filter->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. |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). and in case of an error, also free(). |
props | Input buffer containing the properties. |
props_size | Size of the properties. This must be the exact size; giving too much or too little input will return LZMA_OPTIONS_ERROR. |
lzma_ret lzma_filter_flags_size | ( | uint32_t * | size, |
const lzma_filter * | filter | ||
) |
Calculate encoded size of a Filter Flags field.
Knowing the size of Filter Flags is useful to know when allocating memory to hold the encoded Filter Flags.
[out] | size | Pointer to integer to hold the calculated size |
filter | Filter ID and associated options whose encoded size is to be calculated |
lzma_ret lzma_filter_flags_encode | ( | const lzma_filter * | filter, |
uint8_t * | out, | ||
size_t * | out_pos, | ||
size_t | out_size | ||
) |
Encode Filter Flags into given buffer.
In contrast to some functions, this doesn't allocate the needed buffer. This is due to how this function is used internally by liblzma.
filter | Filter ID and options to be encoded | |
[out] | out | Beginning of the output buffer |
[out] | out_pos | out[*out_pos] is the next write position. This is updated by the encoder. |
out_size | out[out_size] is the first byte to not write. |
lzma_ret lzma_filter_flags_decode | ( | lzma_filter * | filter, |
const lzma_allocator * | allocator, | ||
const uint8_t * | in, | ||
size_t * | in_pos, | ||
size_t | in_size | ||
) |
Decode Filter Flags from given buffer.
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.
[out] | filter | Destination 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. |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). | |
in | Beginning of the input buffer | |
[out] | in_pos | The next byte will be read from in[*in_pos]. *in_pos is updated only if decoding succeeds. |
in_size | Size of the input buffer; the first byte that won't be read is in[in_size]. |
const char * lzma_str_to_filters | ( | const char * | str, |
int * | error_pos, | ||
lzma_filter * | filters, | ||
uint32_t | flags, | ||
const lzma_allocator * | allocator | ||
) |
Convert a string to a filter chain.
This tries to make it easier to write applications that allow users to set custom compression options. This only handles the filter configuration (including presets) but not the number of threads, block size, check type, or memory limits.
The input string can be either a preset or a filter chain. Presets begin with a digit 0-9 and may be followed by zero or more flags which are lower-case letters. Currently only "e" is supported, matching LZMA_PRESET_EXTREME. For partial xz command line syntax compatibility, a preset string may start with a single dash "-".
A filter chain consists of one or more "filtername:opt1=value1,opt2=value2" strings separated by one or more spaces. Leading and trailing spaces are ignored. All names and values must be lower-case. Extra commas in the option list are ignored. The order of filters is significant: when encoding, the uncompressed input data goes to the leftmost filter first. Normally "lzma2" is the last filter in the chain.
If one wishes to avoid spaces, for example, to avoid shell quoting, it is possible to use two dashes "--" instead of spaces to separate the filters.
For xz command line compatibility, each filter may be prefixed with two dashes "--" and the colon ":" separating the filter name from the options may be replaced with an equals sign "=".
By default, only filters that can be used in the .xz format are accepted. To allow all filters (LZMA1) use the flag LZMA_STR_ALL_FILTERS.
By default, very basic validation is done for the filter chain as a whole, for example, that LZMA2 is only used as the last filter in the chain. The validation isn't perfect though and it's possible that this function succeeds but using the filter chain for encoding or decoding will still result in LZMA_OPTIONS_ERROR. To disable this validation, use the flag LZMA_STR_NO_VALIDATION.
The available filter names and their options are available via lzma_str_list_filters(). See the xz man page for the description of filter names and options.
For command line applications, below is an example how an error message can be displayed. Note the use of an empty string for the field width. If "^" was used there it would create an off-by-one error except at the very beginning of the line.
str | User-supplied string describing a preset or a filter chain. If a default value is needed and you don't know what would be good, use "6" since that is the default preset in xz too. | |
[out] | error_pos | If this isn't NULL, this value will be set on both success and on all errors. This tells the location of the error in the string. This is an int to make it straightforward to use this as printf() field width. The value is guaranteed to be in the range [0, INT_MAX] even if strlen(str) somehow was greater than INT_MAX. |
[out] | filters | An array of lzma_filter structures. There must be LZMA_FILTERS_MAX + 1 (that is, five) elements in the array. The old contents are ignored so it doesn't need to be initialized. This array is modified only if this function returns NULL. Once the allocated filter options are no longer needed, lzma_filters_free() can be used to free the options (it doesn't free the filters array itself). |
flags | Bitwise-or of zero or more of the flags LZMA_STR_ALL_FILTERS and LZMA_STR_NO_VALIDATION. | |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). |
lzma_ret lzma_str_from_filters | ( | char ** | str, |
const lzma_filter * | filters, | ||
uint32_t | flags, | ||
const lzma_allocator * | allocator | ||
) |
Convert a filter chain to a string.
Use cases:
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".
[out] | str | On 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. |
filters | Array of filters terminated with .id == LZMA_VLI_UNKNOWN. | |
flags | Bitwise-or of zero or more of the flags LZMA_STR_ENCODER, LZMA_STR_DECODER, LZMA_STR_GETOPT_LONG, and LZMA_STR_NO_SPACES. | |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). |
lzma_ret lzma_str_list_filters | ( | char ** | str, |
lzma_vli | filter_id, | ||
uint32_t | flags, | ||
const lzma_allocator * | allocator | ||
) |
List available filters and/or their options (for help message)
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:
str | On 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_id | Filter ID or LZMA_VLI_UNKNOWN. |
flags | Bitwise-or of zero or more of the flags LZMA_STR_ALL_FILTERS, LZMA_STR_ENCODER, LZMA_STR_DECODER, and LZMA_STR_GETOPT_LONG. |
allocator | lzma_allocator for custom allocator functions. Set to NULL to use malloc() and free(). |