@@ -34,10 +34,6 @@ const {
34
34
swap16 : _swap16 ,
35
35
swap32 : _swap32 ,
36
36
swap64 : _swap64 ,
37
- writeDoubleBE : _writeDoubleBE ,
38
- writeDoubleLE : _writeDoubleLE ,
39
- writeFloatBE : _writeFloatBE ,
40
- writeFloatLE : _writeFloatLE ,
41
37
kMaxLength,
42
38
kStringMaxLength
43
39
} = process . binding ( 'buffer' ) ;
@@ -85,6 +81,12 @@ const constants = Object.defineProperties({}, {
85
81
}
86
82
} ) ;
87
83
84
+ // Temporary buffers to convert numbers.
85
+ const float32Array = new Float32Array ( 1 ) ;
86
+ const uInt8Float32Array = new Uint8Array ( float32Array . buffer ) ;
87
+ const float64Array = new Float64Array ( 1 ) ;
88
+ const uInt8Float64Array = new Uint8Array ( float64Array . buffer ) ;
89
+
88
90
Buffer . poolSize = 8 * 1024 ;
89
91
var poolSize , poolOffset , allocPool ;
90
92
@@ -1297,12 +1299,16 @@ Buffer.prototype.readFloatLE = function(offset, noAssert) {
1297
1299
return toFloat ( this . readUInt32LE ( offset , noAssert ) ) ;
1298
1300
} ;
1299
1301
1302
+ function checkOOB ( buffer , offset , ext ) {
1303
+ if ( offset + ext > buffer . length )
1304
+ throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
1305
+ }
1306
+
1300
1307
1301
1308
function checkInt ( buffer , value , offset , ext , max , min ) {
1302
1309
if ( value > max || value < min )
1303
1310
throw new errors . RangeError ( 'ERR_INVALID_OPT_VALUE' , 'value' , value ) ;
1304
- if ( offset + ext > buffer . length )
1305
- throw new errors . RangeError ( 'ERR_INDEX_OUT_OF_RANGE' ) ;
1311
+ checkOOB ( buffer , offset , ext ) ;
1306
1312
}
1307
1313
1308
1314
@@ -1520,49 +1526,83 @@ Buffer.prototype.writeInt32BE = function writeInt32BE(value, offset, noAssert) {
1520
1526
return offset + 4 ;
1521
1527
} ;
1522
1528
1523
-
1524
- Buffer . prototype . writeFloatLE = function writeFloatLE ( val , offset , noAssert ) {
1529
+ function writeDoubleForwards ( val , offset , noAssert ) {
1525
1530
val = + val ;
1526
1531
offset = offset >>> 0 ;
1527
1532
if ( ! noAssert )
1528
- _writeFloatLE ( this , val , offset ) ;
1529
- else
1530
- _writeFloatLE ( this , val , offset , true ) ;
1531
- return offset + 4 ;
1532
- } ;
1533
-
1533
+ checkOOB ( this , offset , 8 ) ;
1534
+
1535
+ float64Array [ 0 ] = val ;
1536
+ this [ offset ++ ] = uInt8Float64Array [ 0 ] ;
1537
+ this [ offset ++ ] = uInt8Float64Array [ 1 ] ;
1538
+ this [ offset ++ ] = uInt8Float64Array [ 2 ] ;
1539
+ this [ offset ++ ] = uInt8Float64Array [ 3 ] ;
1540
+ this [ offset ++ ] = uInt8Float64Array [ 4 ] ;
1541
+ this [ offset ++ ] = uInt8Float64Array [ 5 ] ;
1542
+ this [ offset ++ ] = uInt8Float64Array [ 6 ] ;
1543
+ this [ offset ++ ] = uInt8Float64Array [ 7 ] ;
1544
+ return offset ;
1545
+ }
1534
1546
1535
- Buffer . prototype . writeFloatBE = function writeFloatBE ( val , offset , noAssert ) {
1547
+ function writeDoubleBackwards ( val , offset , noAssert ) {
1536
1548
val = + val ;
1537
1549
offset = offset >>> 0 ;
1538
1550
if ( ! noAssert )
1539
- _writeFloatBE ( this , val , offset ) ;
1540
- else
1541
- _writeFloatBE ( this , val , offset , true ) ;
1542
- return offset + 4 ;
1543
- } ;
1544
-
1551
+ checkOOB ( this , offset , 8 ) ;
1552
+
1553
+ float64Array [ 0 ] = val ;
1554
+ this [ offset ++ ] = uInt8Float64Array [ 7 ] ;
1555
+ this [ offset ++ ] = uInt8Float64Array [ 6 ] ;
1556
+ this [ offset ++ ] = uInt8Float64Array [ 5 ] ;
1557
+ this [ offset ++ ] = uInt8Float64Array [ 4 ] ;
1558
+ this [ offset ++ ] = uInt8Float64Array [ 3 ] ;
1559
+ this [ offset ++ ] = uInt8Float64Array [ 2 ] ;
1560
+ this [ offset ++ ] = uInt8Float64Array [ 1 ] ;
1561
+ this [ offset ++ ] = uInt8Float64Array [ 0 ] ;
1562
+ return offset ;
1563
+ }
1545
1564
1546
- Buffer . prototype . writeDoubleLE = function writeDoubleLE ( val , offset , noAssert ) {
1565
+ function writeFloatForwards ( val , offset , noAssert ) {
1547
1566
val = + val ;
1548
1567
offset = offset >>> 0 ;
1549
1568
if ( ! noAssert )
1550
- _writeDoubleLE ( this , val , offset ) ;
1551
- else
1552
- _writeDoubleLE ( this , val , offset , true ) ;
1553
- return offset + 8 ;
1554
- } ;
1555
-
1569
+ checkOOB ( this , offset , 4 ) ;
1570
+
1571
+ float32Array [ 0 ] = val ;
1572
+ this [ offset ++ ] = uInt8Float32Array [ 0 ] ;
1573
+ this [ offset ++ ] = uInt8Float32Array [ 1 ] ;
1574
+ this [ offset ++ ] = uInt8Float32Array [ 2 ] ;
1575
+ this [ offset ++ ] = uInt8Float32Array [ 3 ] ;
1576
+ return offset ;
1577
+ }
1556
1578
1557
- Buffer . prototype . writeDoubleBE = function writeDoubleBE ( val , offset , noAssert ) {
1579
+ function writeFloatBackwards ( val , offset , noAssert ) {
1558
1580
val = + val ;
1559
1581
offset = offset >>> 0 ;
1560
1582
if ( ! noAssert )
1561
- _writeDoubleBE ( this , val , offset ) ;
1562
- else
1563
- _writeDoubleBE ( this , val , offset , true ) ;
1564
- return offset + 8 ;
1565
- } ;
1583
+ checkOOB ( this , offset , 4 ) ;
1584
+
1585
+ float32Array [ 0 ] = val ;
1586
+ this [ offset ++ ] = uInt8Float32Array [ 3 ] ;
1587
+ this [ offset ++ ] = uInt8Float32Array [ 2 ] ;
1588
+ this [ offset ++ ] = uInt8Float32Array [ 1 ] ;
1589
+ this [ offset ++ ] = uInt8Float32Array [ 0 ] ;
1590
+ return offset ;
1591
+ }
1592
+
1593
+ // Check endianness.
1594
+ float32Array [ 0 ] = - 1 ;
1595
+ if ( uInt8Float32Array [ 3 ] === 0 ) { // Big endian.
1596
+ Buffer . prototype . writeFloatLE = writeFloatBackwards ;
1597
+ Buffer . prototype . writeFloatBE = writeFloatForwards ;
1598
+ Buffer . prototype . writeDoubleLE = writeDoubleBackwards ;
1599
+ Buffer . prototype . writeDoubleBE = writeDoubleForwards ;
1600
+ } else { // Small endian.
1601
+ Buffer . prototype . writeFloatLE = writeFloatForwards ;
1602
+ Buffer . prototype . writeFloatBE = writeFloatBackwards ;
1603
+ Buffer . prototype . writeDoubleLE = writeDoubleForwards ;
1604
+ Buffer . prototype . writeDoubleBE = writeDoubleBackwards ;
1605
+ }
1566
1606
1567
1607
function swap ( b , n , m ) {
1568
1608
const i = b [ n ] ;
0 commit comments