filter.h revision 207842
176082Sbmah/** 276082Sbmah * \file lzma/filter.h 376082Sbmah * \brief Common filter related types 476082Sbmah */ 576082Sbmah 676082Sbmah/* 776082Sbmah * Author: Lasse Collin 876082Sbmah * 976082Sbmah * This file has been put into the public domain. 1076082Sbmah * You can do whatever you want with this file. 1176082Sbmah * 1276082Sbmah * See ../lzma.h for information about liblzma as a whole. 1376082Sbmah */ 1476082Sbmah 1576082Sbmah#ifndef LZMA_H_INTERNAL 1676082Sbmah# error Never include this file directly. Use <lzma.h> instead. 1776082Sbmah#endif 1876082Sbmah 1976082Sbmah 2076082Sbmah/** 2176082Sbmah * \brief Maximum number of filters in a chain 2276082Sbmah * 2376082Sbmah * A filter chain can have 1-4 filters, of which three are allowed to change 2476082Sbmah * the size of the data. Usually only one or two filters are needed. 2576082Sbmah */ 26109307Sbmah#define LZMA_FILTERS_MAX 4 27109543Sbmah 28109543Sbmah 29109543Sbmah/** 30109543Sbmah * \brief Filter options 31109543Sbmah * 32109543Sbmah * This structure is used to pass Filter ID and a pointer filter's 33109307Sbmah * options to liblzma. A few functions work with a single lzma_filter 3477914Sbmah * structure, while most functions expect a filter chain. 3576082Sbmah * 3676082Sbmah * A filter chain is indicated with an array of lzma_filter structures. 3776082Sbmah * The array is terminated with .id = LZMA_VLI_UNKNOWN. Thus, the filter 3876082Sbmah * array must have LZMA_FILTERS_MAX + 1 elements (that is, five) to 3976082Sbmah * be able to hold any arbitrary filter chain. This is important when 4076082Sbmah * using lzma_block_header_decode() from block.h, because too small 4176082Sbmah * array would make liblzma write past the end of the filters array. 4276082Sbmah */ 4376082Sbmahtypedef struct { 4488820Sbmah /** 45108829Sbmah * \brief Filter ID 4676082Sbmah * 4776082Sbmah * Use constants whose name begin with `LZMA_FILTER_' to specify 4876082Sbmah * different filters. In an array of lzma_filter structures, use 4976082Sbmah * LZMA_VLI_UNKNOWN to indicate end of filters. 5077914Sbmah * 5179807Sbmah * \note This is not an enum, because on some systems enums 52109543Sbmah * cannot be 64-bit. 53109543Sbmah */ 54109543Sbmah lzma_vli id; 55109543Sbmah 56109543Sbmah /** 57109543Sbmah * \brief Pointer to filter-specific options structure 58112874Sbmah * 59112874Sbmah * If the filter doesn't need options, set this to NULL. If id is 60112874Sbmah * set to LZMA_VLI_UNKNOWN, options is ignored, and thus 6192295Sbmah * doesn't need be initialized. 6292295Sbmah * 6392295Sbmah * Some filters support changing the options in the middle of 6492295Sbmah * the encoding process. These filters store the pointer of the 6592295Sbmah * options structure and communicate with the application via 6677914Sbmah * modifications of the options structure. 67109307Sbmah */ 68109543Sbmah void *options; 69109543Sbmah 70109543Sbmah} lzma_filter; 71109543Sbmah 72109543Sbmah 73109543Sbmah/** 74116130Sbmah * \brief Test if the given Filter ID is supported for encoding 7577914Sbmah * 7677914Sbmah * Return true if the give Filter ID is supported for encoding by this 77109143Sroam * liblzma build. Otherwise false is returned. 7876082Sbmah * 7976082Sbmah * There is no way to list which filters are available in this particular 8079807Sbmah * liblzma version and build. It would be useless, because the application 8192295Sbmah * couldn't know what kind of options the filter would need. 82109543Sbmah */ 83109543Sbmahextern LZMA_API(lzma_bool) lzma_filter_encoder_is_supported(lzma_vli id) 84109543Sbmah lzma_nothrow lzma_attr_const; 85109543Sbmah 86109543Sbmah 87109543Sbmah/** 8892295Sbmah * \brief Test if the given Filter ID is supported for decoding 8992295Sbmah * 9092295Sbmah * Return true if the give Filter ID is supported for decoding by this 9179807Sbmah * liblzma build. Otherwise false is returned. 9292295Sbmah */ 9392295Sbmahextern LZMA_API(lzma_bool) lzma_filter_decoder_is_supported(lzma_vli id) 9492295Sbmah lzma_nothrow lzma_attr_const; 9592295Sbmah 9692295Sbmah 9792295Sbmah/** 9892295Sbmah * \brief Copy the filters array 9992295Sbmah * 10076082Sbmah * Copy the Filter IDs and filter-specific options from src to dest. 10179807Sbmah * Up to LZMA_FILTERS_MAX filters are copied, plus the terminating 10292295Sbmah * .id == LZMA_VLI_UNKNOWN. Thus, dest should have at least 10392295Sbmah * LZMA_FILTERS_MAX + 1 elements space unless the caller knows that 10476082Sbmah * src is smaller than that. 10577914Sbmah * 10692295Sbmah * Unless the filter-specific options is NULL, the Filter ID has to be 10792295Sbmah * supported by liblzma, because liblzma needs to know the size of every 10892295Sbmah * filter-specific options structure. The filter-specific options are not 10976082Sbmah * validated. If options is NULL, any unsupported Filter IDs are copied 11076082Sbmah * without returning an error. 111109143Sroam * 11276082Sbmah * Old filter-specific options in dest are not freed, so dest doesn't 113109309Sbmah * need to be initialized by the caller in any way. 114115963Sbmah * 115115963Sbmah * If an error occurs, memory possibly already allocated by this function 116115963Sbmah * is always freed. 117109309Sbmah * 118115963Sbmah * \return - LZMA_OK 119118405Sbmah * - LZMA_MEM_ERROR 120118405Sbmah * - LZMA_OPTIONS_ERROR: Unsupported Filter ID and its options 121118405Sbmah * is not NULL. 122118405Sbmah * - LZMA_PROG_ERROR: src or dest is NULL. 123118405Sbmah */ 124118405Sbmahextern LZMA_API(lzma_ret) lzma_filters_copy(const lzma_filter *src, 125118405Sbmah lzma_filter *dest, lzma_allocator *allocator) lzma_nothrow; 126115963Sbmah 127111435Sbmah 128115963Sbmah/** 129111435Sbmah * \brief Calculate rough memory requirements for raw encoder 130115963Sbmah * 131115963Sbmah * Because the calculation is rough, this function can be used to calculate 132111834Sbmah * the memory requirements for Block and Stream encoders too. 133115963Sbmah * 134115963Sbmah * \param filters Array of filters terminated with 135115963Sbmah * .id == LZMA_VLI_UNKNOWN. 136112435Sbmah * 137115963Sbmah * \return Rough number of bytes of memory required for the given 138115965Sbmah * filter chain when encoding. 139115965Sbmah */ 140115965Sbmahextern LZMA_API(uint64_t) lzma_raw_encoder_memusage(const lzma_filter *filters) 141115965Sbmah lzma_nothrow lzma_attr_pure; 142115965Sbmah 143115965Sbmah 144116787Sbmah/** 145116787Sbmah * \brief Calculate rough memory requirements for raw decoder 146116787Sbmah * 147116787Sbmah * Because the calculation is rough, this function can be used to calculate 148116787Sbmah * the memory requirements for Block and Stream decoders too. 149116787Sbmah * 150116787Sbmah * \param filters Array of filters terminated with 151116787Sbmah * .id == LZMA_VLI_UNKNOWN. 152116787Sbmah * 153116787Sbmah * \return Rough number of bytes of memory required for the given 154116787Sbmah * filter chain when decoding. 155116787Sbmah */ 156116787Sbmahextern LZMA_API(uint64_t) lzma_raw_decoder_memusage(const lzma_filter *filters) 157116787Sbmah lzma_nothrow lzma_attr_pure; 158115963Sbmah 159112477Sbmah 16076082Sbmah/** 16176082Sbmah * \brief Initialize raw encoder 162109309Sbmah * 163109309Sbmah * This function may be useful when implementing custom file formats. 164109309Sbmah * 165115963Sbmah * \param strm Pointer to properly prepared lzma_stream 166115963Sbmah * \param filters Array of lzma_filter structures. The end of the 167115963Sbmah * array must be marked with .id = LZMA_VLI_UNKNOWN. 168109583Schris * 169115963Sbmah * The `action' with lzma_code() can be LZMA_RUN, LZMA_SYNC_FLUSH (if the 170115965Sbmah * filter chain supports it), or LZMA_FINISH. 171115965Sbmah * 172115965Sbmah * \return - LZMA_OK 173115965Sbmah * - LZMA_MEM_ERROR 174115965Sbmah * - LZMA_OPTIONS_ERROR 175115965Sbmah * - LZMA_PROG_ERROR 176115965Sbmah */ 177115965Sbmahextern LZMA_API(lzma_ret) lzma_raw_encoder( 178115965Sbmah lzma_stream *strm, const lzma_filter *filters) 179115965Sbmah lzma_nothrow lzma_attr_warn_unused_result; 180115965Sbmah 181115965Sbmah 182115965Sbmah/** 183115965Sbmah * \brief Initialize raw decoder 184115965Sbmah * 185115965Sbmah * The initialization of raw decoder goes similarly to raw encoder. 186115965Sbmah * 187115965Sbmah * The `action' with lzma_code() can be LZMA_RUN or LZMA_FINISH. Using 188115965Sbmah * LZMA_FINISH is not required, it is supported just for convenience. 189115965Sbmah * 190115965Sbmah * \return - LZMA_OK 191115965Sbmah * - LZMA_MEM_ERROR 192115965Sbmah * - LZMA_OPTIONS_ERROR 193115965Sbmah * - LZMA_PROG_ERROR 194115965Sbmah */ 195115965Sbmahextern LZMA_API(lzma_ret) lzma_raw_decoder( 196115965Sbmah lzma_stream *strm, const lzma_filter *filters) 197115965Sbmah lzma_nothrow lzma_attr_warn_unused_result; 198115965Sbmah 199115965Sbmah 200115965Sbmah/** 201115965Sbmah * \brief Update the filter chain in the encoder 202115965Sbmah * 203115965Sbmah * This function is for advanced users only. This function has two slightly 204115965Sbmah * different purposes: 205115965Sbmah * 206115965Sbmah * - After LZMA_FULL_FLUSH when using Stream encoder: Set a new filter 207116630Sbmah * chain, which will be used starting from the next Block. 208115963Sbmah * 209109309Sbmah * - After LZMA_SYNC_FLUSH using Raw, Block, or Stream encoder: Change 210115963Sbmah * the filter-specific options in the middle of encoding. The actual 211109583Schris * filters in the chain (Filter IDs) cannot be changed. In the future, 21276082Sbmah * it might become possible to change the filter options without 213 * using LZMA_SYNC_FLUSH. 214 * 215 * While rarely useful, this function may be called also when no data has 216 * been compressed yet. In that case, this function will behave as if 217 * LZMA_FULL_FLUSH (Stream encoder) or LZMA_SYNC_FLUSH (Raw or Block 218 * encoder) had been used right before calling this function. 219 * 220 * \return - LZMA_OK 221 * - LZMA_MEM_ERROR 222 * - LZMA_MEMLIMIT_ERROR 223 * - LZMA_OPTIONS_ERROR 224 * - LZMA_PROG_ERROR 225 */ 226extern LZMA_API(lzma_ret) lzma_filters_update( 227 lzma_stream *strm, const lzma_filter *filters) lzma_nothrow; 228 229 230/** 231 * \brief Single-call raw encoder 232 * 233 * \param filters Array of lzma_filter structures. The end of the 234 * array must be marked with .id = LZMA_VLI_UNKNOWN. 235 * \param allocator lzma_allocator for custom allocator functions. 236 * Set to NULL to use malloc() and free(). 237 * \param in Beginning of the input buffer 238 * \param in_size Size of the input buffer 239 * \param out Beginning of the output buffer 240 * \param out_pos The next byte will be written to out[*out_pos]. 241 * *out_pos is updated only if encoding succeeds. 242 * \param out_size Size of the out buffer; the first byte into 243 * which no data is written to is out[out_size]. 244 * 245 * \return - LZMA_OK: Encoding was successful. 246 * - LZMA_BUF_ERROR: Not enough output buffer space. 247 * - LZMA_OPTIONS_ERROR 248 * - LZMA_MEM_ERROR 249 * - LZMA_DATA_ERROR 250 * - LZMA_PROG_ERROR 251 * 252 * \note There is no function to calculate how big output buffer 253 * would surely be big enough. (lzma_stream_buffer_bound() 254 * works only for lzma_stream_buffer_encode().) 255 */ 256extern LZMA_API(lzma_ret) lzma_raw_buffer_encode( 257 const lzma_filter *filters, lzma_allocator *allocator, 258 const uint8_t *in, size_t in_size, uint8_t *out, 259 size_t *out_pos, size_t out_size) lzma_nothrow; 260 261 262/** 263 * \brief Single-call raw decoder 264 * 265 * \param filters Array of lzma_filter structures. The end of the 266 * array must be marked with .id = LZMA_VLI_UNKNOWN. 267 * \param allocator lzma_allocator for custom allocator functions. 268 * Set to NULL to use malloc() and free(). 269 * \param in Beginning of the input buffer 270 * \param in_pos The next byte will be read from in[*in_pos]. 271 * *in_pos is updated only if decoding succeeds. 272 * \param in_size Size of the input buffer; the first byte that 273 * won't be read is in[in_size]. 274 * \param out Beginning of the output buffer 275 * \param out_pos The next byte will be written to out[*out_pos]. 276 * *out_pos is updated only if encoding succeeds. 277 * \param out_size Size of the out buffer; the first byte into 278 * which no data is written to is out[out_size]. 279 */ 280extern LZMA_API(lzma_ret) lzma_raw_buffer_decode( 281 const lzma_filter *filters, lzma_allocator *allocator, 282 const uint8_t *in, size_t *in_pos, size_t in_size, 283 uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; 284 285 286/** 287 * \brief Get the size of the Filter Properties field 288 * 289 * This function may be useful when implementing custom file formats 290 * using the raw encoder and decoder. 291 * 292 * \param size Pointer to uint32_t to hold the size of the properties 293 * \param filter Filter ID and options (the size of the properties may 294 * vary depending on the options) 295 * 296 * \return - LZMA_OK 297 * - LZMA_OPTIONS_ERROR 298 * - LZMA_PROG_ERROR 299 * 300 * \note This function validates the Filter ID, but does not 301 * necessarily validate the options. Thus, it is possible 302 * that this returns LZMA_OK while the following call to 303 * lzma_properties_encode() returns LZMA_OPTIONS_ERROR. 304 */ 305extern LZMA_API(lzma_ret) lzma_properties_size( 306 uint32_t *size, const lzma_filter *filter) lzma_nothrow; 307 308 309/** 310 * \brief Encode the Filter Properties field 311 * 312 * \param filter Filter ID and options 313 * \param props Buffer to hold the encoded options. The size of 314 * buffer must have been already determined with 315 * lzma_properties_size(). 316 * 317 * \return - LZMA_OK 318 * - LZMA_OPTIONS_ERROR 319 * - LZMA_PROG_ERROR 320 * 321 * \note Even this function won't validate more options than actually 322 * necessary. Thus, it is possible that encoding the properties 323 * succeeds but using the same options to initialize the encoder 324 * will fail. 325 * 326 * \note It is OK to skip calling this function if 327 * lzma_properties_size() indicated that the size 328 * of the Filter Properties field is zero. 329 */ 330extern LZMA_API(lzma_ret) lzma_properties_encode( 331 const lzma_filter *filter, uint8_t *props) lzma_nothrow; 332 333 334/** 335 * \brief Decode the Filter Properties field 336 * 337 * \param filter filter->id must have been set to the correct 338 * Filter ID. filter->options doesn't need to be 339 * initialized (it's not freed by this function). The 340 * decoded options will be stored to filter->options. 341 * filter->options is set to NULL if there are no 342 * properties or if an error occurs. 343 * \param allocator Custom memory allocator used to allocate the 344 * options. Set to NULL to use the default malloc(), 345 * and in case of an error, also free(). 346 * \param props Input buffer containing the properties. 347 * \param props_size Size of the properties. This must be the exact 348 * size; giving too much or too little input will 349 * return LZMA_OPTIONS_ERROR. 350 * 351 * \return - LZMA_OK 352 * - LZMA_OPTIONS_ERROR 353 * - LZMA_MEM_ERROR 354 */ 355extern LZMA_API(lzma_ret) lzma_properties_decode( 356 lzma_filter *filter, lzma_allocator *allocator, 357 const uint8_t *props, size_t props_size) lzma_nothrow; 358 359 360/** 361 * \brief Calculate encoded size of a Filter Flags field 362 * 363 * Knowing the size of Filter Flags is useful to know when allocating 364 * memory to hold the encoded Filter Flags. 365 * 366 * \param size Pointer to integer to hold the calculated size 367 * \param filters Filter ID and associated options whose encoded 368 * size is to be calculated 369 * 370 * \return - LZMA_OK: *size set successfully. Note that this doesn't 371 * guarantee that filters->options is valid, thus 372 * lzma_filter_flags_encode() may still fail. 373 * - LZMA_OPTIONS_ERROR: Unknown Filter ID or unsupported options. 374 * - LZMA_PROG_ERROR: Invalid options 375 * 376 * \note If you need to calculate size of List of Filter Flags, 377 * you need to loop over every lzma_filter entry. 378 */ 379extern LZMA_API(lzma_ret) lzma_filter_flags_size( 380 uint32_t *size, const lzma_filter *filters) 381 lzma_nothrow lzma_attr_warn_unused_result; 382 383 384/** 385 * \brief Encode Filter Flags into given buffer 386 * 387 * In contrast to some functions, this doesn't allocate the needed buffer. 388 * This is due to how this function is used internally by liblzma. 389 * 390 * \param filters Filter ID and options to be encoded 391 * \param out Beginning of the output buffer 392 * \param out_pos out[*out_pos] is the next write position. This 393 * is updated by the encoder. 394 * \param out_size out[out_size] is the first byte to not write. 395 * 396 * \return - LZMA_OK: Encoding was successful. 397 * - LZMA_OPTIONS_ERROR: Invalid or unsupported options. 398 * - LZMA_PROG_ERROR: Invalid options or not enough output 399 * buffer space (you should have checked it with 400 * lzma_filter_flags_size()). 401 */ 402extern LZMA_API(lzma_ret) lzma_filter_flags_encode(const lzma_filter *filters, 403 uint8_t *out, size_t *out_pos, size_t out_size) 404 lzma_nothrow lzma_attr_warn_unused_result; 405 406 407/** 408 * \brief Decode Filter Flags from given buffer 409 * 410 * The decoded result is stored into *filters. filters->options is 411 * initialized but the old value is NOT free()d. 412 * 413 * \return - LZMA_OK 414 * - LZMA_OPTIONS_ERROR 415 * - LZMA_MEM_ERROR 416 * - LZMA_PROG_ERROR 417 */ 418extern LZMA_API(lzma_ret) lzma_filter_flags_decode( 419 lzma_filter *filters, lzma_allocator *allocator, 420 const uint8_t *in, size_t *in_pos, size_t in_size) 421 lzma_nothrow lzma_attr_warn_unused_result; 422