/
pb.go
45 lines (41 loc) · 870 Bytes
/
pb.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
package utils
import (
"encoding/binary"
"fmt"
"io"
"math"
"github.com/golang/protobuf/proto"
)
func MarshalSized(pb proto.Message) ([]byte, error) {
b := proto.NewBuffer(make([]byte, 4))
err := b.Marshal(pb)
if err != nil {
return nil, err
}
rv := b.Bytes()
if len(rv) > math.MaxInt32 {
return nil, fmt.Errorf("protobuf too large")
}
if len(rv) < 4 {
panic("unexpected")
}
binary.BigEndian.PutUint32(rv[:4], uint32(len(rv)-4))
return rv, nil
}
func UnmarshalSized(source io.Reader, pb proto.Message) error {
var sizeBytes [4]byte
_, err := io.ReadFull(source, sizeBytes[:])
if err != nil {
return err
}
size := binary.BigEndian.Uint32(sizeBytes[:])
data := make([]byte, size)
_, err = io.ReadFull(source, data)
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return err
}
return proto.Unmarshal(data, pb)
}