forked from andig/gosml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
62 lines (50 loc) · 1.27 KB
/
status.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
package sml
import "fmt"
func StatusParse(buf *Buffer) (int64, error) {
/*
if (BufOptionalIsSkipped(buf)) {
return 0;
}
int max = 1;
int type = BufGetNextType(buf);
unsigned char byte = BufGetCurrentByte(buf);
Status *status = StatusInit();
status->type = type;
switch (type) {
case TYPEUNSIGNED:
// get maximal size, if not all bytes are used (example: only 6 bytes for a u64)
while (max < ((byte & LENGTHFIELD) - 1)) {
max <<= 1;
}
status->data.status8 = NumberParse(buf, type, max);
status->type |= max;
break;
default:
buf->error = 1;
break;
}
*/
// TODO proper type handling
if skip := BufOptionalIsSkipped(buf); skip {
return 0, nil
}
Debug(buf, "StatusParse")
var max uint8 = 1
var status8 int64
typefield := BufGetNextType(buf)
statusType := typefield
b := BufGetCurrentByte(buf)
if typefield == TYPEUNSIGNED {
// get maximal size, if not all bytes are used (example: only 6 bytes for a u64)
for max < ((b & LENGTHFIELD) - 1) {
max = max << 1
}
if _, err := NumberParse(buf, typefield, int(max)); err != nil {
return 0, err
}
statusType = statusType | max
} else {
return 0, fmt.Errorf("Unexpected type %02x (expected %02x)", typefield, TYPEUNSIGNED)
}
return status8, nil
}