From 33005e8f761f1e8f0b678a7c98e1022789f5933d Mon Sep 17 00:00:00 2001 From: Andrey Sidorov Date: Mon, 31 Mar 2014 14:05:51 +1100 Subject: [PATCH] fix: signed 13 and 32 bit ints (l and s formats) --- lib/unpackstream.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/unpackstream.js b/lib/unpackstream.js index 5ff46e7..934f2f6 100644 --- a/lib/unpackstream.js +++ b/lib/unpackstream.js @@ -55,7 +55,7 @@ ReadFormatRequest.prototype.execute = function(bufferlist, tag1, tag2) // maybe best approach is to wait all data required for format and then process fixed buffer // TODO: byte order!!! switch (arg) { - case 'C': + case 'C': this.data.push(bufferlist.getbyte()); break; case 'S': @@ -121,7 +121,7 @@ UnpackStream.prototype.unpackTo = function(destination, names_formats, callback) { var names = []; var format = ''; - + for (var i=0; i < names_formats.length; ++i) { var off = 0; @@ -172,7 +172,7 @@ UnpackStream.prototype.resume = function() } UnpackStream.prototype.getbyte = function() -{ +{ var res = 0; var b = this.readlist[0]; if (this.offset + 1 < b.length) @@ -180,7 +180,7 @@ UnpackStream.prototype.getbyte = function() res = b[this.offset]; this.offset++; this.length--; - + } else { // last byte in current buffer, shift read list @@ -202,7 +202,7 @@ UnpackStream.prototype.pstr = function(str) if (len == 0) return; // nothing to write var buf = new Buffer(len); - buf.write(str, 'binary'); + buf.write(str, 'binary'); this.write_queue.push(buf); } */ @@ -211,7 +211,7 @@ UnpackStream.prototype.pstr = function(str) UnpackStream.prototype.pack = function(format, args) { var packetlength = 0; - + var arg = 0; for (var i = 0; i < format.length; ++i) { @@ -238,20 +238,30 @@ UnpackStream.prototype.pack = function(format, args) { switch(format[i]) { - case 'x': + case 'x': buf[offset++] = 0; break; case 'C': var n = args[arg++]; buf[offset++] = n; break; - case 's': // TODO: implement signed INT16!!! + case 's': + var n = args[arg++]; + buf.writeInt16LE(n, offset); + offset += 2; + break; + case 'S': var n = args[arg++]; buf[offset++] = n & 0xff; buf[offset++] = (n >> 8) & 0xff; break; - case 'l': // TODO: implement signed INT32!!! + case 'l': + var n = args[arg++]; + buf.writeInt32LE(n, offset); + offset += 4; + break; + case 'L': var n = args[arg++]; buf[offset++] = n & 0xff; @@ -281,7 +291,7 @@ UnpackStream.prototype.pack = function(format, args) for (; c < len; ++c) buf[offset++] = 0; break; - } + } } this.write_queue.push(buf); this.write_length += buf.length; @@ -290,7 +300,7 @@ UnpackStream.prototype.pack = function(format, args) UnpackStream.prototype.flush = function(stream) { - // TODO: measure performance benefit of + // TODO: measure performance benefit of // creating and writing one big concatenated buffer // TODO: check write result