buffer: refactor all read/write functions

There are a lot of changes in this commit:

1) Remove the `noAssert` argument from all read and write functions.
2) Improve the performance of all read floating point functions
   significantly. This is done by switching to TypedArrays as the
   write floating point write functions.
3) No implicit type coercion for offset and byteLength anymore.
4) Adds a lot of tests.
5) Moves the read and write functions to the internal buffer file
   to split the files in smaller chunks.
6) Reworked a lot of existing tests.
7) Improve the performane of all all read write functions by using
   a faster input validation and by improving function logic.
8) Significantly improved the performance of all read int functions.
   This is done by using a implementation without a loop.
9) Improved error handling.
10) Rename test file to use the correct subsystem.

PR-URL: https://github.com/nodejs/node/pull/18395
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
Ruben Bridgewater 2018-01-30 17:34:25 +01:00
parent a6c490cc8e
commit e8bb1f35df
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
21 changed files with 2015 additions and 1839 deletions

View File

@ -1538,31 +1538,32 @@ deprecated: v8.0.0
The `buf.parent` property is a deprecated alias for `buf.buffer`.
### buf.readDoubleBE(offset[, noAssert])
### buf.readDoubleLE(offset[, noAssert])
### buf.readDoubleBE(offset)
### buf.readDoubleLE(offset)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {number}
Reads a 64-bit double from `buf` at the specified `offset` with specified
endian format (`readDoubleBE()` returns big endian, `readDoubleLE()` returns
little endian).
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
const buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]);
console.log(buf.readDoubleBE());
console.log(buf.readDoubleBE(0));
// Prints: 8.20788039913184e-304
console.log(buf.readDoubleLE());
console.log(buf.readDoubleLE(0));
// Prints: 5.447603722011605e-270
console.log(buf.readDoubleLE(1));
// Throws an exception: RangeError: Index out of range
@ -1571,31 +1572,32 @@ console.log(buf.readDoubleLE(1, true));
// This will result in a segmentation fault! Don't do this!
```
### buf.readFloatBE(offset[, noAssert])
### buf.readFloatLE(offset[, noAssert])
### buf.readFloatBE(offset)
### buf.readFloatLE(offset)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 4`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {number}
Reads a 32-bit float from `buf` at the specified `offset` with specified
endian format (`readFloatBE()` returns big endian, `readFloatLE()` returns
little endian).
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
const buf = Buffer.from([1, 2, 3, 4]);
console.log(buf.readFloatBE());
console.log(buf.readFloatBE(0));
// Prints: 2.387939260590663e-38
console.log(buf.readFloatLE());
console.log(buf.readFloatLE(0));
// Prints: 1.539989614439558e-36
console.log(buf.readFloatLE(1));
// Throws an exception: RangeError: Index out of range
@ -1604,20 +1606,21 @@ console.log(buf.readFloatLE(1, true));
// This will result in a segmentation fault! Don't do this!
```
### buf.readInt8(offset[, noAssert])
### buf.readInt8(offset)
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 1`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {integer}
Reads a signed 8-bit integer from `buf` at the specified `offset`.
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Integers read from a `Buffer` are interpreted as two's complement signed values.
Examples:
@ -1633,23 +1636,24 @@ console.log(buf.readInt8(2));
// Throws an exception: RangeError: Index out of range
```
### buf.readInt16BE(offset[, noAssert])
### buf.readInt16LE(offset[, noAssert])
### buf.readInt16BE(offset)
### buf.readInt16LE(offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 2`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {integer}
Reads a signed 16-bit integer from `buf` at the specified `offset` with
the specified endian format (`readInt16BE()` returns big endian,
`readInt16LE()` returns little endian).
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Integers read from a `Buffer` are interpreted as two's complement signed values.
Examples:
@ -1657,31 +1661,32 @@ Examples:
```js
const buf = Buffer.from([0, 5]);
console.log(buf.readInt16BE());
console.log(buf.readInt16BE(0));
// Prints: 5
console.log(buf.readInt16LE());
console.log(buf.readInt16LE(0));
// Prints: 1280
console.log(buf.readInt16LE(1));
// Throws an exception: RangeError: Index out of range
```
### buf.readInt32BE(offset[, noAssert])
### buf.readInt32LE(offset[, noAssert])
### buf.readInt32BE(offset)
### buf.readInt32LE(offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 4`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {integer}
Reads a signed 32-bit integer from `buf` at the specified `offset` with
the specified endian format (`readInt32BE()` returns big endian,
`readInt32LE()` returns little endian).
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Integers read from a `Buffer` are interpreted as two's complement signed values.
Examples:
@ -1689,32 +1694,33 @@ Examples:
```js
const buf = Buffer.from([0, 0, 0, 5]);
console.log(buf.readInt32BE());
console.log(buf.readInt32BE(0));
// Prints: 5
console.log(buf.readInt32LE());
console.log(buf.readInt32LE(0));
// Prints: 83886080
console.log(buf.readInt32LE(1));
// Throws an exception: RangeError: Index out of range
```
### buf.readIntBE(offset, byteLength[, noAssert])
### buf.readIntLE(offset, byteLength[, noAssert])
### buf.readIntBE(offset, byteLength)
### buf.readIntLE(offset, byteLength)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset and
byteLength to uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - byteLength`.
* `byteLength` {integer} Number of bytes to read. Must satisfy: `0 < byteLength <= 6`.
* `noAssert` {boolean} Skip `offset` and `byteLength` validation? **Default:** `false`.
* Returns: {integer}
Reads `byteLength` number of bytes from `buf` at the specified `offset`
and interprets the result as a two's complement signed value. Supports up to 48
bits of accuracy.
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
@ -1730,20 +1736,21 @@ console.log(buf.readIntBE(1, 0).toString(16));
// Throws ERR_OUT_OF_RANGE:
```
### buf.readUInt8(offset[, noAssert])
### buf.readUInt8(offset)
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 1`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {integer}
Reads an unsigned 8-bit integer from `buf` at the specified `offset`.
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
@ -1757,23 +1764,24 @@ console.log(buf.readUInt8(2));
// Throws an exception: RangeError: Index out of range
```
### buf.readUInt16BE(offset[, noAssert])
### buf.readUInt16LE(offset[, noAssert])
### buf.readUInt16BE(offset)
### buf.readUInt16LE(offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 2`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {integer}
Reads an unsigned 16-bit integer from `buf` at the specified `offset` with
specified endian format (`readUInt16BE()` returns big endian, `readUInt16LE()`
returns little endian).
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
@ -1791,23 +1799,24 @@ console.log(buf.readUInt16LE(2).toString(16));
// Throws an exception: RangeError: Index out of range
```
### buf.readUInt32BE(offset[, noAssert])
### buf.readUInt32LE(offset[, noAssert])
### buf.readUInt32BE(offset)
### buf.readUInt32LE(offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 4`.
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {integer}
Reads an unsigned 32-bit integer from `buf` at the specified `offset` with
specified endian format (`readUInt32BE()` returns big endian,
`readUInt32LE()` returns little endian).
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
@ -1821,24 +1830,25 @@ console.log(buf.readUInt32LE(1).toString(16));
// Throws an exception: RangeError: Index out of range
```
### buf.readUIntBE(offset, byteLength[, noAssert])
### buf.readUIntLE(offset, byteLength[, noAssert])
### buf.readUIntBE(offset, byteLength)
### buf.readUIntLE(offset, byteLength)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset and
byteLength to uint32 anymore.
-->
* `offset` {integer} Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - byteLength`.
* `byteLength` {integer} Number of bytes to read. Must satisfy: `0 < byteLength <= 6`.
* `noAssert` {boolean} Skip `offset` and `byteLength` validation? **Default:** `false`
* Returns: {integer}
Reads `byteLength` number of bytes from `buf` at the specified `offset`
and interprets the result as an unsigned integer. Supports up to 48
bits of accuracy.
Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the resulting behavior is undefined.
Examples:
```js
@ -2149,15 +2159,19 @@ console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
// Prints: 12 bytes: ½ + ¼ = ¾
```
### buf.writeDoubleBE(value, offset[, noAssert])
### buf.writeDoubleLE(value, offset[, noAssert])
### buf.writeDoubleBE(value, offset)
### buf.writeDoubleLE(value, offset)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {number} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 8`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset` with specified endian
@ -2165,9 +2179,6 @@ format (`writeDoubleBE()` writes big endian, `writeDoubleLE()` writes little
endian). `value` *should* be a valid 64-bit double. Behavior is undefined when
`value` is anything other than a 64-bit double.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js
@ -2184,15 +2195,19 @@ console.log(buf);
// Prints: <Buffer d7 5f f9 dd b7 d5 eb 43>
```
### buf.writeFloatBE(value, offset[, noAssert])
### buf.writeFloatLE(value, offset[, noAssert])
### buf.writeFloatBE(value, offset)
### buf.writeFloatLE(value, offset)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {number} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 4`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset` with specified endian
@ -2200,9 +2215,6 @@ format (`writeFloatBE()` writes big endian, `writeFloatLE()` writes little
endian). `value` *should* be a valid 32-bit float. Behavior is undefined when
`value` is anything other than a 32-bit float.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js
@ -2219,23 +2231,24 @@ console.log(buf);
// Prints: <Buffer bb fe 4a 4f>
```
### buf.writeInt8(value, offset[, noAssert])
### buf.writeInt8(value, offset)
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 1`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset`. `value` *should* be a valid
signed 8-bit integer. Behavior is undefined when `value` is anything other than
a signed 8-bit integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
`value` is interpreted and written as a two's complement signed integer.
Examples:
@ -2250,15 +2263,19 @@ console.log(buf);
// Prints: <Buffer 02 fe>
```
### buf.writeInt16BE(value, offset[, noAssert])
### buf.writeInt16LE(value, offset[, noAssert])
### buf.writeInt16BE(value, offset)
### buf.writeInt16LE(value, offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 2`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset` with specified endian
@ -2266,9 +2283,6 @@ format (`writeInt16BE()` writes big endian, `writeInt16LE()` writes little
endian). `value` *should* be a valid signed 16-bit integer. Behavior is undefined
when `value` is anything other than a signed 16-bit integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
`value` is interpreted and written as a two's complement signed integer.
Examples:
@ -2283,15 +2297,19 @@ console.log(buf);
// Prints: <Buffer 01 02 04 03>
```
### buf.writeInt32BE(value, offset[, noAssert])
### buf.writeInt32LE(value, offset[, noAssert])
### buf.writeInt32BE(value, offset)
### buf.writeInt32LE(value, offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 4`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset` with specified endian
@ -2299,9 +2317,6 @@ format (`writeInt32BE()` writes big endian, `writeInt32LE()` writes little
endian). `value` *should* be a valid signed 32-bit integer. Behavior is undefined
when `value` is anything other than a signed 32-bit integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
`value` is interpreted and written as a two's complement signed integer.
Examples:
@ -2316,26 +2331,26 @@ console.log(buf);
// Prints: <Buffer 01 02 03 04 08 07 06 05>
```
### buf.writeIntBE(value, offset, byteLength[, noAssert])
### buf.writeIntLE(value, offset, byteLength[, noAssert])
### buf.writeIntBE(value, offset, byteLength)
### buf.writeIntLE(value, offset, byteLength)
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset and
byteLength to uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - byteLength`.
* `byteLength` {integer} Number of bytes to write. Must satisfy: `0 < byteLength <= 6`.
* `noAssert` {boolean} Skip `value`, `offset`, and `byteLength` validation?
**Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `byteLength` bytes of `value` to `buf` at the specified `offset`.
Supports up to 48 bits of accuracy. Behavior is undefined when `value` is
anything other than a signed integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js
@ -2352,23 +2367,24 @@ console.log(buf);
// Prints: <Buffer ab 90 78 56 34 12>
```
### buf.writeUInt8(value, offset[, noAssert])
### buf.writeUInt8(value, offset)
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 1`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset`. `value` *should* be a
valid unsigned 8-bit integer. Behavior is undefined when `value` is anything
other than an unsigned 8-bit integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js
@ -2383,15 +2399,19 @@ console.log(buf);
// Prints: <Buffer 03 04 23 42>
```
### buf.writeUInt16BE(value, offset[, noAssert])
### buf.writeUInt16LE(value, offset[, noAssert])
### buf.writeUInt16BE(value, offset)
### buf.writeUInt16LE(value, offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 2`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset` with specified endian
@ -2399,9 +2419,6 @@ format (`writeUInt16BE()` writes big endian, `writeUInt16LE()` writes little
endian). `value` should be a valid unsigned 16-bit integer. Behavior is
undefined when `value` is anything other than an unsigned 16-bit integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js
@ -2420,15 +2437,19 @@ console.log(buf);
// Prints: <Buffer ad de ef be>
```
### buf.writeUInt32BE(value, offset[, noAssert])
### buf.writeUInt32LE(value, offset[, noAssert])
### buf.writeUInt32BE(value, offset)
### buf.writeUInt32LE(value, offset)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset to
uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - 4`.
* `noAssert` {boolean} Skip `value` and `offset` validation? **Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
Writes `value` to `buf` at the specified `offset` with specified endian
@ -2436,9 +2457,6 @@ format (`writeUInt32BE()` writes big endian, `writeUInt32LE()` writes little
endian). `value` should be a valid unsigned 32-bit integer. Behavior is
undefined when `value` is anything other than an unsigned 32-bit integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js
@ -2455,16 +2473,20 @@ console.log(buf);
// Prints: <Buffer ce fa ed fe>
```
### buf.writeUIntBE(value, offset, byteLength[, noAssert])
### buf.writeUIntLE(value, offset, byteLength[, noAssert])
### buf.writeUIntBE(value, offset, byteLength)
### buf.writeUIntLE(value, offset, byteLength)
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed noAssert and no implicit coercion of the offset and
byteLength to uint32 anymore.
-->
* `value` {integer} Number to be written to `buf`.
* `offset` {integer} Number of bytes to skip before starting to write. Must satisfy: `0 <= offset <= buf.length - byteLength`.
* `byteLength` {integer} Number of bytes to write. Must satisfy: `0 < byteLength <= 6`.
* `noAssert` {boolean} Skip `value`, `offset`, and `byteLength` validation?
**Default:** `false`
* Returns: {integer} `offset` plus the number of bytes written.
@ -2472,9 +2494,6 @@ Writes `byteLength` bytes of `value` to `buf` at the specified `offset`.
Supports up to 48 bits of accuracy. Behavior is undefined when `value` is
anything other than an unsigned integer.
Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the resulting behavior is undefined.
Examples:
```js

