Browse Source

Merge pull request #664 from brain5lug/tag-cleanup

Tag class clean-up
pull/666/head
orignal 8 years ago committed by GitHub
parent
commit
b54892a783
  1. 151
      Tag.h

151
Tag.h

@ -1,3 +1,6 @@
#ifndef TAG_H__
#define TAG_H__
/* /*
* Copyright (c) 2013-2016, The PurpleI2P Project * Copyright (c) 2013-2016, The PurpleI2P Project
* *
@ -6,92 +9,80 @@
* See full license text in LICENSE file at top of project tree * See full license text in LICENSE file at top of project tree
*/ */
#ifndef TAG_H__ #include <boost/static_assert.hpp>
#define TAG_H__ #include <string.h>
#include <string.h> /* memcpy */
#include "Base.h" #include "Base.h"
namespace i2p { namespace i2p {
namespace data { namespace data {
template<int sz>
class Tag template<size_t sz>
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;
Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); }; return true;
Tag (const Tag<sz>& ) = default; }
#ifndef _WIN32 // FIXME!!! msvs 2013 can't compile it
Tag (Tag<sz>&& ) = default; void Fill(uint8_t c)
#endif {
Tag () = default; memset(m_Buf, c, sz);
}
Tag<sz>& operator= (const Tag<sz>& ) = default;
#ifndef _WIN32 std::string ToBase64 () const
Tag<sz>& operator= (Tag<sz>&& ) = default; {
#endif char str[sz*2];
size_t l = i2p::data::ByteStreamToBase64 (m_Buf, sz, str, sz*2);
uint8_t * operator()() { return m_Buf; }; return std::string (str, str + l);
const uint8_t * operator()() const { return m_Buf; }; }
operator uint8_t * () { return m_Buf; }; std::string ToBase32 () const
operator const uint8_t * () const { return m_Buf; }; {
char str[sz*2];
const uint64_t * GetLL () const { return ll; }; size_t l = i2p::data::ByteStreamToBase32 (m_Buf, sz, str, sz*2);
return std::string (str, str + l);
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; };
void FromBase32 (const std::string& s)
bool IsZero () const {
{ i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz);
for (int i = 0; i < sz/8; i++) }
if (ll[i]) return false;
return true; void FromBase64 (const std::string& s)
} {
i2p::data::Base64ToByteStream (s.c_str (), s.length (), m_Buf, sz);
const uint8_t * data() const { return m_Buf; } }
/** fill with a value */ private:
void Fill(uint8_t c)
{ union // 8 bytes aligned
memset(m_Buf, c, sz); {
} uint8_t m_Buf[sz];
uint64_t ll[sz/8];
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 ToBase32 () const
{
char str[sz*2];
int l = i2p::data::ByteStreamToBase32 (m_Buf, sz, str, sz*2);
str[l] = 0;
return std::string (str);
}
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);
}
private:
union // 8 bytes alignment
{
uint8_t m_Buf[sz];
uint64_t ll[sz/8];
};
}; };
};
} // data } // data
} // i2p } // i2p

Loading…
Cancel
Save