forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrepo_utils_reader.go
231 lines (199 loc) · 4.03 KB
/
repo_utils_reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package git
import (
"bytes"
"compress/zlib"
"errors"
"io"
"io/ioutil"
)
var (
// ObjectReader implemented ReadCloser
_ io.ReadCloser = new(readCloser)
_ io.ReaderAt = new(readAter)
)
type readCloser struct {
r io.ReadCloser
c io.Closer
}
func (o *readCloser) Read(p []byte) (n int, err error) {
return o.r.Read(p)
}
func (o *readCloser) Close() error {
errr := o.r.Close()
errc := o.c.Close()
if errr != nil && errc != nil {
// in case both fail, return the concatenated error
return errors.New(errr.Error() + ", " + errc.Error())
}
if errr != nil {
return errr
}
if errc != nil {
return errc
}
return nil
}
func newReadCloser(r io.ReadCloser, c io.Closer) io.ReadCloser {
return &readCloser{r, c}
}
func wrapReadCloser(wrap io.Reader, rc io.ReadCloser) io.ReadCloser {
return newReadCloser(ioutil.NopCloser(wrap), rc)
}
type bufReadCloser struct {
r *bytes.Buffer
}
func (o *bufReadCloser) Read(p []byte) (n int, err error) {
return o.r.Read(p)
}
func (o *bufReadCloser) Close() error {
return nil
}
func newBufReadCloser(buf []byte) io.ReadCloser {
o := new(bufReadCloser)
o.r = bytes.NewBuffer(buf)
return o
}
type readAter struct {
buf []byte
}
func (o *readAter) ReadAt(p []byte, off int64) (n int, err error) {
if int(off) >= len(o.buf) {
err = io.EOF
return
}
length := len(p)
if length == 0 {
return
}
if length > len(o.buf[off:]) {
length = len(o.buf[off:])
}
copy(p, o.buf[off:])
n = length
return
}
// Read deflated object from the file.
func readerDecompressed(r io.ReadCloser) (io.ReadCloser, error) {
zr, err := zlib.NewReader(r)
if err != nil {
return nil, err
}
return newReadCloser(zr, r), nil
}
// buf must be large enough to read the number.
func readerLittleEndianBase128Number(r io.Reader) (int64, int) {
zpos := 0
buf := []byte{0}
n, err := r.Read(buf)
if err != nil {
return 0, n
}
length := int64(buf[0] & 0x7f)
shift := uint64(0)
for buf[0]&0x80 > 0 {
shift += 7
n, err := r.Read(buf)
if err != nil {
return 0, zpos + n
}
zpos += n
length |= int64(buf[0]&0x7f) << shift
}
zpos += 1
return length, zpos
}
func readerApplyDelta(br io.ReaderAt, dr io.Reader, resultLen int64) (res []byte, err error) {
var (
resultpos uint64
)
buf := []byte{0}
res = make([]byte, resultLen)
read := func(r io.Reader) (ret bool) {
var n int
n, err = r.Read(buf)
if err == io.EOF {
err = nil
return n == 1
}
if n == 0 || err != nil {
return
}
ret = true
return
}
readAt := func(r io.ReaderAt, off int64) (ret bool) {
var n int
n, err = r.ReadAt(buf, off)
if err == io.EOF {
err = nil
return n == 1
}
if n == 0 || err != nil {
return
}
ret = true
return
}
for {
// two modes: copy and insert. copy reads offset and len from the delta
// instructions and copy len bytes from offset into the resulting object
// insert takes up to 127 bytes and insert them into the
// resulting object
if !read(dr) {
return
}
opcode := buf[0]
if opcode&0x80 > 0 {
// Copy from base to dest
copy_offset := uint64(0)
copy_length := uint64(0)
shift := uint(0)
for i := 0; i < 4; i++ {
if opcode&0x01 > 0 {
if !read(dr) {
return
}
copy_offset |= uint64(buf[0]) << shift
}
opcode >>= 1
shift += 8
}
shift = 0
for i := 0; i < 3; i++ {
if opcode&0x01 > 0 {
if !read(dr) {
return
}
copy_length |= uint64(buf[0]) << shift
}
opcode >>= 1
shift += 8
}
if copy_length == 0 {
copy_length = 1 << 16
}
brOffset := int64(copy_offset)
for i := uint64(0); i < copy_length; i++ {
if !readAt(br, brOffset) {
return
}
res[resultpos] = buf[0]
resultpos++
brOffset++
}
} else if opcode > 0 {
// insert n bytes at the end of the resulting object. n==opcode
for i := 0; i < int(opcode); i++ {
if !read(dr) {
return
}
res[resultpos] = buf[0]
resultpos++
}
} else {
return nil, errors.New("[readerApplyDelta] opcode == 0")
}
}
// TODO: check if resultlen == resultpos
return
}