From 10334c139670861d4860da59ad837fc3fe6fd41e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Fri, 3 Jun 2011 13:09:08 +0200
Subject: Ensure safety on onDataRead and onDataWritten signals.


diff --git a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h
index 3cd0947..caec38c 100644
--- a/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h
+++ b/Swift/Controllers/UIInterfaces/XMLConsoleWidget.h
@@ -6,15 +6,15 @@
 
 #pragma once
 
-#include <string>
+#include <Swiften/Base/SafeByteArray.h>
 
 namespace Swift {
 	class XMLConsoleWidget {
 		public:
 			virtual ~XMLConsoleWidget();
 
-			virtual void handleDataRead(const std::string& data) = 0;
-			virtual void handleDataWritten(const std::string& data) = 0;
+			virtual void handleDataRead(const SafeByteArray& data) = 0;
+			virtual void handleDataWritten(const SafeByteArray& data) = 0;
 
 			virtual void show() = 0;
 			virtual void activate() = 0;
diff --git a/Swift/Controllers/XMLConsoleController.cpp b/Swift/Controllers/XMLConsoleController.cpp
index a3510d1..d21f312 100644
--- a/Swift/Controllers/XMLConsoleController.cpp
+++ b/Swift/Controllers/XMLConsoleController.cpp
@@ -30,13 +30,13 @@ void XMLConsoleController::handleUIEvent(boost::shared_ptr<UIEvent> rawEvent) {
 	}
 }
 
-void XMLConsoleController::handleDataRead(const std::string& data) {
+void XMLConsoleController::handleDataRead(const SafeByteArray& data) {
 	if (xmlConsoleWidget) {
 		xmlConsoleWidget->handleDataRead(data);
 	}
 }
 
-void XMLConsoleController::handleDataWritten(const std::string& data) {
+void XMLConsoleController::handleDataWritten(const SafeByteArray& data) {
 	if (xmlConsoleWidget) {
 		xmlConsoleWidget->handleDataWritten(data);
 	}
diff --git a/Swift/Controllers/XMLConsoleController.h b/Swift/Controllers/XMLConsoleController.h
index d12982f..6426a85 100644
--- a/Swift/Controllers/XMLConsoleController.h
+++ b/Swift/Controllers/XMLConsoleController.h
@@ -11,6 +11,7 @@
 #include <boost/shared_ptr.hpp>
 
 #include "Swift/Controllers/UIEvents/UIEventStream.h"
+#include <Swiften/Base/SafeByteArray.h>
 
 namespace Swift {
 	
@@ -23,8 +24,8 @@ namespace Swift {
 			~XMLConsoleController();
 
 		public:
-			void handleDataRead(const std::string& data);
-			void handleDataWritten(const std::string& data);
+			void handleDataRead(const SafeByteArray& data);
+			void handleDataWritten(const SafeByteArray& data);
 
 		private:
 			void handleUIEvent(boost::shared_ptr<UIEvent> event);
diff --git a/Swift/QtUI/QtXMLConsoleWidget.cpp b/Swift/QtUI/QtXMLConsoleWidget.cpp
index c1b1d0d..b0c0385 100644
--- a/Swift/QtUI/QtXMLConsoleWidget.cpp
+++ b/Swift/QtUI/QtXMLConsoleWidget.cpp
@@ -71,12 +71,12 @@ void QtXMLConsoleWidget::closeEvent(QCloseEvent* event) {
 	event->accept();
 }
 
-void QtXMLConsoleWidget::handleDataRead(const std::string& data) {
-	appendTextIfEnabled(std::string(tr("<!-- IN -->").toUtf8()) + "\n" + data + "\n", QColor(33,98,33));
+void QtXMLConsoleWidget::handleDataRead(const SafeByteArray& data) {
+	appendTextIfEnabled(std::string(tr("<!-- IN -->").toUtf8()) + "\n" + safeByteArrayToString(data) + "\n", QColor(33,98,33));
 }
 
-void QtXMLConsoleWidget::handleDataWritten(const std::string& data) {
-	appendTextIfEnabled(std::string(tr("<!-- OUT -->").toUtf8()) + "\n" + data + "\n", QColor(155,1,0));
+void QtXMLConsoleWidget::handleDataWritten(const SafeByteArray& data) {
+	appendTextIfEnabled(std::string(tr("<!-- OUT -->").toUtf8()) + "\n" + safeByteArrayToString(data) + "\n", QColor(155,1,0));
 }
 
 void QtXMLConsoleWidget::appendTextIfEnabled(const std::string& data, const QColor& color) {
diff --git a/Swift/QtUI/QtXMLConsoleWidget.h b/Swift/QtUI/QtXMLConsoleWidget.h
index a345495..73a3bad 100644
--- a/Swift/QtUI/QtXMLConsoleWidget.h
+++ b/Swift/QtUI/QtXMLConsoleWidget.h
@@ -23,8 +23,8 @@ namespace Swift {
 			void show();
 			void activate();
 
-			virtual void handleDataRead(const std::string& data);
-			virtual void handleDataWritten(const std::string& data);
+			virtual void handleDataRead(const SafeByteArray& data);
+			virtual void handleDataWritten(const SafeByteArray& data);
 
 		private:
 			virtual void closeEvent(QCloseEvent* event);
diff --git a/Swiften/Base/SafeByteArray.h b/Swiften/Base/SafeByteArray.h
index 832b116..2f3fbfa 100644
--- a/Swiften/Base/SafeByteArray.h
+++ b/Swiften/Base/SafeByteArray.h
@@ -39,5 +39,11 @@ namespace Swift {
 	}
 
 	SafeByteArray createSafeByteArray(const SafeString& s);
+
+	/* WARNING! This breaks the safety of the data in the safe byte array.
+	 * Do not use in modes that require data safety. */
+	inline std::string safeByteArrayToString(const SafeByteArray& b) {
+		return byteArrayToString(ByteArray(b.begin(), b.end()));
+	}
 }
 
diff --git a/Swiften/Base/SafeString.h b/Swiften/Base/SafeString.h
index bb6d6c7..d549bd1 100644
--- a/Swiften/Base/SafeString.h
+++ b/Swiften/Base/SafeString.h
@@ -26,6 +26,8 @@ namespace Swift {
 
 			SafeString(const char*);
 
+			SafeString(const char* begin, const char* end) : data(begin, end) {
+			}
 
 			std::string toString() const {
 				return data.empty() ? std::string() : std::string(&data[0], data.size());
diff --git a/Swiften/Client/ClientXMLTracer.cpp b/Swiften/Client/ClientXMLTracer.cpp
index a26ce66..441da9b 100644
--- a/Swiften/Client/ClientXMLTracer.cpp
+++ b/Swiften/Client/ClientXMLTracer.cpp
@@ -9,6 +9,8 @@
 #include <iostream>
 #include <boost/bind.hpp>
 
+#include <Swiften/Base/SafeString.h>
+
 namespace Swift {
 
 ClientXMLTracer::ClientXMLTracer(CoreClient* client) {
@@ -16,9 +18,9 @@ ClientXMLTracer::ClientXMLTracer(CoreClient* client) {
 	client->onDataWritten.connect(boost::bind(&ClientXMLTracer::printData, '>', _1));
 }
 
-void ClientXMLTracer::printData(char direction, const std::string& data) {
+void ClientXMLTracer::printData(char direction, const SafeByteArray& data) {
 	printLine(direction);
-	std::cerr << data << std::endl;
+	std::cerr << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
 }
 
 void ClientXMLTracer::printLine(char c) {
diff --git a/Swiften/Client/ClientXMLTracer.h b/Swiften/Client/ClientXMLTracer.h
index 617c53f..dd94e0e 100644
--- a/Swiften/Client/ClientXMLTracer.h
+++ b/Swiften/Client/ClientXMLTracer.h
@@ -7,6 +7,7 @@
 #pragma once
 
 #include <Swiften/Client/CoreClient.h>
+#include <Swiften/Base/SafeByteArray.h>
 
 namespace Swift {
 	class ClientXMLTracer {
@@ -14,7 +15,7 @@ namespace Swift {
 			ClientXMLTracer(CoreClient* client);
 
 		private:
-			static void printData(char direction, const std::string& data);
+			static void printData(char direction, const SafeByteArray& data);
 			static void printLine(char c);
 	};
 }
diff --git a/Swiften/Client/CoreClient.cpp b/Swiften/Client/CoreClient.cpp
index 9907f5d..9eec7ca 100644
--- a/Swiften/Client/CoreClient.cpp
+++ b/Swiften/Client/CoreClient.cpp
@@ -260,11 +260,11 @@ void CoreClient::handleNeedCredentials() {
 	}
 }
 
-void CoreClient::handleDataRead(const std::string& data) {
+void CoreClient::handleDataRead(const SafeByteArray& data) {
 	onDataRead(data);
 }
 
-void CoreClient::handleDataWritten(const std::string& data) {
+void CoreClient::handleDataWritten(const SafeByteArray& data) {
 	onDataWritten(data);
 }
 
diff --git a/Swiften/Client/CoreClient.h b/Swiften/Client/CoreClient.h
index 9806d35..841c32c 100644
--- a/Swiften/Client/CoreClient.h
+++ b/Swiften/Client/CoreClient.h
@@ -15,6 +15,7 @@
 #include <Swiften/Client/ClientError.h>
 #include <Swiften/Client/ClientOptions.h>
 #include <Swiften/Base/SafeString.h>
+#include <Swiften/Base/SafeByteArray.h>
 
 namespace Swift {
 	class ChainedConnector;
@@ -156,7 +157,7 @@ namespace Swift {
 			 * This signal is emitted before the XML data is parsed,
 			 * so this data is unformatted.
 			 */
-			boost::signal<void (const std::string&)> onDataRead;
+			boost::signal<void (const SafeByteArray&)> onDataRead;
 
 			/**
 			 * Emitted when the client sends data.
@@ -164,7 +165,7 @@ namespace Swift {
 			 * This signal is emitted after the XML was serialized, and 
 			 * is unformatted.
 			 */
-			boost::signal<void (const std::string&)> onDataWritten;
+			boost::signal<void (const SafeByteArray&)> onDataWritten;
 
 			/**
 			 * Emitted when a message is received.
@@ -194,8 +195,8 @@ namespace Swift {
 			void handleStanzaChannelAvailableChanged(bool available);
 			void handleSessionFinished(boost::shared_ptr<Error>);
 			void handleNeedCredentials();
-			void handleDataRead(const std::string&);
-			void handleDataWritten(const std::string&);
+			void handleDataRead(const SafeByteArray&);
+			void handleDataWritten(const SafeByteArray&);
 			void handlePresenceReceived(boost::shared_ptr<Presence>);
 			void handleMessageReceived(boost::shared_ptr<Message>);
 			void handleStanzaAcked(boost::shared_ptr<Stanza>);
diff --git a/Swiften/Component/ComponentXMLTracer.cpp b/Swiften/Component/ComponentXMLTracer.cpp
index b952c29..d77eef7 100644
--- a/Swiften/Component/ComponentXMLTracer.cpp
+++ b/Swiften/Component/ComponentXMLTracer.cpp
@@ -16,9 +16,9 @@ ComponentXMLTracer::ComponentXMLTracer(CoreComponent* client) {
 	client->onDataWritten.connect(boost::bind(&ComponentXMLTracer::printData, '>', _1));
 }
 
-void ComponentXMLTracer::printData(char direction, const std::string& data) {
+void ComponentXMLTracer::printData(char direction, const SafeByteArray& data) {
 	printLine(direction);
-	std::cerr << data << std::endl;
+	std::cerr << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
 }
 
 void ComponentXMLTracer::printLine(char c) {
diff --git a/Swiften/Component/ComponentXMLTracer.h b/Swiften/Component/ComponentXMLTracer.h
index 2db690f..c12ec07 100644
--- a/Swiften/Component/ComponentXMLTracer.h
+++ b/Swiften/Component/ComponentXMLTracer.h
@@ -14,7 +14,7 @@ namespace Swift {
 			ComponentXMLTracer(CoreComponent* component);
 
 		private:
-			static void printData(char direction, const std::string& data);
+			static void printData(char direction, const SafeByteArray& data);
 			static void printLine(char c);
 	};
 }
diff --git a/Swiften/Component/CoreComponent.cpp b/Swiften/Component/CoreComponent.cpp
index 7f02768..7ee1ff5 100644
--- a/Swiften/Component/CoreComponent.cpp
+++ b/Swiften/Component/CoreComponent.cpp
@@ -139,11 +139,11 @@ void CoreComponent::handleSessionFinished(boost::shared_ptr<Error> error) {
 	}
 }
 
-void CoreComponent::handleDataRead(const std::string& data) {
+void CoreComponent::handleDataRead(const SafeByteArray& data) {
 	onDataRead(data);
 }
 
-void CoreComponent::handleDataWritten(const std::string& data) {
+void CoreComponent::handleDataWritten(const SafeByteArray& data) {
 	onDataWritten(data);
 }
 
diff --git a/Swiften/Component/CoreComponent.h b/Swiften/Component/CoreComponent.h
index 26f0024..e7945d1 100644
--- a/Swiften/Component/CoreComponent.h
+++ b/Swiften/Component/CoreComponent.h
@@ -22,6 +22,7 @@
 #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h>
 #include <Swiften/Component/ComponentSessionStanzaChannel.h>
 #include <Swiften/Entity/Entity.h>
+#include <Swiften/Base/SafeByteArray.h>
 
 namespace Swift {
 	class IQRouter;
@@ -72,8 +73,8 @@ namespace Swift {
 		public:
 			boost::signal<void (const ComponentError&)> onError;
 			boost::signal<void ()> onConnected;
-			boost::signal<void (const std::string&)> onDataRead;
-			boost::signal<void (const std::string&)> onDataWritten;
+			boost::signal<void (const SafeByteArray&)> onDataRead;
+			boost::signal<void (const SafeByteArray&)> onDataWritten;
 
 			boost::signal<void (boost::shared_ptr<Message>)> onMessageReceived;
 			boost::signal<void (boost::shared_ptr<Presence>) > onPresenceReceived;
@@ -82,8 +83,8 @@ namespace Swift {
 			void handleConnectorFinished(boost::shared_ptr<Connection>);
 			void handleStanzaChannelAvailableChanged(bool available);
 			void handleSessionFinished(boost::shared_ptr<Error>);
-			void handleDataRead(const std::string&);
-			void handleDataWritten(const std::string&);
+			void handleDataRead(const SafeByteArray&);
+			void handleDataWritten(const SafeByteArray&);
 
 		private:
 			EventLoop* eventLoop;
diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp
index a1ec907..d08be4f 100644
--- a/Swiften/Session/BasicSessionStream.cpp
+++ b/Swiften/Session/BasicSessionStream.cpp
@@ -193,11 +193,11 @@ void BasicSessionStream::handleConnectionFinished(const boost::optional<Connecti
 }
 
 void BasicSessionStream::handleDataRead(const SafeByteArray& data) {
-	onDataRead(byteArrayToString(ByteArray(data.begin(), data.end())));
+	onDataRead(data);
 }
 
 void BasicSessionStream::handleDataWritten(const SafeByteArray& data) {
-	onDataWritten(byteArrayToString(ByteArray(data.begin(), data.end())));
+	onDataWritten(data);
 }
 
 };
diff --git a/Swiften/Session/SessionStream.h b/Swiften/Session/SessionStream.h
index 38f5d93..e6b9469 100644
--- a/Swiften/Session/SessionStream.h
+++ b/Swiften/Session/SessionStream.h
@@ -13,6 +13,7 @@
 #include <Swiften/Elements/ProtocolHeader.h>
 #include <Swiften/Elements/Element.h>
 #include <Swiften/Base/Error.h>
+#include <Swiften/Base/SafeByteArray.h>
 #include <Swiften/TLS/PKCS12Certificate.h>
 #include <Swiften/TLS/Certificate.h>
 #include <Swiften/TLS/CertificateVerificationError.h>
@@ -71,8 +72,8 @@ namespace Swift {
 			boost::signal<void (boost::shared_ptr<Element>)> onElementReceived;
 			boost::signal<void (boost::shared_ptr<Error>)> onClosed;
 			boost::signal<void ()> onTLSEncrypted;
-			boost::signal<void (const std::string&)> onDataRead;
-			boost::signal<void (const std::string&)> onDataWritten;
+			boost::signal<void (const SafeByteArray&)> onDataRead;
+			boost::signal<void (const SafeByteArray&)> onDataWritten;
 
 		protected:
 			const PKCS12Certificate& getTLSCertificate() const {
-- 
cgit v0.10.2-6-g49f6