Blob.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_BLOB_HPP
  14. #define ZT_BLOB_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "TriviallyCopyable.hpp"
  18. #include <algorithm>
  19. // This header contains simple statically sized binary object types.
  20. namespace ZeroTier {
  21. /**
  22. * Blob type for SHA384 hashes
  23. */
  24. struct SHA384Hash
  25. {
  26. uint64_t data[6];
  27. ZT_INLINE SHA384Hash() noexcept
  28. { Utils::zero< sizeof(data) >(data); }
  29. explicit ZT_INLINE SHA384Hash(const void *const d) noexcept
  30. { Utils::copy< 48 >(data, d); }
  31. ZT_INLINE const uint8_t *bytes() const noexcept
  32. { return reinterpret_cast<const uint8_t *>(data); }
  33. ZT_INLINE unsigned long hashCode() const noexcept
  34. { return (unsigned long)data[0]; }
  35. ZT_INLINE operator bool() const noexcept
  36. { return ((data[0] != 0) && (data[1] != 0) && (data[2] != 0) && (data[3] != 0) && (data[4] != 0) && (data[5] != 0)); }
  37. ZT_INLINE bool operator==(const SHA384Hash &b) const noexcept
  38. { 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])); }
  39. ZT_INLINE bool operator!=(const SHA384Hash &b) const noexcept
  40. { return !(*this == b); }
  41. ZT_INLINE bool operator<(const SHA384Hash &b) const noexcept
  42. { return (memcmp(data, b.data, 48) < 0); }
  43. ZT_INLINE bool operator>(const SHA384Hash &b) const noexcept
  44. { return (memcmp(data, b.data, 48) > 0); }
  45. ZT_INLINE bool operator<=(const SHA384Hash &b) const noexcept
  46. { return (memcmp(data, b.data, 48) <= 0); }
  47. ZT_INLINE bool operator>=(const SHA384Hash &b) const noexcept
  48. { return (memcmp(data, b.data, 48) >= 0); }
  49. };
  50. /**
  51. * Blob type for 128-bit GUIDs, UUIDs, etc.
  52. */
  53. struct UniqueID
  54. {
  55. uint64_t data[2];
  56. ZT_INLINE UniqueID() noexcept
  57. { Utils::zero< sizeof(data) >(data); }
  58. ZT_INLINE UniqueID(const uint64_t a, const uint64_t b) noexcept
  59. {
  60. data[0] = a;
  61. data[1] = b;
  62. }
  63. explicit ZT_INLINE UniqueID(const void *const d) noexcept
  64. { Utils::copy< 16 >(data, d); }
  65. ZT_INLINE const uint8_t *bytes() const noexcept
  66. { return reinterpret_cast<const uint8_t *>(data); }
  67. ZT_INLINE unsigned long hashCode() const noexcept
  68. { return (unsigned long)(data[0] ^ data[1]); }
  69. ZT_INLINE operator bool() const noexcept
  70. { return ((data[0] != 0) && (data[1] != 0)); }
  71. ZT_INLINE bool operator==(const UniqueID &b) const noexcept
  72. { return ((data[0] == b.data[0]) && (data[1] == b.data[1])); }
  73. ZT_INLINE bool operator!=(const UniqueID &b) const noexcept
  74. { return !(*this == b); }
  75. ZT_INLINE bool operator<(const UniqueID &b) const noexcept
  76. { return (memcmp(data, b.data, 16) < 0); }
  77. ZT_INLINE bool operator>(const UniqueID &b) const noexcept
  78. { return (memcmp(data, b.data, 16) > 0); }
  79. ZT_INLINE bool operator<=(const UniqueID &b) const noexcept
  80. { return (memcmp(data, b.data, 16) <= 0); }
  81. ZT_INLINE bool operator>=(const UniqueID &b) const noexcept
  82. { return (memcmp(data, b.data, 16) >= 0); }
  83. };
  84. static_assert(sizeof(SHA384Hash) == 48, "SHA384Hash contains unnecessary padding");
  85. static_assert(sizeof(UniqueID) == 16, "UniqueID contains unnecessary padding");
  86. template< unsigned long S >
  87. struct Blob
  88. {
  89. uint8_t data[S];
  90. };
  91. } // namespace ZeroTier
  92. #endif