1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #939 from knight42/base32

Implement base32 & unify base64
This commit is contained in:
Nathan Ross 2016-08-06 13:03:48 -04:00 committed by GitHub
commit 2eeb7b77cc
14 changed files with 407 additions and 125 deletions

View file

@ -36,6 +36,7 @@ unix = [
"users", "users",
] ]
generic = [ generic = [
"base32",
"base64", "base64",
"basename", "basename",
"cat", "cat",
@ -102,6 +103,7 @@ default = ["generic", "unix"]
[dependencies] [dependencies]
uucore = { path="src/uucore" } uucore = { path="src/uucore" }
arch = { optional=true, path="src/arch" } arch = { optional=true, path="src/arch" }
base32 = { optional=true, path="src/base32" }
base64 = { optional=true, path="src/base64" } base64 = { optional=true, path="src/base64" }
basename = { optional=true, path="src/basename" } basename = { optional=true, path="src/basename" }
cat = { optional=true, path="src/cat" } cat = { optional=true, path="src/cat" }

View file

@ -40,6 +40,7 @@ BUSYBOX_SRC:=$(BUSYBOX_ROOT)/busybox-$(BUSYBOX_VER)/
# Possible programs # Possible programs
PROGS := \ PROGS := \
base32 \
base64 \ base64 \
basename \ basename \
cat \ cat \
@ -137,6 +138,7 @@ UTILS ?= $(PROGS)
# Programs with usable tests # Programs with usable tests
TEST_PROGS := \ TEST_PROGS := \
base32 \
base64 \ base64 \
basename \ basename \
cat \ cat \

View file

@ -149,7 +149,6 @@ To do
- chcon - chcon
- runcon - runcon
- base32
- md5sum - md5sum
- sha1sum - sha1sum
- sha224sum - sha224sum

20
src/base32/Cargo.toml Normal file
View file

@ -0,0 +1,20 @@
[package]
name = "base32"
version = "0.0.1"
authors = []
[lib]
name = "uu_base32"
path = "base32.rs"
[dependencies]
getopts = "*"
uucore = { path="../uucore" }
[dependencies.clippy]
version = "*"
optional = true
[[bin]]
name = "base32"
path = "main.rs"

110
src/base32/base32.rs Normal file
View file

@ -0,0 +1,110 @@
// This file is part of the uutils coreutils package.
//
// (c) Jian Zeng <anonymousknight96@gmail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
#![crate_name = "uu_base32"]
extern crate getopts;
#[macro_use]
extern crate uucore;
use uucore::encoding::{Data, Format, wrap_print};
use getopts::Options;
use std::fs::File;
use std::io::{BufReader, Read, stdin, Write};
use std::path::Path;
static NAME: &'static str = "base32";
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub fn uumain(args: Vec<String>) -> 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("", "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) => {
disp_err!("{}", e);
return 1;
}
};
if matches.opt_present("help") {
return help(&opts);
} else if matches.opt_present("version") {
return version();
}
let line_wrap = match matches.opt_str("wrap") {
Some(s) => {
match s.parse() {
Ok(n) => n,
Err(e) => {
crash!(1, "invalid wrap size: {}: {}", s, e);
}
}
}
None => 76,
};
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<Read>)
} 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<Read>)
};
let mut data = Data::new(input, Format::Base32)
.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 help(opts: &Options) -> i32 {
let msg = format!("Usage: {} [OPTION]... [FILE]\n\n\
Base32 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 base32 alphabet in RFC \
4648.\nWhen decoding, the input may contain newlines in addition \
to the bytes of the formal\nbase32 alphabet. Use --ignore-garbage \
to attempt to recover from any other\nnon-alphabet bytes in the \
encoded stream.",
NAME);
print!("{}", opts.usage(&msg));
0
}
fn version() -> i32 {
println!("{} {}", NAME, VERSION);
0
}

5
src/base32/main.rs Normal file
View file

