Skip to content

Commit

Permalink
If glib is too old, generate uuid manually when needed (see #1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed Feb 28, 2020
1 parent 761e47a commit a696c9b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,42 @@ guint64 janus_random_uint64(void) {
}

char *janus_random_uuid(void) {
#if GLIB_CHECK_VERSION(2, 52, 0)
return g_uuid_string_random();
#else
/* g_uuid_string_random is only available from glib 2.52, so if it's
* not available we have to do it manually: the following code is
* heavily based on https://github.com/rxi/uuid4 (MIT license) */
const char *template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
const char *samples = "0123456789abcdef";
union { unsigned char b[16]; uint64_t word[2]; } rnd;
rnd.word[0] = janus_random_uint64();
rnd.word[1] = janus_random_uint64();
/* Generate the string */
char uuid[37], *dst = uuid;
const char *p = template;
int i = 0, n = 0;
while(*p) {
n = rnd.b[i >> 1];
n = (i & 1) ? (n >> 4) : (n & 0xf);
switch (*p) {
case 'x':
*dst = samples[n];
i++;
break;
case 'y':
*dst = samples[(n & 0x3) + 8];
i++;
break;
default:
*dst = *p;
}
p++;
dst++;
}
uuid[36] = '\0';
return g_strdup(uuid);
#endif
}

guint64 *janus_uint64_dup(guint64 num) {
Expand Down

0 comments on commit a696c9b

Please sign in to comment.