Skip to content

buffer: create relative reading methods #58295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
143 changes: 143 additions & 0 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ function readBigUInt64LE(offset = 0) {
return BigInt(lo) + (BigInt(hi) << 32n);
}

function getBigUInt64LE() {
const val = this.readBigUInt64LE(this.readerIndex);
this.readerIndex += 8;
return val;
}

function readBigUInt64BE(offset = 0) {
validateNumber(offset, 'offset');
const first = this[offset];
Expand All @@ -138,6 +144,12 @@ function readBigUInt64BE(offset = 0) {
return (BigInt(hi) << 32n) + BigInt(lo);
}

function getBigUInt64BE() {
const val = this.readBigUInt64BE(this.readerIndex);
this.readerIndex += 8;
return val;
}

function readBigInt64LE(offset = 0) {
validateNumber(offset, 'offset');
const first = this[offset];
Expand All @@ -156,6 +168,12 @@ function readBigInt64LE(offset = 0) {
this[++offset] * 2 ** 24);
}

function getBigInt64LE() {
const val = this.readBigInt64LE(this.readerIndex);
this.readerIndex += 8;
return val;
}

function readBigInt64BE(offset = 0) {
validateNumber(offset, 'offset');
const first = this[offset];
Expand All @@ -174,6 +192,12 @@ function readBigInt64BE(offset = 0) {
last);
}

function getBigInt64BE() {
const val = this.readBigInt64BE(this.readerIndex);
this.readerIndex += 8;
return val;
}

function readUIntLE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
Expand All @@ -193,6 +217,12 @@ function readUIntLE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function getUIntLE(byteLength) {
const val = this.readUIntLE(this.readerIndex, byteLength);
this.readerIndex += byteLength;
return val;
}

function readUInt48LE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand Down Expand Up @@ -234,6 +264,12 @@ function readUInt32LE(offset = 0) {
last * 2 ** 24;
}

function getUint32LE() {
const val = this.readUInt32LE(this.readerIndex);
this.readerIndex += 4;
return val;
}

function readUInt24LE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand All @@ -254,6 +290,12 @@ function readUInt16LE(offset = 0) {
return first + last * 2 ** 8;
}

function getUint16LE() {
const val = this.readUInt16LE(this.readerIndex);
this.readerIndex += 2;
return val;
}

function readUInt8(offset = 0) {
validateNumber(offset, 'offset');
const val = this[offset];
Expand All @@ -263,6 +305,12 @@ function readUInt8(offset = 0) {
return val;
}

function getUint8() {
const val = this.readUInt8(this.readerIndex);
this.readerIndex += 1;
return val;
}

function readUIntBE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
Expand All @@ -282,6 +330,12 @@ function readUIntBE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function getUIntBE(byteLength) {
const val = this.readUIntBE(this.readerIndex, byteLength);
this.readerIndex += byteLength;
return val;
}

function readUInt48BE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand Down Expand Up @@ -323,6 +377,12 @@ function readUInt32BE(offset = 0) {
last;
}

function getUint32BE() {
const val = this.readUInt32BE(this.readerIndex);
this.readerIndex += 4;
return val;
}

function readUInt24BE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand All @@ -343,6 +403,12 @@ function readUInt16BE(offset = 0) {
return first * 2 ** 8 + last;
}

function getUint16BE() {
const val = this.readUInt16BE(this.readerIndex);
this.readerIndex += 2;
return val;
}

function readIntLE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
Expand All @@ -362,6 +428,12 @@ function readIntLE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function getIntLE(byteLength) {
const val = this.readIntLE(this.readerIndex, byteLength);
this.readerIndex += byteLength;
return val;
}

function readInt48LE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand Down Expand Up @@ -404,6 +476,12 @@ function readInt32LE(offset = 0) {
(last << 24); // Overflow
}

function getInt32LE() {
const val = this.readInt32LE(this.readerIndex);
this.readerIndex += 4;
return val;
}

function readInt24LE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand All @@ -426,6 +504,12 @@ function readInt16LE(offset = 0) {
return val | (val & 2 ** 15) * 0x1fffe;
}

function getInt16LE() {
const val = this.readInt16LE(this.readerIndex);
this.readerIndex += 2;
return val;
}

function readInt8(offset = 0) {
validateNumber(offset, 'offset');
const val = this[offset];
Expand All @@ -435,6 +519,12 @@ function readInt8(offset = 0) {
return val | (val & 2 ** 7) * 0x1fffffe;
}

function getInt8() {
const val = this.readInt8(this.readerIndex);
this.readerIndex += 1;
return val;
}

function readIntBE(offset, byteLength) {
if (offset === undefined)
throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);
Expand All @@ -454,6 +544,12 @@ function readIntBE(offset, byteLength) {
boundsError(byteLength, 6, 'byteLength');
}

function getIntBE(byteLength) {
const val = this.readIntBE(this.readerIndex, byteLength);
this.readerIndex += byteLength;
return val;
}

function readInt48BE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand Down Expand Up @@ -496,6 +592,12 @@ function readInt32BE(offset = 0) {
last;
}

function getInt32BE() {
const val = this.readInt32BE(this.readerIndex);
this.readerIndex += 4;
return val;
}

function readInt24BE(buf, offset = 0) {
validateNumber(offset, 'offset');
const first = buf[offset];
Expand All @@ -518,6 +620,12 @@ function readInt16BE(offset = 0) {
return val | (val & 2 ** 15) * 0x1fffe;
}

function getInt16BE() {
const val = this.readInt16BE(this.readerIndex);
this.readerIndex += 2;
return val;
}

// Read floats
function readFloatBackwards(offset = 0) {
validateNumber(offset, 'offset');
Expand Down Expand Up @@ -960,6 +1068,10 @@ function writeFloatBackwards(val, offset = 0) {
return offset;
}

function reset() {
this.readerIndex = 0;
}

class FastBuffer extends Uint8Array {
// Using an explicit constructor here is necessary to avoid relying on
// `Array.prototype[Symbol.iterator]`, which can be mutated by users.
Expand All @@ -970,12 +1082,20 @@ class FastBuffer extends Uint8Array {
}

function addBufferPrototypeMethods(proto) {
proto.readerIndex = 0;

proto.readBigUInt64LE = readBigUInt64LE;
proto.getBigUInt64LE = getBigUInt64LE;
proto.readBigUInt64BE = readBigUInt64BE;
proto.getBigUInt64BE = getBigUInt64BE;
proto.readBigUint64LE = readBigUInt64LE;
proto.getBigUint64LE = getBigUInt64LE;
proto.readBigUint64BE = readBigUInt64BE;
proto.getBigUint64BE = getBigUInt64BE;
proto.readBigInt64LE = readBigInt64LE;
proto.getBigInt64LE = getBigInt64LE;
proto.readBigInt64BE = readBigInt64BE;
proto.getBigInt64BE = getBigInt64BE;
proto.writeBigUInt64LE = writeBigUInt64LE;
proto.writeBigUInt64BE = writeBigUInt64BE;
proto.writeBigUint64LE = writeBigUInt64LE;
Expand All @@ -984,12 +1104,26 @@ function addBufferPrototypeMethods(proto) {
proto.writeBigInt64BE = writeBigInt64BE;

proto.readUIntLE = readUIntLE;
proto.getUIntLE = getUIntLE;
proto.getUintLE = getUIntLE;
proto.readUInt32LE = readUInt32LE;
proto.getUInt32LE = getUint32LE;
proto.getUint32LE = getUint32LE;
proto.readUInt16LE = readUInt16LE;
proto.getUInt16LE = getUint16LE;
proto.getUint16LE = getUint16LE;
proto.readUInt8 = readUInt8;
proto.getUInt8 = getUint8;
proto.getUint8 = getUint8;
proto.readUIntBE = readUIntBE;
proto.getUIntBE = getUIntBE;
proto.getUIntBE = getUIntBE;
proto.readUInt32BE = readUInt32BE;
proto.getUInt32BE = getUint32BE;
proto.getUint32BE = getUint32BE;
proto.readUInt16BE = readUInt16BE;
proto.getUInt16BE = getUint16BE;
proto.getUint16BE = getUint16BE;
proto.readUintLE = readUIntLE;
proto.readUint32LE = readUInt32LE;
proto.readUint16LE = readUInt16LE;
Expand All @@ -998,12 +1132,21 @@ function addBufferPrototypeMethods(proto) {
proto.readUint32BE = readUInt32BE;
proto.readUint16BE = readUInt16BE;
proto.readIntLE = readIntLE;
proto.getIntLE = getIntLE;
proto.readInt32LE = readInt32LE;
proto.getInt32LE = getInt32LE;
proto.readInt16LE = readInt16LE;
proto.getInt16LE = getInt16LE;
proto.readInt8 = readInt8;
proto.getInt8 = getInt8;
proto.readIntBE = readIntBE;
proto.getIntBE = getIntBE;
proto.readInt32BE = readInt32BE;
proto.getInt32BE = getInt32BE;
proto.readInt16BE = readInt16BE;
proto.getInt16BE = getInt16BE;

proto.reset = reset;

proto.writeUIntLE = writeUIntLE;
proto.writeUInt32LE = writeUInt32LE;
Expand Down
Loading
Loading