@ -0,0 +1,5 @@
extern crate uu_base32;
fn main() {
std::process::exit(uu_base32::uumain(std::env::args().collect()));
}

View file

@ -9,8 +9,6 @@ path = "base64.rs"
[dependencies] [dependencies]
getopts = "*" getopts = "*"
libc = "*"
rustc-serialize = "*"
uucore = { path="../uucore" } uucore = { path="../uucore" }
[[bin]] [[bin]]

View file

@ -1,149 +1,96 @@
#![crate_name = "uu_base64"] #![crate_name = "uu_base64"]
/* // This file is part of the uutils coreutils package.
* This file is part of the uutils coreutils package. //
* // (c) Jordy Dickinson <jordy.dickinson@gmail.com>
* (c) Jordy Dickinson <jordy.dickinson@gmail.com> // (c) Jian Zeng <anonymousknight96@gmail.com>
* //
* For the full copyright and license information, please view the LICENSE file // For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code. // that was distributed with this source code.
*/ //
extern crate rustc_serialize as serialize;
extern crate getopts; extern crate getopts;
extern crate libc;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use uucore::encoding::{Data, Format, wrap_print};
use getopts::Options; use getopts::Options;
use serialize::base64::{self, FromBase64, ToBase64};
use std::ascii::AsciiExt;
use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, Read, stdin, stdout, Write}; use std::io::{BufReader, Read, stdin, Write};
use std::path::Path; use std::path::Path;
enum Mode {
Decode,
Encode,
Help,
Version
}
static NAME: &'static str = "base64"; static NAME: &'static str = "base64";
static VERSION: &'static str = env!("CARGO_PKG_VERSION"); static VERSION: &'static str = env!("CARGO_PKG_VERSION");
pub type FileOrStdReader = BufReader<Box<Read+'static>>;
pub fn uumain(args: Vec<String>) -> i32 { pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = Options::new(); let mut opts = Options::new();
opts.optflag("d", "decode", "decode data"); opts.optflag("d", "decode", "decode data");
opts.optflag("i", "ignore-garbage", "when decoding, ignore non-alphabetic characters"); opts.optflag("i",
opts.optopt("w", "wrap", "wrap encoded lines after COLS character (default 76, 0 to disable wrapping)", "COLS"); "ignore-garbage",
opts.optflag("h", "help", "display this help text and exit"); "when decoding, ignore non-alphabetic characters");
opts.optflag("V", "version", "output version information and exit"); 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..]) { let matches = match opts.parse(&args[1..]) {
Ok(m) => m, Ok(m) => m,
Err(e) => { crash!(1, "{}", e) } Err(e) => {
}; disp_err!("{}", e);
return 1;
let mode = if matches.opt_present("help") {
Mode::Help
} 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");
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);
} }
},
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<Read+'static>)
} else {
let path = Path::new(&matches.free[0][..]);
file_buf = safe_unwrap!(File::open(&path));
BufReader::new(Box::new(file_buf) as Box<Read+'static>)
}; };
match mode { if matches.opt_present("help") {
Mode::Decode => decode(&mut input, ignore_garbage), return help(&opts);
Mode::Encode => encode(&mut input, line_wrap), } else if matches.opt_present("version") {
Mode::Help => help(opts), return version();
Mode::Version => version() }
let line_wrap = match matches.opt_str("wrap") {
Some(s) => {
match s.parse() {
Ok(n) => n,
Err(e) => {
crash!(1, "invalid wrap size: {}: {}", s, e);
}
}
}
None => 76,
};
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<Read>)
} 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<Read>)
};
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 0
} }
fn decode(input: &mut FileOrStdReader, ignore_garbage: bool) { fn help(opts: &Options) -> i32 {
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<u8> = vec!();
input.read_to_end(&mut to_encode).unwrap();
let encoded = to_encode.to_base64(b64_conf);
println!("{}", &encoded[..]);
}
fn help(opts: Options) {
let msg = format!("Usage: {} [OPTION]... [FILE]\n\n\ let msg = format!("Usage: {} [OPTION]... [FILE]\n\n\
Base64 encode or decode FILE, or standard input, to standard output.\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\ 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 \ 3548. When\ndecoding, the input may contain newlines in addition \
to the bytes of the formal\nbase64 alphabet. Use --ignore-garbage \ to the bytes of the formal\nbase64 alphabet. Use --ignore-garbage \
to attempt to recover from any other\nnon-alphabet bytes in the \ to attempt to recover from any other\nnon-alphabet bytes in the \
encoded stream.", NAME); encoded stream.",
NAME);
print!("{}", opts.usage(&msg)); print!("{}", opts.usage(&msg));
0
} }
fn version() { fn version() -> i32 {
println!("{} {}", NAME, VERSION); println!("{} {}", NAME, VERSION);
0
} }

