Reviewed-By: Joye...">

buffer: add {read|write}Big[U]Int64{BE|LE} methods · nodejs/node@3d8532f (original) (raw)

`@@ -63,6 +63,82 @@ function boundsError(value, length, type) {

`

63

63

`}

`

64

64

``

65

65

`// Read integers.

`

``

66

`+

function readBigUInt64LE(offset = 0) {

`

``

67

`+

validateNumber(offset, 'offset');

`

``

68

`+

const first = this[offset];

`

``

69

`+

const last = this[offset + 7];

`

``

70

`+

if (first === undefined || last === undefined)

`

``

71

`+

boundsError(offset, this.length - 8);

`

``

72

+

``

73

`+

const lo = first +

`

``

74

`+

this[++offset] * 2 ** 8 +

`

``

75

`+

this[++offset] * 2 ** 16 +

`

``

76

`+

this[++offset] * 2 ** 24;

`

``

77

+

``

78

`+

const hi = this[++offset] +

`

``

79

`+

this[++offset] * 2 ** 8 +

`

``

80

`+

this[++offset] * 2 ** 16 +

`

``

81

`+

last * 2 ** 24;

`

``

82

+

``

83

`+

return BigInt(lo) + (BigInt(hi) << 32n);

`

``

84

`+

}

`

``

85

+

``

86

`+

function readBigUInt64BE(offset = 0) {

`

``

87

`+

validateNumber(offset, 'offset');

`

``

88

`+

const first = this[offset];

`

``

89

`+

const last = this[offset + 7];

`

``

90

`+

if (first === undefined || last === undefined)

`

``

91

`+

boundsError(offset, this.length - 8);

`

``

92

+

``

93

`+

const hi = first * 2 ** 24 +

`

``

94

`+

this[++offset] * 2 ** 16 +

`

``

95

`+

this[++offset] * 2 ** 8 +

`

``

96

`+

this[++offset];

`

``

97

+

``

98

`+

const lo = this[++offset] * 2 ** 24 +

`

``

99

`+

this[++offset] * 2 ** 16 +

`

``

100

`+

this[++offset] * 2 ** 8 +

`

``

101

`+

last;

`

``

102

+

``

103

`+

return (BigInt(hi) << 32n) + BigInt(lo);

`

``

104

`+

}

`

``

105

+

``

106

`+

function readBigInt64LE(offset = 0) {

`

``

107

`+

validateNumber(offset, 'offset');

`

``

108

`+

const first = this[offset];

`

``

109

`+

const last = this[offset + 7];

`

``

110

`+

if (first === undefined || last === undefined)

`

``

111

`+

boundsError(offset, this.length - 8);

`

``

112

+

``

113

`+

const val = this[offset + 4] +

`

``

114

`+

this[offset + 5] * 2 ** 8 +

`

``

115

`+

this[offset + 6] * 2 ** 16 +

`

``

116

`+

(last << 24); // Overflow

`

``

117

`+

return (BigInt(val) << 32n) +

`

``

118

`+

BigInt(first +

`

``

119

`+

this[++offset] * 2 ** 8 +

`

``

120

`+

this[++offset] * 2 ** 16 +

`

``

121

`+

this[++offset] * 2 ** 24);

`

``

122

`+

}

`

``

123

+

``

124

`+

function readBigInt64BE(offset = 0) {

`

``

125

`+

validateNumber(offset, 'offset');

`

``

126

`+

const first = this[offset];

`

``

127

`+

const last = this[offset + 7];

`

``

128

`+

if (first === undefined || last === undefined)

`

``

129

`+

boundsError(offset, this.length - 8);

`

``

130

+

``

131

`+

const val = (first << 24) + // Overflow

`

``

132

`+

this[++offset] * 2 ** 16 +

`

``

133

`+

this[++offset] * 2 ** 8 +

`

``

134

`+

this[++offset];

`

``

135

`+

return (BigInt(val) << 32n) +

`

``

136

`+

BigInt(this[++offset] * 2 ** 24 +

`

``

137

`+

this[++offset] * 2 ** 16 +

`

``

138

`+

this[++offset] * 2 ** 8 +

`

``

139

`+

last);

`

``

140

`+

}

`

``

141

+

66

142

`function readUIntLE(offset, byteLength) {

`

67

143

`if (offset === undefined)

`

68

144

`throw new ERR_INVALID_ARG_TYPE('offset', 'number', offset);

`

`@@ -473,6 +549,68 @@ function readDoubleForwards(offset = 0) {

`

473

549

`}

`

474

550

``

475

551

`// Write integers.

`

``

552

`+

function writeBigU_Int64LE(buf, value, offset, min, max) {

`

