-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathxxhash.lua
81 lines (62 loc) · 1.6 KB
/
xxhash.lua
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
--[=[
XXHASH: extremely fast non-cryptographic hash.
Written by Cosmin Apreutesei. Public Domain.
xxhash32|64|128(data[, len[, seed]]) -> hash
Compute a 32|64|128 bit hash.
xxhash128_digest([seed]) -> st
st:reset()
st:update(s | buf,sz)
st:hash() -> hash
st:free()
]=]
local ffi = require'ffi'
local bit = require'bit'
require'xxhash_h'
local C = ffi.load'xxhash'
function xxhash32 (data, sz, seed) return C.XXH32 (data, sz or #data, seed or 0) end
function xxhash64 (data, sz, seed) return C.XXH64 (data, sz or #data, seed or 0) end
function xxhash128(data, sz, seed) return C.XXH128(data, sz or #data, seed or 0) end
local h = {}
function h:bin()
return ffi.string(self, 16)
end
function h:hex()
local h = bit.tohex
return
h(self.u32[0])..
h(self.u32[1])..
h(self.u32[2])..
h(self.u32[3])
end
ffi.metatype('XXH128_hash_t', {__index = h})
local st = {}
local st_meta = {__index = st}
function st:free()
assert(C.XXH3_freeState(self) == 0)
end
st_meta.__gc = st.free
function st:reset(seed)
assert(C.XXH3_128bits_reset_withSeed(self, seed or 0) == 0)
return self
end
function st:update(s, len)
assert(C.XXH3_128bits_update(self, s, len or #s) == 0)
return self
end
function st:hash()
return C.XXH3_128bits_digest(self)
end
function xxhash128_digest(seed)
local st = C.XXH3_createState()
assert(st ~= nil)
return st:reset(seed)
end
ffi.metatype('XXH3_state_t', st_meta)
if not ... then --self-test
local st = xxhash128_digest()
st:update('abcd')
st:update('1324')
assert(st:hash():bin() == xxhash128('abcd1324'):bin())
assert(st:hash():hex() == xxhash128('abcd1324'):hex())
print'ok'
end