1
0

lzma12.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. * \file lzma/lzma12.h
  3. * \brief LZMA1 and LZMA2 filters
  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. * \brief LZMA1 Filter ID
  18. *
  19. * LZMA1 is the very same thing as what was called just LZMA in LZMA Utils,
  20. * 7-Zip, and LZMA SDK. It's called LZMA1 here to prevent developers from
  21. * accidentally using LZMA when they actually want LZMA2.
  22. *
  23. * LZMA1 shouldn't be used for new applications unless you _really_ know
  24. * what you are doing. LZMA2 is almost always a better choice.
  25. */
  26. #define LZMA_FILTER_LZMA1 LZMA_VLI_C(0x4000000000000001)
  27. /**
  28. * \brief LZMA2 Filter ID
  29. *
  30. * Usually you want this instead of LZMA1. Compared to LZMA1, LZMA2 adds
  31. * support for LZMA_SYNC_FLUSH, uncompressed chunks (smaller expansion
  32. * when trying to compress uncompressible data), possibility to change
  33. * lc/lp/pb in the middle of encoding, and some other internal improvements.
  34. */
  35. #define LZMA_FILTER_LZMA2 LZMA_VLI_C(0x21)
  36. /**
  37. * \brief Match finders
  38. *
  39. * Match finder has major effect on both speed and compression ratio.
  40. * Usually hash chains are faster than binary trees.
  41. *
  42. * If you will use LZMA_SYNC_FLUSH often, the hash chains may be a better
  43. * choice, because binary trees get much higher compression ratio penalty
  44. * with LZMA_SYNC_FLUSH.
  45. *
  46. * The memory usage formulas are only rough estimates, which are closest to
  47. * reality when dict_size is a power of two. The formulas are more complex
  48. * in reality, and can also change a little between liblzma versions. Use
  49. * lzma_raw_encoder_memusage() to get more accurate estimate of memory usage.
  50. */
  51. typedef enum {
  52. LZMA_MF_HC3 = 0x03,
  53. /**<
  54. * \brief Hash Chain with 2- and 3-byte hashing
  55. *
  56. * Minimum nice_len: 3
  57. *
  58. * Memory usage:
  59. * - dict_size <= 16 MiB: dict_size * 7.5
  60. * - dict_size > 16 MiB: dict_size * 5.5 + 64 MiB
  61. */
  62. LZMA_MF_HC4 = 0x04,
  63. /**<
  64. * \brief Hash Chain with 2-, 3-, and 4-byte hashing
  65. *
  66. * Minimum nice_len: 4
  67. *
  68. * Memory usage:
  69. * - dict_size <= 32 MiB: dict_size * 7.5
  70. * - dict_size > 32 MiB: dict_size * 6.5
  71. */
  72. LZMA_MF_BT2 = 0x12,
  73. /**<
  74. * \brief Binary Tree with 2-byte hashing
  75. *
  76. * Minimum nice_len: 2
  77. *
  78. * Memory usage: dict_size * 9.5
  79. */
  80. LZMA_MF_BT3 = 0x13,
  81. /**<
  82. * \brief Binary Tree with 2- and 3-byte hashing
  83. *
  84. * Minimum nice_len: 3
  85. *
  86. * Memory usage:
  87. * - dict_size <= 16 MiB: dict_size * 11.5
  88. * - dict_size > 16 MiB: dict_size * 9.5 + 64 MiB
  89. */
  90. LZMA_MF_BT4 = 0x14
  91. /**<
  92. * \brief Binary Tree with 2-, 3-, and 4-byte hashing
  93. *
  94. * Minimum nice_len: 4
  95. *
  96. * Memory usage:
  97. * - dict_size <= 32 MiB: dict_size * 11.5
  98. * - dict_size > 32 MiB: dict_size * 10.5
  99. */
  100. } lzma_match_finder;
  101. /**
  102. * \brief Test if given match finder is supported
  103. *
  104. * Return true if the given match finder is supported by this liblzma build.
  105. * Otherwise false is returned. It is safe to call this with a value that
  106. * isn't listed in lzma_match_finder enumeration; the return value will be
  107. * false.
  108. *
  109. * There is no way to list which match finders are available in this
  110. * particular liblzma version and build. It would be useless, because
  111. * a new match finder, which the application developer wasn't aware,
  112. * could require giving additional options to the encoder that the older
  113. * match finders don't need.
  114. */
  115. extern LZMA_API(lzma_bool) lzma_mf_is_supported(lzma_match_finder match_finder)
  116. lzma_nothrow lzma_attr_const;
  117. /**
  118. * \brief Compression modes
  119. *
  120. * This selects the function used to analyze the data produced by the match
  121. * finder.
  122. */
  123. typedef enum {
  124. LZMA_MODE_FAST = 1,
  125. /**<
  126. * \brief Fast compression
  127. *
  128. * Fast mode is usually at its best when combined with
  129. * a hash chain match finder.
  130. */
  131. LZMA_MODE_NORMAL = 2
  132. /**<
  133. * \brief Normal compression
  134. *
  135. * This is usually notably slower than fast mode. Use this
  136. * together with binary tree match finders to expose the
  137. * full potential of the LZMA1 or LZMA2 encoder.
  138. */
  139. } lzma_mode;
  140. /**
  141. * \brief Test if given compression mode is supported
  142. *
  143. * Return true if the given compression mode is supported by this liblzma
  144. * build. Otherwise false is returned. It is safe to call this with a value
  145. * that isn't listed in lzma_mode enumeration; the return value will be false.
  146. *
  147. * There is no way to list which modes are available in this particular
  148. * liblzma version and build. It would be useless, because a new compression
  149. * mode, which the application developer wasn't aware, could require giving
  150. * additional options to the encoder that the older modes don't need.
  151. */
  152. extern LZMA_API(lzma_bool) lzma_mode_is_supported(lzma_mode mode)
  153. lzma_nothrow lzma_attr_const;
  154. /**
  155. * \brief Options specific to the LZMA1 and LZMA2 filters
  156. *
  157. * Since LZMA1 and LZMA2 share most of the code, it's simplest to share
  158. * the options structure too. For encoding, all but the reserved variables
  159. * need to be initialized unless specifically mentioned otherwise.
  160. * lzma_lzma_preset() can be used to get a good starting point.
  161. *
  162. * For raw decoding, both LZMA1 and LZMA2 need dict_size, preset_dict, and
  163. * preset_dict_size (if preset_dict != NULL). LZMA1 needs also lc, lp, and pb.
  164. */
  165. typedef struct {
  166. /**
  167. * \brief Dictionary size in bytes
  168. *
  169. * Dictionary size indicates how many bytes of the recently processed
  170. * uncompressed data is kept in memory. One method to reduce size of
  171. * the uncompressed data is to store distance-length pairs, which
  172. * indicate what data to repeat from the dictionary buffer. Thus,
  173. * the bigger the dictionary, the better the compression ratio
  174. * usually is.
  175. *
  176. * Maximum size of the dictionary depends on multiple things:
  177. * - Memory usage limit
  178. * - Available address space (not a problem on 64-bit systems)
  179. * - Selected match finder (encoder only)
  180. *
  181. * Currently the maximum dictionary size for encoding is 1.5 GiB
  182. * (i.e. (UINT32_C(1) << 30) + (UINT32_C(1) << 29)) even on 64-bit
  183. * systems for certain match finder implementation reasons. In the
  184. * future, there may be match finders that support bigger
  185. * dictionaries.
  186. *
  187. * Decoder already supports dictionaries up to 4 GiB - 1 B (i.e.
  188. * UINT32_MAX), so increasing the maximum dictionary size of the
  189. * encoder won't cause problems for old decoders.
  190. *
  191. * Because extremely small dictionaries sizes would have unneeded
  192. * overhead in the decoder, the minimum dictionary size is 4096 bytes.
  193. *
  194. * \note When decoding, too big dictionary does no other harm
  195. * than wasting memory.
  196. */
  197. uint32_t dict_size;
  198. # define LZMA_DICT_SIZE_MIN UINT32_C(4096)
  199. # define LZMA_DICT_SIZE_DEFAULT (UINT32_C(1) << 23)
  200. /**
  201. * \brief Pointer to an initial dictionary
  202. *
  203. * It is possible to initialize the LZ77 history window using
  204. * a preset dictionary. It is useful when compressing many
  205. * similar, relatively small chunks of data independently from
  206. * each other. The preset dictionary should contain typical
  207. * strings that occur in the files being compressed. The most
  208. * probable strings should be near the end of the preset dictionary.
  209. *
  210. * This feature should be used only in special situations. For
  211. * now, it works correctly only with raw encoding and decoding.
  212. * Currently none of the container formats supported by
  213. * liblzma allow preset dictionary when decoding, thus if
  214. * you create a .xz or .lzma file with preset dictionary, it
  215. * cannot be decoded with the regular decoder functions. In the
  216. * future, the .xz format will likely get support for preset
  217. * dictionary though.
  218. */
  219. const uint8_t *preset_dict;
  220. /**
  221. * \brief Size of the preset dictionary
  222. *
  223. * Specifies the size of the preset dictionary. If the size is
  224. * bigger than dict_size, only the last dict_size bytes are
  225. * processed.
  226. *
  227. * This variable is read only when preset_dict is not NULL.
  228. * If preset_dict is not NULL but preset_dict_size is zero,
  229. * no preset dictionary is used (identical to only setting
  230. * preset_dict to NULL).
  231. */
  232. uint32_t preset_dict_size;
  233. /**
  234. * \brief Number of literal context bits
  235. *
  236. * How many of the highest bits of the previous uncompressed
  237. * eight-bit byte (also known as `literal') are taken into
  238. * account when predicting the bits of the next literal.
  239. *
  240. * E.g. in typical English text, an upper-case letter is
  241. * often followed by a lower-case letter, and a lower-case
  242. * letter is usually followed by another lower-case letter.
  243. * In the US-ASCII character set, the highest three bits are 010
  244. * for upper-case letters and 011 for lower-case letters.
  245. * When lc is at least 3, the literal coding can take advantage of
  246. * this property in the uncompressed data.
  247. *
  248. * There is a limit that applies to literal context bits and literal
  249. * position bits together: lc + lp <= 4. Without this limit the
  250. * decoding could become very slow, which could have security related
  251. * results in some cases like email servers doing virus scanning.
  252. * This limit also simplifies the internal implementation in liblzma.
  253. *
  254. * There may be LZMA1 streams that have lc + lp > 4 (maximum possible
  255. * lc would be 8). It is not possible to decode such streams with
  256. * liblzma.
  257. */
  258. uint32_t lc;
  259. # define LZMA_LCLP_MIN 0
  260. # define LZMA_LCLP_MAX 4
  261. # define LZMA_LC_DEFAULT 3
  262. /**
  263. * \brief Number of literal position bits
  264. *
  265. * lp affects what kind of alignment in the uncompressed data is
  266. * assumed when encoding literals. A literal is a single 8-bit byte.
  267. * See pb below for more information about alignment.
  268. */
  269. uint32_t lp;
  270. # define LZMA_LP_DEFAULT 0
  271. /**
  272. * \brief Number of position bits
  273. *
  274. * pb affects what kind of alignment in the uncompressed data is
  275. * assumed in general. The default means four-byte alignment
  276. * (2^ pb =2^2=4), which is often a good choice when there's
  277. * no better guess.
  278. *
  279. * When the alignment is known, setting pb accordingly may reduce
  280. * the file size a little. E.g. with text files having one-byte
  281. * alignment (US-ASCII, ISO-8859-*, UTF-8), setting pb=0 can
  282. * improve compression slightly. For UTF-16 text, pb=1 is a good
  283. * choice. If the alignment is an odd number like 3 bytes, pb=0
  284. * might be the best choice.
  285. *
  286. * Even though the assumed alignment can be adjusted with pb and
  287. * lp, LZMA1 and LZMA2 still slightly favor 16-byte alignment.
  288. * It might be worth taking into account when designing file formats
  289. * that are likely to be often compressed with LZMA1 or LZMA2.
  290. */
  291. uint32_t pb;
  292. # define LZMA_PB_MIN 0
  293. # define LZMA_PB_MAX 4
  294. # define LZMA_PB_DEFAULT 2
  295. /** Compression mode */
  296. lzma_mode mode;
  297. /**
  298. * \brief Nice length of a match
  299. *
  300. * This determines how many bytes the encoder compares from the match
  301. * candidates when looking for the best match. Once a match of at
  302. * least nice_len bytes long is found, the encoder stops looking for
  303. * better candidates and encodes the match. (Naturally, if the found
  304. * match is actually longer than nice_len, the actual length is
  305. * encoded; it's not truncated to nice_len.)
  306. *
  307. * Bigger values usually increase the compression ratio and
  308. * compression time. For most files, 32 to 128 is a good value,
  309. * which gives very good compression ratio at good speed.
  310. *
  311. * The exact minimum value depends on the match finder. The maximum
  312. * is 273, which is the maximum length of a match that LZMA1 and
  313. * LZMA2 can encode.
  314. */
  315. uint32_t nice_len;
  316. /** Match finder ID */
  317. lzma_match_finder mf;
  318. /**
  319. * \brief Maximum search depth in the match finder
  320. *
  321. * For every input byte, match finder searches through the hash chain
  322. * or binary tree in a loop, each iteration going one step deeper in
  323. * the chain or tree. The searching stops if
  324. * - a match of at least nice_len bytes long is found;
  325. * - all match candidates from the hash chain or binary tree have
  326. * been checked; or
  327. * - maximum search depth is reached.
  328. *
  329. * Maximum search depth is needed to prevent the match finder from
  330. * wasting too much time in case there are lots of short match
  331. * candidates. On the other hand, stopping the search before all
  332. * candidates have been checked can reduce compression ratio.
  333. *
  334. * Setting depth to zero tells liblzma to use an automatic default
  335. * value, that depends on the selected match finder and nice_len.
  336. * The default is in the range [4, 200] or so (it may vary between
  337. * liblzma versions).
  338. *
  339. * Using a bigger depth value than the default can increase
  340. * compression ratio in some cases. There is no strict maximum value,
  341. * but high values (thousands or millions) should be used with care:
  342. * the encoder could remain fast enough with typical input, but
  343. * malicious input could cause the match finder to slow down
  344. * dramatically, possibly creating a denial of service attack.
  345. */
  346. uint32_t depth;
  347. /*
  348. * Reserved space to allow possible future extensions without
  349. * breaking the ABI. You should not touch these, because the names
  350. * of these variables may change. These are and will never be used
  351. * with the currently supported options, so it is safe to leave these
  352. * uninitialized.
  353. */
  354. uint32_t reserved_int1;
  355. uint32_t reserved_int2;
  356. uint32_t reserved_int3;
  357. uint32_t reserved_int4;
  358. uint32_t reserved_int5;
  359. uint32_t reserved_int6;
  360. uint32_t reserved_int7;
  361. uint32_t reserved_int8;
  362. lzma_reserved_enum reserved_enum1;
  363. lzma_reserved_enum reserved_enum2;
  364. lzma_reserved_enum reserved_enum3;
  365. lzma_reserved_enum reserved_enum4;
  366. void *reserved_ptr1;
  367. void *reserved_ptr2;
  368. } lzma_options_lzma;
  369. /**
  370. * \brief Set a compression preset to lzma_options_lzma structure
  371. *
  372. * 0 is the fastest and 9 is the slowest. These match the switches -0 .. -9
  373. * of the xz command line tool. In addition, it is possible to bitwise-or
  374. * flags to the preset. Currently only LZMA_PRESET_EXTREME is supported.
  375. * The flags are defined in container.h, because the flags are used also
  376. * with lzma_easy_encoder().
  377. *
  378. * The preset values are subject to changes between liblzma versions.
  379. *
  380. * This function is available only if LZMA1 or LZMA2 encoder has been enabled
  381. * when building liblzma.
  382. *
  383. * \return On success, false is returned. If the preset is not
  384. * supported, true is returned.
  385. */
  386. extern LZMA_API(lzma_bool) lzma_lzma_preset(
  387. lzma_options_lzma *options, uint32_t preset) lzma_nothrow;