-
Notifications
You must be signed in to change notification settings - Fork 1
/
reading.go
111 lines (100 loc) · 2.23 KB
/
reading.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
101
102
103
104
105
106
107
108
109
110
111
package ninep
import (
"encoding/binary"
"io"
)
func readString(r io.Reader, s *string) error {
var sz uint16
if err := binary.Read(r, binary.LittleEndian, &sz); err != nil {
return err
}
buf := make([]byte, sz)
if err := binary.Read(r, binary.LittleEndian, &buf); err != nil {
return err
}
*s = string(buf)
return nil
}
func readStringSlice(r io.Reader, ss *[]string) error {
var size int16
if err := binary.Read(r, binary.LittleEndian, &size); err != nil {
return err
}
*ss = make([]string, 0, size)
for i := int16(0); i < size; i++ {
var s string
if err := readString(r, &s); err != nil {
return err
}
*ss = append(*ss, s)
}
return nil
}
func readQIDSlice(r io.Reader, qs *[]QID) error {
var size uint16
if err := binary.Read(r, binary.LittleEndian, &size); err != nil {
return err
}
*qs = make([]QID, 0, size)
for i := uint16(0); i < size; i++ {
var q QID
if err := readQID(r, &q); err != nil {
return err
}
*qs = append(*qs, q)
}
return nil
}
func readQID(r io.Reader, q *QID) error {
return binary.Read(r, binary.LittleEndian, q)
}
// Note: This *populates* a byte slice passed in from the outside.
func readAndFillByteSlice(r io.Reader, bs []byte) (uint32, error) {
var size uint32
if err := binary.Read(r, binary.LittleEndian, &size); err != nil {
return 0, err
}
n := size
if n > uint32(len(bs)) {
n = uint32(len(bs))
}
bs = bs[:n]
if _, err := io.ReadFull(r, bs); err != nil {
return 0, err
}
if err := skip(r, int(size-n)); err != nil {
return 0, err
}
return n, nil
}
func readUint8(r io.Reader, out *uint8) error {
return binary.Read(r, binary.LittleEndian, out)
}
func readUint16(r io.Reader, out *uint16) error {
return binary.Read(r, binary.LittleEndian, out)
}
func readUint32(r io.Reader, out *uint32) error {
return binary.Read(r, binary.LittleEndian, out)
}
func readUint64(r io.Reader, out *uint64) error {
return binary.Read(r, binary.LittleEndian, out)
}
func skip(r io.Reader, n int) error {
if s, ok := r.(io.ReadSeeker); ok {
_, err := s.Seek(int64(n), io.SeekCurrent)
return err
}
var buffer [1024]byte
buf := buffer[:]
for n > 0 {
if n < len(buf) {
buf = buf[:n]
}
nn, err := r.Read(buf)
if err != nil {
return err
}
n -= nn
}
return nil
}