decompress_unlzma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. long (*fill)(void*, unsigned long);
  58. uint8_t *ptr;
  59. uint8_t *buffer;
  60. uint8_t *buffer_end;
  61. long 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 long INIT nofill(void *buffer, unsigned long 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. long (*fill)(void*, unsigned long),
  86. char *buffer, long 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. long (*flush)(void*, unsigned long);
  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 int 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. if (wr->flush((char *)wr->buffer, wr->header->dict_size)
  273. != wr->header->dict_size)
  274. return -1;
  275. }
  276. return 0;
  277. }
  278. static inline int INIT copy_byte(struct writer *wr, uint32_t offs)
  279. {
  280. return write_byte(wr, peek_old_byte(wr, offs));
  281. }
  282. static inline int INIT copy_bytes(struct writer *wr,
  283. uint32_t rep0, int len)
  284. {
  285. do {
  286. if (copy_byte(wr, rep0))
  287. return -1;
  288. len--;
  289. } while (len != 0 && wr->buffer_pos < wr->header->dst_size);
  290. return len;
  291. }
  292. static inline int 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. if (cst->state < 4)
  325. cst->state = 0;
  326. else if (cst->state < 10)
  327. cst->state -= 3;
  328. else
  329. cst->state -= 6;
  330. return write_byte(wr, mi);
  331. }
  332. static inline int 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. return copy_byte(wr, cst->rep0);
  362. } else {
  363. rc_update_bit_1(rc, prob);
  364. }
  365. } else {
  366. uint32_t distance;
  367. rc_update_bit_1(rc, prob);
  368. prob = p + LZMA_IS_REP_G1 + cst->state;
  369. if (rc_is_bit_0(rc, prob)) {
  370. rc_update_bit_0(rc, prob);
  371. distance = cst->rep1;
  372. } else {
  373. rc_update_bit_1(rc, prob);
  374. prob = p + LZMA_IS_REP_G2 + cst->state;
  375. if (rc_is_bit_0(rc, prob)) {
  376. rc_update_bit_0(rc, prob);
  377. distance = cst->rep2;
  378. } else {
  379. rc_update_bit_1(rc, prob);
  380. distance = cst->rep3;
  381. cst->rep3 = cst->rep2;
  382. }
  383. cst->rep2 = cst->rep1;
  384. }
  385. cst->rep1 = cst->rep0;
  386. cst->rep0 = distance;
  387. }
  388. cst->state = cst->state < LZMA_NUM_LIT_STATES ? 8 : 11;
  389. prob = p + LZMA_REP_LEN_CODER;
  390. }
  391. prob_len = prob + LZMA_LEN_CHOICE;
  392. if (rc_is_bit_0(rc, prob_len)) {
  393. rc_update_bit_0(rc, prob_len);
  394. prob_len = (prob + LZMA_LEN_LOW
  395. + (pos_state <<
  396. LZMA_LEN_NUM_LOW_BITS));
  397. offset = 0;
  398. num_bits = LZMA_LEN_NUM_LOW_BITS;
  399. } else {
  400. rc_update_bit_1(rc, prob_len);
  401. prob_len = prob + LZMA_LEN_CHOICE_2;
  402. if (rc_is_bit_0(rc, prob_len)) {
  403. rc_update_bit_0(rc, prob_len);
  404. prob_len = (prob + LZMA_LEN_MID
  405. + (pos_state <<
  406. LZMA_LEN_NUM_MID_BITS));
  407. offset = 1 << LZMA_LEN_NUM_LOW_BITS;
  408. num_bits = LZMA_LEN_NUM_MID_BITS;
  409. } else {
  410. rc_update_bit_1(rc, prob_len);
  411. prob_len = prob + LZMA_LEN_HIGH;
  412. offset = ((1 << LZMA_LEN_NUM_LOW_BITS)
  413. + (1 << LZMA_LEN_NUM_MID_BITS));
  414. num_bits = LZMA_LEN_NUM_HIGH_BITS;
  415. }
  416. }
  417. rc_bit_tree_decode(rc, prob_len, num_bits, &len);
  418. len += offset;
  419. if (cst->state < 4) {
  420. int pos_slot;
  421. cst->state += LZMA_NUM_LIT_STATES;
  422. prob =
  423. p + LZMA_POS_SLOT +
  424. ((len <
  425. LZMA_NUM_LEN_TO_POS_STATES ? len :
  426. LZMA_NUM_LEN_TO_POS_STATES - 1)
  427. << LZMA_NUM_POS_SLOT_BITS);
  428. rc_bit_tree_decode(rc, prob,
  429. LZMA_NUM_POS_SLOT_BITS,
  430. &pos_slot);
  431. if (pos_slot >= LZMA_START_POS_MODEL_INDEX) {
  432. int i, mi;
  433. num_bits = (pos_slot >> 1) - 1;
  434. cst->rep0 = 2 | (pos_slot & 1);
  435. if (pos_slot < LZMA_END_POS_MODEL_INDEX) {
  436. cst->rep0 <<= num_bits;
  437. prob = p + LZMA_SPEC_POS +
  438. cst->rep0 - pos_slot - 1;
  439. } else {
  440. num_bits -= LZMA_NUM_ALIGN_BITS;
  441. while (num_bits--)
  442. cst->rep0 = (cst->rep0 << 1) |
  443. rc_direct_bit(rc);
  444. prob = p + LZMA_ALIGN;
  445. cst->rep0 <<= LZMA_NUM_ALIGN_BITS;
  446. num_bits = LZMA_NUM_ALIGN_BITS;
  447. }
  448. i = 1;
  449. mi = 1;
  450. while (num_bits--) {
  451. if (rc_get_bit(rc, prob + mi, &mi))
  452. cst->rep0 |= i;
  453. i <<= 1;
  454. }
  455. } else
  456. cst->rep0 = pos_slot;
  457. if (++(cst->rep0) == 0)
  458. return 0;
  459. if (cst->rep0 > wr->header->dict_size
  460. || cst->rep0 > get_pos(wr))
  461. return -1;
  462. }
  463. len += LZMA_MATCH_MIN_LEN;
  464. return copy_bytes(wr, cst->rep0, len);
  465. }
  466. STATIC inline int INIT unlzma(unsigned char *buf, long in_len,
  467. long (*fill)(void*, unsigned long),
  468. long (*flush)(void*, unsigned long),
  469. unsigned char *output,
  470. long *posp,
  471. void(*error)(char *x)
  472. )
  473. {
  474. struct lzma_header header;
  475. int lc, pb, lp;
  476. uint32_t pos_state_mask;
  477. uint32_t literal_pos_mask;
  478. uint16_t *p;
  479. int num_probs;
  480. struct rc rc;
  481. int i, mi;
  482. struct writer wr;
  483. struct cstate cst;
  484. unsigned char *inbuf;
  485. int ret = -1;
  486. rc.error = error;
  487. if (buf)
  488. inbuf = buf;
  489. else
  490. inbuf = malloc(LZMA_IOBUF_SIZE);
  491. if (!inbuf) {
  492. error("Could not allocate input buffer");
  493. goto exit_0;
  494. }
  495. cst.state = 0;
  496. cst.rep0 = cst.rep1 = cst.rep2 = cst.rep3 = 1;
  497. wr.header = &header;
  498. wr.flush = flush;
  499. wr.global_pos = 0;
  500. wr.previous_byte = 0;
  501. wr.buffer_pos = 0;
  502. rc_init(&rc, fill, inbuf, in_len);
  503. for (i = 0; i < sizeof(header); i++) {
  504. if (rc.ptr >= rc.buffer_end)
  505. rc_read(&rc);
  506. ((unsigned char *)&header)[i] = *rc.ptr++;
  507. }
  508. if (header.pos >= (9 * 5 * 5)) {
  509. error("bad header");
  510. goto exit_1;
  511. }
  512. mi = 0;
  513. lc = header.pos;
  514. while (lc >= 9) {
  515. mi++;
  516. lc -= 9;
  517. }
  518. pb = 0;
  519. lp = mi;
  520. while (lp >= 5) {
  521. pb++;
  522. lp -= 5;
  523. }
  524. pos_state_mask = (1 << pb) - 1;
  525. literal_pos_mask = (1 << lp) - 1;
  526. ENDIAN_CONVERT(header.dict_size);
  527. ENDIAN_CONVERT(header.dst_size);
  528. if (header.dict_size == 0)
  529. header.dict_size = 1;
  530. if (output)
  531. wr.buffer = output;
  532. else {
  533. wr.bufsize = MIN(header.dst_size, header.dict_size);
  534. wr.buffer = large_malloc(wr.bufsize);
  535. }
  536. if (wr.buffer == NULL)
  537. goto exit_1;
  538. num_probs = LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp));
  539. p = (uint16_t *) large_malloc(num_probs * sizeof(*p));
  540. if (p == NULL)
  541. goto exit_2;
  542. num_probs = LZMA_LITERAL + (LZMA_LIT_SIZE << (lc + lp));
  543. for (i = 0; i < num_probs; i++)
  544. p[i] = (1 << RC_MODEL_TOTAL_BITS) >> 1;
  545. rc_init_code(&rc);
  546. while (get_pos(&wr) < header.dst_size) {
  547. int pos_state = get_pos(&wr) & pos_state_mask;
  548. uint16_t *prob = p + LZMA_IS_MATCH +
  549. (cst.state << LZMA_NUM_POS_BITS_MAX) + pos_state;
  550. if (rc_is_bit_0(&rc, prob)) {
  551. if (process_bit0(&wr, &rc, &cst, p, pos_state, prob,
  552. lc, literal_pos_mask)) {
  553. error("LZMA data is corrupt");
  554. goto exit_3;
  555. }
  556. } else {
  557. if (process_bit1(&wr, &rc, &cst, p, pos_state, prob)) {
  558. error("LZMA data is corrupt");
  559. goto exit_3;
  560. }
  561. if (cst.rep0 == 0)
  562. break;
  563. }
  564. if (rc.buffer_size <= 0)
  565. goto exit_3;
  566. }
  567. if (posp)
  568. *posp = rc.ptr-rc.buffer;
  569. if (!wr.flush || wr.flush(wr.buffer, wr.buffer_pos) == wr.buffer_pos)
  570. ret = 0;
  571. exit_3:
  572. large_free(p);
  573. exit_2:
  574. if (!output)
  575. large_free(wr.buffer);
  576. exit_1:
  577. if (!buf)
  578. free(inbuf);
  579. exit_0:
  580. return ret;
  581. }
  582. #ifdef PREBOOT
  583. STATIC int INIT __decompress(unsigned char *buf, long in_len,
  584. long (*fill)(void*, unsigned long),
  585. long (*flush)(void*, unsigned long),
  586. unsigned char *output, long out_len,
  587. long *posp,
  588. void (*error)(char *x))
  589. {
  590. return unlzma(buf, in_len - 4, fill, flush, output, posp, error);
  591. }
  592. #endif