depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit 029cae75e52654c51daa671cec151874ce93a448
parent f0a82b3a64457d18b29ae497688cfbe9890443e2
Author: Antoine A <>
Date:   Thu,  2 Dec 2021 16:47:29 +0100

Simplify doc and add fuzzing

Diffstat:
Muri-pack/Cargo.toml | 4++++
Muri-pack/src/lib.rs | 70+++++++++++++++++++++++-----------------------------------------------
2 files changed, 27 insertions(+), 47 deletions(-)

diff --git a/uri-pack/Cargo.toml b/uri-pack/Cargo.toml @@ -19,6 +19,10 @@ url = "2.2.2" criterion = "0.3.5" # Fast insecure random fastrand = "1.5.0" +# Fuzzing test +quickcheck = "1.0.3" +quickcheck_macros = "1.0.0" + [[bench]] name = "pack" diff --git a/uri-pack/src/lib.rs b/uri-pack/src/lib.rs @@ -65,33 +65,13 @@ pub fn pack_uri(uri: &str) -> Result<Vec<u8>, EncodeErr> { return Err(EncodeErr::UnsupportedChar(*char)); } - // Holds pending bits beginning from the most significant bits - // If buff contains 10110: - // buff_bits _____ - // buff 10110000 + // Holds [buff_bits] pending bits beginning from the most significant bits let (mut buff, mut buff_bits) = (0u8, 0u8); + // Write [nb_bits] less significant bits from [nb] to [buff] let mut write_bits = |nb: u8, mut nb_bits: u8| { - // buff_bits ____ - // buff 10110000 - // nb_bits ____ - // nb 00011010 - // - // ___ - // writable 00011010 - // ___ - // rmv_right 00001101 - // ___ - // rmv_left 10100000 - // ___ - // align 00000101 - // - // buff_bits ________ - // buff 10110101 - // nb_bits _ - // nb 00011010 while nb_bits > 0 { - // Amount of bits we can write in buff + // Amount of bits we can write in buffer let writable = (8 - buff_bits).min(nb_bits); // Remove non writable bits let rmv_right = nb >> nb_bits - writable; @@ -99,12 +79,12 @@ pub fn pack_uri(uri: &str) -> Result<Vec<u8>, EncodeErr> { // Align remaining bits with buff blank bits let align = rmv_left >> buff_bits; - // Write bits in buff + // Write bits in buffer buff = buff | align; buff_bits += writable; nb_bits -= writable; - // Store buff if full + // Store buffer if full if buff_bits == 8 { vec.push(buff); buff = 0; @@ -137,31 +117,12 @@ pub fn unpack_uri(bytes: &[u8]) -> Result<String, DecodeErr> { let mut buf = String::with_capacity(bytes.len()); let mut iter = bytes.iter(); - // Holds pending bits beginning from the most significant bits - // If buff contains 10110: - // buff_bits _____ - // buff 10110000 + // Holds [buff_bits] pending bits beginning from the most significant bits let (mut buff, mut buff_bits) = (0u8, 0u8); + // Write [nb_bits] less significant bits from [buff] to [nb] let mut read_nb = |mut nb_bits: u8| -> Result<u8, DecodeErr> { - // buff_bits _____ - // buff 10110000 - // nb_bits ___ - // nb 00000000 - // - // ___ - // readable 10110000 - // ___ - // rmv_left 01100000 - // ___ - // align 00000011 - // - // buff_bits _ - // buff 10110000 - // nb_bits - // nb 00000011 - - let mut nb = 8; + let mut nb = 0; while nb_bits > 0 { // Load buff if empty if buff_bits == 0 { @@ -195,6 +156,10 @@ pub fn unpack_uri(bytes: &[u8]) -> Result<String, DecodeErr> { } #[cfg(test)] +#[macro_use(quickcheck)] +extern crate quickcheck_macros; + +#[cfg(test)] mod test { use std::str::FromStr; @@ -260,4 +225,15 @@ mod test { assert_eq!(href, decoded); } } + + #[quickcheck] + fn fuzz(input: String) -> bool { + if input.as_bytes().iter().all(supported_ascii) { + let packed = pack_uri(&input).unwrap(); + let unpacked = unpack_uri(&packed).unwrap(); + input == unpacked + } else { + true + } + } }