View File

@ -68,6 +68,10 @@ internalBuffer.FastBuffer = FastBuffer;
Buffer.prototype = FastBuffer.prototype;
for (const [name, method] of Object.entries(internalBuffer.readWrites)) {
Buffer.prototype[name] = method;
}
const constants = Object.defineProperties({}, {
MAX_LENGTH: {
value: kMaxLength,
@ -81,12 +85,6 @@ const constants = Object.defineProperties({}, {
}
});
// Temporary buffers to convert numbers.
const float32Array = new Float32Array(1);
const uInt8Float32Array = new Uint8Array(float32Array.buffer);
const float64Array = new Float64Array(1);
const uInt8Float64Array = new Uint8Array(float64Array.buffer);
Buffer.poolSize = 8 * 1024;
var poolSize, poolOffset, allocPool;
@ -998,551 +996,6 @@ Buffer.prototype.slice = function slice(start, end) {
return new FastBuffer(this.buffer, this.byteOffset + start, newLength);
};
function checkOffset(offset, ext, length) {
if (offset + ext > length)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
}
function checkByteLength(byteLength) {
if (byteLength < 1 || byteLength > 6) {
throw new errors.RangeError('ERR_OUT_OF_RANGE',
'byteLength',
'>= 1 and <= 6');
}
}
Buffer.prototype.readUIntLE =
function readUIntLE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
checkByteLength(byteLength);
checkOffset(offset, byteLength, this.length);
}
var val = this[offset];
var mul = 1;
var i = 0;
while (++i < byteLength && (mul *= 0x100))
val += this[offset + i] * mul;
return val;
};
Buffer.prototype.readUIntBE =
function readUIntBE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
checkByteLength(byteLength);
checkOffset(offset, byteLength, this.length);
}
var val = this[offset + --byteLength];
var mul = 1;
while (byteLength > 0 && (mul *= 0x100))
val += this[offset + --byteLength] * mul;
return val;
};
Buffer.prototype.readUInt8 = function readUInt8(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 1, this.length);
return this[offset];
};
Buffer.prototype.readUInt16LE = function readUInt16LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return this[offset] | (this[offset + 1] << 8);
};
Buffer.prototype.readUInt16BE = function readUInt16BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
return (this[offset] << 8) | this[offset + 1];
};
Buffer.prototype.readUInt32LE = function readUInt32LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return ((this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16)) +
(this[offset + 3] * 0x1000000);
};
Buffer.prototype.readUInt32BE = function readUInt32BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return (this[offset] * 0x1000000) +
((this[offset + 1] << 16) |
(this[offset + 2] << 8) |
this[offset + 3]);
};
Buffer.prototype.readIntLE = function readIntLE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
checkByteLength(byteLength);
checkOffset(offset, byteLength, this.length);
}
var val = this[offset];
var mul = 1;
var i = 0;
while (++i < byteLength && (mul *= 0x100))
val += this[offset + i] * mul;
mul *= 0x80;
if (val >= mul)
val -= Math.pow(2, 8 * byteLength);
return val;
};
Buffer.prototype.readIntBE = function readIntBE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
checkByteLength(byteLength);
checkOffset(offset, byteLength, this.length);
}
var i = byteLength;
var mul = 1;
var val = this[offset + --i];
while (i > 0 && (mul *= 0x100))
val += this[offset + --i] * mul;
mul *= 0x80;
if (val >= mul)
val -= Math.pow(2, 8 * byteLength);
return val;
};
Buffer.prototype.readInt8 = function readInt8(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 1, this.length);
var val = this[offset];
return !(val & 0x80) ? val : (0xff - val + 1) * -1;
};
Buffer.prototype.readInt16LE = function readInt16LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
var val = this[offset] | (this[offset + 1] << 8);
return (val & 0x8000) ? val | 0xFFFF0000 : val;
};
Buffer.prototype.readInt16BE = function readInt16BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 2, this.length);
var val = this[offset + 1] | (this[offset] << 8);
return (val & 0x8000) ? val | 0xFFFF0000 : val;
};
Buffer.prototype.readInt32LE = function readInt32LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return (this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16) |
(this[offset + 3] << 24);
};
Buffer.prototype.readInt32BE = function readInt32BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 4, this.length);
return (this[offset] << 24) |
(this[offset + 1] << 16) |
(this[offset + 2] << 8) |
(this[offset + 3]);
};
// For the casual reader who has not at the current time memorized the
// IEEE-754 standard in full detail: floating point numbers consist of
// a fraction, an exponent and a sign bit: 23+8+1 bits for single precision
// numbers and 52+11+1 bits for double precision numbers.
//
// A zero exponent is either a positive or negative zero, if the fraction
// is zero, or a denormalized number when it is non-zero. Multiplying the
// fraction by the smallest possible denormal yields the denormalized number.
//
// An all-bits-one exponent is either a positive or negative infinity, if
// the fraction is zero, or NaN when it is non-zero. The standard allows
// both quiet and signaling NaNs but since NaN is a canonical value in
// JavaScript, we cannot (and do not) distinguish between the two.
//
// Other exponents are regular numbers and are computed by subtracting the bias
// from the exponent (127 for single precision, 1023 for double precision),
// yielding an exponent in the ranges -126-127 and -1022-1024 respectively.
//
// Of interest is that the fraction of a normal number has an extra bit of
// precision that is not stored but is reconstructed by adding one after
// multiplying the fraction with the result of 2**-bits_in_fraction.
function toDouble(x0, x1) {
const frac = x0 + 0x100000000 * (x1 & 0xFFFFF);
const expt = (x1 >>> 20) & 2047;
const sign = (x1 >>> 31) ? -1 : 1;
if (expt === 0) {
if (frac === 0) return sign * 0;
return sign * frac * 2 ** -1074;
} else if (expt === 2047) {
if (frac === 0) return sign * Infinity;
return NaN;
}
return sign * 2 ** (expt - 1023) * (1 + frac * 2 ** -52);
}
function toFloat(x) {
const frac = x & 0x7FFFFF;
const expt = (x >>> 23) & 255;
const sign = (x >>> 31) ? -1 : 1;
if (expt === 0) {
if (frac === 0) return sign * 0;
return sign * frac * 2 ** -149;
} else if (expt === 255) {
if (frac === 0) return sign * Infinity;
return NaN;
}
return sign * 2 ** (expt - 127) * (1 + frac * 2 ** -23);
}
Buffer.prototype.readDoubleBE = function(offset, noAssert) {
offset = offset >>> 0;
const x1 = this.readUInt32BE(offset + 0, noAssert);
const x0 = this.readUInt32BE(offset + 4, noAssert);
return toDouble(x0, x1);
};
Buffer.prototype.readDoubleLE = function(offset, noAssert) {
offset = offset >>> 0;
const x0 = this.readUInt32LE(offset + 0, noAssert);
const x1 = this.readUInt32LE(offset + 4, noAssert);
return toDouble(x0, x1);
};
Buffer.prototype.readFloatBE = function(offset, noAssert) {
offset = offset >>> 0;
return toFloat(this.readUInt32BE(offset, noAssert));
};
Buffer.prototype.readFloatLE = function(offset, noAssert) {
offset = offset >>> 0;
return toFloat(this.readUInt32LE(offset, noAssert));
};
function checkOOB(buffer, offset, ext) {
if (offset + ext > buffer.length)
throw new errors.RangeError('ERR_INDEX_OUT_OF_RANGE');
}
function checkInt(buffer, value, offset, ext, max, min) {
if (value > max || value < min)
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'value', value);
checkOOB(buffer, offset, ext);
}
Buffer.prototype.writeUIntLE =
function writeUIntLE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
checkInt(this, value, offset, byteLength, maxBytes, 0);
}
var mul = 1;
var i = 0;
this[offset] = value;
while (++i < byteLength && (mul *= 0x100))
this[offset + i] = (value / mul) >>> 0;
return offset + byteLength;
};
Buffer.prototype.writeUIntBE =
function writeUIntBE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
checkInt(this, value, offset, byteLength, maxBytes, 0);
}
var i = byteLength - 1;
var mul = 1;
this[offset + i] = value;
while (--i >= 0 && (mul *= 0x100))
this[offset + i] = (value / mul) >>> 0;
return offset + byteLength;
};
Buffer.prototype.writeUInt8 =
function writeUInt8(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 1, 0xff, 0);
this[offset] = value;
return offset + 1;
};
Buffer.prototype.writeUInt16LE =
function writeUInt16LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
this[offset] = value;
this[offset + 1] = (value >>> 8);
return offset + 2;
};
Buffer.prototype.writeUInt16BE =
function writeUInt16BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0xffff, 0);
this[offset] = (value >>> 8);
this[offset + 1] = value;
return offset + 2;
};
Buffer.prototype.writeUInt32LE =
function writeUInt32LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
this[offset + 3] = (value >>> 24);
this[offset + 2] = (value >>> 16);
this[offset + 1] = (value >>> 8);
this[offset] = value;
return offset + 4;
};
Buffer.prototype.writeUInt32BE =
function writeUInt32BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0xffffffff, 0);
this[offset] = (value >>> 24);
this[offset + 1] = (value >>> 16);
this[offset + 2] = (value >>> 8);
this[offset + 3] = value;
return offset + 4;
};
Buffer.prototype.writeIntLE =
function writeIntLE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
checkInt(this,
value,
offset,
byteLength,
Math.pow(2, 8 * byteLength - 1) - 1,
-Math.pow(2, 8 * byteLength - 1));
}
var i = 0;
var mul = 1;
var sub = 0;
this[offset] = value;
while (++i < byteLength && (mul *= 0x100)) {
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0)
sub = 1;
this[offset + i] = ((value / mul) >> 0) - sub;
}
return offset + byteLength;
};
Buffer.prototype.writeIntBE =
function writeIntBE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
checkInt(this,
value,
offset,
byteLength,
Math.pow(2, 8 * byteLength - 1) - 1,
-Math.pow(2, 8 * byteLength - 1));
}
var i = byteLength - 1;
var mul = 1;
var sub = 0;
this[offset + i] = value;
while (--i >= 0 && (mul *= 0x100)) {
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0)
sub = 1;
this[offset + i] = ((value / mul) >> 0) - sub;
}
return offset + byteLength;
};
Buffer.prototype.writeInt8 = function writeInt8(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 1, 0x7f, -0x80);
this[offset] = value;
return offset + 1;
};
Buffer.prototype.writeInt16LE = function writeInt16LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
this[offset] = value;
this[offset + 1] = (value >>> 8);
return offset + 2;
};
Buffer.prototype.writeInt16BE = function writeInt16BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 2, 0x7fff, -0x8000);
this[offset] = (value >>> 8);
this[offset + 1] = value;
return offset + 2;
};
Buffer.prototype.writeInt32LE = function writeInt32LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
this[offset] = value;
this[offset + 1] = (value >>> 8);
this[offset + 2] = (value >>> 16);
this[offset + 3] = (value >>> 24);
return offset + 4;
};
Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert)
checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000);
this[offset] = (value >>> 24);
this[offset + 1] = (value >>> 16);
this[offset + 2] = (value >>> 8);
this[offset + 3] = value;
return offset + 4;
};
function writeDoubleForwards(val, offset, noAssert) {
val = +val;
offset = offset >>> 0;
if (!noAssert)
checkOOB(this, offset, 8);
float64Array[0] = val;
this[offset++] = uInt8Float64Array[0];
this[offset++] = uInt8Float64Array[1];
this[offset++] = uInt8Float64Array[2];
this[offset++] = uInt8Float64Array[3];
this[offset++] = uInt8Float64Array[4];
this[offset++] = uInt8Float64Array[5];
this[offset++] = uInt8Float64Array[6];
this[offset++] = uInt8Float64Array[7];
return offset;
}
function writeDoubleBackwards(val, offset, noAssert) {
val = +val;
offset = offset >>> 0;
if (!noAssert)
checkOOB(this, offset, 8);
float64Array[0] = val;
this[offset++] = uInt8Float64Array[7];
this[offset++] = uInt8Float64Array[6];
this[offset++] = uInt8Float64Array[5];
this[offset++] = uInt8Float64Array[4];
this[offset++] = uInt8Float64Array[3];
this[offset++] = uInt8Float64Array[2];
this[offset++] = uInt8Float64Array[1];
this[offset++] = uInt8Float64Array[0];
return offset;
}
function writeFloatForwards(val, offset, noAssert) {
val = +val;
offset = offset >>> 0;
if (!noAssert)
checkOOB(this, offset, 4);
float32Array[0] = val;
this[offset++] = uInt8Float32Array[0];
this[offset++] = uInt8Float32Array[1];
this[offset++] = uInt8Float32Array[2];
this[offset++] = uInt8Float32Array[3];
return offset;
}
function writeFloatBackwards(val, offset, noAssert) {
val = +val;
offset = offset >>> 0;
if (!noAssert)
checkOOB(this, offset, 4);
float32Array[0] = val;
this[offset++] = uInt8Float32Array[3];
this[offset++] = uInt8Float32Array[2];
this[offset++] = uInt8Float32Array[1];
this[offset++] = uInt8Float32Array[0];
return offset;
}
// Check endianness.
float32Array[0] = -1;
if (uInt8Float32Array[3] === 0) { // Big endian.
Buffer.prototype.writeFloatLE = writeFloatBackwards;
Buffer.prototype.writeFloatBE = writeFloatForwards;
Buffer.prototype.writeDoubleLE = writeDoubleBackwards;
Buffer.prototype.writeDoubleBE = writeDoubleForwards;
} else { // Small endian.
Buffer.prototype.writeFloatLE = writeFloatForwards;
Buffer.prototype.writeFloatBE = writeFloatBackwards;
Buffer.prototype.writeDoubleLE = writeDoubleForwards;
Buffer.prototype.writeDoubleBE = writeDoubleBackwards;
}
function swap(b, n, m) {
const i = b[n];
b[n] = b[m];

View File

@ -1,13 +1,797 @@
'use strict';
const binding = process.binding('buffer');
const { TypeError, RangeError } = require('internal/errors');
const { setupBufferJS } = binding;
// Remove from the binding so that function is only available as exported here.
// (That is, for internal use only.)
delete binding.setupBufferJS;
// Temporary buffers to convert numbers.
const float32Array = new Float32Array(1);
const uInt8Float32Array = new Uint8Array(float32Array.buffer);
const float64Array = new Float64Array(1);
const uInt8Float64Array = new Uint8Array(float64Array.buffer);
// Check endianness.
float32Array[0] = -1;
const bigEndian = uInt8Float32Array[3] === 0;
function checkBounds(buf, offset, byteLength) {
checkNumberType(offset);
if (buf[offset] === undefined || buf[offset + byteLength] === undefined)
boundsError(offset, buf.length - (byteLength + 1));
}
function checkInt(value, min, max, buf, offset, byteLength) {
if (value > max || value < min) {
throw new RangeError('ERR_OUT_OF_RANGE',
'value', `>= ${min} and <= ${max}`, value);
}
checkBounds(buf, offset, byteLength);
}
function checkNumberType(value, type) {
if (typeof value !== 'number') {
throw new TypeError('ERR_INVALID_ARG_TYPE',
type || 'offset', 'number', value);
}
}
function boundsError(value, length, type) {
if (Math.floor(value) !== value) {
checkNumberType(value, type);
throw new RangeError('ERR_OUT_OF_RANGE',
type || 'offset', 'an integer', value);
}
if (length < 0)
throw new RangeError('ERR_BUFFER_OUT_OF_BOUNDS', null, true);
throw new RangeError('ERR_OUT_OF_RANGE',
type || 'offset',
`>= ${type ? 1 : 0} and <= ${length}`,
value);
}
// Read integers.
function readUIntLE(offset, byteLength) {
if (byteLength === 6)
return readUInt48LE(this, offset);
if (byteLength === 5)
return readUInt40LE(this, offset);
if (byteLength === 3)
return readUInt24LE(this, offset);
if (byteLength === 4)
return this.readUInt32LE(offset);
if (byteLength === 2)
return this.readUInt16LE(offset);
if (byteLength === 1)
return this.readUInt8(offset);
boundsError(byteLength, 6, 'byteLength');
}
function readUInt48LE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 6);
return first +
buf[++offset] * 2 ** 8 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 24 +
(buf[++offset] + last * 2 ** 8) * 2 ** 32;
}
function readUInt40LE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 5);
return first +
buf[++offset] * 2 ** 8 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 24 +
last * 2 ** 32;
}
function readUInt32LE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 4);
return first +
this[++offset] * 2 ** 8 +
this[++offset] * 2 ** 16 +
last * 2 ** 24;
}
function readUInt24LE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 3);
return first + buf[++offset] * 2 ** 8 + last * 2 ** 16;
}
function readUInt16LE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 2);
return first + last * 2 ** 8;
}
function readUInt8(offset) {
checkNumberType(offset);
const val = this[offset];
if (val === undefined)
boundsError(offset, this.length - 1);
return val;
}
function readUIntBE(offset, byteLength) {
if (byteLength === 6)
return readUInt48BE(this, offset);
if (byteLength === 5)
return readUInt40BE(this, offset);
if (byteLength === 3)
return readUInt24BE(this, offset);
if (byteLength === 4)
return this.readUInt32BE(offset);
if (byteLength === 2)
return this.readUInt16BE(offset);
if (byteLength === 1)
return this.readUInt8(offset);
boundsError(byteLength, 6, 'byteLength');
}
function readUInt48BE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 6);
return (first * 2 ** 8 + buf[++offset]) * 2 ** 32 +
buf[++offset] * 2 ** 24 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 8 +
last;
}
function readUInt40BE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 5);
return first * 2 ** 32 +
buf[++offset] * 2 ** 24 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 8 +
last;
}
function readUInt32BE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 4);
return first * 2 ** 24 +
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 8 +
last;
}
function readUInt24BE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 3);
return first * 2 ** 16 + buf[++offset] * 2 ** 8 + last;
}
function readUInt16BE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 2);
return first * 2 ** 8 + last;
}
function readIntLE(offset, byteLength) {
if (byteLength === 6)
return readInt48LE(this, offset);
if (byteLength === 5)
return readInt40LE(this, offset);
if (byteLength === 3)
return readInt24LE(this, offset);
if (byteLength === 4)
return this.readInt32LE(offset);
if (byteLength === 2)
return this.readInt16LE(offset);
if (byteLength === 1)
return this.readInt8(offset);
boundsError(byteLength, 6, 'byteLength');
}
function readInt48LE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 6);
const val = buf[offset + 4] + last * 2 ** 8;
return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 +
first +
buf[++offset] * 2 ** 8 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 24;
}
function readInt40LE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 5);
return (last | (last & 2 ** 7) * 0x1fffffe) * 2 ** 32 +
first +
buf[++offset] * 2 ** 8 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 24;
}
function readInt32LE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 4);
return first +
this[++offset] * 2 ** 8 +
this[++offset] * 2 ** 16 +
(last << 24); // Overflow
}
function readInt24LE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 3);
const val = first + buf[++offset] * 2 ** 8 + last * 2 ** 16;
return val | (val & 2 ** 23) * 0x1fe;
}
function readInt16LE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 2);
const val = first + last * 2 ** 8;
return val | (val & 2 ** 15) * 0x1fffe;
}
function readInt8(offset) {
checkNumberType(offset);
const val = this[offset];
if (val === undefined)
boundsError(offset, this.length - 1);
return val | (val & 2 ** 7) * 0x1fffffe;
}
function readIntBE(offset, byteLength) {
if (byteLength === 6)
return readInt48BE(this, offset);
if (byteLength === 5)
return readInt40BE(this, offset);
if (byteLength === 3)
return readInt24BE(this, offset);
if (byteLength === 4)
return this.readInt32BE(offset);
if (byteLength === 2)
return this.readInt16BE(offset);
if (byteLength === 1)
return this.readInt8(offset);
boundsError(byteLength, 6, 'byteLength');
}
function readInt48BE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 5];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 6);
const val = buf[++offset] + first * 2 ** 8;
return (val | (val & 2 ** 15) * 0x1fffe) * 2 ** 32 +
buf[++offset] * 2 ** 24 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 8 +
last;
}
function readInt40BE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 4];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 5);
return (first | (first & 2 ** 7) * 0x1fffffe) * 2 ** 32 +
buf[++offset] * 2 ** 24 +
buf[++offset] * 2 ** 16 +
buf[++offset] * 2 ** 8 +
last;
}
function readInt32BE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 4);
return (first << 24) + // Overflow
this[++offset] * 2 ** 16 +
this[++offset] * 2 ** 8 +
last;
}
function readInt24BE(buf, offset) {
checkNumberType(offset);
const first = buf[offset];
const last = buf[offset + 2];
if (first === undefined || last === undefined)
boundsError(offset, buf.length - 3);
const val = first * 2 ** 16 + buf[++offset] * 2 ** 8 + last;
return val | (val & 2 ** 23) * 0x1fe;
}
function readInt16BE(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 1];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 2);
const val = first * 2 ** 8 + last;
return val | (val & 2 ** 15) * 0x1fffe;
}
// Read floats
function readFloatBackwards(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 4);
uInt8Float32Array[3] = first;
uInt8Float32Array[2] = this[++offset];
uInt8Float32Array[1] = this[++offset];
uInt8Float32Array[0] = last;
return float32Array[0];
}
function readFloatForwards(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 3];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 4);
uInt8Float32Array[0] = first;
uInt8Float32Array[1] = this[++offset];
uInt8Float32Array[2] = this[++offset];
uInt8Float32Array[3] = last;
return float32Array[0];
}
function readDoubleBackwards(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 7];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 8);
uInt8Float64Array[7] = first;
uInt8Float64Array[6] = this[++offset];
uInt8Float64Array[5] = this[++offset];
uInt8Float64Array[4] = this[++offset];
uInt8Float64Array[3] = this[++offset];
uInt8Float64Array[2] = this[++offset];
uInt8Float64Array[1] = this[++offset];
uInt8Float64Array[0] = last;
return float64Array[0];
}
function readDoubleForwards(offset) {
checkNumberType(offset);
const first = this[offset];
const last = this[offset + 7];
if (first === undefined || last === undefined)
boundsError(offset, this.length - 8);
uInt8Float64Array[0] = first;
uInt8Float64Array[1] = this[++offset];
uInt8Float64Array[2] = this[++offset];
uInt8Float64Array[3] = this[++offset];
uInt8Float64Array[4] = this[++offset];
uInt8Float64Array[5] = this[++offset];
uInt8Float64Array[6] = this[++offset];
uInt8Float64Array[7] = last;
return float64Array[0];
}
// Write integers.
function writeUIntLE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff);
if (byteLength === 5)
return writeU_Int40LE(this, value, offset, 0, 0xffffffffff);
if (byteLength === 3)
return writeU_Int24LE(this, value, offset, 0, 0xffffff);
if (byteLength === 4)
return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
if (byteLength === 2)
return writeU_Int16LE(this, value, offset, 0, 0xffff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, 0, 0xff);
boundsError(byteLength, 6, 'byteLength');
}
function writeU_Int48LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 5);
const newVal = Math.floor(value * 2 ** -32);
buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 24);
buf[offset++] = newVal;
buf[offset++] = (newVal >>> 8);
return offset;
}
function writeU_Int40LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 4);
const newVal = value;
buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 24);
buf[offset++] = Math.floor(newVal * 2 ** -32);
return offset;
}
function writeU_Int32LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 3);
buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 24);
return offset;
}
function writeUInt32LE(value, offset) {
return writeU_Int32LE(this, value, offset, 0, 0xffffffff);
}
function writeU_Int24LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 2);
buf[offset++] = value;
buf[offset++] = (value >>> 8);
buf[offset++] = (value >>> 16);
return offset;
}
function writeU_Int16LE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 1);
buf[offset++] = value;
buf[offset++] = (value >>> 8);
return offset;
}
function writeUInt16LE(value, offset) {
return writeU_Int16LE(this, value, offset, 0, 0xffff);
}
function writeU_Int8(buf, value, offset, min, max) {
value = +value;
// `checkInt()` can not be used here because it checks two entries.
checkNumberType(offset);
if (value > max || value < min) {
throw new RangeError('ERR_OUT_OF_RANGE',
'value',
`>= ${min} and <= ${max}`, value);
}
if (buf[offset] === undefined)
boundsError(offset, buf.length - 1);
buf[offset] = value;
return offset + 1;
}
function writeUInt8(value, offset) {
return writeU_Int8(this, value, offset, 0, 0xff);
}
function writeUIntBE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48BE(this, value, offset, 0, 0xffffffffffffff);
if (byteLength === 5)
return writeU_Int40BE(this, value, offset, 0, 0xffffffffff);
if (byteLength === 3)
return writeU_Int24BE(this, value, offset, 0, 0xffffff);
if (byteLength === 4)
return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
if (byteLength === 2)
return writeU_Int16BE(this, value, offset, 0, 0xffff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, 0, 0xff);
boundsError(byteLength, 6, 'byteLength');
}
function writeU_Int48BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 5);
const newVal = Math.floor(value * 2 ** -32);
buf[offset++] = (newVal >>> 8);
buf[offset++] = newVal;
buf[offset++] = (value >>> 24);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
}
function writeU_Int40BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 4);
buf[offset++] = Math.floor(value * 2 ** -32);
buf[offset++] = (value >>> 24);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
}
function writeU_Int32BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 3);
buf[offset++] = (value >>> 24);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
}
function writeUInt32BE(value, offset) {
return writeU_Int32BE(this, value, offset, 0, 0xffffffff);
}
function writeU_Int24BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 2);
buf[offset++] = (value >>> 16);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
}
function writeU_Int16BE(buf, value, offset, min, max) {
value = +value;
checkInt(value, min, max, buf, offset, 1);
buf[offset++] = (value >>> 8);
buf[offset++] = value;
return offset;
}
function writeUInt16BE(value, offset) {
return writeU_Int16BE(this, value, offset, 0, 0xffffffff);
}
function writeIntLE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48LE(this, value, offset, -0x800000000000, 0x7fffffffffff);
if (byteLength === 5)
return writeU_Int40LE(this, value, offset, -0x8000000000, 0x7fffffffff);
if (byteLength === 3)
return writeU_Int24LE(this, value, offset, -0x800000, 0x7fffff);
if (byteLength === 4)
return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
if (byteLength === 2)
return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, -0x80, 0x7f);
boundsError(byteLength, 6, 'byteLength');
}
function writeInt32LE(value, offset) {
return writeU_Int32LE(this, value, offset, -0x80000000, 0x7fffffff);
}
function writeInt16LE(value, offset) {
return writeU_Int16LE(this, value, offset, -0x8000, 0x7fff);
}
function writeInt8(value, offset) {
return writeU_Int8(this, value, offset, -0x80, 0x7f);
}
function writeIntBE(value, offset, byteLength) {
if (byteLength === 6)
return writeU_Int48BE(this, value, offset, -0x800000000000, 0x7fffffffffff);
if (byteLength === 5)
return writeU_Int40BE(this, value, offset, -0x8000000000, 0x7fffffffff);
if (byteLength === 3)
return writeU_Int24BE(this, value, offset, -0x800000, 0x7fffff);
if (byteLength === 4)
return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
if (byteLength === 2)
return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
if (byteLength === 1)
return writeU_Int8(this, value, offset, -0x80, 0x7f);
boundsError(byteLength, 6, 'byteLength');
}
function writeInt32BE(value, offset) {
return writeU_Int32BE(this, value, offset, -0x80000000, 0x7fffffff);
}
function writeInt16BE(value, offset) {
return writeU_Int16BE(this, value, offset, -0x8000, 0x7fff);
}
// Write floats.
function writeDoubleForwards(val, offset) {
val = +val;
checkBounds(this, offset, 7);
float64Array[0] = val;
this[offset++] = uInt8Float64Array[0];
this[offset++] = uInt8Float64Array[1];
this[offset++] = uInt8Float64Array[2];
this[offset++] = uInt8Float64Array[3];
this[offset++] = uInt8Float64Array[4];
this[offset++] = uInt8Float64Array[5];
this[offset++] = uInt8Float64Array[6];
this[offset++] = uInt8Float64Array[7];
return offset;
}
function writeDoubleBackwards(val, offset) {
val = +val;
checkBounds(this, offset, 7);
float64Array[0] = val;
this[offset++] = uInt8Float64Array[7];
this[offset++] = uInt8Float64Array[6];
this[offset++] = uInt8Float64Array[5];
this[offset++] = uInt8Float64Array[4];
this[offset++] = uInt8Float64Array[3];
this[offset++] = uInt8Float64Array[2];
this[offset++] = uInt8Float64Array[1];
this[offset++] = uInt8Float64Array[0];
return offset;
}
function writeFloatForwards(val, offset) {
val = +val;
checkBounds(this, offset, 3);
float32Array[0] = val;
this[offset++] = uInt8Float32Array[0];
this[offset++] = uInt8Float32Array[1];
this[offset++] = uInt8Float32Array[2];
this[offset++] = uInt8Float32Array[3];
return offset;
}
function writeFloatBackwards(val, offset) {
val = +val;
checkBounds(this, offset, 3);
float32Array[0] = val;
this[offset++] = uInt8Float32Array[3];
this[offset++] = uInt8Float32Array[2];
this[offset++] = uInt8Float32Array[1];
this[offset++] = uInt8Float32Array[0];
return offset;
}
// FastBuffer wil be inserted here by lib/buffer.js
module.exports = {
setupBufferJS
setupBufferJS,
// Container to export all read write functions.
readWrites: {
readUIntLE,
readUInt32LE,
readUInt16LE,
readUInt8,
readUIntBE,
readUInt32BE,
readUInt16BE,
readIntLE,
readInt32LE,
readInt16LE,
readInt8,
readIntBE,
readInt32BE,
readInt16BE,
writeUIntLE,
writeUInt32LE,
writeUInt16LE,
writeUInt8,
writeUIntBE,
writeUInt32BE,
writeUInt16BE,
writeIntLE,
writeInt32LE,
writeInt16LE,
writeInt8,
writeIntBE,
writeInt32BE,
writeInt16BE,
readFloatLE: bigEndian ? readFloatBackwards : readFloatForwards,
readFloatBE: bigEndian ? readFloatForwards : readFloatBackwards,
readDoubleLE: bigEndian ? readDoubleBackwards : readDoubleForwards,
readDoubleBE: bigEndian ? readDoubleForwards : readDoubleBackwards,
writeFloatLE: bigEndian ? writeFloatBackwards : writeFloatForwards,
writeFloatBE: bigEndian ? writeFloatForwards : writeFloatBackwards,
writeDoubleLE: bigEndian ? writeDoubleBackwards : writeDoubleForwards,
writeDoubleBE: bigEndian ? writeDoubleForwards : writeDoubleBackwards
}
};

