| 172 | #[pyfunction(name = "a2b_hex")] |
| 173 | #[pyfunction] |
| 174 | fn unhexlify(data: ArgAsciiBuffer, vm: &VirtualMachine) -> PyResult<Vec<u8>> { |
| 175 | data.with_ref(|hex_bytes| { |
| 176 | if hex_bytes.len() % 2 != 0 { |
| 177 | return Err(super::new_binascii_error( |
| 178 | "Odd-length string".to_owned(), |
| 179 | vm, |
| 180 | )); |
| 181 | } |
| 182 | |
| 183 | let mut unhex = Vec::<u8>::with_capacity(hex_bytes.len() / 2); |
| 184 | for (n1, n2) in hex_bytes.iter().tuples() { |
| 185 | if let (Some(n1), Some(n2)) = (unhex_nibble(*n1), unhex_nibble(*n2)) { |
| 186 | unhex.push((n1 << 4) | n2); |
| 187 | } else { |
| 188 | return Err(super::new_binascii_error( |
| 189 | "Non-hexadecimal digit found".to_owned(), |
| 190 | vm, |
| 191 | )); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | Ok(unhex) |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | #[pyfunction] |
| 200 | pub(crate) fn crc32(data: ArgBytesLike, init: OptionalArg<PyIntRef>) -> u32 { |