blob: 121c3b53a6a85378a64a21518c50067f820f5f60 [file] [log] [blame]
Denys Vlasenko602ce692010-05-30 03:35:18 +02001/*
2 * .xz Stream decoder
3 *
4 * Author: Lasse Collin <lasse.collin@tukaani.org>
5 *
6 * This file has been put into the public domain.
7 * You can do whatever you want with this file.
8 */
9
10#include "xz_private.h"
11#include "xz_stream.h"
12
13/* Hash used to validate the Index field */
14struct xz_dec_hash {
15 vli_type unpadded;
16 vli_type uncompressed;
17 uint32_t crc32;
18};
19
20struct xz_dec {
21 /* Position in dec_main() */
22 enum {
23 SEQ_STREAM_HEADER,
24 SEQ_BLOCK_START,
25 SEQ_BLOCK_HEADER,
26 SEQ_BLOCK_UNCOMPRESS,
27 SEQ_BLOCK_PADDING,
28 SEQ_BLOCK_CHECK,
29 SEQ_INDEX,
30 SEQ_INDEX_PADDING,
31 SEQ_INDEX_CRC32,
32 SEQ_STREAM_FOOTER
33 } sequence;
34
35 /* Position in variable-length integers and Check fields */
36 uint32_t pos;
37
38 /* Variable-length integer decoded by dec_vli() */
39 vli_type vli;
40
41 /* Saved in_pos and out_pos */
42 size_t in_start;
43 size_t out_start;
44
45 /* CRC32 value in Block or Index */
46 uint32_t crc32;
47
48 /* True if CRC32 is calculated from uncompressed data */
49 uint8_t crc_type;
50
51 /* True if we are operating in single-call mode. */
52 bool single_call;
53
54 /*
55 * True if the next call to xz_dec_run() is allowed to return
56 * XZ_BUF_ERROR.
57 */
58 bool allow_buf_error;
59
60 /* Information stored in Block Header */
61 struct {
62 /*
63 * Value stored in the Compressed Size field, or
64 * VLI_UNKNOWN if Compressed Size is not present.
65 */
66 vli_type compressed;
67
68 /*
69 * Value stored in the Uncompressed Size field, or
70 * VLI_UNKNOWN if Uncompressed Size is not present.
71 */
72 vli_type uncompressed;
73
74 /* Size of the Block Header field */
75 uint32_t size;
76 } block_header;
77
78 /* Information collected when decoding Blocks */
79 struct {
80 /* Observed compressed size of the current Block */
81 vli_type compressed;
82
83 /* Observed uncompressed size of the current Block */
84 vli_type uncompressed;
85
86 /* Number of Blocks decoded so far */
87 vli_type count;
88
89 /*
90 * Hash calculated from the Block sizes. This is used to
91 * validate the Index field.
92 */
93 struct xz_dec_hash hash;
94 } block;
95
96 /* Variables needed when verifying the Index field */
97 struct {
98 /* Position in dec_index() */
99 enum {
100 SEQ_INDEX_COUNT,
101 SEQ_INDEX_UNPADDED,
102 SEQ_INDEX_UNCOMPRESSED
103 } sequence;
104
105 /* Size of the Index in bytes */
106 vli_type size;
107
108 /* Number of Records (matches block.count in valid files) */
109 vli_type count;
110
111 /*
112 * Hash calculated from the Records (matches block.hash in
113 * valid files).
114 */
115 struct xz_dec_hash hash;
116 } index;
117
118 /*
119 * Temporary buffer needed to hold Stream Header, Block Header,
120 * and Stream Footer. The Block Header is the biggest (1 KiB)
121 * so we reserve space according to that. buf[] has to be aligned
122 * to a multiple of four bytes; the size_t variables before it
123 * should guarantee this.
124 */
125 struct {
126 size_t pos;
127 size_t size;
128 uint8_t buf[1024];
129 } temp;
130
131 struct xz_dec_lzma2 *lzma2;
132
133#ifdef XZ_DEC_BCJ
134 struct xz_dec_bcj *bcj;
135 bool bcj_active;
136#endif
137
138 uint32_t crc32_table[256];
139};
140
141/*
142 * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
143 * must have set s->temp.pos to indicate how much data we are supposed
144 * to copy into s->temp.buf. Return true once s->temp.pos has reached
145 * s->temp.size.
146 */
147static bool XZ_FUNC fill_temp(struct xz_dec *s, struct xz_buf *b)
148{
149 size_t copy_size = min_t(size_t,
150 b->in_size - b->in_pos, s->temp.size - s->temp.pos);
151
152 memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
153 b->in_pos += copy_size;
154 s->temp.pos += copy_size;
155
156 if (s->temp.pos == s->temp.size) {
157 s->temp.pos = 0;
158 return true;
159 }
160
161 return false;
162}
163
164/* Decode a variable-length integer (little-endian base-128 encoding) */
165static enum xz_ret XZ_FUNC dec_vli(struct xz_dec *s,
166 const uint8_t *in, size_t *in_pos, size_t in_size)
167{
168 uint8_t byte;
169
170 if (s->pos == 0)
171 s->vli = 0;
172
173 while (*in_pos < in_size) {
174 byte = in[*in_pos];
175 ++*in_pos;
176
177 s->vli |= (vli_type)(byte & 0x7F) << s->pos;
178
179 if ((byte & 0x80) == 0) {
180 /* Don't allow non-minimal encodings. */
181 if (byte == 0 && s->pos != 0)
182 return XZ_DATA_ERROR;
183
184 s->pos = 0;
185 return XZ_STREAM_END;
186 }
187
188 s->pos += 7;
189 if (s->pos == 7 * VLI_BYTES_MAX)
190 return XZ_DATA_ERROR;
191 }
192
193 return XZ_OK;
194}
195
196/*
197 * Decode the Compressed Data field from a Block. Update and validate
198 * the observed compressed and uncompressed sizes of the Block so that
199 * they don't exceed the values possibly stored in the Block Header
200 * (validation assumes that no integer overflow occurs, since vli_type
201 * is normally uint64_t). Update the CRC32 if presence of the CRC32
202 * field was indicated in Stream Header.
203 *
204 * Once the decoding is finished, validate that the observed sizes match
205 * the sizes possibly stored in the Block Header. Update the hash and
206 * Block count, which are later used to validate the Index field.
207 */
208static enum xz_ret XZ_FUNC dec_block(struct xz_dec *s, struct xz_buf *b)
209{
210 enum xz_ret ret;
211
212 s->in_start = b->in_pos;
213 s->out_start = b->out_pos;
214
215#ifdef XZ_DEC_BCJ
216 if (s->bcj_active)
217 ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
218 else
219#endif
220 ret = xz_dec_lzma2_run(s->lzma2, b);
221
222 s->block.compressed += b->in_pos - s->in_start;
223 s->block.uncompressed += b->out_pos - s->out_start;
224
225 /*
226 * There is no need to separately check for VLI_UNKNOWN, since
227 * the observed sizes are always smaller than VLI_UNKNOWN.
228 */
229 if (s->block.compressed > s->block_header.compressed
230 || s->block.uncompressed
231 > s->block_header.uncompressed)
232 return XZ_DATA_ERROR;
233
234 if (s->crc_type == 0x01)
235 s->crc32 = xz_crc32(s->crc32_table,
236 b->out + s->out_start,
237 b->out_pos - s->out_start, s->crc32);
238
239 if (ret == XZ_STREAM_END) {
240 if (s->block_header.compressed != VLI_UNKNOWN
241 && s->block_header.compressed
242 != s->block.compressed)
243 return XZ_DATA_ERROR;
244
245 if (s->block_header.uncompressed != VLI_UNKNOWN
246 && s->block_header.uncompressed
247 != s->block.uncompressed)
248 return XZ_DATA_ERROR;
249
250 s->block.hash.unpadded += s->block_header.size
251 + s->block.compressed;
252 if (s->crc_type == 0x01)
253 s->block.hash.unpadded += 4;
254 if (s->crc_type == 0x04) /* CRC64 */
255 s->block.hash.unpadded += 8;
256 if (s->crc_type == 0x0A) /* SHA-256 */
257 s->block.hash.unpadded += 32;
258
259 s->block.hash.uncompressed += s->block.uncompressed;
260 s->block.hash.crc32 = xz_crc32(s->crc32_table,
261 (const uint8_t *)&s->block.hash,
262 sizeof(s->block.hash), s->block.hash.crc32);
263
264 ++s->block.count;
265 }
266
267 return ret;
268}
269
270/* Update the Index size and the CRC32 value. */
271static void XZ_FUNC index_update(struct xz_dec *s, const struct xz_buf *b)
272{
273 size_t in_used = b->in_pos - s->in_start;
274 s->index.size += in_used;
275 s->crc32 = xz_crc32(s->crc32_table, b->in + s->in_start, in_used, s->crc32);
276}
277
278/*
279 * Decode the Number of Records, Unpadded Size, and Uncompressed Size
280 * fields from the Index field. That is, Index Padding and CRC32 are not
281 * decoded by this function.
282 *
283 * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
284 * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
285 */
286static enum xz_ret XZ_FUNC dec_index(struct xz_dec *s, struct xz_buf *b)
287{
288 enum xz_ret ret;
289
290 do {
291 ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
292 if (ret != XZ_STREAM_END) {
293 index_update(s, b);
294 return ret;
295 }
296
297 switch (s->index.sequence) {
298 case SEQ_INDEX_COUNT:
299 s->index.count = s->vli;
300
301 /*
302 * Validate that the Number of Records field
303 * indicates the same number of Records as
304 * there were Blocks in the Stream.
305 */
306 if (s->index.count != s->block.count)
307 return XZ_DATA_ERROR;
308
309 s->index.sequence = SEQ_INDEX_UNPADDED;
310 break;
311
312 case SEQ_INDEX_UNPADDED:
313 s->index.hash.unpadded += s->vli;
314 s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
315 break;
316
317 case SEQ_INDEX_UNCOMPRESSED:
318 s->index.hash.uncompressed += s->vli;
319 s->index.hash.crc32 = xz_crc32(s->crc32_table,
320 (const uint8_t *)&s->index.hash,
321 sizeof(s->index.hash),
322 s->index.hash.crc32);
323 --s->index.count;
324 s->index.sequence = SEQ_INDEX_UNPADDED;
325 break;
326 }
327 } while (s->index.count > 0);
328
329 return XZ_STREAM_END;
330}
331
332/*
333 * Validate that the next four input bytes match the value of s->crc32.
334 * s->pos must be zero when starting to validate the first byte.
335 */
336static enum xz_ret XZ_FUNC crc32_validate(struct xz_dec *s, struct xz_buf *b)
337{
338 do {
339 if (b->in_pos == b->in_size)
340 return XZ_OK;
341
342 if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++])
343 return XZ_DATA_ERROR;
344
345 s->pos += 8;
346
347 } while (s->pos < 32);
348
349 s->crc32 = 0;
350 s->pos = 0;
351
352 return XZ_STREAM_END;
353}
354
355/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
356static enum xz_ret XZ_FUNC dec_stream_header(struct xz_dec *s)
357{
358 if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
359 return XZ_FORMAT_ERROR;
360
361 if (xz_crc32(s->crc32_table, s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)
362 != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
363 return XZ_DATA_ERROR;
364
365 /*
366 * Decode the Stream Flags field. Of integrity checks, we support
367 * only none (Check ID = 0) and CRC32 (Check ID = 1).
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200368 * We also accept CRC64 and SHA-256, but they will not be verified.
Denys Vlasenko602ce692010-05-30 03:35:18 +0200369 */
370 if (s->temp.buf[HEADER_MAGIC_SIZE] != 0
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200371 || (s->temp.buf[HEADER_MAGIC_SIZE + 1] > 1
372 && s->temp.buf[HEADER_MAGIC_SIZE + 1] != 0x04 /* CRC64 */
373 && s->temp.buf[HEADER_MAGIC_SIZE + 1] != 0x0A /* SHA-256 */
374 )
Denys Vlasenko602ce692010-05-30 03:35:18 +0200375 ) {
Denys Vlasenko602ce692010-05-30 03:35:18 +0200376 return XZ_OPTIONS_ERROR;
377 }
378
379 s->crc_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];
380
381 return XZ_OK;
382}
383
384/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
385static enum xz_ret XZ_FUNC dec_stream_footer(struct xz_dec *s)
386{
387 if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
388 return XZ_DATA_ERROR;
389
390 if (xz_crc32(s->crc32_table, s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))
391 return XZ_DATA_ERROR;
392
393 /*
394 * Validate Backward Size. Note that we never added the size of the
395 * Index CRC32 field to s->index.size, thus we use s->index.size / 4
396 * instead of s->index.size / 4 - 1.
397 */
398 if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
399 return XZ_DATA_ERROR;
400
401 if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->crc_type)
402 return XZ_DATA_ERROR;
403
404 /*
405 * Use XZ_STREAM_END instead of XZ_OK to be more convenient
406 * for the caller.
407 */
408 return XZ_STREAM_END;
409}
410
411/* Decode the Block Header and initialize the filter chain. */
412static enum xz_ret XZ_FUNC dec_block_header(struct xz_dec *s)
413{
414 enum xz_ret ret;
415
416 /*
417 * Validate the CRC32. We know that the temp buffer is at least
418 * eight bytes so this is safe.
419 */
420 s->temp.size -= 4;
421 if (xz_crc32(s->crc32_table, s->temp.buf, s->temp.size, 0)
422 != get_le32(s->temp.buf + s->temp.size))
423 return XZ_DATA_ERROR;
424
425 s->temp.pos = 2;
426
427 /*
428 * Catch unsupported Block Flags. We support only one or two filters
429 * in the chain, so we catch that with the same test.
430 */
431#ifdef XZ_DEC_BCJ
432 if (s->temp.buf[1] & 0x3E)
433#else
434 if (s->temp.buf[1] & 0x3F)
435#endif
Denys Vlasenko602ce692010-05-30 03:35:18 +0200436 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200437
438 /* Compressed Size */
439 if (s->temp.buf[1] & 0x40) {
440 if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
441 != XZ_STREAM_END)
442 return XZ_DATA_ERROR;
443
444 s->block_header.compressed = s->vli;
445 } else {
446 s->block_header.compressed = VLI_UNKNOWN;
447 }
448
449 /* Uncompressed Size */
450 if (s->temp.buf[1] & 0x80) {
451 if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
452 != XZ_STREAM_END)
453 return XZ_DATA_ERROR;
454
455 s->block_header.uncompressed = s->vli;
456 } else {
457 s->block_header.uncompressed = VLI_UNKNOWN;
458 }
459
460#ifdef XZ_DEC_BCJ
461 /* If there are two filters, the first one must be a BCJ filter. */
462 s->bcj_active = s->temp.buf[1] & 0x01;
463 if (s->bcj_active) {
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200464 if (s->temp.size - s->temp.pos < 2)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200465 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200466
467 ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
468 if (ret != XZ_OK)
469 return ret;
470
471 /*
472 * We don't support custom start offset,
473 * so Size of Properties must be zero.
474 */
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200475 if (s->temp.buf[s->temp.pos++] != 0x00)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200476 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200477 }
478#endif
479
480 /* Valid Filter Flags always take at least two bytes. */
481 if (s->temp.size - s->temp.pos < 2)
482 return XZ_DATA_ERROR;
483
484 /* Filter ID = LZMA2 */
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200485 if (s->temp.buf[s->temp.pos++] != 0x21)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200486 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200487
488 /* Size of Properties = 1-byte Filter Properties */
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200489 if (s->temp.buf[s->temp.pos++] != 0x01)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200490 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200491
492 /* Filter Properties contains LZMA2 dictionary size. */
493 if (s->temp.size - s->temp.pos < 1)
494 return XZ_DATA_ERROR;
495
496 ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
497 if (ret != XZ_OK)
498 return ret;
499
500 /* The rest must be Header Padding. */
501 while (s->temp.pos < s->temp.size)
Denys Vlasenkoacaaca82010-05-30 04:50:21 +0200502 if (s->temp.buf[s->temp.pos++] != 0x00)
Denys Vlasenko602ce692010-05-30 03:35:18 +0200503 return XZ_OPTIONS_ERROR;
Denys Vlasenko602ce692010-05-30 03:35:18 +0200504
505 s->temp.pos = 0;
506 s->block.compressed = 0;
507 s->block.uncompressed = 0;
508
509 return XZ_OK;
510}
511
512static enum xz_ret XZ_FUNC dec_main(struct xz_dec *s, struct xz_buf *b)
513{
514 enum xz_ret ret;
515
516 /*
517 * Store the start position for the case when we are in the middle
518 * of the Index field.
519 */
520 s->in_start = b->in_pos;
521
522 while (true) {
523 switch (s->sequence) {
524 case SEQ_STREAM_HEADER:
525 /*
526 * Stream Header is copied to s->temp, and then
527 * decoded from there. This way if the caller
528 * gives us only little input at a time, we can
529 * still keep the Stream Header decoding code
530 * simple. Similar approach is used in many places
531 * in this file.
532 */
533 if (!fill_temp(s, b))
534 return XZ_OK;
535
536 ret = dec_stream_header(s);
537 if (ret != XZ_OK)
538 return ret;
539
540 s->sequence = SEQ_BLOCK_START;
541
542 case SEQ_BLOCK_START:
543 /* We need one byte of input to continue. */
544 if (b->in_pos == b->in_size)
545 return XZ_OK;
546
547 /* See if this is the beginning of the Index field. */
548 if (b->in[b->in_pos] == 0) {
549 s->in_start = b->in_pos++;
550 s->sequence = SEQ_INDEX;
551 break;
552 }
553
554 /*
555 * Calculate the size of the Block Header and
556 * prepare to decode it.
557 */
558 s->block_header.size
559 = ((uint32_t)b->in[b->in_pos] + 1) * 4;
560
561 s->temp.size = s->block_header.size;
562 s->temp.pos = 0;
563 s->sequence = SEQ_BLOCK_HEADER;
564
565 case SEQ_BLOCK_HEADER:
566 if (!fill_temp(s, b))
567 return XZ_OK;
568
569 ret = dec_block_header(s);
570 if (ret != XZ_OK)
571 return ret;
572
573 s->sequence = SEQ_BLOCK_UNCOMPRESS;
574
575 case SEQ_BLOCK_UNCOMPRESS:
576 ret = dec_block(s, b);
577 if (ret != XZ_STREAM_END)
578 return ret;
579
580 s->sequence = SEQ_BLOCK_PADDING;
581
582 case SEQ_BLOCK_PADDING:
583 /*
584 * Size of Compressed Data + Block Padding
585 * must be a multiple of four. We don't need
586 * s->block.compressed for anything else
587 * anymore, so we use it here to test the size
588 * of the Block Padding field.
589 */
590 while (s->block.compressed & 3) {
591 if (b->in_pos == b->in_size)
592 return XZ_OK;
593
594 if (b->in[b->in_pos++] != 0)
595 return XZ_DATA_ERROR;
596
597 ++s->block.compressed;
598 }
599
600 s->sequence = SEQ_BLOCK_CHECK;
601
602 case SEQ_BLOCK_CHECK:
603 if (s->crc_type == 0x01) {
604 ret = crc32_validate(s, b);
605 if (ret != XZ_STREAM_END)
606 return ret;
607 }
608
609 s->sequence = SEQ_BLOCK_START;
610 break;
611
612 case SEQ_INDEX:
613 ret = dec_index(s, b);
614 if (ret != XZ_STREAM_END)
615 return ret;
616
617 s->sequence = SEQ_INDEX_PADDING;
618
619 case SEQ_INDEX_PADDING:
620 while ((s->index.size + (b->in_pos - s->in_start))
621 & 3) {
622 if (b->in_pos == b->in_size) {
623 index_update(s, b);
624 return XZ_OK;
625 }
626
627 if (b->in[b->in_pos++] != 0)
628 return XZ_DATA_ERROR;
629 }
630
631 /* Finish the CRC32 value and Index size. */
632 index_update(s, b);
633
634 /* Compare the hashes to validate the Index field. */
635 if (!memeq(&s->block.hash, &s->index.hash,
636 sizeof(s->block.hash)))
637 return XZ_DATA_ERROR;
638
639 s->sequence = SEQ_INDEX_CRC32;
640
641 case SEQ_INDEX_CRC32:
642 ret = crc32_validate(s, b);
643 if (ret != XZ_STREAM_END)
644 return ret;
645
646 s->temp.size = STREAM_HEADER_SIZE;
647 s->sequence = SEQ_STREAM_FOOTER;
648
649 case SEQ_STREAM_FOOTER:
650 if (!fill_temp(s, b))
651 return XZ_OK;
652
653 return dec_stream_footer(s);
654 }
655 }
656
657 /* Never reached */
658}
659
660/*
661 * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
662 * multi-call and single-call decoding.
663 *
664 * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
665 * are not going to make any progress anymore. This is to prevent the caller
666 * from calling us infinitely when the input file is truncated or otherwise
667 * corrupt. Since zlib-style API allows that the caller fills the input buffer
668 * only when the decoder doesn't produce any new output, we have to be careful
669 * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
670 * after the second consecutive call to xz_dec_run() that makes no progress.
671 *
672 * In single-call mode, if we couldn't decode everything and no error
673 * occurred, either the input is truncated or the output buffer is too small.
674 * Since we know that the last input byte never produces any output, we know
675 * that if all the input was consumed and decoding wasn't finished, the file
676 * must be corrupt. Otherwise the output buffer has to be too small or the
677 * file is corrupt in a way that decoding it produces too big output.
678 *
679 * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
680 * their original values. This is because with some filter chains there won't
681 * be any valid uncompressed data in the output buffer unless the decoding
682 * actually succeeds (that's the price to pay of using the output buffer as
683 * the workspace).
684 */
685XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b)
686{
687 size_t in_start;
688 size_t out_start;
689 enum xz_ret ret;
690
691 if (s->single_call)
692 xz_dec_reset(s);
693
694 in_start = b->in_pos;
695 out_start = b->out_pos;
696 ret = dec_main(s, b);
697
698 if (s->single_call) {
699 if (ret == XZ_OK)
700 ret = b->in_pos == b->in_size
701 ? XZ_DATA_ERROR : XZ_BUF_ERROR;
702
703 if (ret != XZ_STREAM_END) {
704 b->in_pos = in_start;
705 b->out_pos = out_start;
706 }
707
708 } else if (ret == XZ_OK && in_start == b->in_pos
709 && out_start == b->out_pos) {
710 if (s->allow_buf_error)
711 ret = XZ_BUF_ERROR;
712
713 s->allow_buf_error = true;
714 } else {
715 s->allow_buf_error = false;
716 }
717
718 return ret;
719}
720
721XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init(uint32_t dict_max)
722{
723 struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL);
724 if (s == NULL)
725 return NULL;
726
727 s->single_call = dict_max == 0;
728
729#ifdef XZ_DEC_BCJ
730 s->bcj = xz_dec_bcj_create(s->single_call);
731 if (s->bcj == NULL)
732 goto error_bcj;
733#endif
734
735 s->lzma2 = xz_dec_lzma2_create(dict_max);
736 if (s->lzma2 == NULL)
737 goto error_lzma2;
738
739 xz_dec_reset(s);
740 return s;
741
742error_lzma2:
743#ifdef XZ_DEC_BCJ
744 xz_dec_bcj_end(s->bcj);
745error_bcj:
746#endif
747 kfree(s);
748 return NULL;
749}
750
751XZ_EXTERN void XZ_FUNC xz_dec_reset(struct xz_dec *s)
752{
753 s->sequence = SEQ_STREAM_HEADER;
754 s->allow_buf_error = false;
755 s->pos = 0;
756 s->crc32 = 0;
757 memzero(&s->block, sizeof(s->block));
758 memzero(&s->index, sizeof(s->index));
759 s->temp.pos = 0;
760 s->temp.size = STREAM_HEADER_SIZE;
761}
762
763XZ_EXTERN void XZ_FUNC xz_dec_end(struct xz_dec *s)
764{
765 if (s != NULL) {
766 xz_dec_lzma2_end(s->lzma2);
767#ifdef XZ_DEC_BCJ
768 xz_dec_bcj_end(s->bcj);
769#endif
770 kfree(s);
771 }
772}