-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathreadBig.ts
More file actions
66 lines (48 loc) · 2.28 KB
/
readBig.ts
File metadata and controls
66 lines (48 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L758
const ERR_BUFFER_OUT_OF_BOUNDS = () => new Error('Attempt to access memory outside buffer bounds')
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L968
const ERR_INVALID_ARG_TYPE = (name: string, expected: string, actual: any) =>
new Error(`The "${name}" argument must be of type ${expected}. Received ${actual}`)
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L1262
const ERR_OUT_OF_RANGE = (str: string, range: string, received: number) =>
new Error(`The value of "${str} is out of range. It must be ${range}. Received ${received}`)
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/validators.js#L127-L130
function validateNumber(value: any, name: string) {
if (typeof value !== 'number') throw ERR_INVALID_ARG_TYPE(name, 'number', value)
}
// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L68-L80
function boundsError(value: number, length: number) {
if (Math.floor(value) !== value) {
validateNumber(value, 'offset')
throw ERR_OUT_OF_RANGE('offset', 'an integer', value)
}
if (length < 0) throw ERR_BUFFER_OUT_OF_BOUNDS()
throw ERR_OUT_OF_RANGE('offset', `>= 0 and <= ${length}`, value)
}
// This function works with react-native >= 0.66.1
export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint {
buffer = buffer.slice(offset)
const bufferLenght = Math.min(buffer.length, 8)
let tot: number = 0
const maxIndex = Math.log2(Number.MAX_VALUE)
for(let index = 0; index < bufferLenght; index++) {
const value = buffer[index]
const exponent = 8*index
if(exponent > maxIndex)
throw new Error("out of range")
const addend = value * Math.pow(2, exponent)
tot += addend
// console.log("index: " + index + ", buffer value: " + value + ", decimal value: " + addend + ", tot: " + tot)
}
return BigInt(tot)
}
// This function works with react-native >= 0.66.1
export function readBigInt64LE(buffer: Buffer, offset = 0): bigint {
const resultUnsigned = readBigUInt64LE(buffer, offset)
const FFFFFFFFFFFFFFFF = BigInt(2**64 - 1);
if(buffer.length >= 8) {
if(buffer[7] >= 128)
return resultUnsigned - FFFFFFFFFFFFFFFF;
}
return resultUnsigned
}