| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago | 1 | //@ args: run |
| 2 | // base16ct 1.0.0, transpiled as a multi-file crate. |
| 3 | // |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago | 4 | // `error.rs`, `lower.rs`, `upper.rs` and `mixed.rs` are the crate's own |
| 5 | // files, byte-for-byte. |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago | 6 | // This file carries `lib.rs`'s core -- `decoded_len`, `encoded_len` and |
| 7 | // `decode_inner` verbatim -- plus a driver, because the runner needs a `main`. |
| 8 | // The `alloc`-gated items are off, as they are by default in the crate. |
| 9 | |
| 10 | mod error; |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago | 11 | mod lower; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago | 12 | mod mixed; |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago | 13 | mod upper; |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago | 14 | |
| 15 | pub use crate::error::{Error, Result}; |
| 16 | |
| 17 | /// Compute decoded length of the given hex-encoded input. |
| 18 | #[inline(always)] |
| 19 | pub fn decoded_len(bytes: &[u8]) -> Result<usize> { |
| 20 | if bytes.len() & 1 == 0 { |
| 21 | Ok(bytes.len() / 2) |
| 22 | } else { |
| 23 | Err(Error::InvalidLength) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// Get the length of Base16 (hex) produced by encoding the given bytes. |
| 28 | #[inline(always)] |
| 29 | pub fn encoded_len(bytes: &[u8]) -> usize { |
| 30 | bytes.len() * 2 |
| 31 | } |
| 32 | |
| 33 | fn decode_inner<'a>( |
| 34 | src: &[u8], |
| 35 | dst: &'a mut [u8], |
| 36 | decode_nibble: impl Fn(u8) -> u16, |
| 37 | ) -> Result<&'a [u8]> { |
| 38 | let dst = dst |
| 39 | .get_mut(..decoded_len(src)?) |
| 40 | .ok_or(Error::InvalidLength)?; |
| 41 | |
| 42 | let mut err: u16 = 0; |
| 43 | for (src, dst) in src.chunks_exact(2).zip(dst.iter_mut()) { |
| 44 | let byte = (decode_nibble(src[0]) << 4) | decode_nibble(src[1]); |
| 45 | err |= byte >> 8; |
| 46 | *dst = byte as u8; |
| 47 | } |
| 48 | |
| 49 | match err { |
| 50 | 0 => Ok(dst), |
| 51 | _ => Err(Error::InvalidEncoding), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn show(tag: &str, r: Result<&[u8]>) { |
| 56 | match r { |
| 57 | Ok(v) => { |
| 58 | print!("{} ok ", tag); |
| 59 | for b in v.iter() { |
| 60 | print!("{:02x}", b); |
| 61 | } |
| 62 | println!(" len={}", v.len()); |
| 63 | } |
| 64 | Err(e) => println!("{} err {:?} / {}", tag, e, e), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn main() { |
| 69 | let mut buf = [0u8; 16]; |
| 70 | |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago | 71 | show("lower", lower::decode(b"abcd1234", &mut buf)); |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago | 72 | show("mixed-l", mixed::decode(b"abcd1234", &mut buf)); |
| 73 | show("mixed-u", mixed::decode(b"ABCD1234", &mut buf)); |
| 74 | show("mixed-m", mixed::decode(b"abCD1234", &mut buf)); |
| 75 | show("edge", mixed::decode(b"00ff7f80", &mut buf)); |
| 76 | show("oddlen", mixed::decode(b"abc", &mut buf)); |
| 77 | show("bad", mixed::decode(b"zzzz", &mut buf)); |
| 78 | show("empty", mixed::decode(b"", &mut buf)); |
| 79 | |
| 80 | let mut small = [0u8; 2]; |
| 81 | show("short-dst", mixed::decode(b"abcd1234", &mut small)); |
| 82 | |
| Add closures and unsafe; base16ct's lower.rs and upper.rs go through 0a375d8 nandithebull 19h ago | 83 | show("upper", upper::decode(b"ABCD1234", &mut buf)); |
| 84 | show("upper-rej", upper::decode(b"abcd1234", &mut buf)); |
| 85 | |
| 86 | let mut enc = [0u8; 8]; |
| 87 | show("encode", lower::encode(b"\xab\xcd\x12\x34", &mut enc)); |
| 88 | let mut encu = [0u8; 8]; |
| 89 | show("encode-up", upper::encode(b"\xab\xcd\x12\x34", &mut encu)); |
| 90 | |
| 91 | // `encode_str` is a closure over an `unsafe` block returning a borrowed |
| 92 | // `&str` -- a view of the bytes just written, not a copy. |
| 93 | let mut enc2 = [0u8; 8]; |
| 94 | match lower::encode_str(b"\xab\xcd\x12\x34", &mut enc2) { |
| 95 | Ok(s) => println!("encode_str ok {} len={}", s, s.len()), |
| 96 | Err(e) => println!("encode_str err {:?}", e), |
| 97 | } |
| 98 | let mut tiny = [0u8; 2]; |
| 99 | match lower::encode_str(b"\xab\xcd", &mut tiny) { |
| 100 | Ok(s) => println!("tiny ok {}", s), |
| 101 | Err(e) => println!("tiny err {:?} / {}", e, e), |
| 102 | } |
| Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 nandithebull 19h ago | 103 | |
| 104 | println!("{} {}", decoded_len(b"abcd").unwrap(), encoded_len(b"\xab\xcd")); |
| 105 | } |