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-06-30 17:08:22 -05:00
|
|
|
/// Contains configuration parameters for to_base64
|
|
|
|
pub struct Config {
|
|
|
|
/// True to use the url-safe encoding format ('-' and '_'), false to use
|
|
|
|
/// the standard encoding format ('+' and '/')
|
|
|
|
pub url_safe: bool,
|
|
|
|
/// True to pad output with '=' characters
|
|
|
|
pub pad: bool,
|
|
|
|
/// Some(len) to wrap lines at len, None to disable line wrapping
|
|
|
|
pub line_length: Option<uint>
|
|
|
|
}
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
/// Configuration for RFC 4648 standard base64 encoding
|
|
|
|
pub static standard: Config =
|
|
|
|
Config {url_safe: false, 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
|
|
|
|
pub static url_safe: Config =
|
|
|
|
Config {url_safe: true, pad: false, line_length: None};
|
|
|
|
|
|
|
|
/// Configuration for RFC 2045 MIME base64 encoding
|
|
|
|
pub static mime: Config =
|
|
|
|
Config {url_safe: false, pad: true, line_length: Some(76)};
|
2012-07-03 23:29:45 -05:00
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
static STANDARD_CHARS: [char, ..64] = [
|
2013-03-24 01:51:18 -05:00
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
|
|
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
|
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
|
|
|
|
];
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
static URLSAFE_CHARS: [char, ..64] = [
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
|
|
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
|
|
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
|
|
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
|
|
|
|
];
|
|
|
|
|
|
|
|
/// 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-03-25 15:21:04 -05:00
|
|
|
impl<'self> ToBase64 for &'self [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-05-27 08:49:54 -05:00
|
|
|
* ~~~ {.rust}
|
2013-06-30 17:08:22 -05:00
|
|
|
* use std::base64::{ToBase64, standard};
|
2013-04-13 07:11:39 -05:00
|
|
|
*
|
|
|
|
* fn main () {
|
2013-06-30 17:08:22 -05:00
|
|
|
* let str = [52,32].to_base64(standard);
|
2013-04-14 08:24:13 -05:00
|
|
|
* println(fmt!("%s", str));
|
2013-04-13 07:11:39 -05:00
|
|
|
* }
|
2013-05-27 08:49:54 -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 {
|
|
|
|
let chars = match config.url_safe {
|
|
|
|
true => URLSAFE_CHARS,
|
|
|
|
false => STANDARD_CHARS
|
|
|
|
};
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut s = ~"";
|
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 {
|
|
|
|
s.push_str("\r\n");
|
|
|
|
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.
|
2013-06-30 17:08:22 -05:00
|
|
|
s.push_char(chars[(n >> 18) & 63]);
|
|
|
|
s.push_char(chars[(n >> 12) & 63]);
|
|
|
|
s.push_char(chars[(n >> 6 ) & 63]);
|
|
|
|
s.push_char(chars[n & 63]);
|
|
|
|
|
|
|
|
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 {
|
|
|
|
s.push_str("\r\n");
|
|
|
|
},
|
|
|
|
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;
|
|
|
|
s.push_char(chars[(n >> 18) & 63]);
|
|
|
|
s.push_char(chars[(n >> 12) & 63]);
|
|
|
|
if config.pad {
|
|
|
|
s.push_str("==");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
2 => {
|
|
|
|
let n = (self[i] as u32) << 16 |
|
|
|
|
(self[i + 1u] as u32) << 8;
|
|
|
|
s.push_char(chars[(n >> 18) & 63]);
|
|
|
|
s.push_char(chars[(n >> 12) & 63]);
|
|
|
|
s.push_char(chars[(n >> 6 ) & 63]);
|
|
|
|
if config.pad {
|
|
|
|
s.push_char('=');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => fail!("Algebra is broken, please alert the math police")
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
impl<'self> ToBase64 for &'self str {
|
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
|
|
|
* Convert any string (literal, `@`, `&`, or `~`) to base64 encoding.
|
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-05-27 08:49:54 -05:00
|
|
|
* ~~~ {.rust}
|
2013-06-30 17:08:22 -05:00
|
|
|
* use std::base64::{ToBase64, standard};
|
2013-04-13 07:11:39 -05:00
|
|
|
*
|
|
|
|
* fn main () {
|
2013-06-30 17:08:22 -05:00
|
|
|
* let str = "Hello, World".to_base64(standard);
|
2013-04-14 08:24:13 -05:00
|
|
|
* println(fmt!("%s",str));
|
2013-04-13 07:11:39 -05:00
|
|
|
* }
|
2013-05-27 08:49:54 -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 {
|
|
|
|
self.as_bytes().to_base64(config)
|
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.
|
2013-03-21 23:34:30 -05:00
|
|
|
fn from_base64(&self) -> ~[u8];
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 20:41:05 -05:00
|
|
|
impl<'self> FromBase64 for &'self [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
|
|
|
* Convert base64 `u8` vector into u8 byte values.
|
2013-04-24 02:25:41 -05:00
|
|
|
* Every 4 encoded characters is converted into 3 octets, modulo padding.
|
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-05-27 08:49:54 -05:00
|
|
|
* ~~~ {.rust}
|
2013-06-30 17:08:22 -05:00
|
|
|
* use std::base64::{ToBase64, FromBase64, standard};
|
2013-04-13 07:11:39 -05:00
|
|
|
*
|
|
|
|
* fn main () {
|
2013-06-30 17:08:22 -05:00
|
|
|
* let str = [52,32].to_base64(standard);
|
2013-04-14 08:24:13 -05:00
|
|
|
* println(fmt!("%s", str));
|
|
|
|
* let bytes = str.from_base64();
|
|
|
|
* println(fmt!("%?",bytes));
|
2013-04-13 07:11:39 -05:00
|
|
|
* }
|
2013-05-27 08:49:54 -05:00
|
|
|
* ~~~
|
2013-04-13 07:11:39 -05:00
|
|
|
*/
|
2013-03-21 23:34:30 -05:00
|
|
|
fn from_base64(&self) -> ~[u8] {
|
2013-06-30 17:08:22 -05:00
|
|
|
let mut r = ~[];
|
|
|
|
let mut buf: u32 = 0;
|
|
|
|
let mut modulus = 0;
|
2012-07-03 23:29:45 -05:00
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
let mut it = self.iter();
|
|
|
|
for it.advance |&byte| {
|
|
|
|
let ch = byte as char;
|
|
|
|
let val = byte as u32;
|
2012-07-03 23:29:45 -05:00
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
match ch {
|
|
|
|
'A'..'Z' => buf |= val - 0x41,
|
|
|
|
'a'..'z' => buf |= val - 0x47,
|
|
|
|
'0'..'9' => buf |= val + 0x04,
|
|
|
|
'+'|'-' => buf |= 0x3E,
|
|
|
|
'/'|'_' => buf |= 0x3F,
|
|
|
|
'\r'|'\n' => loop,
|
|
|
|
'=' => break,
|
|
|
|
_ => fail!("Invalid Base64 character")
|
|
|
|
}
|
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-06-30 17:08:22 -05:00
|
|
|
if !it.all(|&byte| {byte as char == '='}) {
|
|
|
|
fail!("Invalid Base64 character");
|
|
|
|
}
|
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 => (),
|
|
|
|
_ => fail!("Invalid Base64 length")
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
2013-06-30 17:08:22 -05:00
|
|
|
|
2012-07-03 23:29:45 -05:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 20:41:05 -05:00
|
|
|
impl<'self> FromBase64 for &'self str {
|
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
|
|
|
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
|
|
|
|
* to the byte values it encodes.
|
2013-04-13 07:11:39 -05:00
|
|
|
*
|
2013-06-28 17:32:26 -05:00
|
|
|
* You can use the `from_bytes` function in `std::str`
|
2013-06-30 17:08:22 -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
|
|
|
*
|
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
|
|
|
* This converts a string literal to base64 and back.
|
2013-04-13 07:11:39 -05:00
|
|
|
*
|
2013-05-27 08:49:54 -05:00
|
|
|
* ~~~ {.rust}
|
2013-06-30 17:08:22 -05:00
|
|
|
* use std::base64::{ToBase64, FromBase64, standard};
|
2013-06-28 17:32:26 -05:00
|
|
|
* use std::str;
|
2013-04-13 07:11:39 -05:00
|
|
|
*
|
|
|
|
* fn main () {
|
2013-06-30 17:08:22 -05:00
|
|
|
* let hello_str = "Hello, World".to_base64(standard);
|
2013-04-14 08:24:13 -05:00
|
|
|
* println(fmt!("%s",hello_str));
|
|
|
|
* let bytes = hello_str.from_base64();
|
|
|
|
* println(fmt!("%?",bytes));
|
|
|
|
* let result_str = str::from_bytes(bytes);
|
|
|
|
* println(fmt!("%s",result_str));
|
2013-04-13 07:11:39 -05:00
|
|
|
* }
|
2013-05-27 08:49:54 -05:00
|
|
|
* ~~~
|
2013-04-13 07:11:39 -05:00
|
|
|
*/
|
2013-03-21 23:34:30 -05:00
|
|
|
fn from_base64(&self) -> ~[u8] {
|
2013-06-10 22:10:37 -05:00
|
|
|
self.as_bytes().from_base64()
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_to_base64_basic() {
|
|
|
|
assert_eq!("".to_base64(standard), ~"");
|
|
|
|
assert_eq!("f".to_base64(standard), ~"Zg==");
|
|
|
|
assert_eq!("fo".to_base64(standard), ~"Zm8=");
|
|
|
|
assert_eq!("foo".to_base64(standard), ~"Zm9v");
|
|
|
|
assert_eq!("foob".to_base64(standard), ~"Zm9vYg==");
|
|
|
|
assert_eq!("fooba".to_base64(standard), ~"Zm9vYmE=");
|
|
|
|
assert_eq!("foobar".to_base64(standard), ~"Zm9vYmFy");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_base64_line_break() {
|
|
|
|
assert!(![0u8, 1000].to_base64(Config {line_length: None, ..standard})
|
|
|
|
.contains("\r\n"));
|
|
|
|
assert_eq!("foobar".to_base64(Config {line_length: Some(4), ..standard}),
|
|
|
|
~"Zm9v\r\nYmFy");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_base64_padding() {
|
|
|
|
assert_eq!("f".to_base64(Config {pad: false, ..standard}), ~"Zg");
|
|
|
|
assert_eq!("fo".to_base64(Config {pad: false, ..standard}), ~"Zm8");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_base64_url_safe() {
|
|
|
|
assert_eq!([251, 255].to_base64(url_safe), ~"-_8");
|
|
|
|
assert_eq!([251, 255].to_base64(standard), ~"+/8=");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_base64_basic() {
|
|
|
|
assert_eq!("".from_base64(), "".as_bytes().to_owned());
|
|
|
|
assert_eq!("Zg==".from_base64(), "f".as_bytes().to_owned());
|
|
|
|
assert_eq!("Zm8=".from_base64(), "fo".as_bytes().to_owned());
|
|
|
|
assert_eq!("Zm9v".from_base64(), "foo".as_bytes().to_owned());
|
|
|
|
assert_eq!("Zm9vYg==".from_base64(), "foob".as_bytes().to_owned());
|
|
|
|
assert_eq!("Zm9vYmE=".from_base64(), "fooba".as_bytes().to_owned());
|
|
|
|
assert_eq!("Zm9vYmFy".from_base64(), "foobar".as_bytes().to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_base64_newlines() {
|
|
|
|
assert_eq!("Zm9v\r\nYmFy".from_base64(), "foobar".as_bytes().to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_base64_urlsafe() {
|
|
|
|
assert_eq!("-_8".from_base64(), "+/8=".from_base64());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_base64_random() {
|
|
|
|
use std::rand::random;
|
|
|
|
use std::vec;
|
2012-07-03 23:29:45 -05:00
|
|
|
|
2013-06-30 17:08:22 -05:00
|
|
|
for 1000.times {
|
|
|
|
let v: ~[u8] = do vec::build |push| {
|
|
|
|
for 100.times {
|
|
|
|
push(random());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(v.to_base64(standard).from_base64(), v);
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
}
|