]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/container.h
b8085ac463a3e2d20156c5778da13d55f993ee8c
[icculus/xz.git] / src / liblzma / api / lzma / container.h
1 /**
2  * \file        lzma/container.h
3  * \brief       File formats
4  *
5  * \author      Copyright (C) 1999-2008 Igor Pavlov
6  * \author      Copyright (C) 2007-2008 Lasse Collin
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  */
18
19 #ifndef LZMA_H_INTERNAL
20 #       error Never include this file directly. Use <lzma.h> instead.
21 #endif
22
23
24 /************
25  * Encoding *
26  ************/
27
28 /**
29  * \brief       Default compression preset
30  *
31  * It's not straightforward to recommend a default preset, because in some
32  * cases keeping the resource usage relatively low is more important that
33  * getting the maximum compression ratio.
34  */
35 #define LZMA_PRESET_DEFAULT     UINT32_C(6)
36
37
38 /**
39  * \brief       Mask for preset level
40  *
41  * This is useful only if you need to extract the level from the preset
42  * variable. That should be rare.
43  */
44 #define LZMA_PRESET_LEVEL_MASK  UINT32_C(0x1F)
45
46
47 /*
48  * Preset flags
49  *
50  * Currently only one flag is defined.
51  */
52
53 /**
54  * \brief       Extreme compression preset
55  *
56  * This flag modifies the preset to make the encoding significantly slower
57  * while improving the compression ratio only marginally. This is useful
58  * when you don't mind wasting time to get as small result as possible.
59  *
60  * This flag doesn't affect the memory usage requirements of the decoder (at
61  * least not significantly). The memory usage of the encoder may be increased
62  * a little but only at the lowest preset levels (0-4 or so).
63  */
64 #define LZMA_PRESET_EXTREME       (UINT32_C(1) << 31)
65
66
67 /**
68  * \brief       Calculate rough memory usage of easy encoder
69  *
70  * This function is a wrapper for lzma_raw_encoder_memusage().
71  *
72  * \param       preset  Compression preset (level and possible flags)
73  */
74 extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset)
75                 lzma_attr_pure;
76
77
78 /**
79  * \brief       Calculate rough decoder memory usage of a preset
80  *
81  * This function is a wrapper for lzma_raw_decoder_memusage().
82  *
83  * \param       preset  Compression preset (level and possible flags)
84  */
85 extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset)
86                 lzma_attr_pure;
87
88
89 /**
90  * \brief       Initialize .xz Stream encoder using a preset number
91  *
92  * This function is intended for those who just want to use the basic features
93  * if liblzma (that is, most developers out there).
94  *
95  * \param       strm    Pointer to lzma_stream that is at least initialized
96  *                      with LZMA_STREAM_INIT.
97  * \param       preset  Compression preset to use. A preset consist of level
98  *                      number and zero or more flags. Usually flags aren't
99  *                      used, so preset is simply a number [0, 9] which match
100  *                      the options -0 .. -9 of the xz command line tool.
101  *                      Additional flags can be be set using bitwise-or with
102  *                      the preset level number, e.g. 6 | LZMA_PRESET_EXTREME.
103  * \param       check   Integrity check type to use. See check.h for available
104  *                      checks. If you are unsure, use LZMA_CHECK_CRC32.
105  *
106  * \return      - LZMA_OK: Initialization succeeded. Use lzma_code() to
107  *                encode your data.
108  *              - LZMA_MEM_ERROR: Memory allocation failed. All memory
109  *                previously allocated for *strm is now freed.
110  *              - LZMA_OPTIONS_ERROR: The given compression level is not
111  *                supported by this build of liblzma.
112  *              - LZMA_UNSUPPORTED_CHECK: The given check type is not
113  *                supported by this liblzma build.
114  *              - LZMA_PROG_ERROR: One or more of the parameters have values
115  *                that will never be valid. For example, strm == NULL.
116  *
117  * If initialization succeeds, use lzma_code() to do the actual encoding.
118  * Valid values for `action' (the second argument of lzma_code()) are
119  * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future,
120  * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH.
121  */
122 extern LZMA_API(lzma_ret) lzma_easy_encoder(
123                 lzma_stream *strm, uint32_t preset, lzma_check check)
124                 lzma_attr_warn_unused_result;
125
126
127 /**
128  * \brief       Single-call .xz Stream encoding using a preset number
129  *
130  * The maximum required output buffer size can be calculated with
131  * lzma_stream_buffer_bound().
132  *
133  * \param       preset      Compression preset to use. See the description
134  *                          in lzma_easy_encoder().
135  * \param       check       Type of the integrity check to calculate from
136  *                          uncompressed data.
137  * \param       allocator   lzma_allocator for custom allocator functions.
138  *                          Set to NULL to use malloc() and free().
139  * \param       in          Beginning of the input buffer
140  * \param       in_size     Size of the input buffer
141  * \param       out         Beginning of the output buffer
142  * \param       out_pos     The next byte will be written to out[*out_pos].
143  *                          *out_pos is updated only if encoding succeeds.
144  * \param       out_size    Size of the out buffer; the first byte into
145  *                          which no data is written to is out[out_size].
146  *
147  * \return      - LZMA_OK: Encoding was successful.
148  *              - LZMA_BUF_ERROR: Not enough output buffer space.
149  *              - LZMA_OPTIONS_ERROR
150  *              - LZMA_MEM_ERROR
151  *              - LZMA_DATA_ERROR
152  *              - LZMA_PROG_ERROR
153  */
154 extern LZMA_API(lzma_ret) lzma_easy_buffer_encode(
155                 uint32_t preset, lzma_check check,
156                 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
157                 uint8_t *out, size_t *out_pos, size_t out_size);
158
159
160 /**
161  * \brief       Initialize .xz Stream encoder using a custom filter chain
162  *
163  * \param       strm    Pointer to properly prepared lzma_stream
164  * \param       filters Array of filters. This must be terminated with
165  *                      filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for
166  *                      more information.
167  * \param       check   Type of the integrity check to calculate from
168  *                      uncompressed data.
169  *
170  * \return      - LZMA_OK: Initialization was successful.
171  *              - LZMA_MEM_ERROR
172  *              - LZMA_OPTIONS_ERROR
173  *              - LZMA_PROG_ERROR
174  */
175 extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
176                 const lzma_filter *filters, lzma_check check)
177                 lzma_attr_warn_unused_result;
178
179
180 /**
181  * \brief       Initialize .lzma encoder (legacy file format)
182  *
183  * The .lzma format is sometimes called the LZMA_Alone format, which is the
184  * reason for the name of this function. The .lzma format supports only the
185  * LZMA1 filter. There is no support for integrity checks like CRC32.
186  *
187  * Use this function if and only if you need to create files readable by
188  * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format
189  * is strongly recommended.
190  *
191  * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH.
192  * No kind of flushing is supported, because the file format doesn't make
193  * it possible.
194  *
195  * \return      - LZMA_OK
196  *              - LZMA_MEM_ERROR
197  *              - LZMA_OPTIONS_ERROR
198  *              - LZMA_PROG_ERROR
199  */
200 extern LZMA_API(lzma_ret) lzma_alone_encoder(
201                 lzma_stream *strm, const lzma_options_lzma *options)
202                 lzma_attr_warn_unused_result;
203
204
205 /**
206  * \brief       Calculate output buffer size for single-call Stream encoder
207  *
208  * When trying to compress uncompressible data, the encoded size will be
209  * slightly bigger than the input data. This function calculates how much
210  * output buffer space is required to be sure that lzma_stream_buffer_encode()
211  * doesn't return LZMA_BUF_ERROR.
212  *
213  * The calculated value is not exact, but it is guaranteed to be big enough.
214  * The actual maximum output space required may be slightly smaller (up to
215  * about 100 bytes). This should not be a problem in practice.
216  *
217  * If the calculated maximum size doesn't fit into size_t or would make the
218  * Stream grow past LZMA_VLI_MAX (which should never happen in practice),
219  * zero is returned to indicate the error.
220  *
221  * \note        The limit calculated by this function applies only to
222  *              single-call encoding. Multi-call encoding may (and probably
223  *              will) have larger maximum expansion when encoding
224  *              uncompressible data. Currently there is no function to
225  *              calculate the maximum expansion of multi-call encoding.
226  */
227 extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size);
228
229
230 /**
231  * \brief       Single-call Stream encoder
232  *
233  * \param       filters     Array of filters. This must be terminated with
234  *                          filters[n].id = LZMA_VLI_UNKNOWN. See filter.h
235  *                          for more information.
236  * \param       check       Type of the integrity check to calculate from
237  *                          uncompressed data.
238  * \param       allocator   lzma_allocator for custom allocator functions.
239  *                          Set to NULL to use malloc() and free().
240  * \param       in          Beginning of the input buffer
241  * \param       in_size     Size of the input buffer
242  * \param       out         Beginning of the output buffer
243  * \param       out_pos     The next byte will be written to out[*out_pos].
244  *                          *out_pos is updated only if encoding succeeds.
245  * \param       out_size    Size of the out buffer; the first byte into
246  *                          which no data is written to is out[out_size].
247  *
248  * \return      - LZMA_OK: Encoding was successful.
249  *              - LZMA_BUF_ERROR: Not enough output buffer space.
250  *              - LZMA_OPTIONS_ERROR
251  *              - LZMA_MEM_ERROR
252  *              - LZMA_DATA_ERROR
253  *              - LZMA_PROG_ERROR
254  */
255 extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
256                 lzma_filter *filters, lzma_check check,
257                 lzma_allocator *allocator, const uint8_t *in, size_t in_size,
258                 uint8_t *out, size_t *out_pos, size_t out_size)
259                 lzma_attr_warn_unused_result;
260
261
262 /************
263  * Decoding *
264  ************/
265
266 /**
267  * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream
268  * being decoded has no integrity check. Note that when used with
269  * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK
270  * if LZMA_TELL_NO_CHECK is used.
271  */
272 #define LZMA_TELL_NO_CHECK              UINT32_C(0x01)
273
274
275 /**
276  * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input
277  * stream has an integrity check, but the type of the integrity check is not
278  * supported by this liblzma version or build. Such files can still be
279  * decoded, but the integrity check cannot be verified.
280  */
281 #define LZMA_TELL_UNSUPPORTED_CHECK     UINT32_C(0x02)
282
283
284 /**
285  * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type
286  * of the integrity check is known. The type can then be got with
287  * lzma_get_check().
288  */
289 #define LZMA_TELL_ANY_CHECK             UINT32_C(0x04)
290
291
292 /**
293  * This flag enables decoding of concatenated files with file formats that
294  * allow concatenating compressed files as is. From the formats currently
295  * supported by liblzma, only the .xz format allows concatenated files.
296  * Concatenated files are not allowed with the legacy .lzma format.
297  *
298  * This flag also affects the usage of the `action' argument for lzma_code().
299  * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END
300  * unless LZMA_FINISH is used as `action'. Thus, the application has to set
301  * LZMA_FINISH in the same way as it does when encoding.
302  *
303  * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH
304  * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required.
305  */
306 #define LZMA_CONCATENATED               UINT32_C(0x08)
307
308
309 /**
310  * \brief       Initialize .xz Stream decoder
311  *
312  * \param       strm        Pointer to properly prepared lzma_stream
313  * \param       memlimit    Rough memory usage limit as bytes
314  * \param       flags       Bitwise-or of zero or more of the decoder flags:
315  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
316  *                          LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED
317  *
318  * \return      - LZMA_OK: Initialization was successful.
319  *              - LZMA_MEM_ERROR: Cannot allocate memory.
320  *              - LZMA_OPTIONS_ERROR: Unsupported flags
321  */
322 extern LZMA_API(lzma_ret) lzma_stream_decoder(
323                 lzma_stream *strm, uint64_t memlimit, uint32_t flags)
324                 lzma_attr_warn_unused_result;
325
326
327 /**
328  * \brief       Decode .xz Streams and .lzma files with autodetection
329  *
330  * This decoder autodetects between the .xz and .lzma file formats, and
331  * calls lzma_stream_decoder() or lzma_alone_decoder() once the type
332  * of the input file has been detected.
333  *
334  * \param       strm        Pointer to properly prepared lzma_stream
335  * \param       memlimit    Rough memory usage limit as bytes
336  * \param       flags       Bitwise-or of flags, or zero for no flags.
337  *
338  * \return      - LZMA_OK: Initialization was successful.
339  *              - LZMA_MEM_ERROR: Cannot allocate memory.
340  *              - LZMA_OPTIONS_ERROR: Unsupported flags
341  */
342 extern LZMA_API(lzma_ret) lzma_auto_decoder(
343                 lzma_stream *strm, uint64_t memlimit, uint32_t flags)
344                 lzma_attr_warn_unused_result;
345
346
347 /**
348  * \brief       Initialize .lzma decoder (legacy file format)
349  *
350  * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
351  * There is no need to use LZMA_FINISH, but allowing it may simplify
352  * certain types of applications.
353  *
354  * \return      - LZMA_OK
355  *              - LZMA_MEM_ERROR
356  */
357 extern LZMA_API(lzma_ret) lzma_alone_decoder(
358                 lzma_stream *strm, uint64_t memlimit)
359                 lzma_attr_warn_unused_result;
360
361
362 /**
363  * \brief       Single-call .xz Stream decoder
364  *
365  * \param       memlimit    Pointer to how much memory the decoder is allowed
366  *                          to allocate. The value pointed by this pointer is
367  *                          modified if and only if LZMA_MEMLIMIT_ERROR is
368  *                          returned.
369  * \param       flags       Bitwise-or of zero or more of the decoder flags:
370  *                          LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
371  *                          LZMA_CONCATENATED. Note that LZMA_TELL_ANY_CHECK
372  *                          is not allowed and will return LZMA_PROG_ERROR.
373  * \param       allocator   lzma_allocator for custom allocator functions.
374  *                          Set to NULL to use malloc() and free().
375  * \param       in          Beginning of the input buffer
376  * \param       in_pos      The next byte will be read from in[*in_pos].
377  *                          *in_pos is updated only if decoding succeeds.
378  * \param       in_size     Size of the input buffer; the first byte that
379  *                          won't be read is in[in_size].
380  * \param       out         Beginning of the output buffer
381  * \param       out_pos     The next byte will be written to out[*out_pos].
382  *                          *out_pos is updated only if encoding succeeds.
383  * \param       out_size    Size of the out buffer; the first byte into
384  *                          which no data is written to is out[out_size].
385  *
386  * \return      - LZMA_OK: Decoding was successful.
387  *              - LZMA_FORMAT_ERROR
388  *              - LZMA_OPTIONS_ERROR
389  *              - LZMA_DATA_ERROR
390  *              - LZMA_NO_CHECK: This can be returned only if using
391  *                the LZMA_TELL_NO_CHECK flag.
392  *              - LZMA_UNSUPPORTED_CHECK: This can be returned only if using
393  *                the LZMA_TELL_UNSUPPORTED_CHECK flag.
394  *              - LZMA_MEM_ERROR
395  *              - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
396  *                The minimum required memlimit value was stored to *memlimit.
397  *              - LZMA_BUF_ERROR: Output buffer was too small.
398  *              - LZMA_PROG_ERROR
399  */
400 extern LZMA_API(lzma_ret) lzma_stream_buffer_decode(
401                 uint64_t *memlimit, uint32_t flags, lzma_allocator *allocator,
402                 const uint8_t *in, size_t *in_pos, size_t in_size,
403                 uint8_t *out, size_t *out_pos, size_t out_size)
404                 lzma_attr_warn_unused_result;