From 41bb9db24566f15d60d2522eaea6f00cbaabdf4a Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Sat, 31 Aug 2013 11:55:27 +0200
Subject: Move newline handling for Base64 to VCard parser. IBB's Base64 won't
 have newlines.

Change-Id: I700949ea572421972b53539b83c4d04ff4d368f0
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

diff --git a/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp
index eda2547..6436e90 100644
--- a/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp
+++ b/Swiften/Parser/PayloadParsers/UnitTest/VCardParserTest.cpp
@@ -21,6 +21,7 @@ class VCardParserTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST_SUITE(VCardParserTest);
 		CPPUNIT_TEST(testParse);
 		CPPUNIT_TEST(testParse_Photo);
+		CPPUNIT_TEST(testParse_NewlinedPhoto);
 		CPPUNIT_TEST(testParse_Nickname);
 		CPPUNIT_TEST_SUITE_END();
 
@@ -166,6 +167,30 @@ class VCardParserTest : public CppUnit::TestFixture {
 			CPPUNIT_ASSERT_EQUAL(createByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"), payload->getPhoto());
 		}
 
+		void testParse_NewlinedPhoto() {
+			PayloadsParserTester parser;
+
+			CPPUNIT_ASSERT(parser.parse(
+				"<vCard xmlns='vcard-temp'>"
+					"<PHOTO>"
+						"<TYPE>image/jpeg</TYPE>"
+						"<BINVAL>"
+							"dTsETKSAskgu2/BqVO+ogcu3DJy4QATGJqpsa6znWwNGiLnVElVVB6PtS+mTiHUXsrOlKvRjtvzV\n"
+							"VDknNaRF58Elmu5EC6VoCllBEEB/lFf0emYn2gkp0X1khNi75dl+rOj95Ar6XuwLh+ZoSStqwOWj\n"
+							"pIpxmZmVw7E69qr0FY0oI3zcaxXwzHw7Lx9Qf4sH7ufQvIN88ga+hwp8MiXevh3Ac8pN00kgINlq\n"
+							"9AY/bYJL418Y/6wWsJbgmrJ/N78wSMpC7VVszLBZVv8uFnupubyi8Ophd/1wIWWzPPwAbBhepWVb\n"
+							"1oPiFEBT5MNKCMTPEi0npXtedVz0HQbbPNIVwmo="
+						"</BINVAL>"
+					"</PHOTO>"
+				"</vCard>"));
+
+			VCard* payload = dynamic_cast<VCard*>(parser.getPayload().get());
+			CPPUNIT_ASSERT_EQUAL(std::string("image/jpeg"), payload->getPhotoType());
+			CPPUNIT_ASSERT_EQUAL(createByteArray("\x75\x3B\x04\x4C\xA4\x80\xB2\x48\x2E\xDB\xF0\x6A\x54\xEF\xA8\x81\xCB\xB7\x0C\x9C\xB8\x40\x04\xC6\x26\xAA\x6C\x6B\xAC\xE7\x5B\x03\x46\x88\xB9\xD5\x12\x55\x55\x07\xA3\xED\x4B\xE9\x93\x88\x75\x17\xB2\xB3\xA5\x2A\xF4\x63\xB6\xFC\xD5\x54\x39\x27\x35\xA4\x45\xE7\xC1\x25\x9A\xEE\x44\x0B\xA5\x68\x0A\x59\x41\x10\x40\x7F\x94\x57\xF4\x7A\x66\x27\xDA\x09\x29\xD1\x7D\x64\x84\xD8\xBB\xE5\xD9\x7E\xAC\xE8\xFD\xE4\x0A\xFA\x5E\xEC\x0B\x87\xE6\x68\x49\x2B\x6A\xC0\xE5\xA3\xA4\x8A\x71\x99\x99\x95\xC3\xB1\x3A\xF6\xAA\xF4\x15\x8D\x28\x23\x7C\xDC\x6B\x15\xF0\xCC\x7C\x3B\x2F\x1F\x50\x7F\x8B\x07\xEE\xE7\xD0\xBC\x83\x7C\xF2\x06\xBE\x87\x0A\x7C\x32\x25\xDE\xBE\x1D\xC0\x73\xCA\x4D\xD3\x49\x20\x20\xD9\x6A\xF4\x06\x3F\x6D\x82\x4B\xE3\x5F\x18\xFF\xAC\x16\xB0\x96\xE0\x9A\xB2\x7F\x37\xBF\x30\x48\xCA\x42\xED\x55\x6C\xCC\xB0\x59\x56\xFF\x2E\x16\x7B\xA9\xB9\xBC\xA2\xF0\xEA\x61\x77\xFD\x70\x21\x65\xB3\x3C\xFC\x00\x6C\x18\x5E\xA5\x65\x5B\xD6\x83\xE2\x14\x40\x53\xE4\xC3\x4A\x08\xC4\xCF\x12\x2D\x27\xA5\x7B\x5E\x75\x5C\xF4\x1D\x06\xDB\x3C\xD2\x15\xC2\x6A", 257), payload->getPhoto());
+		}
+
+
+
 		void testParse_Nickname() {
 			PayloadsParserTester parser;
 
diff --git a/Swiften/Parser/PayloadParsers/VCardParser.cpp b/Swiften/Parser/PayloadParsers/VCardParser.cpp
index 620a307..0028411 100644
--- a/Swiften/Parser/PayloadParsers/VCardParser.cpp
+++ b/Swiften/Parser/PayloadParsers/VCardParser.cpp
@@ -81,6 +81,8 @@ void VCardParser::handleEndElement(const std::string& element, const std::string
 		getPayloadInternal()->setPhotoType(currentText_);
 	}
 	else if (elementHierarchy == "/vCard/PHOTO/BINVAL") {
+		currentText_.erase(std::remove(currentText_.begin(), currentText_.end(), '\n'), currentText_.end());
+		currentText_.erase(std::remove(currentText_.begin(), currentText_.end(), '\r'), currentText_.end());
 		getPayloadInternal()->setPhoto(Base64::decode(currentText_));
 	}
 	else if (elementHierarchy == "/vCard/PHOTO") {
diff --git a/Swiften/StringCodecs/Base64.cpp b/Swiften/StringCodecs/Base64.cpp
index ec6f960..e1d70a0 100644
--- a/Swiften/StringCodecs/Base64.cpp
+++ b/Swiften/StringCodecs/Base64.cpp
@@ -70,11 +70,8 @@ SafeByteArray Base64::encode(const SafeByteArray& s) {
 	return encodeDetail<SafeByteArray>(s);
 }
 
-ByteArray Base64::decode(const std::string& input1) {
+ByteArray Base64::decode(const std::string& input) {
 	ByteArray result;
-	std::string input(input1);
-	input.erase(std::remove(input.begin(), input.end(), '\n'), input.end());
-	input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
 
 	if (input.size() % 4) {
 		return ByteArray();
diff --git a/Swiften/StringCodecs/UnitTest/Base64Test.cpp b/Swiften/StringCodecs/UnitTest/Base64Test.cpp
index d7703d9..4005047 100644
--- a/Swiften/StringCodecs/UnitTest/Base64Test.cpp
+++ b/Swiften/StringCodecs/UnitTest/Base64Test.cpp
@@ -21,7 +21,6 @@ class Base64Test : public CppUnit::TestFixture {
 		CPPUNIT_TEST(testEncodeDecodeTwoBytesPadding);
 		CPPUNIT_TEST(testEncode_NoData);
 		CPPUNIT_TEST(testDecode_NoData);
-		CPPUNIT_TEST(testDecode_DataWithNewlines);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -63,15 +62,6 @@ class Base64Test : public CppUnit::TestFixture {
 			ByteArray result(Base64::decode(""));
 			CPPUNIT_ASSERT_EQUAL(ByteArray(), result);
 		}
-
-		void testDecode_DataWithNewlines() {
-			ByteArray result(Base64::decode("dTsETKSAskgu2/BqVO+ogcu3DJy4QATGJqpsa6znWwNGiLnVElVVB6PtS+mTiHUXsrOlKvRjtvzV\n"
-											"VDknNaRF58Elmu5EC6VoCllBEEB/lFf0emYn2gkp0X1khNi75dl+rOj95Ar6XuwLh+ZoSStqwOWj\n"
-											"pIpxmZmVw7E69qr0FY0oI3zcaxXwzHw7Lx9Qf4sH7ufQvIN88ga+hwp8MiXevh3Ac8pN00kgINlq\n"
-											"9AY/bYJL418Y/6wWsJbgmrJ/N78wSMpC7VVszLBZVv8uFnupubyi8Ophd/1wIWWzPPwAbBhepWVb\n"
-											"1oPiFEBT5MNKCMTPEi0npXtedVz0HQbbPNIVwmo="));
-			CPPUNIT_ASSERT_EQUAL(createByteArray("\x75\x3B\x04\x4C\xA4\x80\xB2\x48\x2E\xDB\xF0\x6A\x54\xEF\xA8\x81\xCB\xB7\x0C\x9C\xB8\x40\x04\xC6\x26\xAA\x6C\x6B\xAC\xE7\x5B\x03\x46\x88\xB9\xD5\x12\x55\x55\x07\xA3\xED\x4B\xE9\x93\x88\x75\x17\xB2\xB3\xA5\x2A\xF4\x63\xB6\xFC\xD5\x54\x39\x27\x35\xA4\x45\xE7\xC1\x25\x9A\xEE\x44\x0B\xA5\x68\x0A\x59\x41\x10\x40\x7F\x94\x57\xF4\x7A\x66\x27\xDA\x09\x29\xD1\x7D\x64\x84\xD8\xBB\xE5\xD9\x7E\xAC\xE8\xFD\xE4\x0A\xFA\x5E\xEC\x0B\x87\xE6\x68\x49\x2B\x6A\xC0\xE5\xA3\xA4\x8A\x71\x99\x99\x95\xC3\xB1\x3A\xF6\xAA\xF4\x15\x8D\x28\x23\x7C\xDC\x6B\x15\xF0\xCC\x7C\x3B\x2F\x1F\x50\x7F\x8B\x07\xEE\xE7\xD0\xBC\x83\x7C\xF2\x06\xBE\x87\x0A\x7C\x32\x25\xDE\xBE\x1D\xC0\x73\xCA\x4D\xD3\x49\x20\x20\xD9\x6A\xF4\x06\x3F\x6D\x82\x4B\xE3\x5F\x18\xFF\xAC\x16\xB0\x96\xE0\x9A\xB2\x7F\x37\xBF\x30\x48\xCA\x42\xED\x55\x6C\xCC\xB0\x59\x56\xFF\x2E\x16\x7B\xA9\xB9\xBC\xA2\xF0\xEA\x61\x77\xFD\x70\x21\x65\xB3\x3C\xFC\x00\x6C\x18\x5E\xA5\x65\x5B\xD6\x83\xE2\x14\x40\x53\xE4\xC3\x4A\x08\xC4\xCF\x12\x2D\x27\xA5\x7B\x5E\x75\x5C\xF4\x1D\x06\xDB\x3C\xD2\x15\xC2\x6A", 257), result);
-		}
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test);
-- 
cgit v0.10.2-6-g49f6