Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / util / mkelfImage / kunzip_src / lib / kunzip.c
1 #include <string.h>
2 #include <stdlib.h>
3
4 /*
5  * gzip support routine declartions..
6  * =========================================================
7  */
8
9 #ifdef DEBUG
10 #  define Trace(x)
11 #  define Tracev(x)
12 #  define Tracevv(x)
13 #  define Tracec(c,x)
14 #  define Tracecv(c,x)
15 #  define DBG(x) printf x
16 #else
17 #  define Trace(x)
18 #  define Tracev(x)
19 #  define Tracevv(x)
20 #  define Tracec(c,x)
21 #  define Tracecv(c,x)
22 #  define DBG(x)
23 #endif
24
25 void error(char *str)
26 {
27         DBG(("%s\n", str));
28 }
29
30 static unsigned char *inbuf;    /* input buffer */
31 static unsigned int insize;     /* valid bytes in inbuf */
32 static unsigned int inptr;      /* index of next byte to be processed in inbuf */
33
34 #if !defined(DEBUG)
35 #define get_byte()  (inptr < insize ? inbuf[inptr++] : 0)
36 #else
37 static unsigned char get_byte(void)
38 {
39         static int count;
40         unsigned char byte = (inptr < insize ? inbuf[inptr++] : 0);
41 #if 0
42         printf("%02x ", byte);
43         if ((++count & 0x0f) == 0) {
44                 printf("\n");
45         }
46 #endif
47         return byte;
48 }
49
50 #endif
51
52 static void flush_window(void);
53
54 static long bytes_out;          /* total bytes compressed */
55 static unsigned outcnt;         /* bytes in output buffer */
56
57 #define WSIZE 0x8000            /* Window size must be at least 32k, and a power of two */
58 static unsigned char window[WSIZE];     /* Sliding window buffer */
59
60 /*
61  * gzip declarations
62  */
63
64 #define OF(args)  args
65 #define STATIC static
66
67
68 #define memzero(s, n)     memset ((s), 0, (n))
69
70 typedef unsigned char uch;
71 typedef unsigned short ush;
72 typedef unsigned long ulg;
73
74
75
76 #include "inflate.c"
77
78
79 /* Variables that gunzip doesn't need to see... */
80 static unsigned char *output_ptr;
81 static unsigned long end_offset;
82 static struct unzip_region {
83         unsigned long start;
84         unsigned long end_offset;
85 } unzip_region;
86
87 /* Data provided by the header */
88 extern unsigned char zipped_data[];
89 extern unsigned char zipped_data_end[];
90 extern unsigned char entry;
91 /* Assembly language routines */
92 extern void jmp_to_program_entry(void *);
93
94 /* ===========================================================================
95  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
96  * (Used for the decompressed data only.)
97  */
98 static void flush_window(void)
99 {
100         ulg c = crc;            /* temporary variable */
101         unsigned n;
102         unsigned long limit;
103         uch *in, *out, ch;
104
105         limit = outcnt;
106
107
108         n = 0;
109         in = window;
110         while (n < outcnt) {
111                 limit = end_offset - bytes_out +n;
112                 if (limit > outcnt) {
113                         limit = outcnt;
114                 }
115                 out = output_ptr;
116                 DBG(("flush 0x%08lx start 0x%08lx limit 0x%08lx\n",
117                         (unsigned long) out, (unsigned long)n, limit));
118                 for (; n < limit; n++) {
119                         ch = *out++ = *in++;
120                         c = crc_32_tab[((int) c ^ ch) & 0xff] ^ (c >> 8);
121                 }
122                 crc = c;
123                 bytes_out += (out - output_ptr);
124                 output_ptr = out;
125                 if (bytes_out == end_offset) {
126                         if (output_ptr == (unsigned char *)(&unzip_region+1)) {
127                                 output_ptr = (unsigned char *)(unzip_region.start);
128                                 end_offset = unzip_region.end_offset;
129                         } else {
130                                 output_ptr = (unsigned char *)&unzip_region;
131                                 end_offset += sizeof(unzip_region);
132                         }
133                 }
134         }
135         outcnt = 0;
136 }
137
138
139 void gunzip_setup(void)
140 {
141         DBG(("gunzip_setup\n"));
142         outcnt = 0;
143         bytes_out = 0;
144
145         end_offset = sizeof(unzip_region);
146         output_ptr = (unsigned char *)&unzip_region;
147
148         inbuf = &zipped_data[0];
149         insize = zipped_data_end - zipped_data;
150         inptr = 0;
151
152         makecrc();
153         DBG(("gunzip_setup_done\n"));
154 }
155
156
157 int kunzip(int argc, char **argv)
158 {
159         DBG(("kunzip\n"));
160         gunzip_setup();
161         DBG(("pre_gunzip\n"));
162         if (gunzip() != 0) {
163                 error("gunzip failed");
164                 while(1) {}
165                 return -1;
166         }
167         DBG(("pre_jmp_to_program_entry: %p\n", &entry ));
168         jmp_to_program_entry(&entry);
169         return 0;
170 }