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