From b12f116a02c658c911d0483423000df942077632 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 2 May 2021 22:29:56 +0200 Subject: [PATCH] glib-aux: add nm_uuid_is_valid_nmlegacy() helper --- src/libnm-glib-aux/nm-uuid.c | 41 ++++++++++++++++++++++++++++++++++++ src/libnm-glib-aux/nm-uuid.h | 4 ++++ 2 files changed, 45 insertions(+) diff --git a/src/libnm-glib-aux/nm-uuid.c b/src/libnm-glib-aux/nm-uuid.c index 7497b7c489..18badc46d8 100644 --- a/src/libnm-glib-aux/nm-uuid.c +++ b/src/libnm-glib-aux/nm-uuid.c @@ -129,6 +129,47 @@ nm_uuid_generate_random(NMUuid *out_uuid) /*****************************************************************************/ +/** + * nm_uuid_is_valid_nmlegacy() + * @str: the string to check whether it's a valid UUID. + * + * Note that this does not perform a strict check. + * Instead, it checks a more relaxed format (including + * non valid UUID strings). This is for backward compatibility, + * where older code did not perform a strict check. + * + * Returns: %TRUE, if the string is a valid legacy format. + * This may not be a valid UUID! + */ +gboolean +nm_uuid_is_valid_nmlegacy(const char *str) +{ + const char *p = str; + int num_dashes = 0; + + if (!p) + return FALSE; + + while (*p) { + if (*p == '-') + num_dashes++; + else if (!g_ascii_isxdigit(*p)) + return FALSE; + p++; + } + + if ((num_dashes == 4) && (p - str == 36)) + return TRUE; + + /* Backwards compat for older configurations */ + if ((num_dashes == 0) && (p - str == 40)) + return TRUE; + + return FALSE; +} + +/*****************************************************************************/ + gboolean nm_uuid_is_null(const NMUuid *uuid) { diff --git a/src/libnm-glib-aux/nm-uuid.h b/src/libnm-glib-aux/nm-uuid.h index c4a6bdb57c..0cb7ec2311 100644 --- a/src/libnm-glib-aux/nm-uuid.h +++ b/src/libnm-glib-aux/nm-uuid.h @@ -33,6 +33,10 @@ gboolean nm_uuid_is_null(const NMUuid *uuid); /*****************************************************************************/ +gboolean nm_uuid_is_valid_nmlegacy(const char *str); + +/*****************************************************************************/ + char *nm_uuid_generate_random_str(char buf[static 37]); #define nm_uuid_generate_random_str_arr(buf) \