-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathberboolean.go
More file actions
94 lines (82 loc) · 1.67 KB
/
berboolean.go
File metadata and controls
94 lines (82 loc) · 1.67 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package asn1ber
import (
"errors"
"io"
"strconv"
)
type BerBoolean struct {
value bool
}
var boolTag = NewBerTag(UNIVERSAL_CLASS, PRIMITIVE, BOOLEAN_TAG)
func NewBerBoolean(v bool) *BerBoolean {
return &BerBoolean{value: v}
}
func (b *BerBoolean) GetValue() bool {
return b.value
}
func (b *BerBoolean) Encode(reversedWriter io.Writer, withTagList ...bool) (int, error) {
var withTag bool
if len(withTagList) > 0 {
withTag = withTagList[0]
} else {
withTag = true
}
codeLength := 1
var bx []byte
if b.value {
bx = []byte{0xFF}
} else {
bx = []byte{0}
}
_, err := reversedWriter.Write(bx)
if err != nil {
return codeLength, err
}
n, err := EncodeLength(codeLength, reversedWriter)
codeLength += n
if err != nil {
return codeLength, err
}
if withTag {
n, err = boolTag.Encode(reversedWriter)
codeLength += n
}
return codeLength, nil
}
func (b *BerBoolean) Decode(input io.Reader, withTagList ...bool) (int, error) {
var withTag bool
if len(withTagList) > 0 {
withTag = withTagList[0]
} else {
withTag = true
}
codeLength := 0
if withTag {
n, err := boolTag.DecodeAndCheck(input)
codeLength += n
if err != nil {
return codeLength, err
}
}
berLength := &BerLength{}
n, err := berLength.Decode(input)
codeLength += n
if err != nil {
return codeLength, err
} else if berLength.Length != 1 {
return codeLength, errors.New("Invalid length for boolean type")
}
nextByte, err := ReadByte(input)
if err != nil {
return codeLength, err
}
codeLength++
b.value = nextByte != 0
return codeLength, nil
}
func (b *BerBoolean) S() string {
return strconv.FormatBool(b.value)
}
func (b *BerBoolean) GetTag() *BerTag {
return boolTag
}