Skip to content

Commit a37f284

Browse files
author
Palmer Cox
committed
Crypto: Add little-endian versions of existing functions: read_u32v_le and write_u32_le.
1 parent a1674b6 commit a37f284

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libextra/crypto/cryptoutil.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ pub fn write_u32_be(dst: &mut[u8], input: u32) {
3636
}
3737
}
3838

39+
/// Write a u32 into a vector, which must be 4 bytes long. The value is written in little-endian
40+
/// format.
41+
pub fn write_u32_le(dst: &mut[u8], input: u32) {
42+
use std::cast::transmute;
43+
use std::unstable::intrinsics::to_le32;
44+
assert!(dst.len() == 4);
45+
unsafe {
46+
let x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
47+
*x = to_le32(input as i32);
48+
}
49+
}
50+
3951
/// Read a vector of bytes into a vector of u64s. The values are read in big-endian format.
4052
pub fn read_u64v_be(dst: &mut[u64], input: &[u8]) {
4153
use std::cast::transmute;
@@ -68,6 +80,22 @@ pub fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
6880
}
6981
}
7082

83+
/// Read a vector of bytes into a vector of u32s. The values are read in little-endian format.
84+
pub fn read_u32v_le(dst: &mut[u32], input: &[u8]) {
85+
use std::cast::transmute;
86+
use std::unstable::intrinsics::to_le32;
87+
assert!(dst.len() * 4 == input.len());
88+
unsafe {
89+
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
90+
let mut y: *i32 = transmute(input.unsafe_ref(0));
91+
do dst.len().times() {
92+
*x = to_le32(*y);
93+
x = x.offset(1);
94+
y = y.offset(1);
95+
}
96+
}
97+
}
98+
7199

72100
/// Returns true if adding the two parameters will result in integer overflow
73101
pub fn will_add_overflow<T: Int + Unsigned>(x: T, y: T) -> bool {

0 commit comments

Comments
 (0)