-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnumeric.js
92 lines (78 loc) · 3.03 KB
/
numeric.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const { PartialReadError, Result } = require('../../utils')
function readI64 (buffer, offset) {
if (offset + 8 > buffer.length) { throw new PartialReadError() }
return new Result([buffer.readInt32BE(offset), buffer.readInt32BE(offset + 4)], 8)
}
function writeI64 (value, buffer, offset) {
buffer.writeInt32BE(value[0], offset)
buffer.writeInt32BE(value[1], offset + 4)
return offset + 8
}
function readLI64 (buffer, offset) {
if (offset + 8 > buffer.length) { throw new PartialReadError() }
return new Result([buffer.readInt32LE(offset + 4), buffer.readInt32LE(offset)], 8)
}
function writeLI64 (value, buffer, offset) {
buffer.writeInt32LE(value[0], offset + 4)
buffer.writeInt32LE(value[1], offset)
return offset + 8
}
function readU64 (buffer, offset) {
if (offset + 8 > buffer.length) { throw new PartialReadError() }
return new Result([buffer.readUInt32BE(offset), buffer.readUInt32BE(offset + 4)], 8)
}
function writeU64 (value, buffer, offset) {
buffer.writeUInt32BE(value[0], offset)
buffer.writeUInt32BE(value[1], offset + 4)
return offset + 8
}
function readLU64 (buffer, offset) {
if (offset + 8 > buffer.length) { throw new PartialReadError() }
return new Result([buffer.readUInt32LE(offset + 4), buffer.readUInt32LE(offset)], 8)
}
function writeLU64 (value, buffer, offset) {
buffer.writeUInt32LE(value[0], offset + 4)
buffer.writeUInt32LE(value[1], offset)
return offset + 8
}
const nums = {
'i8': ['readInt8', 'writeInt8', 1],
'u8': ['readUInt8', 'writeUInt8', 1],
'i16': ['readInt16BE', 'writeInt16BE', 2],
'u16': ['readUInt16BE', 'writeUInt16BE', 2],
'i32': ['readInt32BE', 'writeInt32BE', 4],
'u32': ['readUInt32BE', 'writeUInt32BE', 4],
'f32': ['readFloatBE', 'writeFloatBE', 4],
'f64': ['readDoubleBE', 'writeDoubleBE', 8],
'li8': ['readInt8', 'writeInt8', 1],
'lu8': ['readUInt8', 'writeUInt8', 1],
'li16': ['readInt16LE', 'writeInt16LE', 2],
'lu16': ['readUInt16LE', 'writeUInt16LE', 2],
'li32': ['readInt32LE', 'writeInt32LE', 4],
'lu32': ['readUInt32LE', 'writeUInt32LE', 4],
'lf32': ['readFloatLE', 'writeFloatLE', 4],
'lf64': ['readDoubleLE', 'writeDoubleLE', 8]
}
const types = {
i64: [readI64, writeI64, 8, require('../../../ProtoDef/schemas/numeric.json')['i64']],
li64: [readLI64, writeLI64, 8, require('../../../ProtoDef/schemas/numeric.json')['li64']],
u64: [readU64, writeU64, 8, require('../../../ProtoDef/schemas/numeric.json')['u64']],
lu64: [readLU64, writeLU64, 8, require('../../../ProtoDef/schemas/numeric.json')['lu64']]
}
function readIntN (method, size, buffer, offset) {
if (offset + size > buffer.length) throw new PartialReadError()
return new Result(buffer[method](offset), size)
}
function writeIntN (method, value, buffer, offset) {
return buffer[method](value, offset)
}
for (const num in nums) {
const [ bufferReader, bufferWriter, size ] = nums[num]
types[num] = [
readIntN.bind(null, bufferReader, size),
writeIntN.bind(null, bufferWriter),
size,
require('../../../ProtoDef/schemas/numeric.json')[num]
]
}
module.exports = types