-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstr.go
84 lines (69 loc) · 2.03 KB
/
cstr.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
package dirsyn
/*
CountryString implements [§ 3.3.4 of RFC 4517]:
CountryString = 2(PrintableCharacter)
From [§ 1.4 of RFC 4512]:
PrintableCharacter = ALPHA / DIGIT / SQUOTE / LPAREN / RPAREN /
PLUS / COMMA / HYPHEN / DOT / EQUALS /
SLASH / COLON / QUESTION / SPACE
PrintableString = 1*PrintableCharacter
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
[§ 3.3.4 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.4
*/
type CountryString string
/*
String returns the string representation of the receiver instance.
*/
func (r CountryString) String() string {
return string(r)
}
/*
IsZero returns a Boolean value indicative of a nil receiver state.
*/
func (r CountryString) IsZero() bool { return len(r) == 0 }
func countryString(x any) (result Boolean) {
_, err := marshalCountryString(x)
result.Set(err == nil)
return
}
/*
CountryString returns an error following an analysis of x in the context of
an [ISO 3166] country code. Note that specific codes -- though syntactically
valid -- should be verified periodically in lieu of significant world events.
[ISO 3166]: https://www.iso.org/iso-3166-country-codes.html
*/
func (r RFC4517) CountryString(x any) (CountryString, error) {
return marshalCountryString(x)
}
func marshalCountryString(x any) (cs CountryString, err error) {
var raw string
switch tv := x.(type) {
case string:
if len(tv) != 2 {
err = errorBadLength("Country String", 0)
return
}
raw = tv
case []byte:
cs, err = marshalCountryString(string(tv))
return
default:
err = errorBadType("Country String")
return
}
if !isUAlpha(rune(raw[0])) || !isUAlpha(rune(raw[1])) {
err = errorTxt("Incompatible characters for Country String: " +
string(raw[0]) + "/" + string(raw[0]))
return
}
var mdata []byte
if mdata, err = asn1m(raw); err == nil {
var testcss CountryString
if _, err = asn1um(mdata, &testcss); err == nil {
if testcss.String() == raw {
cs = CountryString(raw)
}
}
}
return
}