container.h revision 213700
1239909Shselasky/** 2239909Shselasky * \file lzma/container.h 3239909Shselasky * \brief File formats 4239909Shselasky */ 5239909Shselasky 6239909Shselasky/* 7239909Shselasky * Author: Lasse Collin 8239909Shselasky * 9239909Shselasky * This file has been put into the public domain. 10239909Shselasky * You can do whatever you want with this file. 11239909Shselasky * 12239909Shselasky * See ../lzma.h for information about liblzma as a whole. 13239909Shselasky */ 14239909Shselasky 15239909Shselasky#ifndef LZMA_H_INTERNAL 16239909Shselasky# error Never include this file directly. Use <lzma.h> instead. 17239909Shselasky#endif 18239909Shselasky 19239909Shselasky 20239909Shselasky/************ 21239909Shselasky * Encoding * 22239909Shselasky ************/ 23239909Shselasky 24239909Shselasky/** 25239909Shselasky * \brief Default compression preset 26239909Shselasky * 27239909Shselasky * It's not straightforward to recommend a default preset, because in some 28239909Shselasky * cases keeping the resource usage relatively low is more important that 29239909Shselasky * getting the maximum compression ratio. 30239909Shselasky */ 31239909Shselasky#define LZMA_PRESET_DEFAULT UINT32_C(6) 32239909Shselasky 33239909Shselasky 34239909Shselasky/** 35239909Shselasky * \brief Mask for preset level 36239909Shselasky * 37239909Shselasky * This is useful only if you need to extract the level from the preset 38239909Shselasky * variable. That should be rare. 39239909Shselasky */ 40239909Shselasky#define LZMA_PRESET_LEVEL_MASK UINT32_C(0x1F) 41239909Shselasky 42239909Shselasky 43239909Shselasky/* 44239909Shselasky * Preset flags 45239909Shselasky * 46239909Shselasky * Currently only one flag is defined. 47239909Shselasky */ 48239909Shselasky 49239909Shselasky/** 50239909Shselasky * \brief Extreme compression preset 51239909Shselasky * 52239909Shselasky * This flag modifies the preset to make the encoding significantly slower 53239909Shselasky * while improving the compression ratio only marginally. This is useful 54239909Shselasky * when you don't mind wasting time to get as small result as possible. 55239909Shselasky * 56239909Shselasky * This flag doesn't affect the memory usage requirements of the decoder (at 57239909Shselasky * least not significantly). The memory usage of the encoder may be increased 58239909Shselasky * a little but only at the lowest preset levels (0-2). 59239909Shselasky */ 60239909Shselasky#define LZMA_PRESET_EXTREME (UINT32_C(1) << 31) 61239909Shselasky 62239909Shselasky 63239909Shselasky/** 64239909Shselasky * \brief Calculate rough memory usage of easy encoder 65239909Shselasky * 66239909Shselasky * This function is a wrapper for lzma_raw_encoder_memusage(). 67239909Shselasky * 68239909Shselasky * \param preset Compression preset (level and possible flags) 69239909Shselasky */ 70239909Shselaskyextern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset) 71239909Shselasky lzma_nothrow lzma_attr_pure; 72239909Shselasky 73239909Shselasky 74239909Shselasky/** 75239909Shselasky * \brief Calculate rough decoder memory usage of a preset 76239909Shselasky * 77239909Shselasky * This function is a wrapper for lzma_raw_decoder_memusage(). 78239909Shselasky * 79239909Shselasky * \param preset Compression preset (level and possible flags) 80239909Shselasky */ 81239909Shselaskyextern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset) 82239909Shselasky lzma_nothrow lzma_attr_pure; 83239909Shselasky 84239909Shselasky 85239909Shselasky/** 86239909Shselasky * \brief Initialize .xz Stream encoder using a preset number 87239909Shselasky * 88239909Shselasky * This function is intended for those who just want to use the basic features 89239909Shselasky * if liblzma (that is, most developers out there). 90239909Shselasky * 91239909Shselasky * \param strm Pointer to lzma_stream that is at least initialized 92239909Shselasky * with LZMA_STREAM_INIT. 93239909Shselasky * \param preset Compression preset to use. A preset consist of level 94239909Shselasky * number and zero or more flags. Usually flags aren't 95239909Shselasky * used, so preset is simply a number [0, 9] which match 96239909Shselasky * the options -0 .. -9 of the xz command line tool. 97239909Shselasky * Additional flags can be be set using bitwise-or with 98239909Shselasky * the preset level number, e.g. 6 | LZMA_PRESET_EXTREME. 99239909Shselasky * \param check Integrity check type to use. See check.h for available 100239909Shselasky * checks. If you are unsure, use LZMA_CHECK_CRC32. 101239909Shselasky * 102239909Shselasky * \return - LZMA_OK: Initialization succeeded. Use lzma_code() to 103239909Shselasky * encode your data. 104239909Shselasky * - LZMA_MEM_ERROR: Memory allocation failed. 105239909Shselasky * - LZMA_OPTIONS_ERROR: The given compression level is not 106239909Shselasky * supported by this build of liblzma. 107239909Shselasky * - LZMA_UNSUPPORTED_CHECK: The given check type is not 108239909Shselasky * supported by this liblzma build. 109239909Shselasky * - LZMA_PROG_ERROR: One or more of the parameters have values 110239909Shselasky * that will never be valid. For example, strm == NULL. 111239909Shselasky * 112239909Shselasky * If initialization fails (return value is not LZMA_OK), all the memory 113239909Shselasky * allocated for *strm by liblzma is always freed. Thus, there is no need 114239909Shselasky * to call lzma_end() after failed initialization. 115239909Shselasky * 116239909Shselasky * If initialization succeeds, use lzma_code() to do the actual encoding. 117239909Shselasky * Valid values for `action' (the second argument of lzma_code()) are 118239909Shselasky * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future, 119239909Shselasky * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH. 120239909Shselasky */ 121239909Shselaskyextern LZMA_API(lzma_ret) lzma_easy_encoder( 122239909Shselasky lzma_stream *strm, uint32_t preset, lzma_check check) 123239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 124239909Shselasky 125239909Shselasky 126239909Shselasky/** 127239909Shselasky * \brief Single-call .xz Stream encoding using a preset number 128239909Shselasky * 129239909Shselasky * The maximum required output buffer size can be calculated with 130239909Shselasky * lzma_stream_buffer_bound(). 131239909Shselasky * 132239909Shselasky * \param preset Compression preset to use. See the description 133239909Shselasky * in lzma_easy_encoder(). 134239909Shselasky * \param check Type of the integrity check to calculate from 135239909Shselasky * uncompressed data. 136239909Shselasky * \param allocator lzma_allocator for custom allocator functions. 137239909Shselasky * Set to NULL to use malloc() and free(). 138239909Shselasky * \param in Beginning of the input buffer 139239909Shselasky * \param in_size Size of the input buffer 140239909Shselasky * \param out Beginning of the output buffer 141239909Shselasky * \param out_pos The next byte will be written to out[*out_pos]. 142239909Shselasky * *out_pos is updated only if encoding succeeds. 143239909Shselasky * \param out_size Size of the out buffer; the first byte into 144239909Shselasky * which no data is written to is out[out_size]. 145239909Shselasky * 146239909Shselasky * \return - LZMA_OK: Encoding was successful. 147239909Shselasky * - LZMA_BUF_ERROR: Not enough output buffer space. 148239909Shselasky * - LZMA_OPTIONS_ERROR 149239909Shselasky * - LZMA_MEM_ERROR 150239909Shselasky * - LZMA_DATA_ERROR 151239909Shselasky * - LZMA_PROG_ERROR 152239909Shselasky */ 153239909Shselaskyextern LZMA_API(lzma_ret) lzma_easy_buffer_encode( 154239909Shselasky uint32_t preset, lzma_check check, 155239909Shselasky lzma_allocator *allocator, const uint8_t *in, size_t in_size, 156239909Shselasky uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; 157239909Shselasky 158239909Shselasky 159239909Shselasky/** 160239909Shselasky * \brief Initialize .xz Stream encoder using a custom filter chain 161239909Shselasky * 162239909Shselasky * \param strm Pointer to properly prepared lzma_stream 163239909Shselasky * \param filters Array of filters. This must be terminated with 164239909Shselasky * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for 165239909Shselasky * more information. 166239909Shselasky * \param check Type of the integrity check to calculate from 167239909Shselasky * uncompressed data. 168239909Shselasky * 169239909Shselasky * \return - LZMA_OK: Initialization was successful. 170239909Shselasky * - LZMA_MEM_ERROR 171239909Shselasky * - LZMA_OPTIONS_ERROR 172239909Shselasky * - LZMA_PROG_ERROR 173239909Shselasky */ 174239909Shselaskyextern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm, 175239909Shselasky const lzma_filter *filters, lzma_check check) 176239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 177239909Shselasky 178239909Shselasky 179239909Shselasky/** 180239909Shselasky * \brief Initialize .lzma encoder (legacy file format) 181239909Shselasky * 182239909Shselasky * The .lzma format is sometimes called the LZMA_Alone format, which is the 183239909Shselasky * reason for the name of this function. The .lzma format supports only the 184239909Shselasky * LZMA1 filter. There is no support for integrity checks like CRC32. 185239909Shselasky * 186239909Shselasky * Use this function if and only if you need to create files readable by 187239909Shselasky * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format 188239909Shselasky * is strongly recommended. 189239909Shselasky * 190239909Shselasky * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH. 191239909Shselasky * No kind of flushing is supported, because the file format doesn't make 192239909Shselasky * it possible. 193239909Shselasky * 194239909Shselasky * \return - LZMA_OK 195239909Shselasky * - LZMA_MEM_ERROR 196239909Shselasky * - LZMA_OPTIONS_ERROR 197239909Shselasky * - LZMA_PROG_ERROR 198239909Shselasky */ 199239909Shselaskyextern LZMA_API(lzma_ret) lzma_alone_encoder( 200239909Shselasky lzma_stream *strm, const lzma_options_lzma *options) 201239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 202239909Shselasky 203239909Shselasky 204239909Shselasky/** 205239909Shselasky * \brief Calculate output buffer size for single-call Stream encoder 206239909Shselasky * 207239909Shselasky * When trying to compress uncompressible data, the encoded size will be 208239909Shselasky * slightly bigger than the input data. This function calculates how much 209239909Shselasky * output buffer space is required to be sure that lzma_stream_buffer_encode() 210239909Shselasky * doesn't return LZMA_BUF_ERROR. 211239909Shselasky * 212239909Shselasky * The calculated value is not exact, but it is guaranteed to be big enough. 213239909Shselasky * The actual maximum output space required may be slightly smaller (up to 214239909Shselasky * about 100 bytes). This should not be a problem in practice. 215239909Shselasky * 216239909Shselasky * If the calculated maximum size doesn't fit into size_t or would make the 217239909Shselasky * Stream grow past LZMA_VLI_MAX (which should never happen in practice), 218239909Shselasky * zero is returned to indicate the error. 219239909Shselasky * 220239909Shselasky * \note The limit calculated by this function applies only to 221239909Shselasky * single-call encoding. Multi-call encoding may (and probably 222239909Shselasky * will) have larger maximum expansion when encoding 223239909Shselasky * uncompressible data. Currently there is no function to 224239909Shselasky * calculate the maximum expansion of multi-call encoding. 225239909Shselasky */ 226239909Shselaskyextern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size) 227239909Shselasky lzma_nothrow; 228239909Shselasky 229239909Shselasky 230239909Shselasky/** 231239909Shselasky * \brief Single-call .xz Stream encoder 232239909Shselasky * 233239909Shselasky * \param filters Array of filters. This must be terminated with 234239909Shselasky * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h 235239909Shselasky * for more information. 236239909Shselasky * \param check Type of the integrity check to calculate from 237239909Shselasky * uncompressed data. 238239909Shselasky * \param allocator lzma_allocator for custom allocator functions. 239239909Shselasky * Set to NULL to use malloc() and free(). 240239909Shselasky * \param in Beginning of the input buffer 241239909Shselasky * \param in_size Size of the input buffer 242239909Shselasky * \param out Beginning of the output buffer 243239909Shselasky * \param out_pos The next byte will be written to out[*out_pos]. 244239909Shselasky * *out_pos is updated only if encoding succeeds. 245239909Shselasky * \param out_size Size of the out buffer; the first byte into 246239909Shselasky * which no data is written to is out[out_size]. 247239909Shselasky * 248239909Shselasky * \return - LZMA_OK: Encoding was successful. 249239909Shselasky * - LZMA_BUF_ERROR: Not enough output buffer space. 250239909Shselasky * - LZMA_OPTIONS_ERROR 251239909Shselasky * - LZMA_MEM_ERROR 252239909Shselasky * - LZMA_DATA_ERROR 253239909Shselasky * - LZMA_PROG_ERROR 254239909Shselasky */ 255239909Shselaskyextern LZMA_API(lzma_ret) lzma_stream_buffer_encode( 256239909Shselasky lzma_filter *filters, lzma_check check, 257239909Shselasky lzma_allocator *allocator, const uint8_t *in, size_t in_size, 258239909Shselasky uint8_t *out, size_t *out_pos, size_t out_size) 259239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 260239909Shselasky 261239909Shselasky 262239909Shselasky/************ 263239909Shselasky * Decoding * 264239909Shselasky ************/ 265239909Shselasky 266239909Shselasky/** 267239909Shselasky * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream 268239909Shselasky * being decoded has no integrity check. Note that when used with 269239909Shselasky * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK 270239909Shselasky * if LZMA_TELL_NO_CHECK is used. 271239909Shselasky */ 272239909Shselasky#define LZMA_TELL_NO_CHECK UINT32_C(0x01) 273239909Shselasky 274239909Shselasky 275239909Shselasky/** 276239909Shselasky * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input 277239909Shselasky * stream has an integrity check, but the type of the integrity check is not 278239909Shselasky * supported by this liblzma version or build. Such files can still be 279239909Shselasky * decoded, but the integrity check cannot be verified. 280239909Shselasky */ 281239909Shselasky#define LZMA_TELL_UNSUPPORTED_CHECK UINT32_C(0x02) 282239909Shselasky 283239909Shselasky 284239909Shselasky/** 285239909Shselasky * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type 286239909Shselasky * of the integrity check is known. The type can then be got with 287239909Shselasky * lzma_get_check(). 288239909Shselasky */ 289239909Shselasky#define LZMA_TELL_ANY_CHECK UINT32_C(0x04) 290239909Shselasky 291239909Shselasky 292239909Shselasky/** 293239909Shselasky * This flag enables decoding of concatenated files with file formats that 294239909Shselasky * allow concatenating compressed files as is. From the formats currently 295239909Shselasky * supported by liblzma, only the .xz format allows concatenated files. 296239909Shselasky * Concatenated files are not allowed with the legacy .lzma format. 297239909Shselasky * 298239909Shselasky * This flag also affects the usage of the `action' argument for lzma_code(). 299239909Shselasky * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END 300239909Shselasky * unless LZMA_FINISH is used as `action'. Thus, the application has to set 301239909Shselasky * LZMA_FINISH in the same way as it does when encoding. 302239909Shselasky * 303239909Shselasky * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH 304239909Shselasky * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required. 305239909Shselasky */ 306239909Shselasky#define LZMA_CONCATENATED UINT32_C(0x08) 307239909Shselasky 308239909Shselasky 309239909Shselasky/** 310239909Shselasky * \brief Initialize .xz Stream decoder 311239909Shselasky * 312239909Shselasky * \param strm Pointer to properly prepared lzma_stream 313239909Shselasky * \param memlimit Rough memory usage limit as bytes 314239909Shselasky * \param flags Bitwise-or of zero or more of the decoder flags: 315239909Shselasky * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, 316239909Shselasky * LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED 317239909Shselasky * 318239909Shselasky * \return - LZMA_OK: Initialization was successful. 319239909Shselasky * - LZMA_MEM_ERROR: Cannot allocate memory. 320239909Shselasky * - LZMA_OPTIONS_ERROR: Unsupported flags 321239909Shselasky */ 322239909Shselaskyextern LZMA_API(lzma_ret) lzma_stream_decoder( 323239909Shselasky lzma_stream *strm, uint64_t memlimit, uint32_t flags) 324239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 325239909Shselasky 326239909Shselasky 327239909Shselasky/** 328239909Shselasky * \brief Decode .xz Streams and .lzma files with autodetection 329239909Shselasky * 330239909Shselasky * This decoder autodetects between the .xz and .lzma file formats, and 331239909Shselasky * calls lzma_stream_decoder() or lzma_alone_decoder() once the type 332239909Shselasky * of the input file has been detected. 333239909Shselasky * 334239909Shselasky * \param strm Pointer to properly prepared lzma_stream 335239909Shselasky * \param memlimit Rough memory usage limit as bytes 336239909Shselasky * \param flags Bitwise-or of flags, or zero for no flags. 337239909Shselasky * 338239909Shselasky * \return - LZMA_OK: Initialization was successful. 339239909Shselasky * - LZMA_MEM_ERROR: Cannot allocate memory. 340239909Shselasky * - LZMA_OPTIONS_ERROR: Unsupported flags 341239909Shselasky */ 342239909Shselaskyextern LZMA_API(lzma_ret) lzma_auto_decoder( 343239909Shselasky lzma_stream *strm, uint64_t memlimit, uint32_t flags) 344239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 345239909Shselasky 346239909Shselasky 347239909Shselasky/** 348239909Shselasky * \brief Initialize .lzma decoder (legacy file format) 349239909Shselasky * 350239909Shselasky * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. 351239909Shselasky * There is no need to use LZMA_FINISH, but allowing it may simplify 352239909Shselasky * certain types of applications. 353239909Shselasky * 354239909Shselasky * \return - LZMA_OK 355239909Shselasky * - LZMA_MEM_ERROR 356239909Shselasky */ 357239909Shselaskyextern LZMA_API(lzma_ret) lzma_alone_decoder( 358239909Shselasky lzma_stream *strm, uint64_t memlimit) 359239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 360239909Shselasky 361239909Shselasky 362239909Shselasky/** 363239909Shselasky * \brief Single-call .xz Stream decoder 364239909Shselasky * 365239909Shselasky * \param memlimit Pointer to how much memory the decoder is allowed 366239909Shselasky * to allocate. The value pointed by this pointer is 367239909Shselasky * modified if and only if LZMA_MEMLIMIT_ERROR is 368239909Shselasky * returned. 369239909Shselasky * \param flags Bitwise-or of zero or more of the decoder flags: 370239909Shselasky * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, 371239909Shselasky * LZMA_CONCATENATED. Note that LZMA_TELL_ANY_CHECK 372239909Shselasky * is not allowed and will return LZMA_PROG_ERROR. 373239909Shselasky * \param allocator lzma_allocator for custom allocator functions. 374239909Shselasky * Set to NULL to use malloc() and free(). 375239909Shselasky * \param in Beginning of the input buffer 376239909Shselasky * \param in_pos The next byte will be read from in[*in_pos]. 377239909Shselasky * *in_pos is updated only if decoding succeeds. 378239909Shselasky * \param in_size Size of the input buffer; the first byte that 379239909Shselasky * won't be read is in[in_size]. 380239909Shselasky * \param out Beginning of the output buffer 381239909Shselasky * \param out_pos The next byte will be written to out[*out_pos]. 382239909Shselasky * *out_pos is updated only if encoding succeeds. 383239909Shselasky * \param out_size Size of the out buffer; the first byte into 384239909Shselasky * which no data is written to is out[out_size]. 385239909Shselasky * 386239909Shselasky * \return - LZMA_OK: Decoding was successful. 387239909Shselasky * - LZMA_FORMAT_ERROR 388239909Shselasky * - LZMA_OPTIONS_ERROR 389239909Shselasky * - LZMA_DATA_ERROR 390239909Shselasky * - LZMA_NO_CHECK: This can be returned only if using 391239909Shselasky * the LZMA_TELL_NO_CHECK flag. 392239909Shselasky * - LZMA_UNSUPPORTED_CHECK: This can be returned only if using 393239909Shselasky * the LZMA_TELL_UNSUPPORTED_CHECK flag. 394239909Shselasky * - LZMA_MEM_ERROR 395239909Shselasky * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. 396239909Shselasky * The minimum required memlimit value was stored to *memlimit. 397239909Shselasky * - LZMA_BUF_ERROR: Output buffer was too small. 398239909Shselasky * - LZMA_PROG_ERROR 399239909Shselasky */ 400239909Shselaskyextern LZMA_API(lzma_ret) lzma_stream_buffer_decode( 401239909Shselasky uint64_t *memlimit, uint32_t flags, lzma_allocator *allocator, 402239909Shselasky const uint8_t *in, size_t *in_pos, size_t in_size, 403239909Shselasky uint8_t *out, size_t *out_pos, size_t out_size) 404239909Shselasky lzma_nothrow lzma_attr_warn_unused_result; 405239909Shselasky