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