-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtree.go
1364 lines (1119 loc) · 31.1 KB
/
subtree.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dirsyn
/*
subtree.go implements the RFC3672 SubtreeSpecification.
*/
import (
ber "github.com/go-asn1-ber/asn1-ber"
)
/*
SubtreeSpecification implements the Subtree Specification construct.
At present, instances of this type are not ASN.1 encode-friendly due
to the use of an interface type for [Refinement] instances. This is
because Go's [encoding/asn1] package does not play nicely with such
types.
A zero instance of this type is equal to "{}" when represented as a
string value, which is a valid default value when populated for an
entry's "[subtreeSpecification]" attribute type instance within a DIT.
From [§ 2.1 of RFC 3672]:
SubtreeSpecification ::= SEQUENCE {
base [0] LocalName DEFAULT { },
COMPONENTS OF ChopSpecification,
specificationFilter [4] Refinement OPTIONAL }
LocalName ::= RDNSequence
ChopSpecification ::= SEQUENCE {
specificExclusions [1] SET OF CHOICE {
chopBefore [0] LocalName,
chopAfter [1] LocalName } OPTIONAL,
minimum [2] BaseDistance DEFAULT 0,
maximum [3] BaseDistance OPTIONAL }
BaseDistance ::= INTEGER (0 .. MAX)
Refinement ::= CHOICE {
item [0] OBJECT-CLASS.&id,
and [1] SET OF Refinement,
or [2] SET OF Refinement,
not [3] Refinement }
From [Appendix A of RFC 3672]:
SubtreeSpecification = "{" [ sp ss-base ]
[ sep sp ss-specificExclusions ]
[ sep sp ss-minimum ]
[ sep sp ss-maximum ]
[ sep sp ss-specificationFilter ]
sp "}"
ss-base = id-base msp LocalName
ss-specificExclusions = id-specificExclusions msp SpecificExclusions
ss-minimum = id-minimum msp BaseDistance
ss-maximum = id-maximum msp BaseDistance
ss-specificationFilter = id-specificationFilter msp Refinement
BaseDistance = INTEGER-0-MAX
From [§ 6 of RFC 3642]:
LocalName = RDNSequence
RDNSequence = dquote *SafeUTF8Character dquote
INTEGER-0-MAX = "0" / positive-number
positive-number = non-zero-digit *decimal-digit
sp = *%x20 ; zero, one or more space characters
msp = 1*%x20 ; one or more space characters
sep = [ "," ]
OBJECT-IDENTIFIER = numeric-oid / descr
numeric-oid = oid-component 1*( "." oid-component )
oid-component = "0" / positive-number
[Appendix A of RFC 3672]: https://datatracker.ietf.org/doc/html/rfc3672#appendix-A
[subtreeSpecification]: https://datatracker.ietf.org/doc/html/rfc3672#section-2.3
[§ 2.1 of RFC 3672]: https://datatracker.ietf.org/doc/html/rfc3672#section-2.1
[§ 6 of RFC 3642]: https://datatracker.ietf.org/doc/html/rfc3642#section-6
*/
type SubtreeSpecification struct {
Base LocalName `asn1:"tag:0,default:"`
// COMPONENTS OF chopSpecification
ChopSpecification
SpecificationFilter Refinement `asn1:"tag:4,optional"`
}
/*
SubtreeSpecification returns an instance of [SubtreeSpecification]
alongside an error.
If the input is nil, the default [SubtreeSpecification] (e.g.: "{}")
is returned.
If the input is a string, an attempt to marshal the value is made. If
the string is zero, this is equivalent to providing nil.
If the input is a *[ber.Packet] instance, it is unmarshaled into the
return instance of [SubtreeSpecification].
Any errors found will result in the return of an invalid [Filter] instance.
*/
func (r RFC3672) SubtreeSpecification(x any) (ss SubtreeSpecification, err error) {
var raw string
switch tv := x.(type) {
case nil:
return
case *ber.Packet:
ss, err = unmarshalSubtreeSpecificationBER(tv)
return
default:
if raw, err = assertString(x, 0, "Subtree Specification"); err != nil {
return
} else if raw == "" {
return // no error
}
}
if err = checkSubtreeEncaps(raw); err != nil {
return
}
raw = trimS(raw[1 : len(raw)-1])
var ranges map[string][]int = make(map[string][]int, 0)
var pos int
if begin := stridx(raw, `base `); begin != -1 {
var end int
begin += 5
if ss.Base, end, err = subtreeBase(raw[begin:]); err != nil {
return
}
pos += begin
end += pos + 1
ranges[`base`] = []int{begin, end}
}
if begin := stridx(raw, `specificExclusions `); begin != -1 {
var end int
begin += 19
if ss.ChopSpecification.Exclusions, end, err = subtreeExclusions(raw, begin); err != nil {
return
}
end = begin + end
ranges[`specificExclusions`] = []int{begin, end}
}
if begin := stridx(raw, `minimum `); begin != -1 {
var end int
begin += 8
if ss.ChopSpecification.Minimum, end, err = subtreeMinMax(raw, begin); err != nil {
return
}
end = begin + end
ranges[`minimum`] = []int{begin, end}
}
if begin := stridx(raw, `maximum `); begin != -1 {
var end int
begin += 8
if ss.ChopSpecification.Maximum, end, err = subtreeMinMax(raw, begin); err != nil {
return
}
end = begin + end
ranges[`maximum`] = []int{begin, end}
}
if begin, end := ss.processSpecFilter(raw); begin > -1 {
ranges[`specificationFilter`] = []int{begin, end}
}
return
}
func (r *SubtreeSpecification) processSpecFilter(raw string) (begin, end int) {
end = -1
if begin = stridx(raw, `specificationFilter `); begin != -1 {
begin += 20
var err error
if r.SpecificationFilter, err = subtreeRefinement(raw, begin); err == nil {
end = begin + len(raw) - begin
}
}
return
}
func checkSubtreeEncaps(raw string) (err error) {
if raw[0] != '{' || raw[len(raw)-1] != '}' {
err = errorTxt("SubtreeSpecification {} encapsulation error")
}
return
}
/*
BER returns the BER encoding of the receiver instance alongside an error.
To decode the return *[ber.Packet], pass it to [RFC3672.SubtreeSpecification]
as the input value.
*/
func (r SubtreeSpecification) BER() (*ber.Packet, error) {
if r.IsZero() {
return ber.NewSequence(`SubtreeSpecification`), nil
}
packet := ber.NewSequence(`SubtreeSpecification`)
if !r.Base.IsZero() {
packet.AppendChild(ber.NewString(
ber.ClassContext,
ber.TypeConstructed,
ber.Tag(0),
r.Base.String(),
`base`))
}
if !r.ChopSpecification.IsZero() {
child, err := r.ChopSpecification.BER()
if err != nil {
return nil, err
}
packet.AppendChild(child)
}
// SpecFilter is an interface, check nil.
if r.SpecificationFilter != nil {
child, err := r.SpecificationFilter.BER()
if err != nil {
return nil, err
}
packet.AppendChild(child)
}
return packet, nil
}
/*
SpecificExclusions implements the chopSpecification specificExclusions ASN.1
SET OF CHOICE component.
From [Appendix A of RFC 3672]:
SpecificExclusions = "{" [ sp SpecificExclusion *( "," sp SpecificExclusion ) ] sp "}"
[Appendix A of RFC 3672]: https://datatracker.ietf.org/doc/html/rfc3672#appendix-A
*/
type SpecificExclusions []SpecificExclusion
func (r SpecificExclusions) Len() int {
return len(r)
}
/*
SpecificExclusion implements the chopSpecification specificExclusion component.
From [§ 2.1 of RFC3672]:
LocalName ::= RDNSequence
SET OF CHOICE {
chopBefore [0] LocalName,
chopAfter [1] LocalName }
From [Appendix A of RFC 3672]:
SpecificExclusion = chopBefore / chopAfter
chopBefore = id-chopBefore ":" LocalName
chopAfter = id-chopAfter ":" LocalName
id-chopBefore = %x63.68.6F.70.42.65.66.6F.72.65 ; "chopBefore"
id-chopAfter = %x63.68.6F.70.41.66.74.65.72 ; "chopAfter"
[Appendix A of RFC 3672]: https://datatracker.ietf.org/doc/html/rfc3672#appendix-A
[§ 2.1 of RFC 3672]: https://datatracker.ietf.org/doc/html/rfc3672#section-2.1
*/
type SpecificExclusion struct {
ChopBefore LocalName `asn1:"tag:0"`
ChopAfter LocalName `asn1:"tag:1"`
}
/*
ChopSpecification implements the chopSpecification component of an
instance of [SubtreeSpecification].
*/
type ChopSpecification struct {
Exclusions SpecificExclusions `asn1:"tag:1,optional"`
Minimum BaseDistance `asn1:"tag:2,default:0"`
Maximum BaseDistance `asn1:"tag:3,optional"`
}
func (r ChopSpecification) IsZero() bool {
return len(r.Exclusions) == 0 &&
r.Minimum == 0 &&
r.Maximum == 0
}
func (r ChopSpecification) BER() (*ber.Packet, error) {
if r.IsZero() {
return nil, errorTxt("Nil ChopSpecification, cannot BER encode")
}
packet := ber.NewSequence(`ChopSpecification`)
if r.Exclusions.Len() > 0 {
child, err := r.Exclusions.BER()
if err != nil {
return nil, err
}
packet.AppendChild(child)
}
packet.AppendChild(ber.NewInteger(
ber.ClassContext,
ber.TypeConstructed,
ber.Tag(2),
int(r.Minimum),
`minimum`))
packet.AppendChild(ber.NewInteger(
ber.ClassContext,
ber.TypeConstructed,
ber.Tag(3),
int(r.Maximum),
`maximum`))
return packet, nil
}
/*
BaseDistance implements the integer value of a minimum and/or maximum
[SubtreeSpecification] depth refinement parameter. An instance of this
type for either use case indicates the subordinate entry depth "range"
whose contents are subject to the influence of the [SubtreeSpecification]
bearing a non-zero value.
A zero instance of this type, unsurprisingly, is zero (0) and indicates
no depth refinement is in force for the respective administrative area.
*/
type BaseDistance int
/*
LocalName implements an "RDNSequence" per [§ 6 of RFC 3642].
Instances of this type may be found within the "base" of a [SubtreeSpecification]
instance, as well as the "name" of a [SpecificExclusion], and are used to describe
an indicated entry set present at, or within, a given administrative area.
A zero instance of this type is equivalent to a null DN, which normally indicates
that the given administrative area is defined by the current subentry.
[§ 6 of RFC 3642]: https://datatracker.ietf.org/doc/html/rfc3642#section-6
*/
type LocalName string
/*
String returns the string representation of the receiver instance.
*/
func (r LocalName) String() string {
return string(r)
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r LocalName) IsZero() bool {
return r == ``
}
func (r SpecificExclusions) IsZero() bool {
return len(r) == 0
}
/*
String returns the string representation of the receiver instance.
*/
func (r SpecificExclusions) String() string {
if len(r) == 0 {
return `{ }`
}
var _s []string
for i := 0; i < len(r); i++ {
_s = append(_s, r[i].String())
}
return `{ ` + join(_s, `, `) + ` }`
}
func (r SpecificExclusions) BER() (*ber.Packet, error) {
if r.IsZero() {
return nil, errorTxt("Nil Exclusions; cannot BER encode")
}
packet := ber.Encode(ber.ClassContext,
ber.TypeConstructed,
ber.Tag(1),
nil,
`specificExclusions`)
for i := 0; i < r.Len(); i++ {
child, err := r[i].BER()
if err != nil {
return nil, err
}
packet.AppendChild(child)
}
return packet, nil
}
func (r SpecificExclusion) IsZero() bool {
return &r == nil
}
/*
String returns the string literal "before" or "after" as the selected
ASN.1 CHOICE. The determination is made based upon non-zeroness of the
respective [LocalName] value. A zero string is returned if the instance
is invalid.
*/
func (r SpecificExclusion) Choice() (se string) {
if chopB := r.ChopBefore.String(); chopB != "" {
se = `before`
} else if chopA := r.ChopAfter.String(); chopA != "" {
se = `after`
}
return
}
/*
String returns the string representation of the receiver instance.
*/
func (r SpecificExclusion) String() (s string) {
choice := r.Choice()
if choice == "before" {
s = `chopBefore ` + `"` + r.ChopBefore.String() + `"`
} else if choice == "after" {
s = `chopAfter ` + `"` + r.ChopAfter.String() + `"`
}
return
}
func (r SpecificExclusion) BER() (*ber.Packet, error) {
if r.IsZero() || r.Choice() == "" {
return nil, errorTxt("Nil SpecificExclusion, cannot BER encode")
}
var (
tag uint64
val string
desc string
)
if r.Choice() == `after` {
tag = uint64(1)
desc = `chopAfter`
val = r.ChopAfter.String()
} else {
desc = `chopBefore`
val = r.ChopBefore.String()
}
return ber.NewString(
ber.ClassContext,
ber.TypeConstructed,
ber.Tag(tag),
val,
desc), nil
}
func subtreeExclusions(raw string, begin int) (excl SpecificExclusions, end int, err error) {
end = -1
if raw[begin] != '{' {
err = errorTxt("Bad exclusion encapsulation")
return
}
var pos int
if pos, end, err = deconstructExclusions(raw, begin); err != nil {
return
}
values := fields(raw[pos:end])
for i := 0; i < len(values); i += 2 {
var ex SpecificExclusion
if !strInSlice(values[i], []string{`chopBefore`, `chopAfter`}) {
err = errorTxt("Unexpected key '" + values[i] + "'")
break
}
after := values[i] == `chopAfter`
localName := trim(trimR(values[i+1], `,`), `"`)
if err = isSafeUTF8(localName); err == nil {
if after {
ex.ChopAfter = LocalName(localName)
} else {
ex.ChopBefore = LocalName(localName)
}
excl = append(excl, ex)
}
}
return
}
func deconstructExclusions(raw string, begin int) (pos, end int, err error) {
pos = -1
if idx := stridx(raw[begin:], `chop`); idx != -1 {
var (
before int = -1
after int = -1
)
if hasPfx(raw[begin+idx+4:], `Before`) {
before = begin + idx
}
if hasPfx(raw[begin+idx+4:], `After`) {
after = begin + idx
}
if after == -1 && before > after {
pos = before
} else if before == -1 && before < after {
pos = after
}
}
if pos == -1 {
err = errorTxt("No chop directive found in value")
return
}
for i, char := range raw[pos:] {
switch char {
case '}':
end = pos + i
break
}
}
return
}
func subtreeMinMax(raw string, begin int) (minmax BaseDistance, end int, err error) {
end = -1
var (
max string
m int
)
for i := 0; i < len(raw[begin:]); i++ {
if isDigit(rune(raw[begin+i])) {
max += string(raw[begin+i])
continue
}
break
}
if m, err = atoi(max); err == nil {
minmax = BaseDistance(m)
end = len(max)
}
return
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r SubtreeSpecification) IsZero() bool {
return r.Base == "" &&
r.ChopSpecification.IsZero() &&
r.SpecificationFilter == nil
}
/*
String returns the string representation of the receiver instance.
*/
func (r SubtreeSpecification) String() (s string) {
if r.IsZero() {
s = `{}`
return
}
var _s []string
if len(r.Base) > 0 {
_s = append(_s, `base `+`"`+string(r.Base)+`"`)
}
if x := r.ChopSpecification.Exclusions; len(x) > 0 {
_s = append(_s, `specificExclusions `+x.String())
}
if r.ChopSpecification.Minimum > 0 {
_s = append(_s, `minimum `+itoa(int(r.ChopSpecification.Minimum)))
}
if r.ChopSpecification.Maximum > 0 {
_s = append(_s, `maximum `+itoa(int(r.ChopSpecification.Maximum)))
}
if r.SpecificationFilter != nil {
x := r.SpecificationFilter.String()
_s = append(_s, `specificationFilter `+x)
}
s = `{` + join(_s, `, `) + `}`
return
}
/*
Refinement implements [Appendix A of RFC 3672], and serves as the
"SpecificationFilter" optionally found within a Subtree Specification.
It is qualified through instances of:
- [ItemRefinement]
- [AndRefinement]
- [OrRefinement]
- [NotRefinement]
From [Appendix A of RFC 3672]:
Refinement = item / and / or / not
item = id-item ":" OBJECT-IDENTIFIER
and = id-and ":" Refinements
or = id-or ":" Refinements
not = id-not ":" Refinement
Refinements = "{" [ sp Refinement *( "," sp Refinement ) ] sp "}"
id-item = %x69.74.65.6D ; "item"
id-and = %x61.6E.64 ; "and"
id-or = %x6F.72 ; "or"
id-not = %x6E.6F.74 ; "not"
From [ITU-T Rec. X.501 clause 12.3.5]:
Refinement ::= CHOICE {
item [0] OBJECT-CLASS.&id,
and [1] SET SIZE (1..MAX) OF Refinement,
or [2] SET SIZE (1..MAX) OF Refinement,
not [3] Refinement,
... }
[ITU-T Rec. X.501 clause 12.3.5]: https://www.itu.int/rec/T-REC-X.501
[Appendix A of RFC 3672]: https://datatracker.ietf.org/doc/html/rfc3672#appendix-A
*/
type Refinement interface {
// BER returns the BER encoding of the receiver
// instance alongside an error.
BER() (*ber.Packet, error)
// Index returns the Nth slice index found within
// the receiver instance. This is only useful if
// the receiver is an AndRefinement or OrRefinement
// Refinement qualifier type instance.
Index(int) Refinement
// IsZero returns a Boolean value indicative of
// a nil receiver state.
IsZero() bool
// String returns the string representation of
// the receiver instance.
String() string
// Choice returns the string CHOICE "name" of the
// receiver instance. Use of this method is merely
// intended as a convenient alternative to type
// assertion checks.
Choice() string
// Len returns the integer length of the receiver
// instance. This is only useful if the receiver is
// an AndRefinement or OrRefinement Refinement
// qualifier type instance.
Len() int
// differentiate Refinement from other interfaces
isRefinement()
}
/*
AndRefinement implements slices of [Refinement], all of which are expected to
evaluate as true during processing.
Instances of this type qualify the [Refinement] interface type.
*/
type AndRefinement []Refinement
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r AndRefinement) IsZero() bool {
return &r == nil
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r OrRefinement) IsZero() bool {
return &r == nil
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r NotRefinement) IsZero() bool {
return r.Refinement == nil
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r ItemRefinement) IsZero() bool {
return string(r) == ""
}
/*
String returns the string representation of the receiver instance.
*/
func (r AndRefinement) String() (s string) {
if !r.IsZero() {
var parts []string
for _, ref := range r {
parts = append(parts, ref.String())
}
s = "and:{" + join(parts, ",") + "}"
}
return
}
/*
Type returns the string literal "and" as the ASN.1 CHOICE.
*/
func (r AndRefinement) Choice() string {
return "and"
}
/*
Len returns the integer length of the receiver instance.
*/
func (r AndRefinement) Len() int {
return len(r)
}
/*
Index returns the Nth slice index found within the receiver instance.
*/
func (r AndRefinement) Index(idx int) (x Refinement) {
rl := r.Len()
x = invalidRefinement{}
if 0 <= idx && idx < rl {
x = r[idx]
}
return
}
/*
OrRefinement implements slices of [Refinement], at least one of which is
expected to evaluate as true during processing.
Instances of this type qualify the [Refinement] interface type.
*/
type OrRefinement []Refinement
/*
String returns the string representation of the receiver instance.
*/
func (r OrRefinement) String() (s string) {
if !r.IsZero() {
var parts []string
for _, ref := range r {
parts = append(parts, ref.String())
}
s = "or:{" + join(parts, ",") + "}"
}
return
}
/*
Type returns the string literal "or" as the ASN.1 CHOICE.
*/
func (r OrRefinement) Choice() string {
return "or"
}
/*
Len returns the integer length of the receiver instance.
*/
func (r OrRefinement) Len() int {
return len(r)
}
/*
Index returns the Nth slice index found within the receiver instance.
*/
func (r OrRefinement) Index(idx int) (x Refinement) {
rl := r.Len()
x = invalidRefinement{}
if 0 <= idx && idx < rl {
x = r[idx]
}
return
}
/*
NotRefinement implements a negated, recursive instance of [Refinement].
Normally during processing, instances of this type are processed first
when present among other qualifiers as siblings (slices), such as with
[AndRefinement] and [OrRefinement] instances.
Instances of this type qualify the [Refinement] interface type.
*/
type NotRefinement struct {
Refinement
}
/*
String returns the string representation of the receiver instance.
*/
func (r NotRefinement) String() string {
if r.IsZero() {
return ``
}
return "not:" + r.Refinement.String()
}
/*
Type returns the string literal "not" as the ASN.1 CHOICE.
*/
func (r NotRefinement) Choice() string {
return "not"
}
/*
Len returns the integer length of the receiver instance.
*/
func (r NotRefinement) Len() (l int) {
if !r.IsZero() {
l = r.Refinement.Len()
}
return
}
/*
Index returns the Nth slice index found within the receiver instance.
*/
func (r NotRefinement) Index(idx int) (x Refinement) {
x = invalidRefinement{}
if !r.IsZero() {
x = r.Refinement.Index(idx)
}
return
}
func (r OrRefinement) isRefinement() {}
func (r AndRefinement) isRefinement() {}
func (r NotRefinement) isRefinement() {}
func (r ItemRefinement) isRefinement() {}
type invalidRefinement struct{}
func (r invalidRefinement) isRefinement() {}
func (r invalidRefinement) String() string { return `` }
func (r invalidRefinement) IsZero() bool { return false }
func (r invalidRefinement) Len() int { return 0 }
func (r invalidRefinement) Index(_ int) Refinement { return invalidRefinement{} }
func (r invalidRefinement) Choice() string { return `invalid` }
func (r invalidRefinement) BER() (*ber.Packet, error) {
return nil, errorTxt("Nil Refinement, cannot BER encode")
}
/*
ItemRefinement implements the core ("atom") value type to be used in
[Refinement] statements, and appears in [AndRefinement], [OrRefinement]
and [NotRefinement] [Refinement] qualifier type instances.
This is the only "tangible" type in the "specificationFilter" formula,
as all other types simply act as contextual "envelopes" meant to,
ultimately, store instances of this type.
Instances of this type qualify the [Refinement] interface type.
*/
type ItemRefinement string
/*
String returns the string representation of the receiver instance.
*/
func (r ItemRefinement) String() (s string) {
if !r.IsZero() {
s = `item:` + string(r)
}
return
}
/*
Type returns the string literal "item" as the ASN.1 CHOICE.
*/
func (r ItemRefinement) Choice() string {
return "item"
}
/*
Len always returns the integer 1 (one). This method only exists to satisfy
Go's interface signature requirements.
*/
func (r ItemRefinement) Len() int {
return 1
}
/*
Index returns the receiver instance of [Refinement]. This method only
exists to satisfy Go's interface signature requirement.
*/
func (r ItemRefinement) Index(_ int) Refinement {
return r
}
func subtreeBase(x any) (base LocalName, end int, err error) {
end = -1
var raw string
if raw, err = assertString(x, 1, "Subtree Base"); err != nil {
return
}
// FIXME - extend spec to allow single quotes?
if raw[0] != '"' {
err = errorTxt("Missing encapsulation (\") for LocalName")
return
}
for i := 1; i < len(raw) && end == -1; i++ {
switch char := rune(raw[i]); char {
case '"':
end = i
break
}
}
if err = isSafeUTF8(raw[1:end]); err == nil {
base = LocalName(raw[1:end])
}
return
}
func subtreeRefinement(x any, begin ...int) (ref Refinement, err error) {
var input string
if input, err = assertString(x, 1, "Specification Filter"); err != nil {
return
}
if len(begin) > 0 {
input = trimS(input[begin[0]:])
} else {
input = trimS(input)
}
if hasPfx(input, "item:") {
ref, err = parseItem(input)
} else if hasPfx(input, "and:") {
ref, err = parseAnd(input)
} else if hasPfx(input, "or:") {
ref, err = parseOr(input)
} else if hasPfx(input, "not:") {
ref, err = parseNot(input)
} else {
err = errorTxt("invalid refinement: " + input)
}
return
}
func parseItem(input string) (Refinement, error) {
parts := splitN(input, ":", 2)