decompress_unlzma.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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_state_t *xstate)
  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. rc_t *rc;
  182. int i;
  183. uint8_t *buffer;
  184. uint8_t previous_byte = 0;
  185. size_t buffer_pos = 0, global_pos = 0;
  186. int len = 0;
  187. int state = 0;
  188. uint32_t rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  189. if (full_read(xstate->src_fd, &header, sizeof(header)) != sizeof(header)
  190. || header.pos >= (9 * 5 * 5)
  191. ) {
  192. bb_error_msg("bad lzma header");
  193. return -1;
  194. }
  195. i = header.pos / 9;
  196. lc = header.pos % 9;
  197. pb = i / 5;
  198. lp = i % 5;
  199. pos_state_mask = (1 << pb) - 1;
  200. literal_pos_mask = (1 << lp) - 1;
  201. /* Example values from linux-3.3.4.tar.lzma:
  202. * dict_size: 64M, dst_size: 2^64-1
  203. */
  204. header.dict_size = SWAP_LE32(header.dict_size);
  205. header.dst_size = SWAP_LE64(header.dst_size);
  206. if (header.dict_size == 0)
  207. header.dict_size++;
  208. buffer = xmalloc(MIN(header.dst_size, header.dict_size));
  209. {
  210. int num_probs;
  211. num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  212. p = xmalloc(num_probs * sizeof(*p));
  213. num_probs += LZMA_LITERAL - LZMA_BASE_SIZE;
  214. for (i = 0; i < num_probs; i++)
  215. p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  216. }
  217. rc = rc_init(xstate->src_fd); /*, RC_BUFFER_SIZE); */
  218. while (global_pos + buffer_pos < header.dst_size) {
  219. int pos_state = (buffer_pos + global_pos) & pos_state_mask;
  220. uint16_t *prob = p + LZMA_IS_MATCH + (state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  221. if (!rc_is_bit_1(rc, prob)) {
  222. static const char next_state[LZMA_NUM_STATES] =
  223. { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 };
  224. int mi = 1;
  225. prob = (p + LZMA_LITERAL
  226. + (LZMA_LIT_SIZE * ((((buffer_pos + global_pos) & literal_pos_mask) << lc)
  227. + (previous_byte >> (8 - lc))
  228. )
  229. )
  230. );
  231. if (state >= LZMA_NUM_LIT_STATES) {
  232. int match_byte;
  233. uint32_t pos;
  234. pos = buffer_pos - rep0;
  235. if ((int32_t)pos < 0)
  236. pos += header.dict_size;
  237. match_byte = buffer[pos];
  238. do {
  239. int bit;
  240. match_byte <<= 1;
  241. bit = match_byte & 0x100;
  242. bit ^= (rc_get_bit(rc, prob + 0x100 + bit + mi, &mi) << 8); /* 0x100 or 0 */
  243. if (bit)
  244. break;
  245. } while (mi < 0x100);
  246. }
  247. while (mi < 0x100) {
  248. rc_get_bit(rc, prob + mi, &mi);
  249. }
  250. state = next_state[state];
  251. previous_byte = (uint8_t) mi;
  252. #if ENABLE_FEATURE_LZMA_FAST
  253. one_byte1:
  254. buffer[buffer_pos++] = previous_byte;
  255. if (buffer_pos == header.dict_size) {
  256. buffer_pos = 0;
  257. global_pos += header.dict_size;
  258. if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
  259. goto bad;
  260. IF_DESKTOP(total_written += header.dict_size;)
  261. }
  262. #else
  263. len = 1;
  264. goto one_byte2;
  265. #endif
  266. } else {
  267. int num_bits;
  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;
  288. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  289. pos = buffer_pos - rep0;
  290. if ((int32_t)pos < 0)
  291. pos += header.dict_size;
  292. previous_byte = buffer[pos];
  293. goto one_byte1;
  294. #else
  295. state = state < LZMA_NUM_LIT_STATES ? 9 : 11;
  296. len = 1;
  297. goto string;
  298. #endif
  299. }
  300. } else {
  301. uint32_t distance;
  302. prob2 += LZMA_IS_REP_G1 - LZMA_IS_REP_G0;
  303. distance = rep1;
  304. if (rc_is_bit_1(rc, prob2)) {
  305. prob2 += LZMA_IS_REP_G2 - LZMA_IS_REP_G1;
  306. distance = rep2;
  307. if (rc_is_bit_1(rc, prob2)) {
  308. distance = rep3;
  309. rep3 = rep2;
  310. }
  311. rep2 = rep1;
  312. }
  313. rep1 = rep0;
  314. rep0 = distance;
  315. }
  316. state = state < LZMA_NUM_LIT_STATES ? 8 : 11;
  317. prob2 = p + LZMA_REP_LEN_CODER;
  318. }
  319. prob_len = prob2 + LZMA_LEN_CHOICE;
  320. num_bits = LZMA_LEN_NUM_LOW_BITS;
  321. if (!rc_is_bit_1(rc, prob_len)) {
  322. prob_len += LZMA_LEN_LOW - LZMA_LEN_CHOICE
  323. + (pos_state << LZMA_LEN_NUM_LOW_BITS);
  324. offset = 0;
  325. } else {
  326. prob_len += LZMA_LEN_CHOICE_2 - LZMA_LEN_CHOICE;
  327. if (!rc_is_bit_1(rc, prob_len)) {
  328. prob_len += LZMA_LEN_MID - LZMA_LEN_CHOICE_2
  329. + (pos_state << LZMA_LEN_NUM_MID_BITS);
  330. offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  331. num_bits += LZMA_LEN_NUM_MID_BITS - LZMA_LEN_NUM_LOW_BITS;
  332. } else {
  333. prob_len += LZMA_LEN_HIGH - LZMA_LEN_CHOICE_2;
  334. offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  335. + (1 << LZMA_LEN_NUM_MID_BITS));
  336. num_bits += LZMA_LEN_NUM_HIGH_BITS - LZMA_LEN_NUM_LOW_BITS;
  337. }
  338. }
  339. rc_bit_tree_decode(rc, prob_len, num_bits, &len);
  340. len += offset;
  341. if (state < 4) {
  342. int pos_slot;
  343. uint16_t *prob3;
  344. state += LZMA_NUM_LIT_STATES;
  345. prob3 = p + LZMA_POS_SLOT +
  346. ((len < LZMA_NUM_LEN_TO_POS_STATES ? len :
  347. LZMA_NUM_LEN_TO_POS_STATES - 1)
  348. << LZMA_NUM_POS_SLOT_BITS);
  349. rc_bit_tree_decode(rc, prob3,
  350. LZMA_NUM_POS_SLOT_BITS, &pos_slot);
  351. rep0 = pos_slot;
  352. if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  353. int i2, mi2, num_bits2 = (pos_slot >> 1) - 1;
  354. rep0 = 2 | (pos_slot & 1);
  355. if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  356. rep0 <<= num_bits2;
  357. prob3 = p + LZMA_SPEC_POS + rep0 - pos_slot - 1;
  358. } else {
  359. for (; num_bits2 != LZMA_NUM_ALIGN_BITS; num_bits2--)
  360. rep0 = (rep0 << 1) | rc_direct_bit(rc);
  361. rep0 <<= LZMA_NUM_ALIGN_BITS;
  362. prob3 = p + LZMA_ALIGN;
  363. }
  364. i2 = 1;
  365. mi2 = 1;
  366. while (num_bits2--) {
  367. if (rc_get_bit(rc, prob3 + mi2, &mi2))
  368. rep0 |= i2;
  369. i2 <<= 1;
  370. }
  371. }
  372. if (++rep0 == 0)
  373. break;
  374. }
  375. len += LZMA_MATCH_MIN_LEN;
  376. /*
  377. * LZMA SDK has this optimized:
  378. * it precalculates size and copies many bytes
  379. * in a loop with simpler checks, a-la:
  380. * do
  381. * *(dest) = *(dest + ofs);
  382. * while (++dest != lim);
  383. * and
  384. * do {
  385. * buffer[buffer_pos++] = buffer[pos];
  386. * if (++pos == header.dict_size)
  387. * pos = 0;
  388. * } while (--cur_len != 0);
  389. * Our code is slower (more checks per byte copy):
  390. */
  391. IF_NOT_FEATURE_LZMA_FAST(string:)
  392. do {
  393. uint32_t pos = buffer_pos - rep0;
  394. if ((int32_t)pos < 0) {
  395. pos += header.dict_size;
  396. /* bug 10436 has an example file where this triggers: */
  397. if ((int32_t)pos < 0)
  398. goto bad;
  399. }
  400. previous_byte = buffer[pos];
  401. IF_NOT_FEATURE_LZMA_FAST(one_byte2:)
  402. buffer[buffer_pos++] = previous_byte;
  403. if (buffer_pos == header.dict_size) {
  404. buffer_pos = 0;
  405. global_pos += header.dict_size;
  406. if (transformer_write(xstate, buffer, header.dict_size) != (ssize_t)header.dict_size)
  407. goto bad;
  408. IF_DESKTOP(total_written += header.dict_size;)
  409. }
  410. len--;
  411. } while (len != 0 && buffer_pos < header.dst_size);
  412. /* FIXME: ...........^^^^^
  413. * shouldn't it be "global_pos + buffer_pos < header.dst_size"?
  414. * It probably should, but it is a "do we accidentally
  415. * unpack more bytes than expected?" check - which
  416. * never happens for well-formed compression data...
  417. */
  418. }
  419. }
  420. {
  421. IF_NOT_DESKTOP(int total_written = 0; /* success */)
  422. IF_DESKTOP(total_written += buffer_pos;)
  423. if (transformer_write(xstate, buffer, buffer_pos) != (ssize_t)buffer_pos) {
  424. bad:
  425. total_written = -1; /* failure */
  426. }
  427. rc_free(rc);
  428. free(p);
  429. free(buffer);
  430. return total_written;
  431. }
  432. }