reallyme-codec-hex 0.3.1

Canonical lowercase hexadecimal encoding for ReallyMe Codec.
Documentation
// SPDX-FileCopyrightText: 2026 ReallyMe LLC
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::HexError;
use zeroize::Zeroizing;

/// Decode canonical lowercase hexadecimal bytes.
pub fn lower_hex_to_bytes(input: &str) -> Result<Vec<u8>, HexError> {
    let (pairs, remainder) = input.as_bytes().as_chunks::<2>();
    if !remainder.is_empty() {
        return Err(HexError::OddLength);
    }

    let mut output = Zeroizing::new(Vec::with_capacity(pairs.len()));
    for [high_byte, low_byte] in pairs {
        let high = lower_hex_value(*high_byte)?;
        let low = lower_hex_value(*low_byte)?;
        output.push((high << 4) | low);
    }

    Ok(core::mem::take(&mut *output))
}

fn lower_hex_value(byte: u8) -> Result<u8, HexError> {
    match byte {
        b'0'..=b'9' => Ok(byte - b'0'),
        b'a'..=b'f' => Ok(byte - b'a' + 10),
        b'A'..=b'F' => Err(HexError::Uppercase),
        _ => Err(HexError::InvalidCharacter),
    }
}