Skip to content

Commit 239d8bd

Browse files
committed
fixed binaryParsers for small negativ values
WARNING: bigint support is not correctly working for really big values. If the value of a integer gets big the number gets fuzzy in javascript. This is not a limitation of this library. If you want to handle bigint with the exact value, get it as string and do not calculate things with it!
1 parent f8962fd commit 239d8bd

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/binaryParsers.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
var parseBits = function(data, bits, offset, callback) {
1+
var parseBits = function(data, bits, offset, invert, callback) {
22
offset = offset || 0;
3+
invert = invert || false;
34
callback = callback || function(lastValue, newValue, bits) { return (lastValue * Math.pow(2, bits)) + newValue; };
45
var offsetBytes = offset >> 3;
56

7+
var inv = function(value) {
8+
if (invert) {
9+
return ~value & 0xff;
10+
}
11+
12+
return value;
13+
};
14+
615
// read first (maybe partial) byte
716
var mask = 0xff;
817
var firstBits = 8 - (offset % 8);
@@ -17,19 +26,19 @@ var parseBits = function(data, bits, offset, callback) {
1726

1827
var result = 0;
1928
if ((offset % 8) + bits >= 8) {
20-
result = callback(0, data[offsetBytes] & mask, firstBits);
29+
result = callback(0, inv(data[offsetBytes]) & mask, firstBits);
2130
}
2231

2332
// read bytes
2433
var bytes = (bits + offset) >> 3;
2534
for (var i = offsetBytes + 1; i < bytes; i++) {
26-
result = callback(result, data[i], 8);
35+
result = callback(result, inv(data[i]), 8);
2736
}
2837

2938
// bits to read, that are not a complete byte
3039
var lastBits = (bits + offset) % 8;
3140
if (lastBits > 0) {
32-
result = callback(result, data[bytes] >> (8 - lastBits), lastBits);
41+
result = callback(result, inv(data[bytes]) >> (8 - lastBits), lastBits);
3342
}
3443

3544
return result;
@@ -60,7 +69,7 @@ var parseFloatFromBits = function(data, precisionBits, exponentBits) {
6069
return lastValue;
6170
};
6271

63-
var mantissa = parseBits(data, precisionBits, exponentBits + 1, parsePrecisionBits);
72+
var mantissa = parseBits(data, precisionBits, exponentBits + 1, false, parsePrecisionBits);
6473

6574
// special cases
6675
if (exponent == (Math.pow(2, exponentBits + 1) - 1)) {
@@ -81,23 +90,23 @@ var parseBool = function(value) {
8190

8291
var parseInt16 = function(value) {
8392
if (parseBits(value, 1) == 1) {
84-
return -1 * (Math.pow(2, 15) - parseBits(value, 15, 1));
93+
return -1 * (parseBits(value, 15, 1, true) + 1);
8594
}
8695

8796
return parseBits(value, 15, 1);
8897
};
8998

9099
var parseInt32 = function(value) {
91100
if (parseBits(value, 1) == 1) {
92-
return -1 * (Math.pow(2, 31) - parseBits(value, 31, 1));
101+
return -1 * (parseBits(value, 31, 1, true) + 1);
93102
}
94103

95104
return parseBits(value, 31, 1);
96105
};
97106

98107
var parseInt64 = function(value) {
99108
if (parseBits(value, 1) == 1) {
100-
return -1 * (Math.pow(2, 63) - parseBits(value, 63, 1));
109+
return -1 * (parseBits(value, 63, 1, true) + 1);
101110
}
102111

103112
return parseBits(value, 63, 1);

0 commit comments

Comments
 (0)