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 different in encoders
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 lzma_code() and
107 * only if the LZMA_TELL_UNSUPPORTED_CHECK flag was used when
108 * initializing the decoder. The decoding can still be
109 * continued normally even if the check type is unsupported,
110 * but naturally the check will not be validated, and possible
111 * 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 internal implementation reasons, the coding cannot
140 * be continued even if more memory were made available after
144 LZMA_MEMLIMIT_ERROR = 6,
146 * \brief Memory usage limit was reached
148 * Decoder would need more memory than allowed by the
149 * specified memory usage limit. To continue decoding,
150 * the memory usage limit has to be increased with
154 LZMA_FORMAT_ERROR = 7,
156 * \brief File format not recognized
158 * The decoder did not recognize the input as supported file
159 * format. This error can occur, for example, when trying to
160 * decode .lzma format file with lzma_stream_decoder,
161 * because lzma_stream_decoder accepts only the .xz format.
164 LZMA_OPTIONS_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 .xz 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 intentional.
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()
247 * After the first use of LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, or LZMA_FINISH,
248 * the same `action' must is used until lzma_code() returns LZMA_STREAM_END.
249 * Also, the amount of input (that is, strm->avail_in) must not be modified
250 * by the application until lzma_code() returns LZMA_STREAM_END. Changing the
251 * `action' or modifying the amount of input will make lzma_code() return
257 * \brief Continue coding
259 * Encoder: Encode as much input as possible. Some internal
260 * buffering will probably be done (depends on the filter
261 * chain in use), which causes latency: the input used won't
262 * usually be decodeable from the output of the same
265 * Decoder: Decode as much input as possible and produce as
266 * much output as possible.
271 * \brief Make all the input available at output
273 * Normally the encoder introduces some latency.
274 * LZMA_SYNC_FLUSH forces all the buffered data to be
275 * available at output without resetting the internal
276 * state of the encoder. This way it is possible to use
277 * compressed stream for example for communication over
280 * Only some filters support LZMA_SYNC_FLUSH. Trying to use
281 * LZMA_SYNC_FLUSH with filters that don't support it will
282 * make lzma_code() return LZMA_OPTIONS_ERROR. For example,
283 * LZMA1 doesn't support LZMA_SYNC_FLUSH but LZMA2 does.
285 * Using LZMA_SYNC_FLUSH very often can dramatically reduce
286 * the compression ratio. With some filters (for example,
287 * LZMA2), finetuning the compression options may help
288 * mitigate this problem significantly.
290 * Decoders don't support LZMA_SYNC_FLUSH.
295 * \brief Make all the input available at output
297 * Finish encoding of the current Block. All the input
298 * data going to the current Block must have been given
299 * to the encoder (the last bytes can still be pending in
300 * next_in). Call lzma_code() with LZMA_FULL_FLUSH until
301 * it returns LZMA_STREAM_END. Then continue normally with
302 * LZMA_RUN or finish the Stream with LZMA_FINISH.
304 * This action is currently supported only by Stream encoder
305 * and easy encoder (which uses Stream encoder). If there is
306 * no unfinished Block, no empty Block is created.
311 * \brief Finish the coding operation
313 * Finishes the coding operation. All the input data must
314 * have been given to the encoder (the last bytes can still
315 * be pending in next_in). Call lzma_code() with LZMA_FINISH
316 * until it returns LZMA_STREAM_END. Once LZMA_FINISH has
317 * been used, the amount of input must no longer be changed
318 * by the application.
320 * When decoding, using LZMA_FINISH is optional unless the
321 * LZMA_CONCATENATED flag was used when the decoder was
322 * initialized. When LZMA_CONCATENATED was not used, the only
323 * effect of LZMA_FINISH is that the amount of input must not
324 * be changed just like in the encoder.
330 * \brief Custom functions for memory handling
332 * A pointer to lzma_allocator may be passed via lzma_stream structure
333 * to liblzma, and some advanced functions take a pointer to lzma_allocator
334 * as a separate function argument. The library will use the functions
335 * specified in lzma_allocator for memory handling instead of the default
336 * malloc() and free().
338 * liblzma doesn't make an internal copy of lzma_allocator. Thus, it is
339 * OK to change these function pointers in the middle of the coding
340 * process, but obviously it must be done carefully to make sure that the
341 * replacement `free' can deallocate memory allocated by the earlier
342 * `alloc' function(s).
346 * \brief Pointer to a custom memory allocation function
348 * If you don't want a custom allocator, but still want
349 * custom free(), set this to NULL and liblzma will use
350 * the standard malloc().
352 * \param opaque lzma_allocator.opaque (see below)
353 * \param nmemb Number of elements like in calloc(). liblzma
354 * will always set nmemb to 1, so it is safe to
355 * ignore nmemb in a custom allocator if you like.
356 * The nmemb argument exists only for
357 * compatibility with zlib and libbzip2.
358 * \param size Size of an element in bytes.
359 * liblzma never sets this to zero.
361 * \return Pointer to the beginning of a memory block of
362 * `size' bytes, or NULL if allocation fails
363 * for some reason. When allocation fails, functions
364 * of liblzma return LZMA_MEM_ERROR.
366 * For performance reasons, the allocator should not waste time
367 * zeroing the allocated buffers. This is not only about speed, but
368 * also memory usage, since the operating system kernel doesn't
369 * necessarily allocate the requested memory in physical memory until
370 * it is actually used. With small input files liblzma may actually
371 * need only a fraction of the memory that it requested for allocation.
373 * \note LZMA_MEM_ERROR is also used when the size of the
374 * allocation would be greater than SIZE_MAX. Thus,
375 * don't assume that the custom allocator must have
376 * returned NULL if some function from liblzma
377 * returns LZMA_MEM_ERROR.
379 void *(LZMA_API_CALL *alloc)(void *opaque, size_t nmemb, size_t size);
382 * \brief Pointer to a custom memory freeing function
384 * If you don't want a custom freeing function, but still
385 * want a custom allocator, set this to NULL and liblzma
386 * will use the standard free().
388 * \param opaque lzma_allocator.opaque (see below)
389 * \param ptr Pointer returned by lzma_allocator.alloc(),
390 * or when it is set to NULL, a pointer returned
391 * by the standard malloc().
393 void (LZMA_API_CALL *free)(void *opaque, void *ptr);
396 * \brief Pointer passed to .alloc() and .free()
398 * opaque is passed as the first argument to lzma_allocator.alloc()
399 * and lzma_allocator.free(). This intended to ease implementing
400 * custom memory allocation functions for use with liblzma.
402 * If you don't need this, you should set this to NULL.
410 * \brief Internal data structure
412 * The contents of this structure is not visible outside the library.
414 typedef struct lzma_internal_s lzma_internal;
418 * \brief Passing data to and from liblzma
420 * The lzma_stream structure is used for
421 * - passing pointers to input and output buffers to liblzma;
422 * - defining custom memory hander functions; and
423 * - holding a pointer to coder-specific internal data structures.
427 * - After allocating lzma_stream (on stack or with malloc()), it must be
428 * initialized to LZMA_STREAM_INIT (see LZMA_STREAM_INIT for details).
430 * - Initialize a coder to the lzma_stream, for example by using
431 * lzma_easy_encoder() or lzma_auto_decoder(). Some notes:
432 * - In contrast to zlib, strm->next_in and strm->next_out are
433 * ignored by all initialization functions, thus it is safe
434 * to not initialize them yet.
435 * - The initialization functions always set strm->total_in and
436 * strm->total_out to zero.
437 * - If the initialization function fails, no memory is left allocated
438 * that would require freeing with lzma_end() even if some memory was
439 * associated with the lzma_stream structure when the initialization
440 * function was called.
442 * - Use lzma_code() to do the actual work.
444 * - Once the coding has been finished, the existing lzma_stream can be
445 * reused. It is OK to reuse lzma_stream with different initialization
446 * function without calling lzma_end() first. Old allocations are
447 * automatically freed.
449 * - Finally, use lzma_end() to free the allocated memory. lzma_end() never
450 * frees the lzma_stream structure itself.
452 * Application may modify the values of total_in and total_out as it wants.
453 * They are updated by liblzma to match the amount of data read and
454 * written, but aren't used for anything else.
457 const uint8_t *next_in; /**< Pointer to the next input byte. */
458 size_t avail_in; /**< Number of available input bytes in next_in. */
459 uint64_t total_in; /**< Total number of bytes read by liblzma. */
461 uint8_t *next_out; /**< Pointer to the next output position. */
462 size_t avail_out; /**< Amount of free space in next_out. */
463 uint64_t total_out; /**< Total number of bytes written by liblzma. */
466 * Custom memory allocation functions. Set to NULL to use
467 * the standard malloc() and free().
469 lzma_allocator *allocator;
471 /** Internal state is not visible to applications. */
472 lzma_internal *internal;
475 * Reserved space to allow possible future extensions without
476 * breaking the ABI. Excluding the initialization of this structure,
477 * you should not touch these, because the names of these variables
482 uint64_t reserved_int1;
483 uint64_t reserved_int2;
484 lzma_reserved_enum reserved_enum1;
485 lzma_reserved_enum reserved_enum2;
491 * \brief Initialization for lzma_stream
493 * When you declare an instance of lzma_stream, you can immediatelly
494 * initialize it so that initialization functions know that no memory
495 * has been allocated yet:
497 * lzma_stream strm = LZMA_STREAM_INIT;
499 * If you need to initialize a dynamically allocated lzma_stream, you can use
500 * memset(strm_pointer, 0, sizeof(lzma_stream)). Strictly speaking, this
501 * violates the C standard since NULL may have different internal
502 * representation than zero, but it should be portable enough in practice.
503 * Anyway, for maximum portability, you can use something like this:
505 * lzma_stream tmp = LZMA_STREAM_INIT;
508 #define LZMA_STREAM_INIT \
509 { NULL, 0, 0, NULL, 0, 0, NULL, NULL, \
510 NULL, NULL, 0, 0, LZMA_RESERVED_ENUM, LZMA_RESERVED_ENUM }
514 * \brief Encode or decode data
516 * Once the lzma_stream has been successfully initialized (e.g. with
517 * lzma_stream_encoder()), the actual encoding or decoding is done
518 * using this function. The application has to update strm->next_in,
519 * strm->avail_in, strm->next_out, and strm->avail_out to pass input
520 * to and get output from liblzma.
522 * See the description of the coder-specific initialization function to find
523 * out what `action' values are supported by the coder. See documentation of
524 * lzma_ret for the possible return values.
526 extern LZMA_API(lzma_ret) lzma_code(lzma_stream *strm, lzma_action action)
527 lzma_attr_warn_unused_result;
531 * \brief Free memory allocated for the coder data structures
533 * \param strm Pointer to lzma_stream that is at least initialized
534 * with LZMA_STREAM_INIT.
536 * After lzma_end(strm), strm->internal is guaranteed to be NULL. No other
537 * members of the lzma_stream structure are touched.
539 * \note zlib indicates an error if application end()s unfinished
540 * stream structure. liblzma doesn't do this, and assumes that
541 * application knows what it is doing.
543 extern LZMA_API(void) lzma_end(lzma_stream *strm);
547 * \brief Get the memory usage of decoder filter chain
549 * This function is currently supported only when *strm has been initialized
550 * with a function that takes a memlimit argument. With other functions, you
551 * should use e.g. lzma_raw_encoder_memusage() or lzma_raw_decoder_memusage()
552 * to estimate the memory requirements.
554 * This function is useful e.g. after LZMA_MEMLIMIT_ERROR to find out how big
555 * the memory usage limit should have been to decode the input. Note that
556 * this may give misleading information if decoding .xz Streams that have
557 * multiple Blocks, because each Block can have different memory requirements.
559 * \return Rough estimate of how much memory is currently allocated
560 * for the filter decoders. If no filter chain is currently
561 * allocated, some non-zero value is still returned, which is
562 * less than or equal to what any filter chain would indicate
563 * as its memory requirement.
565 * If this function isn't supported by *strm or some other error
566 * occurs, zero is returned.
568 extern LZMA_API(uint64_t) lzma_memusage(const lzma_stream *strm);
572 * \brief Get the current memory usage limit
574 * This function is supported only when *strm has been initialized with
575 * a function that takes a memlimit argument.
577 * \return On success, the current memory usage limit is returned
578 * (always non-zero). On error, zero is returned.
580 extern LZMA_API(uint64_t) lzma_memlimit_get(const lzma_stream *strm);
584 * \brief Set the memory usage limit
586 * This function is supported only when *strm has been initialized with
587 * a function that takes a memlimit argument.
589 * \return - LZMA_OK: New memory usage limit successfully set.
590 * - LZMA_MEMLIMIT_ERROR: The new limit is too small.
591 * The limit was not changed.
592 * - LZMA_PROG_ERROR: Invalid arguments, e.g. *strm doesn't
593 * support memory usage limit or memlimit was zero.
595 extern LZMA_API(lzma_ret) lzma_memlimit_set(
596 lzma_stream *strm, uint64_t memlimit);