Files
noVNC/core/inflator.js
T

67 lines
1.9 KiB
JavaScript
Raw Normal View History

/*
* noVNC: HTML5 VNC client
* Copyright (C) 2020 The noVNC Authors
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
2017-02-03 23:55:00 -05:00
import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
import ZStream from "../vendor/pako/lib/zlib/zstream.js";
2016-09-14 13:52:53 -04:00
2018-07-05 21:31:56 +02:00
export default class Inflate {
constructor() {
this.strm = new ZStream();
this.chunkSize = 1024 * 10 * 10;
this.strm.output = new Uint8Array(this.chunkSize);
this.windowBits = 5;
inflateInit(this.strm, this.windowBits);
}
2020-02-03 10:19:00 +01:00
setInput(data) {
if (!data) {
//FIXME: flush remaining data.
2020-05-31 01:36:41 +02:00
/* eslint-disable camelcase */
2020-02-03 10:19:00 +01:00
this.strm.input = null;
this.strm.avail_in = 0;
this.strm.next_in = 0;
} else {
this.strm.input = data;
this.strm.avail_in = this.strm.input.length;
this.strm.next_in = 0;
2020-05-31 01:36:41 +02:00
/* eslint-enable camelcase */
2020-02-03 10:19:00 +01:00
}
}
2016-09-14 13:52:53 -04:00
2020-02-03 10:19:00 +01:00
inflate(expected) {
2016-09-14 13:52:53 -04:00
// resize our output buffer if it's too small
// (we could just use multiple chunks, but that would cause an extra
// allocation each time to flatten the chunks)
if (expected > this.chunkSize) {
this.chunkSize = expected;
this.strm.output = new Uint8Array(this.chunkSize);
}
2020-05-31 01:36:41 +02:00
/* eslint-disable camelcase */
2020-02-03 10:19:00 +01:00
this.strm.next_out = 0;
this.strm.avail_out = expected;
2020-05-31 01:36:41 +02:00
/* eslint-enable camelcase */
2016-09-14 13:52:53 -04:00
2020-02-03 10:04:20 +01:00
let ret = inflate(this.strm, 0); // Flush argument not used.
if (ret < 0) {
throw new Error("zlib inflate failed");
}
2016-09-14 13:52:53 -04:00
2020-02-03 09:57:56 +01:00
if (this.strm.next_out != expected) {
throw new Error("Incomplete zlib block");
}
2016-09-14 13:52:53 -04:00
return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
2018-07-05 21:31:56 +02:00
}
2016-09-14 13:52:53 -04:00
2018-07-05 21:31:56 +02:00
reset() {
2017-02-03 23:55:00 -05:00
inflateReset(this.strm);
2015-05-18 19:01:58 -04:00
}
2018-05-24 00:25:44 +03:00
}