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