/[packages]/cauldron/xen/current/SOURCES/xen-4.1.2-have-xz-kernel-support.patch
ViewVC logotype

Contents of /cauldron/xen/current/SOURCES/xen-4.1.2-have-xz-kernel-support.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226963 - (show annotations) (download)
Mon Mar 26 20:54:32 2012 UTC (8 years, 2 months ago) by alien
File size: 96560 byte(s)
SILENT: forgot to add patch
1
2 # HG changeset patch
3 # User Jan Beulich <jbeulich@novell.com>
4 # Date 1299687538 0
5 # Node ID 9eb9948904cd0b9db29110cdf0b0aaaf7ad16879
6 # Parent d428fa67abaa0db20b915a697f1d5ba16e554185
7 Add Dom0 xz kernel decompression
8
9 Largely taken from Linux 2.6.38 and made build/work for Xen.
10
11 Signed-off-by: Jan Beulich <jbeulich@novell.com>
12
13 diff -r d428fa67abaa -r 9eb9948904cd xen/common/Makefile
14 --- a/xen/common/Makefile Wed Mar 09 16:17:26 2011 +0000
15 +++ b/xen/common/Makefile Wed Mar 09 16:18:58 2011 +0000
16 @@ -43,7 +43,7 @@
17 obj-y += rbtree.o
18 obj-y += lzo.o
19
20 -obj-$(CONFIG_X86) += decompress.o bunzip2.o unlzma.o unlzo.o
21 +obj-$(CONFIG_X86) += decompress.o bunzip2.o unxz.o unlzma.o unlzo.o
22
23 obj-$(perfc) += perfc.o
24 obj-$(crash_debug) += gdbstub.o
25 diff -r d428fa67abaa -r 9eb9948904cd xen/common/decompress.c
26 --- a/xen/common/decompress.c Wed Mar 09 16:17:26 2011 +0000
27 +++ b/xen/common/decompress.c Wed Mar 09 16:18:58 2011 +0000
28 @@ -20,6 +20,9 @@
29 if ( len >= 3 && !memcmp(inbuf, "\x42\x5a\x68", 3) )
30 return bunzip2(inbuf, len, NULL, NULL, outbuf, NULL, error);
31
32 + if ( len >= 6 && !memcmp(inbuf, "\3757zXZ", 6) )
33 + return unxz(inbuf, len, NULL, NULL, outbuf, NULL, error);
34 +
35 if ( len >= 2 && !memcmp(inbuf, "\135\000", 2) )
36 return unlzma(inbuf, len, NULL, NULL, outbuf, NULL, error);
37
38 diff -r d428fa67abaa -r 9eb9948904cd xen/common/decompress.h
39 --- a/xen/common/decompress.h Wed Mar 09 16:17:26 2011 +0000
40 +++ b/xen/common/decompress.h Wed Mar 09 16:18:58 2011 +0000
41 @@ -8,6 +8,7 @@
42
43 #define STATIC
44 #define INIT __init
45 +#define INITDATA __initdata
46
47 static void(*__initdata error)(const char *);
48 #define set_error_fn(x) error = x;
49 diff -r d428fa67abaa -r 9eb9948904cd xen/common/unxz.c
50 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
51 +++ b/xen/common/unxz.c Wed Mar 09 16:18:58 2011 +0000
52 @@ -0,0 +1,306 @@
53 +/*
54 + * Wrapper for decompressing XZ-compressed kernel, initramfs, and initrd
55 + *
56 + * Author: Lasse Collin <lasse.collin@tukaani.org>
57 + *
58 + * This file has been put into the public domain.
59 + * You can do whatever you want with this file.
60 + */
61 +
62 +/*
63 + * Important notes about in-place decompression
64 + *
65 + * At least on x86, the kernel is decompressed in place: the compressed data
66 + * is placed to the end of the output buffer, and the decompressor overwrites
67 + * most of the compressed data. There must be enough safety margin to
68 + * guarantee that the write position is always behind the read position.
69 + *
70 + * The safety margin for XZ with LZMA2 or BCJ+LZMA2 is calculated below.
71 + * Note that the margin with XZ is bigger than with Deflate (gzip)!
72 + *
73 + * The worst case for in-place decompression is that the beginning of
74 + * the file is compressed extremely well, and the rest of the file is
75 + * uncompressible. Thus, we must look for worst-case expansion when the
76 + * compressor is encoding uncompressible data.
77 + *
78 + * The structure of the .xz file in case of a compresed kernel is as follows.
79 + * Sizes (as bytes) of the fields are in parenthesis.
80 + *
81 + * Stream Header (12)
82 + * Block Header:
83 + * Block Header (8-12)
84 + * Compressed Data (N)
85 + * Block Padding (0-3)
86 + * CRC32 (4)
87 + * Index (8-20)
88 + * Stream Footer (12)
89 + *
90 + * Normally there is exactly one Block, but let's assume that there are
91 + * 2-4 Blocks just in case. Because Stream Header and also Block Header
92 + * of the first Block don't make the decompressor produce any uncompressed
93 + * data, we can ignore them from our calculations. Block Headers of possible
94 + * additional Blocks have to be taken into account still. With these
95 + * assumptions, it is safe to assume that the total header overhead is
96 + * less than 128 bytes.
97 + *
98 + * Compressed Data contains LZMA2 or BCJ+LZMA2 encoded data. Since BCJ
99 + * doesn't change the size of the data, it is enough to calculate the
100 + * safety margin for LZMA2.
101 + *
102 + * LZMA2 stores the data in chunks. Each chunk has a header whose size is
103 + * a maximum of 6 bytes, but to get round 2^n numbers, let's assume that
104 + * the maximum chunk header size is 8 bytes. After the chunk header, there
105 + * may be up to 64 KiB of actual payload in the chunk. Often the payload is
106 + * quite a bit smaller though; to be safe, let's assume that an average
107 + * chunk has only 32 KiB of payload.
108 + *
109 + * The maximum uncompressed size of the payload is 2 MiB. The minimum
110 + * uncompressed size of the payload is in practice never less than the
111 + * payload size itself. The LZMA2 format would allow uncompressed size
112 + * to be less than the payload size, but no sane compressor creates such
113 + * files. LZMA2 supports storing uncompressible data in uncompressed form,
114 + * so there's never a need to create payloads whose uncompressed size is
115 + * smaller than the compressed size.
116 + *
117 + * The assumption, that the uncompressed size of the payload is never
118 + * smaller than the payload itself, is valid only when talking about
119 + * the payload as a whole. It is possible that the payload has parts where
120 + * the decompressor consumes more input than it produces output. Calculating
121 + * the worst case for this would be tricky. Instead of trying to do that,
122 + * let's simply make sure that the decompressor never overwrites any bytes
123 + * of the payload which it is currently reading.
124 + *
125 + * Now we have enough information to calculate the safety margin. We need
126 + * - 128 bytes for the .xz file format headers;
127 + * - 8 bytes per every 32 KiB of uncompressed size (one LZMA2 chunk header
128 + * per chunk, each chunk having average payload size of 32 KiB); and
129 + * - 64 KiB (biggest possible LZMA2 chunk payload size) to make sure that
130 + * the decompressor never overwrites anything from the LZMA2 chunk
131 + * payload it is currently reading.
132 + *
133 + * We get the following formula:
134 + *
135 + * safety_margin = 128 + uncompressed_size * 8 / 32768 + 65536
136 + * = 128 + (uncompressed_size >> 12) + 65536
137 + *
138 + * For comparision, according to arch/x86/boot/compressed/misc.c, the
139 + * equivalent formula for Deflate is this:
140 + *
141 + * safety_margin = 18 + (uncompressed_size >> 12) + 32768
142 + *
143 + * Thus, when updating Deflate-only in-place kernel decompressor to
144 + * support XZ, the fixed overhead has to be increased from 18+32768 bytes
145 + * to 128+65536 bytes.
146 + */
147 +
148 +#include "decompress.h"
149 +
150 +#define XZ_EXTERN STATIC
151 +
152 +/*
153 + * For boot time use, we enable only the BCJ filter of the current
154 + * architecture or none if no BCJ filter is available for the architecture.
155 + */
156 +#ifdef CONFIG_X86
157 +# define XZ_DEC_X86
158 +#endif
159 +#ifdef CONFIG_PPC
160 +# define XZ_DEC_POWERPC
161 +#endif
162 +#ifdef CONFIG_ARM
163 +# define XZ_DEC_ARM
164 +#endif
165 +#ifdef CONFIG_IA64
166 +# define XZ_DEC_IA64
167 +#endif
168 +#ifdef CONFIG_SPARC
169 +# define XZ_DEC_SPARC
170 +#endif
171 +
172 +/*
173 + * This will get the basic headers so that memeq() and others
174 + * can be defined.
175 + */
176 +#include "xz/private.h"
177 +
178 +/*
179 + * memeq and memzero are not used much and any remotely sane implementation
180 + * is fast enough. memcpy/memmove speed matters in multi-call mode, but
181 + * the kernel image is decompressed in single-call mode, in which only
182 + * memcpy speed can matter and only if there is a lot of uncompressible data
183 + * (LZMA2 stores uncompressible chunks in uncompressed form). Thus, the
184 + * functions below should just be kept small; it's probably not worth
185 + * optimizing for speed.
186 + */
187 +
188 +#ifndef memeq
189 +#define memeq(p1, p2, sz) (memcmp(p1, p2, sz) == 0)
190 +#endif
191 +
192 +#ifndef memzero
193 +#define memzero(p, sz) memset(p, 0, sz)
194 +#endif
195 +
196 +#include "xz/crc32.c"
197 +#include "xz/dec_stream.c"
198 +#include "xz/dec_lzma2.c"
199 +#include "xz/dec_bcj.c"
200 +
201 +/* Size of the input and output buffers in multi-call mode */
202 +#define XZ_IOBUF_SIZE 4096
203 +
204 +/*
205 + * This function implements the API defined in <linux/decompress/generic.h>.
206 + *
207 + * This wrapper will automatically choose single-call or multi-call mode
208 + * of the native XZ decoder API. The single-call mode can be used only when
209 + * both input and output buffers are available as a single chunk, i.e. when
210 + * fill() and flush() won't be used.
211 + */
212 +STATIC int INIT unxz(unsigned char *in, unsigned int in_size,
213 + int (*fill)(void *dest, unsigned int size),
214 + int (*flush)(void *src, unsigned int size),
215 + unsigned char *out, unsigned int *in_used,
216 + void (*error_fn)(const char *x))
217 +{
218 + struct xz_buf b;
219 + struct xz_dec *s;
220 + enum xz_ret ret;
221 + bool_t must_free_in = false;
222 +
223 + set_error_fn(error_fn);
224 +
225 + xz_crc32_init();
226 +
227 + if (in_used != NULL)
228 + *in_used = 0;
229 +
230 + if (fill == NULL && flush == NULL)
231 + s = xz_dec_init(XZ_SINGLE, 0);
232 + else
233 + s = xz_dec_init(XZ_DYNALLOC, (uint32_t)-1);
234 +
235 + if (s == NULL)
236 + goto error_alloc_state;
237 +
238 + if (flush == NULL) {
239 + b.out = out;
240 + b.out_size = (size_t)-1;
241 + } else {
242 + b.out_size = XZ_IOBUF_SIZE;
243 + b.out = malloc(XZ_IOBUF_SIZE);
244 + if (b.out == NULL)
245 + goto error_alloc_out;
246 + }
247 +
248 + if (in == NULL) {
249 + must_free_in = true;
250 + in = malloc(XZ_IOBUF_SIZE);
251 + if (in == NULL)
252 + goto error_alloc_in;
253 + }
254 +
255 + b.in = in;
256 + b.in_pos = 0;
257 + b.in_size = in_size;
258 + b.out_pos = 0;
259 +
260 + if (fill == NULL && flush == NULL) {
261 + ret = xz_dec_run(s, &b);
262 + } else {
263 + do {
264 + if (b.in_pos == b.in_size && fill != NULL) {
265 + if (in_used != NULL)
266 + *in_used += b.in_pos;
267 +
268 + b.in_pos = 0;
269 +
270 + in_size = fill(in, XZ_IOBUF_SIZE);
271 + if (in_size < 0) {
272 + /*
273 + * This isn't an optimal error code
274 + * but it probably isn't worth making
275 + * a new one either.
276 + */
277 + ret = XZ_BUF_ERROR;
278 + break;
279 + }
280 +
281 + b.in_size = in_size;
282 + }
283 +
284 + ret = xz_dec_run(s, &b);
285 +
286 + if (flush != NULL && (b.out_pos == b.out_size
287 + || (ret != XZ_OK && b.out_pos > 0))) {
288 + /*
289 + * Setting ret here may hide an error
290 + * returned by xz_dec_run(), but probably
291 + * it's not too bad.
292 + */
293 + if (flush(b.out, b.out_pos) != (int)b.out_pos)
294 + ret = XZ_BUF_ERROR;
295 +
296 + b.out_pos = 0;
297 + }
298 + } while (ret == XZ_OK);
299 +
300 + if (must_free_in)
301 + free(in);
302 +
303 + if (flush != NULL)
304 + free(b.out);
305 + }
306 +
307 + if (in_used != NULL)
308 + *in_used += b.in_pos;
309 +
310 + xz_dec_end(s);
311 +
312 + switch (ret) {
313 + case XZ_STREAM_END:
314 + return 0;
315 +
316 + case XZ_MEM_ERROR:
317 + /* This can occur only in multi-call mode. */
318 + error("XZ decompressor ran out of memory");
319 + break;
320 +
321 + case XZ_FORMAT_ERROR:
322 + error("Input is not in the XZ format (wrong magic bytes)");
323 + break;
324 +
325 + case XZ_OPTIONS_ERROR:
326 + error("Input was encoded with settings that are not "
327 + "supported by this XZ decoder");
328 + break;
329 +
330 + case XZ_DATA_ERROR:
331 + case XZ_BUF_ERROR:
332 + error("XZ-compressed data is corrupt");
333 + break;
334 +
335 + default:
336 + error("Bug in the XZ decompressor");
337 + break;
338 + }
339 +
340 + return -1;
341 +
342 +error_alloc_in:
343 + if (flush != NULL)
344 + free(b.out);
345 +
346 +error_alloc_out:
347 + xz_dec_end(s);
348 +
349 +error_alloc_state:
350 + error("XZ decompressor ran out of memory");
351 + return -1;
352 +}
353 +
354 +/*
355 + * This macro is used by architecture-specific files to decompress
356 + * the kernel image.
357 + */
358 +#define decompress unxz
359 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/crc32.c
360 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
361 +++ b/xen/common/xz/crc32.c Wed Mar 09 16:18:58 2011 +0000
362 @@ -0,0 +1,51 @@
363 +/*
364 + * CRC32 using the polynomial from IEEE-802.3
365 + *
366 + * Authors: Lasse Collin <lasse.collin@tukaani.org>
367 + * Igor Pavlov <http://7-zip.org/>
368 + *
369 + * This file has been put into the public domain.
370 + * You can do whatever you want with this file.
371 + */
372 +
373 +/*
374 + * This is not the fastest implementation, but it is pretty compact.
375 + * The fastest versions of xz_crc32() on modern CPUs without hardware
376 + * accelerated CRC instruction are 3-5 times as fast as this version,
377 + * but they are bigger and use more memory for the lookup table.
378 + */
379 +
380 +#include "private.h"
381 +
382 +XZ_EXTERN uint32_t INITDATA xz_crc32_table[256];
383 +
384 +XZ_EXTERN void INIT xz_crc32_init(void)
385 +{
386 + const uint32_t poly = 0xEDB88320;
387 +
388 + uint32_t i;
389 + uint32_t j;
390 + uint32_t r;
391 +
392 + for (i = 0; i < 256; ++i) {
393 + r = i;
394 + for (j = 0; j < 8; ++j)
395 + r = (r >> 1) ^ (poly & ~((r & 1) - 1));
396 +
397 + xz_crc32_table[i] = r;
398 + }
399 +
400 + return;
401 +}
402 +
403 +XZ_EXTERN uint32_t INIT xz_crc32(const uint8_t *buf, size_t size, uint32_t crc)
404 +{
405 + crc = ~crc;
406 +
407 + while (size != 0) {
408 + crc = xz_crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
409 + --size;
410 + }
411 +
412 + return ~crc;
413 +}
414 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/dec_bcj.c
415 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
416 +++ b/xen/common/xz/dec_bcj.c Wed Mar 09 16:18:58 2011 +0000
417 @@ -0,0 +1,562 @@
418 +/*
419 + * Branch/Call/Jump (BCJ) filter decoders
420 + *
421 + * Authors: Lasse Collin <lasse.collin@tukaani.org>
422 + * Igor Pavlov <http://7-zip.org/>
423 + *
424 + * This file has been put into the public domain.
425 + * You can do whatever you want with this file.
426 + */
427 +
428 +#include "private.h"
429 +
430 +/*
431 + * The rest of the file is inside this ifdef. It makes things a little more
432 + * convenient when building without support for any BCJ filters.
433 + */
434 +#ifdef XZ_DEC_BCJ
435 +
436 +struct xz_dec_bcj {
437 + /* Type of the BCJ filter being used */
438 + enum {
439 + BCJ_X86 = 4, /* x86 or x86-64 */
440 + BCJ_POWERPC = 5, /* Big endian only */
441 + BCJ_IA64 = 6, /* Big or little endian */
442 + BCJ_ARM = 7, /* Little endian only */
443 + BCJ_ARMTHUMB = 8, /* Little endian only */
444 + BCJ_SPARC = 9 /* Big or little endian */
445 + } type;
446 +
447 + /*
448 + * Return value of the next filter in the chain. We need to preserve
449 + * this information across calls, because we must not call the next
450 + * filter anymore once it has returned XZ_STREAM_END.
451 + */
452 + enum xz_ret ret;
453 +
454 + /* True if we are operating in single-call mode. */
455 + bool_t single_call;
456 +
457 + /*
458 + * Absolute position relative to the beginning of the uncompressed
459 + * data (in a single .xz Block). We care only about the lowest 32
460 + * bits so this doesn't need to be uint64_t even with big files.
461 + */
462 + uint32_t pos;
463 +
464 + /* x86 filter state */
465 + uint32_t x86_prev_mask;
466 +
467 + /* Temporary space to hold the variables from struct xz_buf */
468 + uint8_t *out;
469 + size_t out_pos;
470 + size_t out_size;
471 +
472 + struct {
473 + /* Amount of already filtered data in the beginning of buf */
474 + size_t filtered;
475 +
476 + /* Total amount of data currently stored in buf */
477 + size_t size;
478 +
479 + /*
480 + * Buffer to hold a mix of filtered and unfiltered data. This
481 + * needs to be big enough to hold Alignment + 2 * Look-ahead:
482 + *
483 + * Type Alignment Look-ahead
484 + * x86 1 4
485 + * PowerPC 4 0
486 + * IA-64 16 0
487 + * ARM 4 0
488 + * ARM-Thumb 2 2
489 + * SPARC 4 0
490 + */
491 + uint8_t buf[16];
492 + } temp;
493 +};
494 +
495 +#ifdef XZ_DEC_X86
496 +/*
497 + * This is used to test the most significant byte of a memory address
498 + * in an x86 instruction.
499 + */
500 +static inline int INIT bcj_x86_test_msbyte(uint8_t b)
501 +{
502 + return b == 0x00 || b == 0xFF;
503 +}
504 +
505 +static size_t INIT bcj_x86(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
506 +{
507 + static /*const*/ bool_t INITDATA mask_to_allowed_status[8]
508 + = { true, true, true, false, true, false, false, false };
509 +
510 + static /*const*/ uint8_t INITDATA mask_to_bit_num[8]
511 + = { 0, 1, 2, 2, 3, 3, 3, 3 };
512 +
513 + size_t i;
514 + size_t prev_pos = (size_t)-1;
515 + uint32_t prev_mask = s->x86_prev_mask;
516 + uint32_t src;
517 + uint32_t dest;
518 + uint32_t j;
519 + uint8_t b;
520 +
521 + if (size <= 4)
522 + return 0;
523 +
524 + size -= 4;
525 + for (i = 0; i < size; ++i) {
526 + if ((buf[i] & 0xFE) != 0xE8)
527 + continue;
528 +
529 + prev_pos = i - prev_pos;
530 + if (prev_pos > 3) {
531 + prev_mask = 0;
532 + } else {
533 + prev_mask = (prev_mask << (prev_pos - 1)) & 7;
534 + if (prev_mask != 0) {
535 + b = buf[i + 4 - mask_to_bit_num[prev_mask]];
536 + if (!mask_to_allowed_status[prev_mask]
537 + || bcj_x86_test_msbyte(b)) {
538 + prev_pos = i;
539 + prev_mask = (prev_mask << 1) | 1;
540 + continue;
541 + }
542 + }
543 + }
544 +
545 + prev_pos = i;
546 +
547 + if (bcj_x86_test_msbyte(buf[i + 4])) {
548 + src = get_unaligned_le32(buf + i + 1);
549 + while (true) {
550 + dest = src - (s->pos + (uint32_t)i + 5);
551 + if (prev_mask == 0)
552 + break;
553 +
554 + j = mask_to_bit_num[prev_mask] * 8;
555 + b = (uint8_t)(dest >> (24 - j));
556 + if (!bcj_x86_test_msbyte(b))
557 + break;
558 +
559 + src = dest ^ (((uint32_t)1 << (32 - j)) - 1);
560 + }
561 +
562 + dest &= 0x01FFFFFF;
563 + dest |= (uint32_t)0 - (dest & 0x01000000);
564 + put_unaligned_le32(dest, buf + i + 1);
565 + i += 4;
566 + } else {
567 + prev_mask = (prev_mask << 1) | 1;
568 + }
569 + }
570 +
571 + prev_pos = i - prev_pos;
572 + s->x86_prev_mask = prev_pos > 3 ? 0 : prev_mask << (prev_pos - 1);
573 + return i;
574 +}
575 +#endif
576 +
577 +#ifdef XZ_DEC_POWERPC
578 +static size_t INIT bcj_powerpc(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
579 +{
580 + size_t i;
581 + uint32_t instr;
582 +
583 + for (i = 0; i + 4 <= size; i += 4) {
584 + instr = get_unaligned_be32(buf + i);
585 + if ((instr & 0xFC000003) == 0x48000001) {
586 + instr &= 0x03FFFFFC;
587 + instr -= s->pos + (uint32_t)i;
588 + instr &= 0x03FFFFFC;
589 + instr |= 0x48000001;
590 + put_unaligned_be32(instr, buf + i);
591 + }
592 + }
593 +
594 + return i;
595 +}
596 +#endif
597 +
598 +#ifdef XZ_DEC_IA64
599 +static size_t INIT bcj_ia64(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
600 +{
601 + static const uint8_t branch_table[32] = {
602 + 0, 0, 0, 0, 0, 0, 0, 0,
603 + 0, 0, 0, 0, 0, 0, 0, 0,
604 + 4, 4, 6, 6, 0, 0, 7, 7,
605 + 4, 4, 0, 0, 4, 4, 0, 0
606 + };
607 +
608 + /*
609 + * The local variables take a little bit stack space, but it's less
610 + * than what LZMA2 decoder takes, so it doesn't make sense to reduce
611 + * stack usage here without doing that for the LZMA2 decoder too.
612 + */
613 +
614 + /* Loop counters */
615 + size_t i;
616 + size_t j;
617 +
618 + /* Instruction slot (0, 1, or 2) in the 128-bit instruction word */
619 + uint32_t slot;
620 +
621 + /* Bitwise offset of the instruction indicated by slot */
622 + uint32_t bit_pos;
623 +
624 + /* bit_pos split into byte and bit parts */
625 + uint32_t byte_pos;
626 + uint32_t bit_res;
627 +
628 + /* Address part of an instruction */
629 + uint32_t addr;
630 +
631 + /* Mask used to detect which instructions to convert */
632 + uint32_t mask;
633 +
634 + /* 41-bit instruction stored somewhere in the lowest 48 bits */
635 + uint64_t instr;
636 +
637 + /* Instruction normalized with bit_res for easier manipulation */
638 + uint64_t norm;
639 +
640 + for (i = 0; i + 16 <= size; i += 16) {
641 + mask = branch_table[buf[i] & 0x1F];
642 + for (slot = 0, bit_pos = 5; slot < 3; ++slot, bit_pos += 41) {
643 + if (((mask >> slot) & 1) == 0)
644 + continue;
645 +
646 + byte_pos = bit_pos >> 3;
647 + bit_res = bit_pos & 7;
648 + instr = 0;
649 + for (j = 0; j < 6; ++j)
650 + instr |= (uint64_t)(buf[i + j + byte_pos])
651 + << (8 * j);
652 +
653 + norm = instr >> bit_res;
654 +
655 + if (((norm >> 37) & 0x0F) == 0x05
656 + && ((norm >> 9) & 0x07) == 0) {
657 + addr = (norm >> 13) & 0x0FFFFF;
658 + addr |= ((uint32_t)(norm >> 36) & 1) << 20;
659 + addr <<= 4;
660 + addr -= s->pos + (uint32_t)i;
661 + addr >>= 4;
662 +
663 + norm &= ~((uint64_t)0x8FFFFF << 13);
664 + norm |= (uint64_t)(addr & 0x0FFFFF) << 13;
665 + norm |= (uint64_t)(addr & 0x100000)
666 + << (36 - 20);
667 +
668 + instr &= (1 << bit_res) - 1;
669 + instr |= norm << bit_res;
670 +
671 + for (j = 0; j < 6; j++)
672 + buf[i + j + byte_pos]
673 + = (uint8_t)(instr >> (8 * j));
674 + }
675 + }
676 + }
677 +
678 + return i;
679 +}
680 +#endif
681 +
682 +#ifdef XZ_DEC_ARM
683 +static size_t INIT bcj_arm(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
684 +{
685 + size_t i;
686 + uint32_t addr;
687 +
688 + for (i = 0; i + 4 <= size; i += 4) {
689 + if (buf[i + 3] == 0xEB) {
690 + addr = (uint32_t)buf[i] | ((uint32_t)buf[i + 1] << 8)
691 + | ((uint32_t)buf[i + 2] << 16);
692 + addr <<= 2;
693 + addr -= s->pos + (uint32_t)i + 8;
694 + addr >>= 2;
695 + buf[i] = (uint8_t)addr;
696 + buf[i + 1] = (uint8_t)(addr >> 8);
697 + buf[i + 2] = (uint8_t)(addr >> 16);
698 + }
699 + }
700 +
701 + return i;
702 +}
703 +#endif
704 +
705 +#ifdef XZ_DEC_ARMTHUMB
706 +static size_t INIT bcj_armthumb(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
707 +{
708 + size_t i;
709 + uint32_t addr;
710 +
711 + for (i = 0; i + 4 <= size; i += 2) {
712 + if ((buf[i + 1] & 0xF8) == 0xF0
713 + && (buf[i + 3] & 0xF8) == 0xF8) {
714 + addr = (((uint32_t)buf[i + 1] & 0x07) << 19)
715 + | ((uint32_t)buf[i] << 11)
716 + | (((uint32_t)buf[i + 3] & 0x07) << 8)
717 + | (uint32_t)buf[i + 2];
718 + addr <<= 1;
719 + addr -= s->pos + (uint32_t)i + 4;
720 + addr >>= 1;
721 + buf[i + 1] = (uint8_t)(0xF0 | ((addr >> 19) & 0x07));
722 + buf[i] = (uint8_t)(addr >> 11);
723 + buf[i + 3] = (uint8_t)(0xF8 | ((addr >> 8) & 0x07));
724 + buf[i + 2] = (uint8_t)addr;
725 + i += 2;
726 + }
727 + }
728 +
729 + return i;
730 +}
731 +#endif
732 +
733 +#ifdef XZ_DEC_SPARC
734 +static size_t INIT bcj_sparc(struct xz_dec_bcj *s, uint8_t *buf, size_t size)
735 +{
736 + size_t i;
737 + uint32_t instr;
738 +
739 + for (i = 0; i + 4 <= size; i += 4) {
740 + instr = get_unaligned_be32(buf + i);
741 + if ((instr >> 22) == 0x100 || (instr >> 22) == 0x1FF) {
742 + instr <<= 2;
743 + instr -= s->pos + (uint32_t)i;
744 + instr >>= 2;
745 + instr = ((uint32_t)0x40000000 - (instr & 0x400000))
746 + | 0x40000000 | (instr & 0x3FFFFF);
747 + put_unaligned_be32(instr, buf + i);
748 + }
749 + }
750 +
751 + return i;
752 +}
753 +#endif
754 +
755 +/*
756 + * Apply the selected BCJ filter. Update *pos and s->pos to match the amount
757 + * of data that got filtered.
758 + *
759 + * NOTE: This is implemented as a switch statement to avoid using function
760 + * pointers, which could be problematic in the kernel boot code, which must
761 + * avoid pointers to static data (at least on x86).
762 + */
763 +static void INIT bcj_apply(struct xz_dec_bcj *s,
764 + uint8_t *buf, size_t *pos, size_t size)
765 +{
766 + size_t filtered;
767 +
768 + buf += *pos;
769 + size -= *pos;
770 +
771 + switch (s->type) {
772 +#ifdef XZ_DEC_X86
773 + case BCJ_X86:
774 + filtered = bcj_x86(s, buf, size);
775 + break;
776 +#endif
777 +#ifdef XZ_DEC_POWERPC
778 + case BCJ_POWERPC:
779 + filtered = bcj_powerpc(s, buf, size);
780 + break;
781 +#endif
782 +#ifdef XZ_DEC_IA64
783 + case BCJ_IA64:
784 + filtered = bcj_ia64(s, buf, size);
785 + break;
786 +#endif
787 +#ifdef XZ_DEC_ARM
788 + case BCJ_ARM:
789 + filtered = bcj_arm(s, buf, size);
790 + break;
791 +#endif
792 +#ifdef XZ_DEC_ARMTHUMB
793 + case BCJ_ARMTHUMB:
794 + filtered = bcj_armthumb(s, buf, size);
795 + break;
796 +#endif
797 +#ifdef XZ_DEC_SPARC
798 + case BCJ_SPARC:
799 + filtered = bcj_sparc(s, buf, size);
800 + break;
801 +#endif
802 + default:
803 + /* Never reached but silence compiler warnings. */
804 + filtered = 0;
805 + break;
806 + }
807 +
808 + *pos += filtered;
809 + s->pos += filtered;
810 +}
811 +
812 +/*
813 + * Flush pending filtered data from temp to the output buffer.
814 + * Move the remaining mixture of possibly filtered and unfiltered
815 + * data to the beginning of temp.
816 + */
817 +static void INIT bcj_flush(struct xz_dec_bcj *s, struct xz_buf *b)
818 +{
819 + size_t copy_size;
820 +
821 + copy_size = min_t(size_t, s->temp.filtered, b->out_size - b->out_pos);
822 + memcpy(b->out + b->out_pos, s->temp.buf, copy_size);
823 + b->out_pos += copy_size;
824 +
825 + s->temp.filtered -= copy_size;
826 + s->temp.size -= copy_size;
827 + memmove(s->temp.buf, s->temp.buf + copy_size, s->temp.size);
828 +}
829 +
830 +/*
831 + * The BCJ filter functions are primitive in sense that they process the
832 + * data in chunks of 1-16 bytes. To hide this issue, this function does
833 + * some buffering.
834 + */
835 +XZ_EXTERN enum xz_ret INIT xz_dec_bcj_run(struct xz_dec_bcj *s,
836 + struct xz_dec_lzma2 *lzma2,
837 + struct xz_buf *b)
838 +{
839 + size_t out_start;
840 +
841 + /*
842 + * Flush pending already filtered data to the output buffer. Return
843 + * immediatelly if we couldn't flush everything, or if the next
844 + * filter in the chain had already returned XZ_STREAM_END.
845 + */
846 + if (s->temp.filtered > 0) {
847 + bcj_flush(s, b);
848 + if (s->temp.filtered > 0)
849 + return XZ_OK;
850 +
851 + if (s->ret == XZ_STREAM_END)
852 + return XZ_STREAM_END;
853 + }
854 +
855 + /*
856 + * If we have more output space than what is currently pending in
857 + * temp, copy the unfiltered data from temp to the output buffer
858 + * and try to fill the output buffer by decoding more data from the
859 + * next filter in the chain. Apply the BCJ filter on the new data
860 + * in the output buffer. If everything cannot be filtered, copy it
861 + * to temp and rewind the output buffer position accordingly.
862 + */
863 + if (s->temp.size < b->out_size - b->out_pos) {
864 + out_start = b->out_pos;
865 + memcpy(b->out + b->out_pos, s->temp.buf, s->temp.size);
866 + b->out_pos += s->temp.size;
867 +
868 + s->ret = xz_dec_lzma2_run(lzma2, b);
869 + if (s->ret != XZ_STREAM_END
870 + && (s->ret != XZ_OK || s->single_call))
871 + return s->ret;
872 +
873 + bcj_apply(s, b->out, &out_start, b->out_pos);
874 +
875 + /*
876 + * As an exception, if the next filter returned XZ_STREAM_END,
877 + * we can do that too, since the last few bytes that remain
878 + * unfiltered are meant to remain unfiltered.
879 + */
880 + if (s->ret == XZ_STREAM_END)
881 + return XZ_STREAM_END;
882 +
883 + s->temp.size = b->out_pos - out_start;
884 + b->out_pos -= s->temp.size;
885 + memcpy(s->temp.buf, b->out + b->out_pos, s->temp.size);
886 + }
887 +
888 + /*
889 + * If we have unfiltered data in temp, try to fill by decoding more
890 + * data from the next filter. Apply the BCJ filter on temp. Then we
891 + * hopefully can fill the actual output buffer by copying filtered
892 + * data from temp. A mix of filtered and unfiltered data may be left
893 + * in temp; it will be taken care on the next call to this function.
894 + */
895 + if (s->temp.size > 0) {
896 + /* Make b->out{,_pos,_size} temporarily point to s->temp. */
897 + s->out = b->out;
898 + s->out_pos = b->out_pos;
899 + s->out_size = b->out_size;
900 + b->out = s->temp.buf;
901 + b->out_pos = s->temp.size;
902 + b->out_size = sizeof(s->temp.buf);
903 +
904 + s->ret = xz_dec_lzma2_run(lzma2, b);
905 +
906 + s->temp.size = b->out_pos;
907 + b->out = s->out;
908 + b->out_pos = s->out_pos;
909 + b->out_size = s->out_size;
910 +
911 + if (s->ret != XZ_OK && s->ret != XZ_STREAM_END)
912 + return s->ret;
913 +
914 + bcj_apply(s, s->temp.buf, &s->temp.filtered, s->temp.size);
915 +
916 + /*
917 + * If the next filter returned XZ_STREAM_END, we mark that
918 + * everything is filtered, since the last unfiltered bytes
919 + * of the stream are meant to be left as is.
920 + */
921 + if (s->ret == XZ_STREAM_END)
922 + s->temp.filtered = s->temp.size;
923 +
924 + bcj_flush(s, b);
925 + if (s->temp.filtered > 0)
926 + return XZ_OK;
927 + }
928 +
929 + return s->ret;
930 +}
931 +
932 +XZ_EXTERN struct xz_dec_bcj *INIT xz_dec_bcj_create(bool_t single_call)
933 +{
934 + struct xz_dec_bcj *s = malloc(sizeof(*s));
935 + if (s != NULL)
936 + s->single_call = single_call;
937 +
938 + return s;
939 +}
940 +
941 +XZ_EXTERN enum xz_ret INIT xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id)
942 +{
943 + switch (id) {
944 +#ifdef XZ_DEC_X86
945 + case BCJ_X86:
946 +#endif
947 +#ifdef XZ_DEC_POWERPC
948 + case BCJ_POWERPC:
949 +#endif
950 +#ifdef XZ_DEC_IA64
951 + case BCJ_IA64:
952 +#endif
953 +#ifdef XZ_DEC_ARM
954 + case BCJ_ARM:
955 +#endif
956 +#ifdef XZ_DEC_ARMTHUMB
957 + case BCJ_ARMTHUMB:
958 +#endif
959 +#ifdef XZ_DEC_SPARC
960 + case BCJ_SPARC:
961 +#endif
962 + break;
963 +
964 + default:
965 + /* Unsupported Filter ID */
966 + return XZ_OPTIONS_ERROR;
967 + }
968 +
969 + s->type = id;
970 + s->ret = XZ_OK;
971 + s->pos = 0;
972 + s->x86_prev_mask = 0;
973 + s->temp.filtered = 0;
974 + s->temp.size = 0;
975 +
976 + return XZ_OK;
977 +}
978 +
979 +#endif
980 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/dec_lzma2.c
981 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
982 +++ b/xen/common/xz/dec_lzma2.c Wed Mar 09 16:18:58 2011 +0000
983 @@ -0,0 +1,1171 @@
984 +/*
985 + * LZMA2 decoder
986 + *
987 + * Authors: Lasse Collin <lasse.collin@tukaani.org>
988 + * Igor Pavlov <http://7-zip.org/>
989 + *
990 + * This file has been put into the public domain.
991 + * You can do whatever you want with this file.
992 + */
993 +
994 +#include "private.h"
995 +#include "lzma2.h"
996 +
997 +/*
998 + * Range decoder initialization eats the first five bytes of each LZMA chunk.
999 + */
1000 +#define RC_INIT_BYTES 5
1001 +
1002 +/*
1003 + * Minimum number of usable input buffer to safely decode one LZMA symbol.
1004 + * The worst case is that we decode 22 bits using probabilities and 26
1005 + * direct bits. This may decode at maximum of 20 bytes of input. However,
1006 + * lzma_main() does an extra normalization before returning, thus we
1007 + * need to put 21 here.
1008 + */
1009 +#define LZMA_IN_REQUIRED 21
1010 +
1011 +/*
1012 + * Dictionary (history buffer)
1013 + *
1014 + * These are always true:
1015 + * start <= pos <= full <= end
1016 + * pos <= limit <= end
1017 + *
1018 + * In multi-call mode, also these are true:
1019 + * end == size
1020 + * size <= size_max
1021 + * allocated <= size
1022 + *
1023 + * Most of these variables are size_t to support single-call mode,
1024 + * in which the dictionary variables address the actual output
1025 + * buffer directly.
1026 + */
1027 +struct dictionary {
1028 + /* Beginning of the history buffer */
1029 + uint8_t *buf;
1030 +
1031 + /* Old position in buf (before decoding more data) */
1032 + size_t start;
1033 +
1034 + /* Position in buf */
1035 + size_t pos;
1036 +
1037 + /*
1038 + * How full dictionary is. This is used to detect corrupt input that
1039 + * would read beyond the beginning of the uncompressed stream.
1040 + */
1041 + size_t full;
1042 +
1043 + /* Write limit; we don't write to buf[limit] or later bytes. */
1044 + size_t limit;
1045 +
1046 + /*
1047 + * End of the dictionary buffer. In multi-call mode, this is
1048 + * the same as the dictionary size. In single-call mode, this
1049 + * indicates the size of the output buffer.
1050 + */
1051 + size_t end;
1052 +
1053 + /*
1054 + * Size of the dictionary as specified in Block Header. This is used
1055 + * together with "full" to detect corrupt input that would make us
1056 + * read beyond the beginning of the uncompressed stream.
1057 + */
1058 + uint32_t size;
1059 +
1060 + /*
1061 + * Maximum allowed dictionary size in multi-call mode.
1062 + * This is ignored in single-call mode.
1063 + */
1064 + uint32_t size_max;
1065 +
1066 + /*
1067 + * Amount of memory currently allocated for the dictionary.
1068 + * This is used only with XZ_DYNALLOC. (With XZ_PREALLOC,
1069 + * size_max is always the same as the allocated size.)
1070 + */
1071 + uint32_t allocated;
1072 +
1073 + /* Operation mode */
1074 + enum xz_mode mode;
1075 +};
1076 +
1077 +/* Range decoder */
1078 +struct rc_dec {
1079 + uint32_t range;
1080 + uint32_t code;
1081 +
1082 + /*
1083 + * Number of initializing bytes remaining to be read
1084 + * by rc_read_init().
1085 + */
1086 + uint32_t init_bytes_left;
1087 +
1088 + /*
1089 + * Buffer from which we read our input. It can be either
1090 + * temp.buf or the caller-provided input buffer.
1091 + */
1092 + const uint8_t *in;
1093 + size_t in_pos;
1094 + size_t in_limit;
1095 +};
1096 +
1097 +/* Probabilities for a length decoder. */
1098 +struct lzma_len_dec {
1099 + /* Probability of match length being at least 10 */
1100 + uint16_t choice;
1101 +
1102 + /* Probability of match length being at least 18 */
1103 + uint16_t choice2;
1104 +
1105 + /* Probabilities for match lengths 2-9 */
1106 + uint16_t low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
1107 +
1108 + /* Probabilities for match lengths 10-17 */
1109 + uint16_t mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
1110 +
1111 + /* Probabilities for match lengths 18-273 */
1112 + uint16_t high[LEN_HIGH_SYMBOLS];
1113 +};
1114 +
1115 +struct lzma_dec {
1116 + /* Distances of latest four matches */
1117 + uint32_t rep0;
1118 + uint32_t rep1;
1119 + uint32_t rep2;
1120 + uint32_t rep3;
1121 +
1122 + /* Types of the most recently seen LZMA symbols */
1123 + enum lzma_state state;
1124 +
1125 + /*
1126 + * Length of a match. This is updated so that dict_repeat can
1127 + * be called again to finish repeating the whole match.
1128 + */
1129 + uint32_t len;
1130 +
1131 + /*
1132 + * LZMA properties or related bit masks (number of literal
1133 + * context bits, a mask dervied from the number of literal
1134 + * position bits, and a mask dervied from the number
1135 + * position bits)
1136 + */
1137 + uint32_t lc;
1138 + uint32_t literal_pos_mask; /* (1 << lp) - 1 */
1139 + uint32_t pos_mask; /* (1 << pb) - 1 */
1140 +
1141 + /* If 1, it's a match. Otherwise it's a single 8-bit literal. */
1142 + uint16_t is_match[STATES][POS_STATES_MAX];
1143 +
1144 + /* If 1, it's a repeated match. The distance is one of rep0 .. rep3. */
1145 + uint16_t is_rep[STATES];
1146 +
1147 + /*
1148 + * If 0, distance of a repeated match is rep0.
1149 + * Otherwise check is_rep1.
1150 + */
1151 + uint16_t is_rep0[STATES];
1152 +
1153 + /*
1154 + * If 0, distance of a repeated match is rep1.
1155 + * Otherwise check is_rep2.
1156 + */
1157 + uint16_t is_rep1[STATES];
1158 +
1159 + /* If 0, distance of a repeated match is rep2. Otherwise it is rep3. */
1160 + uint16_t is_rep2[STATES];
1161 +
1162 + /*
1163 + * If 1, the repeated match has length of one byte. Otherwise
1164 + * the length is decoded from rep_len_decoder.
1165 + */
1166 + uint16_t is_rep0_long[STATES][POS_STATES_MAX];
1167 +
1168 + /*
1169 + * Probability tree for the highest two bits of the match
1170 + * distance. There is a separate probability tree for match
1171 + * lengths of 2 (i.e. MATCH_LEN_MIN), 3, 4, and [5, 273].
1172 + */
1173 + uint16_t dist_slot[DIST_STATES][DIST_SLOTS];
1174 +
1175 + /*
1176 + * Probility trees for additional bits for match distance
1177 + * when the distance is in the range [4, 127].
1178 + */
1179 + uint16_t dist_special[FULL_DISTANCES - DIST_MODEL_END];
1180 +
1181 + /*
1182 + * Probability tree for the lowest four bits of a match
1183 + * distance that is equal to or greater than 128.
1184 + */
1185 + uint16_t dist_align[ALIGN_SIZE];
1186 +
1187 + /* Length of a normal match */
1188 + struct lzma_len_dec match_len_dec;
1189 +
1190 + /* Length of a repeated match */
1191 + struct lzma_len_dec rep_len_dec;
1192 +
1193 + /* Probabilities of literals */
1194 + uint16_t literal[LITERAL_CODERS_MAX][LITERAL_CODER_SIZE];
1195 +};
1196 +
1197 +struct lzma2_dec {
1198 + /* Position in xz_dec_lzma2_run(). */
1199 + enum lzma2_seq {
1200 + SEQ_CONTROL,
1201 + SEQ_UNCOMPRESSED_1,
1202 + SEQ_UNCOMPRESSED_2,
1203 + SEQ_COMPRESSED_0,
1204 + SEQ_COMPRESSED_1,
1205 + SEQ_PROPERTIES,
1206 + SEQ_LZMA_PREPARE,
1207 + SEQ_LZMA_RUN,
1208 + SEQ_COPY
1209 + } sequence;
1210 +
1211 + /* Next position after decoding the compressed size of the chunk. */
1212 + enum lzma2_seq next_sequence;
1213 +
1214 + /* Uncompressed size of LZMA chunk (2 MiB at maximum) */
1215 + uint32_t uncompressed;
1216 +
1217 + /*
1218 + * Compressed size of LZMA chunk or compressed/uncompressed
1219 + * size of uncompressed chunk (64 KiB at maximum)
1220 + */
1221 + uint32_t compressed;
1222 +
1223 + /*
1224 + * True if dictionary reset is needed. This is false before
1225 + * the first chunk (LZMA or uncompressed).
1226 + */
1227 + bool_t need_dict_reset;
1228 +
1229 + /*
1230 + * True if new LZMA properties are needed. This is false
1231 + * before the first LZMA chunk.
1232 + */
1233 + bool_t need_props;
1234 +};
1235 +
1236 +struct xz_dec_lzma2 {
1237 + /*
1238 + * The order below is important on x86 to reduce code size and
1239 + * it shouldn't hurt on other platforms. Everything up to and
1240 + * including lzma.pos_mask are in the first 128 bytes on x86-32,
1241 + * which allows using smaller instructions to access those
1242 + * variables. On x86-64, fewer variables fit into the first 128
1243 + * bytes, but this is still the best order without sacrificing
1244 + * the readability by splitting the structures.
1245 + */
1246 + struct rc_dec rc;
1247 + struct dictionary dict;
1248 + struct lzma2_dec lzma2;
1249 + struct lzma_dec lzma;
1250 +
1251 + /*
1252 + * Temporary buffer which holds small number of input bytes between
1253 + * decoder calls. See lzma2_lzma() for details.
1254 + */
1255 + struct {
1256 + uint32_t size;
1257 + uint8_t buf[3 * LZMA_IN_REQUIRED];
1258 + } temp;
1259 +};
1260 +
1261 +/**************
1262 + * Dictionary *
1263 + **************/
1264 +
1265 +/*
1266 + * Reset the dictionary state. When in single-call mode, set up the beginning
1267 + * of the dictionary to point to the actual output buffer.
1268 + */
1269 +static void INIT dict_reset(struct dictionary *dict, struct xz_buf *b)
1270 +{
1271 + if (DEC_IS_SINGLE(dict->mode)) {
1272 + dict->buf = b->out + b->out_pos;
1273 + dict->end = b->out_size - b->out_pos;
1274 + }
1275 +
1276 + dict->start = 0;
1277 + dict->pos = 0;
1278 + dict->limit = 0;
1279 + dict->full = 0;
1280 +}
1281 +
1282 +/* Set dictionary write limit */
1283 +static void INIT dict_limit(struct dictionary *dict, size_t out_max)
1284 +{
1285 + if (dict->end - dict->pos <= out_max)
1286 + dict->limit = dict->end;
1287 + else
1288 + dict->limit = dict->pos + out_max;
1289 +}
1290 +
1291 +/* Return true if at least one byte can be written into the dictionary. */
1292 +static inline bool_t INIT dict_has_space(const struct dictionary *dict)
1293 +{
1294 + return dict->pos < dict->limit;
1295 +}
1296 +
1297 +/*
1298 + * Get a byte from the dictionary at the given distance. The distance is
1299 + * assumed to valid, or as a special case, zero when the dictionary is
1300 + * still empty. This special case is needed for single-call decoding to
1301 + * avoid writing a '\0' to the end of the destination buffer.
1302 + */
1303 +static inline uint32_t INIT dict_get(const struct dictionary *dict, uint32_t dist)
1304 +{
1305 + size_t offset = dict->pos - dist - 1;
1306 +
1307 + if (dist >= dict->pos)
1308 + offset += dict->end;
1309 +
1310 + return dict->full > 0 ? dict->buf[offset] : 0;
1311 +}
1312 +
1313 +/*
1314 + * Put one byte into the dictionary. It is assumed that there is space for it.
1315 + */
1316 +static inline void INIT dict_put(struct dictionary *dict, uint8_t byte)
1317 +{
1318 + dict->buf[dict->pos++] = byte;
1319 +
1320 + if (dict->full < dict->pos)
1321 + dict->full = dict->pos;
1322 +}
1323 +
1324 +/*
1325 + * Repeat given number of bytes from the given distance. If the distance is
1326 + * invalid, false is returned. On success, true is returned and *len is
1327 + * updated to indicate how many bytes were left to be repeated.
1328 + */
1329 +static bool_t INIT dict_repeat(struct dictionary *dict, uint32_t *len, uint32_t dist)
1330 +{
1331 + size_t back;
1332 + uint32_t left;
1333 +
1334 + if (dist >= dict->full || dist >= dict->size)
1335 + return false;
1336 +
1337 + left = min_t(size_t, dict->limit - dict->pos, *len);
1338 + *len -= left;
1339 +
1340 + back = dict->pos - dist - 1;
1341 + if (dist >= dict->pos)
1342 + back += dict->end;
1343 +
1344 + do {
1345 + dict->buf[dict->pos++] = dict->buf[back++];
1346 + if (back == dict->end)
1347 + back = 0;
1348 + } while (--left > 0);
1349 +
1350 + if (dict->full < dict->pos)
1351 + dict->full = dict->pos;
1352 +
1353 + return true;
1354 +}
1355 +
1356 +/* Copy uncompressed data as is from input to dictionary and output buffers. */
1357 +static void INIT dict_uncompressed(struct dictionary *dict, struct xz_buf *b,
1358 + uint32_t *left)
1359 +{
1360 + size_t copy_size;
1361 +
1362 + while (*left > 0 && b->in_pos < b->in_size
1363 + && b->out_pos < b->out_size) {
1364 + copy_size = min(b->in_size - b->in_pos,
1365 + b->out_size - b->out_pos);
1366 + if (copy_size > dict->end - dict->pos)
1367 + copy_size = dict->end - dict->pos;
1368 + if (copy_size > *left)
1369 + copy_size = *left;
1370 +
1371 + *left -= copy_size;
1372 +
1373 + memcpy(dict->buf + dict->pos, b->in + b->in_pos, copy_size);
1374 + dict->pos += copy_size;
1375 +
1376 + if (dict->full < dict->pos)
1377 + dict->full = dict->pos;
1378 +
1379 + if (DEC_IS_MULTI(dict->mode)) {
1380 + if (dict->pos == dict->end)
1381 + dict->pos = 0;
1382 +
1383 + memcpy(b->out + b->out_pos, b->in + b->in_pos,
1384 + copy_size);
1385 + }
1386 +
1387 + dict->start = dict->pos;
1388 +
1389 + b->out_pos += copy_size;
1390 + b->in_pos += copy_size;
1391 + }
1392 +}
1393 +
1394 +/*
1395 + * Flush pending data from dictionary to b->out. It is assumed that there is
1396 + * enough space in b->out. This is guaranteed because caller uses dict_limit()
1397 + * before decoding data into the dictionary.
1398 + */
1399 +static uint32_t INIT dict_flush(struct dictionary *dict, struct xz_buf *b)
1400 +{
1401 + size_t copy_size = dict->pos - dict->start;
1402 +
1403 + if (DEC_IS_MULTI(dict->mode)) {
1404 + if (dict->pos == dict->end)
1405 + dict->pos = 0;
1406 +
1407 + memcpy(b->out + b->out_pos, dict->buf + dict->start,
1408 + copy_size);
1409 + }
1410 +
1411 + dict->start = dict->pos;
1412 + b->out_pos += copy_size;
1413 + return copy_size;
1414 +}
1415 +
1416 +/*****************
1417 + * Range decoder *
1418 + *****************/
1419 +
1420 +/* Reset the range decoder. */
1421 +static void INIT rc_reset(struct rc_dec *rc)
1422 +{
1423 + rc->range = (uint32_t)-1;
1424 + rc->code = 0;
1425 + rc->init_bytes_left = RC_INIT_BYTES;
1426 +}
1427 +
1428 +/*
1429 + * Read the first five initial bytes into rc->code if they haven't been
1430 + * read already. (Yes, the first byte gets completely ignored.)
1431 + */
1432 +static bool_t INIT rc_read_init(struct rc_dec *rc, struct xz_buf *b)
1433 +{
1434 + while (rc->init_bytes_left > 0) {
1435 + if (b->in_pos == b->in_size)
1436 + return false;
1437 +
1438 + rc->code = (rc->code << 8) + b->in[b->in_pos++];
1439 + --rc->init_bytes_left;
1440 + }
1441 +
1442 + return true;
1443 +}
1444 +
1445 +/* Return true if there may not be enough input for the next decoding loop. */
1446 +static inline bool_t INIT rc_limit_exceeded(const struct rc_dec *rc)
1447 +{
1448 + return rc->in_pos > rc->in_limit;
1449 +}
1450 +
1451 +/*
1452 + * Return true if it is possible (from point of view of range decoder) that
1453 + * we have reached the end of the LZMA chunk.
1454 + */
1455 +static inline bool_t INIT rc_is_finished(const struct rc_dec *rc)
1456 +{
1457 + return rc->code == 0;
1458 +}
1459 +
1460 +/* Read the next input byte if needed. */
1461 +static always_inline void rc_normalize(struct rc_dec *rc)
1462 +{
1463 + if (rc->range < RC_TOP_VALUE) {
1464 + rc->range <<= RC_SHIFT_BITS;
1465 + rc->code = (rc->code << RC_SHIFT_BITS) + rc->in[rc->in_pos++];
1466 + }
1467 +}
1468 +
1469 +/*
1470 + * Decode one bit. In some versions, this function has been splitted in three
1471 + * functions so that the compiler is supposed to be able to more easily avoid
1472 + * an extra branch. In this particular version of the LZMA decoder, this
1473 + * doesn't seem to be a good idea (tested with GCC 3.3.6, 3.4.6, and 4.3.3
1474 + * on x86). Using a non-splitted version results in nicer looking code too.
1475 + *
1476 + * NOTE: This must return an int. Do not make it return a bool or the speed
1477 + * of the code generated by GCC 3.x decreases 10-15 %. (GCC 4.3 doesn't care,
1478 + * and it generates 10-20 % faster code than GCC 3.x from this file anyway.)
1479 + */
1480 +static always_inline int rc_bit(struct rc_dec *rc, uint16_t *prob)
1481 +{
1482 + uint32_t bound;
1483 + int bit;
1484 +
1485 + rc_normalize(rc);
1486 + bound = (rc->range >> RC_BIT_MODEL_TOTAL_BITS) * *prob;
1487 + if (rc->code < bound) {
1488 + rc->range = bound;
1489 + *prob += (RC_BIT_MODEL_TOTAL - *prob) >> RC_MOVE_BITS;
1490 + bit = 0;
1491 + } else {
1492 + rc->range -= bound;
1493 + rc->code -= bound;
1494 + *prob -= *prob >> RC_MOVE_BITS;
1495 + bit = 1;
1496 + }
1497 +
1498 + return bit;
1499 +}
1500 +
1501 +/* Decode a bittree starting from the most significant bit. */
1502 +static always_inline uint32_t rc_bittree(struct rc_dec *rc,
1503 + uint16_t *probs, uint32_t limit)
1504 +{
1505 + uint32_t symbol = 1;
1506 +
1507 + do {
1508 + if (rc_bit(rc, &probs[symbol]))
1509 + symbol = (symbol << 1) + 1;
1510 + else
1511 + symbol <<= 1;
1512 + } while (symbol < limit);
1513 +
1514 + return symbol;
1515 +}
1516 +
1517 +/* Decode a bittree starting from the least significant bit. */
1518 +static always_inline void rc_bittree_reverse(struct rc_dec *rc,
1519 + uint16_t *probs,
1520 + uint32_t *dest, uint32_t limit)
1521 +{
1522 + uint32_t symbol = 1;
1523 + uint32_t i = 0;
1524 +
1525 + do {
1526 + if (rc_bit(rc, &probs[symbol])) {
1527 + symbol = (symbol << 1) + 1;
1528 + *dest += 1 << i;
1529 + } else {
1530 + symbol <<= 1;
1531 + }
1532 + } while (++i < limit);
1533 +}
1534 +
1535 +/* Decode direct bits (fixed fifty-fifty probability) */
1536 +static inline void INIT rc_direct(struct rc_dec *rc, uint32_t *dest, uint32_t limit)
1537 +{
1538 + uint32_t mask;
1539 +
1540 + do {
1541 + rc_normalize(rc);
1542 + rc->range >>= 1;
1543 + rc->code -= rc->range;
1544 + mask = (uint32_t)0 - (rc->code >> 31);
1545 + rc->code += rc->range & mask;
1546 + *dest = (*dest << 1) + (mask + 1);
1547 + } while (--limit > 0);
1548 +}
1549 +
1550 +/********
1551 + * LZMA *
1552 + ********/
1553 +
1554 +/* Get pointer to literal coder probability array. */
1555 +static uint16_t *INIT lzma_literal_probs(struct xz_dec_lzma2 *s)
1556 +{
1557 + uint32_t prev_byte = dict_get(&s->dict, 0);
1558 + uint32_t low = prev_byte >> (8 - s->lzma.lc);
1559 + uint32_t high = (s->dict.pos & s->lzma.literal_pos_mask) << s->lzma.lc;
1560 + return s->lzma.literal[low + high];
1561 +}
1562 +
1563 +/* Decode a literal (one 8-bit byte) */
1564 +static void INIT lzma_literal(struct xz_dec_lzma2 *s)
1565 +{
1566 + uint16_t *probs;
1567 + uint32_t symbol;
1568 + uint32_t match_byte;
1569 + uint32_t match_bit;
1570 + uint32_t offset;
1571 + uint32_t i;
1572 +
1573 + probs = lzma_literal_probs(s);
1574 +
1575 + if (lzma_state_is_literal(s->lzma.state)) {
1576 + symbol = rc_bittree(&s->rc, probs, 0x100);
1577 + } else {
1578 + symbol = 1;
1579 + match_byte = dict_get(&s->dict, s->lzma.rep0) << 1;
1580 + offset = 0x100;
1581 +
1582 + do {
1583 + match_bit = match_byte & offset;
1584 + match_byte <<= 1;
1585 + i = offset + match_bit + symbol;
1586 +
1587 + if (rc_bit(&s->rc, &probs[i])) {
1588 + symbol = (symbol << 1) + 1;
1589 + offset &= match_bit;
1590 + } else {
1591 + symbol <<= 1;
1592 + offset &= ~match_bit;
1593 + }
1594 + } while (symbol < 0x100);
1595 + }
1596 +
1597 + dict_put(&s->dict, (uint8_t)symbol);
1598 + lzma_state_literal(&s->lzma.state);
1599 +}
1600 +
1601 +/* Decode the length of the match into s->lzma.len. */
1602 +static void INIT lzma_len(struct xz_dec_lzma2 *s, struct lzma_len_dec *l,
1603 + uint32_t pos_state)
1604 +{
1605 + uint16_t *probs;
1606 + uint32_t limit;
1607 +
1608 + if (!rc_bit(&s->rc, &l->choice)) {
1609 + probs = l->low[pos_state];
1610 + limit = LEN_LOW_SYMBOLS;
1611 + s->lzma.len = MATCH_LEN_MIN;
1612 + } else {
1613 + if (!rc_bit(&s->rc, &l->choice2)) {
1614 + probs = l->mid[pos_state];
1615 + limit = LEN_MID_SYMBOLS;
1616 + s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS;
1617 + } else {
1618 + probs = l->high;
1619 + limit = LEN_HIGH_SYMBOLS;
1620 + s->lzma.len = MATCH_LEN_MIN + LEN_LOW_SYMBOLS
1621 + + LEN_MID_SYMBOLS;
1622 + }
1623 + }
1624 +
1625 + s->lzma.len += rc_bittree(&s->rc, probs, limit) - limit;
1626 +}
1627 +
1628 +/* Decode a match. The distance will be stored in s->lzma.rep0. */
1629 +static void INIT lzma_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
1630 +{
1631 + uint16_t *probs;
1632 + uint32_t dist_slot;
1633 + uint32_t limit;
1634 +
1635 + lzma_state_match(&s->lzma.state);
1636 +
1637 + s->lzma.rep3 = s->lzma.rep2;
1638 + s->lzma.rep2 = s->lzma.rep1;
1639 + s->lzma.rep1 = s->lzma.rep0;
1640 +
1641 + lzma_len(s, &s->lzma.match_len_dec, pos_state);
1642 +
1643 + probs = s->lzma.dist_slot[lzma_get_dist_state(s->lzma.len)];
1644 + dist_slot = rc_bittree(&s->rc, probs, DIST_SLOTS) - DIST_SLOTS;
1645 +
1646 + if (dist_slot < DIST_MODEL_START) {
1647 + s->lzma.rep0 = dist_slot;
1648 + } else {
1649 + limit = (dist_slot >> 1) - 1;
1650 + s->lzma.rep0 = 2 + (dist_slot & 1);
1651 +
1652 + if (dist_slot < DIST_MODEL_END) {
1653 + s->lzma.rep0 <<= limit;
1654 + probs = s->lzma.dist_special + s->lzma.rep0
1655 + - dist_slot - 1;
1656 + rc_bittree_reverse(&s->rc, probs,
1657 + &s->lzma.rep0, limit);
1658 + } else {
1659 + rc_direct(&s->rc, &s->lzma.rep0, limit - ALIGN_BITS);
1660 + s->lzma.rep0 <<= ALIGN_BITS;
1661 + rc_bittree_reverse(&s->rc, s->lzma.dist_align,
1662 + &s->lzma.rep0, ALIGN_BITS);
1663 + }
1664 + }
1665 +}
1666 +
1667 +/*
1668 + * Decode a repeated match. The distance is one of the four most recently
1669 + * seen matches. The distance will be stored in s->lzma.rep0.
1670 + */
1671 +static void INIT lzma_rep_match(struct xz_dec_lzma2 *s, uint32_t pos_state)
1672 +{
1673 + uint32_t tmp;
1674 +
1675 + if (!rc_bit(&s->rc, &s->lzma.is_rep0[s->lzma.state])) {
1676 + if (!rc_bit(&s->rc, &s->lzma.is_rep0_long[
1677 + s->lzma.state][pos_state])) {
1678 + lzma_state_short_rep(&s->lzma.state);
1679 + s->lzma.len = 1;
1680 + return;
1681 + }
1682 + } else {
1683 + if (!rc_bit(&s->rc, &s->lzma.is_rep1[s->lzma.state])) {
1684 + tmp = s->lzma.rep1;
1685 + } else {
1686 + if (!rc_bit(&s->rc, &s->lzma.is_rep2[s->lzma.state])) {
1687 + tmp = s->lzma.rep2;
1688 + } else {
1689 + tmp = s->lzma.rep3;
1690 + s->lzma.rep3 = s->lzma.rep2;
1691 + }
1692 +
1693 + s->lzma.rep2 = s->lzma.rep1;
1694 + }
1695 +
1696 + s->lzma.rep1 = s->lzma.rep0;
1697 + s->lzma.rep0 = tmp;
1698 + }
1699 +
1700 + lzma_state_long_rep(&s->lzma.state);
1701 + lzma_len(s, &s->lzma.rep_len_dec, pos_state);
1702 +}
1703 +
1704 +/* LZMA decoder core */
1705 +static bool_t INIT lzma_main(struct xz_dec_lzma2 *s)
1706 +{
1707 + uint32_t pos_state;
1708 +
1709 + /*
1710 + * If the dictionary was reached during the previous call, try to
1711 + * finish the possibly pending repeat in the dictionary.
1712 + */
1713 + if (dict_has_space(&s->dict) && s->lzma.len > 0)
1714 + dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0);
1715 +
1716 + /*
1717 + * Decode more LZMA symbols. One iteration may consume up to
1718 + * LZMA_IN_REQUIRED - 1 bytes.
1719 + */
1720 + while (dict_has_space(&s->dict) && !rc_limit_exceeded(&s->rc)) {
1721 + pos_state = s->dict.pos & s->lzma.pos_mask;
1722 +
1723 + if (!rc_bit(&s->rc, &s->lzma.is_match[
1724 + s->lzma.state][pos_state])) {
1725 + lzma_literal(s);
1726 + } else {
1727 + if (rc_bit(&s->rc, &s->lzma.is_rep[s->lzma.state]))
1728 + lzma_rep_match(s, pos_state);
1729 + else
1730 + lzma_match(s, pos_state);
1731 +
1732 + if (!dict_repeat(&s->dict, &s->lzma.len, s->lzma.rep0))
1733 + return false;
1734 + }
1735 + }
1736 +
1737 + /*
1738 + * Having the range decoder always normalized when we are outside
1739 + * this function makes it easier to correctly handle end of the chunk.
1740 + */
1741 + rc_normalize(&s->rc);
1742 +
1743 + return true;
1744 +}
1745 +
1746 +/*
1747 + * Reset the LZMA decoder and range decoder state. Dictionary is nore reset
1748 + * here, because LZMA state may be reset without resetting the dictionary.
1749 + */
1750 +static void INIT lzma_reset(struct xz_dec_lzma2 *s)
1751 +{
1752 + uint16_t *probs;
1753 + size_t i;
1754 +
1755 + s->lzma.state = STATE_LIT_LIT;
1756 + s->lzma.rep0 = 0;
1757 + s->lzma.rep1 = 0;
1758 + s->lzma.rep2 = 0;
1759 + s->lzma.rep3 = 0;
1760 +
1761 + /*
1762 + * All probabilities are initialized to the same value. This hack
1763 + * makes the code smaller by avoiding a separate loop for each
1764 + * probability array.
1765 + *
1766 + * This could be optimized so that only that part of literal
1767 + * probabilities that are actually required. In the common case
1768 + * we would write 12 KiB less.
1769 + */
1770 + probs = s->lzma.is_match[0];
1771 + for (i = 0; i < PROBS_TOTAL; ++i)
1772 + probs[i] = RC_BIT_MODEL_TOTAL / 2;
1773 +
1774 + rc_reset(&s->rc);
1775 +}
1776 +
1777 +/*
1778 + * Decode and validate LZMA properties (lc/lp/pb) and calculate the bit masks
1779 + * from the decoded lp and pb values. On success, the LZMA decoder state is
1780 + * reset and true is returned.
1781 + */
1782 +static bool_t INIT lzma_props(struct xz_dec_lzma2 *s, uint8_t props)
1783 +{
1784 + if (props > (4 * 5 + 4) * 9 + 8)
1785 + return false;
1786 +
1787 + s->lzma.pos_mask = 0;
1788 + while (props >= 9 * 5) {
1789 + props -= 9 * 5;
1790 + ++s->lzma.pos_mask;
1791 + }
1792 +
1793 + s->lzma.pos_mask = (1 << s->lzma.pos_mask) - 1;
1794 +
1795 + s->lzma.literal_pos_mask = 0;
1796 + while (props >= 9) {
1797 + props -= 9;
1798 + ++s->lzma.literal_pos_mask;
1799 + }
1800 +
1801 + s->lzma.lc = props;
1802 +
1803 + if (s->lzma.lc + s->lzma.literal_pos_mask > 4)
1804 + return false;
1805 +
1806 + s->lzma.literal_pos_mask = (1 << s->lzma.literal_pos_mask) - 1;
1807 +
1808 + lzma_reset(s);
1809 +
1810 + return true;
1811 +}
1812 +
1813 +/*********
1814 + * LZMA2 *
1815 + *********/
1816 +
1817 +/*
1818 + * The LZMA decoder assumes that if the input limit (s->rc.in_limit) hasn't
1819 + * been exceeded, it is safe to read up to LZMA_IN_REQUIRED bytes. This
1820 + * wrapper function takes care of making the LZMA decoder's assumption safe.
1821 + *
1822 + * As long as there is plenty of input left to be decoded in the current LZMA
1823 + * chunk, we decode directly from the caller-supplied input buffer until
1824 + * there's LZMA_IN_REQUIRED bytes left. Those remaining bytes are copied into
1825 + * s->temp.buf, which (hopefully) gets filled on the next call to this
1826 + * function. We decode a few bytes from the temporary buffer so that we can
1827 + * continue decoding from the caller-supplied input buffer again.
1828 + */
1829 +static bool_t INIT lzma2_lzma(struct xz_dec_lzma2 *s, struct xz_buf *b)
1830 +{
1831 + size_t in_avail;
1832 + uint32_t tmp;
1833 +
1834 + in_avail = b->in_size - b->in_pos;
1835 + if (s->temp.size > 0 || s->lzma2.compressed == 0) {
1836 + tmp = 2 * LZMA_IN_REQUIRED - s->temp.size;
1837 + if (tmp > s->lzma2.compressed - s->temp.size)
1838 + tmp = s->lzma2.compressed - s->temp.size;
1839 + if (tmp > in_avail)
1840 + tmp = in_avail;
1841 +
1842 + memcpy(s->temp.buf + s->temp.size, b->in + b->in_pos, tmp);
1843 +
1844 + if (s->temp.size + tmp == s->lzma2.compressed) {
1845 + memzero(s->temp.buf + s->temp.size + tmp,
1846 + sizeof(s->temp.buf)
1847 + - s->temp.size - tmp);
1848 + s->rc.in_limit = s->temp.size + tmp;
1849 + } else if (s->temp.size + tmp < LZMA_IN_REQUIRED) {
1850 + s->temp.size += tmp;
1851 + b->in_pos += tmp;
1852 + return true;
1853 + } else {
1854 + s->rc.in_limit = s->temp.size + tmp - LZMA_IN_REQUIRED;
1855 + }
1856 +
1857 + s->rc.in = s->temp.buf;
1858 + s->rc.in_pos = 0;
1859 +
1860 + if (!lzma_main(s) || s->rc.in_pos > s->temp.size + tmp)
1861 + return false;
1862 +
1863 + s->lzma2.compressed -= s->rc.in_pos;
1864 +
1865 + if (s->rc.in_pos < s->temp.size) {
1866 + s->temp.size -= s->rc.in_pos;
1867 + memmove(s->temp.buf, s->temp.buf + s->rc.in_pos,
1868 + s->temp.size);
1869 + return true;
1870 + }
1871 +
1872 + b->in_pos += s->rc.in_pos - s->temp.size;
1873 + s->temp.size = 0;
1874 + }
1875 +
1876 + in_avail = b->in_size - b->in_pos;
1877 + if (in_avail >= LZMA_IN_REQUIRED) {
1878 + s->rc.in = b->in;
1879 + s->rc.in_pos = b->in_pos;
1880 +
1881 + if (in_avail >= s->lzma2.compressed + LZMA_IN_REQUIRED)
1882 + s->rc.in_limit = b->in_pos + s->lzma2.compressed;
1883 + else
1884 + s->rc.in_limit = b->in_size - LZMA_IN_REQUIRED;
1885 +
1886 + if (!lzma_main(s))
1887 + return false;
1888 +
1889 + in_avail = s->rc.in_pos - b->in_pos;
1890 + if (in_avail > s->lzma2.compressed)
1891 + return false;
1892 +
1893 + s->lzma2.compressed -= in_avail;
1894 + b->in_pos = s->rc.in_pos;
1895 + }
1896 +
1897 + in_avail = b->in_size - b->in_pos;
1898 + if (in_avail < LZMA_IN_REQUIRED) {
1899 + if (in_avail > s->lzma2.compressed)
1900 + in_avail = s->lzma2.compressed;
1901 +
1902 + memcpy(s->temp.buf, b->in + b->in_pos, in_avail);
1903 + s->temp.size = in_avail;
1904 + b->in_pos += in_avail;
1905 + }
1906 +
1907 + return true;
1908 +}
1909 +
1910 +/*
1911 + * Take care of the LZMA2 control layer, and forward the job of actual LZMA
1912 + * decoding or copying of uncompressed chunks to other functions.
1913 + */
1914 +XZ_EXTERN enum xz_ret INIT xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
1915 + struct xz_buf *b)
1916 +{
1917 + uint32_t tmp;
1918 +
1919 + while (b->in_pos < b->in_size || s->lzma2.sequence == SEQ_LZMA_RUN) {
1920 + switch (s->lzma2.sequence) {
1921 + case SEQ_CONTROL:
1922 + /*
1923 + * LZMA2 control byte
1924 + *
1925 + * Exact values:
1926 + * 0x00 End marker
1927 + * 0x01 Dictionary reset followed by
1928 + * an uncompressed chunk
1929 + * 0x02 Uncompressed chunk (no dictionary reset)
1930 + *
1931 + * Highest three bits (s->control & 0xE0):
1932 + * 0xE0 Dictionary reset, new properties and state
1933 + * reset, followed by LZMA compressed chunk
1934 + * 0xC0 New properties and state reset, followed
1935 + * by LZMA compressed chunk (no dictionary
1936 + * reset)
1937 + * 0xA0 State reset using old properties,
1938 + * followed by LZMA compressed chunk (no
1939 + * dictionary reset)
1940 + * 0x80 LZMA chunk (no dictionary or state reset)
1941 + *
1942 + * For LZMA compressed chunks, the lowest five bits
1943 + * (s->control & 1F) are the highest bits of the
1944 + * uncompressed size (bits 16-20).
1945 + *
1946 + * A new LZMA2 stream must begin with a dictionary
1947 + * reset. The first LZMA chunk must set new
1948 + * properties and reset the LZMA state.
1949 + *
1950 + * Values that don't match anything described above
1951 + * are invalid and we return XZ_DATA_ERROR.
1952 + */
1953 + tmp = b->in[b->in_pos++];
1954 +
1955 + if (tmp >= 0xE0 || tmp == 0x01) {
1956 + s->lzma2.need_props = true;
1957 + s->lzma2.need_dict_reset = false;
1958 + dict_reset(&s->dict, b);
1959 + } else if (s->lzma2.need_dict_reset) {
1960 + return XZ_DATA_ERROR;
1961 + }
1962 +
1963 + if (tmp >= 0x80) {
1964 + s->lzma2.uncompressed = (tmp & 0x1F) << 16;
1965 + s->lzma2.sequence = SEQ_UNCOMPRESSED_1;
1966 +
1967 + if (tmp >= 0xC0) {
1968 + /*
1969 + * When there are new properties,
1970 + * state reset is done at
1971 + * SEQ_PROPERTIES.
1972 + */
1973 + s->lzma2.need_props = false;
1974 + s->lzma2.next_sequence
1975 + = SEQ_PROPERTIES;
1976 +
1977 + } else if (s->lzma2.need_props) {
1978 + return XZ_DATA_ERROR;
1979 +
1980 + } else {
1981 + s->lzma2.next_sequence
1982 + = SEQ_LZMA_PREPARE;
1983 + if (tmp >= 0xA0)
1984 + lzma_reset(s);
1985 + }
1986 + } else {
1987 + if (tmp == 0x00)
1988 + return XZ_STREAM_END;
1989 +
1990 + if (tmp > 0x02)
1991 + return XZ_DATA_ERROR;
1992 +
1993 + s->lzma2.sequence = SEQ_COMPRESSED_0;
1994 + s->lzma2.next_sequence = SEQ_COPY;
1995 + }
1996 +
1997 + break;
1998 +
1999 + case SEQ_UNCOMPRESSED_1:
2000 + s->lzma2.uncompressed
2001 + += (uint32_t)b->in[b->in_pos++] << 8;
2002 + s->lzma2.sequence = SEQ_UNCOMPRESSED_2;
2003 + break;
2004 +
2005 + case SEQ_UNCOMPRESSED_2:
2006 + s->lzma2.uncompressed
2007 + += (uint32_t)b->in[b->in_pos++] + 1;
2008 + s->lzma2.sequence = SEQ_COMPRESSED_0;
2009 + break;
2010 +
2011 + case SEQ_COMPRESSED_0:
2012 + s->lzma2.compressed
2013 + = (uint32_t)b->in[b->in_pos++] << 8;
2014 + s->lzma2.sequence = SEQ_COMPRESSED_1;
2015 + break;
2016 +
2017 + case SEQ_COMPRESSED_1:
2018 + s->lzma2.compressed
2019 + += (uint32_t)b->in[b->in_pos++] + 1;
2020 + s->lzma2.sequence = s->lzma2.next_sequence;
2021 + break;
2022 +
2023 + case SEQ_PROPERTIES:
2024 + if (!lzma_props(s, b->in[b->in_pos++]))
2025 + return XZ_DATA_ERROR;
2026 +
2027 + s->lzma2.sequence = SEQ_LZMA_PREPARE;
2028 +
2029 + case SEQ_LZMA_PREPARE:
2030 + if (s->lzma2.compressed < RC_INIT_BYTES)
2031 + return XZ_DATA_ERROR;
2032 +
2033 + if (!rc_read_init(&s->rc, b))
2034 + return XZ_OK;
2035 +
2036 + s->lzma2.compressed -= RC_INIT_BYTES;
2037 + s->lzma2.sequence = SEQ_LZMA_RUN;
2038 +
2039 + case SEQ_LZMA_RUN:
2040 + /*
2041 + * Set dictionary limit to indicate how much we want
2042 + * to be encoded at maximum. Decode new data into the
2043 + * dictionary. Flush the new data from dictionary to
2044 + * b->out. Check if we finished decoding this chunk.
2045 + * In case the dictionary got full but we didn't fill
2046 + * the output buffer yet, we may run this loop
2047 + * multiple times without changing s->lzma2.sequence.
2048 + */
2049 + dict_limit(&s->dict, min_t(size_t,
2050 + b->out_size - b->out_pos,
2051 + s->lzma2.uncompressed));
2052 + if (!lzma2_lzma(s, b))
2053 + return XZ_DATA_ERROR;
2054 +
2055 + s->lzma2.uncompressed -= dict_flush(&s->dict, b);
2056 +
2057 + if (s->lzma2.uncompressed == 0) {
2058 + if (s->lzma2.compressed > 0 || s->lzma.len > 0
2059 + || !rc_is_finished(&s->rc))
2060 + return XZ_DATA_ERROR;
2061 +
2062 + rc_reset(&s->rc);
2063 + s->lzma2.sequence = SEQ_CONTROL;
2064 +
2065 + } else if (b->out_pos == b->out_size
2066 + || (b->in_pos == b->in_size
2067 + && s->temp.size
2068 + < s->lzma2.compressed)) {
2069 + return XZ_OK;
2070 + }
2071 +
2072 + break;
2073 +
2074 + case SEQ_COPY:
2075 + dict_uncompressed(&s->dict, b, &s->lzma2.compressed);
2076 + if (s->lzma2.compressed > 0)
2077 + return XZ_OK;
2078 +
2079 + s->lzma2.sequence = SEQ_CONTROL;
2080 + break;
2081 + }
2082 + }
2083 +
2084 + return XZ_OK;
2085 +}
2086 +
2087 +XZ_EXTERN struct xz_dec_lzma2 *INIT xz_dec_lzma2_create(enum xz_mode mode,
2088 + uint32_t dict_max)
2089 +{
2090 + struct xz_dec_lzma2 *s = malloc(sizeof(*s));
2091 + if (s == NULL)
2092 + return NULL;
2093 +
2094 + s->dict.mode = mode;
2095 + s->dict.size_max = dict_max;
2096 +
2097 + if (DEC_IS_PREALLOC(mode)) {
2098 + s->dict.buf = large_malloc(dict_max);
2099 + if (s->dict.buf == NULL) {
2100 + free(s);
2101 + return NULL;
2102 + }
2103 + } else if (DEC_IS_DYNALLOC(mode)) {
2104 + s->dict.buf = NULL;
2105 + s->dict.allocated = 0;
2106 + }
2107 +
2108 + return s;
2109 +}
2110 +
2111 +XZ_EXTERN enum xz_ret INIT xz_dec_lzma2_reset(struct xz_dec_lzma2 *s, uint8_t props)
2112 +{
2113 + /* This limits dictionary size to 3 GiB to keep parsing simpler. */
2114 + if (props > 39)
2115 + return XZ_OPTIONS_ERROR;
2116 +
2117 + s->dict.size = 2 + (props & 1);
2118 + s->dict.size <<= (props >> 1) + 11;
2119 +
2120 + if (DEC_IS_MULTI(s->dict.mode)) {
2121 + if (s->dict.size > s->dict.size_max)
2122 + return XZ_MEMLIMIT_ERROR;
2123 +
2124 + s->dict.end = s->dict.size;
2125 +
2126 + if (DEC_IS_DYNALLOC(s->dict.mode)) {
2127 + if (s->dict.allocated < s->dict.size) {
2128 + large_free(s->dict.buf);
2129 + s->dict.buf = large_malloc(s->dict.size);
2130 + if (s->dict.buf == NULL) {
2131 + s->dict.allocated = 0;
2132 + return XZ_MEM_ERROR;
2133 + }
2134 + }
2135 + }
2136 + }
2137 +
2138 + s->lzma.len = 0;
2139 +
2140 + s->lzma2.sequence = SEQ_CONTROL;
2141 + s->lzma2.need_dict_reset = true;
2142 +
2143 + s->temp.size = 0;
2144 +
2145 + return XZ_OK;
2146 +}
2147 +
2148 +XZ_EXTERN void INIT xz_dec_lzma2_end(struct xz_dec_lzma2 *s)
2149 +{
2150 + if (DEC_IS_MULTI(s->dict.mode))
2151 + large_free(s->dict.buf);
2152 +
2153 + free(s);
2154 +}
2155 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/dec_stream.c
2156 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2157 +++ b/xen/common/xz/dec_stream.c Wed Mar 09 16:18:58 2011 +0000
2158 @@ -0,0 +1,821 @@
2159 +/*
2160 + * .xz Stream decoder
2161 + *
2162 + * Author: Lasse Collin <lasse.collin@tukaani.org>
2163 + *
2164 + * This file has been put into the public domain.
2165 + * You can do whatever you want with this file.
2166 + */
2167 +
2168 +#include "private.h"
2169 +#include "stream.h"
2170 +
2171 +/* Hash used to validate the Index field */
2172 +struct xz_dec_hash {
2173 + vli_type unpadded;
2174 + vli_type uncompressed;
2175 + uint32_t crc32;
2176 +};
2177 +
2178 +struct xz_dec {
2179 + /* Position in dec_main() */
2180 + enum {
2181 + SEQ_STREAM_HEADER,
2182 + SEQ_BLOCK_START,
2183 + SEQ_BLOCK_HEADER,
2184 + SEQ_BLOCK_UNCOMPRESS,
2185 + SEQ_BLOCK_PADDING,
2186 + SEQ_BLOCK_CHECK,
2187 + SEQ_INDEX,
2188 + SEQ_INDEX_PADDING,
2189 + SEQ_INDEX_CRC32,
2190 + SEQ_STREAM_FOOTER
2191 + } sequence;
2192 +
2193 + /* Position in variable-length integers and Check fields */
2194 + uint32_t pos;
2195 +
2196 + /* Variable-length integer decoded by dec_vli() */
2197 + vli_type vli;
2198 +
2199 + /* Saved in_pos and out_pos */
2200 + size_t in_start;
2201 + size_t out_start;
2202 +
2203 + /* CRC32 value in Block or Index */
2204 + uint32_t crc32;
2205 +
2206 + /* Type of the integrity check calculated from uncompressed data */
2207 + enum xz_check check_type;
2208 +
2209 + /* Operation mode */
2210 + enum xz_mode mode;
2211 +
2212 + /*
2213 + * True if the next call to xz_dec_run() is allowed to return
2214 + * XZ_BUF_ERROR.
2215 + */
2216 + bool_t allow_buf_error;
2217 +
2218 + /* Information stored in Block Header */
2219 + struct {
2220 + /*
2221 + * Value stored in the Compressed Size field, or
2222 + * VLI_UNKNOWN if Compressed Size is not present.
2223 + */
2224 + vli_type compressed;
2225 +
2226 + /*
2227 + * Value stored in the Uncompressed Size field, or
2228 + * VLI_UNKNOWN if Uncompressed Size is not present.
2229 + */
2230 + vli_type uncompressed;
2231 +
2232 + /* Size of the Block Header field */
2233 + uint32_t size;
2234 + } block_header;
2235 +
2236 + /* Information collected when decoding Blocks */
2237 + struct {
2238 + /* Observed compressed size of the current Block */
2239 + vli_type compressed;
2240 +
2241 + /* Observed uncompressed size of the current Block */
2242 + vli_type uncompressed;
2243 +
2244 + /* Number of Blocks decoded so far */
2245 + vli_type count;
2246 +
2247 + /*
2248 + * Hash calculated from the Block sizes. This is used to
2249 + * validate the Index field.
2250 + */
2251 + struct xz_dec_hash hash;
2252 + } block;
2253 +
2254 + /* Variables needed when verifying the Index field */
2255 + struct {
2256 + /* Position in dec_index() */
2257 + enum {
2258 + SEQ_INDEX_COUNT,
2259 + SEQ_INDEX_UNPADDED,
2260 + SEQ_INDEX_UNCOMPRESSED
2261 + } sequence;
2262 +
2263 + /* Size of the Index in bytes */
2264 + vli_type size;
2265 +
2266 + /* Number of Records (matches block.count in valid files) */
2267 + vli_type count;
2268 +
2269 + /*
2270 + * Hash calculated from the Records (matches block.hash in
2271 + * valid files).
2272 + */
2273 + struct xz_dec_hash hash;
2274 + } index;
2275 +
2276 + /*
2277 + * Temporary buffer needed to hold Stream Header, Block Header,
2278 + * and Stream Footer. The Block Header is the biggest (1 KiB)
2279 + * so we reserve space according to that. buf[] has to be aligned
2280 + * to a multiple of four bytes; the size_t variables before it
2281 + * should guarantee this.
2282 + */
2283 + struct {
2284 + size_t pos;
2285 + size_t size;
2286 + uint8_t buf[1024];
2287 + } temp;
2288 +
2289 + struct xz_dec_lzma2 *lzma2;
2290 +
2291 +#ifdef XZ_DEC_BCJ
2292 + struct xz_dec_bcj *bcj;
2293 + bool_t bcj_active;
2294 +#endif
2295 +};
2296 +
2297 +#ifdef XZ_DEC_ANY_CHECK
2298 +/* Sizes of the Check field with different Check IDs */
2299 +static const uint8_t check_sizes[16] = {
2300 + 0,
2301 + 4, 4, 4,
2302 + 8, 8, 8,
2303 + 16, 16, 16,
2304 + 32, 32, 32,
2305 + 64, 64, 64
2306 +};
2307 +#endif
2308 +
2309 +/*
2310 + * Fill s->temp by copying data starting from b->in[b->in_pos]. Caller
2311 + * must have set s->temp.pos to indicate how much data we are supposed
2312 + * to copy into s->temp.buf. Return true once s->temp.pos has reached
2313 + * s->temp.size.
2314 + */
2315 +static bool_t INIT fill_temp(struct xz_dec *s, struct xz_buf *b)
2316 +{
2317 + size_t copy_size = min_t(size_t,
2318 + b->in_size - b->in_pos, s->temp.size - s->temp.pos);
2319 +
2320 + memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);
2321 + b->in_pos += copy_size;
2322 + s->temp.pos += copy_size;
2323 +
2324 + if (s->temp.pos == s->temp.size) {
2325 + s->temp.pos = 0;
2326 + return true;
2327 + }
2328 +
2329 + return false;
2330 +}
2331 +
2332 +/* Decode a variable-length integer (little-endian base-128 encoding) */
2333 +static enum xz_ret INIT dec_vli(struct xz_dec *s, const uint8_t *in,
2334 + size_t *in_pos, size_t in_size)
2335 +{
2336 + uint8_t byte;
2337 +
2338 + if (s->pos == 0)
2339 + s->vli = 0;
2340 +
2341 + while (*in_pos < in_size) {
2342 + byte = in[*in_pos];
2343 + ++*in_pos;
2344 +
2345 + s->vli |= (vli_type)(byte & 0x7F) << s->pos;
2346 +
2347 + if ((byte & 0x80) == 0) {
2348 + /* Don't allow non-minimal encodings. */
2349 + if (byte == 0 && s->pos != 0)
2350 + return XZ_DATA_ERROR;
2351 +
2352 + s->pos = 0;
2353 + return XZ_STREAM_END;
2354 + }
2355 +
2356 + s->pos += 7;
2357 + if (s->pos == 7 * VLI_BYTES_MAX)
2358 + return XZ_DATA_ERROR;
2359 + }
2360 +
2361 + return XZ_OK;
2362 +}
2363 +
2364 +/*
2365 + * Decode the Compressed Data field from a Block. Update and validate
2366 + * the observed compressed and uncompressed sizes of the Block so that
2367 + * they don't exceed the values possibly stored in the Block Header
2368 + * (validation assumes that no integer overflow occurs, since vli_type
2369 + * is normally uint64_t). Update the CRC32 if presence of the CRC32
2370 + * field was indicated in Stream Header.
2371 + *
2372 + * Once the decoding is finished, validate that the observed sizes match
2373 + * the sizes possibly stored in the Block Header. Update the hash and
2374 + * Block count, which are later used to validate the Index field.
2375 + */
2376 +static enum xz_ret INIT dec_block(struct xz_dec *s, struct xz_buf *b)
2377 +{
2378 + enum xz_ret ret;
2379 +
2380 + s->in_start = b->in_pos;
2381 + s->out_start = b->out_pos;
2382 +
2383 +#ifdef XZ_DEC_BCJ
2384 + if (s->bcj_active)
2385 + ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);
2386 + else
2387 +#endif
2388 + ret = xz_dec_lzma2_run(s->lzma2, b);
2389 +
2390 + s->block.compressed += b->in_pos - s->in_start;
2391 + s->block.uncompressed += b->out_pos - s->out_start;
2392 +
2393 + /*
2394 + * There is no need to separately check for VLI_UNKNOWN, since
2395 + * the observed sizes are always smaller than VLI_UNKNOWN.
2396 + */
2397 + if (s->block.compressed > s->block_header.compressed
2398 + || s->block.uncompressed
2399 + > s->block_header.uncompressed)
2400 + return XZ_DATA_ERROR;
2401 +
2402 + if (s->check_type == XZ_CHECK_CRC32)
2403 + s->crc32 = xz_crc32(b->out + s->out_start,
2404 + b->out_pos - s->out_start, s->crc32);
2405 +
2406 + if (ret == XZ_STREAM_END) {
2407 + if (s->block_header.compressed != VLI_UNKNOWN
2408 + && s->block_header.compressed
2409 + != s->block.compressed)
2410 + return XZ_DATA_ERROR;
2411 +
2412 + if (s->block_header.uncompressed != VLI_UNKNOWN
2413 + && s->block_header.uncompressed
2414 + != s->block.uncompressed)
2415 + return XZ_DATA_ERROR;
2416 +
2417 + s->block.hash.unpadded += s->block_header.size
2418 + + s->block.compressed;
2419 +
2420 +#ifdef XZ_DEC_ANY_CHECK
2421 + s->block.hash.unpadded += check_sizes[s->check_type];
2422 +#else
2423 + if (s->check_type == XZ_CHECK_CRC32)
2424 + s->block.hash.unpadded += 4;
2425 +#endif
2426 +
2427 + s->block.hash.uncompressed += s->block.uncompressed;
2428 + s->block.hash.crc32 = xz_crc32(
2429 + (const uint8_t *)&s->block.hash,
2430 + sizeof(s->block.hash), s->block.hash.crc32);
2431 +
2432 + ++s->block.count;
2433 + }
2434 +
2435 + return ret;
2436 +}
2437 +
2438 +/* Update the Index size and the CRC32 value. */
2439 +static void INIT index_update(struct xz_dec *s, const struct xz_buf *b)
2440 +{
2441 + size_t in_used = b->in_pos - s->in_start;
2442 + s->index.size += in_used;
2443 + s->crc32 = xz_crc32(b->in + s->in_start, in_used, s->crc32);
2444 +}
2445 +
2446 +/*
2447 + * Decode the Number of Records, Unpadded Size, and Uncompressed Size
2448 + * fields from the Index field. That is, Index Padding and CRC32 are not
2449 + * decoded by this function.
2450 + *
2451 + * This can return XZ_OK (more input needed), XZ_STREAM_END (everything
2452 + * successfully decoded), or XZ_DATA_ERROR (input is corrupt).
2453 + */
2454 +static enum xz_ret INIT dec_index(struct xz_dec *s, struct xz_buf *b)
2455 +{
2456 + enum xz_ret ret;
2457 +
2458 + do {
2459 + ret = dec_vli(s, b->in, &b->in_pos, b->in_size);
2460 + if (ret != XZ_STREAM_END) {
2461 + index_update(s, b);
2462 + return ret;
2463 + }
2464 +
2465 + switch (s->index.sequence) {
2466 + case SEQ_INDEX_COUNT:
2467 + s->index.count = s->vli;
2468 +
2469 + /*
2470 + * Validate that the Number of Records field
2471 + * indicates the same number of Records as
2472 + * there were Blocks in the Stream.
2473 + */
2474 + if (s->index.count != s->block.count)
2475 + return XZ_DATA_ERROR;
2476 +
2477 + s->index.sequence = SEQ_INDEX_UNPADDED;
2478 + break;
2479 +
2480 + case SEQ_INDEX_UNPADDED:
2481 + s->index.hash.unpadded += s->vli;
2482 + s->index.sequence = SEQ_INDEX_UNCOMPRESSED;
2483 + break;
2484 +
2485 + case SEQ_INDEX_UNCOMPRESSED:
2486 + s->index.hash.uncompressed += s->vli;
2487 + s->index.hash.crc32 = xz_crc32(
2488 + (const uint8_t *)&s->index.hash,
2489 + sizeof(s->index.hash),
2490 + s->index.hash.crc32);
2491 + --s->index.count;
2492 + s->index.sequence = SEQ_INDEX_UNPADDED;
2493 + break;
2494 + }
2495 + } while (s->index.count > 0);
2496 +
2497 + return XZ_STREAM_END;
2498 +}
2499 +
2500 +/*
2501 + * Validate that the next four input bytes match the value of s->crc32.
2502 + * s->pos must be zero when starting to validate the first byte.
2503 + */
2504 +static enum xz_ret INIT crc32_validate(struct xz_dec *s, struct xz_buf *b)
2505 +{
2506 + do {
2507 + if (b->in_pos == b->in_size)
2508 + return XZ_OK;
2509 +
2510 + if (((s->crc32 >> s->pos) & 0xFF) != b->in[b->in_pos++])
2511 + return XZ_DATA_ERROR;
2512 +
2513 + s->pos += 8;
2514 +
2515 + } while (s->pos < 32);
2516 +
2517 + s->crc32 = 0;
2518 + s->pos = 0;
2519 +
2520 + return XZ_STREAM_END;
2521 +}
2522 +
2523 +#ifdef XZ_DEC_ANY_CHECK
2524 +/*
2525 + * Skip over the Check field when the Check ID is not supported.
2526 + * Returns true once the whole Check field has been skipped over.
2527 + */
2528 +static bool_t INIT check_skip(struct xz_dec *s, struct xz_buf *b)
2529 +{
2530 + while (s->pos < check_sizes[s->check_type]) {
2531 + if (b->in_pos == b->in_size)
2532 + return false;
2533 +
2534 + ++b->in_pos;
2535 + ++s->pos;
2536 + }
2537 +
2538 + s->pos = 0;
2539 +
2540 + return true;
2541 +}
2542 +#endif
2543 +
2544 +/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */
2545 +static enum xz_ret INIT dec_stream_header(struct xz_dec *s)
2546 +{
2547 + if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))
2548 + return XZ_FORMAT_ERROR;
2549 +
2550 + if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)
2551 + != get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))
2552 + return XZ_DATA_ERROR;
2553 +
2554 + if (s->temp.buf[HEADER_MAGIC_SIZE] != 0)
2555 + return XZ_OPTIONS_ERROR;
2556 +
2557 + /*
2558 + * Of integrity checks, we support only none (Check ID = 0) and
2559 + * CRC32 (Check ID = 1). However, if XZ_DEC_ANY_CHECK is defined,
2560 + * we will accept other check types too, but then the check won't
2561 + * be verified and a warning (XZ_UNSUPPORTED_CHECK) will be given.
2562 + */
2563 + s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];
2564 +
2565 +#ifdef XZ_DEC_ANY_CHECK
2566 + if (s->check_type > XZ_CHECK_MAX)
2567 + return XZ_OPTIONS_ERROR;
2568 +
2569 + if (s->check_type > XZ_CHECK_CRC32)
2570 + return XZ_UNSUPPORTED_CHECK;
2571 +#else
2572 + if (s->check_type > XZ_CHECK_CRC32)
2573 + return XZ_OPTIONS_ERROR;
2574 +#endif
2575 +
2576 + return XZ_OK;
2577 +}
2578 +
2579 +/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */
2580 +static enum xz_ret INIT dec_stream_footer(struct xz_dec *s)
2581 +{
2582 + if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))
2583 + return XZ_DATA_ERROR;
2584 +
2585 + if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))
2586 + return XZ_DATA_ERROR;
2587 +
2588 + /*
2589 + * Validate Backward Size. Note that we never added the size of the
2590 + * Index CRC32 field to s->index.size, thus we use s->index.size / 4
2591 + * instead of s->index.size / 4 - 1.
2592 + */
2593 + if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))
2594 + return XZ_DATA_ERROR;
2595 +
2596 + if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type)
2597 + return XZ_DATA_ERROR;
2598 +
2599 + /*
2600 + * Use XZ_STREAM_END instead of XZ_OK to be more convenient
2601 + * for the caller.
2602 + */
2603 + return XZ_STREAM_END;
2604 +}
2605 +
2606 +/* Decode the Block Header and initialize the filter chain. */
2607 +static enum xz_ret INIT dec_block_header(struct xz_dec *s)
2608 +{
2609 + enum xz_ret ret;
2610 +
2611 + /*
2612 + * Validate the CRC32. We know that the temp buffer is at least
2613 + * eight bytes so this is safe.
2614 + */
2615 + s->temp.size -= 4;
2616 + if (xz_crc32(s->temp.buf, s->temp.size, 0)
2617 + != get_le32(s->temp.buf + s->temp.size))
2618 + return XZ_DATA_ERROR;
2619 +
2620 + s->temp.pos = 2;
2621 +
2622 + /*
2623 + * Catch unsupported Block Flags. We support only one or two filters
2624 + * in the chain, so we catch that with the same test.
2625 + */
2626 +#ifdef XZ_DEC_BCJ
2627 + if (s->temp.buf[1] & 0x3E)
2628 +#else
2629 + if (s->temp.buf[1] & 0x3F)
2630 +#endif
2631 + return XZ_OPTIONS_ERROR;
2632 +
2633 + /* Compressed Size */
2634 + if (s->temp.buf[1] & 0x40) {
2635 + if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
2636 + != XZ_STREAM_END)
2637 + return XZ_DATA_ERROR;
2638 +
2639 + s->block_header.compressed = s->vli;
2640 + } else {
2641 + s->block_header.compressed = VLI_UNKNOWN;
2642 + }
2643 +
2644 + /* Uncompressed Size */
2645 + if (s->temp.buf[1] & 0x80) {
2646 + if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)
2647 + != XZ_STREAM_END)
2648 + return XZ_DATA_ERROR;
2649 +
2650 + s->block_header.uncompressed = s->vli;
2651 + } else {
2652 + s->block_header.uncompressed = VLI_UNKNOWN;
2653 + }
2654 +
2655 +#ifdef XZ_DEC_BCJ
2656 + /* If there are two filters, the first one must be a BCJ filter. */
2657 + s->bcj_active = s->temp.buf[1] & 0x01;
2658 + if (s->bcj_active) {
2659 + if (s->temp.size - s->temp.pos < 2)
2660 + return XZ_OPTIONS_ERROR;
2661 +
2662 + ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);
2663 + if (ret != XZ_OK)
2664 + return ret;
2665 +
2666 + /*
2667 + * We don't support custom start offset,
2668 + * so Size of Properties must be zero.
2669 + */
2670 + if (s->temp.buf[s->temp.pos++] != 0x00)
2671 + return XZ_OPTIONS_ERROR;
2672 + }
2673 +#endif
2674 +
2675 + /* Valid Filter Flags always take at least two bytes. */
2676 + if (s->temp.size - s->temp.pos < 2)
2677 + return XZ_DATA_ERROR;
2678 +
2679 + /* Filter ID = LZMA2 */
2680 + if (s->temp.buf[s->temp.pos++] != 0x21)
2681 + return XZ_OPTIONS_ERROR;
2682 +
2683 + /* Size of Properties = 1-byte Filter Properties */
2684 + if (s->temp.buf[s->temp.pos++] != 0x01)
2685 + return XZ_OPTIONS_ERROR;
2686 +
2687 + /* Filter Properties contains LZMA2 dictionary size. */
2688 + if (s->temp.size - s->temp.pos < 1)
2689 + return XZ_DATA_ERROR;
2690 +
2691 + ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);
2692 + if (ret != XZ_OK)
2693 + return ret;
2694 +
2695 + /* The rest must be Header Padding. */
2696 + while (s->temp.pos < s->temp.size)
2697 + if (s->temp.buf[s->temp.pos++] != 0x00)
2698 + return XZ_OPTIONS_ERROR;
2699 +
2700 + s->temp.pos = 0;
2701 + s->block.compressed = 0;
2702 + s->block.uncompressed = 0;
2703 +
2704 + return XZ_OK;
2705 +}
2706 +
2707 +static enum xz_ret INIT dec_main(struct xz_dec *s, struct xz_buf *b)
2708 +{
2709 + enum xz_ret ret;
2710 +
2711 + /*
2712 + * Store the start position for the case when we are in the middle
2713 + * of the Index field.
2714 + */
2715 + s->in_start = b->in_pos;
2716 +
2717 + while (true) {
2718 + switch (s->sequence) {
2719 + case SEQ_STREAM_HEADER:
2720 + /*
2721 + * Stream Header is copied to s->temp, and then
2722 + * decoded from there. This way if the caller
2723 + * gives us only little input at a time, we can
2724 + * still keep the Stream Header decoding code
2725 + * simple. Similar approach is used in many places
2726 + * in this file.
2727 + */
2728 + if (!fill_temp(s, b))
2729 + return XZ_OK;
2730 +
2731 + /*
2732 + * If dec_stream_header() returns
2733 + * XZ_UNSUPPORTED_CHECK, it is still possible
2734 + * to continue decoding if working in multi-call
2735 + * mode. Thus, update s->sequence before calling
2736 + * dec_stream_header().
2737 + */
2738 + s->sequence = SEQ_BLOCK_START;
2739 +
2740 + ret = dec_stream_header(s);
2741 + if (ret != XZ_OK)
2742 + return ret;
2743 +
2744 + case SEQ_BLOCK_START:
2745 + /* We need one byte of input to continue. */
2746 + if (b->in_pos == b->in_size)
2747 + return XZ_OK;
2748 +
2749 + /* See if this is the beginning of the Index field. */
2750 + if (b->in[b->in_pos] == 0) {
2751 + s->in_start = b->in_pos++;
2752 + s->sequence = SEQ_INDEX;
2753 + break;
2754 + }
2755 +
2756 + /*
2757 + * Calculate the size of the Block Header and
2758 + * prepare to decode it.
2759 + */
2760 + s->block_header.size
2761 + = ((uint32_t)b->in[b->in_pos] + 1) * 4;
2762 +
2763 + s->temp.size = s->block_header.size;
2764 + s->temp.pos = 0;
2765 + s->sequence = SEQ_BLOCK_HEADER;
2766 +
2767 + case SEQ_BLOCK_HEADER:
2768 + if (!fill_temp(s, b))
2769 + return XZ_OK;
2770 +
2771 + ret = dec_block_header(s);
2772 + if (ret != XZ_OK)
2773 + return ret;
2774 +
2775 + s->sequence = SEQ_BLOCK_UNCOMPRESS;
2776 +
2777 + case SEQ_BLOCK_UNCOMPRESS:
2778 + ret = dec_block(s, b);
2779 + if (ret != XZ_STREAM_END)
2780 + return ret;
2781 +
2782 + s->sequence = SEQ_BLOCK_PADDING;
2783 +
2784 + case SEQ_BLOCK_PADDING:
2785 + /*
2786 + * Size of Compressed Data + Block Padding
2787 + * must be a multiple of four. We don't need
2788 + * s->block.compressed for anything else
2789 + * anymore, so we use it here to test the size
2790 + * of the Block Padding field.
2791 + */
2792 + while (s->block.compressed & 3) {
2793 + if (b->in_pos == b->in_size)
2794 + return XZ_OK;
2795 +
2796 + if (b->in[b->in_pos++] != 0)
2797 + return XZ_DATA_ERROR;
2798 +
2799 + ++s->block.compressed;
2800 + }
2801 +
2802 + s->sequence = SEQ_BLOCK_CHECK;
2803 +
2804 + case SEQ_BLOCK_CHECK:
2805 + if (s->check_type == XZ_CHECK_CRC32) {
2806 + ret = crc32_validate(s, b);
2807 + if (ret != XZ_STREAM_END)
2808 + return ret;
2809 + }
2810 +#ifdef XZ_DEC_ANY_CHECK
2811 + else if (!check_skip(s, b)) {
2812 + return XZ_OK;
2813 + }
2814 +#endif
2815 +
2816 + s->sequence = SEQ_BLOCK_START;
2817 + break;
2818 +
2819 + case SEQ_INDEX:
2820 + ret = dec_index(s, b);
2821 + if (ret != XZ_STREAM_END)
2822 + return ret;
2823 +
2824 + s->sequence = SEQ_INDEX_PADDING;
2825 +
2826 + case SEQ_INDEX_PADDING:
2827 + while ((s->index.size + (b->in_pos - s->in_start))
2828 + & 3) {
2829 + if (b->in_pos == b->in_size) {
2830 + index_update(s, b);
2831 + return XZ_OK;
2832 + }
2833 +
2834 + if (b->in[b->in_pos++] != 0)
2835 + return XZ_DATA_ERROR;
2836 + }
2837 +
2838 + /* Finish the CRC32 value and Index size. */
2839 + index_update(s, b);
2840 +
2841 + /* Compare the hashes to validate the Index field. */
2842 + if (!memeq(&s->block.hash, &s->index.hash,
2843 + sizeof(s->block.hash)))
2844 + return XZ_DATA_ERROR;
2845 +
2846 + s->sequence = SEQ_INDEX_CRC32;
2847 +
2848 + case SEQ_INDEX_CRC32:
2849 + ret = crc32_validate(s, b);
2850 + if (ret != XZ_STREAM_END)
2851 + return ret;
2852 +
2853 + s->temp.size = STREAM_HEADER_SIZE;
2854 + s->sequence = SEQ_STREAM_FOOTER;
2855 +
2856 + case SEQ_STREAM_FOOTER:
2857 + if (!fill_temp(s, b))
2858 + return XZ_OK;
2859 +
2860 + return dec_stream_footer(s);
2861 + }
2862 + }
2863 +
2864 + /* Never reached */
2865 +}
2866 +
2867 +XZ_EXTERN void INIT xz_dec_reset(struct xz_dec *s)
2868 +{
2869 + s->sequence = SEQ_STREAM_HEADER;
2870 + s->allow_buf_error = false;
2871 + s->pos = 0;
2872 + s->crc32 = 0;
2873 + memzero(&s->block, sizeof(s->block));
2874 + memzero(&s->index, sizeof(s->index));
2875 + s->temp.pos = 0;
2876 + s->temp.size = STREAM_HEADER_SIZE;
2877 +}
2878 +
2879 +/*
2880 + * xz_dec_run() is a wrapper for dec_main() to handle some special cases in
2881 + * multi-call and single-call decoding.
2882 + *
2883 + * In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we
2884 + * are not going to make any progress anymore. This is to prevent the caller
2885 + * from calling us infinitely when the input file is truncated or otherwise
2886 + * corrupt. Since zlib-style API allows that the caller fills the input buffer
2887 + * only when the decoder doesn't produce any new output, we have to be careful
2888 + * to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only
2889 + * after the second consecutive call to xz_dec_run() that makes no progress.
2890 + *
2891 + * In single-call mode, if we couldn't decode everything and no error
2892 + * occurred, either the input is truncated or the output buffer is too small.
2893 + * Since we know that the last input byte never produces any output, we know
2894 + * that if all the input was consumed and decoding wasn't finished, the file
2895 + * must be corrupt. Otherwise the output buffer has to be too small or the
2896 + * file is corrupt in a way that decoding it produces too big output.
2897 + *
2898 + * If single-call decoding fails, we reset b->in_pos and b->out_pos back to
2899 + * their original values. This is because with some filter chains there won't
2900 + * be any valid uncompressed data in the output buffer unless the decoding
2901 + * actually succeeds (that's the price to pay of using the output buffer as
2902 + * the workspace).
2903 + */
2904 +XZ_EXTERN enum xz_ret INIT xz_dec_run(struct xz_dec *s, struct xz_buf *b)
2905 +{
2906 + size_t in_start;
2907 + size_t out_start;
2908 + enum xz_ret ret;
2909 +
2910 + if (DEC_IS_SINGLE(s->mode))
2911 + xz_dec_reset(s);
2912 +
2913 + in_start = b->in_pos;
2914 + out_start = b->out_pos;
2915 + ret = dec_main(s, b);
2916 +
2917 + if (DEC_IS_SINGLE(s->mode)) {
2918 + if (ret == XZ_OK)
2919 + ret = b->in_pos == b->in_size
2920 + ? XZ_DATA_ERROR : XZ_BUF_ERROR;
2921 +
2922 + if (ret != XZ_STREAM_END) {
2923 + b->in_pos = in_start;
2924 + b->out_pos = out_start;
2925 + }
2926 +
2927 + } else if (ret == XZ_OK && in_start == b->in_pos
2928 + && out_start == b->out_pos) {
2929 + if (s->allow_buf_error)
2930 + ret = XZ_BUF_ERROR;
2931 +
2932 + s->allow_buf_error = true;
2933 + } else {
2934 + s->allow_buf_error = false;
2935 + }
2936 +
2937 + return ret;
2938 +}
2939 +
2940 +XZ_EXTERN struct xz_dec *INIT xz_dec_init(enum xz_mode mode, uint32_t dict_max)
2941 +{
2942 + struct xz_dec *s = malloc(sizeof(*s));
2943 + if (s == NULL)
2944 + return NULL;
2945 +
2946 + s->mode = mode;
2947 +
2948 +#ifdef XZ_DEC_BCJ
2949 + s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode));
2950 + if (s->bcj == NULL)
2951 + goto error_bcj;
2952 +#endif
2953 +
2954 + s->lzma2 = xz_dec_lzma2_create(mode, dict_max);
2955 + if (s->lzma2 == NULL)
2956 + goto error_lzma2;
2957 +
2958 + xz_dec_reset(s);
2959 + return s;
2960 +
2961 +error_lzma2:
2962 +#ifdef XZ_DEC_BCJ
2963 + xz_dec_bcj_end(s->bcj);
2964 +error_bcj:
2965 +#endif
2966 + free(s);
2967 + return NULL;
2968 +}
2969 +
2970 +XZ_EXTERN void INIT xz_dec_end(struct xz_dec *s)
2971 +{
2972 + if (s != NULL) {
2973 + xz_dec_lzma2_end(s->lzma2);
2974 +#ifdef XZ_DEC_BCJ
2975 + xz_dec_bcj_end(s->bcj);
2976 +#endif
2977 + free(s);
2978 + }
2979 +}
2980 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/lzma2.h
2981 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2982 +++ b/xen/common/xz/lzma2.h Wed Mar 09 16:18:58 2011 +0000
2983 @@ -0,0 +1,204 @@
2984 +/*
2985 + * LZMA2 definitions
2986 + *
2987 + * Authors: Lasse Collin <lasse.collin@tukaani.org>
2988 + * Igor Pavlov <http://7-zip.org/>
2989 + *
2990 + * This file has been put into the public domain.
2991 + * You can do whatever you want with this file.
2992 + */
2993 +
2994 +#ifndef XZ_LZMA2_H
2995 +#define XZ_LZMA2_H
2996 +
2997 +/* Range coder constants */
2998 +#define RC_SHIFT_BITS 8
2999 +#define RC_TOP_BITS 24
3000 +#define RC_TOP_VALUE (1 << RC_TOP_BITS)
3001 +#define RC_BIT_MODEL_TOTAL_BITS 11
3002 +#define RC_BIT_MODEL_TOTAL (1 << RC_BIT_MODEL_TOTAL_BITS)
3003 +#define RC_MOVE_BITS 5
3004 +
3005 +/*
3006 + * Maximum number of position states. A position state is the lowest pb
3007 + * number of bits of the current uncompressed offset. In some places there
3008 + * are different sets of probabilities for different position states.
3009 + */
3010 +#define POS_STATES_MAX (1 << 4)
3011 +
3012 +/*
3013 + * This enum is used to track which LZMA symbols have occurred most recently
3014 + * and in which order. This information is used to predict the next symbol.
3015 + *
3016 + * Symbols:
3017 + * - Literal: One 8-bit byte
3018 + * - Match: Repeat a chunk of data at some distance
3019 + * - Long repeat: Multi-byte match at a recently seen distance
3020 + * - Short repeat: One-byte repeat at a recently seen distance
3021 + *
3022 + * The symbol names are in from STATE_oldest_older_previous. REP means
3023 + * either short or long repeated match, and NONLIT means any non-literal.
3024 + */
3025 +enum lzma_state {
3026 + STATE_LIT_LIT,
3027 + STATE_MATCH_LIT_LIT,
3028 + STATE_REP_LIT_LIT,
3029 + STATE_SHORTREP_LIT_LIT,
3030 + STATE_MATCH_LIT,
3031 + STATE_REP_LIT,
3032 + STATE_SHORTREP_LIT,
3033 + STATE_LIT_MATCH,
3034 + STATE_LIT_LONGREP,
3035 + STATE_LIT_SHORTREP,
3036 + STATE_NONLIT_MATCH,
3037 + STATE_NONLIT_REP
3038 +};
3039 +
3040 +/* Total number of states */
3041 +#define STATES 12
3042 +
3043 +/* The lowest 7 states indicate that the previous state was a literal. */
3044 +#define LIT_STATES 7
3045 +
3046 +/* Indicate that the latest symbol was a literal. */
3047 +static inline void INIT lzma_state_literal(enum lzma_state *state)
3048 +{
3049 + if (*state <= STATE_SHORTREP_LIT_LIT)
3050 + *state = STATE_LIT_LIT;
3051 + else if (*state <= STATE_LIT_SHORTREP)
3052 + *state -= 3;
3053 + else
3054 + *state -= 6;
3055 +}
3056 +
3057 +/* Indicate that the latest symbol was a match. */
3058 +static inline void INIT lzma_state_match(enum lzma_state *state)
3059 +{
3060 + *state = *state < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH;
3061 +}
3062 +
3063 +/* Indicate that the latest state was a long repeated match. */
3064 +static inline void INIT lzma_state_long_rep(enum lzma_state *state)
3065 +{
3066 + *state = *state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP;
3067 +}
3068 +
3069 +/* Indicate that the latest symbol was a short match. */
3070 +static inline void INIT lzma_state_short_rep(enum lzma_state *state)
3071 +{
3072 + *state = *state < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP;
3073 +}
3074 +
3075 +/* Test if the previous symbol was a literal. */
3076 +static inline bool_t INIT lzma_state_is_literal(enum lzma_state state)
3077 +{
3078 + return state < LIT_STATES;
3079 +}
3080 +
3081 +/* Each literal coder is divided in three sections:
3082 + * - 0x001-0x0FF: Without match byte
3083 + * - 0x101-0x1FF: With match byte; match bit is 0
3084 + * - 0x201-0x2FF: With match byte; match bit is 1
3085 + *
3086 + * Match byte is used when the previous LZMA symbol was something else than
3087 + * a literal (that is, it was some kind of match).
3088 + */
3089 +#define LITERAL_CODER_SIZE 0x300
3090 +
3091 +/* Maximum number of literal coders */
3092 +#define LITERAL_CODERS_MAX (1 << 4)
3093 +
3094 +/* Minimum length of a match is two bytes. */
3095 +#define MATCH_LEN_MIN 2
3096 +
3097 +/* Match length is encoded with 4, 5, or 10 bits.
3098 + *
3099 + * Length Bits
3100 + * 2-9 4 = Choice=0 + 3 bits
3101 + * 10-17 5 = Choice=1 + Choice2=0 + 3 bits
3102 + * 18-273 10 = Choice=1 + Choice2=1 + 8 bits
3103 + */
3104 +#define LEN_LOW_BITS 3
3105 +#define LEN_LOW_SYMBOLS (1 << LEN_LOW_BITS)
3106 +#define LEN_MID_BITS 3
3107 +#define LEN_MID_SYMBOLS (1 << LEN_MID_BITS)
3108 +#define LEN_HIGH_BITS 8
3109 +#define LEN_HIGH_SYMBOLS (1 << LEN_HIGH_BITS)
3110 +#define LEN_SYMBOLS (LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS + LEN_HIGH_SYMBOLS)
3111 +
3112 +/*
3113 + * Maximum length of a match is 273 which is a result of the encoding
3114 + * described above.
3115 + */
3116 +#define MATCH_LEN_MAX (MATCH_LEN_MIN + LEN_SYMBOLS - 1)
3117 +
3118 +/*
3119 + * Different sets of probabilities are used for match distances that have
3120 + * very short match length: Lengths of 2, 3, and 4 bytes have a separate
3121 + * set of probabilities for each length. The matches with longer length
3122 + * use a shared set of probabilities.
3123 + */
3124 +#define DIST_STATES 4
3125 +
3126 +/*
3127 + * Get the index of the appropriate probability array for decoding
3128 + * the distance slot.
3129 + */
3130 +static inline uint32_t INIT lzma_get_dist_state(uint32_t len)
3131 +{
3132 + return len < DIST_STATES + MATCH_LEN_MIN
3133 + ? len - MATCH_LEN_MIN : DIST_STATES - 1;
3134 +}
3135 +
3136 +/*
3137 + * The highest two bits of a 32-bit match distance are encoded using six bits.
3138 + * This six-bit value is called a distance slot. This way encoding a 32-bit
3139 + * value takes 6-36 bits, larger values taking more bits.
3140 + */
3141 +#define DIST_SLOT_BITS 6
3142 +#define DIST_SLOTS (1 << DIST_SLOT_BITS)
3143 +
3144 +/* Match distances up to 127 are fully encoded using probabilities. Since
3145 + * the highest two bits (distance slot) are always encoded using six bits,
3146 + * the distances 0-3 don't need any additional bits to encode, since the
3147 + * distance slot itself is the same as the actual distance. DIST_MODEL_START
3148 + * indicates the first distance slot where at least one additional bit is
3149 + * needed.
3150 + */
3151 +#define DIST_MODEL_START 4
3152 +
3153 +/*
3154 + * Match distances greater than 127 are encoded in three pieces:
3155 + * - distance slot: the highest two bits
3156 + * - direct bits: 2-26 bits below the highest two bits
3157 + * - alignment bits: four lowest bits
3158 + *
3159 + * Direct bits don't use any probabilities.
3160 + *
3161 + * The distance slot value of 14 is for distances 128-191.
3162 + */
3163 +#define DIST_MODEL_END 14
3164 +
3165 +/* Distance slots that indicate a distance <= 127. */
3166 +#define FULL_DISTANCES_BITS (DIST_MODEL_END / 2)
3167 +#define FULL_DISTANCES (1 << FULL_DISTANCES_BITS)
3168 +
3169 +/*
3170 + * For match distances greater than 127, only the highest two bits and the
3171 + * lowest four bits (alignment) is encoded using probabilities.
3172 + */
3173 +#define ALIGN_BITS 4
3174 +#define ALIGN_SIZE (1 << ALIGN_BITS)
3175 +#define ALIGN_MASK (ALIGN_SIZE - 1)
3176 +
3177 +/* Total number of all probability variables */
3178 +#define PROBS_TOTAL (1846 + LITERAL_CODERS_MAX * LITERAL_CODER_SIZE)
3179 +
3180 +/*
3181 + * LZMA remembers the four most recent match distances. Reusing these
3182 + * distances tends to take less space than re-encoding the actual
3183 + * distance value.
3184 + */
3185 +#define REPS 4
3186 +
3187 +#endif
3188 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/private.h
3189 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3190 +++ b/xen/common/xz/private.h Wed Mar 09 16:18:58 2011 +0000
3191 @@ -0,0 +1,271 @@
3192 +/*
3193 + * Private includes and definitions
3194 + *
3195 + * Author: Lasse Collin <lasse.collin@tukaani.org>
3196 + *
3197 + * This file has been put into the public domain.
3198 + * You can do whatever you want with this file.
3199 + */
3200 +
3201 +#ifndef XZ_PRIVATE_H
3202 +#define XZ_PRIVATE_H
3203 +
3204 +#include <xen/kernel.h>
3205 +#include <asm/byteorder.h>
3206 +#define get_le32(p) le32_to_cpup((const uint32_t *)(p))
3207 +
3208 +#if 1 /* ndef CONFIG_??? */
3209 +static inline u32 INIT get_unaligned_le32(void *p)
3210 +{
3211 + return le32_to_cpup(p);
3212 +}
3213 +
3214 +static inline void INIT put_unaligned_le32(u32 val, void *p)
3215 +{
3216 + *(__force __le32*)p = cpu_to_le32(val);
3217 +}
3218 +#else
3219 +#include <asm/unaligned.h>
3220 +
3221 +static inline u32 INIT get_unaligned_le32(void *p)
3222 +{
3223 + return le32_to_cpu(__get_unaligned(p, 4));
3224 +}
3225 +
3226 +static inline void INIT put_unaligned_le32(u32 val, void *p)
3227 +{
3228 + __put_unaligned(cpu_to_le32(val), p, 4);
3229 +}
3230 +#endif
3231 +
3232 +#define false 0
3233 +#define true 1
3234 +
3235 +/**
3236 + * enum xz_mode - Operation mode
3237 + *
3238 + * @XZ_SINGLE: Single-call mode. This uses less RAM than
3239 + * than multi-call modes, because the LZMA2
3240 + * dictionary doesn't need to be allocated as
3241 + * part of the decoder state. All required data
3242 + * structures are allocated at initialization,
3243 + * so xz_dec_run() cannot return XZ_MEM_ERROR.
3244 + * @XZ_PREALLOC: Multi-call mode with preallocated LZMA2
3245 + * dictionary buffer. All data structures are
3246 + * allocated at initialization, so xz_dec_run()
3247 + * cannot return XZ_MEM_ERROR.
3248 + * @XZ_DYNALLOC: Multi-call mode. The LZMA2 dictionary is
3249 + * allocated once the required size has been
3250 + * parsed from the stream headers. If the
3251 + * allocation fails, xz_dec_run() will return
3252 + * XZ_MEM_ERROR.
3253 + *
3254 + * It is possible to enable support only for a subset of the above
3255 + * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
3256 + * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
3257 + * with support for all operation modes, but the preboot code may
3258 + * be built with fewer features to minimize code size.
3259 + */
3260 +enum xz_mode {
3261 + XZ_SINGLE,
3262 + XZ_PREALLOC,
3263 + XZ_DYNALLOC
3264 +};
3265 +
3266 +/**
3267 + * enum xz_ret - Return codes
3268 + * @XZ_OK: Everything is OK so far. More input or more
3269 + * output space is required to continue. This
3270 + * return code is possible only in multi-call mode
3271 + * (XZ_PREALLOC or XZ_DYNALLOC).
3272 + * @XZ_STREAM_END: Operation finished successfully.
3273 + * @XZ_UNSUPPORTED_CHECK: Integrity check type is not supported. Decoding
3274 + * is still possible in multi-call mode by simply
3275 + * calling xz_dec_run() again.
3276 + * Note that this return value is used only if
3277 + * XZ_DEC_ANY_CHECK was defined at build time,
3278 + * which is not used in the kernel. Unsupported
3279 + * check types return XZ_OPTIONS_ERROR if
3280 + * XZ_DEC_ANY_CHECK was not defined at build time.
3281 + * @XZ_MEM_ERROR: Allocating memory failed. This return code is
3282 + * possible only if the decoder was initialized
3283 + * with XZ_DYNALLOC. The amount of memory that was
3284 + * tried to be allocated was no more than the
3285 + * dict_max argument given to xz_dec_init().
3286 + * @XZ_MEMLIMIT_ERROR: A bigger LZMA2 dictionary would be needed than
3287 + * allowed by the dict_max argument given to
3288 + * xz_dec_init(). This return value is possible
3289 + * only in multi-call mode (XZ_PREALLOC or
3290 + * XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
3291 + * ignores the dict_max argument.
3292 + * @XZ_FORMAT_ERROR: File format was not recognized (wrong magic
3293 + * bytes).
3294 + * @XZ_OPTIONS_ERROR: This implementation doesn't support the requested
3295 + * compression options. In the decoder this means
3296 + * that the header CRC32 matches, but the header
3297 + * itself specifies something that we don't support.
3298 + * @XZ_DATA_ERROR: Compressed data is corrupt.
3299 + * @XZ_BUF_ERROR: Cannot make any progress. Details are slightly
3300 + * different between multi-call and single-call
3301 + * mode; more information below.
3302 + *
3303 + * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
3304 + * to XZ code cannot consume any input and cannot produce any new output.
3305 + * This happens when there is no new input available, or the output buffer
3306 + * is full while at least one output byte is still pending. Assuming your
3307 + * code is not buggy, you can get this error only when decoding a compressed
3308 + * stream that is truncated or otherwise corrupt.
3309 + *
3310 + * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
3311 + * is too small or the compressed input is corrupt in a way that makes the
3312 + * decoder produce more output than the caller expected. When it is
3313 + * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
3314 + * is used instead of XZ_BUF_ERROR.
3315 + */
3316 +enum xz_ret {
3317 + XZ_OK,
3318 + XZ_STREAM_END,
3319 + XZ_UNSUPPORTED_CHECK,
3320 + XZ_MEM_ERROR,
3321 + XZ_MEMLIMIT_ERROR,
3322 + XZ_FORMAT_ERROR,
3323 + XZ_OPTIONS_ERROR,
3324 + XZ_DATA_ERROR,
3325 + XZ_BUF_ERROR
3326 +};
3327 +
3328 +/**
3329 + * struct xz_buf - Passing input and output buffers to XZ code
3330 + * @in: Beginning of the input buffer. This may be NULL if and only
3331 + * if in_pos is equal to in_size.
3332 + * @in_pos: Current position in the input buffer. This must not exceed
3333 + * in_size.
3334 + * @in_size: Size of the input buffer
3335 + * @out: Beginning of the output buffer. This may be NULL if and only
3336 + * if out_pos is equal to out_size.
3337 + * @out_pos: Current position in the output buffer. This must not exceed
3338 + * out_size.
3339 + * @out_size: Size of the output buffer
3340 + *
3341 + * Only the contents of the output buffer from out[out_pos] onward, and
3342 + * the variables in_pos and out_pos are modified by the XZ code.
3343 + */
3344 +struct xz_buf {
3345 + const uint8_t *in;
3346 + size_t in_pos;
3347 + size_t in_size;
3348 +
3349 + uint8_t *out;
3350 + size_t out_pos;
3351 + size_t out_size;
3352 +};
3353 +
3354 +/**
3355 + * struct xz_dec - Opaque type to hold the XZ decoder state
3356 + */
3357 +struct xz_dec;
3358 +
3359 +/* If no specific decoding mode is requested, enable support for all modes. */
3360 +#if !defined(XZ_DEC_SINGLE) && !defined(XZ_DEC_PREALLOC) \
3361 + && !defined(XZ_DEC_DYNALLOC)
3362 +# define XZ_DEC_SINGLE
3363 +# define XZ_DEC_PREALLOC
3364 +# define XZ_DEC_DYNALLOC
3365 +#endif
3366 +
3367 +/*
3368 + * The DEC_IS_foo(mode) macros are used in "if" statements. If only some
3369 + * of the supported modes are enabled, these macros will evaluate to true or
3370 + * false at compile time and thus allow the compiler to omit unneeded code.
3371 + */
3372 +#ifdef XZ_DEC_SINGLE
3373 +# define DEC_IS_SINGLE(mode) ((mode) == XZ_SINGLE)
3374 +#else
3375 +# define DEC_IS_SINGLE(mode) (false)
3376 +#endif
3377 +
3378 +#ifdef XZ_DEC_PREALLOC
3379 +# define DEC_IS_PREALLOC(mode) ((mode) == XZ_PREALLOC)
3380 +#else
3381 +# define DEC_IS_PREALLOC(mode) (false)
3382 +#endif
3383 +
3384 +#ifdef XZ_DEC_DYNALLOC
3385 +# define DEC_IS_DYNALLOC(mode) ((mode) == XZ_DYNALLOC)
3386 +#else
3387 +# define DEC_IS_DYNALLOC(mode) (false)
3388 +#endif
3389 +
3390 +#if !defined(XZ_DEC_SINGLE)
3391 +# define DEC_IS_MULTI(mode) (true)
3392 +#elif defined(XZ_DEC_PREALLOC) || defined(XZ_DEC_DYNALLOC)
3393 +# define DEC_IS_MULTI(mode) ((mode) != XZ_SINGLE)
3394 +#else
3395 +# define DEC_IS_MULTI(mode) (false)
3396 +#endif
3397 +
3398 +/*
3399 + * If any of the BCJ filter decoders are wanted, define XZ_DEC_BCJ.
3400 + * XZ_DEC_BCJ is used to enable generic support for BCJ decoders.
3401 + */
3402 +#ifndef XZ_DEC_BCJ
3403 +# if defined(XZ_DEC_X86) || defined(XZ_DEC_POWERPC) \
3404 + || defined(XZ_DEC_IA64) || defined(XZ_DEC_ARM) \
3405 + || defined(XZ_DEC_ARM) || defined(XZ_DEC_ARMTHUMB) \
3406 + || defined(XZ_DEC_SPARC)
3407 +# define XZ_DEC_BCJ
3408 +# endif
3409 +#endif
3410 +
3411 +/*
3412 + * Allocate memory for LZMA2 decoder. xz_dec_lzma2_reset() must be used
3413 + * before calling xz_dec_lzma2_run().
3414 + */
3415 +XZ_EXTERN struct xz_dec_lzma2 *xz_dec_lzma2_create(enum xz_mode mode,
3416 + uint32_t dict_max);
3417 +
3418 +/*
3419 + * Decode the LZMA2 properties (one byte) and reset the decoder. Return
3420 + * XZ_OK on success, XZ_MEMLIMIT_ERROR if the preallocated dictionary is not
3421 + * big enough, and XZ_OPTIONS_ERROR if props indicates something that this
3422 + * decoder doesn't support.
3423 + */
3424 +XZ_EXTERN enum xz_ret xz_dec_lzma2_reset(struct xz_dec_lzma2 *s,
3425 + uint8_t props);
3426 +
3427 +/* Decode raw LZMA2 stream from b->in to b->out. */
3428 +XZ_EXTERN enum xz_ret xz_dec_lzma2_run(struct xz_dec_lzma2 *s,
3429 + struct xz_buf *b);
3430 +
3431 +/* Free the memory allocated for the LZMA2 decoder. */
3432 +XZ_EXTERN void xz_dec_lzma2_end(struct xz_dec_lzma2 *s);
3433 +
3434 +#ifdef XZ_DEC_BCJ
3435 +/*
3436 + * Allocate memory for BCJ decoders. xz_dec_bcj_reset() must be used before
3437 + * calling xz_dec_bcj_run().
3438 + */
3439 +XZ_EXTERN struct xz_dec_bcj *xz_dec_bcj_create(bool_t single_call);
3440 +
3441 +/*
3442 + * Decode the Filter ID of a BCJ filter. This implementation doesn't
3443 + * support custom start offsets, so no decoding of Filter Properties
3444 + * is needed. Returns XZ_OK if the given Filter ID is supported.
3445 + * Otherwise XZ_OPTIONS_ERROR is returned.
3446 + */
3447 +XZ_EXTERN enum xz_ret xz_dec_bcj_reset(struct xz_dec_bcj *s, uint8_t id);
3448 +
3449 +/*
3450 + * Decode raw BCJ + LZMA2 stream. This must be used only if there actually is
3451 + * a BCJ filter in the chain. If the chain has only LZMA2, xz_dec_lzma2_run()
3452 + * must be called directly.
3453 + */
3454 +XZ_EXTERN enum xz_ret xz_dec_bcj_run(struct xz_dec_bcj *s,
3455 + struct xz_dec_lzma2 *lzma2,
3456 + struct xz_buf *b);
3457 +
3458 +/* Free the memory allocated for the BCJ filters. */
3459 +#define xz_dec_bcj_end(s) free(s)
3460 +#endif
3461 +
3462 +#endif
3463 diff -r d428fa67abaa -r 9eb9948904cd xen/common/xz/stream.h
3464 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3465 +++ b/xen/common/xz/stream.h Wed Mar 09 16:18:58 2011 +0000
3466 @@ -0,0 +1,55 @@
3467 +/*
3468 + * Definitions for handling the .xz file format
3469 + *
3470 + * Author: Lasse Collin <lasse.collin@tukaani.org>
3471 + *
3472 + * This file has been put into the public domain.
3473 + * You can do whatever you want with this file.
3474 + */
3475 +
3476 +#ifndef XZ_STREAM_H
3477 +#define XZ_STREAM_H
3478 +
3479 +/*
3480 + * See the .xz file format specification at
3481 + * http://tukaani.org/xz/xz-file-format.txt
3482 + * to understand the container format.
3483 + */
3484 +
3485 +#define STREAM_HEADER_SIZE 12
3486 +
3487 +#define HEADER_MAGIC "\3757zXZ"
3488 +#define HEADER_MAGIC_SIZE 6
3489 +
3490 +#define FOOTER_MAGIC "YZ"
3491 +#define FOOTER_MAGIC_SIZE 2
3492 +
3493 +/*
3494 + * Variable-length integer can hold a 63-bit unsigned integer or a special
3495 + * value indicating that the value is unknown.
3496 + *
3497 + * Experimental: vli_type can be defined to uint32_t to save a few bytes
3498 + * in code size (no effect on speed). Doing so limits the uncompressed and
3499 + * compressed size of the file to less than 256 MiB and may also weaken
3500 + * error detection slightly.
3501 + */
3502 +typedef uint64_t vli_type;
3503 +
3504 +#define VLI_MAX ((vli_type)-1 / 2)
3505 +#define VLI_UNKNOWN ((vli_type)-1)
3506 +
3507 +/* Maximum encoded size of a VLI */
3508 +#define VLI_BYTES_MAX (sizeof(vli_type) * 8 / 7)
3509 +
3510 +/* Integrity Check types */
3511 +enum xz_check {
3512 + XZ_CHECK_NONE = 0,
3513 + XZ_CHECK_CRC32 = 1,
3514 + XZ_CHECK_CRC64 = 4,
3515 + XZ_CHECK_SHA256 = 10
3516 +};
3517 +
3518 +/* Maximum possible Check ID */
3519 +#define XZ_CHECK_MAX 15
3520 +
3521 +#endif
3522 diff -r d428fa67abaa -r 9eb9948904cd xen/include/xen/decompress.h
3523 --- a/xen/include/xen/decompress.h Wed Mar 09 16:17:26 2011 +0000
3524 +++ b/xen/include/xen/decompress.h Wed Mar 09 16:18:58 2011 +0000
3525 @@ -31,7 +31,7 @@
3526 * dependent).
3527 */
3528
3529 -decompress_fn bunzip2, unlzma, unlzo;
3530 +decompress_fn bunzip2, unxz, unlzma, unlzo;
3531
3532 int decompress(void *inbuf, unsigned int len, void *outbuf);
3533
3534

  ViewVC Help
Powered by ViewVC 1.1.28