Merge pull request #13 from jaybosamiya/randomness

Introduces (optional) randomness to state order
This commit is contained in:
8051Enthusiast 2020-04-27 03:55:48 +02:00 committed by GitHub
commit 6b720da3ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 1 deletions

71
Cargo.lock generated
View File

@ -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"

View File

@ -13,3 +13,4 @@ license = "Unlicense"
[dependencies]
regex-automata = "0.1"
clap = "2.33"
rand = "0.7.3"

View File

@ -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;
@ -33,6 +34,7 @@ fn determine_state_positions<D: DFA>(
// keep track of visited states
let mut state_set = HashSet::new();
state_vec.push(dfa.start_state());
while let Some(&current_state) = state_vec.get(current_index) {
// queue all unvisited states from current state
for &next_byte in validlist {
@ -41,6 +43,12 @@ fn determine_state_positions<D: DFA>(
state_vec.push(next_state);
}
}
current_index += 1;
}
state_vec[1..].shuffle(&mut thread_rng());
for &current_state in &state_vec {
// 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) {
@ -61,7 +69,6 @@ fn determine_state_positions<D: DFA>(
}
None => return Err("State machine exceeds Fate32 capacity!"),
}
current_index += 1;
}
Ok(StateFatMap {
blocks: current_block - 2,
@ -118,6 +125,35 @@ fn regex_to_fat32<D: DFA, W: Write>(
}
fn main() {
let matches = App::new("regex2fat")
.version("0.1.0")
.author("8051Enthusiast")
.about("Convert regex DFAs to FAT32 file systems")
.arg(
Arg::with_name("anchor")
.short("a")
.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)
.index(1)
.help("The regex pattern to match"),
)
.arg(
Arg::with_name("outfile")
.required(true)
.index(2)
.help("The file to write the fat fs to"),
)
.get_matches();
let matches =
App::new("regex2fat")
.version("0.1.0")
@ -147,6 +183,12 @@ fn main() {
.long("nomatch")
.help("Generate NOMATCH files (off by default)"),
)
.arg(
Arg::with_name("randomize")
.short("r")
.long("randomize")
.help("Randomize cluster numbers for the states (off by default)"),
)
.get_matches();
let pattern = matches.value_of("pattern").unwrap();
let dfa = dense::Builder::new()