forked from andig/gosml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.go
100 lines (74 loc) · 1.68 KB
/
shared.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
package sml
import "fmt"
const (
MESSAGEEND = 0x00
TYPEFIELD = 0x70
LENGTHFIELD = 0x0F
ANOTHERTL = 0x80
TYPEOCTETSTRING = 0x00
TYPEBOOLEAN = 0x40
TYPEINTEGER = 0x50
TYPEUNSIGNED = 0x60
TYPELIST = 0x70
OPTIONALSKIPPED = 0x01
)
type Buffer struct {
Bytes []byte
Cursor int
}
func BufGetCurrentByte(buf *Buffer) byte {
return buf.Bytes[buf.Cursor]
}
func BufUpdateBytesRead(buf *Buffer, delta int) {
buf.Cursor += delta
}
func Expect(buf *Buffer, expectedType uint8, expectedLength int) error {
if err := ExpectType(buf, expectedType); err != nil {
return err
}
if length := BufGetNextLength(buf); length != expectedLength {
return fmt.Errorf("Invalid length: %d (expected %d)", length, expectedLength)
}
return nil
}
func ExpectType(buf *Buffer, expectedType uint8) error {
if typefield := BufGetNextType(buf); typefield != expectedType {
return fmt.Errorf("Unexpected type %02x (expected %02x)", typefield, expectedType)
}
return nil
}
func BufGetNextType(buf *Buffer) uint8 {
return BufGetCurrentByte(buf) & TYPEFIELD
}
func BufGetNextLength(buf *Buffer) int {
var length uint8
var list int
b := BufGetCurrentByte(buf)
// not a list
if b&TYPEFIELD != TYPELIST {
list = -1
}
for {
b := BufGetCurrentByte(buf)
length = length << 4
length = length | (b & LENGTHFIELD)
if b&ANOTHERTL != ANOTHERTL {
break
}
// another TL field used
BufUpdateBytesRead(buf, 1)
// not a list
if list != 0 {
list--
}
}
BufUpdateBytesRead(buf, 1)
return int(length) + list
}
func BufOptionalIsSkipped(buf *Buffer) bool {
if BufGetCurrentByte(buf) == OPTIONALSKIPPED {
BufUpdateBytesRead(buf, 1)
return true
}
return false
}