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 earlier
24 * LZMA Utils, 7-Zip, and LZMA SDK. It's called LZMA1 here to prevent
25 * developers from accidentally using LZMA when they actually want LZMA2.
27 #define LZMA_FILTER_LZMA1 LZMA_VLI_C(0x4000000000000001)
30 * \brief LZMA2 Filter ID
32 * Usually you want this instead of LZMA1. Compared to LZMA1, LZMA2 adds
33 * support for LZMA_SYNC_FLUSH, uncompressed chunks (expands uncompressible
34 * data less), possibility to change lc/lp/pb in the middle of encoding, and
35 * some other internal improvements.
37 #define LZMA_FILTER_LZMA2 LZMA_VLI_C(0x21)
41 * \brief Match finders
43 * Match finder has major effect on both speed and compression ratio.
44 * Usually hash chains are faster than binary trees.
46 * The memory usage formulas are only rough estimates, which are closest to
47 * reality when dict_size is a power of two. The formulas are more complex
48 * in reality, and can also change a little between liblzma versions. Use
49 * lzma_memusage_encoder() to get more accurate estimate of memory usage.
54 * \brief Hash Chain with 2- and 3-byte hashing
59 * - dict_size <= 16 MiB: dict_size * 7.5
60 * - dict_size > 16 MiB: dict_size * 5.5 + 64 MiB
65 * \brief Hash Chain with 2-, 3-, and 4-byte hashing
69 * Memory usage: dict_size * 7.5
74 * \brief Binary Tree with 2-byte hashing
78 * Memory usage: dict_size * 9.5
83 * \brief Binary Tree with 2- and 3-byte hashing
88 * - dict_size <= 16 MiB: dict_size * 11.5
89 * - dict_size > 16 MiB: dict_size * 9.5 + 64 MiB
94 * \brief Binary Tree with 2-, 3-, and 4-byte hashing
98 * Memory usage: dict_size * 11.5
104 * \brief Test if given match finder is supported
106 * Returns true if the given match finder is supported by this liblzma build.
107 * Otherwise false is returned. It is safe to call this with a value that
108 * isn't listed in lzma_match_finder enumeration; the return value will be
111 * There is no way to list which match finders are available in this
112 * particular liblzma version and build. It would be useless, because
113 * a new match finder, which the application developer wasn't aware,
114 * could require giving additional options to the encoder that the older
115 * match finders don't need.
117 extern LZMA_API(lzma_bool) lzma_mf_is_supported(lzma_match_finder match_finder)
122 * \brief LZMA compression modes
124 * This selects the function used to analyze the data produced by the match
130 * \brief Fast compression
132 * Fast mode is usually at its best when combined with
133 * a hash chain match finder.
138 * \brief Normal compression
140 * This is usually notably slower than fast mode. Use this
141 * together with binary tree match finders to expose the
142 * full potential of the LZMA encoder.
148 * \brief Test if given compression mode is supported
150 * Returns true if the given compression mode is supported by this liblzma
151 * build. Otherwise false is returned. It is safe to call this with a value
152 * that isn't listed in lzma_mode enumeration; the return value will be false.
154 * There is no way to list which modes are available in this particular
155 * liblzma version and build. It would be useless, because a new compression
156 * mode, which the application developer wasn't aware, could require giving
157 * additional options to the encoder that the older modes don't need.
159 extern LZMA_API(lzma_bool) lzma_mode_is_supported(lzma_mode mode)
164 * \brief Options specific to the LZMA1 and LZMA2 filters
166 * Since LZMA1 and LZMA2 share most of the code, it's simplest to share
167 * the options structure too. For encoding, all but the reserved variables
168 * need to be initialized unless specifically mentioned otherwise.
170 * For raw decoding, both LZMA1 and LZMA2 need dict_size, preset_dict, and
171 * preset_dict_size (if preset_dict != NULL). LZMA1 needs also lc, lp, and pb.
175 * \brief Dictionary size in bytes
177 * Dictionary size indicates how many bytes of the recently processed
178 * uncompressed data is kept in memory. One method to reduce size of
179 * the uncompressed data is to store distance-length pairs, which
180 * indicate what data to repeat from the dictionary buffer. Thus,
181 * the bigger the dictionary, the better compression ratio usually is.
183 * Maximum size of the dictionary depends on multiple things:
184 * - Memory usage limit
185 * - Available address space (not a problem on 64-bit systems)
186 * - Selected match finder (encoder only)
188 * Currently the maximum dictionary size for encoding is 1.5 GiB
189 * (i.e. (UINT32_C(1) << 30) + (UINT32_C(1) << 29)) even on 64-bit
190 * systems for certain match finder implementation reasons. In future,
191 * there may be match finders that support bigger dictionaries (3 GiB
192 * will probably be the maximum).
194 * Decoder already supports dictionaries up to 4 GiB - 1 B (i.e.
195 * UINT32_MAX), so increasing the maximum dictionary size of the
196 * encoder won't cause problems for old decoders.
198 * Because extremely small dictionaries sizes would have unneeded
199 * overhead in the decoder, the minimum dictionary size is 4096 bytes.
201 * \note When decoding, too big dictionary does no other harm
202 * than wasting memory.
205 # define LZMA_DICT_SIZE_MIN UINT32_C(4096)
206 # define LZMA_DICT_SIZE_DEFAULT (UINT32_C(1) << 23)
209 * \brief Pointer to an initial dictionary
211 * It is possible to initialize the LZ77 history window using
212 * a preset dictionary. Here is a good quote from zlib's
213 * documentation; this applies to LZMA as is:
215 * "The dictionary should consist of strings (byte sequences) that
216 * are likely to be encountered later in the data to be compressed,
217 * with the most commonly used strings preferably put towards the
218 * end of the dictionary. Using a dictionary is most useful when
219 * the data to be compressed is short and can be predicted with
220 * good accuracy; the data can then be compressed better than
221 * with the default empty dictionary."
222 * (From deflateSetDictionary() in zlib.h of zlib version 1.2.3)
224 * This feature should be used only in special situations.
225 * It works correctly only with raw encoding and decoding.
226 * Currently none of the container formats supported by
227 * liblzma allow preset dictionary when decoding, thus if
228 * you create a .lzma file with preset dictionary, it cannot
229 * be decoded with the regular .lzma decoder functions.
231 * \todo This feature is not implemented yet.
233 const uint8_t *preset_dict;
236 * \brief Size of the preset dictionary
238 * Specifies the size of the preset dictionary. If the size is
239 * bigger than dict_size, only the last dict_size bytes are processed.
241 * This variable is read only when preset_dict is not NULL.
243 uint32_t preset_dict_size;
246 * \brief Number of literal context bits
248 * How many of the highest bits of the previous uncompressed
249 * eight-bit byte (also known as `literal') are taken into
250 * account when predicting the bits of the next literal.
254 * There is a limit that applies to literal context bits and literal
255 * position bits together: lc + lp <= 4. Without this limit the
256 * decoding could become very slow, which could have security related
257 * results in some cases like email servers doing virus scanning.
258 * This limit also simplifies the internal implementation in liblzma.
260 * There may be LZMA streams that have lc + lp > 4 (maximum lc
261 * possible would be 8). It is not possible to decode such streams
265 # define LZMA_LCLP_MIN 0
266 # define LZMA_LCLP_MAX 4
267 # define LZMA_LC_DEFAULT 3
270 * \brief Number of literal position bits
272 * How many of the lowest bits of the current position (number
273 * of bytes from the beginning of the uncompressed data) in the
274 * uncompressed data is taken into account when predicting the
275 * bits of the next literal (a single eight-bit byte).
280 # define LZMA_LP_DEFAULT 0
283 * \brief Number of position bits
285 * How many of the lowest bits of the current position in the
286 * uncompressed data is taken into account when estimating
287 * probabilities of matches. A match is a sequence of bytes for
288 * which a matching sequence is found from the dictionary and
289 * thus can be stored as distance-length pair.
291 * Example: If most of the matches occur at byte positions of
292 * 8 * n + 3, that is, 3, 11, 19, ... set pb to 3, because 2**3 == 8.
295 # define LZMA_PB_MIN 0
296 # define LZMA_PB_MAX 4
297 # define LZMA_PB_DEFAULT 2
300 * \brief Indicate if the options structure is persistent
302 * If this is true, the application must keep this options structure
303 * available after the LZMA2 encoder has been initialized. With
304 * persistent structure it is possible to change some encoder options
305 * in the middle of the encoding process without resetting the encoder.
307 * This option is used only by LZMA2. LZMA1 ignores this and it is
308 * safe to not initialize this when encoding with LZMA1.
310 lzma_bool persistent;
312 /** LZMA compression mode */
316 * \brief Nice length of a match
318 * This determines how many bytes the encoder compares from the match
319 * candidates when looking for the best match. Once a match of at
320 * least nice_len bytes long is found, the encoder stops looking for
321 * better condidates and encodes the match. (Naturally, if the found
322 * match is actually longer than nice_len, the actual length is
323 * encoded; it's not truncated to nice_len.)
325 * Bigger values usually increase the compression ratio and
326 * compression time. For most files, 30 to 100 is a good value,
327 * which gives very good compression ratio at good speed.
329 * The exact minimum value depends on the match finder. The maximum is
330 * 273, which is the maximum length of a match that LZMA can encode.
334 /** Match finder ID */
335 lzma_match_finder mf;
338 * \brief Maximum search depth in the match finder
340 * For every input byte, match finder searches through the hash chain
341 * or binary tree in a loop, each iteration going one step deeper in
342 * the chain or tree. The searching stops if
343 * - a match of at least nice_len bytes long is found;
344 * - all match candidates from the hash chain or binary tree have
346 * - maximum search depth is reached.
348 * Maximum search depth is needed to prevent the match finder from
349 * wasting too much time in case there are lots of short match
350 * candidates. On the other hand, stopping the search before all
351 * candidates have been checked can reduce compression ratio.
353 * Setting depth to zero tells liblzma to use an automatic default
354 * value, that depends on the selected match finder and nice_len.
355 * The default is in the range [10, 200] or so (it may vary between
358 * Using a bigger depth value than the default can increase
359 * compression ratio in some cases. There is no strict maximum value,
360 * but high values (thousands or millions) should be used with care:
361 * the encoder could remain fast enough with typical input, but
362 * malicious input could cause the match finder to slow down
363 * dramatically, possibly creating a denial of service attack.
368 * Reserved space to allow possible future extensions without
369 * breaking the ABI. You should not touch these, because the names
370 * of these variables may change. These are and will never be used
371 * with the currently supported options, so it is safe to leave these
376 uint32_t reserved_int1;
377 uint32_t reserved_int2;
378 uint32_t reserved_int3;
379 uint32_t reserved_int4;
380 uint32_t reserved_int5;
381 uint32_t reserved_int6;
382 uint32_t reserved_int7;
383 uint32_t reserved_int8;
384 lzma_reserved_enum reserved_enum1;
385 lzma_reserved_enum reserved_enum2;
386 lzma_reserved_enum reserved_enum3;
387 lzma_reserved_enum reserved_enum4;
393 * \brief Set a compression preset to lzma_options_lzma structure
395 * 0 is the fastest and 9 is the slowest. These match the switches -0 .. -9
396 * of the xz command line tool. In addition, it is possible to bitwise-or
397 * flags to the preset. Currently only LZMA_PRESET_EXTREME is supported.
398 * The flags are defined in container.h, because the flags are used also
399 * with lzma_easy_encoder().
401 * The preset values are subject to changes between liblzma versions.
403 * This function is available only if LZMA1 or LZMA2 encoder has been enabled
404 * when building liblzma.
406 extern LZMA_API(lzma_bool) lzma_lzma_preset(
407 lzma_options_lzma *options, uint32_t preset);