View File

@ -42,13 +42,6 @@ assert.throws(function() {
Buffer.from(new AB());
}, TypeError);
// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
const b = Buffer.allocUnsafe(1);
b.writeFloatLE(11.11, 0, true);
b.writeFloatBE(11.11, 0, true);
b.writeDoubleLE(11.11, 0, true);
b.writeDoubleBE(11.11, 0, true);
// Test the byteOffset and length arguments
{
const ab = new Uint8Array(5);

View File

@ -6,16 +6,11 @@ const assert = require('assert');
const buf = Buffer.from([0xa4, 0xfd, 0x48, 0xea, 0xcf, 0xff, 0xd9, 0x01, 0xde]);
function read(buff, funx, args, expected) {
assert.strictEqual(buff[funx](...args), expected);
common.expectsError(
() => buff[funx](-1, args[1]),
{
code: 'ERR_INDEX_OUT_OF_RANGE'
}
{ code: 'ERR_OUT_OF_RANGE' }
);
assert.strictEqual(buff[funx](...args, true), expected);
}
// testing basic functionality of readDoubleBE() and readDoubleLE()
@ -123,7 +118,7 @@ assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
(0xFFFFFFFF >> (32 - bits)));
});
// test for common read(U)IntLE/BE
// Test for common read(U)IntLE/BE
{
const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
@ -144,19 +139,3 @@ assert.throws(() => Buffer.allocUnsafe(8).readFloatLE(-1), RangeError);
assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
}
// test for byteLength parameter not between 1 and 6 (inclusive)
common.expectsError(() => { buf.readIntLE(1); }, { code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntLE(1, 'string'); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntLE(1, 0); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntLE(1, 7); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1); }, { code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1, 'string'); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1, 0); },
{ code: 'ERR_OUT_OF_RANGE' });
common.expectsError(() => { buf.readIntBE(1, 7); },
{ code: 'ERR_OUT_OF_RANGE' });

