-
Notifications
You must be signed in to change notification settings - Fork 3
/
framedview.go
52 lines (47 loc) · 1.03 KB
/
framedview.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
package chitin
import (
"io"
"github.com/dchest/varuint"
)
// NewFramedView opens a view on the bytes given, providing access to
// framed contents as bytes.
func NewFramedView(data []byte) *FramedView {
return &FramedView{data: data}
}
// FramedView is a view on framed bytes.
//
// See https://chitin.io/spec/v1/#f-e-m
type FramedView struct {
data []byte
}
// Next returns the contents of the next frame.
//
// Returns io.EOF if there are no more frames. Returns
// io.ErrUnexpectedEOF on truncated data.
func (v *FramedView) Next() ([]byte, error) {
loop:
if len(v.data) == 0 {
return nil, io.EOF
}
l, n := varuint.Uint64(v.data)
if n < 0 {
return nil, io.ErrUnexpectedEOF
}
if l == 0 {
goto loop
}
l--
const maxInt = int(^uint(0) >> 1)
if l > uint64(maxInt) {
// technically, it has to be truncated because it wouldn't fit
// in memory ;)
return nil, io.ErrUnexpectedEOF
}
end := n + int(l)
if end > len(v.data) {
return nil, io.ErrUnexpectedEOF
}
b := v.data[n:end]
v.data = v.data[end:]
return b, nil
}