Debian Codegraph @

wasm/browser/src/zlib/inflate.js

raw
1 /* eslint-disable unicorn/numeric-separators-style,camelcase,no-unused-expressions */
2 goog.provide("Zlib.Inflate");
3
4 goog.require("Zlib.Adler32");
5 goog.require("Zlib.RawInflate");
6
7 goog.scope(function () {
8 /**
9 * @constructor
10 * @param {Uint8Array} input
11 * @param {Object} [opt_parameters]
12 *
13 * opt_params は以下のプロパティを指定する事ができます。
14 * - index: input buffer の deflate コンテナの開始位置.
15 * - blockSize: バッファのブロックサイズ.
16 * - verify: 伸張が終わった後 adler-32 checksum の検証を行うか.
17 * - bufferType: Zlib.Inflate.BufferType の値によってバッファの管理方法を指定する.
18 * Zlib.Inflate.BufferType は Zlib.RawInflate.BufferType のエイリアス.
19 */
20 Zlib.Inflate = function (input, opt_parameters) {
21 /** @type {Uint8Array} */
22 this.input = input;
23 /** @type {number} */
24 this.ip = 0;
25 /** @type {Zlib.RawInflate} */
26 this.rawinflate;
27 /** @type {(boolean|undefined)} verify flag. */
28 this.verify;
29
30 // option parameters
31 if (opt_parameters || !(opt_parameters = {})) {
32 if (opt_parameters.index) {
33 this.ip = opt_parameters.index;
34 }
35 if (opt_parameters.verify) {
36 this.verify = opt_parameters.verify;
37 }
38 }
39
40 // Compression Method and Flags
41 /** @type {number} */
42 const cmf = input[this.ip++];
43 /** @type {number} */
44 const flg = input[this.ip++];
45
46 // compression method
47 switch (cmf & 0x0f) {
48 case Zlib.CompressionMethod.DEFLATE:
49 this.method = Zlib.CompressionMethod.DEFLATE;
50 break;
51 default:
52 throw new Error("unsupported compression method");
53 }
54
55 // fcheck
56 if (((cmf << 8) + flg) % 31 !== 0) {
57 throw new Error("invalid fcheck flag:" + (((cmf << 8) + flg) % 31));
58 }
59
60 // fdict (not supported)
61 if (flg & 0x20) {
62 throw new Error("fdict flag is not supported");
63 }
64
65 // RawInflate
66 this.rawinflate = new Zlib.RawInflate(input, {
67 index: this.ip,
68 bufferSize: opt_parameters.bufferSize,
69 bufferType: opt_parameters.bufferType,
70 resize: opt_parameters.resize,
71 });
72 };
73
74 /**
75 * @enum {number}
76 */
77 Zlib.Inflate.BufferType = Zlib.RawInflate.BufferType;
78
79 /**
80 * decompress.
81 * @return {Uint8Array} inflated buffer.
82 */
83 Zlib.Inflate.prototype.decompress = function () {
84 /** @type {Uint8Array} input buffer. */
85 const input = this.input;
86 /** @type {number} adler-32 checksum */
87 let adler32;
88 /** @type {Uint8Array} inflated buffer. */
89 const buffer = this.rawinflate.decompress();
90 this.ip = this.rawinflate.ip;
91
92 // verify adler-32
93 if (this.verify) {
94 adler32 =
95 ((input[this.ip++] << 24) |
96 (input[this.ip++] << 16) |
97 (input[this.ip++] << 8) |
98 input[this.ip++]) >>>
99 0;
100
101 if (adler32 !== Zlib.Adler32(buffer)) {
102 throw new Error("invalid adler-32 checksum");
103 }
104 }
105
106 return buffer;
107 };
108
109 // end of scope
110 });