Commit c577ca23 authored by Mike Tinglof's avatar Mike Tinglof

keep zlib history so we can decode as a stream

parent de84e098
...@@ -201,6 +201,7 @@ function TINF() { ...@@ -201,6 +201,7 @@ function TINF() {
this.OK = 0; this.OK = 0;
this.DATA_ERROR = (-3); this.DATA_ERROR = (-3);
this.WINDOW_SIZE = 32768;
/* ------------------------------ * /* ------------------------------ *
* -- internal data structures -- * * -- internal data structures -- *
...@@ -218,6 +219,8 @@ this.DATA = function(that) { ...@@ -218,6 +219,8 @@ this.DATA = function(that) {
this.bitcount = 0; this.bitcount = 0;
this.dest = []; this.dest = [];
this.history = [];
this.ltree = new that.TREE(); /* dynamic length/symbol tree */ this.ltree = new that.TREE(); /* dynamic length/symbol tree */
this.dtree = new that.TREE(); /* dynamic distance tree */ this.dtree = new that.TREE(); /* dynamic distance tree */
...@@ -482,7 +485,7 @@ this.inflate_block_data = function(d, lt, dt) ...@@ -482,7 +485,7 @@ this.inflate_block_data = function(d, lt, dt)
if (sym < 256) if (sym < 256)
{ {
ddest[ddestlength++] = String.fromCharCode(sym); ddest[ddestlength++] = sym; // ? String.fromCharCode(sym);
} else { } else {
var length, dist, offs; var length, dist, offs;
...@@ -500,7 +503,10 @@ this.inflate_block_data = function(d, lt, dt) ...@@ -500,7 +503,10 @@ this.inflate_block_data = function(d, lt, dt)
/* copy match */ /* copy match */
for (i = offs; i < offs + length; ++i) { 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() ...@@ -576,7 +582,7 @@ this.init = function()
this.reset = function() this.reset = function()
{ {
this.d = new this.DATA(this); this.d = new this.DATA(this);
this.header = null; delete this.header;
} }
/* inflate stream from source to dest */ /* inflate stream from source to dest */
...@@ -597,9 +603,9 @@ this.uncompress = function(source, offset) ...@@ -597,9 +603,9 @@ this.uncompress = function(source, offset)
d.dest = []; d.dest = [];
// Skip zlib header at start of stream // Skip zlib header at start of stream
/*if (typeof header == 'undefined') { if (typeof this.header == 'undefined') {
header = this.read_bits(d, 16, 0); this.header = this.read_bits(d, 16, 0);
}*/ }
do { do {
...@@ -633,12 +639,22 @@ this.uncompress = function(source, offset) ...@@ -633,12 +639,22 @@ this.uncompress = function(source, offset)
if (res != this.OK) return { 'status' : this.DATA_ERROR }; 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]') { if (Object.prototype.toString.call(source) !== '[object Array]') {
d.dest = d.dest.join(''); 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 }; return { 'status' : this.OK, 'data' : d.dest };
} }
......
...@@ -1293,8 +1293,8 @@ encHandlers.TIGHT = function display_tight() { ...@@ -1293,8 +1293,8 @@ encHandlers.TIGHT = function display_tight() {
var decompress = function(data) { var decompress = function(data) {
// TODO: process resetStreams here // TODO: process resetStreams here
var uncompressed = FBU.zlibs[streamId].uncompress(data, 0); var uncompressed = FBU.zlibs[streamId].uncompress(data, 0);
/*if (uncompressed.status != 0) if (uncompressed.status != 0)
throw("Invalid data in zlib stream");*/ throw("Invalid data in zlib stream");
return uncompressed.data; return uncompressed.data;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment