if (!noAssert)
checkInt(this, value, offset, 1, 0xff, 0);
this[offset] = value;
+ return offset + 1;
};
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
writeUInt16(this, value, offset, false);
+ return offset + 2;
};
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
writeUInt16(this, value, offset, true);
+ return offset + 2;
};
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
writeUInt32(this, value, offset, false);
+ return offset + 4;
};
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
writeUInt32(this, value, offset, true);
+ return offset + 4;
};
checkInt(this, value, offset, 1, 0x7f, -0x80);
if (value < 0) value = 0xff + value + 1;
this[offset] = value;
+ return offset + 1;
};
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
if (value < 0) value = 0xffff + value + 1;
writeUInt16(this, value, offset, false);
+ return offset + 2;
};
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
if (value < 0) value = 0xffff + value + 1;
writeUInt16(this, value, offset, true);
+ return offset + 2;
};
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
if (value < 0) value = 0xffffffff + value + 1;
writeUInt32(this, value, offset, false);
+ return offset + 4;
};
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
if (value < 0) value = 0xffffffff + value + 1;
writeUInt32(this, value, offset, true);
+ return offset + 4;
};
template <typename T, enum Endianness endianness>
-void WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
+uint32_t WriteFloatGeneric(const FunctionCallbackInfo<Value>& args) {
bool doAssert = !args[2]->BooleanValue();
T val = static_cast<T>(args[0]->NumberValue());
size_t offset;
- CHECK_NOT_OOB(ParseArrayIndex(args[1], 0, &offset));
+ if (!ParseArrayIndex(args[1], 0, &offset)) {
+ ThrowRangeError("out of range index");
+ return 0;
+ }
if (doAssert) {
size_t len = Length(args.This());
- if (offset + sizeof(T) > len || offset + sizeof(T) < offset)
- return ThrowRangeError("Trying to write beyond buffer length");
+ if (offset + sizeof(T) > len || offset + sizeof(T) < offset) {
+ ThrowRangeError("Trying to write beyond buffer length");
+ return 0;
+ }
}
union NoAlias {
char* ptr = static_cast<char*>(data) + offset;
if (endianness != GetEndianness()) Swizzle(na.bytes, sizeof(na.bytes));
memcpy(ptr, na.bytes, sizeof(na.bytes));
+ return offset + sizeof(na.bytes);
}
void WriteFloatLE(const FunctionCallbackInfo<Value>& args) {
- WriteFloatGeneric<float, kLittleEndian>(args);
+ args.GetReturnValue().Set(WriteFloatGeneric<float, kLittleEndian>(args));
}
void WriteFloatBE(const FunctionCallbackInfo<Value>& args) {
- WriteFloatGeneric<float, kBigEndian>(args);
+ args.GetReturnValue().Set(WriteFloatGeneric<float, kBigEndian>(args));
}
void WriteDoubleLE(const FunctionCallbackInfo<Value>& args) {
- WriteFloatGeneric<double, kLittleEndian>(args);
+ args.GetReturnValue().Set(WriteFloatGeneric<double, kLittleEndian>(args));
}
void WriteDoubleBE(const FunctionCallbackInfo<Value>& args) {
- WriteFloatGeneric<double, kBigEndian>(args);
+ args.GetReturnValue().Set(WriteFloatGeneric<double, kBigEndian>(args));
}
assert.equal(buf[3], 0xFF);
});
+// test offset returns are correct
+var b = new Buffer(16);
+assert.equal(4, b.writeUInt32LE(0, 0));
+assert.equal(6, b.writeUInt16LE(0, 4));
+assert.equal(7, b.writeUInt8(0, 6));
+assert.equal(8, b.writeInt8(0, 7));
+assert.equal(16, b.writeDoubleLE(0, 8));
+
// test for buffer overrun
buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
var sub = buf.slice(0, 4); // length: 4