2 pub fn slice_to_be16(v: &[u8]) -> u16 {
3 ((v[0] as u16) << 8*1) |
7 pub fn slice_to_be32(v: &[u8]) -> u32 {
8 ((v[0] as u32) << 8*3) |
9 ((v[1] as u32) << 8*2) |
10 ((v[2] as u32) << 8*1) |
11 ((v[3] as u32) << 8*0)
13 #[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
15 pub fn slice_to_le32(v: &[u8]) -> u32 {
16 ((v[0] as u32) << 8*0) |
17 ((v[1] as u32) << 8*1) |
18 ((v[2] as u32) << 8*2) |
19 ((v[3] as u32) << 8*3)
22 pub fn slice_to_be48(v: &[u8]) -> u64 {
23 ((v[0] as u64) << 8*5) |
24 ((v[1] as u64) << 8*4) |
25 ((v[2] as u64) << 8*3) |
26 ((v[3] as u64) << 8*2) |
27 ((v[4] as u64) << 8*1) |
28 ((v[5] as u64) << 8*0)
31 pub fn slice_to_be64(v: &[u8]) -> u64 {
32 ((v[0] as u64) << 8*7) |
33 ((v[1] as u64) << 8*6) |
34 ((v[2] as u64) << 8*5) |
35 ((v[3] as u64) << 8*4) |
36 ((v[4] as u64) << 8*3) |
37 ((v[5] as u64) << 8*2) |
38 ((v[6] as u64) << 8*1) |
39 ((v[7] as u64) << 8*0)
43 pub fn be16_to_array(u: u16) -> [u8; 2] {
45 v[0] = ((u >> 8*1) & 0xff) as u8;
46 v[1] = ((u >> 8*0) & 0xff) as u8;
50 pub fn be32_to_array(u: u32) -> [u8; 4] {
52 v[0] = ((u >> 8*3) & 0xff) as u8;
53 v[1] = ((u >> 8*2) & 0xff) as u8;
54 v[2] = ((u >> 8*1) & 0xff) as u8;
55 v[3] = ((u >> 8*0) & 0xff) as u8;
58 #[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
60 pub fn le32_to_array(u: u32) -> [u8; 4] {
62 v[0] = ((u >> 8*0) & 0xff) as u8;
63 v[1] = ((u >> 8*1) & 0xff) as u8;
64 v[2] = ((u >> 8*2) & 0xff) as u8;
65 v[3] = ((u >> 8*3) & 0xff) as u8;
69 pub fn be48_to_array(u: u64) -> [u8; 6] {
70 assert!(u & 0xffff_0000_0000_0000 == 0);
72 v[0] = ((u >> 8*5) & 0xff) as u8;
73 v[1] = ((u >> 8*4) & 0xff) as u8;
74 v[2] = ((u >> 8*3) & 0xff) as u8;
75 v[3] = ((u >> 8*2) & 0xff) as u8;
76 v[4] = ((u >> 8*1) & 0xff) as u8;
77 v[5] = ((u >> 8*0) & 0xff) as u8;
81 pub fn be64_to_array(u: u64) -> [u8; 8] {
83 v[0] = ((u >> 8*7) & 0xff) as u8;
84 v[1] = ((u >> 8*6) & 0xff) as u8;
85 v[2] = ((u >> 8*5) & 0xff) as u8;
86 v[3] = ((u >> 8*4) & 0xff) as u8;
87 v[4] = ((u >> 8*3) & 0xff) as u8;
88 v[5] = ((u >> 8*2) & 0xff) as u8;
89 v[6] = ((u >> 8*1) & 0xff) as u8;
90 v[7] = ((u >> 8*0) & 0xff) as u8;
95 pub fn le64_to_array(u: u64) -> [u8; 8] {
97 v[0] = ((u >> 8*0) & 0xff) as u8;
98 v[1] = ((u >> 8*1) & 0xff) as u8;
99 v[2] = ((u >> 8*2) & 0xff) as u8;
100 v[3] = ((u >> 8*3) & 0xff) as u8;
101 v[4] = ((u >> 8*4) & 0xff) as u8;
102 v[5] = ((u >> 8*5) & 0xff) as u8;
103 v[6] = ((u >> 8*6) & 0xff) as u8;
104 v[7] = ((u >> 8*7) & 0xff) as u8;
114 assert_eq!(slice_to_be16(&[0xde, 0xad]), 0xdead);
115 assert_eq!(slice_to_be32(&[0xde, 0xad, 0xbe, 0xef]), 0xdeadbeef);
116 assert_eq!(slice_to_le32(&[0xef, 0xbe, 0xad, 0xde]), 0xdeadbeef);
117 assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad);
118 assert_eq!(slice_to_be64(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]), 0xdeadbeef1bad1dea);
119 assert_eq!(be16_to_array(0xdead), [0xde, 0xad]);
120 assert_eq!(be32_to_array(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]);
121 assert_eq!(le32_to_array(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]);
122 assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]);
123 assert_eq!(be64_to_array(0xdeadbeef1bad1dea), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]);
124 assert_eq!(le64_to_array(0xdeadbeef1bad1dea), [0xea, 0x1d, 0xad, 0x1b, 0xef, 0xbe, 0xad, 0xde]);