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-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::str;
|
|
|
|
use core::vec;
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// A trait for converting a value to base64 encoding.
|
2012-09-27 18:43:15 -05:00
|
|
|
pub trait ToBase64 {
|
2013-05-28 22:11:41 -05:00
|
|
|
/// Converts the value of `self` to a base64 value, returning the owned
|
|
|
|
/// string
|
2013-03-21 23:34:30 -05:00
|
|
|
fn to_base64(&self) -> ~str;
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
|
2013-03-26 22:52:16 -05:00
|
|
|
static 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-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-04-13 07:11:39 -05:00
|
|
|
* extern mod std;
|
|
|
|
* use std::base64::ToBase64;
|
|
|
|
*
|
|
|
|
* fn main () {
|
2013-04-14 08:24:13 -05:00
|
|
|
* let str = [52,32].to_base64();
|
|
|
|
* 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-03-21 23:34:30 -05:00
|
|
|
fn to_base64(&self) -> ~str {
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut s = ~"";
|
2013-04-08 15:50:34 -05:00
|
|
|
let len = self.len();
|
|
|
|
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
|
|
|
|
|
|
|
|
let mut i = 0u;
|
|
|
|
|
|
|
|
while i < len - (len % 3u) {
|
|
|
|
let n = (self[i] as uint) << 16u |
|
|
|
|
(self[i + 1u] as uint) << 8u |
|
|
|
|
(self[i + 2u] as uint);
|
|
|
|
|
|
|
|
// This 24-bit number gets separated into four 6-bit numbers.
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 6u) & 63u]);
|
|
|
|
str::push_char(&mut s, CHARS[n & 63u]);
|
|
|
|
|
|
|
|
i += 3u;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Heh, would be cool if we knew this was exhaustive
|
|
|
|
// (the dream of bounded integer types)
|
|
|
|
match len % 3 {
|
|
|
|
0 => (),
|
|
|
|
1 => {
|
|
|
|
let n = (self[i] as uint) << 16u;
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
|
|
|
|
str::push_char(&mut s, '=');
|
|
|
|
str::push_char(&mut s, '=');
|
|
|
|
}
|
|
|
|
2 => {
|
|
|
|
let n = (self[i] as uint) << 16u |
|
|
|
|
(self[i + 1u] as uint) << 8u;
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 18u) & 63u]);
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 12u) & 63u]);
|
|
|
|
str::push_char(&mut s, CHARS[(n >> 6u) & 63u]);
|
|
|
|
str::push_char(&mut s, '=');
|
|
|
|
}
|
2013-05-05 17:18:51 -05:00
|
|
|
_ => 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-04-13 07:11:39 -05:00
|
|
|
* extern mod std;
|
|
|
|
* use std::base64::ToBase64;
|
|
|
|
*
|
|
|
|
* fn main () {
|
2013-04-14 08:24:13 -05:00
|
|
|
* let str = "Hello, World".to_base64();
|
|
|
|
* 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-03-21 23:34:30 -05:00
|
|
|
fn to_base64(&self) -> ~str {
|
2013-03-07 20:11:09 -06:00
|
|
|
str::to_bytes(*self).to_base64()
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)]
|
2012-09-27 18:43:15 -05:00
|
|
|
pub trait FromBase64 {
|
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-04-13 07:11:39 -05:00
|
|
|
* extern mod std;
|
|
|
|
* use std::base64::ToBase64;
|
|
|
|
* use std::base64::FromBase64;
|
|
|
|
*
|
|
|
|
* fn main () {
|
2013-04-14 08:24:13 -05:00
|
|
|
* let str = [52,32].to_base64();
|
|
|
|
* 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-05-05 17:18:51 -05:00
|
|
|
if self.len() % 4u != 0u { fail!("invalid base64 length"); }
|
2012-07-03 23:29:45 -05:00
|
|
|
|
|
|
|
let len = self.len();
|
|
|
|
let mut padding = 0u;
|
|
|
|
|
|
|
|
if len != 0u {
|
|
|
|
if self[len - 1u] == '=' as u8 { padding += 1u; }
|
|
|
|
if self[len - 2u] == '=' as u8 { padding += 1u; }
|
|
|
|
}
|
|
|
|
|
2012-09-21 20:43:30 -05:00
|
|
|
let mut r = vec::with_capacity((len / 4u) * 3u - padding);
|
2012-07-03 23:29:45 -05:00
|
|
|
|
2013-04-08 15:50:34 -05:00
|
|
|
let mut i = 0u;
|
|
|
|
while i < len {
|
|
|
|
let mut n = 0u;
|
|
|
|
|
2013-04-28 23:03:55 -05:00
|
|
|
for 4u.times {
|
2013-04-08 15:50:34 -05:00
|
|
|
let ch = self[i] as char;
|
|
|
|
n <<= 6u;
|
|
|
|
|
2013-05-08 08:50:15 -05:00
|
|
|
match ch {
|
|
|
|
'A'..'Z' => n |= (ch as uint) - 0x41,
|
|
|
|
'a'..'z' => n |= (ch as uint) - 0x47,
|
|
|
|
'0'..'9' => n |= (ch as uint) + 0x04,
|
|
|
|
'+' => n |= 0x3E,
|
|
|
|
'/' => n |= 0x3F,
|
|
|
|
'=' => {
|
|
|
|
match len - i {
|
|
|
|
1u => {
|
|
|
|
r.push(((n >> 16u) & 0xFFu) as u8);
|
|
|
|
r.push(((n >> 8u ) & 0xFFu) as u8);
|
|
|
|
return copy r;
|
|
|
|
}
|
|
|
|
2u => {
|
|
|
|
r.push(((n >> 10u) & 0xFFu) as u8);
|
|
|
|
return copy r;
|
|
|
|
}
|
2013-05-05 17:18:51 -05:00
|
|
|
_ => fail!("invalid base64 padding")
|
2013-05-08 08:50:15 -05:00
|
|
|
}
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
2013-05-05 17:18:51 -05:00
|
|
|
_ => fail!("invalid base64 character")
|
2013-04-08 15:50:34 -05:00
|
|
|
}
|
2012-11-17 13:00:38 -06:00
|
|
|
|
2013-04-08 15:50:34 -05:00
|
|
|
i += 1u;
|
|
|
|
};
|
2012-11-17 13:00:38 -06:00
|
|
|
|
2013-04-08 15:50:34 -05:00
|
|
|
r.push(((n >> 16u) & 0xFFu) as u8);
|
|
|
|
r.push(((n >> 8u ) & 0xFFu) as u8);
|
|
|
|
r.push(((n ) & 0xFFu) as u8);
|
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
|
|
|
*
|
|
|
|
* You can use the `from_bytes` function in `core::str`
|
|
|
|
* to turn a `[u8]` into a string with characters corresponding to those values.
|
|
|
|
*
|
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-04-13 07:11:39 -05:00
|
|
|
* extern mod std;
|
|
|
|
* use std::base64::ToBase64;
|
|
|
|
* use std::base64::FromBase64;
|
|
|
|
* use core::str;
|
|
|
|
*
|
|
|
|
* fn main () {
|
2013-04-14 08:24:13 -05:00
|
|
|
* let hello_str = "Hello, World".to_base64();
|
|
|
|
* 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-03-07 20:11:09 -06:00
|
|
|
str::to_bytes(*self).from_base64()
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-12-27 20:24:18 -06:00
|
|
|
use core::str;
|
|
|
|
|
2012-07-03 23:29:45 -05:00
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_to_base64() {
|
2013-05-28 20:41:05 -05:00
|
|
|
assert_eq!("".to_base64(), ~"");
|
|
|
|
assert_eq!("f".to_base64(), ~"Zg==");
|
|
|
|
assert_eq!("fo".to_base64(), ~"Zm8=");
|
|
|
|
assert_eq!("foo".to_base64(), ~"Zm9v");
|
|
|
|
assert_eq!("foob".to_base64(), ~"Zm9vYg==");
|
|
|
|
assert_eq!("fooba".to_base64(), ~"Zm9vYmE=");
|
|
|
|
assert_eq!("foobar".to_base64(), ~"Zm9vYmFy");
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2013-04-15 10:08:52 -05:00
|
|
|
fn test_from_base64() {
|
2013-05-28 20:41:05 -05:00
|
|
|
assert_eq!("".from_base64(), str::to_bytes(""));
|
|
|
|
assert_eq!("Zg==".from_base64(), str::to_bytes("f"));
|
|
|
|
assert_eq!("Zm8=".from_base64(), str::to_bytes("fo"));
|
|
|
|
assert_eq!("Zm9v".from_base64(), str::to_bytes("foo"));
|
|
|
|
assert_eq!("Zm9vYg==".from_base64(), str::to_bytes("foob"));
|
|
|
|
assert_eq!("Zm9vYmE=".from_base64(), str::to_bytes("fooba"))
|
|
|
|
assert_eq!("Zm9vYmFy".from_base64(), str::to_bytes("foobar"));
|
2012-07-03 23:29:45 -05:00
|
|
|
}
|
|
|
|
}
|