xz_lzma2.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* SPDX-License-Identifier: 0BSD */
  2. /*
  3. * LZMA2 definitions
  4. *
  5. * Authors: Lasse Collin <lasse.collin@tukaani.org>
  6. * Igor Pavlov <https://7-zip.org/>
  7. */
  8. #ifndef XZ_LZMA2_H
  9. #define XZ_LZMA2_H
  10. /* Range coder constants */
  11. #define RC_SHIFT_BITS 8
  12. #define RC_TOP_BITS 24
  13. #define RC_TOP_VALUE (1 << RC_TOP_BITS)
  14. #define RC_BIT_MODEL_TOTAL_BITS 11
  15. #define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)
  16. #define RC_MOVE_BITS 5
  17. /*
  18. * Maximum number of position states. A position state is the lowest pb
  19. * number of bits of the current uncompressed offset. In some places there
  20. * are different sets of probabilities for different position states.
  21. */
  22. #define POS_STATES_MAX (1 << 4)
  23. /*
  24. * This enum is used to track which LZMA symbols have occurred most recently
  25. * and in which order. This information is used to predict the next symbol.
  26. *
  27. * Symbols:
  28. * - Literal: One 8-bit byte
  29. * - Match: Repeat a chunk of data at some distance
  30. * - Long repeat: Multi-byte match at a recently seen distance
  31. * - Short repeat: One-byte repeat at a recently seen distance
  32. *
  33. * The symbol names are in from STATE_oldest_older_previous. REP means
  34. * either short or long repeated match, and NONLIT means any non-literal.
  35. */
  36. enum lzma_state {
  37. STATE_LIT_LIT,
  38. STATE_MATCH_LIT_LIT,
  39. STATE_REP_LIT_LIT,
  40. STATE_SHORTREP_LIT_LIT,
  41. STATE_MATCH_LIT,
  42. STATE_REP_LIT,
  43. STATE_SHORTREP_LIT,
  44. STATE_LIT_MATCH,
  45. STATE_LIT_LONGREP,
  46. STATE_LIT_SHORTREP,
  47. STATE_NONLIT_MATCH,
  48. STATE_NONLIT_REP
  49. };
  50. /* Total number of states */
  51. #define STATES 12
  52. /* The lowest 7 states indicate that the previous state was a literal. */
  53. #define LIT_STATES 7
  54. /* Indicate that the latest symbol was a literal. */
  55. static inline void lzma_state_literal(enum lzma_state *state)
  56. {
  57. if (*state <= STATE_SHORTREP_LIT_LIT)
  58. *state = STATE_LIT_LIT;
  59. else if (*state <= STATE_LIT_SHORTREP)
  60. *state -= 3;
  61. else
  62. *state -= 6;
  63. }
  64. /* Indicate that the latest symbol was a match. */
  65. static inline void lzma_state_match(enum lzma_state *state)
  66. {
  67. *state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;
  68. }
  69. /* Indicate that the latest state was a long repeated match. */
  70. static inline void lzma_state_long_rep(enum lzma_state *state)
  71. {
  72. *state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;
  73. }
  74. /* Indicate that the latest symbol was a short match. */
  75. static inline void lzma_state_short_rep(enum lzma_state *state)
  76. {
  77. *state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;
  78. }
  79. /* Test if the previous symbol was a literal. */
  80. static inline bool lzma_state_is_literal(enum lzma_state state)
  81. {
  82. return state < LIT_STATES;
  83. }
  84. /* Each literal coder is divided in three sections:
  85. * - 0x001-0x0FF: Without match byte
  86. * - 0x101-0x1FF: With match byte; match bit is 0
  87. * - 0x201-0x2FF: With match byte; match bit is 1
  88. *
  89. * Match byte is used when the previous LZMA symbol was something else than
  90. * a literal (that is, it was some kind of match).
  91. */
  92. #define LITERAL_CODER_SIZE 0x300
  93. /* Maximum number of literal coders */
  94. #define LITERAL_CODERS_MAX (1 << 4)
  95. /* Minimum length of a match is two bytes. */
  96. #define MATCH_LEN_MIN 2
  97. /* Match length is encoded with 4, 5, or 10 bits.
  98. *
  99. * Length Bits
  100. * 2-9 4 = Choice=0 + 3 bits
  101. * 10-17 5 = Choice=1 + Choice2=0 + 3 bits
  102. * 18-273 10 = Choice=1 + Choice2=1 + 8 bits
  103. */
  104. #define LEN_LOW_BITS 3
  105. #define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
  106. #define LEN_MID_BITS 3
  107. #define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
  108. #define LEN_HIGH_BITS 8
  109. #define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
  110. #define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
  111. /*
  112. * Maximum length of a match is 273 which is a result of the encoding
  113. * described above.
  114. */
  115. #define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
  116. /*
  117. * Different sets of probabilities are used for match distances that have
  118. * very short match length: Lengths of 2, 3, and 4 bytes have a separate
  119. * set of probabilities for each length. The matches with longer length
  120. * use a shared set of probabilities.
  121. */
  122. #define DIST_STATES 4
  123. /*
  124. * Get the index of the appropriate probability array for decoding
  125. * the distance slot.
  126. */
  127. static inline uint32_t lzma_get_dist_state(uint32_t len)
  128. {
  129. return len < DIST_STATES + MATCH_LEN_MIN
  130. ? len - MATCH_LEN_MIN : DIST_STATES - 1;
  131. }
  132. /*
  133. * The highest two bits of a 32-bit match distance are encoded using six bits.
  134. * This six-bit value is called a distance slot. This way encoding a 32-bit
  135. * value takes 6-36 bits, larger values taking more bits.
  136. */
  137. #define DIST_SLOT_BITS 6
  138. #define DIST_SLOTS (1 << DIST_SLOT_BITS)
  139. /* Match distances up to 127 are fully encoded using probabilities. Since
  140. * the highest two bits (distance slot) are always encoded using six bits,
  141. * the distances 0-3 don't need any additional bits to encode, since the
  142. * distance slot itself is the same as the actual distance. DIST_MODEL_START
  143. * indicates the first distance slot where at least one additional bit is
  144. * needed.
  145. */
  146. #define DIST_MODEL_START 4
  147. /*
  148. * Match distances greater than 127 are encoded in three pieces:
  149. * - distance slot: the highest two bits
  150. * - direct bits: 2-26 bits below the highest two bits
  151. * - alignment bits: four lowest bits
  152. *
  153. * Direct bits don't use any probabilities.
  154. *
  155. * The distance slot value of 14 is for distances 128-191.
  156. */
  157. #define DIST_MODEL_END 14
  158. /* Distance slots that indicate a distance <= 127. */
  159. #define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
  160. #define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
  161. /*
  162. * For match distances greater than 127, only the highest two bits and the
  163. * lowest four bits (alignment) is encoded using probabilities.
  164. */
  165. #define ALIGN_BITS 4
  166. #define ALIGN_SIZE (1 << ALIGN_BITS)
  167. #define ALIGN_MASK (ALIGN_SIZE - 1)
  168. /* Total number of all probability variables */
  169. #define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)
  170. /*
  171. * LZMA remembers the four most recent match distances. Reusing these
  172. * distances tends to take less space than re-encoding the actual
  173. * distance value.
  174. */
  175. #define REPS 4
  176. #endif