Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Created October 22, 2019 07:55
Show Gist options
  • Save nanpuyue/3082c5eefd3bbadeac7841cd77f23cc9 to your computer and use it in GitHub Desktop.
Save nanpuyue/3082c5eefd3bbadeac7841cd77f23cc9 to your computer and use it in GitHub Desktop.
use std::env;
use std::fs::File;
use std::io::{stdin, Read};
use crc_all::CrcAlgo;
use lazy_static::lazy_static;
const BUFFER_SIZE: usize = 1024 * 16;
fn crc32sum<R: Read>(r: &mut R) -> u32 {
lazy_static! {
static ref CRC32: CrcAlgo<u32> =
CrcAlgo::<u32>::new(0x04c11db7, 32, 0xffffffff, 0xffffffff, true);
}
let crc = &mut 0;
CRC32.init_crc(crc);
let mut buf = Vec::with_capacity(BUFFER_SIZE);
unsafe {
buf.set_len(BUFFER_SIZE);
}
let mut n;
while {
n = r.read(buf.as_mut()).unwrap();
n > 0
} {
CRC32.update_crc(crc, &buf[..n]);
}
CRC32.finish_crc(crc)
}
fn main() {
let args = env::args();
if args.len() > 1 {
for path in args.skip(1) {
println!(
"{:08x} {}",
crc32sum(&mut File::open(&path).unwrap()),
&path
);
}
} else {
println!("{:08x} {}", crc32sum(&mut stdin().lock()), "-");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment