forked from DefinitelyTyped/DefinitelyTyped
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-parser-tests.ts
128 lines (117 loc) · 3.26 KB
/
binary-parser-tests.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { Parser } from "binary-parser";
// Build an IP packet header Parser
const ipHeader = new Parser()
.endianness("big")
.bit4("version")
.bit4("headerLength")
.uint8("tos")
.uint16("packetLength")
.uint16("id")
.bit3("offset")
.bit13("fragOffset")
.uint8("ttl")
.uint8("protocol")
.uint16("checksum")
.array("src", {
type: "uint8",
length: 4,
})
.array("dst", {
type: "uint8",
length: 4,
});
// Prepare buffer to parse.
const buf = new Buffer("450002c5939900002c06ef98adc24f6c850186d1", "hex");
// Parse buffer and show result
ipHeader.parse(buf);
const parser2 = new Parser()
// Signed 32-bit integer (little endian)
.int32le("a")
// Unsigned 8-bit integer
.uint8("b")
// Signed 16-bit integer (big endian)
.int16be("c");
const parser3 = new Parser()
// 32-bit floating value (big endian)
.floatbe("a")
// 64-bit floating value (little endian)
.doublele("b");
const parser4 = new Parser()
// Statically sized array
.array("data", {
type: "int32",
length: 8,
})
// Dynamically sized array (references another variable)
.uint8("dataLength")
.array("data2", {
type: "int32",
length: "dataLength",
})
// Dynamically sized array (with some calculation)
.array("data3", {
type: "int32",
length: () => 4, // other fields are available through this
})
// Statically sized array
.array("data4", {
type: "int32",
lengthInBytes: 16,
})
// Dynamically sized array (references another variable)
.uint8("dataLengthInBytes")
.array("data5", {
type: "int32",
lengthInBytes: "dataLengthInBytes",
})
// Dynamically sized array (with some calculation)
.array("data6", {
type: "int32",
lengthInBytes: () => 4, // other fields are available through this
})
// Dynamically sized array (with stop-check on parsed item)
.array("data7", {
type: "int32",
readUntil: (item, buffer) => true, // stop when specific item is parsed. buffer can be used to perform a read-ahead.
});
const parser5 = new Parser()
.array("ipv4", {
type: "uint8",
length: "4",
formatter: (arr) => {},
});
const parser6 = new Parser()
.nest("nested", {
type: new Parser()
.array("points", {
type: new Parser()
.uint8("x")
.uint8("y"),
length: 2,
}),
})
.choice("optional", {
tag: "nested.points[0].x",
choices: {
1: new Parser()
.uint8("number"),
},
});
const result = parser6.parse(Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05]));
// See the inferred static types on the IntelliSense.
result.nested.points[0].x;
result.nested.points[1].y;
result.optional.number;
const parser7 = new Parser()
// Signed 64-bit integer
.int64("a")
// Unsigned 64-bit integer
.uint64("b")
// Signed 64-bit integer (little endian)
.int64le("c")
// Signed 64-bit integer (big endian)
.int64be("d")
// Unsigned 64-bit integer (little endian)
.uint64le("e")
// Unsigned 64-bit integer (big endian)
.uint64be("f");