1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
11 pub fn slice_to_be16(v: &[u8]) -> u16 {
12 ((v[0] as u16) << 8*1) |
13 ((v[1] as u16) << 8*0)
16 pub fn slice_to_be32(v: &[u8]) -> u32 {
17 ((v[0] as u32) << 8*3) |
18 ((v[1] as u32) << 8*2) |
19 ((v[2] as u32) << 8*1) |
20 ((v[3] as u32) << 8*0)
22 #[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
24 pub fn slice_to_le32(v: &[u8]) -> u32 {
25 ((v[0] as u32) << 8*0) |
26 ((v[1] as u32) << 8*1) |
27 ((v[2] as u32) << 8*2) |
28 ((v[3] as u32) << 8*3)
31 pub fn slice_to_be48(v: &[u8]) -> u64 {
32 ((v[0] as u64) << 8*5) |
33 ((v[1] as u64) << 8*4) |
34 ((v[2] as u64) << 8*3) |
35 ((v[3] as u64) << 8*2) |
36 ((v[4] as u64) << 8*1) |
37 ((v[5] as u64) << 8*0)
40 pub fn slice_to_be64(v: &[u8]) -> u64 {
41 ((v[0] as u64) << 8*7) |
42 ((v[1] as u64) << 8*6) |
43 ((v[2] as u64) << 8*5) |
44 ((v[3] as u64) << 8*4) |
45 ((v[4] as u64) << 8*3) |
46 ((v[5] as u64) << 8*2) |
47 ((v[6] as u64) << 8*1) |
48 ((v[7] as u64) << 8*0)
52 pub fn be16_to_array(u: u16) -> [u8; 2] {
54 v[0] = ((u >> 8*1) & 0xff) as u8;
55 v[1] = ((u >> 8*0) & 0xff) as u8;
59 pub fn be32_to_array(u: u32) -> [u8; 4] {
61 v[0] = ((u >> 8*3) & 0xff) as u8;
62 v[1] = ((u >> 8*2) & 0xff) as u8;
63 v[2] = ((u >> 8*1) & 0xff) as u8;
64 v[3] = ((u >> 8*0) & 0xff) as u8;
67 #[cfg(not(feature = "fuzztarget"))] // Used only by poly1305
69 pub fn le32_to_array(u: u32) -> [u8; 4] {
71 v[0] = ((u >> 8*0) & 0xff) as u8;
72 v[1] = ((u >> 8*1) & 0xff) as u8;
73 v[2] = ((u >> 8*2) & 0xff) as u8;
74 v[3] = ((u >> 8*3) & 0xff) as u8;
78 pub fn be48_to_array(u: u64) -> [u8; 6] {
79 assert!(u & 0xffff_0000_0000_0000 == 0);
81 v[0] = ((u >> 8*5) & 0xff) as u8;
82 v[1] = ((u >> 8*4) & 0xff) as u8;
83 v[2] = ((u >> 8*3) & 0xff) as u8;
84 v[3] = ((u >> 8*2) & 0xff) as u8;
85 v[4] = ((u >> 8*1) & 0xff) as u8;
86 v[5] = ((u >> 8*0) & 0xff) as u8;
90 pub fn be64_to_array(u: u64) -> [u8; 8] {
92 v[0] = ((u >> 8*7) & 0xff) as u8;
93 v[1] = ((u >> 8*6) & 0xff) as u8;
94 v[2] = ((u >> 8*5) & 0xff) as u8;
95 v[3] = ((u >> 8*4) & 0xff) as u8;
96 v[4] = ((u >> 8*3) & 0xff) as u8;
97 v[5] = ((u >> 8*2) & 0xff) as u8;
98 v[6] = ((u >> 8*1) & 0xff) as u8;
99 v[7] = ((u >> 8*0) & 0xff) as u8;
104 pub fn le64_to_array(u: u64) -> [u8; 8] {
106 v[0] = ((u >> 8*0) & 0xff) as u8;
107 v[1] = ((u >> 8*1) & 0xff) as u8;
108 v[2] = ((u >> 8*2) & 0xff) as u8;
109 v[3] = ((u >> 8*3) & 0xff) as u8;
110 v[4] = ((u >> 8*4) & 0xff) as u8;
111 v[5] = ((u >> 8*5) & 0xff) as u8;
112 v[6] = ((u >> 8*6) & 0xff) as u8;
113 v[7] = ((u >> 8*7) & 0xff) as u8;
123 assert_eq!(slice_to_be16(&[0xde, 0xad]), 0xdead);
124 assert_eq!(slice_to_be32(&[0xde, 0xad, 0xbe, 0xef]), 0xdeadbeef);
125 assert_eq!(slice_to_le32(&[0xef, 0xbe, 0xad, 0xde]), 0xdeadbeef);
126 assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad);
127 assert_eq!(slice_to_be64(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]), 0xdeadbeef1bad1dea);
128 assert_eq!(be16_to_array(0xdead), [0xde, 0xad]);
129 assert_eq!(be32_to_array(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]);
130 assert_eq!(le32_to_array(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]);
131 assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]);
132 assert_eq!(be64_to_array(0xdeadbeef1bad1dea), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]);
133 assert_eq!(le64_to_array(0xdeadbeef1bad1dea), [0xea, 0x1d, 0xad, 0x1b, 0xef, 0xbe, 0xad, 0xde]);