forked from andig/gosml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
102 lines (85 loc) · 2 KB
/
value.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
package sml
import "fmt"
type Value struct {
Typ uint8
DataBytes OctetString
DataBoolean bool
DataInt int64
}
func ValueParse(buf *Buffer) (Value, error) {
/*
if (BufOptionalIsSkipped(buf)) {
return 0;
}
int max = 1;
int type = BufGetNextType(buf);
unsigned char byte = BufGetCurrentByte(buf);
Value *value = ValueInit();
value->type = type;
switch (type) {
case TYPEOCTETSTRING:
value->data.bytes = OctetStringParse(buf);
break;
case TYPEBOOLEAN:
value->data.boolean = BooleanParse(buf);
break;
case TYPEUNSIGNED:
case TYPEINTEGER:
// get maximal size, if not all bytes are used (example: only 6 bytes for a u64)
while (max < ((byte & LENGTHFIELD) - 1)) {
max <<= 1;
}
value->data.uint8 = NumberParse(buf, type, max);
value->type |= max;
break;
default:
buf->error = 1;
break;
}
*/
value := Value{}
if BufOptionalIsSkipped(buf) {
return value, nil
}
Debug(buf, "ValueParse")
typefield := BufGetNextType(buf)
b := BufGetCurrentByte(buf)
max := 1
value.Typ = typefield
var err error
switch typefield {
case TYPEOCTETSTRING:
value.DataBytes, err = OctetStringParse(buf)
if err != nil {
return value, err
}
case TYPEBOOLEAN:
value.DataBoolean, err = BooleanParse(buf)
if err != nil {
return value, err
}
case TYPEUNSIGNED:
// get maximal size, if not all bytes are used (example: only 6 bytes for a u64)
for max < int((b&LENGTHFIELD)-1) {
max = max << 1
}
value.DataInt, err = NumberParse(buf, typefield, max)
if err != nil {
return value, err
}
value.Typ = value.Typ | uint8(max)
case TYPEINTEGER:
// get maximal size, if not all bytes are used (example: only 6 bytes for a u64)
for max < int((b&LENGTHFIELD)-1) {
max = max << 1
}
value.DataInt, err = NumberParse(buf, typefield, max)
if err != nil {
return value, err
}
value.Typ = value.Typ | uint8(max)
default:
return value, fmt.Errorf("Unexpected type %02x", typefield)
}
return value, nil
}