diff --git a/lib/proxy.js b/lib/proxy.js index b0b3b86..e72c5fc 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -48,11 +48,9 @@ Proxy.prototype.invoke = function(method, args, callback) { }; if (this.username) { - options.auth = { - user: this.username, - pass: this.password, - sendImmediately: true - }; + // Use iso-8859-1 character encoding for basic authentication parameters + options.headers['Authorization'] = 'Basic ' + + Buffer.from(this.username + ':' + this.password, 'latin1').toString('base64'); } var req = require(this.protocol).request(options, function(res) { @@ -87,4 +85,4 @@ Proxy.prototype.invoke = function(method, args, callback) { }; -module.exports = Proxy; \ No newline at end of file +module.exports = Proxy; diff --git a/lib/reader2.js b/lib/reader2.js index 6c22e07..0295523 100644 --- a/lib/reader2.js +++ b/lib/reader2.js @@ -229,6 +229,33 @@ Reader.prototype.readClassDef = function(data) { } }; +Reader.prototype.readUTF8String = function(len) { + + if (len === 0) + return ""; + + var startPos = this.reader.tell(), byteLength; + + while (len--) { + var head = this.reader.nextUInt8(); + if (head < 0x80) { + continue; + } else if ((head & 0xe0) === 0xc0) { + this.reader.move(1); + } else if ((head & 0xf0) === 0xe0) { + this.reader.move(2); + } else if ((head & 0xf8) === 0xf0) { + this.reader.move(3); + } else { + throw new Error("String is not in valid UTF-8 format"); + } + } + + byteLength = this.reader.tell() - startPos; + this.reader.seek(startPos); + + return this.reader.nextString(byteLength, 'utf8'); +} Reader.prototype.readString = function(data) { if (data) @@ -236,17 +263,17 @@ Reader.prototype.readString = function(data) { var code = this.reader.nextUInt8(); if (code >= 0 && code < 32) { - return this.reader.nextString(code); + return this.readUTF8String(code); } else if (code >= 0x30 && code <= 0x33) { this.reader.move(-1); var len = this.reader.nextUInt16BE() - 0x3000; - return this.reader.nextString(len); + return this.readUTF8String(len); } else if (code === 0x53) { var len = this.reader.nextUInt16BE(); - return this.reader.nextString(len); + return this.readUTF8String(len); } else if (code === 0x52) { var len = this.reader.nextUInt16BE(); - return this.reader.nextString(len) + this.readString(); + return this.readUTF8String(len) + this.readString(); } }; @@ -389,9 +416,11 @@ Reader.prototype.readDouble = function(data) { return this.reader.nextInt8(); else if (code === 0x5e) return this.reader.nextInt16BE(); - else if (code === 0x5f) - return this.reader.nextFloatBE(); - else if (code === 0x44) { + else if (code === 0x5f) { + // While the specification suggests reading a FloatBE here, + // this is the way how it's handled by the Java implementation + return this.reader.nextInt32BE() * 0.001; + } else if (code === 0x44) { return this.reader.nextDoubleBE(); } }; diff --git a/lib/writer2.js b/lib/writer2.js index 5cde437..de43c4b 100644 --- a/lib/writer2.js +++ b/lib/writer2.js @@ -366,11 +366,11 @@ Writer.prototype.writeList = function(data) { }; Writer.prototype.writeType = function(data) { - this.writeString(data); if (this.typeRefs.hasOwnProperty(data)) { this.writeInt(this.typeRefs[data]); } else { this.typeRefs[data] = Object.keys(this.typeRefs).length; + this.writeString(data); } return this; }; @@ -388,4 +388,4 @@ function cap(str) { return str.charAt(0).toUpperCase() + str.slice(1); } -module.exports = Writer; \ No newline at end of file +module.exports = Writer;