3 * \brief .xz Block handling
9 * This file has been put into the public domain.
10 * You can do whatever you want with this file.
12 * See ../lzma.h for information about liblzma as a whole.
15 #ifndef LZMA_H_INTERNAL
16 # error Never include this file directly. Use <lzma.h> instead.
21 * \brief Options for the Block and Block Header encoders and decoders
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().
32 * \brief Block format version
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
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.
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()
58 * - lzma_block_header_decode()
63 * \brief Size of the Block Header field
65 * This is always a multiple of four.
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()
77 * - lzma_block_header_size()
78 * - lzma_block_buffer_encode()
81 # define LZMA_BLOCK_HEADER_SIZE_MIN 8
82 # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
85 * \brief Type of integrity Check
87 * The Check ID is not stored into the Block Header, thus its value
88 * must be provided also when decoding.
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()
104 * \brief Size of the Compressed Data in bytes
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
111 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
112 * verify that the size of the Compressed Data field matches
115 * Usually you don't know this value when encoding in streamed mode,
116 * and thus cannot write this field into the Block Header.
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:
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.
126 * - Call lzma_block_header_size() to see how much space you need to
127 * reserve for the Block Header.
129 * - Encode the Block using lzma_block_encoder() and lzma_code().
130 * It sets compressed_size to the correct value.
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.
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()
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()
156 lzma_vli compressed_size;
159 * \brief Uncompressed Size in bytes
161 * This is handled very similarly to compressed_size above.
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.
168 * - lzma_block_header_size()
169 * - lzma_block_header_encode()
170 * - lzma_block_decoder()
171 * - lzma_block_buffer_decode()
174 * - lzma_block_header_decode()
175 * - lzma_block_encoder()
176 * - lzma_block_decoder()
177 * - lzma_block_buffer_encode()
178 * - lzma_block_buffer_decode()
180 lzma_vli uncompressed_size;
183 * \brief Array of filters
185 * There can be 1-4 filters. The end of the array is marked with
186 * .id = LZMA_VLI_UNKNOWN.
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()
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.
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.
208 lzma_filter *filters;
211 * \brief Raw value stored in the Check field
213 * After successful coding, the first lzma_check_size(check) bytes
214 * of this array contain the raw value stored in the Check field.
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.
220 * - lzma_block_encoder()
221 * - lzma_block_decoder()
222 * - lzma_block_buffer_encode()
223 * - lzma_block_buffer_decode()
225 uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
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
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;
262 * \brief Decode the Block Header Size field
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.
270 * There is no encoding macro, because Block Header encoder is enough for that.
272 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
276 * \brief Calculate Block Header Size
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.
284 * \return - LZMA_OK: Size calculated successfully and stored to
285 * block->header_size.
286 * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
288 * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
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.
296 extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
297 lzma_nothrow lzma_attr_warn_unused_result;
301 * \brief Encode Block Header
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
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.
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.
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;
324 * \brief Decode Block Header
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.
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.
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.
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;
354 * \brief Validate and set Compressed Size according to Unpadded Size
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:
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.
365 * - If Compressed Size was present in Block Header, the new value
366 * calculated from Unpadded Size is compared against the value
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.
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.
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;
386 * \brief Calculate Unpadded Size
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
394 * \return Unpadded Size on success, or zero on error.
396 extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
397 lzma_nothrow lzma_attr_pure;
401 * \brief Calculate the total encoded size of a Block
403 * This is equivalent to lzma_block_unpadded_size() except that the returned
404 * value includes the size of the Block Padding field.
406 * \return On success, total encoded size of the Block. On error,
409 extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
410 lzma_nothrow lzma_attr_pure;
414 * \brief Initialize .xz Block encoder
416 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
417 * filter chain supports it), and LZMA_FINISH.
419 * \return - LZMA_OK: All good, continue with lzma_code().
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.
427 extern LZMA_API(lzma_ret) lzma_block_encoder(
428 lzma_stream *strm, lzma_block *block)
429 lzma_nothrow lzma_attr_warn_unused_result;
433 * \brief Initialize .xz Block decoder
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.
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
445 extern LZMA_API(lzma_ret) lzma_block_decoder(
446 lzma_stream *strm, lzma_block *block)
447 lzma_nothrow lzma_attr_warn_unused_result;
451 * \brief Calculate maximum output size for single-call Block encoding
453 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
454 * See the documentation of lzma_stream_buffer_bound().
456 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
461 * \brief Single-call .xz Block encoder
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.
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.
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].
489 * \return - LZMA_OK: Encoding was successful.
490 * - LZMA_BUF_ERROR: Not enough output buffer space.
491 * - LZMA_OPTIONS_ERROR
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;
504 * \brief Single-call .xz Block decoder
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.
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].
523 * \return - LZMA_OK: Decoding was successful.
524 * - LZMA_OPTIONS_ERROR
527 * - LZMA_BUF_ERROR: Output buffer was too small.
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)