2
0
Fork 0
mirror of https://github.com/uutils/coreutils synced 2024-12-18 17:14:42 +00:00
coreutils/src/cksum/cksum.rs

113 lines
2.6 KiB
Rust
Raw Normal View History

#![crate_name = "uu_cksum"]
2014-05-16 21:01:20 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) Michael Gehring <mg@ebfe.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#[macro_use]
extern crate uucore;
use std::fs::File;
2018-03-12 08:20:58 +00:00
use std::io::{self, stdin, BufReader, Read};
#[cfg(not(windows))]
use std::mem;
use std::path::Path;
include!(concat!(env!("OUT_DIR"), "/crc_table.rs"));
2014-05-16 21:01:20 +00:00
2017-12-11 04:57:39 +00:00
static SYNTAX: &'static str = "[OPTIONS] [FILE]...";
static SUMMARY: &'static str = "Print CRC and size for each file";
static LONG_HELP: &'static str = "";
2014-05-16 21:01:20 +00:00
#[inline]
fn crc_update(crc: u32, input: u8) -> u32 {
2015-01-10 00:51:51 +00:00
(crc << 8) ^ CRC_TABLE[((crc >> 24) as usize ^ input as usize) & 0xFF]
2014-05-16 21:01:20 +00:00
}
#[inline]
2015-01-10 00:51:51 +00:00
fn crc_final(mut crc: u32, mut length: usize) -> u32 {
2014-05-16 21:01:20 +00:00
while length != 0 {
crc = crc_update(crc, length as u8);
length >>= 8;
}
!crc
}
#[cfg(windows)]
fn init_byte_array() -> Vec<u8> {
vec![0; 1024 * 1024]
}
#[cfg(not(windows))]
2018-03-12 08:20:58 +00:00
fn init_byte_array() -> [u8; 1024 * 1024] {
unsafe { mem::uninitialized() }
}
2014-07-11 18:09:43 +00:00
#[inline]
fn cksum(fname: &str) -> io::Result<(u32, usize)> {
2014-05-16 21:01:20 +00:00
let mut crc = 0u32;
2015-02-22 12:01:40 +00:00
let mut size = 0usize;
2014-05-16 21:01:20 +00:00
let file;
2018-03-12 08:20:58 +00:00
let mut rd: Box<Read> = match fname {
"-" => Box::new(stdin()),
2014-07-11 18:09:43 +00:00
_ => {
file = try!(File::open(&Path::new(fname)));
Box::new(BufReader::new(file))
2014-07-11 18:09:43 +00:00
}
};
let mut bytes = init_byte_array();
2014-05-16 21:01:20 +00:00
loop {
2014-11-19 20:55:25 +00:00
match rd.read(&mut bytes) {
Ok(num_bytes) => {
if num_bytes == 0 {
return Ok((crc_final(crc, size), size));
}
for &b in bytes[..num_bytes].iter() {
crc = crc_update(crc, b);
}
size += num_bytes;
2014-05-16 21:01:20 +00:00
}
2018-03-12 08:20:58 +00:00
Err(err) => return Err(err),
2014-05-16 21:01:20 +00:00
}
}
//Ok((0 as u32,0 as usize))
2014-05-16 21:01:20 +00:00
}
pub fn uumain(args: Vec<String>) -> i32 {
2018-03-12 08:20:58 +00:00
let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args);
2014-05-16 21:01:20 +00:00
let files = matches.free;
if files.is_empty() {
match cksum("-") {
Ok((crc, size)) => println!("{} {}", crc, size),
Err(err) => {
2014-06-09 03:26:51 +00:00
show_error!("{}", err);
return 2;
}
2014-05-16 21:01:20 +00:00
}
return 0;
2014-05-16 21:01:20 +00:00
}
let mut exit_code = 0;
2016-01-05 19:42:52 +00:00
for fname in &files {
match cksum(fname.as_ref()) {
2014-05-16 21:01:20 +00:00
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
Err(err) => {
2014-06-09 03:26:51 +00:00
show_error!("'{}' {}", fname, err);
exit_code = 2;
}
2014-05-16 21:01:20 +00:00
}
}
exit_code
2014-05-16 21:01:20 +00:00
}