fc075b00410be9211490628b05b5aa06f10f0d51.svn-base
5.71 KB
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
(function () {
'use strict';
var crc32 = require('crc32'),
deflate = require('deflate-js'),
// magic numbers marking this file as GZIP
ID1 = 0x1F,
ID2 = 0x8B,
compressionMethods = {
'deflate': 8
},
possibleFlags = {
'FTEXT': 0x01,
'FHCRC': 0x02,
'FEXTRA': 0x04,
'FNAME': 0x08,
'FCOMMENT': 0x10
},
osMap = {
'fat': 0, // FAT file system (DOS, OS/2, NT) + PKZIPW 2.50 VFAT, NTFS
'amiga': 1, // Amiga
'vmz': 2, // VMS (VAX or Alpha AXP)
'unix': 3, // Unix
'vm/cms': 4, // VM/CMS
'atari': 5, // Atari
'hpfs': 6, // HPFS file system (OS/2, NT 3.x)
'macintosh': 7, // Macintosh
'z-system': 8, // Z-System
'cplm': 9, // CP/M
'tops-20': 10, // TOPS-20
'ntfs': 11, // NTFS file system (NT)
'qdos': 12, // SMS/QDOS
'acorn': 13, // Acorn RISC OS
'vfat': 14, // VFAT file system (Win95, NT)
'vms': 15, // MVS (code also taken for PRIMOS)
'beos': 16, // BeOS (BeBox or PowerMac)
'tandem': 17, // Tandem/NSK
'theos': 18 // THEOS
},
os = 'unix',
DEFAULT_LEVEL = 6;
function putByte(n, arr) {
arr.push(n & 0xFF);
}
// LSB first
function putShort(n, arr) {
arr.push(n & 0xFF);
arr.push(n >>> 8);
}
// LSB first
function putLong(n, arr) {
putShort(n & 0xffff, arr);
putShort(n >>> 16, arr);
}
function putString(s, arr) {
var i, len = s.length;
for (i = 0; i < len; i += 1) {
putByte(s.charCodeAt(i), arr);
}
}
function readByte(arr) {
return arr.shift();
}
function readShort(arr) {
return arr.shift() | (arr.shift() << 8);
}
function readLong(arr) {
var n1 = readShort(arr),
n2 = readShort(arr);
// JavaScript can't handle bits in the position 32
// we'll emulate this by removing the left-most bit (if it exists)
// and add it back in via multiplication, which does work
if (n2 > 32768) {
n2 -= 32768;
return ((n2 << 16) | n1) + 32768 * Math.pow(2, 16);
}
return (n2 << 16) | n1;
}
function readString(arr) {
var charArr = [];
// turn all bytes into chars until the terminating null
while (arr[0] !== 0) {
charArr.push(String.fromCharCode(arr.shift()));
}
// throw away terminating null
arr.shift();
// join all characters into a cohesive string
return charArr.join('');
}
/*
* Reads n number of bytes and return as an array.
*
* @param arr- Array of bytes to read from
* @param n- Number of bytes to read
*/
function readBytes(arr, n) {
var i, ret = [];
for (i = 0; i < n; i += 1) {
ret.push(arr.shift());
}
return ret;
}
/*
* ZIPs a file in GZIP format. The format is as given by the spec, found at:
* http://www.gzip.org/zlib/rfc-gzip.html
*
* Omitted parts in this implementation:
*/
function zip(data, options) {
var flags = 0,
level,
crc, out = [];
if (!options) {
options = {};
}
level = options.level || DEFAULT_LEVEL;
if (typeof data === 'string') {
data = Array.prototype.map.call(data, function (char) {
return char.charCodeAt(0);
});
}
// magic number marking this file as GZIP
putByte(ID1, out);
putByte(ID2, out);
putByte(compressionMethods['deflate'], out);
if (options.name) {
flags |= possibleFlags['FNAME'];
}
putByte(flags, out);
putLong(options.timestamp || parseInt(Date.now() / 1000, 10), out);
// put deflate args (extra flags)
if (level === 1) {
// fastest algorithm
putByte(4, out);
} else if (level === 9) {
// maximum compression (fastest algorithm)
putByte(2, out);
} else {
putByte(0, out);
}
// OS identifier
putByte(osMap[os], out);
if (options.name) {
// ignore the directory part
putString(options.name.substring(options.name.lastIndexOf('/') + 1), out);
// terminating null
putByte(0, out);
}
deflate.deflate(data, level).forEach(function (byte) {
putByte(byte, out);
});
putLong(parseInt(crc32(data), 16), out);
putLong(data.length, out);
return out;
}
function unzip(data, options) {
// start with a copy of the array
var arr = Array.prototype.slice.call(data, 0),
t,
compressionMethod,
flags,
mtime,
xFlags,
key,
os,
crc,
size,
res;
// check the first two bytes for the magic numbers
if (readByte(arr) !== ID1 || readByte(arr) !== ID2) {
throw 'Not a GZIP file';
}
t = readByte(arr);
t = Object.keys(compressionMethods).some(function (key) {
compressionMethod = key;
return compressionMethods[key] === t;
});
if (!t) {
throw 'Unsupported compression method';
}
flags = readByte(arr);
mtime = readLong(arr);
xFlags = readByte(arr);
t = readByte(arr);
Object.keys(osMap).some(function (key) {
if (osMap[key] === t) {
os = key;
return true;
}
});
// just throw away the bytes for now
if (flags & possibleFlags['FEXTRA']) {
t = readShort(arr);
readBytes(arr, t);
}
// just throw away for now
if (flags & possibleFlags['FNAME']) {
readString(arr);
}
// just throw away for now
if (flags & possibleFlags['FCOMMENT']) {
readString(arr);
}
// just throw away for now
if (flags & possibleFlags['FHCRC']) {
readShort(arr);
}
if (compressionMethod === 'deflate') {
// give deflate everything but the last 8 bytes
// the last 8 bytes are for the CRC32 checksum and filesize
res = deflate.inflate(arr.splice(0, arr.length - 8));
}
if (flags & possibleFlags['FTEXT']) {
res = Array.prototype.map.call(res, function (byte) {
return String.fromCharCode(byte);
}).join('');
}
crc = readLong(arr);
if (crc !== parseInt(crc32(res), 16)) {
throw 'Checksum does not match';
}
size = readLong(arr);
if (size !== res.length) {
throw 'Size of decompressed file not correct';
}
return res;
}
module.exports = {
zip: zip,
unzip: unzip,
get DEFAULT_LEVEL() {
return DEFAULT_LEVEL;
}
};
}());