View File

@ -0,0 +1,138 @@
'use strict';
require('../common');
const assert = require('assert');
// Test (64 bit) double
const buffer = Buffer.allocUnsafe(8);
buffer[0] = 0x55;
buffer[1] = 0x55;
buffer[2] = 0x55;
buffer[3] = 0x55;
buffer[4] = 0x55;
buffer[5] = 0x55;
buffer[6] = 0xd5;
buffer[7] = 0x3f;
assert.strictEqual(buffer.readDoubleBE(0), 1.1945305291680097e+103);
assert.strictEqual(buffer.readDoubleLE(0), 0.3333333333333333);
buffer[0] = 1;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(buffer.readDoubleBE(0), 7.291122019655968e-304);
assert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000002);
buffer[0] = 2;
assert.strictEqual(buffer.readDoubleBE(0), 4.778309726801735e-299);
assert.strictEqual(buffer.readDoubleLE(0), 1.0000000000000004);
buffer[0] = 1;
buffer[6] = 0;
buffer[7] = 0;
assert.strictEqual(buffer.readDoubleBE(0), 7.291122019556398e-304);
assert.strictEqual(buffer.readDoubleLE(0), 5e-324);
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0xff;
buffer[3] = 0xff;
buffer[4] = 0xff;
buffer[5] = 0xff;
buffer[6] = 0x0f;
buffer[7] = 0x00;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(buffer.readDoubleLE(0), 2.225073858507201e-308);
buffer[6] = 0xef;
buffer[7] = 0x7f;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(buffer.readDoubleLE(0), 1.7976931348623157e+308);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(buffer.readDoubleBE(0), 3.03865e-319);
assert.strictEqual(buffer.readDoubleLE(0), 1);
buffer[6] = 0;
buffer[7] = 0x40;
assert.strictEqual(buffer.readDoubleBE(0), 3.16e-322);
assert.strictEqual(buffer.readDoubleLE(0), 2);
buffer[7] = 0xc0;
assert.strictEqual(buffer.readDoubleBE(0), 9.5e-322);
assert.strictEqual(buffer.readDoubleLE(0), -2);
buffer[6] = 0x10;
buffer[7] = 0;
assert.strictEqual(buffer.readDoubleBE(0), 2.0237e-320);
assert.strictEqual(buffer.readDoubleLE(0), 2.2250738585072014e-308);
buffer[6] = 0;
assert.strictEqual(buffer.readDoubleBE(0), 0);
assert.strictEqual(buffer.readDoubleLE(0), 0);
assert.ok(1 / buffer.readDoubleLE(0) >= 0);
buffer[7] = 0x80;
assert.strictEqual(buffer.readDoubleBE(0), 6.3e-322);
assert.strictEqual(buffer.readDoubleLE(0), -0);
assert.ok(1 / buffer.readDoubleLE(0) < 0);
buffer[6] = 0xf0;
buffer[7] = 0x7f;
assert.strictEqual(buffer.readDoubleBE(0), 3.0418e-319);
assert.strictEqual(buffer.readDoubleLE(0), Infinity);
buffer[7] = 0xff;
assert.strictEqual(buffer.readDoubleBE(0), 3.04814e-319);
assert.strictEqual(buffer.readDoubleLE(0), -Infinity);
['readDoubleLE', 'readDoubleBE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](off),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
[Infinity, -1, 1].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 0. Received ${offset}`
});
});
assert.throws(
() => Buffer.alloc(1)[fn](1),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});

View File

@ -0,0 +1,101 @@
'use strict';
require('../common');
const assert = require('assert');
// Test 32 bit float
const buffer = Buffer.alloc(4);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x3f;
assert.strictEqual(buffer.readFloatBE(0), 4.600602988224807e-41);
assert.strictEqual(buffer.readFloatLE(0), 1);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0xc0;
assert.strictEqual(buffer.readFloatBE(0), 2.6904930515036488e-43);
assert.strictEqual(buffer.readFloatLE(0), -2);
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0x7f;
buffer[3] = 0x7f;
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.strictEqual(buffer.readFloatLE(0), 3.4028234663852886e+38);
buffer[0] = 0xab;
buffer[1] = 0xaa;
buffer[2] = 0xaa;
buffer[3] = 0x3e;
assert.strictEqual(buffer.readFloatBE(0), -1.2126478207002966e-12);
assert.strictEqual(buffer.readFloatLE(0), 0.3333333432674408);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
assert.strictEqual(buffer.readFloatBE(0), 0);
assert.strictEqual(buffer.readFloatLE(0), 0);
assert.ok(1 / buffer.readFloatLE(0) >= 0);
buffer[3] = 0x80;
assert.strictEqual(buffer.readFloatBE(0), 1.793662034335766e-43);
assert.strictEqual(buffer.readFloatLE(0), -0);
assert.ok(1 / buffer.readFloatLE(0) < 0);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x7f;
assert.strictEqual(buffer.readFloatBE(0), 4.609571298396486e-41);
assert.strictEqual(buffer.readFloatLE(0), Infinity);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0xff;
assert.strictEqual(buffer.readFloatBE(0), 4.627507918739843e-41);
assert.strictEqual(buffer.readFloatLE(0), -Infinity);
['readFloatLE', 'readFloatBE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](off),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
[Infinity, -1, 1].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 0. Received ${offset}`
});
});
assert.throws(
() => Buffer.alloc(1)[fn](1),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});

