mirror of
https://gitee.com/openharmony/third_party_rust_rust-base64
synced 2024-12-12 15:37:00 +00:00
Change encode's return from Result<String, Err> to Vec<u8> and decode's from Result<String, Err> to Result<Vec<u8>, Err>
Closes #1
This commit is contained in:
parent
b89fe664db
commit
b7a2bbc4d5
51
src/lib.rs
51
src/lib.rs
@ -55,8 +55,8 @@ impl From<string::FromUtf8Error> for Base64Error {
|
||||
}
|
||||
}
|
||||
|
||||
///Encode from string reference as base64.
|
||||
///Returns a Result containing a String.
|
||||
///Encode from octets as base64.
|
||||
///Returns a String.
|
||||
///
|
||||
///# Example
|
||||
///
|
||||
@ -64,19 +64,16 @@ impl From<string::FromUtf8Error> for Base64Error {
|
||||
///extern crate base64;
|
||||
///
|
||||
///fn main() {
|
||||
/// let b64 = base64::encode("hello world").unwrap();
|
||||
/// let b64 = base64::encode(b"hello world");
|
||||
/// println!("{}", b64);
|
||||
///}
|
||||
///```
|
||||
pub fn encode(input: &str) -> Result<String, Base64Error> {
|
||||
match u8en(input.as_bytes()) {
|
||||
Ok(bytes) => Ok(try!(String::from_utf8(bytes))),
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
pub fn encode(input: &[u8]) -> String {
|
||||
String::from_utf8(u8en(input)).unwrap() //FIXME
|
||||
}
|
||||
|
||||
///Decode from string reference as utf8.
|
||||
///Returns a Result containing a String.
|
||||
///Decode from string reference as octets.
|
||||
///Returns a Result containing a Vec<u8>.
|
||||
///
|
||||
///# Example
|
||||
///
|
||||
@ -84,19 +81,16 @@ pub fn encode(input: &str) -> Result<String, Base64Error> {
|
||||
///extern crate base64;
|
||||
///
|
||||
///fn main() {
|
||||
/// let text = base64::decode("aGVsbG8gd29ybGQ=").unwrap();
|
||||
/// println!("{}", text);
|
||||
/// let bytes = base64::decode("aGVsbG8gd29ybGQ=").unwrap();
|
||||
/// println!("{:?}", bytes);
|
||||
///}
|
||||
///```
|
||||
pub fn decode(input: &str) -> Result<String, Base64Error> {
|
||||
match u8de(input.as_bytes()) {
|
||||
Ok(bytes) => Ok(try!(String::from_utf8(bytes))),
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
pub fn decode(input: &str) -> Result<Vec<u8>, Base64Error> {
|
||||
u8de(input.as_bytes())
|
||||
}
|
||||
|
||||
///Decode from string reference as utf8.
|
||||
///Returns a Result containing a String.
|
||||
///Decode from string reference as octets.
|
||||
///Returns a Result containing a Vec<u8>.
|
||||
///Ignores extraneous whitespace.
|
||||
///
|
||||
///# Example
|
||||
@ -105,11 +99,11 @@ pub fn decode(input: &str) -> Result<String, Base64Error> {
|
||||
///extern crate base64;
|
||||
///
|
||||
///fn main() {
|
||||
/// let text = base64::decode_ws("aG VsbG8gd2\r\n9ybGQ=").unwrap();
|
||||
/// println!("{}", text);
|
||||
/// let bytes = base64::decode_ws("aG VsbG8gd2\r\n9ybGQ=").unwrap();
|
||||
/// println!("{:?}", bytes);
|
||||
///}
|
||||
///```
|
||||
pub fn decode_ws(input: &str) -> Result<String, Base64Error> {
|
||||
pub fn decode_ws(input: &str) -> Result<Vec<u8>, Base64Error> {
|
||||
let bytes = input.as_bytes();
|
||||
let mut raw = Vec::<u8>::with_capacity(input.len());
|
||||
|
||||
@ -120,14 +114,11 @@ pub fn decode_ws(input: &str) -> Result<String, Base64Error> {
|
||||
}
|
||||
}
|
||||
|
||||
match u8de(&raw) {
|
||||
Ok(bytes) => Ok(try!(String::from_utf8(bytes))),
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
u8de(&raw)
|
||||
}
|
||||
|
||||
///Encode arbitrary octets.
|
||||
///Returns a Result containing a Vec<u8>.
|
||||
///Returns a Vec<u8>.
|
||||
///
|
||||
///# Example
|
||||
///
|
||||
@ -135,11 +126,11 @@ pub fn decode_ws(input: &str) -> Result<String, Base64Error> {
|
||||
///extern crate base64;
|
||||
///
|
||||
///fn main() {
|
||||
/// let bytes = base64::u8en(&[104, 105]).unwrap();
|
||||
/// let bytes = base64::u8en(&[104, 105]);
|
||||
/// println!("{:?}", bytes);
|
||||
///}
|
||||
///```
|
||||
pub fn u8en(bytes: &[u8]) -> Result<Vec<u8>, Base64Error> {
|
||||
pub fn u8en(bytes: &[u8]) -> Vec<u8> {
|
||||
let rem = bytes.len() % 3;
|
||||
let div = bytes.len() - rem;
|
||||
|
||||
@ -168,7 +159,7 @@ pub fn u8en(bytes: &[u8]) -> Result<Vec<u8>, Base64Error> {
|
||||
raw.push(0x3d);
|
||||
}
|
||||
|
||||
Ok(raw)
|
||||
raw
|
||||
}
|
||||
|
||||
///Decode base64 as octets.
|
||||
|
@ -3,54 +3,66 @@ extern crate base64;
|
||||
|
||||
use base64::*;
|
||||
|
||||
fn compare_encode(expected: &str, target: &[u8]) {
|
||||
assert_eq!(expected, encode(target));
|
||||
}
|
||||
|
||||
fn compare_decode(expected: &str, target: &str) {
|
||||
assert_eq!(expected, String::from_utf8(decode(target).unwrap()).unwrap());
|
||||
}
|
||||
|
||||
fn compare_decode_ws(expected: &str, target: &str) {
|
||||
assert_eq!(expected, String::from_utf8(decode_ws(target).unwrap()).unwrap());
|
||||
}
|
||||
|
||||
//-------
|
||||
//decode
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_0() {
|
||||
assert_eq!("", decode("").unwrap());
|
||||
compare_decode("", "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_1() {
|
||||
assert_eq!("f", decode("Zg==").unwrap());
|
||||
compare_decode("f", "Zg==");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_2() {
|
||||
assert_eq!("fo", decode("Zm8=").unwrap());
|
||||
compare_decode("fo", "Zm8=");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_3() {
|
||||
assert_eq!("foo", decode("Zm9v").unwrap());
|
||||
compare_decode("foo", "Zm9v");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_4() {
|
||||
assert_eq!("foob", decode("Zm9vYg==").unwrap());
|
||||
compare_decode("foob", "Zm9vYg==");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_5() {
|
||||
assert_eq!("fooba", decode("Zm9vYmE=").unwrap());
|
||||
compare_decode("fooba", "Zm9vYmE=");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decode_rfc4648_6() {
|
||||
assert_eq!("foobar", decode("Zm9vYmFy").unwrap());
|
||||
compare_decode("foobar", "Zm9vYmFy");
|
||||
}
|
||||
|
||||
//this is a MAY in the rfc
|
||||
#[test]
|
||||
fn decode_allow_extra_pad() {
|
||||
assert_eq!("alice", decode("YWxpY2U=====").unwrap());
|
||||
compare_decode("alice", "YWxpY2U=====");
|
||||
}
|
||||
|
||||
//same
|
||||
#[test]
|
||||
fn decode_allow_absurd_pad() {
|
||||
assert_eq!("alice", decode("==Y=Wx===pY=2U=====").unwrap());
|
||||
compare_decode("alice", "==Y=Wx===pY=2U=====");
|
||||
}
|
||||
|
||||
//TODO like, write a thing to test every ascii val lol
|
||||
@ -90,14 +102,14 @@ fn decode_reject_null() {
|
||||
assert!(decode("YWx\0pY2U=").is_ok());
|
||||
}
|
||||
|
||||
|
||||
//TODO unicode tests
|
||||
//put in a seperate file so this remains valid ascii
|
||||
|
||||
#[test]
|
||||
fn decode_ws_absurd_whitespace() {
|
||||
assert_eq!("how could you let this happen",
|
||||
decode_ws("\n aG93I\n\nGNvd\r\nWxkI HlvdSB \tsZXQgdGh\rpcyBo\x0cYXBwZW4 = ")
|
||||
.unwrap());
|
||||
compare_decode_ws("how could you let this happen",
|
||||
"\n aG93I\n\nGNvd\r\nWxkI HlvdSB \tsZXQgdGh\rpcyBo\x0cYXBwZW4 = ");
|
||||
}
|
||||
|
||||
//-------
|
||||
@ -105,66 +117,65 @@ fn decode_ws_absurd_whitespace() {
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_0() {
|
||||
assert_eq!(encode("").unwrap(), "");
|
||||
compare_encode("", b"");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_1() {
|
||||
assert_eq!(encode("f").unwrap(), "Zg==");
|
||||
compare_encode("Zg==", b"f");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_2() {
|
||||
assert_eq!(encode("fo").unwrap(), "Zm8=");
|
||||
compare_encode("Zm8=", b"fo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_3() {
|
||||
assert_eq!(encode("foo").unwrap(), "Zm9v");
|
||||
compare_encode("Zm9v", b"foo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_4() {
|
||||
assert_eq!(encode("foob").unwrap(), "Zm9vYg==");
|
||||
compare_encode("Zm9vYg==", b"foob");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_5() {
|
||||
assert_eq!(encode("fooba").unwrap(), "Zm9vYmE=");
|
||||
compare_encode("Zm9vYmE=", b"fooba");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encode_rfc4648_6() {
|
||||
assert_eq!(encode("foobar").unwrap(), "Zm9vYmFy");
|
||||
compare_encode("Zm9vYmFy", b"foobar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u8en_all_ascii() {
|
||||
fn encode_all_ascii() {
|
||||
let mut ascii = Vec::<u8>::with_capacity(128);
|
||||
|
||||
for i in 0..128 {
|
||||
ascii.push(i);
|
||||
}
|
||||
|
||||
assert!(u8en(&ascii).is_ok());
|
||||
compare_encode("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn8=", &ascii);
|
||||
}
|
||||
|
||||
//this doesn't actually overflow lol
|
||||
#[test]
|
||||
#[allow(overflowing_literals)]
|
||||
fn u8en_all_bytes() {
|
||||
fn encode_all_bytes() {
|
||||
let mut bytes = Vec::<u8>::with_capacity(256);
|
||||
|
||||
for i in 0..256 {
|
||||
for i in 0..255 {
|
||||
bytes.push(i);
|
||||
}
|
||||
bytes.push(255); //bug with "overflowing" ranges?
|
||||
|
||||
assert!(u8en(&bytes).is_ok());
|
||||
compare_encode("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==", &bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn because_we_can() {
|
||||
assert_eq!("alice", decode("YWxpY2U=").unwrap());
|
||||
assert_eq!("alice", decode(&(encode("alice").unwrap())).unwrap());
|
||||
assert_eq!("alice", decode(&(encode(&(decode(&(encode("alice").unwrap())).unwrap())).unwrap())).unwrap());
|
||||
compare_decode("alice", "YWxpY2U=");
|
||||
compare_decode("alice", &encode(b"alice"));
|
||||
compare_decode("alice", &encode(&decode(&encode(b"alice")).unwrap()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user