-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.c
446 lines (364 loc) · 9.45 KB
/
zip.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <zlib.h>
#include "zip.h"
/* Central Directory Header Signature */
#define CDHDR_SIG 0x02014b50
/* Local File Header Signature */
#define LFHDR_SIG 0x04034b50
/* End-Of-Central-Directory-Record Signature */
#define EOCDR_SIG 0x06054b50
#define COMPRESSION_METHOD_NONE 0x0000
#define COMPRESSION_METHOD_DEFLATE 0x0008
/* Local File Header */
struct lfhdr {
uint32_t sig;
uint16_t vers_needed_to_extract;
uint16_t flags;
uint16_t compression_method;
uint16_t last_modif_time;
uint16_t last_modif_date;
uint32_t crc32;
uint32_t compressed_sz;
uint32_t uncompressed_sz;
uint16_t fname_len;
uint16_t extra_field_len;
} __attribute__ ((packed));
/*
struct data_descr {
uint32_t crc32;
uint32_t compressed_sz;
uint32_t uncompressed_sz;
} __attribute__ ((packed));
*/
/* Central Directory Header */
struct cdhdr {
uint32_t sig;
uint16_t vers_made_by;
uint16_t vers_needed_to_extract;
uint16_t flags;
uint16_t compression_method;
uint16_t last_modif_time;
uint16_t last_modif_date;
uint32_t crc32;
uint32_t compressed_sz;
uint32_t uncompressed_sz;
uint16_t fname_len;
uint16_t extra_field_len;
uint16_t comment_len;
uint16_t disk_number_start;
uint16_t internal_file_attr;
uint32_t external_file_attr;
uint32_t lfhdr_off;
} __attribute__ ((packed));
/* End-Of-Central-Directory Record */
struct eocdr {
uint32_t sig;
uint16_t cur_disk;
uint16_t central_dir_start_disk;
uint16_t nentries;
uint16_t nentries_total;
uint32_t central_dir_sz;
uint32_t central_dir_off;
uint16_t comment_sz;
} __attribute__ ((packed));
#define READ_EOCDR_BUF 4096
static int read_eocdr(FILE *fp, struct eocdr *r, struct ebuf *ebuf)
{
int n;
char buf[READ_EOCDR_BUF];
char *p;
/* Suppose, that EOCDR is in the last N bytes */
if (fseek(fp, -READ_EOCDR_BUF-1, SEEK_END)) {
ebuf_add(ebuf, "zip: failed to seek while looking for End-Of-Central-Dir Record: %s\n", strerror(errno));
return -1;
}
n = fread(buf, 1, READ_EOCDR_BUF, fp);
if (n <= 0) {
ebuf_add(ebuf, "zip: failed to read the End-Of-Central-Dir Record: %s\n", strerror(errno));
return -1;
}
p = buf-1;
do {
p++;
/* Find the first byte of EOCDR signature */
p = memchr(p, 0x50, buf + n - p);
} while (p && (buf + n - p >= 4) && *(uint32_t *)p != EOCDR_SIG);
if (!p || (buf + n - p < 4)) {
ebuf_add(ebuf, "zip: failed to found the End-Of-Central-Dir Record\n");
return -1;
}
memcpy(r, p, sizeof(*r));
if (r->nentries != r->nentries_total ||
r->cur_disk != r->central_dir_start_disk) {
ebuf_add(ebuf, "zip: unexpected values in the End-Of-Central-Dir Record\n");
return -1;
}
return 0;
}
static int ls_central_dir(FILE *fp, struct eocdr *eocdr, struct ebuf *ebuf,
int (*f)(FILE *, struct cdhdr *, const char *, int *, void *), void
*f_priv)
{
struct cdhdr cdhdr;
const char *p;
int fnlen, nentries = (int)eocdr->nentries_total, fin;
char fname[256];
long cur_pos;
if (fseek(fp, (long)eocdr->central_dir_off, SEEK_SET)) {
ebuf_add(ebuf, "zip: failed to seek to the Central Dir: %s\n", strerror(errno));
return -1;
}
fin = 0;
while (nentries-- && !fin) {
if (fread(&cdhdr, sizeof(cdhdr), 1, fp) != 1) {
ebuf_add(ebuf, "zip: failed to read Central Dir header: %s\n", strerror(errno));
return -1;
}
if (cdhdr.sig != CDHDR_SIG) {
ebuf_add(ebuf, "zip: invalid Central Dir header signature\n");
return -1;
}
fnlen = cdhdr.fname_len;
if (fnlen > sizeof(fname) - 1) {
ebuf_add(ebuf, "zip: too long fname in Central Dir header: %d\n", fnlen);
return -1;
}
if (fread(fname, fnlen, 1, fp) != 1) {
ebuf_add(ebuf, "zip: failed to read Central Dir fname: %s\n", strerror(errno));
return -1;
}
fname[fnlen] = '\0';
cur_pos = ftell(fp);
if (f(fp, &cdhdr, fname, &fin, f_priv))
return -1;
cur_pos += cdhdr.extra_field_len + cdhdr.comment_len;
if (fseek(fp, cur_pos, SEEK_SET)) {
ebuf_add(ebuf, "zip: failed to seek to the next Central Dir header: %s\n", strerror(errno));
return -1;
}
}
return 0;
}
struct extract_ctx {
const char *fname;
int (*wr)(const char *, int, void *);
void *wr_priv;
int found;
struct ebuf *ebuf;
};
static int decompress(FILE *in, struct cdhdr *cdhdr, struct extract_ctx *ctx)
{
char ibuf[1024];
char obuf[1024];
int r, nleft, err = -1;
z_stream zs;
unsigned long crc;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
zs.avail_in = 0;
zs.next_in = Z_NULL;
r = inflateInit2(&zs, -15);
if (r != Z_OK) {
ebuf_add(ctx->ebuf, "zip: failed to init zlib inflate stream: %d", r);
return -1;
}
for (nleft = cdhdr->compressed_sz, crc = crc32(0L, Z_NULL, 0); nleft && r != Z_STREAM_END;) {
zs.avail_in = fread(ibuf, 1, nleft > sizeof(ibuf) ? sizeof(ibuf) : nleft, in);
if (ferror(in)) {
ebuf_add(ctx->ebuf, "zip: failed to read: %s\n", strerror(errno));
goto fin;
}
if (zs.avail_in == 0) {
ebuf_add(ctx->ebuf, "zip: read unexpected EOF while decompressing\n");
goto fin;
}
nleft -= zs.avail_in;
zs.next_in = ibuf;
do {
zs.avail_out = sizeof(obuf);
zs.next_out = obuf;
r = inflate(&zs, Z_NO_FLUSH);
if (r == Z_NEED_DICT || r == Z_DATA_ERROR ||
r == Z_MEM_ERROR) {
ebuf_add(ctx->ebuf, "zip: zlib inflate failed: %d\n", r);
goto fin;
}
if (ctx->wr(obuf, sizeof(obuf) - zs.avail_out, ctx->wr_priv)) {
ebuf_add(ctx->ebuf, "zip: failed to write decompressed data\n");
goto fin;
}
crc = crc32(crc, obuf, sizeof(obuf) - zs.avail_out);
} while (zs.avail_out == 0);
}
if (r != Z_STREAM_END) {
ebuf_add(ctx->ebuf, "zip: unexpected end of compressed data\n");
goto fin;
}
if (nleft) {
ebuf_add(ctx->ebuf, "zip: unexpected end of zlib inflate stream\n");
goto fin;
}
if (crc != cdhdr->crc32) {
ebuf_add(ctx->ebuf, "zip: CRC mismatch\n");
goto fin;
}
err = 0;
fin:
inflateEnd(&zs);
return err;
}
static int extract(FILE *fp, struct cdhdr *cdhdr, const char *fname, int *fin,
void *priv)
{
struct lfhdr lfhdr;
int nleft, n;
unsigned long crc;
struct extract_ctx *ctx = (struct extract_ctx *)priv;
char buf[256];
if (strcmp(fname, ctx->fname))
return 0;
ctx->found = 1;
*fin = 1;
if (fseek(fp, cdhdr->lfhdr_off, SEEK_SET)) {
ebuf_add(ctx->ebuf, "zip: failed to seek to Local File header off=%ld: %s\n",
(long)cdhdr->lfhdr_off, strerror(errno));
return -1;
}
if (fread(&lfhdr, sizeof(lfhdr), 1, fp) != 1) {
ebuf_add(ctx->ebuf, "zip: failed to read Local File header: %s\n",
strerror(errno));
return -1;
}
if (lfhdr.sig != LFHDR_SIG) {
ebuf_add(ctx->ebuf, "zip: invalid Local File header signature\n");
return -1;
}
/* TODO: compare CDHDR to LFHDR */
if (fseek(fp, lfhdr.fname_len + lfhdr.extra_field_len, SEEK_CUR)) {
ebuf_add(ctx->ebuf, "zip: failed to seek to file data: %s\n",
strerror(errno));
return -1;
}
if (lfhdr.compression_method) {
if (lfhdr.compression_method == COMPRESSION_METHOD_DEFLATE) {
if (decompress(fp, cdhdr, ctx))
return -1;
goto fin;
}
ebuf_add(ctx->ebuf, "zip: file compression method is not deflate\n");
return -1;
}
/* No compression */
crc = crc32(0L, Z_NULL, 0);
for (nleft = lfhdr.compressed_sz; nleft; nleft -= n) {
n = fread(buf, 1, nleft > sizeof(buf) ? sizeof(buf) : nleft, fp);
if (n <= 0) {
ebuf_add(ctx->ebuf, "zip: failed to read: %s\n",
strerror(errno));
return -1;
}
if (ctx->wr(buf, n, ctx->wr_priv)) {
ebuf_add(ctx->ebuf, "zip: failed to write extracted data\n");
return -1;
}
crc = crc32(crc, (unsigned char *)buf, n);
}
if (lfhdr.crc32 != crc) {
ebuf_add(ctx->ebuf, "zip: extracted file CRC mismatch\n");
return -1;
}
fin:
if (ctx->wr(NULL, 0, ctx->wr_priv)) {
ebuf_add(ctx->ebuf, "zip: failed to write extracted extracted data: %s\n");
return -1;
}
return 0;
}
int zip_extract(const char *zip, const char *fname,
int (*wr)(const char *, int, void *), void *wr_priv,
struct ebuf *ebuf)
{
struct eocdr eocdr;
FILE *fp;
int r = -1;
struct extract_ctx ctx;
fp = fopen(zip, "rb+");
if (!fp) {
ebuf_add(ebuf, "zip: failed to open zip-file: %s\n",
strerror(errno));
return -1;
}
if (read_eocdr(fp, &eocdr, ebuf))
goto fin;
ctx.fname = fname;
ctx.wr = wr;
ctx.wr_priv = wr_priv;
ctx.ebuf = ebuf;
ctx.found = 0;
r = ls_central_dir(fp, &eocdr, ebuf, extract, &ctx);
if (!r) {
if (!ctx.found) {
ebuf_add(ebuf, "zip: file not found\n");
r = -1;
}
}
fin:
fclose(fp);
return r;
}
#ifdef ZIP_MAIN
struct extr_wr_ctx {
FILE *fp;
struct ebuf *ebuf;
};
static int extr_wr(const char *buf, int n, void *priv)
{
struct extr_wr_ctx *ctx = (struct extr_wr_ctx *)priv;
if (!n) { /* End of data? */
if (fflush(ctx->fp))
goto wr_err;
} else {
if (fwrite(buf, n, 1, ctx->fp) != 1)
goto wr_err;
}
return 0;
wr_err:
ebuf_add(ctx->ebuf, "zip: failed to write to file: %s\n",
strerror(errno));
return -1;
}
int main(int argc, char *argv[])
{
struct ebuf ebuf;
char ebuf_buf[256];
FILE *fp;
struct extr_wr_ctx ewr_ctx;
if (argc != 4) {
fprintf(stderr, "Extract file from zip-archive.\n Usage: <zip-file> <fname> <to>\n");
return -1;
}
fp = fopen(argv[3], "wb");
if (!fp) {
fprintf(stderr, "Failed to create output file: %s\n",
strerror(errno));
return -1;
}
ebuf_init(&ebuf, ebuf_buf, sizeof(ebuf_buf));
ewr_ctx.fp = fp;
ewr_ctx.ebuf = &ebuf;
if (zip_extract(argv[1], argv[2], extr_wr, &ewr_ctx, &ebuf)) {
fprintf(stderr, "%s\n", ebuf_s(&ebuf));
fclose(fp);
return -1;
}
fclose(fp);
printf("OK\n");
return 0;
}
#endif