View File

@ -0,0 +1,195 @@
'use strict';
require('../common');
const assert = require('assert');
// Test OOB
{
const buffer = Buffer.alloc(4);
['Int8', 'Int16BE', 'Int16LE', 'Int32BE', 'Int32LE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => buffer[`read${fn}`](o),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => buffer[`read${fn}`](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]'
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[`read${fn}`](offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
// Test 8 bit signed integers
{
const data = Buffer.alloc(4);
data[0] = 0x23;
assert.strictEqual(data.readInt8(0), 0x23);
data[0] = 0xff;
assert.strictEqual(data.readInt8(0), -1);
data[0] = 0x87;
data[1] = 0xab;
data[2] = 0x7c;
data[3] = 0xef;
assert.strictEqual(data.readInt8(0), -121);
assert.strictEqual(data.readInt8(1), -85);
assert.strictEqual(data.readInt8(2), 124);
assert.strictEqual(data.readInt8(3), -17);
}
// Test 16 bit integers
{
const buffer = Buffer.alloc(6);
buffer[0] = 0x16;
buffer[1] = 0x79;
assert.strictEqual(buffer.readInt16BE(0), 0x1679);
assert.strictEqual(buffer.readInt16LE(0), 0x7916);
buffer[0] = 0xff;
buffer[1] = 0x80;
assert.strictEqual(buffer.readInt16BE(0), -128);
assert.strictEqual(buffer.readInt16LE(0), -32513);
buffer[0] = 0x77;
buffer[1] = 0x65;
buffer[2] = 0x65;
buffer[3] = 0x6e;
buffer[4] = 0x69;
buffer[5] = 0x78;
assert.strictEqual(buffer.readInt16BE(0), 0x7765);
assert.strictEqual(buffer.readInt16BE(1), 0x6565);
assert.strictEqual(buffer.readInt16BE(2), 0x656e);
assert.strictEqual(buffer.readInt16BE(3), 0x6e69);
assert.strictEqual(buffer.readInt16BE(4), 0x6978);
assert.strictEqual(buffer.readInt16LE(0), 0x6577);
assert.strictEqual(buffer.readInt16LE(1), 0x6565);
assert.strictEqual(buffer.readInt16LE(2), 0x6e65);
assert.strictEqual(buffer.readInt16LE(3), 0x696e);
assert.strictEqual(buffer.readInt16LE(4), 0x7869);
}
// Test 32 bit integers
{
const buffer = Buffer.alloc(6);
buffer[0] = 0x43;
buffer[1] = 0x53;
buffer[2] = 0x16;
buffer[3] = 0x79;
assert.strictEqual(buffer.readInt32BE(0), 0x43531679);
assert.strictEqual(buffer.readInt32LE(0), 0x79165343);
buffer[0] = 0xff;
buffer[1] = 0xfe;
buffer[2] = 0xef;
buffer[3] = 0xfa;
assert.strictEqual(buffer.readInt32BE(0), -69638);
assert.strictEqual(buffer.readInt32LE(0), -84934913);
buffer[0] = 0x42;
buffer[1] = 0xc3;
buffer[2] = 0x95;
buffer[3] = 0xa9;
buffer[4] = 0x36;
buffer[5] = 0x17;
assert.strictEqual(buffer.readInt32BE(0), 0x42c395a9);
assert.strictEqual(buffer.readInt32BE(1), -1013601994);
assert.strictEqual(buffer.readInt32BE(2), -1784072681);
assert.strictEqual(buffer.readInt32LE(0), -1449802942);
assert.strictEqual(buffer.readInt32LE(1), 917083587);
assert.strictEqual(buffer.readInt32LE(2), 389458325);
}
// Test Int
{
const buffer = Buffer.alloc(8);
// Check byteLength.
['readIntBE', 'readIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((len) => {
assert.throws(
() => buffer[fn](0, len),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((byteLength) => {
assert.throws(
() => buffer[fn](0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "byteLength" is out of range. ' +
`It must be >= 1 and <= 6. Received ${byteLength}`
});
});
[NaN, 1.01].forEach((byteLength) => {
assert.throws(
() => buffer[fn](0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "byteLength" is out of range. ' +
`It must be an integer. Received ${byteLength}`
});
});
});
// Test 1 to 6 bytes.
for (let i = 1; i < 6; i++) {
['readIntBE', 'readIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => buffer[fn](o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => buffer[fn](offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
}

View File

@ -1,95 +0,0 @@
'use strict';
require('../common');
const assert = require('assert');
// testing buffer write functions
const outOfRange = /^RangeError\b.*\bIndex out of range$/;
function write(funx, args, result, res) {
{
const buf = Buffer.alloc(9);
assert.strictEqual(buf[funx](...args), result);
assert.deepStrictEqual(buf, res);
}
writeInvalidOffset(-1);
writeInvalidOffset(9);
{
const buf2 = Buffer.alloc(9);
assert.strictEqual(buf2[funx](...args, true), result);
assert.deepStrictEqual(buf2, res);
}
function writeInvalidOffset(offset) {
const newArgs = Array.from(args);
newArgs[1] = offset;
assert.throws(() => Buffer.alloc(9)[funx](...newArgs), outOfRange);
const buf = Buffer.alloc(9);
buf[funx](...newArgs, true);
assert.deepStrictEqual(buf, Buffer.alloc(9));
}
}
write('writeInt8', [1, 0], 1, Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 0]));
write('writeIntBE', [1, 1, 4], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeIntLE', [1, 1, 4], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeInt16LE', [1, 1], 3, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeInt16BE', [1, 1], 3, Buffer.from([0, 0, 1, 0, 0, 0, 0, 0, 0]));
write('writeInt32LE', [1, 1], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeInt32BE', [1, 1], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeUInt8', [1, 0], 1, Buffer.from([1, 0, 0, 0, 0, 0, 0, 0, 0]));
write('writeUIntLE', [1, 1, 4], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeUIntBE', [1, 1, 4], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeUInt16LE', [1, 1], 3, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeUInt16BE', [1, 1], 3, Buffer.from([0, 0, 1, 0, 0, 0, 0, 0, 0]));
write('writeUInt32LE', [1, 1], 5, Buffer.from([0, 1, 0, 0, 0, 0, 0, 0, 0]));
write('writeUInt32BE', [1, 1], 5, Buffer.from([0, 0, 0, 0, 1, 0, 0, 0, 0]));
write('writeDoubleBE', [1, 1], 9, Buffer.from([0, 63, 240, 0, 0, 0, 0, 0, 0]));
write('writeDoubleLE', [1, 1], 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 240, 63]));
write('writeFloatBE', [1, 1], 5, Buffer.from([0, 63, 128, 0, 0, 0, 0, 0, 0]));
write('writeFloatLE', [1, 1], 5, Buffer.from([0, 0, 0, 128, 63, 0, 0, 0, 0]));
function writePartial(funx, args, result, res) {
assert.throws(() => Buffer.alloc(9)[funx](...args), outOfRange);
const buf = Buffer.alloc(9);
assert.strictEqual(buf[funx](...args, true), result);
assert.deepStrictEqual(buf, res);
}
// Test partial writes (cases where the buffer isn't large enough to hold the
// entire value, but is large enough to hold parts of it).
writePartial('writeIntBE', [0x0eadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0x0e, 0xad, 0xbe]));
writePartial('writeIntLE', [0x0eadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeInt16BE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x12]));
writePartial('writeInt16LE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x34]));
writePartial('writeInt32BE', [0x0eadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0x0e, 0xad, 0xbe]));
writePartial('writeInt32LE', [0x0eadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeUIntBE', [0xdeadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe]));
writePartial('writeUIntLE', [0xdeadbeef, 6, 4], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeUInt16BE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x12]));
writePartial('writeUInt16LE', [0x1234, 8], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0x34]));
writePartial('writeUInt32BE', [0xdeadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xde, 0xad, 0xbe]));
writePartial('writeUInt32LE', [0xdeadbeef, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0xef, 0xbe, 0xad]));
writePartial('writeDoubleBE', [1, 2], 10,
Buffer.from([0, 0, 63, 240, 0, 0, 0, 0, 0]));
writePartial('writeDoubleLE', [1, 2], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 240]));
writePartial('writeFloatBE', [1, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 63, 128, 0]));
writePartial('writeFloatLE', [1, 6], 10,
Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 128]));

View File

