-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal-basetypes.go
More file actions
119 lines (109 loc) · 2.4 KB
/
Copy pathjournal-basetypes.go
File metadata and controls
119 lines (109 loc) · 2.4 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2014 Peter Lemenkov <lemenkov@gmail.com>
package journal
// SDID128 represents a 128-bit systemd identifier (file_id, machine_id, boot_id, seqnum_id).
type SDID128 [16]byte
// JenkinsHash computes the Jenkins lookup3 hashlittle2 hash used by journal files
// without the KEYED_HASH incompatible flag.
// Returns (uint64(pc) << 32) | uint64(pb) per the journal file format spec.
func JenkinsHash(data []byte) uint64 {
pc, pb := jenkinsHashlittle2(data, 0, 0)
return uint64(pc)<<32 | uint64(pb)
}
func rot32(x, k uint32) uint32 {
return (x << k) | (x >> (32 - k))
}
func jenkinsMix(a, b, c uint32) (uint32, uint32, uint32) {
a -= c
a ^= rot32(c, 4)
c += b
b -= a
b ^= rot32(a, 6)
a += c
c -= b
c ^= rot32(b, 8)
b += a
a -= c
a ^= rot32(c, 16)
c += b
b -= a
b ^= rot32(a, 19)
a += c
c -= b
c ^= rot32(b, 4)
b += a
return a, b, c
}
func jenkinsFinal(a, b, c uint32) (uint32, uint32, uint32) {
c ^= b
c -= rot32(b, 14)
a ^= c
a -= rot32(c, 11)
b ^= a
b -= rot32(a, 25)
c ^= b
c -= rot32(b, 16)
a ^= c
a -= rot32(c, 4)
b ^= a
b -= rot32(a, 14)
c ^= b
c -= rot32(b, 24)
return a, b, c
}
// jenkinsHashlittle2 is a Go translation of Bob Jenkins' hashlittle2() from lookup3.c.
func jenkinsHashlittle2(data []byte, initPC, initPB uint32) (uint32, uint32) {
length := len(data)
a := uint32(0xdeadbeef) + uint32(length) + initPC
b := a
c := a + initPB
i := 0
for length > 12 {
a += uint32(data[i]) | uint32(data[i+1])<<8 | uint32(data[i+2])<<16 | uint32(data[i+3])<<24
b += uint32(data[i+4]) | uint32(data[i+5])<<8 | uint32(data[i+6])<<16 | uint32(data[i+7])<<24
c += uint32(data[i+8]) | uint32(data[i+9])<<8 | uint32(data[i+10])<<16 | uint32(data[i+11])<<24
a, b, c = jenkinsMix(a, b, c)
i += 12
length -= 12
}
k := data[i:]
switch length {
case 12:
c += uint32(k[11]) << 24
fallthrough
case 11:
c += uint32(k[10]) << 16
fallthrough
case 10:
c += uint32(k[9]) << 8
fallthrough
case 9:
c += uint32(k[8])
fallthrough
case 8:
b += uint32(k[7]) << 24
fallthrough
case 7:
b += uint32(k[6]) << 16
fallthrough
case 6:
b += uint32(k[5]) << 8
fallthrough
case 5:
b += uint32(k[4])
fallthrough
case 4:
a += uint32(k[3]) << 24
fallthrough
case 3:
a += uint32(k[2]) << 16
fallthrough
case 2:
a += uint32(k[1]) << 8
fallthrough
case 1:
a += uint32(k[0])
a, b, c = jenkinsFinal(a, b, c)
}
return c, b
}