2014-07-06 08:13:36 +00:00
|
|
|
#![crate_name = "base64"]
|
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.
|
|
|
|
*/
|
|
|
|
|
2015-04-25 22:22:56 +00:00
|
|
|
extern crate rustc_serialize as serialize;
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2015-11-24 01:00:51 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate uucore;
|
|
|
|
|
2015-05-21 02:45:43 +00:00
|
|
|
use getopts::Options;
|
|
|
|
use serialize::base64::{self, FromBase64, ToBase64};
|
2014-12-30 19:11:06 +00:00
|
|
|
use std::ascii::AsciiExt;
|
2015-01-10 00:16:05 +00:00
|
|
|
use std::error::Error;
|
2015-04-23 05:03:35 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{BufReader, Read, stdin, stdout, Write};
|
|
|
|
use std::path::Path;
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2015-05-21 02:45:43 +00:00
|
|
|
enum Mode {
|
|
|
|
Decode,
|
|
|
|
Encode,
|
|
|
|
Help,
|
|
|
|
Version
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
static NAME: &'static str = "base64";
|
2015-11-25 09:52:10 +00:00
|
|
|
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
2014-02-07 06:39:07 +00:00
|
|
|
|
2015-04-23 05:03:35 +00:00
|
|
|
pub type FileOrStdReader = BufReader<Box<Read+'static>>;
|
|
|
|
|
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("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");
|
|
|
|
let matches = match opts.parse(&args[1..]) {
|
2013-12-27 16:27:41 +00:00
|
|
|
Ok(m) => m,
|
2015-05-29 03:23:54 +00:00
|
|
|
Err(e) => { crash!(1, "{}", e) }
|
2013-12-27 16:27:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mode = if matches.opt_present("help") {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Help
|
2013-12-27 16:27:41 +00:00
|
|
|
} else if matches.opt_present("version") {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Version
|
2013-12-27 16:27:41 +00:00
|
|
|
} else if matches.opt_present("decode") {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Decode
|
2013-12-27 16:27:41 +00:00
|
|
|
} else {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Encode
|
2013-12-27 16:27:41 +00:00
|
|
|
};
|
|
|
|
let ignore_garbage = matches.opt_present("ignore-garbage");
|
|
|
|
let line_wrap = match matches.opt_str("wrap") {
|
2015-01-01 11:14:28 +00:00
|
|
|
Some(s) => match s.parse() {
|
2015-02-03 21:19:13 +00:00
|
|
|
Ok(s) => s,
|
|
|
|
Err(e)=> {
|
2015-05-29 03:23:54 +00:00
|
|
|
crash!(1, "Argument to option 'wrap' improperly formatted: {}", e);
|
2013-12-27 16:27:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
None => 76
|
|
|
|
};
|
2015-07-09 02:24:02 +00:00
|
|
|
let stdin_buf;
|
|
|
|
let file_buf;
|
2015-04-23 05:03:35 +00:00
|
|
|
let mut input = if matches.free.is_empty() || &matches.free[0][..] == "-" {
|
|
|
|
stdin_buf = stdin();
|
2015-04-28 21:51:13 +00:00
|
|
|
BufReader::new(Box::new(stdin_buf) as Box<Read+'static>)
|
2013-12-27 16:27:41 +00:00
|
|
|
} else {
|
2015-04-23 05:03:35 +00:00
|
|
|
let path = Path::new(&matches.free[0][..]);
|
|
|
|
file_buf = safe_unwrap!(File::open(&path));
|
2015-04-28 21:51:13 +00:00
|
|
|
BufReader::new(Box::new(file_buf) as Box<Read+'static>)
|
2013-12-27 16:27:41 +00:00
|
|
|
};
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
match mode {
|
2015-04-23 05:03:35 +00:00
|
|
|
Mode::Decode => decode(&mut input, ignore_garbage),
|
|
|
|
Mode::Encode => encode(&mut input, line_wrap),
|
2015-05-21 02:45:43 +00:00
|
|
|
Mode::Help => help(opts),
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Version => version()
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
2014-06-08 07:56:37 +00:00
|
|
|
|
2014-06-12 04:41:53 +00:00
|
|
|
0
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 05:03:35 +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;
|
|
|
|
}
|
2014-07-12 03:39:15 +00:00
|
|
|
|
2015-04-23 05:03:35 +00:00
|
|
|
match to_decode[..].from_base64() {
|
2013-12-20 19:34:45 +00:00
|
|
|
Ok(bytes) => {
|
|
|
|
let mut out = stdout();
|
|
|
|
|
2015-04-23 05:03:35 +00:00
|
|
|
match out.write_all(&bytes[..]) {
|
2014-02-07 06:39:07 +00:00
|
|
|
Ok(_) => {}
|
2014-07-12 03:39:15 +00:00
|
|
|
Err(f) => { crash!(1, "{}", f); }
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
|
|
|
match out.flush() {
|
|
|
|
Ok(_) => {}
|
2014-07-12 03:39:15 +00:00
|
|
|
Err(f) => { crash!(1, "{}", f); }
|
2014-02-07 06:39:07 +00:00
|
|
|
}
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
2013-12-20 21:15:27 +00:00
|
|
|
Err(s) => {
|
2015-05-29 03:23:54 +00:00
|
|
|
crash!(1, "{} ({:?})", s.description(), s);
|
2013-12-20 21:15:27 +00:00
|
|
|
}
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 05:03:35 +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,
|
2014-12-06 10:57:08 +00:00
|
|
|
newline: base64::Newline::LF,
|
2013-12-20 19:34:45 +00:00
|
|
|
pad: true,
|
2013-12-27 16:27:41 +00:00
|
|
|
line_length: match line_wrap {
|
2013-12-20 19:34:45 +00:00
|
|
|
0 => None,
|
2013-12-27 16:27:41 +00:00
|
|
|
_ => Some(line_wrap)
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
};
|
2015-04-23 05:03:35 +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
|
|
|
|
2015-04-23 05:03:35 +00:00
|
|
|
println!("{}", &encoded[..]);
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 02:45:43 +00:00
|
|
|
fn help(opts: Options) {
|
|
|
|
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\
|
|
|
|
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.", NAME);
|
|
|
|
|
|
|
|
print!("{}", opts.usage(&msg));
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn version() {
|
2015-05-21 02:45:43 +00:00
|
|
|
println!("{} {}", NAME, VERSION);
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
2015-11-22 20:45:04 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn main() {
|
|
|
|
std::process::exit(uumain(std::env::args().collect()));
|
|
|
|
}
|