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.
|
|
|
|
*/
|
|
|
|
|
2014-03-31 16:40:21 +00:00
|
|
|
#![feature(phase)]
|
|
|
|
#![feature(macro_rules)]
|
2014-02-07 06:39:07 +00:00
|
|
|
|
2014-02-16 21:29:31 +00:00
|
|
|
extern crate serialize;
|
|
|
|
extern crate getopts;
|
2014-04-07 22:43:34 +00:00
|
|
|
extern crate libc;
|
2014-06-12 04:43:47 +00:00
|
|
|
#[phase(plugin, link)] extern crate log;
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2014-07-12 03:39:15 +00:00
|
|
|
use std::io::{println, File, stdout};
|
|
|
|
use std::io::stdio::stdin_raw;
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2014-02-07 06:39:07 +00:00
|
|
|
use getopts::{
|
2013-12-27 16:27:41 +00:00
|
|
|
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"]
|
2014-02-07 06:39:07 +00:00
|
|
|
mod util;
|
|
|
|
|
|
|
|
static NAME: &'static str = "base64";
|
|
|
|
|
2014-06-08 07:56:37 +00:00
|
|
|
pub fn uumain(args: Vec<String>) -> int {
|
2014-05-30 08:35:54 +00:00
|
|
|
let opts = [
|
2013-12-27 16:27:41 +00:00
|
|
|
optflag("d", "decode", "decode data"),
|
2014-01-07 00:35:24 +00:00
|
|
|
optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"),
|
2013-12-27 16:27:41 +00:00
|
|
|
optopt("w", "wrap",
|
2014-01-07 00:35:24 +00:00
|
|
|
"wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", "COLS"
|
|
|
|
),
|
2013-12-27 16:27:41 +00:00
|
|
|
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) {
|
2013-12-27 16:27:41 +00:00
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => {
|
2014-06-15 10:50:40 +00:00
|
|
|
error!("error: {}", e);
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2013-12-27 16:27:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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);
|
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") {
|
2014-05-17 10:32:14 +00:00
|
|
|
Some(s) => match from_str(s.as_slice()) {
|
2013-12-27 16:27:41 +00:00
|
|
|
Some(s) => s,
|
|
|
|
None => {
|
2014-11-21 09:09:43 +00:00
|
|
|
error!("error: {}", "Argument to option 'wrap' improperly formatted.");
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2013-12-27 16:27:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
None => 76
|
|
|
|
};
|
2014-07-12 03:39:15 +00:00
|
|
|
let mut stdin_buf;
|
|
|
|
let mut file_buf;
|
2014-07-20 01:13:55 +00:00
|
|
|
let input = if matches.free.is_empty() || matches.free[0].as_slice() == "-" {
|
2014-07-12 03:39:15 +00:00
|
|
|
stdin_buf = stdin_raw();
|
|
|
|
&mut stdin_buf as &mut Reader
|
2013-12-27 16:27:41 +00:00
|
|
|
} else {
|
2014-07-20 01:13:55 +00:00
|
|
|
let path = Path::new(matches.free[0].as_slice());
|
2014-07-12 03:39:15 +00:00
|
|
|
file_buf = File::open(&path);
|
|
|
|
&mut file_buf as &mut Reader
|
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 {
|
2014-11-19 20:50:37 +00:00
|
|
|
Mode::Decode => decode(input, ignore_garbage),
|
|
|
|
Mode::Encode => encode(input, line_wrap),
|
|
|
|
Mode::Help => help(progname.as_slice(), usage.as_slice()),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
fn decode(input: &mut Reader, ignore_garbage: bool) {
|
2014-07-09 08:29:50 +00:00
|
|
|
let mut to_decode = match input.read_to_string() {
|
2014-02-05 03:39:17 +00:00
|
|
|
Ok(m) => m,
|
2014-10-30 09:06:47 +00:00
|
|
|
Err(f) => panic!(f)
|
2014-04-25 17:38:09 +00:00
|
|
|
};
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2014-07-12 03:39:15 +00:00
|
|
|
let slice =
|
|
|
|
if ignore_garbage {
|
|
|
|
to_decode.as_slice()
|
|
|
|
.trim_chars(|c: char| {
|
|
|
|
let num = match c.to_ascii_opt() {
|
2014-11-28 22:49:22 +00:00
|
|
|
Some(ascii) => ascii.as_byte(),
|
2014-07-12 03:39:15 +00:00
|
|
|
None => return false
|
|
|
|
};
|
|
|
|
!(num >= 'a' as u8 && num <= 'z' as u8 ||
|
|
|
|
num >= 'A' as u8 && num <= 'Z' as u8 ||
|
|
|
|
num >= '0' as u8 && num <= '9' as u8 ||
|
|
|
|
num == '+' as u8 || num == '/' as u8)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
to_decode = to_decode.as_slice().replace("\n", "");
|
|
|
|
to_decode.as_slice()
|
|
|
|
};
|
|
|
|
|
|
|
|
match slice.from_base64() {
|
2013-12-20 19:34:45 +00:00
|
|
|
Ok(bytes) => {
|
|
|
|
let mut out = stdout();
|
|
|
|
|
2014-05-16 08:34:02 +00:00
|
|
|
match out.write(bytes.as_slice()) {
|
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) => {
|
2014-07-12 03:39:15 +00:00
|
|
|
error!("error: {}", s);
|
2014-10-30 09:06:47 +00:00
|
|
|
panic!()
|
2013-12-20 21:15:27 +00:00
|
|
|
}
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
fn encode(input: &mut Reader, line_wrap: uint) {
|
2013-12-20 19:34:45 +00:00
|
|
|
let b64_conf = base64::Config {
|
|
|
|
char_set: base64::Standard,
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2014-02-05 03:39:17 +00:00
|
|
|
let to_encode = match input.read_to_end() {
|
|
|
|
Ok(m) => m,
|
2014-07-12 03:39:15 +00:00
|
|
|
Err(err) => crash!(1, "{}", err)
|
2014-02-05 03:39:17 +00:00
|
|
|
};
|
2014-05-17 10:32:14 +00:00
|
|
|
let encoded = to_encode.as_slice().to_base64(b64_conf);
|
2013-12-20 19:34:45 +00:00
|
|
|
|
|
|
|
// To my knowledge, RFC 3548 does not specify which line endings to use. It
|
|
|
|
// seems that rust's base64 algorithm uses CRLF as prescribed by RFC 2045.
|
|
|
|
// However, since GNU base64 outputs only LF (presumably because that is
|
|
|
|
// the standard UNIX line ending), we strip CRs from the output to maintain
|
|
|
|
// compatibility.
|
2014-10-10 16:36:15 +00:00
|
|
|
let output = encoded.replace("\r", "");
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2014-10-10 16:36:15 +00:00
|
|
|
println(output.as_slice());
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
fn help(progname: &str, usage: &str) {
|
2014-11-21 09:09:43 +00:00
|
|
|
println!("Usage: {} [OPTION]... [FILE]", progname);
|
2014-01-13 09:55:04 +00:00
|
|
|
println!("");
|
2013-12-27 16:27:41 +00:00
|
|
|
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\
|
2013-12-21 03:27:49 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn version() {
|
2014-01-13 09:55:04 +00:00
|
|
|
println!("base64 1.0.0");
|
2013-12-20 19:34:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Mode {
|
|
|
|
Decode,
|
|
|
|
Encode,
|
|
|
|
Help,
|
|
|
|
Version
|
|
|
|
}
|