aboutsummaryrefslogblamecommitdiffstats
path: root/packfile/reader.go
blob: 3725822f454c96115894438bf29a3460f741682c (plain) (tree)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412



























































































































































































































































































































































































































                                                                                                 
package packfile

import (
	"bytes"
	"compress/zlib"
	"encoding/binary"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
)

const MaxObjectsLimit = 1000000

type PackfileReader struct {
	r io.Reader

	objects map[string]packfileObject
	offsets map[int]string
	deltas  []packfileDelta

	// The give back logic is explained in the giveBack method.
	startedGivingBack bool
	givebackBuffer    []byte
	givenBack         io.Reader
	contentCallback   ContentCallback
}

// Sometimes, after reading an object from a packfile, there will be
// a few bytes with garbage data before the next object comes by.
// There is no way of reliably noticing this until when trying to read the
// next object and failing because zlib parses an invalid header. We can't
// notice before, because parsing the object's header (size, type, etc.)
// doesn't fail.
//
// At that point, we want to give back to the reader the bytes we've read
// since the last object, shift the input by one byte, and try again. That's
// why we save the bytes we read on each object and, if it fails in the middle
// of parsing it, those bytes will be read the next times you call Read() on
// a objectReader derived from a PackfileReader.readObject, until they run out.
func (pr *PackfileReader) giveBack() {
	pr.givenBack = bytes.NewReader(pr.givebackBuffer)
	pr.givebackBuffer = nil
}

type packfileObject struct {
	bytes []byte
	typ   int8
}

type packfileDelta struct {
	hash  string
	delta []byte
}

func NewPackfileReader(r io.Reader, contentCallback ContentCallback) (*PackfileReader, error) {
	return &PackfileReader{
		r:               r,
		objects:         map[string]packfileObject{},
		offsets:         map[int]string{},
		contentCallback: contentCallback,
	}, nil
}

func (p *PackfileReader) Read() (*Packfile, error) {
	packfile := NewPackfile()

	if err := p.validateSignature(); err != nil {
		if err == io.EOF {
			// This is an empty repo. It's OK.
			return packfile, nil
		}
		return nil, err
	}

	var err error
	ver, err := p.readInt32()
	if err != nil {
		return nil, err
	}

	count, err := p.readInt32()
	if err != nil {
		return nil, err
	}

	packfile.Version = uint32(ver)
	packfile.ObjectCount = int(count)

	if packfile.ObjectCount > MaxObjectsLimit {
		return nil, NewError("too many objects (%d)", packfile.ObjectCount)
	}

	if err := p.readObjects(packfile); err != nil {
		return nil, err
	}

	return packfile, nil
}

func (p *PackfileReader) validateSignature() error {
	var signature = make([]byte, 4)
	if _, err := p.r.Read(signature); err != nil {
		return err
	}

	if !bytes.Equal(signature, []byte{'P', 'A', 'C', 'K'}) {
		return NewError("Pack file does not start with 'PACK'")
	}

	return nil
}

func (p *PackfileReader) readInt32() (uint32, error) {
	var value uint32
	if err := binary.Read(p.r, binary.BigEndian, &value); err != nil {
		fmt.Println(err)

		return 0, err
	}

	return value, nil
}

func (p *PackfileReader) readObjects(packfile *Packfile) error {
	p.startedGivingBack = true

	offset := 12
	for i := 0; i < packfile.ObjectCount; i++ {
		r, err := p.readObject(packfile, offset)
		if err != nil && err != io.EOF {
			return err
		}

		p.offsets[offset] = r.hash
		offset += r.counter + 4

		unknownForBytes := make([]byte, 4)
		p.r.Read(unknownForBytes)

		if err == io.EOF {
			break
		}
	}

	return nil
}

const (
	OBJ_COMMIT    = 1
	OBJ_TREE      = 2
	OBJ_BLOB      = 3
	OBJ_TAG       = 4
	OBJ_OFS_DELTA = 6
	OBJ_REF_DELTA = 7
)

const SIZE_LIMIT uint64 = 1 << 32 //4GB

type objectReader struct {
	pr     *PackfileReader
	pf     *Packfile
	offset int
	hash   string

	typ     int8
	size    uint64
	counter int
}

func (p *PackfileReader) readObject(packfile *Packfile, offset int) (*objectReader, error) {
	o, err := newObjectReader(p, packfile, offset)
	if err != nil {
		return nil, err
	}

	switch o.typ {
	case OBJ_REF_DELTA:
		err = o.readREFDelta()
	case OBJ_OFS_DELTA:
		err = o.readOFSDelta()
	case OBJ_COMMIT, OBJ_TREE, OBJ_BLOB, OBJ_TAG:
		err = o.readObject()
	default:
		err = NewError("Invalid git object tag %q", o.typ)
	}
	if err == ErrZlibHeader {
		p.giveBack()
		io.CopyN(ioutil.Discard, p.r, 1)
		return p.readObject(packfile, offset)
	}

	return o, err
}

