blob: d9862aa8ca255c9731179575d29a253fe2c6db3c [file] [log] [blame]
Chris Zankele7d163f2005-06-30 02:58:59 -07001#include <linux/zlib.h>
Chris Zankel4bedea92005-06-23 22:01:12 -07002
3/* bits taken from ppc */
4
5extern void *avail_ram, *end_avail;
6
7void exit (void)
8{
9 for (;;);
10}
11
Chris Zankele7d163f2005-06-30 02:58:59 -070012void *zalloc(unsigned size)
Chris Zankel4bedea92005-06-23 22:01:12 -070013{
14 void *p = avail_ram;
15
Chris Zankel4bedea92005-06-23 22:01:12 -070016 size = (size + 7) & -8;
17 avail_ram += size;
18 if (avail_ram > end_avail) {
19 //puts("oops... out of memory\n");
20 //pause();
21 exit ();
22 }
23 return p;
24}
25
Chris Zankel4bedea92005-06-23 22:01:12 -070026#define HEAD_CRC 2
27#define EXTRA_FIELD 4
28#define ORIG_NAME 8
29#define COMMENT 0x10
30#define RESERVED 0xe0
31
32#define DEFLATED 8
33
34void gunzip (void *dst, int dstlen, unsigned char *src, int *lenp)
35{
36 z_stream s;
37 int r, i, flags;
38
39 /* skip header */
Chris Zankel4bedea92005-06-23 22:01:12 -070040 i = 10;
41 flags = src[3];
42 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
43 //puts("bad gzipped data\n");
44 exit();
45 }
46 if ((flags & EXTRA_FIELD) != 0)
47 i = 12 + src[10] + (src[11] << 8);
48 if ((flags & ORIG_NAME) != 0)
49 while (src[i++] != 0)
50 ;
51 if ((flags & COMMENT) != 0)
52 while (src[i++] != 0)
53 ;
54 if ((flags & HEAD_CRC) != 0)
55 i += 2;
56 if (i >= *lenp) {
57 //puts("gunzip: ran out of data in header\n");
58 exit();
59 }
60
Chris Zankele7d163f2005-06-30 02:58:59 -070061 s.workspace = zalloc(zlib_inflate_workspacesize());
62 r = zlib_inflateInit2(&s, -MAX_WBITS);
Chris Zankel4bedea92005-06-23 22:01:12 -070063 if (r != Z_OK) {
64 //puts("inflateInit2 returned "); puthex(r); puts("\n");
65 exit();
66 }
67 s.next_in = src + i;
68 s.avail_in = *lenp - i;
69 s.next_out = dst;
70 s.avail_out = dstlen;
Chris Zankele7d163f2005-06-30 02:58:59 -070071 r = zlib_inflate(&s, Z_FINISH);
Chris Zankel4bedea92005-06-23 22:01:12 -070072 if (r != Z_OK && r != Z_STREAM_END) {
73 //puts("inflate returned "); puthex(r); puts("\n");
74 exit();
75 }
76 *lenp = s.next_out - (unsigned char *) dst;
Chris Zankele7d163f2005-06-30 02:58:59 -070077 zlib_inflateEnd(&s);
Chris Zankel4bedea92005-06-23 22:01:12 -070078}
79