]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/lzma.h
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / liblzma / api / lzma / lzma.h
1 /**
2  * \file        lzma/lzma.h
3  * \brief       LZMA1 and LZMA2 filters
4  */
5
6 /*
7  * Author: Lasse Collin
8  *
9  * This file has been put into the public domain.
10  * You can do whatever you want with this file.
11  *
12  * See ../lzma.h for information about liblzma as a whole.
13  */
14
15 #ifndef LZMA_H_INTERNAL
16 #       error Never include this file directly. Use <lzma.h> instead.
17 #endif
18
19
20 /**
21  * \brief       LZMA1 Filter ID
22  *
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.
26  */
27 #define LZMA_FILTER_LZMA1       LZMA_VLI_C(0x4000000000000001)
28
29 /**
30  * \brief       LZMA2 Filter ID
31  *
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.
36  */
37 #define LZMA_FILTER_LZMA2       LZMA_VLI_C(0x21)
38
39
40 /**
41  * \brief       Match finders
42  *
43  * Match finder has major effect on both speed and compression ratio.
44  * Usually hash chains are faster than binary trees.
45  *
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.
50  */
51 typedef enum {
52         LZMA_MF_HC3     = 0x03,
53                 /**<
54                  * \brief       Hash Chain with 2- and 3-byte hashing
55                  *
56                  * Minimum nice_len: 3
57                  *
58                  * Memory usage:
59                  *  - dict_size <= 16 MiB: dict_size * 7.5
60                  *  - dict_size > 16 MiB: dict_size * 5.5 + 64 MiB
61                  */
62
63         LZMA_MF_HC4     = 0x04,
64                 /**<
65                  * \brief       Hash Chain with 2-, 3-, and 4-byte hashing
66                  *
67                  * Minimum nice_len: 4
68                  *
69                  * Memory usage: dict_size * 7.5
70                  */
71
72         LZMA_MF_BT2     = 0x12,
73                 /**<
74                  * \brief       Binary Tree with 2-byte hashing
75                  *
76                  * Minimum nice_len: 2
77                  *
78                  * Memory usage: dict_size * 9.5
79                  */
80
81         LZMA_MF_BT3     = 0x13,
82                 /**<
83                  * \brief       Binary Tree with 2- and 3-byte hashing
84                  *
85                  * Minimum nice_len: 3
86                  *
87                  * Memory usage:
88                  *  - dict_size <= 16 MiB: dict_size * 11.5
89                  *  - dict_size > 16 MiB: dict_size * 9.5 + 64 MiB
90                  */
91
92         LZMA_MF_BT4     = 0x14
93                 /**<
94                  * \brief       Binary Tree with 2-, 3-, and 4-byte hashing
95                  *
96                  * Minimum nice_len: 4
97                  *
98                  * Memory usage: dict_size * 11.5
99                  */
100 } lzma_match_finder;
101
102
103 /**
104  * \brief       Test if given match finder is supported
105  *
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
109  * false.
110  *
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.
116  */
117 extern LZMA_API(lzma_bool) lzma_mf_is_supported(lzma_match_finder match_finder)
118                 lzma_attr_const;
119
120
121 /**
122  * \brief       LZMA compression modes
123  *
124  * This selects the function used to analyze the data produced by the match
125  * finder.
126  */
127 typedef enum {
128         LZMA_MODE_FAST = 1,
129                 /**<
130                  * \brief       Fast compression
131                  *
132                  * Fast mode is usually at its best when combined with
133                  * a hash chain match finder.
134                  */
135
136         LZMA_MODE_NORMAL = 2
137                 /**<
138                  * \brief       Normal compression
139                  *
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.
143                  */
144 } lzma_mode;
145
146
147 /**
148  * \brief       Test if given compression mode is supported
149  *
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.
153  *
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.
158  */
159 extern LZMA_API(lzma_bool) lzma_mode_is_supported(lzma_mode mode)
160                 lzma_attr_const;
161
162
163 /**
164  * \brief       Options specific to the LZMA1 and LZMA2 filters
165  *
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.
169  *
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.
172  */
173 typedef struct {
174         /**
175          * \brief       Dictionary size in bytes
176          *
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.
182          *
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)
187          *
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).
193          *
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.
197          *
198          * Because extremely small dictionaries sizes would have unneeded
199          * overhead in the decoder, the minimum dictionary size is 4096 bytes.
200          *
201          * \note        When decoding, too big dictionary does no other harm
202          *              than wasting memory.
203          */
204         uint32_t dict_size;
205 #       define LZMA_DICT_SIZE_MIN       UINT32_C(4096)
206 #       define LZMA_DICT_SIZE_DEFAULT   (UINT32_C(1) << 23)
207
208         /**
209          * \brief       Pointer to an initial dictionary
210          *
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:
214          *
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)
223          *
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.
230          *
231          * \todo        This feature is not implemented yet.
232          */
233         const uint8_t *preset_dict;
234
235         /**
236          * \brief       Size of the preset dictionary
237          *
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.
240          *
241          * This variable is read only when preset_dict is not NULL.
242          */
243         uint32_t preset_dict_size;
244
245         /**
246          * \brief       Number of literal context bits
247          *
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.
251          *
252          * \todo        Example
253          *
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.
259          *
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
262          * with liblzma.
263          */
264         uint32_t lc;
265 #       define LZMA_LCLP_MIN    0
266 #       define LZMA_LCLP_MAX    4
267 #       define LZMA_LC_DEFAULT  3
268
269         /**
270          * \brief       Number of literal position bits
271          *
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).
276          *
277          * \todo        Example
278          */
279         uint32_t lp;
280 #       define LZMA_LP_DEFAULT  0
281
282         /**
283          * \brief       Number of position bits
284          *
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.
290          *
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.
293          */
294         uint32_t pb;
295 #       define LZMA_PB_MIN      0
296 #       define LZMA_PB_MAX      4
297 #       define LZMA_PB_DEFAULT  2
298
299         /**
300          * \brief       Indicate if the options structure is persistent
301          *
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.
306          *
307          * This option is used only by LZMA2. LZMA1 ignores this and it is
308          * safe to not initialize this when encoding with LZMA1.
309          */
310         lzma_bool persistent;
311
312         /** LZMA compression mode */
313         lzma_mode mode;
314
315         /**
316          * \brief       Nice length of a match
317          *
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.)
324          *
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.
328          *
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.
331          */
332         uint32_t nice_len;
333
334         /** Match finder ID */
335         lzma_match_finder mf;
336
337         /**
338          * \brief       Maximum search depth in the match finder
339          *
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
345          *    been checked; or
346          *  - maximum search depth is reached.
347          *
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.
352          *
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
356          * liblzma versions).
357          *
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.
364          */
365         uint32_t depth;
366
367         /*
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
372          * uninitialized.
373          */
374         void *reserved_ptr1;
375         void *reserved_ptr2;
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;
388
389 } lzma_options_lzma;
390
391
392 /**
393  * \brief       Set a compression preset to lzma_options_lzma structure
394  *
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().
400  *
401  * The preset values are subject to changes between liblzma versions.
402  *
403  * This function is available only if LZMA1 or LZMA2 encoder has been enabled
404  * when building liblzma.
405  */
406 extern LZMA_API(lzma_bool) lzma_lzma_preset(
407                 lzma_options_lzma *options, uint32_t preset);