/
ber.go
188 lines (161 loc) · 3.56 KB
/
ber.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package ber
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
)
type byteReader interface {
io.Reader
ReadByte() (byte, error)
}
type TypeError struct {
Tag Tag
Kind reflect.Kind
}
func (t *TypeError) Error() string {
return fmt.Sprintf("incompatible types %s and %s", t.Tag, t.Kind)
}
func parseInteger(v reflect.Value, b []byte) error {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
default:
return &TypeError{TagInteger, v.Kind()}
}
if len(b) > 8 {
return errors.New("value does not fit in a int64")
}
var n int64
for i, v := range b {
shift := uint((len(b) - i - 1) * 8)
if i == 0 {
if v&0x80 != 0 {
n -= 0x80 << shift
v &= 0x7f
}
}
n += int64(v) << shift
}
reflect.Value(v).SetInt(n)
return nil
}
func Unmarshal(data []byte, v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("ber: value must be non nil pointer")
}
elem := rv.Elem()
c, isPrimative, t, data, err := readNext(bytes.NewReader(data))
if err != nil {
return err
}
_, _ = c, isPrimative
switch t {
case TagInteger:
return parseInteger(elem, data)
default:
return fmt.Errorf("unsupported tag: %s", t)
}
}
type reader struct {
*bufio.Reader
}
// the bytes stop being valid
func readNext(r byteReader) (c Class, isPrimative bool, t Tag, data []byte, err error) {
var b byte
if b, err = r.ReadByte(); err != nil {
return
}
i := identifier(b)
c = i.Class()
t = i.Tag()
// check primative or constructed (6th bit)
isPrimative = i&0x20 == 0
if b, err = r.ReadByte(); err != nil {
return
}
// indefinate length
if b == 0x80 {
err = errors.New("ber: indefinate length encodings not supported")
return
}
var n int
if b&0x80 == 0 {
// short form
n = int(b)
} else {
// long form
if n = int(b & 0x7f); n > 4 {
err = errors.New("length octet: long form longer than 4 bytes")
}
length := make([]byte, 4)
if _, err = io.ReadFull(r, length[4-n:]); err != nil {
return
}
n = int(binary.BigEndian.Uint32(length))
}
data = make([]byte, n)
_, err = io.ReadFull(r, data)
return
}
//go:generate stringer -type=Class
type Class byte
const (
ClassUniversal Class = iota
ClassApplication
ClassContextSpecific
ClassPrivate
)
type identifier byte
func (i identifier) Class() Class {
switch i >> 6 {
case 0:
return ClassUniversal
case 1:
return ClassApplication
case 2:
return ClassContextSpecific
default:
return ClassPrivate
}
}
func (i identifier) Tag() Tag {
return Tag(i & 0x1f)
}
//go:generate stringer -type=Tag
type Tag byte
const (
TagEOC Tag = 0x00
TagBoolean Tag = 0x01
TagInteger Tag = 0x02
TagBitString Tag = 0x03
TagOctetString Tag = 0x04
TagNull Tag = 0x05
TagObjectIdentifier Tag = 0x06
TagObjectDescriptor Tag = 0x07
TagExternal Tag = 0x08
TagReal Tag = 0x09
TagEnumerated Tag = 0x0a
TagEmbeddedPDV Tag = 0x0b
TagUTF8String Tag = 0x0c
TagRelativeOID Tag = 0x0d
TagSequence Tag = 0x10
TagSet Tag = 0x11
TagNumericString Tag = 0x12
TagPrintableString Tag = 0x13
TagT61String Tag = 0x14
TagVideotexString Tag = 0x15
TagIA5String Tag = 0x16
TagUTCTime Tag = 0x17
TagGeneralizedTime Tag = 0x18
TagGraphicString Tag = 0x19
TagVisiableString Tag = 0x1A
TagGeneralString Tag = 0x1b
TagUniversalString Tag = 0x1c
TagCharacterString Tag = 0x1d
TagBMPString Tag = 0x1e
TagUseLongForm Tag = 0x1f
)