3 * \brief .lzma 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_encoder()
40 * - lzma_block_decoder()
43 * - lzma_block_header_size()
44 * - lzma_block_header_decode()
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_encoder()
58 * - lzma_block_decoder()
60 lzma_check_type check;
63 * \brief Size of the Compressed Data in bytes
65 * Usually you don't know this value when encoding in streamed mode.
66 * In non-streamed mode you can reserve space for this field when
67 * encoding the Block Header the first time, and then re-encode the
68 * Block Header and copy it over the original one after the encoding
69 * of the Block has been finished.
72 * - lzma_block_header_size()
73 * - lzma_block_header_encode()
74 * - lzma_block_encoder()
75 * - lzma_block_decoder()
78 * - lzma_block_header_decode()
79 * - lzma_block_encoder()
80 * - lzma_block_decoder()
82 lzma_vli compressed_size;
85 * \brief Uncompressed Size in bytes
87 * Encoder: If this value is not LZMA_VLI_VALUE_UNKNOWN, it is stored
88 * to the Uncompressed Size field in the Block Header. The real
89 * uncompressed size of the data being compressed must match
90 * the Uncompressed Size or LZMA_HEADER_ERROR is returned.
92 * If Uncompressed Size is unknown, End of Payload Marker must
93 * be used. If uncompressed_size == LZMA_VLI_VALUE_UNKNOWN and
94 * has_eopm == 0, LZMA_HEADER_ERROR will be returned.
96 * Decoder: If this value is not LZMA_VLI_VALUE_UNKNOWN, it is
97 * compared to the real Uncompressed Size. If they do not match,
98 * LZMA_HEADER_ERROR is returned.
101 * - lzma_block_header_size()
102 * - lzma_block_header_encode()
103 * - lzma_block_encoder()
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_VALUE_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_VALUE_UNKNOWN and .options == NULL. If
129 * decoding fails, all filters[] are guaranteed to be
130 * LZMA_VLI_VALUE_UNKNOWN and NULL.
132 * \note Because of the array is terminated with
133 * .id = LZMA_VLI_VALUE_UNKNOWN, the actual array must
134 * have LZMA_BLOCK_FILTERS_MAX + 1 members or the Block
135 * Header decoder will overflow the buffer.
137 lzma_options_filter *filters;
138 # define LZMA_BLOCK_FILTERS_MAX 4
140 } lzma_options_block;
144 * \brief Decodes the Block Header Size field
146 * To decode Block Header using lzma_block_header_decode(), the size of the
147 * Block Header has to be known and stored into lzma_options_block.header_size.
148 * The size can be calculated from the first byte of a Block using this macro.
149 * Note that if the first byte is 0x00, it indicates beginning of Index; use
150 * this macro only when the byte is not 0x00.
152 #define lzma_block_header_size_decode(b) (((uint32_t)(b) + 1) * 4)
156 * \brief Calculates the size of Block Header
158 * \return - LZMA_OK: Size calculated successfully and stored to
159 * options->header_size.
160 * - LZMA_HEADER_ERROR: Unsupported filters or filter options.
161 * - LZMA_PROG_ERROR: Invalid options
163 * \note This doesn't check that all the options are valid i.e. this
164 * may return LZMA_OK even if lzma_block_header_encode() or
165 * lzma_block_encoder() would fail.
167 extern lzma_ret lzma_block_header_size(lzma_options_block *options);
171 * \brief Encodes Block Header
173 * Encoding of the Block options is done with a single call instead of
174 * first initializing and then doing the actual work with lzma_code().
176 * \param out Beginning of the output buffer. This must be
177 * at least options->header_size bytes.
178 * \param options Block options to be encoded.
180 * \return - LZMA_OK: Encoding was successful. options->header_size
181 * bytes were written to output buffer.
182 * - LZMA_HEADER_ERROR: Invalid or unsupported options.
185 extern lzma_ret lzma_block_header_encode(
186 const lzma_options_block *options, uint8_t *out);
190 * \brief Decodes Block Header
192 * Decoding of the Block options is done with a single call instead of
193 * first initializing and then doing the actual work with lzma_code().
195 * \param options Destination for block options
196 * \param allocator lzma_allocator for custom allocator functions.
197 * Set to NULL to use malloc().
198 * \param in Beginning of the input buffer. This must be
199 * at least options->header_size bytes.
201 * \return - LZMA_OK: Decoding was successful. options->header_size
202 * bytes were written to output buffer.
203 * - LZMA_HEADER_ERROR: Invalid or unsupported options.
206 extern lzma_ret lzma_block_header_decode(lzma_options_block *options,
207 lzma_allocator *allocator, const uint8_t *in);
211 * \brief Sets Compressed Size according to Total Size
213 * Block Header stores Compressed Size, but Index has Total Size. If the
214 * application has already parsed the Index and is now decoding Blocks,
215 * it can calculate Compressed Size from Total Size. This function does
216 * exactly that with error checking, so application doesn't need to check,
217 * for example, if the value in Index is too small to contain even the
218 * Block Header. Note that you need to call this function after decoding
219 * the Block Header field.
221 * \return - LZMA_OK: options->compressed_size was set successfully.
222 * - LZMA_DATA_ERROR: total_size is too small compared to
223 * options->header_size and lzma_check_sizes[options->check].
224 * - LZMA_PROG_ERROR: Some values are invalid. For example,
225 * total_size and options->header_size must be multiples
226 * of four, total_size must be at least 12, and
227 * options->header_size between 8 and 1024 inclusive.
229 extern lzma_ret lzma_block_total_size_set(
230 lzma_options_block *options, lzma_vli total_size);
234 * \brief Calculates Total Size
236 * This function can be useful after decoding a Block to get Total Size
237 * that is stored in Index.
239 * \return Total Size on success, or zero on error.
241 extern lzma_vli lzma_block_total_size_get(const lzma_options_block *options);
245 * \brief Initializes .lzma Block encoder
247 * This function is required for multi-thread encoding. It may also be
248 * useful when implementing custom file formats.
250 * \return - LZMA_OK: All good, continue with lzma_code().
252 * - LZMA_HEADER_ERROR
253 * - LZMA_DATA_ERROR: Limits (total_limit and uncompressed_limit)
254 * have been reached already.
255 * - LZMA_UNSUPPORTED_CHECK: options->check specfies a Check
256 * that is not supported by this buid of liblzma. Initializing
257 * the encoder failed.
260 * lzma_code() can return FIXME
262 extern lzma_ret lzma_block_encoder(
263 lzma_stream *strm, lzma_options_block *options);
267 * \brief Initializes decoder for .lzma Block
269 * \return - LZMA_OK: All good, continue with lzma_code().
270 * - LZMA_UNSUPPORTED_CHECK: Initialization was successful, but
271 * the given Check type is not supported, thus Check will be
276 extern lzma_ret lzma_block_decoder(
277 lzma_stream *strm, lzma_options_block *options);