-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathastToBinary.js
More file actions
64 lines (53 loc) · 1.2 KB
/
astToBinary.js
File metadata and controls
64 lines (53 loc) · 1.2 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
import { Buffer } from 'buffer';
export const astToBinary = (ast, bytes = []) => {
const {subvalues, suffix} = ast
const {length} = subvalues
bytes.push(
length & 0xff,
(length >> 8 & 0xff),
// assume rest is 0000
// (length >> 16 & 0xff),
// (length >> 32 & 0xff),
)
for (const {prefix, value} of subvalues) {
const buf = Buffer.from(prefix, 'utf8')
const {length} = buf
bytes.push(
length & 0xff,
(length >> 8 & 0xff),
(length >> 16 & 0xff),
(length >> 24 & 0xff),
// assume rest is 0000
)
for (let b of buf) {
bytes.push(b)
}
astToBinary(value, bytes)
}
{
const buf = Buffer.from(suffix, 'utf8')
const {length} = buf
bytes.push(
length & 0xff,
(length >> 8 & 0xff),
(length >> 16 & 0xff),
(length >> 24 & 0xff),
// assume rest is 0000
)
for (let b of buf) {
bytes.push(b)
}
}
return bytes
}
// BPC = 2
// BPL = 4
// Jevko:
// BPC bytes: Subvalue Count (SC) -- number of elements
// (SC*Subvalue)
// BPL bytes: Suffix Length (SL) -- in bytes
// SL bytes: Suffix (S)
// Subvalue:
// BPL bytes: Prefix Length (PL) -- in bytes
// PL bytes: Prefix (P)
// (Jevko)