0e01c2185cc819327066dab0b9580059432d2d97
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
9 #include <common/compat/string.h>
10 #include <common/random.h>
11 #include <common/error.h>
22 static const lttng_uuid nil_uuid
;
23 static bool lttng_uuid_is_init
;
25 void lttng_uuid_to_str(const lttng_uuid uuid
, char *uuid_str
)
27 sprintf(uuid_str
, LTTNG_UUID_FMT
, LTTNG_UUID_FMT_VALUES(uuid
));
30 int lttng_uuid_from_str(const char *str_in
, lttng_uuid uuid_out
)
35 if ((str_in
== NULL
) || (uuid_out
== NULL
)) {
40 if (lttng_strnlen(str_in
, LTTNG_UUID_STR_LEN
) != LTTNG_UUID_STR_LEN
- 1) {
45 /* Scan to a temporary location in case of a partial match. */
46 if (sscanf(str_in
, LTTNG_UUID_FMT
, LTTNG_UUID_SCAN_VALUES(uuid_scan
)) !=
51 lttng_uuid_copy(uuid_out
, uuid_scan
);
56 bool lttng_uuid_is_equal(const lttng_uuid a
, const lttng_uuid b
)
58 return memcmp(a
, b
, LTTNG_UUID_LEN
) == 0;
61 bool lttng_uuid_is_nil(const lttng_uuid uuid
)
63 return memcmp(nil_uuid
, uuid
, sizeof(lttng_uuid
)) == 0;
66 void lttng_uuid_copy(lttng_uuid dst
, const lttng_uuid src
)
68 memcpy(dst
, src
, LTTNG_UUID_LEN
);
72 * Generate a random UUID according to RFC4122, section 4.4.
74 int lttng_uuid_generate(lttng_uuid uuid_out
)
78 if (uuid_out
== NULL
) {
83 if (!lttng_uuid_is_init
) {
86 ret
= lttng_produce_best_effort_random_seed(&new_seed
);
88 ERR("Failed to initialize random seed while generating UUID");
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.031615 seconds and 3 git commands to generate.