Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit eac6cde

Browse files
dongcarlapoelstra
authored andcommitted
Add engine_input_impl! macro.
This was previously removed to aid restructuring, but should be added back so we don't have to remember to modify every HashEngine every time we modify the input function.
1 parent 761af28 commit eac6cde

File tree

5 files changed

+34
-93
lines changed

5 files changed

+34
-93
lines changed

src/ripemd160.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,7 @@ impl EngineTrait for HashEngine {
6464

6565
const BLOCK_SIZE: usize = 64;
6666

67-
#[cfg(not(feature = "fuzztarget"))]
68-
fn input(&mut self, mut inp: &[u8]) {
69-
while !inp.is_empty() {
70-
let buf_idx = self.length % <Self as EngineTrait>::BLOCK_SIZE;
71-
let rem_len = <Self as EngineTrait>::BLOCK_SIZE - buf_idx;
72-
let write_len = cmp::min(rem_len, inp.len());
73-
74-
self.buffer[buf_idx..buf_idx + write_len].copy_from_slice(&inp[..write_len]);
75-
self.length += write_len;
76-
if self.length % <Self as EngineTrait>::BLOCK_SIZE == 0 {
77-
self.process_block();
78-
}
79-
inp = &inp[write_len..];
80-
}
81-
}
82-
83-
#[cfg(feature = "fuzztarget")]
84-
fn input(&mut self, inp: &[u8]) {
85-
for c in inp {
86-
self.buffer[0] ^= *c;
87-
}
88-
self.length += inp.len();
89-
}
67+
engine_input_impl!();
9068
}
9169

9270
/// Output of the RIPEMD160 hash function

src/sha1.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,8 @@ impl EngineTrait for HashEngine {
5151

5252

5353
const BLOCK_SIZE: usize = 64;
54-
#[cfg(not(feature = "fuzztarget"))]
55-
fn input(&mut self, mut inp: &[u8]) {
56-
while !inp.is_empty() {
57-
let buf_idx = self.length % <Self as EngineTrait>::BLOCK_SIZE;
58-
let rem_len = <Self as EngineTrait>::BLOCK_SIZE - buf_idx;
59-
let write_len = cmp::min(rem_len, inp.len());
60-
61-
self.buffer[buf_idx..buf_idx + write_len].copy_from_slice(&inp[..write_len]);
62-
self.length += write_len;
63-
if self.length % <Self as EngineTrait>::BLOCK_SIZE == 0 {
64-
self.process_block();
65-
}
66-
inp = &inp[write_len..];
67-
}
68-
}
69-
70-
#[cfg(feature = "fuzztarget")]
71-
fn input(&mut self, inp: &[u8]) {
72-
for c in inp {
73-
self.buffer[0] ^= *c;
74-
}
75-
self.length += inp.len();
76-
}
54+
55+
engine_input_impl!();
7756
}
7857

7958
/// Output of the SHA1 hash function

src/sha256.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,7 @@ impl EngineTrait for HashEngine {
6060

6161
const BLOCK_SIZE: usize = 64;
6262

63-
#[cfg(not(feature = "fuzztarget"))]
64-
fn input(&mut self, mut inp: &[u8]) {
65-
while !inp.is_empty() {
66-
let buf_idx = self.length % <Self as EngineTrait>::BLOCK_SIZE;
67-
let rem_len = <Self as EngineTrait>::BLOCK_SIZE - buf_idx;
68-
let write_len = cmp::min(rem_len, inp.len());
69-
70-
self.buffer[buf_idx..buf_idx + write_len]
71-
.copy_from_slice(&inp[..write_len]);
72-
self.length += write_len;
73-
if self.length % <Self as EngineTrait>::BLOCK_SIZE == 0 {
74-
self.process_block();
75-
}
76-
inp = &inp[write_len..];
77-
}
78-
}
79-
80-
#[cfg(feature = "fuzztarget")]
81-
fn input(&mut self, inp: &[u8]) {
82-
for c in inp {
83-
self.buffer[0] ^= *c;
84-
}
85-
self.length += inp.len();
86-
}
63+
engine_input_impl!();
8764
}
8865

8966
/// Output of the SHA256 hash function

src/sha512.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,7 @@ impl EngineTrait for HashEngine {
6565

6666
const BLOCK_SIZE: usize = 128;
6767

68-
#[cfg(not(feature = "fuzztarget"))]
69-
fn input(&mut self, mut inp: &[u8]) {
70-
while !inp.is_empty() {
71-
let buf_idx = self.length % <Self as EngineTrait>::BLOCK_SIZE;
72-
let rem_len = <Self as EngineTrait>::BLOCK_SIZE - buf_idx;
73-
let write_len = cmp::min(rem_len, inp.len());
74-
75-
self.buffer[buf_idx..buf_idx + write_len].copy_from_slice(&inp[..write_len]);
76-
self.length += write_len;
77-
if self.length % <Self as EngineTrait>::BLOCK_SIZE == 0 {
78-
self.process_block();
79-
}
80-
inp = &inp[write_len..];
81-
}
82-
}
83-
84-
#[cfg(feature = "fuzztarget")]
85-
fn input(&mut self, inp: &[u8]) {
86-
for c in inp {
87-
self.buffer[0] ^= *c;
88-
}
89-
self.length += inp.len();
90-
}
68+
engine_input_impl!();
9169
}
9270

9371
/// Output of the SHA256 hash function

src/util.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,35 @@ macro_rules! borrow_slice_impl(
9999
)
100100
);
101101

102+
macro_rules! engine_input_impl(
103+
() => (
104+
#[cfg(not(feature = "fuzztarget"))]
105+
fn input(&mut self, mut inp: &[u8]) {
106+
while !inp.is_empty() {
107+
let buf_idx = self.length % <Self as EngineTrait>::BLOCK_SIZE;
108+
let rem_len = <Self as EngineTrait>::BLOCK_SIZE - buf_idx;
109+
let write_len = cmp::min(rem_len, inp.len());
110+
111+
self.buffer[buf_idx..buf_idx + write_len]
112+
.copy_from_slice(&inp[..write_len]);
113+
self.length += write_len;
114+
if self.length % <Self as EngineTrait>::BLOCK_SIZE == 0 {
115+
self.process_block();
116+
}
117+
inp = &inp[write_len..];
118+
}
119+
}
120+
121+
#[cfg(feature = "fuzztarget")]
122+
fn input(&mut self, inp: &[u8]) {
123+
for c in inp {
124+
self.buffer[0] ^= *c;
125+
}
126+
self.length += inp.len();
127+
}
128+
)
129+
);
130+
102131
#[cfg(test)]
103132
mod test {
104133
use Hash;

0 commit comments

Comments
 (0)