source: trip-planner-front/node_modules/pako/lib/inflate.js@ 571e0df

Last change on this file since 571e0df was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 12.1 KB
Line 
1'use strict';
2
3
4var zlib_inflate = require('./zlib/inflate');
5var utils = require('./utils/common');
6var strings = require('./utils/strings');
7var c = require('./zlib/constants');
8var msg = require('./zlib/messages');
9var ZStream = require('./zlib/zstream');
10var GZheader = require('./zlib/gzheader');
11
12var toString = Object.prototype.toString;
13
14/**
15 * class Inflate
16 *
17 * Generic JS-style wrapper for zlib calls. If you don't need
18 * streaming behaviour - use more simple functions: [[inflate]]
19 * and [[inflateRaw]].
20 **/
21
22/* internal
23 * inflate.chunks -> Array
24 *
25 * Chunks of output data, if [[Inflate#onData]] not overridden.
26 **/
27
28/**
29 * Inflate.result -> Uint8Array|Array|String
30 *
31 * Uncompressed result, generated by default [[Inflate#onData]]
32 * and [[Inflate#onEnd]] handlers. Filled after you push last chunk
33 * (call [[Inflate#push]] with `Z_FINISH` / `true` param) or if you
34 * push a chunk with explicit flush (call [[Inflate#push]] with
35 * `Z_SYNC_FLUSH` param).
36 **/
37
38/**
39 * Inflate.err -> Number
40 *
41 * Error code after inflate finished. 0 (Z_OK) on success.
42 * Should be checked if broken data possible.
43 **/
44
45/**
46 * Inflate.msg -> String
47 *
48 * Error message, if [[Inflate.err]] != 0
49 **/
50
51
52/**
53 * new Inflate(options)
54 * - options (Object): zlib inflate options.
55 *
56 * Creates new inflator instance with specified params. Throws exception
57 * on bad params. Supported options:
58 *
59 * - `windowBits`
60 * - `dictionary`
61 *
62 * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
63 * for more information on these.
64 *
65 * Additional options, for internal needs:
66 *
67 * - `chunkSize` - size of generated data chunks (16K by default)
68 * - `raw` (Boolean) - do raw inflate
69 * - `to` (String) - if equal to 'string', then result will be converted
70 * from utf8 to utf16 (javascript) string. When string output requested,
71 * chunk length can differ from `chunkSize`, depending on content.
72 *
73 * By default, when no options set, autodetect deflate/gzip data format via
74 * wrapper header.
75 *
76 * ##### Example:
77 *
78 * ```javascript
79 * var pako = require('pako')
80 * , chunk1 = Uint8Array([1,2,3,4,5,6,7,8,9])
81 * , chunk2 = Uint8Array([10,11,12,13,14,15,16,17,18,19]);
82 *
83 * var inflate = new pako.Inflate({ level: 3});
84 *
85 * inflate.push(chunk1, false);
86 * inflate.push(chunk2, true); // true -> last chunk
87 *
88 * if (inflate.err) { throw new Error(inflate.err); }
89 *
90 * console.log(inflate.result);
91 * ```
92 **/
93function Inflate(options) {
94 if (!(this instanceof Inflate)) return new Inflate(options);
95
96 this.options = utils.assign({
97 chunkSize: 16384,
98 windowBits: 0,
99 to: ''
100 }, options || {});
101
102 var opt = this.options;
103
104 // Force window size for `raw` data, if not set directly,
105 // because we have no header for autodetect.
106 if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
107 opt.windowBits = -opt.windowBits;
108 if (opt.windowBits === 0) { opt.windowBits = -15; }
109 }
110
111 // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
112 if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
113 !(options && options.windowBits)) {
114 opt.windowBits += 32;
115 }
116
117 // Gzip header has no info about windows size, we can do autodetect only
118 // for deflate. So, if window size not set, force it to max when gzip possible
119 if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
120 // bit 3 (16) -> gzipped data
121 // bit 4 (32) -> autodetect gzip/deflate
122 if ((opt.windowBits & 15) === 0) {
123 opt.windowBits |= 15;
124 }
125 }
126
127 this.err = 0; // error code, if happens (0 = Z_OK)
128 this.msg = ''; // error message
129 this.ended = false; // used to avoid multiple onEnd() calls
130 this.chunks = []; // chunks of compressed data
131
132 this.strm = new ZStream();
133 this.strm.avail_out = 0;
134
135 var status = zlib_inflate.inflateInit2(
136 this.strm,
137 opt.windowBits
138 );
139
140 if (status !== c.Z_OK) {
141 throw new Error(msg[status]);
142 }
143
144 this.header = new GZheader();
145
146 zlib_inflate.inflateGetHeader(this.strm, this.header);
147
148 // Setup dictionary
149 if (opt.dictionary) {
150 // Convert data if needed
151 if (typeof opt.dictionary === 'string') {
152 opt.dictionary = strings.string2buf(opt.dictionary);
153 } else if (toString.call(opt.dictionary) === '[object ArrayBuffer]') {
154 opt.dictionary = new Uint8Array(opt.dictionary);
155 }
156 if (opt.raw) { //In raw mode we need to set the dictionary early
157 status = zlib_inflate.inflateSetDictionary(this.strm, opt.dictionary);
158 if (status !== c.Z_OK) {
159 throw new Error(msg[status]);
160 }
161 }
162 }
163}
164
165/**
166 * Inflate#push(data[, mode]) -> Boolean
167 * - data (Uint8Array|Array|ArrayBuffer|String): input data
168 * - mode (Number|Boolean): 0..6 for corresponding Z_NO_FLUSH..Z_TREE modes.
169 * See constants. Skipped or `false` means Z_NO_FLUSH, `true` means Z_FINISH.
170 *
171 * Sends input data to inflate pipe, generating [[Inflate#onData]] calls with
172 * new output chunks. Returns `true` on success. The last data block must have
173 * mode Z_FINISH (or `true`). That will flush internal pending buffers and call
174 * [[Inflate#onEnd]]. For interim explicit flushes (without ending the stream) you
175 * can use mode Z_SYNC_FLUSH, keeping the decompression context.
176 *
177 * On fail call [[Inflate#onEnd]] with error code and return false.
178 *
179 * We strongly recommend to use `Uint8Array` on input for best speed (output
180 * format is detected automatically). Also, don't skip last param and always
181 * use the same type in your code (boolean or number). That will improve JS speed.
182 *
183 * For regular `Array`-s make sure all elements are [0..255].
184 *
185 * ##### Example
186 *
187 * ```javascript
188 * push(chunk, false); // push one of data chunks
189 * ...
190 * push(chunk, true); // push last chunk
191 * ```
192 **/
193Inflate.prototype.push = function (data, mode) {
194 var strm = this.strm;
195 var chunkSize = this.options.chunkSize;
196 var dictionary = this.options.dictionary;
197 var status, _mode;
198 var next_out_utf8, tail, utf8str;
199
200 // Flag to properly process Z_BUF_ERROR on testing inflate call
201 // when we check that all output data was flushed.
202 var allowBufError = false;
203
204 if (this.ended) { return false; }
205 _mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
206
207 // Convert data if needed
208 if (typeof data === 'string') {
209 // Only binary strings can be decompressed on practice
210 strm.input = strings.binstring2buf(data);
211 } else if (toString.call(data) === '[object ArrayBuffer]') {
212 strm.input = new Uint8Array(data);
213 } else {
214 strm.input = data;
215 }
216
217 strm.next_in = 0;
218 strm.avail_in = strm.input.length;
219
220 do {
221 if (strm.avail_out === 0) {
222 strm.output = new utils.Buf8(chunkSize);
223 strm.next_out = 0;
224 strm.avail_out = chunkSize;
225 }
226
227 status = zlib_inflate.inflate(strm, c.Z_NO_FLUSH); /* no bad return value */
228
229 if (status === c.Z_NEED_DICT && dictionary) {
230 status = zlib_inflate.inflateSetDictionary(this.strm, dictionary);
231 }
232
233 if (status === c.Z_BUF_ERROR && allowBufError === true) {
234 status = c.Z_OK;
235 allowBufError = false;
236 }
237
238 if (status !== c.Z_STREAM_END && status !== c.Z_OK) {
239 this.onEnd(status);
240 this.ended = true;
241 return false;
242 }
243
244 if (strm.next_out) {
245 if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && (_mode === c.Z_FINISH || _mode === c.Z_SYNC_FLUSH))) {
246
247 if (this.options.to === 'string') {
248
249 next_out_utf8 = strings.utf8border(strm.output, strm.next_out);
250
251 tail = strm.next_out - next_out_utf8;
252 utf8str = strings.buf2string(strm.output, next_out_utf8);
253
254 // move tail
255 strm.next_out = tail;
256 strm.avail_out = chunkSize - tail;
257 if (tail) { utils.arraySet(strm.output, strm.output, next_out_utf8, tail, 0); }
258
259 this.onData(utf8str);
260
261 } else {
262 this.onData(utils.shrinkBuf(strm.output, strm.next_out));
263 }
264 }
265 }
266
267 // When no more input data, we should check that internal inflate buffers
268 // are flushed. The only way to do it when avail_out = 0 - run one more
269 // inflate pass. But if output data not exists, inflate return Z_BUF_ERROR.
270 // Here we set flag to process this error properly.
271 //
272 // NOTE. Deflate does not return error in this case and does not needs such
273 // logic.
274 if (strm.avail_in === 0 && strm.avail_out === 0) {
275 allowBufError = true;
276 }
277
278 } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
279
280 if (status === c.Z_STREAM_END) {
281 _mode = c.Z_FINISH;
282 }
283
284 // Finalize on the last chunk.
285 if (_mode === c.Z_FINISH) {
286 status = zlib_inflate.inflateEnd(this.strm);
287 this.onEnd(status);
288 this.ended = true;
289 return status === c.Z_OK;
290 }
291
292 // callback interim results if Z_SYNC_FLUSH.
293 if (_mode === c.Z_SYNC_FLUSH) {
294 this.onEnd(c.Z_OK);
295 strm.avail_out = 0;
296 return true;
297 }
298
299 return true;
300};
301
302
303/**
304 * Inflate#onData(chunk) -> Void
305 * - chunk (Uint8Array|Array|String): output data. Type of array depends
306 * on js engine support. When string output requested, each chunk
307 * will be string.
308 *
309 * By default, stores data blocks in `chunks[]` property and glue
310 * those in `onEnd`. Override this handler, if you need another behaviour.
311 **/
312Inflate.prototype.onData = function (chunk) {
313 this.chunks.push(chunk);
314};
315
316
317/**
318 * Inflate#onEnd(status) -> Void
319 * - status (Number): inflate status. 0 (Z_OK) on success,
320 * other if not.
321 *
322 * Called either after you tell inflate that the input stream is
323 * complete (Z_FINISH) or should be flushed (Z_SYNC_FLUSH)
324 * or if an error happened. By default - join collected chunks,
325 * free memory and fill `results` / `err` properties.
326 **/
327Inflate.prototype.onEnd = function (status) {
328 // On success - join
329 if (status === c.Z_OK) {
330 if (this.options.to === 'string') {
331 // Glue & convert here, until we teach pako to send
332 // utf8 aligned strings to onData
333 this.result = this.chunks.join('');
334 } else {
335 this.result = utils.flattenChunks(this.chunks);
336 }
337 }
338 this.chunks = [];
339 this.err = status;
340 this.msg = this.strm.msg;
341};
342
343
344/**
345 * inflate(data[, options]) -> Uint8Array|Array|String
346 * - data (Uint8Array|Array|String): input data to decompress.
347 * - options (Object): zlib inflate options.
348 *
349 * Decompress `data` with inflate/ungzip and `options`. Autodetect
350 * format via wrapper header by default. That's why we don't provide
351 * separate `ungzip` method.
352 *
353 * Supported options are:
354 *
355 * - windowBits
356 *
357 * [http://zlib.net/manual.html#Advanced](http://zlib.net/manual.html#Advanced)
358 * for more information.
359 *
360 * Sugar (options):
361 *
362 * - `raw` (Boolean) - say that we work with raw stream, if you don't wish to specify
363 * negative windowBits implicitly.
364 * - `to` (String) - if equal to 'string', then result will be converted
365 * from utf8 to utf16 (javascript) string. When string output requested,
366 * chunk length can differ from `chunkSize`, depending on content.
367 *
368 *
369 * ##### Example:
370 *
371 * ```javascript
372 * var pako = require('pako')
373 * , input = pako.deflate([1,2,3,4,5,6,7,8,9])
374 * , output;
375 *
376 * try {
377 * output = pako.inflate(input);
378 * } catch (err)
379 * console.log(err);
380 * }
381 * ```
382 **/
383function inflate(input, options) {
384 var inflator = new Inflate(options);
385
386 inflator.push(input, true);
387
388 // That will never happens, if you don't cheat with options :)
389 if (inflator.err) { throw inflator.msg || msg[inflator.err]; }
390
391 return inflator.result;
392}
393
394
395/**
396 * inflateRaw(data[, options]) -> Uint8Array|Array|String
397 * - data (Uint8Array|Array|String): input data to decompress.
398 * - options (Object): zlib inflate options.
399 *
400 * The same as [[inflate]], but creates raw data, without wrapper
401 * (header and adler32 crc).
402 **/
403function inflateRaw(input, options) {
404 options = options || {};
405 options.raw = true;
406 return inflate(input, options);
407}
408
409
410/**
411 * ungzip(data[, options]) -> Uint8Array|Array|String
412 * - data (Uint8Array|Array|String): input data to decompress.
413 * - options (Object): zlib inflate options.
414 *
415 * Just shortcut to [[inflate]], because it autodetects format
416 * by header.content. Done for convenience.
417 **/
418
419
420exports.Inflate = Inflate;
421exports.inflate = inflate;
422exports.inflateRaw = inflateRaw;
423exports.ungzip = inflate;
Note: See TracBrowser for help on using the repository browser.