Incorporate endianness into buffer.read* function names instead of passing in a boolean flag
This commit is contained in:
parent
a002f4f453
commit
b7c23ac3f5
@ -155,8 +155,8 @@ indexes.
|
||||
|
||||
**Modifying the new buffer slice will modify memory in the original buffer!**
|
||||
|
||||
Example: build a Buffer with the ASCII alphabet, take a slice, then modify one byte
|
||||
from the original Buffer.
|
||||
Example: build a Buffer with the ASCII alphabet, take a slice, then modify one
|
||||
byte from the original Buffer.
|
||||
|
||||
var buf1 = new Buffer(26);
|
||||
|
||||
@ -172,11 +172,12 @@ from the original Buffer.
|
||||
// abc
|
||||
// !bc
|
||||
|
||||
### buffer.readUInt8(offset, bigEndian)
|
||||
### buffer.readUInt8(offset, noAssert=false)
|
||||
|
||||
Reads an unsigned 8 bit integer from the buffer at the specified offset. If
|
||||
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
|
||||
little endian.
|
||||
Reads an unsigned 8 bit integer from the buffer at the specified offset.
|
||||
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Example:
|
||||
|
||||
@ -188,24 +189,22 @@ Example:
|
||||
buf[3] = 0x42;
|
||||
|
||||
for (ii = 0; ii < buf.length; ii++) {
|
||||
console.log(buf.readUInt8(ii, true));
|
||||
console.log(buf.readUInt8(ii, false));
|
||||
console.log(buf.readUInt8(ii);
|
||||
}
|
||||
|
||||
// 0x3
|
||||
// 0x3
|
||||
// 0x4
|
||||
// 0x4
|
||||
// 0x23
|
||||
// 0x23
|
||||
// 0x42
|
||||
// 0x42
|
||||
|
||||
### buffer.readUInt16(offset, bigEndian)
|
||||
### buffer.readUInt16LE(offset, noAssert=false)
|
||||
### buffer.readUInt16BE(offset, noAssert=false)
|
||||
|
||||
Reads an unsigned 16 bit integer from the buffer at the specified offset. If
|
||||
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
|
||||
little endian.
|
||||
Reads an unsigned 16 bit integer from the buffer at the specified offset with
|
||||
specified endian format.
|
||||
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Example:
|
||||
|
||||
@ -216,12 +215,12 @@ Example:
|
||||
buf[2] = 0x23;
|
||||
buf[3] = 0x42;
|
||||
|
||||
console.log(buf.readUInt16(0, true));
|
||||
console.log(buf.readUInt16(0, false));
|
||||
console.log(buf.readUInt16(1, true));
|
||||
console.log(buf.readUInt16(1, false));
|
||||
console.log(buf.readUInt16(2, true));
|
||||
console.log(buf.readUInt16(2, false));
|
||||
console.log(buf.readUInt16BE(0));
|
||||
console.log(buf.readUInt16LE(0));
|
||||
console.log(buf.readUInt16BE(1));
|
||||
console.log(buf.readUInt16LE(1));
|
||||
console.log(buf.readUInt16BE(2));
|
||||
console.log(buf.readUInt16LE(2));
|
||||
|
||||
// 0x0304
|
||||
// 0x0403
|
||||
@ -230,12 +229,14 @@ Example:
|
||||
// 0x2342
|
||||
// 0x4223
|
||||
|
||||
### buffer.readUInt32(offset, bigEndian)
|
||||
### buffer.readUInt32LE(offset, noAssert=false)
|
||||
### buffer.readUInt32BE(offset, noAssert=false)
|
||||
|
||||
Reads an unsigned 32 bit integer from the buffer at the specified offset. If
|
||||
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
|
||||
little endian.
|
||||
Reads an unsigned 32 bit integer from the buffer at the specified offset with
|
||||
specified endian format.
|
||||
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Example:
|
||||
|
||||
@ -246,43 +247,54 @@ Example:
|
||||
buf[2] = 0x23;
|
||||
buf[3] = 0x42;
|
||||
|
||||
console.log(buf.readUInt32(0, true));
|
||||
console.log(buf.readUInt32(0, false));
|
||||
console.log(buf.readUInt32BE(0));
|
||||
console.log(buf.readUInt32LE(0));
|
||||
|
||||
// 0x03042342
|
||||
// 0x42230403
|
||||
|
||||
### buffer.readInt8(offset, bigEndian)
|
||||
### buffer.readInt8(offset, noAssert=false)
|
||||
|
||||
Reads a signed 8 bit integer from the buffer at the specified offset. If
|
||||
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
|
||||
little endian.
|
||||
Reads a signed 8 bit integer from the buffer at the specified offset.
|
||||
|
||||
Works as `buffer.readUInt8`, except buffer contents are treated as twos
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Works as `buffer.readUInt8`, except buffer contents are treated as two's
|
||||
complement signed values.
|
||||
|
||||
### buffer.readInt16(offset, bigEndian)
|
||||
### buffer.readInt16LE(offset, noAssert=false)
|
||||
### buffer.readInt16BE(offset, noAssert=false)
|
||||
|
||||
Reads a signed 16 bit integer from the buffer at the specified offset. If
|
||||
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
|
||||
little endian.
|
||||
Reads a signed 16 bit integer from the buffer at the specified offset with
|
||||
specified endian format.
|
||||
|
||||
Works as `buffer.readUInt16`, except buffer contents are treated as twos
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Works as `buffer.readUInt16*`, except buffer contents are treated as two's
|
||||
complement signed values.
|
||||
|
||||
### buffer.readInt32(offset, bigEndian)
|
||||
### buffer.readInt32LE(offset, noAssert=false)
|
||||
### buffer.readInt32BE(offset, noAssert=false)
|
||||
|
||||
Reads a signed 32 bit integer from the buffer at the specified offset. If
|
||||
`bigEndian` is true, reads bytes in a big endian format, otherwise reads them as
|
||||
little endian.
|
||||
Reads a signed 32 bit integer from the buffer at the specified offset with
|
||||
specified endian format.
|
||||
|
||||
Works as `buffer.readUInt32`, except buffer contents are treated as twos
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Works as `buffer.readUInt32*`, except buffer contents are treated as two's
|
||||
complement signed values.
|
||||
|
||||
### buffer.readFloat(offset, bigEndian)
|
||||
### buffer.readFloatLE(offset, noAssert=false)
|
||||
### buffer.readFloatBE(offset, noAssert=false)
|
||||
|
||||
Reads a 32 bit float from the buffer at the specified offset.
|
||||
Reads a 32 bit float from the buffer at the specified offset with specified
|
||||
endian format.
|
||||
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Example:
|
||||
|
||||
@ -293,15 +305,18 @@ Example:
|
||||
buf[2] = 0x80;
|
||||
buf[3] = 0x3f;
|
||||
|
||||
console.log(buf.readFloat(0, false));
|
||||
console.log(buf.readFloatLE(0));
|
||||
|
||||
// 0x01
|
||||
|
||||
### buffer.readDouble(offset, bigEndian)
|
||||
### buffer.readDoubleLE(offset, noAssert=false)
|
||||
### buffer.readDoubleBE(offset, noAssert=false)
|
||||
|
||||
Reads a 64 bit double from the buffer at the specified offset. Endian must be
|
||||
either true or false and specifies what endian ordering to read the bytes
|
||||
from the buffer in.
|
||||
Reads a 64 bit double from the buffer at the specified offset with specified
|
||||
endian format.
|
||||
|
||||
Set `noAssert` to true to skip validation of `offset`. This means that `offset`
|
||||
may be beyond the end of the buffer.
|
||||
|
||||
Example:
|
||||
|
||||
@ -316,152 +331,175 @@ Example:
|
||||
buf[6] = 0xd5;
|
||||
buf[7] = 0x3f;
|
||||
|
||||
console.log(buf.readDouble(0, false));
|
||||
console.log(buf.readDoubleLE(0));
|
||||
|
||||
// 0.3333333333333333
|
||||
|
||||
### buffer.writeUInt8(value, offset, bigEndian)
|
||||
### buffer.writeUInt8(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 8 bit unsigned integer.
|
||||
Writes `value` to the buffer at the specified offset. Note, `value` must be a
|
||||
valid unsigned 8 bit integer.
|
||||
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
Example:
|
||||
|
||||
var buf = new Buffer(4);
|
||||
buf.writeUInt8(0x3, 0, true);
|
||||
buf.writeUInt8(0x4, 1, true);
|
||||
buf.writeUInt8(0x23, 2, true);
|
||||
buf.writeUInt8(0x42, 3, true);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
buf.writeUInt8(0x3, 0, false);
|
||||
buf.writeUInt8(0x4, 1, false);
|
||||
buf.writeUInt8(0x23, 2, false);
|
||||
buf.writeUInt8(0x42, 3, false);
|
||||
buf.writeUInt8(0x3, 0);
|
||||
buf.writeUInt8(0x4, 1);
|
||||
buf.writeUInt8(0x23, 2);
|
||||
buf.writeUInt8(0x42, 3);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
// <Buffer 03 04 23 42>
|
||||
// <Buffer 03 04 23 42>
|
||||
|
||||
### buffer.writeUInt16(value, offset, bigEndian)
|
||||
### buffer.writeUInt16LE(value, offset, noAssert=false)
|
||||
### buffer.writeUInt16BE(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 16 bit unsigned integer.
|
||||
format. Note, `value` must be a valid unsigned 16 bit integer.
|
||||
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
Example:
|
||||
|
||||
var buf = new Buffer(4);
|
||||
buf.writeUInt16(0xdead, 0, true);
|
||||
buf.writeUInt16(0xbeef, 2, true);
|
||||
buf.writeUInt16BE(0xdead, 0);
|
||||
buf.writeUInt16BE(0xbeef, 2);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
buf.writeUInt16(0xdead, 0, false);
|
||||
buf.writeUInt16(0xbeef, 2, false);
|
||||
buf.writeUInt16LE(0xdead, 0);
|
||||
buf.writeUInt16LE(0xbeef, 2);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
// <Buffer de ad be ef>
|
||||
// <Buffer ad de ef be>
|
||||
|
||||
### buffer.writeUInt32(value, offset, bigEndian)
|
||||
### buffer.writeUInt32LE(value, offset, noAssert=false)
|
||||
### buffer.writeUInt32BE(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 32 bit unsigned integer.
|
||||
format. Note, `value` must be a valid unsigned 32 bit integer.
|
||||
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
Example:
|
||||
|
||||
var buf = new Buffer(4);
|
||||
buf.writeUInt32(0xfeedface, 0, true);
|
||||
buf.writeUInt32BE(0xfeedface, 0);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
buf.writeUInt32(0xfeedface, 0, false);
|
||||
buf.writeUInt32LE(0xfeedface, 0);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
// <Buffer fe ed fa ce>
|
||||
// <Buffer ce fa ed fe>
|
||||
|
||||
### buffer.writeInt8(value, offset, bigEndian)
|
||||
### buffer.writeInt8(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 16 bit signed integer.
|
||||
Writes `value` to the buffer at the specified offset. Note, `value` must be a
|
||||
valid signed 8 bit integer.
|
||||
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
Works as `buffer.writeUInt8`, except value is written out as a two's complement
|
||||
signed integer into `buffer`.
|
||||
|
||||
### buffer.writeInt16(value, offset, bigEndian)
|
||||
### buffer.writeInt16LE(value, offset, noAssert=false)
|
||||
### buffer.writeInt16BE(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 16 bit unsigned integer.
|
||||
format. Note, `value` must be a valid signed 16 bit integer.
|
||||
|
||||
Works as `buffer.writeUInt16`, except value is written out as a two's complement
|
||||
signed integer into `buffer`.
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
### buffer.writeInt32(value, offset, bigEndian)
|
||||
Works as `buffer.writeUInt16*`, except value is written out as a two's
|
||||
complement signed integer into `buffer`.
|
||||
|
||||
### buffer.writeInt32LE(value, offset, noAssert=false)
|
||||
### buffer.writeInt32BE(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 32 bit signed integer.
|
||||
format. Note, `value` must be a valid signed 32 bit integer.
|
||||
|
||||
Works as `buffer.writeUInt32`, except value is written out as a two's complement
|
||||
signed integer into `buffer`.
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
### buffer.writeFloat(value, offset, bigEndian)
|
||||
Works as `buffer.writeUInt32*`, except value is written out as a two's
|
||||
complement signed integer into `buffer`.
|
||||
|
||||
### buffer.writeFloatLE(value, offset, noAssert=false)
|
||||
### buffer.writeFloatBE(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 32 bit float.
|
||||
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
Example:
|
||||
|
||||
var buf = new Buffer(4);
|
||||
buf.writeFloat(0xcafebabe, 0, true);
|
||||
buf.writeFloatBE(0xcafebabe, 0);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
buf.writeFloat(0xcafebabe, 0, false);
|
||||
buf.writeFloatLE(0xcafebabe, 0);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
// <Buffer 4f 4a fe bb>
|
||||
// <Buffer bb fe 4a 4f>
|
||||
|
||||
### buffer.writeDouble(value, offset, bigEndian)
|
||||
### buffer.writeDoubleLE(value, offset, noAssert=false)
|
||||
### buffer.writeDoubleBE(value, offset, noAssert=false)
|
||||
|
||||
Writes `value` to the buffer at the specified offset with specified endian
|
||||
format. Note, `value` must be a valid 64 bit double.
|
||||
|
||||
Set `noAssert` to true to skip validation of `value` and `offset`. This means
|
||||
that `value` may be too large for the specific function and `offset` may be
|
||||
beyond the end of the buffer leading to the values being silently dropped. This
|
||||
should not be used unless you are certain of correctness.
|
||||
|
||||
Example:
|
||||
|
||||
var buf = new Buffer(8);
|
||||
buf.writeFloat(0xdeadbeefcafebabe, 0, true);
|
||||
buf.writeDoubleBE(0xdeadbeefcafebabe, 0);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
buf.writeFloat(0xdeadbeefcafebabe, 0, false);
|
||||
buf.writeDoubleLE(0xdeadbeefcafebabe, 0);
|
||||
|
||||
console.log(buf);
|
||||
|
||||
// <Buffer 43 eb d5 b7 dd f9 5f d7>
|
||||
// <Buffer d7 5f f9 dd b7 d5 eb 43>
|
||||
|
||||
### buffer.readUInt8NoChk(value, offset, bigEndian)
|
||||
### buffer.readUInt16NoChk(value, offset, bigEndian)
|
||||
### buffer.readUInt32NoChk(value, offset, bigEndian)
|
||||
### buffer.writeUInt8NoChk(value, offset, bigEndian)
|
||||
### buffer.writeUInt16NoChk(value, offset, bigEndian)
|
||||
### buffer.writeUInt32NoChk(value, offset, bigEndian)
|
||||
|
||||
These functions all work as per the versions without the NoChk suffix. These
|
||||
functions allow you to do use the raw functionality without any kind of
|
||||
validation for correctness. This means that value may be too large for the
|
||||
specific function and offset may be beyond the end of the buffer leading to the
|
||||
values being silently dropped. These should not be used unless you are certain
|
||||
of correctness.
|
||||
|
||||
|
||||
### buffer.fill(value, offset=0, length=-1)
|
||||
|
||||
Fills the buffer with the specified value. If the offset and length are not
|
||||
@ -472,5 +510,5 @@ given it will fill the entire buffer.
|
||||
|
||||
### INSPECT_MAX_BYTES
|
||||
|
||||
How many bytes will be returned when `b.inspect()` is called. This can
|
||||
How many bytes will be returned when `buffer.inspect()` is called. This can
|
||||
be overriden by user modules.
|
||||
|
332
lib/buffer.js
332
lib/buffer.js
@ -536,28 +536,36 @@ Buffer.prototype.asciiWrite = function(string, offset) {
|
||||
return this.write(string, offset, 'ascii');
|
||||
};
|
||||
|
||||
Buffer.prototype.readUInt8NoChk = function(offset, bigEndian) {
|
||||
var buffer = this;
|
||||
return buffer[offset];
|
||||
};
|
||||
|
||||
Buffer.prototype.readUInt8 = function(offset, bigEndian) {
|
||||
Buffer.prototype.readUInt8 = function(offset, noAssert) {
|
||||
var buffer = this;
|
||||
|
||||
if (!noAssert) {
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
return buffer.readUInt8NoChk(offset, bigEndian);
|
||||
return buffer[offset];
|
||||
};
|
||||
|
||||
Buffer.prototype.readUInt16NoChk = function(offset, bigEndian) {
|
||||
function readUInt16(buffer, offset, isBigEndian, noAssert) {
|
||||
var val = 0;
|
||||
var buffer = this;
|
||||
|
||||
if (bigEndian) {
|
||||
|
||||
if (!noAssert) {
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset + 1 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
if (isBigEndian) {
|
||||
val = buffer[offset] << 8;
|
||||
val |= buffer[offset + 1];
|
||||
} else {
|
||||
@ -566,28 +574,31 @@ Buffer.prototype.readUInt16NoChk = function(offset, bigEndian) {
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
|
||||
return readUInt16(this, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readUInt16 = function(offset, bigEndian) {
|
||||
var buffer = this;
|
||||
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
|
||||
return readUInt16(this, offset, true, noAssert);
|
||||
};
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
function readUInt32(buffer, offset, isBigEndian, noAssert) {
|
||||
var val = 0;
|
||||
|
||||
if (!noAssert) {
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset + 1 < buffer.length,
|
||||
assert.ok(offset + 3 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
return buffer.readUInt16NoChk(offset, bigEndian);
|
||||
};
|
||||
|
||||
Buffer.prototype.readUInt32NoChk = function(offset, bigEndian) {
|
||||
var val = 0;
|
||||
var buffer = this;
|
||||
|
||||
if (bigEndian) {
|
||||
if (isBigEndian) {
|
||||
val = buffer[offset + 1] << 16;
|
||||
val |= buffer[offset + 2] << 8;
|
||||
val |= buffer[offset + 3];
|
||||
@ -600,22 +611,14 @@ Buffer.prototype.readUInt32NoChk = function(offset, bigEndian) {
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
|
||||
return readUInt32(this, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readUInt32 = function(offset, bigEndian) {
|
||||
var val = 0;
|
||||
var buffer = this;
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset + 3 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
|
||||
return buffer.readUInt32NoChk(offset, bigEndian);
|
||||
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
|
||||
return readUInt32(this, offset, true, noAssert);
|
||||
};
|
||||
|
||||
|
||||
@ -664,15 +667,17 @@ Buffer.prototype.readUInt32 = function(offset, bigEndian) {
|
||||
* (0x007f + 1) * -1
|
||||
* (0x0080) * -1
|
||||
*/
|
||||
Buffer.prototype.readInt8 = function(offset, bigEndian) {
|
||||
Buffer.prototype.readInt8 = function(offset, noAssert) {
|
||||
var buffer = this;
|
||||
var neg;
|
||||
|
||||
if (!noAssert) {
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
neg = buffer[offset] & 0x80;
|
||||
if (!neg) {
|
||||
@ -682,12 +687,11 @@ Buffer.prototype.readInt8 = function(offset, bigEndian) {
|
||||
return ((0xff - buffer[offset] + 1) * -1);
|
||||
};
|
||||
|
||||
|
||||
Buffer.prototype.readInt16 = function(offset, bigEndian) {
|
||||
var buffer = this;
|
||||
function readInt16(buffer, offset, isBigEndian, noAssert) {
|
||||
var neg;
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
if (!noAssert) {
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -695,22 +699,30 @@ Buffer.prototype.readInt16 = function(offset, bigEndian) {
|
||||
|
||||
assert.ok(offset + 1 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
val = buffer.readUInt16NoChk(offset, bigEndian);
|
||||
val = readUInt16(buffer, offset, isBigEndian, noAssert);
|
||||
neg = val & 0x8000;
|
||||
if (!neg) {
|
||||
return val;
|
||||
}
|
||||
|
||||
return (0xffff - val + 1) * -1;
|
||||
}
|
||||
|
||||
Buffer.prototype.readInt16LE = function(offset, noAssert) {
|
||||
return readInt16(this, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readInt16BE = function(offset, noAssert) {
|
||||
return readInt16(this, offset, true, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readInt32 = function(offset, bigEndian) {
|
||||
var buffer = this;
|
||||
function readInt32(buffer, offset, isBigEndian, noAssert) {
|
||||
var neg;
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
if (!noAssert) {
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -718,41 +730,65 @@ Buffer.prototype.readInt32 = function(offset, bigEndian) {
|
||||
|
||||
assert.ok(offset + 3 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
val = buffer.readUInt32NoChk(offset, bigEndian);
|
||||
val = readUInt32(buffer, offset, isBigEndian, noAssert);
|
||||
neg = val & 0x80000000;
|
||||
if (!neg) {
|
||||
return (val);
|
||||
}
|
||||
|
||||
return (0xffffffff - val + 1) * -1;
|
||||
}
|
||||
|
||||
Buffer.prototype.readInt32LE = function(offset, noAssert) {
|
||||
return readInt32(this, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readInt32BE = function(offset, noAssert) {
|
||||
return readInt32(this, offset, true, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readFloat = function(offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
function readFloat(buffer, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset + 3 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
return require('buffer_ieee754').readIEEE754(buffer, offset, bigEndian,
|
||||
return require('buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
||||
23, 4);
|
||||
}
|
||||
|
||||
Buffer.prototype.readFloatLE = function(offset, noAssert) {
|
||||
return readFloat(this, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readDouble = function(offset, bigEndian) {
|
||||
var buffer = this;
|
||||
Buffer.prototype.readFloatBE = function(offset, noAssert) {
|
||||
return readFloat(this, offset, true, noAssert);
|
||||
};
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
function readDouble(buffer, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset + 7 < buffer.length,
|
||||
'Trying to read beyond buffer length');
|
||||
}
|
||||
|
||||
return require('buffer_ieee754').readIEEE754(buffer, offset, bigEndian,
|
||||
return require('buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
||||
52, 8);
|
||||
}
|
||||
|
||||
Buffer.prototype.readDoubleLE = function(offset, noAssert) {
|
||||
return readDouble(this, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.readDoubleBE = function(offset, noAssert) {
|
||||
return readDouble(this, offset, true, noAssert);
|
||||
};
|
||||
|
||||
|
||||
@ -777,20 +813,13 @@ function verifuint(value, max) {
|
||||
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
||||
}
|
||||
|
||||
Buffer.prototype.writeUInt8NoChk = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
buffer[offset] = value;
|
||||
};
|
||||
|
||||
Buffer.prototype.writeUInt8 = function(value, offset, bigEndian) {
|
||||
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
|
||||
var buffer = this;
|
||||
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
@ -798,30 +827,17 @@ Buffer.prototype.writeUInt8 = function(value, offset, bigEndian) {
|
||||
'trying to write beyond buffer length');
|
||||
|
||||
verifuint(value, 0xff);
|
||||
|
||||
buffer.writeUInt8NoChk(value, offset, bigEndian);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeUInt16NoChk = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
if (bigEndian) {
|
||||
buffer[offset] = (value & 0xff00) >>> 8;
|
||||
buffer[offset + 1] = value & 0x00ff;
|
||||
} else {
|
||||
buffer[offset + 1] = (value & 0xff00) >>> 8;
|
||||
buffer[offset] = value & 0x00ff;
|
||||
}
|
||||
|
||||
buffer[offset] = value;
|
||||
};
|
||||
|
||||
Buffer.prototype.writeUInt16 = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -831,14 +847,43 @@ Buffer.prototype.writeUInt16 = function(value, offset, bigEndian) {
|
||||
'trying to write beyond buffer length');
|
||||
|
||||
verifuint(value, 0xffff);
|
||||
}
|
||||
|
||||
buffer.writeUInt16NoChk(value, offset, bigEndian);
|
||||
if (isBigEndian) {
|
||||
buffer[offset] = (value & 0xff00) >>> 8;
|
||||
buffer[offset + 1] = value & 0x00ff;
|
||||
} else {
|
||||
buffer[offset + 1] = (value & 0xff00) >>> 8;
|
||||
buffer[offset] = value & 0x00ff;
|
||||
}
|
||||
}
|
||||
|
||||
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
|
||||
writeUInt16(this, value, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeUInt32NoChk = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
|
||||
writeUInt16(this, value, offset, true, noAssert);
|
||||
};
|
||||
|
||||
if (bigEndian) {
|
||||
function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset + 3 < buffer.length,
|
||||
'trying to write beyond buffer length');
|
||||
|
||||
verifuint(value, 0xffffffff);
|
||||
}
|
||||
|
||||
if (isBigEndian) {
|
||||
buffer[offset] = (value >>> 24) & 0xff;
|
||||
buffer[offset + 1] = (value >>> 16) & 0xff;
|
||||
buffer[offset + 2] = (value >>> 8) & 0xff;
|
||||
@ -849,26 +894,14 @@ Buffer.prototype.writeUInt32NoChk = function(value, offset, bigEndian) {
|
||||
buffer[offset + 1] = (value >>> 8) & 0xff;
|
||||
buffer[offset] = value & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
|
||||
writeUInt32(this, value, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeUInt32 = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
assert.ok(offset + 3 < buffer.length,
|
||||
'trying to write beyond buffer length');
|
||||
|
||||
verifuint(value, 0xffffffff);
|
||||
|
||||
buffer.writeUInt32NoChk(value, offset, bigEndian);
|
||||
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
|
||||
writeUInt32(this, value, offset, true, noAssert);
|
||||
};
|
||||
|
||||
|
||||
@ -923,7 +956,6 @@ function verifsint(value, max, min) {
|
||||
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
||||
}
|
||||
|
||||
|
||||
function verifIEEE754(value, max, min) {
|
||||
assert.ok(typeof (value) == 'number',
|
||||
'cannot write a non-number as a number');
|
||||
@ -933,16 +965,13 @@ function verifIEEE754(value, max, min) {
|
||||
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
||||
}
|
||||
|
||||
|
||||
Buffer.prototype.writeInt8 = function(value, offset, bigEndian) {
|
||||
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
|
||||
var buffer = this;
|
||||
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
'missing offset');
|
||||
|
||||
@ -950,22 +979,21 @@ Buffer.prototype.writeInt8 = function(value, offset, bigEndian) {
|
||||
'Trying to write beyond buffer length');
|
||||
|
||||
verifsint(value, 0x7f, -0xf0);
|
||||
}
|
||||
|
||||
if (value >= 0) {
|
||||
buffer.writeUInt8NoChk(value, offset, bigEndian);
|
||||
buffer.writeUInt8(value, offset, noAssert);
|
||||
} else {
|
||||
buffer.writeUInt8NoChk(0xff + value + 1, offset, bigEndian);
|
||||
buffer.writeUInt8(0xff + value + 1, offset, noAssert);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Buffer.prototype.writeInt16 = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
function writeInt16(buffer, value, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -975,22 +1003,29 @@ Buffer.prototype.writeInt16 = function(value, offset, bigEndian) {
|
||||
'Trying to write beyond buffer length');
|
||||
|
||||
verifsint(value, 0x7fff, -0xf000);
|
||||
}
|
||||
|
||||
if (value >= 0) {
|
||||
buffer.writeUInt16NoChk(value, offset, bigEndian);
|
||||
writeUInt16(buffer, value, offset, isBigEndian, noAssert);
|
||||
} else {
|
||||
buffer.writeUInt16NoChk(0xffff + value + 1, offset, bigEndian);
|
||||
writeUInt16(buffer, 0xffff + value + 1, offset, isBigEndian, noAssert);
|
||||
}
|
||||
}
|
||||
|
||||
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
|
||||
writeInt16(this, value, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
|
||||
writeInt16(this, value, offset, true, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeInt32 = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
function writeInt32(buffer, value, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -1000,21 +1035,29 @@ Buffer.prototype.writeInt32 = function(value, offset, bigEndian) {
|
||||
'Trying to write beyond buffer length');
|
||||
|
||||
verifsint(value, 0x7fffffff, -0xf0000000);
|
||||
if (value >= 0) {
|
||||
buffer.writeUInt32NoChk(value, offset, bigEndian);
|
||||
} else {
|
||||
buffer.writeUInt32NoChk(0xffffffff + value + 1, offset, bigEndian);
|
||||
}
|
||||
|
||||
if (value >= 0) {
|
||||
writeUInt32(buffer, value, offset, isBigEndian, noAssert);
|
||||
} else {
|
||||
writeUInt32(buffer, 0xffffffff + value + 1, offset, isBigEndian, noAssert);
|
||||
}
|
||||
}
|
||||
|
||||
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
|
||||
writeInt32(this, value, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
|
||||
writeInt32(this, value, offset, true, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeFloat = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -1024,18 +1067,26 @@ Buffer.prototype.writeFloat = function(value, offset, bigEndian) {
|
||||
'Trying to write beyond buffer length');
|
||||
|
||||
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
|
||||
require('buffer_ieee754').writeIEEE754(buffer, value, offset, bigEndian,
|
||||
}
|
||||
|
||||
require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
||||
23, 4);
|
||||
}
|
||||
|
||||
Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
|
||||
writeFloat(this, value, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
|
||||
writeFloat(this, value, offset, true, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeDouble = function(value, offset, bigEndian) {
|
||||
var buffer = this;
|
||||
|
||||
function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
|
||||
if (!noAssert) {
|
||||
assert.ok(value !== undefined && value !== null,
|
||||
'missing value');
|
||||
|
||||
assert.ok(typeof (bigEndian) === 'boolean',
|
||||
assert.ok(typeof (isBigEndian) === 'boolean',
|
||||
'missing or invalid endian');
|
||||
|
||||
assert.ok(offset !== undefined && offset !== null,
|
||||
@ -1045,7 +1096,16 @@ Buffer.prototype.writeDouble = function(value, offset, bigEndian) {
|
||||
'Trying to write beyond buffer length');
|
||||
|
||||
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
|
||||
require('buffer_ieee754').writeIEEE754(buffer, value, offset, bigEndian,
|
||||
}
|
||||
|
||||
require('buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
||||
52, 8);
|
||||
}
|
||||
|
||||
Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
|
||||
writeDouble(this, value, offset, false, noAssert);
|
||||
};
|
||||
|
||||
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
|
||||
writeDouble(this, value, offset, true, noAssert);
|
||||
};
|
||||
|
@ -30,15 +30,14 @@
|
||||
//
|
||||
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
|
||||
|
||||
exports.readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
|
||||
exports.readIEEE754 = function(buffer, offset, isBE, mLen, nBytes) {
|
||||
var e, m,
|
||||
bBE = endian,
|
||||
eLen = nBytes * 8 - mLen - 1,
|
||||
eMax = (1 << eLen) - 1,
|
||||
eBias = eMax >> 1,
|
||||
nBits = -7,
|
||||
i = bBE ? 0 : (nBytes - 1),
|
||||
d = bBE ? 1 : -1,
|
||||
i = isBE ? 0 : (nBytes - 1),
|
||||
d = isBE ? 1 : -1,
|
||||
s = buffer[offset + i];
|
||||
|
||||
i += d;
|
||||
@ -64,15 +63,14 @@ exports.readIEEE754 = function(buffer, offset, endian, mLen, nBytes) {
|
||||
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
||||
};
|
||||
|
||||
exports.writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) {
|
||||
exports.writeIEEE754 = function(buffer, value, offset, isBE, mLen, nBytes) {
|
||||
var e, m, c,
|
||||
bBE = endian,
|
||||
eLen = nBytes * 8 - mLen - 1,
|
||||
eMax = (1 << eLen) - 1,
|
||||
eBias = eMax >> 1,
|
||||
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
|
||||
i = bBE ? (nBytes-1) : 0,
|
||||
d = bBE ? -1 : 1,
|
||||
i = isBE ? (nBytes-1) : 0,
|
||||
d = isBE ? -1 : 1,
|
||||
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
||||
|
||||
value = Math.abs(value);
|
||||
|
@ -8,6 +8,7 @@ var ASSERT = require('assert');
|
||||
*/
|
||||
function test() {
|
||||
var buffer = new Buffer(8);
|
||||
|
||||
buffer[0] = 0x55;
|
||||
buffer[1] = 0x55;
|
||||
buffer[2] = 0x55;
|
||||
@ -16,8 +17,8 @@ function test() {
|
||||
buffer[5] = 0x55;
|
||||
buffer[6] = 0xd5;
|
||||
buffer[7] = 0x3f;
|
||||
ASSERT.equal(1.1945305291680097e+103, buffer.readDouble(0, true));
|
||||
ASSERT.equal(0.3333333333333333, buffer.readDouble(0, false));
|
||||
ASSERT.equal(1.1945305291680097e+103, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(0.3333333333333333, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[0] = 1;
|
||||
buffer[1] = 0;
|
||||
@ -27,18 +28,18 @@ function test() {
|
||||
buffer[5] = 0;
|
||||
buffer[6] = 0xf0;
|
||||
buffer[7] = 0x3f;
|
||||
ASSERT.equal(7.291122019655968e-304, buffer.readDouble(0, true));
|
||||
ASSERT.equal(1.0000000000000002, buffer.readDouble(0, false));
|
||||
ASSERT.equal(7.291122019655968e-304, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(1.0000000000000002, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[0] = 2;
|
||||
ASSERT.equal(4.778309726801735e-299, buffer.readDouble(0, true));
|
||||
ASSERT.equal(1.0000000000000004, buffer.readDouble(0, false));
|
||||
ASSERT.equal(4.778309726801735e-299, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(1.0000000000000004, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[0] = 1;
|
||||
buffer[6] = 0;
|
||||
buffer[7] = 0;
|
||||
ASSERT.equal(7.291122019556398e-304, buffer.readDouble(0, true));
|
||||
ASSERT.equal(5e-324, buffer.readDouble(0, false));
|
||||
ASSERT.equal(7.291122019556398e-304, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(5e-324, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[0] = 0xff;
|
||||
buffer[1] = 0xff;
|
||||
@ -48,13 +49,13 @@ function test() {
|
||||
buffer[5] = 0xff;
|
||||
buffer[6] = 0x0f;
|
||||
buffer[7] = 0x00;
|
||||
ASSERT.ok(isNaN(buffer.readDouble(0, true)));
|
||||
ASSERT.equal(2.225073858507201e-308, buffer.readDouble(0, false));
|
||||
ASSERT.ok(isNaN(buffer.readDoubleBE(0)));
|
||||
ASSERT.equal(2.225073858507201e-308, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[6] = 0xef;
|
||||
buffer[7] = 0x7f;
|
||||
ASSERT.ok(isNaN(buffer.readDouble(0, true)));
|
||||
ASSERT.equal(1.7976931348623157e+308, buffer.readDouble(0, false));
|
||||
ASSERT.ok(isNaN(buffer.readDoubleBE(0)));
|
||||
ASSERT.equal(1.7976931348623157e+308, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
@ -64,42 +65,42 @@ function test() {
|
||||
buffer[5] = 0;
|
||||
buffer[6] = 0xf0;
|
||||
buffer[7] = 0x3f;
|
||||
ASSERT.equal(3.03865e-319, buffer.readDouble(0, true));
|
||||
ASSERT.equal(1, buffer.readDouble(0, false));
|
||||
ASSERT.equal(3.03865e-319, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(1, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[6] = 0;
|
||||
buffer[7] = 0x40;
|
||||
ASSERT.equal(3.16e-322, buffer.readDouble(0, true));
|
||||
ASSERT.equal(2, buffer.readDouble(0, false));
|
||||
ASSERT.equal(3.16e-322, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(2, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[7] = 0xc0;
|
||||
ASSERT.equal(9.5e-322, buffer.readDouble(0, true));
|
||||
ASSERT.equal(-2, buffer.readDouble(0, false));
|
||||
ASSERT.equal(9.5e-322, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(-2, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[6] = 0x10;
|
||||
buffer[7] = 0;
|
||||
ASSERT.equal(2.0237e-320, buffer.readDouble(0, true));
|
||||
ASSERT.equal(2.2250738585072014e-308, buffer.readDouble(0, false));
|
||||
ASSERT.equal(2.0237e-320, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(2.2250738585072014e-308, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[6] = 0;
|
||||
ASSERT.equal(0, buffer.readDouble(0, true));
|
||||
ASSERT.equal(0, buffer.readDouble(0, false));
|
||||
ASSERT.equal(false, 1/buffer.readDouble(0, false)<0);
|
||||
ASSERT.equal(0, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(0, buffer.readDoubleLE(0));
|
||||
ASSERT.equal(false, 1/buffer.readDoubleLE(0)<0);
|
||||
|
||||
buffer[7] = 0x80;
|
||||
ASSERT.equal(6.3e-322, buffer.readDouble(0, true));
|
||||
ASSERT.equal(0, buffer.readDouble(0, false));
|
||||
ASSERT.equal(true, 1/buffer.readDouble(0, false)<0);
|
||||
ASSERT.equal(6.3e-322, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(0, buffer.readDoubleLE(0));
|
||||
ASSERT.equal(true, 1/buffer.readDoubleLE(0)<0);
|
||||
|
||||
buffer[6] = 0xf0;
|
||||
buffer[7] = 0x7f;
|
||||
ASSERT.equal(3.0418e-319, buffer.readDouble(0, true));
|
||||
ASSERT.equal(Infinity, buffer.readDouble(0, false));
|
||||
ASSERT.equal(3.0418e-319, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(Infinity, buffer.readDoubleLE(0));
|
||||
|
||||
buffer[6] = 0xf0;
|
||||
buffer[7] = 0xff;
|
||||
ASSERT.equal(3.04814e-319, buffer.readDouble(0, true));
|
||||
ASSERT.equal(-Infinity, buffer.readDouble(0, false));
|
||||
ASSERT.equal(3.04814e-319, buffer.readDoubleBE(0));
|
||||
ASSERT.equal(-Infinity, buffer.readDoubleLE(0));
|
||||
}
|
||||
|
||||
|
||||
|
@ -8,60 +8,61 @@ var ASSERT = require('assert');
|
||||
*/
|
||||
function test() {
|
||||
var buffer = new Buffer(4);
|
||||
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
buffer[2] = 0x80;
|
||||
buffer[3] = 0x3f;
|
||||
ASSERT.equal(4.600602988224807e-41, buffer.readFloat(0, true));
|
||||
ASSERT.equal(1, buffer.readFloat(0, false));
|
||||
ASSERT.equal(4.600602988224807e-41, buffer.readFloatBE(0));
|
||||
ASSERT.equal(1, buffer.readFloatLE(0));
|
||||
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
buffer[2] = 0;
|
||||
buffer[3] = 0xc0;
|
||||
ASSERT.equal(2.6904930515036488e-43, buffer.readFloat(0, true));
|
||||
ASSERT.equal(-2, buffer.readFloat(0, false));
|
||||
ASSERT.equal(2.6904930515036488e-43, buffer.readFloatBE(0));
|
||||
ASSERT.equal(-2, buffer.readFloatLE(0));
|
||||
|
||||
buffer[0] = 0xff;
|
||||
buffer[1] = 0xff;
|
||||
buffer[2] = 0x7f;
|
||||
buffer[3] = 0x7f;
|
||||
ASSERT.ok(isNaN(buffer.readFloat(0, true)));
|
||||
ASSERT.equal(3.4028234663852886e+38, buffer.readFloat(0, false));
|
||||
ASSERT.ok(isNaN(buffer.readFloatBE(0)));
|
||||
ASSERT.equal(3.4028234663852886e+38, buffer.readFloatLE(0));
|
||||
|
||||
buffer[0] = 0xab;
|
||||
buffer[1] = 0xaa;
|
||||
buffer[2] = 0xaa;
|
||||
buffer[3] = 0x3e;
|
||||
ASSERT.equal(-1.2126478207002966e-12, buffer.readFloat(0, true));
|
||||
ASSERT.equal(0.3333333432674408, buffer.readFloat(0, false));
|
||||
ASSERT.equal(-1.2126478207002966e-12, buffer.readFloatBE(0));
|
||||
ASSERT.equal(0.3333333432674408, buffer.readFloatLE(0));
|
||||
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
buffer[2] = 0;
|
||||
buffer[3] = 0;
|
||||
ASSERT.equal(0, buffer.readFloat(0, true));
|
||||
ASSERT.equal(0, buffer.readFloat(0, false));
|
||||
ASSERT.equal(false, 1/buffer.readFloat(0, false)<0);
|
||||
ASSERT.equal(0, buffer.readFloatBE(0));
|
||||
ASSERT.equal(0, buffer.readFloatLE(0));
|
||||
ASSERT.equal(false, 1/buffer.readFloatLE(0)<0);
|
||||
|
||||
buffer[3] = 0x80;
|
||||
ASSERT.equal(1.793662034335766e-43, buffer.readFloat(0, true));
|
||||
ASSERT.equal(0, buffer.readFloat(0, false));
|
||||
ASSERT.equal(true, 1/buffer.readFloat(0, false)<0);
|
||||
ASSERT.equal(1.793662034335766e-43, buffer.readFloatBE(0));
|
||||
ASSERT.equal(0, buffer.readFloatLE(0));
|
||||
ASSERT.equal(true, 1/buffer.readFloatLE(0)<0);
|
||||
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
buffer[2] = 0x80;
|
||||
buffer[3] = 0x7f;
|
||||
ASSERT.equal(4.609571298396486e-41, buffer.readFloat(0, true));
|
||||
ASSERT.equal(Infinity, buffer.readFloat(0, false));
|
||||
ASSERT.equal(4.609571298396486e-41, buffer.readFloatBE(0));
|
||||
ASSERT.equal(Infinity, buffer.readFloatLE(0));
|
||||
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
buffer[2] = 0x80;
|
||||
buffer[3] = 0xff;
|
||||
ASSERT.equal(4.627507918739843e-41, buffer.readFloat(0, true));
|
||||
ASSERT.equal(-Infinity, buffer.readFloat(0, false));
|
||||
ASSERT.equal(4.627507918739843e-41, buffer.readFloatBE(0));
|
||||
ASSERT.equal(-Infinity, buffer.readFloatLE(0));
|
||||
}
|
||||
|
||||
|
||||
|
@ -10,39 +10,34 @@ function test8() {
|
||||
var data = new Buffer(4);
|
||||
|
||||
data[0] = 0x23;
|
||||
ASSERT.equal(0x23, data.readInt8(0, true));
|
||||
ASSERT.equal(0x23, data.readInt8(0, false));
|
||||
ASSERT.equal(0x23, data.readInt8(0));
|
||||
|
||||
data[0] = 0xff;
|
||||
ASSERT.equal(-1, data.readInt8(0, true));
|
||||
ASSERT.equal(-1, data.readInt8(0, false));
|
||||
ASSERT.equal(-1, data.readInt8(0));
|
||||
|
||||
data[0] = 0x87;
|
||||
data[1] = 0xab;
|
||||
data[2] = 0x7c;
|
||||
data[3] = 0xef;
|
||||
ASSERT.equal(-121, data.readInt8(0, true));
|
||||
ASSERT.equal(-85, data.readInt8(1, true));
|
||||
ASSERT.equal(124, data.readInt8(2, true));
|
||||
ASSERT.equal(-17, data.readInt8(3, true));
|
||||
ASSERT.equal(-121, data.readInt8(0, false));
|
||||
ASSERT.equal(-85, data.readInt8(1, false));
|
||||
ASSERT.equal(124, data.readInt8(2, false));
|
||||
ASSERT.equal(-17, data.readInt8(3, false));
|
||||
ASSERT.equal(-121, data.readInt8(0));
|
||||
ASSERT.equal(-85, data.readInt8(1));
|
||||
ASSERT.equal(124, data.readInt8(2));
|
||||
ASSERT.equal(-17, data.readInt8(3));
|
||||
}
|
||||
|
||||
|
||||
function test16() {
|
||||
var buffer = new Buffer(6);
|
||||
|
||||
buffer[0] = 0x16;
|
||||
buffer[1] = 0x79;
|
||||
ASSERT.equal(0x1679, buffer.readInt16(0, true));
|
||||
ASSERT.equal(0x7916, buffer.readInt16(0, false));
|
||||
ASSERT.equal(0x1679, buffer.readInt16BE(0));
|
||||
ASSERT.equal(0x7916, buffer.readInt16LE(0));
|
||||
|
||||
buffer[0] = 0xff;
|
||||
buffer[1] = 0x80;
|
||||
ASSERT.equal(-128, buffer.readInt16(0, true));
|
||||
ASSERT.equal(-32513, buffer.readInt16(0, false));
|
||||
ASSERT.equal(-128, buffer.readInt16BE(0));
|
||||
ASSERT.equal(-32513, buffer.readInt16LE(0));
|
||||
|
||||
/* test offset with weenix */
|
||||
buffer[0] = 0x77;
|
||||
@ -51,34 +46,35 @@ function test16() {
|
||||
buffer[3] = 0x6e;
|
||||
buffer[4] = 0x69;
|
||||
buffer[5] = 0x78;
|
||||
ASSERT.equal(0x7765, buffer.readInt16(0, true));
|
||||
ASSERT.equal(0x6565, buffer.readInt16(1, true));
|
||||
ASSERT.equal(0x656e, buffer.readInt16(2, true));
|
||||
ASSERT.equal(0x6e69, buffer.readInt16(3, true));
|
||||
ASSERT.equal(0x6978, buffer.readInt16(4, true));
|
||||
ASSERT.equal(0x6577, buffer.readInt16(0, false));
|
||||
ASSERT.equal(0x6565, buffer.readInt16(1, false));
|
||||
ASSERT.equal(0x6e65, buffer.readInt16(2, false));
|
||||
ASSERT.equal(0x696e, buffer.readInt16(3, false));
|
||||
ASSERT.equal(0x7869, buffer.readInt16(4, false));
|
||||
ASSERT.equal(0x7765, buffer.readInt16BE(0));
|
||||
ASSERT.equal(0x6565, buffer.readInt16BE(1));
|
||||
ASSERT.equal(0x656e, buffer.readInt16BE(2));
|
||||
ASSERT.equal(0x6e69, buffer.readInt16BE(3));
|
||||
ASSERT.equal(0x6978, buffer.readInt16BE(4));
|
||||
ASSERT.equal(0x6577, buffer.readInt16LE(0));
|
||||
ASSERT.equal(0x6565, buffer.readInt16LE(1));
|
||||
ASSERT.equal(0x6e65, buffer.readInt16LE(2));
|
||||
ASSERT.equal(0x696e, buffer.readInt16LE(3));
|
||||
ASSERT.equal(0x7869, buffer.readInt16LE(4));
|
||||
}
|
||||
|
||||
|
||||
function test32() {
|
||||
var buffer = new Buffer(6);
|
||||
|
||||
buffer[0] = 0x43;
|
||||
buffer[1] = 0x53;
|
||||
buffer[2] = 0x16;
|
||||
buffer[3] = 0x79;
|
||||
ASSERT.equal(0x43531679, buffer.readInt32(0, true));
|
||||
ASSERT.equal(0x79165343, buffer.readInt32(0, false));
|
||||
ASSERT.equal(0x43531679, buffer.readInt32BE(0));
|
||||
ASSERT.equal(0x79165343, buffer.readInt32LE(0));
|
||||
|
||||
buffer[0] = 0xff;
|
||||
buffer[1] = 0xfe;
|
||||
buffer[2] = 0xef;
|
||||
buffer[3] = 0xfa;
|
||||
ASSERT.equal(-69638, buffer.readInt32(0, true));
|
||||
ASSERT.equal(-84934913, buffer.readInt32(0, false));
|
||||
ASSERT.equal(-69638, buffer.readInt32BE(0));
|
||||
ASSERT.equal(-84934913, buffer.readInt32LE(0));
|
||||
|
||||
buffer[0] = 0x42;
|
||||
buffer[1] = 0xc3;
|
||||
@ -86,12 +82,12 @@ function test32() {
|
||||
buffer[3] = 0xa9;
|
||||
buffer[4] = 0x36;
|
||||
buffer[5] = 0x17;
|
||||
ASSERT.equal(0x42c395a9, buffer.readInt32(0, true));
|
||||
ASSERT.equal(-1013601994, buffer.readInt32(1, true));
|
||||
ASSERT.equal(-1784072681, buffer.readInt32(2, true));
|
||||
ASSERT.equal(-1449802942, buffer.readInt32(0, false));
|
||||
ASSERT.equal(917083587, buffer.readInt32(1, false));
|
||||
ASSERT.equal(389458325, buffer.readInt32(2, false));
|
||||
ASSERT.equal(0x42c395a9, buffer.readInt32BE(0));
|
||||
ASSERT.equal(-1013601994, buffer.readInt32BE(1));
|
||||
ASSERT.equal(-1784072681, buffer.readInt32BE(2));
|
||||
ASSERT.equal(-1449802942, buffer.readInt32LE(0));
|
||||
ASSERT.equal(917083587, buffer.readInt32LE(1));
|
||||
ASSERT.equal(389458325, buffer.readInt32LE(2));
|
||||
}
|
||||
|
||||
|
||||
|
@ -13,21 +13,18 @@ var ASSERT = require('assert');
|
||||
*/
|
||||
function test8() {
|
||||
var data = new Buffer(4);
|
||||
|
||||
data[0] = 23;
|
||||
data[1] = 23;
|
||||
data[2] = 23;
|
||||
data[3] = 23;
|
||||
ASSERT.equal(23, data.readUInt8(0, true));
|
||||
ASSERT.equal(23, data.readUInt8(0, false));
|
||||
ASSERT.equal(23, data.readUInt8(1, true));
|
||||
ASSERT.equal(23, data.readUInt8(1, false));
|
||||
ASSERT.equal(23, data.readUInt8(2, true));
|
||||
ASSERT.equal(23, data.readUInt8(2, false));
|
||||
ASSERT.equal(23, data.readUInt8(3, true));
|
||||
ASSERT.equal(23, data.readUInt8(3, false));
|
||||
ASSERT.equal(23, data.readUInt8(0));
|
||||
ASSERT.equal(23, data.readUInt8(1));
|
||||
ASSERT.equal(23, data.readUInt8(2));
|
||||
ASSERT.equal(23, data.readUInt8(3));
|
||||
|
||||
data[0] = 255; /* If it became a signed int, would be -1 */
|
||||
ASSERT.equal(255, data.readUInt8(0, true));
|
||||
ASSERT.equal(255, data.readUInt8(0, false));
|
||||
ASSERT.equal(255, data.readUInt8(0));
|
||||
}
|
||||
|
||||
|
||||
@ -46,20 +43,17 @@ function test16() {
|
||||
data[1] = 0x23;
|
||||
data[2] = 0x42;
|
||||
data[3] = 0x3f;
|
||||
|
||||
ASSERT.equal(0x23, data.readUInt16(0, true));
|
||||
ASSERT.equal(0x2342, data.readUInt16(1, true));
|
||||
ASSERT.equal(0x423f, data.readUInt16(2, true));
|
||||
|
||||
ASSERT.equal(0x2300, data.readUInt16(0, false));
|
||||
ASSERT.equal(0x4223, data.readUInt16(1, false));
|
||||
ASSERT.equal(0x3f42, data.readUInt16(2, false));
|
||||
ASSERT.equal(0x23, data.readUInt16BE(0));
|
||||
ASSERT.equal(0x2342, data.readUInt16BE(1));
|
||||
ASSERT.equal(0x423f, data.readUInt16BE(2));
|
||||
ASSERT.equal(0x2300, data.readUInt16LE(0));
|
||||
ASSERT.equal(0x4223, data.readUInt16LE(1));
|
||||
ASSERT.equal(0x3f42, data.readUInt16LE(2));
|
||||
|
||||
data[0] = 0xfe;
|
||||
data[1] = 0xfe;
|
||||
|
||||
ASSERT.equal(0xfefe, data.readUInt16(0, true));
|
||||
ASSERT.equal(0xfefe, data.readUInt16(0, false));
|
||||
ASSERT.equal(0xfefe, data.readUInt16BE(0));
|
||||
ASSERT.equal(0xfefe, data.readUInt16LE(0));
|
||||
}
|
||||
|
||||
|
||||
@ -72,20 +66,19 @@ function test16() {
|
||||
*/
|
||||
function test32() {
|
||||
var data = new Buffer(8);
|
||||
|
||||
data[0] = 0x32;
|
||||
data[1] = 0x65;
|
||||
data[2] = 0x42;
|
||||
data[3] = 0x56;
|
||||
data[4] = 0x23;
|
||||
data[5] = 0xff;
|
||||
|
||||
ASSERT.equal(0x32654256, data.readUInt32(0, true));
|
||||
ASSERT.equal(0x65425623, data.readUInt32(1, true));
|
||||
ASSERT.equal(0x425623ff, data.readUInt32(2, true));
|
||||
|
||||
ASSERT.equal(0x56426532, data.readUInt32(0, false));
|
||||
ASSERT.equal(0x23564265, data.readUInt32(1, false));
|
||||
ASSERT.equal(0xff235642, data.readUInt32(2, false));
|
||||
ASSERT.equal(0x32654256, data.readUInt32BE(0));
|
||||
ASSERT.equal(0x65425623, data.readUInt32BE(1));
|
||||
ASSERT.equal(0x425623ff, data.readUInt32BE(2));
|
||||
ASSERT.equal(0x56426532, data.readUInt32LE(0));
|
||||
ASSERT.equal(0x23564265, data.readUInt32LE(1));
|
||||
ASSERT.equal(0xff235642, data.readUInt32LE(2));
|
||||
}
|
||||
|
||||
|
||||
|
@ -5,8 +5,9 @@ var ASSERT = require('assert');
|
||||
|
||||
function test() {
|
||||
var buffer = new Buffer(16);
|
||||
buffer.writeDouble(2.225073858507201e-308, 0, true);
|
||||
buffer.writeDouble(2.225073858507201e-308, 8, false);
|
||||
|
||||
buffer.writeDoubleBE(2.225073858507201e-308, 0);
|
||||
buffer.writeDoubleLE(2.225073858507201e-308, 8);
|
||||
ASSERT.equal(0x00, buffer[0]);
|
||||
ASSERT.equal(0x0f, buffer[1]);
|
||||
ASSERT.equal(0xff, buffer[2]);
|
||||
@ -24,8 +25,8 @@ function test() {
|
||||
ASSERT.equal(0x0f, buffer[14]);
|
||||
ASSERT.equal(0x00, buffer[15]);
|
||||
|
||||
buffer.writeDouble(1.0000000000000004, 0, true);
|
||||
buffer.writeDouble(1.0000000000000004, 8, false);
|
||||
buffer.writeDoubleBE(1.0000000000000004, 0);
|
||||
buffer.writeDoubleLE(1.0000000000000004, 8);
|
||||
ASSERT.equal(0x3f, buffer[0]);
|
||||
ASSERT.equal(0xf0, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
@ -43,8 +44,8 @@ function test() {
|
||||
ASSERT.equal(0xf0, buffer[14]);
|
||||
ASSERT.equal(0x3f, buffer[15]);
|
||||
|
||||
buffer.writeDouble(-2, 0, true);
|
||||
buffer.writeDouble(-2, 8, false);
|
||||
buffer.writeDoubleBE(-2, 0);
|
||||
buffer.writeDoubleLE(-2, 8);
|
||||
ASSERT.equal(0xc0, buffer[0]);
|
||||
ASSERT.equal(0x00, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
@ -62,8 +63,8 @@ function test() {
|
||||
ASSERT.equal(0x00, buffer[14]);
|
||||
ASSERT.equal(0xc0, buffer[15]);
|
||||
|
||||
buffer.writeDouble(1.7976931348623157e+308, 0, true);
|
||||
buffer.writeDouble(1.7976931348623157e+308, 8, false);
|
||||
buffer.writeDoubleBE(1.7976931348623157e+308, 0);
|
||||
buffer.writeDoubleLE(1.7976931348623157e+308, 8);
|
||||
ASSERT.equal(0x7f, buffer[0]);
|
||||
ASSERT.equal(0xef, buffer[1]);
|
||||
ASSERT.equal(0xff, buffer[2]);
|
||||
@ -81,8 +82,8 @@ function test() {
|
||||
ASSERT.equal(0xef, buffer[14]);
|
||||
ASSERT.equal(0x7f, buffer[15]);
|
||||
|
||||
buffer.writeDouble(0*-1, 0, true);
|
||||
buffer.writeDouble(0*-1, 8, false);
|
||||
buffer.writeDoubleBE(0*-1, 0);
|
||||
buffer.writeDoubleLE(0*-1, 8);
|
||||
ASSERT.equal(0x80, buffer[0]);
|
||||
ASSERT.equal(0x00, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
|
@ -5,8 +5,9 @@ var ASSERT = require('assert');
|
||||
|
||||
function test() {
|
||||
var buffer = new Buffer(8);
|
||||
buffer.writeFloat(1, 0, true);
|
||||
buffer.writeFloat(1, 4, false);
|
||||
|
||||
buffer.writeFloatBE(1, 0);
|
||||
buffer.writeFloatLE(1, 4);
|
||||
ASSERT.equal(0x3f, buffer[0]);
|
||||
ASSERT.equal(0x80, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
@ -16,8 +17,8 @@ function test() {
|
||||
ASSERT.equal(0x80, buffer[6]);
|
||||
ASSERT.equal(0x3f, buffer[7]);
|
||||
|
||||
buffer.writeFloat(1.793662034335766e-43, 0, true);
|
||||
buffer.writeFloat(1.793662034335766e-43, 4, false);
|
||||
buffer.writeFloatBE(1.793662034335766e-43, 0);
|
||||
buffer.writeFloatLE(1.793662034335766e-43, 4);
|
||||
ASSERT.equal(0x00, buffer[0]);
|
||||
ASSERT.equal(0x00, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
@ -27,8 +28,8 @@ function test() {
|
||||
ASSERT.equal(0x00, buffer[6]);
|
||||
ASSERT.equal(0x00, buffer[7]);
|
||||
|
||||
buffer.writeFloat(1/3, 0, true);
|
||||
buffer.writeFloat(1/3, 4, false);
|
||||
buffer.writeFloatBE(1/3, 0);
|
||||
buffer.writeFloatLE(1/3, 4);
|
||||
ASSERT.equal(0x3e, buffer[0]);
|
||||
ASSERT.equal(0xaa, buffer[1]);
|
||||
ASSERT.equal(0xaa, buffer[2]);
|
||||
@ -38,8 +39,8 @@ function test() {
|
||||
ASSERT.equal(0xaa, buffer[6]);
|
||||
ASSERT.equal(0x3e, buffer[7]);
|
||||
|
||||
buffer.writeFloat(3.4028234663852886e+38, 0, true);
|
||||
buffer.writeFloat(3.4028234663852886e+38, 4, false);
|
||||
buffer.writeFloatBE(3.4028234663852886e+38, 0);
|
||||
buffer.writeFloatLE(3.4028234663852886e+38, 4);
|
||||
ASSERT.equal(0x7f, buffer[0]);
|
||||
ASSERT.equal(0x7f, buffer[1]);
|
||||
ASSERT.equal(0xff, buffer[2]);
|
||||
@ -49,8 +50,8 @@ function test() {
|
||||
ASSERT.equal(0x7f, buffer[6]);
|
||||
ASSERT.equal(0x7f, buffer[7]);
|
||||
|
||||
buffer.writeFloat(0*-1, 0, true);
|
||||
buffer.writeFloat(0*-1, 4, false);
|
||||
buffer.writeFloatBE(0*-1, 0);
|
||||
buffer.writeFloatLE(0*-1, 4);
|
||||
ASSERT.equal(0x80, buffer[0]);
|
||||
ASSERT.equal(0x00, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
|
@ -4,44 +4,43 @@
|
||||
var ASSERT = require('assert');
|
||||
|
||||
function test8() {
|
||||
var buffer = new Buffer(4);
|
||||
buffer.writeInt8(0x23, 0, true);
|
||||
buffer.writeInt8(0x23, 1, false);
|
||||
buffer.writeInt8(-5, 2, true);
|
||||
buffer.writeInt8(-5, 3, false);
|
||||
var buffer = new Buffer(2);
|
||||
|
||||
buffer.writeInt8(0x23, 0);
|
||||
buffer.writeInt8(-5, 1);
|
||||
|
||||
ASSERT.equal(0x23, buffer[0]);
|
||||
ASSERT.equal(0x23, buffer[1]);
|
||||
ASSERT.equal(0xfb, buffer[2]);
|
||||
ASSERT.equal(0xfb, buffer[3]);
|
||||
ASSERT.equal(0xfb, buffer[1]);
|
||||
|
||||
/* Make sure we handle truncation correctly */
|
||||
ASSERT.throws(function() {
|
||||
buffer.writeInt8(0xabc, 0, true);
|
||||
buffer.writeInt8(0xabc, 0);
|
||||
});
|
||||
ASSERT.throws(function() {
|
||||
buffer.writeInt8(0xabc, 0, false);
|
||||
buffer.writeInt8(0xabc, 0);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function test16() {
|
||||
var buffer = new Buffer(6);
|
||||
buffer.writeInt16(0x0023, 0, true);
|
||||
buffer.writeInt16(0x0023, 2, false);
|
||||
|
||||
buffer.writeInt16BE(0x0023, 0);
|
||||
buffer.writeInt16LE(0x0023, 2);
|
||||
ASSERT.equal(0x00, buffer[0]);
|
||||
ASSERT.equal(0x23, buffer[1]);
|
||||
ASSERT.equal(0x23, buffer[2]);
|
||||
ASSERT.equal(0x00, buffer[3]);
|
||||
buffer.writeInt16(-5, 0, true);
|
||||
buffer.writeInt16(-5, 2, false);
|
||||
|
||||
buffer.writeInt16BE(-5, 0);
|
||||
buffer.writeInt16LE(-5, 2);
|
||||
ASSERT.equal(0xff, buffer[0]);
|
||||
ASSERT.equal(0xfb, buffer[1]);
|
||||
ASSERT.equal(0xfb, buffer[2]);
|
||||
ASSERT.equal(0xff, buffer[3]);
|
||||
|
||||
buffer.writeInt16(-1679, 1, true);
|
||||
buffer.writeInt16(-1679, 3, false);
|
||||
buffer.writeInt16BE(-1679, 1);
|
||||
buffer.writeInt16LE(-1679, 3);
|
||||
ASSERT.equal(0xf9, buffer[1]);
|
||||
ASSERT.equal(0x71, buffer[2]);
|
||||
ASSERT.equal(0x71, buffer[3]);
|
||||
@ -51,8 +50,9 @@ function test16() {
|
||||
|
||||
function test32() {
|
||||
var buffer = new Buffer(8);
|
||||
buffer.writeInt32(0x23, 0, true);
|
||||
buffer.writeInt32(0x23, 4, false);
|
||||
|
||||
buffer.writeInt32BE(0x23, 0);
|
||||
buffer.writeInt32LE(0x23, 4);
|
||||
ASSERT.equal(0x00, buffer[0]);
|
||||
ASSERT.equal(0x00, buffer[1]);
|
||||
ASSERT.equal(0x00, buffer[2]);
|
||||
@ -62,8 +62,8 @@ function test32() {
|
||||
ASSERT.equal(0x00, buffer[6]);
|
||||
ASSERT.equal(0x00, buffer[7]);
|
||||
|
||||
buffer.writeInt32(-5, 0, true);
|
||||
buffer.writeInt32(-5, 4, false);
|
||||
buffer.writeInt32BE(-5, 0);
|
||||
buffer.writeInt32LE(-5, 4);
|
||||
ASSERT.equal(0xff, buffer[0]);
|
||||
ASSERT.equal(0xff, buffer[1]);
|
||||
ASSERT.equal(0xff, buffer[2]);
|
||||
@ -73,8 +73,8 @@ function test32() {
|
||||
ASSERT.equal(0xff, buffer[6]);
|
||||
ASSERT.equal(0xff, buffer[7]);
|
||||
|
||||
buffer.writeInt32(-805306713, 0, true);
|
||||
buffer.writeInt32(-805306713, 4, false);
|
||||
buffer.writeInt32BE(-805306713, 0);
|
||||
buffer.writeInt32LE(-805306713, 4);
|
||||
ASSERT.equal(0xcf, buffer[0]);
|
||||
ASSERT.equal(0xff, buffer[1]);
|
||||
ASSERT.equal(0xfe, buffer[2]);
|
||||
|
@ -12,25 +12,29 @@ var ASSERT = require('assert');
|
||||
*/
|
||||
function test8() {
|
||||
var data = new Buffer(4);
|
||||
data.writeUInt8(23, 0, true);
|
||||
data.writeUInt8(23, 1, true);
|
||||
data.writeUInt8(23, 2, true);
|
||||
data.writeUInt8(23, 3, true);
|
||||
|
||||
data.writeUInt8(23, 0);
|
||||
data.writeUInt8(23, 1);
|
||||
data.writeUInt8(23, 2);
|
||||
data.writeUInt8(23, 3);
|
||||
ASSERT.equal(23, data[0]);
|
||||
ASSERT.equal(23, data[1]);
|
||||
ASSERT.equal(23, data[2]);
|
||||
ASSERT.equal(23, data[3]);
|
||||
data.writeUInt8(23, 0, false);
|
||||
data.writeUInt8(23, 1, false);
|
||||
data.writeUInt8(23, 2, false);
|
||||
data.writeUInt8(23, 3, false);
|
||||
|
||||
data.writeUInt8(23, 0);
|
||||
data.writeUInt8(23, 1);
|
||||
data.writeUInt8(23, 2);
|
||||
data.writeUInt8(23, 3);
|
||||
ASSERT.equal(23, data[0]);
|
||||
ASSERT.equal(23, data[1]);
|
||||
ASSERT.equal(23, data[2]);
|
||||
ASSERT.equal(23, data[3]);
|
||||
data.writeUInt8(255, 0, true);
|
||||
|
||||
data.writeUInt8(255, 0);
|
||||
ASSERT.equal(255, data[0]);
|
||||
data.writeUInt8(255, 0, false);
|
||||
|
||||
data.writeUInt8(255, 0);
|
||||
ASSERT.equal(255, data[0]);
|
||||
}
|
||||
|
||||
@ -38,34 +42,37 @@ function test8() {
|
||||
function test16() {
|
||||
var value = 0x2343;
|
||||
var data = new Buffer(4);
|
||||
data.writeUInt16(value, 0, true);
|
||||
|
||||
data.writeUInt16BE(value, 0);
|
||||
ASSERT.equal(0x23, data[0]);
|
||||
ASSERT.equal(0x43, data[1]);
|
||||
data.writeUInt16(value, 1, true);
|
||||
|
||||
data.writeUInt16BE(value, 1);
|
||||
ASSERT.equal(0x23, data[1]);
|
||||
ASSERT.equal(0x43, data[2]);
|
||||
data.writeUInt16(value, 2, true);
|
||||
|
||||
data.writeUInt16BE(value, 2);
|
||||
ASSERT.equal(0x23, data[2]);
|
||||
ASSERT.equal(0x43, data[3]);
|
||||
|
||||
data.writeUInt16(value, 0, false);
|
||||
data.writeUInt16LE(value, 0);
|
||||
ASSERT.equal(0x23, data[1]);
|
||||
ASSERT.equal(0x43, data[0]);
|
||||
|
||||
data.writeUInt16(value, 1, false);
|
||||
data.writeUInt16LE(value, 1);
|
||||
ASSERT.equal(0x23, data[2]);
|
||||
ASSERT.equal(0x43, data[1]);
|
||||
|
||||
data.writeUInt16(value, 2, false);
|
||||
data.writeUInt16LE(value, 2);
|
||||
ASSERT.equal(0x23, data[3]);
|
||||
ASSERT.equal(0x43, data[2]);
|
||||
|
||||
value = 0xff80;
|
||||
data.writeUInt16(value, 0, false);
|
||||
data.writeUInt16LE(value, 0);
|
||||
ASSERT.equal(0xff, data[1]);
|
||||
ASSERT.equal(0x80, data[0]);
|
||||
|
||||
data.writeUInt16(value, 0, true);
|
||||
data.writeUInt16BE(value, 0);
|
||||
ASSERT.equal(0xff, data[0]);
|
||||
ASSERT.equal(0x80, data[1]);
|
||||
}
|
||||
@ -75,37 +82,37 @@ function test32() {
|
||||
var data = new Buffer(6);
|
||||
var value = 0xe7f90a6d;
|
||||
|
||||
data.writeUInt32(value, 0, true);
|
||||
data.writeUInt32BE(value, 0);
|
||||
ASSERT.equal(0xe7, data[0]);
|
||||
ASSERT.equal(0xf9, data[1]);
|
||||
ASSERT.equal(0x0a, data[2]);
|
||||
ASSERT.equal(0x6d, data[3]);
|
||||
|
||||
data.writeUInt32(value, 1, true);
|
||||
data.writeUInt32BE(value, 1);
|
||||
ASSERT.equal(0xe7, data[1]);
|
||||
ASSERT.equal(0xf9, data[2]);
|
||||
ASSERT.equal(0x0a, data[3]);
|
||||
ASSERT.equal(0x6d, data[4]);
|
||||
|
||||
data.writeUInt32(value, 2, true);
|
||||
data.writeUInt32BE(value, 2);
|
||||
ASSERT.equal(0xe7, data[2]);
|
||||
ASSERT.equal(0xf9, data[3]);
|
||||
ASSERT.equal(0x0a, data[4]);
|
||||
ASSERT.equal(0x6d, data[5]);
|
||||
|
||||
data.writeUInt32(value, 0, false);
|
||||
data.writeUInt32LE(value, 0);
|
||||
ASSERT.equal(0xe7, data[3]);
|
||||
ASSERT.equal(0xf9, data[2]);
|
||||
ASSERT.equal(0x0a, data[1]);
|
||||
ASSERT.equal(0x6d, data[0]);
|
||||
|
||||
data.writeUInt32(value, 1, false);
|
||||
data.writeUInt32LE(value, 1);
|
||||
ASSERT.equal(0xe7, data[4]);
|
||||
ASSERT.equal(0xf9, data[3]);
|
||||
ASSERT.equal(0x0a, data[2]);
|
||||
ASSERT.equal(0x6d, data[1]);
|
||||
|
||||
data.writeUInt32(value, 2, false);
|
||||
data.writeUInt32LE(value, 2);
|
||||
ASSERT.equal(0xe7, data[5]);
|
||||
ASSERT.equal(0xf9, data[4]);
|
||||
ASSERT.equal(0x0a, data[3]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user