chiark / gitweb /
Rework hex parsing
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 30 May 2021 21:27:20 +0000 (22:27 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 1 Jun 2021 00:32:13 +0000 (01:32 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/utils.rs

index e2b1faa29b77d2fbf4786bd48b9861a4f5052eb6..70337a79abe93e9a20f185932f6209139f15e5f8 100644 (file)
@@ -650,9 +650,13 @@ pub fn fmt_hex(f: &mut Formatter, buf: &[u8]) {
 }
 
 #[throws(as Option)]
-pub fn parse_fixed_hex<const N: usize>(s: &str) -> [u8; N] {
-  if s.len() != N*2 || ! s.is_ascii() { throw!() }
-  let mut buf = [0u8; N];
+#[must_use]
+pub fn parse_slice_hex(s: &str, buf: &mut [u8]) -> usize {
+  let l = s.len();
+  if l % 1 != 0 { throw!() }
+  let l = l/2;
+  if l > buf.len() { throw!() }
+
   for (h, o) in izip!(
     s.as_bytes().chunks(2),
     buf.iter_mut(),
@@ -660,6 +664,15 @@ pub fn parse_fixed_hex<const N: usize>(s: &str) -> [u8; N] {
     let h = str::from_utf8(h).ok()?;
     *o = u8::from_str_radix(h,16).ok()?;
   }
+
+  l
+}
+
+#[throws(as Option)]
+pub fn parse_fixed_hex<const N: usize>(s: &str) -> [u8; N] {
+  let mut buf = [0u8; N];
+  let l = parse_slice_hex(s, &mut buf)?;
+  if l != N { throw!() }
   buf
 }