devela/data/hash/fx/
mod.rs

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
// devela::data::hash::fx

use crate::{ConstDefault, Hash, Hasher, HasherBuildDefault};

/// A builder for default Fx hashers.
pub type HasherBuildFx = HasherBuildDefault<HasherFx<usize>>;

/// This hashing algorithm was extracted from the Rustc compiler.
///
/// This is the same hashing algorithm used for some internal operations in
/// Firefox. The strength of this algorithm is in hashing 4 bytes at a time on
/// any platform, where the FNV algorithm works on one byte at a time.
///
/// This hashing algorithm should not be used for cryptographic,
/// or in scenarios where DoS attacks are a concern.
///
/// It's implemented for
/// [u32](#impl-HasherFx<u32>),
/// [u64](#impl-HasherFx<u64>) and
/// [usize](##impl-HasherFx<usize>).
///
/// # Derived Work
#[doc = include_str!("./MODIFICATIONS.md")]
#[must_use]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HasherFx<T> {
    state: T,
}

const ROTATE: u32 = 5;
const SEED32: u32 = 0x9e_37_79_b9;
const SEED64: u64 = 0x51_7c_c1_b7_27_22_0a_95;
#[cfg(target_pointer_width = "32")]
const SEED: usize = SEED32 as usize;
#[cfg(target_pointer_width = "64")]
const SEED: usize = SEED64 as usize;

macro_rules! impl_fx {
    () => { impl_fx![u32:SEED32, u64:SEED64, usize:SEED]; };

    ($($t:ty:$seed:ident),+) =>  { $( impl_fx![@$t:$seed]; )+ };
    (@$t:ty:$seed:ident) =>  {
        impl ConstDefault for HasherFx<$t> { const DEFAULT: Self = Self { state: 0 }; }
        impl Default for HasherFx<$t> { fn default() -> Self { Self::DEFAULT } }

        impl HasherFx<$t> {
            /* state-full methods */

            /// Returns a default Fx hasher.
            pub const fn new() -> Self { Self::DEFAULT }

            /// Returns a new Fx hasher with the given `seed`.
            pub const fn with_seed(seed: $t) -> Self { Self { state: seed } }

            /// A convenience method for when you need a quick hash.
            pub fn hash<T: Hash + ?Sized>(v: &T) -> $t {
                let mut state = Self::new();
                v.hash(&mut state);
                state.state
            }

            /// A const method for when you need a hash of a byte slice.
            pub const fn hash_bytes(v: &[u8]) -> $t {
                Self::write(0, v)
            }

            const fn hash_word(mut hash: $t, word: $t) -> $t {
                hash = hash.rotate_left(ROTATE);
                hash ^= word;
                hash = hash.wrapping_mul($seed);
                hash
            }
        }
    };
}
impl_fx!();

/* impl HasherFx::write */

impl HasherFx<u32> {
    const fn write(mut hash: u32, bytes: &[u8]) -> u32 {
        let mut cursor = 0;
        while bytes.len() - cursor >= 4 {
            let word = u32::from_ne_bytes([
                bytes[cursor],
                bytes[cursor + 1],
                bytes[cursor + 2],
                bytes[cursor + 3],
            ]);
            hash = Self::hash_word(hash, word);
            cursor += 4;
        }
        if bytes.len() - cursor >= 2 {
            let word = u16::from_ne_bytes([bytes[cursor], bytes[cursor + 1]]);
            hash = Self::hash_word(hash, word as u32);
            cursor += 2;
        }
        if bytes.len() - cursor >= 1 {
            hash = Self::hash_word(hash, bytes[cursor] as u32);
        }
        hash
    }
}

