2013-04-14 07:01:54 -05:00
|
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2013-03-27 02:40:15 -05:00
|
|
|
|
//! Base64 binary-to-text encoding
|
2013-08-04 22:51:26 -05:00
|
|
|
|
use std::str;
|
2014-02-19 20:56:33 -06:00
|
|
|
|
use std::fmt;
|
2013-03-27 02:40:15 -05:00
|
|
|
|
|
2013-06-30 23:08:49 -05:00
|
|
|
|
/// Available encoding character sets
|
|
|
|
|
pub enum CharacterSet {
|
2013-09-26 00:57:26 -05:00
|
|
|
|
/// The standard character set (uses `+` and `/`)
|
2013-06-30 23:08:49 -05:00
|
|
|
|
Standard,
|
2013-09-26 00:57:26 -05:00
|
|
|
|
/// The URL safe character set (uses `-` and `_`)
|
2013-06-30 23:08:49 -05:00
|
|
|
|
UrlSafe
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 00:57:26 -05:00
|
|
|
|
/// Contains configuration parameters for `to_base64`.
|
2013-06-30 17:08:22 -05:00
|
|
|
|
pub struct Config {
|
2013-06-30 23:08:49 -05:00
|
|
|
|
/// Character set to use
|
2014-04-03 02:28:40 -05:00
|
|
|
|
pub char_set: CharacterSet,
|
2013-09-26 00:57:26 -05:00
|
|
|
|
/// True to pad output with `=` characters
|
2014-04-03 02:28:40 -05:00
|
|
|
|
pub pad: bool,
|
2013-09-26 00:57:26 -05:00
|
|
|
|
/// `Some(len)` to wrap lines at `len`, `None` to disable line wrapping
|
2014-04-03 02:28:40 -05:00
|
|
|
|
pub line_length: Option<uint>
|
2013-06-30 17:08:22 -05:00
|
|
|
|
}
|
2013-05-17 17:28:44 -05:00
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
/// Configuration for RFC 4648 standard base64 encoding
|
2013-07-03 22:33:55 -05:00
|
|
|
|
pub static STANDARD: Config =
|
2013-06-30 23:08:49 -05:00
|
|
|
|
Config {char_set: Standard, pad: true, line_length: None};
|
2013-05-24 21:35:29 -05:00
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
/// Configuration for RFC 4648 base64url encoding
|
2013-07-03 22:33:55 -05:00
|
|
|
|
pub static URL_SAFE: Config =
|
2013-06-30 23:08:49 -05:00
|
|
|
|
Config {char_set: UrlSafe, pad: false, line_length: None};
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
|
|
|
|
/// Configuration for RFC 2045 MIME base64 encoding
|
2013-07-03 22:33:55 -05:00
|
|
|
|
pub static MIME: Config =
|
2013-06-30 23:08:49 -05:00
|
|
|
|
Config {char_set: Standard, pad: true, line_length: Some(76)};
|
2012-07-03 23:29:45 -05:00
|
|
|
|
|
2013-08-04 22:51:26 -05:00
|
|
|
|
static STANDARD_CHARS: &'static[u8] = bytes!("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
|
|
|
"abcdefghijklmnopqrstuvwxyz",
|
|
|
|
|
"0123456789+/");
|
|
|
|
|
|
|
|
|
|
static URLSAFE_CHARS: &'static[u8] = bytes!("ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
|
|
|
"abcdefghijklmnopqrstuvwxyz",
|
|
|
|
|
"0123456789-_");
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
|
|
|
|
/// A trait for converting a value to base64 encoding.
|
|
|
|
|
pub trait ToBase64 {
|
|
|
|
|
/// Converts the value of `self` to a base64 value following the specified
|
|
|
|
|
/// format configuration, returning the owned string.
|
|
|
|
|
fn to_base64(&self, config: Config) -> ~str;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
|
impl<'a> ToBase64 for &'a [u8] {
|
2013-04-13 07:11:39 -05:00
|
|
|
|
/**
|
Doc review, as requested :-).
Mostly just phrasing things differently, which is a matter of taste. Feel free to use or not use any of the changes I'm suggesting.
I would say this one thing should be changed, though, not necessarily the way I changed it here.
* Convert any string (literal, `@`, `&`, `~`)
* that contains a base64 encoded value, to the byte values it encodes.
If this structure is going to be used, either the entire clause, 'that contains a base64 encoded value', should be bracketed by commas, or the comma at the end of the clause should be removed.
2013-04-13 15:17:30 -05:00
|
|
|
|
* Turn a vector of `u8` bytes into a base64 string.
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*
|
2013-05-27 08:49:54 -05:00
|
|
|
|
* # Example
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*
|
2013-09-23 19:20:36 -05:00
|
|
|
|
* ```rust
|
2014-02-14 12:10:06 -06:00
|
|
|
|
* extern crate serialize;
|
2014-02-11 18:40:52 -06:00
|
|
|
|
* use serialize::base64::{ToBase64, STANDARD};
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*
|
|
|
|
|
* fn main () {
|
2013-09-20 08:17:32 -05:00
|
|
|
|
* let str = [52,32].to_base64(STANDARD);
|
|
|
|
|
* println!("base 64 output: {}", str);
|
2013-04-13 07:11:39 -05:00
|
|
|
|
* }
|
2013-09-23 19:20:36 -05:00
|
|
|
|
* ```
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*/
|
2013-06-30 17:08:22 -05:00
|
|
|
|
fn to_base64(&self, config: Config) -> ~str {
|
2013-08-04 22:51:26 -05:00
|
|
|
|
let bytes = match config.char_set {
|
2013-06-30 23:08:49 -05:00
|
|
|
|
Standard => STANDARD_CHARS,
|
|
|
|
|
UrlSafe => URLSAFE_CHARS
|
2013-06-30 17:08:22 -05:00
|
|
|
|
};
|
|
|
|
|
|
2014-04-17 17:59:07 -05:00
|
|
|
|
let mut v = Vec::new();
|
2013-06-30 17:08:22 -05:00
|
|
|
|
let mut i = 0;
|
|
|
|
|
let mut cur_length = 0;
|
2013-04-08 15:50:34 -05:00
|
|
|
|
let len = self.len();
|
2013-06-30 17:08:22 -05:00
|
|
|
|
while i < len - (len % 3) {
|
|
|
|
|
match config.line_length {
|
|
|
|
|
Some(line_length) =>
|
|
|
|
|
if cur_length >= line_length {
|
2013-08-04 22:51:26 -05:00
|
|
|
|
v.push('\r' as u8);
|
|
|
|
|
v.push('\n' as u8);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
cur_length = 0;
|
|
|
|
|
},
|
|
|
|
|
None => ()
|
|
|
|
|
}
|
2013-04-08 15:50:34 -05:00
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
let n = (self[i] as u32) << 16 |
|
|
|
|
|
(self[i + 1] as u32) << 8 |
|
|
|
|
|
(self[i + 2] as u32);
|
2013-04-08 15:50:34 -05:00
|
|
|
|
|
|
|
|
|
// This 24-bit number gets separated into four 6-bit numbers.
|
2014-04-01 22:39:26 -05:00
|
|
|
|
v.push(bytes[((n >> 18) & 63) as uint]);
|
|
|
|
|
v.push(bytes[((n >> 12) & 63) as uint]);
|
|
|
|
|
v.push(bytes[((n >> 6 ) & 63) as uint]);
|
|
|
|
|
v.push(bytes[(n & 63) as uint]);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
|
|
|
|
cur_length += 4;
|
|
|
|
|
i += 3;
|
|
|
|
|
}
|
2013-04-08 15:50:34 -05:00
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
if len % 3 != 0 {
|
|
|
|
|
match config.line_length {
|
|
|
|
|
Some(line_length) =>
|
|
|
|
|
if cur_length >= line_length {
|
2013-08-04 22:51:26 -05:00
|
|
|
|
v.push('\r' as u8);
|
|
|
|
|
v.push('\n' as u8);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
},
|
|
|
|
|
None => ()
|
|
|
|
|
}
|
2013-04-08 15:50:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Heh, would be cool if we knew this was exhaustive
|
|
|
|
|
// (the dream of bounded integer types)
|
|
|
|
|
match len % 3 {
|
2013-06-30 17:08:22 -05:00
|
|
|
|
0 => (),
|
|
|
|
|
1 => {
|
|
|
|
|
let n = (self[i] as u32) << 16;
|
2014-04-01 22:39:26 -05:00
|
|
|
|
v.push(bytes[((n >> 18) & 63) as uint]);
|
|
|
|
|
v.push(bytes[((n >> 12) & 63) as uint]);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
if config.pad {
|
2013-08-04 22:51:26 -05:00
|
|
|
|
v.push('=' as u8);
|
|
|
|
|
v.push('=' as u8);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
2 => {
|
|
|
|
|
let n = (self[i] as u32) << 16 |
|
|
|
|
|
(self[i + 1u] as u32) << 8;
|
2014-04-01 22:39:26 -05:00
|
|
|
|
v.push(bytes[((n >> 18) & 63) as uint]);
|
|
|
|
|
v.push(bytes[((n >> 12) & 63) as uint]);
|
|
|
|
|
v.push(bytes[((n >> 6 ) & 63) as uint]);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
if config.pad {
|
2013-08-04 22:51:26 -05:00
|
|
|
|
v.push('=' as u8);
|
2013-06-30 17:08:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
|
_ => fail!("Algebra is broken, please alert the math police")
|
2012-07-03 23:29:45 -05:00
|
|
|
|
}
|
2013-08-04 22:51:26 -05:00
|
|
|
|
|
|
|
|
|
unsafe {
|
2014-05-04 01:34:26 -05:00
|
|
|
|
str::raw::from_utf8(v.as_slice()).to_owned()
|
2013-08-04 22:51:26 -05:00
|
|
|
|
}
|
2012-07-03 23:29:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
/// A trait for converting from base64 encoded values.
|
2012-09-27 18:43:15 -05:00
|
|
|
|
pub trait FromBase64 {
|
2013-06-30 17:08:22 -05:00
|
|
|
|
/// Converts the value of `self`, interpreted as base64 encoded data, into
|
|
|
|
|
/// an owned vector of bytes, returning the vector.
|
2014-05-04 01:34:26 -05:00
|
|
|
|
fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error>;
|
2014-01-16 01:15:04 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Errors that can occur when decoding a base64 encoded string
|
|
|
|
|
pub enum FromBase64Error {
|
|
|
|
|
/// The input contained a character not part of the base64 format
|
|
|
|
|
InvalidBase64Character(char, uint),
|
|
|
|
|
/// The input had an invalid length
|
|
|
|
|
InvalidBase64Length,
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 20:56:33 -06:00
|
|
|
|
impl fmt::Show for FromBase64Error {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-01-16 01:15:04 -06:00
|
|
|
|
match *self {
|
|
|
|
|
InvalidBase64Character(ch, idx) =>
|
2014-02-19 20:56:33 -06:00
|
|
|
|
write!(f.buf, "Invalid character '{}' at position {}", ch, idx),
|
|
|
|
|
InvalidBase64Length => write!(f.buf, "Invalid length"),
|
2014-01-16 01:15:04 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-03 23:29:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
|
impl<'a> FromBase64 for &'a str {
|
2013-04-13 07:11:39 -05:00
|
|
|
|
/**
|
2013-08-05 00:25:15 -05:00
|
|
|
|
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
|
|
|
|
|
* to the byte values it encodes.
|
|
|
|
|
*
|
2013-11-28 06:52:11 -06:00
|
|
|
|
* You can use the `from_utf8_owned` function in `std::str`
|
2013-08-05 00:25:15 -05:00
|
|
|
|
* to turn a `[u8]` into a string with characters corresponding to those
|
|
|
|
|
* values.
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*
|
2013-05-27 08:49:54 -05:00
|
|
|
|
* # Example
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*
|
2013-08-05 00:25:15 -05:00
|
|
|
|
* This converts a string literal to base64 and back.
|
|
|
|
|
*
|
2013-09-23 19:20:36 -05:00
|
|
|
|
* ```rust
|
2014-02-14 12:10:06 -06:00
|
|
|
|
* extern crate serialize;
|
2014-02-11 18:40:52 -06:00
|
|
|
|
* use serialize::base64::{ToBase64, FromBase64, STANDARD};
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*
|
|
|
|
|
* fn main () {
|
2013-09-20 08:17:32 -05:00
|
|
|
|
* let hello_str = bytes!("Hello, World").to_base64(STANDARD);
|
|
|
|
|
* println!("base64 output: {}", hello_str);
|
|
|
|
|
* let res = hello_str.from_base64();
|
|
|
|
|
* if res.is_ok() {
|
2014-05-04 01:34:26 -05:00
|
|
|
|
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
|
2014-02-15 15:15:03 -06:00
|
|
|
|
* if opt_bytes.is_some() {
|
|
|
|
|
* println!("decoded from base64: {}", opt_bytes.unwrap());
|
2013-09-20 08:17:32 -05:00
|
|
|
|
* }
|
|
|
|
|
* }
|
2013-04-13 07:11:39 -05:00
|
|
|
|
* }
|
2013-09-23 19:20:36 -05:00
|
|
|
|
* ```
|
2013-04-13 07:11:39 -05:00
|
|
|
|
*/
|
2014-05-04 01:34:26 -05:00
|
|
|
|
fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error> {
|
2014-04-17 17:59:07 -05:00
|
|
|
|
let mut r = Vec::new();
|
2013-06-30 17:08:22 -05:00
|
|
|
|
let mut buf: u32 = 0;
|
|
|
|
|
let mut modulus = 0;
|
2012-07-03 23:29:45 -05:00
|
|
|
|
|
2013-11-23 04:18:51 -06:00
|
|
|
|
let mut it = self.bytes().enumerate();
|
2013-08-05 00:25:15 -05:00
|
|
|
|
for (idx, byte) in it {
|
2013-06-30 17:08:22 -05:00
|
|
|
|
let val = byte as u32;
|
2012-07-03 23:29:45 -05:00
|
|
|
|
|
2013-08-05 00:25:15 -05:00
|
|
|
|
match byte as char {
|
2013-07-02 14:47:32 -05:00
|
|
|
|
'A'..'Z' => buf |= val - 0x41,
|
|
|
|
|
'a'..'z' => buf |= val - 0x47,
|
|
|
|
|
'0'..'9' => buf |= val + 0x04,
|
|
|
|
|
'+'|'-' => buf |= 0x3E,
|
|
|
|
|
'/'|'_' => buf |= 0x3F,
|
2013-10-01 16:31:03 -05:00
|
|
|
|
'\r'|'\n' => continue,
|
2013-07-02 14:47:32 -05:00
|
|
|
|
'=' => break,
|
2014-01-16 01:15:04 -06:00
|
|
|
|
_ => return Err(InvalidBase64Character(self.char_at(idx), idx)),
|
2013-06-30 17:08:22 -05:00
|
|
|
|
}
|
2012-07-03 23:29:45 -05:00
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
buf <<= 6;
|
|
|
|
|
modulus += 1;
|
|
|
|
|
if modulus == 4 {
|
|
|
|
|
modulus = 0;
|
|
|
|
|
r.push((buf >> 22) as u8);
|
|
|
|
|
r.push((buf >> 14) as u8);
|
|
|
|
|
r.push((buf >> 6 ) as u8);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-11-17 13:00:38 -06:00
|
|
|
|
|
2013-08-05 00:25:15 -05:00
|
|
|
|
for (idx, byte) in it {
|
2014-01-18 12:18:44 -06:00
|
|
|
|
match byte as char {
|
|
|
|
|
'='|'\r'|'\n' => continue,
|
|
|
|
|
_ => return Err(InvalidBase64Character(self.char_at(idx), idx)),
|
2013-08-05 00:25:15 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
}
|
2012-11-17 13:00:38 -06:00
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
|
match modulus {
|
|
|
|
|
2 => {
|
|
|
|
|
r.push((buf >> 10) as u8);
|
|
|
|
|
}
|
|
|
|
|
3 => {
|
|
|
|
|
r.push((buf >> 16) as u8);
|
|
|
|
|
r.push((buf >> 8 ) as u8);
|
|
|
|
|
}
|
|
|
|
|
0 => (),
|
2014-01-16 01:15:04 -06:00
|
|
|
|
_ => return Err(InvalidBase64Length),
|
2012-07-03 23:29:45 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2014-05-04 01:34:26 -05:00
|
|
|
|
Ok(r)
|
2012-07-03 23:29:45 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[cfg(test)]
|
2014-02-13 19:49:11 -06:00
|
|
|
|
mod tests {
|
|
|
|
|
extern crate test;
|
2014-03-01 18:33:24 -06:00
|
|
|
|
extern crate rand;
|
2014-03-31 20:16:35 -05:00
|
|
|
|
use self::test::Bencher;
|
2014-02-11 18:40:52 -06:00
|
|
|
|
use base64::{Config, FromBase64, ToBase64, STANDARD, URL_SAFE};
|
2013-07-19 16:07:00 -05:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_to_base64_basic() {
|
2014-04-15 20:17:48 -05:00
|
|
|
|
assert_eq!("".as_bytes().to_base64(STANDARD), "".to_owned());
|
|
|
|
|
assert_eq!("f".as_bytes().to_base64(STANDARD), "Zg==".to_owned());
|
|
|
|
|
assert_eq!("fo".as_bytes().to_base64(STANDARD), "Zm8=".to_owned());
|
|
|
|
|
assert_eq!("foo".as_bytes().to_base64(STANDARD), "Zm9v".to_owned());
|
|
|
|
|
assert_eq!("foob".as_bytes().to_base64(STANDARD), "Zm9vYg==".to_owned());
|
|
|
|
|
assert_eq!("fooba".as_bytes().to_base64(STANDARD), "Zm9vYmE=".to_owned());
|
|
|
|
|
assert_eq!("foobar".as_bytes().to_base64(STANDARD), "Zm9vYmFy".to_owned());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_to_base64_line_break() {
|
2013-11-25 02:10:31 -06:00
|
|
|
|
assert!(![0u8, ..1000].to_base64(Config {line_length: None, ..STANDARD})
|
2013-07-19 16:07:00 -05:00
|
|
|
|
.contains("\r\n"));
|
2013-08-05 00:25:15 -05:00
|
|
|
|
assert_eq!("foobar".as_bytes().to_base64(Config {line_length: Some(4),
|
|
|
|
|
..STANDARD}),
|
2014-04-15 20:17:48 -05:00
|
|
|
|
"Zm9v\r\nYmFy".to_owned());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_to_base64_padding() {
|
2014-04-15 20:17:48 -05:00
|
|
|
|
assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zg".to_owned());
|
|
|
|
|
assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zm8".to_owned());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_to_base64_url_safe() {
|
2014-04-15 20:17:48 -05:00
|
|
|
|
assert_eq!([251, 255].to_base64(URL_SAFE), "-_8".to_owned());
|
|
|
|
|
assert_eq!([251, 255].to_base64(STANDARD), "+/8=".to_owned());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_from_base64_basic() {
|
2014-05-04 01:34:26 -05:00
|
|
|
|
assert_eq!("".from_base64().unwrap().as_slice(), "".as_bytes());
|
|
|
|
|
assert_eq!("Zg==".from_base64().unwrap().as_slice(), "f".as_bytes());
|
|
|
|
|
assert_eq!("Zm8=".from_base64().unwrap().as_slice(), "fo".as_bytes());
|
|
|
|
|
assert_eq!("Zm9v".from_base64().unwrap().as_slice(), "foo".as_bytes());
|
|
|
|
|
assert_eq!("Zm9vYg==".from_base64().unwrap().as_slice(), "foob".as_bytes());
|
|
|
|
|
assert_eq!("Zm9vYmE=".from_base64().unwrap().as_slice(), "fooba".as_bytes());
|
|
|
|
|
assert_eq!("Zm9vYmFy".from_base64().unwrap().as_slice(), "foobar".as_bytes());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_from_base64_newlines() {
|
2014-05-04 01:34:26 -05:00
|
|
|
|
assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap().as_slice(),
|
|
|
|
|
"foobar".as_bytes());
|
|
|
|
|
assert_eq!("Zm9vYg==\r\n".from_base64().unwrap().as_slice(),
|
|
|
|
|
"foob".as_bytes());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_from_base64_urlsafe() {
|
2013-08-03 18:59:24 -05:00
|
|
|
|
assert_eq!("-_8".from_base64().unwrap(), "+/8=".from_base64().unwrap());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 23:08:49 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_from_base64_invalid_char() {
|
|
|
|
|
assert!("Zm$=".from_base64().is_err())
|
2013-08-05 00:25:15 -05:00
|
|
|
|
assert!("Zg==$".from_base64().is_err());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
2013-06-30 23:08:49 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_from_base64_invalid_padding() {
|
|
|
|
|
assert!("Z===".from_base64().is_err());
|
|
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_base64_random() {
|
2014-03-01 18:33:24 -06:00
|
|
|
|
use self::rand::{task_rng, random, Rng};
|
2012-07-03 23:29:45 -05:00
|
|
|
|
|
2014-01-29 18:20:34 -06:00
|
|
|
|
for _ in range(0, 1000) {
|
2013-10-10 04:18:07 -05:00
|
|
|
|
let times = task_rng().gen_range(1u, 100);
|
2014-04-17 17:59:07 -05:00
|
|
|
|
let v = Vec::from_fn(times, |_| random::<u8>());
|
2014-05-04 01:34:26 -05:00
|
|
|
|
assert_eq!(v.as_slice().to_base64(STANDARD).from_base64().unwrap().as_slice(),
|
|
|
|
|
v.as_slice());
|
2014-01-29 18:20:34 -06:00
|
|
|
|
}
|
2012-07-03 23:29:45 -05:00
|
|
|
|
}
|
2013-07-19 16:07:00 -05:00
|
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
|
pub fn bench_to_base64(b: &mut Bencher) {
|
2013-07-19 16:07:00 -05:00
|
|
|
|
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
|
|
|
|
|
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
|
2014-03-31 20:16:35 -05:00
|
|
|
|
b.iter(|| {
|
2013-08-05 00:25:15 -05:00
|
|
|
|
s.as_bytes().to_base64(STANDARD);
|
2013-11-21 21:20:48 -06:00
|
|
|
|
});
|
2014-03-31 20:16:35 -05:00
|
|
|
|
b.bytes = s.len() as u64;
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
|
pub fn bench_from_base64(b: &mut Bencher) {
|
2013-07-19 16:07:00 -05:00
|
|
|
|
let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
|
|
|
|
|
ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
|
2014-03-31 20:16:35 -05:00
|
|
|
|
let sb = s.as_bytes().to_base64(STANDARD);
|
|
|
|
|
b.iter(|| {
|
|
|
|
|
sb.from_base64().unwrap();
|
2013-11-21 21:20:48 -06:00
|
|
|
|
});
|
2014-03-31 20:16:35 -05:00
|
|
|
|
b.bytes = sb.len() as u64;
|
2013-07-19 16:07:00 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 21:18:19 -05:00
|
|
|
|
}
|