1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#![no_std]

extern crate alloc;

// --- alloc ---
use alloc::{string::String, vec::Vec};
// --- core ---
use core::char;
// // --- crates.io ---
// use thiserror::Error as ThisError;

pub type ArrayBytesResult<T> = Result<T, Error>;

// #[derive(Debug, ThisError)]
// #[cfg_attr(test, derive(PartialEq))]
// pub enum Error {
// 	#[error("Invalid length: {}", length)]
// 	InvalidLength { length: usize },
// 	#[error("Invalid char boundary at: {}", index)]
// 	InvalidCharBoundary { index: usize },
// }
#[derive(Debug)]
#[cfg_attr(test, derive(PartialEq))]
pub enum Error {
	InvalidLength { length: usize },
	InvalidCharBoundary { index: usize },
}

/// `Slice`/`Vec` to a fixed length `u8` array
#[macro_export]
macro_rules! array {
	($bytes:expr, $len:expr) => {{
		unsafe { *($bytes.as_ptr() as *const [u8; $len]) }
	}};
}

/// hex to bytes
///
/// error while length is a odd number or any byte out of radix
pub fn hex2bytes(hex: impl AsRef<str>) -> ArrayBytesResult<Vec<u8>> {
	let hex = hex.as_ref();

	if hex.len() % 2 != 0 {
		return Err(Error::InvalidLength {
			length: if hex.starts_with("0x") {
				hex.len() - 2
			} else {
				hex.len()
			},
		});
	}

	let mut bytes = Vec::new();

	for i in (if hex.starts_with("0x") { 2 } else { 0 }..hex.len()).step_by(2) {
		for i in i + 1..i + 3 {
			if !hex.is_char_boundary(i) {
				return Err(Error::InvalidCharBoundary { index: i });
			}
		}

		// radix is always 16 which will never fail this; qed
		bytes.push(u8::from_str_radix(&hex[i..i + 2], 16).unwrap());
	}

	Ok(bytes)
}

/// just like `hex2bytes` but without checking
pub fn hex2bytes_unchecked(hex: impl AsRef<str>) -> Vec<u8> {
	let hex = hex.as_ref();

	(if hex.starts_with("0x") { 2 } else { 0 }..hex.len())
		.step_by(2)
		.map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap())
		.collect()
}

/// just like `hex2bytes_unchecked` but to a fixed length array
#[macro_export]
macro_rules! hex2array_unchecked {
	($hex:expr, $len:expr) => {{
		$crate::array!($crate::hex2bytes_unchecked($hex), $len)
	}};
}

/// bytes to hex
pub fn bytes2hex(prefix: impl AsRef<str>, bytes: impl AsRef<[u8]>) -> String {
	let prefix = prefix.as_ref();
	let bytes = bytes.as_ref();
	let mut hex = String::with_capacity(prefix.len() + bytes.len() * 2);

	for byte in prefix.chars() {
		hex.push(byte);
	}
	for byte in bytes.iter() {
		hex.push(char::from_digit((byte >> 4) as _, 16).unwrap());
		hex.push(char::from_digit((byte & 0xf) as _, 16).unwrap());
	}

	hex
}

#[cfg(test)]
mod test {
	// --- array-bytes ---
	use crate::*;

	macro_rules! vec {
		($v:expr; $n:expr) => {{
			let mut v = Vec::new();

			for _ in 0..$n {
				v.push($v);
			}

			v
		}};
	}

	#[test]
	fn array_should_work() {
		for v in 0u8..16 {
			assert_eq!([v; 8], array!(vec![v; 10], 8));
		}
	}

	#[test]
	fn hex2bytes_should_work() {
		assert_eq!(
			hex2bytes("49204c6f766520596f75").unwrap(),
			b"I Love You".to_vec()
		);
		assert_eq!(
			hex2bytes("0x49204c6f766520596f75").unwrap(),
			b"I Love You".to_vec()
		);

		assert_eq!(
			hex2bytes(String::from("我爱你")).unwrap_err(),
			Error::InvalidLength { length: 9 }
		);
		assert_eq!(
			hex2bytes(String::from("0x我爱你")).unwrap_err(),
			Error::InvalidLength { length: 9 }
		);

		assert_eq!(
			hex2bytes(String::from("我爱你 ")).unwrap_err(),
			Error::InvalidCharBoundary { index: 1 }
		);
		assert_eq!(
			hex2bytes(String::from(" 我爱你")).unwrap_err(),
			Error::InvalidCharBoundary { index: 2 }
		);
	}

	#[test]
	fn hex2bytes_unchecked_should_work() {
		assert_eq!(
			hex2bytes_unchecked("49204c6f766520596f75"),
			b"I Love You".to_vec()
		);
		assert_eq!(
			hex2bytes_unchecked("0x49204c6f766520596f75"),
			b"I Love You".to_vec()
		);
	}

	#[test]
	fn hex2array_unchecked_should_work() {
		assert_eq!(
			hex2array_unchecked!("49204c6f766520596f75", 10),
			*b"I Love You"
		);
		assert_eq!(
			hex2array_unchecked!("0x49204c6f766520596f75", 10),
			*b"I Love You"
		);
	}

	#[test]
	fn bytes2hex_should_work() {
		assert_eq!(
			bytes2hex("", b"I Love You"),
			String::from("49204c6f766520596f75")
		);
		assert_eq!(
			bytes2hex("0x", b"I Love You"),
			String::from("0x49204c6f766520596f75")
		);
	}
}