``

553

`+

checkInt(value, min, max, buf, offset, 7);

`

``

554

+

``

555

`+

let lo = Number(value & 0xffffffffn);

`

``

556

`+

buf[offset++] = lo;

`

``

557

`+

lo = lo >> 8;

`

``

558

`+

buf[offset++] = lo;

`

``

559

`+

lo = lo >> 8;

`

``

560

`+

buf[offset++] = lo;

`

``

561

`+

lo = lo >> 8;

`

``

562

`+

buf[offset++] = lo;

`

``

563

`+

let hi = Number(value >> 32n & 0xffffffffn);

`

``

564

`+

buf[offset++] = hi;

`

``

565

`+

hi = hi >> 8;

`

``

566

`+

buf[offset++] = hi;

`

``

567

`+

hi = hi >> 8;

`

``

568

`+

buf[offset++] = hi;

`

``

569

`+

hi = hi >> 8;

`

``

570

`+

buf[offset++] = hi;

`

``

571

`+

return offset;

`

``

572

`+

}

`

``

573

+

``

574

`+

function writeBigUInt64LE(value, offset = 0) {

`

``

575

`+

return writeBigU_Int64LE(this, value, offset, 0n, 0xffffffffffffffffn);

`

``

576

`+

}

`

``

577

+

``

578

`+

function writeBigU_Int64BE(buf, value, offset, min, max) {

`

``

579

`+

checkInt(value, min, max, buf, offset, 7);

`

``

580

+

``

581

`+

let lo = Number(value & 0xffffffffn);

`

``

582

`+

buf[offset + 7] = lo;

`

``

583

`+

lo = lo >> 8;

`

``

584

`+

buf[offset + 6] = lo;

`

``

585

`+

lo = lo >> 8;

`

``

586

`+

buf[offset + 5] = lo;

`

``

587

`+

lo = lo >> 8;

`

``

588

`+

buf[offset + 4] = lo;

`

``

589

`+

let hi = Number(value >> 32n & 0xffffffffn);

`

``

590

`+

buf[offset + 3] = hi;

`

``

591

`+

hi = hi >> 8;

`

``

592

`+

buf[offset + 2] = hi;

`

``

593

`+

hi = hi >> 8;

`

``

594

`+

buf[offset + 1] = hi;

`

``

595

`+

hi = hi >> 8;

`

``

596

`+

buf[offset] = hi;

`

``

597

`+

return offset + 8;

`

``

598

`+

}

`

``

599

+

``

600

`+

function writeBigUInt64BE(value, offset = 0) {

`

``

601

`+

return writeBigU_Int64BE(this, value, offset, 0n, 0xffffffffffffffffn);

`

``

602

`+

}

`

``

603

+

``

604

`+

function writeBigInt64LE(value, offset = 0) {

`

``

605

`+

return writeBigU_Int64LE(

`

``

606

`+

this, value, offset, -0x8000000000000000n, 0x7fffffffffffffffn);

`

``

607

`+

}

`

``

608

+

``

609

`+

function writeBigInt64BE(value, offset = 0) {

`

``

610

`+

return writeBigU_Int64BE(

`

``

611

`+

this, value, offset, -0x8000000000000000n, 0x7fffffffffffffffn);

`

``

612

`+

}

`

``

613

+

476

614

`function writeUIntLE(value, offset, byteLength) {

`

477

615

`if (byteLength === 6)

`

478

616

`return writeU_Int48LE(this, value, offset, 0, 0xffffffffffff);

`

`@@ -790,6 +928,15 @@ function writeFloatBackwards(val, offset = 0) {

`

790

928

`class FastBuffer extends Uint8Array {}

`

791

929

``

792

930

`function addBufferPrototypeMethods(proto) {

`

``

931

`+

proto.readBigUInt64LE = readBigUInt64LE,

`

``

932

`+

proto.readBigUInt64BE = readBigUInt64BE,

`

``

933

`+

proto.readBigInt64LE = readBigInt64LE,

`

``

934

`+

proto.readBigInt64BE = readBigInt64BE,

`

``

935

`+

proto.writeBigUInt64LE = writeBigUInt64LE,

`

``

936

`+

proto.writeBigUInt64BE = writeBigUInt64BE,

`

``

937

`+

proto.writeBigInt64LE = writeBigInt64LE,

`

``

938

`+

proto.writeBigInt64BE = writeBigInt64BE,

`

``

939

+

793

940

`proto.readUIntLE = readUIntLE;

`

794

941

`proto.readUInt32LE = readUInt32LE;

`

795

942

`proto.readUInt16LE = readUInt16LE;

`