From 34e2d3a0e8829617ac07c75e0af59571d9fd8e15 Mon Sep 17 00:00:00 2001 From: Matthew Jackson <1085847+MattJackson@users.noreply.github.com> Date: Sun, 28 Jun 2026 22:12:07 -0700 Subject: [PATCH] 1.2.0: route online/keydb hex parsing through libfreemkv::hex (one parser) --- src/keydb_format.rs | 30 ++++-------------------------- src/online.rs | 17 +++-------------- 2 files changed, 7 insertions(+), 40 deletions(-) diff --git a/src/keydb_format.rs b/src/keydb_format.rs index fca6aa5..a06a477 100644 --- a/src/keydb_format.rs +++ b/src/keydb_format.rs @@ -95,18 +95,8 @@ pub struct DiscEntry { /// codepoint) must not panic on a mid-codepoint slice. Any non-hex /// byte yields `None`. pub(crate) fn parse_hex(s: &str) -> Option> { - let s = s.trim().trim_start_matches("0x").trim_start_matches("0X"); - let bytes = s.as_bytes(); - if bytes.len() % 2 != 0 { - return None; - } - let mut out = Vec::with_capacity(bytes.len() / 2); - for pair in bytes.chunks_exact(2) { - let hi = (pair[0] as char).to_digit(16)?; - let lo = (pair[1] as char).to_digit(16)?; - out.push((hi * 16 + lo) as u8); - } - Some(out) + // The one workspace hex parser (strips an optional 0x/0X, byte-based). + libfreemkv::hex::parse_hex_bytes(s) } /// Read the run of consecutive ASCII decimal digits immediately following the @@ -147,23 +137,11 @@ fn parse_revoked_at_mkb(line: &str) -> Option { /// Parse hex into a fixed-size array. pub(crate) fn parse_hex16(s: &str) -> Option<[u8; 16]> { - let v = parse_hex(s)?; - if v.len() != 16 { - return None; - } - let mut out = [0u8; 16]; - out.copy_from_slice(&v); - Some(out) + libfreemkv::hex::parse_hex_fixed::<16>(s) } pub(crate) fn parse_hex20(s: &str) -> Option<[u8; 20]> { - let v = parse_hex(s)?; - if v.len() != 20 { - return None; - } - let mut out = [0u8; 20]; - out.copy_from_slice(&v); - Some(out) + libfreemkv::hex::parse_hex_fixed::<20>(s) } impl KeyDb { diff --git a/src/online.rs b/src/online.rs index ddf8434..62e5b4f 100644 --- a/src/online.rs +++ b/src/online.rs @@ -346,20 +346,9 @@ fn bearer_header(secret: &str) -> Option { } fn parse_uk(hex: &str) -> Option<[u8; 16]> { - if hex.len() != 32 { - return None; - } - // Reject any non-hex byte up front. `u8::from_str_radix` on a 2-char - // window otherwise accepts sign prefixes (e.g. "+5", "-A"), letting a - // signed/whitespace-tainted string slip through as a valid key. - if !hex.bytes().all(|b| b.is_ascii_hexdigit()) { - return None; - } - let mut out = [0u8; 16]; - for (i, b) in out.iter_mut().enumerate() { - *b = u8::from_str_radix(hex.get(i * 2..i * 2 + 2)?, 16).ok()?; - } - Some(out) + // The one workspace hex parser: byte-based (rejects sign chars / multi-byte), + // 32 hex digits → [u8; 16], with an optional 0x/0X prefix tolerated. + libfreemkv::hex::parse_hex_fixed::<16>(hex) } #[cfg(test)]