#![allow(warnings, missing_docs)]
extern crate std;
use std::{
io::Read,
path::{Path, PathBuf},
};
#[cfg(doc)]
use crate as entest;
use entest::{Entest, EntestResult};
use clap::{Parser, CommandFactory};
#[derive(Debug, Parser)]
#[command(name="entest", version, author, about="entest (entropy test) is a program that applies tests to byte sequences stored in files or streams. A rust implementation similar to ent tool: https://www.fourmilab.ch/random/")]
pub struct Opt {
#[arg(long, short='i')]
info: bool,
#[arg(long, short='b')]
bits: bool,
#[arg(long, short='c')]
counts: bool,
#[arg(long, short='f')]
fold: bool,
#[arg(long, short='t')]
terse: bool,
#[arg(long, short='u')]
usage: bool,
file: Option<PathBuf>,
}
#[inline(always)]
fn from_reader<R: Read>(reader: &mut R) -> std::io::Result<EntestResult> {
let mut buf = [0u8; 8192];
let mut entest = Entest::new();
loop {
let len = reader.read(&mut buf)?;
if len == 0 {
break;
}
entest.update(&buf[..len]);
}
Ok(entest.finalize())
}
#[inline(always)]
fn from_stdin() -> std::io::Result<EntestResult> {
let stdin = std::io::stdin();
let mut stdin = std::io::BufReader::new(stdin.lock());
from_reader(&mut stdin)
}
#[inline(always)]
fn from_file<P: AsRef<Path>>(path: P) -> std::io::Result<EntestResult> {
let file = std::fs::File::open(path)?;
let mut file = std::io::BufReader::new(file);
from_reader(&mut file)
}
#[inline(always)]
fn result_main() -> std::io::Result<()> {
let opt = Opt::parse();
if opt.info {
eprintln!("(LOG_SQRT_PI = {})", entest::chisqr::LOG_SQRT_PI);
eprintln!("(I_SQRT_PI = {})", entest::chisqr::I_SQRT_PI);
}
if opt.usage {
Opt::command().print_help()?;
return Ok(());
}
let er =
if let Some(file) = opt.file {
from_file(file)?
} else {
from_stdin()?
};
println!("{er}");
Ok(())
}
#[inline(always)]
fn main() {
result_main().unwrap()
}