15a645f22SBen Gras /** 25a645f22SBen Gras * \file lzma/container.h 35a645f22SBen Gras * \brief File formats 45a645f22SBen Gras */ 55a645f22SBen Gras 65a645f22SBen Gras /* 75a645f22SBen Gras * Author: Lasse Collin 85a645f22SBen Gras * 95a645f22SBen Gras * This file has been put into the public domain. 105a645f22SBen Gras * You can do whatever you want with this file. 115a645f22SBen Gras * 125a645f22SBen Gras * See ../lzma.h for information about liblzma as a whole. 135a645f22SBen Gras */ 145a645f22SBen Gras 155a645f22SBen Gras #ifndef LZMA_H_INTERNAL 165a645f22SBen Gras # error Never include this file directly. Use <lzma.h> instead. 175a645f22SBen Gras #endif 185a645f22SBen Gras 195a645f22SBen Gras 205a645f22SBen Gras /************ 215a645f22SBen Gras * Encoding * 225a645f22SBen Gras ************/ 235a645f22SBen Gras 245a645f22SBen Gras /** 255a645f22SBen Gras * \brief Default compression preset 265a645f22SBen Gras * 275a645f22SBen Gras * It's not straightforward to recommend a default preset, because in some 285a645f22SBen Gras * cases keeping the resource usage relatively low is more important that 295a645f22SBen Gras * getting the maximum compression ratio. 305a645f22SBen Gras */ 315a645f22SBen Gras #define LZMA_PRESET_DEFAULT UINT32_C(6) 325a645f22SBen Gras 335a645f22SBen Gras 345a645f22SBen Gras /** 355a645f22SBen Gras * \brief Mask for preset level 365a645f22SBen Gras * 375a645f22SBen Gras * This is useful only if you need to extract the level from the preset 385a645f22SBen Gras * variable. That should be rare. 395a645f22SBen Gras */ 405a645f22SBen Gras #define LZMA_PRESET_LEVEL_MASK UINT32_C(0x1F) 415a645f22SBen Gras 425a645f22SBen Gras 435a645f22SBen Gras /* 445a645f22SBen Gras * Preset flags 455a645f22SBen Gras * 465a645f22SBen Gras * Currently only one flag is defined. 475a645f22SBen Gras */ 485a645f22SBen Gras 495a645f22SBen Gras /** 505a645f22SBen Gras * \brief Extreme compression preset 515a645f22SBen Gras * 525a645f22SBen Gras * This flag modifies the preset to make the encoding significantly slower 535a645f22SBen Gras * while improving the compression ratio only marginally. This is useful 545a645f22SBen Gras * when you don't mind wasting time to get as small result as possible. 555a645f22SBen Gras * 565a645f22SBen Gras * This flag doesn't affect the memory usage requirements of the decoder (at 575a645f22SBen Gras * least not significantly). The memory usage of the encoder may be increased 585a645f22SBen Gras * a little but only at the lowest preset levels (0-3). 595a645f22SBen Gras */ 605a645f22SBen Gras #define LZMA_PRESET_EXTREME (UINT32_C(1) << 31) 615a645f22SBen Gras 625a645f22SBen Gras 635a645f22SBen Gras /** 64*0a6a1f1dSLionel Sambuc * \brief Multithreading options 65*0a6a1f1dSLionel Sambuc */ 66*0a6a1f1dSLionel Sambuc typedef struct { 67*0a6a1f1dSLionel Sambuc /** 68*0a6a1f1dSLionel Sambuc * \brief Flags 69*0a6a1f1dSLionel Sambuc * 70*0a6a1f1dSLionel Sambuc * Set this to zero if no flags are wanted. 71*0a6a1f1dSLionel Sambuc * 72*0a6a1f1dSLionel Sambuc * No flags are currently supported. 73*0a6a1f1dSLionel Sambuc */ 74*0a6a1f1dSLionel Sambuc uint32_t flags; 75*0a6a1f1dSLionel Sambuc 76*0a6a1f1dSLionel Sambuc /** 77*0a6a1f1dSLionel Sambuc * \brief Number of worker threads to use 78*0a6a1f1dSLionel Sambuc */ 79*0a6a1f1dSLionel Sambuc uint32_t threads; 80*0a6a1f1dSLionel Sambuc 81*0a6a1f1dSLionel Sambuc /** 82*0a6a1f1dSLionel Sambuc * \brief Maximum uncompressed size of a Block 83*0a6a1f1dSLionel Sambuc * 84*0a6a1f1dSLionel Sambuc * The encoder will start a new .xz Block every block_size bytes. 85*0a6a1f1dSLionel Sambuc * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code() 86*0a6a1f1dSLionel Sambuc * the caller may tell liblzma to start a new Block earlier. 87*0a6a1f1dSLionel Sambuc * 88*0a6a1f1dSLionel Sambuc * With LZMA2, a recommended block size is 2-4 times the LZMA2 89*0a6a1f1dSLionel Sambuc * dictionary size. With very small dictionaries, it is recommended 90*0a6a1f1dSLionel Sambuc * to use at least 1 MiB block size for good compression ratio, even 91*0a6a1f1dSLionel Sambuc * if this is more than four times the dictionary size. Note that 92*0a6a1f1dSLionel Sambuc * these are only recommendations for typical use cases; feel free 93*0a6a1f1dSLionel Sambuc * to use other values. Just keep in mind that using a block size 94*0a6a1f1dSLionel Sambuc * less than the LZMA2 dictionary size is waste of RAM. 95*0a6a1f1dSLionel Sambuc * 96*0a6a1f1dSLionel Sambuc * Set this to 0 to let liblzma choose the block size depending 97*0a6a1f1dSLionel Sambuc * on the compression options. For LZMA2 it will be 3*dict_size 98*0a6a1f1dSLionel Sambuc * or 1 MiB, whichever is more. 99*0a6a1f1dSLionel Sambuc * 100*0a6a1f1dSLionel Sambuc * For each thread, about 3 * block_size bytes of memory will be 101*0a6a1f1dSLionel Sambuc * allocated. This may change in later liblzma versions. If so, 102*0a6a1f1dSLionel Sambuc * the memory usage will probably be reduced, not increased. 103*0a6a1f1dSLionel Sambuc */ 104*0a6a1f1dSLionel Sambuc uint64_t block_size; 105*0a6a1f1dSLionel Sambuc 106*0a6a1f1dSLionel Sambuc /** 107*0a6a1f1dSLionel Sambuc * \brief Timeout to allow lzma_code() to return early 108*0a6a1f1dSLionel Sambuc * 109*0a6a1f1dSLionel Sambuc * Multithreading can make liblzma to consume input and produce 110*0a6a1f1dSLionel Sambuc * output in a very bursty way: it may first read a lot of input 111*0a6a1f1dSLionel Sambuc * to fill internal buffers, then no input or output occurs for 112*0a6a1f1dSLionel Sambuc * a while. 113*0a6a1f1dSLionel Sambuc * 114*0a6a1f1dSLionel Sambuc * In single-threaded mode, lzma_code() won't return until it has 115*0a6a1f1dSLionel Sambuc * either consumed all the input or filled the output buffer. If 116*0a6a1f1dSLionel Sambuc * this is done in multithreaded mode, it may cause a call 117*0a6a1f1dSLionel Sambuc * lzma_code() to take even tens of seconds, which isn't acceptable 118*0a6a1f1dSLionel Sambuc * in all applications. 119*0a6a1f1dSLionel Sambuc * 120*0a6a1f1dSLionel Sambuc * To avoid very long blocking times in lzma_code(), a timeout 121*0a6a1f1dSLionel Sambuc * (in milliseconds) may be set here. If lzma_code() would block 122*0a6a1f1dSLionel Sambuc * longer than this number of milliseconds, it will return with 123*0a6a1f1dSLionel Sambuc * LZMA_OK. Reasonable values are 100 ms or more. The xz command 124*0a6a1f1dSLionel Sambuc * line tool uses 300 ms. 125*0a6a1f1dSLionel Sambuc * 126*0a6a1f1dSLionel Sambuc * If long blocking times are fine for you, set timeout to a special 127*0a6a1f1dSLionel Sambuc * value of 0, which will disable the timeout mechanism and will make 128*0a6a1f1dSLionel Sambuc * lzma_code() block until all the input is consumed or the output 129*0a6a1f1dSLionel Sambuc * buffer has been filled. 130*0a6a1f1dSLionel Sambuc * 131*0a6a1f1dSLionel Sambuc * \note Even with a timeout, lzma_code() might sometimes take 132*0a6a1f1dSLionel Sambuc * somewhat long time to return. No timing guarantees 133*0a6a1f1dSLionel Sambuc * are made. 134*0a6a1f1dSLionel Sambuc */ 135*0a6a1f1dSLionel Sambuc uint32_t timeout; 136*0a6a1f1dSLionel Sambuc 137*0a6a1f1dSLionel Sambuc /** 138*0a6a1f1dSLionel Sambuc * \brief Compression preset (level and possible flags) 139*0a6a1f1dSLionel Sambuc * 140*0a6a1f1dSLionel Sambuc * The preset is set just like with lzma_easy_encoder(). 141*0a6a1f1dSLionel Sambuc * The preset is ignored if filters below is non-NULL. 142*0a6a1f1dSLionel Sambuc */ 143*0a6a1f1dSLionel Sambuc uint32_t preset; 144*0a6a1f1dSLionel Sambuc 145*0a6a1f1dSLionel Sambuc /** 146*0a6a1f1dSLionel Sambuc * \brief Filter chain (alternative to a preset) 147*0a6a1f1dSLionel Sambuc * 148*0a6a1f1dSLionel Sambuc * If this is NULL, the preset above is used. Otherwise the preset 149*0a6a1f1dSLionel Sambuc * is ignored and the filter chain specified here is used. 150*0a6a1f1dSLionel Sambuc */ 151*0a6a1f1dSLionel Sambuc const lzma_filter *filters; 152*0a6a1f1dSLionel Sambuc 153*0a6a1f1dSLionel Sambuc /** 154*0a6a1f1dSLionel Sambuc * \brief Integrity check type 155*0a6a1f1dSLionel Sambuc * 156*0a6a1f1dSLionel Sambuc * See check.h for available checks. The xz command line tool 157*0a6a1f1dSLionel Sambuc * defaults to LZMA_CHECK_CRC64, which is a good choice if you 158*0a6a1f1dSLionel Sambuc * are unsure. 159*0a6a1f1dSLionel Sambuc */ 160*0a6a1f1dSLionel Sambuc lzma_check check; 161*0a6a1f1dSLionel Sambuc 162*0a6a1f1dSLionel Sambuc /* 163*0a6a1f1dSLionel Sambuc * Reserved space to allow possible future extensions without 164*0a6a1f1dSLionel Sambuc * breaking the ABI. You should not touch these, because the names 165*0a6a1f1dSLionel Sambuc * of these variables may change. These are and will never be used 166*0a6a1f1dSLionel Sambuc * with the currently supported options, so it is safe to leave these 167*0a6a1f1dSLionel Sambuc * uninitialized. 168*0a6a1f1dSLionel Sambuc */ 169*0a6a1f1dSLionel Sambuc lzma_reserved_enum reserved_enum1; 170*0a6a1f1dSLionel Sambuc lzma_reserved_enum reserved_enum2; 171*0a6a1f1dSLionel Sambuc lzma_reserved_enum reserved_enum3; 172*0a6a1f1dSLionel Sambuc uint32_t reserved_int1; 173*0a6a1f1dSLionel Sambuc uint32_t reserved_int2; 174*0a6a1f1dSLionel Sambuc uint32_t reserved_int3; 175*0a6a1f1dSLionel Sambuc uint32_t reserved_int4; 176*0a6a1f1dSLionel Sambuc uint64_t reserved_int5; 177*0a6a1f1dSLionel Sambuc uint64_t reserved_int6; 178*0a6a1f1dSLionel Sambuc uint64_t reserved_int7; 179*0a6a1f1dSLionel Sambuc uint64_t reserved_int8; 180*0a6a1f1dSLionel Sambuc void *reserved_ptr1; 181*0a6a1f1dSLionel Sambuc void *reserved_ptr2; 182*0a6a1f1dSLionel Sambuc void *reserved_ptr3; 183*0a6a1f1dSLionel Sambuc void *reserved_ptr4; 184*0a6a1f1dSLionel Sambuc 185*0a6a1f1dSLionel Sambuc } lzma_mt; 186*0a6a1f1dSLionel Sambuc 187*0a6a1f1dSLionel Sambuc 188*0a6a1f1dSLionel Sambuc /** 1895a645f22SBen Gras * \brief Calculate approximate memory usage of easy encoder 1905a645f22SBen Gras * 1915a645f22SBen Gras * This function is a wrapper for lzma_raw_encoder_memusage(). 1925a645f22SBen Gras * 1935a645f22SBen Gras * \param preset Compression preset (level and possible flags) 19411be35a1SLionel Sambuc * 19511be35a1SLionel Sambuc * \return Number of bytes of memory required for the given 19611be35a1SLionel Sambuc * preset when encoding. If an error occurs, for example 19711be35a1SLionel Sambuc * due to unsupported preset, UINT64_MAX is returned. 1985a645f22SBen Gras */ 1995a645f22SBen Gras extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset) 2005a645f22SBen Gras lzma_nothrow lzma_attr_pure; 2015a645f22SBen Gras 2025a645f22SBen Gras 2035a645f22SBen Gras /** 2045a645f22SBen Gras * \brief Calculate approximate decoder memory usage of a preset 2055a645f22SBen Gras * 2065a645f22SBen Gras * This function is a wrapper for lzma_raw_decoder_memusage(). 2075a645f22SBen Gras * 2085a645f22SBen Gras * \param preset Compression preset (level and possible flags) 20911be35a1SLionel Sambuc * 21011be35a1SLionel Sambuc * \return Number of bytes of memory required to decompress a file 21111be35a1SLionel Sambuc * that was compressed using the given preset. If an error 21211be35a1SLionel Sambuc * occurs, for example due to unsupported preset, UINT64_MAX 21311be35a1SLionel Sambuc * is returned. 2145a645f22SBen Gras */ 2155a645f22SBen Gras extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset) 2165a645f22SBen Gras lzma_nothrow lzma_attr_pure; 2175a645f22SBen Gras 2185a645f22SBen Gras 2195a645f22SBen Gras /** 2205a645f22SBen Gras * \brief Initialize .xz Stream encoder using a preset number 2215a645f22SBen Gras * 2225a645f22SBen Gras * This function is intended for those who just want to use the basic features 2235a645f22SBen Gras * if liblzma (that is, most developers out there). 2245a645f22SBen Gras * 2255a645f22SBen Gras * \param strm Pointer to lzma_stream that is at least initialized 2265a645f22SBen Gras * with LZMA_STREAM_INIT. 2275a645f22SBen Gras * \param preset Compression preset to use. A preset consist of level 2285a645f22SBen Gras * number and zero or more flags. Usually flags aren't 2295a645f22SBen Gras * used, so preset is simply a number [0, 9] which match 2305a645f22SBen Gras * the options -0 ... -9 of the xz command line tool. 2315a645f22SBen Gras * Additional flags can be be set using bitwise-or with 2325a645f22SBen Gras * the preset level number, e.g. 6 | LZMA_PRESET_EXTREME. 2335a645f22SBen Gras * \param check Integrity check type to use. See check.h for available 2345a645f22SBen Gras * checks. The xz command line tool defaults to 2355a645f22SBen Gras * LZMA_CHECK_CRC64, which is a good choice if you are 2365a645f22SBen Gras * unsure. LZMA_CHECK_CRC32 is good too as long as the 2375a645f22SBen Gras * uncompressed file is not many gigabytes. 2385a645f22SBen Gras * 2395a645f22SBen Gras * \return - LZMA_OK: Initialization succeeded. Use lzma_code() to 2405a645f22SBen Gras * encode your data. 2415a645f22SBen Gras * - LZMA_MEM_ERROR: Memory allocation failed. 2425a645f22SBen Gras * - LZMA_OPTIONS_ERROR: The given compression preset is not 2435a645f22SBen Gras * supported by this build of liblzma. 2445a645f22SBen Gras * - LZMA_UNSUPPORTED_CHECK: The given check type is not 2455a645f22SBen Gras * supported by this liblzma build. 2465a645f22SBen Gras * - LZMA_PROG_ERROR: One or more of the parameters have values 2475a645f22SBen Gras * that will never be valid. For example, strm == NULL. 2485a645f22SBen Gras * 2495a645f22SBen Gras * If initialization fails (return value is not LZMA_OK), all the memory 2505a645f22SBen Gras * allocated for *strm by liblzma is always freed. Thus, there is no need 2515a645f22SBen Gras * to call lzma_end() after failed initialization. 2525a645f22SBen Gras * 2535a645f22SBen Gras * If initialization succeeds, use lzma_code() to do the actual encoding. 2545a645f22SBen Gras * Valid values for `action' (the second argument of lzma_code()) are 2555a645f22SBen Gras * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future, 2565a645f22SBen Gras * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH. 2575a645f22SBen Gras */ 2585a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_easy_encoder( 2595a645f22SBen Gras lzma_stream *strm, uint32_t preset, lzma_check check) 2605a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 2615a645f22SBen Gras 2625a645f22SBen Gras 2635a645f22SBen Gras /** 2645a645f22SBen Gras * \brief Single-call .xz Stream encoding using a preset number 2655a645f22SBen Gras * 2665a645f22SBen Gras * The maximum required output buffer size can be calculated with 2675a645f22SBen Gras * lzma_stream_buffer_bound(). 2685a645f22SBen Gras * 2695a645f22SBen Gras * \param preset Compression preset to use. See the description 2705a645f22SBen Gras * in lzma_easy_encoder(). 2715a645f22SBen Gras * \param check Type of the integrity check to calculate from 2725a645f22SBen Gras * uncompressed data. 2735a645f22SBen Gras * \param allocator lzma_allocator for custom allocator functions. 2745a645f22SBen Gras * Set to NULL to use malloc() and free(). 2755a645f22SBen Gras * \param in Beginning of the input buffer 2765a645f22SBen Gras * \param in_size Size of the input buffer 2775a645f22SBen Gras * \param out Beginning of the output buffer 2785a645f22SBen Gras * \param out_pos The next byte will be written to out[*out_pos]. 2795a645f22SBen Gras * *out_pos is updated only if encoding succeeds. 2805a645f22SBen Gras * \param out_size Size of the out buffer; the first byte into 2815a645f22SBen Gras * which no data is written to is out[out_size]. 2825a645f22SBen Gras * 2835a645f22SBen Gras * \return - LZMA_OK: Encoding was successful. 2845a645f22SBen Gras * - LZMA_BUF_ERROR: Not enough output buffer space. 28511be35a1SLionel Sambuc * - LZMA_UNSUPPORTED_CHECK 2865a645f22SBen Gras * - LZMA_OPTIONS_ERROR 2875a645f22SBen Gras * - LZMA_MEM_ERROR 2885a645f22SBen Gras * - LZMA_DATA_ERROR 2895a645f22SBen Gras * - LZMA_PROG_ERROR 2905a645f22SBen Gras */ 2915a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_easy_buffer_encode( 2925a645f22SBen Gras uint32_t preset, lzma_check check, 293*0a6a1f1dSLionel Sambuc const lzma_allocator *allocator, 294*0a6a1f1dSLionel Sambuc const uint8_t *in, size_t in_size, 2955a645f22SBen Gras uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; 2965a645f22SBen Gras 2975a645f22SBen Gras 2985a645f22SBen Gras /** 2995a645f22SBen Gras * \brief Initialize .xz Stream encoder using a custom filter chain 3005a645f22SBen Gras * 3015a645f22SBen Gras * \param strm Pointer to properly prepared lzma_stream 3025a645f22SBen Gras * \param filters Array of filters. This must be terminated with 3035a645f22SBen Gras * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for 3045a645f22SBen Gras * more information. 3055a645f22SBen Gras * \param check Type of the integrity check to calculate from 3065a645f22SBen Gras * uncompressed data. 3075a645f22SBen Gras * 3085a645f22SBen Gras * \return - LZMA_OK: Initialization was successful. 3095a645f22SBen Gras * - LZMA_MEM_ERROR 31011be35a1SLionel Sambuc * - LZMA_UNSUPPORTED_CHECK 3115a645f22SBen Gras * - LZMA_OPTIONS_ERROR 3125a645f22SBen Gras * - LZMA_PROG_ERROR 3135a645f22SBen Gras */ 3145a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm, 3155a645f22SBen Gras const lzma_filter *filters, lzma_check check) 3165a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 3175a645f22SBen Gras 3185a645f22SBen Gras 3195a645f22SBen Gras /** 320*0a6a1f1dSLionel Sambuc * \brief Calculate approximate memory usage of multithreaded .xz encoder 321*0a6a1f1dSLionel Sambuc * 322*0a6a1f1dSLionel Sambuc * Since doing the encoding in threaded mode doesn't affect the memory 323*0a6a1f1dSLionel Sambuc * requirements of single-threaded decompressor, you can use 324*0a6a1f1dSLionel Sambuc * lzma_easy_decoder_memusage(options->preset) or 325*0a6a1f1dSLionel Sambuc * lzma_raw_decoder_memusage(options->filters) to calculate 326*0a6a1f1dSLionel Sambuc * the decompressor memory requirements. 327*0a6a1f1dSLionel Sambuc * 328*0a6a1f1dSLionel Sambuc * \param options Compression options 329*0a6a1f1dSLionel Sambuc * 330*0a6a1f1dSLionel Sambuc * \return Number of bytes of memory required for encoding with the 331*0a6a1f1dSLionel Sambuc * given options. If an error occurs, for example due to 332*0a6a1f1dSLionel Sambuc * unsupported preset or filter chain, UINT64_MAX is returned. 333*0a6a1f1dSLionel Sambuc */ 334*0a6a1f1dSLionel Sambuc extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage( 335*0a6a1f1dSLionel Sambuc const lzma_mt *options) lzma_nothrow lzma_attr_pure; 336*0a6a1f1dSLionel Sambuc 337*0a6a1f1dSLionel Sambuc 338*0a6a1f1dSLionel Sambuc /** 339*0a6a1f1dSLionel Sambuc * \brief Initialize multithreaded .xz Stream encoder 340*0a6a1f1dSLionel Sambuc * 341*0a6a1f1dSLionel Sambuc * This provides the functionality of lzma_easy_encoder() and 342*0a6a1f1dSLionel Sambuc * lzma_stream_encoder() as a single function for multithreaded use. 343*0a6a1f1dSLionel Sambuc * 344*0a6a1f1dSLionel Sambuc * The supported actions for lzma_code() are LZMA_RUN, LZMA_FULL_FLUSH, 345*0a6a1f1dSLionel Sambuc * LZMA_FULL_BARRIER, and LZMA_FINISH. Support for LZMA_SYNC_FLUSH might be 346*0a6a1f1dSLionel Sambuc * added in the future. 347*0a6a1f1dSLionel Sambuc * 348*0a6a1f1dSLionel Sambuc * \param strm Pointer to properly prepared lzma_stream 349*0a6a1f1dSLionel Sambuc * \param options Pointer to multithreaded compression options 350*0a6a1f1dSLionel Sambuc * 351*0a6a1f1dSLionel Sambuc * \return - LZMA_OK 352*0a6a1f1dSLionel Sambuc * - LZMA_MEM_ERROR 353*0a6a1f1dSLionel Sambuc * - LZMA_UNSUPPORTED_CHECK 354*0a6a1f1dSLionel Sambuc * - LZMA_OPTIONS_ERROR 355*0a6a1f1dSLionel Sambuc * - LZMA_PROG_ERROR 356*0a6a1f1dSLionel Sambuc */ 357*0a6a1f1dSLionel Sambuc extern LZMA_API(lzma_ret) lzma_stream_encoder_mt( 358*0a6a1f1dSLionel Sambuc lzma_stream *strm, const lzma_mt *options) 359*0a6a1f1dSLionel Sambuc lzma_nothrow lzma_attr_warn_unused_result; 360*0a6a1f1dSLionel Sambuc 361*0a6a1f1dSLionel Sambuc 362*0a6a1f1dSLionel Sambuc /** 3635a645f22SBen Gras * \brief Initialize .lzma encoder (legacy file format) 3645a645f22SBen Gras * 3655a645f22SBen Gras * The .lzma format is sometimes called the LZMA_Alone format, which is the 3665a645f22SBen Gras * reason for the name of this function. The .lzma format supports only the 3675a645f22SBen Gras * LZMA1 filter. There is no support for integrity checks like CRC32. 3685a645f22SBen Gras * 3695a645f22SBen Gras * Use this function if and only if you need to create files readable by 3705a645f22SBen Gras * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format 3715a645f22SBen Gras * is strongly recommended. 3725a645f22SBen Gras * 3735a645f22SBen Gras * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH. 3745a645f22SBen Gras * No kind of flushing is supported, because the file format doesn't make 3755a645f22SBen Gras * it possible. 3765a645f22SBen Gras * 3775a645f22SBen Gras * \return - LZMA_OK 3785a645f22SBen Gras * - LZMA_MEM_ERROR 3795a645f22SBen Gras * - LZMA_OPTIONS_ERROR 3805a645f22SBen Gras * - LZMA_PROG_ERROR 3815a645f22SBen Gras */ 3825a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_alone_encoder( 3835a645f22SBen Gras lzma_stream *strm, const lzma_options_lzma *options) 3845a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 3855a645f22SBen Gras 3865a645f22SBen Gras 3875a645f22SBen Gras /** 3885a645f22SBen Gras * \brief Calculate output buffer size for single-call Stream encoder 3895a645f22SBen Gras * 3905a645f22SBen Gras * When trying to compress uncompressible data, the encoded size will be 3915a645f22SBen Gras * slightly bigger than the input data. This function calculates how much 3925a645f22SBen Gras * output buffer space is required to be sure that lzma_stream_buffer_encode() 3935a645f22SBen Gras * doesn't return LZMA_BUF_ERROR. 3945a645f22SBen Gras * 3955a645f22SBen Gras * The calculated value is not exact, but it is guaranteed to be big enough. 3965a645f22SBen Gras * The actual maximum output space required may be slightly smaller (up to 3975a645f22SBen Gras * about 100 bytes). This should not be a problem in practice. 3985a645f22SBen Gras * 3995a645f22SBen Gras * If the calculated maximum size doesn't fit into size_t or would make the 4005a645f22SBen Gras * Stream grow past LZMA_VLI_MAX (which should never happen in practice), 4015a645f22SBen Gras * zero is returned to indicate the error. 4025a645f22SBen Gras * 4035a645f22SBen Gras * \note The limit calculated by this function applies only to 4045a645f22SBen Gras * single-call encoding. Multi-call encoding may (and probably 4055a645f22SBen Gras * will) have larger maximum expansion when encoding 4065a645f22SBen Gras * uncompressible data. Currently there is no function to 4075a645f22SBen Gras * calculate the maximum expansion of multi-call encoding. 4085a645f22SBen Gras */ 4095a645f22SBen Gras extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size) 4105a645f22SBen Gras lzma_nothrow; 4115a645f22SBen Gras 4125a645f22SBen Gras 4135a645f22SBen Gras /** 4145a645f22SBen Gras * \brief Single-call .xz Stream encoder 4155a645f22SBen Gras * 4165a645f22SBen Gras * \param filters Array of filters. This must be terminated with 4175a645f22SBen Gras * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h 4185a645f22SBen Gras * for more information. 4195a645f22SBen Gras * \param check Type of the integrity check to calculate from 4205a645f22SBen Gras * uncompressed data. 4215a645f22SBen Gras * \param allocator lzma_allocator for custom allocator functions. 4225a645f22SBen Gras * Set to NULL to use malloc() and free(). 4235a645f22SBen Gras * \param in Beginning of the input buffer 4245a645f22SBen Gras * \param in_size Size of the input buffer 4255a645f22SBen Gras * \param out Beginning of the output buffer 4265a645f22SBen Gras * \param out_pos The next byte will be written to out[*out_pos]. 4275a645f22SBen Gras * *out_pos is updated only if encoding succeeds. 4285a645f22SBen Gras * \param out_size Size of the out buffer; the first byte into 4295a645f22SBen Gras * which no data is written to is out[out_size]. 4305a645f22SBen Gras * 4315a645f22SBen Gras * \return - LZMA_OK: Encoding was successful. 4325a645f22SBen Gras * - LZMA_BUF_ERROR: Not enough output buffer space. 43311be35a1SLionel Sambuc * - LZMA_UNSUPPORTED_CHECK 4345a645f22SBen Gras * - LZMA_OPTIONS_ERROR 4355a645f22SBen Gras * - LZMA_MEM_ERROR 4365a645f22SBen Gras * - LZMA_DATA_ERROR 4375a645f22SBen Gras * - LZMA_PROG_ERROR 4385a645f22SBen Gras */ 4395a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_stream_buffer_encode( 4405a645f22SBen Gras lzma_filter *filters, lzma_check check, 441*0a6a1f1dSLionel Sambuc const lzma_allocator *allocator, 442*0a6a1f1dSLionel Sambuc const uint8_t *in, size_t in_size, 4435a645f22SBen Gras uint8_t *out, size_t *out_pos, size_t out_size) 4445a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 4455a645f22SBen Gras 4465a645f22SBen Gras 4475a645f22SBen Gras /************ 4485a645f22SBen Gras * Decoding * 4495a645f22SBen Gras ************/ 4505a645f22SBen Gras 4515a645f22SBen Gras /** 4525a645f22SBen Gras * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream 4535a645f22SBen Gras * being decoded has no integrity check. Note that when used with 4545a645f22SBen Gras * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK 4555a645f22SBen Gras * if LZMA_TELL_NO_CHECK is used. 4565a645f22SBen Gras */ 4575a645f22SBen Gras #define LZMA_TELL_NO_CHECK UINT32_C(0x01) 4585a645f22SBen Gras 4595a645f22SBen Gras 4605a645f22SBen Gras /** 4615a645f22SBen Gras * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input 4625a645f22SBen Gras * stream has an integrity check, but the type of the integrity check is not 4635a645f22SBen Gras * supported by this liblzma version or build. Such files can still be 4645a645f22SBen Gras * decoded, but the integrity check cannot be verified. 4655a645f22SBen Gras */ 4665a645f22SBen Gras #define LZMA_TELL_UNSUPPORTED_CHECK UINT32_C(0x02) 4675a645f22SBen Gras 4685a645f22SBen Gras 4695a645f22SBen Gras /** 4705a645f22SBen Gras * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type 4715a645f22SBen Gras * of the integrity check is known. The type can then be got with 4725a645f22SBen Gras * lzma_get_check(). 4735a645f22SBen Gras */ 4745a645f22SBen Gras #define LZMA_TELL_ANY_CHECK UINT32_C(0x04) 4755a645f22SBen Gras 4765a645f22SBen Gras 4775a645f22SBen Gras /** 478*0a6a1f1dSLionel Sambuc * This flag makes lzma_code() not calculate and verify the integrity check 479*0a6a1f1dSLionel Sambuc * of the compressed data in .xz files. This means that invalid integrity 480*0a6a1f1dSLionel Sambuc * check values won't be detected and LZMA_DATA_ERROR won't be returned in 481*0a6a1f1dSLionel Sambuc * such cases. 482*0a6a1f1dSLionel Sambuc * 483*0a6a1f1dSLionel Sambuc * This flag only affects the checks of the compressed data itself; the CRC32 484*0a6a1f1dSLionel Sambuc * values in the .xz headers will still be verified normally. 485*0a6a1f1dSLionel Sambuc * 486*0a6a1f1dSLionel Sambuc * Don't use this flag unless you know what you are doing. Possible reasons 487*0a6a1f1dSLionel Sambuc * to use this flag: 488*0a6a1f1dSLionel Sambuc * 489*0a6a1f1dSLionel Sambuc * - Trying to recover data from a corrupt .xz file. 490*0a6a1f1dSLionel Sambuc * 491*0a6a1f1dSLionel Sambuc * - Speeding up decompression, which matters mostly with SHA-256 492*0a6a1f1dSLionel Sambuc * or with files that have compressed extremely well. It's recommended 493*0a6a1f1dSLionel Sambuc * to not use this flag for this purpose unless the file integrity is 494*0a6a1f1dSLionel Sambuc * verified externally in some other way. 495*0a6a1f1dSLionel Sambuc * 496*0a6a1f1dSLionel Sambuc * Support for this flag was added in liblzma 5.1.4beta. 497*0a6a1f1dSLionel Sambuc */ 498*0a6a1f1dSLionel Sambuc #define LZMA_IGNORE_CHECK UINT32_C(0x10) 499*0a6a1f1dSLionel Sambuc 500*0a6a1f1dSLionel Sambuc 501*0a6a1f1dSLionel Sambuc /** 5025a645f22SBen Gras * This flag enables decoding of concatenated files with file formats that 5035a645f22SBen Gras * allow concatenating compressed files as is. From the formats currently 5045a645f22SBen Gras * supported by liblzma, only the .xz format allows concatenated files. 5055a645f22SBen Gras * Concatenated files are not allowed with the legacy .lzma format. 5065a645f22SBen Gras * 5075a645f22SBen Gras * This flag also affects the usage of the `action' argument for lzma_code(). 5085a645f22SBen Gras * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END 5095a645f22SBen Gras * unless LZMA_FINISH is used as `action'. Thus, the application has to set 5105a645f22SBen Gras * LZMA_FINISH in the same way as it does when encoding. 5115a645f22SBen Gras * 5125a645f22SBen Gras * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH 5135a645f22SBen Gras * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required. 5145a645f22SBen Gras */ 5155a645f22SBen Gras #define LZMA_CONCATENATED UINT32_C(0x08) 5165a645f22SBen Gras 5175a645f22SBen Gras 5185a645f22SBen Gras /** 5195a645f22SBen Gras * \brief Initialize .xz Stream decoder 5205a645f22SBen Gras * 5215a645f22SBen Gras * \param strm Pointer to properly prepared lzma_stream 5225a645f22SBen Gras * \param memlimit Memory usage limit as bytes. Use UINT64_MAX 5235a645f22SBen Gras * to effectively disable the limiter. 5245a645f22SBen Gras * \param flags Bitwise-or of zero or more of the decoder flags: 5255a645f22SBen Gras * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, 5265a645f22SBen Gras * LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED 5275a645f22SBen Gras * 5285a645f22SBen Gras * \return - LZMA_OK: Initialization was successful. 5295a645f22SBen Gras * - LZMA_MEM_ERROR: Cannot allocate memory. 5305a645f22SBen Gras * - LZMA_OPTIONS_ERROR: Unsupported flags 5315a645f22SBen Gras * - LZMA_PROG_ERROR 5325a645f22SBen Gras */ 5335a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_stream_decoder( 5345a645f22SBen Gras lzma_stream *strm, uint64_t memlimit, uint32_t flags) 5355a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 5365a645f22SBen Gras 5375a645f22SBen Gras 5385a645f22SBen Gras /** 5395a645f22SBen Gras * \brief Decode .xz Streams and .lzma files with autodetection 5405a645f22SBen Gras * 5415a645f22SBen Gras * This decoder autodetects between the .xz and .lzma file formats, and 5425a645f22SBen Gras * calls lzma_stream_decoder() or lzma_alone_decoder() once the type 5435a645f22SBen Gras * of the input file has been detected. 5445a645f22SBen Gras * 5455a645f22SBen Gras * \param strm Pointer to properly prepared lzma_stream 5465a645f22SBen Gras * \param memlimit Memory usage limit as bytes. Use UINT64_MAX 5475a645f22SBen Gras * to effectively disable the limiter. 5485a645f22SBen Gras * \param flags Bitwise-or of flags, or zero for no flags. 5495a645f22SBen Gras * 5505a645f22SBen Gras * \return - LZMA_OK: Initialization was successful. 5515a645f22SBen Gras * - LZMA_MEM_ERROR: Cannot allocate memory. 5525a645f22SBen Gras * - LZMA_OPTIONS_ERROR: Unsupported flags 5535a645f22SBen Gras * - LZMA_PROG_ERROR 5545a645f22SBen Gras */ 5555a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_auto_decoder( 5565a645f22SBen Gras lzma_stream *strm, uint64_t memlimit, uint32_t flags) 5575a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 5585a645f22SBen Gras 5595a645f22SBen Gras 5605a645f22SBen Gras /** 5615a645f22SBen Gras * \brief Initialize .lzma decoder (legacy file format) 5625a645f22SBen Gras * 5635a645f22SBen Gras * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH. 5645a645f22SBen Gras * There is no need to use LZMA_FINISH, but allowing it may simplify 5655a645f22SBen Gras * certain types of applications. 5665a645f22SBen Gras * 5675a645f22SBen Gras * \return - LZMA_OK 5685a645f22SBen Gras * - LZMA_MEM_ERROR 5695a645f22SBen Gras * - LZMA_PROG_ERROR 5705a645f22SBen Gras */ 5715a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_alone_decoder( 5725a645f22SBen Gras lzma_stream *strm, uint64_t memlimit) 5735a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 5745a645f22SBen Gras 5755a645f22SBen Gras 5765a645f22SBen Gras /** 5775a645f22SBen Gras * \brief Single-call .xz Stream decoder 5785a645f22SBen Gras * 5795a645f22SBen Gras * \param memlimit Pointer to how much memory the decoder is allowed 5805a645f22SBen Gras * to allocate. The value pointed by this pointer is 5815a645f22SBen Gras * modified if and only if LZMA_MEMLIMIT_ERROR is 5825a645f22SBen Gras * returned. 5835a645f22SBen Gras * \param flags Bitwise-or of zero or more of the decoder flags: 5845a645f22SBen Gras * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK, 5855a645f22SBen Gras * LZMA_CONCATENATED. Note that LZMA_TELL_ANY_CHECK 5865a645f22SBen Gras * is not allowed and will return LZMA_PROG_ERROR. 5875a645f22SBen Gras * \param allocator lzma_allocator for custom allocator functions. 5885a645f22SBen Gras * Set to NULL to use malloc() and free(). 5895a645f22SBen Gras * \param in Beginning of the input buffer 5905a645f22SBen Gras * \param in_pos The next byte will be read from in[*in_pos]. 5915a645f22SBen Gras * *in_pos is updated only if decoding succeeds. 5925a645f22SBen Gras * \param in_size Size of the input buffer; the first byte that 5935a645f22SBen Gras * won't be read is in[in_size]. 5945a645f22SBen Gras * \param out Beginning of the output buffer 5955a645f22SBen Gras * \param out_pos The next byte will be written to out[*out_pos]. 5965a645f22SBen Gras * *out_pos is updated only if decoding succeeds. 5975a645f22SBen Gras * \param out_size Size of the out buffer; the first byte into 5985a645f22SBen Gras * which no data is written to is out[out_size]. 5995a645f22SBen Gras * 6005a645f22SBen Gras * \return - LZMA_OK: Decoding was successful. 6015a645f22SBen Gras * - LZMA_FORMAT_ERROR 6025a645f22SBen Gras * - LZMA_OPTIONS_ERROR 6035a645f22SBen Gras * - LZMA_DATA_ERROR 6045a645f22SBen Gras * - LZMA_NO_CHECK: This can be returned only if using 6055a645f22SBen Gras * the LZMA_TELL_NO_CHECK flag. 6065a645f22SBen Gras * - LZMA_UNSUPPORTED_CHECK: This can be returned only if using 6075a645f22SBen Gras * the LZMA_TELL_UNSUPPORTED_CHECK flag. 6085a645f22SBen Gras * - LZMA_MEM_ERROR 6095a645f22SBen Gras * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached. 6105a645f22SBen Gras * The minimum required memlimit value was stored to *memlimit. 6115a645f22SBen Gras * - LZMA_BUF_ERROR: Output buffer was too small. 6125a645f22SBen Gras * - LZMA_PROG_ERROR 6135a645f22SBen Gras */ 6145a645f22SBen Gras extern LZMA_API(lzma_ret) lzma_stream_buffer_decode( 615*0a6a1f1dSLionel Sambuc uint64_t *memlimit, uint32_t flags, 616*0a6a1f1dSLionel Sambuc const lzma_allocator *allocator, 6175a645f22SBen Gras const uint8_t *in, size_t *in_pos, size_t in_size, 6185a645f22SBen Gras uint8_t *out, size_t *out_pos, size_t out_size) 6195a645f22SBen Gras lzma_nothrow lzma_attr_warn_unused_result; 620