diff --git a/lib/reader2.js b/lib/reader2.js index 6c22e07..4fcf0a4 100644 --- a/lib/reader2.js +++ b/lib/reader2.js @@ -236,20 +236,46 @@ Reader.prototype.readString = function(data) { var code = this.reader.nextUInt8(); if (code >= 0 && code < 32) { - return this.reader.nextString(code); + return this._nextString(code); } else if (code >= 0x30 && code <= 0x33) { this.reader.move(-1); var len = this.reader.nextUInt16BE() - 0x3000; - return this.reader.nextString(len); + return this._nextString(len); } else if (code === 0x53) { var len = this.reader.nextUInt16BE(); - return this.reader.nextString(len); + return this._nextString(len); } else if (code === 0x52) { var len = this.reader.nextUInt16BE(); - return this.reader.nextString(len) + this.readString(); + return this._nextString(len) + this.readString(); } }; +Reader.prototype._nextString = function(length, encoding) { + + assert(length >= 0, 'Length must be no negative'); + + var tbuf = []; + while(length > 0) { + + var t = this.reader.nextUInt8(); + tbuf.push(t); + if(t >= 0xC2 && t < 0xE0) { + tbuf.push(this.reader.nextUInt8()); + } + else if(t >= 0xE0 && t < 0xF0) { + tbuf.push(this.reader.nextUInt8()); + tbuf.push(this.reader.nextUInt8()); + } + else if(t >= 0xF0 && t < 0xF5) { + tbuf.push(this.reader.nextUInt8()); + tbuf.push(this.reader.nextUInt8()); + tbuf.push(this.reader.nextUInt8()); + } + length--; + } + return new Buffer(tbuf).toString(encoding); +}; + Reader.prototype.readLong = function(data) { if (data) this.reader = new BufferReader(data);