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