-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement md5sum (closes #47) #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "md5sum/rust-crypto"] | ||
path = md5sum/rust-crypto | ||
url = git://github.com/DaGenix/rust-crypto.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,6 @@ To do | |
- ls-vdir | ||
- ls | ||
- make-prime-list | ||
- md5sum | ||
- mkfifo | ||
- mknod | ||
- mktemp | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Binaries | ||
RUSTC ?= rustc | ||
RM := rm | ||
|
||
# Flags | ||
RUSTCFLAGS := --opt-level=3 | ||
RMFLAGS := |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
include ../common.mk | ||
|
||
all: ../build/md5sum | ||
|
||
CRYPTO_DIR := rust-crypto | ||
CRYPTO_LIB := $(CRYPTO_DIR)/$(shell $(RUSTC) --crate-file-name --crate-type rlib $(CRYPTO_DIR)/src/rust-crypto/lib.rs) | ||
|
||
../build/md5sum: md5sum.rs $(CRYPTO_LIB) | ||
$(RUSTC) $(RUSTFLAGS) -L $(CRYPTO_DIR) -o $@ $< | ||
|
||
$(CRYPTO_LIB): $(CRYPTO_DIR)/src/rust-crypto/*.rs | ||
cd $(CRYPTO_DIR) && make | ||
|
||
clean: | ||
cd $(CRYPTO_DIR) && make clean | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#[crate_id(name = "md5sum", vers = "1.0.0", author = "Arcterus")]; | ||
|
||
#[feature(macro_rules)]; | ||
|
||
extern crate crypto = "rust-crypto"; | ||
extern crate getopts; | ||
|
||
use std::io::fs::File; | ||
use std::io::BufferedReader; | ||
use std::os; | ||
use crypto::digest::Digest; | ||
|
||
#[path = "../common/util.rs"] | ||
mod util; | ||
|
||
static NAME: &'static str = "md5sum"; | ||
static VERSION: &'static str = "1.0.0"; | ||
|
||
fn main() { | ||
let args = os::args(); | ||
|
||
let program = args[0].clone(); | ||
|
||
let opts = [ | ||
getopts::optflag("b", "binary", "read in binary mode"), | ||
getopts::optflag("c", "check", "read MD5 sums from the FILEs and check them"), | ||
getopts::optflag("", "tag", "create a BSD-style checksum"), | ||
getopts::optflag("t", "text", "read in text mode (default)"), | ||
getopts::optflag("q", "quiet", "don't print OK for each successfully verified file"), | ||
getopts::optflag("s", "status", "don't output anything, status code shows success"), | ||
getopts::optflag("", "strict", "exit non-zero for improperly formatted checksum lines"), | ||
getopts::optflag("w", "warn", "warn about improperly formatted checksum lines"), | ||
getopts::optflag("h", "help", "display this help and exit"), | ||
getopts::optflag("V", "version", "output version information and exit") | ||
]; | ||
|
||
let matches = match getopts::getopts(args.tail(), opts) { | ||
Ok(m) => m, | ||
Err(f) => crash!(1, "{}", f.to_err_msg()) | ||
}; | ||
|
||
if matches.opt_present("help") { | ||
println!("{} v{}", NAME, VERSION); | ||
println!(""); | ||
println!("Usage:"); | ||
println!(" {} [OPTION]... [FILE]...", program); | ||
println!(""); | ||
print!("{}", getopts::usage("Compute and check MD5 message digests.", opts)); | ||
} else if matches.opt_present("version") { | ||
println!("{} v{}", NAME, VERSION); | ||
} else { | ||
let binary = matches.opt_present("binary"); | ||
let check = matches.opt_present("check"); | ||
let tag = matches.opt_present("tag"); | ||
let status = matches.opt_present("status"); | ||
let quiet = matches.opt_present("quiet") || status; | ||
let strict = matches.opt_present("strict"); | ||
let warn = matches.opt_present("warn") && !status; | ||
md5sum(matches.free, binary, check, tag, status, quiet, strict, warn); | ||
} | ||
} | ||
|
||
fn md5sum(files: Vec<~str>, binary: bool, check: bool, tag: bool, status: bool, quiet: bool, strict: bool, warn: bool) { | ||
let mut md5 = crypto::md5::Md5::new(); | ||
let bytes = md5.output_bits() / 4; | ||
let mut bad_format = 0; | ||
let mut failed = 0; | ||
for filename in files.iter() { | ||
let filename: &str = *filename; | ||
let mut file = safe_unwrap!(File::open(&Path::new(filename))); | ||
if check { | ||
let mut buffer = BufferedReader::new(file); | ||
for (i, line) in buffer.lines().enumerate() { | ||
let line = safe_unwrap!(line); | ||
let (ck_filename, sum) = match from_gnu(line, bytes) { | ||
Some(m) => m, | ||
None => match from_bsd(line, bytes) { | ||
Some(m) => m, | ||
None => { | ||
bad_format += 1; | ||
if strict { | ||
os::set_exit_status(1); | ||
} | ||
if warn { | ||
show_warning!("{}: {}: improperly formatted MD5 checksum line", filename, i + 1); | ||
} | ||
continue; | ||
} | ||
} | ||
}; | ||
let real_sum = calc_sum(&mut md5, &mut safe_unwrap!(File::open(&Path::new(ck_filename))), binary); | ||
if sum == real_sum { | ||
if !quiet { | ||
println!("{}: OK", ck_filename); | ||
} | ||
} else { | ||
if !status { | ||
println!("{}: FAILED", ck_filename); | ||
} | ||
failed += 1; | ||
os::set_exit_status(1); | ||
} | ||
} | ||
} else { | ||
let sum = calc_sum(&mut md5, &mut file, binary); | ||
if tag { | ||
println!("MD5 ({}) = {}", filename, sum); | ||
} else { | ||
println!("{} {}", sum, filename); | ||
} | ||
} | ||
} | ||
if !status { | ||
if bad_format == 1 { | ||
show_warning!("{} line is improperly formatted", bad_format); | ||
} else if bad_format > 1 { | ||
show_warning!("{} lines are improperly formatted", bad_format); | ||
} | ||
if failed > 0 { | ||
show_warning!("{} computed checksum did NOT match", failed); | ||
} | ||
} | ||
} | ||
|
||
fn calc_sum(md5: &mut crypto::md5::Md5, file: &mut File, binary: bool) -> ~str { | ||
let data = | ||
if binary { | ||
safe_unwrap!(file.read_to_end()) | ||
} else { | ||
let val = safe_unwrap!(file.read_to_str()).into_bytes(); // XXX: i don't know why the variable is necessary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is almost certainly rust-lang/rust#5941, you can work around it via (safe_unwrap!(file.read_to_str())).into_bytes() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you very much. Now it makes sense as to why that's happening. |
||
val | ||
}; | ||
md5.reset(); | ||
md5.input(data); | ||
md5.result_str() | ||
} | ||
|
||
fn from_gnu<'a>(line: &'a str, bytes: uint) -> Option<(&'a str, &'a str)> { | ||
let sum = line.slice_to(bytes); | ||
if sum.len() < bytes || line.slice(bytes, bytes + 2) != " " { | ||
None | ||
} else { | ||
Some((line.slice(bytes + 2, line.len() - 1), sum)) | ||
} | ||
} | ||
|
||
fn from_bsd<'a>(line: &'a str, bytes: uint) -> Option<(&'a str, &'a str)> { | ||
if line.slice(0, 5) == "MD5 (" { | ||
let rparen = match line.find(')') { | ||
Some(m) => m, | ||
None => return None | ||
}; | ||
if rparen > 5 && line.slice(rparen + 1, rparen + 4) == " = " && line.len() - 1 == rparen + 4 + bytes { | ||
return Some((line.slice(5, rparen), line.slice(rparen + 4, line.len() - 1))); | ||
} | ||
} | ||
None | ||
} |
Submodule rust-crypto
added at
fd168c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully we can get rid of all this once cargo is usable and rust-crypto can just be added as a dependency that way :)