From a696c9b0a378ad4ab20ac3e50100077ac919427c Mon Sep 17 00:00:00 2001 From: Lorenzo Miniero Date: Fri, 28 Feb 2020 16:04:28 +0100 Subject: [PATCH] If glib is too old, generate uuid manually when needed (see #1880) --- utils.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/utils.c b/utils.c index e838e2c013..07b0cd888c 100644 --- a/utils.c +++ b/utils.c @@ -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) {