-
Notifications
You must be signed in to change notification settings - Fork 0
/
substr.go
339 lines (281 loc) · 7.33 KB
/
substr.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package dirsyn
/*
SubstringAssertion implements the Substring Assertion.
From [§ 3.3.30 of RFC 4517]:
SubstringAssertion = [ initial ] any [ final ]
initial = substring
any = ASTERISK *(substring ASTERISK)
final = substring
ASTERISK = %x2A ; asterisk ("*")
substring = 1*substring-character
substring-character = %x00-29
/ (%x5C "2A") ; escaped "*"
/ %x2B-5B
/ (%x5C "5C") ; escaped "\"
/ %x5D-7F
/ UTFMB
From [§ 2 of RFC 4515]:
SubstringFilter ::= SEQUENCE {
type AttributeDescription,
-- initial and final can occur at most once
substrings SEQUENCE SIZE (1..MAX) OF substring CHOICE {
initial [0] AssertionValue,
any [1] AssertionValue,
final [2] AssertionValue } }
From [§ 3 of RFC 4515]:
initial = assertionvalue
any = ASTERISK *(assertionvalue ASTERISK)
final = assertionvalue
[§ 2 of RFC 4515]: https://datatracker.ietf.org/doc/html/rfc4515#section-2
[§ 3 of RFC 4515]: https://datatracker.ietf.org/doc/html/rfc4515#section-3
[§ 3.3.30 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.30
*/
type SubstringAssertion struct {
Initial AssertionValue `asn1:"tag:0"`
Any AssertionValue `asn1:"tag:1"`
Final AssertionValue `asn1:"tag:2"`
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r SubstringAssertion) IsZero() bool {
return len(r.Initial) == 0 &&
len(r.Any) == 0 &&
len(r.Final) == 0
}
/*
String returns the string representation of the receiver instance.
*/
func (r SubstringAssertion) String() (s string) {
Any := func() string {
if len(r.Any) > 0 {
return `*` + r.Any.String() + `*`
}
return `*`
}
if !r.IsZero() {
bld := newStrBuilder()
if len(r.Initial) > 0 {
bld.WriteString(r.Initial.String())
bld.WriteString(Any())
if len(r.Final) > 0 {
bld.WriteString(r.Final.String())
}
} else if len(r.Final) > 0 {
bld.WriteString(Any())
bld.WriteString(r.Final.String())
} else {
// If a star is the only value,
// don't save anything.
bld.WriteString(Any())
}
s = bld.String()
}
return
}
/*
SubstringAssertion returns an error following an analysis of x in the
context of a Substring Assertion.
*/
func (r RFC4517) SubstringAssertion(x any) (SubstringAssertion, error) {
return marshalSubstringAssertion(x)
}
func substringAssertion(x any) (result Boolean) {
_, err := marshalSubstringAssertion(x)
result.Set(err == nil)
return
}
func marshalSubstringAssertion(z any) (ssa SubstringAssertion, err error) {
var x string
if x, err = assertSubstringAssertion(z); err != nil {
return
}
x = trimS(x)
f := hasPfx(x, `*`)
l := hasSfx(x, `*`)
if cntns(x, `**`) {
err = errorTxt("SubstringAssertion cannot contain consecutive asterisks")
return
} else if !cntns(x, `*`) {
err = errorTxt("SubstringAssertion requires at least one asterisk")
return
}
if f && l {
// Any only
ssa.Any, err = substrProcess1(x)
} else if f && !l {
// Final + Any
ssa.Any, ssa.Final, err = substrProcess2(x)
} else if !f && l {
// Initial + Any
ssa.Initial, ssa.Any, err = substrProcess3(x)
} else if !f && !l {
ssa.Initial, ssa.Any, ssa.Final, err = substrProcess4(x)
}
return
}
func substrProcess1(x string) (a AssertionValue, err error) {
z := x[1 : len(x)-1]
sp := split(z, `*`)
asp := join(sp, ``)
if err = assertionValueRunes(asp); err == nil {
a = AssertionValue(z)
}
return
}
func substrProcess2(x string) (a, f AssertionValue, err error) {
z := x[1:]
sp := split(z, `*`)
for idx := 0; idx < len(sp); idx++ {
if err = assertionValueRunes(sp[idx]); err != nil {
return
}
}
if len(sp) == 1 {
f = AssertionValue(sp[len(sp)-1])
} else {
a = AssertionValue(join(sp[:len(sp)-1], `*`))
f = AssertionValue(sp[len(sp)-1])
}
return
}
func substrProcess3(x string) (i, a AssertionValue, err error) {
z := x[:len(x)-1]
sp := split(z, `*`)
for idx := 0; idx < len(sp); idx++ {
if err = assertionValueRunes(sp[idx]); err != nil {
return
}
}
if len(sp) == 1 {
i = AssertionValue(sp[0])
} else {
i = AssertionValue(sp[0])
a = AssertionValue(join(sp[1:], `*`))
}
return
}
func substrProcess4(x string) (i, a, f AssertionValue, err error) {
sp := split(x, `*`)
for idx := 0; idx < len(sp); idx++ {
if err = assertionValueRunes(sp[idx]); err != nil {
return
}
}
switch len(sp) {
case 0, 1:
err = errorTxt("SubstringAssertion requires at least one asterisk")
case 2:
i = AssertionValue(sp[0])
f = AssertionValue(sp[1])
default:
i = AssertionValue(sp[0])
a = AssertionValue(join(sp[1:len(sp)-1], `*`))
f = AssertionValue(sp[len(sp)-1])
}
return
}
func assertSubstringAssertion(x any) (value string, err error) {
switch tv := x.(type) {
case string:
value = tv
case []byte:
value = string(tv)
case SubstringAssertion:
value = tv.String()
default:
err = errorBadType("SubstringAssertion")
}
return
}
/*
caseIgnoreSubstringsMatch implements [§ 4.2.13 of RFC 4517].
OID: 2.5.13.4.
[§ 4.2.13 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.13
*/
func caseIgnoreSubstringsMatch(a, b any) (result Boolean, err error) {
result, err = substringsMatch(a, b, true)
return
}
/*
caseIgnoreSubstringsMatch implements [§ 4.2.6 of RFC 4517].
OID: 2.5.13.7.
[§ 4.2.6 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.6
*/
func caseExactSubstringsMatch(a, b any) (result Boolean, err error) {
result, err = substringsMatch(a, b, false)
return
}
func substringsMatch(a, b any, caseIgnore ...bool) (result Boolean, err error) {
var value string
if value, err = assertString(a, 1, "actual value"); err != nil {
return
}
var B SubstringAssertion
if B, err = marshalSubstringAssertion(b); err != nil {
return
}
caseHandler := func(val string) string { return val }
if len(caseIgnore) > 0 {
if caseIgnore[0] {
caseHandler = lc
}
}
value = caseHandler(value)
if B.Any == nil {
err = errorBadType("Missing SubstringAssertion.Any")
return
}
if B.Initial != nil {
initialStr := caseHandler(string(B.Initial))
if !hasPfx(value, initialStr) {
result.Set(false)
return
}
value = trimPfx(value, initialStr)
}
anyStr := `*` + trim(caseHandler(string(B.Any)), `*`) + `*`
substrings := split(anyStr, "*")
for _, substr := range substrings {
index := stridx(value, substr)
if index == -1 {
result.Set(false)
return
}
value = value[index+len(substr):]
}
if B.Final != nil {
finalStr := caseHandler(string(B.Final))
if !hasSfx(value, finalStr) {
result.Set(false)
return
}
}
result.Set(true)
return
}
func prepareStringListAssertion(a, b any) (str1, str2 string, err error) {
assertSubstringsList := func(x any) (list string, err error) {
var ok bool
var slices []string
if slices, ok = x.([]string); ok {
list = join(slices, ``)
list = repAll(list, `\\`, ``)
list = repAll(list, `$`, ``)
} else {
errorBadType("substringslist")
}
return
}
if str1, err = assertSubstringsList(a); err == nil {
str2, err = assertSubstringsList(b)
}
return
}
func caseIgnoreListSubstringsMatch(a, b any) (result Boolean, err error) {
var str1, str2 string
if str1, str2, err = prepareStringListAssertion(a, b); err == nil {
result, err = caseIgnoreSubstringsMatch(str1, str2)
}
return
}