forked from pointfreeco/swift-parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryData.swift
201 lines (183 loc) · 5.5 KB
/
BinaryData.swift
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import Benchmark
import Foundation
import Parsing
/// This benchmark demonstrates how to parse raw data, which is just a collection of `UInt8` values
/// (bytes).
///
/// The data format we parse is the header for DNS packets, as specified
/// [here](https://tools.ietf.org/html/rfc1035#page-26). It consists of 12 bytes, and contains
/// information for 13 fields:
///
/// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/// | ID |
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/// |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/// | QDCOUNT |
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/// | ANCOUNT |
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/// | NSCOUNT |
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
/// | ARCOUNT |
/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
let binaryDataSuite = BenchmarkSuite(name: "BinaryData") { suite in
#if swift(>=5.8)
struct Word16Parser: Parser {
func parse(_ input: inout ArraySlice<UInt8>) throws -> UInt16 {
guard input.count >= 2
else {
struct ParsingError: Error {}
throw ParsingError()
}
let output = UInt16(input[input.startIndex]) + UInt16(input[input.startIndex + 1]) << 8
input.removeFirst(2)
return output
}
}
struct DNSHeaderParser: Parser {
var body: some Parser<ArraySlice<UInt8>, DNSHeader> {
Parse { id, fields1, fields2, counts in
DNSHeader(
id: id,
qr: fields1.qr,
opcode: fields1.opcode,
aa: fields1.aa,
tc: fields1.tc,
rd: fields1.rd,
ra: fields2.ra,
z: fields2.z,
rcode: fields2.rcode,
qdcount: counts.qd,
ancount: counts.an,
nscount: counts.ns,
arcount: counts.ar
)
} with: {
Word16Parser()
First().map { byte in
(
qr: Bit(rawValue: byte & 0b00000001)!,
opcode: Opcode(rawValue: (byte & 0b00011110) >> 1),
aa: Bit(rawValue: (byte & 0b00100000) >> 5)!,
tc: Bit(rawValue: (byte & 0b01000000) >> 6)!,
rd: Bit(rawValue: (byte & 0b10000000) >> 7)!
)
}
First().map { byte in
(
ra: Bit(rawValue: byte & 0b00000001)!,
z: UInt3(uint8: (byte & 0b00001110) >> 1)!,
rcode: Rcode(rawValue: (byte & 0b11110000) >> 4)
)
}
Parse {
(qd: $0, an: $1, ns: $2, ar: $3)
} with: {
Word16Parser()
Word16Parser()
Word16Parser()
Word16Parser()
}
}
}
}
let input: [UInt8] = [
// header
42, 142,
0b10100011, 0b00110000,
128, 0,
100, 200,
254, 1,
128, 128,
// rest of packet
0xDE, 0xAD, 0xBE, 0xEF,
]
var output: DNSHeader!
var rest: ArraySlice<UInt8>!
suite.benchmark("Parser") {
var input = input[...]
output = try DNSHeaderParser().parse(&input)
rest = input
} tearDown: {
precondition(
output
== DNSHeader(
id: 36_394,
qr: .one,
opcode: .inverseQuery,
aa: .one,
tc: .zero,
rd: .one,
ra: .zero,
z: UInt3(uint8: 0)!,
rcode: .nameError,
qdcount: 128,
ancount: 51_300,
nscount: 510,
arcount: 32_896
)
)
precondition(rest == [0xDE, 0xAD, 0xBE, 0xEF])
}
struct DNSHeader: Equatable {
let id: UInt16
let qr: Bit
let opcode: Opcode
let aa: Bit
let tc: Bit
let rd: Bit
let ra: Bit
let z: UInt3
let rcode: Rcode
let qdcount: UInt16
let ancount: UInt16
let nscount: UInt16
let arcount: UInt16
}
enum Bit: Equatable {
case zero, one
init?(rawValue: UInt8) {
if rawValue == 0 {
self = .zero
} else if rawValue == 1 {
self = .one
} else {
return nil
}
}
}
struct UInt3: Equatable {
let bit0: Bit
let bit1: Bit
let bit2: Bit
init?(uint8: UInt8) {
guard
uint8 & 0b11111000 == 0,
let bit0 = Bit(rawValue: uint8 & 0b001),
let bit1 = Bit(rawValue: uint8 & 0b010),
let bit2 = Bit(rawValue: uint8 & 0b100)
else { return nil }
self.bit0 = bit0
self.bit1 = bit1
self.bit2 = bit2
}
}
struct Rcode: Equatable, RawRepresentable {
let rawValue: UInt8
static let noError = Self(rawValue: 0)
static let formatError = Self(rawValue: 1)
static let serverFailure = Self(rawValue: 2)
static let nameError = Self(rawValue: 3)
static let notImplemented = Self(rawValue: 4)
static let refused = Self(rawValue: 5)
}
struct Opcode: Equatable, RawRepresentable {
let rawValue: UInt8
static let standardQuery = Self(rawValue: 0)
static let inverseQuery = Self(rawValue: 1)
static let status = Self(rawValue: 2)
}
#endif
}