@ -0,0 +1,119 @@
'use strict';
// Tests to verify doubles are correctly written
require('../common');
const assert = require('assert');
const buffer = Buffer.allocUnsafe(16);
buffer.writeDoubleBE(2.225073858507201e-308, 0);
buffer.writeDoubleLE(2.225073858507201e-308, 8);
assert.ok(buffer.equals(new Uint8Array([
0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00
])));
buffer.writeDoubleBE(1.0000000000000004, 0);
buffer.writeDoubleLE(1.0000000000000004, 8);
assert.ok(buffer.equals(new Uint8Array([
0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f
])));
buffer.writeDoubleBE(-2, 0);
buffer.writeDoubleLE(-2, 8);
assert.ok(buffer.equals(new Uint8Array([
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0
])));
buffer.writeDoubleBE(1.7976931348623157e+308, 0);
buffer.writeDoubleLE(1.7976931348623157e+308, 8);
assert.ok(buffer.equals(new Uint8Array([
0x7f, 0xef, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0x7f
])));
buffer.writeDoubleBE(0 * -1, 0);
buffer.writeDoubleLE(0 * -1, 8);
assert.ok(buffer.equals(new Uint8Array([
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
])));
buffer.writeDoubleBE(Infinity, 0);
buffer.writeDoubleLE(Infinity, 8);
assert.ok(buffer.equals(new Uint8Array([
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x7F
])));
assert.strictEqual(buffer.readDoubleBE(0), Infinity);
assert.strictEqual(buffer.readDoubleLE(8), Infinity);
buffer.writeDoubleBE(-Infinity, 0);
buffer.writeDoubleLE(-Infinity, 8);
assert.ok(buffer.equals(new Uint8Array([
0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xFF
])));
assert.strictEqual(buffer.readDoubleBE(0), -Infinity);
assert.strictEqual(buffer.readDoubleLE(8), -Infinity);
buffer.writeDoubleBE(NaN, 0);
buffer.writeDoubleLE(NaN, 8);
assert.ok(buffer.equals(new Uint8Array([
0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x7F
])));
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
// OOB in writeDouble{LE,BE} should throw.
{
const small = Buffer.allocUnsafe(1);
['writeDoubleLE', 'writeDoubleBE'].forEach((fn) => {
assert.throws(
() => small[fn](11.11, 0),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => small[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1, 9].forEach((offset) => {
assert.throws(
() => buffer[fn](23, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 8. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](42, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}

View File

@ -0,0 +1,102 @@
'use strict';
// Tests to verify floats are correctly written
require('../common');
const assert = require('assert');
const buffer = Buffer.allocUnsafe(8);
buffer.writeFloatBE(1, 0);
buffer.writeFloatLE(1, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x3f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f ])));
buffer.writeFloatBE(1 / 3, 0);
buffer.writeFloatLE(1 / 3, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x3e, 0xaa, 0xaa, 0xab, 0xab, 0xaa, 0xaa, 0x3e ])));
buffer.writeFloatBE(3.4028234663852886e+38, 0);
buffer.writeFloatLE(3.4028234663852886e+38, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f ])));
buffer.writeFloatLE(1.1754943508222875e-38, 0);
buffer.writeFloatBE(1.1754943508222875e-38, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x00, 0x00 ])));
buffer.writeFloatBE(0 * -1, 0);
buffer.writeFloatLE(0 * -1, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 ])));
buffer.writeFloatBE(Infinity, 0);
buffer.writeFloatLE(Infinity, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x7F ])));
assert.strictEqual(buffer.readFloatBE(0), Infinity);
assert.strictEqual(buffer.readFloatLE(4), Infinity);
buffer.writeFloatBE(-Infinity, 0);
buffer.writeFloatLE(-Infinity, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF ])));
assert.strictEqual(buffer.readFloatBE(0), -Infinity);
assert.strictEqual(buffer.readFloatLE(4), -Infinity);
buffer.writeFloatBE(NaN, 0);
buffer.writeFloatLE(NaN, 4);
assert.ok(buffer.equals(
new Uint8Array([ 0x7F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x7F ])));
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.ok(Number.isNaN(buffer.readFloatLE(4)));
// OOB in writeFloat{LE,BE} should throw.
{
const small = Buffer.allocUnsafe(1);
['writeFloatLE', 'writeFloatBE'].forEach((fn) => {
assert.throws(
() => small[fn](11.11, 0),
{
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
name: 'RangeError [ERR_BUFFER_OUT_OF_BOUNDS]',
message: 'Attempt to write outside buffer bounds'
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => small[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' }
);
});
[Infinity, -1, 5].forEach((offset) => {
assert.throws(
() => buffer[fn](23, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= 4. Received ${offset}`
}
);
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => buffer[fn](42, offset),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}

View File

@ -0,0 +1,235 @@
'use strict';
// Tests to verify signed integers are correctly written
const common = require('../common');
const assert = require('assert');
const errorOutOfBounds = common.expectsError({
code: 'ERR_OUT_OF_RANGE',
type: RangeError,
message: new RegExp('^The value of "value" is out of range\\. ' +
'It must be >= -\\d+ and <= \\d+\\. Received .+$')
}, 10);
// Test 8 bit
{
const buffer = Buffer.alloc(2);
buffer.writeInt8(0x23, 0);
buffer.writeInt8(-5, 1);
assert.ok(buffer.equals(new Uint8Array([ 0x23, 0xfb ])));
/* Make sure we handle min/max correctly */
buffer.writeInt8(0x7f, 0);
buffer.writeInt8(-0x80, 1);
assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0x80 ])));
assert.throws(() => {
buffer.writeInt8(0x7f + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt8(-0x80 - 1, 0);
}, errorOutOfBounds);
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer.writeInt8(23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((off) => {
assert.throws(
() => buffer.writeInt8(23, off),
{ code: 'ERR_OUT_OF_RANGE' });
});
}
// Test 16 bit
{
const buffer = Buffer.alloc(4);
buffer.writeInt16BE(0x0023, 0);
buffer.writeInt16LE(0x0023, 2);
assert.ok(buffer.equals(new Uint8Array([ 0x00, 0x23, 0x23, 0x00 ])));
buffer.writeInt16BE(-5, 0);
buffer.writeInt16LE(-5, 2);
assert.ok(buffer.equals(new Uint8Array([ 0xff, 0xfb, 0xfb, 0xff ])));
buffer.writeInt16BE(-1679, 0);
buffer.writeInt16LE(-1679, 2);
assert.ok(buffer.equals(new Uint8Array([ 0xf9, 0x71, 0x71, 0xf9 ])));
/* Make sure we handle min/max correctly */
buffer.writeInt16BE(0x7fff, 0);
buffer.writeInt16BE(-0x8000, 2);
assert.ok(buffer.equals(new Uint8Array([ 0x7f, 0xff, 0x80, 0x00 ])));
buffer.writeInt16LE(0x7fff, 0);
buffer.writeInt16LE(-0x8000, 2);
assert.ok(buffer.equals(new Uint8Array([ 0xff, 0x7f, 0x00, 0x80 ])));
['writeInt16BE', 'writeInt16LE'].forEach((fn) => {
assert.throws(() => {
buffer[fn](0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer[fn](-0x8000 - 1, 0);
}, errorOutOfBounds);
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_OUT_OF_RANGE' });
});
});
}
// Test 32 bit
{
const buffer = Buffer.alloc(8);
buffer.writeInt32BE(0x23, 0);
buffer.writeInt32LE(0x23, 4);
assert.ok(buffer.equals(new Uint8Array([
0x00, 0x00, 0x00, 0x23, 0x23, 0x00, 0x00, 0x00
])));
buffer.writeInt32BE(-5, 0);
buffer.writeInt32LE(-5, 4);
assert.ok(buffer.equals(new Uint8Array([
0xff, 0xff, 0xff, 0xfb, 0xfb, 0xff, 0xff, 0xff
])));
buffer.writeInt32BE(-805306713, 0);
buffer.writeInt32LE(-805306713, 4);
assert.ok(buffer.equals(new Uint8Array([
0xcf, 0xff, 0xfe, 0xa7, 0xa7, 0xfe, 0xff, 0xcf
])));
/* Make sure we handle min/max correctly */
buffer.writeInt32BE(0x7fffffff, 0);
buffer.writeInt32BE(-0x80000000, 4);
assert.ok(buffer.equals(new Uint8Array([
0x7f, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00
])));
buffer.writeInt32LE(0x7fffffff, 0);
buffer.writeInt32LE(-0x80000000, 4);
assert.ok(buffer.equals(new Uint8Array([
0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x80
])));
['writeInt32BE', 'writeInt32LE'].forEach((fn) => {
assert.throws(() => {
buffer[fn](0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer[fn](-0x80000000 - 1, 0);
}, errorOutOfBounds);
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((off) => {
assert.throws(
() => buffer[fn](23, off),
{ code: 'ERR_OUT_OF_RANGE' });
});
});
}
// Test Int
{
const data = Buffer.alloc(8);
// Check byteLength.
['writeIntBE', 'writeIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](23, 0, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((offset) => {
assert.throws(
() => data[fn](23, 0, offset),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "byteLength" is out of range. ' +
`It must be >= 1 and <= 6. Received ${offset}`
}
);
});
[NaN, 1.01].forEach((byteLength) => {
assert.throws(
() => data[fn](42, 0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "byteLength" is out of range. ' +
`It must be an integer. Received ${byteLength}`
});
});
});
// Test 1 to 6 bytes.
for (let i = 1; i < 6; i++) {
['writeIntBE', 'writeIntLE'].forEach((fn) => {
const min = -(2 ** (i * 8 - 1));
const max = 2 ** (i * 8 - 1) - 1;
[min - 1, max + 1].forEach((val) => {
assert.throws(() => {
data[fn](val, 0, i);
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "value" is out of range. ' +
`It must be >= ${min} and <= ${max}. Received ${val}`
});
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](min, o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => data[fn](min, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => data[fn](max, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
}
}

View File

@ -0,0 +1,190 @@
'use strict';
require('../common');
const assert = require('assert');
/*
* We need to check the following things:
* - We are correctly resolving big endian (doesn't mean anything for 8 bit)
* - Correctly resolving little endian (doesn't mean anything for 8 bit)
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
{ // OOB
const data = Buffer.alloc(8);
['UInt8', 'UInt16BE', 'UInt16LE', 'UInt32BE', 'UInt32LE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[`write${fn}`](23, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[NaN, Infinity, -1, 1.01].forEach((o) => {
assert.throws(
() => data[`write${fn}`](23, o),
{ code: 'ERR_OUT_OF_RANGE' });
});
});
}
{ // Test 8 bit
const data = Buffer.alloc(4);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.ok(data.equals(new Uint8Array([23, 23, 23, 23])));
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.ok(data.equals(new Uint8Array([23, 23, 23, 23])));
data.writeUInt8(255, 0);
assert.strictEqual(data[0], 255);
data.writeUInt8(255, 0);
assert.strictEqual(data[0], 255);
}
// Test 16 bit
{
let value = 0x2343;
const data = Buffer.alloc(4);
data.writeUInt16BE(value, 0);
assert.ok(data.equals(new Uint8Array([0x23, 0x43, 0, 0])));
data.writeUInt16BE(value, 1);
assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x43, 0])));
data.writeUInt16BE(value, 2);
assert.ok(data.equals(new Uint8Array([0x23, 0x23, 0x23, 0x43])));
data.writeUInt16LE(value, 0);
assert.ok(data.equals(new Uint8Array([0x43, 0x23, 0x23, 0x43])));
data.writeUInt16LE(value, 1);
assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x23, 0x43])));
data.writeUInt16LE(value, 2);
assert.ok(data.equals(new Uint8Array([0x43, 0x43, 0x43, 0x23])));
value = 0xff80;
data.writeUInt16LE(value, 0);
assert.ok(data.equals(new Uint8Array([0x80, 0xff, 0x43, 0x23])));
data.writeUInt16BE(value, 0);
assert.ok(data.equals(new Uint8Array([0xff, 0x80, 0x43, 0x23])));
}
// Test 32 bit
{
const data = Buffer.alloc(6);
const value = 0xe7f90a6d;
data.writeUInt32BE(value, 0);
assert.ok(data.equals(new Uint8Array([0xe7, 0xf9, 0x0a, 0x6d, 0, 0])));
data.writeUInt32BE(value, 1);
assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xf9, 0x0a, 0x6d, 0])));
data.writeUInt32BE(value, 2);
assert.ok(data.equals(new Uint8Array([0xe7, 0xe7, 0xe7, 0xf9, 0x0a, 0x6d])));
data.writeUInt32LE(value, 0);
assert.ok(data.equals(new Uint8Array([0x6d, 0x0a, 0xf9, 0xe7, 0x0a, 0x6d])));
data.writeUInt32LE(value, 1);
assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x0a, 0xf9, 0xe7, 0x6d])));
data.writeUInt32LE(value, 2);
assert.ok(data.equals(new Uint8Array([0x6d, 0x6d, 0x6d, 0x0a, 0xf9, 0xe7])));
}
// Test UInt
{
const data = Buffer.alloc(8);
let val = 0x100;
// Check byteLength.
['writeUIntBE', 'writeUIntLE'].forEach((fn) => {
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](23, 0, o),
{ code: 'ERR_INVALID_ARG_TYPE' });
});
[Infinity, -1].forEach((offset) => {
assert.throws(
() => data[fn](23, 0, offset),
{
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "byteLength" is out of range. ' +
`It must be >= 1 and <= 6. Received ${offset}`
}
);
});
[NaN, 1.01].forEach((byteLength) => {
assert.throws(
() => data[fn](42, 0, byteLength),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "byteLength" is out of range. ' +
`It must be an integer. Received ${byteLength}`
});
});
});
// Test 1 to 6 bytes.
for (let i = 1; i < 6; i++) {
['writeUIntBE', 'writeUIntLE'].forEach((fn) => {
assert.throws(() => {
data[fn](val, 0, i);
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "value" is out of range. ' +
`It must be >= 0 and <= ${val - 1}. Received ${val}`
});
['', '0', null, undefined, {}, [], () => {}, true, false].forEach((o) => {
assert.throws(
() => data[fn](23, o, i),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]'
});
});
[Infinity, -1, -4294967295].forEach((offset) => {
assert.throws(
() => data[fn](val - 1, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be >= 0 and <= ${8 - i}. Received ${offset}`
});
});
[NaN, 1.01].forEach((offset) => {
assert.throws(
() => data[fn](val - 1, offset, i),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "offset" is out of range. ' +
`It must be an integer. Received ${offset}`
});
});
});
val *= 0x100;
}
}

View File

