From 136bd9f48390f34b5ef3369a12b49e6e16c78fd9 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 28 Mar 2016 11:23:08 +0200 Subject: [PATCH] =?UTF-8?q?Extract=20SHA-1=E2=80=99s=20cast=20with=20`as`?= =?UTF-8?q?=20into=20a=20dedicated=20function.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/briansmith/ring/issues/31#issuecomment-202311233 --- src/digest/sha1.rs | 4 +--- src/polyfill.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/digest/sha1.rs b/src/digest/sha1.rs index 256ac5e0ca..8a0425fc15 100644 --- a/src/digest/sha1.rs +++ b/src/digest/sha1.rs @@ -37,9 +37,7 @@ type W32 = Wrapping; 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], diff --git a/src/polyfill.rs b/src/polyfill.rs index cbed509b49..b6b5747060 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -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) + } + } +}