decompress_unlzma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Small lzma deflate implementation.
  4. * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
  5. *
  6. * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  7. * Copyright (C) 1999-2005 Igor Pavlov
  8. *
  9. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10. */
  11. #include "libbb.h"
  12. #include "bb_archive.h"
  13. #if ENABLE_FEATURE_LZMA_FAST
  14. # define speed_inline ALWAYS_INLINE
  15. # define size_inline
  16. #else
  17. # define speed_inline
  18. # define size_inline ALWAYS_INLINE
  19. #endif
  20. typedef struct {
  21. int fd;
  22. uint8_t *ptr;
  23. /* Was keeping rc on stack in unlzma and separately allocating buffer,
  24. * but with "buffer 'attached to' allocated rc" code is smaller: */
  25. /* uint8_t *buffer; */
  26. #define RC_BUFFER ((uint8_t*)(rc+1))
  27. uint8_t *buffer_end;
  28. /* Had provisions for variable buffer, but we don't need it here */
  29. /* int buffer_size; */
  30. #define RC_BUFFER_SIZE 0x10000
  31. uint32_t code;
  32. uint32_t range;
  33. uint32_t bound;
  34. } rc_t;
  35. #define RC_TOP_BITS 24
  36. #define RC_MOVE_BITS 5
  37. #define RC_MODEL_TOTAL_BITS 11
  38. /* Called once in rc_do_normalize() */
  39. static void rc_read(rc_t *rc)
  40. {
  41. int buffer_size = safe_read(rc->fd, RC_BUFFER, RC_BUFFER_SIZE);
  42. //TODO: return -1 instead
  43. //This will make unlzma delete broken unpacked file on unpack errors
  44. if (buffer_size <= 0)
  45. bb_error_msg_and_die("unexpected EOF");
  46. rc->buffer_end = RC_BUFFER + buffer_size;
  47. rc->ptr = RC_BUFFER;
  48. }
  49. /* Called twice, but one callsite is in speed_inline'd rc_is_bit_1() */
  50. static void rc_do_normalize(rc_t *rc)
  51. {
  52. if (rc->ptr >= rc->buffer_end)
  53. rc_read(rc);
  54. rc->range <<= 8;
  55. rc->code = (rc->code << 8) | *rc->ptr++;
  56. }
  57. static ALWAYS_INLINE void rc_normalize(rc_t *rc)
  58. {
  59. if (rc->range < (1 << RC_TOP_BITS)) {
  60. rc_do_normalize(rc);
  61. }
  62. }
  63. /* Called once */
  64. static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
  65. {
  66. int i;
  67. rc_t *rc;
  68. rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
  69. rc->fd = fd;
  70. /* rc->ptr = rc->buffer_end; */
  71. for (i = 0; i < 5; i++) {
  72. rc_do_normalize(rc);
  73. }
  74. rc->range = 0xffffffff;
  75. return rc;
  76. }
  77. /* Called once */
  78. static ALWAYS_INLINE void rc_free(rc_t *rc)
  79. {
  80. free(rc);
  81. }
  82. /* rc_is_bit_1 is called 9 times */
  83. static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
  84. {
  85. rc_normalize(rc);
  86. rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
  87. if (rc->code < rc->bound) {
  88. rc->range = rc->bound;
  89. *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
  90. return 0;
  91. }
  92. rc->range -= rc->bound;
  93. rc->code -= rc->bound;
  94. *p -= *p >> RC_MOVE_BITS;
  95. return 1;
  96. }
  97. /* Called 4 times in unlzma loop */
  98. static ALWAYS_INLINE int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
  99. {
  100. int ret = rc_is_bit_1(rc, p);
  101. *symbol = *symbol * 2 + ret;
  102. return ret;
  103. }
  104. /* Called once */
  105. static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
  106. {
  107. rc_normalize(rc);
  108. rc->range >>= 1;
  109. if (rc->code >= rc->range) {
  110. rc->code -= rc->range;
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. /* Called twice */
  116. static speed_inline void
  117. rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
  118. {
  119. int i = num_levels;
  120. *symbol = 1;
  121. while (i--)
  122. rc_get_bit(rc, p + *symbol, symbol);
  123. *symbol -= 1 << num_levels;
  124. }
  125. typedef struct {
  126. uint8_t pos;
  127. uint32_t dict_size;
  128. uint64_t dst_size;
  129. } PACKED lzma_header_t;
  130. /* #defines will force compiler to compute/optimize each one with each usage.
  131. * Have heart and use enum instead. */
  132. enum {
  133. LZMA_BASE_SIZE = 1846,
  134. LZMA_LIT_SIZE = 768,
  135. LZMA_NUM_POS_BITS_MAX = 4,
  136. LZMA_LEN_NUM_LOW_BITS = 3,
  137. LZMA_LEN_NUM_MID_BITS = 3,
  138. LZMA_LEN_NUM_HIGH_BITS = 8,
  139. LZMA_LEN_CHOICE = 0,
  140. LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
  141. LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
  142. LZMA_LEN_MID = (LZMA_LEN_LOW \
  143. + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
  144. LZMA_LEN_HIGH = (LZMA_LEN_MID \
  145. + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
  146. LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
  147. LZMA_NUM_STATES = 12,
  148. LZMA_NUM_LIT_STATES = 7,
  149. LZMA_START_POS_MODEL_INDEX = 4,
  150. LZMA_END_POS_MODEL_INDEX = 14,
  151. LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
  152. LZMA_NUM_POS_SLOT_BITS = 6,
  153. LZMA_NUM_LEN_TO_POS_STATES = 4,
  154. LZMA_NUM_ALIGN_BITS = 4,
  155. LZMA_MATCH_MIN_LEN = 2,
  156. LZMA_IS_MATCH = 0,
  157. LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
  158. LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
  159. LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
  160. LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
  161. LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
  162. LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
  163. + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
  164. LZMA_SPEC_POS = (LZMA_POS_SLOT \
  165. + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
  166. LZMA_ALIGN = (LZMA_SPEC_POS \
  167. + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
  168. LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
  169. LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
  170. LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
  171. };
  172. IF_DESKTOP(long long) int FAST_FUNC
  173. unpack_lzma_stream(transformer_aux_data_t *aux UNUSED_PARAM, int src_fd, int dst_fd)
  174. {
  175. IF_DESKTOP(long long total_written = 0;)
  176. lzma_header_t header;
  177. int lc, pb, lp;
  178. uint32_t pos_state_mask;
  179. uint32_t literal_pos_mask;
  180. uint16_t *p;
  181. int num_bits;
  182. int num_probs;
  183. rc_t *rc;
  184. int i;
  185. uint8_t *buffer;
  186. uint8_t previous_byte = 0;
  187. size_t buffer_pos = 0, global_pos = 0;
  188. int len = 0;
  189. int state = 0;
  190. uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  191. if (full_read(src_fd, &header, sizeof(header)) != sizeof(header)
  192. || header.pos >= (9 * 5 * 5)
  193. ) {
  194. bb_error_msg("bad lzma header");
  195. return -1;
  196. }
  197. i = header.pos / 9;
  198. lc = header.pos % 9;
  199. pb = i / 5;
  200. lp = i % 5;
  201. pos_state_mask = (1 << pb) - 1;
  202. literal_pos_mask = (1 << lp) - 1;
  203. header.dict_size = SWAP_LE32(header.dict_size);
  204. header.dst_size = SWAP_LE64(header.dst_size);
  205. if (header.dict_size == 0)
  206. header.dict_size++;
  207. buffer = xmalloc(MIN(header.dst_size, header.dict_size));
  208. num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  209. p = xmalloc(num_probs * sizeof(*p));
  210. num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
  211. for (i = 0; i < num_probs; i++)
  212. p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  213. rc = rc_init(src_fd); /*, RC_BUFFER_SIZE); */
  214. while (global_pos + buffer_pos < header.dst_size) {
  215. int pos_state = (buffer_pos + global_pos) & pos_state_mask;
  216. uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  217. if (!rc_is_bit_1(rc, prob)) {
  218. static const char next_state[LZMA_NUM_STATES] =
  219. { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
  220. int mi = 1;
  221. prob = (p + LZMA_LITERAL
  222. + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
  223. + (previous_byte >> (8 - lc))
  224. )
  225. )
  226. );
  227. if (state >= LZMA_NUM_LIT_STATES) {
  228. int match_byte;
  229. uint32_t pos = buffer_pos - rep0;
  230. while (pos >= header.dict_size)
  231. pos += header.dict_size;
  232. match_byte = buffer[pos];
  233. do {
  234. int bit;
  235. match_byte <<= 1;
  236. bit = match_byte & 0x100;
  237. bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
  238. if (bit)
  239. break;
  240. } while (mi < 0x100);
  241. }
  242. while (mi < 0x100) {
  243. rc_get_bit(rc, prob + mi, &mi);
  244. }
  245. state = next_state[state];
  246. previous_byte = (uint8_t) mi;
  247. #if ENABLE_FEATURE_LZMA_FAST
  248. one_byte1:
  249. buffer[buffer_pos++] = previous_byte;
  250. if (buffer_pos == header.dict_size) {
  251. buffer_pos = 0;
  252. global_pos += header.dict_size;
  253. if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
  254. goto bad;
  255. IF_DESKTOP(total_written += header.dict_size;)
  256. }
  257. #else
  258. len = 1;
  259. goto one_byte2;
  260. #endif
  261. } else {
  262. int offset;
  263. uint16_t *prob2;
  264. #define prob_len prob2
  265. prob2 = p + LZMA_IS_REP + state;
  266. if (!rc_is_bit_1(rc, prob2)) {
  267. rep3 = rep2;
  268. rep2 = rep1;
  269. rep1 = rep0;
  270. state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
  271. prob2 = p + LZMA_LEN_CODER;
  272. } else {
  273. prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
  274. if (!rc_is_bit_1(rc, prob2)) {
  275. prob2 = (p + LZMA_IS_REP_0_LONG
  276. + (state << LZMA_NUM_POS_BITS_MAX)
  277. + pos_state
  278. );
  279. if (!rc_is_bit_1(rc, prob2)) {
  280. #if ENABLE_FEATURE_LZMA_FAST
  281. uint32_t pos = buffer_pos - rep0;
  282. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  283. while (pos >= header.dict_size)
  284. pos += header.dict_size;
  285. previous_byte = buffer[pos];
  286. goto one_byte1;
  287. #else
  288. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  289. len = 1;
  290. goto string;
  291. #endif
  292. }
  293. } else {
  294. uint32_t distance;
  295. prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
  296. distance = rep1;
  297. if (rc_is_bit_1(rc, prob2)) {
  298. prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
  299. distance = rep2;
  300. if (rc_is_bit_1(rc, prob2)) {
  301. distance = rep3;
  302. rep3 = rep2;
  303. }
  304. rep2 = rep1;
  305. }
  306. rep1 = rep0;
  307. rep0 = distance;
  308. }
  309. state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
  310. prob2 = p + LZMA_REP_LEN_CODER;
  311. }
  312. prob_len = prob2 + LZMA_LEN_CHOICE;
  313. num_bits = LZMA_LEN_NUM_LOW_BITS;
  314. if (!rc_is_bit_1(rc, prob_len)) {
  315. prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
  316. + (pos_state << LZMA_LEN_NUM_LOW_BITS);
  317. offset = 0;
  318. } else {
  319. prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
  320. if (!rc_is_bit_1(rc, prob_len)) {
  321. prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
  322. + (pos_state << LZMA_LEN_NUM_MID_BITS);
  323. offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  324. num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
  325. } else {
  326. prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
  327. offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  328. + (1 << LZMA_LEN_NUM_MID_BITS));
  329. num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
  330. }
  331. }
  332. rc_bit_tree_decode(rc, prob_len, num_bits, &len);
  333. len += offset;
  334. if (state < 4) {
  335. int pos_slot;
  336. uint16_t *prob3;
  337. state += LZMA_NUM_LIT_STATES;
  338. prob3 = p + LZMA_POS_SLOT +
  339. ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
  340. LZMA_NUM_LEN_TO_POS_STATES - 1)
  341. << LZMA_NUM_POS_SLOT_BITS);
  342. rc_bit_tree_decode(rc, prob3,
  343. LZMA_NUM_POS_SLOT_BITS, &pos_slot);
  344. rep0 = pos_slot;
  345. if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  346. int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
  347. rep0 = 2 | (pos_slot & 1);
  348. if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  349. rep0 <<= num_bits2;
  350. prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
  351. } else {
  352. for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
  353. rep0 = (rep0 << 1) | rc_direct_bit(rc);
  354. rep0 <<= LZMA_NUM_ALIGN_BITS;
  355. prob3 = p + LZMA_ALIGN;
  356. }
  357. i2 = 1;
  358. mi2 = 1;
  359. while (num_bits2--) {
  360. if (rc_get_bit(rc, prob3 + mi2, &mi2))
  361. rep0 |= i2;
  362. i2 <<= 1;
  363. }
  364. }
  365. if (++rep0 == 0)
  366. break;
  367. }
  368. len += LZMA_MATCH_MIN_LEN;
  369. IF_NOT_FEATURE_LZMA_FAST(string:)
  370. do {
  371. uint32_t pos = buffer_pos - rep0;
  372. while (pos >= header.dict_size)
  373. pos += header.dict_size;
  374. previous_byte = buffer[pos];
  375. IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
  376. buffer[buffer_pos++] = previous_byte;
  377. if (buffer_pos == header.dict_size) {
  378. buffer_pos = 0;
  379. global_pos += header.dict_size;
  380. if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
  381. goto bad;
  382. IF_DESKTOP(total_written += header.dict_size;)
  383. }
  384. len--;
  385. } while (len != 0 && buffer_pos < header.dst_size);
  386. }
  387. }
  388. {
  389. IF_NOT_DESKTOP(int total_written = 0; /* success */)
  390. IF_DESKTOP(total_written += buffer_pos;)
  391. if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
  392. bad:
  393. total_written = -1; /* failure */
  394. }
  395. rc_free(rc);
  396. free(p);
  397. free(buffer);
  398. return total_written;
  399. }
  400. }