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_be48(v: &[u8]) -> u64 {
12 ((v[0] as u64) << 8*5) |
13 ((v[1] as u64) << 8*4) |
14 ((v[2] as u64) << 8*3) |
15 ((v[3] as u64) << 8*2) |
16 ((v[4] as u64) << 8*1) |
17 ((v[5] as u64) << 8*0)
20 pub fn slice_to_be64(v: &[u8]) -> u64 {
21 ((v[0] as u64) << 8*7) |
22 ((v[1] as u64) << 8*6) |
23 ((v[2] as u64) << 8*5) |
24 ((v[3] as u64) << 8*4) |
25 ((v[4] as u64) << 8*3) |
26 ((v[5] as u64) << 8*2) |
27 ((v[6] as u64) << 8*1) |
28 ((v[7] as u64) << 8*0)
32 pub fn be16_to_array(u: u16) -> [u8; 2] {
34 v[0] = ((u >> 8*1) & 0xff) as u8;
35 v[1] = ((u >> 8*0) & 0xff) as u8;
39 pub fn be32_to_array(u: u32) -> [u8; 4] {
41 v[0] = ((u >> 8*3) & 0xff) as u8;
42 v[1] = ((u >> 8*2) & 0xff) as u8;
43 v[2] = ((u >> 8*1) & 0xff) as u8;
44 v[3] = ((u >> 8*0) & 0xff) as u8;
48 pub fn be48_to_array(u: u64) -> [u8; 6] {
49 assert!(u & 0xffff_0000_0000_0000 == 0);
51 v[0] = ((u >> 8*5) & 0xff) as u8;
52 v[1] = ((u >> 8*4) & 0xff) as u8;
53 v[2] = ((u >> 8*3) & 0xff) as u8;
54 v[3] = ((u >> 8*2) & 0xff) as u8;
55 v[4] = ((u >> 8*1) & 0xff) as u8;
56 v[5] = ((u >> 8*0) & 0xff) as u8;
60 pub fn be64_to_array(u: u64) -> [u8; 8] {
62 v[0] = ((u >> 8*7) & 0xff) as u8;
63 v[1] = ((u >> 8*6) & 0xff) as u8;
64 v[2] = ((u >> 8*5) & 0xff) as u8;
65 v[3] = ((u >> 8*4) & 0xff) as u8;
66 v[4] = ((u >> 8*3) & 0xff) as u8;
67 v[5] = ((u >> 8*2) & 0xff) as u8;
68 v[6] = ((u >> 8*1) & 0xff) as u8;
69 v[7] = ((u >> 8*0) & 0xff) as u8;
79 assert_eq!(slice_to_be48(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]), 0xdeadbeef1bad);
80 assert_eq!(slice_to_be64(&[0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]), 0xdeadbeef1bad1dea);
81 assert_eq!(be16_to_array(0xdead), [0xde, 0xad]);
82 assert_eq!(be32_to_array(0xdeadbeef), [0xde, 0xad, 0xbe, 0xef]);
83 assert_eq!(be48_to_array(0xdeadbeef1bad), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad]);
84 assert_eq!(be64_to_array(0xdeadbeef1bad1dea), [0xde, 0xad, 0xbe, 0xef, 0x1b, 0xad, 0x1d, 0xea]);