3 * \brief Common filter related types and functions
9 * This file has been put into the public domain.
10 * You can do whatever you want with this file.
12 * See ../lzma.h for information about liblzma as a whole.
15 #ifndef LZMA_H_INTERNAL
16 # error Never include this file directly. Use <lzma.h> instead.
21 * \brief Maximum number of filters in a chain
23 * A filter chain can have 1-4 filters, of which three are allowed to change
24 * the size of the data. Usually only one or two filters are needed.
26 #define LZMA_FILTERS_MAX 4
30 * \brief Filter options
32 * This structure is used to pass Filter ID and a pointer filter's
33 * options to liblzma. A few functions work with a single lzma_filter
34 * structure, while most functions expect a filter chain.
36 * A filter chain is indicated with an array of lzma_filter structures.
37 * The array is terminated with .id = LZMA_VLI_UNKNOWN. Thus, the filter
38 * array must have LZMA_FILTERS_MAX + 1 elements (that is, five) to
39 * be able to hold any arbitrary filter chain. This is important when
40 * using lzma_block_header_decode() from block.h, because too small
41 * array would make liblzma write past the end of the filters array.
47 * Use constants whose name begin with `LZMA_FILTER_' to specify
48 * different filters. In an array of lzma_filter structures, use
49 * LZMA_VLI_UNKNOWN to indicate end of filters.
51 * \note This is not an enum, because on some systems enums
57 * \brief Pointer to filter-specific options structure
59 * If the filter doesn't need options, set this to NULL. If id is
60 * set to LZMA_VLI_UNKNOWN, options is ignored, and thus
61 * doesn't need be initialized.
69 * \brief Test if the given Filter ID is supported for encoding
71 * Return true if the give Filter ID is supported for encoding by this
72 * liblzma build. Otherwise false is returned.
74 * There is no way to list which filters are available in this particular
75 * liblzma version and build. It would be useless, because the application
76 * couldn't know what kind of options the filter would need.
78 extern LZMA_API(lzma_bool) lzma_filter_encoder_is_supported(lzma_vli id)
79 lzma_nothrow lzma_attr_const;
83 * \brief Test if the given Filter ID is supported for decoding
85 * Return true if the give Filter ID is supported for decoding by this
86 * liblzma build. Otherwise false is returned.
88 extern LZMA_API(lzma_bool) lzma_filter_decoder_is_supported(lzma_vli id)
89 lzma_nothrow lzma_attr_const;
93 * \brief Copy the filters array
95 * Copy the Filter IDs and filter-specific options from src to dest.
96 * Up to LZMA_FILTERS_MAX filters are copied, plus the terminating
97 * .id == LZMA_VLI_UNKNOWN. Thus, dest should have at least
98 * LZMA_FILTERS_MAX + 1 elements space unless the caller knows that
99 * src is smaller than that.
101 * Unless the filter-specific options is NULL, the Filter ID has to be
102 * supported by liblzma, because liblzma needs to know the size of every
103 * filter-specific options structure. The filter-specific options are not
104 * validated. If options is NULL, any unsupported Filter IDs are copied
105 * without returning an error.
107 * Old filter-specific options in dest are not freed, so dest doesn't
108 * need to be initialized by the caller in any way.
110 * If an error occurs, memory possibly already allocated by this function
115 * - LZMA_OPTIONS_ERROR: Unsupported Filter ID and its options
117 * - LZMA_PROG_ERROR: src or dest is NULL.
119 extern LZMA_API(lzma_ret) lzma_filters_copy(const lzma_filter *src,
120 lzma_filter *dest, lzma_allocator *allocator) lzma_nothrow;
124 * \brief Calculate approximate memory requirements for raw encoder
126 * This function can be used to calculate the memory requirements for
127 * Block and Stream encoders too because Block and Stream encoders don't
128 * need significantly more memory than raw encoder.
130 * \param filters Array of filters terminated with
131 * .id == LZMA_VLI_UNKNOWN.
133 * \return Number of bytes of memory required for the given
134 * filter chain when encoding.
136 extern LZMA_API(uint64_t) lzma_raw_encoder_memusage(const lzma_filter *filters)
137 lzma_nothrow lzma_attr_pure;
141 * \brief Calculate approximate memory requirements for raw decoder
143 * This function can be used to calculate the memory requirements for
144 * Block and Stream decoders too because Block and Stream decoders don't
145 * need significantly more memory than raw decoder.
147 * \param filters Array of filters terminated with
148 * .id == LZMA_VLI_UNKNOWN.
150 * \return Number of bytes of memory required for the given
151 * filter chain when decoding.
153 extern LZMA_API(uint64_t) lzma_raw_decoder_memusage(const lzma_filter *filters)
154 lzma_nothrow lzma_attr_pure;
158 * \brief Initialize raw encoder
160 * This function may be useful when implementing custom file formats.
162 * \param strm Pointer to properly prepared lzma_stream
163 * \param filters Array of lzma_filter structures. The end of the
164 * array must be marked with .id = LZMA_VLI_UNKNOWN.
166 * The `action' with lzma_code() can be LZMA_RUN, LZMA_SYNC_FLUSH (if the
167 * filter chain supports it), or LZMA_FINISH.
171 * - LZMA_OPTIONS_ERROR
174 extern LZMA_API(lzma_ret) lzma_raw_encoder(
175 lzma_stream *strm, const lzma_filter *filters)
176 lzma_nothrow lzma_attr_warn_unused_result;
180 * \brief Initialize raw decoder
182 * The initialization of raw decoder goes similarly to raw encoder.
184 * The `action' with lzma_code() can be LZMA_RUN or LZMA_FINISH. Using
185 * LZMA_FINISH is not required, it is supported just for convenience.
189 * - LZMA_OPTIONS_ERROR
192 extern LZMA_API(lzma_ret) lzma_raw_decoder(
193 lzma_stream *strm, const lzma_filter *filters)
194 lzma_nothrow lzma_attr_warn_unused_result;
198 * \brief Update the filter chain in the encoder
200 * This function is for advanced users only. This function has two slightly
201 * different purposes:
203 * - After LZMA_FULL_FLUSH when using Stream encoder: Set a new filter
204 * chain, which will be used starting from the next Block.
206 * - After LZMA_SYNC_FLUSH using Raw, Block, or Stream encoder: Change
207 * the filter-specific options in the middle of encoding. The actual
208 * filters in the chain (Filter IDs) cannot be changed. In the future,
209 * it might become possible to change the filter options without
210 * using LZMA_SYNC_FLUSH.
212 * While rarely useful, this function may be called also when no data has
213 * been compressed yet. In that case, this function will behave as if
214 * LZMA_FULL_FLUSH (Stream encoder) or LZMA_SYNC_FLUSH (Raw or Block
215 * encoder) had been used right before calling this function.
219 * - LZMA_MEMLIMIT_ERROR
220 * - LZMA_OPTIONS_ERROR
223 extern LZMA_API(lzma_ret) lzma_filters_update(
224 lzma_stream *strm, const lzma_filter *filters) lzma_nothrow;
228 * \brief Single-call raw encoder
230 * \param filters Array of lzma_filter structures. The end of the
231 * array must be marked with .id = LZMA_VLI_UNKNOWN.
232 * \param allocator lzma_allocator for custom allocator functions.
233 * Set to NULL to use malloc() and free().
234 * \param in Beginning of the input buffer
235 * \param in_size Size of the input buffer
236 * \param out Beginning of the output buffer
237 * \param out_pos The next byte will be written to out[*out_pos].
238 * *out_pos is updated only if encoding succeeds.
239 * \param out_size Size of the out buffer; the first byte into
240 * which no data is written to is out[out_size].
242 * \return - LZMA_OK: Encoding was successful.
243 * - LZMA_BUF_ERROR: Not enough output buffer space.
244 * - LZMA_OPTIONS_ERROR
249 * \note There is no function to calculate how big output buffer
250 * would surely be big enough. (lzma_stream_buffer_bound()
251 * works only for lzma_stream_buffer_encode(); raw encoder
252 * won't necessarily meet that bound.)
254 extern LZMA_API(lzma_ret) lzma_raw_buffer_encode(
255 const lzma_filter *filters, lzma_allocator *allocator,
256 const uint8_t *in, size_t in_size, uint8_t *out,
257 size_t *out_pos, size_t out_size) lzma_nothrow;
261 * \brief Single-call raw decoder
263 * \param filters Array of lzma_filter structures. The end of the
264 * array must be marked with .id = LZMA_VLI_UNKNOWN.
265 * \param allocator lzma_allocator for custom allocator functions.
266 * Set to NULL to use malloc() and free().
267 * \param in Beginning of the input buffer
268 * \param in_pos The next byte will be read from in[*in_pos].
269 * *in_pos is updated only if decoding succeeds.
270 * \param in_size Size of the input buffer; the first byte that
271 * won't be read is in[in_size].
272 * \param out Beginning of the output buffer
273 * \param out_pos The next byte will be written to out[*out_pos].
274 * *out_pos is updated only if encoding succeeds.
275 * \param out_size Size of the out buffer; the first byte into
276 * which no data is written to is out[out_size].
278 extern LZMA_API(lzma_ret) lzma_raw_buffer_decode(
279 const lzma_filter *filters, lzma_allocator *allocator,
280 const uint8_t *in, size_t *in_pos, size_t in_size,
281 uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
285 * \brief Get the size of the Filter Properties field
287 * This function may be useful when implementing custom file formats
288 * using the raw encoder and decoder.
290 * \param size Pointer to uint32_t to hold the size of the properties
291 * \param filter Filter ID and options (the size of the properties may
292 * vary depending on the options)
295 * - LZMA_OPTIONS_ERROR
298 * \note This function validates the Filter ID, but does not
299 * necessarily validate the options. Thus, it is possible
300 * that this returns LZMA_OK while the following call to
301 * lzma_properties_encode() returns LZMA_OPTIONS_ERROR.
303 extern LZMA_API(lzma_ret) lzma_properties_size(
304 uint32_t *size, const lzma_filter *filter) lzma_nothrow;
308 * \brief Encode the Filter Properties field
310 * \param filter Filter ID and options
311 * \param props Buffer to hold the encoded options. The size of
312 * buffer must have been already determined with
313 * lzma_properties_size().
316 * - LZMA_OPTIONS_ERROR
319 * \note Even this function won't validate more options than actually
320 * necessary. Thus, it is possible that encoding the properties
321 * succeeds but using the same options to initialize the encoder
324 * \note If lzma_properties_size() indicated that the size
325 * of the Filter Properties field is zero, calling
326 * lzma_properties_encode() is not required, but it
327 * won't do any harm either.
329 extern LZMA_API(lzma_ret) lzma_properties_encode(
330 const lzma_filter *filter, uint8_t *props) lzma_nothrow;
334 * \brief Decode the Filter Properties field
336 * \param filter filter->id must have been set to the correct
337 * Filter ID. filter->options doesn't need to be
338 * initialized (it's not freed by this function). The
339 * decoded options will be stored to filter->options.
340 * filter->options is set to NULL if there are no
341 * properties or if an error occurs.
342 * \param allocator Custom memory allocator used to allocate the
343 * options. Set to NULL to use the default malloc(),
344 * and in case of an error, also free().
345 * \param props Input buffer containing the properties.
346 * \param props_size Size of the properties. This must be the exact
347 * size; giving too much or too little input will
348 * return LZMA_OPTIONS_ERROR.
351 * - LZMA_OPTIONS_ERROR
354 extern LZMA_API(lzma_ret) lzma_properties_decode(
355 lzma_filter *filter, lzma_allocator *allocator,
356 const uint8_t *props, size_t props_size) lzma_nothrow;
360 * \brief Calculate encoded size of a Filter Flags field
362 * Knowing the size of Filter Flags is useful to know when allocating
363 * memory to hold the encoded Filter Flags.
365 * \param size Pointer to integer to hold the calculated size
366 * \param filter Filter ID and associated options whose encoded
367 * size is to be calculated
369 * \return - LZMA_OK: *size set successfully. Note that this doesn't
370 * guarantee that filter->options is valid, thus
371 * lzma_filter_flags_encode() may still fail.
372 * - LZMA_OPTIONS_ERROR: Unknown Filter ID or unsupported options.
373 * - LZMA_PROG_ERROR: Invalid options
375 * \note If you need to calculate size of List of Filter Flags,
376 * you need to loop over every lzma_filter entry.
378 extern LZMA_API(lzma_ret) lzma_filter_flags_size(
379 uint32_t *size, const lzma_filter *filter)
380 lzma_nothrow lzma_attr_warn_unused_result;
384 * \brief Encode Filter Flags into given buffer
386 * In contrast to some functions, this doesn't allocate the needed buffer.
387 * This is due to how this function is used internally by liblzma.
389 * \param filter Filter ID and options to be encoded
390 * \param out Beginning of the output buffer
391 * \param out_pos out[*out_pos] is the next write position. This
392 * is updated by the encoder.
393 * \param out_size out[out_size] is the first byte to not write.
395 * \return - LZMA_OK: Encoding was successful.
396 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options.
397 * - LZMA_PROG_ERROR: Invalid options or not enough output
398 * buffer space (you should have checked it with
399 * lzma_filter_flags_size()).
401 extern LZMA_API(lzma_ret) lzma_filter_flags_encode(const lzma_filter *filter,
402 uint8_t *out, size_t *out_pos, size_t out_size)
403 lzma_nothrow lzma_attr_warn_unused_result;
407 * \brief Decode Filter Flags from given buffer
409 * The decoded result is stored into *filter. The old value of
410 * filter->options is not free()d.
413 * - LZMA_OPTIONS_ERROR
417 extern LZMA_API(lzma_ret) lzma_filter_flags_decode(
418 lzma_filter *filter, lzma_allocator *allocator,
419 const uint8_t *in, size_t *in_pos, size_t in_size)
420 lzma_nothrow lzma_attr_warn_unused_result;