decompress_unlzma.c 15 KB

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