From 0d48744c0fbfe89526da8748b359791081c5cba2 Mon Sep 17 00:00:00 2001 From: Knight Date: Sat, 6 Aug 2016 11:47:09 +0800 Subject: [PATCH] base64: simplified --- src/base64/Cargo.toml | 2 - src/base64/base64.rs | 182 +++++++++++++++--------------------------- tests/test_base64.rs | 12 +-- 3 files changed, 72 insertions(+), 124 deletions(-) diff --git a/src/base64/Cargo.toml b/src/base64/Cargo.toml index 0759de865..0da38e783 100644 --- a/src/base64/Cargo.toml +++ b/src/base64/Cargo.toml @@ -9,8 +9,6 @@ path = "base64.rs" [dependencies] getopts = "*" -libc = "*" -rustc-serialize = "*" uucore = { path="../uucore" } [[bin]] diff --git a/src/base64/base64.rs b/src/base64/base64.rs index d08746381..85b8f7bfa 100644 --- a/src/base64/base64.rs +++ b/src/base64/base64.rs @@ -1,149 +1,96 @@ #![crate_name = "uu_base64"] -/* - * This file is part of the uutils coreutils package. - * - * (c) Jordy Dickinson - * - * For the full copyright and license information, please view the LICENSE file - * that was distributed with this source code. - */ +// This file is part of the uutils coreutils package. +// +// (c) Jordy Dickinson +// (c) Jian Zeng +// +// For the full copyright and license information, please view the LICENSE file +// that was distributed with this source code. +// -extern crate rustc_serialize as serialize; extern crate getopts; -extern crate libc; #[macro_use] extern crate uucore; +use uucore::encoding::{Data, Format, wrap_print}; use getopts::Options; -use serialize::base64::{self, FromBase64, ToBase64}; -use std::ascii::AsciiExt; -use std::error::Error; use std::fs::File; -use std::io::{BufReader, Read, stdin, stdout, Write}; +use std::io::{BufReader, Read, stdin, Write}; use std::path::Path; -enum Mode { - Decode, - Encode, - Help, - Version -} - static NAME: &'static str = "base64"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); -pub type FileOrStdReader = BufReader>; - pub fn uumain(args: Vec) -> i32 { let mut opts = Options::new(); opts.optflag("d", "decode", "decode data"); - opts.optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"); - opts.optopt("w", "wrap", "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", "COLS"); - opts.optflag("h", "help", "display this help text and exit"); - opts.optflag("V", "version", "output version information and exit"); + opts.optflag("i", + "ignore-garbage", + "when decoding, ignore non-alphabetic characters"); + opts.optopt("w", + "wrap", + "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", + "COLS"); + opts.optflag("", "help", "display this help text and exit"); + opts.optflag("", "version", "output version information and exit"); let matches = match opts.parse(&args[1..]) { Ok(m) => m, - Err(e) => { crash!(1, "{}", e) } + Err(e) => { + disp_err!("{}", e); + return 1; + } }; - let mode = if matches.opt_present("help") { - Mode::Help + if matches.opt_present("help") { + return help(&opts); } else if matches.opt_present("version") { - Mode::Version - } else if matches.opt_present("decode") { - Mode::Decode - } else { - Mode::Encode - }; - let ignore_garbage = matches.opt_present("ignore-garbage"); + return version(); + } + let line_wrap = match matches.opt_str("wrap") { - Some(s) => match s.parse() { - Ok(s) => s, - Err(e)=> { - crash!(1, "Argument to option 'wrap' improperly formatted: {}", e); + Some(s) => { + match s.parse() { + Ok(n) => n, + Err(e) => { + crash!(1, "invalid wrap size: ‘{}’: {}", s, e); + } } - }, - None => 76 - }; - let stdin_buf; - let file_buf; - let mut input = if matches.free.is_empty() || &matches.free[0][..] == "-" { - stdin_buf = stdin(); - BufReader::new(Box::new(stdin_buf) as Box) - } else { - let path = Path::new(&matches.free[0][..]); - file_buf = safe_unwrap!(File::open(&path)); - BufReader::new(Box::new(file_buf) as Box) + } + None => 76, }; - match mode { - Mode::Decode => decode(&mut input, ignore_garbage), - Mode::Encode => encode(&mut input, line_wrap), - Mode::Help => help(opts), - Mode::Version => version() + if matches.free.len() > 1 { + disp_err!("extra operand ‘{}’", matches.free[0]); + return 1; + } + + let input = if matches.free.is_empty() || &matches.free[0][..] == "-" { + BufReader::new(Box::new(stdin()) as Box) + } else { + let path = Path::new(matches.free[0].as_str()); + let file_buf = safe_unwrap!(File::open(&path)); + BufReader::new(Box::new(file_buf) as Box) + }; + + let mut data = Data::new(input, Format::Base64) + .line_wrap(line_wrap) + .ignore_garbage(matches.opt_present("ignore-garbage")); + + if !matches.opt_present("decode") { + wrap_print(line_wrap, data.encode()); + } else { + match data.decode() { + Ok(s) => print!("{}", String::from_utf8(s).unwrap()), + Err(_) => crash!(1, "invalid input"), + } } 0 } -fn decode(input: &mut FileOrStdReader, ignore_garbage: bool) { - let mut to_decode = String::new(); - input.read_to_string(&mut to_decode).unwrap(); - - if ignore_garbage { - let mut clean = String::new(); - clean.extend(to_decode.chars().filter(|&c| { - if !c.is_ascii() { - false - } else { - c >= 'a' && c <= 'z' || - c >= 'A' && c <= 'Z' || - c >= '0' && c <= '9' || - c == '+' || c == '/' - } - })); - to_decode = clean; - } - - match to_decode[..].from_base64() { - Ok(bytes) => { - let mut out = stdout(); - - match out.write_all(&bytes[..]) { - Ok(_) => {} - Err(f) => { crash!(1, "{}", f); } - } - match out.flush() { - Ok(_) => {} - Err(f) => { crash!(1, "{}", f); } - } - } - Err(s) => { - crash!(1, "{} ({:?})", s.description(), s); - } - } -} - -fn encode(input: &mut FileOrStdReader, line_wrap: usize) { - let b64_conf = base64::Config { - char_set: base64::Standard, - newline: base64::Newline::LF, - pad: true, - line_length: match line_wrap { - 0 => None, - _ => Some(line_wrap) - } - }; - let mut to_encode: Vec = vec!(); - input.read_to_end(&mut to_encode).unwrap(); - let encoded = to_encode.to_base64(b64_conf); - - println!("{}", &encoded[..]); -} - -fn help(opts: Options) { +fn help(opts: &Options) -> i32 { let msg = format!("Usage: {} [OPTION]... [FILE]\n\n\ Base64 encode or decode FILE, or standard input, to standard output.\n\ With no FILE, or when FILE is -, read standard input.\n\n\ @@ -151,11 +98,14 @@ fn help(opts: Options) { 3548. When\ndecoding, the input may contain newlines in addition \ to the bytes of the formal\nbase64 alphabet. Use --ignore-garbage \ to attempt to recover from any other\nnon-alphabet bytes in the \ - encoded stream.", NAME); + encoded stream.", + NAME); print!("{}", opts.usage(&msg)); + 0 } -fn version() { +fn version() -> i32 { println!("{} {}", NAME, VERSION); + 0 } diff --git a/tests/test_base64.rs b/tests/test_base64.rs index 1d314c361..3425923bb 100644 --- a/tests/test_base64.rs +++ b/tests/test_base64.rs @@ -33,7 +33,7 @@ fn test_garbage() { .arg("-d") .pipe_in(input) .fails() - .stderr_only("base64: error: invalid character (Invalid character '0' at position 20)\n"); + .stderr_only("base64: error: invalid input\n"); } #[test] @@ -68,9 +68,8 @@ fn test_wrap_no_arg() { new_ucmd() .arg(wrap_param) .fails() - .stderr_only( - format!("base64: error: Argument to option '{}' missing.", - if wrap_param == "-w" { "w" } else { "wrap" })); + .stderr_only(format!("base64: Argument to option '{}' missing.\nTry 'base64 --help' for more information.\n", + if wrap_param == "-w" { "w" } else { "wrap" })); } } @@ -78,8 +77,9 @@ fn test_wrap_no_arg() { fn test_wrap_bad_arg() { for wrap_param in vec!["-w", "--wrap"] { new_ucmd() - .arg(wrap_param).arg("b") + .arg(wrap_param) + .arg("b") .fails() - .stderr_only("base64: error: Argument to option 'wrap' improperly formatted: invalid digit found in string"); + .stderr_only("base64: error: invalid wrap size: ‘b’: invalid digit found in string\n"); } }