aboutsummaryrefslogtreecommitdiff
path: root/format/commitgraph/bloom/settings.go
blob: 764653bd23600d34b37653b741f0cbb75a31712c (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
package bloom

import (
	"encoding/binary"

	"codeberg.org/lindenii/furgit/internal/intconv"
)

// Settings describe the changed-paths Bloom filter parameters stored in
// commit-graph BDAT chunks.
//
// Obviously, they must match the repository's commit-graph settings to
// interpret filters correctly.
type Settings struct {
	HashVersion    uint32
	NumHashes      uint32
	BitsPerEntry   uint32
	MaxChangePaths uint32
}

// ParseSettings reads Bloom filter settings from a BDAT chunk header.
func ParseSettings(bdat []byte) (*Settings, error) {
	if len(bdat) < DataHeaderSize {
		return nil, ErrInvalid
	}

	settings := &Settings{
		HashVersion:    binary.BigEndian.Uint32(bdat[0:4]),
		NumHashes:      binary.BigEndian.Uint32(bdat[4:8]),
		BitsPerEntry:   binary.BigEndian.Uint32(bdat[8:12]),
		MaxChangePaths: DefaultMaxChange,
	}

	switch settings.HashVersion {
	case 1, 2:
	default:
		return nil, ErrInvalid
	}

	if settings.NumHashes == 0 {
		return nil, ErrInvalid
	}

	_, err := intconv.Uint32ToInt(settings.NumHashes)
	if err != nil {
		return nil, ErrInvalid
	}

	return settings, nil
}