decompress_unlzma.c 15 KB

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