glib-aux: add nm_uuid_parse() helper

This commit is contained in:
Thomas Haller 2021-05-02 11:57:17 +02:00
parent 596bf4b91b
commit e7568e29b2
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 77 additions and 0 deletions

View file

@ -28,3 +28,68 @@ nm_uuid_unparse_case(const NMUuid *uuid, char out_str[static 37], gboolean upper
return out_str;
}
/*****************************************************************************/
gboolean
nm_uuid_parse_full(const char *str, NMUuid *out_uuid, gboolean *out_is_normalized)
{
NMUuid uuid;
guint8 * p;
int i;
gboolean is_normalized = TRUE;
nm_assert(str);
p = uuid.uuid;
for (i = 0; TRUE;) {
int v0;
int v1;
if (NM_IN_SET(i, 8, 13, 18, 23)) {
if (str[i] != '-')
return FALSE;
i++;
continue;
}
if (i == 36) {
if (str[i] != '\0')
return FALSE;
NM_SET_OUT(out_is_normalized, is_normalized);
NM_SET_OUT(out_uuid, uuid);
return TRUE;
}
#define _hexchar(ch, out_is_normalized2) \
({ \
const char _ch = (ch); \
int _result = -1; \
\
if (_ch >= '0') { \
if (_ch <= '9') \
_result = (_ch - '0'); \
else if (_ch >= 'A') { \
if (_ch <= 'F') { \
*(out_is_normalized2) = FALSE; \
_result = ((int) _ch) + (10 - (int) 'A'); \
} else if (_ch >= 'a' && _ch <= 'f') \
_result = ((int) _ch) + (10 - (int) 'a'); \
} \
} \
\
_result; \
})
v0 = _hexchar(str[i++], &is_normalized);
if (v0 < 0)
return FALSE;
v1 = _hexchar(str[i++], &is_normalized);
if (v1 < 0)
return FALSE;
*(p++) = (v0 << 4) + v1;
}
}

View file

@ -15,4 +15,16 @@ nm_uuid_unparse(const NMUuid *uuid, char out_str[static 37])
return nm_uuid_unparse_case(uuid, out_str, FALSE);
}
gboolean nm_uuid_parse_full(const char *str, NMUuid *out_uuid, gboolean *out_is_normalized);
static inline NMUuid *
nm_uuid_parse(const char *str, NMUuid *out_uuid)
{
nm_assert(out_uuid);
if (!nm_uuid_parse_full(str, out_uuid, NULL))
return NULL;
return out_uuid;
}
#endif /* __NM_UUID_H__ */