decompress_unlzma.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /* Lzma decompressor for Linux kernel. Shamelessly snarfed
  2. *from busybox 1.1.1
  3. *
  4. *Linux kernel adaptation
  5. *Copyright (C) 2006 Alain < alain@knaff.lu >
  6. *
  7. *Based on small lzma deflate implementation/Small range coder
  8. *implementation for lzma.
  9. *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
  10. *
  11. *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  12. *Copyright (C) 1999-2005 Igor Pavlov
  13. *
  14. *Copyrights of the parts, see headers below.
  15. *
  16. *
  17. *This program is free software; you can redistribute it and/or
  18. *modify it under the terms of the GNU Lesser General Public
  19. *License as published by the Free Software Foundation; either
  20. *version 2.1 of the License, or (at your option) any later version.
  21. *
  22. *This program is distributed in the hope that it will be useful,
  23. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. *Lesser General Public License for more details.
  26. *
  27. *You should have received a copy of the GNU Lesser General Public
  28. *License along with this library; if not, write to the Free Software
  29. *Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. #ifndef STATIC
  32. #include <linux/decompress/unlzma.h>
  33. #endif /* STATIC */
  34. #include <linux/decompress/mm.h>
  35. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  36. static long long INIT read_int(unsigned char *ptr, int size)
  37. {
  38. int i;
  39. long long ret = 0;
  40. for (i = 0; i < size; i++)
  41. ret = (ret << 8) | ptr[size-i-1];
  42. return ret;
  43. }
  44. #define ENDIAN_CONVERT(x) \
  45. x = (typeof(x))read_int((unsigned char *)&x, sizeof(x))
  46. /* Small range coder implementation for lzma.
  47. *Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
  48. *
  49. *Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  50. *Copyright (c) 1999-2005 Igor Pavlov
  51. */
  52. #include <linux/compiler.h>
  53. #define LZMA_IOBUF_SIZE 0x10000
  54. struct rc {
  55. int (*fill)(void*, unsigned int);
  56. uint8_t *ptr;
  57. uint8_t *buffer;
  58. uint8_t *buffer_end;
  59. int buffer_size;
  60. uint32_t code;
  61. uint32_t range;
  62. uint32_t bound;
  63. };
  64. #define RC_TOP_BITS 24
  65. #define RC_MOVE_BITS 5
  66. #define RC_MODEL_TOTAL_BITS 11
  67. /* Called twice: once at startup and once in rc_normalize() */
  68. static void INIT rc_read(struct rc *rc)
  69. {
  70. rc->buffer_size = rc->fill((char *)rc->buffer, LZMA_IOBUF_SIZE);
  71. if (rc->buffer_size <= 0)
  72. error("unexpected EOF");
  73. rc->ptr = rc->buffer;
  74. rc->buffer_end = rc->buffer + rc->buffer_size;
  75. }
  76. /* Called once */
  77. static inline void INIT rc_init(struct rc *rc,
  78. int (*fill)(void*, unsigned int),
  79. char *buffer, int buffer_size)
  80. {
  81. rc->fill = fill;
  82. rc->buffer = (uint8_t *)buffer;
  83. rc->buffer_size = buffer_size;
  84. rc->buffer_end = rc->buffer + rc->buffer_size;
  85. rc->ptr = rc->buffer;
  86. rc->code = 0;
  87. rc->range = 0xFFFFFFFF;
  88. }
  89. static inline void INIT rc_init_code(struct rc *rc)
  90. {
  91. int i;
  92. for (i = 0; i < 5; i++) {
  93. if (rc->ptr >= rc->buffer_end)
  94. rc_read(rc);
  95. rc->code = (rc->code << 8) | *rc->ptr++;
  96. }
  97. }
  98. /* Called once. TODO: bb_maybe_free() */
  99. static inline void INIT rc_free(struct rc *rc)
  100. {
  101. free(rc->buffer);
  102. }
  103. /* Called twice, but one callsite is in inline'd rc_is_bit_0_helper() */
  104. static void INIT rc_do_normalize(struct rc *rc)
  105. {
  106. if (rc->ptr >= rc->buffer_end)
  107. rc_read(rc);
  108. rc->range <<= 8;
  109. rc->code = (rc->code << 8) | *rc->ptr++;
  110. }
  111. static inline void INIT rc_normalize(struct rc *rc)
  112. {
  113. if (rc->range < (1 << RC_TOP_BITS))
  114. rc_do_normalize(rc);
  115. }
  116. /* Called 9 times */
  117. /* Why rc_is_bit_0_helper exists?
  118. *Because we want to always expose (rc->code < rc->bound) to optimizer
  119. */
  120. static inline uint32_t INIT rc_is_bit_0_helper(struct rc *rc, uint16_t *p)
  121. {
  122. rc_normalize(rc);
  123. rc->bound = *p * (rc->range >> RC_MODEL_TOTAL_BITS);
  124. return rc->bound;
  125. }
  126. static inline int INIT rc_is_bit_0(struct rc *rc, uint16_t *p)
  127. {
  128. uint32_t t = rc_is_bit_0_helper(rc, p);
  129. return rc->code < t;
  130. }
  131. /* Called ~10 times, but very small, thus inlined */
  132. static inline void INIT rc_update_bit_0(struct rc *rc, uint16_t *p)
  133. {
  134. rc->range = rc->bound;
  135. *p += ((1 << RC_MODEL_TOTAL_BITS) - *p) >> RC_MOVE_BITS;
  136. }
  137. static inline void rc_update_bit_1(struct rc *rc, uint16_t *p)
  138. {
  139. rc->range -= rc->bound;
  140. rc->code -= rc->bound;
  141. *p -= *p >> RC_MOVE_BITS;
  142. }
  143. /* Called 4 times in unlzma loop */
  144. static int INIT rc_get_bit(struct rc *rc, uint16_t *p, int *symbol)
  145. {
  146. if (rc_is_bit_0(rc, p)) {
  147. rc_update_bit_0(rc, p);
  148. *symbol *= 2;
  149. return 0;
  150. } else {
  151. rc_update_bit_1(rc, p);
  152. *symbol = *symbol * 2 + 1;
  153. return 1;
  154. }
  155. }
  156. /* Called once */
  157. static inline int INIT rc_direct_bit(struct rc *rc)
  158. {
  159. rc_normalize(rc);
  160. rc->range >>= 1;
  161. if (rc->code >= rc->range) {
  162. rc->code -= rc->range;
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. /* Called twice */
  168. static inline void INIT
  169. rc_bit_tree_decode(struct rc *rc, uint16_t *p, int num_levels, int *symbol)
  170. {
  171. int i = num_levels;
  172. *symbol = 1;
  173. while (i--)
  174. rc_get_bit(rc, p + *symbol, symbol);
  175. *symbol -= 1 << num_levels;
  176. }
  177. /*
  178. * Small lzma deflate implementation.
  179. * Copyright (C) 2006 Aurelien Jacobs < aurel@gnuage.org >
  180. *
  181. * Based on LzmaDecode.c from the LZMA SDK 4.22 (http://www.7-zip.org/)
  182. * Copyright (C) 1999-2005 Igor Pavlov
  183. */
  184. struct lzma_header {
  185. uint8_t pos;
  186. uint32_t dict_size;
  187. uint64_t dst_size;
  188. } __attribute__ ((packed)) ;
  189. #define LZMA_BASE_SIZE 1846
  190. #define LZMA_LIT_SIZE 768
  191. #define LZMA_NUM_POS_BITS_MAX 4
  192. #define LZMA_LEN_NUM_LOW_BITS 3
  193. #define LZMA_LEN_NUM_MID_BITS 3
  194. #define LZMA_LEN_NUM_HIGH_BITS 8
  195. #define LZMA_LEN_CHOICE 0
  196. #define LZMA_LEN_CHOICE_2 (LZMA_LEN_CHOICE + 1)
  197. #define LZMA_LEN_LOW (LZMA_LEN_CHOICE_2 + 1)
  198. #define LZMA_LEN_MID (LZMA_LEN_LOW \
  199. + (1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_LOW_BITS)))
  200. #define LZMA_LEN_HIGH (LZMA_LEN_MID \
  201. +(1 << (LZMA_NUM_POS_BITS_MAX + LZMA_LEN_NUM_MID_BITS)))
  202. #define LZMA_NUM_LEN_PROBS (LZMA_LEN_HIGH + (1 << LZMA_LEN_NUM_HIGH_BITS))
  203. #define LZMA_NUM_STATES 12
  204. #define LZMA_NUM_LIT_STATES 7
  205. #define LZMA_START_POS_MODEL_INDEX 4
  206. #define LZMA_END_POS_MODEL_INDEX 14
  207. #define LZMA_NUM_FULL_DISTANCES (1 << (LZMA_END_POS_MODEL_INDEX >> 1))
  208. #define LZMA_NUM_POS_SLOT_BITS 6
  209. #define LZMA_NUM_LEN_TO_POS_STATES 4
  210. #define LZMA_NUM_ALIGN_BITS 4
  211. #define LZMA_MATCH_MIN_LEN 2
  212. #define LZMA_IS_MATCH 0
  213. #define LZMA_IS_REP (LZMA_IS_MATCH + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
  214. #define LZMA_IS_REP_G0 (LZMA_IS_REP + LZMA_NUM_STATES)
  215. #define LZMA_IS_REP_G1 (LZMA_IS_REP_G0 + LZMA_NUM_STATES)
  216. #define LZMA_IS_REP_G2 (LZMA_IS_REP_G1 + LZMA_NUM_STATES)
  217. #define LZMA_IS_REP_0_LONG (LZMA_IS_REP_G2 + LZMA_NUM_STATES)
  218. #define LZMA_POS_SLOT (LZMA_IS_REP_0_LONG \
  219. + (LZMA_NUM_STATES << LZMA_NUM_POS_BITS_MAX))
  220. #define LZMA_SPEC_POS (LZMA_POS_SLOT \
  221. +(LZMA_NUM_LEN_TO_POS_STATES << LZMA_NUM_POS_SLOT_BITS))
  222. #define LZMA_ALIGN (LZMA_SPEC_POS \
  223. + LZMA_NUM_FULL_DISTANCES - LZMA_END_POS_MODEL_INDEX)
  224. #define LZMA_LEN_CODER (LZMA_ALIGN + (1 << LZMA_NUM_ALIGN_BITS))
  225. #define LZMA_REP_LEN_CODER (LZMA_LEN_CODER + LZMA_NUM_LEN_PROBS)
  226. #define LZMA_LITERAL (LZMA_REP_LEN_CODER + LZMA_NUM_LEN_PROBS)
  227. struct writer {
  228. uint8_t *buffer;
  229. uint8_t previous_byte;
  230. size_t buffer_pos;
  231. int bufsize;
  232. size_t global_pos;
  233. int(*flush)(void*, unsigned int);
  234. struct lzma_header *header;
  235. };
  236. struct cstate {
  237. int state;
  238. uint32_t rep0, rep1, rep2, rep3;
  239. };
  240. static inline size_t INIT get_pos(struct writer *wr)
  241. {
  242. return
  243. wr->global_pos + wr->buffer_pos;
  244. }
  245. static inline uint8_t INIT peek_old_byte(struct writer *wr,
  246. uint32_t offs)
  247. {
  248. if (!wr->flush) {
  249. int32_t pos;
  250. while (offs > wr->header->dict_size)
  251. offs -= wr->header->dict_size;
  252. pos = wr->buffer_pos - offs;
  253. return wr->buffer[pos];
  254. } else {
  255. uint32_t pos = wr->buffer_pos - offs;
  256. while (pos >= wr->header->dict_size)
  257. pos += wr->header->dict_size;
  258. return wr->buffer[pos];
  259. }
  260. }
  261. static inline void INIT write_byte(struct writer *wr, uint8_t byte)
  262. {
  263. wr->buffer[wr->buffer_pos++] = wr->previous_byte = byte;
  264. if (wr->flush && wr->buffer_pos == wr->header->dict_size) {
  265. wr->buffer_pos = 0;
  266. wr->global_pos += wr->header->dict_size;
  267. wr->flush((char *)wr->buffer, wr->header->dict_size);
  268. }
  269. }
  270. static inline void INIT copy_byte(struct writer *wr, uint32_t offs)
  271. {
  272. write_byte(wr, peek_old_byte(wr, offs));
  273. }
  274. static inline void INIT copy_bytes(struct writer *wr,
  275. uint32_t rep0, int len)
  276. {
  277. do {
  278. copy_byte(wr, rep0);
  279. len--;
  280. } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
  281. }
  282. static inline void INIT process_bit0(struct writer *wr, struct rc *rc,
  283. struct cstate *cst, uint16_t *p,
  284. int pos_state, uint16_t *prob,
  285. int lc, uint32_t literal_pos_mask) {
  286. int mi = 1;
  287. rc_update_bit_0(rc, prob);
  288. prob = (p + LZMA_LITERAL +
  289. (LZMA_LIT_SIZE
  290. * (((get_pos(wr) & literal_pos_mask) << lc)
  291. + (wr->previous_byte >> (8 - lc))))
  292. );
  293. if (cst->state >= LZMA_NUM_LIT_STATES) {
  294. int match_byte = peek_old_byte(wr, cst->rep0);
  295. do {
  296. int bit;
  297. uint16_t *prob_lit;
  298. match_byte <<= 1;
  299. bit = match_byte & 0x100;
  300. prob_lit = prob + 0x100 + bit + mi;
  301. if (rc_get_bit(rc, prob_lit, &mi)) {
  302. if (!bit)
  303. break;
  304. } else {
  305. if (bit)
  306. break;
  307. }
  308. } while (mi < 0x100);
  309. }
  310. while (mi < 0x100) {
  311. uint16_t *prob_lit = prob + mi;
  312. rc_get_bit(rc, prob_lit, &mi);
  313. }
  314. write_byte(wr, mi);
  315. if (cst->state < 4)
  316. cst->state = 0;
  317. else if (cst->state < 10)
  318. cst->state -= 3;
  319. else
  320. cst->state -= 6;
  321. }
  322. static inline void INIT process_bit1(struct writer *wr, struct rc *rc,
  323. struct cstate *cst, uint16_t *p,
  324. int pos_state, uint16_t *prob) {
  325. int offset;
  326. uint16_t *prob_len;
  327. int num_bits;
  328. int len;
  329. rc_update_bit_1(rc, prob);
  330. prob = p + LZMA_IS_REP + cst->state;
  331. if (rc_is_bit_0(rc, prob)) {
  332. rc_update_bit_0(rc, prob);
  333. cst->rep3 = cst->rep2;
  334. cst->rep2 = cst->rep1;
  335. cst->rep1 = cst->rep0;
  336. cst->state = cst->state < LZMA_NUM_LIT_STATES ? 0 : 3;
  337. prob = p + LZMA_LEN_CODER;
  338. } else {
  339. rc_update_bit_1(rc, prob);
  340. prob = p + LZMA_IS_REP_G0 + cst->state;
  341. if (rc_is_bit_0(rc, prob)) {
  342. rc_update_bit_0(rc, prob);
  343. prob = (p + LZMA_IS_REP_0_LONG
  344. + (cst->state <<
  345. LZMA_NUM_POS_BITS_MAX) +
  346. pos_state);
  347. if (rc_is_bit_0(rc, prob)) {
  348. rc_update_bit_0(rc, prob);
  349. cst->state = cst->state < LZMA_NUM_LIT_STATES ?
  350. 9 : 11;
  351. copy_byte(wr, cst->rep0);
  352. return;
  353. } else {
  354. rc_update_bit_1(rc, prob);
  355. }
  356. } else {
  357. uint32_t distance;
  358. rc_update_bit_1(rc, prob);
  359. prob = p + LZMA_IS_REP_G1 + cst->state;
  360. if (rc_is_bit_0(rc, prob)) {
  361. rc_update_bit_0(rc, prob);
  362. distance = cst->rep1;
  363. } else {
  364. rc_update_bit_1(rc, prob);
  365. prob = p + LZMA_IS_REP_G2 + cst->state;
  366. if (rc_is_bit_0(rc, prob)) {
  367. rc_update_bit_0(rc, prob);
  368. distance = cst->rep2;
  369. } else {
  370. rc_update_bit_1(rc, prob);
  371. distance = cst->rep3;
  372. cst->rep3 = cst->rep2;
  373. }
  374. cst->rep2 = cst->rep1;
  375. }
  376. cst->rep1 = cst->rep0;
  377. cst->rep0 = distance;
  378. }
  379. cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
  380. prob = p + LZMA_REP_LEN_CODER;
  381. }
  382. prob_len = prob + LZMA_LEN_CHOICE;
  383. if (rc_is_bit_0(rc, prob_len)) {
  384. rc_update_bit_0(rc, prob_len);
  385. prob_len = (prob + LZMA_LEN_LOW
  386. + (pos_state <<
  387. LZMA_LEN_NUM_LOW_BITS));
  388. offset = 0;
  389. num_bits = LZMA_LEN_NUM_LOW_BITS;
  390. } else {
  391. rc_update_bit_1(rc, prob_len);
  392. prob_len = prob + LZMA_LEN_CHOICE_2;
  393. if (rc_is_bit_0(rc, prob_len)) {
  394. rc_update_bit_0(rc, prob_len);
  395. prob_len = (prob + LZMA_LEN_MID
  396. + (pos_state <<
  397. LZMA_LEN_NUM_MID_BITS));
  398. offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  399. num_bits = LZMA_LEN_NUM_MID_BITS;
  400. } else {
  401. rc_update_bit_1(rc, prob_len);
  402. prob_len = prob + LZMA_LEN_HIGH;
  403. offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  404. + (1 << LZMA_LEN_NUM_MID_BITS));
  405. num_bits = LZMA_LEN_NUM_HIGH_BITS;
  406. }
  407. }
  408. rc_bit_tree_decode(rc, prob_len, num_bits, &len);
  409. len += offset;
  410. if (cst->state < 4) {
  411. int pos_slot;
  412. cst->state += LZMA_NUM_LIT_STATES;
  413. prob =
  414. p + LZMA_POS_SLOT +
  415. ((len <
  416. LZMA_NUM_LEN_TO_POS_STATES ? len :
  417. LZMA_NUM_LEN_TO_POS_STATES - 1)
  418. << LZMA_NUM_POS_SLOT_BITS);
  419. rc_bit_tree_decode(rc, prob,
  420. LZMA_NUM_POS_SLOT_BITS,
  421. &pos_slot);
  422. if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  423. int i, mi;
  424. num_bits = (pos_slot >> 1) - 1;
  425. cst->rep0 = 2 | (pos_slot & 1);
  426. if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  427. cst->rep0 <<= num_bits;
  428. prob = p + LZMA_SPEC_POS +
  429. cst->rep0 - pos_slot - 1;
  430. } else {
  431. num_bits -= LZMA_NUM_ALIGN_BITS;
  432. while (num_bits--)
  433. cst->rep0 = (cst->rep0 << 1) |
  434. rc_direct_bit(rc);
  435. prob = p + LZMA_ALIGN;
  436. cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
  437. num_bits = LZMA_NUM_ALIGN_BITS;
  438. }
  439. i = 1;
  440. mi = 1;
  441. while (num_bits--) {
  442. if (rc_get_bit(rc, prob + mi, &mi))
  443. cst->rep0 |= i;
  444. i <<= 1;
  445. }
  446. } else
  447. cst->rep0 = pos_slot;
  448. if (++(cst->rep0) == 0)
  449. return;
  450. }
  451. len += LZMA_MATCH_MIN_LEN;
  452. copy_bytes(wr, cst->rep0, len);
  453. }
  454. STATIC inline int INIT unlzma(unsigned char *buf, int in_len,
  455. int(*fill)(void*, unsigned int),
  456. int(*flush)(void*, unsigned int),
  457. unsigned char *output,
  458. int *posp,
  459. void(*error_fn)(char *x)
  460. )
  461. {
  462. struct lzma_header header;
  463. int lc, pb, lp;
  464. uint32_t pos_state_mask;
  465. uint32_t literal_pos_mask;
  466. uint16_t *p;
  467. int num_probs;
  468. struct rc rc;
  469. int i, mi;
  470. struct writer wr;
  471. struct cstate cst;
  472. unsigned char *inbuf;
  473. int ret = -1;
  474. set_error_fn(error_fn);
  475. if (!flush)
  476. in_len -= 4; /* Uncompressed size hack active in pre-boot
  477. environment */
  478. if (buf)
  479. inbuf = buf;
  480. else
  481. inbuf = malloc(LZMA_IOBUF_SIZE);
  482. if (!inbuf) {
  483. error("Could not allocate input bufer");
  484. goto exit_0;
  485. }
  486. cst.state = 0;
  487. cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
  488. wr.header = &header;
  489. wr.flush = flush;
  490. wr.global_pos = 0;
  491. wr.previous_byte = 0;
  492. wr.buffer_pos = 0;
  493. rc_init(&rc, fill, inbuf, in_len);
  494. for (i = 0; i < sizeof(header); i++) {
  495. if (rc.ptr >= rc.buffer_end)
  496. rc_read(&rc);
  497. ((unsigned char *)&header)[i] = *rc.ptr++;
  498. }
  499. if (header.pos >= (9 * 5 * 5))
  500. error("bad header");
  501. mi = 0;
  502. lc = header.pos;
  503. while (lc >= 9) {
  504. mi++;
  505. lc -= 9;
  506. }
  507. pb = 0;
  508. lp = mi;
  509. while (lp >= 5) {
  510. pb++;
  511. lp -= 5;
  512. }
  513. pos_state_mask = (1 << pb) - 1;
  514. literal_pos_mask = (1 << lp) - 1;
  515. ENDIAN_CONVERT(header.dict_size);
  516. ENDIAN_CONVERT(header.dst_size);
  517. if (header.dict_size == 0)
  518. header.dict_size = 1;
  519. if (output)
  520. wr.buffer = output;
  521. else {
  522. wr.bufsize = MIN(header.dst_size, header.dict_size);
  523. wr.buffer = large_malloc(wr.bufsize);
  524. }
  525. if (wr.buffer == NULL)
  526. goto exit_1;
  527. num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  528. p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
  529. if (p == 0)
  530. goto exit_2;
  531. num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
  532. for (i = 0; i < num_probs; i++)
  533. p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  534. rc_init_code(&rc);
  535. while (get_pos(&wr) < header.dst_size) {
  536. int pos_state = get_pos(&wr) & pos_state_mask;
  537. uint16_t *prob = p + LZMA_IS_MATCH +
  538. (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  539. if (rc_is_bit_0(&rc, prob))
  540. process_bit0(&wr, &rc, &cst, p, pos_state, prob,
  541. lc, literal_pos_mask);
  542. else {
  543. process_bit1(&wr, &rc, &cst, p, pos_state, prob);
  544. if (cst.rep0 == 0)
  545. break;
  546. }
  547. }
  548. if (posp)
  549. *posp = rc.ptr-rc.buffer;
  550. if (wr.flush)
  551. wr.flush(wr.buffer, wr.buffer_pos);
  552. ret = 0;
  553. large_free(p);
  554. exit_2:
  555. if (!output)
  556. large_free(wr.buffer);
  557. exit_1:
  558. if (!buf)
  559. free(inbuf);
  560. exit_0:
  561. return ret;
  562. }
  563. #define decompress unlzma