container.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. * \file lzma/container.h
  3. * \brief File formats
  4. */
  5. /*
  6. * Author: Lasse Collin
  7. *
  8. * This file has been put into the public domain.
  9. * You can do whatever you want with this file.
  10. *
  11. * See ../lzma.h for information about liblzma as a whole.
  12. */
  13. #ifndef LZMA_H_INTERNAL
  14. # error Never include this file directly. Use <lzma.h> instead.
  15. #endif
  16. /************
  17. * Encoding *
  18. ************/
  19. /**
  20. * \brief Default compression preset
  21. *
  22. * It's not straightforward to recommend a default preset, because in some
  23. * cases keeping the resource usage relatively low is more important that
  24. * getting the maximum compression ratio.
  25. */
  26. #define LZMA_PRESET_DEFAULT UINT32_C(6)
  27. /**
  28. * \brief Mask for preset level
  29. *
  30. * This is useful only if you need to extract the level from the preset
  31. * variable. That should be rare.
  32. */
  33. #define LZMA_PRESET_LEVEL_MASK UINT32_C(0x1F)
  34. /*
  35. * Preset flags
  36. *
  37. * Currently only one flag is defined.
  38. */
  39. /**
  40. * \brief Extreme compression preset
  41. *
  42. * This flag modifies the preset to make the encoding significantly slower
  43. * while improving the compression ratio only marginally. This is useful
  44. * when you don't mind wasting time to get as small result as possible.
  45. *
  46. * This flag doesn't affect the memory usage requirements of the decoder (at
  47. * least not significantly). The memory usage of the encoder may be increased
  48. * a little but only at the lowest preset levels (0-3).
  49. */
  50. #define LZMA_PRESET_EXTREME (UINT32_C(1) << 31)
  51. /**
  52. * \brief Calculate approximate memory usage of easy encoder
  53. *
  54. * This function is a wrapper for lzma_raw_encoder_memusage().
  55. *
  56. * \param preset Compression preset (level and possible flags)
  57. *
  58. * \return Number of bytes of memory required for the given
  59. * preset when encoding. If an error occurs, for example
  60. * due to unsupported preset, UINT64_MAX is returned.
  61. */
  62. extern LZMA_API(uint64_t) lzma_easy_encoder_memusage(uint32_t preset)
  63. lzma_nothrow lzma_attr_pure;
  64. /**
  65. * \brief Calculate approximate decoder memory usage of a preset
  66. *
  67. * This function is a wrapper for lzma_raw_decoder_memusage().
  68. *
  69. * \param preset Compression preset (level and possible flags)
  70. *
  71. * \return Number of bytes of memory required to decompress a file
  72. * that was compressed using the given preset. If an error
  73. * occurs, for example due to unsupported preset, UINT64_MAX
  74. * is returned.
  75. */
  76. extern LZMA_API(uint64_t) lzma_easy_decoder_memusage(uint32_t preset)
  77. lzma_nothrow lzma_attr_pure;
  78. /**
  79. * \brief Initialize .xz Stream encoder using a preset number
  80. *
  81. * This function is intended for those who just want to use the basic features
  82. * if liblzma (that is, most developers out there).
  83. *
  84. * \param strm Pointer to lzma_stream that is at least initialized
  85. * with LZMA_STREAM_INIT.
  86. * \param preset Compression preset to use. A preset consist of level
  87. * number and zero or more flags. Usually flags aren't
  88. * used, so preset is simply a number [0, 9] which match
  89. * the options -0 ... -9 of the xz command line tool.
  90. * Additional flags can be be set using bitwise-or with
  91. * the preset level number, e.g. 6 | LZMA_PRESET_EXTREME.
  92. * \param check Integrity check type to use. See check.h for available
  93. * checks. The xz command line tool defaults to
  94. * LZMA_CHECK_CRC64, which is a good choice if you are
  95. * unsure. LZMA_CHECK_CRC32 is good too as long as the
  96. * uncompressed file is not many gigabytes.
  97. *
  98. * \return - LZMA_OK: Initialization succeeded. Use lzma_code() to
  99. * encode your data.
  100. * - LZMA_MEM_ERROR: Memory allocation failed.
  101. * - LZMA_OPTIONS_ERROR: The given compression preset is not
  102. * supported by this build of liblzma.
  103. * - LZMA_UNSUPPORTED_CHECK: The given check type is not
  104. * supported by this liblzma build.
  105. * - LZMA_PROG_ERROR: One or more of the parameters have values
  106. * that will never be valid. For example, strm == NULL.
  107. *
  108. * If initialization fails (return value is not LZMA_OK), all the memory
  109. * allocated for *strm by liblzma is always freed. Thus, there is no need
  110. * to call lzma_end() after failed initialization.
  111. *
  112. * If initialization succeeds, use lzma_code() to do the actual encoding.
  113. * Valid values for `action' (the second argument of lzma_code()) are
  114. * LZMA_RUN, LZMA_SYNC_FLUSH, LZMA_FULL_FLUSH, and LZMA_FINISH. In future,
  115. * there may be compression levels or flags that don't support LZMA_SYNC_FLUSH.
  116. */
  117. extern LZMA_API(lzma_ret) lzma_easy_encoder(
  118. lzma_stream *strm, uint32_t preset, lzma_check check)
  119. lzma_nothrow lzma_attr_warn_unused_result;
  120. /**
  121. * \brief Single-call .xz Stream encoding using a preset number
  122. *
  123. * The maximum required output buffer size can be calculated with
  124. * lzma_stream_buffer_bound().
  125. *
  126. * \param preset Compression preset to use. See the description
  127. * in lzma_easy_encoder().
  128. * \param check Type of the integrity check to calculate from
  129. * uncompressed data.
  130. * \param allocator lzma_allocator for custom allocator functions.
  131. * Set to NULL to use malloc() and free().
  132. * \param in Beginning of the input buffer
  133. * \param in_size Size of the input buffer
  134. * \param out Beginning of the output buffer
  135. * \param out_pos The next byte will be written to out[*out_pos].
  136. * *out_pos is updated only if encoding succeeds.
  137. * \param out_size Size of the out buffer; the first byte into
  138. * which no data is written to is out[out_size].
  139. *
  140. * \return - LZMA_OK: Encoding was successful.
  141. * - LZMA_BUF_ERROR: Not enough output buffer space.
  142. * - LZMA_UNSUPPORTED_CHECK
  143. * - LZMA_OPTIONS_ERROR
  144. * - LZMA_MEM_ERROR
  145. * - LZMA_DATA_ERROR
  146. * - LZMA_PROG_ERROR
  147. */
  148. extern LZMA_API(lzma_ret) lzma_easy_buffer_encode(
  149. uint32_t preset, lzma_check check,
  150. lzma_allocator *allocator, const uint8_t *in, size_t in_size,
  151. uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
  152. /**
  153. * \brief Initialize .xz Stream encoder using a custom filter chain
  154. *
  155. * \param strm Pointer to properly prepared lzma_stream
  156. * \param filters Array of filters. This must be terminated with
  157. * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h for
  158. * more information.
  159. * \param check Type of the integrity check to calculate from
  160. * uncompressed data.
  161. *
  162. * \return - LZMA_OK: Initialization was successful.
  163. * - LZMA_MEM_ERROR
  164. * - LZMA_UNSUPPORTED_CHECK
  165. * - LZMA_OPTIONS_ERROR
  166. * - LZMA_PROG_ERROR
  167. */
  168. extern LZMA_API(lzma_ret) lzma_stream_encoder(lzma_stream *strm,
  169. const lzma_filter *filters, lzma_check check)
  170. lzma_nothrow lzma_attr_warn_unused_result;
  171. /**
  172. * \brief Initialize .lzma encoder (legacy file format)
  173. *
  174. * The .lzma format is sometimes called the LZMA_Alone format, which is the
  175. * reason for the name of this function. The .lzma format supports only the
  176. * LZMA1 filter. There is no support for integrity checks like CRC32.
  177. *
  178. * Use this function if and only if you need to create files readable by
  179. * legacy LZMA tools such as LZMA Utils 4.32.x. Moving to the .xz format
  180. * is strongly recommended.
  181. *
  182. * The valid action values for lzma_code() are LZMA_RUN and LZMA_FINISH.
  183. * No kind of flushing is supported, because the file format doesn't make
  184. * it possible.
  185. *
  186. * \return - LZMA_OK
  187. * - LZMA_MEM_ERROR
  188. * - LZMA_OPTIONS_ERROR
  189. * - LZMA_PROG_ERROR
  190. */
  191. extern LZMA_API(lzma_ret) lzma_alone_encoder(
  192. lzma_stream *strm, const lzma_options_lzma *options)
  193. lzma_nothrow lzma_attr_warn_unused_result;
  194. /**
  195. * \brief Calculate output buffer size for single-call Stream encoder
  196. *
  197. * When trying to compress uncompressible data, the encoded size will be
  198. * slightly bigger than the input data. This function calculates how much
  199. * output buffer space is required to be sure that lzma_stream_buffer_encode()
  200. * doesn't return LZMA_BUF_ERROR.
  201. *
  202. * The calculated value is not exact, but it is guaranteed to be big enough.
  203. * The actual maximum output space required may be slightly smaller (up to
  204. * about 100 bytes). This should not be a problem in practice.
  205. *
  206. * If the calculated maximum size doesn't fit into size_t or would make the
  207. * Stream grow past LZMA_VLI_MAX (which should never happen in practice),
  208. * zero is returned to indicate the error.
  209. *
  210. * \note The limit calculated by this function applies only to
  211. * single-call encoding. Multi-call encoding may (and probably
  212. * will) have larger maximum expansion when encoding
  213. * uncompressible data. Currently there is no function to
  214. * calculate the maximum expansion of multi-call encoding.
  215. */
  216. extern LZMA_API(size_t) lzma_stream_buffer_bound(size_t uncompressed_size)
  217. lzma_nothrow;
  218. /**
  219. * \brief Single-call .xz Stream encoder
  220. *
  221. * \param filters Array of filters. This must be terminated with
  222. * filters[n].id = LZMA_VLI_UNKNOWN. See filter.h
  223. * for more information.
  224. * \param check Type of the integrity check to calculate from
  225. * uncompressed data.
  226. * \param allocator lzma_allocator for custom allocator functions.
  227. * Set to NULL to use malloc() and free().
  228. * \param in Beginning of the input buffer
  229. * \param in_size Size of the input buffer
  230. * \param out Beginning of the output buffer
  231. * \param out_pos The next byte will be written to out[*out_pos].
  232. * *out_pos is updated only if encoding succeeds.
  233. * \param out_size Size of the out buffer; the first byte into
  234. * which no data is written to is out[out_size].
  235. *
  236. * \return - LZMA_OK: Encoding was successful.
  237. * - LZMA_BUF_ERROR: Not enough output buffer space.
  238. * - LZMA_UNSUPPORTED_CHECK
  239. * - LZMA_OPTIONS_ERROR
  240. * - LZMA_MEM_ERROR
  241. * - LZMA_DATA_ERROR
  242. * - LZMA_PROG_ERROR
  243. */
  244. extern LZMA_API(lzma_ret) lzma_stream_buffer_encode(
  245. lzma_filter *filters, lzma_check check,
  246. lzma_allocator *allocator, const uint8_t *in, size_t in_size,
  247. uint8_t *out, size_t *out_pos, size_t out_size)
  248. lzma_nothrow lzma_attr_warn_unused_result;
  249. /************
  250. * Decoding *
  251. ************/
  252. /**
  253. * This flag makes lzma_code() return LZMA_NO_CHECK if the input stream
  254. * being decoded has no integrity check. Note that when used with
  255. * lzma_auto_decoder(), all .lzma files will trigger LZMA_NO_CHECK
  256. * if LZMA_TELL_NO_CHECK is used.
  257. */
  258. #define LZMA_TELL_NO_CHECK UINT32_C(0x01)
  259. /**
  260. * This flag makes lzma_code() return LZMA_UNSUPPORTED_CHECK if the input
  261. * stream has an integrity check, but the type of the integrity check is not
  262. * supported by this liblzma version or build. Such files can still be
  263. * decoded, but the integrity check cannot be verified.
  264. */
  265. #define LZMA_TELL_UNSUPPORTED_CHECK UINT32_C(0x02)
  266. /**
  267. * This flag makes lzma_code() return LZMA_GET_CHECK as soon as the type
  268. * of the integrity check is known. The type can then be got with
  269. * lzma_get_check().
  270. */
  271. #define LZMA_TELL_ANY_CHECK UINT32_C(0x04)
  272. /**
  273. * This flag enables decoding of concatenated files with file formats that
  274. * allow concatenating compressed files as is. From the formats currently
  275. * supported by liblzma, only the .xz format allows concatenated files.
  276. * Concatenated files are not allowed with the legacy .lzma format.
  277. *
  278. * This flag also affects the usage of the `action' argument for lzma_code().
  279. * When LZMA_CONCATENATED is used, lzma_code() won't return LZMA_STREAM_END
  280. * unless LZMA_FINISH is used as `action'. Thus, the application has to set
  281. * LZMA_FINISH in the same way as it does when encoding.
  282. *
  283. * If LZMA_CONCATENATED is not used, the decoders still accept LZMA_FINISH
  284. * as `action' for lzma_code(), but the usage of LZMA_FINISH isn't required.
  285. */
  286. #define LZMA_CONCATENATED UINT32_C(0x08)
  287. /**
  288. * \brief Initialize .xz Stream decoder
  289. *
  290. * \param strm Pointer to properly prepared lzma_stream
  291. * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
  292. * to effectively disable the limiter.
  293. * \param flags Bitwise-or of zero or more of the decoder flags:
  294. * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
  295. * LZMA_TELL_ANY_CHECK, LZMA_CONCATENATED
  296. *
  297. * \return - LZMA_OK: Initialization was successful.
  298. * - LZMA_MEM_ERROR: Cannot allocate memory.
  299. * - LZMA_OPTIONS_ERROR: Unsupported flags
  300. * - LZMA_PROG_ERROR
  301. */
  302. extern LZMA_API(lzma_ret) lzma_stream_decoder(
  303. lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  304. lzma_nothrow lzma_attr_warn_unused_result;
  305. /**
  306. * \brief Decode .xz Streams and .lzma files with autodetection
  307. *
  308. * This decoder autodetects between the .xz and .lzma file formats, and
  309. * calls lzma_stream_decoder() or lzma_alone_decoder() once the type
  310. * of the input file has been detected.
  311. *
  312. * \param strm Pointer to properly prepared lzma_stream
  313. * \param memlimit Memory usage limit as bytes. Use UINT64_MAX
  314. * to effectively disable the limiter.
  315. * \param flags Bitwise-or of flags, or zero for no flags.
  316. *
  317. * \return - LZMA_OK: Initialization was successful.
  318. * - LZMA_MEM_ERROR: Cannot allocate memory.
  319. * - LZMA_OPTIONS_ERROR: Unsupported flags
  320. * - LZMA_PROG_ERROR
  321. */
  322. extern LZMA_API(lzma_ret) lzma_auto_decoder(
  323. lzma_stream *strm, uint64_t memlimit, uint32_t flags)
  324. lzma_nothrow lzma_attr_warn_unused_result;
  325. /**
  326. * \brief Initialize .lzma decoder (legacy file format)
  327. *
  328. * Valid `action' arguments to lzma_code() are LZMA_RUN and LZMA_FINISH.
  329. * There is no need to use LZMA_FINISH, but allowing it may simplify
  330. * certain types of applications.
  331. *
  332. * \return - LZMA_OK
  333. * - LZMA_MEM_ERROR
  334. * - LZMA_PROG_ERROR
  335. */
  336. extern LZMA_API(lzma_ret) lzma_alone_decoder(
  337. lzma_stream *strm, uint64_t memlimit)
  338. lzma_nothrow lzma_attr_warn_unused_result;
  339. /**
  340. * \brief Single-call .xz Stream decoder
  341. *
  342. * \param memlimit Pointer to how much memory the decoder is allowed
  343. * to allocate. The value pointed by this pointer is
  344. * modified if and only if LZMA_MEMLIMIT_ERROR is
  345. * returned.
  346. * \param flags Bitwise-or of zero or more of the decoder flags:
  347. * LZMA_TELL_NO_CHECK, LZMA_TELL_UNSUPPORTED_CHECK,
  348. * LZMA_CONCATENATED. Note that LZMA_TELL_ANY_CHECK
  349. * is not allowed and will return LZMA_PROG_ERROR.
  350. * \param allocator lzma_allocator for custom allocator functions.
  351. * Set to NULL to use malloc() and free().
  352. * \param in Beginning of the input buffer
  353. * \param in_pos The next byte will be read from in[*in_pos].
  354. * *in_pos is updated only if decoding succeeds.
  355. * \param in_size Size of the input buffer; the first byte that
  356. * won't be read is in[in_size].
  357. * \param out Beginning of the output buffer
  358. * \param out_pos The next byte will be written to out[*out_pos].
  359. * *out_pos is updated only if decoding succeeds.
  360. * \param out_size Size of the out buffer; the first byte into
  361. * which no data is written to is out[out_size].
  362. *
  363. * \return - LZMA_OK: Decoding was successful.
  364. * - LZMA_FORMAT_ERROR
  365. * - LZMA_OPTIONS_ERROR
  366. * - LZMA_DATA_ERROR
  367. * - LZMA_NO_CHECK: This can be returned only if using
  368. * the LZMA_TELL_NO_CHECK flag.
  369. * - LZMA_UNSUPPORTED_CHECK: This can be returned only if using
  370. * the LZMA_TELL_UNSUPPORTED_CHECK flag.
  371. * - LZMA_MEM_ERROR
  372. * - LZMA_MEMLIMIT_ERROR: Memory usage limit was reached.
  373. * The minimum required memlimit value was stored to *memlimit.
  374. * - LZMA_BUF_ERROR: Output buffer was too small.
  375. * - LZMA_PROG_ERROR
  376. */
  377. extern LZMA_API(lzma_ret) lzma_stream_buffer_decode(
  378. uint64_t *memlimit, uint32_t flags, lzma_allocator *allocator,
  379. const uint8_t *in, size_t *in_pos, size_t in_size,
  380. uint8_t *out, size_t *out_pos, size_t out_size)
  381. lzma_nothrow lzma_attr_warn_unused_result;