]>
Commit | Line | Data |
---|---|---|
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 | /* eslint-disable camelcase */ | |
26 | this.strm.input = null; | |
27 | this.strm.avail_in = 0; | |
28 | this.strm.next_in = 0; | |
29 | } else { | |
30 | this.strm.input = data; | |
31 | this.strm.avail_in = this.strm.input.length; | |
32 | this.strm.next_in = 0; | |
33 | /* eslint-enable camelcase */ | |
34 | } | |
35 | } | |
36 | ||
37 | inflate(expected) { | |
38 | // resize our output buffer if it's too small | |
39 | // (we could just use multiple chunks, but that would cause an extra | |
40 | // allocation each time to flatten the chunks) | |
41 | if (expected > this.chunkSize) { | |
42 | this.chunkSize = expected; | |
43 | this.strm.output = new Uint8Array(this.chunkSize); | |
44 | } | |
45 | ||
46 | /* eslint-disable camelcase */ | |
47 | this.strm.next_out = 0; | |
48 | this.strm.avail_out = expected; | |
49 | /* eslint-enable camelcase */ | |
50 | ||
51 | let ret = inflate(this.strm, 0); // Flush argument not used. | |
52 | if (ret < 0) { | |
53 | throw new Error("zlib inflate failed"); | |
54 | } | |
55 | ||
56 | if (this.strm.next_out != expected) { | |
57 | throw new Error("Incomplete zlib block"); | |
58 | } | |
59 | ||
60 | return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out); | |
61 | } | |
62 | ||
63 | reset() { | |
64 | inflateReset(this.strm); | |
65 | } | |
66 | } |