@@ -52,6 +52,82 @@ function boundsError(value, length, type) {
52
52
}
53
53
54
54
// Read integers.
55
+ function readBigUInt64LE ( offset = 0 ) {
56
+ validateNumber ( offset , 'offset' ) ;
57
+ const first = this [ offset ] ;
58
+ const last = this [ offset + 7 ] ;
59
+ if ( first === undefined || last === undefined )
60
+ boundsError ( offset , this . length - 8 ) ;
61
+
62
+ const lo = first +
63
+ this [ ++ offset ] * 2 ** 8 +
64
+ this [ ++ offset ] * 2 ** 16 +
65
+ this [ ++ offset ] * 2 ** 24 ;
66
+
67
+ const hi = this [ ++ offset ] +
68
+ this [ ++ offset ] * 2 ** 8 +
69
+ this [ ++ offset ] * 2 ** 16 +
70
+ last * 2 ** 24 ;
71
+
72
+ return BigInt ( lo ) + ( BigInt ( hi ) << 32n ) ;
73
+ }
74
+
75
+ function readBigUInt64BE ( offset = 0 ) {
76
+ validateNumber ( offset , 'offset' ) ;
77
+ const first = this [ offset ] ;
78
+ const last = this [ offset + 7 ] ;
79
+ if ( first === undefined || last === undefined )
80
+ boundsError ( offset , this . length - 8 ) ;
81
+
82
+ const hi = first * 2 ** 24 +
83
+ this [ ++ offset ] * 2 ** 16 +
84
+ this [ ++ offset ] * 2 ** 8 +
85
+ this [ ++ offset ] ;
86
+
87
+ const lo = this [ ++ offset ] * 2 ** 24 +
88
+ this [ ++ offset ] * 2 ** 16 +
89
+ this [ ++ offset ] * 2 ** 8 +
90
+ last ;
91
+
92
+ return ( BigInt ( hi ) << 32n ) + BigInt ( lo ) ;
93
+ }
94
+
95
+ function readBigInt64LE ( offset = 0 ) {
96
+ validateNumber ( offset , 'offset' ) ;
97
+ const first = this [ offset ] ;
98
+ const last = this [ offset + 7 ] ;
99
+ if ( first === undefined || last === undefined )
100
+ boundsError ( offset , this . length - 8 ) ;
101
+
102
+ const val = this [ offset + 4 ] +
103
+ this [ offset + 5 ] * 2 ** 8 +
104
+ this [ offset + 6 ] * 2 ** 16 +
105
+ ( last << 24 ) ; // Overflow
106
+ return ( BigInt ( val ) << 32n ) +
107
+ BigInt ( first +
108
+ this [ ++ offset ] * 2 ** 8 +
109
+ this [ ++ offset ] * 2 ** 16 +
110
+ this [ ++ offset ] * 2 ** 24 ) ;
111
+ }
112
+
113
+ function readBigInt64BE ( offset = 0 ) {
114
+ validateNumber ( offset , 'offset' ) ;
115
+ const first = this [ offset ] ;
116
+ const last = this [ offset + 7 ] ;
117
+ if ( first === undefined || last === undefined )
118
+ boundsError ( offset , this . length - 8 ) ;
119
+
120
+ const val = ( first << 24 ) + // Overflow
121
+ this [ ++ offset ] * 2 ** 16 +
122
+ this [ ++ offset ] * 2 ** 8 +
123
+ this [ ++ offset ] ;
124
+ return ( BigInt ( val ) << 32n ) +
125
+ BigInt ( this [ ++ offset ] * 2 ** 24 +
126
+ this [ ++ offset ] * 2 ** 16 +
127
+ this [ ++ offset ] * 2 ** 8 +
128
+ last ) ;
129
+ }
130
+
55
131
function readUIntLE ( offset , byteLength ) {
56
132
if ( byteLength === 6 )
57
133
return readUInt48LE ( this , offset ) ;
@@ -454,6 +530,68 @@ function readDoubleForwards(offset = 0) {
454
530
}
455
531
456
532
// Write integers.
533
+ function writeBigU_Int64LE ( buf , value , offset , min , max ) {
534
+ checkInt ( value , min , max , buf , offset , 7 ) ;
535
+
536
+ let lo = Number ( value & 0xffffffffn ) ;
537
+ buf [ offset ++ ] = lo ;
538
+ lo = lo >> 8 ;
539
+ buf [ offset ++ ] = lo ;
540
+ lo = lo >> 8 ;
541
+ buf [ offset ++ ] = lo ;
542
+ lo = lo >> 8 ;
543
+ buf [ offset ++ ] = lo ;
544
+ let hi = Number ( value >> 32n & 0xffffffffn ) ;
545
+ buf [ offset ++ ] = hi ;
546
+ hi = hi >> 8 ;
547
+ buf [ offset ++ ] = hi ;
548
+ hi = hi >> 8 ;
549
+ buf [ offset ++ ] = hi ;
550
+ hi = hi >> 8 ;
551
+ buf [ offset ++ ] = hi ;
552
+ return offset ;
553
+ }
554
+
555
+ function writeBigUInt64LE ( value , offset = 0 ) {
556
+ return writeBigU_Int64LE ( this , value , offset , 0n , 0xffffffffffffffffn ) ;
557
+ }
558
+
559
+ function writeBigU_Int64BE ( buf , value , offset , min , max ) {
560
+ checkInt ( value , min , max , buf , offset , 7 ) ;
561
+
562
+ let lo = Number ( value & 0xffffffffn ) ;
563
+ buf [ offset + 7 ] = lo ;
564
+ lo = lo >> 8 ;
565
+ buf [ offset + 6 ] = lo ;
566
+ lo = lo >> 8 ;
567
+ buf [ offset + 5 ] = lo ;
568
+ lo = lo >> 8 ;
569
+ buf [ offset + 4 ] = lo ;
570
+ let hi = Number ( value >> 32n & 0xffffffffn ) ;
571
+ buf [ offset + 3 ] = hi ;
572
+ hi = hi >> 8 ;
573
+ buf [ offset + 2 ] = hi ;
574
+ hi = hi >> 8 ;
575
+ buf [ offset + 1 ] = hi ;
576
+ hi = hi >> 8 ;
577
+ buf [ offset ] = hi ;
578
+ return offset + 8 ;
579
+ }
580
+
581
+ function writeBigUInt64BE ( value , offset = 0 ) {
582
+ return writeBigU_Int64BE ( this , value , offset , 0n , 0xffffffffffffffffn ) ;
583
+ }
584
+
585
+ function writeBigInt64LE ( value , offset = 0 ) {
586
+ return writeBigU_Int64LE (
587
+ this , value , offset , - 0x8000000000000000n , 0x7fffffffffffffffn ) ;
588
+ }
589
+
590
+ function writeBigInt64BE ( value , offset = 0 ) {
591
+ return writeBigU_Int64BE (
592
+ this , value , offset , - 0x8000000000000000n , 0x7fffffffffffffffn ) ;
593
+ }
594
+
457
595
function writeUIntLE ( value , offset = 0 , byteLength ) {
458
596
if ( byteLength === 6 )
459
597
return writeU_Int48LE ( this , value , offset , 0 , 0xffffffffffff ) ;
@@ -773,6 +911,14 @@ module.exports = {
773
911
setupBufferJS,
774
912
// Container to export all read write functions.
775
913
readWrites : {
914
+ readBigUInt64LE,
915
+ readBigUInt64BE,
916
+ readBigInt64LE,
917
+ readBigInt64BE,
918
+ writeBigUInt64LE,
919
+ writeBigUInt64BE,
920
+ writeBigInt64LE,
921
+ writeBigInt64BE,
776
922
readUIntLE,
777
923
readUInt32LE,
778
924
readUInt16LE,
0 commit comments