2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2022-02-05 22:14:25 +00:00
|
|
|
* Copyright (c) 2013-2022, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
#ifndef TAG_H__
|
|
|
|
#define TAG_H__
|
|
|
|
|
|
|
|
#include <boost/static_assert.hpp>
|
|
|
|
#include <string.h>
|
2016-11-14 17:05:44 +00:00
|
|
|
#include <openssl/rand.h>
|
2016-06-28 00:00:00 +00:00
|
|
|
#include "Base.h"
|
|
|
|
|
|
|
|
namespace i2p {
|
|
|
|
namespace data {
|
2020-03-01 10:25:50 +00:00
|
|
|
template<size_t sz>
|
|
|
|
class Tag
|
2016-10-10 21:46:18 +00:00
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
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 !(*this == other); }
|
|
|
|
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
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Randomize()
|
|
|
|
{
|
|
|
|
RAND_bytes(m_Buf, sz);
|
|
|
|
}
|
|
|
|
|
2022-02-05 22:14:25 +00:00
|
|
|
std::string ToBase64 (size_t len = sz) const
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
|
|
|
char str[sz*2];
|
2022-02-05 22:14:25 +00:00
|
|
|
size_t l = i2p::data::ByteStreamToBase64 (m_Buf, len, str, sz*2);
|
2020-03-01 10:25:50 +00:00
|
|
|
return std::string (str, str + l);
|
|
|
|
}
|
|
|
|
|
2022-02-05 22:14:25 +00:00
|
|
|
std::string ToBase32 (size_t len = sz) const
|
2020-03-01 10:25:50 +00:00
|
|
|
{
|
|
|
|
char str[sz*2];
|
2022-02-05 22:14:25 +00:00
|
|
|
size_t l = i2p::data::ByteStreamToBase32 (m_Buf, len, str, sz*2);
|
2020-03-01 10:25:50 +00:00
|
|
|
return std::string (str, str + l);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t FromBase32 (const std::string& s)
|
|
|
|
{
|
|
|
|
return i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t FromBase64 (const std::string& s)
|
|
|
|
{
|
|
|
|
return 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-06-28 00:00:00 +00:00
|
|
|
};
|
|
|
|
} // data
|
|
|
|
} // i2p
|
|
|
|
|
2020-01-28 15:03:51 +00:00
|
|
|
namespace std
|
|
|
|
{
|
2020-03-01 10:25:50 +00:00
|
|
|
// hash for std::unordered_map
|
|
|
|
template<size_t sz> struct hash<i2p::data::Tag<sz> >
|
|
|
|
{
|
|
|
|
size_t operator()(const i2p::data::Tag<sz>& s) const
|
|
|
|
{
|
|
|
|
return s.GetLL ()[0];
|
|
|
|
}
|
|
|
|
};
|
2020-01-28 15:03:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:00:00 +00:00
|
|
|
#endif /* TAG_H__ */
|