3 * \brief LZMA1 and LZMA2 filters
9 * This file has been put into the public domain.
10 * You can do whatever you want with this file.
12 * See ../lzma.h for information about liblzma as a whole.
15 #ifndef LZMA_H_INTERNAL
16 # error Never include this file directly. Use <lzma.h> instead.
21 * \brief LZMA1 Filter ID
23 * LZMA1 is the very same thing as what was called just LZMA in LZMA Utils,
24 * 7-Zip, and LZMA SDK. It's called LZMA1 here to prevent developers from
25 * accidentally using LZMA when they actually want LZMA2.
27 * LZMA1 shouldn't be used for new applications unless you _really_ know
28 * what you are doing. LZMA2 is almost always a better choice.
30 #define LZMA_FILTER_LZMA1 LZMA_VLI_C(0x4000000000000001)
33 * \brief LZMA2 Filter ID
35 * Usually you want this instead of LZMA1. Compared to LZMA1, LZMA2 adds
36 * support for LZMA_SYNC_FLUSH, uncompressed chunks (smaller expansion
37 * when trying to compress uncompressible data), possibility to change
38 * lc/lp/pb in the middle of encoding, and some other internal improvements.
40 #define LZMA_FILTER_LZMA2 LZMA_VLI_C(0x21)
44 * \brief Match finders
46 * Match finder has major effect on both speed and compression ratio.
47 * Usually hash chains are faster than binary trees.
49 * The memory usage formulas are only rough estimates, which are closest to
50 * reality when dict_size is a power of two. The formulas are more complex
51 * in reality, and can also change a little between liblzma versions. Use
52 * lzma_memusage_encoder() to get more accurate estimate of memory usage.
57 * \brief Hash Chain with 2- and 3-byte hashing
62 * - dict_size <= 16 MiB: dict_size * 7.5
63 * - dict_size > 16 MiB: dict_size * 5.5 + 64 MiB
68 * \brief Hash Chain with 2-, 3-, and 4-byte hashing
72 * Memory usage: dict_size * 7.5
77 * \brief Binary Tree with 2-byte hashing
81 * Memory usage: dict_size * 9.5
86 * \brief Binary Tree with 2- and 3-byte hashing
91 * - dict_size <= 16 MiB: dict_size * 11.5
92 * - dict_size > 16 MiB: dict_size * 9.5 + 64 MiB
97 * \brief Binary Tree with 2-, 3-, and 4-byte hashing
101 * Memory usage: dict_size * 11.5
107 * \brief Test if given match finder is supported
109 * Return true if the given match finder is supported by this liblzma build.
110 * Otherwise false is returned. It is safe to call this with a value that
111 * isn't listed in lzma_match_finder enumeration; the return value will be
114 * There is no way to list which match finders are available in this
115 * particular liblzma version and build. It would be useless, because
116 * a new match finder, which the application developer wasn't aware,
117 * could require giving additional options to the encoder that the older
118 * match finders don't need.
120 extern LZMA_API(lzma_bool) lzma_mf_is_supported(lzma_match_finder match_finder)
121 lzma_nothrow lzma_attr_const;
125 * \brief Compression modes
127 * This selects the function used to analyze the data produced by the match
133 * \brief Fast compression
135 * Fast mode is usually at its best when combined with
136 * a hash chain match finder.
141 * \brief Normal compression
143 * This is usually notably slower than fast mode. Use this
144 * together with binary tree match finders to expose the
145 * full potential of the LZMA1 or LZMA2 encoder.
151 * \brief Test if given compression mode is supported
153 * Return true if the given compression mode is supported by this liblzma
154 * build. Otherwise false is returned. It is safe to call this with a value
155 * that isn't listed in lzma_mode enumeration; the return value will be false.
157 * There is no way to list which modes are available in this particular
158 * liblzma version and build. It would be useless, because a new compression
159 * mode, which the application developer wasn't aware, could require giving
160 * additional options to the encoder that the older modes don't need.
162 extern LZMA_API(lzma_bool) lzma_mode_is_supported(lzma_mode mode)
163 lzma_nothrow lzma_attr_const;
167 * \brief Options specific to the LZMA1 and LZMA2 filters
169 * Since LZMA1 and LZMA2 share most of the code, it's simplest to share
170 * the options structure too. For encoding, all but the reserved variables
171 * need to be initialized unless specifically mentioned otherwise.
173 * For raw decoding, both LZMA1 and LZMA2 need dict_size, preset_dict, and
174 * preset_dict_size (if preset_dict != NULL). LZMA1 needs also lc, lp, and pb.
178 * \brief Dictionary size in bytes
180 * Dictionary size indicates how many bytes of the recently processed
181 * uncompressed data is kept in memory. One method to reduce size of
182 * the uncompressed data is to store distance-length pairs, which
183 * indicate what data to repeat from the dictionary buffer. Thus,
184 * the bigger the dictionary, the better the compression ratio
187 * Maximum size of the dictionary depends on multiple things:
188 * - Memory usage limit
189 * - Available address space (not a problem on 64-bit systems)
190 * - Selected match finder (encoder only)
192 * Currently the maximum dictionary size for encoding is 1.5 GiB
193 * (i.e. (UINT32_C(1) << 30) + (UINT32_C(1) << 29)) even on 64-bit
194 * systems for certain match finder implementation reasons. In the
195 * future, there may be match finders that support bigger
198 * Decoder already supports dictionaries up to 4 GiB - 1 B (i.e.
199 * UINT32_MAX), so increasing the maximum dictionary size of the
200 * encoder won't cause problems for old decoders.
202 * Because extremely small dictionaries sizes would have unneeded
203 * overhead in the decoder, the minimum dictionary size is 4096 bytes.
205 * \note When decoding, too big dictionary does no other harm
206 * than wasting memory.
209 # define LZMA_DICT_SIZE_MIN UINT32_C(4096)
210 # define LZMA_DICT_SIZE_DEFAULT (UINT32_C(1) << 23)
213 * \brief Pointer to an initial dictionary
215 * It is possible to initialize the LZ77 history window using
216 * a preset dictionary. It is useful when compressing many
217 * similar, relatively small chunks of data independently from
218 * each other. The preset dictionary should contain typical
219 * strings that occur in the files being compressed. The most
220 * probable strings should be near the end of the preset dictionary.
222 * This feature should be used only in special situations. For
223 * now, it works correctly only with raw encoding and decoding.
224 * Currently none of the container formats supported by
225 * liblzma allow preset dictionary when decoding, thus if
226 * you create a .xz or .lzma file with preset dictionary, it
227 * cannot be decoded with the regular decoder functions. In the
228 * future, the .xz format will likely get support for preset
231 const uint8_t *preset_dict;
234 * \brief Size of the preset dictionary
236 * Specifies the size of the preset dictionary. If the size is
237 * bigger than dict_size, only the last dict_size bytes are
240 * This variable is read only when preset_dict is not NULL.
241 * If preset_dict is not NULL but preset_dict_size is zero,
242 * no preset dictionary is used (identical to only setting
243 * preset_dict to NULL).
245 uint32_t preset_dict_size;
248 * \brief Number of literal context bits
250 * How many of the highest bits of the previous uncompressed
251 * eight-bit byte (also known as `literal') are taken into
252 * account when predicting the bits of the next literal.
256 * There is a limit that applies to literal context bits and literal
257 * position bits together: lc + lp <= 4. Without this limit the
258 * decoding could become very slow, which could have security related
259 * results in some cases like email servers doing virus scanning.
260 * This limit also simplifies the internal implementation in liblzma.
262 * There may be LZMA1 streams that have lc + lp > 4 (maximum possible
263 * lc would be 8). It is not possible to decode such streams with
267 # define LZMA_LCLP_MIN 0
268 # define LZMA_LCLP_MAX 4
269 # define LZMA_LC_DEFAULT 3
272 * \brief Number of literal position bits
274 * How many of the lowest bits of the current position (number
275 * of bytes from the beginning of the uncompressed data) in the
276 * uncompressed data is taken into account when predicting the
277 * bits of the next literal (a single eight-bit byte).
282 # define LZMA_LP_DEFAULT 0
285 * \brief Number of position bits
287 * How many of the lowest bits of the current position in the
288 * uncompressed data is taken into account when estimating
289 * probabilities of matches. A match is a sequence of bytes for
290 * which a matching sequence is found from the dictionary and
291 * thus can be stored as distance-length pair.
293 * Example: If most of the matches occur at byte positions of
294 * 8 * n + 3, that is, 3, 11, 19, ... set pb to 3, because 2**3 == 8.
297 # define LZMA_PB_MIN 0
298 # define LZMA_PB_MAX 4
299 # define LZMA_PB_DEFAULT 2
301 /** Compression mode */
305 * \brief Nice length of a match
307 * This determines how many bytes the encoder compares from the match
308 * candidates when looking for the best match. Once a match of at
309 * least nice_len bytes long is found, the encoder stops looking for
310 * better candidates and encodes the match. (Naturally, if the found
311 * match is actually longer than nice_len, the actual length is
312 * encoded; it's not truncated to nice_len.)
314 * Bigger values usually increase the compression ratio and
315 * compression time. For most files, 32 to 128 is a good value,
316 * which gives very good compression ratio at good speed.
318 * The exact minimum value depends on the match finder. The maximum
319 * is 273, which is the maximum length of a match that LZMA1 and
324 /** Match finder ID */
325 lzma_match_finder mf;
328 * \brief Maximum search depth in the match finder
330 * For every input byte, match finder searches through the hash chain
331 * or binary tree in a loop, each iteration going one step deeper in
332 * the chain or tree. The searching stops if
333 * - a match of at least nice_len bytes long is found;
334 * - all match candidates from the hash chain or binary tree have
336 * - maximum search depth is reached.
338 * Maximum search depth is needed to prevent the match finder from
339 * wasting too much time in case there are lots of short match
340 * candidates. On the other hand, stopping the search before all
341 * candidates have been checked can reduce compression ratio.
343 * Setting depth to zero tells liblzma to use an automatic default
344 * value, that depends on the selected match finder and nice_len.
345 * The default is in the range [10, 200] or so (it may vary between
348 * Using a bigger depth value than the default can increase
349 * compression ratio in some cases. There is no strict maximum value,
350 * but high values (thousands or millions) should be used with care:
351 * the encoder could remain fast enough with typical input, but
352 * malicious input could cause the match finder to slow down
353 * dramatically, possibly creating a denial of service attack.
358 * Reserved space to allow possible future extensions without
359 * breaking the ABI. You should not touch these, because the names
360 * of these variables may change. These are and will never be used
361 * with the currently supported options, so it is safe to leave these
366 uint32_t reserved_int1;
367 uint32_t reserved_int2;
368 uint32_t reserved_int3;
369 uint32_t reserved_int4;
370 uint32_t reserved_int5;
371 uint32_t reserved_int6;
372 uint32_t reserved_int7;
373 uint32_t reserved_int8;
374 lzma_reserved_enum reserved_enum1;
375 lzma_reserved_enum reserved_enum2;
376 lzma_reserved_enum reserved_enum3;
377 lzma_reserved_enum reserved_enum4;
383 * \brief Set a compression preset to lzma_options_lzma structure
385 * 0 is the fastest and 9 is the slowest. These match the switches -0 .. -9
386 * of the xz command line tool. In addition, it is possible to bitwise-or
387 * flags to the preset. Currently only LZMA_PRESET_EXTREME is supported.
388 * The flags are defined in container.h, because the flags are used also
389 * with lzma_easy_encoder().
391 * The preset values are subject to changes between liblzma versions.
393 * This function is available only if LZMA1 or LZMA2 encoder has been enabled
394 * when building liblzma.
396 extern LZMA_API(lzma_bool) lzma_lzma_preset(
397 lzma_options_lzma *options, uint32_t preset) lzma_nothrow;