]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/api/lzma/block.h
Updates to liblzma API headers.
[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          * uncompressed_size is needed by fewer functions than
164          * compressed_size. This is because uncompressed_size isn't
165          * needed to 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          * \brief       Raw value stored in the Check field
212          *
213          * After successful coding, the first lzma_check_size(check) bytes
214          * of this array contain the raw value stored in the Check field.
215          *
216          * Note that CRC32 and CRC64 are stored in little endian byte order.
217          * Take it into account if you display the Check values to the user.
218          *
219          * Written by:
220          *  - lzma_block_encoder()
221          *  - lzma_block_decoder()
222          *  - lzma_block_buffer_encode()
223          *  - lzma_block_buffer_decode()
224          */
225         uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
226
227         /*
228          * Reserved space to allow possible future extensions without
229          * breaking the ABI. You should not touch these, because the names
230          * of these variables may change. These are and will never be used
231          * with the currently supported options, so it is safe to leave these
232          * uninitialized.
233          */
234         void *reserved_ptr1;
235         void *reserved_ptr2;
236         void *reserved_ptr3;
237         uint32_t reserved_int1;
238         uint32_t reserved_int2;
239         lzma_vli reserved_int3;
240         lzma_vli reserved_int4;
241         lzma_vli reserved_int5;
242         lzma_vli reserved_int6;
243         lzma_vli reserved_int7;
244         lzma_vli reserved_int8;
245         lzma_reserved_enum reserved_enum1;
246         lzma_reserved_enum reserved_enum2;
247         lzma_reserved_enum reserved_enum3;
248         lzma_reserved_enum reserved_enum4;
249         lzma_bool reserved_bool1;
250         lzma_bool reserved_bool2;
251         lzma_bool reserved_bool3;
252         lzma_bool reserved_bool4;
253         lzma_bool reserved_bool5;
254         lzma_bool reserved_bool6;
255         lzma_bool reserved_bool7;
256         lzma_bool reserved_bool8;
257
258 } lzma_block;
259
260
261 /**
262  * \brief       Decode the Block Header Size field
263  *
264  * To decode Block Header using lzma_block_header_decode(), the size of the
265  * Block Header has to be known and stored into lzma_block.header_size.
266  * The size can be calculated from the first byte of a Block using this macro.
267  * Note that if the first byte is 0x00, it indicates beginning of Index; use
268  * this macro only when the byte is not 0x00.
269  *
270  * There is no encoding macro, because Block Header encoder is enough for that.
271  */
272 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
273
274
275 /**
276  * \brief       Calculate Block Header Size
277  *
278  * Calculate the minimum size needed for the Block Header field using the
279  * settings specified in the lzma_block structure. Note that it is OK to
280  * increase the calculated header_size value as long as it is a multiple of
281  * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
282  * just means that lzma_block_header_encode() will add Header Padding.
283  *
284  * \return      - LZMA_OK: Size calculated successfully and stored to
285  *                block->header_size.
286  *              - LZMA_OPTIONS_ERROR: Unsupported version, filters or
287  *                filter options.
288  *              - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
289  *
290  * \note        This doesn't check that all the options are valid i.e. this
291  *              may return LZMA_OK even if lzma_block_header_encode() or
292  *              lzma_block_encoder() would fail. If you want to validate the
293  *              filter chain, consider using lzma_memlimit_encoder() which as
294  *              a side-effect validates the filter chain.
295  */
296 extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
297                 lzma_nothrow lzma_attr_warn_unused_result;
298
299
300 /**
301  * \brief       Encode Block Header
302  *
303  * The caller must have calculated the size of the Block Header already with
304  * lzma_block_header_size(). If a value larger than the one calculated by
305  * lzma_block_header_size() is used, the Block Header will be padded to the
306  * specified size.
307  *
308  * \param       out         Beginning of the output buffer. This must be
309  *                          at least block->header_size bytes.
310  * \param       block       Block options to be encoded.
311  *
312  * \return      - LZMA_OK: Encoding was successful. block->header_size
313  *                bytes were written to output buffer.
314  *              - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
315  *              - LZMA_PROG_ERROR: Invalid arguments, for example
316  *                block->header_size is invalid or block->filters is NULL.
317  */
318 extern LZMA_API(lzma_ret) lzma_block_header_encode(
319                 const lzma_block *block, uint8_t *out)
320                 lzma_nothrow lzma_attr_warn_unused_result;
321
322
323 /**
324  * \brief       Decode Block Header
325  *
326  * The size of the Block Header must have already been decoded with
327  * lzma_block_header_size_decode() macro and stored to block->header_size.
328  * block->filters must have been allocated, but not necessarily initialized.
329  * Possible existing filter options are _not_ freed.
330  *
331  * \param       block       Destination for block options with header_size
332  *                          properly initialized.
333  * \param       allocator   lzma_allocator for custom allocator functions.
334  *                          Set to NULL to use malloc() (and also free()
335  *                          if an error occurs).
336  * \param       in          Beginning of the input buffer. This must be
337  *                          at least block->header_size bytes.
338  *
339  * \return      - LZMA_OK: Decoding was successful. block->header_size
340  *                bytes were read from the input buffer.
341  *              - LZMA_OPTIONS_ERROR: The Block Header specifies some
342  *                unsupported options such as unsupported filters.
343  *              - LZMA_DATA_ERROR: Block Header is corrupt, for example,
344  *                the CRC32 doesn't match.
345  *              - LZMA_PROG_ERROR: Invalid arguments, for example
346  *                block->header_size is invalid or block->filters is NULL.
347  */
348 extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
349                 lzma_allocator *allocator, const uint8_t *in)
350                 lzma_nothrow lzma_attr_warn_unused_result;
351
352
353 /**
354  * \brief       Validate and set Compressed Size according to Unpadded Size
355  *
356  * Block Header stores Compressed Size, but Index has Unpadded Size. If the
357  * application has already parsed the Index and is now decoding Blocks,
358  * it can calculate Compressed Size from Unpadded Size. This function does
359  * exactly that with error checking:
360  *
361  *  - Compressed Size calculated from Unpadded Size must be positive integer,
362  *    that is, Unpadded Size must be big enough that after Block Header and
363  *    Check fields there's still at least one byte for Compressed Size.
364  *
365  *  - If Compressed Size was present in Block Header, the new value
366  *    calculated from Unpadded Size is compared against the value
367  *    from Block Header.
368  *
369  * \note        This function must be called _after_ decoding the Block Header
370  *              field so that it can properly validate Compressed Size if it
371  *              was present in Block Header.
372  *
373  * \return      - LZMA_OK: block->compressed_size was set successfully.
374  *              - LZMA_DATA_ERROR: unpadded_size is too small compared to
375  *                block->header_size and lzma_check_size(block->check).
376  *              - LZMA_PROG_ERROR: Some values are invalid. For example,
377  *                block->header_size must be a multiple of four and
378  *                between 8 and 1024 inclusive.
379  */
380 extern LZMA_API(lzma_ret) lzma_block_compressed_size(
381                 lzma_block *block, lzma_vli unpadded_size)
382                 lzma_nothrow lzma_attr_warn_unused_result;
383
384
385 /**
386  * \brief       Calculate Unpadded Size
387  *
388  * The Index field stores Unpadded Size and Uncompressed Size. The latter
389  * can be taken directly from the lzma_block structure after coding a Block,
390  * but Unpadded Size needs to be calculated from Block Header Size,
391  * Compressed Size, and size of the Check field. This is where this function
392  * is needed.
393  *
394  * \return      Unpadded Size on success, or zero on error.
395  */
396 extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
397                 lzma_nothrow lzma_attr_pure;
398
399
400 /**
401  * \brief       Calculate the total encoded size of a Block
402  *
403  * This is equivalent to lzma_block_unpadded_size() except that the returned
404  * value includes the size of the Block Padding field.
405  *
406  * \return      On success, total encoded size of the Block. On error,
407  *              zero is returned.
408  */
409 extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
410                 lzma_nothrow lzma_attr_pure;
411
412
413 /**
414  * \brief       Initialize .xz Block encoder
415  *
416  * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
417  * filter chain supports it), and LZMA_FINISH.
418  *
419  * \return      - LZMA_OK: All good, continue with lzma_code().
420  *              - LZMA_MEM_ERROR
421  *              - LZMA_OPTIONS_ERROR
422  *              - LZMA_UNSUPPORTED_CHECK: block->check specfies a Check ID
423  *                that is not supported by this buid of liblzma. Initializing
424  *                the encoder failed.
425  *              - LZMA_PROG_ERROR
426  */
427 extern LZMA_API(lzma_ret) lzma_block_encoder(
428                 lzma_stream *strm, lzma_block *block)
429                 lzma_nothrow lzma_attr_warn_unused_result;
430
431
432 /**
433  * \brief       Initialize .xz Block decoder
434  *
435  * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
436  * LZMA_FINISH is not required. It is supported only for convenience.
437  *
438  * \return      - LZMA_OK: All good, continue with lzma_code().
439  *              - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
440  *                the given Check ID is not supported, thus Check will be
441  *                ignored.
442  *              - LZMA_PROG_ERROR
443  *              - LZMA_MEM_ERROR
444  */
445 extern LZMA_API(lzma_ret) lzma_block_decoder(
446                 lzma_stream *strm, lzma_block *block)
447                 lzma_nothrow lzma_attr_warn_unused_result;
448
449
450 /**
451  * \brief       Calculate maximum output size for single-call Block encoding
452  *
453  * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
454  * See the documentation of lzma_stream_buffer_bound().
455  */
456 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
457                 lzma_nothrow;
458
459
460 /**
461  * \brief       Single-call .xz Block encoder
462  *
463  * In contrast to the multi-call encoder initialized with
464  * lzma_block_encoder(), this function encodes also the Block Header. This
465  * is required to make it possible to write appropriate Block Header also
466  * in case the data isn't compressible, and different filter chain has to be
467  * used to encode the data in uncompressed form using uncompressed chunks
468  * of the LZMA2 filter.
469  *
470  * When the data isn't compressible, header_size, compressed_size, and
471  * uncompressed_size are set just like when the data was compressible, but
472  * it is possible that header_size is too small to hold the filter chain
473  * specified in block->filters, because that isn't necessarily the filter
474  * chain that was actually used to encode the data. lzma_block_unpadded_size()
475  * still works normally, because it doesn't read the filters array.
476  *
477  * \param       block       Block options: block->version, block->check,
478  *                          and block->filters must have been initialized.
479  * \param       allocator   lzma_allocator for custom allocator functions.
480  *                          Set to NULL to use malloc() and free().
481  * \param       in          Beginning of the input buffer
482  * \param       in_size     Size of the input buffer
483  * \param       out         Beginning of the output buffer
484  * \param       out_pos     The next byte will be written to out[*out_pos].
485  *                          *out_pos is updated only if encoding succeeds.
486  * \param       out_size    Size of the out buffer; the first byte into
487  *                          which no data is written to is out[out_size].
488  *
489  * \return      - LZMA_OK: Encoding was successful.
490  *              - LZMA_BUF_ERROR: Not enough output buffer space.
491  *              - LZMA_OPTIONS_ERROR
492  *              - LZMA_MEM_ERROR
493  *              - LZMA_DATA_ERROR
494  *              - LZMA_PROG_ERROR
495  */
496 extern LZMA_API(lzma_ret) lzma_block_buffer_encode(
497                 lzma_block *block, lzma_allocator *allocator,
498                 const uint8_t *in, size_t in_size,
499                 uint8_t *out, size_t *out_pos, size_t out_size)
500                 lzma_nothrow lzma_attr_warn_unused_result;
501
502
503 /**
504  * \brief       Single-call .xz Block decoder
505  *
506  * This is single-call equivalent of lzma_block_decoder(), and requires that
507  * the caller has already decoded Block Header and checked its memory usage.
508  *
509  * \param       block       Block options just like with lzma_block_decoder().
510  * \param       allocator   lzma_allocator for custom allocator functions.
511  *                          Set to NULL to use malloc() and free().
512  * \param       in          Beginning of the input buffer
513  * \param       in_pos      The next byte will be read from in[*in_pos].
514  *                          *in_pos is updated only if decoding succeeds.
515  * \param       in_size     Size of the input buffer; the first byte that
516  *                          won't be read is in[in_size].
517  * \param       out         Beginning of the output buffer
518  * \param       out_pos     The next byte will be written to out[*out_pos].
519  *                          *out_pos is updated only if encoding succeeds.
520  * \param       out_size    Size of the out buffer; the first byte into
521  *                          which no data is written to is out[out_size].
522  *
523  * \return      - LZMA_OK: Decoding was successful.
524  *              - LZMA_OPTIONS_ERROR
525  *              - LZMA_DATA_ERROR
526  *              - LZMA_MEM_ERROR
527  *              - LZMA_BUF_ERROR: Output buffer was too small.
528  *              - LZMA_PROG_ERROR
529  */
530 extern LZMA_API(lzma_ret) lzma_block_buffer_decode(
531                 lzma_block *block, lzma_allocator *allocator,
532                 const uint8_t *in, size_t *in_pos, size_t in_size,
533                 uint8_t *out, size_t *out_pos, size_t out_size)
534                 lzma_nothrow;