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
|
package ber
import (
"bytes"
"reflect"
)
type encodeState struct {
bytes.Buffer
}
/*
func Marshal(v interface{}) ([]byte, error) {
e := &encodeState{}
err := e.marshal(v)
if err != nil {
return nil, err
}
return e.Bytes(), nil
}
*/
func (e *encodeState) marshal(v interface{}) (err error) {
e.reflectValue(reflect.ValueOf(v))
return nil
}
func (e *encodeState) reflectValue(v reflect.Value) {
valueEncoder(v)(e, v)
}
type encoderFunc func(e *encodeState, v reflect.Value)
func valueEncoder(v reflect.Value) encoderFunc {
if !v.IsValid() {
return invalidValueEncoder
}
return newTypeEncoder(v.Type())
}
func invalidValueEncoder(e *encodeState, v reflect.Value) {
e.WriteByte(0)
}
func newTypeEncoder(t reflect.Type) encoderFunc {
switch t.Kind() {
case reflect.Int:
return intEncoder
case reflect.Struct:
return structEncoder
case reflect.Slice:
return sliceEncoder
default:
return unsupportedTypeEncoder
}
}
type UnsupportedTypeError struct {
Type reflect.Type
}
func (e *UnsupportedTypeError) Error() string {
return "BER: unsupported type: " + e.Type.String()
}
func unsupportedTypeEncoder(e *encodeState, v reflect.Value) {
e.error(&UnsupportedTypeError{v.Type()})
}
func (e *encodeState) error(err error) {
panic(err)
}
func intEncoder(e *encodeState, v reflect.Value) {
/*
b := MarshalInt(int(v.Int()))
l := MarshalLen(len(b))
t := MarshalClass(classUniversal, kindPrimitive, tagInteger)
e.Write(t)
e.Write(l)
e.Write(b)
*/
}
func objEncoder(e *encodeState, v reflect.Value) {
/*
b := MarshalOID(v.Interface().(OID))
l := MarshalLen(len(b))
t := MarshalClass(classUniversal, kindPrimitive, tagObjectIdentifier)
e.Write(t)
e.Write(l)
e.Write(b)
*/
}
func structEncoder(e *encodeState, v reflect.Value) {
/*
sube := &encodeState{}
for i := 0; i < v.NumField(); i++ {
sube.reflectValue(v.Field(i))
}
b := sube.Bytes()
l := MarshalLen(len(b))
t := MarshalClass(classUniversal, kindConstructed, tagSequence)
e.Write(t)
e.Write(l)
e.Write(b)
*/
}
func sliceEncoder(e *encodeState, v reflect.Value) {
/*
if v.IsNil() {
e.Write(MarshalClass(classUniversal, kindPrimitive, tagNull))
e.Write([]byte{0})
} else {
e.Write(v.Bytes())
}
*/
}
|