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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//!Safe bindings to [xxHash](https://github.com/Cyan4973/xxHash)

#![no_std]
#![deny(warnings)]
#![warn(missing_docs)]
#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]

use xxhash_c_sys as sys;

use core::{hash, mem};

#[inline(always)]
///Calculates 32bit hash of provided `input`
///
///Optimal on 32bit targets.
pub fn xxh32(input: &[u8], seed: u32) -> u32 {
    unsafe {
        sys::XXH32(input.as_ptr() as _, input.len(), seed)
    }
}

#[inline(always)]
///Calculates 64bit hash of provided `input`
///
///Runs faster on 64bit systems, and slower on 32bit systems
pub fn xxh64(input: &[u8], seed: u64) -> u64 {
    unsafe {
        sys::XXH64(input.as_ptr() as _, input.len(), seed)
    }
}

#[inline(always)]
///Calculates 64bit hash of provided `input` using XXH3.
///
///Runs faster on 64bit systems and generally faster comparing to `xxh64`.
pub fn xxh3_64(input: &[u8]) -> u64 {
    unsafe {
        sys::XXH3_64bits(input.as_ptr() as _, input.len())
    }
}

#[inline(always)]
///Calculates 128bit hash of provided `input` using XXH3.
///
///Generally the same algorithm as 64bit version, but allowing to get 128bit output.
pub fn xxh3_128(input: &[u8]) -> u128 {
    let result = unsafe {
        sys::XXH3_128bits(input.as_ptr() as _, input.len())
    };

    (result.high64 as u128) << 64 | result.low64 as u128
}

///Streaming version of `XXH64` algorithm.
pub struct XXH64 {
    state: mem::MaybeUninit<sys::XXH64_state_t>,
}

impl XXH64 {
    #[inline]
    ///Creates uninitialized instance.
    ///
    ///It is unsafe to use any method before calling `reset`
    pub const unsafe fn uninit() -> Self {
        let state = mem::MaybeUninit::uninit();
        Self {
            state
        }
    }

    #[inline]
    ///Creates new instance.
    pub fn new(seed: u64) -> Self {
        let mut result = unsafe {
            Self::uninit()
        };

        result.reset(seed);

        result
    }

    #[inline]
    ///Resets hasher's state.
    pub fn reset(&mut self, seed: u64) {
        let result = unsafe { sys::XXH64_reset(self.state.as_mut_ptr(), seed) };
        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

impl hash::Hasher for XXH64 {
    #[inline]
    fn finish(&self) -> u64 {
        unsafe {
            sys::XXH64_digest(self.state.as_ptr())
        }
    }

    #[inline]
    fn write(&mut self, input: &[u8]) {
        let result = unsafe {
            sys::XXH64_update(self.state.as_mut_ptr(), input.as_ptr() as _, input.len())
        };

        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

impl Default for XXH64 {
    #[inline(always)]
    fn default() -> Self {
        Self::new(0)
    }
}

///Describes method to reset XXH3 algorithm state.
///
///Policies:
///- [Default](html.Xxh3DefaultReset.struct) - requires nothing and just resets using default values.
///- Seed - updates with `u64` seed.
///- Secret - updates with specified slice of bytes. It should be no less than `xxhash_c_sys::XXH3_SECRET_SIZE_MIN`
pub trait Xxh3Reset {
    ///Reset implementation
    fn reset(self, state: *mut sys::XXH3_state_t);
}

///Default reset policy.
pub struct Xxh3DefaultReset;

impl Xxh3Reset for Xxh3DefaultReset {
    fn reset(self, state: *mut sys::XXH3_state_t) {
        let result = unsafe { sys::XXH3_64bits_reset(state) };
        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

impl Xxh3Reset for u64 {
    fn reset(self, state: *mut sys::XXH3_state_t) {
        let result = unsafe { sys::XXH3_64bits_reset_withSeed(state, self) };
        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

impl Xxh3Reset for &'_ [u8] {
    fn reset(self, state: *mut sys::XXH3_state_t) {
        debug_assert!(self.len() >= xxhash_c_sys::XXH3_SECRET_SIZE_MIN as usize);
        let result = unsafe { sys::XXH3_64bits_reset_withSecret(state, self.as_ptr() as _, self.len()) };
        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

///Streaming version of `XXH3` 64 bit algorithm.
///
///*NOTE:* state is rather large for `XXH3` so it is advised to allocate it on heap if you plan to move it around.
pub struct XXH3_64 {
    state: mem::MaybeUninit<sys::XXH3_state_t>,
}

impl XXH3_64 {
    #[inline]
    ///Creates uninitialized instance.
    ///
    ///It is unsafe to use any method before calling `reset`
    pub const unsafe fn uninit() -> Self {
        let state = mem::MaybeUninit::uninit();
        Self {
            state
        }
    }

    #[inline]
    ///Creates new instance.
    pub fn new() -> Self {
        let mut result = unsafe {
            Self::uninit()
        };

        result.reset(Xxh3DefaultReset);

        result
    }

    #[inline(always)]
    ///Resets hasher's state according to specified reset policy.
    pub fn reset<R: Xxh3Reset>(&mut self, reset: R) {
        reset.reset(self.state.as_mut_ptr());
    }
}

impl hash::Hasher for XXH3_64 {
    #[inline]
    fn finish(&self) -> u64 {
        unsafe {
            sys::XXH3_64bits_digest(self.state.as_ptr())
        }
    }

    #[inline]
    fn write(&mut self, input: &[u8]) {
        let result = unsafe {
            sys::XXH3_64bits_update(self.state.as_mut_ptr(), input.as_ptr() as _, input.len())
        };

        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

impl Default for XXH3_64 {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

///Streaming version of `XXH3` 128 bit algorithm.
pub struct XXH3_128 {
    state: mem::MaybeUninit<sys::XXH3_state_t>,
}

impl XXH3_128 {
    #[inline]
    ///Creates uninitialized instance.
    ///
    ///It is unsafe to use any method before calling `reset`
    pub const unsafe fn uninit() -> Self {
        let state = mem::MaybeUninit::uninit();
        Self { state }
    }

    #[inline]
    ///Creates new instance.
    pub fn new() -> Self {
        let mut result = unsafe { Self::uninit() };

        result.reset(Xxh3DefaultReset);

        result
    }

    #[inline(always)]
    ///Resets hasher's state according to specified reset policy.
    pub fn reset<R: Xxh3Reset>(&mut self, reset: R) {
        reset.reset(self.state.as_mut_ptr());
    }

    #[inline]
    ///Calculates hash's result
    pub fn finish(&self) -> u128 {
        let result = unsafe { sys::XXH3_128bits_digest(self.state.as_ptr()) };
        (result.high64 as u128) << 64 | result.low64 as u128
    }

    #[inline]
    ///Adds new chunk of data to the hash.
    pub fn write(&mut self, input: &[u8]) {
        let result = unsafe {
            sys::XXH3_128bits_update(self.state.as_mut_ptr(), input.as_ptr() as _, input.len())
        };

        debug_assert_eq!(result, sys::XXH_errorcode_XXH_OK);
    }
}

impl Default for XXH3_128 {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}