xz_lzma2.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* xz_lzma2.h - LZMA2 definitions */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This file is based on code from XZ embedded project
  21. * http://tukaani.org/xz/embedded.html
  22. */
  23. #ifndef XZ_LZMA2_H
  24. #define XZ_LZMA2_H
  25. /* dictionary size hard limit
  26. * actual size limit is calculated as shown in 5.3.1
  27. * http://tukaani.org/xz/xz-file-format.txt
  28. *
  29. * if bits > 39 dictionary_size = UINT32_MAX
  30. * else
  31. * dictionary_size = 2 | (bits & 1);
  32. * dictionary_size <<= bits / 2 + 11;
  33. *
  34. * i.e.
  35. * 0 - 4 KiB
  36. * 6 - 32 KiB
  37. * 30 - 128MiB
  38. * 39 - 3072 MiB
  39. * 40 - 4096 MiB - 1 B
  40. * note: implementation supports 39 at maximum
  41. */
  42. #define DICT_BIT_SIZE 30
  43. /* Range coder constants */
  44. #define RC_SHIFT_BITS 8
  45. #define RC_TOP_BITS 24
  46. #define RC_TOP_VALUE (1 << RC_TOP_BITS)
  47. #define RC_BIT_MODEL_TOTAL_BITS 11
  48. #define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)
  49. #define RC_MOVE_BITS 5
  50. /*
  51. * Maximum number of position states. A position state is the lowest pb
  52. * number of bits of the current uncompressed offset. In some places there
  53. * are different sets of probabilities for different position states.
  54. */
  55. #define POS_STATES_MAX (1 << 4)
  56. /*
  57. * This enum is used to track which LZMA symbols have occurred most recently
  58. * and in which order. This information is used to predict the next symbol.
  59. *
  60. * Symbols:
  61. * - Literal: One 8-bit byte
  62. * - Match: Repeat a chunk of data at some distance
  63. * - Long repeat: Multi-byte match at a recently seen distance
  64. * - Short repeat: One-byte repeat at a recently seen distance
  65. *
  66. * The symbol names are in from STATE_oldest_older_previous. REP means
  67. * either short or long repeated match, and NONLIT means any non-literal.
  68. */
  69. enum lzma_state {
  70. STATE_LIT_LIT,
  71. STATE_MATCH_LIT_LIT,
  72. STATE_REP_LIT_LIT,
  73. STATE_SHORTREP_LIT_LIT,
  74. STATE_MATCH_LIT,
  75. STATE_REP_LIT,
  76. STATE_SHORTREP_LIT,
  77. STATE_LIT_MATCH,
  78. STATE_LIT_LONGREP,
  79. STATE_LIT_SHORTREP,
  80. STATE_NONLIT_MATCH,
  81. STATE_NONLIT_REP
  82. };
  83. /* Total number of states */
  84. #define STATES 12
  85. /* The lowest 7 states indicate that the previous state was a literal. */
  86. #define LIT_STATES 7
  87. /* Indicate that the latest symbol was a literal. */
  88. static inline void lzma_state_literal(enum lzma_state *state)
  89. {
  90. if (*state <= STATE_SHORTREP_LIT_LIT)
  91. *state = STATE_LIT_LIT;
  92. else if (*state <= STATE_LIT_SHORTREP)
  93. *state -= 3;
  94. else
  95. *state -= 6;
  96. }
  97. /* Indicate that the latest symbol was a match. */
  98. static inline void lzma_state_match(enum lzma_state *state)
  99. {
  100. *state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;
  101. }
  102. /* Indicate that the latest state was a long repeated match. */
  103. static inline void lzma_state_long_rep(enum lzma_state *state)
  104. {
  105. *state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;
  106. }
  107. /* Indicate that the latest symbol was a short match. */
  108. static inline void lzma_state_short_rep(enum lzma_state *state)
  109. {
  110. *state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;
  111. }
  112. /* Test if the previous symbol was a literal. */
  113. static inline bool lzma_state_is_literal(enum lzma_state state)
  114. {
  115. return state < LIT_STATES;
  116. }
  117. /* Each literal coder is divided in three sections:
  118. * - 0x001-0x0FF: Without match byte
  119. * - 0x101-0x1FF: With match byte; match bit is 0
  120. * - 0x201-0x2FF: With match byte; match bit is 1
  121. *
  122. * Match byte is used when the previous LZMA symbol was something else than
  123. * a literal (that is, it was some kind of match).
  124. */
  125. #define LITERAL_CODER_SIZE 0x300
  126. /* Maximum number of literal coders */
  127. #define LITERAL_CODERS_MAX (1 << 4)
  128. /* Minimum length of a match is two bytes. */
  129. #define MATCH_LEN_MIN 2
  130. /* Match length is encoded with 4, 5, or 10 bits.
  131. *
  132. * Length Bits
  133. * 2-9 4 = Choice=0 + 3 bits
  134. * 10-17 5 = Choice=1 + Choice2=0 + 3 bits
  135. * 18-273 10 = Choice=1 + Choice2=1 + 8 bits
  136. */
  137. #define LEN_LOW_BITS 3
  138. #define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
  139. #define LEN_MID_BITS 3
  140. #define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
  141. #define LEN_HIGH_BITS 8
  142. #define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
  143. #define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
  144. /*
  145. * Maximum length of a match is 273 which is a result of the encoding
  146. * described above.
  147. */
  148. #define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
  149. /*
  150. * Different sets of probabilities are used for match distances that have
  151. * very short match length: Lengths of 2, 3, and 4 bytes have a separate
  152. * set of probabilities for each length. The matches with longer length
  153. * use a shared set of probabilities.
  154. */
  155. #define DIST_STATES 4
  156. /*
  157. * Get the index of the appropriate probability array for decoding
  158. * the distance slot.
  159. */
  160. static inline uint32_t lzma_get_dist_state(uint32_t len)
  161. {
  162. return len < DIST_STATES + MATCH_LEN_MIN
  163. ? len - MATCH_LEN_MIN : DIST_STATES - 1;
  164. }
  165. /*
  166. * The highest two bits of a 32-bit match distance are encoded using six bits.
  167. * This six-bit value is called a distance slot. This way encoding a 32-bit
  168. * value takes 6-36 bits, larger values taking more bits.
  169. */
  170. #define DIST_SLOT_BITS 6
  171. #define DIST_SLOTS (1 << DIST_SLOT_BITS)
  172. /* Match distances up to 127 are fully encoded using probabilities. Since
  173. * the highest two bits (distance slot) are always encoded using six bits,
  174. * the distances 0-3 don't need any additional bits to encode, since the
  175. * distance slot itself is the same as the actual distance. DIST_MODEL_START
  176. * indicates the first distance slot where at least one additional bit is
  177. * needed.
  178. */
  179. #define DIST_MODEL_START 4
  180. /*
  181. * Match distances greater than 127 are encoded in three pieces:
  182. * - distance slot: the highest two bits
  183. * - direct bits: 2-26 bits below the highest two bits
  184. * - alignment bits: four lowest bits
  185. *
  186. * Direct bits don't use any probabilities.
  187. *
  188. * The distance slot value of 14 is for distances 128-191.
  189. */
  190. #define DIST_MODEL_END 14
  191. /* Distance slots that indicate a distance <= 127. */
  192. #define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
  193. #define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
  194. /*
  195. * For match distances greater than 127, only the highest two bits and the
  196. * lowest four bits (alignment) is encoded using probabilities.
  197. */
  198. #define ALIGN_BITS 4
  199. #define ALIGN_SIZE (1 << ALIGN_BITS)
  200. #define ALIGN_MASK (ALIGN_SIZE - 1)
  201. /* Total number of all probability variables */
  202. #define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)
  203. /*
  204. * LZMA remembers the four most recent match distances. Reusing these
  205. * distances tends to take less space than re-encoding the actual
  206. * distance value.
  207. */
  208. #define REPS 4
  209. #endif