auto merge of #11652 : hdima/rust/base64-padding-newlines, r=alexcrichton

Ignore all newline characters in Base64 decoder to make it compatible with other Base64 decoders.

Most of the Base64 decoder implementations ignore all newline characters in the input string. There are some examples:

Python:

```python
>>> "
A
Q
=
=
".decode("base64")
'\x01'
```

Ruby:

```ruby
irb(main):001:0> "
A
Q
=
=
".unpack("m")
=> [""]
```

Erlang:

```erlang
1> base64:decode("
A
Q
=
=
").
<<1>>
```

Moreover some Base64 encoders append newline character at the end of the output string by default:

Python:

```python
>>> "".encode("base64")
'AQ==
'
```

Ruby:

```ruby
irb(main):001:0> [""].pack("m")
=> "AQ==
"
```

So I think it's fairly important for Rust Base64 decoder to accept Base64 inputs even with newline characters in the padding.
This commit is contained in:
bors 2014-01-19 22:31:42 -08:00
commit a0ecb15411

View File

@ -237,8 +237,9 @@ impl<'a> FromBase64 for &'a str {
}
for (idx, byte) in it {
if (byte as char) != '=' {
return Err(InvalidBase64Character(self.char_at(idx), idx));
match byte as char {
'='|'\r'|'\n' => continue,
_ => return Err(InvalidBase64Character(self.char_at(idx), idx)),
}
}
@ -310,6 +311,8 @@ mod test {
fn test_from_base64_newlines() {
assert_eq!("Zm9v\r\nYmFy".from_base64().unwrap(),
"foobar".as_bytes().to_owned());
assert_eq!("Zm9vYg==\r\n".from_base64().unwrap(),
"foob".as_bytes().to_owned());
}
#[test]