gzip.js
5.2 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
/**
* @fileoverview GZIP (RFC1952) 実装.
*/
goog.provide('Zlib.Gzip');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.CRC32');
goog.require('Zlib.RawDeflate');
goog.scope(function() {
/**
* @constructor
* @param {!(Array|Uint8Array)} input input buffer.
* @param {Object=} opt_params option parameters.
*/
Zlib.Gzip = function(input, opt_params) {
/** @type {!(Array.<number>|Uint8Array)} input buffer. */
this.input = input;
/** @type {number} input buffer pointer. */
this.ip = 0;
/** @type {!(Array.<number>|Uint8Array)} output buffer. */
this.output;
/** @type {number} output buffer. */
this.op = 0;
/** @type {!Object} flags option flags. */
this.flags = {};
/** @type {!string} filename. */
this.filename;
/** @type {!string} comment. */
this.comment;
/** @type {!Object} deflate options. */
this.deflateOptions;
// option parameters
if (opt_params) {
if (opt_params['flags']) {
this.flags = opt_params['flags'];
}
if (typeof opt_params['filename'] === 'string') {
this.filename = opt_params['filename'];
}
if (typeof opt_params['comment'] === 'string') {
this.comment = opt_params['comment'];
}
if (opt_params['deflateOptions']) {
this.deflateOptions = opt_params['deflateOptions'];
}
}
if (!this.deflateOptions) {
this.deflateOptions = {};
}
};
/**
* @type {number}
* @const
*/
Zlib.Gzip.DefaultBufferSize = 0x8000;
/**
* encode gzip members.
* @return {!(Array|Uint8Array)} gzip binary array.
*/
Zlib.Gzip.prototype.compress = function() {
/** @type {number} flags. */
var flg;
/** @type {number} modification time. */
var mtime;
/** @type {number} CRC-16 value for FHCRC flag. */
var crc16;
/** @type {number} CRC-32 value for verification. */
var crc32;
/** @type {!Zlib.RawDeflate} raw deflate object. */
var rawdeflate;
/** @type {number} character code */
var c;
/** @type {number} loop counter. */
var i;
/** @type {number} loop limiter. */
var il;
/** @type {!(Array|Uint8Array)} output buffer. */
var output =
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.Gzip.DefaultBufferSize);
/** @type {number} output buffer pointer. */
var op = 0;
var input = this.input;
var ip = this.ip;
var filename = this.filename;
var comment = this.comment;
// check signature
output[op++] = 0x1f;
output[op++] = 0x8b;
// check compression method
output[op++] = 8; /* XXX: use Zlib const */
// flags
flg = 0;
if (this.flags['fname']) flg |= Zlib.Gzip.FlagsMask.FNAME;
if (this.flags['fcomment']) flg |= Zlib.Gzip.FlagsMask.FCOMMENT;
if (this.flags['fhcrc']) flg |= Zlib.Gzip.FlagsMask.FHCRC;
// XXX: FTEXT
// XXX: FEXTRA
output[op++] = flg;
// modification time
mtime = (Date.now ? Date.now() : +new Date()) / 1000 | 0;
output[op++] = mtime & 0xff;
output[op++] = mtime >>> 8 & 0xff;
output[op++] = mtime >>> 16 & 0xff;
output[op++] = mtime >>> 24 & 0xff;
// extra flags
output[op++] = 0;
// operating system
output[op++] = Zlib.Gzip.OperatingSystem.UNKNOWN;
// extra
/* NOP */
// fname
if (this.flags['fname'] !== void 0) {
for (i = 0, il = filename.length; i < il; ++i) {
c = filename.charCodeAt(i);
if (c > 0xff) { output[op++] = (c >>> 8) & 0xff; }
output[op++] = c & 0xff;
}
output[op++] = 0; // null termination
}
// fcomment
if (this.flags['comment']) {
for (i = 0, il = comment.length; i < il; ++i) {
c = comment.charCodeAt(i);
if (c > 0xff) { output[op++] = (c >>> 8) & 0xff; }
output[op++] = c & 0xff;
}
output[op++] = 0; // null termination
}
// fhcrc
if (this.flags['fhcrc']) {
crc16 = Zlib.CRC32.calc(output, 0, op) & 0xffff;
output[op++] = (crc16 ) & 0xff;
output[op++] = (crc16 >>> 8) & 0xff;
}
// add compress option
this.deflateOptions['outputBuffer'] = output;
this.deflateOptions['outputIndex'] = op;
// compress
rawdeflate = new Zlib.RawDeflate(input, this.deflateOptions);
output = rawdeflate.compress();
op = rawdeflate.op;
// expand buffer
if (USE_TYPEDARRAY) {
if (op + 8 > output.buffer.byteLength) {
this.output = new Uint8Array(op + 8);
this.output.set(new Uint8Array(output.buffer));
output = this.output;
} else {
output = new Uint8Array(output.buffer);
}
}
// crc32
crc32 = Zlib.CRC32.calc(input);
output[op++] = (crc32 ) & 0xff;
output[op++] = (crc32 >>> 8) & 0xff;
output[op++] = (crc32 >>> 16) & 0xff;
output[op++] = (crc32 >>> 24) & 0xff;
// input size
il = input.length;
output[op++] = (il ) & 0xff;
output[op++] = (il >>> 8) & 0xff;
output[op++] = (il >>> 16) & 0xff;
output[op++] = (il >>> 24) & 0xff;
this.ip = ip;
if (USE_TYPEDARRAY && op < output.length) {
this.output = output = output.subarray(0, op);
}
return output;
};
/** @enum {number} */
Zlib.Gzip.OperatingSystem = {
FAT: 0,
AMIGA: 1,
VMS: 2,
UNIX: 3,
VM_CMS: 4,
ATARI_TOS: 5,
HPFS: 6,
MACINTOSH: 7,
Z_SYSTEM: 8,
CP_M: 9,
TOPS_20: 10,
NTFS: 11,
QDOS: 12,
ACORN_RISCOS: 13,
UNKNOWN: 255
};
/** @enum {number} */
Zlib.Gzip.FlagsMask = {
FTEXT: 0x01,
FHCRC: 0x02,
FEXTRA: 0x04,
FNAME: 0x08,
FCOMMENT: 0x10
};
});
/* vim:set expandtab ts=2 sw=2 tw=80: */