1 /* Copyright (c) 2023, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 */
15 use alloc::vec::Vec;
16
17 #[allow(clippy::expect_used, clippy::unwrap_used, clippy::indexing_slicing)]
decode_hex<const N: usize>(s: &str) -> [u8; N]18 pub(crate) fn decode_hex<const N: usize>(s: &str) -> [u8; N] {
19 (0..s.len())
20 .step_by(2)
21 .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("Invalid hex string"))
22 .collect::<Vec<u8>>()
23 .as_slice()
24 .try_into()
25 .unwrap()
26 }
27
28 #[allow(clippy::expect_used, clippy::unwrap_used, clippy::indexing_slicing)]
decode_hex_into_vec(s: &str) -> Vec<u8>29 pub(crate) fn decode_hex_into_vec(s: &str) -> Vec<u8> {
30 (0..s.len())
31 .step_by(2)
32 .map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("Invalid hex string"))
33 .collect::<Vec<u8>>()
34 }
35