1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#include "u.h"
#include "lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include <flate.h>
typedef struct Biobuf Biobuf;
struct Biobuf
{
uchar *bp;
uchar *p;
uchar *ep;
};
static int header(Biobuf*);
static int trailer(Biobuf*, Biobuf*);
static int getc(void*);
static ulong offset(Biobuf*);
static int crcwrite(void *out, void *buf, int n);
static ulong get4(Biobuf *b);
static ulong Boffset(Biobuf *bp);
/* GZIP flags */
enum {
Ftext= (1<<0),
Fhcrc= (1<<1),
Fextra= (1<<2),
Fname= (1<<3),
Fcomment= (1<<4),
GZCRCPOLY = 0xedb88320UL,
};
static ulong *crctab;
static ulong crc;
extern void diff(char*); //XXX
int
gunzip(uchar *out, int outn, uchar *in, int inn)
{
Biobuf bin, bout;
int err;
crc = 0;
crctab = mkcrctab(GZCRCPOLY);
err = inflateinit();
if(err != FlateOk)
print("inflateinit failed: %s\n", flateerr(err));
bin.bp = bin.p = in;
bin.ep = in+inn;
bout.bp = bout.p = out;
bout.ep = out+outn;
err = header(&bin);
if(err != FlateOk)
return err;
err = inflate(&bout, crcwrite, &bin, getc);
if(err != FlateOk)
print("inflate failed: %s\n", flateerr(err));
err = trailer(&bout, &bin);
if(err != FlateOk)
return err;
return Boffset(&bout);
}
static int
header(Biobuf *bin)
{
int i, flag;
if(getc(bin) != 0x1f || getc(bin) != 0x8b){
print("bad magic\n");
return FlateCorrupted;
}
if(getc(bin) != 8){
print("unknown compression type\n");
return FlateCorrupted;
}
flag = getc(bin);
/* mod time */
get4(bin);
/* extra flags */
getc(bin);
/* OS type */
getc(bin);
if(flag & Fextra)
for(i=getc(bin); i>0; i--)
getc(bin);
/* name */
if(flag&Fname)
while(getc(bin) != 0)
;
/* comment */
if(flag&Fcomment)
while(getc(bin) != 0)
;
/* crc16 */
if(flag&Fhcrc) {
getc(bin);
getc(bin);
}
return FlateOk;
}
static int
trailer(Biobuf *bout, Biobuf *bin)
{
/* crc32 */
if(crc != get4(bin)){
print("crc mismatch\n");
return FlateCorrupted;
}
/* length */
if(get4(bin) != Boffset(bout)){
print("bad output len\n");
return FlateCorrupted;
}
return FlateOk;
}
static ulong
get4(Biobuf *b)
{
ulong v;
int i, c;
v = 0;
for(i = 0; i < 4; i++){
c = getc(b);
v |= c << (i * 8);
}
return v;
}
static int
getc(void *in)
{
Biobuf *bp = in;
// if((bp->p - bp->bp) % 10000 == 0)
// print(".");
if(bp->p >= bp->ep)
return -1;
return *bp->p++;
}
static ulong
Boffset(Biobuf *bp)
{
return bp->p - bp->bp;
}
static int
crcwrite(void *out, void *buf, int n)
{
Biobuf *bp;
int nn;
crc = blockcrc(crctab, crc, buf, n);
bp = out;
nn = n;
if(nn > bp->ep-bp->p)
nn = bp->ep-bp->p;
if(nn > 0)
memmove(bp->p, buf, nn);
bp->p += n;
return n;
}
#undef malloc
#undef free
void *
malloc(ulong n)
{
return ialloc(n, 8);
}
void
free(void *)
{
}
|