1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Base64 binary-to-text encoding
13 /// Available encoding character sets
14 pub enum CharacterSet {
15 /// The standard character set (uses '+' and '/')
17 /// The URL safe character set (uses '-' and '_')
21 /// Contains configuration parameters for to_base64
23 /// Character set to use
24 char_set: CharacterSet,
25 /// True to pad output with '=' characters
27 /// Some(len) to wrap lines at len, None to disable line wrapping
28 line_length: Option<uint>
31 /// Configuration for RFC 4648 standard base64 encoding
32 pub static STANDARD: Config =
33 Config {char_set: Standard, pad: true, line_length: None};
35 /// Configuration for RFC 4648 base64url encoding
36 pub static URL_SAFE: Config =
37 Config {char_set: UrlSafe, pad: false, line_length: None};
39 /// Configuration for RFC 2045 MIME base64 encoding
40 pub static MIME: Config =
41 Config {char_set: Standard, pad: true, line_length: Some(76)};
43 static STANDARD_CHARS: [char, ..64] = [
44 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
45 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
46 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
47 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
48 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
51 static URLSAFE_CHARS: [char, ..64] = [
52 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
53 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
54 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
55 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
56 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
59 /// A trait for converting a value to base64 encoding.
61 /// Converts the value of `self` to a base64 value following the specified
62 /// format configuration, returning the owned string.
63 fn to_base64(&self, config: Config) -> ~str;
66 impl<'self> ToBase64 for &'self [u8] {
68 * Turn a vector of `u8` bytes into a base64 string.
74 * use extra::base64::{ToBase64, standard};
77 * let str = [52,32].to_base64(standard);
78 * printfln!("%s", str);
82 fn to_base64(&self, config: Config) -> ~str {
83 let chars = match config.char_set {
84 Standard => STANDARD_CHARS,
85 UrlSafe => URLSAFE_CHARS
90 let mut cur_length = 0;
92 while i < len - (len % 3) {
93 match config.line_length {
95 if cur_length >= line_length {
102 let n = (self[i] as u32) << 16 |
103 (self[i + 1] as u32) << 8 |
104 (self[i + 2] as u32);
106 // This 24-bit number gets separated into four 6-bit numbers.
107 s.push_char(chars[(n >> 18) & 63]);
108 s.push_char(chars[(n >> 12) & 63]);
109 s.push_char(chars[(n >> 6 ) & 63]);
110 s.push_char(chars[n & 63]);
117 match config.line_length {
119 if cur_length >= line_length {
126 // Heh, would be cool if we knew this was exhaustive
127 // (the dream of bounded integer types)
131 let n = (self[i] as u32) << 16;
132 s.push_char(chars[(n >> 18) & 63]);
133 s.push_char(chars[(n >> 12) & 63]);
139 let n = (self[i] as u32) << 16 |
140 (self[i + 1u] as u32) << 8;
141 s.push_char(chars[(n >> 18) & 63]);
142 s.push_char(chars[(n >> 12) & 63]);
143 s.push_char(chars[(n >> 6 ) & 63]);
148 _ => fail!("Algebra is broken, please alert the math police")
154 impl<'self> ToBase64 for &'self str {
156 * Convert any string (literal, `@`, `&`, or `~`) to base64 encoding.
163 * use extra::base64::{ToBase64, standard};
166 * let str = "Hello, World".to_base64(standard);
167 * printfln!("%s", str);
172 fn to_base64(&self, config: Config) -> ~str {
173 self.as_bytes().to_base64(config)
177 /// A trait for converting from base64 encoded values.
178 pub trait FromBase64 {
179 /// Converts the value of `self`, interpreted as base64 encoded data, into
180 /// an owned vector of bytes, returning the vector.
181 fn from_base64(&self) -> Result<~[u8], ~str>;
184 impl<'self> FromBase64 for &'self [u8] {
186 * Convert base64 `u8` vector into u8 byte values.
187 * Every 4 encoded characters is converted into 3 octets, modulo padding.
193 * use extra::base64::{ToBase64, FromBase64, standard};
196 * let str = [52,32].to_base64(standard);
197 * printfln!("%s", str);
198 * let bytes = str.from_base64();
199 * printfln!("%?", bytes);
203 fn from_base64(&self) -> Result<~[u8], ~str> {
205 let mut buf: u32 = 0;
208 let mut it = self.iter();
210 let ch = byte as char;
211 let val = byte as u32;
214 'A'..'Z' => buf |= val - 0x41,
215 'a'..'z' => buf |= val - 0x47,
216 '0'..'9' => buf |= val + 0x04,
217 '+'|'-' => buf |= 0x3E,
218 '/'|'_' => buf |= 0x3F,
221 _ => return Err(~"Invalid Base64 character")
228 r.push((buf >> 22) as u8);
229 r.push((buf >> 14) as u8);
230 r.push((buf >> 6 ) as u8);
234 if !it.all(|&byte| {byte as char == '='}) {
235 return Err(~"Invalid Base64 character");
240 r.push((buf >> 10) as u8);
243 r.push((buf >> 16) as u8);
244 r.push((buf >> 8 ) as u8);
247 _ => return Err(~"Invalid Base64 length")
254 impl<'self> FromBase64 for &'self str {
256 * Convert any base64 encoded string (literal, `@`, `&`, or `~`)
257 * to the byte values it encodes.
259 * You can use the `from_bytes` function in `std::str`
260 * to turn a `[u8]` into a string with characters corresponding to those
265 * This converts a string literal to base64 and back.
269 * use extra::base64::{ToBase64, FromBase64, standard};
273 * let hello_str = "Hello, World".to_base64(standard);
274 * printfln!("%s", hello_str);
275 * let bytes = hello_str.from_base64();
276 * printfln!("%?", bytes);
277 * let result_str = str::from_bytes(bytes);
278 * printfln!("%s", result_str);
282 fn from_base64(&self) -> Result<~[u8], ~str> {
283 self.as_bytes().from_base64()
289 use test::BenchHarness;
293 fn test_to_base64_basic() {
294 assert_eq!("".to_base64(STANDARD), ~"");
295 assert_eq!("f".to_base64(STANDARD), ~"Zg==");
296 assert_eq!("fo".to_base64(STANDARD), ~"Zm8=");
297 assert_eq!("foo".to_base64(STANDARD), ~"Zm9v");
298 assert_eq!("foob".to_base64(STANDARD), ~"Zm9vYg==");
299 assert_eq!("fooba".to_base64(STANDARD), ~"Zm9vYmE=");
300 assert_eq!("foobar".to_base64(STANDARD), ~"Zm9vYmFy");
304 fn test_to_base64_line_break() {
305 assert!(![0u8, 1000].to_base64(Config {line_length: None, ..STANDARD})
307 assert_eq!("foobar".to_base64(Config {line_length: Some(4), ..STANDARD}),
312 fn test_to_base64_padding() {
313 assert_eq!("f".to_base64(Config {pad: false, ..STANDARD}), ~"Zg");
314 assert_eq!("fo".to_base64(Config {pad: false, ..STANDARD}), ~"Zm8");
318 fn test_to_base64_url_safe() {
319 assert_eq!([251, 255].to_base64(URL_SAFE), ~"-_8");
320 assert_eq!([251, 255].to_base64(STANDARD), ~"+/8=");
324 fn test_from_base64_basic() {
325 assert_eq!("".from_base64().unwrap(), "".as_bytes().to_owned());
326 assert_eq!("Zg==".from_base64().unwrap(), "f".as_bytes().to_owned());
327 assert_eq!("Zm8=".from_base64().unwrap(), "fo".as_bytes().to_owned());
328 assert_eq!("Zm9v".from_base64().unwrap(), "foo".as_bytes().to_owned());
329 assert_eq!("Zm9vYg==".from_base64().unwrap(), "foob".as_bytes().to_owned());
330 assert_eq!("Zm9vYmE=".from_base64().unwrap(), "fooba".as_bytes().to_owned());
331 assert_eq!("Zm9vYmFy".from_base64().unwrap(), "foobar".as_bytes().to_owned());
335 fn test_from_base64_newlines() {
336 assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap(),
337 "foobar".as_bytes().to_owned());
341 fn test_from_base64_urlsafe() {
342 assert_eq!("-_8".from_base64().unwrap(), "+/8=".from_base64().unwrap());
346 fn test_from_base64_invalid_char() {
347 assert!("Zm$=".from_base64().is_err())
348 assert!("Zg==$".from_base64().is_err());
352 fn test_from_base64_invalid_padding() {
353 assert!("Z===".from_base64().is_err());
357 fn test_base64_random() {
358 use std::rand::{task_rng, random, RngUtil};
362 let v: ~[u8] = do vec::build |push| {
363 do task_rng().gen_uint_range(1, 100).times {
367 assert_eq!(v.to_base64(STANDARD).from_base64().unwrap(), v);
372 pub fn to_base64(bh: & mut BenchHarness) {
373 let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
374 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
376 s.to_base64(STANDARD);
378 bh.bytes = s.len() as u64;
382 pub fn from_base64(bh: & mut BenchHarness) {
383 let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
384 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
385 let b = s.to_base64(STANDARD);
389 bh.bytes = b.len() as u64;