RandomUuid.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
69{
70 return (!EBIT_TEST(clockSeqHiAndReserved, 6) &&
76}
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 const auto savedFlags = os.flags();
95 const auto savedFill = os.fill('0');
96
97 os << std::hex;
98
99 os <<
100 std::setw(8) << timeLow << '-' <<
101 std::setw(4) << timeMid << '-' <<
102 std::setw(4) << timeHiAndVersion << '-' <<
103 std::setw(2) << +clockSeqHiAndReserved << std::setw(2) << +clockSeqLow << '-';
104
105 for (size_t i = 0; i < sizeof(node); ++i)
106 os << std::setw(2) << +node[i];
107
108 os.fill(savedFill);
109 os.flags(savedFlags);
110}
111
112bool
114{
115 return memcmp(raw(), other.raw(), sizeof(*this)) == 0;
116}
117
118std::ostream &
119operator<<(std::ostream &os, const RandomUuid &uuid)
120{
121 uuid.print(os);
122 return os;
123}
124
#define Here()
source code location of the caller
Definition: Here.h:15
std::ostream & operator<<(std::ostream &os, const RandomUuid &uuid)
Definition: RandomUuid.cc:119
#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
Definition: RandomUuid.cc:113
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
Definition: TextException.h:21
#define EBIT_CLR(flag, bit)
Definition: defines.h:68
#define EBIT_SET(flag, bit)
Definition: defines.h:67
#define EBIT_TEST(flag, bit)
Definition: defines.h:69
Definition: parse.c:104

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors