decompress_unlzma.c 15 KB

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