-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
291 lines (276 loc) · 7.67 KB
/
Copy pathparse.go
File metadata and controls
291 lines (276 loc) · 7.67 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package uid
import (
"encoding/base32"
"encoding/base64"
"encoding/binary"
"slices"
"strings"
)
// Parse attempts to parse `src` into a UUID and returns the parsed UUID and `true` on success.
// On failure, Parse returns the Nil UUID and `false`.
//
//nolint:mnd // locality of behavior
func Parse(src string) (UUID, bool) {
ln := len(src)
switch ln {
case 36:
return parseCanonical(src)
case 16:
return parseBytes([]byte(src))
case 26:
return parseCompact32(src)
case 22:
return parseCompact64(src)
case 38, 28, 24:
return parseWrapped(src)
}
return UUID{}, false
}
// parseWrapped strips json quotes (any form) or ms-style braces (canonical only) and re-parses.
func parseWrapped(src string) (UUID, bool) {
ln := len(src)
if src[0] == '"' && src[ln-1] == '"' || ln == 38 && src[0] == '{' && src[ln-1] == '}' {
return Parse(src[1 : ln-1])
}
return UUID{}, false
}
//nolint:cyclop // parsers...
func canonicalV(s string) Version {
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
return versionBad // malformed
}
varR := rune(s[19])
switch rune(s[14]) {
case '4':
switch varR {
case '8', '9', 'A', 'a', 'b', 'B':
return Version4
}
case '7':
switch varR {
case '8', '9', 'A', 'a', 'b', 'B':
return Version7
}
case '0':
if s == NilCanonical {
return VersionNil
}
case 'f', 'F':
if strings.ToLower(s) == MaxCanonical {
return VersionMax
}
}
return versionBad
}
func bytesV(b []byte) Version {
vrsn := Version(b[6] >> 4) //nolint:mnd // lob
switch vrsn { //nolint:exhaustive // golf
case Version4, Version7:
if Variant(b[8]>>6) == Variant9562 { //nolint:mnd // lob
return vrsn
}
case VersionNil:
if slices.Equal(b, bytesNil[:]) {
return VersionNil
}
case VersionMax:
if slices.Equal(b, bytesMax[:]) {
return VersionMax
}
}
return versionBad
}
func ncn64V(s string) Version {
varR := rune(s[21])
switch rune(s[0]) {
case 'E':
switch varR {
case 'I', 'J', 'K', 'L':
return Version4
}
case 'H':
switch varR {
case 'I', 'J', 'K', 'L':
return Version7
}
case 'A':
if s == NilCompact64 {
return VersionNil
}
case 'P':
if s == MaxCompact64 {
return VersionMax
}
}
return versionBad
}
func ncn32V(s string) Version {
varR := rune(s[25])
switch rune(s[0]) {
case 'E', 'e':
switch varR {
case 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L':
return Version4
}
case 'H', 'h':
switch varR {
case 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L':
return Version7
}
case 'A', 'a':
if strings.EqualFold(s, NilCompact32) {
return VersionNil
}
case 'P', 'p':
if strings.EqualFold(s, MaxCompact32) {
return VersionMax
}
}
return versionBad
}
//nolint:gochecknoglobals // ref
var canonOffsets = [16]byte{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}
func parseCanonical(src string) (UUID, bool) {
switch canonicalV(src) { //nolint:exhaustive // golf
case versionBad:
return UUID{}, false
case VersionNil:
return UUID{bytesNil}, true
case VersionMax:
return UUID{bytesMax}, true
}
// valid version+variant but not nil/max
tgt := [16]byte{}
for i, x := range canonOffsets {
if !c2b(&tgt[i], src[x], src[x+1]) {
return UUID{}, false
}
}
return UUID{tgt}, true
}
func parseBytes(b []byte) (UUID, bool) {
switch bytesV(b) { //nolint:exhaustive // golf
case versionBad:
return UUID{}, false
case VersionNil:
return UUID{bytesNil}, true
case VersionMax:
return UUID{bytesMax}, true
}
// valid version+variant but not nil/max
var out UUID
copy(out.b[:], b)
return out, true
}
func parseCompact32(src string) (UUID, bool) {
v := ncn32V(src)
if v == versionBad {
return UUID{}, false
}
// s/c nil
if v == VersionNil {
return UUID{bytesNil}, true
}
// s/c max
if v == VersionMax {
return UUID{bytesMax}, true
}
// not Nil or Max, uppercase ASCII byte-wise (unicode folding can change length) and decode with padding.
// decoder rejects non-alphabet (incl. non-ASCII) bytes.
var buf [26]byte
copy(buf[:25], src[1:])
buf[25] = 'A'
for i, c := range buf {
if 'a' <= c && c <= 'z' {
buf[i] = c - ('a' - 'A')
}
}
var out UUID
n, err := b32decoder.Decode(out.b[:], buf[:])
if err != nil || n != 16 { // decoder swallows \r\n: short output means invalid input
return UUID{}, false
}
out.b[15] <<= 1 // unshift bookend
unshift(&out.b, uint32(v))
return out, true
}
func parseCompact64(src string) (UUID, bool) {
v := ncn64V(src)
if v == versionBad {
return UUID{}, false
}
// s/c nil
if v == VersionNil {
return UUID{bytesNil}, true
}
// s/c max
if v == VersionMax {
return UUID{bytesMax}, true
}
// not Nil or Max, decode with padding. bookend src[21] is validated uppercase; decoder rejects non-alphabet
// (incl. non-ASCII) bytes.
var buf [22]byte
copy(buf[:21], src[1:])
buf[21] = 'A'
var out UUID
n, err := base64.RawURLEncoding.Decode(out.b[:], buf[:])
if err != nil || n != 16 { // decoder swallows \r\n: short output means invalid input
return UUID{}, false
}
out.b[15] <<= 2
unshift(&out.b, uint32(v))
return out, true
}
//nolint:mnd // locality of behavior
func unshift(tgt *[16]byte, version uint32) {
version &= 0xf
ints := [4]uint32{
binary.BigEndian.Uint32(tgt[0:4]),
binary.BigEndian.Uint32(tgt[4:8]),
binary.BigEndian.Uint32(tgt[8:12]),
binary.BigEndian.Uint32(tgt[12:16]),
}
variant := (ints[3] & 0xf0) << 24
ints[3] >>= 8
ints[3] |= (ints[2] & 0xff) << 24
ints[2] >>= 8
ints[2] |= ((ints[1] & 0xf) << 24) | variant
ints[1] = (ints[1] & 0xffff0000) | (version << 12) | ((ints[1] >> 4) & 0xfff)
binary.BigEndian.PutUint32(tgt[0:4], ints[0])
binary.BigEndian.PutUint32(tgt[4:8], ints[1])
binary.BigEndian.PutUint32(tgt[8:12], ints[2])
binary.BigEndian.PutUint32(tgt[12:16], ints[3])
}
// c2b sets v to the possibly hex decoded value of characters c1 and c2. returns false if v is invalid.
//
//nolint:mnd // lob
func c2b(b *byte, c1, c2 byte) bool {
c1, c2 = c2h[c1], c2h[c2]
*b = (c1 << 4) | c2
return c1|c2 != 0xff
}
//nolint:gochecknoglobals // ref
var (
// map ascii -> hex (0xff is invalid). lookup table method borrowed from google/uuid.
c2h = [256]byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, // valid '0'-'9'
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf, // valid 'A'-'F'
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf, // valid 'a'-'f'
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
}
b32decoder = base32.StdEncoding.WithPadding(base32.NoPadding)
)