View file

@ -6,6 +6,7 @@ authors = []
[dependencies] [dependencies]
libc = "*" libc = "*"
winapi = "*" winapi = "*"
data-encoding = "^1.1"
[lib] [lib]
path = "lib.rs" path = "lib.rs"

101
src/uucore/encoding.rs Normal file
View file

@ -0,0 +1,101 @@
// This file is part of the uutils coreutils package.
//
// (c) Jian Zeng <anonymousknight96@gmail.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
extern crate data_encoding;
use self::data_encoding::{base64, base32, decode};
use std::io::Read;
pub type DecodeResult = Result<Vec<u8>, decode::Error>;
#[derive(Clone, Copy)]
pub enum Format {
Base32,
Base64,
}
use self::Format::*;
pub fn encode(f: Format, input: &[u8]) -> String {
match f {
Base32 => base32::encode(input),
Base64 => base64::encode(input),
}
}
pub fn decode(f: Format, input: &[u8]) -> DecodeResult {
match f {
Base32 => base32::decode(input),
Base64 => base64::decode(input),
}
}
pub struct Data<R: Read> {
line_wrap: usize,
ignore_garbage: bool,
input: R,
format: Format,
alphabet: &'static str,
}
impl<R: Read> Data<R> {
pub fn new(input: R, format: Format) -> Self {
Data {
line_wrap: 76,
ignore_garbage: false,
input: input,
format: format,
alphabet: match format {
Base32 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",
Base64 => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=+/",
},
}
}
pub fn line_wrap(mut self, wrap: usize) -> Self {
self.line_wrap = wrap;
self
}
pub fn ignore_garbage(mut self, ignore: bool) -> Self {
self.ignore_garbage = ignore;
self
}
pub fn decode(&mut self) -> DecodeResult {
let mut buf = String::new();
self.input.read_to_string(&mut buf).unwrap();
let clean = if self.ignore_garbage {
buf.chars()
.filter(|&c| self.alphabet.contains(c))
.collect::<String>()
} else {
buf.chars()
.filter(|&c| c != '\r' && c != '\n')
.collect::<String>()
};
decode(self.format, clean.as_bytes())
}
pub fn encode(&mut self) -> String {
let mut buf: Vec<u8> = vec![];
self.input.read_to_end(&mut buf).unwrap();
encode(self.format, buf.as_slice())
}
}
pub fn wrap_print(line_wrap: usize, res: String) {
if line_wrap == 0 {
return print!("{}", res);
}
use std::cmp::min;
let mut start = 0;
while start < res.len() {
let end = min(start + line_wrap, res.len());
println!("{}", &res[start..end]);
start = end;
}
}

View file

@ -7,6 +7,7 @@ mod macros;
pub mod fs; pub mod fs;
pub mod parse_time; pub mod parse_time;
pub mod utf8; pub mod utf8;
pub mod encoding;
#[cfg(unix)] pub mod c_types; #[cfg(unix)] pub mod c_types;
#[cfg(unix)] pub mod process; #[cfg(unix)] pub mod process;

92
tests/test_base32.rs Normal file
View file

