2
* @fileoverview GZIP (RFC1952) 実装.
4
goog.provide('Zlib.Gzip');
6
goog.require('USE_TYPEDARRAY');
7
goog.require('Zlib.CRC32');
8
goog.require('Zlib.RawDeflate');
10
goog.scope(function() {
14
* @param {!(Array|Uint8Array)} input input buffer.
15
* @param {Object=} opt_params option parameters.
17
Zlib.Gzip = function(input, opt_params) {
18
/** @type {!(Array.<number>|Uint8Array)} input buffer. */
20
/** @type {number} input buffer pointer. */
22
/** @type {!(Array.<number>|Uint8Array)} output buffer. */
24
/** @type {number} output buffer. */
26
/** @type {!Object} flags option flags. */
28
/** @type {!string} filename. */
30
/** @type {!string} comment. */
32
/** @type {!Object} deflate options. */
37
if (opt_params['flags']) {
38
this.flags = opt_params['flags'];
40
if (typeof opt_params['filename'] === 'string') {
41
this.filename = opt_params['filename'];
43
if (typeof opt_params['comment'] === 'string') {
44
this.comment = opt_params['comment'];
46
if (opt_params['deflateOptions']) {
47
this.deflateOptions = opt_params['deflateOptions'];
51
if (!this.deflateOptions) {
52
this.deflateOptions = {};
60
Zlib.Gzip.DefaultBufferSize = 0x8000;
63
* encode gzip members.
64
* @return {!(Array|Uint8Array)} gzip binary array.
66
Zlib.Gzip.prototype.compress = function() {
67
/** @type {number} flags. */
69
/** @type {number} modification time. */
71
/** @type {number} CRC-16 value for FHCRC flag. */
73
/** @type {number} CRC-32 value for verification. */
75
/** @type {!Zlib.RawDeflate} raw deflate object. */
77
/** @type {number} character code */
79
/** @type {number} loop counter. */
81
/** @type {number} loop limiter. */
83
/** @type {!(Array|Uint8Array)} output buffer. */
85
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.Gzip.DefaultBufferSize);
86
/** @type {number} output buffer pointer. */
89
var input = this.input;
91
var filename = this.filename;
92
var comment = this.comment;
98
// check compression method
99
output[op++] = 8; /* XXX: use Zlib const */
103
if (this.flags['fname']) flg |= Zlib.Gzip.FlagsMask.FNAME;
104
if (this.flags['fcomment']) flg |= Zlib.Gzip.FlagsMask.FCOMMENT;
105
if (this.flags['fhcrc']) flg |= Zlib.Gzip.FlagsMask.FHCRC;
111
mtime = (Date.now ? Date.now() : +new Date()) / 1000 | 0;
112
output[op++] = mtime & 0xff;
113
output[op++] = mtime >>> 8 & 0xff;
114
output[op++] = mtime >>> 16 & 0xff;
115
output[op++] = mtime >>> 24 & 0xff;
121
output[op++] = Zlib.Gzip.OperatingSystem.UNKNOWN;
127
if (this.flags['fname'] !== void 0) {
128
for (i = 0, il = filename.length; i < il; ++i) {
129
c = filename.charCodeAt(i);
130
if (c > 0xff) { output[op++] = (c >>> 8) & 0xff; }
131
output[op++] = c & 0xff;
133
output[op++] = 0; // null termination
137
if (this.flags['comment']) {
138
for (i = 0, il = comment.length; i < il; ++i) {
139
c = comment.charCodeAt(i);
140
if (c > 0xff) { output[op++] = (c >>> 8) & 0xff; }
141
output[op++] = c & 0xff;
143
output[op++] = 0; // null termination
147
if (this.flags['fhcrc']) {
148
crc16 = Zlib.CRC32.calc(output, 0, op) & 0xffff;
149
output[op++] = (crc16 ) & 0xff;
150
output[op++] = (crc16 >>> 8) & 0xff;
153
// add compress option
154
this.deflateOptions['outputBuffer'] = output;
155
this.deflateOptions['outputIndex'] = op;
158
rawdeflate = new Zlib.RawDeflate(input, this.deflateOptions);
159
output = rawdeflate.compress();
163
if (USE_TYPEDARRAY) {
164
if (op + 8 > output.buffer.byteLength) {
165
this.output = new Uint8Array(op + 8);
166
this.output.set(new Uint8Array(output.buffer));
167
output = this.output;
169
output = new Uint8Array(output.buffer);
174
crc32 = Zlib.CRC32.calc(input);
175
output[op++] = (crc32 ) & 0xff;
176
output[op++] = (crc32 >>> 8) & 0xff;
177
output[op++] = (crc32 >>> 16) & 0xff;
178
output[op++] = (crc32 >>> 24) & 0xff;
182
output[op++] = (il ) & 0xff;
183
output[op++] = (il >>> 8) & 0xff;
184
output[op++] = (il >>> 16) & 0xff;
185
output[op++] = (il >>> 24) & 0xff;
189
if (USE_TYPEDARRAY && op < output.length) {
190
this.output = output = output.subarray(0, op);
196
/** @enum {number} */
197
Zlib.Gzip.OperatingSystem = {
215
/** @enum {number} */
216
Zlib.Gzip.FlagsMask = {
225
/* vim:set expandtab ts=2 sw=2 tw=80: */