1 | // Copyright 2018 Brian Smith. |
2 | // |
3 | // Permission to use, copy, modify, and/or distribute this software for any |
4 | // purpose with or without fee is hereby granted, provided that the above |
5 | // copyright notice and this permission notice appear in all copies. |
6 | // |
7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
10 | // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
14 | |
15 | use super::block::{Block, BLOCK_LEN}; |
16 | |
17 | #[cfg (target_arch = "x86" )] |
18 | pub fn shift_full_blocks<F>(in_out: &mut [u8], src: core::ops::RangeFrom<usize>, mut transform: F) |
19 | where |
20 | F: FnMut(&[u8; BLOCK_LEN]) -> Block, |
21 | { |
22 | let in_out_len = in_out[src.clone()].len(); |
23 | |
24 | for i in (0..in_out_len).step_by(BLOCK_LEN) { |
25 | let block = { |
26 | let input = |
27 | <&[u8; BLOCK_LEN]>::try_from(&in_out[(src.start + i)..][..BLOCK_LEN]).unwrap(); |
28 | transform(input) |
29 | }; |
30 | let output = <&mut [u8; BLOCK_LEN]>::try_from(&mut in_out[i..][..BLOCK_LEN]).unwrap(); |
31 | *output = *block.as_ref(); |
32 | } |
33 | } |
34 | |
35 | pub fn shift_partial<F>((in_prefix_len: usize, in_out: &mut [u8]): (usize, &mut [u8]), transform: F) |
36 | where |
37 | F: FnOnce(&[u8]) -> Block, |
38 | { |
39 | let (block: Block, in_out_len: usize) = { |
40 | let input: &[u8] = &in_out[in_prefix_len..]; |
41 | let in_out_len: usize = input.len(); |
42 | if in_out_len == 0 { |
43 | return; |
44 | } |
45 | debug_assert!(in_out_len < BLOCK_LEN); |
46 | (transform(input), in_out_len) |
47 | }; |
48 | in_out[..in_out_len].copy_from_slice(&block.as_ref()[..in_out_len]); |
49 | } |
50 | |