Decodes a hex-encoded byte slice into binary data. Returns `true` if decoding succeeded, `false` if the input contains invalid hex characters.
(bytes: &[u8], out: &mut Vec<u8>)
| 82 | /// Decodes a hex-encoded byte slice into binary data. |
| 83 | /// Returns `true` if decoding succeeded, `false` if the input contains invalid hex characters. |
| 84 | fn unhex_common(bytes: &[u8], out: &mut Vec<u8>) -> bool { |
| 85 | if bytes.is_empty() { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | let mut i = 0usize; |
| 90 | |
| 91 | // If the hex string length is odd, implicitly left-pad with '0'. |
| 92 | if (bytes.len() & 1) == 1 { |
| 93 | match hex_nibble(bytes[0]) { |
| 94 | // Equivalent to (0 << 4) | lo |
| 95 | Some(lo) => out.push(lo), |
| 96 | None => return false, |
| 97 | } |
| 98 | i = 1; |
| 99 | } |
| 100 | |
| 101 | while i + 1 < bytes.len() { |
| 102 | match (hex_nibble(bytes[i]), hex_nibble(bytes[i + 1])) { |
| 103 | (Some(hi), Some(lo)) => out.push((hi << 4) | lo), |
| 104 | _ => return false, |
| 105 | } |
| 106 | i += 2; |
| 107 | } |
| 108 | |
| 109 | true |
| 110 | } |
| 111 | |
| 112 | /// Converts an iterator of hex strings to a binary array. |
| 113 | fn unhex_array<I, T>( |
no test coverage detected
searching dependent graphs…