d59f750ca0bab0a2aabac414b7c9c462db30d4a5
[babeltrace.git] / src / common / uuid.c
1 /*
2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <glib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <inttypes.h>
27
28 #include "common/assert.h"
29 #include "common/uuid.h"
30
31
32 /*
33 * Generate a random UUID according to RFC4122, section 4.4.
34 */
35 BT_HIDDEN
36 void bt_uuid_generate(bt_uuid_t uuid_out)
37 {
38 int i;
39 GRand *rand;
40
41 BT_ASSERT(uuid_out);
42
43 rand = g_rand_new();
44
45 /*
46 * Generate 16 bytes of random bits.
47 */
48 for (i = 0; i < BT_UUID_LEN; i++) {
49 uuid_out[i] = (uint8_t) g_rand_int(rand);
50 }
51
52 /*
53 * Set the two most significant bits (bits 6 and 7) of the
54 * clock_seq_hi_and_reserved to zero and one, respectively.
55 */
56 uuid_out[8] &= ~(1 << 6);
57 uuid_out[8] |= (1 << 7);
58
59 /*
60 * Set the four most significant bits (bits 12 through 15) of the
61 * time_hi_and_version field to the 4-bit version number from
62 * Section 4.1.3.
63 */
64 uuid_out[6] &= 0x0f;
65 uuid_out[6] |= (BT_UUID_VER << 4);
66
67 g_rand_free(rand);
68 }
69
70 BT_HIDDEN
71 void bt_uuid_to_str(const bt_uuid_t uuid_in, char *str_out)
72 {
73 BT_ASSERT_DBG(uuid_in);
74 BT_ASSERT_DBG(str_out);
75
76 sprintf(str_out, BT_UUID_FMT, BT_UUID_FMT_VALUES(uuid_in));
77 }
78
79 BT_HIDDEN
80 int bt_uuid_from_str(const char *str_in, bt_uuid_t uuid_out)
81 {
82 int ret = 0;
83 bt_uuid_t uuid_scan;
84
85 BT_ASSERT_DBG(uuid_out);
86 BT_ASSERT_DBG(str_in);
87
88 if (strnlen(str_in, BT_UUID_STR_LEN + 1) != BT_UUID_STR_LEN) {
89 ret = -1;
90 goto end;
91 }
92
93 /* Scan to a temporary location in case of a partial match. */
94 if (sscanf(str_in, BT_UUID_FMT, BT_UUID_SCAN_VALUES(uuid_scan)) != BT_UUID_LEN) {
95 ret = -1;
96 }
97
98 bt_uuid_copy(uuid_out, uuid_scan);
99 end:
100 return ret;
101 }
102
103 BT_HIDDEN
104 int bt_uuid_compare(const bt_uuid_t uuid_a, const bt_uuid_t uuid_b)
105 {
106 return memcmp(uuid_a, uuid_b, BT_UUID_LEN);
107 }
108
109 BT_HIDDEN
110 void bt_uuid_copy(bt_uuid_t uuid_dest, const bt_uuid_t uuid_src)
111 {
112 BT_ASSERT(uuid_dest);
113 BT_ASSERT(uuid_src);
114 BT_ASSERT(uuid_dest != uuid_src);
115
116 memcpy(uuid_dest, uuid_src, BT_UUID_LEN);
117 }
This page took 0.033043 seconds and 3 git commands to generate.