1207753Smm/**
2207753Smm * \file        lzma/block.h
3207753Smm * \brief       .xz Block handling
4207753Smm */
5207753Smm
6207753Smm/*
7207753Smm * Author: Lasse Collin
8207753Smm *
9207753Smm * This file has been put into the public domain.
10207753Smm * You can do whatever you want with this file.
11207753Smm *
12207753Smm * See ../lzma.h for information about liblzma as a whole.
13207753Smm */
14207753Smm
15207753Smm#ifndef LZMA_H_INTERNAL
16207753Smm#	error Never include this file directly. Use <lzma.h> instead.
17207753Smm#endif
18207753Smm
19207753Smm
20207753Smm/**
21207753Smm * \brief       Options for the Block and Block Header encoders and decoders
22207753Smm *
23207753Smm * Different Block handling functions use different parts of this structure.
24207753Smm * Some read some members, other functions write, and some do both. Only the
25207753Smm * members listed for reading need to be initialized when the specified
26207753Smm * functions are called. The members marked for writing will be assigned
27207753Smm * new values at some point either by calling the given function or by
28207753Smm * later calls to lzma_code().
29207753Smm */
30207753Smmtypedef struct {
31207753Smm	/**
32207753Smm	 * \brief       Block format version
33207753Smm	 *
34292588Sdelphij	 * To prevent API and ABI breakages when new features are needed,
35292588Sdelphij	 * a version number is used to indicate which fields in this
36292588Sdelphij	 * structure are in use:
37292588Sdelphij	 *   - liblzma >= 5.0.0: version = 0 is supported.
38292588Sdelphij	 *   - liblzma >= 5.1.4beta: Support for version = 1 was added,
39292588Sdelphij	 *     which adds the ignore_check field.
40207753Smm	 *
41292588Sdelphij	 * If version is greater than one, most Block related functions
42292588Sdelphij	 * will return LZMA_OPTIONS_ERROR (lzma_block_header_decode() works
43292588Sdelphij	 * with any version value).
44292588Sdelphij	 *
45207753Smm	 * Read by:
46207753Smm	 *  - All functions that take pointer to lzma_block as argument,
47207753Smm	 *    including lzma_block_header_decode().
48207753Smm	 *
49207753Smm	 * Written by:
50207753Smm	 *  - lzma_block_header_decode()
51207753Smm	 */
52207753Smm	uint32_t version;
53207753Smm
54207753Smm	/**
55207753Smm	 * \brief       Size of the Block Header field
56207753Smm	 *
57207753Smm	 * This is always a multiple of four.
58207753Smm	 *
59207753Smm	 * Read by:
60207753Smm	 *  - lzma_block_header_encode()
61207753Smm	 *  - lzma_block_header_decode()
62207753Smm	 *  - lzma_block_compressed_size()
63207753Smm	 *  - lzma_block_unpadded_size()
64207753Smm	 *  - lzma_block_total_size()
65207753Smm	 *  - lzma_block_decoder()
66207753Smm	 *  - lzma_block_buffer_decode()
67207753Smm	 *
68207753Smm	 * Written by:
69207753Smm	 *  - lzma_block_header_size()
70207753Smm	 *  - lzma_block_buffer_encode()
71207753Smm	 */
72207753Smm	uint32_t header_size;
73207753Smm#	define LZMA_BLOCK_HEADER_SIZE_MIN 8
74207753Smm#	define LZMA_BLOCK_HEADER_SIZE_MAX 1024
75207753Smm
76207753Smm	/**
77207753Smm	 * \brief       Type of integrity Check
78207753Smm	 *
79207753Smm	 * The Check ID is not stored into the Block Header, thus its value
80207753Smm	 * must be provided also when decoding.
81207753Smm	 *
82207753Smm	 * Read by:
83207753Smm	 *  - lzma_block_header_encode()
84207753Smm	 *  - lzma_block_header_decode()
85207753Smm	 *  - lzma_block_compressed_size()
86207753Smm	 *  - lzma_block_unpadded_size()
87207753Smm	 *  - lzma_block_total_size()
88207753Smm	 *  - lzma_block_encoder()
89207753Smm	 *  - lzma_block_decoder()
90207753Smm	 *  - lzma_block_buffer_encode()
91207753Smm	 *  - lzma_block_buffer_decode()
92207753Smm	 */
93207753Smm	lzma_check check;
94207753Smm
95207753Smm	/**
96207753Smm	 * \brief       Size of the Compressed Data in bytes
97207753Smm	 *
98207753Smm	 * Encoding: If this is not LZMA_VLI_UNKNOWN, Block Header encoder
99207753Smm	 * will store this value to the Block Header. Block encoder doesn't
100207753Smm	 * care about this value, but will set it once the encoding has been
101207753Smm	 * finished.
102207753Smm	 *
103207753Smm	 * Decoding: If this is not LZMA_VLI_UNKNOWN, Block decoder will
104207753Smm	 * verify that the size of the Compressed Data field matches
105207753Smm	 * compressed_size.
106207753Smm	 *
107207753Smm	 * Usually you don't know this value when encoding in streamed mode,
108207753Smm	 * and thus cannot write this field into the Block Header.
109207753Smm	 *
110207753Smm	 * In non-streamed mode you can reserve space for this field before
111207753Smm	 * encoding the actual Block. After encoding the data, finish the
112207753Smm	 * Block by encoding the Block Header. Steps in detail:
113207753Smm	 *
114207753Smm	 *  - Set compressed_size to some big enough value. If you don't know
115207753Smm	 *    better, use LZMA_VLI_MAX, but remember that bigger values take
116207753Smm	 *    more space in Block Header.
117207753Smm	 *
118207753Smm	 *  - Call lzma_block_header_size() to see how much space you need to
119207753Smm	 *    reserve for the Block Header.
120207753Smm	 *
121207753Smm	 *  - Encode the Block using lzma_block_encoder() and lzma_code().
122207753Smm	 *    It sets compressed_size to the correct value.
123207753Smm	 *
124207753Smm	 *  - Use lzma_block_header_encode() to encode the Block Header.
125207753Smm	 *    Because space was reserved in the first step, you don't need
126207753Smm	 *    to call lzma_block_header_size() anymore, because due to
127207753Smm	 *    reserving, header_size has to be big enough. If it is "too big",
128207753Smm	 *    lzma_block_header_encode() will add enough Header Padding to
129207753Smm	 *    make Block Header to match the size specified by header_size.
130207753Smm	 *
131207753Smm	 * Read by:
132207753Smm	 *  - lzma_block_header_size()
133207753Smm	 *  - lzma_block_header_encode()
134207753Smm	 *  - lzma_block_compressed_size()
135207753Smm	 *  - lzma_block_unpadded_size()
136207753Smm	 *  - lzma_block_total_size()
137207753Smm	 *  - lzma_block_decoder()
138207753Smm	 *  - lzma_block_buffer_decode()
139207753Smm	 *
140207753Smm	 * Written by:
141207753Smm	 *  - lzma_block_header_decode()
142207753Smm	 *  - lzma_block_compressed_size()
143207753Smm	 *  - lzma_block_encoder()
144207753Smm	 *  - lzma_block_decoder()
145207753Smm	 *  - lzma_block_buffer_encode()
146207753Smm	 *  - lzma_block_buffer_decode()
147207753Smm	 */
148207753Smm	lzma_vli compressed_size;
149207753Smm
150207753Smm	/**
151207753Smm	 * \brief       Uncompressed Size in bytes
152207753Smm	 *
153207753Smm	 * This is handled very similarly to compressed_size above.
154207753Smm	 *
155207753Smm	 * uncompressed_size is needed by fewer functions than
156207753Smm	 * compressed_size. This is because uncompressed_size isn't
157207753Smm	 * needed to validate that Block stays within proper limits.
158207753Smm	 *
159207753Smm	 * Read by:
160207753Smm	 *  - lzma_block_header_size()
161207753Smm	 *  - lzma_block_header_encode()
162207753Smm	 *  - lzma_block_decoder()
163207753Smm	 *  - lzma_block_buffer_decode()
164207753Smm	 *
165207753Smm	 * Written by:
166207753Smm	 *  - lzma_block_header_decode()
167207753Smm	 *  - lzma_block_encoder()
168207753Smm	 *  - lzma_block_decoder()
169207753Smm	 *  - lzma_block_buffer_encode()
170207753Smm	 *  - lzma_block_buffer_decode()
171207753Smm	 */
172207753Smm	lzma_vli uncompressed_size;
173207753Smm
174207753Smm	/**
175207753Smm	 * \brief       Array of filters
176207753Smm	 *
177207753Smm	 * There can be 1-4 filters. The end of the array is marked with
178207753Smm	 * .id = LZMA_VLI_UNKNOWN.
179207753Smm	 *
180207753Smm	 * Read by:
181207753Smm	 *  - lzma_block_header_size()
182207753Smm	 *  - lzma_block_header_encode()
183207753Smm	 *  - lzma_block_encoder()
184207753Smm	 *  - lzma_block_decoder()
185207753Smm	 *  - lzma_block_buffer_encode()
186207753Smm	 *  - lzma_block_buffer_decode()
187207753Smm	 *
188207753Smm	 * Written by:
189207753Smm	 *  - lzma_block_header_decode(): Note that this does NOT free()
190207753Smm	 *    the old filter options structures. All unused filters[] will
191207753Smm	 *    have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
192207753Smm	 *    decoding fails, all filters[] are guaranteed to be
193207753Smm	 *    LZMA_VLI_UNKNOWN and NULL.
194207753Smm	 *
195207753Smm	 * \note        Because of the array is terminated with
196207753Smm	 *              .id = LZMA_VLI_UNKNOWN, the actual array must
197207753Smm	 *              have LZMA_FILTERS_MAX + 1 members or the Block
198207753Smm	 *              Header decoder will overflow the buffer.
199207753Smm	 */
200207753Smm	lzma_filter *filters;
201207753Smm
202207753Smm	/**
203207753Smm	 * \brief       Raw value stored in the Check field
204207753Smm	 *
205207753Smm	 * After successful coding, the first lzma_check_size(check) bytes
206207753Smm	 * of this array contain the raw value stored in the Check field.
207207753Smm	 *
208207753Smm	 * Note that CRC32 and CRC64 are stored in little endian byte order.
209207753Smm	 * Take it into account if you display the Check values to the user.
210207753Smm	 *
211207753Smm	 * Written by:
212207753Smm	 *  - lzma_block_encoder()
213207753Smm	 *  - lzma_block_decoder()
214207753Smm	 *  - lzma_block_buffer_encode()
215207753Smm	 *  - lzma_block_buffer_decode()
216207753Smm	 */
217207753Smm	uint8_t raw_check[LZMA_CHECK_SIZE_MAX];
218207753Smm
219207753Smm	/*
220207753Smm	 * Reserved space to allow possible future extensions without
221207753Smm	 * breaking the ABI. You should not touch these, because the names
222207753Smm	 * of these variables may change. These are and will never be used
223207753Smm	 * with the currently supported options, so it is safe to leave these
224207753Smm	 * uninitialized.
225207753Smm	 */
226207753Smm	void *reserved_ptr1;
227207753Smm	void *reserved_ptr2;
228207753Smm	void *reserved_ptr3;
229207753Smm	uint32_t reserved_int1;
230207753Smm	uint32_t reserved_int2;
231207753Smm	lzma_vli reserved_int3;
232207753Smm	lzma_vli reserved_int4;
233207753Smm	lzma_vli reserved_int5;
234207753Smm	lzma_vli reserved_int6;
235207753Smm	lzma_vli reserved_int7;
236207753Smm	lzma_vli reserved_int8;
237207753Smm	lzma_reserved_enum reserved_enum1;
238207753Smm	lzma_reserved_enum reserved_enum2;
239207753Smm	lzma_reserved_enum reserved_enum3;
240207753Smm	lzma_reserved_enum reserved_enum4;
241292588Sdelphij
242292588Sdelphij	/**
243292588Sdelphij	 * \brief       A flag to Block decoder to not verify the Check field
244292588Sdelphij	 *
245292588Sdelphij	 * This field is supported by liblzma >= 5.1.4beta if .version >= 1.
246292588Sdelphij	 *
247292588Sdelphij	 * If this is set to true, the integrity check won't be calculated
248292588Sdelphij	 * and verified. Unless you know what you are doing, you should
249292588Sdelphij	 * leave this to false. (A reason to set this to true is when the
250292588Sdelphij	 * file integrity is verified externally anyway and you want to
251292588Sdelphij	 * speed up the decompression, which matters mostly when using
252292588Sdelphij	 * SHA-256 as the integrity check.)
253292588Sdelphij	 *
254292588Sdelphij	 * If .version >= 1, read by:
255292588Sdelphij	 *   - lzma_block_decoder()
256292588Sdelphij	 *   - lzma_block_buffer_decode()
257292588Sdelphij	 *
258292588Sdelphij	 * Written by (.version is ignored):
259292588Sdelphij	 *   - lzma_block_header_decode() always sets this to false
260292588Sdelphij	 */
261292588Sdelphij	lzma_bool ignore_check;
262292588Sdelphij
263207753Smm	lzma_bool reserved_bool2;
264207753Smm	lzma_bool reserved_bool3;
265207753Smm	lzma_bool reserved_bool4;
266207753Smm	lzma_bool reserved_bool5;
267207753Smm	lzma_bool reserved_bool6;
268207753Smm	lzma_bool reserved_bool7;
269207753Smm	lzma_bool reserved_bool8;
270207753Smm
271207753Smm} lzma_block;
272207753Smm
273207753Smm
274207753Smm/**
275207753Smm * \brief       Decode the Block Header Size field
276207753Smm *
277207753Smm * To decode Block Header using lzma_block_header_decode(), the size of the
278207753Smm * Block Header has to be known and stored into lzma_block.header_size.
279207753Smm * The size can be calculated from the first byte of a Block using this macro.
280207753Smm * Note that if the first byte is 0x00, it indicates beginning of Index; use
281207753Smm * this macro only when the byte is not 0x00.
282207753Smm *
283207753Smm * There is no encoding macro, because Block Header encoder is enough for that.
284207753Smm */
285207753Smm#define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
286207753Smm
287207753Smm
288207753Smm/**
289207753Smm * \brief       Calculate Block Header Size
290207753Smm *
291207753Smm * Calculate the minimum size needed for the Block Header field using the
292207753Smm * settings specified in the lzma_block structure. Note that it is OK to
293207753Smm * increase the calculated header_size value as long as it is a multiple of
294207753Smm * four and doesn't exceed LZMA_BLOCK_HEADER_SIZE_MAX. Increasing header_size
295207753Smm * just means that lzma_block_header_encode() will add Header Padding.
296207753Smm *
297207753Smm * \return      - LZMA_OK: Size calculated successfully and stored to
298207753Smm *                block->header_size.
299207753Smm *              - LZMA_OPTIONS_ERROR: Unsupported version, filters or
300207753Smm *                filter options.
301207753Smm *              - LZMA_PROG_ERROR: Invalid values like compressed_size == 0.
302207753Smm *
303207753Smm * \note        This doesn't check that all the options are valid i.e. this
304207753Smm *              may return LZMA_OK even if lzma_block_header_encode() or
305207753Smm *              lzma_block_encoder() would fail. If you want to validate the
306207753Smm *              filter chain, consider using lzma_memlimit_encoder() which as
307207753Smm *              a side-effect validates the filter chain.
308207753Smm */
309207753Smmextern LZMA_API(lzma_ret) lzma_block_header_size(lzma_block *block)
310207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
311207753Smm
312207753Smm
313207753Smm/**
314207753Smm * \brief       Encode Block Header
315207753Smm *
316207753Smm * The caller must have calculated the size of the Block Header already with
317207753Smm * lzma_block_header_size(). If a value larger than the one calculated by
318207753Smm * lzma_block_header_size() is used, the Block Header will be padded to the
319207753Smm * specified size.
320207753Smm *
321207753Smm * \param       out         Beginning of the output buffer. This must be
322207753Smm *                          at least block->header_size bytes.
323207753Smm * \param       block       Block options to be encoded.
324207753Smm *
325207753Smm * \return      - LZMA_OK: Encoding was successful. block->header_size
326207753Smm *                bytes were written to output buffer.
327207753Smm *              - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
328207753Smm *              - LZMA_PROG_ERROR: Invalid arguments, for example
329207753Smm *                block->header_size is invalid or block->filters is NULL.
330207753Smm */
331207753Smmextern LZMA_API(lzma_ret) lzma_block_header_encode(
332207753Smm		const lzma_block *block, uint8_t *out)
333207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
334207753Smm
335207753Smm
336207753Smm/**
337207753Smm * \brief       Decode Block Header
338207753Smm *
339292588Sdelphij * block->version should (usually) be set to the highest value supported
340292588Sdelphij * by the application. If the application sets block->version to a value
341292588Sdelphij * higher than supported by the current liblzma version, this function will
342292588Sdelphij * downgrade block->version to the highest value supported by it. Thus one
343292588Sdelphij * should check the value of block->version after calling this function if
344292588Sdelphij * block->version was set to a non-zero value and the application doesn't
345292588Sdelphij * otherwise know that the liblzma version being used is new enough to
346292588Sdelphij * support the specified block->version.
347207753Smm *
348207753Smm * The size of the Block Header must have already been decoded with
349207753Smm * lzma_block_header_size_decode() macro and stored to block->header_size.
350207753Smm *
351274261Sdelphij * The integrity check type from Stream Header must have been stored
352274261Sdelphij * to block->check.
353274261Sdelphij *
354215187Smm * block->filters must have been allocated, but they don't need to be
355215187Smm * initialized (possible existing filter options are not freed).
356207753Smm *
357207753Smm * \param       block       Destination for Block options.
358207753Smm * \param       allocator   lzma_allocator for custom allocator functions.
359207753Smm *                          Set to NULL to use malloc() (and also free()
360207753Smm *                          if an error occurs).
361207753Smm * \param       in          Beginning of the input buffer. This must be
362207753Smm *                          at least block->header_size bytes.
363207753Smm *
364207753Smm * \return      - LZMA_OK: Decoding was successful. block->header_size
365207753Smm *                bytes were read from the input buffer.
366207753Smm *              - LZMA_OPTIONS_ERROR: The Block Header specifies some
367207753Smm *                unsupported options such as unsupported filters. This can
368207753Smm *                happen also if block->version was set to a too low value
369207753Smm *                compared to what would be required to properly represent
370207753Smm *                the information stored in the Block Header.
371207753Smm *              - LZMA_DATA_ERROR: Block Header is corrupt, for example,
372207753Smm *                the CRC32 doesn't match.
373207753Smm *              - LZMA_PROG_ERROR: Invalid arguments, for example
374207753Smm *                block->header_size is invalid or block->filters is NULL.
375207753Smm */
376207753Smmextern LZMA_API(lzma_ret) lzma_block_header_decode(lzma_block *block,
377292588Sdelphij		const lzma_allocator *allocator, const uint8_t *in)
378207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
379207753Smm
380207753Smm
381207753Smm/**
382207753Smm * \brief       Validate and set Compressed Size according to Unpadded Size
383207753Smm *
384207753Smm * Block Header stores Compressed Size, but Index has Unpadded Size. If the
385207753Smm * application has already parsed the Index and is now decoding Blocks,
386207753Smm * it can calculate Compressed Size from Unpadded Size. This function does
387207753Smm * exactly that with error checking:
388207753Smm *
389207753Smm *  - Compressed Size calculated from Unpadded Size must be positive integer,
390207753Smm *    that is, Unpadded Size must be big enough that after Block Header and
391207753Smm *    Check fields there's still at least one byte for Compressed Size.
392207753Smm *
393207753Smm *  - If Compressed Size was present in Block Header, the new value
394207753Smm *    calculated from Unpadded Size is compared against the value
395207753Smm *    from Block Header.
396207753Smm *
397207753Smm * \note        This function must be called _after_ decoding the Block Header
398207753Smm *              field so that it can properly validate Compressed Size if it
399207753Smm *              was present in Block Header.
400207753Smm *
401207753Smm * \return      - LZMA_OK: block->compressed_size was set successfully.
402207753Smm *              - LZMA_DATA_ERROR: unpadded_size is too small compared to
403207753Smm *                block->header_size and lzma_check_size(block->check).
404207753Smm *              - LZMA_PROG_ERROR: Some values are invalid. For example,
405207753Smm *                block->header_size must be a multiple of four and
406207753Smm *                between 8 and 1024 inclusive.
407207753Smm */
408207753Smmextern LZMA_API(lzma_ret) lzma_block_compressed_size(
409207753Smm		lzma_block *block, lzma_vli unpadded_size)
410207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
411207753Smm
412207753Smm
413207753Smm/**
414207753Smm * \brief       Calculate Unpadded Size
415207753Smm *
416207753Smm * The Index field stores Unpadded Size and Uncompressed Size. The latter
417207753Smm * can be taken directly from the lzma_block structure after coding a Block,
418207753Smm * but Unpadded Size needs to be calculated from Block Header Size,
419207753Smm * Compressed Size, and size of the Check field. This is where this function
420207753Smm * is needed.
421207753Smm *
422207753Smm * \return      Unpadded Size on success, or zero on error.
423207753Smm */
424207753Smmextern LZMA_API(lzma_vli) lzma_block_unpadded_size(const lzma_block *block)
425207753Smm		lzma_nothrow lzma_attr_pure;
426207753Smm
427207753Smm
428207753Smm/**
429207753Smm * \brief       Calculate the total encoded size of a Block
430207753Smm *
431207753Smm * This is equivalent to lzma_block_unpadded_size() except that the returned
432207753Smm * value includes the size of the Block Padding field.
433207753Smm *
434207753Smm * \return      On success, total encoded size of the Block. On error,
435207753Smm *              zero is returned.
436207753Smm */
437207753Smmextern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
438207753Smm		lzma_nothrow lzma_attr_pure;
439207753Smm
440207753Smm
441207753Smm/**
442207753Smm * \brief       Initialize .xz Block encoder
443207753Smm *
444207753Smm * Valid actions for lzma_code() are LZMA_RUN, LZMA_SYNC_FLUSH (only if the
445207753Smm * filter chain supports it), and LZMA_FINISH.
446207753Smm *
447207753Smm * \return      - LZMA_OK: All good, continue with lzma_code().
448207753Smm *              - LZMA_MEM_ERROR
449207753Smm *              - LZMA_OPTIONS_ERROR
450207753Smm *              - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
451207753Smm *                that is not supported by this buid of liblzma. Initializing
452207753Smm *                the encoder failed.
453207753Smm *              - LZMA_PROG_ERROR
454207753Smm */
455207753Smmextern LZMA_API(lzma_ret) lzma_block_encoder(
456207753Smm		lzma_stream *strm, lzma_block *block)
457207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
458207753Smm
459207753Smm
460207753Smm/**
461207753Smm * \brief       Initialize .xz Block decoder
462207753Smm *
463207753Smm * Valid actions for lzma_code() are LZMA_RUN and LZMA_FINISH. Using
464207753Smm * LZMA_FINISH is not required. It is supported only for convenience.
465207753Smm *
466207753Smm * \return      - LZMA_OK: All good, continue with lzma_code().
467207753Smm *              - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
468207753Smm *                the given Check ID is not supported, thus Check will be
469207753Smm *                ignored.
470207753Smm *              - LZMA_PROG_ERROR
471207753Smm *              - LZMA_MEM_ERROR
472207753Smm */
473207753Smmextern LZMA_API(lzma_ret) lzma_block_decoder(
474207753Smm		lzma_stream *strm, lzma_block *block)
475207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
476207753Smm
477207753Smm
478207753Smm/**
479207753Smm * \brief       Calculate maximum output size for single-call Block encoding
480207753Smm *
481207753Smm * This is equivalent to lzma_stream_buffer_bound() but for .xz Blocks.
482207753Smm * See the documentation of lzma_stream_buffer_bound().
483207753Smm */
484207753Smmextern LZMA_API(size_t) lzma_block_buffer_bound(size_t uncompressed_size)
485207753Smm		lzma_nothrow;
486207753Smm
487207753Smm
488207753Smm/**
489207753Smm * \brief       Single-call .xz Block encoder
490207753Smm *
491207753Smm * In contrast to the multi-call encoder initialized with
492207753Smm * lzma_block_encoder(), this function encodes also the Block Header. This
493207753Smm * is required to make it possible to write appropriate Block Header also
494207753Smm * in case the data isn't compressible, and different filter chain has to be
495207753Smm * used to encode the data in uncompressed form using uncompressed chunks
496207753Smm * of the LZMA2 filter.
497207753Smm *
498207753Smm * When the data isn't compressible, header_size, compressed_size, and
499207753Smm * uncompressed_size are set just like when the data was compressible, but
500207753Smm * it is possible that header_size is too small to hold the filter chain
501207753Smm * specified in block->filters, because that isn't necessarily the filter
502207753Smm * chain that was actually used to encode the data. lzma_block_unpadded_size()
503207753Smm * still works normally, because it doesn't read the filters array.
504207753Smm *
505207753Smm * \param       block       Block options: block->version, block->check,
506207753Smm *                          and block->filters must have been initialized.
507207753Smm * \param       allocator   lzma_allocator for custom allocator functions.
508207753Smm *                          Set to NULL to use malloc() and free().
509207753Smm * \param       in          Beginning of the input buffer
510207753Smm * \param       in_size     Size of the input buffer
511207753Smm * \param       out         Beginning of the output buffer
512207753Smm * \param       out_pos     The next byte will be written to out[*out_pos].
513207753Smm *                          *out_pos is updated only if encoding succeeds.
514207753Smm * \param       out_size    Size of the out buffer; the first byte into
515207753Smm *                          which no data is written to is out[out_size].
516207753Smm *
517207753Smm * \return      - LZMA_OK: Encoding was successful.
518207753Smm *              - LZMA_BUF_ERROR: Not enough output buffer space.
519223935Smm *              - LZMA_UNSUPPORTED_CHECK
520207753Smm *              - LZMA_OPTIONS_ERROR
521207753Smm *              - LZMA_MEM_ERROR
522207753Smm *              - LZMA_DATA_ERROR
523207753Smm *              - LZMA_PROG_ERROR
524207753Smm */
525207753Smmextern LZMA_API(lzma_ret) lzma_block_buffer_encode(
526292588Sdelphij		lzma_block *block, const lzma_allocator *allocator,
527207753Smm		const uint8_t *in, size_t in_size,
528207753Smm		uint8_t *out, size_t *out_pos, size_t out_size)
529207753Smm		lzma_nothrow lzma_attr_warn_unused_result;
530207753Smm
531207753Smm
532207753Smm/**
533292588Sdelphij * \brief       Single-call uncompressed .xz Block encoder
534292588Sdelphij *
535292588Sdelphij * This is like lzma_block_buffer_encode() except this doesn't try to
536292588Sdelphij * compress the data and instead encodes the data using LZMA2 uncompressed
537292588Sdelphij * chunks. The required output buffer size can be determined with
538292588Sdelphij * lzma_block_buffer_bound().
539292588Sdelphij *
540292588Sdelphij * Since the data won't be compressed, this function ignores block->filters.
541292588Sdelphij * This function doesn't take lzma_allocator because this function doesn't
542292588Sdelphij * allocate any memory from the heap.
543292588Sdelphij */
544292588Sdelphijextern LZMA_API(lzma_ret) lzma_block_uncomp_encode(lzma_block *block,
545292588Sdelphij		const uint8_t *in, size_t in_size,
546292588Sdelphij		uint8_t *out, size_t *out_pos, size_t out_size)
547292588Sdelphij		lzma_nothrow lzma_attr_warn_unused_result;
548292588Sdelphij
549292588Sdelphij
550292588Sdelphij/**
551207753Smm * \brief       Single-call .xz Block decoder
552207753Smm *
553207753Smm * This is single-call equivalent of lzma_block_decoder(), and requires that
554207753Smm * the caller has already decoded Block Header and checked its memory usage.
555207753Smm *
556207753Smm * \param       block       Block options just like with lzma_block_decoder().
557207753Smm * \param       allocator   lzma_allocator for custom allocator functions.
558207753Smm *                          Set to NULL to use malloc() and free().
559207753Smm * \param       in          Beginning of the input buffer
560207753Smm * \param       in_pos      The next byte will be read from in[*in_pos].
561207753Smm *                          *in_pos is updated only if decoding succeeds.
562207753Smm * \param       in_size     Size of the input buffer; the first byte that
563207753Smm *                          won't be read is in[in_size].
564207753Smm * \param       out         Beginning of the output buffer
565207753Smm * \param       out_pos     The next byte will be written to out[*out_pos].
566207753Smm *                          *out_pos is updated only if encoding succeeds.
567207753Smm * \param       out_size    Size of the out buffer; the first byte into
568207753Smm *                          which no data is written to is out[out_size].
569207753Smm *
570207753Smm * \return      - LZMA_OK: Decoding was successful.
571207753Smm *              - LZMA_OPTIONS_ERROR
572207753Smm *              - LZMA_DATA_ERROR
573207753Smm *              - LZMA_MEM_ERROR
574207753Smm *              - LZMA_BUF_ERROR: Output buffer was too small.
575207753Smm *              - LZMA_PROG_ERROR
576207753Smm */
577207753Smmextern LZMA_API(lzma_ret) lzma_block_buffer_decode(
578292588Sdelphij		lzma_block *block, const lzma_allocator *allocator,
579207753Smm		const uint8_t *in, size_t *in_pos, size_t in_size,
580207753Smm		uint8_t *out, size_t *out_pos, size_t out_size)
581207753Smm		lzma_nothrow;
582