From 141fb78237bd6dd0d4b9dee80093ff8c9a0840fa Mon Sep 17 00:00:00 2001 From: brain5lug Date: Tue, 11 Oct 2016 00:19:34 +0300 Subject: [PATCH 1/2] Tag class clean-up --- Tag.h | 137 +++++++++++++++++++++++++++------------------------------- 1 file changed, 64 insertions(+), 73 deletions(-) diff --git a/Tag.h b/Tag.h index 92e2f1a5..e6e7ffd4 100644 --- a/Tag.h +++ b/Tag.h @@ -1,3 +1,6 @@ +#ifndef TAG_H__ +#define TAG_H__ + /* * Copyright (c) 2013-2016, The PurpleI2P Project * @@ -6,92 +9,80 @@ * See full license text in LICENSE file at top of project tree */ -#ifndef TAG_H__ -#define TAG_H__ - -#include /* memcpy */ - +#include +#include #include "Base.h" namespace i2p { namespace data { - template - class Tag + +template +class Tag +{ + BOOST_STATIC_ASSERT_MSG(sz % 8 == 0, "Tag size must be multiple of 8 bytes"); + +public: + + Tag () = default; + Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); } + + bool operator== (const Tag& other) const { return !memcmp (m_Buf, other.m_Buf, sz); } + bool operator< (const Tag& other) const { return memcmp (m_Buf, other.m_Buf, sz) < 0; } + + 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; } + + const uint8_t * data() const { return m_Buf; } + const uint64_t * GetLL () const { return ll; } + + bool IsZero () const { - public: + for (size_t i = 0; i < sz/8; ++i) + if (ll[i]) return false; + return true; + } - Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); }; - Tag (const Tag& ) = default; -#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it - Tag (Tag&& ) = default; -#endif - Tag () = default; - - Tag& operator= (const Tag& ) = default; -#ifndef _WIN32 - Tag& operator= (Tag&& ) = 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; }; - - const uint64_t * GetLL () const { return ll; }; - - bool operator== (const Tag& other) const { return !memcmp (m_Buf, other.m_Buf, sz); }; - bool operator< (const Tag& other) const { return memcmp (m_Buf, other.m_Buf, sz) < 0; }; - - bool IsZero () const - { - for (int i = 0; i < sz/8; i++) - if (ll[i]) return false; - return true; - } - - const uint8_t * data() const { return m_Buf; } - - /** fill with a value */ - void Fill(uint8_t c) - { - memset(m_Buf, c, sz); - } + void Fill(uint8_t c) + { + memset(m_Buf, c, sz); + } - std::string ToBase64 () const - { - char str[sz*2]; - int l = i2p::data::ByteStreamToBase64 (m_Buf, sz, str, sz*2); - str[l] = 0; - return std::string (str); - } + std::string ToBase64 () const + { + char str[sz*2]; + size_t l = i2p::data::ByteStreamToBase64 (m_Buf, sz, str, sz*2); + return std::string (str, str + l); + } - std::string ToBase32 () const - { - char str[sz*2]; - int l = i2p::data::ByteStreamToBase32 (m_Buf, sz, str, sz*2); - str[l] = 0; - return std::string (str); - } + std::string ToBase32 () const + { + char str[sz*2]; + size_t l = i2p::data::ByteStreamToBase32 (m_Buf, sz, str, sz*2); + return std::string (str, str + l); + } - void FromBase32 (const std::string& s) - { - i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz); - } + void FromBase32 (const std::string& s) + { + i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz); + } - void FromBase64 (const std::string& s) - { - i2p::data::Base64ToByteStream (s.c_str (), s.length (), m_Buf, sz); - } + void FromBase64 (const std::string& s) + { + i2p::data::Base64ToByteStream (s.c_str (), s.length (), m_Buf, sz); + } - private: +private: - union // 8 bytes alignment - { - uint8_t m_Buf[sz]; - uint64_t ll[sz/8]; - }; + union // 8 bytes aligned + { + uint8_t m_Buf[sz]; + uint64_t ll[sz/8]; }; +}; + } // data } // i2p From 16c37a0f3d55a9f138de7d1c1df6bfabfcaa393c Mon Sep 17 00:00:00 2001 From: brain5lug Date: Tue, 11 Oct 2016 00:46:18 +0300 Subject: [PATCH 2/2] indentation fix for missed Fill function --- Tag.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tag.h b/Tag.h index e6e7ffd4..30dfa654 100644 --- a/Tag.h +++ b/Tag.h @@ -45,10 +45,10 @@ public: return true; } - void Fill(uint8_t c) - { - memset(m_Buf, c, sz); - } + void Fill(uint8_t c) + { + memset(m_Buf, c, sz); + } std::string ToBase64 () const {