fix: signed 13 and 32 bit ints (l and s formats)

This commit is contained in:
Andrey Sidorov 2014-03-31 14:05:51 +11:00
parent a930aa4d13
commit 33005e8f76

View file

@ -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 // maybe best approach is to wait all data required for format and then process fixed buffer
// TODO: byte order!!! // TODO: byte order!!!
switch (arg) { switch (arg) {
case 'C': case 'C':
this.data.push(bufferlist.getbyte()); this.data.push(bufferlist.getbyte());
break; break;
case 'S': case 'S':
@ -121,7 +121,7 @@ UnpackStream.prototype.unpackTo = function(destination, names_formats, callback)
{ {
var names = []; var names = [];
var format = ''; var format = '';
for (var i=0; i < names_formats.length; ++i) for (var i=0; i < names_formats.length; ++i)
{ {
var off = 0; var off = 0;
@ -172,7 +172,7 @@ UnpackStream.prototype.resume = function()
} }
UnpackStream.prototype.getbyte = function() UnpackStream.prototype.getbyte = function()
{ {
var res = 0; var res = 0;
var b = this.readlist[0]; var b = this.readlist[0];
if (this.offset + 1 < b.length) if (this.offset + 1 < b.length)
@ -180,7 +180,7 @@ UnpackStream.prototype.getbyte = function()
res = b[this.offset]; res = b[this.offset];
this.offset++; this.offset++;
this.length--; this.length--;
} else { } else {
// last byte in current buffer, shift read list // last byte in current buffer, shift read list
@ -202,7 +202,7 @@ UnpackStream.prototype.pstr = function(str)
if (len == 0) if (len == 0)
return; // nothing to write return; // nothing to write
var buf = new Buffer(len); var buf = new Buffer(len);
buf.write(str, 'binary'); buf.write(str, 'binary');
this.write_queue.push(buf); this.write_queue.push(buf);
} }
*/ */
@ -211,7 +211,7 @@ UnpackStream.prototype.pstr = function(str)
UnpackStream.prototype.pack = function(format, args) UnpackStream.prototype.pack = function(format, args)
{ {
var packetlength = 0; var packetlength = 0;
var arg = 0; var arg = 0;
for (var i = 0; i < format.length; ++i) for (var i = 0; i < format.length; ++i)
{ {
@ -238,20 +238,30 @@ UnpackStream.prototype.pack = function(format, args)
{ {
switch(format[i]) switch(format[i])
{ {
case 'x': case 'x':
buf[offset++] = 0; buf[offset++] = 0;
break; break;
case 'C': case 'C':
var n = args[arg++]; var n = args[arg++];
buf[offset++] = n; buf[offset++] = n;
break; break;
case 's': // TODO: implement signed INT16!!! case 's':
var n = args[arg++];
buf.writeInt16LE(n, offset);
offset += 2;
break;
case 'S': case 'S':
var n = args[arg++]; var n = args[arg++];
buf[offset++] = n & 0xff; buf[offset++] = n & 0xff;
buf[offset++] = (n >> 8) & 0xff; buf[offset++] = (n >> 8) & 0xff;
break; break;
case 'l': // TODO: implement signed INT32!!! case 'l':
var n = args[arg++];
buf.writeInt32LE(n, offset);
offset += 4;
break;
case 'L': case 'L':
var n = args[arg++]; var n = args[arg++];
buf[offset++] = n & 0xff; buf[offset++] = n & 0xff;
@ -281,7 +291,7 @@ UnpackStream.prototype.pack = function(format, args)
for (; c < len; ++c) for (; c < len; ++c)
buf[offset++] = 0; buf[offset++] = 0;
break; break;
} }
} }
this.write_queue.push(buf); this.write_queue.push(buf);
this.write_length += buf.length; this.write_length += buf.length;
@ -290,7 +300,7 @@ UnpackStream.prototype.pack = function(format, args)
UnpackStream.prototype.flush = function(stream) UnpackStream.prototype.flush = function(stream)
{ {
// TODO: measure performance benefit of // TODO: measure performance benefit of
// creating and writing one big concatenated buffer // creating and writing one big concatenated buffer
// TODO: check write result // TODO: check write result