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