aboutsummaryrefslogtreecommitdiff
path: root/format/pack/pack.go
blob: e87e33600c2e97c2a1e6acee97688d808ee03ddd (about) (plain) (blame)
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
// Package pack provides Git packfile format parsing primitives.
package pack

import (
	"fmt"

	"codeberg.org/lindenii/furgit/objecttype"
)

// Signature is the 4-byte "PACK" magic at the start of pack files.
const Signature = 0x5041434b

// VersionSupported reports whether one pack version is supported.
func VersionSupported(version uint32) bool {
	return version == 2 || version == 3
}

// IsBaseObjectType reports whether ty is one of the four canonical object
// types encoded directly in pack entries.
func IsBaseObjectType(ty objecttype.Type) bool {
	switch ty {
	case objecttype.TypeCommit, objecttype.TypeTree, objecttype.TypeBlob, objecttype.TypeTag:
		return true
	case objecttype.TypeInvalid, objecttype.TypeFuture, objecttype.TypeOfsDelta, objecttype.TypeRefDelta:
		return false
	default:
		return false
	}
}

// ParseOfsDeltaDistance parses one ofs-delta backward distance.
func ParseOfsDeltaDistance(buf []byte) (uint64, int, error) {
	if len(buf) == 0 {
		return 0, 0, fmt.Errorf("format/pack: malformed ofs-delta distance")
	}

	b := buf[0]
	dist := uint64(b & 0x7f)

	consumed := 1
	for b&0x80 != 0 {
		if consumed >= len(buf) {
			return 0, 0, fmt.Errorf("format/pack: malformed ofs-delta distance")
		}

		b = buf[consumed]
		consumed++
		dist = ((dist + 1) << 7) + uint64(b&0x7f)
	}

	return dist, consumed, nil
}