Vendor asic-rs 0.8.1 (280 crates, offline builds) + wxWidgets 3.3.3 (pristine + patch queue); UI spec + ADR-0003; M1 on asic-rs
This commit is contained in:
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
use core::fmt;
|
||||
|
||||
/// The error returned when slice can not be converted into array.
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct IntoArrayError;
|
||||
|
||||
impl fmt::Display for IntoArrayError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.write_str("Failed to convert into array.")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
impl std::error::Error for IntoArrayError {}
|
||||
|
||||
/// The error returned when input and output slices have different length
|
||||
/// and thus can not be converted to `InOutBuf`.
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct NotEqualError;
|
||||
|
||||
impl fmt::Display for NotEqualError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.write_str("Length of input slices is not equal to each other")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
impl std::error::Error for NotEqualError {}
|
||||
|
||||
/// Padding error. Usually emitted when size of output buffer is insufficient.
|
||||
#[cfg(feature = "block-padding")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct PadError;
|
||||
|
||||
#[cfg(feature = "block-padding")]
|
||||
impl fmt::Display for PadError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.write_str("Padding error")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "block-padding")]
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
impl std::error::Error for PadError {}
|
||||
|
||||
/// Output buffer is smaller than input buffer.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct OutIsTooSmallError;
|
||||
|
||||
impl fmt::Display for OutIsTooSmallError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||
f.write_str("Output buffer is smaller than input")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||
impl std::error::Error for OutIsTooSmallError {}
|
||||
Vendored
+174
@@ -0,0 +1,174 @@
|
||||
use crate::InOutBuf;
|
||||
use core::{marker::PhantomData, ptr};
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
|
||||
/// Custom pointer type which contains one immutable (input) and one mutable
|
||||
/// (output) pointer, which are either equal or non-overlapping.
|
||||
pub struct InOut<'inp, 'out, T> {
|
||||
pub(crate) in_ptr: *const T,
|
||||
pub(crate) out_ptr: *mut T,
|
||||
pub(crate) _pd: PhantomData<(&'inp T, &'out mut T)>,
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T> InOut<'inp, 'out, T> {
|
||||
/// Reborrow `self`.
|
||||
#[inline(always)]
|
||||
pub fn reborrow<'a>(&'a mut self) -> InOut<'a, 'a, T> {
|
||||
Self {
|
||||
in_ptr: self.in_ptr,
|
||||
out_ptr: self.out_ptr,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get immutable reference to the input value.
|
||||
#[inline(always)]
|
||||
pub fn get_in<'a>(&'a self) -> &'a T {
|
||||
unsafe { &*self.in_ptr }
|
||||
}
|
||||
|
||||
/// Get mutable reference to the output value.
|
||||
#[inline(always)]
|
||||
pub fn get_out<'a>(&'a mut self) -> &'a mut T {
|
||||
unsafe { &mut *self.out_ptr }
|
||||
}
|
||||
|
||||
/// Convert `self` to a pair of raw input and output pointers.
|
||||
#[inline(always)]
|
||||
pub fn into_raw(self) -> (*const T, *mut T) {
|
||||
(self.in_ptr, self.out_ptr)
|
||||
}
|
||||
|
||||
/// Create `InOut` from raw input and output pointers.
|
||||
///
|
||||
/// # Safety
|
||||
/// Behavior is undefined if any of the following conditions are violated:
|
||||
/// - `in_ptr` must point to a properly initialized value of type `T` and
|
||||
/// must be valid for reads.
|
||||
/// - `out_ptr` must point to a properly initialized value of type `T` and
|
||||
/// must be valid for both reads and writes.
|
||||
/// - `in_ptr` and `out_ptr` must be either equal or non-overlapping.
|
||||
/// - If `in_ptr` and `out_ptr` are equal, then the memory referenced by
|
||||
/// them must not be accessed through any other pointer (not derived from
|
||||
/// the return value) for the duration of lifetime 'a. Both read and write
|
||||
/// accesses are forbidden.
|
||||
/// - If `in_ptr` and `out_ptr` are not equal, then the memory referenced by
|
||||
/// `out_ptr` must not be accessed through any other pointer (not derived from
|
||||
/// the return value) for the duration of lifetime `'a`. Both read and write
|
||||
/// accesses are forbidden. The memory referenced by `in_ptr` must not be
|
||||
/// mutated for the duration of lifetime `'a`, except inside an `UnsafeCell`.
|
||||
#[inline(always)]
|
||||
pub unsafe fn from_raw(in_ptr: *const T, out_ptr: *mut T) -> InOut<'inp, 'out, T> {
|
||||
Self {
|
||||
in_ptr,
|
||||
out_ptr,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T: Clone> InOut<'inp, 'out, T> {
|
||||
/// Clone input value and return it.
|
||||
#[inline(always)]
|
||||
pub fn clone_in(&self) -> T {
|
||||
unsafe { (&*self.in_ptr).clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> From<&'a mut T> for InOut<'a, 'a, T> {
|
||||
#[inline(always)]
|
||||
fn from(val: &'a mut T) -> Self {
|
||||
let p = val as *mut T;
|
||||
Self {
|
||||
in_ptr: p,
|
||||
out_ptr: p,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T> From<(&'inp T, &'out mut T)> for InOut<'inp, 'out, T> {
|
||||
#[inline(always)]
|
||||
fn from((in_val, out_val): (&'inp T, &'out mut T)) -> Self {
|
||||
Self {
|
||||
in_ptr: in_val as *const T,
|
||||
out_ptr: out_val as *mut T,
|
||||
_pd: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T, N: ArrayLength<T>> InOut<'inp, 'out, GenericArray<T, N>> {
|
||||
/// Returns `InOut` for the given position.
|
||||
///
|
||||
/// # Panics
|
||||
/// If `pos` greater or equal to array length.
|
||||
#[inline(always)]
|
||||
pub fn get<'a>(&'a mut self, pos: usize) -> InOut<'a, 'a, T> {
|
||||
assert!(pos < N::USIZE);
|
||||
unsafe {
|
||||
InOut {
|
||||
in_ptr: (self.in_ptr as *const T).add(pos),
|
||||
out_ptr: (self.out_ptr as *mut T).add(pos),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `InOut` array to `InOutBuf`.
|
||||
#[inline(always)]
|
||||
pub fn into_buf(self) -> InOutBuf<'inp, 'out, T> {
|
||||
InOutBuf {
|
||||
in_ptr: self.in_ptr as *const T,
|
||||
out_ptr: self.out_ptr as *mut T,
|
||||
len: N::USIZE,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, N: ArrayLength<u8>> InOut<'inp, 'out, GenericArray<u8, N>> {
|
||||
/// XOR `data` with values behind the input slice and write
|
||||
/// result to the output slice.
|
||||
///
|
||||
/// # Panics
|
||||
/// If `data` length is not equal to the buffer length.
|
||||
#[inline(always)]
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
pub fn xor_in2out(&mut self, data: &GenericArray<u8, N>) {
|
||||
unsafe {
|
||||
let input = ptr::read(self.in_ptr);
|
||||
let mut temp = GenericArray::<u8, N>::default();
|
||||
for i in 0..N::USIZE {
|
||||
temp[i] = input[i] ^ data[i];
|
||||
}
|
||||
ptr::write(self.out_ptr, temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, N, M> InOut<'inp, 'out, GenericArray<GenericArray<u8, N>, M>>
|
||||
where
|
||||
N: ArrayLength<u8>,
|
||||
M: ArrayLength<GenericArray<u8, N>>,
|
||||
{
|
||||
/// XOR `data` with values behind the input slice and write
|
||||
/// result to the output slice.
|
||||
///
|
||||
/// # Panics
|
||||
/// If `data` length is not equal to the buffer length.
|
||||
#[inline(always)]
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
pub fn xor_in2out(&mut self, data: &GenericArray<GenericArray<u8, N>, M>) {
|
||||
unsafe {
|
||||
let input = ptr::read(self.in_ptr);
|
||||
let mut temp = GenericArray::<GenericArray<u8, N>, M>::default();
|
||||
for i in 0..M::USIZE {
|
||||
for j in 0..N::USIZE {
|
||||
temp[i][j] = input[i][j] ^ data[i][j];
|
||||
}
|
||||
}
|
||||
ptr::write(self.out_ptr, temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+303
@@ -0,0 +1,303 @@
|
||||
use crate::{
|
||||
errors::{IntoArrayError, NotEqualError},
|
||||
InOut,
|
||||
};
|
||||
use core::{marker::PhantomData, slice};
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
|
||||
/// Custom slice type which references one immutable (input) slice and one
|
||||
/// mutable (output) slice of equal length. Input and output slices are
|
||||
/// either the same or do not overlap.
|
||||
pub struct InOutBuf<'inp, 'out, T> {
|
||||
pub(crate) in_ptr: *const T,
|
||||
pub(crate) out_ptr: *mut T,
|
||||
pub(crate) len: usize,
|
||||
pub(crate) _pd: PhantomData<(&'inp T, &'out mut T)>,
|
||||
}
|
||||
|
||||
impl<'a, T> From<&'a mut [T]> for InOutBuf<'a, 'a, T> {
|
||||
#[inline(always)]
|
||||
fn from(buf: &'a mut [T]) -> Self {
|
||||
let p = buf.as_mut_ptr();
|
||||
Self {
|
||||
in_ptr: p,
|
||||
out_ptr: p,
|
||||
len: buf.len(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> InOutBuf<'a, 'a, T> {
|
||||
/// Create `InOutBuf` from a single mutable reference.
|
||||
#[inline(always)]
|
||||
pub fn from_mut(val: &'a mut T) -> InOutBuf<'a, 'a, T> {
|
||||
let p = val as *mut T;
|
||||
Self {
|
||||
in_ptr: p,
|
||||
out_ptr: p,
|
||||
len: 1,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T> IntoIterator for InOutBuf<'inp, 'out, T> {
|
||||
type Item = InOut<'inp, 'out, T>;
|
||||
type IntoIter = InOutBufIter<'inp, 'out, T>;
|
||||
|
||||
#[inline(always)]
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
InOutBufIter { buf: self, pos: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T> InOutBuf<'inp, 'out, T> {
|
||||
/// Create `InOutBuf` from a pair of immutable and mutable references.
|
||||
#[inline(always)]
|
||||
pub fn from_ref_mut(in_val: &'inp T, out_val: &'out mut T) -> Self {
|
||||
Self {
|
||||
in_ptr: in_val as *const T,
|
||||
out_ptr: out_val as *mut T,
|
||||
len: 1,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create `InOutBuf` from immutable and mutable slices.
|
||||
///
|
||||
/// Returns an error if length of slices is not equal to each other.
|
||||
#[inline(always)]
|
||||
pub fn new(in_buf: &'inp [T], out_buf: &'out mut [T]) -> Result<Self, NotEqualError> {
|
||||
if in_buf.len() != out_buf.len() {
|
||||
Err(NotEqualError)
|
||||
} else {
|
||||
Ok(Self {
|
||||
in_ptr: in_buf.as_ptr(),
|
||||
out_ptr: out_buf.as_mut_ptr(),
|
||||
len: in_buf.len(),
|
||||
_pd: Default::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get length of the inner buffers.
|
||||
#[inline(always)]
|
||||
pub fn len(&self) -> usize {
|
||||
self.len
|
||||
}
|
||||
|
||||
/// Returns `true` if the buffer has a length of 0.
|
||||
#[inline(always)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.len == 0
|
||||
}
|
||||
|
||||
/// Returns `InOut` for given position.
|
||||
///
|
||||
/// # Panics
|
||||
/// If `pos` greater or equal to buffer length.
|
||||
#[inline(always)]
|
||||
pub fn get<'a>(&'a mut self, pos: usize) -> InOut<'a, 'a, T> {
|
||||
assert!(pos < self.len);
|
||||
unsafe {
|
||||
InOut {
|
||||
in_ptr: self.in_ptr.add(pos),
|
||||
out_ptr: self.out_ptr.add(pos),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get input slice.
|
||||
#[inline(always)]
|
||||
pub fn get_in<'a>(&'a self) -> &'a [T] {
|
||||
unsafe { slice::from_raw_parts(self.in_ptr, self.len) }
|
||||
}
|
||||
|
||||
/// Get output slice.
|
||||
#[inline(always)]
|
||||
pub fn get_out<'a>(&'a mut self) -> &'a mut [T] {
|
||||
unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) }
|
||||
}
|
||||
|
||||
/// Consume self and return output slice with lifetime `'a`.
|
||||
#[inline(always)]
|
||||
pub fn into_out(self) -> &'out mut [T] {
|
||||
unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) }
|
||||
}
|
||||
|
||||
/// Get raw input and output pointers.
|
||||
#[inline(always)]
|
||||
pub fn into_raw(self) -> (*const T, *mut T) {
|
||||
(self.in_ptr, self.out_ptr)
|
||||
}
|
||||
|
||||
/// Reborrow `self`.
|
||||
#[inline(always)]
|
||||
pub fn reborrow<'a>(&'a mut self) -> InOutBuf<'a, 'a, T> {
|
||||
Self {
|
||||
in_ptr: self.in_ptr,
|
||||
out_ptr: self.out_ptr,
|
||||
len: self.len,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create [`InOutBuf`] from raw input and output pointers.
|
||||
///
|
||||
/// # Safety
|
||||
/// Behavior is undefined if any of the following conditions are violated:
|
||||
/// - `in_ptr` must point to a properly initialized value of type `T` and
|
||||
/// must be valid for reads for `len * mem::size_of::<T>()` many bytes.
|
||||
/// - `out_ptr` must point to a properly initialized value of type `T` and
|
||||
/// must be valid for both reads and writes for `len * mem::size_of::<T>()`
|
||||
/// many bytes.
|
||||
/// - `in_ptr` and `out_ptr` must be either equal or non-overlapping.
|
||||
/// - If `in_ptr` and `out_ptr` are equal, then the memory referenced by
|
||||
/// them must not be accessed through any other pointer (not derived from
|
||||
/// the return value) for the duration of lifetime 'a. Both read and write
|
||||
/// accesses are forbidden.
|
||||
/// - If `in_ptr` and `out_ptr` are not equal, then the memory referenced by
|
||||
/// `out_ptr` must not be accessed through any other pointer (not derived from
|
||||
/// the return value) for the duration of lifetime 'a. Both read and write
|
||||
/// accesses are forbidden. The memory referenced by `in_ptr` must not be
|
||||
/// mutated for the duration of lifetime `'a`, except inside an `UnsafeCell`.
|
||||
/// - The total size `len * mem::size_of::<T>()` must be no larger than `isize::MAX`.
|
||||
#[inline(always)]
|
||||
pub unsafe fn from_raw(
|
||||
in_ptr: *const T,
|
||||
out_ptr: *mut T,
|
||||
len: usize,
|
||||
) -> InOutBuf<'inp, 'out, T> {
|
||||
Self {
|
||||
in_ptr,
|
||||
out_ptr,
|
||||
len,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Divides one buffer into two at `mid` index.
|
||||
///
|
||||
/// The first will contain all indices from `[0, mid)` (excluding
|
||||
/// the index `mid` itself) and the second will contain all
|
||||
/// indices from `[mid, len)` (excluding the index `len` itself).
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `mid > len`.
|
||||
#[inline(always)]
|
||||
pub fn split_at(self, mid: usize) -> (InOutBuf<'inp, 'out, T>, InOutBuf<'inp, 'out, T>) {
|
||||
assert!(mid <= self.len);
|
||||
let (tail_in_ptr, tail_out_ptr) = unsafe { (self.in_ptr.add(mid), self.out_ptr.add(mid)) };
|
||||
(
|
||||
InOutBuf {
|
||||
in_ptr: self.in_ptr,
|
||||
out_ptr: self.out_ptr,
|
||||
len: mid,
|
||||
_pd: PhantomData,
|
||||
},
|
||||
InOutBuf {
|
||||
in_ptr: tail_in_ptr,
|
||||
out_ptr: tail_out_ptr,
|
||||
len: self.len() - mid,
|
||||
_pd: PhantomData,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Partition buffer into 2 parts: buffer of arrays and tail.
|
||||
#[inline(always)]
|
||||
pub fn into_chunks<N: ArrayLength<T>>(
|
||||
self,
|
||||
) -> (
|
||||
InOutBuf<'inp, 'out, GenericArray<T, N>>,
|
||||
InOutBuf<'inp, 'out, T>,
|
||||
) {
|
||||
let chunks = self.len() / N::USIZE;
|
||||
let tail_pos = N::USIZE * chunks;
|
||||
let tail_len = self.len() - tail_pos;
|
||||
unsafe {
|
||||
let chunks = InOutBuf {
|
||||
in_ptr: self.in_ptr as *const GenericArray<T, N>,
|
||||
out_ptr: self.out_ptr as *mut GenericArray<T, N>,
|
||||
len: chunks,
|
||||
_pd: PhantomData,
|
||||
};
|
||||
let tail = InOutBuf {
|
||||
in_ptr: self.in_ptr.add(tail_pos),
|
||||
out_ptr: self.out_ptr.add(tail_pos),
|
||||
len: tail_len,
|
||||
_pd: PhantomData,
|
||||
};
|
||||
(chunks, tail)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out> InOutBuf<'inp, 'out, u8> {
|
||||
/// XORs `data` with values behind the input slice and write
|
||||
/// result to the output slice.
|
||||
///
|
||||
/// # Panics
|
||||
/// If `data` length is not equal to the buffer length.
|
||||
#[inline(always)]
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
pub fn xor_in2out(&mut self, data: &[u8]) {
|
||||
assert_eq!(self.len(), data.len());
|
||||
unsafe {
|
||||
for i in 0..data.len() {
|
||||
let in_ptr = self.in_ptr.add(i);
|
||||
let out_ptr = self.out_ptr.add(i);
|
||||
*out_ptr = *in_ptr ^ data[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T, N> TryInto<InOut<'inp, 'out, GenericArray<T, N>>> for InOutBuf<'inp, 'out, T>
|
||||
where
|
||||
N: ArrayLength<T>,
|
||||
{
|
||||
type Error = IntoArrayError;
|
||||
|
||||
#[inline(always)]
|
||||
fn try_into(self) -> Result<InOut<'inp, 'out, GenericArray<T, N>>, Self::Error> {
|
||||
if self.len() == N::USIZE {
|
||||
Ok(InOut {
|
||||
in_ptr: self.in_ptr as *const _,
|
||||
out_ptr: self.out_ptr as *mut _,
|
||||
_pd: PhantomData,
|
||||
})
|
||||
} else {
|
||||
Err(IntoArrayError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator over [`InOutBuf`].
|
||||
pub struct InOutBufIter<'inp, 'out, T> {
|
||||
buf: InOutBuf<'inp, 'out, T>,
|
||||
pos: usize,
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T> Iterator for InOutBufIter<'inp, 'out, T> {
|
||||
type Item = InOut<'inp, 'out, T>;
|
||||
|
||||
#[inline(always)]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.buf.len() == self.pos {
|
||||
return None;
|
||||
}
|
||||
let res = unsafe {
|
||||
InOut {
|
||||
in_ptr: self.buf.in_ptr.add(self.pos),
|
||||
out_ptr: self.buf.out_ptr.add(self.pos),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
};
|
||||
self.pos += 1;
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
//! Collection of custom reference types for code generic over in-place and
|
||||
//! buffer-to-buffer modes of operation.
|
||||
|
||||
#![no_std]
|
||||
#![doc(
|
||||
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
|
||||
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
|
||||
html_root_url = "https://docs.rs/inout/0.1.4"
|
||||
)]
|
||||
#![allow(clippy::needless_lifetimes)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![warn(missing_docs, rust_2018_idioms)]
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
extern crate std;
|
||||
|
||||
#[cfg(feature = "block-padding")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
|
||||
pub use block_padding;
|
||||
|
||||
mod errors;
|
||||
mod inout;
|
||||
mod inout_buf;
|
||||
mod reserved;
|
||||
|
||||
pub use crate::{errors::*, inout::*, inout_buf::*, reserved::*};
|
||||
Vendored
+226
@@ -0,0 +1,226 @@
|
||||
use crate::errors::OutIsTooSmallError;
|
||||
use core::{marker::PhantomData, slice};
|
||||
|
||||
#[cfg(feature = "block-padding")]
|
||||
use crate::errors::PadError;
|
||||
#[cfg(feature = "block-padding")]
|
||||
use crate::{InOut, InOutBuf};
|
||||
#[cfg(feature = "block-padding")]
|
||||
use block_padding::{PadType, Padding};
|
||||
#[cfg(feature = "block-padding")]
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
|
||||
/// Custom slice type which references one immutable (input) slice and one
|
||||
/// mutable (output) slice. Input and output slices are either the same or
|
||||
/// do not overlap. Length of the output slice is always equal or bigger than
|
||||
/// length of the input slice.
|
||||
pub struct InOutBufReserved<'inp, 'out, T> {
|
||||
in_ptr: *const T,
|
||||
out_ptr: *mut T,
|
||||
in_len: usize,
|
||||
out_len: usize,
|
||||
_pd: PhantomData<(&'inp T, &'out mut T)>,
|
||||
}
|
||||
|
||||
impl<'a, T> InOutBufReserved<'a, 'a, T> {
|
||||
/// Crate [`InOutBufReserved`] from a single mutable slice.
|
||||
pub fn from_mut_slice(buf: &'a mut [T], msg_len: usize) -> Result<Self, OutIsTooSmallError> {
|
||||
if msg_len > buf.len() {
|
||||
return Err(OutIsTooSmallError);
|
||||
}
|
||||
let p = buf.as_mut_ptr();
|
||||
let out_len = buf.len();
|
||||
Ok(Self {
|
||||
in_ptr: p,
|
||||
out_ptr: p,
|
||||
in_len: msg_len,
|
||||
out_len,
|
||||
_pd: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create [`InOutBufReserved`] from raw input and output pointers.
|
||||
///
|
||||
/// # Safety
|
||||
/// Behavior is undefined if any of the following conditions are violated:
|
||||
/// - `in_ptr` must point to a properly initialized value of type `T` and
|
||||
/// must be valid for reads for `in_len * mem::size_of::<T>()` many bytes.
|
||||
/// - `out_ptr` must point to a properly initialized value of type `T` and
|
||||
/// must be valid for both reads and writes for `out_len * mem::size_of::<T>()`
|
||||
/// many bytes.
|
||||
/// - `in_ptr` and `out_ptr` must be either equal or non-overlapping.
|
||||
/// - If `in_ptr` and `out_ptr` are equal, then the memory referenced by
|
||||
/// them must not be accessed through any other pointer (not derived from
|
||||
/// the return value) for the duration of lifetime 'a. Both read and write
|
||||
/// accesses are forbidden.
|
||||
/// - If `in_ptr` and `out_ptr` are not equal, then the memory referenced by
|
||||
/// `out_ptr` must not be accessed through any other pointer (not derived from
|
||||
/// the return value) for the duration of lifetime 'a. Both read and write
|
||||
/// accesses are forbidden. The memory referenced by `in_ptr` must not be
|
||||
/// mutated for the duration of lifetime `'a`, except inside an `UnsafeCell`.
|
||||
/// - The total size `in_len * mem::size_of::<T>()` and
|
||||
/// `out_len * mem::size_of::<T>()` must be no larger than `isize::MAX`.
|
||||
#[inline(always)]
|
||||
pub unsafe fn from_raw(
|
||||
in_ptr: *const T,
|
||||
in_len: usize,
|
||||
out_ptr: *mut T,
|
||||
out_len: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
in_ptr,
|
||||
out_ptr,
|
||||
in_len,
|
||||
out_len,
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get raw input and output pointers.
|
||||
#[inline(always)]
|
||||
pub fn into_raw(self) -> (*const T, *mut T) {
|
||||
(self.in_ptr, self.out_ptr)
|
||||
}
|
||||
|
||||
/// Get input buffer length.
|
||||
#[inline(always)]
|
||||
pub fn get_in_len(&self) -> usize {
|
||||
self.in_len
|
||||
}
|
||||
|
||||
/// Get output buffer length.
|
||||
#[inline(always)]
|
||||
pub fn get_out_len(&self) -> usize {
|
||||
self.out_len
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out, T> InOutBufReserved<'inp, 'out, T> {
|
||||
/// Crate [`InOutBufReserved`] from two separate slices.
|
||||
pub fn from_slices(
|
||||
in_buf: &'inp [T],
|
||||
out_buf: &'out mut [T],
|
||||
) -> Result<Self, OutIsTooSmallError> {
|
||||
if in_buf.len() > out_buf.len() {
|
||||
return Err(OutIsTooSmallError);
|
||||
}
|
||||
Ok(Self {
|
||||
in_ptr: in_buf.as_ptr(),
|
||||
out_ptr: out_buf.as_mut_ptr(),
|
||||
in_len: in_buf.len(),
|
||||
out_len: out_buf.len(),
|
||||
_pd: PhantomData,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get input slice.
|
||||
#[inline(always)]
|
||||
pub fn get_in<'a>(&'a self) -> &'a [T] {
|
||||
unsafe { slice::from_raw_parts(self.in_ptr, self.in_len) }
|
||||
}
|
||||
|
||||
/// Get output slice.
|
||||
#[inline(always)]
|
||||
pub fn get_out<'a>(&'a mut self) -> &'a mut [T] {
|
||||
unsafe { slice::from_raw_parts_mut(self.out_ptr, self.out_len) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {
|
||||
/// Transform buffer into [`PaddedInOutBuf`] using padding algorithm `P`.
|
||||
#[cfg(feature = "block-padding")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
|
||||
#[inline(always)]
|
||||
pub fn into_padded_blocks<P, BS>(self) -> Result<PaddedInOutBuf<'inp, 'out, BS>, PadError>
|
||||
where
|
||||
P: Padding<BS>,
|
||||
BS: ArrayLength<u8>,
|
||||
{
|
||||
let bs = BS::USIZE;
|
||||
let blocks_len = self.in_len / bs;
|
||||
let tail_len = self.in_len - bs * blocks_len;
|
||||
let blocks = unsafe {
|
||||
InOutBuf::from_raw(
|
||||
self.in_ptr as *const GenericArray<u8, BS>,
|
||||
self.out_ptr as *mut GenericArray<u8, BS>,
|
||||
blocks_len,
|
||||
)
|
||||
};
|
||||
let mut tail_in = GenericArray::<u8, BS>::default();
|
||||
let tail_out = match P::TYPE {
|
||||
PadType::NoPadding | PadType::Ambiguous if tail_len == 0 => None,
|
||||
PadType::NoPadding => return Err(PadError),
|
||||
PadType::Reversible | PadType::Ambiguous => {
|
||||
let blen = bs * blocks_len;
|
||||
let res_len = blen + bs;
|
||||
if res_len > self.out_len {
|
||||
return Err(PadError);
|
||||
}
|
||||
// SAFETY: `in_ptr + blen..in_ptr + blen + tail_len`
|
||||
// is valid region for reads and `tail_len` is smaller than `BS`.
|
||||
// we have verified that `blen + bs <= out_len`, in other words,
|
||||
// `out_ptr + blen..out_ptr + blen + bs` is valid region
|
||||
// for writes.
|
||||
let out_block = unsafe {
|
||||
core::ptr::copy_nonoverlapping(
|
||||
self.in_ptr.add(blen),
|
||||
tail_in.as_mut_ptr(),
|
||||
tail_len,
|
||||
);
|
||||
&mut *(self.out_ptr.add(blen) as *mut GenericArray<u8, BS>)
|
||||
};
|
||||
P::pad(&mut tail_in, tail_len);
|
||||
Some(out_block)
|
||||
}
|
||||
};
|
||||
Ok(PaddedInOutBuf {
|
||||
blocks,
|
||||
tail_in,
|
||||
tail_out,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Variant of [`InOutBuf`] with optional padded tail block.
|
||||
#[cfg(feature = "block-padding")]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "block-padding")))]
|
||||
pub struct PaddedInOutBuf<'inp, 'out, BS: ArrayLength<u8>> {
|
||||
blocks: InOutBuf<'inp, 'out, GenericArray<u8, BS>>,
|
||||
tail_in: GenericArray<u8, BS>,
|
||||
tail_out: Option<&'out mut GenericArray<u8, BS>>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "block-padding")]
|
||||
impl<'inp, 'out, BS: ArrayLength<u8>> PaddedInOutBuf<'inp, 'out, BS> {
|
||||
/// Get full blocks.
|
||||
#[inline(always)]
|
||||
pub fn get_blocks<'a>(&'a mut self) -> InOutBuf<'a, 'a, GenericArray<u8, BS>> {
|
||||
self.blocks.reborrow()
|
||||
}
|
||||
|
||||
/// Get padded tail block.
|
||||
///
|
||||
/// For paddings with `P::TYPE = PadType::Reversible` it always returns `Some`.
|
||||
#[inline(always)]
|
||||
pub fn get_tail_block<'a>(&'a mut self) -> Option<InOut<'a, 'a, GenericArray<u8, BS>>> {
|
||||
match self.tail_out.as_deref_mut() {
|
||||
Some(out_block) => Some((&self.tail_in, out_block).into()),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert buffer into output slice.
|
||||
#[inline(always)]
|
||||
pub fn into_out(self) -> &'out [u8] {
|
||||
let total_blocks = if self.tail_out.is_some() {
|
||||
self.blocks.len() + 1
|
||||
} else {
|
||||
self.blocks.len()
|
||||
};
|
||||
let res_len = BS::USIZE * total_blocks;
|
||||
let (_, out_ptr) = self.blocks.into_raw();
|
||||
// SAFETY: `res_len` is always valid for the output buffer since
|
||||
// it's checked during type construction
|
||||
unsafe { slice::from_raw_parts(out_ptr as *const u8, res_len) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user