2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
11 #include <common/compat/string.hpp>
12 #include <common/error.hpp>
13 #include <common/format.hpp>
14 #include <common/random.hpp>
24 const lttng_uuid nil_uuid
= {};
25 bool lttng_uuid_is_init
;
28 void lttng_uuid_to_str(const lttng_uuid
& uuid
, char *uuid_str
)
30 snprintf(uuid_str
, LTTNG_UUID_STR_LEN
, LTTNG_UUID_FMT
, LTTNG_UUID_FMT_VALUES(uuid
));
33 std::string
lttng::utils::uuid_to_str(const lttng_uuid
& uuid
)
35 std::string
uuid_str(LTTNG_UUID_STR_LEN
, '\0');
37 ::lttng_uuid_to_str(uuid
, &uuid_str
[0]);
39 /* Don't include '\0' in the C++ string. */
40 uuid_str
.resize(uuid_str
.size() - 1);
45 int lttng_uuid_from_str(const char *str_in
, lttng_uuid
& uuid_out
)
48 lttng_uuid uuid_scan
= {};
50 if (str_in
== nullptr) {
55 if (lttng_strnlen(str_in
, LTTNG_UUID_STR_LEN
) != LTTNG_UUID_STR_LEN
- 1) {
60 /* Scan to a temporary location in case of a partial match. */
61 if (sscanf(str_in
, LTTNG_UUID_FMT
, LTTNG_UUID_SCAN_VALUES(uuid_scan
)) != LTTNG_UUID_LEN
) {
71 bool lttng_uuid_is_nil(const lttng_uuid
& uuid
)
73 return uuid
== nil_uuid
;
77 * Generate a random UUID according to RFC4122, section 4.4.
79 int lttng_uuid_generate(lttng_uuid
& uuid_out
)
83 if (!lttng_uuid_is_init
) {
85 srand(lttng::random::produce_best_effort_random_seed());
86 } catch (const std::exception
& e
) {
87 ERR("Failed to initialize random seed during generation of UUID: %s",
93 lttng_uuid_is_init
= true;
97 * Generate 16 bytes of random bits.
99 for (i
= 0; i
< LTTNG_UUID_LEN
; i
++) {
100 uuid_out
[i
] = (uint8_t) rand();
104 * Set the two most significant bits (bits 6 and 7) of the
105 * clock_seq_hi_and_reserved to zero and one, respectively.
107 uuid_out
[8] &= ~(1 << 6);
108 uuid_out
[8] |= (1 << 7);
111 * Set the four most significant bits (bits 12 through 15) of the
112 * time_hi_and_version field to the 4-bit version number from
116 uuid_out
[6] |= (LTTNG_UUID_VER
<< 4);
This page took 0.030503 seconds and 4 git commands to generate.