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 | 1
1
1
1
1
1
1
1
1
1
1
1
1
21
21
21
21
21
21
21
21
21
21
1
21
21
21
21
21
21
21
21
1
8
8
8
1
29
29
29
29
29
29
29
29
316
28
28
28
27
27
27
27
27
1
26
26
1
1
1
26
26
26
26
26
18
26
26
26
26
26
26
26
26
26
1
18
18
18
1
5
5
1
5
5
5
1
69
21
21
21
48
48
48
48
48
48
48
48
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
47
82
82
82
82
82
82
82
82
47
47
47
47
47
46
44
43
43
1
33
33
33
33
33
33
33
33
33
32
32
32
32
32
5
4
1
31
31
31
25
1
30
30
4
30
33
1
1
34
1
34
34
34
34
34
34
34
34
1
157
157
157
157
157
1
1
120
20
100
100
189
100
1
3
| var fs = require("fs");
var zlib = require("zlib");
var fd_slicer = require("fd-slicer");
var util = require("util");
var EventEmitter = require("events").EventEmitter;
var PassThrough = require("stream").PassThrough;
exports.open = open;
exports.fromFd = fromFd;
exports.fromBuffer = fromBuffer;
exports.ZipFile = ZipFile;
exports.Entry = Entry;
exports.dosDateTimeToDate = dosDateTimeToDate;
// cd - Central Directory
// cdr - Central Directory Record
// eocdr - End of Central Directory Record
function open(path, options, callback) {
Eif (typeof options === "function") {
callback = options;
options = null;
}
Eif (options == null) options = {autoClose: true};
Iif (callback == null) callback = defaultCallback;
fs.open(path, "r", function(err, fd) {
Iif (err) return callback(err);
fromFd(fd, options, function(err, zipfile) {
if (err) fs.close(fd, defaultCallback);
callback(err, zipfile);
});
});
}
function fromFd(fd, options, callback) {
Iif (typeof options === "function") {
callback = options;
options = null;
}
Iif (options == null) options = {autoClose: false};
Iif (callback == null) callback = defaultCallback;
fs.fstat(fd, function(err, stats) {
Iif (err) return callback(err);
var fdSlicer = fd_slicer.createFromFd(fd, {autoClose: true});
// this ref is unreffed in zipfile.close()
fdSlicer.ref();
fromFdSlicer(fdSlicer, stats.size, options, callback);
});
}
function fromBuffer(buffer, callback) {
Iif (callback == null) callback = defaultCallback;
// i got your open file right here.
var fdSlicer = fd_slicer.createFromBuffer(buffer);
fromFdSlicer(fdSlicer, buffer.length, {}, callback);
}
function fromFdSlicer(fdSlicer, totalSize, options, callback) {
// search backwards for the eocdr signature.
// the last field of the eocdr is a variable-length comment.
// the comment size is encoded in a 2-byte field in the eocdr, which we can't find without trudging backwards through the comment to find it.
// as a consequence of this design decision, it's possible to have ambiguous zip file metadata if a coherent eocdr was in the comment.
// we search backwards for a eocdr signature, and hope that whoever made the zip file was smart enough to forbid the eocdr signature in the comment.
var eocdrWithoutCommentSize = 22;
var maxCommentSize = 0x10000; // 2-byte size
var bufferSize = Math.min(eocdrWithoutCommentSize + maxCommentSize, totalSize);
var buffer = new Buffer(bufferSize);
var bufferReadStart = totalSize - buffer.length;
readFdSlicerNoEof(fdSlicer, buffer, 0, bufferSize, bufferReadStart, function(err) {
Iif (err) return callback(err);
for (var i = bufferSize - eocdrWithoutCommentSize; i >= 0; i -= 1) {
if (buffer.readUInt32LE(i) !== 0x06054b50) continue;
// found eocdr
var eocdrBuffer = buffer.slice(i);
// 0 - End of central directory signature = 0x06054b50
// 4 - Number of this disk
var diskNumber = eocdrBuffer.readUInt16LE(4);
if (diskNumber !== 0) return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber));
// 6 - Disk where central directory starts
// 8 - Number of central directory records on this disk
// 10 - Total number of central directory records
var entryCount = eocdrBuffer.readUInt16LE(10);
// 12 - Size of central directory (bytes)
// 16 - Offset of start of central directory, relative to start of archive
var cdOffset = eocdrBuffer.readUInt32LE(16);
// 20 - Comment length
var commentLength = eocdrBuffer.readUInt16LE(20);
var expectedCommentLength = eocdrBuffer.length - eocdrWithoutCommentSize;
if (commentLength !== expectedCommentLength) {
return callback(new Error("invalid comment length. expected: " + expectedCommentLength + ". found: " + commentLength));
}
// 22 - Comment
// the encoding is always cp437.
var comment = bufferToString(eocdrBuffer, 22, eocdrBuffer.length, false);
return callback(null, new ZipFile(fdSlicer, cdOffset, totalSize, entryCount, comment, options.autoClose));
}
callback(new Error("end of central directory record signature not found"));
});
}
util.inherits(ZipFile, EventEmitter);
function ZipFile(fdSlicer, cdOffset, fileSize, entryCount, comment, autoClose) {
var self = this;
EventEmitter.call(self);
self.fdSlicer = fdSlicer;
// forward close events
self.fdSlicer.on("error", function(err) {
// error closing the fd
emitError(self, err);
});
self.fdSlicer.once("close", function() {
self.emit("close");
});
self.readEntryCursor = cdOffset;
self.fileSize = fileSize;
self.entryCount = entryCount;
self.comment = comment;
self.entriesRead = 0;
self.autoClose = !!autoClose;
self.isOpen = true;
self.emittedError = false;
// make sure events don't fire outta here until the client has a chance to attach listeners
setImmediate(function() { readEntries(self); });
}
ZipFile.prototype.close = function() {
Iif (!this.isOpen) return;
this.isOpen = false;
this.fdSlicer.unref();
};
function emitErrorAndAutoClose(self, err) {
Eif (self.autoClose) self.close();
emitError(self, err);
}
function emitError(self, err) {
Iif (self.emittedError) return;
self.emittedError = true;
self.emit("error", err);
}
function readEntries(self) {
if (self.entryCount === self.entriesRead) {
// done with metadata
if (self.autoClose) self.close();
Iif (self.emittedError) return;
return self.emit("end");
}
Iif (self.emittedError) return;
var buffer = new Buffer(46);
readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
Iif (err) return emitErrorAndAutoClose(self, err);
Iif (self.emittedError) return;
var entry = new Entry();
// 0 - Central directory file header signature
var signature = buffer.readUInt32LE(0);
if (signature !== 0x02014b50) return emitErrorAndAutoClose(self, new Error("invalid central directory file header signature: 0x" + signature.toString(16)));
// 4 - Version made by
entry.versionMadeBy = buffer.readUInt16LE(4);
// 6 - Version needed to extract (minimum)
entry.versionNeededToExtract = buffer.readUInt16LE(6);
// 8 - General purpose bit flag
entry.generalPurposeBitFlag = buffer.readUInt16LE(8);
// 10 - Compression method
entry.compressionMethod = buffer.readUInt16LE(10);
// 12 - File last modification time
entry.lastModFileTime = buffer.readUInt16LE(12);
// 14 - File last modification date
entry.lastModFileDate = buffer.readUInt16LE(14);
// 16 - CRC-32
entry.crc32 = buffer.readUInt32LE(16);
// 20 - Compressed size
entry.compressedSize = buffer.readUInt32LE(20);
// 24 - Uncompressed size
entry.uncompressedSize = buffer.readUInt32LE(24);
// 28 - File name length (n)
entry.fileNameLength = buffer.readUInt16LE(28);
// 30 - Extra field length (m)
entry.extraFieldLength = buffer.readUInt16LE(30);
// 32 - File comment length (k)
entry.fileCommentLength = buffer.readUInt16LE(32);
// 34 - Disk number where file starts
// 36 - Internal file attributes
entry.internalFileAttributes = buffer.readUInt16LE(36);
// 38 - External file attributes
entry.externalFileAttributes = buffer.readUInt32LE(38);
// 42 - Relative offset of local file header
entry.relativeOffsetOfLocalHeader = buffer.readUInt32LE(42);
self.readEntryCursor += 46;
buffer = new Buffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength);
readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
Iif (err) return emitErrorAndAutoClose(self, err);
Iif (self.emittedError) return;
// 46 - File name
var isUtf8 = entry.generalPurposeBitFlag & 0x800
try {
entry.fileName = bufferToString(buffer, 0, entry.fileNameLength, isUtf8);
} catch (e) {
return emitErrorAndAutoClose(self, e);
}
// 46+n - Extra field
var fileCommentStart = entry.fileNameLength + entry.extraFieldLength;
var extraFieldBuffer = buffer.slice(entry.fileNameLength, fileCommentStart);
entry.extraFields = [];
var i = 0;
while (i < extraFieldBuffer.length) {
var headerId = extraFieldBuffer.readUInt16LE(i + 0);
var dataSize = extraFieldBuffer.readUInt16LE(i + 2);
var dataStart = i + 4;
var dataEnd = dataStart + dataSize;
var dataBuffer = new Buffer(dataSize);
extraFieldBuffer.copy(dataBuffer, 0, dataStart, dataEnd);
entry.extraFields.push({
id: headerId,
data: dataBuffer,
});
i = dataEnd;
}
// 46+n+m - File comment
try {
entry.fileComment = bufferToString(buffer, fileCommentStart, fileCommentStart + entry.fileCommentLength, isUtf8);
} catch (e) {
return emitErrorAndAutoClose(self, e);
}
self.readEntryCursor += buffer.length;
self.entriesRead += 1;
// validate file name
if (entry.fileName.indexOf("\\") !== -1) return emitErrorAndAutoClose(self, new Error("invalid characters in fileName: " + entry.fileName));
if (/^[a-zA-Z]:/.test(entry.fileName) || /^\//.test(entry.fileName)) return emitErrorAndAutoClose(self, new Error("absolute path: " + entry.fileName));
if (entry.fileName.split("/").indexOf("..") !== -1) return emitErrorAndAutoClose(self, new Error("invalid relative path: " + entry.fileName));
self.emit("entry", entry);
readEntries(self);
});
});
}
ZipFile.prototype.openReadStream = function(entry, callback) {
var self = this;
Iif (!self.isOpen) return callback(new Error("closed"));
// make sure we don't lose the fd before we open the actual read stream
self.fdSlicer.ref();
var buffer = new Buffer(30);
readFdSlicerNoEof(self.fdSlicer, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader, function(err) {
try {
Iif (err) return callback(err);
// 0 - Local file header signature = 0x04034b50
var signature = buffer.readUInt32LE(0);
if (signature !== 0x04034b50) return callback(new Error("invalid local file header signature: 0x" + signature.toString(16)));
// all this should be redundant
// 4 - Version needed to extract (minimum)
// 6 - General purpose bit flag
// 8 - Compression method
// 10 - File last modification time
// 12 - File last modification date
// 14 - CRC-32
// 18 - Compressed size
// 22 - Uncompressed size
// 26 - File name length (n)
var fileNameLength = buffer.readUInt16LE(26);
// 28 - Extra field length (m)
var extraFieldLength = buffer.readUInt16LE(28);
// 30 - File name
// 30+n - Extra field
var localFileHeaderEnd = entry.relativeOffsetOfLocalHeader + buffer.length + fileNameLength + extraFieldLength;
var filterStream = null;
if (entry.compressionMethod === 0) {
// 0 - The file is stored (no compression)
} else if (entry.compressionMethod === 8) {
// 8 - The file is Deflated
filterStream = zlib.createInflateRaw();
} else {
return callback(new Error("unsupported compression method: " + entry.compressionMethod));
}
var fileDataStart = localFileHeaderEnd;
var fileDataEnd = fileDataStart + entry.compressedSize;
if (entry.compressedSize !== 0) {
// bounds check now, because the read streams will probably not complain loud enough.
// since we're dealing with an unsigned offset plus an unsigned size,
// we only have 1 thing to check for.
if (fileDataEnd > self.fileSize) {
return callback(new Error("file data overflows file bounds: " +
fileDataStart + " + " + entry.compressedSize + " > " + self.fileSize));
}
}
var stream = self.fdSlicer.createReadStream({start: fileDataStart, end: fileDataEnd});
if (filterStream != null) {
stream = stream.pipe(filterStream);
}
callback(null, stream);
} finally {
self.fdSlicer.unref();
}
});
};
function Entry() {
}
Entry.prototype.getLastModDate = function() {
return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime);
};
function dosDateTimeToDate(date, time) {
var day = date & 0x1f; // 1-31
var month = (date >> 5 & 0xf) - 1; // 1-12, 0-11
var year = (date >> 9 & 0x7f) + 1980; // 0-128, 1980-2108
var millisecond = 0;
var second = (time & 0x1f) * 2; // 0-29, 0-58 (even numbers)
var minute = time >> 5 & 0x3f; // 0-59
var hour = time >> 11 & 0x1f; // 0-23
return new Date(year, month, day, hour, minute, second, millisecond);
}
function readFdSlicerNoEof(fdSlicer, buffer, offset, length, position, callback) {
Iif (length === 0) {
// fs.read will throw an out-of-bounds error if you try to read 0 bytes from a 0 byte file
return setImmediate(function() { callback(null, new Buffer(0)); });
}
fdSlicer.read(buffer, offset, length, position, function(err, bytesRead) {
Iif (err) return callback(err);
Iif (bytesRead < length) return callback(new Error("unexpected EOF"));
callback();
});
}
var cp437 = '\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
function bufferToString(buffer, start, end, isUtf8) {
if (isUtf8) {
return buffer.toString("utf8", start, end);
} else {
var result = "";
for (var i = start; i < end; i++) {
result += cp437[buffer[i]];
}
return result;
}
}
function defaultCallback(err) {
Iif (err) throw err;
}
|