diff options
author | Chris Zankel <czankel@tensilica.com> | 2005-06-24 01:01:12 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-06-24 03:05:21 -0400 |
commit | 4bedea94545165364618d403d03b61d797acba0b (patch) | |
tree | 04626bb71e0fb5ea5c5d5aa4fedc813301bea6a6 | |
parent | 8e1a6dd2fddcc73c9e933758361e3d9c076c688a (diff) |
[PATCH] xtensa: Architecture support for Tensilica Xtensa Part 2
The attached patches provides part 2 of an architecture implementation for the
Tensilica Xtensa CPU series.
Signed-off-by: Chris Zankel <chris@zankel.net>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
-rw-r--r-- | arch/xtensa/boot/Makefile | 37 | ||||
-rw-r--r-- | arch/xtensa/boot/boot-elf/Makefile | 52 | ||||
-rw-r--r-- | arch/xtensa/boot/boot-elf/boot.ld | 71 | ||||
-rw-r--r-- | arch/xtensa/boot/boot-elf/bootstrap.S | 37 | ||||
-rw-r--r-- | arch/xtensa/boot/boot-redboot/Makefile | 35 | ||||
-rw-r--r-- | arch/xtensa/boot/boot-redboot/boot.ld | 66 | ||||
-rw-r--r-- | arch/xtensa/boot/boot-redboot/bootstrap.S | 246 | ||||
-rw-r--r-- | arch/xtensa/boot/include/zlib.h | 433 | ||||
-rw-r--r-- | arch/xtensa/boot/lib/Makefile | 6 | ||||
-rw-r--r-- | arch/xtensa/boot/lib/memcpy.S | 36 | ||||
-rw-r--r-- | arch/xtensa/boot/lib/zlib.c | 2150 | ||||
-rw-r--r-- | arch/xtensa/boot/lib/zmem.c | 87 | ||||
-rw-r--r-- | arch/xtensa/boot/ramdisk/Makefile | 23 |
13 files changed, 3279 insertions, 0 deletions
diff --git a/arch/xtensa/boot/Makefile b/arch/xtensa/boot/Makefile new file mode 100644 index 000000000000..260f456ccf0b --- /dev/null +++ b/arch/xtensa/boot/Makefile | |||
@@ -0,0 +1,37 @@ | |||
1 | # | ||
2 | # arch/xtensa/boot/Makefile | ||
3 | # | ||
4 | # This file is subject to the terms and conditions of the GNU General Public | ||
5 | # License. See the file "COPYING" in the main directory of this archive | ||
6 | # for more details. | ||
7 | # | ||
8 | # | ||
9 | |||
10 | |||
11 | CFLAGS += -fno-builtin -Iarch/$(ARCH)/boot/include | ||
12 | HOSTFLAGS += -Iarch/$(ARCH)/boot/include | ||
13 | |||
14 | BIG_ENDIAN := $(shell echo -e "\#ifdef __XTENSA_EL__\nint little;\n\#else\nint big;\n\#endif" | $(CC) -E -|grep -c big) | ||
15 | |||
16 | |||
17 | export CFLAGS | ||
18 | export AFLAGS | ||
19 | export BIG_ENDIAN | ||
20 | |||
21 | # Subdirs for the boot loader(s) | ||
22 | |||
23 | bootdir-$(CONFIG_XTENSA_PLATFORM_ISS) += boot-elf | ||
24 | bootdir-$(CONFIG_XTENSA_PLATFORM_XT2000) += boot-redboot boot-elf | ||
25 | |||
26 | subdir-y := lib/ | ||
27 | |||
28 | subdir-y += boot-elf/ boot-redboot/ | ||
29 | |||
30 | zImage zImage.initrd Image Image.initrd: $(bootdir-y) | ||
31 | |||
32 | $(bootdir-y): $(addprefix $(obj)/,$(subdir-y)) \ | ||
33 | $(addprefix $(obj)/,$(host-progs)) | ||
34 | $(Q)$(MAKE) $(build)=$(obj)/$@ $(MAKECMDGOALS) | ||
35 | |||
36 | |||
37 | |||
diff --git a/arch/xtensa/boot/boot-elf/Makefile b/arch/xtensa/boot/boot-elf/Makefile new file mode 100644 index 000000000000..f6ef6a369667 --- /dev/null +++ b/arch/xtensa/boot/boot-elf/Makefile | |||
@@ -0,0 +1,52 @@ | |||
1 | # | ||
2 | # This file is subject to the terms and conditions of the GNU General Public | ||
3 | # License. See the file "COPYING" in the main directory of this archive | ||
4 | # for more details. | ||
5 | # | ||
6 | |||
7 | GZIP = gzip | ||
8 | GZIP_FLAGS = -v9fc | ||
9 | |||
10 | ifeq ($(BIG_ENDIAN),1) | ||
11 | OBJCOPY_ARGS := -O elf32-xtensa-be | ||
12 | else | ||
13 | OBJCOPY_ARGS := -O elf32-xtensa-le | ||
14 | endif | ||
15 | |||
16 | export OBJCOPY_ARGS | ||
17 | |||
18 | boot-y := bootstrap.o | ||
19 | |||
20 | OBJS := $(addprefix $(obj)/,$(boot-y)) | ||
21 | |||
22 | Image: vmlinux $(OBJS) | ||
23 | $(OBJCOPY) --strip-all -R .comment -R .xt.insn -O binary \ | ||
24 | vmlinux vmlinux.tmp | ||
25 | $(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \ | ||
26 | --add-section image=vmlinux.tmp \ | ||
27 | --set-section-flags image=contents,alloc,load,load,data \ | ||
28 | $(OBJS) $@.tmp | ||
29 | $(LD) $(LDFLAGS) $(LDFLAGS_vmlinux) \ | ||
30 | -T arch/$(ARCH)/boot/boot-elf/boot.ld \ | ||
31 | -o arch/$(ARCH)/boot/$@.elf $@.tmp | ||
32 | rm -f $@.tmp vmlinux.tmp | ||
33 | |||
34 | Image.initrd: vmlinux $(OBJS) | ||
35 | $(OBJCOPY) --strip-all -R .comment -R .xt.insn -O binary \ | ||
36 | --add-section .initrd=arch/$(ARCH)/boot/ramdisk \ | ||
37 | --set-section-flags .initrd=contents,alloc,load,load,data \ | ||
38 | vmlinux vmlinux.tmp | ||
39 | $(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \ | ||
40 | --add-section image=vmlinux.tmp \ | ||
41 | --set-section-flags image=contents,alloc,load,load,data \ | ||
42 | $(OBJS) $@.tmp | ||
43 | $(LD) $(LDFLAGS) $(LDFLAGS_vmlinux) \ | ||
44 | -T arch/$(ARCH)/boot/boot-elf/boot.ld \ | ||
45 | -o arch/$(ARCH)/boot/$@.elf $@.tmp | ||
46 | rm -f $@.tmp vmlinux.tmp | ||
47 | |||
48 | |||
49 | zImage: Image | ||
50 | |||
51 | zImage.initrd: Image.initrd | ||
52 | |||
diff --git a/arch/xtensa/boot/boot-elf/boot.ld b/arch/xtensa/boot/boot-elf/boot.ld new file mode 100644 index 000000000000..4ab06a0a7a6b --- /dev/null +++ b/arch/xtensa/boot/boot-elf/boot.ld | |||
@@ -0,0 +1,71 @@ | |||
1 | OUTPUT_ARCH(xtensa) | ||
2 | |||
3 | SECTIONS | ||
4 | { | ||
5 | .start 0xD0000000 : { *(.start) } | ||
6 | |||
7 | .text 0xD0000000: | ||
8 | { | ||
9 | __reloc_start = . ; | ||
10 | _text_start = . ; | ||
11 | *(.literal .text.literal .text) | ||
12 | _text_end = . ; | ||
13 | } | ||
14 | |||
15 | .rodata ALIGN(0x04): | ||
16 | { | ||
17 | *(.rodata) | ||
18 | *(.rodata1) | ||
19 | } | ||
20 | |||
21 | .data ALIGN(0x04): | ||
22 | { | ||
23 | *(.data) | ||
24 | *(.data1) | ||
25 | *(.sdata) | ||
26 | *(.sdata2) | ||
27 | *(.got.plt) | ||
28 | *(.got) | ||
29 | *(.dynamic) | ||
30 | } | ||
31 | |||
32 | __reloc_end = . ; | ||
33 | |||
34 | .initrd ALIGN(0x10) : | ||
35 | { | ||
36 | boot_initrd_start = . ; | ||
37 | *(.initrd) | ||
38 | boot_initrd_end = .; | ||
39 | } | ||
40 | |||
41 | . = ALIGN(0x10); | ||
42 | __image_load = . ; | ||
43 | .image 0xd0001000: | ||
44 | { | ||
45 | _image_start = .; | ||
46 | *(image) | ||
47 | . = (. + 3) & ~ 3; | ||
48 | _image_end = . ; | ||
49 | } | ||
50 | |||
51 | |||
52 | .bss ((LOADADDR(.image) + SIZEOF(.image) + 3) & ~ 3): | ||
53 | { | ||
54 | __bss_start = .; | ||
55 | *(.sbss) | ||
56 | *(.scommon) | ||
57 | *(.dynbss) | ||
58 | *(.bss) | ||
59 | __bss_end = .; | ||
60 | } | ||
61 | _end = .; | ||
62 | _param_start = .; | ||
63 | |||
64 | .ResetVector.text 0xfe000020 : | ||
65 | { | ||
66 | *(.ResetVector.text) | ||
67 | } | ||
68 | |||
69 | |||
70 | PROVIDE (end = .); | ||
71 | } | ||
diff --git a/arch/xtensa/boot/boot-elf/bootstrap.S b/arch/xtensa/boot/boot-elf/bootstrap.S new file mode 100644 index 000000000000..7cba94abdab8 --- /dev/null +++ b/arch/xtensa/boot/boot-elf/bootstrap.S | |||
@@ -0,0 +1,37 @@ | |||
1 | |||
2 | #include <xtensa/config/specreg.h> | ||
3 | #include <xtensa/config/core.h> | ||
4 | |||
5 | #include <linux/config.h> | ||
6 | #include <asm/bootparam.h> | ||
7 | |||
8 | |||
9 | /* ResetVector | ||
10 | */ | ||
11 | .section .ResetVector.text, "ax" | ||
12 | .global _ResetVector | ||
13 | _ResetVector: | ||
14 | _j reset | ||
15 | .align 4 | ||
16 | RomInitAddr: | ||
17 | .word 0xd0001000 | ||
18 | RomBootParam: | ||
19 | .word _bootparam | ||
20 | reset: | ||
21 | l32r a0, RomInitAddr | ||
22 | l32r a2, RomBootParam | ||
23 | movi a3, 0 | ||
24 | movi a4, 0 | ||
25 | jx a0 | ||
26 | |||
27 | .align 4 | ||
28 | .section .bootstrap.data, "aw" | ||
29 | |||
30 | .globl _bootparam | ||
31 | _bootparam: | ||
32 | .short BP_TAG_FIRST | ||
33 | .short 4 | ||
34 | .long BP_VERSION | ||
35 | .short BP_TAG_LAST | ||
36 | .short 0 | ||
37 | .long 0 | ||
diff --git a/arch/xtensa/boot/boot-redboot/Makefile b/arch/xtensa/boot/boot-redboot/Makefile new file mode 100644 index 000000000000..ca8a68bc8472 --- /dev/null +++ b/arch/xtensa/boot/boot-redboot/Makefile | |||
@@ -0,0 +1,35 @@ | |||
1 | # | ||
2 | # This file is subject to the terms and conditions of the GNU General Public | ||
3 | # License. See the file "COPYING" in the main directory of this archive | ||
4 | # for more details. | ||
5 | # | ||
6 | |||
7 | GZIP = gzip | ||
8 | GZIP_FLAGS = -v9fc | ||
9 | ifeq ($(BIG_ENDIAN),1) | ||
10 | OBJCOPY_ARGS := -O elf32-xtensa-be | ||
11 | else | ||
12 | OBJCOPY_ARGS := -O elf32-xtensa-le | ||
13 | endif | ||
14 | |||
15 | LD_ARGS = -T $(obj)/boot.ld | ||
16 | |||
17 | boot-y := bootstrap.o | ||
18 | |||
19 | OBJS := $(addprefix $(obj)/,$(boot-y)) | ||
20 | LIBS := arch/$(ARCH)/boot/lib/lib.a arch/$(ARCH)/lib/lib.a | ||
21 | |||
22 | LIBGCC := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) | ||
23 | |||
24 | zImage: vmlinux $(OBJS) $(LIBS) | ||
25 | $(OBJCOPY) --strip-all -R .comment -R .xt.insn -O binary \ | ||
26 | $(TOPDIR)/vmlinux vmlinux.tmp | ||
27 | gzip -vf9 vmlinux.tmp | ||
28 | $(OBJCOPY) $(OBJCOPY_ARGS) -R .comment \ | ||
29 | --add-section image=vmlinux.tmp.gz \ | ||
30 | --set-section-flags image=contents,alloc,load,load,data \ | ||
31 | $(OBJS) $@.tmp | ||
32 | $(LD) $(LD_ARGS) -o $@.elf $@.tmp $(LIBS) -L/xtensa-elf/lib $(LIBGCC) | ||
33 | $(OBJCOPY) -S -O binary $@.elf arch/$(ARCH)/boot/images/$@.redboot | ||
34 | # rm -f $@.tmp $@.elf vmlinux.tmp.gz | ||
35 | |||
diff --git a/arch/xtensa/boot/boot-redboot/boot.ld b/arch/xtensa/boot/boot-redboot/boot.ld new file mode 100644 index 000000000000..65b726410e8a --- /dev/null +++ b/arch/xtensa/boot/boot-redboot/boot.ld | |||
@@ -0,0 +1,66 @@ | |||
1 | OUTPUT_ARCH(xtensa) | ||
2 | |||
3 | SECTIONS | ||
4 | { | ||
5 | .start 0xD0200000 : { *(.start) } | ||
6 | |||
7 | .text : | ||
8 | { | ||
9 | __reloc_start = . ; | ||
10 | _text_start = . ; | ||
11 | *(.literal .text.literal .text) | ||
12 | _text_end = . ; | ||
13 | } | ||
14 | |||
15 | .rodata ALIGN(0x04): | ||
16 | { | ||
17 | *(.rodata) | ||
18 | *(.rodata1) | ||
19 | } | ||
20 | |||
21 | .data ALIGN(0x04): | ||
22 | { | ||
23 | *(.data) | ||
24 | *(.data1) | ||
25 | *(.sdata) | ||
26 | *(.sdata2) | ||
27 | *(.got.plt) | ||
28 | *(.got) | ||
29 | *(.dynamic) | ||
30 | } | ||
31 | |||
32 | __reloc_end = . ; | ||
33 | |||
34 | .initrd ALIGN(0x10) : | ||
35 | { | ||
36 | boot_initrd_start = . ; | ||
37 | *(.initrd) | ||
38 | boot_initrd_end = .; | ||
39 | } | ||
40 | |||
41 | . = ALIGN(0x10); | ||
42 | __image_load = . ; | ||
43 | .image 0xd0001000: AT(__image_load) | ||
44 | { | ||
45 | _image_start = .; | ||
46 | *(image) | ||
47 | . = (. + 3) & ~ 3; | ||
48 | _image_end = . ; | ||
49 | } | ||
50 | |||
51 | |||
52 | .bss ((LOADADDR(.image) + SIZEOF(.image) + 3) & ~ 3): | ||
53 | { | ||
54 | __bss_start = .; | ||
55 | *(.sbss) | ||
56 | *(.scommon) | ||
57 | *(.dynbss) | ||
58 | *(.bss) | ||
59 | __bss_end = .; | ||
60 | } | ||
61 | _end = .; | ||
62 | _param_start = .; | ||
63 | |||
64 | |||
65 | PROVIDE (end = .); | ||
66 | } | ||
diff --git a/arch/xtensa/boot/boot-redboot/bootstrap.S b/arch/xtensa/boot/boot-redboot/bootstrap.S new file mode 100644 index 000000000000..ee636b0da81c --- /dev/null +++ b/arch/xtensa/boot/boot-redboot/bootstrap.S | |||
@@ -0,0 +1,246 @@ | |||
1 | |||
2 | #define _ASMLANGUAGE | ||
3 | #include <xtensa/config/specreg.h> | ||
4 | #include <xtensa/config/core.h> | ||
5 | #include <xtensa/cacheasm.h> | ||
6 | |||
7 | /* | ||
8 | * RB-Data: RedBoot data/bss | ||
9 | * P: Boot-Parameters | ||
10 | * L: Kernel-Loader | ||
11 | * | ||
12 | * The Linux-Kernel image including the loader must be loaded | ||
13 | * to a position so that the kernel and the boot parameters | ||
14 | * can fit in the space before the load address. | ||
15 | * ______________________________________________________ | ||
16 | * |_RB-Data_|_P_|__________|_L_|___Linux-Kernel___|______| | ||
17 | * ^ | ||
18 | * ^ Load address | ||
19 | * ______________________________________________________ | ||
20 | * |___Linux-Kernel___|_P_|_L_|___________________________| | ||
21 | * | ||
22 | * The loader copies the parameter to the position that will | ||
23 | * be the end of the kernel and itself to the end of the | ||
24 | * parameter list. | ||
25 | */ | ||
26 | |||
27 | /* Make sure we have enough space for the 'uncompressor' */ | ||
28 | |||
29 | #define STACK_SIZE 32768 | ||
30 | #define HEAP_SIZE (131072*4) | ||
31 | |||
32 | # a2: Parameter list | ||
33 | # a3: Size of parameter list | ||
34 | |||
35 | .section .start, "ax" | ||
36 | |||
37 | .globl __start | ||
38 | /* this must be the first byte of the loader! */ | ||
39 | __start: | ||
40 | entry sp, 32 # we do not intend to return | ||
41 | _call0 _start | ||
42 | __start_a0: | ||
43 | .align 4 | ||
44 | |||
45 | .section .text, "ax" | ||
46 | .begin literal_prefix .text | ||
47 | |||
48 | /* put literals in here! */ | ||
49 | |||
50 | .globl _start | ||
51 | _start: | ||
52 | |||
53 | /* 'reset' window registers */ | ||
54 | |||
55 | movi a4, 1 | ||
56 | wsr a4, PS | ||
57 | rsync | ||
58 | |||
59 | rsr a5, WINDOWBASE | ||
60 | ssl a5 | ||
61 | sll a4, a4 | ||
62 | wsr a4, WINDOWSTART | ||
63 | rsync | ||
64 | |||
65 | movi a4, 0x00040000 | ||
66 | wsr a4, PS | ||
67 | rsync | ||
68 | |||
69 | /* copy the loader to its address | ||
70 | * Note: The loader itself is a very small piece, so we assume we | ||
71 | * don't partially overlap. We also assume (even more important) | ||
72 | * that the kernel image is out of the way. Usually, when the | ||
73 | * load address of this image is not at an arbitrary address, | ||
74 | * but aligned to some 10K's we shouldn't overlap. | ||
75 | */ | ||
76 | |||
77 | /* Note: The assembler cannot relax "addi a0, a0, ..." to an | ||
78 | l32r, so we load to a4 first. */ | ||
79 | |||
80 | addi a4, a0, __start - __start_a0 | ||
81 | mov a0, a4 | ||
82 | movi a4, __start | ||
83 | movi a5, __reloc_end | ||
84 | |||
85 | # a0: address where this code has been loaded | ||
86 | # a4: compiled address of __start | ||
87 | # a5: compiled end address | ||
88 | |||
89 | mov.n a7, a0 | ||
90 | mov.n a8, a4 | ||
91 | |||
92 | 1: | ||
93 | l32i a10, a7, 0 | ||
94 | l32i a11, a7, 4 | ||
95 | s32i a10, a8, 0 | ||
96 | s32i a11, a8, 4 | ||
97 | l32i a10, a7, 8 | ||
98 | l32i a11, a7, 12 | ||
99 | s32i a10, a8, 8 | ||
100 | s32i a11, a8, 12 | ||
101 | addi a8, a8, 16 | ||
102 | addi a7, a7, 16 | ||
103 | blt a8, a5, 1b | ||
104 | |||
105 | |||
106 | /* We have to flush and invalidate the caches here before we jump. */ | ||
107 | |||
108 | #if XCHAL_DCACHE_IS_WRITEBACK | ||
109 | dcache_writeback_all a5, a6 | ||
110 | #endif | ||
111 | icache_invalidate_all a5, a6 | ||
112 | |||
113 | movi a11, _reloc | ||
114 | jx a11 | ||
115 | |||
116 | .globl _reloc | ||
117 | _reloc: | ||
118 | |||
119 | /* RedBoot is now at the end of the memory, so we don't have | ||
120 | * to copy the parameter list. Keep the code around; in case | ||
121 | * we need it again. */ | ||
122 | #if 0 | ||
123 | # a0: load address | ||
124 | # a2: start address of parameter list | ||
125 | # a3: length of parameter list | ||
126 | # a4: __start | ||
127 | |||
128 | /* copy the parameter list out of the way */ | ||
129 | |||
130 | movi a6, _param_start | ||
131 | add a3, a2, a3 | ||
132 | 2: | ||
133 | l32i a8, a2, 0 | ||
134 | s32i a8, a6, 0 | ||
135 | addi a2, a2, 4 | ||
136 | addi a6, a6, 4 | ||
137 | blt a2, a3, 2b | ||
138 | #endif | ||
139 | |||
140 | /* clear BSS section */ | ||
141 | movi a6, __bss_start | ||
142 | movi a7, __bss_end | ||
143 | movi.n a5, 0 | ||
144 | 3: | ||
145 | s32i a5, a6, 0 | ||
146 | addi a6, a6, 4 | ||
147 | blt a6, a7, 3b | ||
148 | |||
149 | movi a5, -16 | ||
150 | movi a1, _stack + STACK_SIZE | ||
151 | and a1, a1, a5 | ||
152 | |||
153 | /* Uncompress the kernel */ | ||
154 | |||
155 | # a0: load address | ||
156 | # a2: boot parameter | ||
157 | # a4: __start | ||
158 | |||
159 | movi a3, __image_load | ||
160 | sub a4, a3, a4 | ||
161 | add a8, a0, a4 | ||
162 | |||
163 | # a1 Stack | ||
164 | # a8(a4) Load address of the image | ||
165 | |||
166 | movi a6, _image_start | ||
167 | movi a10, _image_end | ||
168 | movi a7, 0x1000000 | ||
169 | sub a11, a10, a6 | ||
170 | movi a9, complen | ||
171 | s32i a11, a9, 0 | ||
172 | |||
173 | movi a0, 0 | ||
174 | |||
175 | # a6 destination | ||
176 | # a7 maximum size of destination | ||
177 | # a8 source | ||
178 | # a9 ptr to length | ||
179 | |||
180 | .extern gunzip | ||
181 | movi a4, gunzip | ||
182 | beqz a4, 1f | ||
183 | |||
184 | callx4 a4 | ||
185 | |||
186 | j 2f | ||
187 | |||
188 | |||
189 | # a6 destination start | ||
190 | # a7 maximum size of destination | ||
191 | # a8 source start | ||
192 | # a9 ptr to length | ||
193 | # a10 destination end | ||
194 | |||
195 | 1: | ||
196 | l32i a9, a8, 0 | ||
197 | l32i a11, a8, 4 | ||
198 | s32i a9, a6, 0 | ||
199 | s32i a11, a6, 4 | ||
200 | l32i a9, a8, 8 | ||
201 | l32i a11, a8, 12 | ||
202 | s32i a9, a6, 8 | ||
203 | s32i a11, a6, 12 | ||
204 | addi a6, a6, 16 | ||
205 | addi a8, a8, 16 | ||
206 | blt a6, a10, 1b | ||
207 | |||
208 | |||
209 | /* jump to the kernel */ | ||
210 | 2: | ||
211 | #if XCHAL_DCACHE_IS_WRITEBACK | ||
212 | dcache_writeback_all a5, a6 | ||
213 | #endif | ||
214 | icache_invalidate_all a5, a6 | ||
215 | |||
216 | movi a5, __start | ||
217 | movi a3, boot_initrd_start | ||
218 | movi a4, boot_initrd_end | ||
219 | sub a3, a3, a5 | ||
220 | sub a4, a4, a5 | ||
221 | add a3, a0, a3 | ||
222 | add a4, a0, a4 | ||
223 | |||
224 | # a2 Boot parameter list | ||
225 | # a3 initrd_start (virtual load address) | ||
226 | # a4 initrd_end (virtual load address) | ||
227 | |||
228 | movi a0, _image_start | ||
229 | jx a0 | ||
230 | |||
231 | .align 16 | ||
232 | .data | ||
233 | .globl avail_ram | ||
234 | avail_ram: | ||
235 | .long _heap | ||
236 | .globl end_avail | ||
237 | end_avail: | ||
238 | .long _heap + HEAP_SIZE | ||
239 | |||
240 | .comm _stack, STACK_SIZE | ||
241 | .comm _heap, HEAP_SIZE | ||
242 | |||
243 | .globl end_avail | ||
244 | .comm complen, 4 | ||
245 | |||
246 | .end literal_prefix | ||
diff --git a/arch/xtensa/boot/include/zlib.h b/arch/xtensa/boot/include/zlib.h new file mode 100644 index 000000000000..ea29b6237852 --- /dev/null +++ b/arch/xtensa/boot/include/zlib.h | |||
@@ -0,0 +1,433 @@ | |||
1 | /* | ||
2 | * BK Id: SCCS/s.zlib.h 1.8 05/18/01 15:17:23 cort | ||
3 | */ | ||
4 | /* | ||
5 | * This file is derived from zlib.h and zconf.h from the zlib-0.95 | ||
6 | * distribution by Jean-loup Gailly and Mark Adler, with some additions | ||
7 | * by Paul Mackerras to aid in implementing Deflate compression and | ||
8 | * decompression for PPP packets. | ||
9 | */ | ||
10 | |||
11 | /* | ||
12 | * ==FILEVERSION 960122== | ||
13 | * | ||
14 | * This marker is used by the Linux installation script to determine | ||
15 | * whether an up-to-date version of this file is already installed. | ||
16 | */ | ||
17 | |||
18 | /* zlib.h -- interface of the 'zlib' general purpose compression library | ||
19 | version 0.95, Aug 16th, 1995. | ||
20 | |||
21 | Copyright (C) 1995 Jean-loup Gailly and Mark Adler | ||
22 | |||
23 | This software is provided 'as-is', without any express or implied | ||
24 | warranty. In no event will the authors be held liable for any damages | ||
25 | arising from the use of this software. | ||
26 | |||
27 | Permission is granted to anyone to use this software for any purpose, | ||
28 | including commercial applications, and to alter it and redistribute it | ||
29 | freely, subject to the following restrictions: | ||
30 | |||
31 | 1. The origin of this software must not be misrepresented; you must not | ||
32 | claim that you wrote the original software. If you use this software | ||
33 | in a product, an acknowledgment in the product documentation would be | ||
34 | appreciated but is not required. | ||
35 | 2. Altered source versions must be plainly marked as such, and must not be | ||
36 | misrepresented as being the original software. | ||
37 | 3. This notice may not be removed or altered from any source distribution. | ||
38 | |||
39 | Jean-loup Gailly Mark Adler | ||
40 | gzip@prep.ai.mit.edu madler@alumni.caltech.edu | ||
41 | */ | ||
42 | |||
43 | #ifndef _ZLIB_H | ||
44 | #define _ZLIB_H | ||
45 | |||
46 | /* #include "zconf.h" */ /* included directly here */ | ||
47 | |||
48 | /* zconf.h -- configuration of the zlib compression library | ||
49 | * Copyright (C) 1995 Jean-loup Gailly. | ||
50 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
51 | */ | ||
52 | |||
53 | /* From: zconf.h,v 1.12 1995/05/03 17:27:12 jloup Exp */ | ||
54 | |||
55 | /* | ||
56 | The library does not install any signal handler. It is recommended to | ||
57 | add at least a handler for SIGSEGV when decompressing; the library checks | ||
58 | the consistency of the input data whenever possible but may go nuts | ||
59 | for some forms of corrupted input. | ||
60 | */ | ||
61 | |||
62 | /* | ||
63 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more | ||
64 | * than 64k bytes at a time (needed on systems with 16-bit int). | ||
65 | * Compile with -DUNALIGNED_OK if it is OK to access shorts or ints | ||
66 | * at addresses which are not a multiple of their size. | ||
67 | * Under DOS, -DFAR=far or -DFAR=__far may be needed. | ||
68 | */ | ||
69 | |||
70 | #ifndef STDC | ||
71 | # if defined(MSDOS) || defined(__STDC__) || defined(__cplusplus) | ||
72 | # define STDC | ||
73 | # endif | ||
74 | #endif | ||
75 | |||
76 | #ifdef __MWERKS__ /* Metrowerks CodeWarrior declares fileno() in unix.h */ | ||
77 | # include <unix.h> | ||
78 | #endif | ||
79 | |||
80 | /* Maximum value for memLevel in deflateInit2 */ | ||
81 | #ifndef MAX_MEM_LEVEL | ||
82 | # ifdef MAXSEG_64K | ||
83 | # define MAX_MEM_LEVEL 8 | ||
84 | # else | ||
85 | # define MAX_MEM_LEVEL 9 | ||
86 | # endif | ||
87 | #endif | ||
88 | |||
89 | #ifndef FAR | ||
90 | # define FAR | ||
91 | #endif | ||
92 | |||
93 | /* Maximum value for windowBits in deflateInit2 and inflateInit2 */ | ||
94 | #ifndef MAX_WBITS | ||
95 | # define MAX_WBITS 15 /* 32K LZ77 window */ | ||
96 | #endif | ||
97 | |||
98 | /* The memory requirements for deflate are (in bytes): | ||
99 | 1 << (windowBits+2) + 1 << (memLevel+9) | ||
100 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) | ||
101 | plus a few kilobytes for small objects. For example, if you want to reduce | ||
102 | the default memory requirements from 256K to 128K, compile with | ||
103 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" | ||
104 | Of course this will generally degrade compression (there's no free lunch). | ||
105 | |||
106 | The memory requirements for inflate are (in bytes) 1 << windowBits | ||
107 | that is, 32K for windowBits=15 (default value) plus a few kilobytes | ||
108 | for small objects. | ||
109 | */ | ||
110 | |||
111 | /* Type declarations */ | ||
112 | |||
113 | #ifndef OF /* function prototypes */ | ||
114 | # ifdef STDC | ||
115 | # define OF(args) args | ||
116 | # else | ||
117 | # define OF(args) () | ||
118 | # endif | ||
119 | #endif | ||
120 | |||
121 | typedef unsigned char Byte; /* 8 bits */ | ||
122 | typedef unsigned int uInt; /* 16 bits or more */ | ||
123 | typedef unsigned long uLong; /* 32 bits or more */ | ||
124 | |||
125 | typedef Byte FAR Bytef; | ||
126 | typedef char FAR charf; | ||
127 | typedef int FAR intf; | ||
128 | typedef uInt FAR uIntf; | ||
129 | typedef uLong FAR uLongf; | ||
130 | |||
131 | #ifdef STDC | ||
132 | typedef void FAR *voidpf; | ||
133 | typedef void *voidp; | ||
134 | #else | ||
135 | typedef Byte FAR *voidpf; | ||
136 | typedef Byte *voidp; | ||
137 | #endif | ||
138 | |||
139 | /* end of original zconf.h */ | ||
140 | |||
141 | #define ZLIB_VERSION "0.95P" | ||
142 | |||
143 | /* | ||
144 | The 'zlib' compression library provides in-memory compression and | ||
145 | decompression functions, including integrity checks of the uncompressed | ||
146 | data. This version of the library supports only one compression method | ||
147 | (deflation) but other algorithms may be added later and will have the same | ||
148 | stream interface. | ||
149 | |||
150 | For compression the application must provide the output buffer and | ||
151 | may optionally provide the input buffer for optimization. For decompression, | ||
152 | the application must provide the input buffer and may optionally provide | ||
153 | the output buffer for optimization. | ||
154 | |||
155 | Compression can be done in a single step if the buffers are large | ||
156 | enough (for example if an input file is mmap'ed), or can be done by | ||
157 | repeated calls of the compression function. In the latter case, the | ||
158 | application must provide more input and/or consume the output | ||
159 | (providing more output space) before each call. | ||
160 | */ | ||
161 | |||
162 | typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); | ||
163 | typedef void (*free_func) OF((voidpf opaque, voidpf address, uInt nbytes)); | ||
164 | |||
165 | struct internal_state; | ||
166 | |||
167 | typedef struct z_stream_s { | ||
168 | Bytef *next_in; /* next input byte */ | ||
169 | uInt avail_in; /* number of bytes available at next_in */ | ||
170 | uLong total_in; /* total nb of input bytes read so far */ | ||
171 | |||
172 | Bytef *next_out; /* next output byte should be put there */ | ||
173 | uInt avail_out; /* remaining free space at next_out */ | ||
174 | uLong total_out; /* total nb of bytes output so far */ | ||
175 | |||
176 | char *msg; /* last error message, NULL if no error */ | ||
177 | struct internal_state FAR *state; /* not visible by applications */ | ||
178 | |||
179 | alloc_func zalloc; /* used to allocate the internal state */ | ||
180 | free_func zfree; /* used to free the internal state */ | ||
181 | voidp opaque; /* private data object passed to zalloc and zfree */ | ||
182 | |||
183 | Byte data_type; /* best guess about the data type: ascii or binary */ | ||
184 | |||
185 | } z_stream; | ||
186 | |||
187 | /* | ||
188 | The application must update next_in and avail_in when avail_in has | ||
189 | dropped to zero. It must update next_out and avail_out when avail_out | ||
190 | has dropped to zero. The application must initialize zalloc, zfree and | ||
191 | opaque before calling the init function. All other fields are set by the | ||
192 | compression library and must not be updated by the application. | ||
193 | |||
194 | The opaque value provided by the application will be passed as the first | ||
195 | parameter for calls of zalloc and zfree. This can be useful for custom | ||
196 | memory management. The compression library attaches no meaning to the | ||
197 | opaque value. | ||
198 | |||
199 | zalloc must return Z_NULL if there is not enough memory for the object. | ||
200 | On 16-bit systems, the functions zalloc and zfree must be able to allocate | ||
201 | exactly 65536 bytes, but will not be required to allocate more than this | ||
202 | if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, | ||
203 | pointers returned by zalloc for objects of exactly 65536 bytes *must* | ||
204 | have their offset normalized to zero. The default allocation function | ||
205 | provided by this library ensures this (see zutil.c). To reduce memory | ||
206 | requirements and avoid any allocation of 64K objects, at the expense of | ||
207 | compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). | ||
208 | |||
209 | The fields total_in and total_out can be used for statistics or | ||
210 | progress reports. After compression, total_in holds the total size of | ||
211 | the uncompressed data and may be saved for use in the decompressor | ||
212 | (particularly if the decompressor wants to decompress everything in | ||
213 | a single step). | ||
214 | */ | ||
215 | |||
216 | /* constants */ | ||
217 | |||
218 | #define Z_NO_FLUSH 0 | ||
219 | #define Z_PARTIAL_FLUSH 1 | ||
220 | #define Z_FULL_FLUSH 2 | ||
221 | #define Z_SYNC_FLUSH 3 /* experimental: partial_flush + byte align */ | ||
222 | #define Z_FINISH 4 | ||
223 | #define Z_PACKET_FLUSH 5 | ||
224 | /* See deflate() below for the usage of these constants */ | ||
225 | |||
226 | #define Z_OK 0 | ||
227 | #define Z_STREAM_END 1 | ||
228 | #define Z_ERRNO (-1) | ||
229 | #define Z_STREAM_ERROR (-2) | ||
230 | #define Z_DATA_ERROR (-3) | ||
231 | #define Z_MEM_ERROR (-4) | ||
232 | #define Z_BUF_ERROR (-5) | ||
233 | /* error codes for the compression/decompression functions */ | ||
234 | |||
235 | #define Z_BEST_SPEED 1 | ||
236 | #define Z_BEST_COMPRESSION 9 | ||
237 | #define Z_DEFAULT_COMPRESSION (-1) | ||
238 | /* compression levels */ | ||
239 | |||
240 | #define Z_FILTERED 1 | ||
241 | #define Z_HUFFMAN_ONLY 2 | ||
242 | #define Z_DEFAULT_STRATEGY 0 | ||
243 | |||
244 | #define Z_BINARY 0 | ||
245 | #define Z_ASCII 1 | ||
246 | #define Z_UNKNOWN 2 | ||
247 | /* Used to set the data_type field */ | ||
248 | |||
249 | #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ | ||
250 | |||
251 | extern char *zlib_version; | ||
252 | /* The application can compare zlib_version and ZLIB_VERSION for consistency. | ||
253 | If the first character differs, the library code actually used is | ||
254 | not compatible with the zlib.h header file used by the application. | ||
255 | */ | ||
256 | |||
257 | /* basic functions */ | ||
258 | |||
259 | extern int inflateInit OF((z_stream *strm)); | ||
260 | /* | ||
261 | Initializes the internal stream state for decompression. The fields | ||
262 | zalloc and zfree must be initialized before by the caller. If zalloc and | ||
263 | zfree are set to Z_NULL, inflateInit updates them to use default allocation | ||
264 | functions. | ||
265 | |||
266 | inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not | ||
267 | enough memory. msg is set to null if there is no error message. | ||
268 | inflateInit does not perform any decompression: this will be done by | ||
269 | inflate(). | ||
270 | */ | ||
271 | |||
272 | |||
273 | extern int inflate OF((z_stream *strm, int flush)); | ||
274 | /* | ||
275 | Performs one or both of the following actions: | ||
276 | |||
277 | - Decompress more input starting at next_in and update next_in and avail_in | ||
278 | accordingly. If not all input can be processed (because there is not | ||
279 | enough room in the output buffer), next_in is updated and processing | ||
280 | will resume at this point for the next call of inflate(). | ||
281 | |||
282 | - Provide more output starting at next_out and update next_out and avail_out | ||
283 | accordingly. inflate() always provides as much output as possible | ||
284 | (until there is no more input data or no more space in the output buffer). | ||
285 | |||
286 | Before the call of inflate(), the application should ensure that at least | ||
287 | one of the actions is possible, by providing more input and/or consuming | ||
288 | more output, and updating the next_* and avail_* values accordingly. | ||
289 | The application can consume the uncompressed output when it wants, for | ||
290 | example when the output buffer is full (avail_out == 0), or after each | ||
291 | call of inflate(). | ||
292 | |||
293 | If the parameter flush is set to Z_PARTIAL_FLUSH or Z_PACKET_FLUSH, | ||
294 | inflate flushes as much output as possible to the output buffer. The | ||
295 | flushing behavior of inflate is not specified for values of the flush | ||
296 | parameter other than Z_PARTIAL_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the | ||
297 | current implementation actually flushes as much output as possible | ||
298 | anyway. For Z_PACKET_FLUSH, inflate checks that once all the input data | ||
299 | has been consumed, it is expecting to see the length field of a stored | ||
300 | block; if not, it returns Z_DATA_ERROR. | ||
301 | |||
302 | inflate() should normally be called until it returns Z_STREAM_END or an | ||
303 | error. However if all decompression is to be performed in a single step | ||
304 | (a single call of inflate), the parameter flush should be set to | ||
305 | Z_FINISH. In this case all pending input is processed and all pending | ||
306 | output is flushed; avail_out must be large enough to hold all the | ||
307 | uncompressed data. (The size of the uncompressed data may have been saved | ||
308 | by the compressor for this purpose.) The next operation on this stream must | ||
309 | be inflateEnd to deallocate the decompression state. The use of Z_FINISH | ||
310 | is never required, but can be used to inform inflate that a faster routine | ||
311 | may be used for the single inflate() call. | ||
312 | |||
313 | inflate() returns Z_OK if some progress has been made (more input | ||
314 | processed or more output produced), Z_STREAM_END if the end of the | ||
315 | compressed data has been reached and all uncompressed output has been | ||
316 | produced, Z_DATA_ERROR if the input data was corrupted, Z_STREAM_ERROR if | ||
317 | the stream structure was inconsistent (for example if next_in or next_out | ||
318 | was NULL), Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if no | ||
319 | progress is possible or if there was not enough room in the output buffer | ||
320 | when Z_FINISH is used. In the Z_DATA_ERROR case, the application may then | ||
321 | call inflateSync to look for a good compression block. */ | ||
322 | |||
323 | |||
324 | extern int inflateEnd OF((z_stream *strm)); | ||
325 | /* | ||
326 | All dynamically allocated data structures for this stream are freed. | ||
327 | This function discards any unprocessed input and does not flush any | ||
328 | pending output. | ||
329 | |||
330 | inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state | ||
331 | was inconsistent. In the error case, msg may be set but then points to a | ||
332 | static string (which must not be deallocated). | ||
333 | */ | ||
334 | |||
335 | /* advanced functions */ | ||
336 | |||
337 | extern int inflateInit2 OF((z_stream *strm, | ||
338 | int windowBits)); | ||
339 | /* | ||
340 | This is another version of inflateInit with more compression options. The | ||
341 | fields next_out, zalloc and zfree must be initialized before by the caller. | ||
342 | |||
343 | The windowBits parameter is the base two logarithm of the maximum window | ||
344 | size (the size of the history buffer). It should be in the range 8..15 for | ||
345 | this version of the library (the value 16 will be allowed soon). The | ||
346 | default value is 15 if inflateInit is used instead. If a compressed stream | ||
347 | with a larger window size is given as input, inflate() will return with | ||
348 | the error code Z_DATA_ERROR instead of trying to allocate a larger window. | ||
349 | |||
350 | If next_out is not null, the library will use this buffer for the history | ||
351 | buffer; the buffer must either be large enough to hold the entire output | ||
352 | data, or have at least 1<<windowBits bytes. If next_out is null, the | ||
353 | library will allocate its own buffer (and leave next_out null). next_in | ||
354 | need not be provided here but must be provided by the application for the | ||
355 | next call of inflate(). | ||
356 | |||
357 | If the history buffer is provided by the application, next_out must | ||
358 | never be changed by the application since the decompressor maintains | ||
359 | history information inside this buffer from call to call; the application | ||
360 | can only reset next_out to the beginning of the history buffer when | ||
361 | avail_out is zero and all output has been consumed. | ||
362 | |||
363 | inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was | ||
364 | not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as | ||
365 | windowBits < 8). msg is set to null if there is no error message. | ||
366 | inflateInit2 does not perform any decompression: this will be done by | ||
367 | inflate(). | ||
368 | */ | ||
369 | |||
370 | extern int inflateSync OF((z_stream *strm)); | ||
371 | /* | ||
372 | Skips invalid compressed data until the special marker (see deflate() | ||
373 | above) can be found, or until all available input is skipped. No output | ||
374 | is provided. | ||
375 | |||
376 | inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR | ||
377 | if no more input was provided, Z_DATA_ERROR if no marker has been found, | ||
378 | or Z_STREAM_ERROR if the stream structure was inconsistent. In the success | ||
379 | case, the application may save the current current value of total_in which | ||
380 | indicates where valid compressed data was found. In the error case, the | ||
381 | application may repeatedly call inflateSync, providing more input each time, | ||
382 | until success or end of the input data. | ||
383 | */ | ||
384 | |||
385 | extern int inflateReset OF((z_stream *strm)); | ||
386 | /* | ||
387 | This function is equivalent to inflateEnd followed by inflateInit, | ||
388 | but does not free and reallocate all the internal decompression state. | ||
389 | The stream will keep attributes that may have been set by inflateInit2. | ||
390 | |||
391 | inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
392 | stream state was inconsistent (such as zalloc or state being NULL). | ||
393 | */ | ||
394 | |||
395 | extern int inflateIncomp OF((z_stream *strm)); | ||
396 | /* | ||
397 | This function adds the data at next_in (avail_in bytes) to the output | ||
398 | history without performing any output. There must be no pending output, | ||
399 | and the decompressor must be expecting to see the start of a block. | ||
400 | Calling this function is equivalent to decompressing a stored block | ||
401 | containing the data at next_in (except that the data is not output). | ||
402 | */ | ||
403 | |||
404 | /* checksum functions */ | ||
405 | |||
406 | /* | ||
407 | This function is not related to compression but is exported | ||
408 | anyway because it might be useful in applications using the | ||
409 | compression library. | ||
410 | */ | ||
411 | |||
412 | extern uLong adler32 OF((uLong adler, Bytef *buf, uInt len)); | ||
413 | |||
414 | /* | ||
415 | Update a running Adler-32 checksum with the bytes buf[0..len-1] and | ||
416 | return the updated checksum. If buf is NULL, this function returns | ||
417 | the required initial value for the checksum. | ||
418 | An Adler-32 checksum is almost as reliable as a CRC32 but can be computed | ||
419 | much faster. Usage example: | ||
420 | |||
421 | uLong adler = adler32(0L, Z_NULL, 0); | ||
422 | |||
423 | while (read_buffer(buffer, length) != EOF) { | ||
424 | adler = adler32(adler, buffer, length); | ||
425 | } | ||
426 | if (adler != original_adler) error(); | ||
427 | */ | ||
428 | |||
429 | #ifndef _Z_UTIL_H | ||
430 | struct internal_state {int dummy;}; /* hack for buggy compilers */ | ||
431 | #endif | ||
432 | |||
433 | #endif /* _ZLIB_H */ | ||
diff --git a/arch/xtensa/boot/lib/Makefile b/arch/xtensa/boot/lib/Makefile new file mode 100644 index 000000000000..c0a74dc3a0df --- /dev/null +++ b/arch/xtensa/boot/lib/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | # | ||
2 | # Makefile for some libs needed by zImage. | ||
3 | # | ||
4 | |||
5 | |||
6 | lib-y := zlib.o zmem.o | ||
diff --git a/arch/xtensa/boot/lib/memcpy.S b/arch/xtensa/boot/lib/memcpy.S new file mode 100644 index 000000000000..a029f5df2d5c --- /dev/null +++ b/arch/xtensa/boot/lib/memcpy.S | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | * arch/xtensa/lib/memcpy.S | ||
3 | * | ||
4 | * ANSI C standard library function memcpy | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of the GNU General | ||
7 | * Public License. See the file "COPYING" in the main directory of | ||
8 | * this archive for more details. | ||
9 | * | ||
10 | * Copyright (C) 2002 Tensilica Inc. | ||
11 | */ | ||
12 | |||
13 | #define _ASMLANGUAGE | ||
14 | #include <xtensa/config/core.h> | ||
15 | |||
16 | .text | ||
17 | .align 4 | ||
18 | .global bcopy | ||
19 | .type bcopy,@function | ||
20 | bcopy: | ||
21 | movi a14, xthal_bcopy // a14 safe to use regardless of whether caller | ||
22 | // used call4 or call8 (can't have used call12) | ||
23 | jx a14 // let the Core HAL do the work | ||
24 | |||
25 | .text | ||
26 | .align 4 | ||
27 | .global memcpy | ||
28 | .type memcpy,@function | ||
29 | memcpy: | ||
30 | .global memmove | ||
31 | .type memmove,@function | ||
32 | memmove: | ||
33 | movi a14, xthal_memcpy // a14 safe to use regardless of whether caller | ||
34 | // used call4 or call8 (can't have used call12) | ||
35 | jx a14 // let the Core HAL do the work | ||
36 | |||
diff --git a/arch/xtensa/boot/lib/zlib.c b/arch/xtensa/boot/lib/zlib.c new file mode 100644 index 000000000000..e3859f631077 --- /dev/null +++ b/arch/xtensa/boot/lib/zlib.c | |||
@@ -0,0 +1,2150 @@ | |||
1 | /* | ||
2 | * BK Id: SCCS/s.zlib.c 1.8 05/18/01 15:17:24 cort | ||
3 | */ | ||
4 | /* | ||
5 | * This file is derived from various .h and .c files from the zlib-0.95 | ||
6 | * distribution by Jean-loup Gailly and Mark Adler, with some additions | ||
7 | * by Paul Mackerras to aid in implementing Deflate compression and | ||
8 | * decompression for PPP packets. See zlib.h for conditions of | ||
9 | * distribution and use. | ||
10 | * | ||
11 | * Changes that have been made include: | ||
12 | * - changed functions not used outside this file to "local" | ||
13 | * - added minCompression parameter to deflateInit2 | ||
14 | * - added Z_PACKET_FLUSH (see zlib.h for details) | ||
15 | * - added inflateIncomp | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | /*+++++*/ | ||
20 | /* zutil.h -- internal interface and configuration of the compression library | ||
21 | * Copyright (C) 1995 Jean-loup Gailly. | ||
22 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
23 | */ | ||
24 | |||
25 | /* WARNING: this file should *not* be used by applications. It is | ||
26 | part of the implementation of the compression library and is | ||
27 | subject to change. Applications should only use zlib.h. | ||
28 | */ | ||
29 | |||
30 | /* From: zutil.h,v 1.9 1995/05/03 17:27:12 jloup Exp */ | ||
31 | |||
32 | #define _Z_UTIL_H | ||
33 | |||
34 | #include "zlib.h" | ||
35 | |||
36 | #ifndef local | ||
37 | # define local static | ||
38 | #endif | ||
39 | /* compile with -Dlocal if your debugger can't find static symbols */ | ||
40 | |||
41 | #define FAR | ||
42 | |||
43 | typedef unsigned char uch; | ||
44 | typedef uch FAR uchf; | ||
45 | typedef unsigned short ush; | ||
46 | typedef ush FAR ushf; | ||
47 | typedef unsigned long ulg; | ||
48 | |||
49 | extern char *z_errmsg[]; /* indexed by 1-zlib_error */ | ||
50 | |||
51 | #define ERR_RETURN(strm,err) return (strm->msg=z_errmsg[1-err], err) | ||
52 | /* To be used only when the state is known to be valid */ | ||
53 | |||
54 | #ifndef NULL | ||
55 | #define NULL ((void *) 0) | ||
56 | #endif | ||
57 | |||
58 | /* common constants */ | ||
59 | |||
60 | #define DEFLATED 8 | ||
61 | |||
62 | #ifndef DEF_WBITS | ||
63 | # define DEF_WBITS MAX_WBITS | ||
64 | #endif | ||
65 | /* default windowBits for decompression. MAX_WBITS is for compression only */ | ||
66 | |||
67 | #if MAX_MEM_LEVEL >= 8 | ||
68 | # define DEF_MEM_LEVEL 8 | ||
69 | #else | ||
70 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL | ||
71 | #endif | ||
72 | /* default memLevel */ | ||
73 | |||
74 | #define STORED_BLOCK 0 | ||
75 | #define STATIC_TREES 1 | ||
76 | #define DYN_TREES 2 | ||
77 | /* The three kinds of block type */ | ||
78 | |||
79 | #define MIN_MATCH 3 | ||
80 | #define MAX_MATCH 258 | ||
81 | /* The minimum and maximum match lengths */ | ||
82 | |||
83 | /* functions */ | ||
84 | |||
85 | #include <linux/string.h> | ||
86 | #define zmemcpy memcpy | ||
87 | #define zmemzero(dest, len) memset(dest, 0, len) | ||
88 | |||
89 | /* Diagnostic functions */ | ||
90 | #ifdef DEBUG_ZLIB | ||
91 | # include <stdio.h> | ||
92 | # ifndef verbose | ||
93 | # define verbose 0 | ||
94 | # endif | ||
95 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} | ||
96 | # define Trace(x) fprintf x | ||
97 | # define Tracev(x) {if (verbose) fprintf x ;} | ||
98 | # define Tracevv(x) {if (verbose>1) fprintf x ;} | ||
99 | # define Tracec(c,x) {if (verbose && (c)) fprintf x ;} | ||
100 | # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;} | ||
101 | #else | ||
102 | # define Assert(cond,msg) | ||
103 | # define Trace(x) | ||
104 | # define Tracev(x) | ||
105 | # define Tracevv(x) | ||
106 | # define Tracec(c,x) | ||
107 | # define Tracecv(c,x) | ||
108 | #endif | ||
109 | |||
110 | |||
111 | typedef uLong (*check_func) OF((uLong check, Bytef *buf, uInt len)); | ||
112 | |||
113 | /* voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); */ | ||
114 | /* void zcfree OF((voidpf opaque, voidpf ptr)); */ | ||
115 | |||
116 | #define ZALLOC(strm, items, size) \ | ||
117 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) | ||
118 | #define ZFREE(strm, addr, size) \ | ||
119 | (*((strm)->zfree))((strm)->opaque, (voidpf)(addr), (size)) | ||
120 | #define TRY_FREE(s, p, n) {if (p) ZFREE(s, p, n);} | ||
121 | |||
122 | /* deflate.h -- internal compression state | ||
123 | * Copyright (C) 1995 Jean-loup Gailly | ||
124 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
125 | */ | ||
126 | |||
127 | /* WARNING: this file should *not* be used by applications. It is | ||
128 | part of the implementation of the compression library and is | ||
129 | subject to change. Applications should only use zlib.h. | ||
130 | */ | ||
131 | |||
132 | /*+++++*/ | ||
133 | /* infblock.h -- header to use infblock.c | ||
134 | * Copyright (C) 1995 Mark Adler | ||
135 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
136 | */ | ||
137 | |||
138 | /* WARNING: this file should *not* be used by applications. It is | ||
139 | part of the implementation of the compression library and is | ||
140 | subject to change. Applications should only use zlib.h. | ||
141 | */ | ||
142 | |||
143 | struct inflate_blocks_state; | ||
144 | typedef struct inflate_blocks_state FAR inflate_blocks_statef; | ||
145 | |||
146 | local inflate_blocks_statef * inflate_blocks_new OF(( | ||
147 | z_stream *z, | ||
148 | check_func c, /* check function */ | ||
149 | uInt w)); /* window size */ | ||
150 | |||
151 | local int inflate_blocks OF(( | ||
152 | inflate_blocks_statef *, | ||
153 | z_stream *, | ||
154 | int)); /* initial return code */ | ||
155 | |||
156 | local void inflate_blocks_reset OF(( | ||
157 | inflate_blocks_statef *, | ||
158 | z_stream *, | ||
159 | uLongf *)); /* check value on output */ | ||
160 | |||
161 | local int inflate_blocks_free OF(( | ||
162 | inflate_blocks_statef *, | ||
163 | z_stream *, | ||
164 | uLongf *)); /* check value on output */ | ||
165 | |||
166 | local int inflate_addhistory OF(( | ||
167 | inflate_blocks_statef *, | ||
168 | z_stream *)); | ||
169 | |||
170 | local int inflate_packet_flush OF(( | ||
171 | inflate_blocks_statef *)); | ||
172 | |||
173 | /*+++++*/ | ||
174 | /* inftrees.h -- header to use inftrees.c | ||
175 | * Copyright (C) 1995 Mark Adler | ||
176 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
177 | */ | ||
178 | |||
179 | /* WARNING: this file should *not* be used by applications. It is | ||
180 | part of the implementation of the compression library and is | ||
181 | subject to change. Applications should only use zlib.h. | ||
182 | */ | ||
183 | |||
184 | /* Huffman code lookup table entry--this entry is four bytes for machines | ||
185 | that have 16-bit pointers (e.g. PC's in the small or medium model). */ | ||
186 | |||
187 | typedef struct inflate_huft_s FAR inflate_huft; | ||
188 | |||
189 | struct inflate_huft_s { | ||
190 | union { | ||
191 | struct { | ||
192 | Byte Exop; /* number of extra bits or operation */ | ||
193 | Byte Bits; /* number of bits in this code or subcode */ | ||
194 | } what; | ||
195 | uInt Nalloc; /* number of these allocated here */ | ||
196 | Bytef *pad; /* pad structure to a power of 2 (4 bytes for */ | ||
197 | } word; /* 16-bit, 8 bytes for 32-bit machines) */ | ||
198 | union { | ||
199 | uInt Base; /* literal, length base, or distance base */ | ||
200 | inflate_huft *Next; /* pointer to next level of table */ | ||
201 | } more; | ||
202 | }; | ||
203 | |||
204 | #ifdef DEBUG_ZLIB | ||
205 | local uInt inflate_hufts; | ||
206 | #endif | ||
207 | |||
208 | local int inflate_trees_bits OF(( | ||
209 | uIntf *, /* 19 code lengths */ | ||
210 | uIntf *, /* bits tree desired/actual depth */ | ||
211 | inflate_huft * FAR *, /* bits tree result */ | ||
212 | z_stream *)); /* for zalloc, zfree functions */ | ||
213 | |||
214 | local int inflate_trees_dynamic OF(( | ||
215 | uInt, /* number of literal/length codes */ | ||
216 | uInt, /* number of distance codes */ | ||
217 | uIntf *, /* that many (total) code lengths */ | ||
218 | uIntf *, /* literal desired/actual bit depth */ | ||
219 | uIntf *, /* distance desired/actual bit depth */ | ||
220 | inflate_huft * FAR *, /* literal/length tree result */ | ||
221 | inflate_huft * FAR *, /* distance tree result */ | ||
222 | z_stream *)); /* for zalloc, zfree functions */ | ||
223 | |||
224 | local int inflate_trees_fixed OF(( | ||
225 | uIntf *, /* literal desired/actual bit depth */ | ||
226 | uIntf *, /* distance desired/actual bit depth */ | ||
227 | inflate_huft * FAR *, /* literal/length tree result */ | ||
228 | inflate_huft * FAR *)); /* distance tree result */ | ||
229 | |||
230 | local int inflate_trees_free OF(( | ||
231 | inflate_huft *, /* tables to free */ | ||
232 | z_stream *)); /* for zfree function */ | ||
233 | |||
234 | |||
235 | /*+++++*/ | ||
236 | /* infcodes.h -- header to use infcodes.c | ||
237 | * Copyright (C) 1995 Mark Adler | ||
238 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
239 | */ | ||
240 | |||
241 | /* WARNING: this file should *not* be used by applications. It is | ||
242 | part of the implementation of the compression library and is | ||
243 | subject to change. Applications should only use zlib.h. | ||
244 | */ | ||
245 | |||
246 | struct inflate_codes_state; | ||
247 | typedef struct inflate_codes_state FAR inflate_codes_statef; | ||
248 | |||
249 | local inflate_codes_statef *inflate_codes_new OF(( | ||
250 | uInt, uInt, | ||
251 | inflate_huft *, inflate_huft *, | ||
252 | z_stream *)); | ||
253 | |||
254 | local int inflate_codes OF(( | ||
255 | inflate_blocks_statef *, | ||
256 | z_stream *, | ||
257 | int)); | ||
258 | |||
259 | local void inflate_codes_free OF(( | ||
260 | inflate_codes_statef *, | ||
261 | z_stream *)); | ||
262 | |||
263 | |||
264 | /*+++++*/ | ||
265 | /* inflate.c -- zlib interface to inflate modules | ||
266 | * Copyright (C) 1995 Mark Adler | ||
267 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
268 | */ | ||
269 | |||
270 | /* inflate private state */ | ||
271 | struct internal_state { | ||
272 | |||
273 | /* mode */ | ||
274 | enum { | ||
275 | METHOD, /* waiting for method byte */ | ||
276 | FLAG, /* waiting for flag byte */ | ||
277 | BLOCKS, /* decompressing blocks */ | ||
278 | CHECK4, /* four check bytes to go */ | ||
279 | CHECK3, /* three check bytes to go */ | ||
280 | CHECK2, /* two check bytes to go */ | ||
281 | CHECK1, /* one check byte to go */ | ||
282 | DONE, /* finished check, done */ | ||
283 | BAD} /* got an error--stay here */ | ||
284 | mode; /* current inflate mode */ | ||
285 | |||
286 | /* mode dependent information */ | ||
287 | union { | ||
288 | uInt method; /* if FLAGS, method byte */ | ||
289 | struct { | ||
290 | uLong was; /* computed check value */ | ||
291 | uLong need; /* stream check value */ | ||
292 | } check; /* if CHECK, check values to compare */ | ||
293 | uInt marker; /* if BAD, inflateSync's marker bytes count */ | ||
294 | } sub; /* submode */ | ||
295 | |||
296 | /* mode independent information */ | ||
297 | int nowrap; /* flag for no wrapper */ | ||
298 | uInt wbits; /* log2(window size) (8..15, defaults to 15) */ | ||
299 | inflate_blocks_statef | ||
300 | *blocks; /* current inflate_blocks state */ | ||
301 | |||
302 | }; | ||
303 | |||
304 | |||
305 | int inflateReset(z) | ||
306 | z_stream *z; | ||
307 | { | ||
308 | uLong c; | ||
309 | |||
310 | if (z == Z_NULL || z->state == Z_NULL) | ||
311 | return Z_STREAM_ERROR; | ||
312 | z->total_in = z->total_out = 0; | ||
313 | z->msg = Z_NULL; | ||
314 | z->state->mode = z->state->nowrap ? BLOCKS : METHOD; | ||
315 | inflate_blocks_reset(z->state->blocks, z, &c); | ||
316 | Trace((stderr, "inflate: reset\n")); | ||
317 | return Z_OK; | ||
318 | } | ||
319 | |||
320 | |||
321 | int inflateEnd(z) | ||
322 | z_stream *z; | ||
323 | { | ||
324 | uLong c; | ||
325 | |||
326 | if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL) | ||
327 | return Z_STREAM_ERROR; | ||
328 | if (z->state->blocks != Z_NULL) | ||
329 | inflate_blocks_free(z->state->blocks, z, &c); | ||
330 | ZFREE(z, z->state, sizeof(struct internal_state)); | ||
331 | z->state = Z_NULL; | ||
332 | Trace((stderr, "inflate: end\n")); | ||
333 | return Z_OK; | ||
334 | } | ||
335 | |||
336 | |||
337 | int inflateInit2(z, w) | ||
338 | z_stream *z; | ||
339 | int w; | ||
340 | { | ||
341 | /* initialize state */ | ||
342 | if (z == Z_NULL) | ||
343 | return Z_STREAM_ERROR; | ||
344 | /* if (z->zalloc == Z_NULL) z->zalloc = zcalloc; */ | ||
345 | /* if (z->zfree == Z_NULL) z->zfree = zcfree; */ | ||
346 | if ((z->state = (struct internal_state FAR *) | ||
347 | ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL) | ||
348 | return Z_MEM_ERROR; | ||
349 | z->state->blocks = Z_NULL; | ||
350 | |||
351 | /* handle undocumented nowrap option (no zlib header or check) */ | ||
352 | z->state->nowrap = 0; | ||
353 | if (w < 0) | ||
354 | { | ||
355 | w = - w; | ||
356 | z->state->nowrap = 1; | ||
357 | } | ||
358 | |||
359 | /* set window size */ | ||
360 | if (w < 8 || w > 15) | ||
361 | { | ||
362 | inflateEnd(z); | ||
363 | return Z_STREAM_ERROR; | ||
364 | } | ||
365 | z->state->wbits = (uInt)w; | ||
366 | |||
367 | /* create inflate_blocks state */ | ||
368 | if ((z->state->blocks = | ||
369 | inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, 1 << w)) | ||
370 | == Z_NULL) | ||
371 | { | ||
372 | inflateEnd(z); | ||
373 | return Z_MEM_ERROR; | ||
374 | } | ||
375 | Trace((stderr, "inflate: allocated\n")); | ||
376 | |||
377 | /* reset state */ | ||
378 | inflateReset(z); | ||
379 | return Z_OK; | ||
380 | } | ||
381 | |||
382 | |||
383 | int inflateInit(z) | ||
384 | z_stream *z; | ||
385 | { | ||
386 | return inflateInit2(z, DEF_WBITS); | ||
387 | } | ||
388 | |||
389 | |||
390 | #define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;} | ||
391 | #define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++) | ||
392 | |||
393 | int inflate(z, f) | ||
394 | z_stream *z; | ||
395 | int f; | ||
396 | { | ||
397 | int r; | ||
398 | uInt b; | ||
399 | |||
400 | if (z == Z_NULL || z->next_in == Z_NULL) | ||
401 | return Z_STREAM_ERROR; | ||
402 | r = Z_BUF_ERROR; | ||
403 | while (1) switch (z->state->mode) | ||
404 | { | ||
405 | case METHOD: | ||
406 | NEEDBYTE | ||
407 | if (((z->state->sub.method = NEXTBYTE) & 0xf) != DEFLATED) | ||
408 | { | ||
409 | z->state->mode = BAD; | ||
410 | z->msg = "unknown compression method"; | ||
411 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
412 | break; | ||
413 | } | ||
414 | if ((z->state->sub.method >> 4) + 8 > z->state->wbits) | ||
415 | { | ||
416 | z->state->mode = BAD; | ||
417 | z->msg = "invalid window size"; | ||
418 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
419 | break; | ||
420 | } | ||
421 | z->state->mode = FLAG; | ||
422 | case FLAG: | ||
423 | NEEDBYTE | ||
424 | if ((b = NEXTBYTE) & 0x20) | ||
425 | { | ||
426 | z->state->mode = BAD; | ||
427 | z->msg = "invalid reserved bit"; | ||
428 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
429 | break; | ||
430 | } | ||
431 | if (((z->state->sub.method << 8) + b) % 31) | ||
432 | { | ||
433 | z->state->mode = BAD; | ||
434 | z->msg = "incorrect header check"; | ||
435 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
436 | break; | ||
437 | } | ||
438 | Trace((stderr, "inflate: zlib header ok\n")); | ||
439 | z->state->mode = BLOCKS; | ||
440 | case BLOCKS: | ||
441 | r = inflate_blocks(z->state->blocks, z, r); | ||
442 | if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0) | ||
443 | r = inflate_packet_flush(z->state->blocks); | ||
444 | if (r == Z_DATA_ERROR) | ||
445 | { | ||
446 | z->state->mode = BAD; | ||
447 | z->state->sub.marker = 0; /* can try inflateSync */ | ||
448 | break; | ||
449 | } | ||
450 | if (r != Z_STREAM_END) | ||
451 | return r; | ||
452 | r = Z_OK; | ||
453 | inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was); | ||
454 | if (z->state->nowrap) | ||
455 | { | ||
456 | z->state->mode = DONE; | ||
457 | break; | ||
458 | } | ||
459 | z->state->mode = CHECK4; | ||
460 | case CHECK4: | ||
461 | NEEDBYTE | ||
462 | z->state->sub.check.need = (uLong)NEXTBYTE << 24; | ||
463 | z->state->mode = CHECK3; | ||
464 | case CHECK3: | ||
465 | NEEDBYTE | ||
466 | z->state->sub.check.need += (uLong)NEXTBYTE << 16; | ||
467 | z->state->mode = CHECK2; | ||
468 | case CHECK2: | ||
469 | NEEDBYTE | ||
470 | z->state->sub.check.need += (uLong)NEXTBYTE << 8; | ||
471 | z->state->mode = CHECK1; | ||
472 | case CHECK1: | ||
473 | NEEDBYTE | ||
474 | z->state->sub.check.need += (uLong)NEXTBYTE; | ||
475 | |||
476 | if (z->state->sub.check.was != z->state->sub.check.need) | ||
477 | { | ||
478 | z->state->mode = BAD; | ||
479 | z->msg = "incorrect data check"; | ||
480 | z->state->sub.marker = 5; /* can't try inflateSync */ | ||
481 | break; | ||
482 | } | ||
483 | Trace((stderr, "inflate: zlib check ok\n")); | ||
484 | z->state->mode = DONE; | ||
485 | case DONE: | ||
486 | return Z_STREAM_END; | ||
487 | case BAD: | ||
488 | return Z_DATA_ERROR; | ||
489 | default: | ||
490 | return Z_STREAM_ERROR; | ||
491 | } | ||
492 | |||
493 | empty: | ||
494 | if (f != Z_PACKET_FLUSH) | ||
495 | return r; | ||
496 | z->state->mode = BAD; | ||
497 | z->state->sub.marker = 0; /* can try inflateSync */ | ||
498 | return Z_DATA_ERROR; | ||
499 | } | ||
500 | |||
501 | /* | ||
502 | * This subroutine adds the data at next_in/avail_in to the output history | ||
503 | * without performing any output. The output buffer must be "caught up"; | ||
504 | * i.e. no pending output (hence s->read equals s->write), and the state must | ||
505 | * be BLOCKS (i.e. we should be willing to see the start of a series of | ||
506 | * BLOCKS). On exit, the output will also be caught up, and the checksum | ||
507 | * will have been updated if need be. | ||
508 | */ | ||
509 | |||
510 | int inflateIncomp(z) | ||
511 | z_stream *z; | ||
512 | { | ||
513 | if (z->state->mode != BLOCKS) | ||
514 | return Z_DATA_ERROR; | ||
515 | return inflate_addhistory(z->state->blocks, z); | ||
516 | } | ||
517 | |||
518 | |||
519 | int inflateSync(z) | ||
520 | z_stream *z; | ||
521 | { | ||
522 | uInt n; /* number of bytes to look at */ | ||
523 | Bytef *p; /* pointer to bytes */ | ||
524 | uInt m; /* number of marker bytes found in a row */ | ||
525 | uLong r, w; /* temporaries to save total_in and total_out */ | ||
526 | |||
527 | /* set up */ | ||
528 | if (z == Z_NULL || z->state == Z_NULL) | ||
529 | return Z_STREAM_ERROR; | ||
530 | if (z->state->mode != BAD) | ||
531 | { | ||
532 | z->state->mode = BAD; | ||
533 | z->state->sub.marker = 0; | ||
534 | } | ||
535 | if ((n = z->avail_in) == 0) | ||
536 | return Z_BUF_ERROR; | ||
537 | p = z->next_in; | ||
538 | m = z->state->sub.marker; | ||
539 | |||
540 | /* search */ | ||
541 | while (n && m < 4) | ||
542 | { | ||
543 | if (*p == (Byte)(m < 2 ? 0 : 0xff)) | ||
544 | m++; | ||
545 | else if (*p) | ||
546 | m = 0; | ||
547 | else | ||
548 | m = 4 - m; | ||
549 | p++, n--; | ||
550 | } | ||
551 | |||
552 | /* restore */ | ||
553 | z->total_in += p - z->next_in; | ||
554 | z->next_in = p; | ||
555 | z->avail_in = n; | ||
556 | z->state->sub.marker = m; | ||
557 | |||
558 | /* return no joy or set up to restart on a new block */ | ||
559 | if (m != 4) | ||
560 | return Z_DATA_ERROR; | ||
561 | r = z->total_in; w = z->total_out; | ||
562 | inflateReset(z); | ||
563 | z->total_in = r; z->total_out = w; | ||
564 | z->state->mode = BLOCKS; | ||
565 | return Z_OK; | ||
566 | } | ||
567 | |||
568 | #undef NEEDBYTE | ||
569 | #undef NEXTBYTE | ||
570 | |||
571 | /*+++++*/ | ||
572 | /* infutil.h -- types and macros common to blocks and codes | ||
573 | * Copyright (C) 1995 Mark Adler | ||
574 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
575 | */ | ||
576 | |||
577 | /* WARNING: this file should *not* be used by applications. It is | ||
578 | part of the implementation of the compression library and is | ||
579 | subject to change. Applications should only use zlib.h. | ||
580 | */ | ||
581 | |||
582 | /* inflate blocks semi-private state */ | ||
583 | struct inflate_blocks_state { | ||
584 | |||
585 | /* mode */ | ||
586 | enum { | ||
587 | TYPE, /* get type bits (3, including end bit) */ | ||
588 | LENS, /* get lengths for stored */ | ||
589 | STORED, /* processing stored block */ | ||
590 | TABLE, /* get table lengths */ | ||
591 | BTREE, /* get bit lengths tree for a dynamic block */ | ||
592 | DTREE, /* get length, distance trees for a dynamic block */ | ||
593 | CODES, /* processing fixed or dynamic block */ | ||
594 | DRY, /* output remaining window bytes */ | ||
595 | DONEB, /* finished last block, done */ | ||
596 | BADB} /* got a data error--stuck here */ | ||
597 | mode; /* current inflate_block mode */ | ||
598 | |||
599 | /* mode dependent information */ | ||
600 | union { | ||
601 | uInt left; /* if STORED, bytes left to copy */ | ||
602 | struct { | ||
603 | uInt table; /* table lengths (14 bits) */ | ||
604 | uInt index; /* index into blens (or border) */ | ||
605 | uIntf *blens; /* bit lengths of codes */ | ||
606 | uInt bb; /* bit length tree depth */ | ||
607 | inflate_huft *tb; /* bit length decoding tree */ | ||
608 | int nblens; /* # elements allocated at blens */ | ||
609 | } trees; /* if DTREE, decoding info for trees */ | ||
610 | struct { | ||
611 | inflate_huft *tl, *td; /* trees to free */ | ||
612 | inflate_codes_statef | ||
613 | *codes; | ||
614 | } decode; /* if CODES, current state */ | ||
615 | } sub; /* submode */ | ||
616 | uInt last; /* true if this block is the last block */ | ||
617 | |||
618 | /* mode independent information */ | ||
619 | uInt bitk; /* bits in bit buffer */ | ||
620 | uLong bitb; /* bit buffer */ | ||
621 | Bytef *window; /* sliding window */ | ||
622 | Bytef *end; /* one byte after sliding window */ | ||
623 | Bytef *read; /* window read pointer */ | ||
624 | Bytef *write; /* window write pointer */ | ||
625 | check_func checkfn; /* check function */ | ||
626 | uLong check; /* check on output */ | ||
627 | |||
628 | }; | ||
629 | |||
630 | |||
631 | /* defines for inflate input/output */ | ||
632 | /* update pointers and return */ | ||
633 | #define UPDBITS {s->bitb=b;s->bitk=k;} | ||
634 | #define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;} | ||
635 | #define UPDOUT {s->write=q;} | ||
636 | #define UPDATE {UPDBITS UPDIN UPDOUT} | ||
637 | #define LEAVE {UPDATE return inflate_flush(s,z,r);} | ||
638 | /* get bytes and bits */ | ||
639 | #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;} | ||
640 | #define NEEDBYTE {if(n)r=Z_OK;else LEAVE} | ||
641 | #define NEXTBYTE (n--,*p++) | ||
642 | #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}} | ||
643 | #define DUMPBITS(j) {b>>=(j);k-=(j);} | ||
644 | /* output bytes */ | ||
645 | #define WAVAIL (q<s->read?s->read-q-1:s->end-q) | ||
646 | #define LOADOUT {q=s->write;m=WAVAIL;} | ||
647 | #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=WAVAIL;}} | ||
648 | #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT} | ||
649 | #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;} | ||
650 | #define OUTBYTE(a) {*q++=(Byte)(a);m--;} | ||
651 | /* load local pointers */ | ||
652 | #define LOAD {LOADIN LOADOUT} | ||
653 | |||
654 | /* | ||
655 | * The IBM 150 firmware munges the data right after _etext[]. This | ||
656 | * protects it. -- Cort | ||
657 | */ | ||
658 | local uInt protect_mask[] = {0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0}; | ||
659 | /* And'ing with mask[n] masks the lower n bits */ | ||
660 | local uInt inflate_mask[] = { | ||
661 | 0x0000, | ||
662 | 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, | ||
663 | 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff | ||
664 | }; | ||
665 | |||
666 | /* copy as much as possible from the sliding window to the output area */ | ||
667 | local int inflate_flush OF(( | ||
668 | inflate_blocks_statef *, | ||
669 | z_stream *, | ||
670 | int)); | ||
671 | |||
672 | /*+++++*/ | ||
673 | /* inffast.h -- header to use inffast.c | ||
674 | * Copyright (C) 1995 Mark Adler | ||
675 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
676 | */ | ||
677 | |||
678 | /* WARNING: this file should *not* be used by applications. It is | ||
679 | part of the implementation of the compression library and is | ||
680 | subject to change. Applications should only use zlib.h. | ||
681 | */ | ||
682 | |||
683 | local int inflate_fast OF(( | ||
684 | uInt, | ||
685 | uInt, | ||
686 | inflate_huft *, | ||
687 | inflate_huft *, | ||
688 | inflate_blocks_statef *, | ||
689 | z_stream *)); | ||
690 | |||
691 | |||
692 | /*+++++*/ | ||
693 | /* infblock.c -- interpret and process block types to last block | ||
694 | * Copyright (C) 1995 Mark Adler | ||
695 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
696 | */ | ||
697 | |||
698 | /* Table for deflate from PKZIP's appnote.txt. */ | ||
699 | local uInt border[] = { /* Order of the bit length code lengths */ | ||
700 | 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | ||
701 | |||
702 | /* | ||
703 | Notes beyond the 1.93a appnote.txt: | ||
704 | |||
705 | 1. Distance pointers never point before the beginning of the output | ||
706 | stream. | ||
707 | 2. Distance pointers can point back across blocks, up to 32k away. | ||
708 | 3. There is an implied maximum of 7 bits for the bit length table and | ||
709 | 15 bits for the actual data. | ||
710 | 4. If only one code exists, then it is encoded using one bit. (Zero | ||
711 | would be more efficient, but perhaps a little confusing.) If two | ||
712 | codes exist, they are coded using one bit each (0 and 1). | ||
713 | 5. There is no way of sending zero distance codes--a dummy must be | ||
714 | sent if there are none. (History: a pre 2.0 version of PKZIP would | ||
715 | store blocks with no distance codes, but this was discovered to be | ||
716 | too harsh a criterion.) Valid only for 1.93a. 2.04c does allow | ||
717 | zero distance codes, which is sent as one code of zero bits in | ||
718 | length. | ||
719 | 6. There are up to 286 literal/length codes. Code 256 represents the | ||
720 | end-of-block. Note however that the static length tree defines | ||
721 | 288 codes just to fill out the Huffman codes. Codes 286 and 287 | ||
722 | cannot be used though, since there is no length base or extra bits | ||
723 | defined for them. Similarily, there are up to 30 distance codes. | ||
724 | However, static trees define 32 codes (all 5 bits) to fill out the | ||
725 | Huffman codes, but the last two had better not show up in the data. | ||
726 | 7. Unzip can check dynamic Huffman blocks for complete code sets. | ||
727 | The exception is that a single code would not be complete (see #4). | ||
728 | 8. The five bits following the block type is really the number of | ||
729 | literal codes sent minus 257. | ||
730 | 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits | ||
731 | (1+6+6). Therefore, to output three times the length, you output | ||
732 | three codes (1+1+1), whereas to output four times the same length, | ||
733 | you only need two codes (1+3). Hmm. | ||
734 | 10. In the tree reconstruction algorithm, Code = Code + Increment | ||
735 | only if BitLength(i) is not zero. (Pretty obvious.) | ||
736 | 11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19) | ||
737 | 12. Note: length code 284 can represent 227-258, but length code 285 | ||
738 | really is 258. The last length deserves its own, short code | ||
739 | since it gets used a lot in very redundant files. The length | ||
740 | 258 is special since 258 - 3 (the min match length) is 255. | ||
741 | 13. The literal/length and distance code bit lengths are read as a | ||
742 | single stream of lengths. It is possible (and advantageous) for | ||
743 | a repeat code (16, 17, or 18) to go across the boundary between | ||
744 | the two sets of lengths. | ||
745 | */ | ||
746 | |||
747 | |||
748 | local void inflate_blocks_reset(s, z, c) | ||
749 | inflate_blocks_statef *s; | ||
750 | z_stream *z; | ||
751 | uLongf *c; | ||
752 | { | ||
753 | if (s->checkfn != Z_NULL) | ||
754 | *c = s->check; | ||
755 | if (s->mode == BTREE || s->mode == DTREE) | ||
756 | ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt)); | ||
757 | if (s->mode == CODES) | ||
758 | { | ||
759 | inflate_codes_free(s->sub.decode.codes, z); | ||
760 | inflate_trees_free(s->sub.decode.td, z); | ||
761 | inflate_trees_free(s->sub.decode.tl, z); | ||
762 | } | ||
763 | s->mode = TYPE; | ||
764 | s->bitk = 0; | ||
765 | s->bitb = 0; | ||
766 | s->read = s->write = s->window; | ||
767 | if (s->checkfn != Z_NULL) | ||
768 | s->check = (*s->checkfn)(0L, Z_NULL, 0); | ||
769 | Trace((stderr, "inflate: blocks reset\n")); | ||
770 | } | ||
771 | |||
772 | |||
773 | local inflate_blocks_statef *inflate_blocks_new(z, c, w) | ||
774 | z_stream *z; | ||
775 | check_func c; | ||
776 | uInt w; | ||
777 | { | ||
778 | inflate_blocks_statef *s; | ||
779 | |||
780 | if ((s = (inflate_blocks_statef *)ZALLOC | ||
781 | (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL) | ||
782 | return s; | ||
783 | if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL) | ||
784 | { | ||
785 | ZFREE(z, s, sizeof(struct inflate_blocks_state)); | ||
786 | return Z_NULL; | ||
787 | } | ||
788 | s->end = s->window + w; | ||
789 | s->checkfn = c; | ||
790 | s->mode = TYPE; | ||
791 | Trace((stderr, "inflate: blocks allocated\n")); | ||
792 | inflate_blocks_reset(s, z, &s->check); | ||
793 | return s; | ||
794 | } | ||
795 | |||
796 | |||
797 | local int inflate_blocks(s, z, r) | ||
798 | inflate_blocks_statef *s; | ||
799 | z_stream *z; | ||
800 | int r; | ||
801 | { | ||
802 | uInt t; /* temporary storage */ | ||
803 | uLong b; /* bit buffer */ | ||
804 | uInt k; /* bits in bit buffer */ | ||
805 | Bytef *p; /* input data pointer */ | ||
806 | uInt n; /* bytes available there */ | ||
807 | Bytef *q; /* output window write pointer */ | ||
808 | uInt m; /* bytes to end of window or read pointer */ | ||
809 | |||
810 | /* copy input/output information to locals (UPDATE macro restores) */ | ||
811 | LOAD | ||
812 | |||
813 | /* process input based on current state */ | ||
814 | while (1) switch (s->mode) | ||
815 | { | ||
816 | case TYPE: | ||
817 | NEEDBITS(3) | ||
818 | t = (uInt)b & 7; | ||
819 | s->last = t & 1; | ||
820 | switch (t >> 1) | ||
821 | { | ||
822 | case 0: /* stored */ | ||
823 | Trace((stderr, "inflate: stored block%s\n", | ||
824 | s->last ? " (last)" : "")); | ||
825 | DUMPBITS(3) | ||
826 | t = k & 7; /* go to byte boundary */ | ||
827 | DUMPBITS(t) | ||
828 | s->mode = LENS; /* get length of stored block */ | ||
829 | break; | ||
830 | case 1: /* fixed */ | ||
831 | Trace((stderr, "inflate: fixed codes block%s\n", | ||
832 | s->last ? " (last)" : "")); | ||
833 | { | ||
834 | uInt bl, bd; | ||
835 | inflate_huft *tl, *td; | ||
836 | |||
837 | inflate_trees_fixed(&bl, &bd, &tl, &td); | ||
838 | s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z); | ||
839 | if (s->sub.decode.codes == Z_NULL) | ||
840 | { | ||
841 | r = Z_MEM_ERROR; | ||
842 | LEAVE | ||
843 | } | ||
844 | s->sub.decode.tl = Z_NULL; /* don't try to free these */ | ||
845 | s->sub.decode.td = Z_NULL; | ||
846 | } | ||
847 | DUMPBITS(3) | ||
848 | s->mode = CODES; | ||
849 | break; | ||
850 | case 2: /* dynamic */ | ||
851 | Trace((stderr, "inflate: dynamic codes block%s\n", | ||
852 | s->last ? " (last)" : "")); | ||
853 | DUMPBITS(3) | ||
854 | s->mode = TABLE; | ||
855 | break; | ||
856 | case 3: /* illegal */ | ||
857 | DUMPBITS(3) | ||
858 | s->mode = BADB; | ||
859 | z->msg = "invalid block type"; | ||
860 | r = Z_DATA_ERROR; | ||
861 | LEAVE | ||
862 | } | ||
863 | break; | ||
864 | case LENS: | ||
865 | NEEDBITS(32) | ||
866 | if (((~b) >> 16) != (b & 0xffff)) | ||
867 | { | ||
868 | s->mode = BADB; | ||
869 | z->msg = "invalid stored block lengths"; | ||
870 | r = Z_DATA_ERROR; | ||
871 | LEAVE | ||
872 | } | ||
873 | s->sub.left = (uInt)b & 0xffff; | ||
874 | b = k = 0; /* dump bits */ | ||
875 | Tracev((stderr, "inflate: stored length %u\n", s->sub.left)); | ||
876 | s->mode = s->sub.left ? STORED : TYPE; | ||
877 | break; | ||
878 | case STORED: | ||
879 | if (n == 0) | ||
880 | LEAVE | ||
881 | NEEDOUT | ||
882 | t = s->sub.left; | ||
883 | if (t > n) t = n; | ||
884 | if (t > m) t = m; | ||
885 | zmemcpy(q, p, t); | ||
886 | p += t; n -= t; | ||
887 | q += t; m -= t; | ||
888 | if ((s->sub.left -= t) != 0) | ||
889 | break; | ||
890 | Tracev((stderr, "inflate: stored end, %lu total out\n", | ||
891 | z->total_out + (q >= s->read ? q - s->read : | ||
892 | (s->end - s->read) + (q - s->window)))); | ||
893 | s->mode = s->last ? DRY : TYPE; | ||
894 | break; | ||
895 | case TABLE: | ||
896 | NEEDBITS(14) | ||
897 | s->sub.trees.table = t = (uInt)b & 0x3fff; | ||
898 | #ifndef PKZIP_BUG_WORKAROUND | ||
899 | if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) | ||
900 | { | ||
901 | s->mode = BADB; | ||
902 | z->msg = "too many length or distance symbols"; | ||
903 | r = Z_DATA_ERROR; | ||
904 | LEAVE | ||
905 | } | ||
906 | #endif | ||
907 | t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); | ||
908 | if (t < 19) | ||
909 | t = 19; | ||
910 | if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL) | ||
911 | { | ||
912 | r = Z_MEM_ERROR; | ||
913 | LEAVE | ||
914 | } | ||
915 | s->sub.trees.nblens = t; | ||
916 | DUMPBITS(14) | ||
917 | s->sub.trees.index = 0; | ||
918 | Tracev((stderr, "inflate: table sizes ok\n")); | ||
919 | s->mode = BTREE; | ||
920 | case BTREE: | ||
921 | while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10)) | ||
922 | { | ||
923 | NEEDBITS(3) | ||
924 | s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7; | ||
925 | DUMPBITS(3) | ||
926 | } | ||
927 | while (s->sub.trees.index < 19) | ||
928 | s->sub.trees.blens[border[s->sub.trees.index++]] = 0; | ||
929 | s->sub.trees.bb = 7; | ||
930 | t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb, | ||
931 | &s->sub.trees.tb, z); | ||
932 | if (t != Z_OK) | ||
933 | { | ||
934 | r = t; | ||
935 | if (r == Z_DATA_ERROR) | ||
936 | s->mode = BADB; | ||
937 | LEAVE | ||
938 | } | ||
939 | s->sub.trees.index = 0; | ||
940 | Tracev((stderr, "inflate: bits tree ok\n")); | ||
941 | s->mode = DTREE; | ||
942 | case DTREE: | ||
943 | while (t = s->sub.trees.table, | ||
944 | s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f)) | ||
945 | { | ||
946 | inflate_huft *h; | ||
947 | uInt i, j, c; | ||
948 | |||
949 | t = s->sub.trees.bb; | ||
950 | NEEDBITS(t) | ||
951 | h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]); | ||
952 | t = h->word.what.Bits; | ||
953 | c = h->more.Base; | ||
954 | if (c < 16) | ||
955 | { | ||
956 | DUMPBITS(t) | ||
957 | s->sub.trees.blens[s->sub.trees.index++] = c; | ||
958 | } | ||
959 | else /* c == 16..18 */ | ||
960 | { | ||
961 | i = c == 18 ? 7 : c - 14; | ||
962 | j = c == 18 ? 11 : 3; | ||
963 | NEEDBITS(t + i) | ||
964 | DUMPBITS(t) | ||
965 | j += (uInt)b & inflate_mask[i]; | ||
966 | DUMPBITS(i) | ||
967 | i = s->sub.trees.index; | ||
968 | t = s->sub.trees.table; | ||
969 | if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || | ||
970 | (c == 16 && i < 1)) | ||
971 | { | ||
972 | s->mode = BADB; | ||
973 | z->msg = "invalid bit length repeat"; | ||
974 | r = Z_DATA_ERROR; | ||
975 | LEAVE | ||
976 | } | ||
977 | c = c == 16 ? s->sub.trees.blens[i - 1] : 0; | ||
978 | do { | ||
979 | s->sub.trees.blens[i++] = c; | ||
980 | } while (--j); | ||
981 | s->sub.trees.index = i; | ||
982 | } | ||
983 | } | ||
984 | inflate_trees_free(s->sub.trees.tb, z); | ||
985 | s->sub.trees.tb = Z_NULL; | ||
986 | { | ||
987 | uInt bl, bd; | ||
988 | inflate_huft *tl, *td; | ||
989 | inflate_codes_statef *c; | ||
990 | |||
991 | bl = 9; /* must be <= 9 for lookahead assumptions */ | ||
992 | bd = 6; /* must be <= 9 for lookahead assumptions */ | ||
993 | t = s->sub.trees.table; | ||
994 | t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f), | ||
995 | s->sub.trees.blens, &bl, &bd, &tl, &td, z); | ||
996 | if (t != Z_OK) | ||
997 | { | ||
998 | if (t == (uInt)Z_DATA_ERROR) | ||
999 | s->mode = BADB; | ||
1000 | r = t; | ||
1001 | LEAVE | ||
1002 | } | ||
1003 | Tracev((stderr, "inflate: trees ok\n")); | ||
1004 | if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL) | ||
1005 | { | ||
1006 | inflate_trees_free(td, z); | ||
1007 | inflate_trees_free(tl, z); | ||
1008 | r = Z_MEM_ERROR; | ||
1009 | LEAVE | ||
1010 | } | ||
1011 | ZFREE(z, s->sub.trees.blens, s->sub.trees.nblens * sizeof(uInt)); | ||
1012 | s->sub.decode.codes = c; | ||
1013 | s->sub.decode.tl = tl; | ||
1014 | s->sub.decode.td = td; | ||
1015 | } | ||
1016 | s->mode = CODES; | ||
1017 | case CODES: | ||
1018 | UPDATE | ||
1019 | if ((r = inflate_codes(s, z, r)) != Z_STREAM_END) | ||
1020 | return inflate_flush(s, z, r); | ||
1021 | r = Z_OK; | ||
1022 | inflate_codes_free(s->sub.decode.codes, z); | ||
1023 | inflate_trees_free(s->sub.decode.td, z); | ||
1024 | inflate_trees_free(s->sub.decode.tl, z); | ||
1025 | LOAD | ||
1026 | Tracev((stderr, "inflate: codes end, %lu total out\n", | ||
1027 | z->total_out + (q >= s->read ? q - s->read : | ||
1028 | (s->end - s->read) + (q - s->window)))); | ||
1029 | if (!s->last) | ||
1030 | { | ||
1031 | s->mode = TYPE; | ||
1032 | break; | ||
1033 | } | ||
1034 | if (k > 7) /* return unused byte, if any */ | ||
1035 | { | ||
1036 | Assert(k < 16, "inflate_codes grabbed too many bytes") | ||
1037 | k -= 8; | ||
1038 | n++; | ||
1039 | p--; /* can always return one */ | ||
1040 | } | ||
1041 | s->mode = DRY; | ||
1042 | case DRY: | ||
1043 | FLUSH | ||
1044 | if (s->read != s->write) | ||
1045 | LEAVE | ||
1046 | s->mode = DONEB; | ||
1047 | case DONEB: | ||
1048 | r = Z_STREAM_END; | ||
1049 | LEAVE | ||
1050 | case BADB: | ||
1051 | r = Z_DATA_ERROR; | ||
1052 | LEAVE | ||
1053 | default: | ||
1054 | r = Z_STREAM_ERROR; | ||
1055 | LEAVE | ||
1056 | } | ||
1057 | } | ||
1058 | |||
1059 | |||
1060 | local int inflate_blocks_free(s, z, c) | ||
1061 | inflate_blocks_statef *s; | ||
1062 | z_stream *z; | ||
1063 | uLongf *c; | ||
1064 | { | ||
1065 | inflate_blocks_reset(s, z, c); | ||
1066 | ZFREE(z, s->window, s->end - s->window); | ||
1067 | ZFREE(z, s, sizeof(struct inflate_blocks_state)); | ||
1068 | Trace((stderr, "inflate: blocks freed\n")); | ||
1069 | return Z_OK; | ||
1070 | } | ||
1071 | |||
1072 | /* | ||
1073 | * This subroutine adds the data at next_in/avail_in to the output history | ||
1074 | * without performing any output. The output buffer must be "caught up"; | ||
1075 | * i.e. no pending output (hence s->read equals s->write), and the state must | ||
1076 | * be BLOCKS (i.e. we should be willing to see the start of a series of | ||
1077 | * BLOCKS). On exit, the output will also be caught up, and the checksum | ||
1078 | * will have been updated if need be. | ||
1079 | */ | ||
1080 | local int inflate_addhistory(s, z) | ||
1081 | inflate_blocks_statef *s; | ||
1082 | z_stream *z; | ||
1083 | { | ||
1084 | uLong b; /* bit buffer */ /* NOT USED HERE */ | ||
1085 | uInt k; /* bits in bit buffer */ /* NOT USED HERE */ | ||
1086 | uInt t; /* temporary storage */ | ||
1087 | Bytef *p; /* input data pointer */ | ||
1088 | uInt n; /* bytes available there */ | ||
1089 | Bytef *q; /* output window write pointer */ | ||
1090 | uInt m; /* bytes to end of window or read pointer */ | ||
1091 | |||
1092 | if (s->read != s->write) | ||
1093 | return Z_STREAM_ERROR; | ||
1094 | if (s->mode != TYPE) | ||
1095 | return Z_DATA_ERROR; | ||
1096 | |||
1097 | /* we're ready to rock */ | ||
1098 | LOAD | ||
1099 | /* while there is input ready, copy to output buffer, moving | ||
1100 | * pointers as needed. | ||
1101 | */ | ||
1102 | while (n) { | ||
1103 | t = n; /* how many to do */ | ||
1104 | /* is there room until end of buffer? */ | ||
1105 | if (t > m) t = m; | ||
1106 | /* update check information */ | ||
1107 | if (s->checkfn != Z_NULL) | ||
1108 | s->check = (*s->checkfn)(s->check, q, t); | ||
1109 | zmemcpy(q, p, t); | ||
1110 | q += t; | ||
1111 | p += t; | ||
1112 | n -= t; | ||
1113 | z->total_out += t; | ||
1114 | s->read = q; /* drag read pointer forward */ | ||
1115 | /* WRAP */ /* expand WRAP macro by hand to handle s->read */ | ||
1116 | if (q == s->end) { | ||
1117 | s->read = q = s->window; | ||
1118 | m = WAVAIL; | ||
1119 | } | ||
1120 | } | ||
1121 | UPDATE | ||
1122 | return Z_OK; | ||
1123 | } | ||
1124 | |||
1125 | |||
1126 | /* | ||
1127 | * At the end of a Deflate-compressed PPP packet, we expect to have seen | ||
1128 | * a `stored' block type value but not the (zero) length bytes. | ||
1129 | */ | ||
1130 | local int inflate_packet_flush(s) | ||
1131 | inflate_blocks_statef *s; | ||
1132 | { | ||
1133 | if (s->mode != LENS) | ||
1134 | return Z_DATA_ERROR; | ||
1135 | s->mode = TYPE; | ||
1136 | return Z_OK; | ||
1137 | } | ||
1138 | |||
1139 | |||
1140 | /*+++++*/ | ||
1141 | /* inftrees.c -- generate Huffman trees for efficient decoding | ||
1142 | * Copyright (C) 1995 Mark Adler | ||
1143 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
1144 | */ | ||
1145 | |||
1146 | /* simplify the use of the inflate_huft type with some defines */ | ||
1147 | #define base more.Base | ||
1148 | #define next more.Next | ||
1149 | #define exop word.what.Exop | ||
1150 | #define bits word.what.Bits | ||
1151 | |||
1152 | |||
1153 | local int huft_build OF(( | ||
1154 | uIntf *, /* code lengths in bits */ | ||
1155 | uInt, /* number of codes */ | ||
1156 | uInt, /* number of "simple" codes */ | ||
1157 | uIntf *, /* list of base values for non-simple codes */ | ||
1158 | uIntf *, /* list of extra bits for non-simple codes */ | ||
1159 | inflate_huft * FAR*,/* result: starting table */ | ||
1160 | uIntf *, /* maximum lookup bits (returns actual) */ | ||
1161 | z_stream *)); /* for zalloc function */ | ||
1162 | |||
1163 | local voidpf falloc OF(( | ||
1164 | voidpf, /* opaque pointer (not used) */ | ||
1165 | uInt, /* number of items */ | ||
1166 | uInt)); /* size of item */ | ||
1167 | |||
1168 | local void ffree OF(( | ||
1169 | voidpf q, /* opaque pointer (not used) */ | ||
1170 | voidpf p, /* what to free (not used) */ | ||
1171 | uInt n)); /* number of bytes (not used) */ | ||
1172 | |||
1173 | /* Tables for deflate from PKZIP's appnote.txt. */ | ||
1174 | local uInt cplens[] = { /* Copy lengths for literal codes 257..285 */ | ||
1175 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | ||
1176 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | ||
1177 | /* actually lengths - 2; also see note #13 above about 258 */ | ||
1178 | local uInt cplext[] = { /* Extra bits for literal codes 257..285 */ | ||
1179 | 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, | ||
1180 | 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 192, 192}; /* 192==invalid */ | ||
1181 | local uInt cpdist[] = { /* Copy offsets for distance codes 0..29 */ | ||
1182 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | ||
1183 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | ||
1184 | 8193, 12289, 16385, 24577}; | ||
1185 | local uInt cpdext[] = { /* Extra bits for distance codes */ | ||
1186 | 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, | ||
1187 | 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, | ||
1188 | 12, 12, 13, 13}; | ||
1189 | |||
1190 | /* | ||
1191 | Huffman code decoding is performed using a multi-level table lookup. | ||
1192 | The fastest way to decode is to simply build a lookup table whose | ||
1193 | size is determined by the longest code. However, the time it takes | ||
1194 | to build this table can also be a factor if the data being decoded | ||
1195 | is not very long. The most common codes are necessarily the | ||
1196 | shortest codes, so those codes dominate the decoding time, and hence | ||
1197 | the speed. The idea is you can have a shorter table that decodes the | ||
1198 | shorter, more probable codes, and then point to subsidiary tables for | ||
1199 | the longer codes. The time it costs to decode the longer codes is | ||
1200 | then traded against the time it takes to make longer tables. | ||
1201 | |||
1202 | This results of this trade are in the variables lbits and dbits | ||
1203 | below. lbits is the number of bits the first level table for literal/ | ||
1204 | length codes can decode in one step, and dbits is the same thing for | ||
1205 | the distance codes. Subsequent tables are also less than or equal to | ||
1206 | those sizes. These values may be adjusted either when all of the | ||
1207 | codes are shorter than that, in which case the longest code length in | ||
1208 | bits is used, or when the shortest code is *longer* than the requested | ||
1209 | table size, in which case the length of the shortest code in bits is | ||
1210 | used. | ||
1211 | |||
1212 | There are two different values for the two tables, since they code a | ||
1213 | different number of possibilities each. The literal/length table | ||
1214 | codes 286 possible values, or in a flat code, a little over eight | ||
1215 | bits. The distance table codes 30 possible values, or a little less | ||
1216 | than five bits, flat. The optimum values for speed end up being | ||
1217 | about one bit more than those, so lbits is 8+1 and dbits is 5+1. | ||
1218 | The optimum values may differ though from machine to machine, and | ||
1219 | possibly even between compilers. Your mileage may vary. | ||
1220 | */ | ||
1221 | |||
1222 | |||
1223 | /* If BMAX needs to be larger than 16, then h and x[] should be uLong. */ | ||
1224 | #define BMAX 15 /* maximum bit length of any code */ | ||
1225 | #define N_MAX 288 /* maximum number of codes in any set */ | ||
1226 | |||
1227 | #ifdef DEBUG_ZLIB | ||
1228 | uInt inflate_hufts; | ||
1229 | #endif | ||
1230 | |||
1231 | local int huft_build(b, n, s, d, e, t, m, zs) | ||
1232 | uIntf *b; /* code lengths in bits (all assumed <= BMAX) */ | ||
1233 | uInt n; /* number of codes (assumed <= N_MAX) */ | ||
1234 | uInt s; /* number of simple-valued codes (0..s-1) */ | ||
1235 | uIntf *d; /* list of base values for non-simple codes */ | ||
1236 | uIntf *e; /* list of extra bits for non-simple codes */ | ||
1237 | inflate_huft * FAR *t; /* result: starting table */ | ||
1238 | uIntf *m; /* maximum lookup bits, returns actual */ | ||
1239 | z_stream *zs; /* for zalloc function */ | ||
1240 | /* Given a list of code lengths and a maximum table size, make a set of | ||
1241 | tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR | ||
1242 | if the given code set is incomplete (the tables are still built in this | ||
1243 | case), Z_DATA_ERROR if the input is invalid (all zero length codes or an | ||
1244 | over-subscribed set of lengths), or Z_MEM_ERROR if not enough memory. */ | ||
1245 | { | ||
1246 | |||
1247 | uInt a; /* counter for codes of length k */ | ||
1248 | uInt c[BMAX+1]; /* bit length count table */ | ||
1249 | uInt f; /* i repeats in table every f entries */ | ||
1250 | int g; /* maximum code length */ | ||
1251 | int h; /* table level */ | ||
1252 | register uInt i; /* counter, current code */ | ||
1253 | register uInt j; /* counter */ | ||
1254 | register int k; /* number of bits in current code */ | ||
1255 | int l; /* bits per table (returned in m) */ | ||
1256 | register uIntf *p; /* pointer into c[], b[], or v[] */ | ||
1257 | inflate_huft *q; /* points to current table */ | ||
1258 | struct inflate_huft_s r; /* table entry for structure assignment */ | ||
1259 | inflate_huft *u[BMAX]; /* table stack */ | ||
1260 | uInt v[N_MAX]; /* values in order of bit length */ | ||
1261 | register int w; /* bits before this table == (l * h) */ | ||
1262 | uInt x[BMAX+1]; /* bit offsets, then code stack */ | ||
1263 | uIntf *xp; /* pointer into x */ | ||
1264 | int y; /* number of dummy codes added */ | ||
1265 | uInt z; /* number of entries in current table */ | ||
1266 | |||
1267 | |||
1268 | /* Generate counts for each bit length */ | ||
1269 | p = c; | ||
1270 | #define C0 *p++ = 0; | ||
1271 | #define C2 C0 C0 C0 C0 | ||
1272 | #define C4 C2 C2 C2 C2 | ||
1273 | C4 /* clear c[]--assume BMAX+1 is 16 */ | ||
1274 | p = b; i = n; | ||
1275 | do { | ||
1276 | c[*p++]++; /* assume all entries <= BMAX */ | ||
1277 | } while (--i); | ||
1278 | if (c[0] == n) /* null input--all zero length codes */ | ||
1279 | { | ||
1280 | *t = (inflate_huft *)Z_NULL; | ||
1281 | *m = 0; | ||
1282 | return Z_OK; | ||
1283 | } | ||
1284 | |||
1285 | |||
1286 | /* Find minimum and maximum length, bound *m by those */ | ||
1287 | l = *m; | ||
1288 | for (j = 1; j <= BMAX; j++) | ||
1289 | if (c[j]) | ||
1290 | break; | ||
1291 | k = j; /* minimum code length */ | ||
1292 | if ((uInt)l < j) | ||
1293 | l = j; | ||
1294 | for (i = BMAX; i; i--) | ||
1295 | if (c[i]) | ||
1296 | break; | ||
1297 | g = i; /* maximum code length */ | ||
1298 | if ((uInt)l > i) | ||
1299 | l = i; | ||
1300 | *m = l; | ||
1301 | |||
1302 | |||
1303 | /* Adjust last length count to fill out codes, if needed */ | ||
1304 | for (y = 1 << j; j < i; j++, y <<= 1) | ||
1305 | if ((y -= c[j]) < 0) | ||
1306 | return Z_DATA_ERROR; | ||
1307 | if ((y -= c[i]) < 0) | ||
1308 | return Z_DATA_ERROR; | ||
1309 | c[i] += y; | ||
1310 | |||
1311 | |||
1312 | /* Generate starting offsets into the value table for each length */ | ||
1313 | x[1] = j = 0; | ||
1314 | p = c + 1; xp = x + 2; | ||
1315 | while (--i) { /* note that i == g from above */ | ||
1316 | *xp++ = (j += *p++); | ||
1317 | } | ||
1318 | |||
1319 | |||
1320 | /* Make a table of values in order of bit lengths */ | ||
1321 | p = b; i = 0; | ||
1322 | do { | ||
1323 | if ((j = *p++) != 0) | ||
1324 | v[x[j]++] = i; | ||
1325 | } while (++i < n); | ||
1326 | |||
1327 | |||
1328 | /* Generate the Huffman codes and for each, make the table entries */ | ||
1329 | x[0] = i = 0; /* first Huffman code is zero */ | ||
1330 | p = v; /* grab values in bit order */ | ||
1331 | h = -1; /* no tables yet--level -1 */ | ||
1332 | w = -l; /* bits decoded == (l * h) */ | ||
1333 | u[0] = (inflate_huft *)Z_NULL; /* just to keep compilers happy */ | ||
1334 | q = (inflate_huft *)Z_NULL; /* ditto */ | ||
1335 | z = 0; /* ditto */ | ||
1336 | |||
1337 | /* go through the bit lengths (k already is bits in shortest code) */ | ||
1338 | for (; k <= g; k++) | ||
1339 | { | ||
1340 | a = c[k]; | ||
1341 | while (a--) | ||
1342 | { | ||
1343 | /* here i is the Huffman code of length k bits for value *p */ | ||
1344 | /* make tables up to required level */ | ||
1345 | while (k > w + l) | ||
1346 | { | ||
1347 | h++; | ||
1348 | w += l; /* previous table always l bits */ | ||
1349 | |||
1350 | /* compute minimum size table less than or equal to l bits */ | ||
1351 | z = (z = g - w) > (uInt)l ? l : z; /* table size upper limit */ | ||
1352 | if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */ | ||
1353 | { /* too few codes for k-w bit table */ | ||
1354 | f -= a + 1; /* deduct codes from patterns left */ | ||
1355 | xp = c + k; | ||
1356 | if (j < z) | ||
1357 | while (++j < z) /* try smaller tables up to z bits */ | ||
1358 | { | ||
1359 | if ((f <<= 1) <= *++xp) | ||
1360 | break; /* enough codes to use up j bits */ | ||
1361 | f -= *xp; /* else deduct codes from patterns */ | ||
1362 | } | ||
1363 | } | ||
1364 | z = 1 << j; /* table entries for j-bit table */ | ||
1365 | |||
1366 | /* allocate and link in new table */ | ||
1367 | if ((q = (inflate_huft *)ZALLOC | ||
1368 | (zs,z + 1,sizeof(inflate_huft))) == Z_NULL) | ||
1369 | { | ||
1370 | if (h) | ||
1371 | inflate_trees_free(u[0], zs); | ||
1372 | return Z_MEM_ERROR; /* not enough memory */ | ||
1373 | } | ||
1374 | q->word.Nalloc = z + 1; | ||
1375 | #ifdef DEBUG_ZLIB | ||
1376 | inflate_hufts += z + 1; | ||
1377 | #endif | ||
1378 | *t = q + 1; /* link to list for huft_free() */ | ||
1379 | *(t = &(q->next)) = Z_NULL; | ||
1380 | u[h] = ++q; /* table starts after link */ | ||
1381 | |||
1382 | /* connect to last table, if there is one */ | ||
1383 | if (h) | ||
1384 | { | ||
1385 | x[h] = i; /* save pattern for backing up */ | ||
1386 | r.bits = (Byte)l; /* bits to dump before this table */ | ||
1387 | r.exop = (Byte)j; /* bits in this table */ | ||
1388 | r.next = q; /* pointer to this table */ | ||
1389 | j = i >> (w - l); /* (get around Turbo C bug) */ | ||
1390 | u[h-1][j] = r; /* connect to last table */ | ||
1391 | } | ||
1392 | } | ||
1393 | |||
1394 | /* set up table entry in r */ | ||
1395 | r.bits = (Byte)(k - w); | ||
1396 | if (p >= v + n) | ||
1397 | r.exop = 128 + 64; /* out of values--invalid code */ | ||
1398 | else if (*p < s) | ||
1399 | { | ||
1400 | r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); /* 256 is end-of-block */ | ||
1401 | r.base = *p++; /* simple code is just the value */ | ||
1402 | } | ||
1403 | else | ||
1404 | { | ||
1405 | r.exop = (Byte)e[*p - s] + 16 + 64; /* non-simple--look up in lists */ | ||
1406 | r.base = d[*p++ - s]; | ||
1407 | } | ||
1408 | |||
1409 | /* fill code-like entries with r */ | ||
1410 | f = 1 << (k - w); | ||
1411 | for (j = i >> w; j < z; j += f) | ||
1412 | q[j] = r; | ||
1413 | |||
1414 | /* backwards increment the k-bit code i */ | ||
1415 | for (j = 1 << (k - 1); i & j; j >>= 1) | ||
1416 | i ^= j; | ||
1417 | i ^= j; | ||
1418 | |||
1419 | /* backup over finished tables */ | ||
1420 | while ((i & ((1 << w) - 1)) != x[h]) | ||
1421 | { | ||
1422 | h--; /* don't need to update q */ | ||
1423 | w -= l; | ||
1424 | } | ||
1425 | } | ||
1426 | } | ||
1427 | |||
1428 | |||
1429 | /* Return Z_BUF_ERROR if we were given an incomplete table */ | ||
1430 | return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK; | ||
1431 | } | ||
1432 | |||
1433 | |||
1434 | local int inflate_trees_bits(c, bb, tb, z) | ||
1435 | uIntf *c; /* 19 code lengths */ | ||
1436 | uIntf *bb; /* bits tree desired/actual depth */ | ||
1437 | inflate_huft * FAR *tb; /* bits tree result */ | ||
1438 | z_stream *z; /* for zfree function */ | ||
1439 | { | ||
1440 | int r; | ||
1441 | |||
1442 | r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); | ||
1443 | if (r == Z_DATA_ERROR) | ||
1444 | z->msg = "oversubscribed dynamic bit lengths tree"; | ||
1445 | else if (r == Z_BUF_ERROR) | ||
1446 | { | ||
1447 | inflate_trees_free(*tb, z); | ||
1448 | z->msg = "incomplete dynamic bit lengths tree"; | ||
1449 | r = Z_DATA_ERROR; | ||
1450 | } | ||
1451 | return r; | ||
1452 | } | ||
1453 | |||
1454 | |||
1455 | local int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z) | ||
1456 | uInt nl; /* number of literal/length codes */ | ||
1457 | uInt nd; /* number of distance codes */ | ||
1458 | uIntf *c; /* that many (total) code lengths */ | ||
1459 | uIntf *bl; /* literal desired/actual bit depth */ | ||
1460 | uIntf *bd; /* distance desired/actual bit depth */ | ||
1461 | inflate_huft * FAR *tl; /* literal/length tree result */ | ||
1462 | inflate_huft * FAR *td; /* distance tree result */ | ||
1463 | z_stream *z; /* for zfree function */ | ||
1464 | { | ||
1465 | int r; | ||
1466 | |||
1467 | /* build literal/length tree */ | ||
1468 | if ((r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z)) != Z_OK) | ||
1469 | { | ||
1470 | if (r == Z_DATA_ERROR) | ||
1471 | z->msg = "oversubscribed literal/length tree"; | ||
1472 | else if (r == Z_BUF_ERROR) | ||
1473 | { | ||
1474 | inflate_trees_free(*tl, z); | ||
1475 | z->msg = "incomplete literal/length tree"; | ||
1476 | r = Z_DATA_ERROR; | ||
1477 | } | ||
1478 | return r; | ||
1479 | } | ||
1480 | |||
1481 | /* build distance tree */ | ||
1482 | if ((r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z)) != Z_OK) | ||
1483 | { | ||
1484 | if (r == Z_DATA_ERROR) | ||
1485 | z->msg = "oversubscribed literal/length tree"; | ||
1486 | else if (r == Z_BUF_ERROR) { | ||
1487 | #ifdef PKZIP_BUG_WORKAROUND | ||
1488 | r = Z_OK; | ||
1489 | } | ||
1490 | #else | ||
1491 | inflate_trees_free(*td, z); | ||
1492 | z->msg = "incomplete literal/length tree"; | ||
1493 | r = Z_DATA_ERROR; | ||
1494 | } | ||
1495 | inflate_trees_free(*tl, z); | ||
1496 | return r; | ||
1497 | #endif | ||
1498 | } | ||
1499 | |||
1500 | /* done */ | ||
1501 | return Z_OK; | ||
1502 | } | ||
1503 | |||
1504 | |||
1505 | /* build fixed tables only once--keep them here */ | ||
1506 | local int fixed_lock = 0; | ||
1507 | local int fixed_built = 0; | ||
1508 | #define FIXEDH 530 /* number of hufts used by fixed tables */ | ||
1509 | local uInt fixed_left = FIXEDH; | ||
1510 | local inflate_huft fixed_mem[FIXEDH]; | ||
1511 | local uInt fixed_bl; | ||
1512 | local uInt fixed_bd; | ||
1513 | local inflate_huft *fixed_tl; | ||
1514 | local inflate_huft *fixed_td; | ||
1515 | |||
1516 | |||
1517 | local voidpf falloc(q, n, s) | ||
1518 | voidpf q; /* opaque pointer (not used) */ | ||
1519 | uInt n; /* number of items */ | ||
1520 | uInt s; /* size of item */ | ||
1521 | { | ||
1522 | Assert(s == sizeof(inflate_huft) && n <= fixed_left, | ||
1523 | "inflate_trees falloc overflow"); | ||
1524 | if (q) s++; /* to make some compilers happy */ | ||
1525 | fixed_left -= n; | ||
1526 | return (voidpf)(fixed_mem + fixed_left); | ||
1527 | } | ||
1528 | |||
1529 | |||
1530 | local void ffree(q, p, n) | ||
1531 | voidpf q; | ||
1532 | voidpf p; | ||
1533 | uInt n; | ||
1534 | { | ||
1535 | Assert(0, "inflate_trees ffree called!"); | ||
1536 | if (q) q = p; /* to make some compilers happy */ | ||
1537 | } | ||
1538 | |||
1539 | |||
1540 | local int inflate_trees_fixed(bl, bd, tl, td) | ||
1541 | uIntf *bl; /* literal desired/actual bit depth */ | ||
1542 | uIntf *bd; /* distance desired/actual bit depth */ | ||
1543 | inflate_huft * FAR *tl; /* literal/length tree result */ | ||
1544 | inflate_huft * FAR *td; /* distance tree result */ | ||
1545 | { | ||
1546 | /* build fixed tables if not built already--lock out other instances */ | ||
1547 | while (++fixed_lock > 1) | ||
1548 | fixed_lock--; | ||
1549 | if (!fixed_built) | ||
1550 | { | ||
1551 | int k; /* temporary variable */ | ||
1552 | unsigned c[288]; /* length list for huft_build */ | ||
1553 | z_stream z; /* for falloc function */ | ||
1554 | |||
1555 | /* set up fake z_stream for memory routines */ | ||
1556 | z.zalloc = falloc; | ||
1557 | z.zfree = ffree; | ||
1558 | z.opaque = Z_NULL; | ||
1559 | |||
1560 | /* literal table */ | ||
1561 | for (k = 0; k < 144; k++) | ||
1562 | c[k] = 8; | ||
1563 | for (; k < 256; k++) | ||
1564 | c[k] = 9; | ||
1565 | for (; k < 280; k++) | ||
1566 | c[k] = 7; | ||
1567 | for (; k < 288; k++) | ||
1568 | c[k] = 8; | ||
1569 | fixed_bl = 7; | ||
1570 | huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z); | ||
1571 | |||
1572 | /* distance table */ | ||
1573 | for (k = 0; k < 30; k++) | ||
1574 | c[k] = 5; | ||
1575 | fixed_bd = 5; | ||
1576 | huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z); | ||
1577 | |||
1578 | /* done */ | ||
1579 | fixed_built = 1; | ||
1580 | } | ||
1581 | fixed_lock--; | ||
1582 | *bl = fixed_bl; | ||
1583 | *bd = fixed_bd; | ||
1584 | *tl = fixed_tl; | ||
1585 | *td = fixed_td; | ||
1586 | return Z_OK; | ||
1587 | } | ||
1588 | |||
1589 | |||
1590 | local int inflate_trees_free(t, z) | ||
1591 | inflate_huft *t; /* table to free */ | ||
1592 | z_stream *z; /* for zfree function */ | ||
1593 | /* Free the malloc'ed tables built by huft_build(), which makes a linked | ||
1594 | list of the tables it made, with the links in a dummy first entry of | ||
1595 | each table. */ | ||
1596 | { | ||
1597 | register inflate_huft *p, *q; | ||
1598 | |||
1599 | /* Go through linked list, freeing from the malloced (t[-1]) address. */ | ||
1600 | p = t; | ||
1601 | while (p != Z_NULL) | ||
1602 | { | ||
1603 | q = (--p)->next; | ||
1604 | ZFREE(z, p, p->word.Nalloc * sizeof(inflate_huft)); | ||
1605 | p = q; | ||
1606 | } | ||
1607 | return Z_OK; | ||
1608 | } | ||
1609 | |||
1610 | /*+++++*/ | ||
1611 | /* infcodes.c -- process literals and length/distance pairs | ||
1612 | * Copyright (C) 1995 Mark Adler | ||
1613 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
1614 | */ | ||
1615 | |||
1616 | /* simplify the use of the inflate_huft type with some defines */ | ||
1617 | #define base more.Base | ||
1618 | #define next more.Next | ||
1619 | #define exop word.what.Exop | ||
1620 | #define bits word.what.Bits | ||
1621 | |||
1622 | /* inflate codes private state */ | ||
1623 | struct inflate_codes_state { | ||
1624 | |||
1625 | /* mode */ | ||
1626 | enum { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ | ||
1627 | START, /* x: set up for LEN */ | ||
1628 | LEN, /* i: get length/literal/eob next */ | ||
1629 | LENEXT, /* i: getting length extra (have base) */ | ||
1630 | DIST, /* i: get distance next */ | ||
1631 | DISTEXT, /* i: getting distance extra */ | ||
1632 | COPY, /* o: copying bytes in window, waiting for space */ | ||
1633 | LIT, /* o: got literal, waiting for output space */ | ||
1634 | WASH, /* o: got eob, possibly still output waiting */ | ||
1635 | END, /* x: got eob and all data flushed */ | ||
1636 | BADCODE} /* x: got error */ | ||
1637 | mode; /* current inflate_codes mode */ | ||
1638 | |||
1639 | /* mode dependent information */ | ||
1640 | uInt len; | ||
1641 | union { | ||
1642 | struct { | ||
1643 | inflate_huft *tree; /* pointer into tree */ | ||
1644 | uInt need; /* bits needed */ | ||
1645 | } code; /* if LEN or DIST, where in tree */ | ||
1646 | uInt lit; /* if LIT, literal */ | ||
1647 | struct { | ||
1648 | uInt get; /* bits to get for extra */ | ||
1649 | uInt dist; /* distance back to copy from */ | ||
1650 | } copy; /* if EXT or COPY, where and how much */ | ||
1651 | } sub; /* submode */ | ||
1652 | |||
1653 | /* mode independent information */ | ||
1654 | Byte lbits; /* ltree bits decoded per branch */ | ||
1655 | Byte dbits; /* dtree bits decoder per branch */ | ||
1656 | inflate_huft *ltree; /* literal/length/eob tree */ | ||
1657 | inflate_huft *dtree; /* distance tree */ | ||
1658 | |||
1659 | }; | ||
1660 | |||
1661 | |||
1662 | local inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z) | ||
1663 | uInt bl, bd; | ||
1664 | inflate_huft *tl, *td; | ||
1665 | z_stream *z; | ||
1666 | { | ||
1667 | inflate_codes_statef *c; | ||
1668 | |||
1669 | if ((c = (inflate_codes_statef *) | ||
1670 | ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL) | ||
1671 | { | ||
1672 | c->mode = START; | ||
1673 | c->lbits = (Byte)bl; | ||
1674 | c->dbits = (Byte)bd; | ||
1675 | c->ltree = tl; | ||
1676 | c->dtree = td; | ||
1677 | Tracev((stderr, "inflate: codes new\n")); | ||
1678 | } | ||
1679 | return c; | ||
1680 | } | ||
1681 | |||
1682 | |||
1683 | local int inflate_codes(s, z, r) | ||
1684 | inflate_blocks_statef *s; | ||
1685 | z_stream *z; | ||
1686 | int r; | ||
1687 | { | ||
1688 | uInt j; /* temporary storage */ | ||
1689 | inflate_huft *t; /* temporary pointer */ | ||
1690 | uInt e; /* extra bits or operation */ | ||
1691 | uLong b; /* bit buffer */ | ||
1692 | uInt k; /* bits in bit buffer */ | ||
1693 | Bytef *p; /* input data pointer */ | ||
1694 | uInt n; /* bytes available there */ | ||
1695 | Bytef *q; /* output window write pointer */ | ||
1696 | uInt m; /* bytes to end of window or read pointer */ | ||
1697 | Bytef *f; /* pointer to copy strings from */ | ||
1698 | inflate_codes_statef *c = s->sub.decode.codes; /* codes state */ | ||
1699 | |||
1700 | /* copy input/output information to locals (UPDATE macro restores) */ | ||
1701 | LOAD | ||
1702 | |||
1703 | /* process input and output based on current state */ | ||
1704 | while (1) switch (c->mode) | ||
1705 | { /* waiting for "i:"=input, "o:"=output, "x:"=nothing */ | ||
1706 | case START: /* x: set up for LEN */ | ||
1707 | #ifndef SLOW | ||
1708 | if (m >= 258 && n >= 10) | ||
1709 | { | ||
1710 | UPDATE | ||
1711 | r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z); | ||
1712 | LOAD | ||
1713 | if (r != Z_OK) | ||
1714 | { | ||
1715 | c->mode = r == Z_STREAM_END ? WASH : BADCODE; | ||
1716 | break; | ||
1717 | } | ||
1718 | } | ||
1719 | #endif /* !SLOW */ | ||
1720 | c->sub.code.need = c->lbits; | ||
1721 | c->sub.code.tree = c->ltree; | ||
1722 | c->mode = LEN; | ||
1723 | case LEN: /* i: get length/literal/eob next */ | ||
1724 | j = c->sub.code.need; | ||
1725 | NEEDBITS(j) | ||
1726 | t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); | ||
1727 | DUMPBITS(t->bits) | ||
1728 | e = (uInt)(t->exop); | ||
1729 | if (e == 0) /* literal */ | ||
1730 | { | ||
1731 | c->sub.lit = t->base; | ||
1732 | Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? | ||
1733 | "inflate: literal '%c'\n" : | ||
1734 | "inflate: literal 0x%02x\n", t->base)); | ||
1735 | c->mode = LIT; | ||
1736 | break; | ||
1737 | } | ||
1738 | if (e & 16) /* length */ | ||
1739 | { | ||
1740 | c->sub.copy.get = e & 15; | ||
1741 | c->len = t->base; | ||
1742 | c->mode = LENEXT; | ||
1743 | break; | ||
1744 | } | ||
1745 | if ((e & 64) == 0) /* next table */ | ||
1746 | { | ||
1747 | c->sub.code.need = e; | ||
1748 | c->sub.code.tree = t->next; | ||
1749 | break; | ||
1750 | } | ||
1751 | if (e & 32) /* end of block */ | ||
1752 | { | ||
1753 | Tracevv((stderr, "inflate: end of block\n")); | ||
1754 | c->mode = WASH; | ||
1755 | break; | ||
1756 | } | ||
1757 | c->mode = BADCODE; /* invalid code */ | ||
1758 | z->msg = "invalid literal/length code"; | ||
1759 | r = Z_DATA_ERROR; | ||
1760 | LEAVE | ||
1761 | case LENEXT: /* i: getting length extra (have base) */ | ||
1762 | j = c->sub.copy.get; | ||
1763 | NEEDBITS(j) | ||
1764 | c->len += (uInt)b & inflate_mask[j]; | ||
1765 | DUMPBITS(j) | ||
1766 | c->sub.code.need = c->dbits; | ||
1767 | c->sub.code.tree = c->dtree; | ||
1768 | Tracevv((stderr, "inflate: length %u\n", c->len)); | ||
1769 | c->mode = DIST; | ||
1770 | case DIST: /* i: get distance next */ | ||
1771 | j = c->sub.code.need; | ||
1772 | NEEDBITS(j) | ||
1773 | t = c->sub.code.tree + ((uInt)b & inflate_mask[j]); | ||
1774 | DUMPBITS(t->bits) | ||
1775 | e = (uInt)(t->exop); | ||
1776 | if (e & 16) /* distance */ | ||
1777 | { | ||
1778 | c->sub.copy.get = e & 15; | ||
1779 | c->sub.copy.dist = t->base; | ||
1780 | c->mode = DISTEXT; | ||
1781 | break; | ||
1782 | } | ||
1783 | if ((e & 64) == 0) /* next table */ | ||
1784 | { | ||
1785 | c->sub.code.need = e; | ||
1786 | c->sub.code.tree = t->next; | ||
1787 | break; | ||
1788 | } | ||
1789 | c->mode = BADCODE; /* invalid code */ | ||
1790 | z->msg = "invalid distance code"; | ||
1791 | r = Z_DATA_ERROR; | ||
1792 | LEAVE | ||
1793 | case DISTEXT: /* i: getting distance extra */ | ||
1794 | j = c->sub.copy.get; | ||
1795 | NEEDBITS(j) | ||
1796 | c->sub.copy.dist += (uInt)b & inflate_mask[j]; | ||
1797 | DUMPBITS(j) | ||
1798 | Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist)); | ||
1799 | c->mode = COPY; | ||
1800 | case COPY: /* o: copying bytes in window, waiting for space */ | ||
1801 | #ifndef __TURBOC__ /* Turbo C bug for following expression */ | ||
1802 | f = (uInt)(q - s->window) < c->sub.copy.dist ? | ||
1803 | s->end - (c->sub.copy.dist - (q - s->window)) : | ||
1804 | q - c->sub.copy.dist; | ||
1805 | #else | ||
1806 | f = q - c->sub.copy.dist; | ||
1807 | if ((uInt)(q - s->window) < c->sub.copy.dist) | ||
1808 | f = s->end - (c->sub.copy.dist - (q - s->window)); | ||
1809 | #endif | ||
1810 | while (c->len) | ||
1811 | { | ||
1812 | NEEDOUT | ||
1813 | OUTBYTE(*f++) | ||
1814 | if (f == s->end) | ||
1815 | f = s->window; | ||
1816 | c->len--; | ||
1817 | } | ||
1818 | c->mode = START; | ||
1819 | break; | ||
1820 | case LIT: /* o: got literal, waiting for output space */ | ||
1821 | NEEDOUT | ||
1822 | OUTBYTE(c->sub.lit) | ||
1823 | c->mode = START; | ||
1824 | break; | ||
1825 | case WASH: /* o: got eob, possibly more output */ | ||
1826 | FLUSH | ||
1827 | if (s->read != s->write) | ||
1828 | LEAVE | ||
1829 | c->mode = END; | ||
1830 | case END: | ||
1831 | r = Z_STREAM_END; | ||
1832 | LEAVE | ||
1833 | case BADCODE: /* x: got error */ | ||
1834 | r = Z_DATA_ERROR; | ||
1835 | LEAVE | ||
1836 | default: | ||
1837 | r = Z_STREAM_ERROR; | ||
1838 | LEAVE | ||
1839 | } | ||
1840 | } | ||
1841 | |||
1842 | |||
1843 | local void inflate_codes_free(c, z) | ||
1844 | inflate_codes_statef *c; | ||
1845 | z_stream *z; | ||
1846 | { | ||
1847 | ZFREE(z, c, sizeof(struct inflate_codes_state)); | ||
1848 | Tracev((stderr, "inflate: codes free\n")); | ||
1849 | } | ||
1850 | |||
1851 | /*+++++*/ | ||
1852 | /* inflate_util.c -- data and routines common to blocks and codes | ||
1853 | * Copyright (C) 1995 Mark Adler | ||
1854 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
1855 | */ | ||
1856 | |||
1857 | /* copy as much as possible from the sliding window to the output area */ | ||
1858 | local int inflate_flush(s, z, r) | ||
1859 | inflate_blocks_statef *s; | ||
1860 | z_stream *z; | ||
1861 | int r; | ||
1862 | { | ||
1863 | uInt n; | ||
1864 | Bytef *p, *q; | ||
1865 | |||
1866 | /* local copies of source and destination pointers */ | ||
1867 | p = z->next_out; | ||
1868 | q = s->read; | ||
1869 | |||
1870 | /* compute number of bytes to copy as far as end of window */ | ||
1871 | n = (uInt)((q <= s->write ? s->write : s->end) - q); | ||
1872 | if (n > z->avail_out) n = z->avail_out; | ||
1873 | if (n && r == Z_BUF_ERROR) r = Z_OK; | ||
1874 | |||
1875 | /* update counters */ | ||
1876 | z->avail_out -= n; | ||
1877 | z->total_out += n; | ||
1878 | |||
1879 | /* update check information */ | ||
1880 | if (s->checkfn != Z_NULL) | ||
1881 | s->check = (*s->checkfn)(s->check, q, n); | ||
1882 | |||
1883 | /* copy as far as end of window */ | ||
1884 | zmemcpy(p, q, n); | ||
1885 | p += n; | ||
1886 | q += n; | ||
1887 | |||
1888 | /* see if more to copy at beginning of window */ | ||
1889 | if (q == s->end) | ||
1890 | { | ||
1891 | /* wrap pointers */ | ||
1892 | q = s->window; | ||
1893 | if (s->write == s->end) | ||
1894 | s->write = s->window; | ||
1895 | |||
1896 | /* compute bytes to copy */ | ||
1897 | n = (uInt)(s->write - q); | ||
1898 | if (n > z->avail_out) n = z->avail_out; | ||
1899 | if (n && r == Z_BUF_ERROR) r = Z_OK; | ||
1900 | |||
1901 | /* update counters */ | ||
1902 | z->avail_out -= n; | ||
1903 | z->total_out += n; | ||
1904 | |||
1905 | /* update check information */ | ||
1906 | if (s->checkfn != Z_NULL) | ||
1907 | s->check = (*s->checkfn)(s->check, q, n); | ||
1908 | |||
1909 | /* copy */ | ||
1910 | zmemcpy(p, q, n); | ||
1911 | p += n; | ||
1912 | q += n; | ||
1913 | } | ||
1914 | |||
1915 | /* update pointers */ | ||
1916 | z->next_out = p; | ||
1917 | s->read = q; | ||
1918 | |||
1919 | /* done */ | ||
1920 | return r; | ||
1921 | } | ||
1922 | |||
1923 | |||
1924 | /*+++++*/ | ||
1925 | /* inffast.c -- process literals and length/distance pairs fast | ||
1926 | * Copyright (C) 1995 Mark Adler | ||
1927 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
1928 | */ | ||
1929 | |||
1930 | /* simplify the use of the inflate_huft type with some defines */ | ||
1931 | #define base more.Base | ||
1932 | #define next more.Next | ||
1933 | #define exop word.what.Exop | ||
1934 | #define bits word.what.Bits | ||
1935 | |||
1936 | /* macros for bit input with no checking and for returning unused bytes */ | ||
1937 | #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}} | ||
1938 | #define UNGRAB {n+=(c=k>>3);p-=c;k&=7;} | ||
1939 | |||
1940 | /* Called with number of bytes left to write in window at least 258 | ||
1941 | (the maximum string length) and number of input bytes available | ||
1942 | at least ten. The ten bytes are six bytes for the longest length/ | ||
1943 | distance pair plus four bytes for overloading the bit buffer. */ | ||
1944 | |||
1945 | local int inflate_fast(bl, bd, tl, td, s, z) | ||
1946 | uInt bl, bd; | ||
1947 | inflate_huft *tl, *td; | ||
1948 | inflate_blocks_statef *s; | ||
1949 | z_stream *z; | ||
1950 | { | ||
1951 | inflate_huft *t; /* temporary pointer */ | ||
1952 | uInt e; /* extra bits or operation */ | ||
1953 | uLong b; /* bit buffer */ | ||
1954 | uInt k; /* bits in bit buffer */ | ||
1955 | Bytef *p; /* input data pointer */ | ||
1956 | uInt n; /* bytes available there */ | ||
1957 | Bytef *q; /* output window write pointer */ | ||
1958 | uInt m; /* bytes to end of window or read pointer */ | ||
1959 | uInt ml; /* mask for literal/length tree */ | ||
1960 | uInt md; /* mask for distance tree */ | ||
1961 | uInt c; /* bytes to copy */ | ||
1962 | uInt d; /* distance back to copy from */ | ||
1963 | Bytef *r; /* copy source pointer */ | ||
1964 | |||
1965 | /* load input, output, bit values */ | ||
1966 | LOAD | ||
1967 | |||
1968 | /* initialize masks */ | ||
1969 | ml = inflate_mask[bl]; | ||
1970 | md = inflate_mask[bd]; | ||
1971 | |||
1972 | /* do until not enough input or output space for fast loop */ | ||
1973 | do { /* assume called with m >= 258 && n >= 10 */ | ||
1974 | /* get literal/length code */ | ||
1975 | GRABBITS(20) /* max bits for literal/length code */ | ||
1976 | if ((e = (t = tl + ((uInt)b & ml))->exop) == 0) | ||
1977 | { | ||
1978 | DUMPBITS(t->bits) | ||
1979 | Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? | ||
1980 | "inflate: * literal '%c'\n" : | ||
1981 | "inflate: * literal 0x%02x\n", t->base)); | ||
1982 | *q++ = (Byte)t->base; | ||
1983 | m--; | ||
1984 | continue; | ||
1985 | } | ||
1986 | do { | ||
1987 | DUMPBITS(t->bits) | ||
1988 | if (e & 16) | ||
1989 | { | ||
1990 | /* get extra bits for length */ | ||
1991 | e &= 15; | ||
1992 | c = t->base + ((uInt)b & inflate_mask[e]); | ||
1993 | DUMPBITS(e) | ||
1994 | Tracevv((stderr, "inflate: * length %u\n", c)); | ||
1995 | |||
1996 | /* decode distance base of block to copy */ | ||
1997 | GRABBITS(15); /* max bits for distance code */ | ||
1998 | e = (t = td + ((uInt)b & md))->exop; | ||
1999 | do { | ||
2000 | DUMPBITS(t->bits) | ||
2001 | if (e & 16) | ||
2002 | { | ||
2003 | /* get extra bits to add to distance base */ | ||
2004 | e &= 15; | ||
2005 | GRABBITS(e) /* get extra bits (up to 13) */ | ||
2006 | d = t->base + ((uInt)b & inflate_mask[e]); | ||
2007 | DUMPBITS(e) | ||
2008 | Tracevv((stderr, "inflate: * distance %u\n", d)); | ||
2009 | |||
2010 | /* do the copy */ | ||
2011 | m -= c; | ||
2012 | if ((uInt)(q - s->window) >= d) /* offset before dest */ | ||
2013 | { /* just copy */ | ||
2014 | r = q - d; | ||
2015 | *q++ = *r++; c--; /* minimum count is three, */ | ||
2016 | *q++ = *r++; c--; /* so unroll loop a little */ | ||
2017 | } | ||
2018 | else /* else offset after destination */ | ||
2019 | { | ||
2020 | e = d - (q - s->window); /* bytes from offset to end */ | ||
2021 | r = s->end - e; /* pointer to offset */ | ||
2022 | if (c > e) /* if source crosses, */ | ||
2023 | { | ||
2024 | c -= e; /* copy to end of window */ | ||
2025 | do { | ||
2026 | *q++ = *r++; | ||
2027 | } while (--e); | ||
2028 | r = s->window; /* copy rest from start of window */ | ||
2029 | } | ||
2030 | } | ||
2031 | do { /* copy all or what's left */ | ||
2032 | *q++ = *r++; | ||
2033 | } while (--c); | ||
2034 | break; | ||
2035 | } | ||
2036 | else if ((e & 64) == 0) | ||
2037 | e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop; | ||
2038 | else | ||
2039 | { | ||
2040 | z->msg = "invalid distance code"; | ||
2041 | UNGRAB | ||
2042 | UPDATE | ||
2043 | return Z_DATA_ERROR; | ||
2044 | } | ||
2045 | } while (1); | ||
2046 | break; | ||
2047 | } | ||
2048 | if ((e & 64) == 0) | ||
2049 | { | ||
2050 | if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0) | ||
2051 | { | ||
2052 | DUMPBITS(t->bits) | ||
2053 | Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ? | ||
2054 | "inflate: * literal '%c'\n" : | ||
2055 | "inflate: * literal 0x%02x\n", t->base)); | ||
2056 | *q++ = (Byte)t->base; | ||
2057 | m--; | ||
2058 | break; | ||
2059 | } | ||
2060 | } | ||
2061 | else if (e & 32) | ||
2062 | { | ||
2063 | Tracevv((stderr, "inflate: * end of block\n")); | ||
2064 | UNGRAB | ||
2065 | UPDATE | ||
2066 | return Z_STREAM_END; | ||
2067 | } | ||
2068 | else | ||
2069 | { | ||
2070 | z->msg = "invalid literal/length code"; | ||
2071 | UNGRAB | ||
2072 | UPDATE | ||
2073 | return Z_DATA_ERROR; | ||
2074 | } | ||
2075 | } while (1); | ||
2076 | } while (m >= 258 && n >= 10); | ||
2077 | |||
2078 | /* not enough input or output--restore pointers and return */ | ||
2079 | UNGRAB | ||
2080 | UPDATE | ||
2081 | return Z_OK; | ||
2082 | } | ||
2083 | |||
2084 | |||
2085 | /*+++++*/ | ||
2086 | /* zutil.c -- target dependent utility functions for the compression library | ||
2087 | * Copyright (C) 1995 Jean-loup Gailly. | ||
2088 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
2089 | */ | ||
2090 | |||
2091 | /* From: zutil.c,v 1.8 1995/05/03 17:27:12 jloup Exp */ | ||
2092 | |||
2093 | char *zlib_version = ZLIB_VERSION; | ||
2094 | |||
2095 | char *z_errmsg[] = { | ||
2096 | "stream end", /* Z_STREAM_END 1 */ | ||
2097 | "", /* Z_OK 0 */ | ||
2098 | "file error", /* Z_ERRNO (-1) */ | ||
2099 | "stream error", /* Z_STREAM_ERROR (-2) */ | ||
2100 | "data error", /* Z_DATA_ERROR (-3) */ | ||
2101 | "insufficient memory", /* Z_MEM_ERROR (-4) */ | ||
2102 | "buffer error", /* Z_BUF_ERROR (-5) */ | ||
2103 | ""}; | ||
2104 | |||
2105 | |||
2106 | /*+++++*/ | ||
2107 | /* adler32.c -- compute the Adler-32 checksum of a data stream | ||
2108 | * Copyright (C) 1995 Mark Adler | ||
2109 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
2110 | */ | ||
2111 | |||
2112 | /* From: adler32.c,v 1.6 1995/05/03 17:27:08 jloup Exp */ | ||
2113 | |||
2114 | #define BASE 65521L /* largest prime smaller than 65536 */ | ||
2115 | #define NMAX 5552 | ||
2116 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ | ||
2117 | |||
2118 | #define DO1(buf) {s1 += *buf++; s2 += s1;} | ||
2119 | #define DO2(buf) DO1(buf); DO1(buf); | ||
2120 | #define DO4(buf) DO2(buf); DO2(buf); | ||
2121 | #define DO8(buf) DO4(buf); DO4(buf); | ||
2122 | #define DO16(buf) DO8(buf); DO8(buf); | ||
2123 | |||
2124 | /* ========================================================================= */ | ||
2125 | uLong adler32(adler, buf, len) | ||
2126 | uLong adler; | ||
2127 | Bytef *buf; | ||
2128 | uInt len; | ||
2129 | { | ||
2130 | unsigned long s1 = adler & 0xffff; | ||
2131 | unsigned long s2 = (adler >> 16) & 0xffff; | ||
2132 | int k; | ||
2133 | |||
2134 | if (buf == Z_NULL) return 1L; | ||
2135 | |||
2136 | while (len > 0) { | ||
2137 | k = len < NMAX ? len : NMAX; | ||
2138 | len -= k; | ||
2139 | while (k >= 16) { | ||
2140 | DO16(buf); | ||
2141 | k -= 16; | ||
2142 | } | ||
2143 | if (k != 0) do { | ||
2144 | DO1(buf); | ||
2145 | } while (--k); | ||
2146 | s1 %= BASE; | ||
2147 | s2 %= BASE; | ||
2148 | } | ||
2149 | return (s2 << 16) | s1; | ||
2150 | } | ||
diff --git a/arch/xtensa/boot/lib/zmem.c b/arch/xtensa/boot/lib/zmem.c new file mode 100644 index 000000000000..7848f126d67d --- /dev/null +++ b/arch/xtensa/boot/lib/zmem.c | |||
@@ -0,0 +1,87 @@ | |||
1 | #include "zlib.h" | ||
2 | |||
3 | /* bits taken from ppc */ | ||
4 | |||
5 | extern void *avail_ram, *end_avail; | ||
6 | |||
7 | void exit (void) | ||
8 | { | ||
9 | for (;;); | ||
10 | } | ||
11 | |||
12 | void *zalloc(void *x, unsigned items, unsigned size) | ||
13 | { | ||
14 | void *p = avail_ram; | ||
15 | |||
16 | size *= items; | ||
17 | size = (size + 7) & -8; | ||
18 | avail_ram += size; | ||
19 | if (avail_ram > end_avail) { | ||
20 | //puts("oops... out of memory\n"); | ||
21 | //pause(); | ||
22 | exit (); | ||
23 | } | ||
24 | return p; | ||
25 | } | ||
26 | |||
27 | void zfree(void *x, void *addr, unsigned nb) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | |||
32 | #define HEAD_CRC 2 | ||
33 | #define EXTRA_FIELD 4 | ||
34 | #define ORIG_NAME 8 | ||
35 | #define COMMENT 0x10 | ||
36 | #define RESERVED 0xe0 | ||
37 | |||
38 | #define DEFLATED 8 | ||
39 | |||
40 | void gunzip (void *dst, int dstlen, unsigned char *src, int *lenp) | ||
41 | { | ||
42 | z_stream s; | ||
43 | int r, i, flags; | ||
44 | |||
45 | /* skip header */ | ||
46 | |||
47 | i = 10; | ||
48 | flags = src[3]; | ||
49 | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { | ||
50 | //puts("bad gzipped data\n"); | ||
51 | exit(); | ||
52 | } | ||
53 | if ((flags & EXTRA_FIELD) != 0) | ||
54 | i = 12 + src[10] + (src[11] << 8); | ||
55 | if ((flags & ORIG_NAME) != 0) | ||
56 | while (src[i++] != 0) | ||
57 | ; | ||
58 | if ((flags & COMMENT) != 0) | ||
59 | while (src[i++] != 0) | ||
60 | ; | ||
61 | if ((flags & HEAD_CRC) != 0) | ||
62 | i += 2; | ||
63 | if (i >= *lenp) { | ||
64 | //puts("gunzip: ran out of data in header\n"); | ||
65 | exit(); | ||
66 | } | ||
67 | |||
68 | s.zalloc = zalloc; | ||
69 | s.zfree = zfree; | ||
70 | r = inflateInit2(&s, -MAX_WBITS); | ||
71 | if (r != Z_OK) { | ||
72 | //puts("inflateInit2 returned "); puthex(r); puts("\n"); | ||
73 | exit(); | ||
74 | } | ||
75 | s.next_in = src + i; | ||
76 | s.avail_in = *lenp - i; | ||
77 | s.next_out = dst; | ||
78 | s.avail_out = dstlen; | ||
79 | r = inflate(&s, Z_FINISH); | ||
80 | if (r != Z_OK && r != Z_STREAM_END) { | ||
81 | //puts("inflate returned "); puthex(r); puts("\n"); | ||
82 | exit(); | ||
83 | } | ||
84 | *lenp = s.next_out - (unsigned char *) dst; | ||
85 | inflateEnd(&s); | ||
86 | } | ||
87 | |||
diff --git a/arch/xtensa/boot/ramdisk/Makefile b/arch/xtensa/boot/ramdisk/Makefile new file mode 100644 index 000000000000..b12f76352438 --- /dev/null +++ b/arch/xtensa/boot/ramdisk/Makefile | |||
@@ -0,0 +1,23 @@ | |||
1 | # | ||
2 | # Makefile for a ramdisk image | ||
3 | # | ||
4 | |||
5 | BIG_ENDIAN := $(shell echo -e "\#ifdef __XTENSA_EL__\nint little;\n\#else\nint big;\n\#endif" | $(CC) -E -|grep -c big) | ||
6 | |||
7 | ifeq ($(BIG_ENDIAN),1) | ||
8 | OBJCOPY_ARGS := -O elf32-xtensa-be | ||
9 | else | ||
10 | OBJCOPY_ARGS := -O elf32-xtensa-le | ||
11 | endif | ||
12 | |||
13 | obj-y = ramdisk.o | ||
14 | |||
15 | RAMDISK_IMAGE = arch/$(ARCH)/boot/ramdisk/$(CONFIG_EMBEDDED_RAMDISK_IMAGE) | ||
16 | |||
17 | arch/$(ARCH)/boot/ramdisk/ramdisk.o: | ||
18 | $(Q)echo -e "dummy:" | $(AS) -o $@; | ||
19 | $(Q)$(OBJCOPY) $(OBJCOPY_ARGS) \ | ||
20 | --add-section .initrd=$(RAMDISK_IMAGE) \ | ||
21 | --set-section-flags .initrd=contents,alloc,load,load,data \ | ||
22 | arch/$(ARCH)/boot/ramdisk/ramdisk.o $@ | ||
23 | |||