File tree 1 file changed +17
-15
lines changed
1 file changed +17
-15
lines changed Original file line number Diff line number Diff line change @@ -22,26 +22,28 @@ use serialize::hex::ToHex;
22
22
/// Write a u32 into a vector, which must be 4 bytes long. The value is written in big-endian
23
23
/// format.
24
24
fn write_u32_be ( dst : & mut [ u8 ] , input : u32 ) {
25
- use std:: mem:: to_be32;
26
- assert ! ( dst. len( ) == 4 ) ;
27
- unsafe {
28
- let x = dst. unsafe_mut_ref ( 0 ) as * mut _ as * mut u32 ;
29
- * x = to_be32 ( input) ;
30
- }
25
+ dst[ 0 ] = ( input >> 24 ) as u8 ;
26
+ dst[ 1 ] = ( input >> 16 ) as u8 ;
27
+ dst[ 2 ] = ( input >> 8 ) as u8 ;
28
+ dst[ 3 ] = input as u8 ;
29
+ }
30
+
31
+ /// Read the value of a vector of bytes as a u32 value in big-endian format.
32
+ fn read_u32_be ( input : & [ u8 ] ) -> u32 {
33
+ return
34
+ ( input[ 0 ] as u32 ) << 24 |
35
+ ( input[ 1 ] as u32 ) << 16 |
36
+ ( input[ 2 ] as u32 ) << 8 |
37
+ ( input[ 3 ] as u32 ) ;
31
38
}
32
39
33
40
/// Read a vector of bytes into a vector of u32s. The values are read in big-endian format.
34
41
fn read_u32v_be ( dst : & mut [ u32 ] , input : & [ u8 ] ) {
35
- use std:: mem:: to_be32;
36
42
assert ! ( dst. len( ) * 4 == input. len( ) ) ;
37
- unsafe {
38
- let mut x = dst. unsafe_mut_ref ( 0 ) as * mut _ as * mut u32 ;
39
- let mut y = input. unsafe_ref ( 0 ) as * const _ as * const u32 ;
40
- for _ in range ( 0 , dst. len ( ) ) {
41
- * x = to_be32 ( * y) ;
42
- x = x. offset ( 1 ) ;
43
- y = y. offset ( 1 ) ;
44
- }
43
+ let mut pos = 0 u;
44
+ for chunk in input. chunks ( 4 ) {
45
+ dst[ pos] = read_u32_be ( chunk) ;
46
+ pos += 1 ;
45
47
}
46
48
}
47
49
You can’t perform that action at this time.
0 commit comments