2013-12-27 22:01:55 +00:00
|
|
|
#[crate_id(name="base64", vers="1.0.0", author="Jordy Dickinson")];
|
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 mod extra;
|
|
|
|
|
|
|
|
use std::char;
|
2014-01-13 09:55:04 +00:00
|
|
|
use std::io::{println, File, stdin, stdout};
|
2013-12-20 19:34:45 +00:00
|
|
|
use std::os;
|
|
|
|
use std::str;
|
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
use extra::getopts::groups::{
|
|
|
|
getopts,
|
|
|
|
optflag,
|
|
|
|
optopt,
|
|
|
|
usage
|
|
|
|
};
|
2013-12-20 19:34:45 +00:00
|
|
|
use extra::base64;
|
|
|
|
use extra::base64::{FromBase64, ToBase64};
|
|
|
|
|
|
|
|
fn main() {
|
2013-12-27 16:27:41 +00:00
|
|
|
let args = ~os::args();
|
|
|
|
let opts = ~[
|
|
|
|
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")
|
|
|
|
];
|
|
|
|
let matches = match getopts(args.tail(), opts) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => {
|
|
|
|
error!("error: {:s}", e.to_err_msg());
|
|
|
|
fail!()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let progname = args[0].clone();
|
2014-01-07 00:35:24 +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") {
|
|
|
|
Help
|
|
|
|
} else if matches.opt_present("version") {
|
|
|
|
Version
|
|
|
|
} else if matches.opt_present("decode") {
|
|
|
|
Decode
|
|
|
|
} else {
|
|
|
|
Encode
|
|
|
|
};
|
|
|
|
let ignore_garbage = matches.opt_present("ignore-garbage");
|
|
|
|
let line_wrap = match matches.opt_str("wrap") {
|
|
|
|
Some(s) => match from_str(s) {
|
|
|
|
Some(s) => s,
|
|
|
|
None => {
|
2014-01-07 00:35:24 +00:00
|
|
|
error!("error: {:s}", "Argument to option 'wrap' improperly formatted.");
|
2013-12-27 16:27:41 +00:00
|
|
|
fail!()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => 76
|
|
|
|
};
|
|
|
|
let mut input = if matches.free.is_empty() || matches.free[0] == ~"-" {
|
|
|
|
~stdin() as ~Reader
|
|
|
|
} else {
|
|
|
|
let path = Path::new(matches.free[0]);
|
|
|
|
~File::open(&path) as ~Reader
|
|
|
|
};
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
match mode {
|
|
|
|
Decode => decode(input, ignore_garbage),
|
|
|
|
Encode => encode(input, line_wrap),
|
|
|
|
Help => help(progname, usage),
|
2013-12-20 19:34:45 +00:00
|
|
|
Version => version()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
fn decode(input: &mut Reader, ignore_garbage: bool) {
|
2014-01-24 17:29:00 +00:00
|
|
|
let mut to_decode = str::from_utf8_owned(input.read_to_end()).unwrap();
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2014-01-24 17:29:00 +00:00
|
|
|
to_decode = str::replace(to_decode, "\n", "");
|
2013-12-20 19:34:45 +00:00
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
if ignore_garbage {
|
2013-12-20 19:34:45 +00:00
|
|
|
let standard_chars =
|
|
|
|
bytes!("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
2013-12-21 03:27:49 +00:00
|
|
|
"abcdefghijklmnopqrstuvwxyz",
|
|
|
|
"0123456789+/").map(|b| char::from_u32(*b as u32).unwrap());
|
2013-12-20 19:34:45 +00:00
|
|
|
|
|
|
|
to_decode = to_decode
|
|
|
|
.trim_chars(&|c| !standard_chars.contains(&c))
|
|
|
|
.to_owned();
|
|
|
|
}
|
|
|
|
|
|
|
|
match to_decode.from_base64() {
|
|
|
|
Ok(bytes) => {
|
|
|
|
let mut out = stdout();
|
|
|
|
|
|
|
|
out.write(bytes);
|
|
|
|
out.flush();
|
|
|
|
}
|
2013-12-20 21:15:27 +00:00
|
|
|
Err(s) => {
|
2014-01-21 19:00:01 +00:00
|
|
|
error!("error: {}", s.to_str());
|
2013-12-20 21:15:27 +00:00
|
|
|
fail!()
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
};
|
2013-12-27 16:27:41 +00:00
|
|
|
let to_encode = input.read_to_end();
|
2013-12-20 19:34:45 +00:00
|
|
|
let mut encoded = to_encode.to_base64(b64_conf);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
encoded = encoded.replace("\r", "");
|
|
|
|
|
|
|
|
println(encoded);
|
|
|
|
}
|
|
|
|
|
2013-12-27 16:27:41 +00:00
|
|
|
fn help(progname: &str, usage: &str) {
|
|
|
|
println!("Usage: {:s} [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
|
|
|
|
2013-12-21 03:27: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|