Skip to content

Commit

Permalink
Extract SHA-1’s cast with as into a dedicated function.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Mar 28, 2016
1 parent c5fc35f commit 136bd9f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/digest/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type W32 = Wrapping<u32>;
pub unsafe extern fn block_data_order(state: &mut [u64; MAX_CHAINING_LEN / 8],
data: *const u8,
num: c::size_t) {
let data = data as *const [u8; BLOCK_LEN];
let blocks = core::slice::from_raw_parts(data, num);
block_data_order_safe(state, blocks)
block_data_order_safe(state, ptr_as_array_slice!(data, BLOCK_LEN, num))
}

fn block_data_order_safe(state: &mut [u64; MAX_CHAINING_LEN / 8],
Expand Down
15 changes: 15 additions & 0 deletions src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,18 @@ macro_rules! slice_as_array_ref_mut {
}
}
}

/// Returns a slice of arrays starting at the given pointer.
macro_rules! ptr_as_array_slice {
($ptr:expr, $block_len:expr, $block_num:expr) => {
{
unsafe fn ptr_as_array_slice<'a, T>(ptr: *const T,
block_num: usize)
-> &'a [[T; $block_len]] {
let ptr = ptr as *const [T; $block_len];
core::slice::from_raw_parts(ptr, block_num)
}
ptr_as_array_slice($ptr, $block_num)
}
}
}

0 comments on commit 136bd9f

Please sign in to comment.