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