-
Notifications
You must be signed in to change notification settings - Fork 13
/
generate-allowlist.py
executable file
·127 lines (105 loc) · 3.31 KB
/
generate-allowlist.py
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
#!/usr/bin/env python3
import re
import sys
def usage():
sys.stderr.write("Usage: $0 allowlist-config-in allowlist-header-out\n")
sys.stderr.write(" Reads in allowlist config, sorting the contents\n")
sys.stderr.write(" and outputs a header file\n")
sys.exit(-1)
class Error(Exception):
pass
class DuplicateEntry(Error):
def __init__(self, e):
super(Error, self).__init__(
"Multiple entries with matching netfn/cmd found ({})".format(e)
)
class ParseError(Error):
def __init__(self, d):
super(Error, self).__init__("Parse error at: '{}'".format(d))
class entry:
linere = re.compile(
r"(0x[0-9a-f]{2}):(0x[0-9a-f]{2})((:(0x[0-9a-f]{4}))?)\s*((//\s*(.*))?)", # noqa: E501
re.I,
)
def __init__(self, data):
# parse data line into values:
# type 1, two values: netfn, cmd
# type 2, three values: netfn, cmd, channels
try:
m = self.linere.fullmatch(data).groups()
except Exception:
raise ParseError(data)
self.netfn = int(m[0], 16)
self.cmd = int(m[1], 16)
if m[4] is not None:
self.channels = int(m[4], 16)
else:
# if no channel was provided, default to previous behavior, which
# is allow all interfaces, including the system interface (ch 15)
self.channels = 0xFFFF
if m[6] is not None:
self.comment = "// " + m[7]
else:
self.comment = "//"
def __str__(self):
return " ".join(
[
"{",
"0x{0.netfn:02x},".format(self),
"0x{0.cmd:02x},".format(self),
"0x{0.channels:04x}".format(self),
"},",
"{0.comment}".format(self),
]
)
def __lt__(self, other):
if self.netfn == other.netfn:
return self.cmd < other.cmd
return self.netfn < other.netfn
def match(self, other):
return (self.netfn == other.netfn) and (self.cmd == other.cmd)
def parse(config):
entries = []
with open(config) as f:
for line in f:
line = line.strip()
if len(line) == 0 or line[0] == "#":
continue
e = entry(line)
if any([e.match(item) for item in entries]):
d = DuplicateEntry(e)
sys.stderr.write("WARNING: {}\n".format(d))
else:
entries.append(e)
entries.sort()
return entries
def output(entries, hppfile):
lines = [
"#pragma once",
"",
"// AUTOGENERATED FILE; DO NOT MODIFY",
"",
"#include <array>",
"#include <tuple>",
"",
(
"using netfncmd_tuple = std::tuple<unsigned char, unsigned char,"
" unsigned short>;"
),
"",
"constexpr const std::array<netfncmd_tuple, {}> allowlist = ".format(
len(entries)
),
"{{",
]
lines.extend([" {}".format(e) for e in entries])
lines.append("}};\n")
with open(hppfile, "w") as hpp:
hpp.write("\n".join(lines))
if __name__ == "__main__":
if len(sys.argv) != 3:
usage()
config = sys.argv[1]
header = sys.argv[2]
entries = parse(config)
output(entries, header)