aboutsummaryrefslogtreecommitdiff
path: root/internal/format/packfile/header.go
blob: 64c7d140b42b3c716dc0246385af4e9bd1d1e3bf (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
53
54
55
56
57
58
59
60
61
62
63
package packfile

import (
	"encoding/binary"
	"errors"
	"fmt"
)

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

	// version is the only pack version this package reads or writes.
	version = 2
)

// HeaderLen is the size of a pack header:
// signature, version, and object count.
const HeaderLen = 12

// ErrMalformedHeader reports that
// a pack header is truncated,
// or has a bad signature or unsupported version.
var ErrMalformedHeader = errors.New("internal/format/packfile: malformed pack header")

// Header is one parsed pack header.
type Header struct {
	// ObjectCount is the number of object entries
	// declared to follow the header.
	ObjectCount uint32
}

// ParseHeader parses and validates the pack header
// at the start of data.
func ParseHeader(data []byte) (Header, error) {
	var zero Header

	if len(data) < HeaderLen {
		return zero, fmt.Errorf("%w: truncated", ErrMalformedHeader)
	}

	if binary.BigEndian.Uint32(data[0:4]) != signature {
		return zero, fmt.Errorf("%w: bad signature", ErrMalformedHeader)
	}

	parsedVersion := binary.BigEndian.Uint32(data[4:8])
	if parsedVersion != version {
		return zero, fmt.Errorf("%w: unsupported version %d", ErrMalformedHeader, parsedVersion)
	}

	return Header{
		ObjectCount: binary.BigEndian.Uint32(data[8:12]),
	}, nil
}

// AppendHeader appends an encoded pack header for objectCount to dst.
func AppendHeader(dst []byte, objectCount uint32) []byte {
	dst = binary.BigEndian.AppendUint32(dst, signature)
	dst = binary.BigEndian.AppendUint32(dst, version)
	dst = binary.BigEndian.AppendUint32(dst, objectCount)

	return dst
}