]> git.proxmox.com Git - mirror_novnc.git/blob - core/inflator.js
Split api of inflate
[mirror_novnc.git] / core / inflator.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2020 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 *
6 * See README.md for usage and integration instructions.
7 */
8
9 import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
10 import ZStream from "../vendor/pako/lib/zlib/zstream.js";
11
12 export default class Inflate {
13 constructor() {
14 this.strm = new ZStream();
15 this.chunkSize = 1024 * 10 * 10;
16 this.strm.output = new Uint8Array(this.chunkSize);
17 this.windowBits = 5;
18
19 inflateInit(this.strm, this.windowBits);
20 }
21
22 setInput(data) {
23 if (!data) {
24 //FIXME: flush remaining data.
25 this.strm.input = null;
26 this.strm.avail_in = 0;
27 this.strm.next_in = 0;
28 } else {
29 this.strm.input = data;
30 this.strm.avail_in = this.strm.input.length;
31 this.strm.next_in = 0;
32 }
33 }
34
35 inflate(expected) {
36 // resize our output buffer if it's too small
37 // (we could just use multiple chunks, but that would cause an extra
38 // allocation each time to flatten the chunks)
39 if (expected > this.chunkSize) {
40 this.chunkSize = expected;
41 this.strm.output = new Uint8Array(this.chunkSize);
42 }
43
44 this.strm.next_out = 0;
45 this.strm.avail_out = this.chunkSize;
46
47 let ret = inflate(this.strm, 0); // Flush argument not used.
48 if (ret < 0) {
49 throw new Error("zlib inflate failed");
50 }
51
52 if (this.strm.next_out != expected) {
53 throw new Error("Incomplete zlib block");
54 }
55
56 return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
57 }
58
59 reset() {
60 inflateReset(this.strm);
61 }
62 }