Fixed inflate utf8 decoder

master
Vitaly Puzrin 2014-04-13 21:30:00 +04:00
rodzic b165c9c945
commit 33c90194d0
2 zmienionych plików z 10 dodań i 12 usunięć

Wyświetl plik

@ -11,9 +11,9 @@ var gzheader = require('./zlib/gzheader');
// calculate tail size of utf8 char by current byte value
/*function utf8tail(code) {
function utf8tail(code) {
return code >= 252 ? 6 : code >= 248 ? 5 : code >= 240 ? 4 : code >= 224 ? 3 : code >= 192 ? 2 : 1;
}*/
}
/**
* class Inflate
@ -180,7 +180,7 @@ Inflate.prototype.push = function(data, mode) {
var strm = this.strm;
var chunkSize = this.options.chunkSize;
var status, _mode;
//var next_out_utf8_index, tail, utf8str;
var next_out_utf8_index, tail, utf8str;
if (this.ended) { return false; }
_mode = (mode === ~~mode) ? mode : ((mode === true) ? c.Z_FINISH : c.Z_NO_FLUSH);
@ -214,14 +214,14 @@ Inflate.prototype.push = function(data, mode) {
if (strm.next_out_index) {
if (strm.avail_out === 0 || status === c.Z_STREAM_END || (strm.avail_in === 0 && _mode === c.Z_FINISH)) {
/*if (this.options.to === 'string') {
if (this.options.to === 'string') {
// realign size to utf8 char border & move tail to start of buffer
next_out_utf8_index = strm.next_out_index - 5;
next_out_utf8_index = strm.next_out_index - 6;
if (next_out_utf8_index < 0) { next_out_utf8_index = 0; }
tail = utf8tail(strm.next_out[next_out_utf8_index]);
while (next_out_utf8_index + tail < strm.next_out_index) {
while (next_out_utf8_index + tail <= strm.next_out_index) {
next_out_utf8_index += tail;
tail = utf8tail(strm.next_out[next_out_utf8_index]);
}
@ -229,9 +229,9 @@ Inflate.prototype.push = function(data, mode) {
// shit happened - broken tail. then take it all.
if (next_out_utf8_index === 0) {
next_out_utf8_index = strm.next_out_index;
tail = 0;
}
tail = strm.next_out_index - next_out_utf8_index;
utf8str = strings.buf2string(strm.next_out, next_out_utf8_index);
// move tail
@ -243,8 +243,7 @@ Inflate.prototype.push = function(data, mode) {
} else {
this.onData(utils.shrinkBuf(strm.next_out, strm.next_out_index));
}*/
this.onData(utils.shrinkBuf(strm.next_out, strm.next_out_index));
}
}
}
} while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== c.Z_STREAM_END);
@ -293,8 +292,7 @@ Inflate.prototype.onEnd = function(status) {
if (this.options.to === 'string') {
// Glue & convert here, until we teach pako to send
// utf8 alligned strings to onData
//this.result = this.chunks.join('');
this.result = strings.buf2string(utils.flattenChunks(this.chunks));
this.result = this.chunks.join('');
} else {
this.result = utils.flattenChunks(this.chunks);
}

Wyświetl plik

@ -50,7 +50,7 @@ describe('Inflate strings', function () {
var data = pako.inflate(deflatedArray, { to: 'string', chunkSize: 99 });
assert.equal(typeof data, 'string');
assert.equal(data, sampleString);
assert(data === sampleString);
});
});