Skip to content

Commit

Permalink
Revert "Hasher::write_u*() are expected keep the native endianness"
Browse files Browse the repository at this point in the history
This reverts commit e5264da.
  • Loading branch information
jedisct1 committed Aug 23, 2023
1 parent 0159101 commit 2e4896c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ impl<S: Sip> Hasher<S> {
// 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<T>(&mut self, _x: T, x: u64) {
let size = mem::size_of::<T>();
Expand Down Expand Up @@ -437,7 +443,7 @@ impl hash::Hasher for SipHasher24 {
impl<S: Sip> hash::Hasher for Hasher<S> {
#[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]
Expand All @@ -447,12 +453,12 @@ impl<S: Sip> hash::Hasher for Hasher<S> {

#[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]
Expand Down
12 changes: 9 additions & 3 deletions src/sip128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ impl<S: Sip> Hasher<S> {
// 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<T>(&mut self, _x: T, x: u64) {
let size = mem::size_of::<T>();
Expand Down Expand Up @@ -505,7 +511,7 @@ impl hash::Hasher for SipHasher24 {
impl<S: Sip> hash::Hasher for Hasher<S> {
#[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]
Expand All @@ -515,12 +521,12 @@ impl<S: Sip> hash::Hasher for Hasher<S> {

#[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]
Expand Down

0 comments on commit 2e4896c

Please sign in to comment.