| 1 | /** |
| 2 | * \file lzma/filter.h |
| 3 | * \brief Common filter related types and functions |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Author: Lasse Collin |
| 8 | * |
| 9 | * This file has been put into the public domain. |
| 10 | * You can do whatever you want with this file. |
| 11 | * |
| 12 | * See ../lzma.h for information about liblzma as a whole. |
| 13 | */ |
| 14 | |
| 15 | #ifndef LZMA_H_INTERNAL |
| 16 | # error Never include this file directly. Use <lzma.h> instead. |
| 17 | #endif |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * \brief Maximum number of filters in a chain |
| 22 | * |
| 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. |
| 25 | */ |
| 26 | #define LZMA_FILTERS_MAX 4 |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * \brief Filter options |
| 31 | * |
| 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. |
| 35 | * |
| 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. |
| 42 | */ |
| 43 | typedef struct { |
| 44 | /** |
| 45 | * \brief Filter ID |
| 46 | * |
| 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. |
| 50 | * |
| 51 | * \note This is not an enum, because on some systems enums |
| 52 | * cannot be 64-bit. |
| 53 | */ |
| 54 | lzma_vli id; |
| 55 | |
| 56 | /** |
| 57 | * \brief Pointer to filter-specific options structure |
| 58 | * |
| 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. |
| 62 | */ |
| 63 | void *options; |
| 64 | |
| 65 | } lzma_filter; |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * \brief Test if the given Filter ID is supported for encoding |
| 70 | * |
| 71 | * Return true if the give Filter ID is supported for encoding by this |
| 72 | * liblzma build. Otherwise false is returned. |
| 73 | * |
| 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. |
| 77 | */ |
| 78 | extern LZMA_API(lzma_bool) lzma_filter_encoder_is_supported(lzma_vli id) |
| 79 | lzma_nothrow lzma_attr_const; |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * \brief Test if the given Filter ID is supported for decoding |
| 84 | * |
| 85 | * Return true if the give Filter ID is supported for decoding by this |
| 86 | * liblzma build. Otherwise false is returned. |
| 87 | */ |
| 88 | extern LZMA_API(lzma_bool) lzma_filter_decoder_is_supported(lzma_vli id) |
| 89 | lzma_nothrow lzma_attr_const; |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * \brief Copy the filters array |
| 94 | * |
| 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. |
| 100 | * |
| 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. |
| 106 | * |
| 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. |
| 109 | * |
| 110 | * If an error occurs, memory possibly already allocated by this function |
| 111 | * is always freed. |
| 112 | * |
| 113 | * \return - LZMA_OK |
| 114 | * - LZMA_MEM_ERROR |
| 115 | * - LZMA_OPTIONS_ERROR: Unsupported Filter ID and its options |
| 116 | * is not NULL. |
| 117 | * - LZMA_PROG_ERROR: src or dest is NULL. |
| 118 | */ |
| 119 | extern LZMA_API(lzma_ret) lzma_filters_copy( |
| 120 | const lzma_filter *src, lzma_filter *dest, |
| 121 | const lzma_allocator *allocator) lzma_nothrow; |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * \brief Calculate approximate memory requirements for raw encoder |
| 126 | * |
| 127 | * This function can be used to calculate the memory requirements for |
| 128 | * Block and Stream encoders too because Block and Stream encoders don't |
| 129 | * need significantly more memory than raw encoder. |
| 130 | * |
| 131 | * \param filters Array of filters terminated with |
| 132 | * .id == LZMA_VLI_UNKNOWN. |
| 133 | * |
| 134 | * \return Number of bytes of memory required for the given |
| 135 | * filter chain when encoding. If an error occurs, |
| 136 | * for example due to unsupported filter chain, |
| 137 | * UINT64_MAX is returned. |
| 138 | */ |
| 139 | extern LZMA_API(uint64_t) lzma_raw_encoder_memusage(const lzma_filter *filters) |
| 140 | lzma_nothrow lzma_attr_pure; |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * \brief Calculate approximate memory requirements for raw decoder |
| 145 | * |
| 146 | * This function can be used to calculate the memory requirements for |
| 147 | * Block and Stream decoders too because Block and Stream decoders don't |
| 148 | * need significantly more memory than raw decoder. |
| 149 | * |
| 150 | * \param filters Array of filters terminated with |
| 151 | * .id == LZMA_VLI_UNKNOWN. |
| 152 | * |
| 153 | * \return Number of bytes of memory required for the given |
| 154 | * filter chain when decoding. If an error occurs, |
| 155 | * for example due to unsupported filter chain, |
| 156 | * UINT64_MAX is returned. |
| 157 | */ |
| 158 | extern LZMA_API(uint64_t) lzma_raw_decoder_memusage(const lzma_filter *filters) |
| 159 | lzma_nothrow lzma_attr_pure; |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * \brief Initialize raw encoder |
| 164 | * |
| 165 | * This function may be useful when implementing custom file formats. |
| 166 | * |
| 167 | * \param strm Pointer to properly prepared lzma_stream |
| 168 | * \param filters Array of lzma_filter structures. The end of the |
| 169 | * array must be marked with .id = LZMA_VLI_UNKNOWN. |
| 170 | * |
| 171 | * The `action' with lzma_code() can be LZMA_RUN, LZMA_SYNC_FLUSH (if the |
| 172 | * filter chain supports it), or LZMA_FINISH. |
| 173 | * |
| 174 | * \return - LZMA_OK |
| 175 | * - LZMA_MEM_ERROR |
| 176 | * - LZMA_OPTIONS_ERROR |
| 177 | * - LZMA_PROG_ERROR |
| 178 | */ |
| 179 | extern LZMA_API(lzma_ret) lzma_raw_encoder( |
| 180 | lzma_stream *strm, const lzma_filter *filters) |
| 181 | lzma_nothrow lzma_attr_warn_unused_result; |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * \brief Initialize raw decoder |
| 186 | * |
| 187 | * The initialization of raw decoder goes similarly to raw encoder. |
| 188 | * |
| 189 | * The `action' with lzma_code() can be LZMA_RUN or LZMA_FINISH. Using |
| 190 | * LZMA_FINISH is not required, it is supported just for convenience. |
| 191 | * |
| 192 | * \return - LZMA_OK |
| 193 | * - LZMA_MEM_ERROR |
| 194 | * - LZMA_OPTIONS_ERROR |
| 195 | * - LZMA_PROG_ERROR |
| 196 | */ |
| 197 | extern LZMA_API(lzma_ret) lzma_raw_decoder( |
| 198 | lzma_stream *strm, const lzma_filter *filters) |
| 199 | lzma_nothrow lzma_attr_warn_unused_result; |
| 200 | |
| 201 | |
| 202 | /** |
| 203 | * \brief Update the filter chain in the encoder |
| 204 | * |
| 205 | * This function is for advanced users only. This function has two slightly |
| 206 | * different purposes: |
| 207 | * |
| 208 | * - After LZMA_FULL_FLUSH when using Stream encoder: Set a new filter |
| 209 | * chain, which will be used starting from the next Block. |
| 210 | * |
| 211 | * - After LZMA_SYNC_FLUSH using Raw, Block, or Stream encoder: Change |
| 212 | * the filter-specific options in the middle of encoding. The actual |
| 213 | * filters in the chain (Filter IDs) cannot be changed. In the future, |
| 214 | * it might become possible to change the filter options without |
| 215 | * using LZMA_SYNC_FLUSH. |
| 216 | * |
| 217 | * While rarely useful, this function may be called also when no data has |
| 218 | * been compressed yet. In that case, this function will behave as if |
| 219 | * LZMA_FULL_FLUSH (Stream encoder) or LZMA_SYNC_FLUSH (Raw or Block |
| 220 | * encoder) had been used right before calling this function. |
| 221 | * |
| 222 | * \return - LZMA_OK |
| 223 | * - LZMA_MEM_ERROR |
| 224 | * - LZMA_MEMLIMIT_ERROR |
| 225 | * - LZMA_OPTIONS_ERROR |
| 226 | * - LZMA_PROG_ERROR |
| 227 | */ |
| 228 | extern LZMA_API(lzma_ret) lzma_filters_update( |
| 229 | lzma_stream *strm, const lzma_filter *filters) lzma_nothrow; |
| 230 | |
| 231 | |
| 232 | /** |
| 233 | * \brief Single-call raw encoder |
| 234 | * |
| 235 | * \param filters Array of lzma_filter structures. The end of the |
| 236 | * array must be marked with .id = LZMA_VLI_UNKNOWN. |
| 237 | * \param allocator lzma_allocator for custom allocator functions. |
| 238 | * Set to NULL to use malloc() and free(). |
| 239 | * \param in Beginning of the input buffer |
| 240 | * \param in_size Size of the input buffer |
| 241 | * \param out Beginning of the output buffer |
| 242 | * \param out_pos The next byte will be written to out[*out_pos]. |
| 243 | * *out_pos is updated only if encoding succeeds. |
| 244 | * \param out_size Size of the out buffer; the first byte into |
| 245 | * which no data is written to is out[out_size]. |
| 246 | * |
| 247 | * \return - LZMA_OK: Encoding was successful. |
| 248 | * - LZMA_BUF_ERROR: Not enough output buffer space. |
| 249 | * - LZMA_OPTIONS_ERROR |
| 250 | * - LZMA_MEM_ERROR |
| 251 | * - LZMA_DATA_ERROR |
| 252 | * - LZMA_PROG_ERROR |
| 253 | * |
| 254 | * \note There is no function to calculate how big output buffer |
| 255 | * would surely be big enough. (lzma_stream_buffer_bound() |
| 256 | * works only for lzma_stream_buffer_encode(); raw encoder |
| 257 | * won't necessarily meet that bound.) |
| 258 | */ |
| 259 | extern LZMA_API(lzma_ret) lzma_raw_buffer_encode( |
| 260 | const lzma_filter *filters, const lzma_allocator *allocator, |
| 261 | const uint8_t *in, size_t in_size, uint8_t *out, |
| 262 | size_t *out_pos, size_t out_size) lzma_nothrow; |
| 263 | |
| 264 | |
| 265 | /** |
| 266 | * \brief Single-call raw decoder |
| 267 | * |
| 268 | * \param filters Array of lzma_filter structures. The end of the |
| 269 | * array must be marked with .id = LZMA_VLI_UNKNOWN. |
| 270 | * \param allocator lzma_allocator for custom allocator functions. |
| 271 | * Set to NULL to use malloc() and free(). |
| 272 | * \param in Beginning of the input buffer |
| 273 | * \param in_pos The next byte will be read from in[*in_pos]. |
| 274 | * *in_pos is updated only if decoding succeeds. |
| 275 | * \param in_size Size of the input buffer; the first byte that |
| 276 | * won't be read is in[in_size]. |
| 277 | * \param out Beginning of the output buffer |
| 278 | * \param out_pos The next byte will be written to out[*out_pos]. |
| 279 | * *out_pos is updated only if encoding succeeds. |
| 280 | * \param out_size Size of the out buffer; the first byte into |
| 281 | * which no data is written to is out[out_size]. |
| 282 | */ |
| 283 | extern LZMA_API(lzma_ret) lzma_raw_buffer_decode( |
| 284 | const lzma_filter *filters, const lzma_allocator *allocator, |
| 285 | const uint8_t *in, size_t *in_pos, size_t in_size, |
| 286 | uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * \brief Get the size of the Filter Properties field |
| 291 | * |
| 292 | * This function may be useful when implementing custom file formats |
| 293 | * using the raw encoder and decoder. |
| 294 | * |
| 295 | * \param size Pointer to uint32_t to hold the size of the properties |
| 296 | * \param filter Filter ID and options (the size of the properties may |
| 297 | * vary depending on the options) |
| 298 | * |
| 299 | * \return - LZMA_OK |
| 300 | * - LZMA_OPTIONS_ERROR |
| 301 | * - LZMA_PROG_ERROR |
| 302 | * |
| 303 | * \note This function validates the Filter ID, but does not |
| 304 | * necessarily validate the options. Thus, it is possible |
| 305 | * that this returns LZMA_OK while the following call to |
| 306 | * lzma_properties_encode() returns LZMA_OPTIONS_ERROR. |
| 307 | */ |
| 308 | extern LZMA_API(lzma_ret) lzma_properties_size( |
| 309 | uint32_t *size, const lzma_filter *filter) lzma_nothrow; |
| 310 | |
| 311 | |
| 312 | /** |
| 313 | * \brief Encode the Filter Properties field |
| 314 | * |
| 315 | * \param filter Filter ID and options |
| 316 | * \param props Buffer to hold the encoded options. The size of |
| 317 | * buffer must have been already determined with |
| 318 | * lzma_properties_size(). |
| 319 | * |
| 320 | * \return - LZMA_OK |
| 321 | * - LZMA_OPTIONS_ERROR |
| 322 | * - LZMA_PROG_ERROR |
| 323 | * |
| 324 | * \note Even this function won't validate more options than actually |
| 325 | * necessary. Thus, it is possible that encoding the properties |
| 326 | * succeeds but using the same options to initialize the encoder |
| 327 | * will fail. |
| 328 | * |
| 329 | * \note If lzma_properties_size() indicated that the size |
| 330 | * of the Filter Properties field is zero, calling |
| 331 | * lzma_properties_encode() is not required, but it |
| 332 | * won't do any harm either. |
| 333 | */ |
| 334 | extern LZMA_API(lzma_ret) lzma_properties_encode( |
| 335 | const lzma_filter *filter, uint8_t *props) lzma_nothrow; |
| 336 | |
| 337 | |
| 338 | /** |
| 339 | * \brief Decode the Filter Properties field |
| 340 | * |
| 341 | * \param filter filter->id must have been set to the correct |
| 342 | * Filter ID. filter->options doesn't need to be |
| 343 | * initialized (it's not freed by this function). The |
| 344 | * decoded options will be stored to filter->options. |
| 345 | * filter->options is set to NULL if there are no |
| 346 | * properties or if an error occurs. |
| 347 | * \param allocator Custom memory allocator used to allocate the |
| 348 | * options. Set to NULL to use the default malloc(), |
| 349 | * and in case of an error, also free(). |
| 350 | * \param props Input buffer containing the properties. |
| 351 | * \param props_size Size of the properties. This must be the exact |
| 352 | * size; giving too much or too little input will |
| 353 | * return LZMA_OPTIONS_ERROR. |
| 354 | * |
| 355 | * \return - LZMA_OK |
| 356 | * - LZMA_OPTIONS_ERROR |
| 357 | * - LZMA_MEM_ERROR |
| 358 | */ |
| 359 | extern LZMA_API(lzma_ret) lzma_properties_decode( |
| 360 | lzma_filter *filter, const lzma_allocator *allocator, |
| 361 | const uint8_t *props, size_t props_size) lzma_nothrow; |
| 362 | |
| 363 | |
| 364 | /** |
| 365 | * \brief Calculate encoded size of a Filter Flags field |
| 366 | * |
| 367 | * Knowing the size of Filter Flags is useful to know when allocating |
| 368 | * memory to hold the encoded Filter Flags. |
| 369 | * |
| 370 | * \param size Pointer to integer to hold the calculated size |
| 371 | * \param filter Filter ID and associated options whose encoded |
| 372 | * size is to be calculated |
| 373 | * |
| 374 | * \return - LZMA_OK: *size set successfully. Note that this doesn't |
| 375 | * guarantee that filter->options is valid, thus |
| 376 | * lzma_filter_flags_encode() may still fail. |
| 377 | * - LZMA_OPTIONS_ERROR: Unknown Filter ID or unsupported options. |
| 378 | * - LZMA_PROG_ERROR: Invalid options |
| 379 | * |
| 380 | * \note If you need to calculate size of List of Filter Flags, |
| 381 | * you need to loop over every lzma_filter entry. |
| 382 | */ |
| 383 | extern LZMA_API(lzma_ret) lzma_filter_flags_size( |
| 384 | uint32_t *size, const lzma_filter *filter) |
| 385 | lzma_nothrow lzma_attr_warn_unused_result; |
| 386 | |
| 387 | |
| 388 | /** |
| 389 | * \brief Encode Filter Flags into given buffer |
| 390 | * |
| 391 | * In contrast to some functions, this doesn't allocate the needed buffer. |
| 392 | * This is due to how this function is used internally by liblzma. |
| 393 | * |
| 394 | * \param filter Filter ID and options to be encoded |
| 395 | * \param out Beginning of the output buffer |
| 396 | * \param out_pos out[*out_pos] is the next write position. This |
| 397 | * is updated by the encoder. |
| 398 | * \param out_size out[out_size] is the first byte to not write. |
| 399 | * |
| 400 | * \return - LZMA_OK: Encoding was successful. |
| 401 | * - LZMA_OPTIONS_ERROR: Invalid or unsupported options. |
| 402 | * - LZMA_PROG_ERROR: Invalid options or not enough output |
| 403 | * buffer space (you should have checked it with |
| 404 | * lzma_filter_flags_size()). |
| 405 | */ |
| 406 | extern LZMA_API(lzma_ret) lzma_filter_flags_encode(const lzma_filter *filter, |
| 407 | uint8_t *out, size_t *out_pos, size_t out_size) |
| 408 | lzma_nothrow lzma_attr_warn_unused_result; |
| 409 | |
| 410 | |
| 411 | /** |
| 412 | * \brief Decode Filter Flags from given buffer |
| 413 | * |
| 414 | * The decoded result is stored into *filter. The old value of |
| 415 | * filter->options is not free()d. |
| 416 | * |
| 417 | * \return - LZMA_OK |
| 418 | * - LZMA_OPTIONS_ERROR |
| 419 | * - LZMA_MEM_ERROR |
| 420 | * - LZMA_PROG_ERROR |
| 421 | */ |
| 422 | extern LZMA_API(lzma_ret) lzma_filter_flags_decode( |
| 423 | lzma_filter *filter, const lzma_allocator *allocator, |
| 424 | const uint8_t *in, size_t *in_pos, size_t in_size) |
| 425 | lzma_nothrow lzma_attr_warn_unused_result; |
| 426 | |