about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPalmer Cox <p@lmercox.com>2013-08-01 22:07:36 -0400
committerPalmer Cox <p@lmercox.com>2013-08-17 00:22:04 -0400
commita37f2844e0acc8c87e4550ebd031bfd1d3dc6c57 (patch)
tree51ed51a149c78eeb0087b79abb84c281495d14a5
parenta1674b6150b20616c954e37206012b356ff81b1c (diff)
downloadrust-a37f2844e0acc8c87e4550ebd031bfd1d3dc6c57.tar.gz
rust-a37f2844e0acc8c87e4550ebd031bfd1d3dc6c57.zip
Crypto: Add little-endian versions of existing functions: read_u32v_le and write_u32_le.
-rw-r--r--src/libextra/crypto/cryptoutil.rs28
1 files changed, 28 insertions, 0 deletions
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<T: Int + Unsigned>(x: T, y: T) -> bool {