-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstring.go
45 lines (34 loc) · 965 Bytes
/
bitstring.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
package asn1
import (
"fmt"
"reflect"
)
type BitString struct {
Bytes []byte
PaddingBits int
}
var bitStringType = reflect.TypeOf(BitString{})
func NewBitString(b []byte, paddingBits int) (BitString, error) {
bitString := BitString{
Bytes: b,
}
if paddingBits > 7 {
return bitString, fmt.Errorf("too many padding bits: expecting <= 7, got: %d", paddingBits)
}
if len(b) == 0 && paddingBits != 0 {
return bitString, fmt.Errorf("empty bit string, but got %d padding bits", paddingBits)
}
// ber does not require padding to be zero-valued
// if paddingBits > 0 && b[len(b) - 1] & ((1 << paddingBits) - 1) != 0 {
// return bitString, fmt.Errorf("Padded bits not zero")
// }
bitString.PaddingBits = paddingBits
return bitString, nil
}
type bitStringEncoder BitString
func (e bitStringEncoder) encode() ([]byte, error) {
buf := make([]byte, len(e.Bytes)+1)
buf[0] = byte(e.PaddingBits)
copy(buf[1:], e.Bytes)
return buf, nil
}