4 * This is a collection of several routines from gzip-1.0.3
7 * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
8 * puts by Nick Holloway 1993, better puts by Martin Mares 1995
9 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
11 * Adapted for MEMDISK by H. Peter Anvin, April 2003
18 #undef DEBUG /* Means something different for this file */
27 #define memzero(s, n) memset ((s), 0, (n))
33 #define WSIZE 0x8000 /* Window size must be at least 32k, */
34 /* and a power of two */
36 static uch
*inbuf
; /* input pointer */
37 static uch window
[WSIZE
]; /* sliding output window buffer */
39 static unsigned insize
; /* total input bytes read */
40 static unsigned inbytes
; /* valid bytes in inbuf */
41 static unsigned outcnt
; /* bytes in output buffer */
44 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
45 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
46 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
47 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
48 #define COMMENT 0x10 /* bit 4 set: file comment present */
49 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
50 #define RESERVED 0xC0 /* bit 6,7: reserved */
52 /* Diagnostic functions */
54 # define Assert(cond,msg) {if(!(cond)) error(msg);}
55 # define Trace(x) fprintf x
56 # define Tracev(x) {if (verbose) fprintf x ;}
57 # define Tracevv(x) {if (verbose>1) fprintf x ;}
58 # define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
59 # define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
61 # define Assert(cond,msg)
69 static int fill_inbuf(void);
70 static void flush_window(void);
71 static void error(char *m
);
72 static void gzip_mark(void **);
73 static void gzip_release(void **);
75 static ulg crc_32_tab
[256];
77 /* Get byte from input buffer */
78 static inline uch
get_byte(void)
85 return fill_inbuf(); /* Input buffer underrun */
89 /* Unget byte from input buffer */
90 static inline void unget_byte(void)
96 static ulg bytes_out
= 0; /* Number of bytes output */
97 static uch
*output_data
; /* Output data pointer */
98 static ulg output_size
; /* Number of output bytes expected */
100 static void *malloc(int size
);
101 static void free(void *where
);
103 static ulg free_mem_ptr
, free_mem_end_ptr
;
107 static void *malloc(int size
)
112 error("malloc error");
114 free_mem_ptr
= (free_mem_ptr
+ 3) & ~3; /* Align */
116 p
= (void *)free_mem_ptr
;
117 free_mem_ptr
+= size
;
119 if (free_mem_ptr
>= free_mem_end_ptr
)
120 error("out of memory");
125 static void free(void *where
)
131 static void gzip_mark(void **ptr
)
133 *ptr
= (void *)free_mem_ptr
;
136 static void gzip_release(void **ptr
)
138 free_mem_ptr
= (long)*ptr
;
141 /* ===========================================================================
142 * Fill the input buffer. This is called only when the buffer is empty
143 * and at least one byte is really needed.
145 static int fill_inbuf(void)
147 /* This should never happen. We have already pointed the algorithm
148 to all the data we have. */
149 die("failed\nDecompression error: ran out of input data\n");
152 /* ===========================================================================
153 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
154 * (Used for the decompressed data only.)
156 static void flush_window(void)
158 ulg c
= crc
; /* temporary variable */
162 if (bytes_out
+ outcnt
> output_size
)
163 error("output buffer overrun");
167 for (n
= 0; n
< outcnt
; n
++) {
169 c
= crc_32_tab
[(c
^ ch
) & 0xff] ^ (c
>> 8);
173 bytes_out
+= (ulg
) outcnt
;
177 static void error(char *x
)
179 die("failed\nDecompression error: %s\n", x
);
190 } __attribute__ ((packed
));
191 /* (followed by optional and variable length "extra", "original name",
192 and "comment" fields) */
194 struct gzip_trailer
{
197 } __attribute__ ((packed
));
200 * <http://www.pkware.com/products/enterprise/white_papers/appnote.html>.
202 struct pkzip_header
{
207 uint16_t modified_time
;
208 uint16_t modified_date
;
212 uint16_t filename_len
;
214 } __attribute__ ((packed
));
215 /* (followed by optional and variable length "filename" and "extra"
219 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
220 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
221 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
222 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
223 #define COMMENT 0x10 /* bit 4 set: file comment present */
224 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
225 #define RESERVED 0xC0 /* bit 6,7: reserved */
227 /* pkzip flag byte */
228 #define PK_ENCRYPTED 0x01 /* bit 0 set: file is encrypted */
229 #define PK_DATADESC 0x08 /* bit 3 set: file has trailing "data
231 #define PK_UNSUPPORTED 0xFFF0 /* All other bits must be zero */
233 /* Return 0 if (indata, size) points to a ZIP file, and fill in
234 compressed data size, uncompressed data size, CRC, and offset of
237 If indata is not a ZIP file, return -1. */
238 int check_zip(void *indata
, uint32_t size
, uint32_t * zbytes_p
,
239 uint32_t * dbytes_p
, uint32_t * orig_crc
, uint32_t * offset_p
)
241 struct gzip_header
*gzh
= (struct gzip_header
*)indata
;
242 struct pkzip_header
*pkzh
= (struct pkzip_header
*)indata
;
245 if (gzh
->magic
== 0x8b1f) {
246 struct gzip_trailer
*gzt
= indata
+ size
- sizeof(struct gzip_trailer
);
247 /* We only support method #8, DEFLATED */
248 if (gzh
->method
!= 8) {
249 error("gzip file uses invalid method");
252 if (gzh
->flags
& ENCRYPTED
) {
253 error("gzip file is encrypted; not supported");
256 if (gzh
->flags
& CONTINUATION
) {
257 error("gzip file is a continuation file; not supported");
260 if (gzh
->flags
& RESERVED
) {
261 error("gzip file has unsupported flags");
264 offset
= sizeof(*gzh
);
265 if (gzh
->flags
& EXTRA_FIELD
) {
266 /* Skip extra field */
267 unsigned len
= *(unsigned *)(indata
+ offset
);
270 if (gzh
->flags
& ORIG_NAME
) {
271 /* Discard the old name */
273 while (p
[offset
] != 0 && offset
< size
) {
279 if (gzh
->flags
& COMMENT
) {
280 /* Discard the comment */
282 while (p
[offset
] != 0 && offset
< size
) {
289 error("gzip file corrupt");
292 *zbytes_p
= size
- offset
- sizeof(struct gzip_trailer
);
293 *dbytes_p
= gzt
->dbytes
;
294 *orig_crc
= gzt
->crc
;
297 } else if (pkzh
->magic
== 0x04034b50UL
) {
298 /* Magic number matches pkzip file. */
300 offset
= sizeof(*pkzh
);
301 if (pkzh
->flags
& PK_ENCRYPTED
) {
302 error("pkzip file is encrypted; not supported");
305 if (pkzh
->flags
& PK_DATADESC
) {
306 error("pkzip file uses data_descriptor field; not supported");
309 if (pkzh
->flags
& PK_UNSUPPORTED
) {
310 error("pkzip file has unsupported flags");
314 /* We only support method #8, DEFLATED */
315 if (pkzh
->method
!= 8) {
316 error("pkzip file uses invalid method");
320 offset
= sizeof(*pkzh
);
322 offset
+= pkzh
->filename_len
;
323 /* skip extra field */
324 offset
+= pkzh
->extra_len
;
326 if (offset
+ pkzh
->zbytes
> size
) {
327 error("pkzip file corrupt");
331 *zbytes_p
= pkzh
->zbytes
;
332 *dbytes_p
= pkzh
->dbytes
;
333 *orig_crc
= pkzh
->crc
;
337 /* Magic number does not match. */
341 error("Internal error in check_zip");
346 * Decompress the image, trying to flush the end of it as close
347 * to end_mem as possible. Return a pointer to the data block,
348 * and change datalen.
352 static char heap
[65536];
354 void *unzip(void *indata
, uint32_t zbytes
, uint32_t dbytes
,
355 uint32_t orig_crc
, void *target
)
357 /* Set up the heap; it is simply a chunk of bss memory */
358 free_mem_ptr
= (size_t)heap
;
359 free_mem_end_ptr
= (size_t)heap
+ sizeof heap
;
361 /* Set up input buffer */
363 /* Sometimes inflate() looks beyond the end of the compressed data,
364 but it always backs up before it is done. So we give it 4 bytes
366 insize
= inbytes
= zbytes
+ 4;
368 /* Set up output buffer */
370 output_data
= target
;
371 output_size
= dbytes
;
377 /* Verify that gunzip() consumed the entire input. */
379 error("compressed data length error");
381 /* Check the uncompressed data length and CRC. */
382 if (bytes_out
!= dbytes
)
383 error("uncompressed data length error");
385 if (orig_crc
!= CRC_VALUE
)