Debian Codegraph @

utils/binary/read.go

raw
1 // Package binary implements syntax-sugar functions on top of the standard
2 // library binary package
3 package binary
4
5 import (
6 "bufio"
7 "encoding/binary"
8 "errors"
9 "io"
10 "math"
11
12 "github.com/go-git/go-git/v5/plumbing"
13 )
14
15 // ErrIntegerOverflow is returned when a Git-format variable-width integer
16 // would not fit into an int64 because the input declares more continuation
17 // bytes than the type can hold.
18 var ErrIntegerOverflow = errors.New("variable-width integer overflow")
19
20 // Read reads structured binary data from r into data. Bytes are read and
21 // decoded in BigEndian order
22 // https://golang.org/pkg/encoding/binary/#Read
23 func Read(r io.Reader, data ...interface{}) error {
24 for _, v := range data {
25 if err := binary.Read(r, binary.BigEndian, v); err != nil {
26 return err
27 }
28 }
29
30 return nil
31 }
32
33 // ReadUntil reads from r untin delim is found
34 func ReadUntil(r io.Reader, delim byte) ([]byte, error) {
35 if bufr, ok := r.(*bufio.Reader); ok {
36 return ReadUntilFromBufioReader(bufr, delim)
37 }
38
39 var buf [1]byte
40 value := make([]byte, 0, 16)
41 for {
42 if _, err := io.ReadFull(r, buf[:]); err != nil {
43 if err == io.EOF {
44 return nil, err
45 }
46
47 return nil, err
48 }
49
50 if buf[0] == delim {
51 return value, nil
52 }
53
54 value = append(value, buf[0])
55 }
56 }
57
58 // ReadUntilFromBufioReader is like bufio.ReadBytes but drops the delimiter
59 // from the result.
60 func ReadUntilFromBufioReader(r *bufio.Reader, delim byte) ([]byte, error) {
61 value, err := r.ReadBytes(delim)
62 if err != nil || len(value) == 0 {
63 return nil, err
64 }
65
66 return value[:len(value)-1], nil
67 }
68
69 // ReadVariableWidthInt reads and returns an int in Git VLQ special format:
70 //
71 // Ordinary VLQ has some redundancies, example: the number 358 can be
72 // encoded as the 2-octet VLQ 0x8166 or the 3-octet VLQ 0x808166 or the
73 // 4-octet VLQ 0x80808166 and so forth.
74 //
75 // To avoid these redundancies, the VLQ format used in Git removes this
76 // prepending redundancy and extends the representable range of shorter
77 // VLQs by adding an offset to VLQs of 2 or more octets in such a way
78 // that the lowest possible value for such an (N+1)-octet VLQ becomes
79 // exactly one more than the maximum possible value for an N-octet VLQ.
80 // In particular, since a 1-octet VLQ can store a maximum value of 127,
81 // the minimum 2-octet VLQ (0x8000) is assigned the value 128 instead of
82 // 0. Conversely, the maximum value of such a 2-octet VLQ (0xff7f) is
83 // 16511 instead of just 16383. Similarly, the minimum 3-octet VLQ
84 // (0x808000) has a value of 16512 instead of zero, which means
85 // that the maximum 3-octet VLQ (0xffff7f) is 2113663 instead of
86 // just 2097151. And so forth.
87 //
88 // This is how the offset is saved in C:
89 //
90 // dheader[pos] = ofs & 127;
91 // while (ofs >>= 7)
92 // dheader[--pos] = 128 | (--ofs & 127);
93 //
94 func ReadVariableWidthInt(r io.Reader) (int64, error) {
95 var c byte
96 if err := Read(r, &c); err != nil {
97 return 0, err
98 }
99
100 var v = int64(c & maskLength)
101 for c&maskContinue > 0 {
102 // Reject input that, after the v++ and shift below, would
103 // not fit in an int64. With v < (MaxInt64-127)>>7, the
104 // post-increment v is at most (MaxInt64-127)>>7 and the
105 // final (v << 7) + (c & 0x7F) stays within int64.
106 if v >= (math.MaxInt64-int64(maskLength))>>lengthBits {
107 return 0, ErrIntegerOverflow
108 }
109
110 v++
111 if err := Read(r, &c); err != nil {
112 return 0, err
113 }
114
115 v = (v << lengthBits) + int64(c&maskLength)
116 }
117
118 return v, nil
119 }
120
121 const (
122 maskContinue = uint8(128) // 1000 000
123 maskLength = uint8(127) // 0111 1111
124 lengthBits = uint8(7) // subsequent bytes has 7 bits to store the length
125 )
126
127 // ReadUint64 reads 8 bytes and returns them as a BigEndian uint32
128 func ReadUint64(r io.Reader) (uint64, error) {
129 var v uint64
130 if err := binary.Read(r, binary.BigEndian, &v); err != nil {
131 return 0, err
132 }
133
134 return v, nil
135 }
136
137 // ReadUint32 reads 4 bytes and returns them as a BigEndian uint32
138 func ReadUint32(r io.Reader) (uint32, error) {
139 var v uint32
140 if err := binary.Read(r, binary.BigEndian, &v); err != nil {
141 return 0, err
142 }
143
144 return v, nil
145 }
146
147 // ReadUint16 reads 2 bytes and returns them as a BigEndian uint16
148 func ReadUint16(r io.Reader) (uint16, error) {
149 var v uint16
150 if err := binary.Read(r, binary.BigEndian, &v); err != nil {
151 return 0, err
152 }
153
154 return v, nil
155 }
156
157 // ReadHash reads a plumbing.Hash from r
158 func ReadHash(r io.Reader) (plumbing.Hash, error) {
159 var h plumbing.Hash
160 if err := binary.Read(r, binary.BigEndian, h[:]); err != nil {
161 return plumbing.ZeroHash, err
162 }
163
164 return h, nil
165 }
166
167 const sniffLen = 8000
168
169 // IsBinary detects if data is a binary value based on:
170 // http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
171 func IsBinary(r io.Reader) (bool, error) {
172 reader := bufio.NewReader(r)
173 c := 0
174 for {
175 if c == sniffLen {
176 break
177 }
178
179 b, err := reader.ReadByte()
180 if err == io.EOF {
181 break
182 }
183 if err != nil {
184 return false, err
185 }
186
187 if b == byte(0) {
188 return true, nil
189 }
190
191 c++
192 }
193
194 return false, nil
195 }