3 * \brief .xz Block handling
5 * \author Copyright (C) 1999-2006 Igor Pavlov
6 * \author Copyright (C) 2007 Lasse Collin
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
19 #ifndef LZMA_H_INTERNAL
20 # error Never include this file directly. Use <lzma.h> instead.
25 * \brief Options for the Block and Block Header encoders and decoders
27 * Different Block handling functions use different parts of this structure.
28 * Some read some members, other functions write, and some do both. Only the
29 * members listed for reading need to be initialized when the specified
30 * functions are called. The members marked for writing will be assigned
31 * new values at some point either by calling the given function or by
32 * later calls to lzma_code().
36 * \brief Block format version
38 * To prevent API and ABI breakages if new features are needed in
39 * Block, a version number is used to indicate which fields in this
40 * structure are in use. For now, version must always be zero.
41 * With non-zero version, most Block related functions will return
44 * The decoding functions will always set this to the lowest value
45 * that supports all the features indicated by the Block Header field.
46 * The application must check that the version number set by the
47 * decoding functions is supported by the application. Otherwise it
48 * is possible that the application will decode the Block incorrectly.
51 * - lzma_block_header_size()
52 * - lzma_block_header_encode()
53 * - lzma_block_compressed_size()
54 * - lzma_block_unpadded_size()
55 * - lzma_block_total_size()
56 * - lzma_block_encoder()
57 * - lzma_block_decoder()
58 * - lzma_block_buffer_encode()
61 * - lzma_block_header_decode()
66 * \brief Size of the Block Header field
68 * This is always a multiple of four.
71 * - lzma_block_header_encode()
72 * - lzma_block_header_decode()
73 * - lzma_block_compressed_size()
74 * - lzma_block_unpadded_size()
75 * - lzma_block_total_size()
76 * - lzma_block_decoder()
79 * - lzma_block_header_size()
80 * - lzma_block_buffer_encode()
83 # define LZMA_BLOCK_HEADER_SIZE_MIN 8
84 # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
87 * \brief Type of integrity Check
89 * The Check ID is not stored into the Block Header, thus its value
90 * must be provided also when decoding.
93 * - lzma_block_header_encode()
94 * - lzma_block_header_decode()
95 * - lzma_block_compressed_size()
96 * - lzma_block_unpadded_size()
97 * - lzma_block_total_size()
98 * - lzma_block_encoder()
99 * - lzma_block_decoder()
100 * - lzma_block_buffer_encode()
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()
149 * - lzma_block_header_decode()
150 * - lzma_block_compressed_size()
151 * - lzma_block_encoder()
152 * - lzma_block_decoder()
153 * - lzma_block_buffer_encode()
155 lzma_vli compressed_size;
158 * \brief Uncompressed Size in bytes
160 * This is handled very similarly to compressed_size above.
162 * Unlike compressed_size, uncompressed_size is needed by fewer
163 * functions. This is because uncompressed_size isn't needed to
164 * validate that Block stays within proper limits.
167 * - lzma_block_header_size()
168 * - lzma_block_header_encode()
169 * - lzma_block_decoder()
172 * - lzma_block_header_decode()
173 * - lzma_block_encoder()
174 * - lzma_block_decoder()
175 * - lzma_block_buffer_encode()
177 lzma_vli uncompressed_size;
180 * \brief Array of filters
182 * There can be 1-4 filters. The end of the array is marked with
183 * .id = LZMA_VLI_UNKNOWN.
186 * - lzma_block_header_size()
187 * - lzma_block_header_encode()
188 * - lzma_block_encoder()
189 * - lzma_block_decoder()
190 * - lzma_block_buffer_encode()
193 * - lzma_block_header_decode(): Note that this does NOT free()
194 * the old filter options structures. All unused filters[] will
195 * have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
196 * decoding fails, all filters[] are guaranteed to be
197 * LZMA_VLI_UNKNOWN and NULL.
199 * \note Because of the array is terminated with
200 * .id = LZMA_VLI_UNKNOWN, the actual array must
201 * have LZMA_FILTERS_MAX + 1 members or the Block
202 * Header decoder will overflow the buffer.
204 lzma_filter *filters;
207 * Reserved space to allow possible future extensions without
208 * breaking the ABI. You should not touch these, because the names
209 * of these variables may change. These are and will never be used
210 * with the currently supported options, so it is safe to leave these
216 uint32_t reserved_int1;
217 uint32_t reserved_int2;
218 lzma_vli reserved_int3;
219 lzma_vli reserved_int4;
220 lzma_vli reserved_int5;
221 lzma_vli reserved_int6;
222 lzma_vli reserved_int7;
223 lzma_vli reserved_int8;
224 lzma_reserved_enum reserved_enum1;
225 lzma_reserved_enum reserved_enum2;
226 lzma_reserved_enum reserved_enum3;
227 lzma_reserved_enum reserved_enum4;
228 lzma_bool reserved_bool1;
229 lzma_bool reserved_bool2;
230 lzma_bool reserved_bool3;
231 lzma_bool reserved_bool4;
232 lzma_bool reserved_bool5;
233 lzma_bool reserved_bool6;
234 lzma_bool reserved_bool7;
235 lzma_bool reserved_bool8;
241 * \brief Decode the Block Header Size field
243 * To decode Block Header using lzma_block_header_decode(), the size of the
244 * Block Header has to be known and stored into lzma_block.header_size.
245 * The size can be calculated from the first byte of a Block using this macro.
246 * Note that if the first byte is 0x00, it indicates beginning of Index; use
247 * this macro only when the byte is not 0x00.
249 * There is no encoding macro, because Block Header encoder is enough for that.
251 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
255 * \brief Calculate Block Header Size
257 * Calculate the minimum size needed for the Block Header field using the
258 * settings specified in the lzma_block structure. Note that it is OK to
259 * increase the calculated header_size value as long as it is a multiple of
260 * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
261 * just means that lzma_block_header_encode() will add Header Padding.
263 * \return - LZMA_OK: Size calculated successfully and stored to
264 * block->header_size.
265 * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
267 * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
269 * \note This doesn't check that all the options are valid i.e. this
270 * may return LZMA_OK even if lzma_block_header_encode() or
271 * lzma_block_encoder() would fail. If you want to validate the
272 * filter chain, consider using lzma_memlimit_encoder() which as
273 * a side-effect validates the filter chain.
275 extern lzma_ret lzma_block_header_size(lzma_block *block)
276 lzma_attr_warn_unused_result;
280 * \brief Encode Block Header
282 * The caller must have calculated the size of the Block Header already with
283 * lzma_block_header_size(). If larger value than the one calculated by
284 * lzma_block_header_size() is used, the Block Header will be padded to the
287 * \param out Beginning of the output buffer. This must be
288 * at least block->header_size bytes.
289 * \param block Block options to be encoded.
291 * \return - LZMA_OK: Encoding was successful. block->header_size
292 * bytes were written to output buffer.
293 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
294 * - LZMA_PROG_ERROR: Invalid arguments, for example
295 * block->header_size is invalid or block->filters is NULL.
297 extern lzma_ret lzma_block_header_encode(const lzma_block *block, uint8_t *out)
298 lzma_attr_warn_unused_result;
302 * \brief Decode Block Header
304 * The size of the Block Header must have already been decoded with
305 * lzma_block_header_size_decode() macro and stored to block->header_size.
306 * block->filters must have been allocated, but not necessarily initialized.
307 * Possible existing filter options are _not_ freed.
309 * \param block Destination for block options with header_size
310 * properly initialized.
311 * \param allocator lzma_allocator for custom allocator functions.
312 * Set to NULL to use malloc() (and also free()
313 * if an error occurs).
314 * \param in Beginning of the input buffer. This must be
315 * at least block->header_size bytes.
317 * \return - LZMA_OK: Decoding was successful. block->header_size
318 * bytes were read from the input buffer.
319 * - LZMA_OPTIONS_ERROR: The Block Header specifies some
320 * unsupported options such as unsupported filters.
321 * - LZMA_DATA_ERROR: Block Header is corrupt, for example,
322 * the CRC32 doesn't match.
323 * - LZMA_PROG_ERROR: Invalid arguments, for example
324 * block->header_size is invalid or block->filters is NULL.
326 extern lzma_ret lzma_block_header_decode(lzma_block *block,
327 lzma_allocator *allocator, const uint8_t *in)
328 lzma_attr_warn_unused_result;
332 * \brief Validate and set Compressed Size according to Unpadded Size
334 * Block Header stores Compressed Size, but Index has Unpadded Size. If the
335 * application has already parsed the Index and is now decoding Blocks,
336 * it can calculate Compressed Size from Unpadded Size. This function does
337 * exactly that with error checking:
339 * - Compressed Size calculated from Unpadded Size must be positive integer,
340 * that is, Unpadded Size must be big enough that after Block Header and
341 * Check fields there's still at least one byte for Compressed Size.
343 * - If Compressed Size was present in Block Header, the new value
344 * calculated from Unpadded Size is compared against the value
347 * \note This function must be called _after_ decoding the Block Header
348 * field so that it can properly validate Compressed Size if it
349 * was present in Block Header.
351 * \return - LZMA_OK: block->compressed_size was set successfully.
352 * - LZMA_DATA_ERROR: unpadded_size is too small compared to
353 * block->header_size and lzma_check_size(block->check).
354 * - LZMA_PROG_ERROR: Some values are invalid. For example,
355 * block->header_size must be a multiple of four and
356 * between 8 and 1024 inclusive.
358 extern lzma_ret lzma_block_compressed_size(
359 lzma_block *block, lzma_vli unpadded_size)
360 lzma_attr_warn_unused_result;
364 * \brief Calculate Unpadded Size
366 * The Index field stores Unpadded Size and Uncompressed Size. The latter
367 * can be taken directly from the lzma_block structure after coding a Block,
368 * but Unpadded Size needs to be calculated from Block Header Size,
369 * Compressed Size, and size of the Check field. This is where this function
372 * \return Unpadded Size on success, or zero on error.
374 extern lzma_vli lzma_block_unpadded_size(const lzma_block *block)
379 * \brief Calculate the total encoded size of a Block
381 * This is equivalent to lzma_block_unpadded_size() except that the returned
382 * value includes the size of the Block Padding field.
384 * \return On success, total encoded size of the Block. On error,
387 extern lzma_vli lzma_block_total_size(const lzma_block *block)
392 * \brief Initialize .xz Block encoder
394 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
395 * filter chain supports it), and LZMA_FINISH.
397 * \return - LZMA_OK: All good, continue with lzma_code().
399 * - LZMA_OPTIONS_ERROR
400 * - LZMA_UNSUPPORTED_CHECK: block->check specfies a Check ID
401 * that is not supported by this buid of liblzma. Initializing
402 * the encoder failed.
405 extern lzma_ret lzma_block_encoder(lzma_stream *strm, lzma_block *block)
406 lzma_attr_warn_unused_result;
410 * \brief Initialize .xz Block decoder
412 * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
413 * LZMA_FINISH is not required. It is supported only for convenience.
415 * \return - LZMA_OK: All good, continue with lzma_code().
416 * - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
417 * the given Check ID is not supported, thus Check will be
422 extern lzma_ret lzma_block_decoder(lzma_stream *strm, lzma_block *block)
423 lzma_attr_warn_unused_result;
427 * \brief Calculate maximum output buffer size for single-call encoding
429 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
430 * See the documentation of lzma_stream_buffer_bound().
432 extern size_t lzma_block_buffer_bound(size_t uncompressed_size);
436 * \brief Single-call .xz Block encoder
438 * In contrast to the multi-call encoder initialized with
439 * lzma_block_encoder(), this function encodes also the Block Header. This
440 * is required to make it possible to write appropriate Block Header also
441 * in case the data isn't compressible, and different filter chain has to be
442 * used to encode the data in uncompressed form using uncompressed chunks
443 * of the LZMA2 filter.
445 * When the data isn't compressible, header_size, compressed_size, and
446 * uncompressed_size are set just like when the data was compressible, but
447 * it is possible that header_size is too small to hold the filter chain
448 * specified in block->filters, because that isn't necessarily the filter
449 * chain that was actually used to encode the data. lzma_block_unpadded_size()
450 * still works normally, because it doesn't read the filters array.
452 * \param block Block options: block->version, block->check,
453 * and block->filters must be initialized.
454 * \param allocator lzma_allocator for custom allocator functions.
455 * Set to NULL to use malloc() and free().
456 * \param in Beginning of the input buffer
457 * \param in_size Size of the input buffer
458 * \param out Beginning of the output buffer
459 * \param out_pos The next byte will be written to out[*out_pos].
460 * *out_pos is updated only if encoding succeeds.
461 * \param out_size Size of the out buffer; the first byte into
462 * which no data is written to is out[out_size].
464 * \return - LZMA_OK: Encoding was successful.
465 * - LZMA_BUF_ERROR: Not enough output buffer space.
466 * - LZMA_OPTIONS_ERROR
471 extern lzma_ret lzma_block_buffer_encode(
472 lzma_block *block, lzma_allocator *allocator,
473 const uint8_t *in, size_t in_size,
474 uint8_t *out, size_t *out_pos, size_t out_size)
475 lzma_attr_warn_unused_result;