1/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: expandtab:ts=8:sw=4:softtabstop=4:
3/**
4 * \file lzma/block.h
5 * \brief .xz Block handling
6 */
7
8/*
9 * Author: Lasse Collin
10 *
11 * This file has been put into the public domain.
12 * You can do whatever you want with this file.
13 *
14 * See ../lzma.h for information about liblzma as a whole.
15 */
16
17#ifndef LZMA_H_INTERNAL
18# error Never include this file directly. Use <lzma.h> instead.
19#endif
20
21
22/**
23 * \brief Options for the Block and Block Header encoders and decoders
24 *
25 * Different Block handling functions use different parts of this structure.
26 * Some read some members, other functions write, and some do both. Only the
27 * members listed for reading need to be initialized when the specified
28 * functions are called. The members marked for writing will be assigned
29 * new values at some point either by calling the given function or by
30 * later calls to lzma_code().
31 */
32typedef struct {
33 /**
34 * \brief Block format version
35 *
36 * To prevent API and ABI breakages if new features are needed in
37 * Block, a version number is used to indicate which fields in this
38 * structure are in use. For now, version must always be zero.
39 * With non-zero version, most Block related functions will return
40 * LZMA_OPTIONS_ERROR.
41 *
42 * The decoding functions will always set this to the lowest value
43 * that supports all the features indicated by the Block Header field.
44 * The application must check that the version number set by the
45 * decoding functions is supported by the application. Otherwise it
46 * is possible that the application will decode the Block incorrectly.
47 *
48 * Read by:
49 * - lzma_block_header_size()
50 * - lzma_block_header_encode()
51 * - lzma_block_compressed_size()
52 * - lzma_block_unpadded_size()
53 * - lzma_block_total_size()
54 * - lzma_block_encoder()
55 * - lzma_block_decoder()
56 * - lzma_block_buffer_encode()
57 * - lzma_block_buffer_decode()
58 *
59 * Written by:
60 * - lzma_block_header_decode()
61 */
62 uint32_t version;
63
64 /**
65 * \brief Size of the Block Header field
66 *
67 * This is always a multiple of four.
68 *
69 * Read by:
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()
77 *
78 * Written by:
79 * - lzma_block_header_size()
80 * - lzma_block_buffer_encode()
81 */
82 uint32_t header_size;
83# define LZMA_BLOCK_HEADER_SIZE_MIN 8
84# define LZMA_BLOCK_HEADER_SIZE_MAX 1024
85
86 /**
87 * \brief Type of integrity Check
88 *
89 * The Check ID is not stored into the Block Header, thus its value
90 * must be provided also when decoding.
91 *
92 * Read by:
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()
101 * - lzma_block_buffer_decode()
102 */
103 lzma_check check;
104
105 /**
106 * \brief Size of the Compressed Data in bytes
107 *
108 * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
109 * will store this value to the Block Header. Block encoder doesn't
110 * care about this value, but will set it once the encoding has been
111 * finished.
112 *
113 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
114 * verify that the size of the Compressed Data field matches
115 * compressed_size.
116 *
117 * Usually you don't know this value when encoding in streamed mode,
118 * and thus cannot write this field into the Block Header.
119 *
120 * In non-streamed mode you can reserve space for this field before
121 * encoding the actual Block. After encoding the data, finish the
122 * Block by encoding the Block Header. Steps in detail:
123 *
124 * - Set compressed_size to some big enough value. If you don't know
125 * better, use LZMA_VLI_MAX, but remember that bigger values take
126 * more space in Block Header.
127 *
128 * - Call lzma_block_header_size() to see how much space you need to
129 * reserve for the Block Header.
130 *
131 * - Encode the Block using lzma_block_encoder() and lzma_code().
132 * It sets compressed_size to the correct value.
133 *
134 * - Use lzma_block_header_encode() to encode the Block Header.
135 * Because space was reserved in the first step, you don't need
136 * to call lzma_block_header_size() anymore, because due to
137 * reserving, header_size has to be big enough. If it is "too big",
138 * lzma_block_header_encode() will add enough Header Padding to
139 * make Block Header to match the size specified by header_size.
140 *
141 * Read by:
142 * - lzma_block_header_size()
143 * - lzma_block_header_encode()
144 * - lzma_block_compressed_size()
145 * - lzma_block_unpadded_size()
146 * - lzma_block_total_size()
147 * - lzma_block_decoder()
148 * - lzma_block_buffer_decode()
149 *
150 * Written by:
151 * - lzma_block_header_decode()
152 * - lzma_block_compressed_size()
153 * - lzma_block_encoder()
154 * - lzma_block_decoder()
155 * - lzma_block_buffer_encode()
156 * - lzma_block_buffer_decode()
157 */
158 lzma_vli compressed_size;
159
160 /**
161 * \brief Uncompressed Size in bytes
162 *
163 * This is handled very similarly to compressed_size above.
164 *
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.
168 *
169 * Read by:
170 * - lzma_block_header_size()
171 * - lzma_block_header_encode()
172 * - lzma_block_decoder()
173 * - lzma_block_buffer_decode()
174 *
175 * Written by:
176 * - lzma_block_header_decode()
177 * - lzma_block_encoder()
178 * - lzma_block_decoder()
179 * - lzma_block_buffer_encode()
180 * - lzma_block_buffer_decode()
181 */
182 lzma_vli uncompressed_size;
183
184 /**
185 * \brief Array of filters
186 *
187 * There can be 1-4 filters. The end of the array is marked with
188 * .id = LZMA_VLI_UNKNOWN.
189 *
190 * Read by:
191 * - lzma_block_header_size()
192 * - lzma_block_header_encode()
193 * - lzma_block_encoder()
194 * - lzma_block_decoder()
195 * - lzma_block_buffer_encode()
196 * - lzma_block_buffer_decode()
197 *
198 * Written by:
199 * - lzma_block_header_decode(): Note that this does NOT free()
200 * the old filter options structures. All unused filters[] will
201 * have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
202 * decoding fails, all filters[] are guaranteed to be
203 * LZMA_VLI_UNKNOWN and NULL.
204 *
205 * \note Because of the array is terminated with
206 * .id = LZMA_VLI_UNKNOWN, the actual array must
207 * have LZMA_FILTERS_MAX + 1 members or the Block
208 * Header decoder will overflow the buffer.
209 */
210 lzma_filter *filters;
211
212 /**
213 * \brief Raw value stored in the Check field
214 *
215 * After successful coding, the first lzma_check_size(check) bytes
216 * of this array contain the raw value stored in the Check field.
217 *
218 * Note that CRC32 and CRC64 are stored in little endian byte order.
219 * Take it into account if you display the Check values to the user.
220 *
221 * Written by:
222 * - lzma_block_encoder()
223 * - lzma_block_decoder()
224 * - lzma_block_buffer_encode()
225 * - lzma_block_buffer_decode()
226 */
227 uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
228
229 /*
230 * Reserved space to allow possible future extensions without
231 * breaking the ABI. You should not touch these, because the names
232 * of these variables may change. These are and will never be used
233 * with the currently supported options, so it is safe to leave these
234 * uninitialized.
235 */
236 void *reserved_ptr1;
237 void *reserved_ptr2;
238 void *reserved_ptr3;
239 uint32_t reserved_int1;
240 uint32_t reserved_int2;
241 lzma_vli reserved_int3;
242 lzma_vli reserved_int4;
243 lzma_vli reserved_int5;
244 lzma_vli reserved_int6;
245 lzma_vli reserved_int7;
246 lzma_vli reserved_int8;
247 lzma_reserved_enum reserved_enum1;
248 lzma_reserved_enum reserved_enum2;
249 lzma_reserved_enum reserved_enum3;
250 lzma_reserved_enum reserved_enum4;
251 lzma_bool reserved_bool1;
252 lzma_bool reserved_bool2;
253 lzma_bool reserved_bool3;
254 lzma_bool reserved_bool4;
255 lzma_bool reserved_bool5;
256 lzma_bool reserved_bool6;
257 lzma_bool reserved_bool7;
258 lzma_bool reserved_bool8;
259
260} lzma_block;
261
262
263/**
264 * \brief Decode the Block Header Size field
265 *
266 * To decode Block Header using lzma_block_header_decode(), the size of the
267 * Block Header has to be known and stored into lzma_block.header_size.
268 * The size can be calculated from the first byte of a Block using this macro.
269 * Note that if the first byte is 0x00, it indicates beginning of Index; use
270 * this macro only when the byte is not 0x00.
271 *
272 * There is no encoding macro, because Block Header encoder is enough for that.
273 */
274#define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
275
276
277/**
278 * \brief Calculate Block Header Size
279 *
280 * Calculate the minimum size needed for the Block Header field using the
281 * settings specified in the lzma_block structure. Note that it is OK to
282 * increase the calculated header_size value as long as it is a multiple of
283 * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
284 * just means that lzma_block_header_encode() will add Header Padding.
285 *
286 * \return - LZMA_OK: Size calculated successfully and stored to
287 * block->header_size.
288 * - LZMA_OPTIONS_ERROR: Unsupported version, filters or
289 * filter options.
290 * - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
291 *
292 * \note This doesn't check that all the options are valid i.e. this
293 * may return LZMA_OK even if lzma_block_header_encode() or
294 * lzma_block_encoder() would fail. If you want to validate the
295 * filter chain, consider using lzma_memlimit_encoder() which as
296 * a side-effect validates the filter chain.
297 */
298extern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
299 lzma_nothrow lzma_attr_warn_unused_result;
300
301
302/**
303 * \brief Encode Block Header
304 *
305 * The caller must have calculated the size of the Block Header already with
306 * lzma_block_header_size(). If a value larger than the one calculated by
307 * lzma_block_header_size() is used, the Block Header will be padded to the
308 * specified size.
309 *
310 * \param out Beginning of the output buffer. This must be
311 * at least block->header_size bytes.
312 * \param block Block options to be encoded.
313 *
314 * \return - LZMA_OK: Encoding was successful. block->header_size
315 * bytes were written to output buffer.
316 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
317 * - LZMA_PROG_ERROR: Invalid arguments, for example
318 * block->header_size is invalid or block->filters is NULL.
319 */
320extern LZMA_API(lzma_ret) lzma_block_header_encode(
321 const lzma_block *block, uint8_t *out)
322 lzma_nothrow lzma_attr_warn_unused_result;
323
324
325/**
326 * \brief Decode Block Header
327 *
328 * The size of the Block Header must have already been decoded with
329 * lzma_block_header_size_decode() macro and stored to block->header_size.
330 * block->filters must have been allocated, but not necessarily initialized.
331 * Possible existing filter options are _not_ freed.
332 *
333 * \param block Destination for block options with header_size
334 * properly initialized.
335 * \param allocator lzma_allocator for custom allocator functions.
336 * Set to NULL to use malloc() (and also free()
337 * if an error occurs).
338 * \param in Beginning of the input buffer. This must be
339 * at least block->header_size bytes.
340 *
341 * \return - LZMA_OK: Decoding was successful. block->header_size
342 * bytes were read from the input buffer.
343 * - LZMA_OPTIONS_ERROR: The Block Header specifies some
344 * unsupported options such as unsupported filters.
345 * - LZMA_DATA_ERROR: Block Header is corrupt, for example,
346 * the CRC32 doesn't match.
347 * - LZMA_PROG_ERROR: Invalid arguments, for example
348 * block->header_size is invalid or block->filters is NULL.
349 */
350extern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
351 lzma_allocator *allocator, const uint8_t *in)
352 lzma_nothrow lzma_attr_warn_unused_result;
353
354
355/**
356 * \brief Validate and set Compressed Size according to Unpadded Size
357 *
358 * Block Header stores Compressed Size, but Index has Unpadded Size. If the
359 * application has already parsed the Index and is now decoding Blocks,
360 * it can calculate Compressed Size from Unpadded Size. This function does
361 * exactly that with error checking:
362 *
363 * - Compressed Size calculated from Unpadded Size must be positive integer,
364 * that is, Unpadded Size must be big enough that after Block Header and
365 * Check fields there's still at least one byte for Compressed Size.
366 *
367 * - If Compressed Size was present in Block Header, the new value
368 * calculated from Unpadded Size is compared against the value
369 * from Block Header.
370 *
371 * \note This function must be called _after_ decoding the Block Header
372 * field so that it can properly validate Compressed Size if it
373 * was present in Block Header.
374 *
375 * \return - LZMA_OK: block->compressed_size was set successfully.
376 * - LZMA_DATA_ERROR: unpadded_size is too small compared to
377 * block->header_size and lzma_check_size(block->check).
378 * - LZMA_PROG_ERROR: Some values are invalid. For example,
379 * block->header_size must be a multiple of four and
380 * between 8 and 1024 inclusive.
381 */
382extern LZMA_API(lzma_ret) lzma_block_compressed_size(
383 lzma_block *block, lzma_vli unpadded_size)
384 lzma_nothrow lzma_attr_warn_unused_result;
385
386
387/**
388 * \brief Calculate Unpadded Size
389 *
390 * The Index field stores Unpadded Size and Uncompressed Size. The latter
391 * can be taken directly from the lzma_block structure after coding a Block,
392 * but Unpadded Size needs to be calculated from Block Header Size,
393 * Compressed Size, and size of the Check field. This is where this function
394 * is needed.
395 *
396 * \return Unpadded Size on success, or zero on error.
397 */
398extern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
399 lzma_nothrow lzma_attr_pure;
400
401
402/**
403 * \brief Calculate the total encoded size of a Block
404 *
405 * This is equivalent to lzma_block_unpadded_size() except that the returned
406 * value includes the size of the Block Padding field.
407 *
408 * \return On success, total encoded size of the Block. On error,
409 * zero is returned.
410 */
411extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
412 lzma_nothrow lzma_attr_pure;
413
414
415/**
416 * \brief Initialize .xz Block encoder
417 *
418 * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
419 * filter chain supports it), and LZMA_FINISH.
420 *
421 * \return - LZMA_OK: All good, continue with lzma_code().
422 * - LZMA_MEM_ERROR
423 * - LZMA_OPTIONS_ERROR
424 * - LZMA_UNSUPPORTED_CHECK: block->check specfies a Check ID
425 * that is not supported by this buid of liblzma. Initializing
426 * the encoder failed.
427 * - LZMA_PROG_ERROR
428 */
429extern LZMA_API(lzma_ret) lzma_block_encoder(
430 lzma_stream *strm, lzma_block *block)
431 lzma_nothrow lzma_attr_warn_unused_result;
432
433
434/**
435 * \brief Initialize .xz Block decoder
436 *
437 * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
438 * LZMA_FINISH is not required. It is supported only for convenience.
439 *
440 * \return - LZMA_OK: All good, continue with lzma_code().
441 * - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
442 * the given Check ID is not supported, thus Check will be
443 * ignored.
444 * - LZMA_PROG_ERROR
445 * - LZMA_MEM_ERROR
446 */
447extern LZMA_API(lzma_ret) lzma_block_decoder(
448 lzma_stream *strm, lzma_block *block)
449 lzma_nothrow lzma_attr_warn_unused_result;
450
451
452/**
453 * \brief Calculate maximum output size for single-call Block encoding
454 *
455 * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
456 * See the documentation of lzma_stream_buffer_bound().
457 */
458extern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
459 lzma_nothrow;
460
461
462/**
463 * \brief Single-call .xz Block encoder
464 *
465 * In contrast to the multi-call encoder initialized with
466 * lzma_block_encoder(), this function encodes also the Block Header. This
467 * is required to make it possible to write appropriate Block Header also
468 * in case the data isn't compressible, and different filter chain has to be
469 * used to encode the data in uncompressed form using uncompressed chunks
470 * of the LZMA2 filter.
471 *
472 * When the data isn't compressible, header_size, compressed_size, and
473 * uncompressed_size are set just like when the data was compressible, but
474 * it is possible that header_size is too small to hold the filter chain
475 * specified in block->filters, because that isn't necessarily the filter
476 * chain that was actually used to encode the data. lzma_block_unpadded_size()
477 * still works normally, because it doesn't read the filters array.
478 *
479 * \param block Block options: block->version, block->check,
480 * and block->filters must have been initialized.
481 * \param allocator lzma_allocator for custom allocator functions.
482 * Set to NULL to use malloc() and free().
483 * \param in Beginning of the input buffer
484 * \param in_size Size of the input buffer
485 * \param out Beginning of the output buffer
486 * \param out_pos The next byte will be written to out[*out_pos].
487 * *out_pos is updated only if encoding succeeds.
488 * \param out_size Size of the out buffer; the first byte into
489 * which no data is written to is out[out_size].
490 *
491 * \return - LZMA_OK: Encoding was successful.
492 * - LZMA_BUF_ERROR: Not enough output buffer space.
493 * - LZMA_OPTIONS_ERROR
494 * - LZMA_MEM_ERROR
495 * - LZMA_DATA_ERROR
496 * - LZMA_PROG_ERROR
497 */
498extern LZMA_API(lzma_ret) lzma_block_buffer_encode(
499 lzma_block *block, lzma_allocator *allocator,
500 const uint8_t *in, size_t in_size,
501 uint8_t *out, size_t *out_pos, size_t out_size)
502 lzma_nothrow lzma_attr_warn_unused_result;
503
504
505/**
506 * \brief Single-call .xz Block decoder
507 *
508 * This is single-call equivalent of lzma_block_decoder(), and requires that
509 * the caller has already decoded Block Header and checked its memory usage.
510 *
511 * \param block Block options just like with lzma_block_decoder().
512 * \param allocator lzma_allocator for custom allocator functions.
513 * Set to NULL to use malloc() and free().
514 * \param in Beginning of the input buffer
515 * \param in_pos The next byte will be read from in[*in_pos].
516 * *in_pos is updated only if decoding succeeds.
517 * \param in_size Size of the input buffer; the first byte that
518 * won't be read is in[in_size].
519 * \param out Beginning of the output buffer
520 * \param out_pos The next byte will be written to out[*out_pos].
521 * *out_pos is updated only if encoding succeeds.
522 * \param out_size Size of the out buffer; the first byte into
523 * which no data is written to is out[out_size].
524 *
525 * \return - LZMA_OK: Decoding was successful.
526 * - LZMA_OPTIONS_ERROR
527 * - LZMA_DATA_ERROR
528 * - LZMA_MEM_ERROR
529 * - LZMA_BUF_ERROR: Output buffer was too small.
530 * - LZMA_PROG_ERROR
531 */
532extern LZMA_API(lzma_ret) lzma_block_buffer_decode(
533 lzma_block *block, lzma_allocator *allocator,
534 const uint8_t *in, size_t *in_pos, size_t in_size,
535 uint8_t *out, size_t *out_pos, size_t out_size)
536 lzma_nothrow;
537