From 682f33a79049fe4987f426e72b0a494b42e6cd9b Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 24 Jan 2012 13:49:49 -0500 Subject: [PATCH 01/13] add javascript zlib --- include/jsunzip.js | 645 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 645 insertions(+) create mode 100755 include/jsunzip.js diff --git a/include/jsunzip.js b/include/jsunzip.js new file mode 100755 index 00000000..8141b778 --- /dev/null +++ b/include/jsunzip.js @@ -0,0 +1,645 @@ +/* + * JSUnzip + * + * Copyright (c) 2011 by Erik Moller + * All Rights Reserved + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +var tinf; + +function JSUnzip() { + + this.getInt = function(offset, size) { + switch (size) { + case 4: + return (this.data.charCodeAt(offset + 3) & 0xff) << 24 | + (this.data.charCodeAt(offset + 2) & 0xff) << 16 | + (this.data.charCodeAt(offset + 1) & 0xff) << 8 | + (this.data.charCodeAt(offset + 0) & 0xff); + break; + case 2: + return (this.data.charCodeAt(offset + 1) & 0xff) << 8 | + (this.data.charCodeAt(offset + 0) & 0xff); + break; + default: + return this.data.charCodeAt(offset) & 0xff; + break; + } + }; + + this.getDOSDate = function(dosdate, dostime) { + var day = dosdate & 0x1f; + var month = ((dosdate >> 5) & 0xf) - 1; + var year = 1980 + ((dosdate >> 9) & 0x7f) + var second = (dostime & 0x1f) * 2; + var minute = (dostime >> 5) & 0x3f; + hour = (dostime >> 11) & 0x1f; + return new Date(year, month, day, hour, minute, second); + } + + this.open = function(data) { + this.data = data; + this.files = []; + + if (this.data.length < 22) + return { 'status' : false, 'error' : 'Invalid data' }; + var endOfCentralDirectory = this.data.length - 22; + while (endOfCentralDirectory >= 0 && this.getInt(endOfCentralDirectory, 4) != 0x06054b50) + --endOfCentralDirectory; + if (endOfCentralDirectory < 0) + return { 'status' : false, 'error' : 'Invalid data' }; + if (this.getInt(endOfCentralDirectory + 4, 2) != 0 || this.getInt(endOfCentralDirectory + 6, 2) != 0) + return { 'status' : false, 'error' : 'No multidisk support' }; + + var entriesInThisDisk = this.getInt(endOfCentralDirectory + 8, 2); + var centralDirectoryOffset = this.getInt(endOfCentralDirectory + 16, 4); + var globalCommentLength = this.getInt(endOfCentralDirectory + 20, 2); + this.comment = this.data.slice(endOfCentralDirectory + 22, endOfCentralDirectory + 22 + globalCommentLength); + + var fileOffset = centralDirectoryOffset; + + for (var i = 0; i < entriesInThisDisk; ++i) { + if (this.getInt(fileOffset + 0, 4) != 0x02014b50) + return { 'status' : false, 'error' : 'Invalid data' }; + if (this.getInt(fileOffset + 6, 2) > 20) + return { 'status' : false, 'error' : 'Unsupported version' }; + if (this.getInt(fileOffset + 8, 2) & 1) + return { 'status' : false, 'error' : 'Encryption not implemented' }; + + var compressionMethod = this.getInt(fileOffset + 10, 2); + if (compressionMethod != 0 && compressionMethod != 8) + return { 'status' : false, 'error' : 'Unsupported compression method' }; + + var lastModFileTime = this.getInt(fileOffset + 12, 2); + var lastModFileDate = this.getInt(fileOffset + 14, 2); + var lastModifiedDate = this.getDOSDate(lastModFileDate, lastModFileTime); + + var crc = this.getInt(fileOffset + 16, 4); + // TODO: crc + + var compressedSize = this.getInt(fileOffset + 20, 4); + var uncompressedSize = this.getInt(fileOffset + 24, 4); + + var fileNameLength = this.getInt(fileOffset + 28, 2); + var extraFieldLength = this.getInt(fileOffset + 30, 2); + var fileCommentLength = this.getInt(fileOffset + 32, 2); + + var relativeOffsetOfLocalHeader = this.getInt(fileOffset + 42, 4); + + var fileName = this.data.slice(fileOffset + 46, fileOffset + 46 + fileNameLength); + var fileComment = this.data.slice(fileOffset + 46 + fileNameLength + extraFieldLength, fileOffset + 46 + fileNameLength + extraFieldLength + fileCommentLength); + + if (this.getInt(relativeOffsetOfLocalHeader + 0, 4) != 0x04034b50) + return { 'status' : false, 'error' : 'Invalid data' }; + var localFileNameLength = this.getInt(relativeOffsetOfLocalHeader + 26, 2); + var localExtraFieldLength = this.getInt(relativeOffsetOfLocalHeader + 28, 2); + var localFileContent = relativeOffsetOfLocalHeader + 30 + localFileNameLength + localExtraFieldLength; + + this.files[fileName] = + { + 'fileComment' : fileComment, + 'compressionMethod' : compressionMethod, + 'compressedSize' : compressedSize, + 'uncompressedSize' : uncompressedSize, + 'localFileContent' : localFileContent, + 'lastModifiedDate' : lastModifiedDate + }; + + fileOffset += 46 + fileNameLength + extraFieldLength + fileCommentLength; + } + return { 'status' : true } + }; + + + this.read = function(fileName) { + var fileInfo = this.files[fileName]; + if (fileInfo) { + if (fileInfo.compressionMethod == 8) { + if (!tinf) { + tinf = new TINF(); + tinf.init(); + } + var result = tinf.uncompress(this.data, fileInfo.localFileContent); + if (result.status == tinf.OK) + return { 'status' : true, 'data' : result.data }; + else + return { 'status' : false, 'error' : result.error }; + } else { + return { 'status' : true, 'data' : this.data.slice(fileInfo.localFileContent, fileInfo.localFileContent + fileInfo.uncompressedSize) }; + } + } + return { 'status' : false, 'error' : "File '" + fileName + "' doesn't exist in zip" }; + }; + +}; + + + +/* + * tinflate - tiny inflate + * + * Copyright (c) 2003 by Joergen Ibsen / Jibz + * All Rights Reserved + * + * http://www.ibsensoftware.com/ + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +/* + * tinflate javascript port by Erik Moller in May 2011. + * emoller@opera.com + */ + +function TINF() { + +this.OK = 0; +this.DATA_ERROR = (-3); + +/* ------------------------------ * + * -- internal data structures -- * + * ------------------------------ */ + +this.TREE = function() { + this.table = new Array(16); /* table of code length counts */ + this.trans = new Array(288); /* code -> symbol translation table */ +}; + +this.DATA = function(that) { + this.source = ''; + this.sourceIndex = 0; + this.tag = 0; + this.bitcount = 0; + + this.dest = []; + + this.ltree = new that.TREE(); /* dynamic length/symbol tree */ + this.dtree = new that.TREE(); /* dynamic distance tree */ +}; + +/* --------------------------------------------------- * + * -- uninitialized global data (static structures) -- * + * --------------------------------------------------- */ + +this.sltree = new this.TREE(); /* fixed length/symbol tree */ +this.sdtree = new this.TREE(); /* fixed distance tree */ + +/* extra bits and base tables for length codes */ +this.length_bits = new Array(30); +this.length_base = new Array(30); + +/* extra bits and base tables for distance codes */ +this.dist_bits = new Array(30); +this.dist_base = new Array(30); + +/* special ordering of code length codes */ +this.clcidx = [ + 16, 17, 18, 0, 8, 7, 9, 6, + 10, 5, 11, 4, 12, 3, 13, 2, + 14, 1, 15 +]; + +/* ----------------------- * + * -- utility functions -- * + * ----------------------- */ + +/* build extra bits and base tables */ +this.build_bits_base = function(bits, base, delta, first) +{ + var i, sum; + + /* build bits table */ + for (i = 0; i < delta; ++i) bits[i] = 0; + for (i = 0; i < 30 - delta; ++i) bits[i + delta] = Math.floor(i / delta); + + /* build base table */ + for (sum = first, i = 0; i < 30; ++i) + { + base[i] = sum; + sum += 1 << bits[i]; + } +} + +/* build the fixed huffman trees */ +this.build_fixed_trees = function(lt, dt) +{ + var i; + + /* build fixed length tree */ + for (i = 0; i < 7; ++i) lt.table[i] = 0; + + lt.table[7] = 24; + lt.table[8] = 152; + lt.table[9] = 112; + + for (i = 0; i < 24; ++i) lt.trans[i] = 256 + i; + for (i = 0; i < 144; ++i) lt.trans[24 + i] = i; + for (i = 0; i < 8; ++i) lt.trans[24 + 144 + i] = 280 + i; + for (i = 0; i < 112; ++i) lt.trans[24 + 144 + 8 + i] = 144 + i; + + /* build fixed distance tree */ + for (i = 0; i < 5; ++i) dt.table[i] = 0; + + dt.table[5] = 32; + + for (i = 0; i < 32; ++i) dt.trans[i] = i; +} + +/* given an array of code lengths, build a tree */ +this.build_tree = function(t, lengths, loffset, num) +{ + var offs = new Array(16); + var i, sum; + + /* clear code length count table */ + for (i = 0; i < 16; ++i) t.table[i] = 0; + + /* scan symbol lengths, and sum code length counts */ + for (i = 0; i < num; ++i) t.table[lengths[loffset + i]]++; + + t.table[0] = 0; + + /* compute offset table for distribution sort */ + for (sum = 0, i = 0; i < 16; ++i) + { + offs[i] = sum; + sum += t.table[i]; + } + + /* create code->symbol translation table (symbols sorted by code) */ + for (i = 0; i < num; ++i) + { + if (lengths[loffset + i]) t.trans[offs[lengths[loffset + i]]++] = i; + } +} + +/* ---------------------- * + * -- decode functions -- * + * ---------------------- */ + +/* get one bit from source stream */ +this.getbit = function(d) +{ + var bit; + + /* check if tag is empty */ + if (!d.bitcount--) + { + /* load next tag */ + d.tag = d.source.charCodeAt(d.sourceIndex++) & 0xff; + d.bitcount = 7; + } + + /* shift bit out of tag */ + bit = d.tag & 0x01; + d.tag >>= 1; + + return bit; +} + +/* read a num bit value from a stream and add base */ +this.read_bits = function(d, num, base) +{ + if (!num) + return base; + + var val = 0; + while (d.bitcount < 24) { + d.tag = d.tag | (d.source.charCodeAt(d.sourceIndex++) & 0xff) << d.bitcount; + d.bitcount += 8; + } + val = d.tag & (0xff >> (8 - num)); + d.tag >>= num; + d.bitcount -= num; + return val + base; +} + +/* given a data stream and a tree, decode a symbol */ +this.decode_symbol = function(d, t) +{ + while (d.bitcount < 24) { + d.tag = d.tag | (d.source.charCodeAt(d.sourceIndex++) & 0xff) << d.bitcount; + d.bitcount += 8; + } + + var sum = 0, cur = 0, len = 0; + do { + cur = 2 * cur + ((d.tag & (1 << len)) >> len); + + ++len; + + sum += t.table[len]; + cur -= t.table[len]; + + } while (cur >= 0); + + d.tag >>= len; + d.bitcount -= len; + + return t.trans[sum + cur]; +} + +/* given a data stream, decode dynamic trees from it */ +this.decode_trees = function(d, lt, dt) +{ + var code_tree = new this.TREE(); + lengths = new Array(288+32); + var hlit, hdist, hclen; + var i, num, length; + + /* get 5 bits HLIT (257-286) */ + hlit = this.read_bits(d, 5, 257); + + /* get 5 bits HDIST (1-32) */ + hdist = this.read_bits(d, 5, 1); + + /* get 4 bits HCLEN (4-19) */ + hclen = this.read_bits(d, 4, 4); + + for (i = 0; i < 19; ++i) lengths[i] = 0; + + /* read code lengths for code length alphabet */ + for (i = 0; i < hclen; ++i) + { + /* get 3 bits code length (0-7) */ + var clen = this.read_bits(d, 3, 0); + + lengths[this.clcidx[i]] = clen; + } + + /* build code length tree */ + this.build_tree(code_tree, lengths, 0, 19); + + /* decode code lengths for the dynamic trees */ + for (num = 0; num < hlit + hdist; ) + { + var sym = this.decode_symbol(d, code_tree); + + switch (sym) + { + case 16: + /* copy previous code length 3-6 times (read 2 bits) */ + { + var prev = lengths[num - 1]; + for (length = this.read_bits(d, 2, 3); length; --length) + { + lengths[num++] = prev; + } + } + break; + case 17: + /* repeat code length 0 for 3-10 times (read 3 bits) */ + for (length = this.read_bits(d, 3, 3); length; --length) + { + lengths[num++] = 0; + } + break; + case 18: + /* repeat code length 0 for 11-138 times (read 7 bits) */ + for (length = this.read_bits(d, 7, 11); length; --length) + { + lengths[num++] = 0; + } + break; + default: + /* values 0-15 represent the actual code lengths */ + lengths[num++] = sym; + break; + } + } + + /* build dynamic trees */ + this.build_tree(lt, lengths, 0, hlit); + this.build_tree(dt, lengths, hlit, hdist); +} + +/* ----------------------------- * + * -- block inflate functions -- * + * ----------------------------- */ + +/* given a stream and two trees, inflate a block of data */ +this.inflate_block_data = function(d, lt, dt) +{ + // js optimization. + var ddest = d.dest; + var ddestlength = ddest.length; + + while (1) + { + var sym = this.decode_symbol(d, lt); + + /* check for end of block */ + if (sym == 256) + { + return this.OK; + } + + if (sym < 256) + { + ddest[ddestlength++] = String.fromCharCode(sym); + } else { + + var length, dist, offs; + var i; + + sym -= 257; + + /* possibly get more bits from length code */ + length = this.read_bits(d, this.length_bits[sym], this.length_base[sym]); + + dist = this.decode_symbol(d, dt); + + /* possibly get more bits from distance code */ + offs = ddestlength - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]); + + /* copy match */ + for (i = offs; i < offs + length; ++i) { + ddest[ddestlength++] = ddest[i]; + } + } + } +} + +/* inflate an uncompressed block of data */ +this.inflate_uncompressed_block = function(d) +{ + var length, invlength; + var i; + + /* get length */ + length = d.source[d.sourceIndex+1]; + length = 256*length + d.source[d.sourceIndex]; + + /* get one's complement of length */ + invlength = d.source[d.sourceIndex+3]; + invlength = 256*invlength + d.source[d.sourceIndex+2]; + + /* check length */ + if (length != (~invlength & 0x0000ffff)) return this.DATA_ERROR; + + d.sourceIndex += 4; + + /* copy block */ + for (i = length; i; --i) + d.dest[d.dest.length] = d.source[d.sourceIndex++]; + + /* make sure we start next block on a byte boundary */ + d.bitcount = 0; + + return this.OK; +} + +/* inflate a block of data compressed with fixed huffman trees */ +this.inflate_fixed_block = function(d) +{ + /* decode block using fixed trees */ + return this.inflate_block_data(d, this.sltree, this.sdtree); +} + +/* inflate a block of data compressed with dynamic huffman trees */ +this.inflate_dynamic_block = function(d) +{ + /* decode trees from stream */ + this.decode_trees(d, d.ltree, d.dtree); + + /* decode block using decoded trees */ + return this.inflate_block_data(d, d.ltree, d.dtree); +} + +/* ---------------------- * + * -- public functions -- * + * ---------------------- */ + +/* initialize global (static) data */ +this.init = function() +{ + /* build fixed huffman trees */ + this.build_fixed_trees(this.sltree, this.sdtree); + + /* build extra bits and base tables */ + this.build_bits_base(this.length_bits, this.length_base, 4, 3); + this.build_bits_base(this.dist_bits, this.dist_base, 2, 1); + + /* fix a special case */ + this.length_bits[28] = 0; + this.length_base[28] = 258; + + this.reset(); +} + +this.reset = function() +{ + this.d = new this.DATA(this); + this.header = null; +} + +/* inflate stream from source to dest */ +this.uncompress = function(source, offset) +{ + var d = this.d; + var bfinal; + + if (Object.prototype.toString.call(source) === '[object Array]') { + source.charCodeAt = function(id) { return this[id]; } + } + + /* initialise data */ + d.source = source; + d.sourceIndex = offset; + d.bitcount = 0; + + d.dest = []; + + // Skip zlib header at start of stream + /*if (typeof header == 'undefined') { + header = this.read_bits(d, 16, 0); + }*/ + + do { + + var btype; + var res; + + /* read final block flag */ + bfinal = this.getbit(d); + + /* read block type (2 bits) */ + btype = this.read_bits(d, 2, 0); + + /* decompress block */ + switch (btype) + { + case 0: + /* decompress uncompressed block */ + res = this.inflate_uncompressed_block(d); + break; + case 1: + /* decompress block with fixed huffman trees */ + res = this.inflate_fixed_block(d); + break; + case 2: + /* decompress block with dynamic huffman trees */ + res = this.inflate_dynamic_block(d); + break; + default: + return { 'status' : this.DATA_ERROR }; + } + + if (res != this.OK) return { 'status' : this.DATA_ERROR }; + + } while (!bfinal); + + if (Object.prototype.toString.call(source) !== '[object Array]') { + d.dest = d.dest.join(''); + } + + return { 'status' : this.OK, 'data' : d.dest }; +} + +}; From de84e098546ea8a1244bddd504fa23ada0bf1f02 Mon Sep 17 00:00:00 2001 From: Mike T Date: Tue, 24 Jan 2012 13:50:42 -0500 Subject: [PATCH 02/13] basic framing for tight is working (decode not complete) --- include/rfb.js | 183 ++++++++++++++++++++++++++++++++++++++++++++++++- include/vnc.js | 1 + 2 files changed, 183 insertions(+), 1 deletion(-) diff --git a/include/rfb.js b/include/rfb.js index 13fdd0a6..318a11b8 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -46,6 +46,7 @@ var that = {}, // Public API methods // In preference order encodings = [ ['COPYRECT', 0x01 ], + ['TIGHT', 0x07 ], ['TIGHT_PNG', -260 ], ['HEXTILE', 0x05 ], ['RRE', 0x02 ], @@ -87,7 +88,9 @@ var that = {}, // Public API methods encoding : 0, subencoding : -1, background : null, - imgQ : [] // TIGHT_PNG image queue + imgQ : [], // TIGHT_PNG image queue + zlibs : [], // TIGHT zlib streams + palette : null }, fb_Bpp = 4, @@ -296,6 +299,7 @@ init_vars = function() { FBU.lines = 0; // RAW FBU.tiles = 0; // HEXTILE FBU.imgQ = []; // TIGHT_PNG image queue + FBU.streams = []; // TIGHT zlib encoders mouse_buttonMask = 0; mouse_arr = []; @@ -303,6 +307,11 @@ init_vars = function() { for (i=0; i < encodings.length; i+=1) { encStats[encodings[i][1]][0] = 0; } + + for (i=0; i < 4; i++) { + FBU.zlibs[i] = new TINF(); + FBU.zlibs[i].init(); + } }; // Print statistics @@ -1257,6 +1266,176 @@ encHandlers.HEXTILE = function display_hextile() { }; +encHandlers.TIGHT = function display_tight() { + Util.Debug(">> display_tight"); + var ctl, cmode, clength, getCLength, color, img, data; + var filterId = -1, resetStreams = 0, streamId = -1; + var rQ = ws.get_rQ(), rQi = ws.get_rQi(); + + FBU.bytes = 1; // compression-control byte + if (ws.rQwait("TIGHT compression-control", FBU.bytes)) { return false; } + + // Get 'compact length' header and data size + getCLength = function (arr) { + var header = 1, data = 0; + data += arr[0] & 0x7f; + if (arr[0] & 0x80) { + header += 1; + data += (arr[1] & 0x7f) << 7; + if (arr[1] & 0x80) { + header += 1; + data += arr[2] << 14; + } + } + return [header, data]; + }; + + var decompress = function(data) { + // TODO: process resetStreams here + var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); + /*if (uncompressed.status != 0) + throw("Invalid data in zlib stream");*/ + return uncompressed.data; + } + + var handlePalette = function() { + var numColors = rQ[rQi + 2] + 1; + var paletteSize = numColors * fb_depth; + FBU.bytes += paletteSize; + + if (ws.rQwait("TIGHT palette " + cmode, FBU.bytes)) { return false; } + + FBU.palette = ws.rQslice(3, 3 + paletteSize); + + var bpp = (numColors <= 2) ? 1 : 8; + var rowSize = Math.floor((FBU.width * bpp + 7) / 8); + if (rowSize * FBU.height < 12) + clength = [0, rowSize * FBU.height]; + else + clength = getCLength(ws.rQslice(3 + paletteSize, 3 + paletteSize + 3)); + FBU.bytes += clength[0] + clength[1]; + if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } + + // Shift ctl, filter id, num colors, palette entries, and clength off + ws.rQshiftBytes(3 + paletteSize + clength[0]); + + // Process data + if (clength[1] < 12) + data = ws.rQshiftBytes(clength[1]); + else + data = decompress(ws.rQshiftBytes(clength[1])); + + return true; + } + + var handleCopy = function() { + var uncompressedSize = FBU.width * FBU.height * fb_depth; + if (uncompressedSize < 12) + clength = [0, uncompressedSize]; + else + clength = getCLength(ws.rQslice(1, 4)); + FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + zlib-data + if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } + + ws.rQshiftBytes(1 + clength[0]); // ctl + clength + if (clength[1] < 12) + data = ws.rQshiftBytes(clength[1]); + else + data = decompress(ws.rQshiftBytes(clength[1])); + + FBU.imgQ.push({ + 'type': 'rgb', + 'img': {'complete': true, 'data': data}, + 'x': FBU.x, + 'y': FBU.y, + 'width': FBU.width, + 'height': FBU.height}); + return true; + } + + ctl = ws.rQpeek8(); + + // Keep tight reset bits + resetStreams = ctl & 0xF; + + // Figure out filter + ctl = ctl >> 4; + streamId = ctl & 0x3; + + if (ctl == 0x08) cmode = "fill"; + else if (ctl == 0x09) cmode = "jpeg"; + else if (ctl & 0x04) cmode = "filter"; + else if (ctl < 0x04) cmode = "copy"; + else throw("Illegal tight compression received, ctl: " + ctl); + + switch (cmode) { + // fill uses fb_depth because TPIXELs drop the padding byte + case "fill": FBU.bytes += fb_depth; break; // TPIXEL + case "jpeg": FBU.bytes += 3; break; // max clength + case "filter": FBU.bytes += 2; break; // filter id + num colors if palette + case "copy": break; + } + + if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } + + //Util.Debug(" ws.rQslice(0,20): " + ws.rQslice(0,20) + " (" + ws.rQlen() + ")"); + //Util.Debug(" cmode: " + cmode); + + // Determine FBU.bytes + switch (cmode) { + case "fill": + ws.rQshift8(); // shift off ctl + color = ws.rQshiftBytes(fb_depth); + FBU.imgQ.push({ + 'type': 'fill', + 'img': {'complete': true}, + 'x': FBU.x, + 'y': FBU.y, + 'width': FBU.width, + 'height': FBU.height, + 'color': color}); + break; + case "jpeg": + clength = getCLength(ws.rQslice(1, 4)); + FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + jpeg-data + if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } + + // We have everything, render it + //Util.Debug(" png, ws.rQlen(): " + ws.rQlen() + ", clength[0]: " + clength[0] + ", clength[1]: " + clength[1]); + ws.rQshiftBytes(1 + clength[0]); // shift off ctl + compact length + img = new Image(); + //img.onload = scan_tight_imgQ; + FBU.imgQ.push({ + 'type': 'img', + 'img': img, + 'x': FBU.x, + 'y': FBU.y}); + img.src = "data:image/" + cmode + + extract_data_uri(ws.rQshiftBytes(clength[1])); + img = null; + break; + case "filter": + filterId = rQ[rQi + 1]; + if (filterId == 1) { + if (!handlePalette()) { return false; } + } else { + // Filter 0, Copy could be valid here, but servers don't send it as an explicit filter + // Filter 2, Gradient is valid but not used if jpeg is enabled + throw("Unsupported tight subencoding received, filter: " + filterId); + } + break; + case "copy": + if (!handleCopy()) { return false; } + break; + } + + FBU.bytes = 0; + FBU.rects -= 1; + //Util.Debug(" ending ws.rQslice(0,20): " + ws.rQslice(0,20) + " (" + ws.rQlen() + ")"); + //Util.Debug("<< display_tight_png"); + return true; +}; + encHandlers.TIGHT_PNG = function display_tight_png() { //Util.Debug(">> display_tight_png"); var ctl, cmode, clength, getCLength, color, img; @@ -1360,6 +1539,8 @@ scan_tight_imgQ = function() { data = imgQ.shift(); if (data.type === 'fill') { display.fillRect(data.x, data.y, data.width, data.height, data.color); + } else if (data.type === 'rgb') { + // TODO } else { ctx.drawImage(data.img, data.x, data.y); } diff --git a/include/vnc.js b/include/vnc.js index f938be7a..2b31f458 100644 --- a/include/vnc.js +++ b/include/vnc.js @@ -36,6 +36,7 @@ function get_INCLUDE_URI() { extra += start + "input.js" + end; extra += start + "display.js" + end; extra += start + "rfb.js" + end; + extra += start + "jsunzip.js" + end; document.write(extra); }()); From c577ca23059b36c0d44d3d1a7d20762c6cd60670 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Tue, 24 Jan 2012 21:18:29 -0500 Subject: [PATCH 03/13] keep zlib history so we can decode as a stream --- include/jsunzip.js | 32 ++++++++++++++++++++++++-------- include/rfb.js | 4 ++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/include/jsunzip.js b/include/jsunzip.js index 8141b778..2ae9e7fd 100755 --- a/include/jsunzip.js +++ b/include/jsunzip.js @@ -201,6 +201,7 @@ function TINF() { this.OK = 0; this.DATA_ERROR = (-3); +this.WINDOW_SIZE = 32768; /* ------------------------------ * * -- internal data structures -- * @@ -218,6 +219,8 @@ this.DATA = function(that) { this.bitcount = 0; this.dest = []; + + this.history = []; this.ltree = new that.TREE(); /* dynamic length/symbol tree */ this.dtree = new that.TREE(); /* dynamic distance tree */ @@ -482,7 +485,7 @@ this.inflate_block_data = function(d, lt, dt) if (sym < 256) { - ddest[ddestlength++] = String.fromCharCode(sym); + ddest[ddestlength++] = sym; // ? String.fromCharCode(sym); } else { var length, dist, offs; @@ -500,7 +503,10 @@ this.inflate_block_data = function(d, lt, dt) /* copy match */ for (i = offs; i < offs + length; ++i) { - ddest[ddestlength++] = ddest[i]; + if (i < 0) + ddest[ddestlength++] = d.history[d.history.length + i]; + else + ddest[ddestlength++] = ddest[i]; } } } @@ -576,7 +582,7 @@ this.init = function() this.reset = function() { this.d = new this.DATA(this); - this.header = null; + delete this.header; } /* inflate stream from source to dest */ @@ -597,9 +603,9 @@ this.uncompress = function(source, offset) d.dest = []; // Skip zlib header at start of stream - /*if (typeof header == 'undefined') { - header = this.read_bits(d, 16, 0); - }*/ + if (typeof this.header == 'undefined') { + this.header = this.read_bits(d, 16, 0); + } do { @@ -633,12 +639,22 @@ this.uncompress = function(source, offset) if (res != this.OK) return { 'status' : this.DATA_ERROR }; - } while (!bfinal); + } while (!bfinal && d.sourceIndex < d.source.length - 3); if (Object.prototype.toString.call(source) !== '[object Array]') { d.dest = d.dest.join(''); } - + else { + if (d.dest.length >= this.WINDOW_SIZE) { + d.history = d.dest.slice(d.dest.length - this.WINDOW_SIZE); + } else { + var overflow = d.history.length + d.dest.length - this.WINDOW_SIZE; + if (overflow > 0) + d.history = d.history.slice(overflow); + } + d.history.push.apply(d.history, d.dest); + } + return { 'status' : this.OK, 'data' : d.dest }; } diff --git a/include/rfb.js b/include/rfb.js index 318a11b8..00784bc5 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -1293,8 +1293,8 @@ encHandlers.TIGHT = function display_tight() { var decompress = function(data) { // TODO: process resetStreams here var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); - /*if (uncompressed.status != 0) - throw("Invalid data in zlib stream");*/ + if (uncompressed.status != 0) + throw("Invalid data in zlib stream"); return uncompressed.data; } From a820f1267a7422ed7561800f54b4ade76f8d4d78 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Wed, 25 Jan 2012 18:09:55 -0500 Subject: [PATCH 04/13] added rgb image drawing, some zlib changes (huffman coding working, but lz77 not so much) --- include/display.js | 31 ++++++++++++++++++++++++++++++- include/jsunzip.js | 32 +++++++++++++++++++++----------- include/rfb.js | 24 ++++++++++++++++++++---- 3 files changed, 71 insertions(+), 16 deletions(-) diff --git a/include/display.js b/include/display.js index bfc9776c..f2ecdba3 100644 --- a/include/display.js +++ b/include/display.js @@ -20,7 +20,7 @@ var that = {}, // Public API methods c_forceCanvas = false, // Predefine function variables (jslint) - imageDataGet, bgrxImageData, cmapImageData, + imageDataGet, rgbImageData, bgrxImageData, cmapImageData, setFillColor, rescale, // The full frame buffer (logical canvas) size @@ -497,6 +497,26 @@ that.finishTile = function() { // else: No-op, if not prefer_js then already done by setSubTile }; +rgbImageData = function(x, y, width, height, arr, offset) { + var img, i, j, data, v = viewport; + /* + if ((x - v.x >= v.w) || (y - v.y >= v.h) || + (x - v.x + width < 0) || (y - v.y + height < 0)) { + // Skipping because outside of viewport + return; + } + */ + img = c_ctx.createImageData(width, height); + data = img.data; + for (i=0, j=offset; i < (width * height * 4); i=i+4, j=j+3) { + data[i ] = arr[j ]; + data[i + 1] = arr[j + 1]; + data[i + 2] = arr[j + 2]; + data[i + 3] = 255; // Set Alpha + } + c_ctx.putImageData(img, x - v.x, y - v.y); +}; + bgrxImageData = function(x, y, width, height, arr, offset) { var img, i, j, data, v = viewport; /* @@ -540,6 +560,15 @@ that.blitImage = function(x, y, width, height, arr, offset) { } }; +that.blitRgbImage = function(x, y, width, height, arr, offset) { + if (conf.true_color) { + rgbImageData(x, y, width, height, arr, offset); + } else { + // prolly wrong... + cmapImageData(x, y, width, height, arr, offset); + } +}; + that.blitStringImage = function(str, x, y) { var img = new Image(); img.onload = function () { diff --git a/include/jsunzip.js b/include/jsunzip.js index 2ae9e7fd..db1a9fcb 100755 --- a/include/jsunzip.js +++ b/include/jsunzip.js @@ -197,8 +197,10 @@ function JSUnzip() { * emoller@opera.com */ +"use strict"; + function TINF() { - + this.OK = 0; this.DATA_ERROR = (-3); this.WINDOW_SIZE = 32768; @@ -392,7 +394,7 @@ this.decode_symbol = function(d, t) this.decode_trees = function(d, lt, dt) { var code_tree = new this.TREE(); - lengths = new Array(288+32); + var lengths = new Array(288+32); var hlit, hdist, hclen; var i, num, length; @@ -518,6 +520,13 @@ this.inflate_uncompressed_block = function(d) var length, invlength; var i; + if (d.bitcount > 7) { + var overflow = Math.floor(d.bitcount / 8); + d.sourceIndex -= overflow; + d.bitcount = 0; + d.tag = 0; + } + /* get length */ length = d.source[d.sourceIndex+1]; length = 256*length + d.source[d.sourceIndex]; @@ -605,7 +614,11 @@ this.uncompress = function(source, offset) // Skip zlib header at start of stream if (typeof this.header == 'undefined') { this.header = this.read_bits(d, 16, 0); + /* byte 0: 0x78, 7 = 32k window size, 8 = deflate */ + /* byte 1: check bits for header and other flags */ } + + var blocks = 0; do { @@ -638,21 +651,18 @@ this.uncompress = function(source, offset) } if (res != this.OK) return { 'status' : this.DATA_ERROR }; + blocks++; + + } while (!bfinal && d.sourceIndex < d.source.length); - } while (!bfinal && d.sourceIndex < d.source.length - 3); - + if (blocks != 2) throw ("Unexpected number of blocks"); + if (Object.prototype.toString.call(source) !== '[object Array]') { d.dest = d.dest.join(''); } else { - if (d.dest.length >= this.WINDOW_SIZE) { - d.history = d.dest.slice(d.dest.length - this.WINDOW_SIZE); - } else { - var overflow = d.history.length + d.dest.length - this.WINDOW_SIZE; - if (overflow > 0) - d.history = d.history.slice(overflow); - } d.history.push.apply(d.history, d.dest); + d.history = d.history.slice(-this.WINDOW_SIZE); } return { 'status' : this.OK, 'data' : d.dest }; diff --git a/include/rfb.js b/include/rfb.js index 00784bc5..420c5e66 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -299,7 +299,7 @@ init_vars = function() { FBU.lines = 0; // RAW FBU.tiles = 0; // HEXTILE FBU.imgQ = []; // TIGHT_PNG image queue - FBU.streams = []; // TIGHT zlib encoders + FBU.zlibs = []; // TIGHT zlib encoders mouse_buttonMask = 0; mouse_arr = []; @@ -1290,11 +1290,22 @@ encHandlers.TIGHT = function display_tight() { return [header, data]; }; + var checksum = function(data) { + var sum=0, i; + for (i=0; i 65536) sum -= 65536; + } + return sum; + } + var decompress = function(data) { // TODO: process resetStreams here var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); if (uncompressed.status != 0) - throw("Invalid data in zlib stream"); + throw("Invalid data in zlib stream"); + Util.Warn("Decompressed " + data.length + " to " + uncompressed.data.length + " checksums " + + checksum(data) + ":" + checksum(uncompressed.data)); return uncompressed.data; } @@ -1319,6 +1330,8 @@ encHandlers.TIGHT = function display_tight() { // Shift ctl, filter id, num colors, palette entries, and clength off ws.rQshiftBytes(3 + paletteSize + clength[0]); + if (streamId == 0) throw ("Wrong stream"); + // Process data if (clength[1] < 12) data = ws.rQshiftBytes(clength[1]); @@ -1337,6 +1350,8 @@ encHandlers.TIGHT = function display_tight() { FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + zlib-data if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } + if (streamId != 0) throw ("Wrong stream"); + ws.rQshiftBytes(1 + clength[0]); // ctl + clength if (clength[1] < 12) data = ws.rQshiftBytes(clength[1]); @@ -1357,7 +1372,8 @@ encHandlers.TIGHT = function display_tight() { // Keep tight reset bits resetStreams = ctl & 0xF; - + if (resetStreams) throw ("Tight reset"); + // Figure out filter ctl = ctl >> 4; streamId = ctl & 0x3; @@ -1540,7 +1556,7 @@ scan_tight_imgQ = function() { if (data.type === 'fill') { display.fillRect(data.x, data.y, data.width, data.height, data.color); } else if (data.type === 'rgb') { - // TODO + display.blitRgbImage(data.x, data.y, data.width, data.height, data.img.data, 0); } else { ctx.drawImage(data.img, data.x, data.y); } From 0fa6748c5290956831b378571d2b81f95b1372f0 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Thu, 26 Jan 2012 14:35:36 -0500 Subject: [PATCH 05/13] fix issue with parsing distance of more then 8 bits; convert to just supporting arrays for buffers --- include/jsunzip.js | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/include/jsunzip.js b/include/jsunzip.js index db1a9fcb..b4b69a20 100755 --- a/include/jsunzip.js +++ b/include/jsunzip.js @@ -337,7 +337,7 @@ this.getbit = function(d) if (!d.bitcount--) { /* load next tag */ - d.tag = d.source.charCodeAt(d.sourceIndex++) & 0xff; + d.tag = d.source[d.sourceIndex++] & 0xff; d.bitcount = 7; } @@ -355,11 +355,11 @@ this.read_bits = function(d, num, base) return base; var val = 0; - while (d.bitcount < 24) { - d.tag = d.tag | (d.source.charCodeAt(d.sourceIndex++) & 0xff) << d.bitcount; + while (d.bitcount < num) { + d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount; d.bitcount += 8; } - val = d.tag & (0xff >> (8 - num)); + val = d.tag & (0xffff >> (16 - num)); d.tag >>= num; d.bitcount -= num; return val + base; @@ -368,8 +368,8 @@ this.read_bits = function(d, num, base) /* given a data stream and a tree, decode a symbol */ this.decode_symbol = function(d, t) { - while (d.bitcount < 24) { - d.tag = d.tag | (d.source.charCodeAt(d.sourceIndex++) & 0xff) << d.bitcount; + while (d.bitcount < 10) { + d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount; d.bitcount += 8; } @@ -488,6 +488,7 @@ this.inflate_block_data = function(d, lt, dt) if (sym < 256) { ddest[ddestlength++] = sym; // ? String.fromCharCode(sym); + d.history.push(sym); } else { var length, dist, offs; @@ -503,12 +504,14 @@ this.inflate_block_data = function(d, lt, dt) /* possibly get more bits from distance code */ offs = ddestlength - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]); + if (offs < 0) + throw ("Invalid zlib offset " + offs); + /* copy match */ for (i = offs; i < offs + length; ++i) { - if (i < 0) - ddest[ddestlength++] = d.history[d.history.length + i]; - else - ddest[ddestlength++] = ddest[i]; + ddest[ddestlength++] = ddest[i]; + //ddest[ddestlength++] = d.history[i]; + //d.history.push(d.history[i]); } } } @@ -541,8 +544,10 @@ this.inflate_uncompressed_block = function(d) d.sourceIndex += 4; /* copy block */ - for (i = length; i; --i) + for (i = length; i; --i) { + d.history.push(d.source[d.sourceIndex]); d.dest[d.dest.length] = d.source[d.sourceIndex++]; + } /* make sure we start next block on a byte boundary */ d.bitcount = 0; @@ -597,13 +602,10 @@ this.reset = function() /* inflate stream from source to dest */ this.uncompress = function(source, offset) { + var d = this.d; var bfinal; - if (Object.prototype.toString.call(source) === '[object Array]') { - source.charCodeAt = function(id) { return this[id]; } - } - /* initialise data */ d.source = source; d.sourceIndex = offset; @@ -655,15 +657,8 @@ this.uncompress = function(source, offset) } while (!bfinal && d.sourceIndex < d.source.length); - if (blocks != 2) throw ("Unexpected number of blocks"); - - if (Object.prototype.toString.call(source) !== '[object Array]') { - d.dest = d.dest.join(''); - } - else { - d.history.push.apply(d.history, d.dest); - d.history = d.history.slice(-this.WINDOW_SIZE); - } + //d.history.push.apply(d.history, d.dest); + d.history = d.history.slice(-this.WINDOW_SIZE); return { 'status' : this.OK, 'data' : d.dest }; } From b0ac240f3124e01b54ccb59cc033b68722964f03 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Sat, 28 Jan 2012 01:56:55 -0500 Subject: [PATCH 06/13] re-enable history buffer (used as sliding window for decompress) --- include/jsunzip.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/include/jsunzip.js b/include/jsunzip.js index b4b69a20..e83d9bac 100755 --- a/include/jsunzip.js +++ b/include/jsunzip.js @@ -198,7 +198,7 @@ function JSUnzip() { */ "use strict"; - + function TINF() { this.OK = 0; @@ -502,16 +502,16 @@ this.inflate_block_data = function(d, lt, dt) dist = this.decode_symbol(d, dt); /* possibly get more bits from distance code */ - offs = ddestlength - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]); + offs = d.history.length - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]); if (offs < 0) throw ("Invalid zlib offset " + offs); /* copy match */ for (i = offs; i < offs + length; ++i) { - ddest[ddestlength++] = ddest[i]; - //ddest[ddestlength++] = d.history[i]; - //d.history.push(d.history[i]); + //ddest[ddestlength++] = ddest[i]; + ddest[ddestlength++] = d.history[i]; + d.history.push(d.history[i]); } } } @@ -657,7 +657,6 @@ this.uncompress = function(source, offset) } while (!bfinal && d.sourceIndex < d.source.length); - //d.history.push.apply(d.history, d.dest); d.history = d.history.slice(-this.WINDOW_SIZE); return { 'status' : this.OK, 'data' : d.dest }; From 6fbc37489fe3da33c94fce989ef12249e8b354db Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Sat, 28 Jan 2012 02:56:19 -0500 Subject: [PATCH 07/13] fix handling of min compression size --- include/rfb.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/include/rfb.js b/include/rfb.js index 420c5e66..0f96a9a0 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -309,6 +309,7 @@ init_vars = function() { } for (i=0; i < 4; i++) { + //FBU.zlibs[i] = new InflateStream(); FBU.zlibs[i] = new TINF(); FBU.zlibs[i].init(); } @@ -1302,10 +1303,21 @@ encHandlers.TIGHT = function display_tight() { var decompress = function(data) { // TODO: process resetStreams here var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); - if (uncompressed.status != 0) + if (uncompressed.status !== 0) throw("Invalid data in zlib stream"); Util.Warn("Decompressed " + data.length + " to " + uncompressed.data.length + " checksums " + checksum(data) + ":" + checksum(uncompressed.data)); + + /* + var i; + var uncompressed2 = zip_inflate(data); + for (i=0;i Date: Sun, 29 Jan 2012 01:55:41 -0500 Subject: [PATCH 08/13] implement tight indexed rectangle; remove some debug code --- include/jsunzip.js | 4 ++-- include/rfb.js | 60 +++++++++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/include/jsunzip.js b/include/jsunzip.js index e83d9bac..1d2957bd 100755 --- a/include/jsunzip.js +++ b/include/jsunzip.js @@ -355,7 +355,7 @@ this.read_bits = function(d, num, base) return base; var val = 0; - while (d.bitcount < num) { + while (d.bitcount < 24) { d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount; d.bitcount += 8; } @@ -368,7 +368,7 @@ this.read_bits = function(d, num, base) /* given a data stream and a tree, decode a symbol */ this.decode_symbol = function(d, t) { - while (d.bitcount < 10) { + while (d.bitcount < 16) { d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount; d.bitcount += 8; } diff --git a/include/rfb.js b/include/rfb.js index 0f96a9a0..3ccd73a4 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -1307,16 +1307,6 @@ encHandlers.TIGHT = function display_tight() { throw("Invalid data in zlib stream"); Util.Warn("Decompressed " + data.length + " to " + uncompressed.data.length + " checksums " + checksum(data) + ":" + checksum(uncompressed.data)); - - /* - var i; - var uncompressed2 = zip_inflate(data); - for (i=0;i= 0; b--) { + var dp = (y*FBU.width + x*8 + 7-b) * 3; + var sp = (data[y*w + x] >> b & 1) * 3; + dest[dp ] = FBU.palette[sp ]; + dest[dp+1] = FBU.palette[sp+1]; + dest[dp+2] = FBU.palette[sp+2]; + } + for (b = 7; b >= 8 - FBU.width % 8; b--) { + var dp = (y*FBU.width + x*8 + 7-b) * 3; + var sp = (data[y*w + x] >> b & 1) * 3; + dest[dp ] = FBU.palette[sp ]; + dest[dp+1] = FBU.palette[sp+1]; + dest[dp+2] = FBU.palette[sp+2]; + } + } + } + } else { + for (y = 0; y < FBU.height; y++) { + for (x = 0; x < FBU.width; x++) { + var dp = (y*FBU.width + x) * 3; + var sp = data[y*FBU.width + x] * 3; + dest[dp ] = FBU.palette[sp ]; + dest[dp+1] = FBU.palette[sp+1]; + dest[dp+2] = FBU.palette[sp+2]; + } + } + } + + FBU.imgQ.push({ + 'type': 'rgb', + 'img': {'complete': true, 'data': dest}, + 'x': FBU.x, + 'y': FBU.y, + 'width': FBU.width, + 'height': FBU.height}); return true; } @@ -1368,8 +1398,6 @@ encHandlers.TIGHT = function display_tight() { FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + zlib-data if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } - if (streamId != 0) throw ("Wrong stream"); - ws.rQshiftBytes(1 + clength[0]); // ctl + clength if (raw) data = ws.rQshiftBytes(clength[1]); From c514fd5e1c74889b3db397dd3987f3e91067b350 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Sun, 29 Jan 2012 02:10:25 -0500 Subject: [PATCH 09/13] don't need to copy palette data until we have all data for rect; change a few comments --- include/rfb.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/rfb.js b/include/rfb.js index 3ccd73a4..ba94fd99 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -1315,11 +1315,8 @@ encHandlers.TIGHT = function display_tight() { var numColors = rQ[rQi + 2] + 1; var paletteSize = numColors * fb_depth; FBU.bytes += paletteSize; - if (ws.rQwait("TIGHT palette " + cmode, FBU.bytes)) { return false; } - FBU.palette = ws.rQslice(3, 3 + paletteSize); - var bpp = (numColors <= 2) ? 1 : 8; var rowSize = Math.floor((FBU.width * bpp + 7) / 8); var raw = false; @@ -1333,14 +1330,16 @@ encHandlers.TIGHT = function display_tight() { if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } // Shift ctl, filter id, num colors, palette entries, and clength off - ws.rQshiftBytes(3 + paletteSize + clength[0]); + ws.rQshiftBytes(3); + FBU.palette = ws.rQshiftBytes(paletteSize); + ws.rQshiftBytes(clength[0]); - // Decompress data if (raw) data = ws.rQshiftBytes(clength[1]); else data = decompress(ws.rQshiftBytes(clength[1])); + // Convert indexed (palette based) image data to RGB var dest = []; var x, y, b; if (numColors == 2) { @@ -1395,10 +1394,12 @@ encHandlers.TIGHT = function display_tight() { } else clength = getCLength(ws.rQslice(1, 4)); - FBU.bytes = 1 + clength[0] + clength[1]; // ctl + clength size + zlib-data + FBU.bytes = 1 + clength[0] + clength[1]; if (ws.rQwait("TIGHT " + cmode, FBU.bytes)) { return false; } - ws.rQshiftBytes(1 + clength[0]); // ctl + clength + // Shift ctl, clength off + ws.rQshiftBytes(1 + clength[0]); + if (raw) data = ws.rQshiftBytes(clength[1]); else From 2cedf4839728bbe4c5db1ab4735eb16b1c30d5ae Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Mon, 30 Jan 2012 02:19:18 -0500 Subject: [PATCH 10/13] add last rect special encoding; fix tight fill subencoding color handling; fix mono indexed rect handling --- include/rfb.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/include/rfb.js b/include/rfb.js index ba94fd99..22165c40 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -55,10 +55,12 @@ var that = {}, // Public API methods ['Cursor', -239 ], // Psuedo-encoding settings - ['JPEG_quality_lo', -32 ], + //['JPEG_quality_lo', -32 ], + ['JPEG_quality_med', -26 ], //['JPEG_quality_hi', -23 ], - ['compress_lo', -255 ] - //['compress_hi', -247 ] + //['compress_lo', -255 ], + ['compress_hi', -247 ], + ['last_rect', -224 ] ], encHandlers = {}, @@ -986,6 +988,11 @@ framebufferUpdate = function() { 'encoding': FBU.encoding, 'encodingName': encNames[FBU.encoding]}); + if (encNames[FBU.encoding] == 'last_rect') { + FBU.rects = 0; + break; + } + if (encNames[FBU.encoding]) { // Debug: /* @@ -1354,13 +1361,13 @@ encHandlers.TIGHT = function display_tight() { dest[dp+1] = FBU.palette[sp+1]; dest[dp+2] = FBU.palette[sp+2]; } - for (b = 7; b >= 8 - FBU.width % 8; b--) { - var dp = (y*FBU.width + x*8 + 7-b) * 3; - var sp = (data[y*w + x] >> b & 1) * 3; - dest[dp ] = FBU.palette[sp ]; - dest[dp+1] = FBU.palette[sp+1]; - dest[dp+2] = FBU.palette[sp+2]; - } + } + for (b = 7; b >= 8 - FBU.width % 8; b--) { + var dp = (y*FBU.width + x*8 + 7-b) * 3; + var sp = (data[y*w + x] >> b & 1) * 3; + dest[dp ] = FBU.palette[sp ]; + dest[dp+1] = FBU.palette[sp+1]; + dest[dp+2] = FBU.palette[sp+2]; } } } else { @@ -1456,7 +1463,7 @@ encHandlers.TIGHT = function display_tight() { 'y': FBU.y, 'width': FBU.width, 'height': FBU.height, - 'color': color}); + 'color': [color[2], color[1], color[0]] }); break; case "jpeg": clength = getCLength(ws.rQslice(1, 4)); From a14b8fae2a9e047268b90f35f3d5e90f4ea5ed0d Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Mon, 30 Jan 2012 02:26:36 -0500 Subject: [PATCH 11/13] comment out per-decompress checksum and logging --- include/rfb.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/rfb.js b/include/rfb.js index 22165c40..64e4b98c 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -1312,8 +1312,8 @@ encHandlers.TIGHT = function display_tight() { var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); if (uncompressed.status !== 0) throw("Invalid data in zlib stream"); - Util.Warn("Decompressed " + data.length + " to " + uncompressed.data.length + " checksums " + - checksum(data) + ":" + checksum(uncompressed.data)); + //Util.Warn("Decompressed " + data.length + " to " + uncompressed.data.length + " checksums " + + // checksum(data) + ":" + checksum(uncompressed.data)); return uncompressed.data; } @@ -1347,6 +1347,7 @@ encHandlers.TIGHT = function display_tight() { data = decompress(ws.rQshiftBytes(clength[1])); // Convert indexed (palette based) image data to RGB + // TODO: reduce number of calculations inside loop var dest = []; var x, y, b; if (numColors == 2) { From 9b75bcaadaa5671683224bd2bf22316d6260d586 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Tue, 31 Jan 2012 00:15:56 -0500 Subject: [PATCH 12/13] add tight zlib stream reset; add error if tight encoding is used w/o true color --- include/rfb.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/rfb.js b/include/rfb.js index 64e4b98c..cb01fe1e 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -1276,6 +1276,11 @@ encHandlers.HEXTILE = function display_hextile() { encHandlers.TIGHT = function display_tight() { Util.Debug(">> display_tight"); + + if (fb_depth == 1) { + fail("Tight protocol handler only implements true color mode"); + } + var ctl, cmode, clength, getCLength, color, img, data; var filterId = -1, resetStreams = 0, streamId = -1; var rQ = ws.get_rQ(), rQi = ws.get_rQi(); @@ -1308,10 +1313,15 @@ encHandlers.TIGHT = function display_tight() { } var decompress = function(data) { - // TODO: process resetStreams here + for (var i=0; i<4; i++) { + if ((resetStreams >> i) & 1) { + FBU.zlibs[i].reset(); + Util.Info("Reset zlib stream " + i); + } + } var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); if (uncompressed.status !== 0) - throw("Invalid data in zlib stream"); + Util.Error("Invalid data in zlib stream"); //Util.Warn("Decompressed " + data.length + " to " + uncompressed.data.length + " checksums " + // checksum(data) + ":" + checksum(uncompressed.data)); @@ -1427,7 +1437,6 @@ encHandlers.TIGHT = function display_tight() { // Keep tight reset bits resetStreams = ctl & 0xF; - if (resetStreams) throw ("Tight reset"); // Figure out filter ctl = ctl >> 4; From d38db74abd0efa34f7297dc19bf603b7f765e0f5 Mon Sep 17 00:00:00 2001 From: Mike Tinglof Date: Fri, 9 Mar 2012 11:02:18 -0500 Subject: [PATCH 13/13] add some documentation; default to existing websocket transport --- README.md | 2 +- include/jsunzip.js | 3 +++ include/rfb.js | 19 +++++++++++++------ include/vnc.js | 2 ++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5bd75ad5..2be5adc8 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ their products including: [Ganeti Web Manager](http://code.osuosl.org/projects/g * Easy site integration and theming (3 example themes included) * Licensed under the [LGPLv3](http://www.gnu.org/licenses/lgpl.html) - +* Support for tight encoding (contributed by Mercuri.ca) ### Screenshots diff --git a/include/jsunzip.js b/include/jsunzip.js index 1d2957bd..f815218f 100755 --- a/include/jsunzip.js +++ b/include/jsunzip.js @@ -195,6 +195,9 @@ function JSUnzip() { /* * tinflate javascript port by Erik Moller in May 2011. * emoller@opera.com + * + * read_bits() patched by mike@imidio.com to allow + * reading more then 8 bits (needed in some zlib streams) */ "use strict"; diff --git a/include/rfb.js b/include/rfb.js index cb01fe1e..c1c121a2 100644 --- a/include/rfb.js +++ b/include/rfb.js @@ -4,6 +4,9 @@ * Licensed under LGPL-3 (see LICENSE.txt) * * See README.md for usage and integration instructions. + * + * TIGHT decoder portion: + * (c) 2012 Michael Tinglof, Joe Balaz, Les Piech (Mercuri.ca) */ /*jslint white: false, browser: true, bitwise: false, plusplus: false */ @@ -275,14 +278,18 @@ function constructor() { function connect() { Util.Debug(">> RFB.connect"); - - var uri = ""; - if (conf.encrypt) { - uri = "wss://"; + var uri; + + if (typeof UsingSocketIO !== "undefined") { + uri = "http://" + rfb_host + ":" + rfb_port + "/" + rfb_path; } else { - uri = "ws://"; + if (conf.encrypt) { + uri = "wss://"; + } else { + uri = "ws://"; + } + uri += rfb_host + ":" + rfb_port + "/" + rfb_path; } - uri += rfb_host + ":" + rfb_port + "/" + rfb_path; Util.Info("connecting to " + uri); ws.open(uri); diff --git a/include/vnc.js b/include/vnc.js index 2b31f458..145e9acb 100644 --- a/include/vnc.js +++ b/include/vnc.js @@ -32,9 +32,11 @@ function get_INCLUDE_URI() { extra += start + "webutil.js" + end; extra += start + "base64.js" + end; extra += start + "websock.js" + end; + // extra += start + "socketio.js" + end; extra += start + "des.js" + end; extra += start + "input.js" + end; extra += start + "display.js" + end; + extra += start + "blowfish.js" + end; extra += start + "rfb.js" + end; extra += start + "jsunzip.js" + end;