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 Header encoder and decoder
27 * Different things use different parts of this structure. Some read
28 * 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 Size of the Block Header
39 * - lzma_block_header_encode()
40 * - lzma_block_header_decode()
41 * - lzma_block_decoder()
44 * - lzma_block_header_size()
47 # define LZMA_BLOCK_HEADER_SIZE_MIN 8
48 # define LZMA_BLOCK_HEADER_SIZE_MAX 1024
51 * \brief Type of integrity Check
53 * The type of the integrity Check is not stored into the Block
54 * Header, thus its value must be provided also when decoding.
57 * - lzma_block_header_encode()
58 * - lzma_block_header_decode()
59 * - lzma_block_encoder()
60 * - lzma_block_decoder()
65 * \brief Size of the Compressed Data in bytes
67 * Usually you don't know this value when encoding in streamed mode.
68 * In non-streamed mode you can reserve space for this field when
69 * encoding the Block Header the first time, and then re-encode the
70 * Block Header and copy it over the original one after the encoding
71 * of the Block has been finished.
74 * - lzma_block_header_size()
75 * - lzma_block_header_encode()
76 * - lzma_block_decoder()
79 * - lzma_block_header_decode()
80 * - lzma_block_encoder()
81 * - lzma_block_decoder()
83 lzma_vli compressed_size;
86 * \brief Uncompressed Size in bytes
88 * Encoder: If this value is not LZMA_VLI_UNKNOWN, it is stored
89 * to the Uncompressed Size field in the Block Header. The real
90 * uncompressed size of the data being compressed must match
91 * the Uncompressed Size or LZMA_OPTIONS_ERROR is returned.
93 * If Uncompressed Size is unknown, End of Payload Marker must
94 * be used. If uncompressed_size == LZMA_VLI_UNKNOWN and
95 * has_eopm == 0, LZMA_OPTIONS_ERROR will be returned.
97 * Decoder: If this value is not LZMA_VLI_UNKNOWN, it is
98 * compared to the real Uncompressed Size. If they do not match,
99 * LZMA_OPTIONS_ERROR is returned.
102 * - lzma_block_header_size()
103 * - lzma_block_header_encode()
104 * - lzma_block_decoder()
107 * - lzma_block_header_decode()
108 * - lzma_block_encoder()
109 * - lzma_block_decoder()
111 lzma_vli uncompressed_size;
114 * \brief Array of filters
116 * There can be 1-4 filters. The end of the array is marked with
117 * .id = LZMA_VLI_UNKNOWN.
120 * - lzma_block_header_size()
121 * - lzma_block_header_encode()
122 * - lzma_block_encoder()
123 * - lzma_block_decoder()
126 * - lzma_block_header_decode(): Note that this does NOT free()
127 * the old filter options structures. All unused filters[] will
128 * have .id == LZMA_VLI_UNKNOWN and .options == NULL. If
129 * decoding fails, all filters[] are guaranteed to be
130 * LZMA_VLI_UNKNOWN and NULL.
132 * \note Because of the array is terminated with
133 * .id = LZMA_VLI_UNKNOWN, the actual array must
134 * have LZMA_FILTERS_MAX + 1 members or the Block
135 * Header decoder will overflow the buffer.
137 lzma_filter *filters;
143 * \brief Decodes the Block Header Size field
145 * To decode Block Header using lzma_block_header_decode(), the size of the
146 * Block Header has to be known and stored into lzma_block.header_size.
147 * The size can be calculated from the first byte of a Block using this macro.
148 * Note that if the first byte is 0x00, it indicates beginning of Index; use
149 * this macro only when the byte is not 0x00.
151 * There is no encoding macro, because Block Header encoder is enough for that.
153 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
157 * \brief Calculates the size of Block Header
159 * \return - LZMA_OK: Size calculated successfully and stored to
160 * options->header_size.
161 * - LZMA_OPTIONS_ERROR: Unsupported filters or filter options.
162 * - LZMA_PROG_ERROR: Invalid options
164 * \note This doesn't check that all the options are valid i.e. this
165 * may return LZMA_OK even if lzma_block_header_encode() or
166 * lzma_block_encoder() would fail.
168 extern lzma_ret lzma_block_header_size(lzma_block *options)
169 lzma_attr_warn_unused_result;
173 * \brief Encodes Block Header
175 * Encoding of the Block options is done with a single call instead of
176 * first initializing and then doing the actual work with lzma_code().
178 * \param out Beginning of the output buffer. This must be
179 * at least options->header_size bytes.
180 * \param options Block options to be encoded.
182 * \return - LZMA_OK: Encoding was successful. options->header_size
183 * bytes were written to output buffer.
184 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
187 extern lzma_ret lzma_block_header_encode(
188 const lzma_block *options, uint8_t *out)
189 lzma_attr_warn_unused_result;
193 * \brief Decodes Block Header
195 * Decoding of the Block options is done with a single call instead of
196 * first initializing and then doing the actual work with lzma_code().
198 * \param options Destination for block options
199 * \param allocator lzma_allocator for custom allocator functions.
200 * Set to NULL to use malloc().
201 * \param in Beginning of the input buffer. This must be
202 * at least options->header_size bytes.
204 * \return - LZMA_OK: Decoding was successful. options->header_size
205 * bytes were written to output buffer.
206 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
209 extern lzma_ret lzma_block_header_decode(lzma_block *options,
210 lzma_allocator *allocator, const uint8_t *in)
211 lzma_attr_warn_unused_result;
215 * \brief Sets Compressed Size according to Unpadded Size
217 * Block Header stores Compressed Size, but Index has Unpadded Size. If the
218 * application has already parsed the Index and is now decoding Blocks,
219 * it can calculate Compressed Size from Unpadded Size. This function does
220 * exactly that with error checking, so application doesn't need to check,
221 * for example, if the value in Index is too small to contain even the
222 * Block Header. Note that you need to call this function _after_ decoding
223 * the Block Header field.
225 * \return - LZMA_OK: options->compressed_size was set successfully.
226 * - LZMA_DATA_ERROR: unpadded_size is too small compared to
227 * options->header_size and lzma_check_sizes[options->check].
228 * - LZMA_PROG_ERROR: Some values are invalid. For example,
229 * options->header_size must be a multiple of four, and
230 * options->header_size between 8 and 1024 inclusive.
232 extern lzma_ret lzma_block_compressed_size(
233 lzma_block *options, lzma_vli unpadded_size)
234 lzma_attr_warn_unused_result;
238 * \brief Calculates Unpadded Size
240 * This function can be useful after decoding a Block to get Unpadded Size
241 * that is stored in Index.
243 * \return Unpadded Size on success, or zero on error.
245 extern lzma_vli lzma_block_unpadded_size(const lzma_block *options)
250 * \brief Calculates the total encoded size of a Block
252 * This is equivalent to lzma_block_unpadded_size() except that the returned
253 * value includes the size of the Block Padding field.
255 * \return On success, total encoded size of the Block. On error,
258 extern lzma_vli lzma_block_total_size(const lzma_block *options)
263 * \brief Initializes .lzma Block encoder
265 * This function is required for multi-thread encoding. It may also be
266 * useful when implementing custom file formats.
268 * \return - LZMA_OK: All good, continue with lzma_code().
270 * - LZMA_OPTIONS_ERROR
271 * - LZMA_UNSUPPORTED_CHECK: options->check specfies a Check
272 * that is not supported by this buid of liblzma. Initializing
273 * the encoder failed.
276 * lzma_code() can return FIXME
278 extern lzma_ret lzma_block_encoder(lzma_stream *strm, lzma_block *options)
279 lzma_attr_warn_unused_result;
283 * \brief Initializes decoder for .lzma Block
285 * \return - LZMA_OK: All good, continue with lzma_code().
286 * - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
287 * the given Check type is not supported, thus Check will be
292 extern lzma_ret lzma_block_decoder(lzma_stream *strm, lzma_block *options)
293 lzma_attr_warn_unused_result;