decompress_unlzma.c 15 KB

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