@ -0,0 +1,92 @@
// This file is part of the uutils coreutils package.
//
// (c) Jian Zeng <anonymousknight96@gmail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.
//
use common::util::*;
static UTIL_NAME: &'static str = "base32";
fn new_ucmd() -> UCommand {
TestScenario::new(UTIL_NAME).ucmd()
}
#[test]
fn test_encode() {
let input = "Hello, World!";
new_ucmd()
.pipe_in(input)
.succeeds()
.stdout_only("JBSWY3DPFQQFO33SNRSCC===\n");
}
#[test]
fn test_decode() {
for decode_param in vec!["-d", "--decode"] {
let input = "JBSWY3DPFQQFO33SNRSCC===\n";
new_ucmd()
.arg(decode_param)
.pipe_in(input)
.succeeds()
.stdout_only("Hello, World!");
}
}
#[test]
fn test_garbage() {
let input = "aGVsbG8sIHdvcmxkIQ==\0";
new_ucmd()
.arg("-d")
.pipe_in(input)
.fails()
.stderr_only("base32: error: invalid input\n");
}
#[test]
fn test_ignore_garbage() {
for ignore_garbage_param in vec!["-i", "--ignore-garbage"] {
let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n";
new_ucmd()
.arg("-d")
.arg(ignore_garbage_param)
.pipe_in(input)
.succeeds()
.stdout_only("Hello, World!");
}
}
#[test]
fn test_wrap() {
for wrap_param in vec!["-w", "--wrap"] {
let input = "The quick brown fox jumps over the lazy dog.";
new_ucmd()
.arg(wrap_param)
.arg("20")
.pipe_in(input)
.succeeds()
.stdout_only("KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n");
}
}
#[test]
fn test_wrap_no_arg() {
for wrap_param in vec!["-w", "--wrap"] {
new_ucmd()
.arg(wrap_param)
.fails()
.stderr_only(format!("base32: Argument to option '{}' missing.\nTry 'base32 --help' for more information.\n",
if wrap_param == "-w" { "w" } else { "wrap" }));
}
}
#[test]
fn test_wrap_bad_arg() {
for wrap_param in vec!["-w", "--wrap"] {
new_ucmd()
.arg(wrap_param).arg("b")
.fails()
.stderr_only("base32: error: invalid wrap size: b: invalid digit found in string\n");
}
}

View file

@ -33,7 +33,7 @@ fn test_garbage() {
.arg("-d") .arg("-d")
.pipe_in(input) .pipe_in(input)
.fails() .fails()
.stderr_only("base64: error: invalid character (Invalid character '0' at position 20)\n"); .stderr_only("base64: error: invalid input\n");
} }
#[test] #[test]
@ -68,8 +68,7 @@ fn test_wrap_no_arg() {
new_ucmd() new_ucmd()
.arg(wrap_param) .arg(wrap_param)
.fails() .fails()
.stderr_only( .stderr_only(format!("base64: Argument to option '{}' missing.\nTry 'base64 --help' for more information.\n",
format!("base64: error: Argument to option '{}' missing.",
if wrap_param == "-w" { "w" } else { "wrap" })); if wrap_param == "-w" { "w" } else { "wrap" }));
} }
} }
@ -78,8 +77,9 @@ fn test_wrap_no_arg() {
fn test_wrap_bad_arg() { fn test_wrap_bad_arg() {
for wrap_param in vec!["-w", "--wrap"] { for wrap_param in vec!["-w", "--wrap"] {
new_ucmd() new_ucmd()
.arg(wrap_param).arg("b") .arg(wrap_param)
.arg("b")
.fails() .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");
} }
} }

View file

@ -48,6 +48,7 @@ macro_rules! generic {
}; };
} }
generic! { generic! {
"base32", test_base32;
"base64", test_base64; "base64", test_base64;
"basename", test_basename; "basename", test_basename;
"cat", test_cat; "cat", test_cat;