3 * \brief Data types and functions used in many places of the public API
5 * \author Copyright (C) 1999-2006 Igor Pavlov
6 * \author Copyright (C) 2007 Lasse Collin
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.
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.
19 #ifndef LZMA_H_INTERNAL
20 # error Never include this file directly. Use <lzma.h> instead.
27 * This is here because C89 doesn't have stdbool.h. To set a value for
28 * variables having type lzma_bool, you can use
29 * - C99's `true' and `false' from stdbool.h;
30 * - C++'s internal `true' and `false'; or
31 * - integers one (true) and zero (false).
33 typedef unsigned char lzma_bool;
37 * \brief Type of reserved enumeration variable in structures
39 * To avoid breaking library ABI when new features are added, several
40 * structures contain extra variables that may be used in future. Since
41 * sizeof(enum) can be different than sizeof(int), and sizeof(enum) may
42 * even vary depending on the range of enumeration constants, we specify
43 * a separate type to be used for reserved enumeration variables. All
44 * enumeration constants in liblzma API will be non-negative and less
45 * than 128, which should guarantee that the ABI won't break even when
46 * new constants are added to existing enumerations.
49 LZMA_RESERVED_ENUM = 0
54 * \brief Return values used by several functions in liblzma
56 * Check the descriptions of specific functions to find out which return
57 * values they can return. With some functions the return values may have
58 * more specific meanings than described here; those differences are
59 * described per-function basis.
64 * \brief Operation completed successfully
69 * \brief End of stream was reached
71 * In encoder, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, or
72 * LZMA_FINISH was finished. In decoder, this indicates
73 * that all the data was successfully decoded.
75 * In all cases, when LZMA_STREAM_END is returned, the last
76 * output bytes should be picked from strm->next_out.
81 * \brief Input stream has no integrity check
83 * This return value can be returned only if the
84 * LZMA_TELL_NO_CHECK flag was used when initializing
85 * the decoder. LZMA_NO_CHECK is just a warning, and
86 * the decoding can be continued normally.
88 * It is possible to call lzma_get_check() immediatelly after
89 * lzma_code has returned LZMA_NO_CHECK. The result will
90 * naturally be LZMA_CHECK_NONE, but the possibility to call
91 * lzma_get_check() may be convenient in some applications.
94 LZMA_UNSUPPORTED_CHECK = 3,
96 * \brief Cannot calculate the integrity check
98 * The usage of this return value is slightly different in
99 * encoders and decoders.
101 * Encoders can return this value only from the initialization
102 * function. If initialization fails with this value, the
103 * encoding cannot be done, because there's no way to produce
104 * output with the correct integrity check.
106 * Decoders can return this value only from the lzma_code
107 * function and only if the LZMA_TELL_UNSUPPORTED_CHECK flag
108 * was used when initializing the decoder. The decoding can
109 * still be continued normally even if the check type is
110 * unsupported, but naturally the check will not be validated,
111 * and possible errors may go undetected.
113 * With decoder, it is possible to call lzma_get_check()
114 * immediatelly after lzma_code has returned
115 * LZMA_UNSUPPORTED_CHECK. This way it is possible to find
116 * out what the unsupported Check ID was.
121 * \brief Integrity check type is now available
123 * This value can be returned only by the lzma_code() function
124 * and only if the decoder was initialized with the
125 * LZMA_TELL_ANY_CHECK flag. LZMA_GET_CHECK tells the
126 * application that it may now call lzma_get_check() to find
127 * out the Check ID. This can be used, for example, to
128 * implement a decoder that accepts only files that have
129 * strong enough integrity check.
134 * \brief Cannot allocate memory
136 * Memory allocation failed, or the size of the allocation
137 * would be greater than SIZE_MAX.
139 * Due to lazy coding, the coding cannot be continued even
140 * if more memory were made available after LZMA_MEM_ERROR.
143 LZMA_MEMLIMIT_ERROR = 6,
145 * \brief Memory usage limit was reached
147 * Decoder would need more memory than allowed by the
148 * specified memory usage limit. To continue decoding,
149 * the memory usage limit has to be increased. See functions
150 * lzma_memlimit_get() and lzma_memlimit_set().
153 LZMA_FORMAT_ERROR = 7,
155 * \brief Unknown file format
157 * The decoder did not recognize the input as supported file
158 * format. This error can occur, for example, when trying to
159 * decode LZMA_Alone format file with lzma_stream_decoder,
160 * because lzma_stream_decoder accepts only the new .lzma
164 LZMA_HEADER_ERROR = 8,
166 * \brief Invalid or unsupported options
168 * Invalid or unsupported options, for example
169 * - unsupported filter(s) or filter options; or
170 * - reserved bits set in headers (decoder only).
172 * Rebuilding liblzma with more features enabled, or
173 * upgrading to a newer version of liblzma may help.
178 * \brief Data is corrupt
180 * The usage of this return value is different in encoders
181 * and decoders. In both encoder and decoder, the coding
182 * cannot continue after this error.
184 * Encoders return this if size limits of the target file
185 * format would be exceeded. These limits are huge, thus
186 * getting this error from an encoder is mostly theoretical.
187 * For example, the maximum compressed and uncompressed
188 * size of a Stream created with lzma_stream_encoder is
189 * 2^63 - 1 bytes (one byte less than 8 EiB).
191 * Decoders return this error if the input data is corrupt.
192 * This can mean, for example, invalid CRC32 in headers
193 * or invalid check of uncompressed data.
198 * \brief No progress is possible
200 * This error code is returned when the coder cannot consume
201 * any new input and produce any new output. The most common
202 * reason for this error is that the input stream being
203 * decoded is truncated or corrupt.
205 * This error is not fatal. Coding can be continued normally
206 * by providing more input and/or more output space, if
209 * Typically the first call to lzma_code() that can do no
210 * progress returns LZMA_OK instead of LZMA_BUF_ERROR. Only
211 * the second consecutive call doing no progress will return
212 * LZMA_BUF_ERROR. This is by design.
214 * With zlib, Z_BUF_ERROR may be returned even if the
215 * application is doing nothing wrong. The above hack
216 * guarantees that liblzma never returns LZMA_BUF_ERROR
217 * to properly written applications unless the input file
218 * is truncated or corrupt. This should simplify the
219 * applications a little.
222 LZMA_PROG_ERROR = 11,
224 * \brief Programming error
226 * This indicates that the arguments given to the function are
227 * invalid or the internal state of the decoder is corrupt.
228 * - Function arguments are invalid or the structures
229 * pointed by the argument pointers are invalid
230 * e.g. if strm->next_out has been set to NULL and
231 * strm->avail_out > 0 when calling lzma_code().
232 * - lzma_* functions have been called in wrong order
233 * e.g. lzma_code() was called right after lzma_end().
234 * - If errors occur randomly, the reason might be flaky
237 * If you think that your code is correct, this error code
238 * can be a sign of a bug in liblzma. See the documentation
239 * how to report bugs.
245 * \brief The `action' argument for lzma_code()
250 * Encoder: Encode as much input as possible. Some internal
251 * buffering will probably be done (depends on the filter
252 * chain in use), which causes latency: the input used won't
253 * usually be decodeable from the output of the same
256 * Decoder: Decode as much input as possible and produce as
257 * much output as possible. This action provides best
258 * throughput, but may introduce latency, because the
259 * decoder may decode more data into its internal buffers
260 * than that fits into next_out.
265 * Encoder: Makes all the data given to liblzma via next_in
266 * available in next_out without resetting the filters. Call
267 * lzma_code() with LZMA_SYNC_FLUSH until it returns
268 * LZMA_STREAM_END. Then continue encoding normally.
270 * \note Synchronous flushing is supported only by
271 * some filters. Using LZMA_SYNC_FLUSH with
272 * which such filters will make lzma_code()
273 * return LZMA_HEADER_ERROR.
275 * Decoders don't support LZMA_SYNC_FLUSH.
280 * Finishes encoding of the current Data Block. All the input
281 * data going to the current Data Block must have been given
282 * to the encoder (the last bytes can still be pending in
283 * next_in). Call lzma_code() with LZMA_FULL_FLUSH until
284 * it returns LZMA_STREAM_END. Then continue normally with
285 * LZMA_RUN or finish the Stream with LZMA_FINISH.
287 * This action is supported only by Stream encoder and easy
288 * encoder (which uses Stream encoder). If there is no
289 * unfinished Block, no empty Block is created.
294 * Finishes the coding operation. All the input data must
295 * have been given to the encoder (the last bytes can still
296 * be pending in next_in). Call lzma_code() with LZMA_FINISH
297 * until it returns LZMA_STREAM_END. Once LZMA_FINISH has
298 * been used, the amount of input must no longer be changed
299 * by the application.
301 * When decoding, using LZMA_FINISH is optional unless the
302 * LZMA_CONCATENATED flag was used when the decoder was
303 * initialized. When LZMA_CONCATENATED was not used, the only
304 * effect of LZMA_FINISH is that the amount of input must not
305 * be changed just like in the encoder.
311 * \brief Custom functions for memory handling
313 * A pointer to lzma_allocator may be passed via lzma_stream structure
314 * to liblzma, and some advanced function take pointer lzma_allocator as
315 * a separate function argument. The library will use the functions
316 * specified in lzma_allocator for memory handling instead of the default
317 * malloc() and free().
319 * liblzma doesn't make an internal copy of lzma_allocator. Thus, it is
320 * OK to change these function pointers in the middle of the coding
321 * process, but obviously it must be done carefully to make sure that the
322 * replacement `free' can deallocate memory allocated by the earlier
323 * `alloc' function(s).
327 * \brief Pointer to custom memory allocation function
329 * If you don't want a custom allocator, but still want
330 * custom free(), set this to NULL and liblzma will use
331 * the standard malloc().
333 * \param opaque lzma_allocator.opaque (see below)
334 * \param nmemb Number of elements like in calloc().
335 * liblzma will always set nmemb to 1.
336 * This argument exists only for
337 * compatibility with zlib and libbzip2.
338 * \param size Size of an element in bytes.
339 * liblzma never sets this to zero.
341 * \return Pointer to the beginning of a memory block of
342 * size nmemb * size, or NULL if allocation fails
343 * for some reason. When allocation fails, functions
344 * of liblzma return LZMA_MEM_ERROR.
346 * For performance reasons, the allocator should not waste time
347 * zeroing the allocated buffers. This is not only about speed, but
348 * also memory usage, since the operating system kernel doesn't
349 * necessarily allocate the requested memory until it is actually
350 * used. With small input files liblzma may actually need only a
351 * fraction of the memory that it requested for allocation.
353 * \note LZMA_MEM_ERROR is also used when the size of the
354 * allocation would be greater than SIZE_MAX. Thus,
355 * don't assume that the custom allocator must have
356 * returned NULL if some function from liblzma
357 * returns LZMA_MEM_ERROR.
359 void *(*alloc)(void *opaque, size_t nmemb, size_t size);
362 * \brief Pointer to custom memory freeing function
364 * If you don't want a custom freeing function, but still
365 * want a custom allocator, set this to NULL and liblzma
366 * will use the standard free().
368 * \param opaque lzma_allocator.opaque (see below)
369 * \param ptr Pointer returned by lzma_allocator.alloc(),
370 * or when it is set to NULL, a pointer returned
371 * by the standard malloc().
373 void (*free)(void *opaque, void *ptr);
376 * \brief Pointer passed to .alloc() and .free()
378 * opaque is passed as the first argument to lzma_allocator.alloc()
379 * and lzma_allocator.free(). This intended to ease implementing
380 * custom memory allocation functions for use with liblzma.
382 * If you don't need this, you should set this to NULL.
390 * \brief Internal data structure
392 * The contents of this structure is not visible outside the library.
394 typedef struct lzma_internal_s lzma_internal;
398 * \brief Passing data to and from liblzma
400 * The lzma_stream structure is used for
401 * - passing pointers to input and output buffers to liblzma;
402 * - defining custom memory hander functions; and
403 * - holding a pointer to coder-specific internal data structures.
405 * When a new lzma_stream structure is allocated (either as automatic variable
406 * on stack or dynamically with malloc()), the new lzma_stream structure must
407 * be initialized to LZMA_STREAM_INIT.
409 * Before initializing a coder (for example, with lzma_stream_decoder()),
412 * Before calling any of the lzma_*_init() functions the first time,
413 * the application must reset lzma_stream to LZMA_STREAM_INIT. The
414 * lzma_*_init() function will verify the options, allocate internal
415 * data structures and store pointer to them into `internal'. Finally
416 * total_in and total_out are reset to zero. In contrast to zlib,
417 * next_in and avail_in are ignored by the initialization functions.
419 * The actual coding is done with the lzma_code() function. Application
420 * must update next_in, avail_in, next_out, and avail_out between
421 * calls to lzma_decode() just like with zlib.
423 * In contrast to zlib, even the decoder requires that there always
424 * is at least one byte space in next_out; if avail_out == 0,
425 * LZMA_BUF_ERROR is returned immediatelly. This shouldn't be a problem
426 * for most applications that already use zlib, but it's still worth
427 * checking your application.
429 * Application may modify values of total_in and total_out as it wants.
430 * They are updated by liblzma to match the amount of data read and
431 * written, but liblzma doesn't use the values internally.
433 * Application must not touch the `internal' pointer.
436 const uint8_t *next_in; /**< Pointer to the next input byte. */
437 size_t avail_in; /**< Number of available input bytes in next_in. */
438 uint64_t total_in; /**< Total number of bytes read by liblzma. */
440 uint8_t *next_out; /**< Pointer to the next output position. */
441 size_t avail_out; /**< Amount of free space in next_out. */
442 uint64_t total_out; /**< Total number of bytes written by liblzma. */
445 * Custom memory allocation functions. Set to NULL to use
446 * the standard malloc() and free().
448 lzma_allocator *allocator;
450 /** Internal state is not visible to applications. */
451 lzma_internal *internal;
454 * Reserved space to allow possible future extensions without
455 * breaking the ABI. Excluding the initialization of this structure,
456 * you should not touch these, because the names of these variables
461 uint64_t reserved_int1;
462 uint64_t reserved_int2;
463 lzma_reserved_enum reserved_enum1;
464 lzma_reserved_enum reserved_enum2;
470 * \brief Initialization for lzma_stream
472 * When you declare an instance of lzma_stream, you can immediatelly
473 * initialize it so that initialization functions know that no memory
474 * has been allocated yet:
476 * lzma_stream strm = LZMA_STREAM_INIT;
478 * If you need to initialize a dynamically allocated lzma_stream, you can use
479 * memset(strm_pointer, 0, sizeof(lzma_stream)). Strictly speaking, this
480 * violates the C standard since NULL may have different internal
481 * representation than zero, but it should be portable enough in practice.
482 * Anyway, for maximum portability, you can use something like this:
484 * lzma_stream tmp = LZMA_STREAM_INIT;
487 #define LZMA_STREAM_INIT \
488 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL, NULL, 0, 0, 0, 0 }
492 * \brief Encodes or decodes data
494 * Once the lzma_stream has been successfully initialized (e.g. with
495 * lzma_stream_encoder()), the actual encoding or decoding is done
496 * using this function. The application has to update strm->next_in,
497 * strm->avail_in, strm->next_out, and strm->avail_out to pass input
498 * to and get output from liblzma.
500 * See the description of the coder-specific initialization function to find
501 * out what `action' values are supported by the coder. See documentation of
502 * lzma_ret for the possible return values.
504 extern lzma_ret lzma_code(lzma_stream *strm, lzma_action action)
505 lzma_attr_warn_unused_result;
509 * \brief Frees memory allocated for the coder data structures
511 * \param strm Pointer to lzma_stream that is at least initialized
512 * with LZMA_STREAM_INIT.
514 * \note zlib indicates an error if application end()s unfinished
515 * stream. liblzma doesn't do this, and assumes that
516 * application knows what it is doing.
518 extern void lzma_end(lzma_stream *strm);