7 cmn "github.com/tendermint/tmlibs/common"
10 func WriteByteSlice(bz []byte, w io.Writer, n *int, err *error) {
11 WriteVarint(len(bz), w, n, err)
12 WriteTo(bz, w, n, err)
15 func ReadByteSlice(r io.Reader, lmt int, n *int, err *error) []byte {
16 length := ReadVarint(r, n, err)
21 *err = ErrBinaryReadInvalidLength
25 // check that length is less than the maximum slice size
26 if length > math.MaxInt32 {
27 *err = ErrBinaryReadOverflow
30 if lmt != 0 && lmt < cmn.MaxInt(length, *n+length) {
31 *err = ErrBinaryReadOverflow
36 return nil // zero value for []byte
39 buf := make([]byte, length)
40 ReadFull(buf, r, n, err)
45 func PutByteSlice(buf []byte, bz []byte) (n int, err error) {
46 n_, err := PutVarint(buf, len(bz))
52 if len(buf) < len(bz) {
53 return 0, ErrBinaryWriteOverflow
56 return n + len(bz), nil
59 func GetByteSlice(buf []byte) (bz []byte, n int, err error) {
60 length, n_, err := GetVarint(buf)
67 return nil, 0, ErrBinaryReadInvalidLength
69 if len(buf) < length {
70 return nil, 0, ErrBinaryReadOverflow
72 buf2 := make([]byte, length)
74 return buf2, n + length, nil
77 // Returns the total encoded size of a byteslice
78 func ByteSliceSize(bz []byte) int {
79 return UvarintSize(uint64(len(bz))) + len(bz)
82 //-----------------------------------------------------------------------------
84 func WriteByteSlices(bzz [][]byte, w io.Writer, n *int, err *error) {
85 WriteVarint(len(bzz), w, n, err)
86 for _, bz := range bzz {
87 WriteByteSlice(bz, w, n, err)
94 func ReadByteSlices(r io.Reader, lmt int, n *int, err *error) [][]byte {
95 length := ReadVarint(r, n, err)
100 *err = ErrBinaryReadInvalidLength
103 if lmt != 0 && lmt < cmn.MaxInt(length, *n+length) {
104 *err = ErrBinaryReadOverflow
108 bzz := make([][]byte, length)
109 for i := 0; i < length; i++ {
110 bz := ReadByteSlice(r, lmt, n, err)