rust/src/libstd/base64.rs

162 lines
4.8 KiB
Rust
Raw Normal View History

2012-08-31 17:14:01 -05:00
#[forbid(deprecated_mode)];
#[forbid(deprecated_pattern)];
2012-09-04 13:23:53 -05:00
use io::Reader;
2012-07-03 23:29:45 -05:00
2012-08-11 09:08:42 -05:00
trait ToBase64 {
fn to_base64() -> ~str;
2012-07-03 23:29:45 -05:00
}
2012-09-04 00:41:10 -05:00
impl &[u8]: ToBase64 {
fn to_base64() -> ~str {
2012-07-03 23:29:45 -05:00
let chars = str::chars(
~"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
2012-07-03 23:29:45 -05:00
);
let len = self.len();
let mut s = ~"";
2012-09-21 20:36:32 -05:00
str::reserve(&mut s, ((len + 3u) / 4u) * 3u);
2012-07-03 23:29:45 -05:00
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.
2012-09-21 20:36:32 -05:00
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]);
2012-07-03 23:29:45 -05:00
i += 3u;
}
2012-08-15 13:55:17 -05:00
// Heh, would be cool if we knew this was exhaustive
// (the dream of bounded integer types)
match len % 3 {
0 => (),
1 => {
2012-07-03 23:29:45 -05:00
let n = (self[i] as uint) << 16u;
2012-09-21 20:36:32 -05:00
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, '=');
2012-07-03 23:29:45 -05:00
}
2012-08-15 13:55:17 -05:00
2 => {
2012-07-03 23:29:45 -05:00
let n = (self[i] as uint) << 16u | (self[i + 1u] as uint) << 8u;
2012-09-21 20:36:32 -05:00
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, '=');
2012-07-03 23:29:45 -05:00
}
2012-08-15 13:55:17 -05:00
_ => fail ~"Algebra is broken, please alert the math police"
2012-07-03 23:29:45 -05:00
}
s
}
}
2012-09-04 00:41:10 -05:00
impl &str: ToBase64 {
fn to_base64() -> ~str {
str::to_bytes(self).to_base64()
2012-07-03 23:29:45 -05:00
}
}
2012-08-11 09:08:42 -05:00
trait FromBase64 {
2012-07-03 23:29:45 -05:00
fn from_base64() -> ~[u8];
}
2012-08-11 09:08:42 -05:00
impl ~[u8]: FromBase64 {
2012-07-03 23:29:45 -05:00
fn from_base64() -> ~[u8] {
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; }
}
let mut r = vec::with_capacity((len / 4u) * 3u - padding);
2012-07-03 23:29:45 -05:00
let mut i = 0u;
while i < len {
let mut n = 0u;
for iter::repeat(4u) {
let ch = self[i] as char;
n <<= 6u;
if ch >= 'A' && ch <= 'Z' {
n |= (ch as uint) - 0x41u;
} else if ch >= 'a' && ch <= 'z' {
n |= (ch as uint) - 0x47u;
} else if ch >= '0' && ch <= '9' {
n |= (ch as uint) + 0x04u;
} else if ch == '+' {
n |= 0x3Eu;
} else if ch == '/' {
n |= 0x3Fu;
} else if ch == '=' {
2012-08-06 14:34:08 -05:00
match len - i {
2012-08-03 21:59:04 -05:00
1u => {
r.push(((n >> 16u) & 0xFFu) as u8);
r.push(((n >> 8u ) & 0xFFu) as u8);
2012-08-01 19:30:05 -05:00
return copy r;
2012-07-03 23:29:45 -05:00
}
2012-08-03 21:59:04 -05:00
2u => {
r.push(((n >> 10u) & 0xFFu) as u8);
2012-08-01 19:30:05 -05:00
return copy r;
2012-07-03 23:29:45 -05:00
}
2012-08-03 21:59:04 -05:00
_ => fail ~"invalid base64 padding"
2012-07-03 23:29:45 -05:00
}
} else {
fail ~"invalid base64 character";
2012-07-03 23:29:45 -05:00
}
i += 1u;
};
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
}
}
2012-08-11 09:08:42 -05:00
impl ~str: FromBase64 {
2012-07-03 23:29:45 -05:00
fn from_base64() -> ~[u8] {
str::to_bytes(self).from_base64()
2012-07-03 23:29:45 -05:00
}
}
#[cfg(test)]
mod tests {
#[legacy_exports];
2012-07-03 23:29:45 -05:00
#[test]
fn test_to_base64() {
assert (~"").to_base64() == ~"";
assert (~"f").to_base64() == ~"Zg==";
assert (~"fo").to_base64() == ~"Zm8=";
assert (~"foo").to_base64() == ~"Zm9v";
assert (~"foob").to_base64() == ~"Zm9vYg==";
assert (~"fooba").to_base64() == ~"Zm9vYmE=";
assert (~"foobar").to_base64() == ~"Zm9vYmFy";
2012-07-03 23:29:45 -05:00
}
#[test]
fn test_from_base64() {
assert (~"").from_base64() == str::to_bytes(~"");
assert (~"Zg==").from_base64() == str::to_bytes(~"f");
assert (~"Zm8=").from_base64() == str::to_bytes(~"fo");
assert (~"Zm9v").from_base64() == str::to_bytes(~"foo");
assert (~"Zm9vYg==").from_base64() == str::to_bytes(~"foob");
assert (~"Zm9vYmE=").from_base64() == str::to_bytes(~"fooba");
assert (~"Zm9vYmFy").from_base64() == str::to_bytes(~"foobar");
2012-07-03 23:29:45 -05:00
}
}