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 * 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.
41 * - All functions that take pointer to lzma_block as argument,
42 * including lzma_block_header_decode().
45 * - lzma_block_header_decode()
50 * \brief Size of the Block Header field
52 * This is always a multiple of four.
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()
64 * - lzma_block_header_size()
65 * - lzma_block_buffer_encode()
68 # define LZMA_BLOCK_HEADER_SIZE_MIN 8
69 # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
72 * \brief Type of integrity Check
74 * The Check ID is not stored into the Block Header, thus its value
75 * must be provided also when decoding.
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()
91 * \brief Size of the Compressed Data in bytes
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
98 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
99 * verify that the size of the Compressed Data field matches
102 * Usually you don't know this value when encoding in streamed mode,
103 * and thus cannot write this field into the Block Header.
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:
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.
113 * - Call lzma_block_header_size() to see how much space you need to
114 * reserve for the Block Header.
116 * - Encode the Block using lzma_block_encoder() and lzma_code().
117 * It sets compressed_size to the correct value.
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.
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()
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()
143 lzma_vli compressed_size;
146 * \brief Uncompressed Size in bytes
148 * This is handled very similarly to compressed_size above.
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.
155 * - lzma_block_header_size()
156 * - lzma_block_header_encode()
157 * - lzma_block_decoder()
158 * - lzma_block_buffer_decode()
161 * - lzma_block_header_decode()
162 * - lzma_block_encoder()
163 * - lzma_block_decoder()
164 * - lzma_block_buffer_encode()
165 * - lzma_block_buffer_decode()
167 lzma_vli uncompressed_size;
170 * \brief Array of filters
172 * There can be 1-4 filters. The end of the array is marked with
173 * .id = LZMA_VLI_UNKNOWN.
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()
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.
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.
195 lzma_filter *filters;
198 * \brief Raw value stored in the Check field
200 * After successful coding, the first lzma_check_size(check) bytes
201 * of this array contain the raw value stored in the Check field.
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.
207 * - lzma_block_encoder()
208 * - lzma_block_decoder()
209 * - lzma_block_buffer_encode()
210 * - lzma_block_buffer_decode()
212 uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
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
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;
249 * \brief Decode the Block Header Size field
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.
257 * There is no encoding macro, because Block Header encoder is enough for that.
259 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
263 * \brief Calculate Block Header Size
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.
271 * \return - LZMA_OK: Size calculated successfully and stored to
272 * block->header_size.
273 * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
275 * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
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.
283 extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
284 lzma_nothrow lzma_attr_warn_unused_result;
288 * \brief Encode Block Header
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
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.
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.
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;
311 * \brief Decode Block Header
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.
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.
321 * block->filters must have been allocated, but not necessarily initialized.
322 * Possible existing filter options are _not_ freed.
324 * \param block Destination for Block options.
325 * \param allocator lzma_allocator for custom allocator functions.
326 * Set to NULL to use malloc() (and also free()
327 * if an error occurs).
328 * \param in Beginning of the input buffer. This must be
329 * at least block->header_size bytes.
331 * \return - LZMA_OK: Decoding was successful. block->header_size
332 * bytes were read from the input buffer.
333 * - LZMA_OPTIONS_ERROR: The Block Header specifies some
334 * unsupported options such as unsupported filters. This can
335 * happen also if block->version was set to a too low value
336 * compared to what would be required to properly represent
337 * the information stored in the Block Header.
338 * - LZMA_DATA_ERROR: Block Header is corrupt, for example,
339 * the CRC32 doesn't match.
340 * - LZMA_PROG_ERROR: Invalid arguments, for example
341 * block->header_size is invalid or block->filters is NULL.
343 extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
344 lzma_allocator *allocator, const uint8_t *in)
345 lzma_nothrow lzma_attr_warn_unused_result;
349 * \brief Validate and set Compressed Size according to Unpadded Size
351 * Block Header stores Compressed Size, but Index has Unpadded Size. If the
352 * application has already parsed the Index and is now decoding Blocks,
353 * it can calculate Compressed Size from Unpadded Size. This function does
354 * exactly that with error checking:
356 * - Compressed Size calculated from Unpadded Size must be positive integer,
357 * that is, Unpadded Size must be big enough that after Block Header and
358 * Check fields there's still at least one byte for Compressed Size.
360 * - If Compressed Size was present in Block Header, the new value
361 * calculated from Unpadded Size is compared against the value
364 * \note This function must be called _after_ decoding the Block Header
365 * field so that it can properly validate Compressed Size if it
366 * was present in Block Header.
368 * \return - LZMA_OK: block->compressed_size was set successfully.
369 * - LZMA_DATA_ERROR: unpadded_size is too small compared to
370 * block->header_size and lzma_check_size(block->check).
371 * - LZMA_PROG_ERROR: Some values are invalid. For example,
372 * block->header_size must be a multiple of four and
373 * between 8 and 1024 inclusive.
375 extern LZMA_API(lzma_ret) lzma_block_compressed_size(
376 lzma_block *block, lzma_vli unpadded_size)
377 lzma_nothrow lzma_attr_warn_unused_result;
381 * \brief Calculate Unpadded Size
383 * The Index field stores Unpadded Size and Uncompressed Size. The latter
384 * can be taken directly from the lzma_block structure after coding a Block,
385 * but Unpadded Size needs to be calculated from Block Header Size,
386 * Compressed Size, and size of the Check field. This is where this function
389 * \return Unpadded Size on success, or zero on error.
391 extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
392 lzma_nothrow lzma_attr_pure;
396 * \brief Calculate the total encoded size of a Block
398 * This is equivalent to lzma_block_unpadded_size() except that the returned
399 * value includes the size of the Block Padding field.
401 * \return On success, total encoded size of the Block. On error,
404 extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
405 lzma_nothrow lzma_attr_pure;
409 * \brief Initialize .xz Block encoder
411 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
412 * filter chain supports it), and LZMA_FINISH.
414 * \return - LZMA_OK: All good, continue with lzma_code().
416 * - LZMA_OPTIONS_ERROR
417 * - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
418 * that is not supported by this buid of liblzma. Initializing
419 * the encoder failed.
422 extern LZMA_API(lzma_ret) lzma_block_encoder(
423 lzma_stream *strm, lzma_block *block)
424 lzma_nothrow lzma_attr_warn_unused_result;
428 * \brief Initialize .xz Block decoder
430 * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
431 * LZMA_FINISH is not required. It is supported only for convenience.
433 * \return - LZMA_OK: All good, continue with lzma_code().
434 * - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
435 * the given Check ID is not supported, thus Check will be
440 extern LZMA_API(lzma_ret) lzma_block_decoder(
441 lzma_stream *strm, lzma_block *block)
442 lzma_nothrow lzma_attr_warn_unused_result;
446 * \brief Calculate maximum output size for single-call Block encoding
448 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
449 * See the documentation of lzma_stream_buffer_bound().
451 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
456 * \brief Single-call .xz Block encoder
458 * In contrast to the multi-call encoder initialized with
459 * lzma_block_encoder(), this function encodes also the Block Header. This
460 * is required to make it possible to write appropriate Block Header also
461 * in case the data isn't compressible, and different filter chain has to be
462 * used to encode the data in uncompressed form using uncompressed chunks
463 * of the LZMA2 filter.
465 * When the data isn't compressible, header_size, compressed_size, and
466 * uncompressed_size are set just like when the data was compressible, but
467 * it is possible that header_size is too small to hold the filter chain
468 * specified in block->filters, because that isn't necessarily the filter
469 * chain that was actually used to encode the data. lzma_block_unpadded_size()
470 * still works normally, because it doesn't read the filters array.
472 * \param block Block options: block->version, block->check,
473 * and block->filters must have been initialized.
474 * \param allocator lzma_allocator for custom allocator functions.
475 * Set to NULL to use malloc() and free().
476 * \param in Beginning of the input buffer
477 * \param in_size Size of the input buffer
478 * \param out Beginning of the output buffer
479 * \param out_pos The next byte will be written to out[*out_pos].
480 * *out_pos is updated only if encoding succeeds.
481 * \param out_size Size of the out buffer; the first byte into
482 * which no data is written to is out[out_size].
484 * \return - LZMA_OK: Encoding was successful.
485 * - LZMA_BUF_ERROR: Not enough output buffer space.
486 * - LZMA_OPTIONS_ERROR
491 extern LZMA_API(lzma_ret) lzma_block_buffer_encode(
492 lzma_block *block, lzma_allocator *allocator,
493 const uint8_t *in, size_t in_size,
494 uint8_t *out, size_t *out_pos, size_t out_size)
495 lzma_nothrow lzma_attr_warn_unused_result;
499 * \brief Single-call .xz Block decoder
501 * This is single-call equivalent of lzma_block_decoder(), and requires that
502 * the caller has already decoded Block Header and checked its memory usage.
504 * \param block Block options just like with lzma_block_decoder().
505 * \param allocator lzma_allocator for custom allocator functions.
506 * Set to NULL to use malloc() and free().
507 * \param in Beginning of the input buffer
508 * \param in_pos The next byte will be read from in[*in_pos].
509 * *in_pos is updated only if decoding succeeds.
510 * \param in_size Size of the input buffer; the first byte that
511 * won't be read is in[in_size].
512 * \param out Beginning of the output buffer
513 * \param out_pos The next byte will be written to out[*out_pos].
514 * *out_pos is updated only if encoding succeeds.
515 * \param out_size Size of the out buffer; the first byte into
516 * which no data is written to is out[out_size].
518 * \return - LZMA_OK: Decoding was successful.
519 * - LZMA_OPTIONS_ERROR
522 * - LZMA_BUF_ERROR: Output buffer was too small.
525 extern LZMA_API(lzma_ret) lzma_block_buffer_decode(
526 lzma_block *block, lzma_allocator *allocator,
527 const uint8_t *in, size_t *in_pos, size_t in_size,
528 uint8_t *out, size_t *out_pos, size_t out_size)