Permalink
Browse files

Fixed write functions, thanks to @ysangkok

  • Loading branch information...
1 parent ee1007e commit d29e35bc7d7eed9b16a1ea6ca40443d6e2ad911a @toots committed Sep 20, 2012
Showing with 35 additions and 13 deletions.
  1. +13 −13 index.js
  2. +22 −0 test/buffer.js
View
@@ -931,7 +931,7 @@ Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
verifuint(value, 0xff);
}
- buffer[offset] = value;
+ buffer.parent[buffer.offset + offset] = value;
};
function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
@@ -952,11 +952,11 @@ function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
}
if (isBigEndian) {
- buffer[offset] = (value & 0xff00) >>> 8;
- buffer[offset + 1] = value & 0x00ff;
+ buffer.parent[buffer.offset + offset] = (value & 0xff00) >>> 8;
+ buffer.parent[buffer.offset + offset + 1] = value & 0x00ff;
} else {
- buffer[offset + 1] = (value & 0xff00) >>> 8;
- buffer[offset] = value & 0x00ff;
+ buffer.parent[buffer.offset + offset + 1] = (value & 0xff00) >>> 8;
+ buffer.parent[buffer.offset + offset] = value & 0x00ff;
}
}
@@ -986,15 +986,15 @@ function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
}
if (isBigEndian) {
- buffer[offset] = (value >>> 24) & 0xff;
- buffer[offset + 1] = (value >>> 16) & 0xff;
- buffer[offset + 2] = (value >>> 8) & 0xff;
- buffer[offset + 3] = value & 0xff;
+ buffer.parent[buffer.offset + offset] = (value >>> 24) & 0xff;
+ buffer.parent[buffer.offset + offset + 1] = (value >>> 16) & 0xff;
+ buffer.parent[buffer.offset + offset + 2] = (value >>> 8) & 0xff;
+ buffer.parent[buffer.offset + offset + 3] = value & 0xff;
} else {
- buffer[offset + 3] = (value >>> 24) & 0xff;
- buffer[offset + 2] = (value >>> 16) & 0xff;
- buffer[offset + 1] = (value >>> 8) & 0xff;
- buffer[offset] = value & 0xff;
+ buffer.parent[buffer.offset + offset + 3] = (value >>> 24) & 0xff;
+ buffer.parent[buffer.offset + offset + 2] = (value >>> 16) & 0xff;
+ buffer.parent[buffer.offset + offset + 1] = (value >>> 8) & 0xff;
+ buffer.parent[buffer.offset + offset] = value & 0xff;
}
}
View
@@ -72,3 +72,25 @@ test('hex buffer to ascii', function (t) {
);
t.end();
});
+
+test("hex of write{Uint,Int}{8,16,32}{LE,BE}", function(t) {
+ t.plan(2*2*2+2);
+ ["UInt","Int"].forEach(function(x){
+ [8,16,32].forEach(function(y){
+ var endianesses = (y === 8) ? [""] : ["LE","BE"];
+ endianesses.forEach(function(z){
+ var v1 = new buffer.Buffer(y / 8);
+ var v2 = new Buffer(y / 8);
+ var fn = "write" + x + y + z;
+ var val = (x === "Int") ? -3 : 3;
+ v1[fn](val, 0);
+ v2[fn](val, 0);
+ t.equal(
+ v1.toString("hex"),
+ v2.toString("hex")
+ );
+ });
+ });
+ });
+ t.end();
+});

0 comments on commit d29e35b

Please sign in to comment.