-
Notifications
You must be signed in to change notification settings - Fork 1
/
writing.go
89 lines (78 loc) · 1.79 KB
/
writing.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
package ninep
import (
"encoding/binary"
"errors"
"io"
)
func writeString(w io.Writer, s string) error {
b := []byte(s)
if len(b) > 0xffff {
return errors.New("string to write is too long")
}
if e := binary.Write(w, binary.LittleEndian, int16(len(b))); e != nil {
return e
}
if e := binary.Write(w, binary.LittleEndian, b); e != nil {
return e
}
return nil
}
func writeStringSlice(w io.Writer, ss []string) error {
if err := writeUint16(w, uint16(len(ss))); err != nil {
return err
}
for _, s := range ss {
if err := writeString(w, s); err != nil {
return err
}
}
return nil
}
func writeQIDSlice(w io.Writer, qs []QID) error {
if err := writeUint16(w, uint16(len(qs))); err != nil {
return err
}
for _, q := range qs {
if err := writeQID(w, q); err != nil {
return err
}
}
return nil
}
func writeByteSlice(w io.Writer, bs []byte) error {
if err := writeUint32(w, uint32(len(bs))); err != nil {
return err
}
n, err := w.Write(bs)
if err != nil {
return err
}
if n < len(bs) {
// TODO: Repeat byte slice write instead.
return errors.New("short write")
}
return nil
}
func writeQID(w io.Writer, q QID) error {
// QID struct is laid out to serialize correctly.
return binary.Write(w, binary.LittleEndian, q)
}
func writeUint8(w io.Writer, v uint8) error {
return binary.Write(w, binary.LittleEndian, v)
}
func writeUint16(w io.Writer, v uint16) error {
return binary.Write(w, binary.LittleEndian, v)
}
func writeUint32(w io.Writer, v uint32) error {
return binary.Write(w, binary.LittleEndian, v)
}
func writeUint64(w io.Writer, v uint64) error {
return binary.Write(w, binary.LittleEndian, v)
}
func stringSliceSize(ss []string) (size uint32) {
size += 2 // number of strings in slice
for _, s := range ss {
size += 2 + uint32(len(s))
}
return size
}