decompress_unlzma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 tarball for details.
  10. */
  11. #include "libbb.h"
  12. #include "unarchive.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 twice: once at startup (LZMA_FAST only) and once in rc_normalize() */
  39. static size_inline 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->ptr = RC_BUFFER;
  47. rc->buffer_end = RC_BUFFER + buffer_size;
  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. /* Called once */
  58. static ALWAYS_INLINE rc_t* rc_init(int fd) /*, int buffer_size) */
  59. {
  60. int i;
  61. rc_t *rc;
  62. rc = xzalloc(sizeof(*rc) + RC_BUFFER_SIZE);
  63. rc->fd = fd;
  64. /* rc->ptr = rc->buffer_end; */
  65. for (i = 0; i < 5; i++) {
  66. #if ENABLE_FEATURE_LZMA_FAST
  67. if (rc->ptr >= rc->buffer_end)
  68. rc_read(rc);
  69. rc->code = (rc->code << 8) | *rc->ptr++;
  70. #else
  71. rc_do_normalize(rc);
  72. #endif
  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. static ALWAYS_INLINE void rc_normalize(rc_t *rc)
  83. {
  84. if (rc->range < (1 << RC_TOP_BITS)) {
  85. rc_do_normalize(rc);
  86. }
  87. }
  88. /* rc_is_bit_1 is called 9 times */
  89. static speed_inline int rc_is_bit_1(rc_t *rc, uint16_t *p)
  90. {
  91. rc_normalize(rc);
  92. rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
  93. if (rc->code < rc->bound) {
  94. rc->range = rc->bound;
  95. *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
  96. return 0;
  97. }
  98. rc->range -= rc->bound;
  99. rc->code -= rc->bound;
  100. *p -= *p >> RC_MOVE_BITS;
  101. return 1;
  102. }
  103. /* Called 4 times in unlzma loop */
  104. static speed_inline int rc_get_bit(rc_t *rc, uint16_t *p, int *symbol)
  105. {
  106. int ret = rc_is_bit_1(rc, p);
  107. *symbol = *symbol * 2 + ret;
  108. return ret;
  109. }
  110. /* Called once */
  111. static ALWAYS_INLINE int rc_direct_bit(rc_t *rc)
  112. {
  113. rc_normalize(rc);
  114. rc->range >>= 1;
  115. if (rc->code >= rc->range) {
  116. rc->code -= rc->range;
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. /* Called twice */
  122. static speed_inline void
  123. rc_bit_tree_decode(rc_t *rc, uint16_t *p, int num_levels, int *symbol)
  124. {
  125. int i = num_levels;
  126. *symbol = 1;
  127. while (i--)
  128. rc_get_bit(rc, p + *symbol, symbol);
  129. *symbol -= 1 << num_levels;
  130. }
  131. typedef struct {
  132. uint8_t pos;
  133. uint32_t dict_size;
  134. uint64_t dst_size;
  135. } PACKED lzma_header_t;
  136. /* #defines will force compiler to compute/optimize each one with each usage.
  137. * Have heart and use enum instead. */
  138. enum {
  139. LZMA_BASE_SIZE = 1846,
  140. LZMA_LIT_SIZE = 768,
  141. LZMA_NUM_POS_BITS_MAX = 4,
  142. LZMA_LEN_NUM_LOW_BITS = 3,
  143. LZMA_LEN_NUM_MID_BITS = 3,
  144. LZMA_LEN_NUM_HIGH_BITS = 8,
  145. LZMA_LEN_CHOICE = 0,
  146. LZMA_LEN_CHOICE_2 = (LZMA_LEN_CHOICE + 1),
  147. LZMA_LEN_LOW = (LZMA_LEN_CHOICE_2 + 1),
  148. LZMA_LEN_MID = (LZMA_LEN_LOW \
  149. + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS))),
  150. LZMA_LEN_HIGH = (LZMA_LEN_MID \
  151. + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS))),
  152. LZMA_NUM_LEN_PROBS = (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS)),
  153. LZMA_NUM_STATES = 12,
  154. LZMA_NUM_LIT_STATES = 7,
  155. LZMA_START_POS_MODEL_INDEX = 4,
  156. LZMA_END_POS_MODEL_INDEX = 14,
  157. LZMA_NUM_FULL_DISTANCES = (1 << (LZMA_END_POS_MODEL_INDEX >> 1)),
  158. LZMA_NUM_POS_SLOT_BITS = 6,
  159. LZMA_NUM_LEN_TO_POS_STATES = 4,
  160. LZMA_NUM_ALIGN_BITS = 4,
  161. LZMA_MATCH_MIN_LEN = 2,
  162. LZMA_IS_MATCH = 0,
  163. LZMA_IS_REP = (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
  164. LZMA_IS_REP_G0 = (LZMA_IS_REP + LZMA_NUM_STATES),
  165. LZMA_IS_REP_G1 = (LZMA_IS_REP_G0 + LZMA_NUM_STATES),
  166. LZMA_IS_REP_G2 = (LZMA_IS_REP_G1 + LZMA_NUM_STATES),
  167. LZMA_IS_REP_0_LONG = (LZMA_IS_REP_G2 + LZMA_NUM_STATES),
  168. LZMA_POS_SLOT = (LZMA_IS_REP_0_LONG \
  169. + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX)),
  170. LZMA_SPEC_POS = (LZMA_POS_SLOT \
  171. + (LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS)),
  172. LZMA_ALIGN = (LZMA_SPEC_POS \
  173. + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX),
  174. LZMA_LEN_CODER = (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS)),
  175. LZMA_REP_LEN_CODER = (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS),
  176. LZMA_LITERAL = (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS),
  177. };
  178. IF_DESKTOP(long long) int FAST_FUNC
  179. unpack_lzma_stream(int src_fd, int dst_fd)
  180. {
  181. IF_DESKTOP(long long total_written = 0;)
  182. lzma_header_t header;
  183. int lc, pb, lp;
  184. uint32_t pos_state_mask;
  185. uint32_t literal_pos_mask;
  186. uint16_t *p;
  187. int num_bits;
  188. int num_probs;
  189. rc_t *rc;
  190. int i;
  191. uint8_t *buffer;
  192. uint8_t previous_byte = 0;
  193. size_t buffer_pos = 0, global_pos = 0;
  194. int len = 0;
  195. int state = 0;
  196. uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  197. if (full_read(src_fd, &header, sizeof(header)) != sizeof(header)
  198. || header.pos >= (9 * 5 * 5)
  199. ) {
  200. bb_error_msg("bad lzma header");
  201. return -1;
  202. }
  203. i = header.pos / 9;
  204. lc = header.pos % 9;
  205. pb = i / 5;
  206. lp = i % 5;
  207. pos_state_mask = (1 << pb) - 1;
  208. literal_pos_mask = (1 << lp) - 1;
  209. header.dict_size = SWAP_LE32(header.dict_size);
  210. header.dst_size = SWAP_LE64(header.dst_size);
  211. if (header.dict_size == 0)
  212. header.dict_size++;
  213. buffer = xmalloc(MIN(header.dst_size, header.dict_size));
  214. num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  215. p = xmalloc(num_probs * sizeof(*p));
  216. num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
  217. for (i = 0; i < num_probs; i++)
  218. p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  219. rc = rc_init(src_fd); /*, RC_BUFFER_SIZE); */
  220. while (global_pos + buffer_pos < header.dst_size) {
  221. int pos_state = (buffer_pos + global_pos) & pos_state_mask;
  222. uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  223. if (!rc_is_bit_1(rc, prob)) {
  224. static const char next_state[LZMA_NUM_STATES] =
  225. { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
  226. int mi = 1;
  227. prob = (p + LZMA_LITERAL
  228. + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
  229. + (previous_byte >> (8 - lc))
  230. )
  231. )
  232. );
  233. if (state >= LZMA_NUM_LIT_STATES) {
  234. int match_byte;
  235. uint32_t pos = buffer_pos - rep0;
  236. while (pos >= header.dict_size)
  237. pos += header.dict_size;
  238. match_byte = buffer[pos];
  239. do {
  240. int bit;
  241. match_byte <<= 1;
  242. bit = match_byte & 0x100;
  243. bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
  244. if (bit)
  245. break;
  246. } while (mi < 0x100);
  247. }
  248. while (mi < 0x100) {
  249. rc_get_bit(rc, prob + mi, &mi);
  250. }
  251. state = next_state[state];
  252. previous_byte = (uint8_t) mi;
  253. #if ENABLE_FEATURE_LZMA_FAST
  254. one_byte1:
  255. buffer[buffer_pos++] = previous_byte;
  256. if (buffer_pos == header.dict_size) {
  257. buffer_pos = 0;
  258. global_pos += header.dict_size;
  259. if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
  260. goto bad;
  261. IF_DESKTOP(total_written += header.dict_size;)
  262. }
  263. #else
  264. len = 1;
  265. goto one_byte2;
  266. #endif
  267. } else {
  268. int offset;
  269. uint16_t *prob2;
  270. #define prob_len prob2
  271. prob2 = p + LZMA_IS_REP + state;
  272. if (!rc_is_bit_1(rc, prob2)) {
  273. rep3 = rep2;
  274. rep2 = rep1;
  275. rep1 = rep0;
  276. state = state < LZMA_NUM_LIT_STATES ? 0 : 3;
  277. prob2 = p + LZMA_LEN_CODER;
  278. } else {
  279. prob2 += LZMA_IS_REP_G0 - LZMA_IS_REP;
  280. if (!rc_is_bit_1(rc, prob2)) {
  281. prob2 = (p + LZMA_IS_REP_0_LONG
  282. + (state << LZMA_NUM_POS_BITS_MAX)
  283. + pos_state
  284. );
  285. if (!rc_is_bit_1(rc, prob2)) {
  286. #if ENABLE_FEATURE_LZMA_FAST
  287. uint32_t pos = buffer_pos - rep0;
  288. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  289. while (pos >= header.dict_size)
  290. pos += header.dict_size;
  291. previous_byte = buffer[pos];
  292. goto one_byte1;
  293. #else
  294. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  295. len = 1;
  296. goto string;
  297. #endif
  298. }
  299. } else {
  300. uint32_t distance;
  301. prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
  302. distance = rep1;
  303. if (rc_is_bit_1(rc, prob2)) {
  304. prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
  305. distance = rep2;
  306. if (rc_is_bit_1(rc, prob2)) {
  307. distance = rep3;
  308. rep3 = rep2;
  309. }
  310. rep2 = rep1;
  311. }
  312. rep1 = rep0;
  313. rep0 = distance;
  314. }
  315. state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
  316. prob2 = p + LZMA_REP_LEN_CODER;
  317. }
  318. prob_len = prob2 + LZMA_LEN_CHOICE;
  319. num_bits = LZMA_LEN_NUM_LOW_BITS;
  320. if (!rc_is_bit_1(rc, prob_len)) {
  321. prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
  322. + (pos_state << LZMA_LEN_NUM_LOW_BITS);
  323. offset = 0;
  324. } else {
  325. prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
  326. if (!rc_is_bit_1(rc, prob_len)) {
  327. prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
  328. + (pos_state << LZMA_LEN_NUM_MID_BITS);
  329. offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  330. num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
  331. } else {
  332. prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
  333. offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  334. + (1 << LZMA_LEN_NUM_MID_BITS));
  335. num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
  336. }
  337. }
  338. rc_bit_tree_decode(rc, prob_len, num_bits, &len);
  339. len += offset;
  340. if (state < 4) {
  341. int pos_slot;
  342. uint16_t *prob3;
  343. state += LZMA_NUM_LIT_STATES;
  344. prob3 = p + LZMA_POS_SLOT +
  345. ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
  346. LZMA_NUM_LEN_TO_POS_STATES - 1)
  347. << LZMA_NUM_POS_SLOT_BITS);
  348. rc_bit_tree_decode(rc, prob3,
  349. LZMA_NUM_POS_SLOT_BITS, &pos_slot);
  350. rep0 = pos_slot;
  351. if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  352. int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
  353. rep0 = 2 | (pos_slot & 1);
  354. if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  355. rep0 <<= num_bits2;
  356. prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
  357. } else {
  358. for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
  359. rep0 = (rep0 << 1) | rc_direct_bit(rc);
  360. rep0 <<= LZMA_NUM_ALIGN_BITS;
  361. prob3 = p + LZMA_ALIGN;
  362. }
  363. i2 = 1;
  364. mi2 = 1;
  365. while (num_bits2--) {
  366. if (rc_get_bit(rc, prob3 + mi2, &mi2))
  367. rep0 |= i2;
  368. i2 <<= 1;
  369. }
  370. }
  371. if (++rep0 == 0)
  372. break;
  373. }
  374. len += LZMA_MATCH_MIN_LEN;
  375. IF_NOT_FEATURE_LZMA_FAST(string:)
  376. do {
  377. uint32_t pos = buffer_pos - rep0;
  378. while (pos >= header.dict_size)
  379. pos += header.dict_size;
  380. previous_byte = buffer[pos];
  381. IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
  382. buffer[buffer_pos++] = previous_byte;
  383. if (buffer_pos == header.dict_size) {
  384. buffer_pos = 0;
  385. global_pos += header.dict_size;
  386. if (full_write(dst_fd, buffer, header.dict_size) != (ssize_t)header.dict_size)
  387. goto bad;
  388. IF_DESKTOP(total_written += header.dict_size;)
  389. }
  390. len--;
  391. } while (len != 0 && buffer_pos < header.dst_size);
  392. }
  393. }
  394. {
  395. IF_NOT_DESKTOP(int total_written = 0; /* success */)
  396. IF_DESKTOP(total_written += buffer_pos;)
  397. if (full_write(dst_fd, buffer, buffer_pos) != (ssize_t)buffer_pos) {
  398. bad:
  399. total_written = -1; /* failure */
  400. }
  401. rc_free(rc);
  402. free(p);
  403. free(buffer);
  404. return total_written;
  405. }
  406. }