forked from sauerbraten/extinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
69 lines (60 loc) · 1.91 KB
/
decode.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
package extinfo
import (
"bytes"
"log"
)
// the current position in a response ([]byte)
// needed, since values are encoded in variable amount of bytes
// global to not have to pass around an int on every dump
var positionInResponse int
// decodes the bytes read from the connection into ints
// returns the decoded byte slice as int and the amount of bytes used up of the slice
func getInt(buf []byte) (int, int) {
// n is the size of the buffer
n := len(buf)
// b is the first byte in buf
b := buf[0]
// 0x80 means: value is contained in the next 2 more bytes
if b == 0x80 {
if n < 3 {
log.Fatal("buf too short!")
}
// 2 next bytes = cd (as in ABCDEFGH...)
cd := int(buf[1]) + int(buf[2])<<8
// return the decoded int and the amount of bytes used
return cd, 3
}
// 0x81 means: value is contained in the next 4 more bytes
if b == 0x81 {
if n < 5 {
log.Fatal("buf too short!")
}
// 4 next bytes = cdef (as in ABCDEFGH...)
cdef := int(buf[1]) + int(buf[2])<<8 + int(buf[3])<<16 + int(buf[4])<<24
// return the decoded int and the amount of bytes used
return cdef, 5
}
// return the decoded int and the amount of bytes used
if b > 0x7F {
return int(b) - int(1<<8), 1
}
return int(b), 1
}
// converts the next bytes up to the first \0 byte into a string
func getString(buf []byte) (string, int) {
end := bytes.Index(buf, []byte{0}) + 1
str := string(decodeCubecode(buf[:end]))
return str, end
}
// returns a decoded int and sets the position to the next attribute's first byte
func dumpInt(buf []byte) int {
decodedInt, bytesRead := getInt(buf[positionInResponse:])
positionInResponse = positionInResponse + bytesRead
return decodedInt
}
// returns a string and sets the position to the next attribute's first byte
func dumpString(buf []byte) string {
decodedString, bytesRead := getString(buf[positionInResponse:])
positionInResponse = positionInResponse + bytesRead
return decodedString
}