From c6af346c9253e28f6d593f18f9bbc533c31c8b59 Mon Sep 17 00:00:00 2001 From: Jay Bosamiya Date: Sun, 26 Apr 2020 14:53:51 -0400 Subject: [PATCH] Introduces randomness to state order --- Cargo.lock | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 15 +++++++++++ 3 files changed, 87 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index f64c90e..1fab22d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,12 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "clap" version = "2.33.0" @@ -47,6 +53,17 @@ dependencies = [ "vec_map", ] +[[package]] +name = "getrandom" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hermit-abi" version = "0.1.10" @@ -62,6 +79,53 @@ version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dea0c0405123bba743ee3f91f49b1c7cfb684eef0da0a50110f758ccf24cdff0" +[[package]] +name = "ppv-lite86" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + [[package]] name = "regex-automata" version = "0.1.9" @@ -83,6 +147,7 @@ name = "regex2fat" version = "0.1.0" dependencies = [ "clap", + "rand", "regex-automata", ] @@ -113,6 +178,12 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi" version = "0.3.8" diff --git a/Cargo.toml b/Cargo.toml index 6a73ff9..574ad9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ license = "Unlicense" [dependencies] regex-automata = "0.1" clap = "2.33" +rand = "0.7.3" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3a7b6fe..d84bd02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod fat32; use clap::{App, Arg}; use fat32::{StateFatMap, StatePosInfo, UFat}; +use rand::{thread_rng,seq::SliceRandom}; use regex_automata::{dense, DFA}; use std::collections::{HashMap, HashSet}; use std::error::Error; @@ -32,6 +33,7 @@ fn determine_state_positions( // keep track of visited states let mut state_set = HashSet::new(); state_vec.push(dfa.start_state()); + while let Some(¤t_state) = state_vec.get(current_index) { // queue all unvisited states from current state for &next_byte in validlist { @@ -40,6 +42,13 @@ fn determine_state_positions( state_vec.push(next_state); } } + current_index += 1; + } + + state_vec.shuffle(&mut thread_rng()); + current_index = 0; + + while let Some(¤t_state) = state_vec.get(current_index) { // relevant for size of directory (but mostly not because it's constant // and they're both the same) let size = if dfa.is_match_state(current_state) { @@ -124,6 +133,12 @@ fn main() { .long("anchor") .help("Anchor regex at beginning (off by default)"), ) + .arg( + Arg::with_name("randomize") + .short("r") + .long("randomize") + .help("Randomize cluster numbers for the states (off by default)"), + ) .arg( Arg::with_name("pattern") .required(true)