impl HasherFx<u64> {
    const fn write(mut hash: u64, bytes: &[u8]) -> u64 {
        let mut cursor = 0;
        while bytes.len() - cursor >= 8 {
            let word = u64::from_ne_bytes([
                bytes[cursor],
                bytes[cursor + 1],
                bytes[cursor + 2],
                bytes[cursor + 3],
                bytes[cursor + 4],
                bytes[cursor + 5],
                bytes[cursor + 6],
                bytes[cursor + 7],
            ]);
            hash = Self::hash_word(hash, word);
            cursor += 8;
        }
        while bytes.len() - cursor >= 4 {
            let word = u32::from_ne_bytes([
                bytes[cursor],
                bytes[cursor + 1],
                bytes[cursor + 2],
                bytes[cursor + 3],
            ]);
            hash = Self::hash_word(hash, word as u64);
            cursor += 4;
        }
        if bytes.len() - cursor >= 2 {
            let word = u16::from_ne_bytes([bytes[cursor], bytes[cursor + 1]]);
            hash = Self::hash_word(hash, word as u64);
            cursor += 2;
        }
        if bytes.len() - cursor >= 1 {
            hash = Self::hash_word(hash, bytes[cursor] as u64);
        }
        hash
    }
}

impl HasherFx<usize> {
    const fn write(hash: usize, bytes: &[u8]) -> usize {
        #[cfg(target_pointer_width = "32")]
        return HasherFx::<u32>::write(hash as u32, bytes) as usize;
        #[cfg(target_pointer_width = "64")]
        return HasherFx::<u64>::write(hash as u64, bytes) as usize;
    }
}

/* impl Hasher for HasherFx */

impl Hasher for HasherFx<u32> {
    fn write(&mut self, bytes: &[u8]) {
        self.state = Self::write(self.state, bytes);
    }
    fn write_u8(&mut self, i: u8) {
        self.state = Self::hash_word(self.state, u32::from(i));
    }
    fn write_u16(&mut self, i: u16) {
        self.state = Self::hash_word(self.state, u32::from(i));
    }
    fn write_u32(&mut self, i: u32) {
        self.state = Self::hash_word(self.state, i);
    }
    fn write_u64(&mut self, i: u64) {
        self.state = Self::hash_word(self.state, i as u32);
        self.state = Self::hash_word(self.state, (i >> 32) as u32);
    }
    fn write_usize(&mut self, i: usize) {
        #[cfg(target_pointer_width = "32")]
        self.write_u32(i as u32);
        #[cfg(target_pointer_width = "64")]
        self.write_u64(i as u64);
    }
    fn finish(&self) -> u64 {
        self.state as u64
    }
}
impl Hasher for HasherFx<u64> {
    fn write(&mut self, bytes: &[u8]) {
        self.state = Self::write(self.state, bytes);
    }
    fn write_u8(&mut self, i: u8) {
        self.state = Self::hash_word(self.state, u64::from(i));
    }
    fn write_u16(&mut self, i: u16) {
        self.state = Self::hash_word(self.state, u64::from(i));
    }
    fn write_u32(&mut self, i: u32) {
        self.state = Self::hash_word(self.state, u64::from(i));
    }
    fn write_u64(&mut self, i: u64) {
        self.state = Self::hash_word(self.state, i);
    }
    fn write_usize(&mut self, i: usize) {
        self.state = Self::hash_word(self.state, i as u64);
    }
    fn finish(&self) -> u64 {
        self.state
    }
}
impl Hasher for HasherFx<usize> {
    fn write(&mut self, bytes: &[u8]) {
        self.state = Self::write(self.state, bytes);
    }
    fn write_u8(&mut self, i: u8) {
        self.state = Self::hash_word(self.state, i as usize);
    }
    fn write_u16(&mut self, i: u16) {
        self.state = Self::hash_word(self.state, i as usize);
    }
    fn write_u32(&mut self, i: u32) {
        self.state = Self::hash_word(self.state, i as usize);
    }
    #[cfg(target_pointer_width = "32")]
    fn write_u64(&mut self, i: u64) {
        self.state = Self::hash_word(self.state, i as usize);
        self.state = Self::hash_word(self.state, (i >> 32) as usize);
    }
    #[cfg(target_pointer_width = "64")]
    fn write_u64(&mut self, i: u64) {
        self.state = Self::hash_word(self.state, i as usize);
    }
    fn write_usize(&mut self, i: usize) {
        self.state = HasherFx::<usize>::hash_word(self.state, i);
    }
    fn finish(&self) -> u64 {
        self.state as u64
    }
}