-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbervisiblestring.go
90 lines (78 loc) · 1.93 KB
/
bervisiblestring.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
package asn1ber
import (
"io"
)
type BerVisibleString struct {
value []byte
}
var berVisibleStringTag = NewBerTag(UNIVERSAL_CLASS, PRIMITIVE, VISIBLE_STRING_TAG)
func NewBerVisibleString(value string) *BerVisibleString {
return &BerVisibleString{value: []byte(value)}
}
func (b *BerVisibleString) GetValue() []byte {
return b.value
}
func (b *BerVisibleString) Encode(reversedWriter io.Writer, withTagList ...bool) (int, error) {
return b.EncodeUsingTag(berVisibleStringTag, reversedWriter, withTagList...)
}
func (b *BerVisibleString) Decode(input io.Reader, withTagList ...bool) (int, error) {
return b.DecodeUsingTag(berVisibleStringTag, input, withTagList...)
}
func (b *BerVisibleString) EncodeUsingTag(tag *BerTag, reversedWriter io.Writer, withTagList ...bool) (int, error) {
var withTag bool
if len(withTagList) > 0 {
withTag = withTagList[0]
} else {
withTag = true
}
_, err := reversedWriter.Write(b.value)
if err != nil {
return 0, err
}
codeLength := len(b.value)
if withTag {
n, err := tag.Encode(reversedWriter)
if err != nil {
return 0, err
}
codeLength += n
}
return codeLength, nil
}
func (b *BerVisibleString) DecodeUsingTag(tag *BerTag, 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 := tag.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
}
b.value = make([]byte, berLength.Length)
if berLength.Length != 0 {
_, err = io.ReadFull(input, b.value)
if err != nil {
return codeLength, err
}
codeLength += len(b.value)
}
return codeLength, nil
}
func (b *BerVisibleString) S() string {
return string(b.value)
}
func (b *BerVisibleString) GetTag() *BerTag {
return berVisibleStringTag
}