OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / byteslice.go
1 package wire
2
3 import (
4         "io"
5         "math"
6
7         cmn "github.com/tendermint/tmlibs/common"
8 )
9
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)
13 }
14
15 func ReadByteSlice(r io.Reader, lmt int, n *int, err *error) []byte {
16         length := ReadVarint(r, n, err)
17         if *err != nil {
18                 return nil
19         }
20         if length < 0 {
21                 *err = ErrBinaryReadInvalidLength
22                 return nil
23         }
24
25         // check that length is less than the maximum slice size
26         if length > math.MaxInt32 {
27                 *err = ErrBinaryReadOverflow
28                 return nil
29         }
30         if lmt != 0 && lmt < cmn.MaxInt(length, *n+length) {
31                 *err = ErrBinaryReadOverflow
32                 return nil
33         }
34
35         /*      if length == 0 {
36                 return nil // zero value for []byte
37         }*/
38
39         buf := make([]byte, length)
40         ReadFull(buf, r, n, err)
41         return buf
42
43 }
44
45 func PutByteSlice(buf []byte, bz []byte) (n int, err error) {
46         n_, err := PutVarint(buf, len(bz))
47         if err != nil {
48                 return 0, err
49         }
50         buf = buf[n_:]
51         n += n_
52         if len(buf) < len(bz) {
53                 return 0, ErrBinaryWriteOverflow
54         }
55         copy(buf, bz)
56         return n + len(bz), nil
57 }
58
59 func GetByteSlice(buf []byte) (bz []byte, n int, err error) {
60         length, n_, err := GetVarint(buf)
61         if err != nil {
62                 return nil, 0, err
63         }
64         buf = buf[n_:]
65         n += n_
66         if length < 0 {
67                 return nil, 0, ErrBinaryReadInvalidLength
68         }
69         if len(buf) < length {
70                 return nil, 0, ErrBinaryReadOverflow
71         }
72         buf2 := make([]byte, length)
73         copy(buf2, buf)
74         return buf2, n + length, nil
75 }
76
77 // Returns the total encoded size of a byteslice
78 func ByteSliceSize(bz []byte) int {
79         return UvarintSize(uint64(len(bz))) + len(bz)
80 }
81
82 //-----------------------------------------------------------------------------
83
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)
88                 if *err != nil {
89                         return
90                 }
91         }
92 }
93
94 func ReadByteSlices(r io.Reader, lmt int, n *int, err *error) [][]byte {
95         length := ReadVarint(r, n, err)
96         if *err != nil {
97                 return nil
98         }
99         if length < 0 {
100                 *err = ErrBinaryReadInvalidLength
101                 return nil
102         }
103         if lmt != 0 && lmt < cmn.MaxInt(length, *n+length) {
104                 *err = ErrBinaryReadOverflow
105                 return nil
106         }
107
108         bzz := make([][]byte, length)
109         for i := 0; i < length; i++ {
110                 bz := ReadByteSlice(r, lmt, n, err)
111                 if *err != nil {
112                         return nil
113                 }
114                 bzz[i] = bz
115         }
116         return bzz
117 }