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 * The integrity check type from Stream Header must have been stored
324 * block->filters must have been allocated, but they don't need to be
325 * initialized (possible existing filter options are not freed).
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.
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.
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;
352 * \brief Validate and set Compressed Size according to Unpadded Size
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:
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.
363 * - If Compressed Size was present in Block Header, the new value
364 * calculated from Unpadded Size is compared against the value
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.
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.
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;
384 * \brief Calculate Unpadded Size
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
392 * \return Unpadded Size on success, or zero on error.
394 extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
395 lzma_nothrow lzma_attr_pure;
399 * \brief Calculate the total encoded size of a Block
401 * This is equivalent to lzma_block_unpadded_size() except that the returned
402 * value includes the size of the Block Padding field.
404 * \return On success, total encoded size of the Block. On error,
407 extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
408 lzma_nothrow lzma_attr_pure;
412 * \brief Initialize .xz Block encoder
414 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
415 * filter chain supports it), and LZMA_FINISH.
417 * \return - LZMA_OK: All good, continue with lzma_code().
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.
425 extern LZMA_API(lzma_ret) lzma_block_encoder(
426 lzma_stream *strm, lzma_block *block)
427 lzma_nothrow lzma_attr_warn_unused_result;
431 * \brief Initialize .xz Block decoder
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.
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
443 extern LZMA_API(lzma_ret) lzma_block_decoder(
444 lzma_stream *strm, lzma_block *block)
445 lzma_nothrow lzma_attr_warn_unused_result;
449 * \brief Calculate maximum output size for single-call Block encoding
451 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
452 * See the documentation of lzma_stream_buffer_bound().
454 extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
459 * \brief Single-call .xz Block encoder
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.
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.
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].
487 * \return - LZMA_OK: Encoding was successful.
488 * - LZMA_BUF_ERROR: Not enough output buffer space.
489 * - LZMA_UNSUPPORTED_CHECK
490 * - LZMA_OPTIONS_ERROR
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;
503 * \brief Single-call .xz Block decoder
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.
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].
522 * \return - LZMA_OK: Decoding was successful.
523 * - LZMA_OPTIONS_ERROR
526 * - LZMA_BUF_ERROR: Output buffer was too small.
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)