glib-aux: add nm_uuid_is_valid_nmlegacy() helper

This commit is contained in:
Thomas Haller 2021-05-02 22:29:56 +02:00
parent 2fcabf5699
commit b12f116a02
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 45 additions and 0 deletions

View file

@ -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)
{

View file

@ -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) \