Skip to content
14 changes: 6 additions & 8 deletions core/decoders/tight.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,9 @@ export default class TightDecoder {
return false;
}

data = this._zlibs[streamId].inflate(data, true, uncompressedSize);
if (data.length != uncompressedSize) {
throw new Error("Incomplete zlib block");
}
this._zlibs[streamId].setInput(data);
data = this._zlibs[streamId].inflate(uncompressedSize);
this._zlibs[streamId].setInput(null);
}

display.blitRgbImage(x, y, width, height, data, 0, false);
Expand Down Expand Up @@ -208,10 +207,9 @@ export default class TightDecoder {
return false;
}

data = this._zlibs[streamId].inflate(data, true, uncompressedSize);
if (data.length != uncompressedSize) {
throw new Error("Incomplete zlib block");
}
this._zlibs[streamId].setInput(data);
data = this._zlibs[streamId].inflate(uncompressedSize);
this._zlibs[streamId].setInput(null);
}

// Convert indexed (palette based) image data to RGB
Expand Down
79 changes: 79 additions & 0 deletions core/deflator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.
*/

import { deflateInit, deflate } from "../vendor/pako/lib/zlib/deflate.js";
import { Z_FULL_FLUSH } from "../vendor/pako/lib/zlib/deflate.js";
import ZStream from "../vendor/pako/lib/zlib/zstream.js";

export default class Deflator {
constructor() {
this.strm = new ZStream();
this.chunkSize = 1024 * 10 * 10;
this.outputBuffer = new Uint8Array(this.chunkSize);
this.windowBits = 5;

deflateInit(this.strm, this.windowBits);
}

deflate(inData) {
this.strm.input = inData;
this.strm.avail_in = this.strm.input.length;
this.strm.next_in = 0;
this.strm.output = this.outputBuffer;
this.strm.avail_out = this.chunkSize;
this.strm.next_out = 0;

let lastRet = deflate(this.strm, Z_FULL_FLUSH);
let outData = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);

if (lastRet < 0) {
throw new Error("zlib deflate failed");
}

if (this.strm.avail_in > 0) {
// Read chunks until done

let chunks = [outData];
let totalLen = outData.length;
do {
this.strm.output = new Uint8Array(this.chunkSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to reset output on the next call to deflate().

this.strm.next_out = 0;
this.strm.avail_out = this.chunkSize;

lastRet = deflate(this.strm, Z_FULL_FLUSH);

if (lastRet < 0) {
throw new Error("zlib deflate failed");
}

let chunk = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
totalLen += chunk.length;
chunks.push(chunk);
} while (this.strm.avail_in > 0);

// Combine chunks into a single data

let newData = new Uint8Array(totalLen);
let offset = 0;

for (let i = 0; i < chunks.length; i++) {
newData.set(chunks[i], offset);
offset += chunks[i].length;
}

outData = newData;
}

this.strm.input = null;
this.strm.avail_in = 0;
this.strm.next_in = 0;

return outData;
}

}
3 changes: 2 additions & 1 deletion core/encodings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const encodings = {
pseudoEncodingContinuousUpdates: -313,
pseudoEncodingCompressLevel9: -247,
pseudoEncodingCompressLevel0: -256,
pseudoEncodingVMwareCursor: 0x574d5664
pseudoEncodingVMwareCursor: 0x574d5664,
pseudoEncodingExtendedClipboard: 0xc0a1e5ce
};

export function encodingName(num) {
Expand Down
38 changes: 31 additions & 7 deletions core/inflator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* 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.
*/

import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
import ZStream from "../vendor/pako/lib/zlib/zstream.js";

Expand All @@ -11,12 +19,20 @@ export default class Inflate {
inflateInit(this.strm, this.windowBits);
}

inflate(data, flush, expected) {
this.strm.input = data;
this.strm.avail_in = this.strm.input.length;
this.strm.next_in = 0;
this.strm.next_out = 0;
setInput(data) {
if (!data) {
//FIXME: flush remaining data.
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;
}
}

inflate(expected) {
// 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)
Expand All @@ -25,9 +41,17 @@ export default class Inflate {
this.strm.output = new Uint8Array(this.chunkSize);
}

this.strm.avail_out = this.chunkSize;
this.strm.next_out = 0;
this.strm.avail_out = expected;

let ret = inflate(this.strm, 0); // Flush argument not used.
if (ret < 0) {
throw new Error("zlib inflate failed");
}

inflate(this.strm, flush);
if (this.strm.next_out != expected) {
throw new Error("Incomplete zlib block");
}

return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
}
Expand Down
Loading