1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kernel.h>
22
23#include <asm/uaccess.h>
24
25#define memzero(s,n) memset ((s),0,(n))
26#define puts srm_printk
27extern long srm_printk(const char *, ...)
28 __attribute__ ((format (printf, 1, 2)));;
29
30
31
32
33#define OF(args) args
34#define STATIC static
35
36typedef unsigned char uch;
37typedef unsigned short ush;
38typedef unsigned long ulg;
39
40#define WSIZE 0x8000
41
42
43static uch *inbuf;
44static uch *window;
45
46static unsigned insize;
47static unsigned inptr;
48static unsigned outcnt;
49
50
51#define ASCII_FLAG 0x01
52#define CONTINUATION 0x02
53#define EXTRA_FIELD 0x04
54#define ORIG_NAME 0x08
55#define COMMENT 0x10
56#define ENCRYPTED 0x20
57#define RESERVED 0xC0
58
59#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
60
61
62#ifdef DEBUG
63# define Assert(cond,msg) {if(!(cond)) error(msg);}
64# define Trace(x) fprintf x
65# define Tracev(x) {if (verbose) fprintf x ;}
66# define Tracevv(x) {if (verbose>1) fprintf x ;}
67# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
68# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
69#else
70# define Assert(cond,msg)
71# define Trace(x)
72# define Tracev(x)
73# define Tracevv(x)
74# define Tracec(c,x)
75# define Tracecv(c,x)
76#endif
77
78static int fill_inbuf(void);
79static void flush_window(void);
80static void error(char *m);
81static void gzip_mark(void **);
82static void gzip_release(void **);
83
84static char *input_data;
85static int input_data_size;
86
87static uch *output_data;
88static ulg output_ptr;
89static ulg bytes_out;
90
91static void *malloc(int size);
92static void free(void *where);
93static void error(char *m);
94static void gzip_mark(void **);
95static void gzip_release(void **);
96
97extern int end;
98static ulg free_mem_ptr;
99static ulg free_mem_ptr_end;
100
101#define HEAP_SIZE 0x2000
102
103#include "../../../lib/inflate.c"
104
105static void *malloc(int size)
106{
107 void *p;
108
109 if (size <0) error("Malloc error\n");
110 if (free_mem_ptr <= 0) error("Memory error\n");
111
112 free_mem_ptr = (free_mem_ptr + 3) & ~3;
113
114 p = (void *)free_mem_ptr;
115 free_mem_ptr += size;
116
117 if (free_mem_ptr >= free_mem_ptr_end)
118 error("Out of memory");
119 return p;
120}
121
122static void free(void *where)
123{
124}
125
126static void gzip_mark(void **ptr)
127{
128 *ptr = (void *) free_mem_ptr;
129}
130
131static void gzip_release(void **ptr)
132{
133 free_mem_ptr = (long) *ptr;
134}
135
136
137
138
139
140int fill_inbuf(void)
141{
142 if (insize != 0)
143 error("ran out of input data\n");
144
145 inbuf = input_data;
146 insize = input_data_size;
147
148 inptr = 1;
149 return inbuf[0];
150}
151
152
153
154
155
156void flush_window(void)
157{
158 ulg c = crc;
159 unsigned n;
160 uch *in, *out, ch;
161
162 in = window;
163 out = &output_data[output_ptr];
164 for (n = 0; n < outcnt; n++) {
165 ch = *out++ = *in++;
166 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
167 }
168 crc = c;
169 bytes_out += (ulg)outcnt;
170 output_ptr += (ulg)outcnt;
171 outcnt = 0;
172
173}
174
175static void error(char *x)
176{
177 puts("\n\n");
178 puts(x);
179 puts("\n\n -- System halted");
180
181 while(1);
182}
183
184unsigned int
185decompress_kernel(void *output_start,
186 void *input_start,
187 size_t ksize,
188 size_t kzsize)
189{
190 output_data = (uch *)output_start;
191 input_data = (uch *)input_start;
192 input_data_size = kzsize;
193
194
195 free_mem_ptr = (ulg)output_start + ksize;
196 free_mem_ptr_end = (ulg)output_start + ksize + 0x200000;
197
198
199
200 window = malloc(WSIZE);
201
202 makecrc();
203
204 gunzip();
205
206 return output_ptr;
207}
208