]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/xz/src/liblzma/api/lzma/lzma.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / xz / 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 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.
26  *
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.
29  */
30 #define LZMA_FILTER_LZMA1       LZMA_VLI_C(0x4000000000000001)
31
32 /**
33  * \brief       LZMA2 Filter ID
34  *
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.
39  */
40 #define LZMA_FILTER_LZMA2       LZMA_VLI_C(0x21)
41
42
43 /**
44  * \brief       Match finders
45  *
46  * Match finder has major effect on both speed and compression ratio.
47  * Usually hash chains are faster than binary trees.
48  *
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.
53  */
54 typedef enum {
55         LZMA_MF_HC3     = 0x03,
56                 /**<
57                  * \brief       Hash Chain with 2- and 3-byte hashing
58                  *
59                  * Minimum nice_len: 3
60                  *
61                  * Memory usage:
62                  *  - dict_size <= 16 MiB: dict_size * 7.5
63                  *  - dict_size > 16 MiB: dict_size * 5.5 + 64 MiB
64                  */
65
66         LZMA_MF_HC4     = 0x04,
67                 /**<
68                  * \brief       Hash Chain with 2-, 3-, and 4-byte hashing
69                  *
70                  * Minimum nice_len: 4
71                  *
72                  * Memory usage: dict_size * 7.5
73                  */
74
75         LZMA_MF_BT2     = 0x12,
76                 /**<
77                  * \brief       Binary Tree with 2-byte hashing
78                  *
79                  * Minimum nice_len: 2
80                  *
81                  * Memory usage: dict_size * 9.5
82                  */
83
84         LZMA_MF_BT3     = 0x13,
85                 /**<
86                  * \brief       Binary Tree with 2- and 3-byte hashing
87                  *
88                  * Minimum nice_len: 3
89                  *
90                  * Memory usage:
91                  *  - dict_size <= 16 MiB: dict_size * 11.5
92                  *  - dict_size > 16 MiB: dict_size * 9.5 + 64 MiB
93                  */
94
95         LZMA_MF_BT4     = 0x14
96                 /**<
97                  * \brief       Binary Tree with 2-, 3-, and 4-byte hashing
98                  *
99                  * Minimum nice_len: 4
100                  *
101                  * Memory usage: dict_size * 11.5
102                  */
103 } lzma_match_finder;
104
105
106 /**
107  * \brief       Test if given match finder is supported
108  *
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
112  * false.
113  *
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.
119  */
120 extern LZMA_API(lzma_bool) lzma_mf_is_supported(lzma_match_finder match_finder)
121                 lzma_nothrow lzma_attr_const;
122
123
124 /**
125  * \brief       Compression modes
126  *
127  * This selects the function used to analyze the data produced by the match
128  * finder.
129  */
130 typedef enum {
131         LZMA_MODE_FAST = 1,
132                 /**<
133                  * \brief       Fast compression
134                  *
135                  * Fast mode is usually at its best when combined with
136                  * a hash chain match finder.
137                  */
138
139         LZMA_MODE_NORMAL = 2
140                 /**<
141                  * \brief       Normal compression
142                  *
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.
146                  */
147 } lzma_mode;
148
149
150 /**
151  * \brief       Test if given compression mode is supported
152  *
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.
156  *
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.
161  */
162 extern LZMA_API(lzma_bool) lzma_mode_is_supported(lzma_mode mode)
163                 lzma_nothrow lzma_attr_const;
164
165
166 /**
167  * \brief       Options specific to the LZMA1 and LZMA2 filters
168  *
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.
172  *
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.
175  */
176 typedef struct {
177         /**
178          * \brief       Dictionary size in bytes
179          *
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
185          * usually is.
186          *
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)
191          *
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
196          * dictionaries.
197          *
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.
201          *
202          * Because extremely small dictionaries sizes would have unneeded
203          * overhead in the decoder, the minimum dictionary size is 4096 bytes.
204          *
205          * \note        When decoding, too big dictionary does no other harm
206          *              than wasting memory.
207          */
208         uint32_t dict_size;
209 #       define LZMA_DICT_SIZE_MIN       UINT32_C(4096)
210 #       define LZMA_DICT_SIZE_DEFAULT   (UINT32_C(1) << 23)
211
212         /**
213          * \brief       Pointer to an initial dictionary
214          *
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.
221          *
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
229          * dictionary though.
230          */
231         const uint8_t *preset_dict;
232
233         /**
234          * \brief       Size of the preset dictionary
235          *
236          * Specifies the size of the preset dictionary. If the size is
237          * bigger than dict_size, only the last dict_size bytes are
238          * processed.
239          *
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).
244          */
245         uint32_t preset_dict_size;
246
247         /**
248          * \brief       Number of literal context bits
249          *
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.
253          *
254          * \todo        Example
255          *
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.
261          *
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
264          * liblzma.
265          */
266         uint32_t lc;
267 #       define LZMA_LCLP_MIN    0
268 #       define LZMA_LCLP_MAX    4
269 #       define LZMA_LC_DEFAULT  3
270
271         /**
272          * \brief       Number of literal position bits
273          *
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).
278          *
279          * \todo        Example
280          */
281         uint32_t lp;
282 #       define LZMA_LP_DEFAULT  0
283
284         /**
285          * \brief       Number of position bits
286          *
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.
292          *
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.
295          */
296         uint32_t pb;
297 #       define LZMA_PB_MIN      0
298 #       define LZMA_PB_MAX      4
299 #       define LZMA_PB_DEFAULT  2
300
301         /** Compression mode */
302         lzma_mode mode;
303
304         /**
305          * \brief       Nice length of a match
306          *
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.)
313          *
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.
317          *
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
320          * LZMA2 can encode.
321          */
322         uint32_t nice_len;
323
324         /** Match finder ID */
325         lzma_match_finder mf;
326
327         /**
328          * \brief       Maximum search depth in the match finder
329          *
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
335          *    been checked; or
336          *  - maximum search depth is reached.
337          *
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.
342          *
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
346          * liblzma versions).
347          *
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.
354          */
355         uint32_t depth;
356
357         /*
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
362          * uninitialized.
363          */
364         void *reserved_ptr1;
365         void *reserved_ptr2;
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;
378
379 } lzma_options_lzma;
380
381
382 /**
383  * \brief       Set a compression preset to lzma_options_lzma structure
384  *
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().
390  *
391  * The preset values are subject to changes between liblzma versions.
392  *
393  * This function is available only if LZMA1 or LZMA2 encoder has been enabled
394  * when building liblzma.
395  */
396 extern LZMA_API(lzma_bool) lzma_lzma_preset(
397                 lzma_options_lzma *options, uint32_t preset) lzma_nothrow;