From 2e4896cc7ca45ddba63301de73c377bdf9db1d50 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 23 Aug 2023 10:52:04 +0200 Subject: [PATCH] Revert "Hasher::write_u*() are expected keep the native endianness" This reverts commit e5264daafb255555180ce2db42343d5f617d55f8. --- src/sip.rs | 12 +++++++++--- src/sip128.rs | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/sip.rs b/src/sip.rs index 4ce2c1f..2e1e307 100644 --- a/src/sip.rs +++ b/src/sip.rs @@ -296,6 +296,12 @@ impl Hasher { // The hashing of multi-byte integers depends on endianness. E.g.: // - little-endian: `write_u32(0xDDCCBBAA)` == `write([0xAA, 0xBB, 0xCC, 0xDD])` // - big-endian: `write_u32(0xDDCCBBAA)` == `write([0xDD, 0xCC, 0xBB, 0xAA])` + // + // This function does the right thing for little-endian hardware. On + // big-endian hardware `x` must be byte-swapped first to give the right + // behaviour. After any byte-swapping, the input must be zero-extended to + // 64-bits. The caller is responsible for the byte-swapping and + // zero-extension. #[inline] fn short_write(&mut self, _x: T, x: u64) { let size = mem::size_of::(); @@ -437,7 +443,7 @@ impl hash::Hasher for SipHasher24 { impl hash::Hasher for Hasher { #[inline] fn write_usize(&mut self, i: usize) { - self.short_write(i, i as u64); + self.short_write(i, i.to_le() as u64); } #[inline] @@ -447,12 +453,12 @@ impl hash::Hasher for Hasher { #[inline] fn write_u32(&mut self, i: u32) { - self.short_write(i, i as u64); + self.short_write(i, i.to_le() as u64); } #[inline] fn write_u64(&mut self, i: u64) { - self.short_write(i, i); + self.short_write(i, i.to_le()); } #[inline] diff --git a/src/sip128.rs b/src/sip128.rs index a953783..5ed14e8 100644 --- a/src/sip128.rs +++ b/src/sip128.rs @@ -341,6 +341,12 @@ impl Hasher { // The hashing of multi-byte integers depends on endianness. E.g.: // - little-endian: `write_u32(0xDDCCBBAA)` == `write([0xAA, 0xBB, 0xCC, 0xDD])` // - big-endian: `write_u32(0xDDCCBBAA)` == `write([0xDD, 0xCC, 0xBB, 0xAA])` + // + // This function does the right thing for little-endian hardware. On + // big-endian hardware `x` must be byte-swapped first to give the right + // behaviour. After any byte-swapping, the input must be zero-extended to + // 64-bits. The caller is responsible for the byte-swapping and + // zero-extension. #[inline] fn short_write(&mut self, _x: T, x: u64) { let size = mem::size_of::(); @@ -505,7 +511,7 @@ impl hash::Hasher for SipHasher24 { impl hash::Hasher for Hasher { #[inline] fn write_usize(&mut self, i: usize) { - self.short_write(i, i as u64); + self.short_write(i, i.to_le() as u64); } #[inline] @@ -515,12 +521,12 @@ impl hash::Hasher for Hasher { #[inline] fn write_u32(&mut self, i: u32) { - self.short_write(i, i as u64); + self.short_write(i, i.to_le() as u64); } #[inline] fn write_u64(&mut self, i: u64) { - self.short_write(i, i); + self.short_write(i, i.to_le()); } #[inline]