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 Return values used by several functions in liblzma
39 * Check the descriptions of specific functions to find out which return
40 * values they can return and the exact meanings of the values in every
41 * situation. The descriptions given here are only suggestive.
46 * \brief Operation completed successfully
51 * \brief End of stream was reached
53 * The application should pick the last remaining output
54 * bytes from strm->next_out.
59 * \brief Programming error
61 * This indicates that the arguments given to the function are
62 * invalid or the internal state of the decoder is corrupt.
63 * - Function arguments are invalid or the structures
64 * pointed by the argument pointers are invalid
65 * e.g. if strm->next_out has been set to NULL and
66 * strm->avail_out > 0 when calling lzma_code().
67 * - lzma_* functions have been called in wrong order
68 * e.g. lzma_code() was called right after lzma_end().
69 * - If errors occur randomly, the reason might be flaky
72 * If you think that your code is correct, this error code
73 * can be a sign of a bug in liblzma. See the documentation
79 * \brief Data is corrupt
81 * - Encoder: The input size doesn't match the uncompressed
82 * size given to lzma_*_encoder_init().
83 * - Decoder: The input is corrupt. This includes corrupted
84 * header, corrupted compressed data, and unmatching
87 * \todo What can be done if encoder returns this?
88 * Probably can continue by fixing the input
89 * amount, but make sure.
94 * \brief Cannot allocate memory
96 * Memory allocation failed.
101 * \brief No progress is possible
103 * This may happen when avail_in or avail_out is zero.
105 * \note This error is not fatal. Coding can continue
106 * normally once the reason for this error has
110 LZMA_HEADER_ERROR = -6,
112 * \brief Invalid or unsupported header
114 * Invalid or unsupported options, for example
115 * - unsupported filter(s) or filter options; or
116 * - reserved bits set in headers (decoder only).
118 * Rebuilding liblzma with more features enabled, or
119 * upgrading to a newer version of liblzma may help.
122 LZMA_UNSUPPORTED_CHECK = -7,
124 * \brief Check type is unknown
126 * The type of Check is not supported, and thus the Check
127 * cannot be calculated. In the encoder, this is an error.
128 * In the decoder, this is only a warning and decoding can
129 * still proceed normally (but the Check is ignored).
132 LZMA_FORMAT_ERROR = -8,
134 * \brief Unknown file format
137 LZMA_MEMLIMIT_ERROR = -9
139 * \brief Memory usage limit was reached
141 * Decoder would need more memory than allowed by the
142 * specified memory usage limit. To continue decoding,
143 * the memory usage limit has to be increased. See functions
144 * lzma_memlimit_get() and lzma_memlimit_set().
150 * \brief The `action' argument for lzma_code()
155 * Encoder: Encode as much input as possible. Some internal
156 * buffering will probably be done (depends on the filter
157 * chain in use), which causes latency: the input used won't
158 * usually be decodeable from the output of the same
161 * Decoder: Decode as much input as possible and produce as
162 * much output as possible. This action provides best
163 * throughput, but may introduce latency, because the
164 * decoder may decode more data into its internal buffers
165 * than that fits into next_out.
170 * Encoder: Makes all the data given to liblzma via next_in
171 * available in next_out without resetting the filters. Call
172 * lzma_code() with LZMA_SYNC_FLUSH until it returns
173 * LZMA_STREAM_END. Then continue encoding normally.
175 * \note Synchronous flushing is supported only by
176 * some filters. Some filters support it only
179 * Decoder: Asks the decoder to decode only as much as is
180 * needed to fill next_out. This decreases latency with some
181 * filters, but is likely to decrease also throughput. It is
182 * a good idea to use this flag only when it is likely that
183 * you don't need more output soon.
185 * \note With decoder, this is not comparable to
186 * zlib's Z_SYNC_FLUSH.
191 * Finishes encoding of the current Data Block. All the input
192 * data going to the current Data Block must have been given
193 * to the encoder (the last bytes can still be pending in
194 * next_in). Call lzma_code() with LZMA_FULL_FLUSH until
195 * it returns LZMA_STREAM_END. Then continue normally with
196 * LZMA_RUN or finish the Stream with LZMA_FINISH.
198 * This action is supported only by Multi-Block Stream
199 * encoder. If there is no unfinished Data Block, no empty
200 * Data Block is created.
205 * Finishes the encoding operation. All the input data must
206 * have been given to the encoder (the last bytes can still
207 * be pending in next_in). Call lzma_code() with LZMA_FINISH
208 * until it returns LZMA_STREAM_END.
210 * This action is not supported by decoders.
216 * \brief Custom functions for memory handling
218 * A pointer to lzma_allocator may be passed via lzma_stream structure
219 * to liblzma. The library will use these functions for memory handling
220 * instead of the default malloc() and free().
222 * liblzma doesn't make an internal copy of lzma_allocator. Thus, it is
223 * OK to change these function pointers in the middle of the coding
224 * process, but obviously it must be done carefully to make sure that the
225 * replacement `free' can deallocate memory allocated by the earlier
226 * `alloc' function(s).
230 * \brief Pointer to custom memory allocation function
232 * Set this to point to your custom memory allocation function.
233 * It can be useful for example if you want to limit how much
234 * memory liblzma is allowed to use: for this, you may use
235 * a pointer to lzma_memory_alloc().
237 * If you don't want a custom allocator, but still want
238 * custom free(), set this to NULL and liblzma will use
239 * the standard malloc().
241 * \param opaque lzma_allocator.opaque (see below)
242 * \param nmemb Number of elements like in calloc().
243 * liblzma will always set nmemb to 1.
244 * This argument exists only for
245 * compatibility with zlib and libbzip2.
246 * \param size Size of an element in bytes.
247 * liblzma never sets this to zero.
249 * \return Pointer to the beginning of a memory block of
250 * size nmemb * size, or NULL if allocation fails
251 * for some reason. When allocation fails, functions
252 * of liblzma return LZMA_MEM_ERROR.
254 void *(*alloc)(void *opaque, size_t nmemb, size_t size);
257 * \brief Pointer to custom memory freeing function
259 * Set this to point to your custom memory freeing function.
260 * If lzma_memory_alloc() is used as allocator, this should
261 * be set to lzma_memory_free().
263 * If you don't want a custom freeing function, but still
264 * want a custom allocator, set this to NULL and liblzma
265 * will use the standard free().
267 * \param opaque lzma_allocator.opaque (see below)
268 * \param ptr Pointer returned by
269 * lzma_allocator.alloc(), or when it
270 * is set to NULL, a pointer returned
271 * by the standard malloc().
273 void (*free)(void *opaque, void *ptr);
276 * \brief Pointer passed to .alloc() and .free()
278 * opaque is passed as the first argument to lzma_allocator.alloc()
279 * and lzma_allocator.free(). This intended to ease implementing
280 * custom memory allocation functions for use with liblzma.
282 * When using lzma_memory_alloc() and lzma_memory_free(), opaque
283 * must point to lzma_memory_limiter structure allocated and
284 * initialized with lzma_memory_limiter_create().
286 * If you don't need this, you should set it to NULL.
294 * \brief Internal data structure
296 * The contents of this structure is not visible outside the library.
298 typedef struct lzma_internal_s lzma_internal;
302 * \brief Passing data to and from liblzma
304 * The lzma_stream structure is used for
305 * - passing pointers to input and output buffers to liblzma;
306 * - defining custom memory hander functions; and
307 * - holding a pointer to coder-specific internal data structures.
309 * Before calling any of the lzma_*_init() functions the first time,
310 * the application must reset lzma_stream to LZMA_STREAM_INIT. The
311 * lzma_*_init() function will verify the options, allocate internal
312 * data structures and store pointer to them into `internal'. Finally
313 * total_in and total_out are reset to zero. In contrast to zlib,
314 * next_in and avail_in are ignored by the initialization functions.
316 * The actual coding is done with the lzma_code() function. Application
317 * must update next_in, avail_in, next_out, and avail_out between
318 * calls to lzma_decode() just like with zlib.
320 * In contrast to zlib, even the decoder requires that there always
321 * is at least one byte space in next_out; if avail_out == 0,
322 * LZMA_BUF_ERROR is returned immediatelly. This shouldn't be a problem
323 * for most applications that already use zlib, but it's still worth
324 * checking your application.
326 * Application may modify values of total_in and total_out as it wants.
327 * They are updated by liblzma to match the amount of data read and
328 * written, but liblzma doesn't use the values internally.
330 * Application must not touch the `internal' pointer.
333 const uint8_t *next_in; /**< Pointer to the next input byte. */
334 size_t avail_in; /**< Number of available input bytes in next_in. */
335 uint64_t total_in; /**< Total number of bytes read by liblzma. */
337 uint8_t *next_out; /**< Pointer to the next output position. */
338 size_t avail_out; /**< Amount of free space in next_out. */
339 uint64_t total_out; /**< Total number of bytes written by liblzma. */
342 * Custom memory allocation functions. Set to NULL to use
343 * the standard malloc() and free().
345 lzma_allocator *allocator;
347 /** Internal state is not visible to outsiders. */
348 lzma_internal *internal;
354 * \brief Initialization for lzma_stream
356 * When you declare an instance of lzma_stream, you can immediatelly
357 * initialize it so that initialization functions know that no memory
358 * has been allocated yet:
360 * lzma_stream strm = LZMA_STREAM_INIT;
362 #define LZMA_STREAM_INIT { NULL, 0, 0, NULL, 0, 0, NULL, NULL }
366 * \brief Initialization for lzma_stream
368 * This is like LZMA_STREAM_INIT, but this can be used when the lzma_stream
369 * has already been allocated:
371 * lzma_stream *strm = malloc(sizeof(lzma_stream));
373 * return LZMA_MEM_ERROR;
374 * *strm = LZMA_STREAM_INIT_VAR;
376 extern const lzma_stream LZMA_STREAM_INIT_VAR;
380 * \brief Encodes or decodes data
382 * Once the lzma_stream has been successfully initialized (e.g. with
383 * lzma_stream_encoder_single()), the actual encoding or decoding is
384 * done using this function.
386 * \return Some coders may have more exact meaning for different return
387 * values, which are mentioned separately in the description of
388 * the initialization functions. Here are the typical meanings:
389 * - LZMA_OK: So far all good.
391 * - Encoder: LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, or
392 * LZMA_FINISH completed.
393 * - Decoder: End of uncompressed data was reached.
394 * - LZMA_BUF_ERROR: Unable to progress. Provide more input or
395 * output space, and call this function again. This cannot
396 * occur if both avail_in and avail_out were non-zero (or
397 * there's a bug in liblzma).
398 * - LZMA_MEM_ERROR: Unable to allocate memory. Due to lazy
399 * programming, the coding cannot continue even if the
400 * application could free more memory. The next call must
401 * be lzma_end() or some initialization function.
403 * - Encoder: Filter(s) cannot process the given data.
404 * - Decoder: Compressed data is corrupt.
405 * - LZMA_HEADER_ERROR: Unsupported options. Rebuilding liblzma
406 * with more features enabled or upgrading to a newer version
407 * may help, although usually this is a sign of invalid options
408 * (encoder) or corrupted input data (decoder).
409 * - LZMA_PROG_ERROR: Invalid arguments or the internal state
410 * of the coder is corrupt.
412 extern lzma_ret lzma_code(lzma_stream *strm, lzma_action action);
416 * \brief Frees memory allocated for the coder data structures
418 * \param strm Pointer to lzma_stream that is at least initialized
419 * with LZMA_STREAM_INIT.
421 * \note zlib indicates an error if application end()s unfinished
422 * stream. liblzma doesn't do this, and assumes that
423 * application knows what it is doing.
425 extern void lzma_end(lzma_stream *strm);