Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,25 @@ console.log(buf.readInt32LE());
console.log(buf.readInt32LE(1));
```

### buf.readInt64LE(offset[, noAssert])
### buf.readInt64BE(offset[, noAssert])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<!-- YAML
added: replaceme
-->

:)

<!-- YAML
added: replaceme
-->

* `offset` {integer} Where to start reading. Must satisfy: `0 <= offset <= buf.length - 8`
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {string}

Reads a signed 64-bit integer from `buf` at the specified `offset` with
the specified endian format (`readInt64BE()` returns big endian,
`readInt64LE()` return little endian).

Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the result should be considered undefined behavior.

Integers read from a `Buffer` are interpreted as two's complement signed values.

### buf.readIntBE(offset, byteLength[, noAssert])
### buf.readIntLE(offset, byteLength[, noAssert])
<!-- YAML
Expand Down Expand Up @@ -1854,6 +1873,38 @@ console.log(buf.readUInt32LE(0).toString(16));
console.log(buf.readUInt32LE(1).toString(16));
```

### buf.readUInt64LE(offset[, noAssert])
### buf.readUInt64BE(offset[, noAssert])
<!-- YAML
added: replaceme
-->

* `offset` {integer} Where to start reading. Must satisfy: `0 <= offset <= buf.length - 8`
* `noAssert` {boolean} Skip `offset` validation? **Default:** `false`
* Returns: {string}

Reads an unsigned 64-bit integer from `buf` at the specified `offset` with
specified endian format (`readUInt64BE()` returns big endian,
`readUInt64LE()` returns little endian).

Setting `noAssert` to `true` allows `offset` to be beyond the end of `buf`, but
the result should be considered undefined behavior.

Examples:

```js
var buf = Buffer.allocUnsafe(8);
buf[0] = buf[1] = buf[2] = buf[3] = 0x00;
buf[4] = buf[5] = buf[6] = buf[7] = 0xff;
// <Buffer 00 00 00 00 ff ff ff ff>

// Prints: '4294967295'
console.log(buf.readUInt64BE(0));

// Prints: '1844674406941458432'
console.log(b.readUInt64LE(0));
```

### buf.readUIntBE(offset, byteLength[, noAssert])
### buf.readUIntLE(offset, byteLength[, noAssert])
<!-- YAML
Expand Down Expand Up @@ -2358,6 +2409,39 @@ buf.writeInt32LE(0x05060708, 4);
console.log(buf);
```

### buf.writeInt64(value, offset[, noAssert])
### buf.writeInt64(value, offset[, noAssert])
<!-- YAML
added: replaceme
-->

* `value` {integer|string} Number to be written to `buf`
* `offset` {integer} Where to start writing. 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
format (`writeInt64BE()` writes big endian, `writeInt64LE()` writes little
endian). `value` *should* be a valid signed 64-bit integer or a String
representation of one. Behavior is undefined when `value` is anything other than
a signed 64-bit integer or a String representation of one.

Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the result should be considered undefined behavior.

`value` is interpreted and written as a two's complement signed integer.

Examples:

```js
const buf = Buffer.allocUnsafe(8);

buf.writeInt64BE('0x0102030405060708', 0);

// Prints: <Buffer 01 02 03 04 05 06 07 08>
console.log(buf);
```

### buf.writeIntBE(value, offset, byteLength[, noAssert])
### buf.writeIntLE(value, offset, byteLength[, noAssert])
<!-- YAML
Expand Down Expand Up @@ -2497,6 +2581,37 @@ buf.writeUInt32LE(0xfeedface, 0);
console.log(buf);
```

### buf.writeUInt64LE(value, offset[, noAssert])
### buf.writeUInt64BE(value, offset[, noAssert])
<!-- YAML
added: replaceme
-->

* `value` {integer|string} Number to be written to `buf`
* `offset` {integer} Where to start writing. 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
format (`writeUInt64BE()` writes big endian, `writeUInt64LE()` writes little
endian). `value` should be a valid unsigned 64-bit integer or a String
representation of one. Behavior is undefined when `value` is anything other than
an unsigned 64-bit integer or a String representation of one.

Setting `noAssert` to `true` allows the encoded form of `value` to extend beyond
the end of `buf`, but the result should be considered undefined behavior.

Examples:

```js
var buf = Buffer.allocUnsafe(8);

buf.writeUInt64LE('0xffffffffffff', 0);

// Prints: <Buffer ff ff ff ff ff ff 00 00>
console.log(buf);
```

### buf.writeUIntBE(value, offset, byteLength[, noAssert])
### buf.writeUIntLE(value, offset, byteLength[, noAssert])
<!-- YAML
Expand Down
81 changes: 81 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const {
readDoubleLE: _readDoubleLE,
readFloatBE: _readFloatBE,
readFloatLE: _readFloatLE,
readInt64BE: _readInt64BE,
readInt64LE: _readInt64LE,
readUInt64BE: _readUInt64BE,
readUInt64LE: _readUInt64LE,
setupBufferJS,
swap16: _swap16,
swap32: _swap32,
Expand All @@ -43,6 +47,10 @@ const {
writeDoubleLE: _writeDoubleLE,
writeFloatBE: _writeFloatBE,
writeFloatLE: _writeFloatLE,
writeInt64BE: _writeInt64BE,
writeInt64LE: _writeInt64LE,
writeUInt64BE: _writeUInt64BE,
writeUInt64LE: _writeUInt64LE,
kMaxLength,
kStringMaxLength
} = process.binding('buffer');
Expand Down Expand Up @@ -1222,6 +1230,38 @@ Buffer.prototype.readDoubleBE = function readDoubleBE(offset, noAssert) {
};


Buffer.prototype.readInt64LE = function readInt64LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 8, this.length);
return _readInt64LE(this, offset);
};


Buffer.prototype.readInt64BE = function readInt64BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 8, this.length);
return _readInt64BE(this, offset);
};


Buffer.prototype.readUInt64LE = function readUInt64LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 8, this.length);
return _readUInt64LE(this, offset);
};


Buffer.prototype.readUInt64BE = function readUInt64BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
checkOffset(offset, 8, this.length);
return _readUInt64BE(this, offset);
};


function checkInt(buffer, value, offset, ext, max, min) {
if (value > max || value < min)
throw new errors.RangeError('ERR_INVALID_OPT_VALUE', 'value', value);
Expand Down Expand Up @@ -1488,6 +1528,47 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE(val, offset, noAssert) {
return offset + 8;
};


Buffer.prototype.writeInt64LE = function writeInt64LE(val, offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
_writeInt64LE(this, val, offset);
else
_writeInt64LE(this, val, offset, true);
return offset + 8;
};


Buffer.prototype.writeInt64BE = function writeInt64BE(val, offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
_writeInt64BE(this, val, offset);
else
_writeInt64BE(this, val, offset, true);
return offset + 8;
};


Buffer.prototype.writeUInt64LE = function writeUInt64LE(val, offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
_writeUInt64LE(this, val, offset);
else
_writeUInt64LE(this, val, offset, true);
return offset + 8;
};


Buffer.prototype.writeUInt64BE = function writeUInt64BE(val, offset, noAssert) {
offset = offset >>> 0;
if (!noAssert)
_writeUInt64BE(this, val, offset);
else
_writeUInt64BE(this, val, offset, true);
return offset + 8;
};


function swap(b, n, m) {
const i = b[n];
b[n] = b[m];
Expand Down
Loading