i2pd/Tag.h

90 lines
1.8 KiB
C
Raw Normal View History

2016-10-10 21:19:34 +00:00
#ifndef TAG_H__
#define TAG_H__
2016-06-28 13:00:00 +00:00
/*
* Copyright (c) 2013-2016, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2016-10-10 21:19:34 +00:00
#include <boost/static_assert.hpp>
#include <string.h>
#include "Base.h"
namespace i2p {
namespace data {
2016-10-10 21:19:34 +00:00
template<size_t sz>
class Tag
{
BOOST_STATIC_ASSERT_MSG(sz % 8 == 0, "Tag size must be multiple of 8 bytes");
2016-10-10 21:19:34 +00:00
public:
2016-10-10 21:19:34 +00:00
Tag () = default;
Tag (const uint8_t * buf) { memcpy (m_Buf, buf, sz); }
2016-10-10 21:19:34 +00:00
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; }
2016-10-10 21:19:34 +00:00
uint8_t * operator()() { return m_Buf; }
const uint8_t * operator()() const { return m_Buf; }
2016-10-10 21:19:34 +00:00
operator uint8_t * () { return m_Buf; }
operator const uint8_t * () const { return m_Buf; }
2016-10-10 21:19:34 +00:00
const uint8_t * data() const { return m_Buf; }
const uint64_t * GetLL () const { return ll; }
2016-09-03 20:12:43 +00:00
2016-10-10 21:19:34 +00:00
bool IsZero () const
{
for (size_t i = 0; i < sz/8; ++i)
if (ll[i]) return false;
return true;
}
void Fill(uint8_t c)
{
memset(m_Buf, c, sz);
}
2016-10-10 21:19:34 +00:00
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];
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 FromBase64 (const std::string& s)
{
i2p::data::Base64ToByteStream (s.c_str (), s.length (), m_Buf, sz);
}
private:
union // 8 bytes aligned
{
uint8_t m_Buf[sz];
uint64_t ll[sz/8];
};
2016-10-10 21:19:34 +00:00
};
} // data
} // i2p
#endif /* TAG_H__ */