From baab5e580d33dfe5143e80c3efa1aa628d3dc084 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 29 Oct 2024 14:04:52 +1100 Subject: [PATCH] hashes: Move private macro We have two files one for public macros and one for private macros. Move the `engine_input_impl` macro to the private macros file. Requires change to call sites because we do not have `use_macros` attribute on the `internal_macros` file. --- hashes/src/internal_macros.rs | 31 +++++++++++++++++++++++++++++++ hashes/src/macros.rs | 30 ------------------------------ hashes/src/ripemd160.rs | 2 +- hashes/src/sha1.rs | 2 +- hashes/src/sha256.rs | 2 +- hashes/src/sha512.rs | 2 +- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index aea52dfd0..c17c8f1a8 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -253,3 +253,34 @@ macro_rules! impl_io_write { } } pub(crate) use impl_io_write; + +macro_rules! engine_input_impl( + () => ( + #[cfg(not(hashes_fuzz))] + fn input(&mut self, mut inp: &[u8]) { + + while !inp.is_empty() { + let buf_idx = $crate::incomplete_block_len(self); + let rem_len = ::BLOCK_SIZE - buf_idx; + let write_len = cmp::min(rem_len, inp.len()); + + self.buffer[buf_idx..buf_idx + write_len] + .copy_from_slice(&inp[..write_len]); + self.bytes_hashed += write_len as u64; + if $crate::incomplete_block_len(self) == 0 { + self.process_block(); + } + inp = &inp[write_len..]; + } + } + + #[cfg(hashes_fuzz)] + fn input(&mut self, inp: &[u8]) { + for c in inp { + self.buffer[0] ^= *c; + } + self.bytes_hashed += inp.len() as u64; + } + ) +); +pub(crate) use engine_input_impl; diff --git a/hashes/src/macros.rs b/hashes/src/macros.rs index 2b5113818..45b7e9f5a 100644 --- a/hashes/src/macros.rs +++ b/hashes/src/macros.rs @@ -70,36 +70,6 @@ macro_rules! borrow_slice_impl( ) ); -macro_rules! engine_input_impl( - () => ( - #[cfg(not(hashes_fuzz))] - fn input(&mut self, mut inp: &[u8]) { - - while !inp.is_empty() { - let buf_idx = $crate::incomplete_block_len(self); - let rem_len = ::BLOCK_SIZE - buf_idx; - let write_len = cmp::min(rem_len, inp.len()); - - self.buffer[buf_idx..buf_idx + write_len] - .copy_from_slice(&inp[..write_len]); - self.bytes_hashed += write_len as u64; - if $crate::incomplete_block_len(self) == 0 { - self.process_block(); - } - inp = &inp[write_len..]; - } - } - - #[cfg(hashes_fuzz)] - fn input(&mut self, inp: &[u8]) { - for c in inp { - self.buffer[0] ^= *c; - } - self.bytes_hashed += inp.len() as u64; - } - ) -); - /// Creates a new newtype around a [`Hash`] type. /// /// The syntax is similar to the usual tuple struct syntax: diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index d77a2d928..3732cfb0b 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -85,7 +85,7 @@ impl crate::HashEngine for HashEngine { fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - engine_input_impl!(); + crate::internal_macros::engine_input_impl!(); } #[cfg(feature = "small-hash")] diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index b028dd75a..134a6178a 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -77,7 +77,7 @@ impl crate::HashEngine for HashEngine { fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - engine_input_impl!(); + crate::internal_macros::engine_input_impl!(); } impl HashEngine { diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 5b85bac4b..7bd2576db 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -132,7 +132,7 @@ impl crate::HashEngine for HashEngine { fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - engine_input_impl!(); + crate::internal_macros::engine_input_impl!(); } impl Hash { diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index 0b3168133..793687abf 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -118,7 +118,7 @@ impl crate::HashEngine for HashEngine { fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed } - engine_input_impl!(); + crate::internal_macros::engine_input_impl!(); } #[allow(non_snake_case)]