From a37f2844e0acc8c87e4550ebd031bfd1d3dc6c57 Mon Sep 17 00:00:00 2001 From: Palmer Cox Date: Thu, 1 Aug 2013 22:07:36 -0400 Subject: [PATCH] Crypto: Add little-endian versions of existing functions: read_u32v_le and write_u32_le. --- src/libextra/crypto/cryptoutil.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libextra/crypto/cryptoutil.rs b/src/libextra/crypto/cryptoutil.rs index 43e3b5c89af..b89f77ec5c1 100644 --- a/src/libextra/crypto/cryptoutil.rs +++ b/src/libextra/crypto/cryptoutil.rs @@ -36,6 +36,18 @@ pub fn write_u32_be(dst: &mut[u8], input: u32) { } } +/// Write a u32 into a vector, which must be 4 bytes long. The value is written in little-endian +/// format. +pub fn write_u32_le(dst: &mut[u8], input: u32) { + use std::cast::transmute; + use std::unstable::intrinsics::to_le32; + assert!(dst.len() == 4); + unsafe { + let x: *mut i32 = transmute(dst.unsafe_mut_ref(0)); + *x = to_le32(input as i32); + } +} + /// Read a vector of bytes into a vector of u64s. The values are read in big-endian format. pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) { use std::cast::transmute; @@ -68,6 +80,22 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) { } } +/// Read a vector of bytes into a vector of u32s. The values are read in little-endian format. +pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) { + use std::cast::transmute; + use std::unstable::intrinsics::to_le32; + assert!(dst.len() * 4 == input.len()); + unsafe { + let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0)); + let mut y: *i32 = transmute(input.unsafe_ref(0)); + do dst.len().times() { + *x = to_le32(*y); + x = x.offset(1); + y = y.offset(1); + } + } +} + /// Returns true if adding the two parameters will result in integer overflow pub fn will_add_overflow(x: T, y: T) -> bool {