main
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
9import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
10import ZStream from "../vendor/pako/lib/zlib/zstream.js";
11
12export 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
18 inflateInit(this.strm);
19 }
20
21 setInput(data) {
22 if (!data) {
23 //FIXME: flush remaining data.
24 /* eslint-disable camelcase */
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 /* eslint-enable camelcase */
33 }
34 }
35
36 inflate(expected) {
37 // resize our output buffer if it's too small
38 // (we could just use multiple chunks, but that would cause an extra
39 // allocation each time to flatten the chunks)
40 if (expected > this.chunkSize) {
41 this.chunkSize = expected;
42 this.strm.output = new Uint8Array(this.chunkSize);
43 }
44
45 /* eslint-disable camelcase */
46 this.strm.next_out = 0;
47 this.strm.avail_out = expected;
48 /* eslint-enable camelcase */
49
50 let ret = inflate(this.strm, 0); // Flush argument not used.
51 if (ret < 0) {
52 throw new Error("zlib inflate failed");
53 }
54
55 if (this.strm.next_out != expected) {
56 throw new Error("Incomplete zlib block");
57 }
58
59 return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
60 }
61
62 reset() {
63 inflateReset(this.strm);
64 }
65}