decompress_unlzma.c 16 KB

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