@ -1,129 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're reading in doubles correctly
*/
require('../common');
const assert = require('assert');
/*
* Test (64 bit) double
*/
const buffer = Buffer.allocUnsafe(8);
buffer[0] = 0x55;
buffer[1] = 0x55;
buffer[2] = 0x55;
buffer[3] = 0x55;
buffer[4] = 0x55;
buffer[5] = 0x55;
buffer[6] = 0xd5;
buffer[7] = 0x3f;
assert.strictEqual(1.1945305291680097e+103, buffer.readDoubleBE(0));
assert.strictEqual(0.3333333333333333, buffer.readDoubleLE(0));
buffer[0] = 1;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(7.291122019655968e-304, buffer.readDoubleBE(0));
assert.strictEqual(1.0000000000000002, buffer.readDoubleLE(0));
buffer[0] = 2;
assert.strictEqual(4.778309726801735e-299, buffer.readDoubleBE(0));
assert.strictEqual(1.0000000000000004, buffer.readDoubleLE(0));
buffer[0] = 1;
buffer[6] = 0;
buffer[7] = 0;
assert.strictEqual(7.291122019556398e-304, buffer.readDoubleBE(0));
assert.strictEqual(5e-324, buffer.readDoubleLE(0));
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0xff;
buffer[3] = 0xff;
buffer[4] = 0xff;
buffer[5] = 0xff;
buffer[6] = 0x0f;
buffer[7] = 0x00;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(2.225073858507201e-308, buffer.readDoubleLE(0));
buffer[6] = 0xef;
buffer[7] = 0x7f;
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.strictEqual(1.7976931348623157e+308, buffer.readDoubleLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
buffer[6] = 0xf0;
buffer[7] = 0x3f;
assert.strictEqual(3.03865e-319, buffer.readDoubleBE(0));
assert.strictEqual(1, buffer.readDoubleLE(0));
buffer[6] = 0;
buffer[7] = 0x40;
assert.strictEqual(3.16e-322, buffer.readDoubleBE(0));
assert.strictEqual(2, buffer.readDoubleLE(0));
buffer[7] = 0xc0;
assert.strictEqual(9.5e-322, buffer.readDoubleBE(0));
assert.strictEqual(-2, buffer.readDoubleLE(0));
buffer[6] = 0x10;
buffer[7] = 0;
assert.strictEqual(2.0237e-320, buffer.readDoubleBE(0));
assert.strictEqual(2.2250738585072014e-308, buffer.readDoubleLE(0));
buffer[6] = 0;
assert.strictEqual(0, buffer.readDoubleBE(0));
assert.strictEqual(0, buffer.readDoubleLE(0));
assert.strictEqual(false, 1 / buffer.readDoubleLE(0) < 0);
buffer[7] = 0x80;
assert.strictEqual(6.3e-322, buffer.readDoubleBE(0));
assert.strictEqual(-0, buffer.readDoubleLE(0));
assert.strictEqual(true, 1 / buffer.readDoubleLE(0) < 0);
buffer[6] = 0xf0;
buffer[7] = 0x7f;
assert.strictEqual(3.0418e-319, buffer.readDoubleBE(0));
assert.strictEqual(Infinity, buffer.readDoubleLE(0));
buffer[7] = 0xff;
assert.strictEqual(3.04814e-319, buffer.readDoubleBE(0));
assert.strictEqual(-Infinity, buffer.readDoubleLE(0));
buffer.writeDoubleBE(246800);
assert.strictEqual(buffer.readDoubleBE(), 246800);
assert.strictEqual(buffer.readDoubleBE(0.7), 246800);
assert.strictEqual(buffer.readDoubleBE(NaN), 246800);

View File

@ -1,92 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're reading in floats correctly
*/
require('../common');
const assert = require('assert');
/*
* Test (32 bit) float
*/
function test(clazz) {
const buffer = new clazz(4);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x3f;
assert.strictEqual(4.600602988224807e-41, buffer.readFloatBE(0));
assert.strictEqual(1, buffer.readFloatLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0xc0;
assert.strictEqual(2.6904930515036488e-43, buffer.readFloatBE(0));
assert.strictEqual(-2, buffer.readFloatLE(0));
buffer[0] = 0xff;
buffer[1] = 0xff;
buffer[2] = 0x7f;
buffer[3] = 0x7f;
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.strictEqual(3.4028234663852886e+38, buffer.readFloatLE(0));
buffer[0] = 0xab;
buffer[1] = 0xaa;
buffer[2] = 0xaa;
buffer[3] = 0x3e;
assert.strictEqual(-1.2126478207002966e-12, buffer.readFloatBE(0));
assert.strictEqual(0.3333333432674408, buffer.readFloatLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
assert.strictEqual(0, buffer.readFloatBE(0));
assert.strictEqual(0, buffer.readFloatLE(0));
assert.strictEqual(false, 1 / buffer.readFloatLE(0) < 0);
buffer[3] = 0x80;
assert.strictEqual(1.793662034335766e-43, buffer.readFloatBE(0));
assert.strictEqual(-0, buffer.readFloatLE(0));
assert.strictEqual(true, 1 / buffer.readFloatLE(0) < 0);
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0x7f;
assert.strictEqual(4.609571298396486e-41, buffer.readFloatBE(0));
assert.strictEqual(Infinity, buffer.readFloatLE(0));
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0x80;
buffer[3] = 0xff;
assert.strictEqual(4.627507918739843e-41, buffer.readFloatBE(0));
assert.strictEqual(-Infinity, buffer.readFloatLE(0));
}
test(Buffer);

View File

@ -1,119 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're reading in signed integers correctly
*/
require('../common');
const assert = require('assert');
/*
* Test 8 bit signed integers
*/
function test8(clazz) {
const data = new clazz(4);
data[0] = 0x23;
assert.strictEqual(0x23, data.readInt8(0));
data[0] = 0xff;
assert.strictEqual(-1, data.readInt8(0));
data[0] = 0x87;
data[1] = 0xab;
data[2] = 0x7c;
data[3] = 0xef;
assert.strictEqual(-121, data.readInt8(0));
assert.strictEqual(-85, data.readInt8(1));
assert.strictEqual(124, data.readInt8(2));
assert.strictEqual(-17, data.readInt8(3));
}
function test16(clazz) {
const buffer = new clazz(6);
buffer[0] = 0x16;
buffer[1] = 0x79;
assert.strictEqual(0x1679, buffer.readInt16BE(0));
assert.strictEqual(0x7916, buffer.readInt16LE(0));
buffer[0] = 0xff;
buffer[1] = 0x80;
assert.strictEqual(-128, buffer.readInt16BE(0));
assert.strictEqual(-32513, buffer.readInt16LE(0));
/* test offset with weenix */
buffer[0] = 0x77;
buffer[1] = 0x65;
buffer[2] = 0x65;
buffer[3] = 0x6e;
buffer[4] = 0x69;
buffer[5] = 0x78;
assert.strictEqual(0x7765, buffer.readInt16BE(0));
assert.strictEqual(0x6565, buffer.readInt16BE(1));
assert.strictEqual(0x656e, buffer.readInt16BE(2));
assert.strictEqual(0x6e69, buffer.readInt16BE(3));
assert.strictEqual(0x6978, buffer.readInt16BE(4));
assert.strictEqual(0x6577, buffer.readInt16LE(0));
assert.strictEqual(0x6565, buffer.readInt16LE(1));
assert.strictEqual(0x6e65, buffer.readInt16LE(2));
assert.strictEqual(0x696e, buffer.readInt16LE(3));
assert.strictEqual(0x7869, buffer.readInt16LE(4));
}
function test32(clazz) {
const buffer = new clazz(6);
buffer[0] = 0x43;
buffer[1] = 0x53;
buffer[2] = 0x16;
buffer[3] = 0x79;
assert.strictEqual(0x43531679, buffer.readInt32BE(0));
assert.strictEqual(0x79165343, buffer.readInt32LE(0));
buffer[0] = 0xff;
buffer[1] = 0xfe;
buffer[2] = 0xef;
buffer[3] = 0xfa;
assert.strictEqual(-69638, buffer.readInt32BE(0));
assert.strictEqual(-84934913, buffer.readInt32LE(0));
buffer[0] = 0x42;
buffer[1] = 0xc3;
buffer[2] = 0x95;
buffer[3] = 0xa9;
buffer[4] = 0x36;
buffer[5] = 0x17;
assert.strictEqual(0x42c395a9, buffer.readInt32BE(0));
assert.strictEqual(-1013601994, buffer.readInt32BE(1));
assert.strictEqual(-1784072681, buffer.readInt32BE(2));
assert.strictEqual(-1449802942, buffer.readInt32LE(0));
assert.strictEqual(917083587, buffer.readInt32LE(1));
assert.strictEqual(389458325, buffer.readInt32LE(2));
}
test8(Buffer);
test16(Buffer);
test32(Buffer);

View File

@ -1,197 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're writing doubles correctly
*/
require('../common');
const assert = require('assert');
function test(clazz) {
const buffer = new clazz(16);
buffer.writeDoubleBE(2.225073858507201e-308, 0);
buffer.writeDoubleLE(2.225073858507201e-308, 8);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x0f, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0xff, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xff, buffer[7]);
assert.strictEqual(0xff, buffer[8]);
assert.strictEqual(0xff, buffer[9]);
assert.strictEqual(0xff, buffer[10]);
assert.strictEqual(0xff, buffer[11]);
assert.strictEqual(0xff, buffer[12]);
assert.strictEqual(0xff, buffer[13]);
assert.strictEqual(0x0f, buffer[14]);
assert.strictEqual(0x00, buffer[15]);
buffer.writeDoubleBE(1.0000000000000004, 0);
buffer.writeDoubleLE(1.0000000000000004, 8);
assert.strictEqual(0x3f, buffer[0]);
assert.strictEqual(0xf0, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x02, buffer[7]);
assert.strictEqual(0x02, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0xf0, buffer[14]);
assert.strictEqual(0x3f, buffer[15]);
buffer.writeDoubleBE(-2, 0);
buffer.writeDoubleLE(-2, 8);
assert.strictEqual(0xc0, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0x00, buffer[14]);
assert.strictEqual(0xc0, buffer[15]);
buffer.writeDoubleBE(1.7976931348623157e+308, 0);
buffer.writeDoubleLE(1.7976931348623157e+308, 8);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0xef, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0xff, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xff, buffer[7]);
assert.strictEqual(0xff, buffer[8]);
assert.strictEqual(0xff, buffer[9]);
assert.strictEqual(0xff, buffer[10]);
assert.strictEqual(0xff, buffer[11]);
assert.strictEqual(0xff, buffer[12]);
assert.strictEqual(0xff, buffer[13]);
assert.strictEqual(0xef, buffer[14]);
assert.strictEqual(0x7f, buffer[15]);
buffer.writeDoubleBE(0 * -1, 0);
buffer.writeDoubleLE(0 * -1, 8);
assert.strictEqual(0x80, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0x00, buffer[14]);
assert.strictEqual(0x80, buffer[15]);
buffer.writeDoubleBE(Infinity, 0);
buffer.writeDoubleLE(Infinity, 8);
assert.strictEqual(0x7F, buffer[0]);
assert.strictEqual(0xF0, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0xF0, buffer[14]);
assert.strictEqual(0x7F, buffer[15]);
assert.strictEqual(Infinity, buffer.readDoubleBE(0));
assert.strictEqual(Infinity, buffer.readDoubleLE(8));
buffer.writeDoubleBE(-Infinity, 0);
buffer.writeDoubleLE(-Infinity, 8);
assert.strictEqual(0xFF, buffer[0]);
assert.strictEqual(0xF0, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.strictEqual(0x00, buffer[8]);
assert.strictEqual(0x00, buffer[9]);
assert.strictEqual(0x00, buffer[10]);
assert.strictEqual(0x00, buffer[11]);
assert.strictEqual(0x00, buffer[12]);
assert.strictEqual(0x00, buffer[13]);
assert.strictEqual(0xF0, buffer[14]);
assert.strictEqual(0xFF, buffer[15]);
assert.strictEqual(-Infinity, buffer.readDoubleBE(0));
assert.strictEqual(-Infinity, buffer.readDoubleLE(8));
buffer.writeDoubleBE(NaN, 0);
buffer.writeDoubleLE(NaN, 8);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[0] || 0xFF === buffer[0]);
// mips processors use a slightly different NaN
assert(0xF8 === buffer[1] || 0xF7 === buffer[1]);
assert(0x00 === buffer[2] || 0xFF === buffer[2]);
assert(0x00 === buffer[3] || 0xFF === buffer[3]);
assert(0x00 === buffer[4] || 0xFF === buffer[4]);
assert(0x00 === buffer[5] || 0xFF === buffer[5]);
assert(0x00 === buffer[6] || 0xFF === buffer[6]);
assert(0x00 === buffer[7] || 0xFF === buffer[7]);
assert(0x00 === buffer[8] || 0xFF === buffer[8]);
assert(0x00 === buffer[9] || 0xFF === buffer[9]);
assert(0x00 === buffer[10] || 0xFF === buffer[10]);
assert(0x00 === buffer[11] || 0xFF === buffer[11]);
assert(0x00 === buffer[12] || 0xFF === buffer[12]);
assert(0x00 === buffer[13] || 0xFF === buffer[13]);
assert(0xF8 === buffer[14] || 0xF7 === buffer[14]);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[15] || 0xFF === buffer[15]);
assert.ok(Number.isNaN(buffer.readDoubleBE(0)));
assert.ok(Number.isNaN(buffer.readDoubleLE(8)));
}
test(Buffer);

