this.OK = 0;
this.DATA_ERROR = (-3);
+this.WINDOW_SIZE = 32768;
/* ------------------------------ *
* -- internal data structures -- *
this.bitcount = 0;
this.dest = [];
+
+ this.history = [];
this.ltree = new that.TREE(); /* dynamic length/symbol tree */
this.dtree = new that.TREE(); /* dynamic distance tree */
if (sym < 256)
{
- ddest[ddestlength++] = String.fromCharCode(sym);
+ ddest[ddestlength++] = sym; // ? String.fromCharCode(sym);
} else {
var length, dist, offs;
/* 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];
}
}
}
this.reset = function()
{
this.d = new this.DATA(this);
- this.header = null;
+ delete this.header;
}
/* inflate stream from source to dest */
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 {
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 };
}
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;
}