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
|
// Utilities for reading and writing of binary data
package rwu
import (
"encoding/binary"
"io"
)
func ReadBool(r io.Reader) (bool, error) {
var c uint8
err := binary.Read(r, binary.LittleEndian, &c)
return c != 0, err
}
func ReadUint8(r io.Reader) (uint8, error) {
var c uint8
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadUint16(r io.Reader) (uint16, error) {
var c uint16
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadUint32(r io.Reader) (uint32, error) {
var c uint32
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadUint64(r io.Reader) (uint64, error) {
var c uint64
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadInt8(r io.Reader) (int8, error) {
var c int8
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadInt16(r io.Reader) (int16, error) {
var c int16
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadInt32(r io.Reader) (int32, error) {
var c int32
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadInt64(r io.Reader) (int64, error) {
var c int64
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadString(r io.Reader) (string, error) {
c := make([]byte, 0)
var err error
for {
var b byte
err = binary.Read(r, binary.LittleEndian, &b)
if b == byte(0x0) || err != nil {
break
}
c = append(c, b)
}
return string(c), err
}
func ReadByte(r io.Reader) (byte, error) {
var c byte
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func ReadBytes(r io.Reader, num int32) ([]byte, error) {
c := make([]byte, num)
err := binary.Read(r, binary.LittleEndian, &c)
return c, err
}
func WriteBool(w io.Writer, b bool) error {
var err error
if b {
_, err = w.Write([]byte{1})
} else {
_, err = w.Write([]byte{0})
}
return err
}
|