View File

@ -1,135 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're writing floats correctly
*/
require('../common');
const assert = require('assert');
function test(clazz) {
const buffer = new clazz(8);
buffer.writeFloatBE(1, 0);
buffer.writeFloatLE(1, 4);
assert.strictEqual(0x3f, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x80, buffer[6]);
assert.strictEqual(0x3f, buffer[7]);
buffer.writeFloatBE(1 / 3, 0);
buffer.writeFloatLE(1 / 3, 4);
assert.strictEqual(0x3e, buffer[0]);
assert.strictEqual(0xaa, buffer[1]);
assert.strictEqual(0xaa, buffer[2]);
assert.strictEqual(0xab, buffer[3]);
assert.strictEqual(0xab, buffer[4]);
assert.strictEqual(0xaa, buffer[5]);
assert.strictEqual(0xaa, buffer[6]);
assert.strictEqual(0x3e, buffer[7]);
buffer.writeFloatBE(3.4028234663852886e+38, 0);
buffer.writeFloatLE(3.4028234663852886e+38, 4);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0x7f, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0xff, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0x7f, buffer[6]);
assert.strictEqual(0x7f, buffer[7]);
buffer.writeFloatLE(1.1754943508222875e-38, 0);
buffer.writeFloatBE(1.1754943508222875e-38, 4);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x80, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x80, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
buffer.writeFloatBE(0 * -1, 0);
buffer.writeFloatLE(0 * -1, 4);
assert.strictEqual(0x80, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x80, buffer[7]);
buffer.writeFloatBE(Infinity, 0);
buffer.writeFloatLE(Infinity, 4);
assert.strictEqual(0x7F, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x80, buffer[6]);
assert.strictEqual(0x7F, buffer[7]);
assert.strictEqual(Infinity, buffer.readFloatBE(0));
assert.strictEqual(Infinity, buffer.readFloatLE(4));
buffer.writeFloatBE(-Infinity, 0);
buffer.writeFloatLE(-Infinity, 4);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0xFF === buffer[0] || 0x7F === buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x80, buffer[6]);
assert.strictEqual(0xFF, buffer[7]);
assert.strictEqual(-Infinity, buffer.readFloatBE(0));
assert.strictEqual(-Infinity, buffer.readFloatLE(4));
buffer.writeFloatBE(NaN, 0);
buffer.writeFloatLE(NaN, 4);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[0] || 0xFF === buffer[0]);
// mips processors use a slightly different NaN
assert(0xC0 === buffer[1] || 0xBF === buffer[1]);
assert(0x00 === buffer[2] || 0xFF === buffer[2]);
assert(0x00 === buffer[3] || 0xFF === buffer[3]);
assert(0x00 === buffer[4] || 0xFF === buffer[4]);
assert(0x00 === buffer[5] || 0xFF === buffer[5]);
assert(0xC0 === buffer[6] || 0xBF === buffer[6]);
// Darwin ia32 does the other kind of NaN.
// Compiler bug. No one really cares.
assert(0x7F === buffer[7] || 0xFF === buffer[7]);
assert.ok(Number.isNaN(buffer.readFloatBE(0)));
assert.ok(Number.isNaN(buffer.readFloatLE(4)));
}
test(Buffer);

View File

@ -1,194 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* Tests to verify we're writing signed integers correctly
*/
const common = require('../common');
const assert = require('assert');
const errorOutOfBounds = common.expectsError({
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError,
message: /^The value "[^"]*" is invalid for option "value"$/
}, 12);
function test8(clazz) {
const buffer = new clazz(2);
buffer.writeInt8(0x23, 0);
buffer.writeInt8(-5, 1);
assert.strictEqual(0x23, buffer[0]);
assert.strictEqual(0xfb, buffer[1]);
/* Make sure we handle truncation correctly */
assert.throws(() => {
buffer.writeInt8(0xabc, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt8(0xabc, 0);
}, errorOutOfBounds);
/* Make sure we handle min/max correctly */
buffer.writeInt8(0x7f, 0);
buffer.writeInt8(-0x80, 1);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0x80, buffer[1]);
assert.throws(() => {
buffer.writeInt8(0x7f + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt8(-0x80 - 1, 0);
}, errorOutOfBounds);
}
function test16(clazz) {
const buffer = new clazz(6);
buffer.writeInt16BE(0x0023, 0);
buffer.writeInt16LE(0x0023, 2);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x23, buffer[1]);
assert.strictEqual(0x23, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
buffer.writeInt16BE(-5, 0);
buffer.writeInt16LE(-5, 2);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0xfb, buffer[1]);
assert.strictEqual(0xfb, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
buffer.writeInt16BE(-1679, 1);
buffer.writeInt16LE(-1679, 3);
assert.strictEqual(0xf9, buffer[1]);
assert.strictEqual(0x71, buffer[2]);
assert.strictEqual(0x71, buffer[3]);
assert.strictEqual(0xf9, buffer[4]);
/* Make sure we handle min/max correctly */
buffer.writeInt16BE(0x7fff, 0);
buffer.writeInt16BE(-0x8000, 2);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0x80, buffer[2]);
assert.strictEqual(0x00, buffer[3]);
assert.throws(() => {
buffer.writeInt16BE(0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt16BE(-0x8000 - 1, 0);
}, errorOutOfBounds);
buffer.writeInt16LE(0x7fff, 0);
buffer.writeInt16LE(-0x8000, 2);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0x7f, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x80, buffer[3]);
assert.throws(() => {
buffer.writeInt16LE(0x7fff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt16LE(-0x8000 - 1, 0);
}, errorOutOfBounds);
}
function test32(clazz) {
const buffer = new clazz(8);
buffer.writeInt32BE(0x23, 0);
buffer.writeInt32LE(0x23, 4);
assert.strictEqual(0x00, buffer[0]);
assert.strictEqual(0x00, buffer[1]);
assert.strictEqual(0x00, buffer[2]);
assert.strictEqual(0x23, buffer[3]);
assert.strictEqual(0x23, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
buffer.writeInt32BE(-5, 0);
buffer.writeInt32LE(-5, 4);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xfb, buffer[3]);
assert.strictEqual(0xfb, buffer[4]);
assert.strictEqual(0xff, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xff, buffer[7]);
buffer.writeInt32BE(-805306713, 0);
buffer.writeInt32LE(-805306713, 4);
assert.strictEqual(0xcf, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xfe, buffer[2]);
assert.strictEqual(0xa7, buffer[3]);
assert.strictEqual(0xa7, buffer[4]);
assert.strictEqual(0xfe, buffer[5]);
assert.strictEqual(0xff, buffer[6]);
assert.strictEqual(0xcf, buffer[7]);
/* Make sure we handle min/max correctly */
buffer.writeInt32BE(0x7fffffff, 0);
buffer.writeInt32BE(-0x80000000, 4);
assert.strictEqual(0x7f, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0xff, buffer[3]);
assert.strictEqual(0x80, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x00, buffer[7]);
assert.throws(() => {
buffer.writeInt32BE(0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt32BE(-0x80000000 - 1, 0);
}, errorOutOfBounds);
buffer.writeInt32LE(0x7fffffff, 0);
buffer.writeInt32LE(-0x80000000, 4);
assert.strictEqual(0xff, buffer[0]);
assert.strictEqual(0xff, buffer[1]);
assert.strictEqual(0xff, buffer[2]);
assert.strictEqual(0x7f, buffer[3]);
assert.strictEqual(0x00, buffer[4]);
assert.strictEqual(0x00, buffer[5]);
assert.strictEqual(0x00, buffer[6]);
assert.strictEqual(0x80, buffer[7]);
assert.throws(() => {
buffer.writeInt32LE(0x7fffffff + 1, 0);
}, errorOutOfBounds);
assert.throws(() => {
buffer.writeInt32LE(-0x80000000 - 1, 0);
}, errorOutOfBounds);
}
test8(Buffer);
test16(Buffer);
test32(Buffer);

View File

@ -1,171 +0,0 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
/*
* A battery of tests to help us read a series of uints
*/
const common = require('../common');
const assert = require('assert');
/*
* We need to check the following things:
* - We are correctly resolving big endian (doesn't mean anything for 8 bit)
* - Correctly resolving little endian (doesn't mean anything for 8 bit)
* - Correctly using the offsets
* - Correctly interpreting values that are beyond the signed range as unsigned
*/
function test8(clazz) {
const data = new clazz(4);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.strictEqual(23, data[0]);
assert.strictEqual(23, data[1]);
assert.strictEqual(23, data[2]);
assert.strictEqual(23, data[3]);
data.writeUInt8(23, 0);
data.writeUInt8(23, 1);
data.writeUInt8(23, 2);
data.writeUInt8(23, 3);
assert.strictEqual(23, data[0]);
assert.strictEqual(23, data[1]);
assert.strictEqual(23, data[2]);
assert.strictEqual(23, data[3]);
data.writeUInt8(255, 0);
assert.strictEqual(255, data[0]);
data.writeUInt8(255, 0);
assert.strictEqual(255, data[0]);
}
function test16(clazz) {
let value = 0x2343;
const data = new clazz(4);
data.writeUInt16BE(value, 0);
assert.strictEqual(0x23, data[0]);
assert.strictEqual(0x43, data[1]);
data.writeUInt16BE(value, 1);
assert.strictEqual(0x23, data[1]);
assert.strictEqual(0x43, data[2]);
data.writeUInt16BE(value, 2);
assert.strictEqual(0x23, data[2]);
assert.strictEqual(0x43, data[3]);
data.writeUInt16LE(value, 0);
assert.strictEqual(0x23, data[1]);
assert.strictEqual(0x43, data[0]);
data.writeUInt16LE(value, 1);
assert.strictEqual(0x23, data[2]);
assert.strictEqual(0x43, data[1]);
data.writeUInt16LE(value, 2);
assert.strictEqual(0x23, data[3]);
assert.strictEqual(0x43, data[2]);
value = 0xff80;
data.writeUInt16LE(value, 0);
assert.strictEqual(0xff, data[1]);
assert.strictEqual(0x80, data[0]);
data.writeUInt16BE(value, 0);
assert.strictEqual(0xff, data[0]);
assert.strictEqual(0x80, data[1]);
}
function test32(clazz) {
const data = new clazz(6);
const value = 0xe7f90a6d;
data.writeUInt32BE(value, 0);
assert.strictEqual(0xe7, data[0]);
assert.strictEqual(0xf9, data[1]);
assert.strictEqual(0x0a, data[2]);
assert.strictEqual(0x6d, data[3]);
data.writeUInt32BE(value, 1);
assert.strictEqual(0xe7, data[1]);
assert.strictEqual(0xf9, data[2]);
assert.strictEqual(0x0a, data[3]);
assert.strictEqual(0x6d, data[4]);
data.writeUInt32BE(value, 2);
assert.strictEqual(0xe7, data[2]);
assert.strictEqual(0xf9, data[3]);
assert.strictEqual(0x0a, data[4]);
assert.strictEqual(0x6d, data[5]);
data.writeUInt32LE(value, 0);
assert.strictEqual(0xe7, data[3]);
assert.strictEqual(0xf9, data[2]);
assert.strictEqual(0x0a, data[1]);
assert.strictEqual(0x6d, data[0]);
data.writeUInt32LE(value, 1);
assert.strictEqual(0xe7, data[4]);
assert.strictEqual(0xf9, data[3]);
assert.strictEqual(0x0a, data[2]);
assert.strictEqual(0x6d, data[1]);
data.writeUInt32LE(value, 2);
assert.strictEqual(0xe7, data[5]);
assert.strictEqual(0xf9, data[4]);
assert.strictEqual(0x0a, data[3]);
assert.strictEqual(0x6d, data[2]);
}
function testUint(clazz) {
const data = new clazz(8);
let val = 1;
// Test 0 to 5 bytes.
for (let i = 0; i <= 5; i++) {
const errMsg = common.expectsError({
code: 'ERR_INVALID_OPT_VALUE',
type: RangeError,
message: /^The value "[^"]*" is invalid for option "value"$/
}, 2);
assert.throws(() => {
data.writeUIntBE(val, 0, i);
}, errMsg);
assert.throws(() => {
data.writeUIntLE(val, 0, i);
}, errMsg);
val *= 0x100;
}
}
test8(Buffer);
test16(Buffer);
test32(Buffer);
testUint(Buffer);