From 13fec9bdfcebca92631863187d47ebdbfd5e6880 Mon Sep 17 00:00:00 2001
From: orignal <romakoshelkin@yandex.ru>
Date: Mon, 7 Jul 2014 19:22:19 -0400
Subject: [PATCH] generic tag introduced

---
 Garlic.cpp   |  5 ++--
 Garlic.h     |  3 ++-
 Identity.cpp |  5 ----
 Identity.h   | 70 +++++++++++++++++++++++++++-------------------------
 SSU.h        | 15 +----------
 5 files changed, 41 insertions(+), 57 deletions(-)

diff --git a/Garlic.cpp b/Garlic.cpp
index 85294271..6ac19dfd 100644
--- a/Garlic.cpp
+++ b/Garlic.cpp
@@ -297,8 +297,7 @@ namespace garlic
 		uint8_t * buf = msg->GetPayload ();
 		uint32_t length = be32toh (*(uint32_t *)buf);
 		buf += 4;
-		std::string sessionTag((const char *)buf, 32);
-		auto it = m_SessionTags.find (sessionTag);
+		auto it = m_SessionTags.find (SessionTag(buf));
 		if (it != m_SessionTags.end ())
 		{
 			// existing session
@@ -340,7 +339,7 @@ namespace garlic
 		uint16_t tagCount = be16toh (*(uint16_t *)buf);
 		buf += 2;
 		for (int i = 0; i < tagCount; i++)
-			m_SessionTags[std::string ((const char *)(buf + i*32), 32)] = decryption;
+			m_SessionTags[SessionTag(buf + i*32)] = decryption;
 		buf += tagCount*32;
 		uint32_t payloadSize = be32toh (*(uint32_t *)buf);
 		if (payloadSize > len)
diff --git a/Garlic.h b/Garlic.h
index 7ffb4ce6..6d88f42e 100644
--- a/Garlic.h
+++ b/Garlic.h
@@ -98,6 +98,7 @@ namespace garlic
 			
 		private:
 
+			typedef i2p::data::Tag<32> SessionTag;
 			bool m_IsRunning;
 			std::thread * m_Thread;	
 			i2p::util::Queue<I2NPMessage> m_Queue;
@@ -106,7 +107,7 @@ namespace garlic
 			std::map<uint32_t, GarlicRoutingSession *> m_CreatedSessions; // msgID -> session
 			// incoming session
 			std::list<i2p::crypto::CBCDecryption *> m_SessionDecryptions; // multiple tags refer to one decyption
-			std::map<std::string, i2p::crypto::CBCDecryption *> m_SessionTags; // tag -> decryption
+			std::map<SessionTag, i2p::crypto::CBCDecryption *> m_SessionTags; // tag -> decryption
 	};	
 
 	extern GarlicRouting routing;
diff --git a/Identity.cpp b/Identity.cpp
index 756eaec3..9a67f89b 100644
--- a/Identity.cpp
+++ b/Identity.cpp
@@ -40,11 +40,6 @@ namespace data
 		return *this;
 	}
 
-    bool IdentHash::FromBase32(const std::string& s)
-    {
-            size_t count = Base32ToByteStream(s.c_str(), s.length(), m_Hash, sizeof(m_Hash));
-            return count == sizeof(m_Hash);
-    }
 
 	Keys CreateRandomKeys ()
 	{
diff --git a/Identity.h b/Identity.h
index 9c91e9e6..655c594a 100644
--- a/Identity.h
+++ b/Identity.h
@@ -9,7 +9,41 @@ namespace i2p
 {
 namespace data
 {
-	class IdentHash;
+	template<int sz>
+	class Tag
+	{
+		public:
+
+			Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); };
+			Tag (const Tag<sz>& ) = default;
+#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it
+			Tag (Tag<sz>&& ) = default;
+#endif
+			Tag () = default;
+			
+			Tag<sz>& operator= (const Tag<sz>& ) = default;
+#ifndef _WIN32
+			Tag<sz>& operator= (Tag<sz>&& ) = default;
+#endif
+			
+			uint8_t * operator()() { return m_Buf; };
+			const uint8_t * operator()() const { return m_Buf; };
+
+			operator uint8_t * () { return m_Buf; };
+			operator const uint8_t * () const { return m_Buf; };
+			
+			bool operator== (const Tag<sz>& other) const { return !memcmp (m_Buf, other.m_Buf, sz); };
+			bool operator< (const Tag<sz>& other) const { return memcmp (m_Buf, other.m_Buf, sz) < 0; };
+
+		private:
+
+			union // 8 bytes alignment
+			{	
+				uint8_t m_Buf[sz];
+				uint64_t ll[sz/8];
+			};		
+	};	
+	typedef Tag<32> IdentHash;
 
 #pragma pack(1)
 
@@ -52,39 +86,7 @@ namespace data
 	};
 	
 #pragma pack()
-
-	class IdentHash
-	{
-		public:
-
-			IdentHash (const uint8_t * hash) { memcpy (m_Hash, hash, 32); };
-			IdentHash (const IdentHash& ) = default;
-#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it
-			IdentHash (IdentHash&& ) = default;
-#endif
-			IdentHash () = default;
-			
-			IdentHash& operator= (const IdentHash& ) = default;
-#ifndef _WIN32
-			IdentHash& operator= (IdentHash&& ) = default;
-#endif
-			
-			uint8_t * operator()() { return m_Hash; };
-			const uint8_t * operator()() const { return m_Hash; };
-
-			operator uint8_t * () { return m_Hash; };
-			operator const uint8_t * () const { return m_Hash; };
-			
-			bool operator== (const IdentHash& other) const { return !memcmp (m_Hash, other.m_Hash, 32); };
-			bool operator< (const IdentHash& other) const { return memcmp (m_Hash, other.m_Hash, 32) < 0; };
-
-            bool FromBase32(const std::string&);
-
-		private:
-
-			uint8_t m_Hash[32];
-	};	
-
+		
 	Keys CreateRandomKeys ();
 	void CreateRandomDHKeysPair (DHKeysPair * keys); // for transport sessions
 
diff --git a/SSU.h b/SSU.h
index b0930370..f938a580 100644
--- a/SSU.h
+++ b/SSU.h
@@ -114,20 +114,7 @@ namespace ssu
 			
 		private:
 
-			union IV
-			{
-				uint8_t buf[16];
-				uint64_t ll[2];
-
-				IV (const IV&) = default;
-				IV (const uint8_t * iv) { memcpy (buf, iv, 16); };
-				bool operator< (const IV& other) const
-				{
-					if (ll[0] != other.ll[0]) return ll[0] < other.ll[0];
-					return ll[1] < other.ll[1];
-				};
-			};			
-
+			typedef i2p::data::Tag<16> IV;			
 			friend class SSUData; // TODO: change in later
 			SSUServer& m_Server;
 			boost::asio::ip::udp::endpoint m_RemoteEndpoint;