This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
forked from free5gc/tlv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
155 lines (142 loc) · 3.89 KB
/
decode.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
// Copyright 2019 Communication Service/Software Laboratory, National Chiao Tung University (free5gc.org)
//
// SPDX-License-Identifier: Apache-2.0
package tlv
import (
"bytes"
"encoding"
"encoding/binary"
"errors"
"fmt"
"reflect"
"strconv"
)
func Unmarshal(b []byte, v interface{}) error {
return decodeValue(b, v)
}
func decodeValue(b []byte, v interface{}) error {
value := reflect.ValueOf(v)
if unmarshaler, ok := value.Interface().(encoding.BinaryUnmarshaler); ok {
err := unmarshaler.UnmarshalBinary(b)
return err
}
value = reflect.Indirect(value)
valueType := reflect.TypeOf(value.Interface())
switch value.Kind() {
case reflect.Int8:
tmp := int64(int8(b[0]))
value.SetInt(tmp)
case reflect.Int16:
tmp := int64(int16(binary.BigEndian.Uint16(b)))
value.SetInt(tmp)
case reflect.Int32:
tmp := int64(int32(binary.BigEndian.Uint32(b)))
value.SetInt(tmp)
case reflect.Int64:
tmp := int64(binary.BigEndian.Uint64(b))
value.SetInt(tmp)
case reflect.Int:
tmp := int64(binary.BigEndian.Uint64(b))
value.SetInt(tmp)
case reflect.Uint8:
tmp := uint64(b[0])
value.SetUint(tmp)
case reflect.Uint16:
tmp := uint64(binary.BigEndian.Uint16(b))
value.SetUint(tmp)
case reflect.Uint32:
tmp := uint64(binary.BigEndian.Uint32(b))
value.SetUint(tmp)
case reflect.Uint64:
tmp := binary.BigEndian.Uint64(b)
value.SetUint(tmp)
case reflect.Uint:
tmp := binary.BigEndian.Uint64(b)
value.SetUint(tmp)
case reflect.String:
value.SetString(string(b))
case reflect.Ptr:
if value.IsNil() {
value.Set(reflect.New(value.Type().Elem()))
}
if err := decodeValue(b, value.Interface()); err != nil {
return err
}
case reflect.Struct:
var tlvFragment fragments
if tlvFragmentTmp, err := parseTLV(b); err != nil {
return err
} else {
tlvFragment = tlvFragmentTmp
}
for i := 0; i < value.NumField(); i++ {
fieldValue := value.Field(i)
fieldType := valueType.Field(i)
tag, hasTLV := fieldType.Tag.Lookup("tlv")
if !hasTLV {
return errors.New("field " + fieldType.Name + " need tag `tlv`")
}
tagVal, err := strconv.Atoi(tag)
if err != nil {
return fmt.Errorf("invalid tlv tag \"%s\", need to be decimal number", tag)
}
if len(tlvFragment[tagVal]) == 0 {
continue
}
if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
} else if fieldValue.Kind() == reflect.Slice && fieldValue.IsNil() {
fieldValue.Set(reflect.MakeSlice(fieldValue.Type(), 0, 1))
}
for _, buf := range tlvFragment[tagVal] {
if fieldValue.Kind() != reflect.Ptr {
fieldValue = fieldValue.Addr()
}
err = decodeValue(buf, fieldValue.Interface())
if err != nil {
return err
}
}
}
case reflect.Slice:
if value.IsNil() {
value.Set(reflect.MakeSlice(value.Type(), 0, 1))
}
if valueType.Elem().Kind() == reflect.Uint8 {
value.SetBytes(b)
} else if valueType.Elem().Kind() == reflect.Ptr || valueType.Elem().Kind() == reflect.Struct ||
isNumber(valueType.Elem()) {
elemValue := reflect.New(valueType.Elem())
if err := decodeValue(b, elemValue.Interface()); err != nil {
return err
}
value.Set(reflect.Append(value, elemValue.Elem()))
} else {
return errors.New("value type `Slice of " + valueType.String() + "` is not support decode")
}
}
return nil
}
func parseTLV(b []byte) (fragments, error) {
tlvFragment := make(fragments)
buffer := bytes.NewBuffer(b)
var tag uint16
var length uint16
for {
if err := binary.Read(buffer, binary.BigEndian, &tag); err != nil {
fmt.Printf("Binary Read error: %v", err)
}
if err := binary.Read(buffer, binary.BigEndian, &length); err != nil {
fmt.Printf("Binary Read error: %v", err)
}
value := make([]byte, length)
if _, err := buffer.Read(value); err != nil {
return nil, err
}
tlvFragment.Add(int(tag), value)
if buffer.Len() == 0 {
break
}
}
return tlvFragment, nil
}