2016-10-06 12:53:48 +02:00
|
|
|
// https://github.com/feross/buffer/blob/master/index.js#L1127
|
2018-12-26 08:13:43 +01:00
|
|
|
function verifuint (value: number, max: number): void {
|
2016-10-06 12:53:48 +02:00
|
|
|
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
|
|
|
|
if (value < 0) throw new Error('specified a negative value for writing an unsigned value')
|
|
|
|
if (value > max) throw new Error('RangeError: value out of range')
|
|
|
|
if (Math.floor(value) !== value) throw new Error('value has a fractional component')
|
|
|
|
}
|
|
|
|
|
2018-12-26 08:13:43 +01:00
|
|
|
export function readUInt64LE (buffer: Buffer, offset: number): number {
|
2018-06-25 08:37:45 +02:00
|
|
|
const a = buffer.readUInt32LE(offset)
|
|
|
|
let b = buffer.readUInt32LE(offset + 4)
|
2014-05-01 11:38:18 +02:00
|
|
|
b *= 0x100000000
|
|
|
|
|
2014-05-31 05:18:57 +02:00
|
|
|
verifuint(b + a, 0x001fffffffffffff)
|
2014-05-01 11:38:18 +02:00
|
|
|
return b + a
|
|
|
|
}
|
|
|
|
|
2018-12-26 08:13:43 +01:00
|
|
|
export function writeUInt64LE (buffer: Buffer, value: number, offset: number): number {
|
2014-05-31 05:18:57 +02:00
|
|
|
verifuint(value, 0x001fffffffffffff)
|
2014-05-01 11:38:18 +02:00
|
|
|
|
|
|
|
buffer.writeInt32LE(value & -1, offset)
|
|
|
|
buffer.writeUInt32LE(Math.floor(value / 0x100000000), offset + 4)
|
2016-10-09 13:53:41 +02:00
|
|
|
return offset + 8
|
2014-05-01 11:38:18 +02:00
|
|
|
}
|
2018-12-29 08:36:36 +01:00
|
|
|
|
|
|
|
export function reverseBuffer (buffer: Buffer): Buffer {
|
|
|
|
if (buffer.length < 1) return buffer
|
|
|
|
let j = buffer.length - 1
|
|
|
|
let tmp = 0
|
|
|
|
for (let i = 0; i < buffer.length / 2; i++) {
|
|
|
|
tmp = buffer[i]
|
|
|
|
buffer[i] = buffer[j]
|
|
|
|
buffer[j] = tmp
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
return buffer
|
|
|
|
}
|