Commit 0fa6748c authored by Mike Tinglof's avatar Mike Tinglof

fix issue with parsing distance of more then 8 bits; convert to just supporting arrays for buffers

parent a820f126
...@@ -337,7 +337,7 @@ this.getbit = function(d) ...@@ -337,7 +337,7 @@ this.getbit = function(d)
if (!d.bitcount--) if (!d.bitcount--)
{ {
/* load next tag */ /* load next tag */
d.tag = d.source.charCodeAt(d.sourceIndex++) & 0xff; d.tag = d.source[d.sourceIndex++] & 0xff;
d.bitcount = 7; d.bitcount = 7;
} }
...@@ -355,11 +355,11 @@ this.read_bits = function(d, num, base) ...@@ -355,11 +355,11 @@ this.read_bits = function(d, num, base)
return base; return base;
var val = 0; var val = 0;
while (d.bitcount < 24) { while (d.bitcount < num) {
d.tag = d.tag | (d.source.charCodeAt(d.sourceIndex++) & 0xff) << d.bitcount; d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount;
d.bitcount += 8; d.bitcount += 8;
} }
val = d.tag & (0xff >> (8 - num)); val = d.tag & (0xffff >> (16 - num));
d.tag >>= num; d.tag >>= num;
d.bitcount -= num; d.bitcount -= num;
return val + base; return val + base;
...@@ -368,8 +368,8 @@ this.read_bits = function(d, num, base) ...@@ -368,8 +368,8 @@ this.read_bits = function(d, num, base)
/* given a data stream and a tree, decode a symbol */ /* given a data stream and a tree, decode a symbol */
this.decode_symbol = function(d, t) this.decode_symbol = function(d, t)
{ {
while (d.bitcount < 24) { while (d.bitcount < 10) {
d.tag = d.tag | (d.source.charCodeAt(d.sourceIndex++) & 0xff) << d.bitcount; d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount;
d.bitcount += 8; d.bitcount += 8;
} }
...@@ -488,6 +488,7 @@ this.inflate_block_data = function(d, lt, dt) ...@@ -488,6 +488,7 @@ this.inflate_block_data = function(d, lt, dt)
if (sym < 256) if (sym < 256)
{ {
ddest[ddestlength++] = sym; // ? String.fromCharCode(sym); ddest[ddestlength++] = sym; // ? String.fromCharCode(sym);
d.history.push(sym);
} else { } else {
var length, dist, offs; var length, dist, offs;
...@@ -503,12 +504,14 @@ this.inflate_block_data = function(d, lt, dt) ...@@ -503,12 +504,14 @@ this.inflate_block_data = function(d, lt, dt)
/* possibly get more bits from distance code */ /* possibly get more bits from distance code */
offs = ddestlength - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]); offs = ddestlength - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]);
if (offs < 0)
throw ("Invalid zlib offset " + offs);
/* copy match */ /* copy match */
for (i = offs; i < offs + length; ++i) { for (i = offs; i < offs + length; ++i) {
if (i < 0) ddest[ddestlength++] = ddest[i];
ddest[ddestlength++] = d.history[d.history.length + i]; //ddest[ddestlength++] = d.history[i];
else //d.history.push(d.history[i]);
ddest[ddestlength++] = ddest[i];
} }
} }
} }
...@@ -541,8 +544,10 @@ this.inflate_uncompressed_block = function(d) ...@@ -541,8 +544,10 @@ this.inflate_uncompressed_block = function(d)
d.sourceIndex += 4; d.sourceIndex += 4;
/* copy block */ /* 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++]; d.dest[d.dest.length] = d.source[d.sourceIndex++];
}
/* make sure we start next block on a byte boundary */ /* make sure we start next block on a byte boundary */
d.bitcount = 0; d.bitcount = 0;
...@@ -597,13 +602,10 @@ this.reset = function() ...@@ -597,13 +602,10 @@ this.reset = function()
/* inflate stream from source to dest */ /* inflate stream from source to dest */
this.uncompress = function(source, offset) this.uncompress = function(source, offset)
{ {
var d = this.d; var d = this.d;
var bfinal; var bfinal;
if (Object.prototype.toString.call(source) === '[object Array]') {
source.charCodeAt = function(id) { return this[id]; }
}
/* initialise data */ /* initialise data */
d.source = source; d.source = source;
d.sourceIndex = offset; d.sourceIndex = offset;
...@@ -655,15 +657,8 @@ this.uncompress = function(source, offset) ...@@ -655,15 +657,8 @@ this.uncompress = function(source, offset)
} while (!bfinal && d.sourceIndex < d.source.length); } while (!bfinal && d.sourceIndex < d.source.length);
if (blocks != 2) throw ("Unexpected number of blocks"); //d.history.push.apply(d.history, d.dest);
d.history = d.history.slice(-this.WINDOW_SIZE);
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);
}
return { 'status' : this.OK, 'data' : d.dest }; return { 'status' : this.OK, 'data' : d.dest };
} }
......
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