-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzipstream.cpp
456 lines (389 loc) · 10.4 KB
/
zipstream.cpp
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
446
447
448
449
450
451
452
453
454
455
456
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <mhash.h>
#include <list>
using namespace std;
#define BUFFERSIZE 262144
//#define USESTORE // Works
//#define USEDEFLATE // Works
//#define USEBZIP2 // Works
//#define USELZMA // Works, at least with 7Zip
#if defined(USESTORE)
// None needed.
#elif defined(USEDEFLATE)
# include <zlib.h>
# ifndef COMPRESSIONLEVEL
# define COMPRESSIONLEVEL Z_DEFAULT_COMPRESSION
# endif
#elif defined(USEBZIP2)
# include <bzlib.h>
# ifndef COMPRESSIONLEVEL
# define COMPRESSIONLEVEL 6
# endif
#elif defined(USELZMA)
# include <lzma.h>
# ifndef COMPRESSIONLEVEL
# define COMPRESSIONLEVEL LZMA_PRESET_DEFAULT
# endif
#else
# warn "No compression method selected, defaulting to STORE."
# define USESTORE
#endif
// https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
struct __attribute__((packed)) LocalHeader {
uint32_t signature;
uint16_t version;
uint16_t flags;
uint16_t method;
uint16_t mtime;
uint16_t mdate;
uint32_t crc;
uint32_t csize;
uint32_t usize;
uint16_t fnamelen;
uint16_t extralen;
};
struct __attribute__((packed)) DataDescriptor {
uint32_t signature;
uint32_t crc;
uint32_t csize;
uint32_t usize;
};
struct __attribute__((packed)) Zip64DataDescriptor {
uint32_t signature;
uint32_t crc;
uint64_t csize;
uint64_t usize;
};
struct __attribute__((packed)) DirectoryHeader {
uint32_t signature;
uint16_t cversion;
uint16_t eversion;
uint16_t flags;
uint16_t method;
uint16_t mtime;
uint16_t mdate;
uint32_t crc;
uint32_t csize;
uint32_t usize;
uint16_t fnamelen;
uint16_t extralen;
uint16_t commentlen;
uint16_t disknum;
uint16_t iattribs;
uint32_t eattribs;
uint32_t offset;
};
struct __attribute__((packed)) Zip64Info {
uint16_t signature;
uint16_t size;
uint64_t usize;
uint64_t csize;
uint64_t offset;
uint32_t disk;
};
struct __attribute__((packed)) Zip64DirectoryRecord {
uint32_t signature;
uint64_t drsize;
uint16_t cversion;
uint16_t eversion;
uint32_t disknum;
uint32_t diskdir;
uint64_t diskcount;
uint64_t direntries;
uint64_t dirsize;
uint64_t diroffset;
};
struct __attribute__((packed)) Zip64DirectoryLocator {
uint32_t signature;
uint32_t dirdisk;
uint64_t diroffset;
uint32_t diskcount;
};
struct __attribute__((packed)) DirectoryEnd {
uint32_t signature;
uint16_t disknum;
uint16_t diskdir;
uint16_t diskcount;
uint16_t direntries;
uint32_t dirsize;
uint32_t diroffset;
uint16_t zipcommentlen;
};
struct ZippedFile {
const char* name;
LocalHeader header;
size_t csize, usize;
ZippedFile(const char* n) {
name = n;
}
};
uint16_t dostime(long time) {
struct tm* btime = localtime(&time);
return (btime->tm_hour << 11) | (btime->tm_min << 5) | (btime->tm_sec / 2);
}
uint16_t dosdate(long time) {
struct tm* btime = localtime(&time);
return ((btime->tm_year-80) << 9) | ((btime->tm_mon+1) << 5) | (btime->tm_mday);
}
ZippedFile* do_file(const char* name) {
ZippedFile* file = new ZippedFile(name);
int fd = open(name, O_RDONLY);
if (fd < 0) return NULL;
struct stat st;
fstat(fd, &st);
LocalHeader header;
header.signature = 0x04034b50;
#if defined(USESTORE)
header.version = 45; // 10 = default, 20 = deflate, 21 = deflate64, 45 = zip64, 46 = bzip2, 63 = lzma
header.flags = 0b1000; // bit 1&2 describe compression, others unimportant
header.method = 0; // 0 = store, 8 = deflate, 9 = deflate64, 12=bzip2, 14 = lzma
#elif defined(USEDEFLATE)
header.version = 45;
header.flags = 0b1000; // 00x = normal compression
header.method = 8;
#elif defined(USEBZIP2)
header.version = 46;
header.flags = 0b1000; // bit 1&2 unused
header.method = 12;
#elif defined(USELZMA)
header.version = 63;
header.flags = 0b1010; // 1x = uses EOS marker to indicate end-of-stream
header.method = 14;
#endif
header.mtime = dostime(st.st_mtime);
header.mdate = dosdate(st.st_mtime);
header.crc = 0;
header.csize = 0;
header.usize = 0; // The spec says this should be left 0. Actual software varies.
header.fnamelen = strlen(name);
header.extralen = 4;
fwrite(&header, 1, sizeof(header), stdout);
fwrite(name, 1, strlen(name), stdout);
Zip64Info zi;
zi.signature = 0x0001;
zi.size = 0;
fwrite(&zi, 1, 4, stdout);
MHASH mh = mhash_init(MHASH_CRC32B);
uint8_t inbuf[BUFFERSIZE];
#ifndef USESTORE
uint8_t outbuf[BUFFERSIZE];
#endif
size_t len, total = 0;
#if defined(USESTORE)
{
while ((len = read(fd, inbuf, BUFFERSIZE)) > 0) {
mhash(mh, inbuf, len);
fwrite(inbuf, 1, len, stdout);
total += len;
}
}
#elif defined(USEDEFLATE)
{
int flush, ret;
z_stream zp;
zp.zalloc = Z_NULL;
zp.zfree = Z_NULL;
zp.opaque = Z_NULL;
ret = deflateInit2(&zp, COMPRESSIONLEVEL, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
assert(ret == Z_OK);
do {
len = read(fd, inbuf, BUFFERSIZE);
flush = (len == BUFFERSIZE) ? Z_NO_FLUSH : Z_FINISH;
mhash(mh, inbuf, len);
zp.avail_in = len;
zp.next_in = inbuf;
do {
zp.avail_out = BUFFERSIZE;
zp.next_out = outbuf;
deflate(&zp, flush);
len = BUFFERSIZE - zp.avail_out;
fwrite(outbuf, 1, len, stdout);
total += len;
} while (zp.avail_out == 0);
} while (flush != Z_FINISH);
deflateEnd(&zp);
}
#elif defined(USEBZIP2)
{
int action, ret;
bz_stream zp;
zp.bzalloc = NULL;
zp.bzfree = NULL;
zp.opaque = NULL;
ret = BZ2_bzCompressInit(&zp, COMPRESSIONLEVEL, 0, 0);
assert(ret == BZ_OK);
do {
len = read(fd, inbuf, BUFFERSIZE);
action = (len == BUFFERSIZE) ? BZ_RUN : BZ_FINISH;
mhash(mh, inbuf, len);
zp.avail_in = len;
zp.next_in = (char*)inbuf;
do {
zp.avail_out = BUFFERSIZE;
zp.next_out = (char*)outbuf;
ret = BZ2_bzCompress(&zp, action);
assert(ret == BZ_RUN_OK || ret == BZ_FINISH_OK || ret == BZ_STREAM_END);
len = BUFFERSIZE - zp.avail_out;
fwrite(outbuf, 1, len, stdout);
total += len;
} while (zp.avail_out == 0 && ret != BZ_STREAM_END);
} while (action != BZ_FINISH);
BZ2_bzCompressEnd(&zp);
}
#elif defined(USELZMA)
{
int ret;
bool firstblock = true;
lzma_action action = LZMA_RUN;
lzma_options_lzma opt;
ret = lzma_lzma_preset(&opt, COMPRESSIONLEVEL);
assert(ret == LZMA_OK);
lzma_stream zp = LZMA_STREAM_INIT;
ret = lzma_alone_encoder(&zp, &opt);
if (ret != LZMA_OK) {
fprintf(stderr, "lzma_alone_encoder returned %d\n", ret);
switch (ret) {
case LZMA_MEM_ERROR:
fprintf(stderr, "LZMA_MEM_ERROR\n");
break;
case LZMA_OPTIONS_ERROR:
fprintf(stderr, "LZMA_OPTIONS_ERROR\n");
break;
case LZMA_UNSUPPORTED_CHECK:
fprintf(stderr, "LZMA_UNSUPPORTED_CHECK\n");
break;
}
exit(3);
}
do {
len = read(fd, inbuf, BUFFERSIZE);
action = (len == BUFFERSIZE) ? LZMA_RUN : LZMA_FINISH;
mhash(mh, inbuf, len);
zp.avail_in = len;
zp.next_in = inbuf;
do {
zp.avail_out = BUFFERSIZE;
zp.next_out = outbuf;
ret = lzma_code(&zp, action);
assert(ret == LZMA_OK || ret == LZMA_STREAM_END);
len = BUFFERSIZE - zp.avail_out;
if (firstblock) {
// liblzma: 5-byte params, 8-byte length header
// 5D 00 00 80 00 | FF FF FF FF FF FF FF FF | 00 1E 0F CA
// 7zip: 4-byte signature, 5-byte params
// 14 00 05 00 | 5D 00 10 00 00 | 00 1E 0F CA
firstblock = false;
const uint32_t signature = 0x00050014;
fwrite(&signature, 1, 4, stdout);
fwrite(outbuf, 1, 5, stdout);
fwrite(outbuf+13, 1, len-13, stdout);
total += len-4;
} else {
fwrite(outbuf, 1, len, stdout);
total += len;
}
} while (zp.avail_out == 0 && ret != LZMA_STREAM_END);
} while (action == LZMA_RUN);
lzma_end(&zp);
}
#endif
mhash_deinit(mh, &header.crc);
header.usize = st.st_size < 0xFFFFFFFF ? st.st_size : 0xFFFFFFFF;
header.csize = total < 0xFFFFFFFF ? total : 0xFFFFFFFF;
Zip64DataDescriptor dd;
dd.signature = 0x08074b50;
dd.crc = header.crc;
dd.csize = total;
dd.usize = st.st_size;
fwrite(&dd, sizeof(dd), 1, stdout);
file->csize = total;
file->usize = st.st_size;
file->header = header;
return file;
}
int main(int argc, char* argv[]) {
list<ZippedFile*> files;
for (int f = 1; f < argc; f++) {
ZippedFile* file = do_file(argv[f]);
if (file == NULL) {
fprintf(stderr, "Error zipping '%s': %m\n", argv[f]);
return 2;
}
files.push_back(file);
}
size_t offset = 0, dirsize = 0;
for (auto i = files.begin(); i != files.end(); i++) {
LocalHeader* lh = &((*i)->header);
DirectoryHeader dh;
dh.signature = 0x02014b50;
dh.cversion = 3; // UNIX
memcpy(&(dh.eversion), &(lh->version), sizeof(LocalHeader)-4);
dh.commentlen = 0;
dh.disknum = 0;
dh.iattribs = 0;
dh.eattribs = 0;
dh.offset = offset < 0xFFFFFFFF ? offset : 0xFFFFFFFF;
Zip64Info zi;
zi.signature = 0x0001;
zi.size = 0;
zi.usize = (*i)->usize;
zi.csize = (*i)->csize;
zi.offset = offset;
if (dh.usize == 0xFFFFFFFF) zi.size += 8;
if (dh.csize == 0xFFFFFFFF) zi.size += 8;
if (dh.offset == 0xFFFFFFFF) zi.size += 8;
dh.extralen = zi.size + 4;
fwrite(&dh, 1, sizeof(dh), stdout);
fwrite((*i)->name, 1, dh.fnamelen, stdout);
fwrite(&zi, 1, 4, stdout);
if (dh.usize == 0xFFFFFFFF) fwrite(&zi.usize, 1, 8, stdout);
if (dh.csize == 0xFFFFFFFF) fwrite(&zi.csize, 1, 8, stdout);
if (dh.offset == 0xFFFFFFFF) fwrite(&zi.offset, 1, 8, stdout);
offset += sizeof(LocalHeader) + dh.fnamelen + 4 + dh.csize + sizeof(Zip64DataDescriptor);
dirsize += sizeof(DirectoryHeader) + dh.fnamelen + dh.extralen;
delete *i;
}
Zip64DirectoryRecord dr;
dr.signature = 0x06064b50;
dr.drsize = sizeof(dr) - 12;
dr.cversion = 3; // Spec suggests this should be 3 = unix, 7Zip sets it matching eversion.
#if defined(USESTORE) || defined(USEDEFLATE)
dr.eversion = 45; // 45 = zip64, 46 = bzip2, 63 = lzma
#elif defined(USEBZIP2)
dr.eversion = 46;
#elif defined(USELZMA)
dr.eversion = 63;
#endif
dr.disknum = 0;
dr.diskdir = 0;
dr.diskcount = argc-1;
dr.direntries = argc-1;
dr.dirsize = dirsize;
dr.diroffset = offset;
fwrite(&dr, 1, sizeof(dr), stdout);
Zip64DirectoryLocator dl;
dl.signature = 0x07064b50;
dl.dirdisk = 0;
dl.diroffset = offset+dirsize;
dl.diskcount = 1;
fwrite(&dl, 1, sizeof(dl), stdout);
DirectoryEnd de;
de.signature = 0x06054b50;
de.disknum = 0;
de.diskdir = 0;
de.diskcount = argc-1;
de.direntries = argc-1;
de.dirsize = dirsize < 0xFFFFFFFF ? dirsize : 0xFFFFFFFF;
de.diroffset = offset < 0xFFFFFFFF ? offset : 0xFFFFFFFF;
de.zipcommentlen = 0;
fwrite(&de, 1, sizeof(de), stdout);
fflush(stdout);
fclose(stdout);
return 0;
}