-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.go
More file actions
72 lines (62 loc) · 1.46 KB
/
Copy pathenum.go
File metadata and controls
72 lines (62 loc) · 1.46 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
package syntax
import (
"github.com/go-directory/encoding/asn1"
)
/*
Enumerated implements the ASN.1 ENUMERATED type (tag 10), per [§ 20 of
ITU-T Rec. X.680]:
EnumeratedType ::=
ENUMERATED "{" Enumerations "}"
[§ 20 of ITU-T Rec. X.680]: https://www.itu.int/rec/T-REC-X.680
*/
type Enumerated int
/*
Encode returns an instance of []byte alongside an error following an
attempt to encode the receiver instance as an ASN.1 ENUMERATED value.
*/
func (r Enumerated) Encode() ([]byte, error) {
return asn1.EncodePrimitive(asn1.TagEnumerated, encodeEnum(int(r)))
}
/*
Decode returns an error following an attempt to decode and write
the input enc value to the receiver instance. The encoding must
not be truncated, and must bear the ENUMERATED tag (0x0A).
*/
func (r *Enumerated) Decode(enc []byte) error {
if len(enc) < 2 || enc[0] != asn1.TagEnumerated {
return errEnumDecode
}
l, n := asn1.ReadPrimitiveLength(enc[1:])
if n == 0 || len(enc) < 1+n+l {
return errEnumDecode
}
v := enc[1+n : 1+n+l]
if len(v) == 0 {
return errEnumDecode
}
x := 0
for i := 0; i < len(v); i++ {
x = (x << 8) | int(v[i])
}
*r = Enumerated(x)
return nil
}
func encodeEnum(n int) []byte {
if n == 0 {
return []byte{0x00}
}
var tmp [8]byte
i := len(tmp)
v := n
for v != 0 && i > 0 {
i--
tmp[i] = byte(v)
v >>= 8
}
out := tmp[i:]
if out[0]&0x80 != 0 {
out = append([]byte{0x00}, out...)
}
return out
}
var errEnumDecode = asn1Error("invalid ENUMERATED encoding")