func newObjectReader(pr *PackfileReader, pf *Packfile, offset int) (*objectReader, error) {
	o := &objectReader{pr: pr, pf: pf, offset: offset}
	buf := make([]byte, 1)
	if _, err := o.Read(buf); err != nil {
		return nil, err
	}

	o.typ = int8((buf[0] >> 4) & 7)
	o.size = uint64(buf[0] & 15)

	var shift uint = 4
	for buf[0]&0x80 == 0x80 {
		if _, err := o.Read(buf); err != nil {
			return nil, err
		}

		o.size += uint64(buf[0]&0x7f) << shift
		shift += 7
	}

	return o, nil
}

func (o *objectReader) readREFDelta() error {
	ref := make([]byte, 20)
	o.Read(ref)

	buf, err := o.inflate()
	if err != nil {
		return err
	}

	refhash := fmt.Sprintf("%x", ref)
	referenced, ok := o.pr.objects[refhash]
	if !ok {
		o.pr.deltas = append(o.pr.deltas, packfileDelta{hash: refhash, delta: buf})
	} else {
		patched := PatchDelta(referenced.bytes, buf)
		if patched == nil {
			return NewError("error while patching %x", ref)
		}
		o.typ = referenced.typ
		err = o.addObject(patched)
		if err != nil {
			return err
		}
	}

	return nil
}

func (o *objectReader) readOFSDelta() error {
	// read negative offset
	var b uint8
	binary.Read(o, binary.BigEndian, &b)
	var noffset int = int(b & 0x7f)
	for (b & 0x80) != 0 {
		noffset += 1
		binary.Read(o, binary.BigEndian, &b)
		noffset = (noffset << 7) + int(b&0x7f)
	}

	buf, err := o.inflate()
	if err != nil {
		return err
	}

	refhash := o.pr.offsets[o.offset-noffset]
	referenced, ok := o.pr.objects[refhash]
	if !ok {
		return NewError("can't find a pack entry at %d", o.offset-noffset)
	} else {
		patched := PatchDelta(referenced.bytes, buf)
		if patched == nil {
			return NewError("error while patching %x", refhash)
		}
		o.typ = referenced.typ
		err = o.addObject(patched)
		if err != nil {
			return err
		}
	}

	return nil
}

func (o *objectReader) readObject() error {
	buf, err := o.inflate()
	if err != nil {
		return err
	}

	return o.addObject(buf)
}

func (o *objectReader) addObject(bytes []byte) error {
	var hash string

	switch o.typ {
	case OBJ_COMMIT:
		c, err := NewCommit(bytes)
		if err != nil {
			return err
		}
		o.pf.Commits[c.Hash()] = c
		hash = c.Hash()
	case OBJ_TREE:
		c, err := NewTree(bytes)
		if err != nil {
			return err
		}
		o.pf.Trees[c.Hash()] = c
		hash = c.Hash()
	case OBJ_BLOB:
		c, err := NewBlob(bytes)
		if err != nil {
			return err
		}
		o.pf.Blobs[c.Hash()] = c
		hash = c.Hash()

		if o.pr.contentCallback != nil {
			o.pr.contentCallback(hash, bytes)
		}
	}

	o.pr.objects[hash] = packfileObject{bytes: bytes, typ: o.typ}
	o.hash = hash

	return nil

}

func (o *objectReader) inflate() ([]byte, error) {
	//Quick fix "Invalid git object tag '\x00'" when the length of a object is 0
	if o.size == 0 {
		buf := make([]byte, 4)
		if _, err := o.Read(buf); err != nil {
			return nil, err
		}

		return nil, nil
	}

	zr, err := zlib.NewReader(o)
	if err != nil {
		if err.Error() == "zlib: invalid header" {
			return nil, ErrZlibHeader
		} else {
			return nil, NewError("error opening packfile's object zlib: %v", err)
		}
	}

	defer zr.Close()

	if o.size > SIZE_LIMIT {
		return nil, NewError("the object size exceeed the allowed limit: %d", o.size)
	}

	buf := make([]byte, o.size)
	read := 0
	for read < int(o.size) {
		n, err := zr.Read(buf[read:])
		if err != nil {
			return nil, err
		}

		read += n
	}

	if read != int(o.size) {
		return nil, NewError("inflated size mismatch, expected %d, got %d", o.size, read)
	}

	return buf, nil
}

func (o *objectReader) Read(p []byte) (int, error) {
	i := 0
	if o.pr.givenBack != nil {
		i1, err := o.pr.givenBack.Read(p)
		if err == nil {
			i += i1
		} else {
			o.pr.givenBack = nil
		}
	}

	i2, err := o.pr.r.Read(p[i:])
	i += i2
	o.counter += i
	if err == nil && o.pr.startedGivingBack {
		o.pr.givebackBuffer = append(o.pr.givebackBuffer, p[:i]...)
	}
	return i, err
}

func (o *objectReader) ReadByte() (byte, error) {
	var c byte
	if err := binary.Read(o, binary.BigEndian, &c); err != nil {
		return 0, err
	}

	return c, nil
}

type ReaderError struct {
	Msg string // description of error
}

func NewError(format string, args ...interface{}) error {
	return &ReaderError{Msg: fmt.Sprintf(format, args...)}
}

func (e *ReaderError) Error() string { return e.Msg }

var ErrZlibHeader = errors.New("zlib: invalid header")