diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..d680b2b --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @rjzak diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..6e57f06 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,14 @@ +version: 2 +updates: +- package-ecosystem: "cargo" + directory: "/" + schedule: + interval: "daily" + reviewers: + - "rjzak" +- package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + reviewers: + - "rjzak" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..c39ef3c --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,16 @@ +name: Test +on: [ push, pull_request ] +jobs: + native: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Rust toolchain + run: rustup show && rustup update + - name: Build murmurhash3 + run: cargo build + - name: cargo test + uses: actions-rs/cargo@v1 + with: + command: test + args: --workspace diff --git a/.gitignore b/.gitignore index 4fffb2f..837949f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +/.idea /target /Cargo.lock diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 69c1b7f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: rust -rust: - - stable - - beta - - nightly - -matrix: - include: - - rust: nightly - env: FEATURES=nightly BENCH=true - -script: - - cargo build --verbose --features ${FEATURES:-""} - - cargo test --verbose --features ${FEATURES:-""} - - '[ "$BENCH" == "true" ] && cargo bench --no-run --verbose --features ${FEATURES:-""} || true' diff --git a/Cargo.toml b/Cargo.toml index dfed0e7..9f3151f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] - -name = "murmurhash3" -version = "0.0.6" -authors = ["mhallin "] +name = "malwaredb-murmurhash3" +version = "0.1.0" +authors = ["mhallin ", "Richard Zak "] +edition = "2021" description = "MurmurHash3 implementation" license = "MIT" readme = "README.rst" diff --git a/src/hasher.rs b/src/hasher.rs index 7ecf363..67c09d7 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -1,30 +1,26 @@ use std::hash::{BuildHasher, Hasher}; -use mmh3_32::murmurhash3_x86_32; +use crate::mmh3_32::murmurhash3_x86_32; pub struct Murmur3Hasher { seed: u32, bytes: Vec, } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Default)] pub struct Murmur3HashState { seed: u32, } impl Murmur3HashState { - pub fn new() -> Murmur3HashState { - return Murmur3HashState { seed: 0 }; - } - pub fn with_seed(seed: u32) -> Murmur3HashState { - return Murmur3HashState { seed }; + Murmur3HashState { seed } } } impl Hasher for Murmur3Hasher { fn finish(&self) -> u64 { - return murmurhash3_x86_32(&self.bytes, self.seed) as u64; + murmurhash3_x86_32(&self.bytes, self.seed) as u64 } fn write(&mut self, bytes: &[u8]) { @@ -51,7 +47,7 @@ mod test { #[test] fn use_in_hashmap() { - let mut hashmap = HashMap::with_capacity_and_hasher(0, Murmur3HashState::new()); + let mut hashmap = HashMap::with_capacity_and_hasher(0, Murmur3HashState::default()); hashmap.insert("one", 1); hashmap.insert("two", 2); diff --git a/src/mmh3_128.rs b/src/mmh3_128.rs index 22a1933..da01943 100644 --- a/src/mmh3_128.rs +++ b/src/mmh3_128.rs @@ -7,13 +7,13 @@ fn fmix64(mut k: u64) -> u64 { k = k.wrapping_mul(0xc4ceb9fe1a85ec53u64); k ^= k >> 33; - return k; + k } fn get_128_block(bytes: &[u8], index: usize) -> (u64, u64) { let b64: &[u64] = unsafe { mem::transmute(bytes) }; - return (b64[index], b64[index + 1]); + (b64[index], b64[index + 1]) } pub fn murmurhash3_x64_128(bytes: &[u8], seed: u64) -> (u64, u64) { @@ -99,7 +99,7 @@ pub fn murmurhash3_x64_128(bytes: &[u8], seed: u64) -> (u64, u64) { k1 ^= (bytes[(block_count * read_size) as usize + 1] as u64) << 8; } if len & 15 >= 1 { - k1 ^= bytes[(block_count * read_size) as usize + 0] as u64; + k1 ^= bytes[(block_count * read_size) as usize] as u64; k1 = k1.wrapping_mul(c1); k1 = k1.rotate_left(31); k1 = k1.wrapping_mul(c2); @@ -118,7 +118,7 @@ pub fn murmurhash3_x64_128(bytes: &[u8], seed: u64) -> (u64, u64) { h1 = h1.wrapping_add(h2); h2 = h2.wrapping_add(h1); - return (h1, h2); + (h1, h2) } #[cfg(test)] @@ -127,77 +127,80 @@ mod test { #[test] fn test_empty_string() { - assert!(murmurhash3_x64_128("".as_bytes(), 0) == (0, 0)); + assert_eq!(murmurhash3_x64_128("".as_bytes(), 0), (0, 0)); } #[test] fn test_tail_lengths() { - assert!( - murmurhash3_x64_128("1".as_bytes(), 0) == (8213365047359667313, 10676604921780958775) + assert_eq!( + murmurhash3_x64_128("1".as_bytes(), 0), + (8213365047359667313, 10676604921780958775) ); - assert!( - murmurhash3_x64_128("12".as_bytes(), 0) == (5355690773644049813, 9855895140584599837) + assert_eq!( + murmurhash3_x64_128("12".as_bytes(), 0), + (5355690773644049813, 9855895140584599837) ); - assert!( - murmurhash3_x64_128("123".as_bytes(), 0) == (10978418110857903978, 4791445053355511657) + assert_eq!( + murmurhash3_x64_128("123".as_bytes(), 0), + (10978418110857903978, 4791445053355511657) ); - assert!( - murmurhash3_x64_128("1234".as_bytes(), 0) == (619023178690193332, 3755592904005385637) + assert_eq!( + murmurhash3_x64_128("1234".as_bytes(), 0), + (619023178690193332, 3755592904005385637) ); - assert!( - murmurhash3_x64_128("12345".as_bytes(), 0) - == (2375712675693977547, 17382870096830835188) + assert_eq!( + murmurhash3_x64_128("12345".as_bytes(), 0), + (2375712675693977547, 17382870096830835188) ); - assert!( - murmurhash3_x64_128("123456".as_bytes(), 0) - == (16435832985690558678, 5882968373513761278) + assert_eq!( + murmurhash3_x64_128("123456".as_bytes(), 0), + (16435832985690558678, 5882968373513761278) ); - assert!( - murmurhash3_x64_128("1234567".as_bytes(), 0) - == (3232113351312417698, 4025181827808483669) + assert_eq!( + murmurhash3_x64_128("1234567".as_bytes(), 0), + (3232113351312417698, 4025181827808483669) ); - assert!( - murmurhash3_x64_128("12345678".as_bytes(), 0) - == (4272337174398058908, 10464973996478965079) + assert_eq!( + murmurhash3_x64_128("12345678".as_bytes(), 0), + (4272337174398058908, 10464973996478965079) ); - assert!( - murmurhash3_x64_128("123456789".as_bytes(), 0) - == (4360720697772133540, 11094893415607738629) + assert_eq!( + murmurhash3_x64_128("123456789".as_bytes(), 0), + (4360720697772133540, 11094893415607738629) ); - assert!( - murmurhash3_x64_128("123456789a".as_bytes(), 0) - == (12594836289594257748, 2662019112679848245) + assert_eq!( + murmurhash3_x64_128("123456789a".as_bytes(), 0), + (12594836289594257748, 2662019112679848245) ); - assert!( - murmurhash3_x64_128("123456789ab".as_bytes(), 0) - == (6978636991469537545, 12243090730442643750) + assert_eq!( + murmurhash3_x64_128("123456789ab".as_bytes(), 0), + (6978636991469537545, 12243090730442643750) ); - assert!( - murmurhash3_x64_128("123456789abc".as_bytes(), 0) - == (211890993682310078, 16480638721813329343) + assert_eq!( + murmurhash3_x64_128("123456789abc".as_bytes(), 0), + (211890993682310078, 16480638721813329343) ); - assert!( - murmurhash3_x64_128("123456789abcd".as_bytes(), 0) - == (12459781455342427559, 3193214493011213179) + assert_eq!( + murmurhash3_x64_128("123456789abcd".as_bytes(), 0), + (12459781455342427559, 3193214493011213179) ); - assert!( - murmurhash3_x64_128("123456789abcde".as_bytes(), 0) - == (12538342858731408721, 9820739847336455216) + assert_eq!( + murmurhash3_x64_128("123456789abcde".as_bytes(), 0), + (12538342858731408721, 9820739847336455216) ); - assert!( - murmurhash3_x64_128("123456789abcdef".as_bytes(), 0) - == (9165946068217512774, 2451472574052603025) + assert_eq!( + murmurhash3_x64_128("123456789abcdef".as_bytes(), 0), + (9165946068217512774, 2451472574052603025) ); - assert!( - murmurhash3_x64_128("123456789abcdef1".as_bytes(), 0) - == (9259082041050667785, 12459473952842597282) + assert_eq!( + murmurhash3_x64_128("123456789abcdef1".as_bytes(), 0), + (9259082041050667785, 12459473952842597282) ); } #[test] fn test_large_data() { - assert!(murmurhash3_x64_128("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0) - == (9455322759164802692, 17863277201603478371)); + assert_eq!(murmurhash3_x64_128("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0), (9455322759164802692, 17863277201603478371)); } #[cfg(feature = "nightly")] diff --git a/src/mmh3_32.rs b/src/mmh3_32.rs index 5f5abc6..fde962c 100644 --- a/src/mmh3_32.rs +++ b/src/mmh3_32.rs @@ -7,13 +7,13 @@ fn fmix32(mut h: u32) -> u32 { h = h.wrapping_mul(0xc2b2ae35); h ^= h >> 16; - return h; + h } fn get_32_block(bytes: &[u8], index: usize) -> u32 { let b32: &[u32] = unsafe { mem::transmute(bytes) }; - return b32[index]; + b32[index] } pub fn murmurhash3_x86_32(bytes: &[u8], seed: u32) -> u32 { @@ -46,7 +46,7 @@ pub fn murmurhash3_x86_32(bytes: &[u8], seed: u32) -> u32 { k1 ^= (bytes[(block_count * read_size) as usize + 1] as u32) << 8; } if len & 3 >= 1 { - k1 ^= bytes[(block_count * read_size) as usize + 0] as u32; + k1 ^= bytes[(block_count * read_size) as usize] as u32; k1 = k1.wrapping_mul(c1); k1 = k1.rotate_left(15); k1 = k1.wrapping_mul(c2); @@ -56,7 +56,7 @@ pub fn murmurhash3_x86_32(bytes: &[u8], seed: u32) -> u32 { h1 ^= bytes.len() as u32; h1 = fmix32(h1); - return h1; + h1 } #[cfg(test)] @@ -65,21 +65,20 @@ mod test { #[test] fn test_empty_string() { - assert!(murmurhash3_x86_32("".as_bytes(), 0) == 0); + assert_eq!(murmurhash3_x86_32("".as_bytes(), 0), 0); } #[test] fn test_tail_lengths() { - assert!(murmurhash3_x86_32("1".as_bytes(), 0) == 2484513939); - assert!(murmurhash3_x86_32("12".as_bytes(), 0) == 4191350549); - assert!(murmurhash3_x86_32("123".as_bytes(), 0) == 2662625771); - assert!(murmurhash3_x86_32("1234".as_bytes(), 0) == 1914461635); + assert_eq!(murmurhash3_x86_32("1".as_bytes(), 0), 2484513939); + assert_eq!(murmurhash3_x86_32("12".as_bytes(), 0), 4191350549); + assert_eq!(murmurhash3_x86_32("123".as_bytes(), 0), 2662625771); + assert_eq!(murmurhash3_x86_32("1234".as_bytes(), 0), 1914461635); } #[test] fn test_large_data() { - assert!(murmurhash3_x86_32("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0) - == 1004899618); + assert_eq!(murmurhash3_x86_32("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at consequat massa. Cras eleifend pellentesque ex, at dignissim libero maximus ut. Sed eget nulla felis".as_bytes(), 0), 1004899618); } #[cfg(feature = "nightly")]