-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
169 lines (158 loc) · 2.85 KB
/
utils.go
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
package goabnf
import "strings"
// atob converts str to byte given the base.
func atob(str, base string) byte {
switch base {
case "B", "b":
return bintob(str)
case "D", "d":
return dectob(str)
case "X", "x":
return hextob(str)
}
// This won't get hit as the ABNF grammar defines only the
// previous bases.
panic("invalid base")
}
func bintob(str string) byte {
str = strings.TrimLeft(str, "0")
if len(str) > 8 { // 8 = ceil(log(base, 2^|us-ascii|)), base=2
panic(&ErrTooLargeNumeral{
Base: "b",
Value: str,
})
}
out := 0
for i := 0; i < len(str); i++ {
c := str[i]
cv := 0
switch c {
case '0':
cv = 0
case '1':
cv = 1
default:
panic("invalid bit: " + string(c))
}
out += cv * pow(2, len(str)-i-1)
}
return byte(out)
}
func dectob(str string) byte {
str = strings.TrimLeft(str, "0")
if len(str) > 3 || (len(str) == 3 && (str[0] == '1' && (str[1] > '2' || (str[1] == '2' && str[2] > '7')))) {
panic(&ErrTooLargeNumeral{
Base: "d",
Value: str,
})
}
out := 0
for i := 0; i < len(str); i++ {
c := str[i]
cv := 0
switch c {
case '0':
cv = 0
case '1':
cv = 1
case '2':
cv = 2
case '3':
cv = 3
case '4':
cv = 4
case '5':
cv = 5
case '6':
cv = 6
case '7':
cv = 7
case '8':
cv = 8
case '9':
cv = 9
default:
panic("invalid dec: " + string(c))
}
out += cv * pow(10, len(str)-i-1)
}
return byte(out)
}
func hextob(str string) byte {
str = strings.TrimLeft(str, "0")
if len(str) > 2 || (len(str) == 2 && str[0] > '7') { // 2 = ceil(log(base, 2^|us-ascii|)), base=16
panic(&ErrTooLargeNumeral{
Base: "x",
Value: str,
})
}
out := 0
for i := 0; i < len(str); i++ {
c := str[i]
cv := 0
switch c {
case '0':
cv = 0
case '1':
cv = 1
case '2':
cv = 2
case '3':
cv = 3
case '4':
cv = 4
case '5':
cv = 5
case '6':
cv = 6
case '7':
cv = 7
case '8':
cv = 8
case '9':
cv = 9
case 'A', 'a':
cv = 10
case 'B', 'b':
cv = 11
case 'C', 'c':
cv = 12
case 'D', 'd':
cv = 13
case 'E', 'e':
cv = 14
case 'F', 'f':
cv = 15
default:
panic("invalid hex: " + string(c))
}
out += cv * pow(16, len(str)-i-1)
}
return byte(out)
}
func pow(v, e int) int {
if e == 0 {
return 1
}
for i := 1; i < e; i++ {
v *= v
}
return v
}
// GetRule returns the rule by the given rulename, whether
// it is a core rule or present in the grammar, or nil if not found.
// It validates the RFC 5234 Section 2.1 "rule names are case insensitive".
func GetRule(rulename string, rulemap map[string]*Rule) *Rule {
if rule := getRuleIn(rulename, coreRules); rule != nil {
return rule
}
return getRuleIn(rulename, rulemap)
}
func getRuleIn(rulename string, rulemap map[string]*Rule) *Rule {
for _, rule := range rulemap {
if strings.EqualFold(rulename, rule.Name) {
return rule
}
}
return nil
}