-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
84 lines (65 loc) · 2.12 KB
/
parser.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
// Binary reader/writer.
// Helpers to read and write to packet.
// This binary parser is needed since we need to communicate with some legacy services.
// For some reason this services use LittleEndian :(
package packet
import "encoding/binary"
import "strings"
type Packet struct {
buffer []byte
}
// Instanciates a new buffer and its reader funcs
func Reader(buffer []byte) *Packet {
return &Packet{buffer}
}
func (p *Packet) ReadUint8(offset int16) uint8 {
return uint8(p.buffer[offset])
}
func (p *Packet) ReadInt8(offset int16) int8 {
return int8(p.buffer[offset])
}
func (p *Packet) ReadUint16(offset int16) uint16 {
return binary.LittleEndian.Uint16(p.buffer[offset:])
}
func (p *Packet) ReadInt16(offset int16) int16 {
return int16(binary.LittleEndian.Uint16(p.buffer[offset:]))
}
func (p *Packet) ReadUint32(offset int16) uint32 {
return binary.LittleEndian.Uint32(p.buffer[offset:])
}
func (p *Packet) ReadInt32(offset int16) int32 {
return int32(binary.LittleEndian.Uint32(p.buffer[offset:]))
}
func (p *Packet) ReadString(offset int16, length int16) string {
return strings.TrimRight(string(p.buffer[offset:offset+length]), "\x00")
}
// Instanciates a new buffer and its writer funcs
func Writer(length int) *Packet {
return &Packet{
buffer: make([]byte, length),
}
}
func (p *Packet) WriteUint8(offset int16, payload uint8) {
p.buffer[offset] = payload
}
func (p *Packet) WriteInt8(offset int16, payload int8) {
p.buffer[offset] = uint8(payload)
}
func (p *Packet) WriteUint16(offset int16, payload uint16) {
binary.LittleEndian.PutUint16(p.buffer[offset:], payload)
}
func (p *Packet) WriteInt16(offset int16, payload int16) {
binary.LittleEndian.PutUint16(p.buffer[offset:], uint16(payload))
}
func (p *Packet) WriteUint32(offset int16, payload uint32) {
binary.LittleEndian.PutUint32(p.buffer[offset:], payload)
}
func (p *Packet) WriteInt32(offset int16, payload int32) {
binary.LittleEndian.PutUint32(p.buffer[offset:], uint32(payload))
}
func (p *Packet) WriteString(offset int16, payload string, length int16) {
copy(p.buffer[offset:offset+length], payload)
}
func (p *Packet) Buffer() []byte {
return p.buffer
}