use std::io::{BufReader, Read}; use std::fs::File; use std::env; fn args() -> Vec<String> { if env::args().count() < 2 { vec!["/dev/stdin".to_owned()] } else { env::args().skip(1).collect() } } fn analyse(name: &str) { let mut acc = [0u64; 0x100]; let mut size = 0u64; for b in BufReader::new(File::open(name).expect(name)).bytes() { acc[b.expect("byte") as usize] += 1; size += 1; } let mut entropy = 0.0; let invsize = 1.0 / size as f64; for count in acc.iter() { if *count > 0 { let p = *count as f64 * invsize; entropy -= p * p.log(256.0); } } println!("{}\t{}", entropy, name); } fn main() { for file in args() { analyse(&file); } }