From 99cde8482efaed4757422d24cd46de8fa64c92cb Mon Sep 17 00:00:00 2001 From: Dmitry Vasiliev Date: Sat, 18 Jan 2014 19:18:44 +0100 Subject: [PATCH] Ignore all newline characters in Base64 decoder Ignore all newline characters in Base64 decoder to make it compatible with other Base64 decoders. --- src/libextra/base64.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libextra/base64.rs b/src/libextra/base64.rs index 6f9475d091ad..1043f700aa7e 100644 --- a/src/libextra/base64.rs +++ b/src/libextra/base64.rs @@ -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]