]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/block.h
Added lzma_stream_buffer_decode() and made minor cleanups.
[icculus/xz.git] / src / liblzma / api / lzma / block.h
1 /**
2  * \file        lzma/block.h
3  * \brief       .xz Block handling
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       Options for the Block and Block Header encoders and decoders
26  *
27  * Different Block handling functions use different parts of this structure.
28  * Some read some members, other functions write, and some do both. Only the
29  * members listed for reading need to be initialized when the specified
30  * functions are called. The members marked for writing will be assigned
31  * new values at some point either by calling the given function or by
32  * later calls to lzma_code().
33  */
34 typedef struct {
35         /**
36          * \brief       Block format version
37          *
38          * To prevent API and ABI breakages if new features are needed in
39          * Block, a version number is used to indicate which fields in this
40          * structure are in use. For now, version must always be zero.
41          * With non-zero version, most Block related functions will return
42          * LZMA_OPTIONS_ERROR.
43          *
44          * The decoding functions will always set this to the lowest value
45          * that supports all the features indicated by the Block Header field.
46          * The application must check that the version number set by the
47          * decoding functions is supported by the application. Otherwise it
48          * is possible that the application will decode the Block incorrectly.
49          *
50          * Read by:
51          *  - lzma_block_header_size()
52          *  - lzma_block_header_encode()
53          *  - lzma_block_compressed_size()
54          *  - lzma_block_unpadded_size()
55          *  - lzma_block_total_size()
56          *  - lzma_block_encoder()
57          *  - lzma_block_decoder()
58          *  - lzma_block_buffer_encode()
59          *
60          * Written by:
61          *  - lzma_block_header_decode()
62          */
63         uint32_t version;
64
65         /**
66          * \brief       Size of the Block Header field
67          *
68          * This is always a multiple of four.
69          *
70          * Read by:
71          *  - lzma_block_header_encode()
72          *  - lzma_block_header_decode()
73          *  - lzma_block_compressed_size()
74          *  - lzma_block_unpadded_size()
75          *  - lzma_block_total_size()
76          *  - lzma_block_decoder()
77          *
78          * Written by:
79          *  - lzma_block_header_size()
80          *  - lzma_block_buffer_encode()
81          */
82         uint32_t header_size;
83 #       define LZMA_BLOCK_HEADER_SIZE_MIN 8
84 #       define LZMA_BLOCK_HEADER_SIZE_MAX 1024
85
86         /**
87          * \brief       Type of integrity Check
88          *
89          * The Check ID is not stored into the Block Header, thus its value
90          * must be provided also when decoding.
91          *
92          * Read by:
93          *  - lzma_block_header_encode()
94          *  - lzma_block_header_decode()
95          *  - lzma_block_compressed_size()
96          *  - lzma_block_unpadded_size()
97          *  - lzma_block_total_size()
98          *  - lzma_block_encoder()
99          *  - lzma_block_decoder()
100          *  - lzma_block_buffer_encode()
101          */
102         lzma_check check;
103
104         /**
105          * \brief       Size of the Compressed Data in bytes
106          *
107          * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
108          * will store this value to the Block Header. Block encoder doesn't
109          * care about this value, but will set it once the encoding has been
110          * finished.
111          *
112          * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
113          * verify that the size of the Compressed Data field matches
114          * compressed_size.
115          *
116          * Usually you don't know this value when encoding in streamed mode,
117          * and thus cannot write this field into the Block Header.
118          *
119          * In non-streamed mode you can reserve space for this field before
120          * encoding the actual Block. After encoding the data, finish the
121          * Block by encoding the Block Header. Steps in detail:
122          *
123          *  - Set compressed_size to some big enough value. If you don't know
124          *    better, use LZMA_VLI_MAX, but remember that bigger values take
125          *    more space in Block Header.
126          *
127          *  - Call lzma_block_header_size() to see how much space you need to
128          *    reserve for the Block Header.
129          *
130          *  - Encode the Block using lzma_block_encoder() and lzma_code().
131          *    It sets compressed_size to the correct value.
132          *
133          *  - Use lzma_block_header_encode() to encode the Block Header.
134          *    Because space was reserved in the first step, you don't need
135          *    to call lzma_block_header_size() anymore, because due to
136          *    reserving, header_size has to be big enough. If it is "too big",
137          *    lzma_block_header_encode() will add enough Header Padding to
138          *    make Block Header to match the size specified by header_size.
139          *
140          * Read by:
141          *  - lzma_block_header_size()
142          *  - lzma_block_header_encode()
143          *  - lzma_block_compressed_size()
144          *  - lzma_block_unpadded_size()
145          *  - lzma_block_total_size()
146          *  - lzma_block_decoder()
147          *
148          * Written by:
149          *  - lzma_block_header_decode()
150          *  - lzma_block_compressed_size()
151          *  - lzma_block_encoder()
152          *  - lzma_block_decoder()
153          *  - lzma_block_buffer_encode()
154          */
155         lzma_vli compressed_size;
156
157         /**
158          * \brief       Uncompressed Size in bytes
159          *
160          * This is handled very similarly to compressed_size above.
161          *
162          * Unlike compressed_size, uncompressed_size is needed by fewer
163          * functions. This is because uncompressed_size isn't needed to
164          * validate that Block stays within proper limits.
165          *
166          * Read by:
167          *  - lzma_block_header_size()
168          *  - lzma_block_header_encode()
169          *  - lzma_block_decoder()
170          *
171          * Written by:
172          *  - lzma_block_header_decode()
173          *  - lzma_block_encoder()
174          *  - lzma_block_decoder()
175          *  - lzma_block_buffer_encode()
176          */
177         lzma_vli uncompressed_size;
178
179         /**
180          * \brief       Array of filters
181          *
182          * There can be 1-4 filters. The end of the array is marked with
183          * .id = LZMA_VLI_UNKNOWN.
184          *
185          * Read by:
186          *  - lzma_block_header_size()
187          *  - lzma_block_header_encode()
188          *  - lzma_block_encoder()
189          *  - lzma_block_decoder()
190          *  - lzma_block_buffer_encode()
191          *
192          * Written by:
193          *  - lzma_block_header_decode(): Note that this does NOT free()
194          *    the old filter options structures. All unused filters[] will
195          *    have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
196          *    decoding fails, all filters[] are guaranteed to be
197          *    LZMA_VLI_UNKNOWN and NULL.
198          *
199          * \note        Because of the array is terminated with
200          *              .id = LZMA_VLI_UNKNOWN, the actual array must
201          *              have LZMA_FILTERS_MAX + 1 members or the Block
202          *              Header decoder will overflow the buffer.
203          */
204         lzma_filter *filters;
205
206         /*
207          * Reserved space to allow possible future extensions without
208          * breaking the ABI. You should not touch these, because the names
209          * of these variables may change. These are and will never be used
210          * with the currently supported options, so it is safe to leave these
211          * uninitialized.
212          */
213         void *reserved_ptr1;
214         void *reserved_ptr2;
215         void *reserved_ptr3;
216         uint32_t reserved_int1;
217         uint32_t reserved_int2;
218         lzma_vli reserved_int3;
219         lzma_vli reserved_int4;
220         lzma_vli reserved_int5;
221         lzma_vli reserved_int6;
222         lzma_vli reserved_int7;
223         lzma_vli reserved_int8;
224         lzma_reserved_enum reserved_enum1;
225         lzma_reserved_enum reserved_enum2;
226         lzma_reserved_enum reserved_enum3;
227         lzma_reserved_enum reserved_enum4;
228         lzma_bool reserved_bool1;
229         lzma_bool reserved_bool2;
230         lzma_bool reserved_bool3;
231         lzma_bool reserved_bool4;
232         lzma_bool reserved_bool5;
233         lzma_bool reserved_bool6;
234         lzma_bool reserved_bool7;
235         lzma_bool reserved_bool8;
236
237 } lzma_block;
238
239
240 /**
241  * \brief       Decode the Block Header Size field
242  *
243  * To decode Block Header using lzma_block_header_decode(), the size of the
244  * Block Header has to be known and stored into lzma_block.header_size.
245  * The size can be calculated from the first byte of a Block using this macro.
246  * Note that if the first byte is 0x00, it indicates beginning of Index; use
247  * this macro only when the byte is not 0x00.
248  *
249  * There is no encoding macro, because Block Header encoder is enough for that.
250  */
251 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
252
253
254 /**
255  * \brief       Calculate Block Header Size
256  *
257  * Calculate the minimum size needed for the Block Header field using the
258  * settings specified in the lzma_block structure. Note that it is OK to
259  * increase the calculated header_size value as long as it is a multiple of
260  * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
261  * just means that lzma_block_header_encode() will add Header Padding.
262  *
263  * \return      - LZMA_OK: Size calculated successfully and stored to
264  *                block->header_size.
265  *              - LZMA_OPTIONS_ERROR: Unsupported version, filters or
266  *                filter options.
267  *              - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
268  *
269  * \note        This doesn't check that all the options are valid i.e. this
270  *              may return LZMA_OK even if lzma_block_header_encode() or
271  *              lzma_block_encoder() would fail. If you want to validate the
272  *              filter chain, consider using lzma_memlimit_encoder() which as
273  *              a side-effect validates the filter chain.
274  */
275 extern lzma_ret lzma_block_header_size(lzma_block *block)
276                 lzma_attr_warn_unused_result;
277
278
279 /**
280  * \brief       Encode Block Header
281  *
282  * The caller must have calculated the size of the Block Header already with
283  * lzma_block_header_size(). If larger value than the one calculated by
284  * lzma_block_header_size() is used, the Block Header will be padded to the
285  * specified size.
286  *
287  * \param       out         Beginning of the output buffer. This must be
288  *                          at least block->header_size bytes.
289  * \param       block       Block options to be encoded.
290  *
291  * \return      - LZMA_OK: Encoding was successful. block->header_size
292  *                bytes were written to output buffer.
293  *              - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
294  *              - LZMA_PROG_ERROR: Invalid arguments, for example
295  *                block->header_size is invalid or block->filters is NULL.
296  */
297 extern lzma_ret lzma_block_header_encode(const lzma_block *block, uint8_t *out)
298                 lzma_attr_warn_unused_result;
299
300
301 /**
302  * \brief       Decode Block Header
303  *
304  * The size of the Block Header must have already been decoded with
305  * lzma_block_header_size_decode() macro and stored to block->header_size.
306  * block->filters must have been allocated, but not necessarily initialized.
307  * Possible existing filter options are _not_ freed.
308  *
309  * \param       block       Destination for block options with header_size
310  *                          properly initialized.
311  * \param       allocator   lzma_allocator for custom allocator functions.
312  *                          Set to NULL to use malloc() (and also free()
313  *                          if an error occurs).
314  * \param       in          Beginning of the input buffer. This must be
315  *                          at least block->header_size bytes.
316  *
317  * \return      - LZMA_OK: Decoding was successful. block->header_size
318  *                bytes were read from the input buffer.
319  *              - LZMA_OPTIONS_ERROR: The Block Header specifies some
320  *                unsupported options such as unsupported filters.
321  *              - LZMA_DATA_ERROR: Block Header is corrupt, for example,
322  *                the CRC32 doesn't match.
323  *              - LZMA_PROG_ERROR: Invalid arguments, for example
324  *                block->header_size is invalid or block->filters is NULL.
325  */
326 extern lzma_ret lzma_block_header_decode(lzma_block *block,
327                 lzma_allocator *allocator, const uint8_t *in)
328                 lzma_attr_warn_unused_result;
329
330
331 /**
332  * \brief       Validate and set Compressed Size according to Unpadded Size
333  *
334  * Block Header stores Compressed Size, but Index has Unpadded Size. If the
335  * application has already parsed the Index and is now decoding Blocks,
336  * it can calculate Compressed Size from Unpadded Size. This function does
337  * exactly that with error checking:
338  *
339  *  - Compressed Size calculated from Unpadded Size must be positive integer,
340  *    that is, Unpadded Size must be big enough that after Block Header and
341  *    Check fields there's still at least one byte for Compressed Size.
342  *
343  *  - If Compressed Size was present in Block Header, the new value
344  *    calculated from Unpadded Size is compared against the value
345  *    from Block Header.
346  *
347  * \note        This function must be called _after_ decoding the Block Header
348  *              field so that it can properly validate Compressed Size if it
349  *              was present in Block Header.
350  *
351  * \return      - LZMA_OK: block->compressed_size was set successfully.
352  *              - LZMA_DATA_ERROR: unpadded_size is too small compared to
353  *                block->header_size and lzma_check_size(block->check).
354  *              - LZMA_PROG_ERROR: Some values are invalid. For example,
355  *                block->header_size must be a multiple of four and
356  *                between 8 and 1024 inclusive.
357  */
358 extern lzma_ret lzma_block_compressed_size(
359                 lzma_block *block, lzma_vli unpadded_size)
360                 lzma_attr_warn_unused_result;
361
362
363 /**
364  * \brief       Calculate Unpadded Size
365  *
366  * The Index field stores Unpadded Size and Uncompressed Size. The latter
367  * can be taken directly from the lzma_block structure after coding a Block,
368  * but Unpadded Size needs to be calculated from Block Header Size,
369  * Compressed Size, and size of the Check field. This is where this function
370  * is needed.
371  *
372  * \return      Unpadded Size on success, or zero on error.
373  */
374 extern lzma_vli lzma_block_unpadded_size(const lzma_block *block)
375                 lzma_attr_pure;
376
377
378 /**
379  * \brief       Calculate the total encoded size of a Block
380  *
381  * This is equivalent to lzma_block_unpadded_size() except that the returned
382  * value includes the size of the Block Padding field.
383  *
384  * \return      On success, total encoded size of the Block. On error,
385  *              zero is returned.
386  */
387 extern lzma_vli lzma_block_total_size(const lzma_block *block)
388                 lzma_attr_pure;
389
390
391 /**
392  * \brief       Initialize .xz Block encoder
393  *
394  * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
395  * filter chain supports it), and LZMA_FINISH.
396  *
397  * \return      - LZMA_OK: All good, continue with lzma_code().
398  *              - LZMA_MEM_ERROR
399  *              - LZMA_OPTIONS_ERROR
400  *              - LZMA_UNSUPPORTED_CHECK: block->check specfies a Check ID
401  *                that is not supported by this buid of liblzma. Initializing
402  *                the encoder failed.
403  *              - LZMA_PROG_ERROR
404  */
405 extern lzma_ret lzma_block_encoder(lzma_stream *strm, lzma_block *block)
406                 lzma_attr_warn_unused_result;
407
408
409 /**
410  * \brief       Initialize .xz Block decoder
411  *
412  * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
413  * LZMA_FINISH is not required. It is supported only for convenience.
414  *
415  * \return      - LZMA_OK: All good, continue with lzma_code().
416  *              - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
417  *                the given Check ID is not supported, thus Check will be
418  *                ignored.
419  *              - LZMA_PROG_ERROR
420  *              - LZMA_MEM_ERROR
421  */
422 extern lzma_ret lzma_block_decoder(lzma_stream *strm, lzma_block *block)
423                 lzma_attr_warn_unused_result;
424
425
426 /**
427  * \brief       Calculate maximum output buffer size for single-call encoding
428  *
429  * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
430  * See the documentation of lzma_stream_buffer_bound().
431  */
432 extern size_t lzma_block_buffer_bound(size_t uncompressed_size);
433
434
435 /**
436  * \brief       Single-call .xz Block encoder
437  *
438  * In contrast to the multi-call encoder initialized with
439  * lzma_block_encoder(), this function encodes also the Block Header. This
440  * is required to make it possible to write appropriate Block Header also
441  * in case the data isn't compressible, and different filter chain has to be
442  * used to encode the data in uncompressed form using uncompressed chunks
443  * of the LZMA2 filter.
444  *
445  * When the data isn't compressible, header_size, compressed_size, and
446  * uncompressed_size are set just like when the data was compressible, but
447  * it is possible that header_size is too small to hold the filter chain
448  * specified in block->filters, because that isn't necessarily the filter
449  * chain that was actually used to encode the data. lzma_block_unpadded_size()
450  * still works normally, because it doesn't read the filters array.
451  *
452  * \param       block       Block options: block->version, block->check,
453  *                          and block->filters must be initialized.
454  * \param       allocator   lzma_allocator for custom allocator functions.
455  *                          Set to NULL to use malloc() and free().
456  * \param       in          Beginning of the input buffer
457  * \param       in_size     Size of the input buffer
458  * \param       out         Beginning of the output buffer
459  * \param       out_pos     The next byte will be written to out[*out_pos].
460  *                          *out_pos is updated only if encoding succeeds.
461  * \param       out_size    Size of the out buffer; the first byte into
462  *                          which no data is written to is out[out_size].
463  *
464  * \return      - LZMA_OK: Encoding was successful.
465  *              - LZMA_BUF_ERROR: Not enough output buffer space.
466  *              - LZMA_OPTIONS_ERROR
467  *              - LZMA_MEM_ERROR
468  *              - LZMA_DATA_ERROR
469  *              - LZMA_PROG_ERROR
470  */
471 extern lzma_ret lzma_block_buffer_encode(
472                 lzma_block *block, lzma_allocator *allocator,
473                 const uint8_t *in, size_t in_size,
474                 uint8_t *out, size_t *out_pos, size_t out_size)
475                 lzma_attr_warn_unused_result;