lzma_common.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file lzma_common.h
  4. /// \brief Private definitions common to LZMA encoder and decoder
  5. ///
  6. // Authors: Igor Pavlov
  7. // 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. ///////////////////////////////////////////////////////////////////////////////
  13. #ifndef LZMA_LZMA_COMMON_H
  14. #define LZMA_LZMA_COMMON_H
  15. #include "common.h"
  16. #include "range_common.h"
  17. ///////////////////
  18. // Miscellaneous //
  19. ///////////////////
  20. /// Maximum number of position states. A position state is the lowest pos bits
  21. /// number of bits of the current uncompressed offset. In some places there
  22. /// are different sets of probabilities for different pos states.
  23. #define POS_STATES_MAX (1 << LZMA_PB_MAX)
  24. /// Validates lc, lp, and pb.
  25. static inline bool
  26. is_lclppb_valid(const lzma_options_lzma *options)
  27. {
  28. return options->lc <= LZMA_LCLP_MAX && options->lp <= LZMA_LCLP_MAX
  29. && options->lc + options->lp <= LZMA_LCLP_MAX
  30. && options->pb <= LZMA_PB_MAX;
  31. }
  32. ///////////
  33. // State //
  34. ///////////
  35. /// This enum is used to track which events have occurred most recently and
  36. /// in which order. This information is used to predict the next event.
  37. ///
  38. /// Events:
  39. /// - Literal: One 8-bit byte
  40. /// - Match: Repeat a chunk of data at some distance
  41. /// - Long repeat: Multi-byte match at a recently seen distance
  42. /// - Short repeat: One-byte repeat at a recently seen distance
  43. ///
  44. /// The event names are in from STATE_oldest_older_previous. REP means
  45. /// either short or long repeated match, and NONLIT means any non-literal.
  46. typedef enum {
  47. STATE_LIT_LIT,
  48. STATE_MATCH_LIT_LIT,
  49. STATE_REP_LIT_LIT,
  50. STATE_SHORTREP_LIT_LIT,
  51. STATE_MATCH_LIT,
  52. STATE_REP_LIT,
  53. STATE_SHORTREP_LIT,
  54. STATE_LIT_MATCH,
  55. STATE_LIT_LONGREP,
  56. STATE_LIT_SHORTREP,
  57. STATE_NONLIT_MATCH,
  58. STATE_NONLIT_REP,
  59. } lzma_lzma_state;
  60. /// Total number of states
  61. #define STATES 12
  62. /// The lowest 7 states indicate that the previous state was a literal.
  63. #define LIT_STATES 7
  64. /// Indicate that the latest state was a literal.
  65. #define update_literal(state) \
  66. state = ((state) <= STATE_SHORTREP_LIT_LIT \
  67. ? STATE_LIT_LIT \
  68. : ((state) <= STATE_LIT_SHORTREP \
  69. ? (state) - 3 \
  70. : (state) - 6))
  71. /// Indicate that the latest state was a match.
  72. #define update_match(state) \
  73. state = ((state) < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH)
  74. /// Indicate that the latest state was a long repeated match.
  75. #define update_long_rep(state) \
  76. state = ((state) < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP)
  77. /// Indicate that the latest state was a short match.
  78. #define update_short_rep(state) \
  79. state = ((state) < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP)
  80. /// Test if the previous state was a literal.
  81. #define is_literal_state(state) \
  82. ((state) < LIT_STATES)
  83. /////////////
  84. // Literal //
  85. /////////////
  86. /// Each literal coder is divided in three sections:
  87. /// - 0x001-0x0FF: Without match byte
  88. /// - 0x101-0x1FF: With match byte; match bit is 0
  89. /// - 0x201-0x2FF: With match byte; match bit is 1
  90. ///
  91. /// Match byte is used when the previous LZMA symbol was something else than
  92. /// a literal (that is, it was some kind of match).
  93. #define LITERAL_CODER_SIZE 0x300
  94. /// Maximum number of literal coders
  95. #define LITERAL_CODERS_MAX (1 << LZMA_LCLP_MAX)
  96. /// Locate the literal coder for the next literal byte. The choice depends on
  97. /// - the lowest literal_pos_bits bits of the position of the current
  98. /// byte; and
  99. /// - the highest literal_context_bits bits of the previous byte.
  100. #define literal_subcoder(probs, lc, lp_mask, pos, prev_byte) \
  101. ((probs)[(((pos) & lp_mask) << lc) + ((prev_byte) >> (8 - lc))])
  102. static inline void
  103. literal_init(probability (*probs)[LITERAL_CODER_SIZE],
  104. uint32_t lc, uint32_t lp)
  105. {
  106. uint32_t coders;
  107. uint32_t i, j;
  108. assert(lc + lp <= LZMA_LCLP_MAX);
  109. coders = 1U << (lc + lp);
  110. for (i = 0; i < coders; ++i)
  111. for (j = 0; j < LITERAL_CODER_SIZE; ++j)
  112. bit_reset(probs[i][j]);
  113. return;
  114. }
  115. //////////////////
  116. // Match length //
  117. //////////////////
  118. // Minimum length of a match is two bytes.
  119. #define MATCH_LEN_MIN 2
  120. // Match length is encoded with 4, 5, or 10 bits.
  121. //
  122. // Length Bits
  123. // 2-9 4 = Choice=0 + 3 bits
  124. // 10-17 5 = Choice=1 + Choice2=0 + 3 bits
  125. // 18-273 10 = Choice=1 + Choice2=1 + 8 bits
  126. #define LEN_LOW_BITS 3
  127. #define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
  128. #define LEN_MID_BITS 3
  129. #define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
  130. #define LEN_HIGH_BITS 8
  131. #define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
  132. #define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
  133. // Maximum length of a match is 273 which is a result of the encoding
  134. // described above.
  135. #define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
  136. ////////////////////
  137. // Match distance //
  138. ////////////////////
  139. // Different set of probabilities is used for match distances that have very
  140. // short match length: Lengths of 2, 3, and 4 bytes have a separate set of
  141. // probabilities for each length. The matches with longer length use a shared
  142. // set of probabilities.
  143. #define LEN_TO_POS_STATES 4
  144. // Macro to get the index of the appropriate probability array.
  145. #define get_len_to_pos_state(len) \
  146. ((len) < LEN_TO_POS_STATES + MATCH_LEN_MIN \
  147. ? (len) - MATCH_LEN_MIN \
  148. : LEN_TO_POS_STATES - 1)
  149. // The highest two bits of a match distance (pos slot) are encoded using six
  150. // bits. See fastpos.h for more explanation.
  151. #define POS_SLOT_BITS 6
  152. #define POS_SLOTS (1 << POS_SLOT_BITS)
  153. // Match distances up to 127 are fully encoded using probabilities. Since
  154. // the highest two bits (pos slot) are always encoded using six bits, the
  155. // distances 0-3 don't need any additional bits to encode, since the pos
  156. // slot itself is the same as the actual distance. START_POS_MODEL_INDEX
  157. // indicates the first pos slot where at least one additional bit is needed.
  158. #define START_POS_MODEL_INDEX 4
  159. // Match distances greater than 127 are encoded in three pieces:
  160. // - pos slot: the highest two bits
  161. // - direct bits: 2-26 bits below the highest two bits
  162. // - alignment bits: four lowest bits
  163. //
  164. // Direct bits don't use any probabilities.
  165. //
  166. // The pos slot value of 14 is for distances 128-191 (see the table in
  167. // fastpos.h to understand why).
  168. #define END_POS_MODEL_INDEX 14
  169. // Pos slots that indicate a distance <= 127.
  170. #define FULL_DISTANCES_BITS (END_POS_MODEL_INDEX / 2)
  171. #define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
  172. // For match distances greater than 127, only the highest two bits and the
  173. // lowest four bits (alignment) is encoded using probabilities.
  174. #define ALIGN_BITS 4
  175. #define ALIGN_TABLE_SIZE (1 << ALIGN_BITS)
  176. #define ALIGN_MASK (ALIGN_TABLE_SIZE - 1)
  177. // LZMA remembers the four most recent match distances. Reusing these distances
  178. // tends to take less space than re-encoding the actual distance value.
  179. #define REP_DISTANCES 4
  180. #endif