2014-07-21 05:22:06 +02:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2015-01-06 20:56:21 +01:00
|
|
|
// Copyright (C) 2012-2015 OpenVPN Technologies, Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-05-06 02:39:39 +02:00
|
|
|
#ifndef OPENVPN_COMMON_HASH_H
|
|
|
|
#define OPENVPN_COMMON_HASH_H
|
|
|
|
|
2015-09-24 22:43:38 +02:00
|
|
|
#include <cstring> // for std::strlen
|
2015-04-24 01:55:07 +02:00
|
|
|
#include <cstdint> // for std::uint32_t, uint64_t
|
2015-06-05 05:31:01 +02:00
|
|
|
#include <functional>
|
2014-08-16 06:19:17 +02:00
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2012-05-06 02:39:39 +02:00
|
|
|
|
2015-09-24 22:43:38 +02:00
|
|
|
#define OPENVPN_HASH_METHOD(T, meth) \
|
|
|
|
namespace std { \
|
|
|
|
template <> \
|
|
|
|
struct hash<T> \
|
|
|
|
{ \
|
|
|
|
inline std::size_t operator()(const T& obj) const \
|
|
|
|
{ \
|
|
|
|
return obj.meth(); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
}
|
2015-06-09 19:21:41 +02:00
|
|
|
|
2012-05-06 02:39:39 +02:00
|
|
|
namespace openvpn {
|
2015-06-05 05:31:01 +02:00
|
|
|
namespace Hash {
|
2012-05-06 02:39:39 +02:00
|
|
|
|
2015-09-24 22:43:38 +02:00
|
|
|
void combine_data(std::size_t& seed, const void *data, std::size_t size);
|
2015-06-09 19:21:41 +02:00
|
|
|
|
2015-06-05 05:31:01 +02:00
|
|
|
template <class T>
|
2015-09-24 22:43:38 +02:00
|
|
|
inline void combine(std::size_t& seed, const T& v)
|
2015-06-05 05:31:01 +02:00
|
|
|
{
|
|
|
|
std::hash<T> hasher;
|
|
|
|
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
|
|
|
|
}
|
2012-05-06 02:39:39 +02:00
|
|
|
|
2015-09-24 22:43:38 +02:00
|
|
|
inline void combine(std::size_t& seed, const char *str)
|
|
|
|
{
|
|
|
|
combine_data(seed, str, std::strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline void combine(std::size_t& seed, const T& first, Args... args)
|
2012-05-06 02:39:39 +02:00
|
|
|
{
|
2015-09-24 22:43:38 +02:00
|
|
|
combine(seed, first);
|
|
|
|
combine(seed, args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline std::size_t value(Args... args)
|
|
|
|
{
|
|
|
|
std::size_t hash = 0;
|
|
|
|
combine(hash, args...);
|
|
|
|
return hash;
|
2012-05-06 02:39:39 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 05:31:01 +02:00
|
|
|
// A hasher that combines a data hash with a stateful seed.
|
|
|
|
template <typename T>
|
|
|
|
class InitialSeed
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InitialSeed(std::size_t seed) : seed_(seed) {}
|
2014-08-16 06:19:17 +02:00
|
|
|
|
2015-06-05 05:31:01 +02:00
|
|
|
std::size_t operator()(const T& obj) const
|
2014-08-16 06:19:17 +02:00
|
|
|
{
|
2015-06-05 05:31:01 +02:00
|
|
|
std::size_t seed = seed_;
|
|
|
|
combine(seed, obj);
|
|
|
|
return seed;
|
2014-08-16 06:19:17 +02:00
|
|
|
}
|
2015-06-05 05:31:01 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::size_t seed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void combine_data(std::size_t& seed, const void *data, std::size_t size)
|
|
|
|
{
|
|
|
|
while (size >= sizeof(std::uint32_t))
|
|
|
|
{
|
|
|
|
combine(seed, static_cast<const std::uint32_t*>(data)[0]);
|
|
|
|
data = static_cast<const std::uint8_t*>(data) + sizeof(std::uint32_t);
|
|
|
|
size -= sizeof(std::uint32_t);
|
|
|
|
}
|
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
combine(seed, static_cast<const std::uint8_t*>(data)[0]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
combine(seed, static_cast<const std::uint16_t*>(data)[0]);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
combine(seed, static_cast<const std::uint16_t*>(data)[0]);
|
|
|
|
combine(seed, static_cast<const std::uint8_t*>(data)[2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-16 06:19:17 +02:00
|
|
|
}
|
2012-05-06 02:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|