2015-12-08 02:42:08 +00:00
|
|
|
#![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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern crate getopts;
|
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2015-05-21 02:45:43 +00:00
|
|
|
use getopts::Options;
|
2015-04-29 20:54:54 +00:00
|
|
|
use std::fs::File;
|
2015-05-21 02:45:43 +00:00
|
|
|
use std::io::{self, stdin, Read, Write, BufReader};
|
2014-07-11 06:38:18 +00:00
|
|
|
use std::mem;
|
2015-05-21 02:45:43 +00:00
|
|
|
use std::path::Path;
|
2014-07-11 06:38:18 +00:00
|
|
|
|
|
|
|
use crc_table::CRC_TABLE;
|
2014-05-16 21:01:20 +00:00
|
|
|
|
2014-07-11 06:38:18 +00:00
|
|
|
mod crc_table;
|
2014-05-16 21:01:20 +00:00
|
|
|
|
2014-07-11 06:38:18 +00:00
|
|
|
static NAME: &'static str = "cksum";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-05-16 21:01:20 +00:00
|
|
|
|
2014-07-11 06:38:18 +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
|
|
|
}
|
|
|
|
|
2014-07-11 06:38:18 +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
|
|
|
|
}
|
|
|
|
|
2014-07-11 18:09:43 +00:00
|
|
|
#[inline]
|
2015-04-29 20:54:54 +00:00
|
|
|
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
|
|
|
|
2015-04-29 20:54:54 +00:00
|
|
|
let file;
|
|
|
|
let mut rd : Box<Read> = match fname {
|
2014-07-11 18:09:43 +00:00
|
|
|
"-" => {
|
2015-04-29 20:54:54 +00:00
|
|
|
Box::new(stdin())
|
2014-07-11 18:09:43 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2015-04-29 20:54:54 +00:00
|
|
|
file = try!(File::open(&Path::new(fname)));
|
|
|
|
Box::new(BufReader::new(file))
|
2014-07-11 18:09:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-08 12:58:23 +00:00
|
|
|
let mut bytes: [u8; 1024 * 1024] = unsafe { mem::uninitialized() };
|
2014-05-16 21:01:20 +00:00
|
|
|
loop {
|
2014-11-19 20:55:25 +00:00
|
|
|
match rd.read(&mut bytes) {
|
2014-07-11 06:38:18 +00:00
|
|
|
Ok(num_bytes) => {
|
2015-04-29 20:54:54 +00:00
|
|
|
if num_bytes == 0 {
|
|
|
|
return Ok((crc_final(crc, size), size));
|
|
|
|
}
|
2015-01-24 09:44:48 +00:00
|
|
|
for &b in bytes[..num_bytes].iter() {
|
2014-07-11 06:38:18 +00:00
|
|
|
crc = crc_update(crc, b);
|
|
|
|
}
|
|
|
|
size += num_bytes;
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
2014-07-11 18:09:43 +00:00
|
|
|
Err(err) => return Err(err)
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:48:07 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> i32 {
|
2015-05-21 02:45:43 +00:00
|
|
|
let mut opts = Options::new();
|
|
|
|
opts.optflag("h", "help", "display this help and exit");
|
|
|
|
opts.optflag("V", "version", "output version information and exit");
|
2014-05-16 21:01:20 +00:00
|
|
|
|
2015-05-21 02:45:43 +00:00
|
|
|
let matches = match opts.parse(&args[1..]) {
|
2014-05-16 21:01:20 +00:00
|
|
|
Ok(m) => m,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(err) => panic!("{}", err),
|
2014-05-16 21:01:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("help") {
|
2015-05-21 02:45:43 +00:00
|
|
|
let msg = format!("{0} {1}
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
{0} [OPTIONS] [FILE]...
|
|
|
|
|
|
|
|
Print CRC and size for each file.", NAME, VERSION);
|
|
|
|
|
|
|
|
print!("{}", opts.usage(&msg));
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if matches.opt_present("version") {
|
|
|
|
println!("{} {}", NAME, VERSION);
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let files = matches.free;
|
|
|
|
|
|
|
|
if files.is_empty() {
|
|
|
|
match cksum("-") {
|
|
|
|
Ok((crc, size)) => println!("{} {}", crc, size),
|
2014-06-09 01:49:06 +00:00
|
|
|
Err(err) => {
|
2014-06-09 03:26:51 +00:00
|
|
|
show_error!("{}", err);
|
2014-06-09 01:49:06 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
return 0;
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 05:30:54 +00:00
|
|
|
let mut exit_code = 0;
|
2014-05-16 21:01:20 +00:00
|
|
|
for fname in files.iter() {
|
2015-04-29 20:54:54 +00:00
|
|
|
match cksum(fname.as_ref()) {
|
2014-05-16 21:01:20 +00:00
|
|
|
Ok((crc, size)) => println!("{} {} {}", crc, size, fname),
|
2014-06-09 01:49:06 +00:00
|
|
|
Err(err) => {
|
2014-06-09 03:26:51 +00:00
|
|
|
show_error!("'{}' {}", fname, err);
|
2014-06-10 05:30:54 +00:00
|
|
|
exit_code = 2;
|
2014-06-09 01:49:06 +00:00
|
|
|
}
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
exit_code
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|