RandomUuid.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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
18static_assert(sizeof(RandomUuid) == 128/8, "RandomUuid has RFC 4122-prescribed 128-bit size");
19
21{
22 // Generate random bits for populating our UUID.
23 static std::mt19937_64 rng(RandomSeed64()); // produces 64-bit sized values
24 const auto rnd1 = rng();
25 const auto rnd2 = rng();
26
27 // No real r.n.g. is perfect, but we assume that std::mt19937_64 quality is
28 // high enough to make any imperfections irrelevant to this specific code.
29
30 // bullet 3 of RFC 4122 Section 4.4 algorithm but setting _all_ bits (KISS)
31 static_assert(sizeof(rnd1) + sizeof(rnd2) == sizeof(*this), "random bits fill a UUID");
32 memcpy(raw(), &rnd1, sizeof(rnd1));
33 memcpy(raw() + sizeof(rnd1), &rnd2, sizeof(rnd2));
34
35 // bullet 2 of RFC 4122 Section 4.4 algorithm
40
41 // bullet 1 of RFC 4122 Section 4.4 algorithm
44
45 assert(sane());
46}
47
49{
50 static_assert(sizeof(*this) == sizeof(Serialized), "RandomUuid is deserialized with 128/8 bytes");
51 memcpy(raw(), bytes.data(), sizeof(*this));
52 timeLow = ntohl(timeLow);
53 timeMid = ntohs(timeMid);
55 if (!sane())
56 throw TextException("malformed version 4 variant 1 UUID", Here());
57}
58
60bool
62{
63 return (!EBIT_TEST(clockSeqHiAndReserved, 6) &&
69}
70
73{
74 assert(sane());
75 auto toNetwork = clone();
76 // Convert all multi-byte fields to network byte order so that the recipient
77 // will consider our ID sane() and print() the same text representation.
78 toNetwork.timeLow = htonl(timeLow);
79 toNetwork.timeMid = htons(timeMid);
80 toNetwork.timeHiAndVersion = htons(timeHiAndVersion);
81 return *reinterpret_cast<const Serialized *>(toNetwork.raw());
82}
83
84void
85RandomUuid::print(std::ostream &os) const
86{
87 const auto savedFlags = os.flags();
88 const auto savedFill = os.fill('0');
89
90 os << std::hex;
91
92 os <<
93 std::setw(8) << timeLow << '-' <<
94 std::setw(4) << timeMid << '-' <<
95 std::setw(4) << timeHiAndVersion << '-' <<
96 std::setw(2) << +clockSeqHiAndReserved << std::setw(2) << +clockSeqLow << '-';
97
98 for (size_t i = 0; i < sizeof(node); ++i)
99 os << std::setw(2) << +node[i];
100
101 os.fill(savedFill);
102 os.flags(savedFlags);
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
std::ostream & operator<<(std::ostream &os, const RandomUuid &uuid)
Definition: RandomUuid.cc:112
#define assert(EX)
Definition: assert.h:19
std::mt19937_64::result_type RandomSeed64()
a 64-bit version of RandomSeed32()
Definition: Random.cc:34
RandomUuid()
creates a new unique ID (i.e. not a "nil UUID" in RFC 4122 terminology)
Definition: RandomUuid.cc:20
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:85
bool sane() const
whether this (being constructed) object follows UUID version 4 variant 1 format
Definition: RandomUuid.cc:61
uint8_t clockSeqLow
Definition: RandomUuid.h:69
Serialized serialize() const
exports UUID value; suitable for long-term storage
Definition: RandomUuid.cc:72
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:106
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