Squid Web Cache master
Loading...
Searching...
No Matches
RandomUuid.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "base/IoManip.h"
11#include "base/Random.h"
12#include "base/RandomUuid.h"
13#include "base/TextException.h"
14#include "defines.h"
15
16#include <iostream>
17
18#if HAVE_NETINET_IN_H
19#include <netinet/in.h>
20#endif
21#if HAVE_ARPA_INET_H
22#include <arpa/inet.h>
23#endif
24
25static_assert(sizeof(RandomUuid) == 128/8, "RandomUuid has RFC 4122-prescribed 128-bit size");
26
28{
29 // Generate random bits for populating our UUID.
30 static std::mt19937_64 rng(RandomSeed64()); // produces 64-bit sized values
31 const auto rnd1 = rng();
32 const auto rnd2 = rng();
33
34 // No real r.n.g. is perfect, but we assume that std::mt19937_64 quality is
35 // high enough to make any imperfections irrelevant to this specific code.
36
37 // bullet 3 of RFC 4122 Section 4.4 algorithm but setting _all_ bits (KISS)
38 static_assert(sizeof(rnd1) + sizeof(rnd2) == sizeof(*this), "random bits fill a UUID");
39 memcpy(raw(), &rnd1, sizeof(rnd1));
40 memcpy(raw() + sizeof(rnd1), &rnd2, sizeof(rnd2));
41
42 // bullet 2 of RFC 4122 Section 4.4 algorithm
47
48 // bullet 1 of RFC 4122 Section 4.4 algorithm
51
52 assert(sane());
53}
54
56{
57 static_assert(sizeof(*this) == sizeof(Serialized), "RandomUuid is deserialized with 128/8 bytes");
58 memcpy(raw(), bytes.data(), sizeof(*this));
59 timeLow = ntohl(timeLow);
60 timeMid = ntohs(timeMid);
62 if (!sane())
63 throw TextException("malformed version 4 variant 1 UUID", Here());
64}
65
67bool
77
80{
81 assert(sane());
82 auto toNetwork = clone();
83 // Convert all multi-byte fields to network byte order so that the recipient
84 // will consider our ID sane() and print() the same text representation.
85 toNetwork.timeLow = htonl(timeLow);
86 toNetwork.timeMid = htons(timeMid);
87 toNetwork.timeHiAndVersion = htons(timeHiAndVersion);
88 return *reinterpret_cast<const Serialized *>(toNetwork.raw());
89}
90
91void
92RandomUuid::print(std::ostream &os) const
93{
94 os <<
95 asHex(timeLow).minDigits(8) << '-' <<
96 asHex(timeMid).minDigits(4) << '-' <<
97 asHex(timeHiAndVersion).minDigits(4) << '-' <<
98 asHex(clockSeqHiAndReserved).minDigits(2) <<
99 asHex(clockSeqLow).minDigits(2) << '-';
100
101 for (size_t i = 0; i < sizeof(node); ++i)
102 os << asHex(node[i]).minDigits(2);
103}
104
105bool
107{
108 return memcmp(raw(), other.raw(), sizeof(*this)) == 0;
109}
110
111std::ostream &
112operator<<(std::ostream &os, const RandomUuid &uuid)
113{
114 uuid.print(os);
115 return os;
116}
117
#define Here()
source code location of the caller
Definition Here.h:15
AsHex< Integer > asHex(const Integer n)
a helper to ease AsHex object creation
Definition IoManip.h:169
std::ostream & operator<<(std::ostream &os, const RandomUuid &uuid)
#define assert(EX)
Definition assert.h:17
std::mt19937_64::result_type RandomSeed64()
a 64-bit version of RandomSeed32()
Definition Random.cc:35
RandomUuid()
creates a new unique ID (i.e. not a "nil UUID" in RFC 4122 terminology)
Definition RandomUuid.cc:27
uint32_t timeLow
Definition RandomUuid.h:65
uint8_t clockSeqHiAndReserved
Definition RandomUuid.h:68
void print(std::ostream &os) const
writes a human-readable representation
Definition RandomUuid.cc:92
bool sane() const
whether this (being constructed) object follows UUID version 4 variant 1 format
Definition RandomUuid.cc:68
uint8_t clockSeqLow
Definition RandomUuid.h:69
Serialized serialize() const
exports UUID value; suitable for long-term storage
Definition RandomUuid.cc:79
uint8_t node[6]
Definition RandomUuid.h:70
std::array< uint8_t, 128/8 > Serialized
UUID representation independent of machine byte-order architecture.
Definition RandomUuid.h:21
RandomUuid clone() const
creates a UUID object with the same value as this UUID
Definition RandomUuid.h:43
bool operator==(const RandomUuid &) const
char * raw()
read/write access to storage bytes
Definition RandomUuid.h:52
uint16_t timeMid
Definition RandomUuid.h:66
uint16_t timeHiAndVersion
Definition RandomUuid.h:67
an std::runtime_error with thrower location info
#define EBIT_CLR(flag, bit)
Definition defines.h:66
#define EBIT_SET(flag, bit)
Definition defines.h:65
#define EBIT_TEST(flag, bit)
Definition defines.h:67
Definition parse.c:104