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 // ignore-lexer-test FIXME #15679
13 //! Base64 binary-to-text encoding
15 pub use self::FromBase64Error::*;
16 pub use self::CharacterSet::*;
21 /// Available encoding character sets
22 pub enum CharacterSet {
23 /// The standard character set (uses `+` and `/`)
25 /// The URL safe character set (uses `-` and `_`)
29 /// Contains configuration parameters for `to_base64`.
31 /// Character set to use
32 pub char_set: CharacterSet,
33 /// True to pad output with `=` characters
35 /// `Some(len)` to wrap lines at `len`, `None` to disable line wrapping
36 pub line_length: Option<uint>
39 /// Configuration for RFC 4648 standard base64 encoding
40 pub static STANDARD: Config =
41 Config {char_set: Standard, pad: true, line_length: None};
43 /// Configuration for RFC 4648 base64url encoding
44 pub static URL_SAFE: Config =
45 Config {char_set: UrlSafe, pad: false, line_length: None};
47 /// Configuration for RFC 2045 MIME base64 encoding
48 pub static MIME: Config =
49 Config {char_set: Standard, pad: true, line_length: Some(76)};
51 static STANDARD_CHARS: &'static[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
52 abcdefghijklmnopqrstuvwxyz\
55 static URLSAFE_CHARS: &'static[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
56 abcdefghijklmnopqrstuvwxyz\
59 /// A trait for converting a value to base64 encoding.
60 pub trait ToBase64 for Sized? {
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) -> String;
66 impl ToBase64 for [u8] {
67 /// Turn a vector of `u8` bytes into a base64 string.
72 /// extern crate serialize;
73 /// use serialize::base64::{ToBase64, STANDARD};
76 /// let str = [52,32].to_base64(STANDARD);
77 /// println!("base 64 output: {}", str);
80 fn to_base64(&self, config: Config) -> String {
81 let bytes = match config.char_set {
82 Standard => STANDARD_CHARS,
83 UrlSafe => URLSAFE_CHARS
86 let mut v = Vec::new();
88 let mut cur_length = 0;
90 while i < len - (len % 3) {
91 match config.line_length {
93 if cur_length >= line_length {
101 let n = (self[i] as u32) << 16 |
102 (self[i + 1] as u32) << 8 |
103 (self[i + 2] as u32);
105 // This 24-bit number gets separated into four 6-bit numbers.
106 v.push(bytes[((n >> 18) & 63) as uint]);
107 v.push(bytes[((n >> 12) & 63) as uint]);
108 v.push(bytes[((n >> 6 ) & 63) as uint]);
109 v.push(bytes[(n & 63) as uint]);
116 match config.line_length {
118 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 v.push(bytes[((n >> 18) & 63) as uint]);
133 v.push(bytes[((n >> 12) & 63) as uint]);
140 let n = (self[i] as u32) << 16 |
141 (self[i + 1u] as u32) << 8;
142 v.push(bytes[((n >> 18) & 63) as uint]);
143 v.push(bytes[((n >> 12) & 63) as uint]);
144 v.push(bytes[((n >> 6 ) & 63) as uint]);
149 _ => panic!("Algebra is broken, please alert the math police")
152 unsafe { String::from_utf8_unchecked(v) }
156 /// A trait for converting from base64 encoded values.
157 pub trait FromBase64 for Sized? {
158 /// Converts the value of `self`, interpreted as base64 encoded data, into
159 /// an owned vector of bytes, returning the vector.
160 fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error>;
163 /// Errors that can occur when decoding a base64 encoded string
164 pub enum FromBase64Error {
165 /// The input contained a character not part of the base64 format
166 InvalidBase64Byte(u8, uint),
167 /// The input had an invalid length
171 impl fmt::Show for FromBase64Error {
172 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
174 InvalidBase64Byte(ch, idx) =>
175 write!(f, "Invalid character '{}' at position {}", ch, idx),
176 InvalidBase64Length => write!(f, "Invalid length"),
181 impl error::Error for FromBase64Error {
182 fn description(&self) -> &str {
184 InvalidBase64Byte(_, _) => "invalid character",
185 InvalidBase64Length => "invalid length",
189 fn detail(&self) -> Option<String> {
190 Some(self.to_string())
194 impl FromBase64 for str {
195 /// Convert any base64 encoded string (literal, `@`, `&`, or `~`)
196 /// to the byte values it encodes.
198 /// You can use the `String::from_utf8` function to turn a `Vec<u8>` into a
199 /// string with characters corresponding to those values.
203 /// This converts a string literal to base64 and back.
206 /// extern crate serialize;
207 /// use serialize::base64::{ToBase64, FromBase64, STANDARD};
210 /// let hello_str = b"Hello, World".to_base64(STANDARD);
211 /// println!("base64 output: {}", hello_str);
212 /// let res = hello_str.as_slice().from_base64();
214 /// let opt_bytes = String::from_utf8(res.unwrap());
215 /// if opt_bytes.is_ok() {
216 /// println!("decoded from base64: {}", opt_bytes.unwrap());
222 fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error> {
223 self.as_bytes().from_base64()
227 impl FromBase64 for [u8] {
228 fn from_base64(&self) -> Result<Vec<u8>, FromBase64Error> {
229 let mut r = Vec::new();
230 let mut buf: u32 = 0;
231 let mut modulus = 0i;
233 let mut it = self.iter().enumerate();
234 for (idx, &byte) in it {
235 let val = byte as u32;
238 b'A'...b'Z' => buf |= val - 0x41,
239 b'a'...b'z' => buf |= val - 0x47,
240 b'0'...b'9' => buf |= val + 0x04,
241 b'+' | b'-' => buf |= 0x3E,
242 b'/' | b'_' => buf |= 0x3F,
243 b'\r' | b'\n' => continue,
245 _ => return Err(InvalidBase64Byte(self[idx], idx)),
252 r.push((buf >> 22) as u8);
253 r.push((buf >> 14) as u8);
254 r.push((buf >> 6 ) as u8);
258 for (idx, &byte) in it {
260 b'=' | b'\r' | b'\n' => continue,
261 _ => return Err(InvalidBase64Byte(self[idx], idx)),
267 r.push((buf >> 10) as u8);
270 r.push((buf >> 16) as u8);
271 r.push((buf >> 8 ) as u8);
274 _ => return Err(InvalidBase64Length),
284 use self::test::Bencher;
285 use base64::{Config, FromBase64, ToBase64, STANDARD, URL_SAFE};
288 fn test_to_base64_basic() {
289 assert_eq!("".as_bytes().to_base64(STANDARD), "".to_string());
290 assert_eq!("f".as_bytes().to_base64(STANDARD), "Zg==".to_string());
291 assert_eq!("fo".as_bytes().to_base64(STANDARD), "Zm8=".to_string());
292 assert_eq!("foo".as_bytes().to_base64(STANDARD), "Zm9v".to_string());
293 assert_eq!("foob".as_bytes().to_base64(STANDARD), "Zm9vYg==".to_string());
294 assert_eq!("fooba".as_bytes().to_base64(STANDARD), "Zm9vYmE=".to_string());
295 assert_eq!("foobar".as_bytes().to_base64(STANDARD), "Zm9vYmFy".to_string());
299 fn test_to_base64_line_break() {
300 assert!(![0u8, ..1000].to_base64(Config {line_length: None, ..STANDARD})
303 assert_eq!("foobar".as_bytes().to_base64(Config {line_length: Some(4),
305 "Zm9v\r\nYmFy".to_string());
309 fn test_to_base64_padding() {
310 assert_eq!("f".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zg".to_string());
311 assert_eq!("fo".as_bytes().to_base64(Config {pad: false, ..STANDARD}), "Zm8".to_string());
315 fn test_to_base64_url_safe() {
316 assert_eq!([251, 255].to_base64(URL_SAFE), "-_8".to_string());
317 assert_eq!([251, 255].to_base64(STANDARD), "+/8=".to_string());
321 fn test_from_base64_basic() {
322 assert_eq!("".from_base64().unwrap().as_slice(), "".as_bytes());
323 assert_eq!("Zg==".from_base64().unwrap().as_slice(), "f".as_bytes());
324 assert_eq!("Zm8=".from_base64().unwrap().as_slice(), "fo".as_bytes());
325 assert_eq!("Zm9v".from_base64().unwrap().as_slice(), "foo".as_bytes());
326 assert_eq!("Zm9vYg==".from_base64().unwrap().as_slice(), "foob".as_bytes());
327 assert_eq!("Zm9vYmE=".from_base64().unwrap().as_slice(), "fooba".as_bytes());
328 assert_eq!("Zm9vYmFy".from_base64().unwrap().as_slice(), "foobar".as_bytes());
332 fn test_from_base64_bytes() {
333 assert_eq!(b"Zm9vYmFy".from_base64().unwrap().as_slice(), "foobar".as_bytes());
337 fn test_from_base64_newlines() {
338 assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap().as_slice(),
339 "foobar".as_bytes());
340 assert_eq!("Zm9vYg==\r\n".from_base64().unwrap().as_slice(),
345 fn test_from_base64_urlsafe() {
346 assert_eq!("-_8".from_base64().unwrap(), "+/8=".from_base64().unwrap());
350 fn test_from_base64_invalid_char() {
351 assert!("Zm$=".from_base64().is_err())
352 assert!("Zg==$".from_base64().is_err());
356 fn test_from_base64_invalid_padding() {
357 assert!("Z===".from_base64().is_err());
361 fn test_base64_random() {
362 use std::rand::{task_rng, random, Rng};
364 for _ in range(0u, 1000) {
365 let times = task_rng().gen_range(1u, 100);
366 let v = Vec::from_fn(times, |_| random::<u8>());
367 assert_eq!(v.as_slice()
378 pub fn bench_to_base64(b: &mut Bencher) {
379 let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
380 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
382 s.as_bytes().to_base64(STANDARD);
384 b.bytes = s.len() as u64;
388 pub fn bench_from_base64(b: &mut Bencher) {
389 let s = "イロハニホヘト チリヌルヲ ワカヨタレソ ツネナラム \
390 ウヰノオクヤマ ケフコエテ アサキユメミシ ヱヒモセスン";
391 let sb = s.as_bytes().to_base64(STANDARD);
393 sb.as_slice().from_base64().unwrap();
395 b.bytes = sb.len() as u64;