-
Notifications
You must be signed in to change notification settings - Fork 4
/
decoder.go
94 lines (77 loc) · 1.71 KB
/
decoder.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
package goloxi
import (
"encoding/binary"
)
type Decoder struct {
buffer []byte
offset int
baseOffset int
}
func NewDecoder(data []byte) *Decoder {
return &Decoder{
buffer: data,
}
}
func (d *Decoder) ReadByte() byte {
c := d.buffer[d.offset]
d.offset++
return c
}
func (d *Decoder) ReadUint8() uint8 {
i := uint8(d.buffer[d.offset])
d.offset++
return i
}
func (d *Decoder) ReadUint16() uint16 {
i := binary.BigEndian.Uint16(d.buffer[d.offset : d.offset+2])
d.offset += 2
return i
}
func (d *Decoder) ReadUint32() uint32 {
i := binary.BigEndian.Uint32(d.buffer[d.offset : d.offset+4])
d.offset += 4
return i
}
func (d *Decoder) ReadUint64() uint64 {
i := binary.BigEndian.Uint64(d.buffer[d.offset : d.offset+8])
d.offset += 8
return i
}
func (d *Decoder) ReadUint128() Uint128 {
hi := binary.BigEndian.Uint64(d.buffer[d.offset : d.offset+8])
lo := binary.BigEndian.Uint64(d.buffer[d.offset+8 : d.offset+16])
d.offset += 16
return Uint128{
Hi: hi,
Lo: lo,
}
}
func (d *Decoder) Skip(n int) {
d.offset += n
}
func (d *Decoder) SkipAlign() {
d.offset += (d.baseOffset+d.offset+7)/8*8 - d.baseOffset - d.offset
}
func (d *Decoder) Read(n int) []byte {
data := d.buffer[d.offset : d.offset+n]
d.offset += n
return data
}
func (d *Decoder) Length() int {
return len(d.buffer) - d.offset
}
func (d *Decoder) Bytes() []byte {
return d.buffer[d.offset:]
}
func (d *Decoder) Offset() int {
return d.offset
}
func (d *Decoder) BaseOffset() int {
return d.baseOffset
}
func (d *Decoder) SliceDecoder(length, rewind int) *Decoder {
newDecoder := NewDecoder(d.buffer[d.offset : d.offset+length-rewind])
newDecoder.baseOffset = d.offset + d.baseOffset
d.offset += length - rewind
return newDecoder
}