|
| 1 | +var msg = require('pako/lib/zlib/messages'); |
| 2 | +var zstream = require('pako/lib/zlib/zstream'); |
| 3 | +var zlib_deflate = require('pako/lib/zlib/deflate.js'); |
| 4 | +var zlib_inflate = require('pako/lib/zlib/inflate.js'); |
| 5 | +var constants = require('pako/lib/zlib/constants'); |
| 6 | + |
| 7 | +for (var key in constants) { |
| 8 | + exports[key] = constants[key]; |
| 9 | +} |
| 10 | + |
| 11 | +// zlib modes |
| 12 | +exports.NONE = 0; |
| 13 | +exports.DEFLATE = 1; |
| 14 | +exports.INFLATE = 2; |
| 15 | +exports.GZIP = 3; |
| 16 | +exports.GUNZIP = 4; |
| 17 | +exports.DEFLATERAW = 5; |
| 18 | +exports.INFLATERAW = 6; |
| 19 | +exports.UNZIP = 7; |
| 20 | + |
| 21 | +function Zlib(mode) { |
| 22 | + if (mode < exports.DEFLATE || mode > exports.UNZIP) |
| 23 | + throw new TypeError("Bad argument"); |
| 24 | + |
| 25 | + this.mode = mode; |
| 26 | + this.init_done = false; |
| 27 | + this.write_in_progress = false; |
| 28 | + this.pending_close = false; |
| 29 | + this.windowBits = 0; |
| 30 | + this.level = 0; |
| 31 | + this.memLevel = 0; |
| 32 | + this.strategy = 0; |
| 33 | + this.dictionary = null; |
| 34 | +} |
| 35 | + |
| 36 | +Zlib.prototype.init = function(windowBits, level, memLevel, strategy, dictionary) { |
| 37 | + this.windowBits = windowBits; |
| 38 | + this.level = level; |
| 39 | + this.memLevel = memLevel; |
| 40 | + this.strategy = strategy; |
| 41 | + // dictionary not supported. |
| 42 | + |
| 43 | + if (this.mode === exports.GZIP || this.mode === exports.GUNZIP) |
| 44 | + this.windowBits += 16; |
| 45 | + |
| 46 | + if (this.mode === exports.UNZIP) |
| 47 | + this.windowBits += 32; |
| 48 | + |
| 49 | + if (this.mode === exports.DEFLATERAW || this.mode === exports.INFLATERAW) |
| 50 | + this.windowBits = -this.windowBits; |
| 51 | + |
| 52 | + this.strm = new zstream(); |
| 53 | + |
| 54 | + switch (this.mode) { |
| 55 | + case exports.DEFLATE: |
| 56 | + case exports.GZIP: |
| 57 | + case exports.DEFLATERAW: |
| 58 | + var status = zlib_deflate.deflateInit2( |
| 59 | + this.strm, |
| 60 | + this.level, |
| 61 | + exports.Z_DEFLATED, |
| 62 | + this.windowBits, |
| 63 | + this.memLevel, |
| 64 | + this.strategy |
| 65 | + ); |
| 66 | + break; |
| 67 | + case exports.INFLATE: |
| 68 | + case exports.GUNZIP: |
| 69 | + case exports.INFLATERAW: |
| 70 | + case exports.UNZIP: |
| 71 | + var status = zlib_inflate.inflateInit2( |
| 72 | + this.strm, |
| 73 | + this.windowBits |
| 74 | + ); |
| 75 | + break; |
| 76 | + default: |
| 77 | + throw new Error("Unknown mode " + this.mode); |
| 78 | + } |
| 79 | + |
| 80 | + if (status !== exports.Z_OK) { |
| 81 | + this._error(status); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + this.write_in_progress = false; |
| 86 | + this.init_done = true; |
| 87 | +}; |
| 88 | + |
| 89 | +Zlib.prototype.params = function() { |
| 90 | + throw new Error("deflateParams Not supported"); |
| 91 | +}; |
| 92 | + |
| 93 | +Zlib.prototype._writeCheck = function() { |
| 94 | + if (!this.init_done) |
| 95 | + throw new Error("write before init"); |
| 96 | + |
| 97 | + if (this.mode === exports.NONE) |
| 98 | + throw new Error("already finalized"); |
| 99 | + |
| 100 | + if (this.write_in_progress) |
| 101 | + throw new Error("write already in progress"); |
| 102 | + |
| 103 | + if (this.pending_close) |
| 104 | + throw new Error("close is pending"); |
| 105 | +}; |
| 106 | + |
| 107 | +Zlib.prototype.write = function(flush, input, in_off, in_len, out, out_off, out_len) { |
| 108 | + this._writeCheck(); |
| 109 | + this.write_in_progress = true; |
| 110 | + |
| 111 | + var self = this; |
| 112 | + process.nextTick(function() { |
| 113 | + self.write_in_progress = false; |
| 114 | + var res = self._write(flush, input, in_off, in_len, out, out_off, out_len); |
| 115 | + self.callback(res[0], res[1]); |
| 116 | + |
| 117 | + if (self.pending_close) |
| 118 | + self.close(); |
| 119 | + }); |
| 120 | + |
| 121 | + return this; |
| 122 | +}; |
| 123 | + |
| 124 | +// set method for Node buffers, used by pako |
| 125 | +function bufferSet(data, offset) { |
| 126 | + for (var i = 0; i < data.length; i++) { |
| 127 | + this[offset + i] = data[i]; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +Zlib.prototype.writeSync = function(flush, input, in_off, in_len, out, out_off, out_len) { |
| 132 | + this._writeCheck(); |
| 133 | + return this._write(flush, input, in_off, in_len, out, out_off, out_len); |
| 134 | +}; |
| 135 | + |
| 136 | +Zlib.prototype._write = function(flush, input, in_off, in_len, out, out_off, out_len) { |
| 137 | + this.write_in_progress = true; |
| 138 | + |
| 139 | + if (flush !== exports.Z_NO_FLUSH && |
| 140 | + flush !== exports.Z_PARTIAL_FLUSH && |
| 141 | + flush !== exports.Z_SYNC_FLUSH && |
| 142 | + flush !== exports.Z_FULL_FLUSH && |
| 143 | + flush !== exports.Z_FINISH && |
| 144 | + flush !== exports.Z_BLOCK) { |
| 145 | + throw new Error("Invalid flush value"); |
| 146 | + } |
| 147 | + |
| 148 | + if (input == null) { |
| 149 | + input = new Buffer(0); |
| 150 | + in_len = 0; |
| 151 | + in_off = 0; |
| 152 | + } |
| 153 | + |
| 154 | + if (out._set) |
| 155 | + out.set = out._set; |
| 156 | + else |
| 157 | + out.set = bufferSet; |
| 158 | + |
| 159 | + var strm = this.strm; |
| 160 | + strm.avail_in = in_len; |
| 161 | + strm.next_in = input; |
| 162 | + strm.next_in_index = in_off; |
| 163 | + strm.avail_out = out_len; |
| 164 | + strm.next_out = out; |
| 165 | + strm.next_out_index = out_off; |
| 166 | + |
| 167 | + switch (this.mode) { |
| 168 | + case exports.DEFLATE: |
| 169 | + case exports.GZIP: |
| 170 | + case exports.DEFLATERAW: |
| 171 | + var status = zlib_deflate.deflate(strm, flush); |
| 172 | + break; |
| 173 | + case exports.UNZIP: |
| 174 | + case exports.INFLATE: |
| 175 | + case exports.GUNZIP: |
| 176 | + case exports.INFLATERAW: |
| 177 | + var status = zlib_inflate.inflate(strm, flush); |
| 178 | + break; |
| 179 | + default: |
| 180 | + throw new Error("Unknown mode " + this.mode); |
| 181 | + } |
| 182 | + |
| 183 | + if (status !== exports.Z_STREAM_END && status !== exports.Z_OK) { |
| 184 | + this._error(status); |
| 185 | + } |
| 186 | + |
| 187 | + this.write_in_progress = false; |
| 188 | + return [strm.avail_in, strm.avail_out]; |
| 189 | +}; |
| 190 | + |
| 191 | +Zlib.prototype.close = function() { |
| 192 | + if (this.write_in_progress) { |
| 193 | + this.pending_close = true; |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + this.pending_close = false; |
| 198 | + |
| 199 | + if (this.mode === exports.DEFLATE || this.mode === exports.GZIP || this.mode === exports.DEFLATERAW) { |
| 200 | + zlib_deflate.deflateEnd(this.strm); |
| 201 | + } else { |
| 202 | + zlib_inflate.inflateEnd(this.strm); |
| 203 | + } |
| 204 | + |
| 205 | + this.mode = exports.NONE; |
| 206 | +}; |
| 207 | + |
| 208 | +Zlib.prototype.reset = function() { |
| 209 | + switch (this.mode) { |
| 210 | + case DEFLATE: |
| 211 | + case DEFLATERAW: |
| 212 | + var status = zlib_deflate.deflateReset(this.strm); |
| 213 | + break; |
| 214 | + case INFLATE: |
| 215 | + case INFLATERAW: |
| 216 | + var status = zlib_inflate.inflateReset(this.strm); |
| 217 | + break; |
| 218 | + } |
| 219 | + |
| 220 | + if (status !== exports.Z_OK) { |
| 221 | + this._error(status); |
| 222 | + } |
| 223 | +}; |
| 224 | + |
| 225 | +Zlib.prototype._error = function(status) { |
| 226 | + this.onerror(msg[status] + ': ' + this.strm.msg, status); |
| 227 | + |
| 228 | + this.write_in_progress = false; |
| 229 | + if (this.pending_close) |
| 230 | + this.close(); |
| 231 | +}; |
| 232 | + |
| 233 | +exports.Zlib = Zlib; |
0 commit comments