/
h2.go
47 lines (38 loc) · 888 Bytes
/
h2.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
package brotli
import "encoding/binary"
// H2 is a Hasher similar to what the reference implementation of brotli
// uses for compression level 2.
type H2 struct {
table []uint32
}
const (
h2TableBits = 16
h2HashLen = 5
)
func (h *H2) Init() {
tableLen := 1 << h2TableBits
if len(h.table) < tableLen {
h.table = make([]uint32, tableLen)
} else {
for i := range h.table {
h.table[i] = 0
}
}
}
func (h *H2) hash(data []byte) uint64 {
hash := (binary.LittleEndian.Uint64(data) << (64 - 8*h2HashLen)) * kHashMul64
return hash >> (64 - h2TableBits)
}
func (h *H2) Store(data []byte, index int) {
hash := h.hash(data[index:])
h.table[hash] = uint32(index)
}
func (h *H2) Candidates(dst []int, data []byte, index int) []int {
hash := h.hash(data[index:])
c := h.table[hash]
if c != 0 {
dst = append(dst, int(c))
}
h.table[hash] = uint32(index)
return dst
}