2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "cksum"]
|
2014-05-16 21:01:20 +00:00
|
|
|
#![feature(macro_rules)]
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2014-07-11 06:38:18 +00:00
|
|
|
use std::io::{EndOfFile, File, IoError, IoResult, print};
|
|
|
|
use std::io::stdio::stdin_raw;
|
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
use crc_table::CRC_TABLE;
|
2014-05-16 21:01:20 +00:00
|
|
|
|
|
|
|
#[path="../common/util.rs"]
|
|
|
|
mod util;
|
|
|
|
|
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";
|
|
|
|
static VERSION: &'static str = "1.0.0";
|
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 {
|
|
|
|
(crc << 8) ^ CRC_TABLE[((crc >> 24) as uint ^ input as uint) & 0xFF]
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 06:38:18 +00:00
|
|
|
#[inline]
|
2014-05-16 21:01:20 +00:00
|
|
|
fn crc_final(mut crc: u32, mut length: uint) -> u32 {
|
|
|
|
while length != 0 {
|
|
|
|
crc = crc_update(crc, length as u8);
|
|
|
|
length >>= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
!crc
|
|
|
|
}
|
|
|
|
|
2014-07-11 18:09:43 +00:00
|
|
|
#[inline]
|
2014-05-16 21:01:20 +00:00
|
|
|
fn cksum(fname: &str) -> IoResult<(u32, uint)> {
|
|
|
|
let mut crc = 0u32;
|
|
|
|
let mut size = 0u;
|
|
|
|
|
2014-07-11 18:09:43 +00:00
|
|
|
let mut stdin_buf;
|
|
|
|
let mut file_buf;
|
|
|
|
let rd = match fname {
|
|
|
|
"-" => {
|
|
|
|
stdin_buf = stdin_raw();
|
|
|
|
&mut stdin_buf as &mut Reader
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
file_buf = try!(File::open(&Path::new(fname)));
|
|
|
|
&mut file_buf as &mut Reader
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-11 06:38:18 +00:00
|
|
|
let mut bytes: [u8, ..1024 * 1024] = unsafe { mem::uninitialized() };
|
2014-05-16 21:01:20 +00:00
|
|
|
loop {
|
2014-07-11 06:38:18 +00:00
|
|
|
match rd.read(bytes) {
|
|
|
|
Ok(num_bytes) => {
|
2014-07-11 18:09:43 +00:00
|
|
|
for &b in bytes.slice_to(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(IoError { kind: EndOfFile, .. }) => return Ok((crc_final(crc, size), size)),
|
|
|
|
Err(err) => return Err(err)
|
2014-05-16 21:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> int {
|
2014-05-16 21:01:20 +00:00
|
|
|
let opts = [
|
|
|
|
getopts::optflag("h", "help", "display this help and exit"),
|
|
|
|
getopts::optflag("V", "version", "output version information and exit"),
|
|
|
|
];
|
|
|
|
|
2014-05-17 10:32:14 +00:00
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
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") {
|
|
|
|
println!("{} {}", NAME, VERSION);
|
|
|
|
println!("");
|
|
|
|
println!("Usage:");
|
|
|
|
println!(" {} [OPTIONS] [FILE]...", NAME);
|
|
|
|
println!("");
|
2014-05-17 10:32:14 +00:00
|
|
|
print(getopts::usage("Print CRC and size for each file.", opts.as_slice()).as_slice());
|
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() {
|
2014-05-17 10:32:14 +00:00
|
|
|
match cksum(fname.as_slice()) {
|
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
|
|
|
}
|