3 * \brief .xz Block handling
4 * \note Never include this file directly. Use <lzma.h> instead.
10 * This file has been put into the public domain.
11 * You can do whatever you want with this file.
14 #ifndef LZMA_H_INTERNAL
15 # error Never include this file directly. Use <lzma.h> instead.
20 * \brief Options for the Block and Block Header encoders and decoders
22 * Different Block handling functions use different parts of this structure.
23 * Some read some members, other functions write, and some do both. Only the
24 * members listed for reading need to be initialized when the specified
25 * functions are called. The members marked for writing will be assigned
26 * new values at some point either by calling the given function or by
27 * later calls to lzma_code().
31 * \brief Block format version
33 * To prevent API and ABI breakages when new features are needed,
34 * a version number is used to indicate which members in this
35 * structure are in use:
36 * - liblzma >= 5.0.0: version = 0 is supported.
37 * - liblzma >= 5.1.4beta: Support for version = 1 was added,
38 * which adds the ignore_check member.
40 * If version is greater than one, most Block related functions
41 * will return LZMA_OPTIONS_ERROR (lzma_block_header_decode() works
42 * with any version value).
45 * - lzma_block_header_size()
46 * - lzma_block_header_encode()
47 * - lzma_block_header_decode()
48 * - lzma_block_compressed_size()
49 * - lzma_block_unpadded_size()
50 * - lzma_block_total_size()
51 * - lzma_block_encoder()
52 * - lzma_block_decoder()
53 * - lzma_block_buffer_encode()
54 * - lzma_block_uncomp_encode()
55 * - lzma_block_buffer_decode()
58 * - lzma_block_header_decode()
63 * \brief Size of the Block Header field in bytes
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()
79 * - lzma_block_uncomp_encode()
82 # define LZMA_BLOCK_HEADER_SIZE_MIN 8
83 # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
86 * \brief Type of integrity Check
88 * The Check ID is not stored into the Block Header, thus its value
89 * must be provided also when decoding.
92 * - lzma_block_header_encode()
93 * - lzma_block_header_decode()
94 * - lzma_block_compressed_size()
95 * - lzma_block_unpadded_size()
96 * - lzma_block_total_size()
97 * - lzma_block_encoder()
98 * - lzma_block_decoder()
99 * - lzma_block_buffer_encode()
100 * - lzma_block_buffer_decode()
105 * \brief Size of the Compressed Data in bytes
107 * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
108 * will store this value to the Block Header. Block encoder doesn't
109 * care about this value, but will set it once the encoding has been
112 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
113 * verify that the size of the Compressed Data field matches
116 * Usually you don't know this value when encoding in streamed mode,
117 * and thus cannot write this field into the Block Header.
119 * In non-streamed mode you can reserve space for this field before
120 * encoding the actual Block. After encoding the data, finish the
121 * Block by encoding the Block Header. Steps in detail:
123 * - Set compressed_size to some big enough value. If you don't know
124 * better, use LZMA_VLI_MAX, but remember that bigger values take
125 * more space in Block Header.
127 * - Call lzma_block_header_size() to see how much space you need to
128 * reserve for the Block Header.
130 * - Encode the Block using lzma_block_encoder() and lzma_code().
131 * It sets compressed_size to the correct value.
133 * - Use lzma_block_header_encode() to encode the Block Header.
134 * Because space was reserved in the first step, you don't need
135 * to call lzma_block_header_size() anymore, because due to
136 * reserving, header_size has to be big enough. If it is "too big",
137 * lzma_block_header_encode() will add enough Header Padding to
138 * make Block Header to match the size specified by header_size.
141 * - lzma_block_header_size()
142 * - lzma_block_header_encode()
143 * - lzma_block_compressed_size()
144 * - lzma_block_unpadded_size()
145 * - lzma_block_total_size()
146 * - lzma_block_decoder()
147 * - lzma_block_buffer_decode()
150 * - lzma_block_header_decode()
151 * - lzma_block_compressed_size()
152 * - lzma_block_encoder()
153 * - lzma_block_decoder()
154 * - lzma_block_buffer_encode()
155 * - lzma_block_uncomp_encode()
156 * - lzma_block_buffer_decode()
158 lzma_vli compressed_size
;
161 * \brief Uncompressed Size in bytes
163 * This is handled very similarly to compressed_size above.
165 * uncompressed_size is needed by fewer functions than
166 * compressed_size. This is because uncompressed_size isn't
167 * needed to validate that Block stays within proper limits.
170 * - lzma_block_header_size()
171 * - lzma_block_header_encode()
172 * - lzma_block_decoder()
173 * - lzma_block_buffer_decode()
176 * - lzma_block_header_decode()
177 * - lzma_block_encoder()
178 * - lzma_block_decoder()
179 * - lzma_block_buffer_encode()
180 * - lzma_block_uncomp_encode()
181 * - lzma_block_buffer_decode()
183 lzma_vli uncompressed_size
;
186 * \brief Array of filters
188 * There can be 1-4 filters. The end of the array is marked with
189 * .id = LZMA_VLI_UNKNOWN.
192 * - lzma_block_header_size()
193 * - lzma_block_header_encode()
194 * - lzma_block_encoder()
195 * - lzma_block_decoder()
196 * - lzma_block_buffer_encode()
197 * - lzma_block_buffer_decode()
200 * - lzma_block_header_decode(): Note that this does NOT free()
201 * the old filter options structures. All unused filters[] will
202 * have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
203 * decoding fails, all filters[] are guaranteed to be
204 * LZMA_VLI_UNKNOWN and NULL.
206 * \note Because of the array is terminated with
207 * .id = LZMA_VLI_UNKNOWN, the actual array must
208 * have LZMA_FILTERS_MAX + 1 members or the Block
209 * Header decoder will overflow the buffer.
211 lzma_filter
*filters
;
214 * \brief Raw value stored in the Check field
216 * After successful coding, the first lzma_check_size(check) bytes
217 * of this array contain the raw value stored in the Check field.
219 * Note that CRC32 and CRC64 are stored in little endian byte order.
220 * Take it into account if you display the Check values to the user.
223 * - lzma_block_encoder()
224 * - lzma_block_decoder()
225 * - lzma_block_buffer_encode()
226 * - lzma_block_uncomp_encode()
227 * - lzma_block_buffer_decode()
229 uint8_t raw_check
[LZMA_CHECK_SIZE_MAX
];
232 * Reserved space to allow possible future extensions without
233 * breaking the ABI. You should not touch these, because the names
234 * of these variables may change. These are and will never be used
235 * with the currently supported options, so it is safe to leave these
239 /** \private Reserved member. */
242 /** \private Reserved member. */
245 /** \private Reserved member. */
248 /** \private Reserved member. */
249 uint32_t reserved_int1
;
251 /** \private Reserved member. */
252 uint32_t reserved_int2
;
254 /** \private Reserved member. */
255 lzma_vli reserved_int3
;
257 /** \private Reserved member. */
258 lzma_vli reserved_int4
;
260 /** \private Reserved member. */
261 lzma_vli reserved_int5
;
263 /** \private Reserved member. */
264 lzma_vli reserved_int6
;
266 /** \private Reserved member. */
267 lzma_vli reserved_int7
;
269 /** \private Reserved member. */
270 lzma_vli reserved_int8
;
272 /** \private Reserved member. */
273 lzma_reserved_enum reserved_enum1
;
275 /** \private Reserved member. */
276 lzma_reserved_enum reserved_enum2
;
278 /** \private Reserved member. */
279 lzma_reserved_enum reserved_enum3
;
281 /** \private Reserved member. */
282 lzma_reserved_enum reserved_enum4
;
285 * \brief A flag to Block decoder to not verify the Check field
287 * This member is supported by liblzma >= 5.1.4beta if .version >= 1.
289 * If this is set to true, the integrity check won't be calculated
290 * and verified. Unless you know what you are doing, you should
291 * leave this to false. (A reason to set this to true is when the
292 * file integrity is verified externally anyway and you want to
293 * speed up the decompression, which matters mostly when using
294 * SHA-256 as the integrity check.)
296 * If .version >= 1, read by:
297 * - lzma_block_decoder()
298 * - lzma_block_buffer_decode()
300 * Written by (.version is ignored):
301 * - lzma_block_header_decode() always sets this to false
303 lzma_bool ignore_check
;
305 /** \private Reserved member. */
306 lzma_bool reserved_bool2
;
308 /** \private Reserved member. */
309 lzma_bool reserved_bool3
;
311 /** \private Reserved member. */
312 lzma_bool reserved_bool4
;
314 /** \private Reserved member. */
315 lzma_bool reserved_bool5
;
317 /** \private Reserved member. */
318 lzma_bool reserved_bool6
;
320 /** \private Reserved member. */
321 lzma_bool reserved_bool7
;
323 /** \private Reserved member. */
324 lzma_bool reserved_bool8
;
330 * \brief Decode the Block Header Size field
332 * To decode Block Header using lzma_block_header_decode(), the size of the
333 * Block Header has to be known and stored into lzma_block.header_size.
334 * The size can be calculated from the first byte of a Block using this macro.
335 * Note that if the first byte is 0x00, it indicates beginning of Index; use
336 * this macro only when the byte is not 0x00.
338 * There is no encoding macro because lzma_block_header_size() and
339 * lzma_block_header_encode() should be used.
341 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
345 * \brief Calculate Block Header Size
347 * Calculate the minimum size needed for the Block Header field using the
348 * settings specified in the lzma_block structure. Note that it is OK to
349 * increase the calculated header_size value as long as it is a multiple of
350 * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
351 * just means that lzma_block_header_encode() will add Header Padding.
353 * \note This doesn't check that all the options are valid i.e. this
354 * may return LZMA_OK even if lzma_block_header_encode() or
355 * lzma_block_encoder() would fail. If you want to validate the
356 * filter chain, consider using lzma_memlimit_encoder() which as
357 * a side-effect validates the filter chain.
359 * \param block Block options
361 * \return Possible lzma_ret values:
362 * - LZMA_OK: Size calculated successfully and stored to
363 * block->header_size.
364 * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
366 * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
368 extern LZMA_API(lzma_ret
) lzma_block_header_size(lzma_block
*block
)
369 lzma_nothrow lzma_attr_warn_unused_result
;
373 * \brief Encode Block Header
375 * The caller must have calculated the size of the Block Header already with
376 * lzma_block_header_size(). If a value larger than the one calculated by
377 * lzma_block_header_size() is used, the Block Header will be padded to the
380 * \param block Block options to be encoded.
381 * \param[out] out Beginning of the output buffer. This must be
382 * at least block->header_size bytes.
384 * \return Possible lzma_ret values:
385 * - LZMA_OK: Encoding was successful. block->header_size
386 * bytes were written to output buffer.
387 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
388 * - LZMA_PROG_ERROR: Invalid arguments, for example
389 * block->header_size is invalid or block->filters is NULL.
391 extern LZMA_API(lzma_ret
) lzma_block_header_encode(
392 const lzma_block
*block
, uint8_t *out
)
393 lzma_nothrow lzma_attr_warn_unused_result
;
397 * \brief Decode Block Header
399 * block->version should (usually) be set to the highest value supported
400 * by the application. If the application sets block->version to a value
401 * higher than supported by the current liblzma version, this function will
402 * downgrade block->version to the highest value supported by it. Thus one
403 * should check the value of block->version after calling this function if
404 * block->version was set to a non-zero value and the application doesn't
405 * otherwise know that the liblzma version being used is new enough to
406 * support the specified block->version.
408 * The size of the Block Header must have already been decoded with
409 * lzma_block_header_size_decode() macro and stored to block->header_size.
411 * The integrity check type from Stream Header must have been stored
414 * block->filters must have been allocated, but they don't need to be
415 * initialized (possible existing filter options are not freed).
417 * \param[out] block Destination for Block options
418 * \param allocator lzma_allocator for custom allocator functions.
419 * Set to NULL to use malloc() (and also free()
420 * if an error occurs).
421 * \param in Beginning of the input buffer. This must be
422 * at least block->header_size bytes.
424 * \return Possible lzma_ret values:
425 * - LZMA_OK: Decoding was successful. block->header_size
426 * bytes were read from the input buffer.
427 * - LZMA_OPTIONS_ERROR: The Block Header specifies some
428 * unsupported options such as unsupported filters. This can
429 * happen also if block->version was set to a too low value
430 * compared to what would be required to properly represent
431 * the information stored in the Block Header.
432 * - LZMA_DATA_ERROR: Block Header is corrupt, for example,
433 * the CRC32 doesn't match.
434 * - LZMA_PROG_ERROR: Invalid arguments, for example
435 * block->header_size is invalid or block->filters is NULL.
437 extern LZMA_API(lzma_ret
) lzma_block_header_decode(lzma_block
*block
,
438 const lzma_allocator
*allocator
, const uint8_t *in
)
439 lzma_nothrow lzma_attr_warn_unused_result
;
443 * \brief Validate and set Compressed Size according to Unpadded Size
445 * Block Header stores Compressed Size, but Index has Unpadded Size. If the
446 * application has already parsed the Index and is now decoding Blocks,
447 * it can calculate Compressed Size from Unpadded Size. This function does
448 * exactly that with error checking:
450 * - Compressed Size calculated from Unpadded Size must be positive integer,
451 * that is, Unpadded Size must be big enough that after Block Header and
452 * Check fields there's still at least one byte for Compressed Size.
454 * - If Compressed Size was present in Block Header, the new value
455 * calculated from Unpadded Size is compared against the value
458 * \note This function must be called _after_ decoding the Block Header
459 * field so that it can properly validate Compressed Size if it
460 * was present in Block Header.
462 * \param block Block options: block->header_size must
463 * already be set with lzma_block_header_size().
464 * \param unpadded_size Unpadded Size from the Index field in bytes
466 * \return Possible lzma_ret values:
467 * - LZMA_OK: block->compressed_size was set successfully.
468 * - LZMA_DATA_ERROR: unpadded_size is too small compared to
469 * block->header_size and lzma_check_size(block->check).
470 * - LZMA_PROG_ERROR: Some values are invalid. For example,
471 * block->header_size must be a multiple of four and
472 * between 8 and 1024 inclusive.
474 extern LZMA_API(lzma_ret
) lzma_block_compressed_size(
475 lzma_block
*block
, lzma_vli unpadded_size
)
476 lzma_nothrow lzma_attr_warn_unused_result
;
480 * \brief Calculate Unpadded Size
482 * The Index field stores Unpadded Size and Uncompressed Size. The latter
483 * can be taken directly from the lzma_block structure after coding a Block,
484 * but Unpadded Size needs to be calculated from Block Header Size,
485 * Compressed Size, and size of the Check field. This is where this function
488 * \param block Block options: block->header_size must already be
489 * set with lzma_block_header_size().
491 * \return Unpadded Size on success, or zero on error.
493 extern LZMA_API(lzma_vli
) lzma_block_unpadded_size(const lzma_block
*block
)
494 lzma_nothrow lzma_attr_pure
;
498 * \brief Calculate the total encoded size of a Block
500 * This is equivalent to lzma_block_unpadded_size() except that the returned
501 * value includes the size of the Block Padding field.
503 * \param block Block options: block->header_size must already be
504 * set with lzma_block_header_size().
506 * \return On success, total encoded size of the Block. On error,
509 extern LZMA_API(lzma_vli
) lzma_block_total_size(const lzma_block
*block
)
510 lzma_nothrow lzma_attr_pure
;
514 * \brief Initialize .xz Block encoder
516 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
517 * filter chain supports it), and LZMA_FINISH.
519 * The Block encoder encodes the Block Data, Block Padding, and Check value.
520 * It does NOT encode the Block Header which can be encoded with
521 * lzma_block_header_encode().
523 * \param strm Pointer to lzma_stream that is at least initialized
524 * with LZMA_STREAM_INIT.
525 * \param block Block options: block->version, block->check,
526 * and block->filters must have been initialized.
528 * \return Possible lzma_ret values:
529 * - LZMA_OK: All good, continue with lzma_code().
531 * - LZMA_OPTIONS_ERROR
532 * - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
533 * that is not supported by this build of liblzma. Initializing
534 * the encoder failed.
537 extern LZMA_API(lzma_ret
) lzma_block_encoder(
538 lzma_stream
*strm
, lzma_block
*block
)
539 lzma_nothrow lzma_attr_warn_unused_result
;
543 * \brief Initialize .xz Block decoder
545 * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
546 * LZMA_FINISH is not required. It is supported only for convenience.
548 * The Block decoder decodes the Block Data, Block Padding, and Check value.
549 * It does NOT decode the Block Header which can be decoded with
550 * lzma_block_header_decode().
552 * \param strm Pointer to lzma_stream that is at least initialized
553 * with LZMA_STREAM_INIT.
554 * \param block Block options
556 * \return Possible lzma_ret values:
557 * - LZMA_OK: All good, continue with lzma_code().
561 extern LZMA_API(lzma_ret
) lzma_block_decoder(
562 lzma_stream
*strm
, lzma_block
*block
)
563 lzma_nothrow lzma_attr_warn_unused_result
;
567 * \brief Calculate maximum output size for single-call Block encoding
569 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
570 * See the documentation of lzma_stream_buffer_bound().
572 * \param uncompressed_size Size of the data to be encoded with the
573 * single-call Block encoder.
575 * \return Maximum output size in bytes for single-call Block encoding.
577 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size
)
582 * \brief Single-call .xz Block encoder
584 * In contrast to the multi-call encoder initialized with
585 * lzma_block_encoder(), this function encodes also the Block Header. This
586 * is required to make it possible to write appropriate Block Header also
587 * in case the data isn't compressible, and different filter chain has to be
588 * used to encode the data in uncompressed form using uncompressed chunks
589 * of the LZMA2 filter.
591 * When the data isn't compressible, header_size, compressed_size, and
592 * uncompressed_size are set just like when the data was compressible, but
593 * it is possible that header_size is too small to hold the filter chain
594 * specified in block->filters, because that isn't necessarily the filter
595 * chain that was actually used to encode the data. lzma_block_unpadded_size()
596 * still works normally, because it doesn't read the filters array.
598 * \param block Block options: block->version, block->check,
599 * and block->filters must have been initialized.
600 * \param allocator lzma_allocator for custom allocator functions.
601 * Set to NULL to use malloc() and free().
602 * \param in Beginning of the input buffer
603 * \param in_size Size of the input buffer
604 * \param[out] out Beginning of the output buffer
605 * \param[out] out_pos The next byte will be written to out[*out_pos].
606 * *out_pos is updated only if encoding succeeds.
607 * \param out_size Size of the out buffer; the first byte into
608 * which no data is written to is out[out_size].
610 * \return Possible lzma_ret values:
611 * - LZMA_OK: Encoding was successful.
612 * - LZMA_BUF_ERROR: Not enough output buffer space.
613 * - LZMA_UNSUPPORTED_CHECK
614 * - LZMA_OPTIONS_ERROR
619 extern LZMA_API(lzma_ret
) lzma_block_buffer_encode(
620 lzma_block
*block
, const lzma_allocator
*allocator
,
621 const uint8_t *in
, size_t in_size
,
622 uint8_t *out
, size_t *out_pos
, size_t out_size
)
623 lzma_nothrow lzma_attr_warn_unused_result
;
627 * \brief Single-call uncompressed .xz Block encoder
629 * This is like lzma_block_buffer_encode() except this doesn't try to
630 * compress the data and instead encodes the data using LZMA2 uncompressed
631 * chunks. The required output buffer size can be determined with
632 * lzma_block_buffer_bound().
634 * Since the data won't be compressed, this function ignores block->filters.
635 * This function doesn't take lzma_allocator because this function doesn't
636 * allocate any memory from the heap.
638 * \param block Block options: block->version, block->check,
639 * and block->filters must have been initialized.
640 * \param in Beginning of the input buffer
641 * \param in_size Size of the input buffer
642 * \param[out] out Beginning of the output buffer
643 * \param[out] out_pos The next byte will be written to out[*out_pos].
644 * *out_pos is updated only if encoding succeeds.
645 * \param out_size Size of the out buffer; the first byte into
646 * which no data is written to is out[out_size].
648 * \return Possible lzma_ret values:
649 * - LZMA_OK: Encoding was successful.
650 * - LZMA_BUF_ERROR: Not enough output buffer space.
651 * - LZMA_UNSUPPORTED_CHECK
652 * - LZMA_OPTIONS_ERROR
657 extern LZMA_API(lzma_ret
) lzma_block_uncomp_encode(lzma_block
*block
,
658 const uint8_t *in
, size_t in_size
,
659 uint8_t *out
, size_t *out_pos
, size_t out_size
)
660 lzma_nothrow lzma_attr_warn_unused_result
;
664 * \brief Single-call .xz Block decoder
666 * This is single-call equivalent of lzma_block_decoder(), and requires that
667 * the caller has already decoded Block Header and checked its memory usage.
669 * \param block Block options
670 * \param allocator lzma_allocator for custom allocator functions.
671 * Set to NULL to use malloc() and free().
672 * \param in Beginning of the input buffer
673 * \param in_pos The next byte will be read from in[*in_pos].
674 * *in_pos is updated only if decoding succeeds.
675 * \param in_size Size of the input buffer; the first byte that
676 * won't be read is in[in_size].
677 * \param[out] out Beginning of the output buffer
678 * \param[out] out_pos The next byte will be written to out[*out_pos].
679 * *out_pos is updated only if encoding succeeds.
680 * \param out_size Size of the out buffer; the first byte into
681 * which no data is written to is out[out_size].
683 * \return Possible lzma_ret values:
684 * - LZMA_OK: Decoding was successful.
685 * - LZMA_OPTIONS_ERROR
688 * - LZMA_BUF_ERROR: Output buffer was too small.
691 extern LZMA_API(lzma_ret
) lzma_block_buffer_decode(
692 lzma_block
*block
, const lzma_allocator
*allocator
,
693 const uint8_t *in
, size_t *in_pos
, size_t in_size
,
694 uint8_t *out
, size_t *out_pos
, size_t out_size
)