3
0

decompress_unlzma.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Small lzma deflate implementation.
  3. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  6. * Copyright (C) 1999-2005 Igor Pavlov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdint.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <byteswap.h>
  26. #include "libbb.h"
  27. #include "rangecoder.h"
  28. typedef struct {
  29. uint8_t pos;
  30. uint32_t dict_size;
  31. uint64_t dst_size;
  32. } __attribute__ ((packed)) lzma_header_t;
  33. #define LZMA_BASE_SIZE 1846
  34. #define LZMA_LIT_SIZE 768
  35. #define LZMA_NUM_POS_BITS_MAX 4
  36. #define LZMA_LEN_NUM_LOW_BITS 3
  37. #define LZMA_LEN_NUM_MID_BITS 3
  38. #define LZMA_LEN_NUM_HIGH_BITS 8
  39. #define LZMA_LEN_CHOICE 0
  40. #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
  41. #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
  42. #define LZMA_LEN_MID (LZMA_LEN_LOW \
  43. + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
  44. #define LZMA_LEN_HIGH (LZMA_LEN_MID \
  45. +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
  46. #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
  47. #define LZMA_NUM_STATES 12
  48. #define LZMA_NUM_LIT_STATES 7
  49. #define LZMA_START_POS_MODEL_INDEX 4
  50. #define LZMA_END_POS_MODEL_INDEX 14
  51. #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
  52. #define LZMA_NUM_POS_SLOT_BITS 6
  53. #define LZMA_NUM_LEN_TO_POS_STATES 4
  54. #define LZMA_NUM_ALIGN_BITS 4
  55. #define LZMA_MATCH_MIN_LEN 2
  56. #define LZMA_IS_MATCH 0
  57. #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES <<LZMA_NUM_POS_BITS_MAX))
  58. #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
  59. #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
  60. #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
  61. #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
  62. #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
  63. + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
  64. #define LZMA_SPEC_POS (LZMA_POS_SLOT \
  65. +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
  66. #define LZMA_ALIGN (LZMA_SPEC_POS \
  67. + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
  68. #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
  69. #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
  70. #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
  71. int unlzma(int src_fd, int dst_fd)
  72. {
  73. lzma_header_t header;
  74. int lc, pb, lp;
  75. uint32_t pos_state_mask;
  76. uint32_t literal_pos_mask;
  77. uint32_t pos;
  78. uint16_t *p;
  79. uint16_t *prob;
  80. uint16_t *prob_lit;
  81. int num_bits;
  82. int num_probs;
  83. rc_t rc;
  84. int i, mi;
  85. uint8_t *buffer;
  86. uint8_t previous_byte = 0;
  87. size_t buffer_pos = 0, global_pos = 0;
  88. int len = 0;
  89. int state = 0;
  90. uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  91. if (read(src_fd, &header, sizeof(header)) != sizeof(header))
  92. bb_error_msg_and_die("can't read header");
  93. if (header.pos >= (9 * 5 * 5))
  94. bb_error_msg_and_die("bad header");
  95. mi = header.pos / 9;
  96. lc = header.pos % 9;
  97. pb = mi / 5;
  98. lp = mi % 5;
  99. pos_state_mask = (1 << pb) - 1;
  100. literal_pos_mask = (1 << lp) - 1;
  101. #if BB_BIG_ENDIAN
  102. header.dict_size = bswap_32(header.dict_size);
  103. header.dst_size = bswap_64(header.dst_size);
  104. #endif
  105. if (header.dict_size == 0)
  106. header.dict_size = 1;
  107. buffer = xmalloc(MIN(header.dst_size, header.dict_size));
  108. num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  109. p = xmalloc(num_probs * sizeof(*p));
  110. num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
  111. for (i = 0; i < num_probs; i++)
  112. p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  113. rc_init(&rc, src_fd, 0x10000);
  114. while (global_pos + buffer_pos < header.dst_size) {
  115. int pos_state = (buffer_pos + global_pos) & pos_state_mask;
  116. prob =
  117. p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  118. if (rc_is_bit_0(&rc, prob)) {
  119. mi = 1;
  120. rc_update_bit_0(&rc, prob);
  121. prob = (p + LZMA_LITERAL + (LZMA_LIT_SIZE
  122. * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
  123. + (previous_byte >> (8 - lc)))));
  124. if (state >= LZMA_NUM_LIT_STATES) {
  125. int match_byte;
  126. pos = buffer_pos - rep0;
  127. while (pos >= header.dict_size)
  128. pos += header.dict_size;
  129. match_byte = buffer[pos];
  130. do {
  131. int bit;
  132. match_byte <<= 1;
  133. bit = match_byte & 0x100;
  134. prob_lit = prob + 0x100 + bit + mi;
  135. if (rc_get_bit(&rc, prob_lit, &mi)) {
  136. if (!bit)
  137. break;
  138. } else {
  139. if (bit)
  140. break;
  141. }
  142. } while (mi < 0x100);
  143. }
  144. while (mi < 0x100) {
  145. prob_lit = prob + mi;
  146. rc_get_bit(&rc, prob_lit, &mi);
  147. }
  148. previous_byte = (uint8_t) mi;
  149. buffer[buffer_pos++] = previous_byte;
  150. if (buffer_pos == header.dict_size) {
  151. buffer_pos = 0;
  152. global_pos += header.dict_size;
  153. write(dst_fd, buffer, header.dict_size);
  154. }
  155. if (state < 4)
  156. state = 0;
  157. else if (state < 10)
  158. state -= 3;
  159. else
  160. state -= 6;
  161. } else {
  162. int offset;
  163. uint16_t *prob_len;
  164. rc_update_bit_1(&rc, prob);
  165. prob = p + LZMA_IS_REP + state;
  166. if (rc_is_bit_0(&rc, prob)) {
  167. rc_update_bit_0(&rc, prob);
  168. rep3 = rep2;
  169. rep2 = rep1;
  170. rep1 = rep0;
  171. state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
  172. prob = p + LZMA_LEN_CODER;
  173. } else {
  174. rc_update_bit_1(&rc, prob);
  175. prob = p + LZMA_IS_REP_G0 + state;
  176. if (rc_is_bit_0(&rc, prob)) {
  177. rc_update_bit_0(&rc, prob);
  178. prob = (p + LZMA_IS_REP_0_LONG
  179. + (state << LZMA_NUM_POS_BITS_MAX) + pos_state);
  180. if (rc_is_bit_0(&rc, prob)) {
  181. rc_update_bit_0(&rc, prob);
  182. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  183. pos = buffer_pos - rep0;
  184. while (pos >= header.dict_size)
  185. pos += header.dict_size;
  186. previous_byte = buffer[pos];
  187. buffer[buffer_pos++] = previous_byte;
  188. if (buffer_pos == header.dict_size) {
  189. buffer_pos = 0;
  190. global_pos += header.dict_size;
  191. write(dst_fd, buffer, header.dict_size);
  192. }
  193. continue;
  194. } else {
  195. rc_update_bit_1(&rc, prob);
  196. }
  197. } else {
  198. uint32_t distance;
  199. rc_update_bit_1(&rc, prob);
  200. prob = p + LZMA_IS_REP_G1 + state;
  201. if (rc_is_bit_0(&rc, prob)) {
  202. rc_update_bit_0(&rc, prob);
  203. distance = rep1;
  204. } else {
  205. rc_update_bit_1(&rc, prob);
  206. prob = p + LZMA_IS_REP_G2 + state;
  207. if (rc_is_bit_0(&rc, prob)) {
  208. rc_update_bit_0(&rc, prob);
  209. distance = rep2;
  210. } else {
  211. rc_update_bit_1(&rc, prob);
  212. distance = rep3;
  213. rep3 = rep2;
  214. }
  215. rep2 = rep1;
  216. }
  217. rep1 = rep0;
  218. rep0 = distance;
  219. }
  220. state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
  221. prob = p + LZMA_REP_LEN_CODER;
  222. }
  223. prob_len = prob + LZMA_LEN_CHOICE;
  224. if (rc_is_bit_0(&rc, prob_len)) {
  225. rc_update_bit_0(&rc, prob_len);
  226. prob_len = (prob + LZMA_LEN_LOW
  227. + (pos_state << LZMA_LEN_NUM_LOW_BITS));
  228. offset = 0;
  229. num_bits = LZMA_LEN_NUM_LOW_BITS;
  230. } else {
  231. rc_update_bit_1(&rc, prob_len);
  232. prob_len = prob + LZMA_LEN_CHOICE_2;
  233. if (rc_is_bit_0(&rc, prob_len)) {
  234. rc_update_bit_0(&rc, prob_len);
  235. prob_len = (prob + LZMA_LEN_MID
  236. + (pos_state << LZMA_LEN_NUM_MID_BITS));
  237. offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  238. num_bits = LZMA_LEN_NUM_MID_BITS;
  239. } else {
  240. rc_update_bit_1(&rc, prob_len);
  241. prob_len = prob + LZMA_LEN_HIGH;
  242. offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  243. + (1 << LZMA_LEN_NUM_MID_BITS));
  244. num_bits = LZMA_LEN_NUM_HIGH_BITS;
  245. }
  246. }
  247. rc_bit_tree_decode(&rc, prob_len, num_bits, &len);
  248. len += offset;
  249. if (state < 4) {
  250. int pos_slot;
  251. state += LZMA_NUM_LIT_STATES;
  252. prob =
  253. p + LZMA_POS_SLOT +
  254. ((len <
  255. LZMA_NUM_LEN_TO_POS_STATES ? len :
  256. LZMA_NUM_LEN_TO_POS_STATES - 1)
  257. << LZMA_NUM_POS_SLOT_BITS);
  258. rc_bit_tree_decode(&rc, prob, LZMA_NUM_POS_SLOT_BITS,
  259. &pos_slot);
  260. if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  261. num_bits = (pos_slot >> 1) - 1;
  262. rep0 = 2 | (pos_slot & 1);
  263. if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  264. rep0 <<= num_bits;
  265. prob = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
  266. } else {
  267. num_bits -= LZMA_NUM_ALIGN_BITS;
  268. while (num_bits--)
  269. rep0 = (rep0 << 1) | rc_direct_bit(&rc);
  270. prob = p + LZMA_ALIGN;
  271. rep0 <<= LZMA_NUM_ALIGN_BITS;
  272. num_bits = LZMA_NUM_ALIGN_BITS;
  273. }
  274. i = 1;
  275. mi = 1;
  276. while (num_bits--) {
  277. if (rc_get_bit(&rc, prob + mi, &mi))
  278. rep0 |= i;
  279. i <<= 1;
  280. }
  281. } else
  282. rep0 = pos_slot;
  283. if (++rep0 == 0)
  284. break;
  285. }
  286. len += LZMA_MATCH_MIN_LEN;
  287. do {
  288. pos = buffer_pos - rep0;
  289. while (pos >= header.dict_size)
  290. pos += header.dict_size;
  291. previous_byte = buffer[pos];
  292. buffer[buffer_pos++] = previous_byte;
  293. if (buffer_pos == header.dict_size) {
  294. buffer_pos = 0;
  295. global_pos += header.dict_size;
  296. write(dst_fd, buffer, header.dict_size);
  297. }
  298. len--;
  299. } while (len != 0 && buffer_pos < header.dst_size);
  300. }
  301. }
  302. write(dst_fd, buffer, buffer_pos);
  303. rc_free(&rc);
  304. return 0;
  305. }
  306. /* vi:set ts=4: */