This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
stream.go
189 lines (168 loc) · 3.14 KB
/
stream.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package capn
import (
"bufio"
"bytes"
"errors"
"io"
)
type Compressor struct {
w *bufio.Writer
}
type Decompressor struct {
r io.Reader
buf [8]byte
bufsz int
zeros int
raw int
}
func NewCompressor(w io.Writer) *Compressor {
return &Compressor{
w: bufio.NewWriter(w),
}
}
// WriteToPacked writes the message that the segment is part of to the
// provided stream in packed form.
func (s *Segment) WriteToPacked(w io.Writer) (int64, error) {
c := NewCompressor(w)
return s.WriteTo(c)
}
func NewDecompressor(r io.Reader) *Decompressor {
return &Decompressor{r: r}
}
// ReadFromPackedStream reads a single message from the stream r in packed
// form returning the first segment. buf can be specified in order to reuse
// the buffer (or it is allocated each call if nil).
func ReadFromPackedStream(r io.Reader, buf *bytes.Buffer) (*Segment, error) {
c := Decompressor{r: r}
return ReadFromStream(&c, buf)
}
func min(a, b int) int {
if b < a {
return b
}
return a
}
func (c *Decompressor) Read(v []byte) (n int, err error) {
if c.raw > 0 {
n, err = c.r.Read(v[:min(len(v), c.raw)])
c.raw -= n
return
}
if c.zeros > 0 {
n = min(len(v), c.zeros)
for i := range v[:n] {
v[i] = 0
}
c.zeros -= n
return
}
if c.bufsz > 0 {
n = copy(v, c.buf[:c.bufsz])
c.bufsz -= n
}
for n < len(v) {
b := [1]byte{}
if _, err = c.r.Read(b[:]); err != nil {
return
}
switch b[0] {
case 0xFF:
io.ReadFull(c.r, c.buf[:])
case 0x00:
if _, err = c.r.Read(b[:]); err != nil {
return
}
zeros := min(int(b[0]), len(v)-n)
for i := range v[n : n+zeros] {
v[i] = 0
}
c.zeros = int(b[0]) - zeros
n += zeros
default:
ones := 0
buf := [8]byte{}
for i := 0; i < 8; i++ {
if (b[0] & (1 << uint(i))) != 0 {
ones++
}
}
io.ReadFull(c.r, buf[:ones])
for i, j := 0, 0; i < 8; i++ {
if (b[0] & (1 << uint(i))) != 0 {
c.buf[i] = buf[j]
j++
} else {
c.buf[i] = 0
}
}
use := copy(v[n:], c.buf[:])
n += use
c.bufsz = 8 - use
}
}
return
}
func (c *Compressor) Write(v []byte) (n int, err error) {
if (len(v) % 8) != 0 {
return 0, errors.New("capnproto: compressor relies on word aligned data")
}
buf := make([]byte, 0, 8)
for n < len(v) {
var hdr byte
buf = buf[:0]
for i, b := range v[n : n+8] {
if b != 0 {
hdr |= 1 << uint(i)
buf = append(buf, b)
}
}
err = c.w.WriteByte(hdr)
if err != nil {
return n, err
}
_, err = c.w.Write(buf)
if err != nil {
return n, err
}
n += 8
switch hdr {
case 0x00:
i := 0
for n < len(v) && little64(v[n:]) == 0 && i < 0xFF {
i++
n += 8
}
err = c.w.WriteByte(byte(i))
if err != nil {
return n, err
}
case 0xFF:
i := n
end := min(len(v), n+0xFF*8)
for i < end {
zeros := 0
for _, b := range v[i : i+8] {
if b == 0 {
zeros++
}
}
if zeros > 7 {
break
}
i += 8
}
rawWords := byte((i - n) / 8)
err := c.w.WriteByte(rawWords)
if err != nil {
return n, err
}
_, err = c.w.Write(v[n:i])
if err != nil {
return n, err
}
n = i
}
}
err = c.w.Flush()
return n, err
}