1 /* guuid.c - UUID functions
3 * Copyright (C) 2013-2015, 2017 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * licence, or (at your option) any later version.
10 * This is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13 * License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * Authors: Marc-André Lureau <marcandre.lureau@redhat.com>
27 #include "gstrfuncs.h"
29 #include "gmessages.h"
30 #include "gchecksum.h"
41 * @short_description: a universally unique identifier
43 * A UUID, or Universally unique identifier, is intended to uniquely
44 * identify information in a distributed environment. For the
45 * definition of UUID, see [RFC 4122](https://tools.ietf.org/html/rfc4122.html).
47 * The creation of UUIDs does not require a centralized authority.
49 * UUIDs are of relatively small size (128 bits, or 16 bytes). The
50 * common string representation (ex:
51 * 1d6c0810-2bd6-45f3-9890-0268422a6f14) needs 37 bytes.
53 * The UUID specification defines 5 versions, and calling
54 * g_uuid_string_random() will generate a unique (or rather random)
55 * UUID of the most common version, version 4.
64 * Creates a string representation of @uuid, of the form
65 * 06e023d5-86d8-420e-8103-383e4566087a (no braces nor urn:uuid:
68 * Returns: (transfer full): A string that should be freed with g_free().
72 g_uuid_to_string (const GUuid
*uuid
)
76 g_return_val_if_fail (uuid
!= NULL
, NULL
);
80 return g_strdup_printf ("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x"
81 "-%02x%02x%02x%02x%02x%02x",
82 bytes
[0], bytes
[1], bytes
[2], bytes
[3],
83 bytes
[4], bytes
[5], bytes
[6], bytes
[7],
84 bytes
[8], bytes
[9], bytes
[10], bytes
[11],
85 bytes
[12], bytes
[13], bytes
[14], bytes
[15]);
89 uuid_parse_string (const gchar
*str
,
93 guint8
*bytes
= tmp
.bytes
;
95 guint expected_len
= 36;
97 if (strlen (str
) != expected_len
)
100 for (i
= 0, j
= 0; i
< 16;)
102 if (j
== 8 || j
== 13 || j
== 18 || j
== 23)
110 hi
= g_ascii_xdigit_value (str
[j
++]);
111 lo
= g_ascii_xdigit_value (str
[j
++]);
113 if (hi
== -1 || lo
== -1)
116 bytes
[i
++] = hi
<< 8 | lo
;
126 * g_uuid_string_is_valid:
127 * @str: a string representing a UUID
129 * Parses the string @str and verify if it is a UUID.
131 * The function accepts the following syntax:
133 * - simple forms (e.g. `f81d4fae-7dec-11d0-a765-00a0c91e6bf6`)
135 * Note that hyphens are required within the UUID string itself,
136 * as per the aforementioned RFC.
138 * Returns: %TRUE if @str is a valid UUID, %FALSE otherwise.
142 g_uuid_string_is_valid (const gchar
*str
)
144 g_return_val_if_fail (str
!= NULL
, FALSE
);
146 return uuid_parse_string (str
, NULL
);
150 uuid_set_version (GUuid
*uuid
, guint version
)
152 guint8
*bytes
= uuid
->bytes
;
155 * Set the four most significant bits (bits 12 through 15) of the
156 * time_hi_and_version field to the 4-bit version number from
160 bytes
[6] |= version
<< 4;
162 * Set the two most significant bits (bits 6 and 7) of the
163 * clock_seq_hi_and_reserved to zero and one, respectively.
170 * g_uuid_generate_v4:
173 * Generates a random UUID (RFC 4122 version 4).
177 g_uuid_generate_v4 (GUuid
*uuid
)
183 g_return_if_fail (uuid
!= NULL
);
186 ints
= (guint32
*) bytes
;
187 for (i
= 0; i
< 4; i
++)
188 ints
[i
] = g_random_int ();
190 uuid_set_version (uuid
, 4);
194 * g_uuid_string_random:
196 * Generates a random UUID (RFC 4122 version 4) as a string.
198 * Returns: (transfer full): A string that should be freed with g_free().
202 g_uuid_string_random (void)
206 g_uuid_generate_v4 (&uuid
);
208 return g_uuid_to_string (&uuid
);