diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-11-18 23:14:47 +0000 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-11-18 23:45:31 +0000 |
commit | 0262ded968ece38cff5cb80626299155ef040228 (patch) | |
tree | a1c089ba7ad59aa99ad60af9d548dcb28a0cc227 /tests | |
parent | 90956826a5e267e8c9d220561e8b2506efe97d02 (diff) | |
download | vt100-rust-0262ded968ece38cff5cb80626299155ef040228.tar.gz vt100-rust-0262ded968ece38cff5cb80626299155ef040228.zip |
add fuzzer input generator
Diffstat (limited to 'tests')
-rw-r--r-- | tests/helpers/mod.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/helpers/mod.rs b/tests/helpers/mod.rs index 20de2d4..d3dd05d 100644 --- a/tests/helpers/mod.rs +++ b/tests/helpers/mod.rs @@ -289,3 +289,62 @@ fn hex_char(c: u8) -> Result<u8, String> { pub fn hex(upper: u8, lower: u8) -> Result<u8, String> { Ok(hex_char(upper)? * 16 + hex_char(lower)?) } + +#[allow(dead_code)] +pub fn unhex(s: &[u8]) -> Vec<u8> { + let mut ret = vec![]; + let mut i = 0; + while i < s.len() { + if s[i] == b'\\' { + match s[i + 1] { + b'\\' => { + ret.push(b'\\'); + i += 2; + } + b'x' => { + let upper = s[i + 2]; + let lower = s[i + 3]; + ret.push(hex(upper, lower).unwrap()); + i += 4; + } + b'u' => { + assert_eq!(s[i + 2], b'{'); + let mut digits = vec![]; + let mut j = i + 3; + while s[j] != b'}' { + digits.push(s[j]); + j += 1; + } + let digits: Vec<_> = digits + .iter() + .copied() + .skip_while(|x| x == &b'0') + .collect(); + let digits = String::from_utf8(digits).unwrap(); + let codepoint = u32::from_str_radix(&digits, 16).unwrap(); + let c = char::try_from(codepoint).unwrap(); + let mut bytes = [0; 4]; + ret.extend(c.encode_utf8(&mut bytes).bytes()); + i = j + 1; + } + b'r' => { + ret.push(0x0d); + i += 2; + } + b'n' => { + ret.push(0x0a); + i += 2; + } + b't' => { + ret.push(0x09); + i += 2; + } + _ => panic!("invalid escape"), + } + } else { + ret.push(s[i]); + i += 1; + } + } + ret +} |