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