coreutils/src/base64/base64.rs

180 lines
4.9 KiB
Rust
Raw Normal View History

2014-07-06 08:13:36 +00:00
#![crate_name = "base64"]
#![feature(box_syntax, collections, rustc_private)]
2013-12-20 19:34:45 +00:00
/*
* This file is part of the uutils coreutils package.
*
* (c) Jordy Dickinson <jordy.dickinson@gmail.com>
*
* 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;
2015-01-08 12:54:22 +00:00
#[macro_use] extern crate log;
2013-12-20 19:34:45 +00:00
2014-12-30 19:11:06 +00:00
use std::ascii::AsciiExt;
2015-01-10 00:16:05 +00:00
use std::error::Error;
use std::fs::File;
use std::io::{BufReader, Read, stdin, stdout, Write};
use std::path::Path;
2013-12-20 19:34:45 +00:00
use getopts::{
getopts,
optflag,
optopt,
usage
};
2014-02-15 04:50:03 +00:00
use serialize::base64;
use serialize::base64::{FromBase64, ToBase64};
2013-12-20 19:34:45 +00:00
2014-02-23 22:17:48 +00:00
#[path = "../common/util.rs"]
2015-01-08 12:54:22 +00:00
#[macro_use]
mod util;
static NAME: &'static str = "base64";
pub type FileOrStdReader = BufReader<Box<Read+'static>>;
pub fn uumain(args: Vec<String>) -> i32 {
2014-05-30 08:35:54 +00:00
let opts = [
optflag("d", "decode", "decode data"),
optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"),
optopt("w", "wrap",
"wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", "COLS"
),
optflag("h", "help", "display this help text and exit"),
optflag("V", "version", "output version information and exit")
];
2014-11-19 20:55:25 +00:00
let matches = match getopts(args.tail(), &opts) {
Ok(m) => m,
Err(e) => {
2015-01-10 00:16:05 +00:00
crash!(1, "error: {}", e);
}
};
2014-07-20 01:13:55 +00:00
let progname = args[0].clone();
2014-11-19 20:55:25 +00:00
let usage = usage("Base64 encode or decode FILE, or standard input, to standard output.", &opts);
let mode = if matches.opt_present("help") {
2014-11-19 20:50:37 +00:00
Mode::Help
} else if matches.opt_present("version") {
2014-11-19 20:50:37 +00:00
Mode::Version
} else if matches.opt_present("decode") {
2014-11-19 20:50:37 +00:00
Mode::Decode
} else {
2014-11-19 20:50:37 +00:00
Mode::Encode
};
let ignore_garbage = matches.opt_present("ignore-garbage");
let line_wrap = match matches.opt_str("wrap") {
Some(s) => match s.parse() {
Ok(s) => s,
Err(e)=> {
crash!(1, "error: Argument to option 'wrap' improperly formatted: {}", e);
}
},
None => 76
};
let mut stdin_buf;
let mut file_buf;
let mut input = if matches.free.is_empty() || &matches.free[0][..] == "-" {
stdin_buf = stdin();
BufReader::new(box stdin_buf as Box<Read+'static>)
} else {
let path = Path::new(&matches.free[0][..]);
file_buf = safe_unwrap!(File::open(&path));
BufReader::new(box file_buf as Box<Read+'static>)
};
2013-12-20 19:34:45 +00:00
match mode {
Mode::Decode => decode(&mut input, ignore_garbage),
Mode::Encode => encode(&mut input, line_wrap),
Mode::Help => help(&progname[..], &usage[..]),
2014-11-19 20:50:37 +00:00
Mode::Version => version()
2013-12-20 19:34:45 +00:00
}
0
2013-12-20 19:34:45 +00:00
}
fn decode(input: &mut FileOrStdReader, ignore_garbage: bool) {
let mut to_decode = String::new();
input.read_to_string(&mut to_decode).unwrap();
2013-12-20 19:34:45 +00:00
2015-01-09 23:13:16 +00:00
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() {
2013-12-20 19:34:45 +00:00
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); }
}
2013-12-20 19:34:45 +00:00
}
2013-12-20 21:15:27 +00:00
Err(s) => {
2015-01-24 02:56:37 +00:00
crash!(1, "error: {} ({:?})", s.description(), s);
2013-12-20 21:15:27 +00:00
}
2013-12-20 19:34:45 +00:00
}
}
fn encode(input: &mut FileOrStdReader, line_wrap: usize) {
2013-12-20 19:34:45 +00:00
let b64_conf = base64::Config {
char_set: base64::Standard,
newline: base64::Newline::LF,
2013-12-20 19:34:45 +00:00
pad: true,
line_length: match line_wrap {
2013-12-20 19:34:45 +00:00
0 => None,
_ => Some(line_wrap)
2013-12-20 19:34:45 +00:00
}
};
let mut to_encode: Vec<u8> = vec!();
input.read_to_end(&mut to_encode).unwrap();
let encoded = to_encode.to_base64(b64_conf);
2013-12-20 19:34:45 +00:00
println!("{}", &encoded[..]);
2013-12-20 19:34:45 +00:00
}
fn help(progname: &str, usage: &str) {
2014-11-21 09:09:43 +00:00
println!("Usage: {} [OPTION]... [FILE]", progname);
println!("");
println!("{}", usage);
2013-12-20 19:34:45 +00:00
2014-05-07 06:25:49 +00:00
let msg = "With no FILE, or when FILE is -, read standard input.\n\n\
The data are encoded as described for the base64 alphabet in RFC \
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.";
2013-12-20 19:34:45 +00:00
println!("{}", msg);
2013-12-20 19:34:45 +00:00
}
fn version() {
println!("base64 1.0.0");
2013-12-20 19:34:45 +00:00
}
enum Mode {
Decode,
Encode,
Help,
Version
}