use alloc::string::String;
pub fn hex_encode(data: &[u8]) -> String {
::hex::encode(data)
}
pub fn hex_encode_upper(data: &[u8]) -> String {
::hex::encode_upper(data)
}
#[must_use]
pub fn hex_decode(text: &str) -> Option<alloc::vec::Vec<u8>> {
::hex::decode(text).ok()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum HexDecodeError {
#[error("hex text has {actual} digit(s), expected {expected}")]
InvalidLength {
expected: usize,
actual: usize,
},
#[error("byte {byte:#04x} at offset {offset} is not a hex digit")]
InvalidDigit {
offset: usize,
byte: u8,
},
}
impl HexDecodeError {
pub fn name(&self) -> &'static str {
match self {
Self::InvalidLength { .. } => "invalid_length",
Self::InvalidDigit { .. } => "invalid_digit",
}
}
}
pub fn hex_decode_into(text: &str, out: &mut [u8]) -> Result<(), HexDecodeError> {
let expected = out.len().saturating_mul(2);
let length_error = HexDecodeError::InvalidLength {
expected,
actual: text.len(),
};
if text.len() != expected {
return Err(length_error);
}
if let Some((offset, byte)) = text
.bytes()
.enumerate()
.find(|(_, b)| !b.is_ascii_hexdigit())
{
return Err(HexDecodeError::InvalidDigit { offset, byte });
}
::hex::decode_to_slice(text, out).map_err(|_| length_error)
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
use alloc::string::ToString;
use alloc::vec::Vec;
#[test]
fn encodes_uppercase_zero_padded() {
assert_eq!(hex_encode_upper(&[0xDE, 0xAD, 0xBE, 0xEF]), "DEADBEEF");
assert_eq!(hex_encode_upper(&[]), "");
}
#[test]
fn encodes_lowercase_zero_padded() {
assert_eq!(hex_encode(&[0xDE, 0xAD, 0xBE, 0xEF]), "deadbeef");
assert_eq!(hex_encode(&[0x00, 0x01, 0x0A]), "00010a");
}
#[test]
fn decode_into_requires_the_exact_length_and_hex_digits() {
let mut out = [0u8; 3];
assert_eq!(hex_decode_into("00Ff12", &mut out), Ok(()));
assert_eq!(out, [0x00, 0xff, 0x12]);
assert_eq!(
hex_decode_into("00ff", &mut out),
Err(HexDecodeError::InvalidLength {
expected: 6,
actual: 4
})
);
assert_eq!(
hex_decode_into("00ff1234", &mut out),
Err(HexDecodeError::InvalidLength {
expected: 6,
actual: 8
})
);
assert_eq!(
hex_decode_into("00ff1g", &mut out),
Err(HexDecodeError::InvalidDigit {
offset: 5,
byte: b'g'
})
);
assert_eq!(hex_decode_into("", &mut []), Ok(()));
assert_eq!(
HexDecodeError::InvalidDigit {
offset: 5,
byte: b'g'
}
.to_string(),
"byte 0x67 at offset 5 is not a hex digit"
);
assert_eq!(
HexDecodeError::InvalidLength {
expected: 6,
actual: 4
}
.name(),
"invalid_length"
);
}
#[test]
fn decode_into_leaves_out_untouched_on_a_bad_digit() {
let mut out = [0xAAu8; 3];
assert_eq!(
hex_decode_into("0011zz", &mut out),
Err(HexDecodeError::InvalidDigit {
offset: 4,
byte: b'z'
})
);
assert_eq!(out, [0xAA; 3]);
}
#[test]
fn decodes_either_case_and_rejects_odd_or_non_hex() {
assert_eq!(hex_decode("00Ff12"), Some(alloc::vec![0x00, 0xff, 0x12]));
assert_eq!(hex_decode(""), Some(alloc::vec::Vec::new()));
assert_eq!(hex_decode("0"), None);
assert_eq!(hex_decode("zz"), None);
}
#[test]
fn empty_input_is_empty_output() {
assert_eq!(hex_encode(&[]), "");
}
#[test]
fn output_is_always_two_chars_per_byte() {
let all: Vec<u8> = (0..=255u8).collect();
let encoded = hex_encode(&all);
assert_eq!(encoded.len(), all.len() * 2);
let expected: String = all.iter().map(|b| format!("{b:02x}")).collect();
assert_eq!(encoded, expected);
}
}