container.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /**
  2. * \file lzma/container.h
  3. * \brief File formats
  4. * \note Never include this file directly. Use <lzma.h> instead.
  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. #ifndef LZMA_H_INTERNAL
  13. # error Never include this file directly. Use <lzma.h> instead.
  14. #endif
  15. /************
  16. * Encoding *
  17. ************/
  18. /**
  19. * \brief Default compression preset
  20. *
  21. * It's not straightforward to recommend a default preset, because in some
  22. * cases keeping the resource usage relatively low is more important that
  23. * getting the maximum compression ratio.
  24. */
  25. #define LZMA_PRESET_DEFAULT UINT32_C(6)
  26. /**
  27. * \brief Mask for preset level
  28. *
  29. * This is useful only if you need to extract the level from the preset
  30. * variable. That should be rare.
  31. */
  32. #define LZMA_PRESET_LEVEL_MASK UINT32_C(0x1F)
  33. /*
  34. * Preset flags
  35. *
  36. * Currently only one flag is defined.
  37. */
  38. /**
  39. * \brief Extreme compression preset
  40. *
  41. * This flag modifies the preset to make the encoding significantly slower
  42. * while improving the compression ratio only marginally. This is useful
  43. * when you don't mind spending time to get as small result as possible.
  44. *
  45. * This flag doesn't affect the memory usage requirements of the decoder (at
  46. * least not significantly). The memory usage of the encoder may be increased
  47. * a little but only at the lowest preset levels (0-3).
  48. */
  49. #define LZMA_PRESET_EXTREME (UINT32_C(1) << 31)
  50. /**
  51. * \brief Multithreading options
  52. */
  53. typedef struct {
  54. /**
  55. * \brief Flags
  56. *
  57. * Set this to zero if no flags are wanted.
  58. *
  59. * Encoder: No flags are currently supported.
  60. *
  61. * Decoder: Bitwise-or of zero or more of the decoder flags:
  62. * - LZMA_TELL_NO_CHECK
  63. * - LZMA_TELL_UNSUPPORTED_CHECK
  64. * - LZMA_TELL_ANY_CHECK
  65. * - LZMA_IGNORE_CHECK
  66. * - LZMA_CONCATENATED
  67. * - LZMA_FAIL_FAST
  68. */
  69. uint32_t flags;
  70. /**
  71. * \brief Number of worker threads to use
  72. */
  73. uint32_t threads;
  74. /**
  75. * \brief Encoder only: Maximum uncompressed size of a Block
  76. *
  77. * The encoder will start a new .xz Block every block_size bytes.
  78. * Using LZMA_FULL_FLUSH or LZMA_FULL_BARRIER with lzma_code()
  79. * the caller may tell liblzma to start a new Block earlier.
  80. *
  81. * With LZMA2, a recommended block size is 2-4 times the LZMA2
  82. * dictionary size. With very small dictionaries, it is recommended
  83. * to use at least 1 MiB block size for good compression ratio, even
  84. * if this is more than four times the dictionary size. Note that
  85. * these are only recommendations for typical use cases; feel free
  86. * to use other values. Just keep in mind that using a block size
  87. * less than the LZMA2 dictionary size is waste of RAM.
  88. *
  89. * Set this to 0 to let liblzma choose the block size depending
  90. * on the compression options. For LZMA2 it will be 3*dict_size
  91. * or 1 MiB, whichever is more.
  92. *
  93. * For each thread, about 3 * block_size bytes of memory will be
  94. * allocated. This may change in later liblzma versions. If so,
  95. * the memory usage will probably be reduced, not increased.
  96. */
  97. uint64_t block_size;
  98. /**
  99. * \brief Timeout to allow lzma_code() to return early
  100. *
  101. * Multithreading can make liblzma consume input and produce
  102. * output in a very bursty way: it may first read a lot of input
  103. * to fill internal buffers, then no input or output occurs for
  104. * a while.
  105. *
  106. * In single-threaded mode, lzma_code() won't return until it has
  107. * either consumed all the input or filled the output buffer. If
  108. * this is done in multithreaded mode, it may cause a call
  109. * lzma_code() to take even tens of seconds, which isn't acceptable
  110. * in all applications.
  111. *
  112. * To avoid very long blocking times in lzma_code(), a timeout
  113. * (in milliseconds) may be set here. If lzma_code() would block
  114. * longer than this number of milliseconds, it will return with
  115. * LZMA_OK. Reasonable values are 100 ms or more. The xz command
  116. * line tool uses 300 ms.
  117. *
  118. * If long blocking times are acceptable, set timeout to a special
  119. * value of 0. This will disable the timeout mechanism and will make
  120. * lzma_code() block until all the input is consumed or the output
  121. * buffer has been filled.
  122. *
  123. * \note Even with a timeout, lzma_code() might sometimes take
  124. * a long time to return. No timing guarantees are made.
  125. */
  126. uint32_t timeout;
  127. /**
  128. * \brief Encoder only: Compression preset
  129. *
  130. * The preset is set just like with lzma_easy_encoder().
  131. * The preset is ignored if filters below is non-NULL.
  132. */
  133. uint32_t preset;
  134. /**
  135. * \brief Encoder only: Filter chain (alternative to a preset)
  136. *
  137. * If this is NULL, the preset above is used. Otherwise the preset
  138. * is ignored and the filter chain specified here is used.
  139. */
  140. const lzma_filter *filters;
  141. /**
  142. * \brief Encoder only: Integrity check type
  143. *
  144. * See check.h for available checks. The xz command line tool
  145. * defaults to LZMA_CHECK_CRC64, which is a good choice if you
  146. * are unsure.
  147. */
  148. lzma_check check;
  149. /*
  150. * Reserved space to allow possible future extensions without
  151. * breaking the ABI. You should not touch these, because the names
  152. * of these variables may change. These are and will never be used
  153. * with the currently supported options, so it is safe to leave these
  154. * uninitialized.
  155. */
  156. /** \private Reserved member. */
  157. lzma_reserved_enum reserved_enum1;
  158. /** \private Reserved member. */
  159. lzma_reserved_enum reserved_enum2;
  160. /** \private Reserved member. */
  161. lzma_reserved_enum reserved_enum3;
  162. /** \private Reserved member. */
  163. uint32_t reserved_int1;
  164. /** \private Reserved member. */
  165. uint32_t reserved_int2;
  166. /** \private Reserved member. */
  167. uint32_t reserved_int3;
  168. /** \private Reserved member. */
  169. uint32_t reserved_int4;
  170. /**
  171. * \brief Memory usage limit to reduce the number of threads
  172. *
  173. * Encoder: Ignored.
  174. *
  175. * Decoder:
  176. *
  177. * If the number of threads has been set so high that more than
  178. * memlimit_threading bytes of memory would be needed, the number
  179. * of threads will be reduced so that the memory usage will not exceed
  180. * memlimit_threading bytes. However, if memlimit_threading cannot
  181. * be met even in single-threaded mode, then decoding will continue
  182. * in single-threaded mode and memlimit_threading may be exceeded
  183. * even by a large amount. That is, memlimit_threading will never make
  184. * lzma_code() return LZMA_MEMLIMIT_ERROR. To truly cap the memory
  185. * usage, see memlimit_stop below.
  186. *
  187. * Setting memlimit_threading to UINT64_MAX or a similar huge value
  188. * means that liblzma is allowed to keep the whole compressed file
  189. * and the whole uncompressed file in memory in addition to the memory
  190. * needed by the decompressor data structures used by each thread!
  191. * In other words, a reasonable value limit must be set here or it
  192. * will cause problems sooner or later. If you have no idea what
  193. * a reasonable value could be, try lzma_physmem() / 4 as a starting
  194. * point. Setting this limit will never prevent decompression of
  195. * a file; this will only reduce the number of threads.
  196. *
  197. * If memlimit_threading is greater than memlimit_stop, then the value
  198. * of memlimit_stop will be used for both.
  199. */
  200. uint64_t memlimit_threading;
  201. /**
  202. * \brief Memory usage limit that should never be exceeded
  203. *
  204. * Encoder: Ignored.
  205. *
  206. * Decoder: If decompressing will need more than this amount of
  207. * memory even in the single-threaded mode, then lzma_code() will
  208. * return LZMA_MEMLIMIT_ERROR.
  209. */
  210. uint64_t memlimit_stop;
  211. /** \private Reserved member. */
  212. uint64_t reserved_int7;
  213. /** \private Reserved member. */
  214. uint64_t reserved_int8;
  215. /** \private Reserved member. */
  216. void *reserved_ptr1;
  217. /** \private Reserved member. */
  218. void *reserved_ptr2;
  219. /** \private Reserved member. */
  220. void *reserved_ptr3;
  221. /** \private Reserved member. */
  222. void *reserved_ptr4;
  223. } lzma_mt;
  224. /**
  225. * \brief Calculate approximate memory usage of easy encoder
  226. *
  227. * This function is a wrapper for lzma_raw_encoder_memusage().
  228. *
  229. * \param preset Compression preset (level and possible flags)
  230. *
  231. * \return Number of bytes of memory required for the given
  232. * preset when encoding or UINT64_MAX on error.
  233. */
  234. extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset)
  235. lzma_nothrow lzma_attr_pure;
  236. /**
  237. * \brief Calculate approximate decoder memory usage of a preset
  238. *
  239. * This function is a wrapper for lzma_raw_decoder_memusage().
  240. *
  241. * \param preset Compression preset (level and possible flags)
  242. *
  243. * \return Number of bytes of memory required to decompress a file
  244. * that was compressed using the given preset or UINT64_MAX
  245. * on error.
  246. */
  247. extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset)
  248. lzma_nothrow lzma_attr_pure;
  249. /**
  250. * \brief Initialize .xz Stream encoder using a preset number
  251. *
  252. * This function is intended for those who just want to use the basic features
  253. * of liblzma (that is, most developers out there).
  254. *
  255. * If initialization fails (return value is not LZMA_OK), all the memory
  256. * allocated for *strm by liblzma is always freed. Thus, there is no need
  257. * to call lzma_end() after failed initialization.
  258. *
  259. * If initialization succeeds, use lzma_code() to do the actual encoding.
  260. * Valid values for `action' (the second argument of lzma_code()) are
  261. * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future,
  262. * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH.
  263. *
  264. * \param strm Pointer to lzma_stream that is at least initialized
  265. * with LZMA_STREAM_INIT.
  266. * \param preset Compression preset to use. A preset consist of level
  267. * number and zero or more flags. Usually flags aren't
  268. * used, so preset is simply a number [0, 9] which match
  269. * the options -0 ... -9 of the xz command line tool.
  270. * Additional flags can be be set using bitwise-or with
  271. * the preset level number, e.g. 6 | LZMA_PRESET_EXTREME.
  272. * \param check Integrity check type to use. See check.h for available
  273. * checks. The xz command line tool defaults to
  274. * LZMA_CHECK_CRC64, which is a good choice if you are
  275. * unsure. LZMA_CHECK_CRC32 is good too as long as the
  276. * uncompressed file is not many gigabytes.
  277. *
  278. * \return Possible lzma_ret values:
  279. * - LZMA_OK: Initialization succeeded. Use lzma_code() to
  280. * encode your data.
  281. * - LZMA_MEM_ERROR: Memory allocation failed.
  282. * - LZMA_OPTIONS_ERROR: The given compression preset is not
  283. * supported by this build of liblzma.
  284. * - LZMA_UNSUPPORTED_CHECK: The given check type is not
  285. * supported by this liblzma build.
  286. * - LZMA_PROG_ERROR: One or more of the parameters have values
  287. * that will never be valid. For example, strm == NULL.
  288. */
  289. extern LZMA_API(lzma_ret) lzma_easy_encoder(
  290. lzma_stream *strm, uint32_t preset, lzma_check check)
  291. lzma_nothrow lzma_attr_warn_unused_result;
  292. /**
  293. * \brief Single-call .xz Stream encoding using a preset number
  294. *
  295. * The maximum required output buffer size can be calculated with
  296. * lzma_stream_buffer_bound().
  297. *
  298. * \param preset Compression preset to use. See the description
  299. * in lzma_easy_encoder().
  300. * \param check Type of the integrity check to calculate from
  301. * uncompressed data.
  302. * \param allocator lzma_allocator for custom allocator functions.
  303. * Set to NULL to use malloc() and free().
  304. * \param in Beginning of the input buffer
  305. * \param in_size Size of the input buffer
  306. * \param[out] out Beginning of the output buffer
  307. * \param[out] out_pos The next byte will be written to out[*out_pos].
  308. * *out_pos is updated only if encoding succeeds.
  309. * \param out_size Size of the out buffer; the first byte into
  310. * which no data is written to is out[out_size].
  311. *
  312. * \return Possible lzma_ret values:
  313. * - LZMA_OK: Encoding was successful.
  314. * - LZMA_BUF_ERROR: Not enough output buffer space.
  315. * - LZMA_UNSUPPORTED_CHECK
  316. * - LZMA_OPTIONS_ERROR
  317. * - LZMA_MEM_ERROR
  318. * - LZMA_DATA_ERROR
  319. * - LZMA_PROG_ERROR
  320. */
  321. extern LZMA_API(lzma_ret) lzma_easy_buffer_encode(
  322. uint32_t preset, lzma_check check,
  323. const lzma_allocator *allocator,
  324. const uint8_t *in, size_t in_size,
  325. uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
  326. /**
  327. * \brief Initialize .xz Stream encoder using a custom filter chain
  328. *
  329. * \param strm Pointer to lzma_stream that is at least initialized
  330. * with LZMA_STREAM_INIT.
  331. * \param filters Array of filters terminated with
  332. * .id == LZMA_VLI_UNKNOWN. See filters.h for more
  333. * information.
  334. * \param check Type of the integrity check to calculate from
  335. * uncompressed data.
  336. *
  337. * \return Possible lzma_ret values:
  338. * - LZMA_OK: Initialization was successful.
  339. * - LZMA_MEM_ERROR
  340. * - LZMA_UNSUPPORTED_CHECK
  341. * - LZMA_OPTIONS_ERROR
  342. * - LZMA_PROG_ERROR
  343. */
  344. extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
  345. const lzma_filter *filters, lzma_check check)
  346. lzma_nothrow lzma_attr_warn_unused_result;
  347. /**
  348. * \brief Calculate approximate memory usage of multithreaded .xz encoder
  349. *
  350. * Since doing the encoding in threaded mode doesn't affect the memory
  351. * requirements of single-threaded decompressor, you can use
  352. * lzma_easy_decoder_memusage(options->preset) or
  353. * lzma_raw_decoder_memusage(options->filters) to calculate
  354. * the decompressor memory requirements.
  355. *
  356. * \param options Compression options
  357. *
  358. * \return Number of bytes of memory required for encoding with the
  359. * given options. If an error occurs, for example due to
  360. * unsupported preset or filter chain, UINT64_MAX is returned.
  361. */
  362. extern LZMA_API(uint64_t) lzma_stream_encoder_mt_memusage(
  363. const lzma_mt *options) lzma_nothrow lzma_attr_pure;
  364. /**
  365. * \brief Initialize multithreaded .xz Stream encoder
  366. *
  367. * This provides the functionality of lzma_easy_encoder() and
  368. * lzma_stream_encoder() as a single function for multithreaded use.
  369. *
  370. * The supported actions for lzma_code() are LZMA_RUN, LZMA_FULL_FLUSH,
  371. * LZMA_FULL_BARRIER, and LZMA_FINISH. Support for LZMA_SYNC_FLUSH might be
  372. * added in the future.
  373. *
  374. * \param strm Pointer to lzma_stream that is at least initialized
  375. * with LZMA_STREAM_INIT.
  376. * \param options Pointer to multithreaded compression options
  377. *
  378. * \return Possible lzma_ret values:
  379. * - LZMA_OK
  380. * - LZMA_MEM_ERROR
  381. * - LZMA_UNSUPPORTED_CHECK
  382. * - LZMA_OPTIONS_ERROR
  383. * - LZMA_PROG_ERROR
  384. */
  385. extern LZMA_API(lzma_ret) lzma_stream_encoder_mt(
  386. lzma_stream *strm, const lzma_mt *options)
  387. lzma_nothrow lzma_attr_warn_unused_result;
  388. /**
  389. * \brief Initialize .lzma encoder (legacy file format)
  390. *
  391. * The .lzma format is sometimes called the LZMA_Alone format, which is the
  392. * reason for the name of this function. The .lzma format supports only the
  393. * LZMA1 filter. There is no support for integrity checks like CRC32.
  394. *
  395. * Use this function if and only if you need to create files readable by
  396. * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format
  397. * is strongly recommended.
  398. *
  399. * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH.
  400. * No kind of flushing is supported, because the file format doesn't make
  401. * it possible.
  402. *
  403. * \param strm Pointer to lzma_stream that is at least initialized
  404. * with LZMA_STREAM_INIT.
  405. * \param options Pointer to encoder options
  406. *
  407. * \return Possible lzma_ret values:
  408. * - LZMA_OK
  409. * - LZMA_MEM_ERROR
  410. * - LZMA_OPTIONS_ERROR
  411. * - LZMA_PROG_ERROR
  412. */
  413. extern LZMA_API(lzma_ret) lzma_alone_encoder(
  414. lzma_stream *strm, const lzma_options_lzma *options)
  415. lzma_nothrow lzma_attr_warn_unused_result;
  416. /**
  417. * \brief Calculate output buffer size for single-call Stream encoder
  418. *
  419. * When trying to compress uncompressible data, the encoded size will be
  420. * slightly bigger than the input data. This function calculates how much
  421. * output buffer space is required to be sure that lzma_stream_buffer_encode()
  422. * doesn't return LZMA_BUF_ERROR.
  423. *
  424. * The calculated value is not exact, but it is guaranteed to be big enough.
  425. * The actual maximum output space required may be slightly smaller (up to
  426. * about 100 bytes). This should not be a problem in practice.
  427. *
  428. * If the calculated maximum size doesn't fit into size_t or would make the
  429. * Stream grow past LZMA_VLI_MAX (which should never happen in practice),
  430. * zero is returned to indicate the error.
  431. *
  432. * \note The limit calculated by this function applies only to
  433. * single-call encoding. Multi-call encoding may (and probably
  434. * will) have larger maximum expansion when encoding
  435. * uncompressible data. Currently there is no function to
  436. * calculate the maximum expansion of multi-call encoding.
  437. *
  438. * \param uncompressed_size Size in bytes of the uncompressed
  439. * input data
  440. *
  441. * \return Maximum number of bytes needed to store the compressed data.
  442. */
  443. extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size)
  444. lzma_nothrow;
  445. /**
  446. * \brief Single-call .xz Stream encoder
  447. *
  448. * \param filters Array of filters terminated with
  449. * .id == LZMA_VLI_UNKNOWN. See filters.h for more
  450. * information.
  451. * \param check Type of the integrity check to calculate from
  452. * uncompressed data.
  453. * \param allocator lzma_allocator for custom allocator functions.
  454. * Set to NULL to use malloc() and free().
  455. * \param in Beginning of the input buffer
  456. * \param in_size Size of the input buffer
  457. * \param[out] out Beginning of the output buffer
  458. * \param[out] out_pos The next byte will be written to out[*out_pos].
  459. * *out_pos is updated only if encoding succeeds.
  460. * \param out_size Size of the out buffer; the first byte into
  461. * which no data is written to is out[out_size].
  462. *
  463. * \return Possible lzma_ret values:
  464. * - LZMA_OK: Encoding was successful.
  465. * - LZMA_BUF_ERROR: Not enough output buffer space.
  466. * - LZMA_UNSUPPORTED_CHECK
  467. * - LZMA_OPTIONS_ERROR
  468. * - LZMA_MEM_ERROR
  469. * - LZMA_DATA_ERROR
  470. * - LZMA_PROG_ERROR
  471. */
  472. extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
  473. lzma_filter *filters, lzma_check check,
  474. const lzma_allocator *allocator,
  475. const uint8_t *in, size_t in_size,
  476. uint8_t *out, size_t *out_pos, size_t out_size)
  477. lzma_nothrow lzma_attr_warn_unused_result;
  478. /**
  479. * \brief MicroLZMA encoder
  480. *
  481. * The MicroLZMA format is a raw LZMA stream whose first byte (always 0x00)
  482. * has been replaced with bitwise-negation of the LZMA properties (lc/lp/pb).
  483. * This encoding ensures that the first byte of MicroLZMA stream is never
  484. * 0x00. There is no end of payload marker and thus the uncompressed size
  485. * must be stored separately. For the best error detection the dictionary
  486. * size should be stored separately as well but alternatively one may use
  487. * the uncompressed size as the dictionary size when decoding.
  488. *
  489. * With the MicroLZMA encoder, lzma_code() behaves slightly unusually.
  490. * The action argument must be LZMA_FINISH and the return value will never be
  491. * LZMA_OK. Thus the encoding is always done with a single lzma_code() after
  492. * the initialization. The benefit of the combination of initialization
  493. * function and lzma_code() is that memory allocations can be re-used for
  494. * better performance.
  495. *
  496. * lzma_code() will try to encode as much input as is possible to fit into
  497. * the given output buffer. If not all input can be encoded, the stream will
  498. * be finished without encoding all the input. The caller must check both
  499. * input and output buffer usage after lzma_code() (total_in and total_out
  500. * in lzma_stream can be convenient). Often lzma_code() can fill the output
  501. * buffer completely if there is a lot of input, but sometimes a few bytes
  502. * may remain unused because the next LZMA symbol would require more space.
  503. *
  504. * lzma_stream.avail_out must be at least 6. Otherwise LZMA_PROG_ERROR
  505. * will be returned.
  506. *
  507. * The LZMA dictionary should be reasonably low to speed up the encoder
  508. * re-initialization. A good value is bigger than the resulting
  509. * uncompressed size of most of the output chunks. For example, if output
  510. * size is 4 KiB, dictionary size of 32 KiB or 64 KiB is good. If the
  511. * data compresses extremely well, even 128 KiB may be useful.
  512. *
  513. * The MicroLZMA format and this encoder variant were made with the EROFS
  514. * file system in mind. This format may be convenient in other embedded
  515. * uses too where many small streams are needed. XZ Embedded includes a
  516. * decoder for this format.
  517. *
  518. * \param strm Pointer to lzma_stream that is at least initialized
  519. * with LZMA_STREAM_INIT.
  520. * \param options Pointer to encoder options
  521. *
  522. * \return Possible lzma_ret values:
  523. * - LZMA_STREAM_END: All good. Check the amounts of input used
  524. * and output produced. Store the amount of input used
  525. * (uncompressed size) as it needs to be known to decompress
  526. * the data.
  527. * - LZMA_OPTIONS_ERROR
  528. * - LZMA_MEM_ERROR
  529. * - LZMA_PROG_ERROR: In addition to the generic reasons for this
  530. * error code, this may also be returned if there isn't enough
  531. * output space (6 bytes) to create a valid MicroLZMA stream.
  532. */
  533. extern LZMA_API(lzma_ret) lzma_microlzma_encoder(
  534. lzma_stream *strm, const lzma_options_lzma *options);
  535. /************
  536. * Decoding *
  537. ************/
  538. /**
  539. * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream
  540. * being decoded has no integrity check. Note that when used with
  541. * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK
  542. * if LZMA_TELL_NO_CHECK is used.
  543. */
  544. #define LZMA_TELL_NO_CHECK UINT32_C(0x01)
  545. /**
  546. * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input
  547. * stream has an integrity check, but the type of the integrity check is not
  548. * supported by this liblzma version or build. Such files can still be
  549. * decoded, but the integrity check cannot be verified.
  550. */
  551. #define LZMA_TELL_UNSUPPORTED_CHECK UINT32_C(0x02)
  552. /**
  553. * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type
  554. * of the integrity check is known. The type can then be got with
  555. * lzma_get_check().
  556. */
  557. #define LZMA_TELL_ANY_CHECK UINT32_C(0x04)
  558. /**
  559. * This flag makes lzma_code() not calculate and verify the integrity check
  560. * of the compressed data in .xz files. This means that invalid integrity
  561. * check values won't be detected and LZMA_DATA_ERROR won't be returned in
  562. * such cases.
  563. *
  564. * This flag only affects the checks of the compressed data itself; the CRC32
  565. * values in the .xz headers will still be verified normally.
  566. *
  567. * Don't use this flag unless you know what you are doing. Possible reasons
  568. * to use this flag:
  569. *
  570. * - Trying to recover data from a corrupt .xz file.
  571. *
  572. * - Speeding up decompression, which matters mostly with SHA-256
  573. * or with files that have compressed extremely well. It's recommended
  574. * to not use this flag for this purpose unless the file integrity is
  575. * verified externally in some other way.
  576. *
  577. * Support for this flag was added in liblzma 5.1.4beta.
  578. */
  579. #define LZMA_IGNORE_CHECK UINT32_C(0x10)
  580. /**
  581. * This flag enables decoding of concatenated files with file formats that
  582. * allow concatenating compressed files as is. From the formats currently
  583. * supported by liblzma, only the .xz and .lz formats allow concatenated
  584. * files. Concatenated files are not allowed with the legacy .lzma format.
  585. *
  586. * This flag also affects the usage of the `action' argument for lzma_code().
  587. * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END
  588. * unless LZMA_FINISH is used as `action'. Thus, the application has to set
  589. * LZMA_FINISH in the same way as it does when encoding.
  590. *
  591. * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH
  592. * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required.
  593. */
  594. #define LZMA_CONCATENATED UINT32_C(0x08)
  595. /**
  596. * This flag makes the threaded decoder report errors (like LZMA_DATA_ERROR)
  597. * as soon as they are detected. This saves time when the application has no
  598. * interest in a partially decompressed truncated or corrupt file. Note that
  599. * due to timing randomness, if the same truncated or corrupt input is
  600. * decompressed multiple times with this flag, a different amount of output
  601. * may be produced by different runs, and even the error code might vary.
  602. *
  603. * When using LZMA_FAIL_FAST, it is recommended to use LZMA_FINISH to tell
  604. * the decoder when no more input will be coming because it can help fast
  605. * detection and reporting of truncated files. Note that in this situation
  606. * truncated files might be diagnosed with LZMA_DATA_ERROR instead of
  607. * LZMA_OK or LZMA_BUF_ERROR!
  608. *
  609. * Without this flag the threaded decoder will provide as much output as
  610. * possible at first and then report the pending error. This default behavior
  611. * matches the single-threaded decoder and provides repeatable behavior
  612. * with truncated or corrupt input. There are a few special cases where the
  613. * behavior can still differ like memory allocation failures (LZMA_MEM_ERROR).
  614. *
  615. * Single-threaded decoders currently ignore this flag.
  616. *
  617. * Support for this flag was added in liblzma 5.3.3alpha. Note that in older
  618. * versions this flag isn't supported (LZMA_OPTIONS_ERROR) even by functions
  619. * that ignore this flag in newer liblzma versions.
  620. */
  621. #define LZMA_FAIL_FAST UINT32_C(0x20)
  622. /**
  623. * \brief Initialize .xz Stream decoder
  624. *
  625. * \param strm Pointer to lzma_stream that is at least initialized
  626. * with LZMA_STREAM_INIT.
  627. * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
  628. * to effectively disable the limiter. liblzma
  629. * 5.2.3 and earlier don't allow 0 here and return
  630. * LZMA_PROG_ERROR; later versions treat 0 as if 1
  631. * had been specified.
  632. * \param flags Bitwise-or of zero or more of the decoder flags:
  633. * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
  634. * LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
  635. * LZMA_CONCATENATED, LZMA_FAIL_FAST
  636. *
  637. * \return Possible lzma_ret values:
  638. * - LZMA_OK: Initialization was successful.
  639. * - LZMA_MEM_ERROR: Cannot allocate memory.
  640. * - LZMA_OPTIONS_ERROR: Unsupported flags
  641. * - LZMA_PROG_ERROR
  642. */
  643. extern LZMA_API(lzma_ret) lzma_stream_decoder(
  644. lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  645. lzma_nothrow lzma_attr_warn_unused_result;
  646. /**
  647. * \brief Initialize multithreaded .xz Stream decoder
  648. *
  649. * The decoder can decode multiple Blocks in parallel. This requires that each
  650. * Block Header contains the Compressed Size and Uncompressed size fields
  651. * which are added by the multi-threaded encoder, see lzma_stream_encoder_mt().
  652. *
  653. * A Stream with one Block will only utilize one thread. A Stream with multiple
  654. * Blocks but without size information in Block Headers will be processed in
  655. * single-threaded mode in the same way as done by lzma_stream_decoder().
  656. * Concatenated Streams are processed one Stream at a time; no inter-Stream
  657. * parallelization is done.
  658. *
  659. * This function behaves like lzma_stream_decoder() when options->threads == 1
  660. * and options->memlimit_threading <= 1.
  661. *
  662. * \param strm Pointer to lzma_stream that is at least initialized
  663. * with LZMA_STREAM_INIT.
  664. * \param options Pointer to multithreaded compression options
  665. *
  666. * \return Possible lzma_ret values:
  667. * - LZMA_OK: Initialization was successful.
  668. * - LZMA_MEM_ERROR: Cannot allocate memory.
  669. * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
  670. * - LZMA_OPTIONS_ERROR: Unsupported flags.
  671. * - LZMA_PROG_ERROR
  672. */
  673. extern LZMA_API(lzma_ret) lzma_stream_decoder_mt(
  674. lzma_stream *strm, const lzma_mt *options)
  675. lzma_nothrow lzma_attr_warn_unused_result;
  676. /**
  677. * \brief Decode .xz, .lzma, and .lz (lzip) files with autodetection
  678. *
  679. * This decoder autodetects between the .xz, .lzma, and .lz file formats,
  680. * and calls lzma_stream_decoder(), lzma_alone_decoder(), or
  681. * lzma_lzip_decoder() once the type of the input file has been detected.
  682. *
  683. * Support for .lz was added in 5.4.0.
  684. *
  685. * If the flag LZMA_CONCATENATED is used and the input is a .lzma file:
  686. * For historical reasons concatenated .lzma files aren't supported.
  687. * If there is trailing data after one .lzma stream, lzma_code() will
  688. * return LZMA_DATA_ERROR. (lzma_alone_decoder() doesn't have such a check
  689. * as it doesn't support any decoder flags. It will return LZMA_STREAM_END
  690. * after one .lzma stream.)
  691. *
  692. * \param strm Pointer to lzma_stream that is at least initialized
  693. * with LZMA_STREAM_INIT.
  694. * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
  695. * to effectively disable the limiter. liblzma
  696. * 5.2.3 and earlier don't allow 0 here and return
  697. * LZMA_PROG_ERROR; later versions treat 0 as if 1
  698. * had been specified.
  699. * \param flags Bitwise-or of zero or more of the decoder flags:
  700. * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
  701. * LZMA_TELL_ANY_CHECK, LZMA_IGNORE_CHECK,
  702. * LZMA_CONCATENATED, LZMA_FAIL_FAST
  703. *
  704. * \return Possible lzma_ret values:
  705. * - LZMA_OK: Initialization was successful.
  706. * - LZMA_MEM_ERROR: Cannot allocate memory.
  707. * - LZMA_OPTIONS_ERROR: Unsupported flags
  708. * - LZMA_PROG_ERROR
  709. */
  710. extern LZMA_API(lzma_ret) lzma_auto_decoder(
  711. lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  712. lzma_nothrow lzma_attr_warn_unused_result;
  713. /**
  714. * \brief Initialize .lzma decoder (legacy file format)
  715. *
  716. * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
  717. * There is no need to use LZMA_FINISH, but it's allowed because it may
  718. * simplify certain types of applications.
  719. *
  720. * \param strm Pointer to lzma_stream that is at least initialized
  721. * with LZMA_STREAM_INIT.
  722. * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
  723. * to effectively disable the limiter. liblzma
  724. * 5.2.3 and earlier don't allow 0 here and return
  725. * LZMA_PROG_ERROR; later versions treat 0 as if 1
  726. * had been specified.
  727. *
  728. * \return Possible lzma_ret values:
  729. * - LZMA_OK
  730. * - LZMA_MEM_ERROR
  731. * - LZMA_PROG_ERROR
  732. */
  733. extern LZMA_API(lzma_ret) lzma_alone_decoder(
  734. lzma_stream *strm, uint64_t memlimit)
  735. lzma_nothrow lzma_attr_warn_unused_result;
  736. /**
  737. * \brief Initialize .lz (lzip) decoder (a foreign file format)
  738. *
  739. * This decoder supports the .lz format version 0 and the unextended .lz
  740. * format version 1:
  741. *
  742. * - Files in the format version 0 were produced by lzip 1.3 and older.
  743. * Such files aren't common but may be found from file archives
  744. * as a few source packages were released in this format. People
  745. * might have old personal files in this format too. Decompression
  746. * support for the format version 0 was removed in lzip 1.18.
  747. *
  748. * - lzip 1.3 added decompression support for .lz format version 1 files.
  749. * Compression support was added in lzip 1.4. In lzip 1.6 the .lz format
  750. * version 1 was extended to support the Sync Flush marker. This extension
  751. * is not supported by liblzma. lzma_code() will return LZMA_DATA_ERROR
  752. * at the location of the Sync Flush marker. In practice files with
  753. * the Sync Flush marker are very rare and thus liblzma can decompress
  754. * almost all .lz files.
  755. *
  756. * Just like with lzma_stream_decoder() for .xz files, LZMA_CONCATENATED
  757. * should be used when decompressing normal standalone .lz files.
  758. *
  759. * The .lz format allows putting non-.lz data at the end of a file after at
  760. * least one valid .lz member. That is, one can append custom data at the end
  761. * of a .lz file and the decoder is required to ignore it. In liblzma this
  762. * is relevant only when LZMA_CONCATENATED is used. In that case lzma_code()
  763. * will return LZMA_STREAM_END and leave lzma_stream.next_in pointing to
  764. * the first byte of the non-.lz data. An exception to this is if the first
  765. * 1-3 bytes of the non-.lz data are identical to the .lz magic bytes
  766. * (0x4C, 0x5A, 0x49, 0x50; "LZIP" in US-ASCII). In such a case the 1-3 bytes
  767. * will have been ignored by lzma_code(). If one wishes to locate the non-.lz
  768. * data reliably, one must ensure that the first byte isn't 0x4C. Actually
  769. * one should ensure that none of the first four bytes of trailing data are
  770. * equal to the magic bytes because lzip >= 1.20 requires it by default.
  771. *
  772. * \param strm Pointer to lzma_stream that is at least initialized
  773. * with LZMA_STREAM_INIT.
  774. * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
  775. * to effectively disable the limiter.
  776. * \param flags Bitwise-or of flags, or zero for no flags.
  777. * All decoder flags listed above are supported
  778. * although only LZMA_CONCATENATED and (in very rare
  779. * cases) LZMA_IGNORE_CHECK are actually useful.
  780. * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
  781. * and LZMA_FAIL_FAST do nothing. LZMA_TELL_ANY_CHECK
  782. * is supported for consistency only as CRC32 is
  783. * always used in the .lz format.
  784. *
  785. * \return Possible lzma_ret values:
  786. * - LZMA_OK: Initialization was successful.
  787. * - LZMA_MEM_ERROR: Cannot allocate memory.
  788. * - LZMA_OPTIONS_ERROR: Unsupported flags
  789. * - LZMA_PROG_ERROR
  790. */
  791. extern LZMA_API(lzma_ret) lzma_lzip_decoder(
  792. lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  793. lzma_nothrow lzma_attr_warn_unused_result;
  794. /**
  795. * \brief Single-call .xz Stream decoder
  796. *
  797. * \param memlimit Pointer to how much memory the decoder is allowed
  798. * to allocate. The value pointed by this pointer is
  799. * modified if and only if LZMA_MEMLIMIT_ERROR is
  800. * returned.
  801. * \param flags Bitwise-or of zero or more of the decoder flags:
  802. * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
  803. * LZMA_IGNORE_CHECK, LZMA_CONCATENATED,
  804. * LZMA_FAIL_FAST. Note that LZMA_TELL_ANY_CHECK
  805. * is not allowed and will return LZMA_PROG_ERROR.
  806. * \param allocator lzma_allocator for custom allocator functions.
  807. * Set to NULL to use malloc() and free().
  808. * \param in Beginning of the input buffer
  809. * \param in_pos The next byte will be read from in[*in_pos].
  810. * *in_pos is updated only if decoding succeeds.
  811. * \param in_size Size of the input buffer; the first byte that
  812. * won't be read is in[in_size].
  813. * \param[out] out Beginning of the output buffer
  814. * \param[out] out_pos The next byte will be written to out[*out_pos].
  815. * *out_pos is updated only if decoding succeeds.
  816. * \param out_size Size of the out buffer; the first byte into
  817. * which no data is written to is out[out_size].
  818. *
  819. * \return Possible lzma_ret values:
  820. * - LZMA_OK: Decoding was successful.
  821. * - LZMA_FORMAT_ERROR
  822. * - LZMA_OPTIONS_ERROR
  823. * - LZMA_DATA_ERROR
  824. * - LZMA_NO_CHECK: This can be returned only if using
  825. * the LZMA_TELL_NO_CHECK flag.
  826. * - LZMA_UNSUPPORTED_CHECK: This can be returned only if using
  827. * the LZMA_TELL_UNSUPPORTED_CHECK flag.
  828. * - LZMA_MEM_ERROR
  829. * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
  830. * The minimum required memlimit value was stored to *memlimit.
  831. * - LZMA_BUF_ERROR: Output buffer was too small.
  832. * - LZMA_PROG_ERROR
  833. */
  834. extern LZMA_API(lzma_ret) lzma_stream_buffer_decode(
  835. uint64_t *memlimit, uint32_t flags,
  836. const lzma_allocator *allocator,
  837. const uint8_t *in, size_t *in_pos, size_t in_size,
  838. uint8_t *out, size_t *out_pos, size_t out_size)
  839. lzma_nothrow lzma_attr_warn_unused_result;
  840. /**
  841. * \brief MicroLZMA decoder
  842. *
  843. * See lzma_microlzma_encoder() for more information.
  844. *
  845. * The lzma_code() usage with this decoder is completely normal. The
  846. * special behavior of lzma_code() applies to lzma_microlzma_encoder() only.
  847. *
  848. * \param strm Pointer to lzma_stream that is at least initialized
  849. * with LZMA_STREAM_INIT.
  850. * \param comp_size Compressed size of the MicroLZMA stream.
  851. * The caller must somehow know this exactly.
  852. * \param uncomp_size Uncompressed size of the MicroLZMA stream.
  853. * If the exact uncompressed size isn't known, this
  854. * can be set to a value that is at most as big as
  855. * the exact uncompressed size would be, but then the
  856. * next argument uncomp_size_is_exact must be false.
  857. * \param uncomp_size_is_exact
  858. * If true, uncomp_size must be exactly correct.
  859. * This will improve error detection at the end of
  860. * the stream. If the exact uncompressed size isn't
  861. * known, this must be false. uncomp_size must still
  862. * be at most as big as the exact uncompressed size
  863. * is. Setting this to false when the exact size is
  864. * known will work but error detection at the end of
  865. * the stream will be weaker.
  866. * \param dict_size LZMA dictionary size that was used when
  867. * compressing the data. It is OK to use a bigger
  868. * value too but liblzma will then allocate more
  869. * memory than would actually be required and error
  870. * detection will be slightly worse. (Note that with
  871. * the implementation in XZ Embedded it doesn't
  872. * affect the memory usage if one specifies bigger
  873. * dictionary than actually required.)
  874. *
  875. * \return Possible lzma_ret values:
  876. * - LZMA_OK
  877. * - LZMA_MEM_ERROR
  878. * - LZMA_OPTIONS_ERROR
  879. * - LZMA_PROG_ERROR
  880. */
  881. extern LZMA_API(lzma_ret) lzma_microlzma_decoder(
  882. lzma_stream *strm, uint64_t comp_size,
  883. uint64_t uncomp_size, lzma_bool uncomp_size_is_exact,
  884. uint32_t dict_size);