Browse Source

std::slice::bytes is deprecated

pull/3/head
Cesar Eduardo Barros 9 years ago
parent
commit
f331cd6c35
  1. 12
      src/blake2.rs
  2. 58
      src/bytes.rs

12
src/blake2.rs

@ -26,7 +26,7 @@ macro_rules! blake2_impl {
use std::io;
use $crate::as_bytes::AsBytes;
use $crate::bytes::{MutableByteVector, copy_memory};
use $crate::bytes::BytesExt;
use $crate::constant_time_eq::constant_time_eq;
use $crate::simd::{Vector4, $vec};
@ -118,7 +118,7 @@ macro_rules! blake2_impl {
};
if kk > 0 {
copy_memory(k, state.m.as_mut_bytes());
state.m.as_mut_bytes().copy_bytes_from(k);
state.t = $bytes * 2;
}
state
@ -152,7 +152,7 @@ macro_rules! blake2_impl {
let part = &rest[..len];
rest = &rest[part.len()..];
copy_memory(part, &mut self.m.as_mut_bytes()[off..]);
self.m.as_mut_bytes()[off..].copy_bytes_from(part);
self.t = self.t.checked_add(part.len() as u64).expect("hash data length overflow");
}
@ -162,14 +162,14 @@ macro_rules! blake2_impl {
let part = &rest[..($bytes * 2)];
rest = &rest[part.len()..];
copy_memory(part, self.m.as_mut_bytes());
self.m.as_mut_bytes().copy_bytes_from(part);
self.t = self.t.checked_add(part.len() as u64).expect("hash data length overflow");
}
if rest.len() > 0 {
self.compress(0, 0);
copy_memory(rest, self.m.as_mut_bytes());
self.m.as_mut_bytes().copy_bytes_from(rest);
self.t = self.t.checked_add(rest.len() as u64).expect("hash data length overflow");
}
}
@ -188,7 +188,7 @@ macro_rules! blake2_impl {
fn finalize_with_flag(mut self, f1: $word) -> $result {
let off = (self.t % ($bytes * 2)) as usize;
if off != 0 {
self.m.as_mut_bytes()[off..].set_memory(0);
self.m.as_mut_bytes()[off..].set_bytes(0);
}
self.compress(!0, f1);

58
src/bytes.rs

@ -1,42 +1,40 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of the Rust distribution and at
// http://rust-lang.org/COPYRIGHT.
// Copyright 2015 blake2-rfc Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//! A local copy of the unstable std::slice::bytes module.
//! Operations on byte slices.
use std::ptr;
/// A trait for operations on mutable `[u8]`s.
pub trait MutableByteVector {
/// Sets all bytes of the receiver to the given value.
fn set_memory(&mut self, value: u8);
/// Operations on byte slices.
pub trait BytesExt {
/// Set all bytes of this slice to the same value.
///
/// Equivalent to C's memset().
fn set_bytes(&mut self, value: u8);
/// Copy all bytes from a source slice to the start of this slice.
///
/// Equivalent to C's memcpy().
fn copy_bytes_from(&mut self, src: &[u8]);
}
impl MutableByteVector for [u8] {
impl BytesExt for [u8] {
#[inline]
fn set_memory(&mut self, value: u8) {
unsafe { ptr::write_bytes(self.as_mut_ptr(), value, self.len()) };
fn set_bytes(&mut self, value: u8) {
unsafe {
ptr::write_bytes(self.as_mut_ptr(), value, self.len());
}
}
}
/// Copies data from `src` to `dst`
///
/// Panics if the length of `dst` is less than the length of `src`.
#[inline]
pub fn copy_memory(src: &[u8], dst: &mut [u8]) {
let len_src = src.len();
assert!(dst.len() >= len_src);
// `dst` is unaliasable, so we know statically it doesn't overlap
// with `src`.
unsafe {
ptr::copy_nonoverlapping(src.as_ptr(),
dst.as_mut_ptr(),
len_src);
#[inline]
fn copy_bytes_from(&mut self, src: &[u8]) {
assert!(src.len() <= self.len());
unsafe {
ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr(), src.len());
}
}
}

Loading…
Cancel
Save