forked from gitchander/asn1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bool.go
65 lines (52 loc) · 1.11 KB
/
bool.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
package der
import (
"reflect"
)
func boolEncode(x bool) []byte {
data := []byte{0}
if x {
data[0] = 0xFF
}
return data
}
func boolDecode(data []byte) (bool, error) {
if len(data) != 1 {
return false, ErrorUnmarshalBytes{data, reflect.Bool}
}
return (data[0] != 0), nil
}
func boolSerialize(v reflect.Value, params ...Parameter) (*Node, error) {
return BoolSerialize(v.Bool(), params...)
}
func boolDeserialize(v reflect.Value, n *Node, params ...Parameter) error {
b, err := BoolDeserialize(n, params...)
if err != nil {
return err
}
v.SetBool(b)
return nil
}
func BoolSerialize(b bool, params ...Parameter) (*Node, error) {
class := CLASS_CONTEXT_SPECIFIC
tag, ok := getTagByParams(params)
if !ok {
class = CLASS_UNIVERSAL
tag = TAG_BOOLEAN
}
n := NewNode(class, tag)
n.SetBool(b)
return n, nil
}
func BoolDeserialize(n *Node, params ...Parameter) (bool, error) {
class := CLASS_CONTEXT_SPECIFIC
tag, ok := getTagByParams(params)
if !ok {
class = CLASS_UNIVERSAL
tag = TAG_BOOLEAN
}
err := CheckNode(n, class, tag)
if err != nil {
return false, err
}
return n.GetBool()
}