/* * Copyright (c)2013-2021 ZeroTier, Inc. * * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * * Change Date: 2026-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. */ /****/ #ifndef ZT_CONTAINERS_HPP #define ZT_CONTAINERS_HPP /* This defines a Map, SortedMap, Vector, etc. based on STL templates. */ #include "Constants.hpp" #include "Utils.hpp" #include #include #include #include #include #include #ifdef __CPP11__ #include #include #include #endif namespace ZeroTier { template class Vector : public std::vector { public: ZT_INLINE Vector() : std::vector() {} template ZT_INLINE Vector(I begin, I end) : std::vector(begin, end) {} }; template class List : public std::list { }; #ifdef __CPP11__ struct intl_MapHasher { template std::size_t operator()(const O &obj) const noexcept { return (std::size_t)obj.hashCode(); } std::size_t operator()(const Vector &bytes) const noexcept { return (std::size_t)Utils::fnv1a32(bytes.data(), (unsigned int)bytes.size()); } std::size_t operator()(const uint64_t i) const noexcept { return (std::size_t)Utils::hash64(i ^ Utils::s_mapNonce); } std::size_t operator()(const int64_t i) const noexcept { return (std::size_t)Utils::hash64((uint64_t)i ^ Utils::s_mapNonce); } std::size_t operator()(const uint32_t i) const noexcept { return (std::size_t)Utils::hash32(i ^ (uint32_t)Utils::s_mapNonce); } std::size_t operator()(const int32_t i) const noexcept { return (std::size_t)Utils::hash32((uint32_t)i ^ (uint32_t)Utils::s_mapNonce); } }; template class Map : public std::unordered_map { }; template class MultiMap : public std::unordered_multimap> { }; #else template class Map : public std::map { }; template class MultiMap : public std::multimap { }; #endif template class SortedMap : public std::map { }; #ifdef __CPP11__ template class ForwardList : public std::forward_list { }; #else template class ForwardList : public std::list { }; #endif template class Set : public std::set> { }; typedef std::string String; /** * A 384-bit hash */ struct H384 { uint64_t data[6]; ZT_INLINE H384() noexcept { Utils::zero(data); } ZT_INLINE H384(const H384 &b) noexcept { Utils::copy<48>(data, b.data); } explicit ZT_INLINE H384(const void *const d) noexcept { Utils::copy<48>(data, d); } ZT_INLINE H384 &operator=(const H384 &b) noexcept { Utils::copy<48>(data, b.data); return *this; } ZT_INLINE unsigned long hashCode() const noexcept { return (unsigned long)data[0]; } ZT_INLINE operator bool() const noexcept { return ((data[0] != 0) && (data[1] != 0) && (data[2] != 0) && (data[3] != 0) && (data[4] != 0) && (data[5] != 0)); } ZT_INLINE bool operator==(const H384 &b) const noexcept { return ((data[0] == b.data[0]) && (data[1] == b.data[1]) && (data[2] == b.data[2]) && (data[3] == b.data[3]) && (data[4] == b.data[4]) && (data[5] == b.data[5])); } ZT_INLINE bool operator!=(const H384 &b) const noexcept { return !(*this == b); } ZT_INLINE bool operator<(const H384 &b) const noexcept { return std::lexicographical_compare(data, data + 6, b.data, b.data + 6); } ZT_INLINE bool operator<=(const H384 &b) const noexcept { return !(b < *this); } ZT_INLINE bool operator>(const H384 &b) const noexcept { return (b < *this); } ZT_INLINE bool operator>=(const H384 &b) const noexcept { return !(*this < b); } }; static_assert(sizeof(H384) == 48, "H384 contains unnecessary padding"); /** * A fixed size byte array * * @tparam S Size in bytes */ template struct Blob { uint8_t data[S]; ZT_INLINE Blob() noexcept { Utils::zero(data); } ZT_INLINE Blob(const Blob &b) noexcept { Utils::copy(data, b.data); } explicit ZT_INLINE Blob(const void *const d) noexcept { Utils::copy(data, d); } explicit ZT_INLINE Blob(const void *const d, const unsigned int l) noexcept { Utils::copy(data, d, (l > (unsigned int)S) ? (unsigned int)S : l); if (l < S) { Utils::zero(data + l, S - l); } } ZT_INLINE Blob &operator=(const Blob &b) noexcept { Utils::copy(data, b.data); return *this; } ZT_INLINE unsigned long hashCode() const noexcept { return Utils::fnv1a32(data, (unsigned int)S); } ZT_INLINE operator bool() const noexcept { return Utils::allZero(data, (unsigned int)S); } ZT_INLINE bool operator==(const Blob &b) const noexcept { return (memcmp(data, b.data, S) == 0); } ZT_INLINE bool operator!=(const Blob &b) const noexcept { return (memcmp(data, b.data, S) != 0); } ZT_INLINE bool operator<(const Blob &b) const noexcept { return (memcmp(data, b.data, S) < 0); } ZT_INLINE bool operator<=(const Blob &b) const noexcept { return (memcmp(data, b.data, S) <= 0); } ZT_INLINE bool operator>(const Blob &b) const noexcept { return (memcmp(data, b.data, S) > 0); } ZT_INLINE bool operator>=(const Blob &b) const noexcept { return (memcmp(data, b.data, S) >= 0); } }; } // namespace ZeroTier #endif