xref: /dragonfly/contrib/xz/src/liblzma/api/lzma/block.h (revision 684cb317)
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 	 * the Block field, a version number is used to indicate which
36 	 * fields in this structure are in use. For now, version must always
37 	 * be zero. With non-zero version, most Block related functions will
38 	 * return LZMA_OPTIONS_ERROR.
39 	 *
40 	 * Read by:
41 	 *  - All functions that take pointer to lzma_block as argument,
42 	 *    including lzma_block_header_decode().
43 	 *
44 	 * Written by:
45 	 *  - lzma_block_header_decode()
46 	 */
47 	uint32_t version;
48 
49 	/**
50 	 * \brief       Size of the Block Header field
51 	 *
52 	 * This is always a multiple of four.
53 	 *
54 	 * Read by:
55 	 *  - lzma_block_header_encode()
56 	 *  - lzma_block_header_decode()
57 	 *  - lzma_block_compressed_size()
58 	 *  - lzma_block_unpadded_size()
59 	 *  - lzma_block_total_size()
60 	 *  - lzma_block_decoder()
61 	 *  - lzma_block_buffer_decode()
62 	 *
63 	 * Written by:
64 	 *  - lzma_block_header_size()
65 	 *  - lzma_block_buffer_encode()
66 	 */
67 	uint32_t header_size;
68 #	define LZMA_BLOCK_HEADER_SIZE_MIN 8
69 #	define LZMA_BLOCK_HEADER_SIZE_MAX 1024
70 
71 	/**
72 	 * \brief       Type of integrity Check
73 	 *
74 	 * The Check ID is not stored into the Block Header, thus its value
75 	 * must be provided also when decoding.
76 	 *
77 	 * Read by:
78 	 *  - lzma_block_header_encode()
79 	 *  - lzma_block_header_decode()
80 	 *  - lzma_block_compressed_size()
81 	 *  - lzma_block_unpadded_size()
82 	 *  - lzma_block_total_size()
83 	 *  - lzma_block_encoder()
84 	 *  - lzma_block_decoder()
85 	 *  - lzma_block_buffer_encode()
86 	 *  - lzma_block_buffer_decode()
87 	 */
88 	lzma_check check;
89 
90 	/**
91 	 * \brief       Size of the Compressed Data in bytes
92 	 *
93 	 * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
94 	 * will store this value to the Block Header. Block encoder doesn't
95 	 * care about this value, but will set it once the encoding has been
96 	 * finished.
97 	 *
98 	 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
99 	 * verify that the size of the Compressed Data field matches
100 	 * compressed_size.
101 	 *
102 	 * Usually you don't know this value when encoding in streamed mode,
103 	 * and thus cannot write this field into the Block Header.
104 	 *
105 	 * In non-streamed mode you can reserve space for this field before
106 	 * encoding the actual Block. After encoding the data, finish the
107 	 * Block by encoding the Block Header. Steps in detail:
108 	 *
109 	 *  - Set compressed_size to some big enough value. If you don't know
110 	 *    better, use LZMA_VLI_MAX, but remember that bigger values take
111 	 *    more space in Block Header.
112 	 *
113 	 *  - Call lzma_block_header_size() to see how much space you need to
114 	 *    reserve for the Block Header.
115 	 *
116 	 *  - Encode the Block using lzma_block_encoder() and lzma_code().
117 	 *    It sets compressed_size to the correct value.
118 	 *
119 	 *  - Use lzma_block_header_encode() to encode the Block Header.
120 	 *    Because space was reserved in the first step, you don't need
121 	 *    to call lzma_block_header_size() anymore, because due to
122 	 *    reserving, header_size has to be big enough. If it is "too big",
123 	 *    lzma_block_header_encode() will add enough Header Padding to
124 	 *    make Block Header to match the size specified by header_size.
125 	 *
126 	 * Read by:
127 	 *  - lzma_block_header_size()
128 	 *  - lzma_block_header_encode()
129 	 *  - lzma_block_compressed_size()
130 	 *  - lzma_block_unpadded_size()
131 	 *  - lzma_block_total_size()
132 	 *  - lzma_block_decoder()
133 	 *  - lzma_block_buffer_decode()
134 	 *
135 	 * Written by:
136 	 *  - lzma_block_header_decode()
137 	 *  - lzma_block_compressed_size()
138 	 *  - lzma_block_encoder()
139 	 *  - lzma_block_decoder()
140 	 *  - lzma_block_buffer_encode()
141 	 *  - lzma_block_buffer_decode()
142 	 */
143 	lzma_vli compressed_size;
144 
145 	/**
146 	 * \brief       Uncompressed Size in bytes
147 	 *
148 	 * This is handled very similarly to compressed_size above.
149 	 *
150 	 * uncompressed_size is needed by fewer functions than
151 	 * compressed_size. This is because uncompressed_size isn't
152 	 * needed to validate that Block stays within proper limits.
153 	 *
154 	 * Read by:
155 	 *  - lzma_block_header_size()
156 	 *  - lzma_block_header_encode()
157 	 *  - lzma_block_decoder()
158 	 *  - lzma_block_buffer_decode()
159 	 *
160 	 * Written by:
161 	 *  - lzma_block_header_decode()
162 	 *  - lzma_block_encoder()
163 	 *  - lzma_block_decoder()
164 	 *  - lzma_block_buffer_encode()
165 	 *  - lzma_block_buffer_decode()
166 	 */
167 	lzma_vli uncompressed_size;
168 
169 	/**
170 	 * \brief       Array of filters
171 	 *
172 	 * There can be 1-4 filters. The end of the array is marked with
173 	 * .id = LZMA_VLI_UNKNOWN.
174 	 *
175 	 * Read by:
176 	 *  - lzma_block_header_size()
177 	 *  - lzma_block_header_encode()
178 	 *  - lzma_block_encoder()
179 	 *  - lzma_block_decoder()
180 	 *  - lzma_block_buffer_encode()
181 	 *  - lzma_block_buffer_decode()
182 	 *
183 	 * Written by:
184 	 *  - lzma_block_header_decode(): Note that this does NOT free()
185 	 *    the old filter options structures. All unused filters[] will
186 	 *    have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
187 	 *    decoding fails, all filters[] are guaranteed to be
188 	 *    LZMA_VLI_UNKNOWN and NULL.
189 	 *
190 	 * \note        Because of the array is terminated with
191 	 *              .id = LZMA_VLI_UNKNOWN, the actual array must
192 	 *              have LZMA_FILTERS_MAX + 1 members or the Block
193 	 *              Header decoder will overflow the buffer.
194 	 */
195 	lzma_filter *filters;
196 
197 	/**
198 	 * \brief       Raw value stored in the Check field
199 	 *
200 	 * After successful coding, the first lzma_check_size(check) bytes
201 	 * of this array contain the raw value stored in the Check field.
202 	 *
203 	 * Note that CRC32 and CRC64 are stored in little endian byte order.
204 	 * Take it into account if you display the Check values to the user.
205 	 *
206 	 * Written by:
207 	 *  - lzma_block_encoder()
208 	 *  - lzma_block_decoder()
209 	 *  - lzma_block_buffer_encode()
210 	 *  - lzma_block_buffer_decode()
211 	 */
212 	uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
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_nothrow 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 a value larger 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_nothrow lzma_attr_warn_unused_result;
308 
309 
310 /**
311  * \brief       Decode Block Header
312  *
313  * block->version should be set to the highest value supported by the
314  * application; currently the only possible version is zero. This function
315  * will set version to the lowest value that still supports all the features
316  * required by the Block Header.
317  *
318  * The size of the Block Header must have already been decoded with
319  * lzma_block_header_size_decode() macro and stored to block->header_size.
320  *
321  * The integrity check type from Stream Header must have been stored
322  * to block->check.
323  *
324  * block->filters must have been allocated, but they don't need to be
325  * initialized (possible existing filter options are not freed).
326  *
327  * \param       block       Destination for Block options.
328  * \param       allocator   lzma_allocator for custom allocator functions.
329  *                          Set to NULL to use malloc() (and also free()
330  *                          if an error occurs).
331  * \param       in          Beginning of the input buffer. This must be
332  *                          at least block->header_size bytes.
333  *
334  * \return      - LZMA_OK: Decoding was successful. block->header_size
335  *                bytes were read from the input buffer.
336  *              - LZMA_OPTIONS_ERROR: The Block Header specifies some
337  *                unsupported options such as unsupported filters. This can
338  *                happen also if block->version was set to a too low value
339  *                compared to what would be required to properly represent
340  *                the information stored in the Block Header.
341  *              - LZMA_DATA_ERROR: Block Header is corrupt, for example,
342  *                the CRC32 doesn't match.
343  *              - LZMA_PROG_ERROR: Invalid arguments, for example
344  *                block->header_size is invalid or block->filters is NULL.
345  */
346 extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
347 		lzma_allocator *allocator, const uint8_t *in)
348 		lzma_nothrow lzma_attr_warn_unused_result;
349 
350 
351 /**
352  * \brief       Validate and set Compressed Size according to Unpadded Size
353  *
354  * Block Header stores Compressed Size, but Index has Unpadded Size. If the
355  * application has already parsed the Index and is now decoding Blocks,
356  * it can calculate Compressed Size from Unpadded Size. This function does
357  * exactly that with error checking:
358  *
359  *  - Compressed Size calculated from Unpadded Size must be positive integer,
360  *    that is, Unpadded Size must be big enough that after Block Header and
361  *    Check fields there's still at least one byte for Compressed Size.
362  *
363  *  - If Compressed Size was present in Block Header, the new value
364  *    calculated from Unpadded Size is compared against the value
365  *    from Block Header.
366  *
367  * \note        This function must be called _after_ decoding the Block Header
368  *              field so that it can properly validate Compressed Size if it
369  *              was present in Block Header.
370  *
371  * \return      - LZMA_OK: block->compressed_size was set successfully.
372  *              - LZMA_DATA_ERROR: unpadded_size is too small compared to
373  *                block->header_size and lzma_check_size(block->check).
374  *              - LZMA_PROG_ERROR: Some values are invalid. For example,
375  *                block->header_size must be a multiple of four and
376  *                between 8 and 1024 inclusive.
377  */
378 extern LZMA_API(lzma_ret) lzma_block_compressed_size(
379 		lzma_block *block, lzma_vli unpadded_size)
380 		lzma_nothrow lzma_attr_warn_unused_result;
381 
382 
383 /**
384  * \brief       Calculate Unpadded Size
385  *
386  * The Index field stores Unpadded Size and Uncompressed Size. The latter
387  * can be taken directly from the lzma_block structure after coding a Block,
388  * but Unpadded Size needs to be calculated from Block Header Size,
389  * Compressed Size, and size of the Check field. This is where this function
390  * is needed.
391  *
392  * \return      Unpadded Size on success, or zero on error.
393  */
394 extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
395 		lzma_nothrow lzma_attr_pure;
396 
397 
398 /**
399  * \brief       Calculate the total encoded size of a Block
400  *
401  * This is equivalent to lzma_block_unpadded_size() except that the returned
402  * value includes the size of the Block Padding field.
403  *
404  * \return      On success, total encoded size of the Block. On error,
405  *              zero is returned.
406  */
407 extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
408 		lzma_nothrow lzma_attr_pure;
409 
410 
411 /**
412  * \brief       Initialize .xz Block encoder
413  *
414  * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
415  * filter chain supports it), and LZMA_FINISH.
416  *
417  * \return      - LZMA_OK: All good, continue with lzma_code().
418  *              - LZMA_MEM_ERROR
419  *              - LZMA_OPTIONS_ERROR
420  *              - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
421  *                that is not supported by this buid of liblzma. Initializing
422  *                the encoder failed.
423  *              - LZMA_PROG_ERROR
424  */
425 extern LZMA_API(lzma_ret) lzma_block_encoder(
426 		lzma_stream *strm, lzma_block *block)
427 		lzma_nothrow lzma_attr_warn_unused_result;
428 
429 
430 /**
431  * \brief       Initialize .xz Block decoder
432  *
433  * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
434  * LZMA_FINISH is not required. It is supported only for convenience.
435  *
436  * \return      - LZMA_OK: All good, continue with lzma_code().
437  *              - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
438  *                the given Check ID is not supported, thus Check will be
439  *                ignored.
440  *              - LZMA_PROG_ERROR
441  *              - LZMA_MEM_ERROR
442  */
443 extern LZMA_API(lzma_ret) lzma_block_decoder(
444 		lzma_stream *strm, lzma_block *block)
445 		lzma_nothrow lzma_attr_warn_unused_result;
446 
447 
448 /**
449  * \brief       Calculate maximum output size for single-call Block encoding
450  *
451  * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
452  * See the documentation of lzma_stream_buffer_bound().
453  */
454 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
455 		lzma_nothrow;
456 
457 
458 /**
459  * \brief       Single-call .xz Block encoder
460  *
461  * In contrast to the multi-call encoder initialized with
462  * lzma_block_encoder(), this function encodes also the Block Header. This
463  * is required to make it possible to write appropriate Block Header also
464  * in case the data isn't compressible, and different filter chain has to be
465  * used to encode the data in uncompressed form using uncompressed chunks
466  * of the LZMA2 filter.
467  *
468  * When the data isn't compressible, header_size, compressed_size, and
469  * uncompressed_size are set just like when the data was compressible, but
470  * it is possible that header_size is too small to hold the filter chain
471  * specified in block->filters, because that isn't necessarily the filter
472  * chain that was actually used to encode the data. lzma_block_unpadded_size()
473  * still works normally, because it doesn't read the filters array.
474  *
475  * \param       block       Block options: block->version, block->check,
476  *                          and block->filters must have been initialized.
477  * \param       allocator   lzma_allocator for custom allocator functions.
478  *                          Set to NULL to use malloc() and free().
479  * \param       in          Beginning of the input buffer
480  * \param       in_size     Size of the input buffer
481  * \param       out         Beginning of the output buffer
482  * \param       out_pos     The next byte will be written to out[*out_pos].
483  *                          *out_pos is updated only if encoding succeeds.
484  * \param       out_size    Size of the out buffer; the first byte into
485  *                          which no data is written to is out[out_size].
486  *
487  * \return      - LZMA_OK: Encoding was successful.
488  *              - LZMA_BUF_ERROR: Not enough output buffer space.
489  *              - LZMA_UNSUPPORTED_CHECK
490  *              - LZMA_OPTIONS_ERROR
491  *              - LZMA_MEM_ERROR
492  *              - LZMA_DATA_ERROR
493  *              - LZMA_PROG_ERROR
494  */
495 extern LZMA_API(lzma_ret) lzma_block_buffer_encode(
496 		lzma_block *block, lzma_allocator *allocator,
497 		const uint8_t *in, size_t in_size,
498 		uint8_t *out, size_t *out_pos, size_t out_size)
499 		lzma_nothrow lzma_attr_warn_unused_result;
500 
501 
502 /**
503  * \brief       Single-call .xz Block decoder
504  *
505  * This is single-call equivalent of lzma_block_decoder(), and requires that
506  * the caller has already decoded Block Header and checked its memory usage.
507  *
508  * \param       block       Block options just like with lzma_block_decoder().
509  * \param       allocator   lzma_allocator for custom allocator functions.
510  *                          Set to NULL to use malloc() and free().
511  * \param       in          Beginning of the input buffer
512  * \param       in_pos      The next byte will be read from in[*in_pos].
513  *                          *in_pos is updated only if decoding succeeds.
514  * \param       in_size     Size of the input buffer; the first byte that
515  *                          won't be read is in[in_size].
516  * \param       out         Beginning of the output buffer
517  * \param       out_pos     The next byte will be written to out[*out_pos].
518  *                          *out_pos is updated only if encoding succeeds.
519  * \param       out_size    Size of the out buffer; the first byte into
520  *                          which no data is written to is out[out_size].
521  *
522  * \return      - LZMA_OK: Decoding was successful.
523  *              - LZMA_OPTIONS_ERROR
524  *              - LZMA_DATA_ERROR
525  *              - LZMA_MEM_ERROR
526  *              - LZMA_BUF_ERROR: Output buffer was too small.
527  *              - LZMA_PROG_ERROR
528  */
529 extern LZMA_API(lzma_ret) lzma_block_buffer_decode(
530 		lzma_block *block, lzma_allocator *allocator,
531 		const uint8_t *in, size_t *in_pos, size_t in_size,
532 		uint8_t *out, size_t *out_pos, size_t out_size)
533 		lzma_nothrow;
534