-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathberbitstring.go
133 lines (118 loc) · 2.57 KB
/
berbitstring.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package asn1ber
import (
"bytes"
"errors"
"fmt"
"io"
)
type BerBitString struct {
value []byte
numBits int
}
var bitStringTag = NewBerTag(UNIVERSAL_CLASS, PRIMITIVE, BIT_STRING_TAG)
func NewBerBitString(value []bool) (*BerBitString, error) {
if value == nil {
return nil, errors.New("value cannot be null")
}
numBits := len(value)
b := &BerBitString{
numBits: numBits,
value: make([]byte, (numBits+7)/8),
}
for i := 0; i < numBits; i++ {
if value[i] {
b.value[i/8] = (byte)(b.value[i/8] | (1 << (7 - (i % 8))))
}
}
return b, nil
}
func (b *BerBitString) GetTag() *BerTag {
return bitStringTag
}
func (b *BerBitString) getValueAsBooleans() []bool {
if b.value == nil {
return nil
}
vals := make([]bool, b.numBits)
for i := 0; i < b.numBits; i++ {
vals[i] = (b.value[i/8] & (1 << (7 - (i % 8)))) > 0
}
return vals
}
func (b *BerBitString) Encode(reversedWriter io.Writer, withTagList ...bool) (int, error) {
var withTag bool
if len(withTagList) > 0 {
withTag = withTagList[0]
} else {
withTag = true
}
for i := len(b.value) - 1; i >= 0; i-- {
_, err := reversedWriter.Write([]byte{b.value[i]})
if err != nil {
return 0, err
}
}
codeLength := len(b.value) + 1
n, err := EncodeLength(codeLength, reversedWriter)
if err != nil {
return 0, err
}
codeLength += n
if withTag {
n, err = bitStringTag.Encode(reversedWriter)
codeLength += n
}
return codeLength, err
}
func (b *BerBitString) 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 := bitStringTag.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-1)
bx := []byte{0}
n, err = input.Read(bx)
unusedBits := int(bx[0])
if err != nil {
return codeLength, err
}
if unusedBits > 7 {
return codeLength, errors.New(fmt.Sprintf("Number of unused bits should be less than 8, found: %d", unusedBits))
}
b.numBits = len(b.value)*8 - unusedBits
if len(b.value) > 0 {
_, err = io.ReadFull(input, b.value)
if err != nil {
return codeLength, err
}
}
codeLength += len(b.value) + 1
return codeLength, nil
}
func (b *BerBitString) S() string {
var buffer bytes.Buffer
for _, bit := range b.getValueAsBooleans() {
if bit {
buffer.WriteString("1")
} else {
buffer.WriteString("0")
}
}
return buffer.String()
}