3 * \brief .xz Block handling
4 * \note Never include this file directly. Use <lzma.h> instead.
6 * See ../lzma.h for information about liblzma as a whole.
10 * Author: Lasse Collin
12 * This file has been put into the public domain.
13 * You can do whatever you want with this file.
16 #ifndef LZMA_H_INTERNAL
17 # error Never include this file directly. Use <lzma.h> instead.
22 * \brief Options for the Block and Block Header encoders and decoders
24 * Different Block handling functions use different parts of this structure.
25 * Some read some members, other functions write, and some do both. Only the
26 * members listed for reading need to be initialized when the specified
27 * functions are called. The members marked for writing will be assigned
28 * new values at some point either by calling the given function or by
29 * later calls to lzma_code().
33 * \brief Block format version
35 * To prevent API and ABI breakages when new features are needed,
36 * a version number is used to indicate which fields in this
37 * structure are in use:
38 * - liblzma >= 5.0.0: version = 0 is supported.
39 * - liblzma >= 5.1.4beta: Support for version = 1 was added,
40 * which adds the ignore_check field.
42 * If version is greater than one, most Block related functions
43 * will return LZMA_OPTIONS_ERROR (lzma_block_header_decode() works
44 * with any version value).
47 * - lzma_block_header_size()
48 * - lzma_block_header_encode()
49 * - lzma_block_header_decode()
50 * - lzma_block_compressed_size()
51 * - lzma_block_unpadded_size()
52 * - lzma_block_total_size()
53 * - lzma_block_encoder()
54 * - lzma_block_decoder()
55 * - lzma_block_buffer_encode()
56 * - lzma_block_uncomp_encode()
57 * - lzma_block_buffer_decode()
60 * - lzma_block_header_decode()
65 * \brief Size of the Block Header field in bytes
67 * This is always a multiple of four.
70 * - lzma_block_header_encode()
71 * - lzma_block_header_decode()
72 * - lzma_block_compressed_size()
73 * - lzma_block_unpadded_size()
74 * - lzma_block_total_size()
75 * - lzma_block_decoder()
76 * - lzma_block_buffer_decode()
79 * - lzma_block_header_size()
80 * - lzma_block_buffer_encode()
81 * - lzma_block_uncomp_encode()
84 # define LZMA_BLOCK_HEADER_SIZE_MIN 8
85 # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
88 * \brief Type of integrity Check
90 * The Check ID is not stored into the Block Header, thus its value
91 * must be provided also when decoding.
94 * - lzma_block_header_encode()
95 * - lzma_block_header_decode()
96 * - lzma_block_compressed_size()
97 * - lzma_block_unpadded_size()
98 * - lzma_block_total_size()
99 * - lzma_block_encoder()
100 * - lzma_block_decoder()
101 * - lzma_block_buffer_encode()
102 * - lzma_block_buffer_decode()
107 * \brief Size of the Compressed Data in bytes
109 * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
110 * will store this value to the Block Header. Block encoder doesn't
111 * care about this value, but will set it once the encoding has been
114 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
115 * verify that the size of the Compressed Data field matches
118 * Usually you don't know this value when encoding in streamed mode,
119 * and thus cannot write this field into the Block Header.
121 * In non-streamed mode you can reserve space for this field before
122 * encoding the actual Block. After encoding the data, finish the
123 * Block by encoding the Block Header. Steps in detail:
125 * - Set compressed_size to some big enough value. If you don't know
126 * better, use LZMA_VLI_MAX, but remember that bigger values take
127 * more space in Block Header.
129 * - Call lzma_block_header_size() to see how much space you need to
130 * reserve for the Block Header.
132 * - Encode the Block using lzma_block_encoder() and lzma_code().
133 * It sets compressed_size to the correct value.
135 * - Use lzma_block_header_encode() to encode the Block Header.
136 * Because space was reserved in the first step, you don't need
137 * to call lzma_block_header_size() anymore, because due to
138 * reserving, header_size has to be big enough. If it is "too big",
139 * lzma_block_header_encode() will add enough Header Padding to
140 * make Block Header to match the size specified by header_size.
143 * - lzma_block_header_size()
144 * - lzma_block_header_encode()
145 * - lzma_block_compressed_size()
146 * - lzma_block_unpadded_size()
147 * - lzma_block_total_size()
148 * - lzma_block_decoder()
149 * - lzma_block_buffer_decode()
152 * - lzma_block_header_decode()
153 * - lzma_block_compressed_size()
154 * - lzma_block_encoder()
155 * - lzma_block_decoder()
156 * - lzma_block_buffer_encode()
157 * - lzma_block_uncomp_encode()
158 * - lzma_block_buffer_decode()
160 lzma_vli compressed_size
;
163 * \brief Uncompressed Size in bytes
165 * This is handled very similarly to compressed_size above.
167 * uncompressed_size is needed by fewer functions than
168 * compressed_size. This is because uncompressed_size isn't
169 * needed to validate that Block stays within proper limits.
172 * - lzma_block_header_size()
173 * - lzma_block_header_encode()
174 * - lzma_block_decoder()
175 * - lzma_block_buffer_decode()
178 * - lzma_block_header_decode()
179 * - lzma_block_encoder()
180 * - lzma_block_decoder()
181 * - lzma_block_buffer_encode()
182 * - lzma_block_uncomp_encode()
183 * - lzma_block_buffer_decode()
185 lzma_vli uncompressed_size
;
188 * \brief Array of filters
190 * There can be 1-4 filters. The end of the array is marked with
191 * .id = LZMA_VLI_UNKNOWN.
194 * - lzma_block_header_size()
195 * - lzma_block_header_encode()
196 * - lzma_block_encoder()
197 * - lzma_block_decoder()
198 * - lzma_block_buffer_encode()
199 * - lzma_block_buffer_decode()
202 * - lzma_block_header_decode(): Note that this does NOT free()
203 * the old filter options structures. All unused filters[] will
204 * have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
205 * decoding fails, all filters[] are guaranteed to be
206 * LZMA_VLI_UNKNOWN and NULL.
208 * \note Because of the array is terminated with
209 * .id = LZMA_VLI_UNKNOWN, the actual array must
210 * have LZMA_FILTERS_MAX + 1 members or the Block
211 * Header decoder will overflow the buffer.
213 lzma_filter
*filters
;
216 * \brief Raw value stored in the Check field
218 * After successful coding, the first lzma_check_size(check) bytes
219 * of this array contain the raw value stored in the Check field.
221 * Note that CRC32 and CRC64 are stored in little endian byte order.
222 * Take it into account if you display the Check values to the user.
225 * - lzma_block_encoder()
226 * - lzma_block_decoder()
227 * - lzma_block_buffer_encode()
228 * - lzma_block_uncomp_encode()
229 * - lzma_block_buffer_decode()
231 uint8_t raw_check
[LZMA_CHECK_SIZE_MAX
];
234 * Reserved space to allow possible future extensions without
235 * breaking the ABI. You should not touch these, because the names
236 * of these variables may change. These are and will never be used
237 * with the currently supported options, so it is safe to leave these
241 /** \private Reserved field. */
244 /** \private Reserved field. */
247 /** \private Reserved field. */
250 /** \private Reserved field. */
251 uint32_t reserved_int1
;
253 /** \private Reserved field. */
254 uint32_t reserved_int2
;
256 /** \private Reserved field. */
257 lzma_vli reserved_int3
;
259 /** \private Reserved field. */
260 lzma_vli reserved_int4
;
262 /** \private Reserved field. */
263 lzma_vli reserved_int5
;
265 /** \private Reserved field. */
266 lzma_vli reserved_int6
;
268 /** \private Reserved field. */
269 lzma_vli reserved_int7
;
271 /** \private Reserved field. */
272 lzma_vli reserved_int8
;
274 /** \private Reserved field. */
275 lzma_reserved_enum reserved_enum1
;
277 /** \private Reserved field. */
278 lzma_reserved_enum reserved_enum2
;
280 /** \private Reserved field. */
281 lzma_reserved_enum reserved_enum3
;
283 /** \private Reserved field. */
284 lzma_reserved_enum reserved_enum4
;
287 * \brief A flag to Block decoder to not verify the Check field
289 * This field is supported by liblzma >= 5.1.4beta if .version >= 1.
291 * If this is set to true, the integrity check won't be calculated
292 * and verified. Unless you know what you are doing, you should
293 * leave this to false. (A reason to set this to true is when the
294 * file integrity is verified externally anyway and you want to
295 * speed up the decompression, which matters mostly when using
296 * SHA-256 as the integrity check.)
298 * If .version >= 1, read by:
299 * - lzma_block_decoder()
300 * - lzma_block_buffer_decode()
302 * Written by (.version is ignored):
303 * - lzma_block_header_decode() always sets this to false
305 lzma_bool ignore_check
;
307 /** \private Reserved field. */
308 lzma_bool reserved_bool2
;
310 /** \private Reserved field. */
311 lzma_bool reserved_bool3
;
313 /** \private Reserved field. */
314 lzma_bool reserved_bool4
;
316 /** \private Reserved field. */
317 lzma_bool reserved_bool5
;
319 /** \private Reserved field. */
320 lzma_bool reserved_bool6
;
322 /** \private Reserved field. */
323 lzma_bool reserved_bool7
;
325 /** \private Reserved field. */
326 lzma_bool reserved_bool8
;
332 * \brief Decode the Block Header Size field
334 * To decode Block Header using lzma_block_header_decode(), the size of the
335 * Block Header has to be known and stored into lzma_block.header_size.
336 * The size can be calculated from the first byte of a Block using this macro.
337 * Note that if the first byte is 0x00, it indicates beginning of Index; use
338 * this macro only when the byte is not 0x00.
340 * There is no encoding macro because lzma_block_header_size() and
341 * lzma_block_header_encode() should be used.
343 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
347 * \brief Calculate Block Header Size
349 * Calculate the minimum size needed for the Block Header field using the
350 * settings specified in the lzma_block structure. Note that it is OK to
351 * increase the calculated header_size value as long as it is a multiple of
352 * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
353 * just means that lzma_block_header_encode() will add Header Padding.
355 * \note This doesn't check that all the options are valid i.e. this
356 * may return LZMA_OK even if lzma_block_header_encode() or
357 * lzma_block_encoder() would fail. If you want to validate the
358 * filter chain, consider using lzma_memlimit_encoder() which as
359 * a side-effect validates the filter chain.
361 * \param block Block options
363 * \return Possible lzma_ret values:
364 * - LZMA_OK: Size calculated successfully and stored to
365 * block->header_size.
366 * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
368 * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
370 extern LZMA_API(lzma_ret
) lzma_block_header_size(lzma_block
*block
)
371 lzma_nothrow lzma_attr_warn_unused_result
;
375 * \brief Encode Block Header
377 * The caller must have calculated the size of the Block Header already with
378 * lzma_block_header_size(). If a value larger than the one calculated by
379 * lzma_block_header_size() is used, the Block Header will be padded to the
382 * \param block Block options to be encoded.
383 * \param[out] out Beginning of the output buffer. This must be
384 * at least block->header_size bytes.
386 * \return Possible lzma_ret values:
387 * - LZMA_OK: Encoding was successful. block->header_size
388 * bytes were written to output buffer.
389 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
390 * - LZMA_PROG_ERROR: Invalid arguments, for example
391 * block->header_size is invalid or block->filters is NULL.
393 extern LZMA_API(lzma_ret
) lzma_block_header_encode(
394 const lzma_block
*block
, uint8_t *out
)
395 lzma_nothrow lzma_attr_warn_unused_result
;
399 * \brief Decode Block Header
401 * block->version should (usually) be set to the highest value supported
402 * by the application. If the application sets block->version to a value
403 * higher than supported by the current liblzma version, this function will
404 * downgrade block->version to the highest value supported by it. Thus one
405 * should check the value of block->version after calling this function if
406 * block->version was set to a non-zero value and the application doesn't
407 * otherwise know that the liblzma version being used is new enough to
408 * support the specified block->version.
410 * The size of the Block Header must have already been decoded with
411 * lzma_block_header_size_decode() macro and stored to block->header_size.
413 * The integrity check type from Stream Header must have been stored
416 * block->filters must have been allocated, but they don't need to be
417 * initialized (possible existing filter options are not freed).
419 * \param[out] block Destination for Block options
420 * \param allocator lzma_allocator for custom allocator functions.
421 * Set to NULL to use malloc() (and also free()
422 * if an error occurs).
423 * \param in Beginning of the input buffer. This must be
424 * at least block->header_size bytes.
426 * \return Possible lzma_ret values:
427 * - LZMA_OK: Decoding was successful. block->header_size
428 * bytes were read from the input buffer.
429 * - LZMA_OPTIONS_ERROR: The Block Header specifies some
430 * unsupported options such as unsupported filters. This can
431 * happen also if block->version was set to a too low value
432 * compared to what would be required to properly represent
433 * the information stored in the Block Header.
434 * - LZMA_DATA_ERROR: Block Header is corrupt, for example,
435 * the CRC32 doesn't match.
436 * - LZMA_PROG_ERROR: Invalid arguments, for example
437 * block->header_size is invalid or block->filters is NULL.
439 extern LZMA_API(lzma_ret
) lzma_block_header_decode(lzma_block
*block
,
440 const lzma_allocator
*allocator
, const uint8_t *in
)
441 lzma_nothrow lzma_attr_warn_unused_result
;
445 * \brief Validate and set Compressed Size according to Unpadded Size
447 * Block Header stores Compressed Size, but Index has Unpadded Size. If the
448 * application has already parsed the Index and is now decoding Blocks,
449 * it can calculate Compressed Size from Unpadded Size. This function does
450 * exactly that with error checking:
452 * - Compressed Size calculated from Unpadded Size must be positive integer,
453 * that is, Unpadded Size must be big enough that after Block Header and
454 * Check fields there's still at least one byte for Compressed Size.
456 * - If Compressed Size was present in Block Header, the new value
457 * calculated from Unpadded Size is compared against the value
460 * \note This function must be called _after_ decoding the Block Header
461 * field so that it can properly validate Compressed Size if it
462 * was present in Block Header.
464 * \param block Block options: block->header_size must
465 * already be set with lzma_block_header_size().
466 * \param unpadded_size Unpadded Size from the Index field in bytes
468 * \return Possible lzma_ret values:
469 * - LZMA_OK: block->compressed_size was set successfully.
470 * - LZMA_DATA_ERROR: unpadded_size is too small compared to
471 * block->header_size and lzma_check_size(block->check).
472 * - LZMA_PROG_ERROR: Some values are invalid. For example,
473 * block->header_size must be a multiple of four and
474 * between 8 and 1024 inclusive.
476 extern LZMA_API(lzma_ret
) lzma_block_compressed_size(
477 lzma_block
*block
, lzma_vli unpadded_size
)
478 lzma_nothrow lzma_attr_warn_unused_result
;
482 * \brief Calculate Unpadded Size
484 * The Index field stores Unpadded Size and Uncompressed Size. The latter
485 * can be taken directly from the lzma_block structure after coding a Block,
486 * but Unpadded Size needs to be calculated from Block Header Size,
487 * Compressed Size, and size of the Check field. This is where this function
490 * \param block Block options: block->header_size must already be
491 * set with lzma_block_header_size().
493 * \return Unpadded Size on success, or zero on error.
495 extern LZMA_API(lzma_vli
) lzma_block_unpadded_size(const lzma_block
*block
)
496 lzma_nothrow lzma_attr_pure
;
500 * \brief Calculate the total encoded size of a Block
502 * This is equivalent to lzma_block_unpadded_size() except that the returned
503 * value includes the size of the Block Padding field.
505 * \param block Block options: block->header_size must already be
506 * set with lzma_block_header_size().
508 * \return On success, total encoded size of the Block. On error,
511 extern LZMA_API(lzma_vli
) lzma_block_total_size(const lzma_block
*block
)
512 lzma_nothrow lzma_attr_pure
;
516 * \brief Initialize .xz Block encoder
518 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
519 * filter chain supports it), and LZMA_FINISH.
521 * The Block encoder encodes the Block Data, Block Padding, and Check value.
522 * It does NOT encode the Block Header which can be encoded with
523 * lzma_block_header_encode().
525 * \param strm Pointer to lzma_stream that is at least initialized
526 * with LZMA_STREAM_INIT.
527 * \param block Block options: block->version, block->check,
528 * and block->filters must have been initialized.
530 * \return Possible lzma_ret values:
531 * - LZMA_OK: All good, continue with lzma_code().
533 * - LZMA_OPTIONS_ERROR
534 * - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
535 * that is not supported by this build of liblzma. Initializing
536 * the encoder failed.
539 extern LZMA_API(lzma_ret
) lzma_block_encoder(
540 lzma_stream
*strm
, lzma_block
*block
)
541 lzma_nothrow lzma_attr_warn_unused_result
;
545 * \brief Initialize .xz Block decoder
547 * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
548 * LZMA_FINISH is not required. It is supported only for convenience.
550 * The Block decoder decodes the Block Data, Block Padding, and Check value.
551 * It does NOT decode the Block Header which can be decoded with
552 * lzma_block_header_decode().
554 * \param strm Pointer to lzma_stream that is at least initialized
555 * with LZMA_STREAM_INIT.
556 * \param block Block options
558 * \return Possible lzma_ret values:
559 * - LZMA_OK: All good, continue with lzma_code().
563 extern LZMA_API(lzma_ret
) lzma_block_decoder(
564 lzma_stream
*strm
, lzma_block
*block
)
565 lzma_nothrow lzma_attr_warn_unused_result
;
569 * \brief Calculate maximum output size for single-call Block encoding
571 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
572 * See the documentation of lzma_stream_buffer_bound().
574 * \param uncompressed_size Size of the data to be encoded with the
575 * single-call Block encoder.
577 * \return Maximum output size in bytes for single-call Block encoding.
579 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size
)
584 * \brief Single-call .xz Block encoder
586 * In contrast to the multi-call encoder initialized with
587 * lzma_block_encoder(), this function encodes also the Block Header. This
588 * is required to make it possible to write appropriate Block Header also
589 * in case the data isn't compressible, and different filter chain has to be
590 * used to encode the data in uncompressed form using uncompressed chunks
591 * of the LZMA2 filter.
593 * When the data isn't compressible, header_size, compressed_size, and
594 * uncompressed_size are set just like when the data was compressible, but
595 * it is possible that header_size is too small to hold the filter chain
596 * specified in block->filters, because that isn't necessarily the filter
597 * chain that was actually used to encode the data. lzma_block_unpadded_size()
598 * still works normally, because it doesn't read the filters array.
600 * \param block Block options: block->version, block->check,
601 * and block->filters must have been initialized.
602 * \param allocator lzma_allocator for custom allocator functions.
603 * Set to NULL to use malloc() and free().
604 * \param in Beginning of the input buffer
605 * \param in_size Size of the input buffer
606 * \param[out] out Beginning of the output buffer
607 * \param[out] out_pos The next byte will be written to out[*out_pos].
608 * *out_pos is updated only if encoding succeeds.
609 * \param out_size Size of the out buffer; the first byte into
610 * which no data is written to is out[out_size].
612 * \return Possible lzma_ret values:
613 * - LZMA_OK: Encoding was successful.
614 * - LZMA_BUF_ERROR: Not enough output buffer space.
615 * - LZMA_UNSUPPORTED_CHECK
616 * - LZMA_OPTIONS_ERROR
621 extern LZMA_API(lzma_ret
) lzma_block_buffer_encode(
622 lzma_block
*block
, const lzma_allocator
*allocator
,
623 const uint8_t *in
, size_t in_size
,
624 uint8_t *out
, size_t *out_pos
, size_t out_size
)
625 lzma_nothrow lzma_attr_warn_unused_result
;
629 * \brief Single-call uncompressed .xz Block encoder
631 * This is like lzma_block_buffer_encode() except this doesn't try to
632 * compress the data and instead encodes the data using LZMA2 uncompressed
633 * chunks. The required output buffer size can be determined with
634 * lzma_block_buffer_bound().
636 * Since the data won't be compressed, this function ignores block->filters.
637 * This function doesn't take lzma_allocator because this function doesn't
638 * allocate any memory from the heap.
640 * \param block Block options: block->version, block->check,
641 * and block->filters must have been initialized.
642 * \param in Beginning of the input buffer
643 * \param in_size Size of the input buffer
644 * \param[out] out Beginning of the output buffer
645 * \param[out] out_pos The next byte will be written to out[*out_pos].
646 * *out_pos is updated only if encoding succeeds.
647 * \param out_size Size of the out buffer; the first byte into
648 * which no data is written to is out[out_size].
650 * \return Possible lzma_ret values:
651 * - LZMA_OK: Encoding was successful.
652 * - LZMA_BUF_ERROR: Not enough output buffer space.
653 * - LZMA_UNSUPPORTED_CHECK
654 * - LZMA_OPTIONS_ERROR
659 extern LZMA_API(lzma_ret
) lzma_block_uncomp_encode(lzma_block
*block
,
660 const uint8_t *in
, size_t in_size
,
661 uint8_t *out
, size_t *out_pos
, size_t out_size
)
662 lzma_nothrow lzma_attr_warn_unused_result
;
666 * \brief Single-call .xz Block decoder
668 * This is single-call equivalent of lzma_block_decoder(), and requires that
669 * the caller has already decoded Block Header and checked its memory usage.
671 * \param block Block options
672 * \param allocator lzma_allocator for custom allocator functions.
673 * Set to NULL to use malloc() and free().
674 * \param in Beginning of the input buffer
675 * \param in_pos The next byte will be read from in[*in_pos].
676 * *in_pos is updated only if decoding succeeds.
677 * \param in_size Size of the input buffer; the first byte that
678 * won't be read is in[in_size].
679 * \param[out] out Beginning of the output buffer
680 * \param[out] out_pos The next byte will be written to out[*out_pos].
681 * *out_pos is updated only if encoding succeeds.
682 * \param out_size Size of the out buffer; the first byte into
683 * which no data is written to is out[out_size].
685 * \return Possible lzma_ret values:
686 * - LZMA_OK: Decoding was successful.
687 * - LZMA_OPTIONS_ERROR
690 * - LZMA_BUF_ERROR: Output buffer was too small.
693 extern LZMA_API(lzma_ret
) lzma_block_buffer_decode(
694 lzma_block
*block
, const lzma_allocator
*allocator
,
695 const uint8_t *in
, size_t *in_pos
, size_t in_size
,
696 uint8_t *out
, size_t *out_pos
, size_t out_size
)