]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/base.h
Imported to git.
[icculus/xz.git] / src / liblzma / api / lzma / base.h
1 /**
2  * \file        lzma/base.h
3  * \brief       Data types and functions used in many places of the public API
4  *
5  * \author      Copyright (C) 1999-2006 Igor Pavlov
6  * \author      Copyright (C) 2007 Lasse Collin
7  *
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.
12  *
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.
17  */
18
19 #ifndef LZMA_H_INTERNAL
20 #       error Never include this file directly. Use <lzma.h> instead.
21 #endif
22
23
24 /**
25  * \brief       Boolean
26  *
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).
32  */
33 typedef unsigned char lzma_bool;
34
35
36 /**
37  * \brief       Return values used by several functions in liblzma
38  *
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.
42  */
43 typedef enum {
44         LZMA_OK                 =  0,
45                 /**<
46                  * \brief       Operation completed successfully
47                  */
48
49         LZMA_STREAM_END         =  1,
50                 /**<
51                  * \brief       End of stream was reached
52                  *
53                  * The application should pick the last remaining output
54                  * bytes from strm->next_out.
55                  */
56
57         LZMA_PROG_ERROR       = -2,
58                 /**<
59                  * \brief       Programming error
60                  *
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
70                  *     hardware.
71                  *
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
74                  * how to report bugs.
75                  */
76
77         LZMA_DATA_ERROR         = -3,
78                 /**<
79                  * \brief       Data is corrupt
80                  *
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
85                  *   integrity Check.
86                  *
87                  * \todo        What can be done if encoder returns this?
88                  *              Probably can continue by fixing the input
89                  *              amount, but make sure.
90                  */
91
92         LZMA_MEM_ERROR          = -4,
93                 /**<
94                  * \brief       Cannot allocate memory
95                  *
96                  * Memory allocation failed.
97                  */
98
99         LZMA_BUF_ERROR          = -5,
100                 /**<
101                  * \brief       No progress is possible
102                  *
103                  * This may happen when avail_in or avail_out is zero.
104                  *
105                  * \note        This error is not fatal. Coding can continue
106                  *              normally once the reason for this error has
107                  *              been fixed.
108                  */
109
110         LZMA_HEADER_ERROR       = -6,
111                 /**<
112                  * \brief       Invalid or unsupported header
113                  *
114                  * Invalid or unsupported options, for example
115                  *  - unsupported filter(s) or filter options; or
116                  *  - reserved bits set in headers (decoder only).
117                  *
118                  * Rebuilding liblzma with more features enabled, or
119                  * upgrading to a newer version of liblzma may help.
120                  */
121
122         LZMA_UNSUPPORTED_CHECK  = -7,
123                 /**<
124                  * \brief       Check type is unknown
125                  *
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).
130                  */
131 } lzma_ret;
132
133
134 /**
135  * \brief       The `action' argument for lzma_code()
136  */
137 typedef enum {
138         LZMA_RUN = 0,
139                 /**<
140                  * Encoder: Encode as much input as possible. Some internal
141                  * buffering will probably be done (depends on the filter
142                  * chain in use), which causes latency: the input used won't
143                  * usually be decodeable from the output of the same
144                  * lzma_code() call.
145                  *
146                  * Decoder: Decode as much input as possible and produce as
147                  * much output as possible. This action provides best
148                  * throughput, but may introduce latency, because the
149                  * decoder may decode more data into its internal buffers
150                  * than that fits into next_out.
151                  */
152
153         LZMA_SYNC_FLUSH = 1,
154                 /**<
155                  * Encoder: Makes all the data given to liblzma via next_in
156                  * available in next_out without resetting the filters. Call
157                  * lzma_code() with LZMA_SYNC_FLUSH until it returns
158                  * LZMA_STREAM_END. Then continue encoding normally.
159                  *
160                  * \note        Synchronous flushing is supported only by
161                  *              some filters. Some filters support it only
162                  *              partially.
163                  *
164                  * Decoder: Asks the decoder to decode only as much as is
165                  * needed to fill next_out. This decreases latency with some
166                  * filters, but is likely to decrease also throughput. It is
167                  * a good idea to use this flag only when it is likely that
168                  * you don't need more output soon.
169                  *
170                  * \note        With decoder, this is not comparable to
171                  *              zlib's Z_SYNC_FLUSH.
172                  */
173
174         LZMA_FULL_FLUSH = 2,
175                 /**<
176                  * Finishes encoding of the current Data Block. All the input
177                  * data going to the current Data Block must have been given
178                  * to the encoder (the last bytes can still be pending in
179                  * next_in). Call lzma_code() with LZMA_FULL_FLUSH until
180                  * it returns LZMA_STREAM_END. Then continue normally with
181                  * LZMA_RUN or finish the Stream with LZMA_FINISH.
182                  *
183                  * This action is supported only by Multi-Block Stream
184                  * encoder. If there is no unfinished Data Block, no empty
185                  * Data Block is created.
186                  */
187
188         LZMA_FINISH = 3
189                 /**<
190                  * Finishes the encoding operation. All the input data must
191                  * have been given to the encoder (the last bytes can still
192                  * be pending in next_in). Call lzma_code() with LZMA_FINISH
193                  * until it returns LZMA_STREAM_END.
194                  *
195                  * This action is not supported by decoders.
196                  */
197 } lzma_action;
198
199
200 /**
201  * \brief       Custom functions for memory handling
202  *
203  * A pointer to lzma_allocator may be passed via lzma_stream structure
204  * to liblzma. The library will use these functions for memory handling
205  * instead of the default malloc() and free().
206  *
207  * liblzma doesn't make an internal copy of lzma_allocator. Thus, it is
208  * OK to change these function pointers in the middle of the coding
209  * process, but obviously it must be done carefully to make sure that the
210  * replacement `free' can deallocate memory allocated by the earlier
211  * `alloc' function(s).
212  */
213 typedef struct {
214         /**
215          * \brief       Pointer to custom memory allocation function
216          *
217          * Set this to point to your custom memory allocation function.
218          * It can be useful for example if you want to limit how much
219          * memory liblzma is allowed to use: for this, you may use
220          * a pointer to lzma_memory_alloc().
221          *
222          * If you don't want a custom allocator, but still want
223          * custom free(), set this to NULL and liblzma will use
224          * the standard malloc().
225          *
226          * \param       opaque  lzma_allocator.opaque (see below)
227          * \param       nmemb   Number of elements like in calloc().
228          *                      liblzma will always set nmemb to 1.
229          *                      This argument exists only for
230          *                      compatibility with zlib and libbzip2.
231          * \param       size    Size of an element in bytes.
232          *                      liblzma never sets this to zero.
233          *
234          * \return      Pointer to the beginning of a memory block of
235          *              size nmemb * size, or NULL if allocation fails
236          *              for some reason. When allocation fails, functions
237          *              of liblzma return LZMA_MEM_ERROR.
238          */
239         void *(*alloc)(void *opaque, size_t nmemb, size_t size);
240
241         /**
242          * \brief       Pointer to custom memory freeing function
243          *
244          * Set this to point to your custom memory freeing function.
245          * If lzma_memory_alloc() is used as allocator, this should
246          * be set to lzma_memory_free().
247          *
248          * If you don't want a custom freeing function, but still
249          * want a custom allocator, set this to NULL and liblzma
250          * will use the standard free().
251          *
252          * \param       opaque  lzma_allocator.opaque (see below)
253          * \param       ptr     Pointer returned by
254          *                      lzma_allocator.alloc(), or when it
255          *                      is set to NULL, a pointer returned
256          *                      by the standard malloc().
257          */
258         void (*free)(void *opaque, void *ptr);
259
260         /**
261          * \brief       Pointer passed to .alloc() and .free()
262          *
263          * opaque is passed as the first argument to lzma_allocator.alloc()
264          * and lzma_allocator.free(). This intended to ease implementing
265          * custom memory allocation functions for use with liblzma.
266          *
267          * When using lzma_memory_alloc() and lzma_memory_free(), opaque
268          * must point to lzma_memory_limitter structure allocated and
269          * initialized with lzma_memory_limitter_create().
270          *
271          * If you don't need this, you should set it to NULL.
272          */
273         void *opaque;
274
275 } lzma_allocator;
276
277
278 /**
279  * \brief       Internal data structure
280  *
281  * The contents of this structure is not visible outside the library.
282  */
283 typedef struct lzma_internal_s lzma_internal;
284
285
286 /**
287  * \brief       Passing data to and from liblzma
288  *
289  * The lzma_stream structure is used for
290  *   - passing pointers to input and output buffers to liblzma;
291  *   - defining custom memory hander functions; and
292  *   - holding a pointer to coder-specific internal data structures.
293  *
294  * Before calling any of the lzma_*_init() functions the first time,
295  * the application must reset lzma_stream to LZMA_STREAM_INIT. The
296  * lzma_*_init() function will verify the options, allocate internal
297  * data structures and store pointer to them into `internal'. Finally
298  * total_in and total_out are reset to zero. In contrast to zlib,
299  * next_in and avail_in are ignored by the initialization functions.
300  *
301  * The actual coding is done with the lzma_code() function. Application
302  * must update next_in, avail_in, next_out, and avail_out between
303  * calls to lzma_decode() just like with zlib.
304  *
305  * In contrast to zlib, even the decoder requires that there always
306  * is at least one byte space in next_out; if avail_out == 0,
307  * LZMA_BUF_ERROR is returned immediatelly. This shouldn't be a problem
308  * for most applications that already use zlib, but it's still worth
309  * checking your application.
310  *
311  * Application may modify values of total_in and total_out as it wants.
312  * They are updated by liblzma to match the amount of data read and
313  * written, but liblzma doesn't use the values internally.
314  *
315  * Application must not touch the `internal' pointer.
316  */
317 typedef struct {
318         uint8_t *next_in;   /**< Pointer to the next input byte. */
319         size_t avail_in;    /**< Number of available input bytes in next_in. */
320         uint64_t total_in;  /**< Total number of bytes read by liblzma. */
321
322         uint8_t *next_out;  /**< Pointer to the next output position. */
323         size_t avail_out;   /**< Amount of free space in next_out. */
324         uint64_t total_out; /**< Total number of bytes written by liblzma. */
325
326         /**
327          * Custom memory allocation functions. Set to NULL to use
328          * the standard malloc() and free().
329          */
330         lzma_allocator *allocator;
331
332         /** Internal state is not visible to outsiders. */
333         lzma_internal *internal;
334
335 } lzma_stream;
336
337
338 /**
339  * \brief       Initialization for lzma_stream
340  *
341  * When you declare an instance of lzma_stream, you can immediatelly
342  * initialize it so that initialization functions know that no memory
343  * has been allocated yet:
344  *
345  *     lzma_stream strm = LZMA_STREAM_INIT;
346  */
347 #define LZMA_STREAM_INIT { NULL, 0, 0, NULL, 0, 0, NULL, NULL }
348
349
350 /**
351  * \brief       Initialization for lzma_stream
352  *
353  * This is like LZMA_STREAM_INIT, but this can be used when the lzma_stream
354  * has already been allocated:
355  *
356  *     lzma_stream *strm = malloc(sizeof(lzma_stream));
357  *     if (strm == NULL)
358  *         return LZMA_MEM_ERROR;
359  *     *strm = LZMA_STREAM_INIT_VAR;
360  */
361 extern const lzma_stream LZMA_STREAM_INIT_VAR;
362
363
364 /**
365  * \brief       Encodes or decodes data
366  *
367  * Once the lzma_stream has been successfully initialized (e.g. with
368  * lzma_stream_encoder_single()), the actual encoding or decoding is
369  * done using this function.
370  *
371  * \return      Some coders may have more exact meaning for different return
372  *              values, which are mentioned separately in the description of
373  *              the initialization functions. Here are the typical meanings:
374  *              - LZMA_OK: So far all good.
375  *              - LZMA_STREAM_END:
376  *                  - Encoder: LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, or
377  *                    LZMA_FINISH completed.
378  *                  - Decoder: End of uncompressed data was reached.
379  *              - LZMA_BUF_ERROR: Unable to progress. Provide more input or
380  *                output space, and call this function again. This cannot
381  *                occur if both avail_in and avail_out were non-zero (or
382  *                there's a bug in liblzma).
383  *              - LZMA_MEM_ERROR: Unable to allocate memory. Due to lazy
384  *                programming, the coding cannot continue even if the
385  *                application could free more memory. The next call must
386  *                be lzma_end() or some initialization function.
387  *              - LZMA_DATA_ERROR:
388  *                  - Encoder: Filter(s) cannot process the given data.
389  *                  - Decoder: Compressed data is corrupt.
390  *              - LZMA_HEADER_ERROR: Unsupported options. Rebuilding liblzma
391  *                with more features enabled or upgrading to a newer version
392  *                may help, although usually this is a sign of invalid options
393  *                (encoder) or corrupted input data (decoder).
394  *              - LZMA_PROG_ERROR: Invalid arguments or the internal state
395  *                of the coder is corrupt.
396  */
397 extern lzma_ret lzma_code(lzma_stream *strm, lzma_action action);
398
399
400 /**
401  * \brief       Frees memory allocated for the coder data structures
402  *
403  * \param       strm    Pointer to lzma_stream that is at least initialized
404  *                      with LZMA_STREAM_INIT.
405  *
406  * \note        zlib indicates an error if application end()s unfinished
407  *              stream. liblzma doesn't do this, and assumes that
408  *              application knows what it is doing.
409  */
410 extern void lzma_end(lzma_stream *strm);