32 #include <sys/types.h>
36 #ifdef HAVE_ARC4RANDOM_BUF
40 #ifdef USE_PROC_FOR_UUID
42 #elif defined HAVE_UUID_UUID_H
44 # include <uuid/uuid.h>
45 #elif defined HAVE_UUID_H
47 # include <arpa/inet.h>
50 #elif defined USE_WIN32_UUID_API
57 # include <arpa/inet.h>
87 bool filled_with_randomness =
false;
88 #if defined HAVE_ARC4RANDOM_BUF
97 arc4random_buf(uuid_data, BINARY_SIZE);
98 filled_with_randomness =
true;
99 # define TRIED_RANDOMNESS
100 #elif defined HAVE_ARC4RANDOM
103 static_assert(BINARY_SIZE % 4 == 0,
"UUID binary size not multiple of 4");
104 for (
unsigned i = 0; i < BINARY_SIZE; i += 4) {
105 uint32_t v = arc4random();
106 memcpy(uuid_data + i, v, 4);
108 filled_with_randomness =
true;
109 # define TRIED_RANDOMNESS
110 #elif defined HAVE_GETENTROPY
114 if (getentropy(uuid_data, BINARY_SIZE) == 0) {
115 filled_with_randomness =
true;
117 # define TRIED_RANDOMNESS
119 if (filled_with_randomness) {
120 uuid_data[6] = (uuid_data[6] & 0x0f) | 0x40;
121 uuid_data[8] = (uuid_data[8] & 0x3f) | 0x80;
125 #ifdef USE_PROC_FOR_UUID
134 char buf[STRING_SIZE];
135 int fd =
open(
"/proc/sys/kernel/random/uuid", O_RDONLY);
136 if (
rare(fd == -1)) {
139 bool failed = (read(fd, buf, STRING_SIZE) != STRING_SIZE);
145 #elif defined HAVE_UUID_UUID_H
148 memcpy(uuid_data, &uu, BINARY_SIZE);
149 #elif defined HAVE_UUID_H
152 uuid_create(&uu, &status);
153 if (status != uuid_s_ok) {
155 throw std::bad_alloc();
157 uu.time_low = htonl(uu.time_low);
158 uu.time_mid = htons(uu.time_mid);
159 uu.time_hi_and_version = htons(uu.time_hi_and_version);
160 memcpy(uuid_data, &uu, BINARY_SIZE);
161 #elif defined USE_WIN32_UUID_API
163 if (
rare(UuidCreate(&uuid) != RPC_S_OK)) {
169 uuid.Data1 = htonl(uuid.Data1);
170 uuid.Data2 = htons(uuid.Data2);
171 uuid.Data3 = htons(uuid.Data3);
172 memcpy(uuid_data, &uuid, BINARY_SIZE);
173 #elif defined TRIED_RANDOMNESS
176 # error Do not know how to generate UUIDs
183 for (
unsigned i = 0; i != BINARY_SIZE; ++i) {
193 result.reserve(STRING_SIZE);
194 for (
unsigned i = 0; i != BINARY_SIZE; ++i) {
195 unsigned char ch = uuid_data[i];
196 result +=
"0123456789abcdef"[ch >> 4];
197 result +=
"0123456789abcdef"[ch & 0x0f];
std::string to_string() const
void parse(const char *in)
DatabaseCreateError indicates a failure to create a database.
Hierarchy of classes which Xapian can throw as exceptions.
Database open(std::string_view host, unsigned int port, unsigned timeout=10000, unsigned connect_timeout=10000)
Construct a Database object for read-only access to a remote database accessed via a TCP connection.
include <fcntl.h>, but working around broken platforms.
include <sys/stat.h> with portability enhancements
<unistd.h>, but with compat.
include <windows.h> without all the bloat and damage.
include <winsock2.h> but working around problems.
Various handy string-related helpers.
char hex_decode(char ch1, char ch2)
Decode a pair of ASCII hex digits.
static constexpr unsigned UUID_GAP_MASK
Bit-mask to determine where to put hyphens in the string representation.
Class for handling UUIDs.