diff options
author | Thomas Haller <thaller@redhat.com> | 2018-10-31 10:14:54 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-10-31 11:34:31 +0100 |
commit | 4db431191cd095ab0584212b59f1c2b9d9201fd2 (patch) | |
tree | 42138d3dabb1b20187ffc5adafa3d31c7d60175b | |
parent | 070a4d935504ee8f9be79ff3c25e650fa695fb2b (diff) | |
download | NetworkManager-4db431191cd095ab0584212b59f1c2b9d9201fd2.tar.gz |
libnm: add nm_utils_uuid_generate_from_string_bin() function
-rw-r--r-- | libnm-core/nm-core-internal.h | 2 | ||||
-rw-r--r-- | libnm-core/nm-utils.c | 53 |
2 files changed, 40 insertions, 15 deletions
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index b06f32d476..80d00d90a6 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -299,6 +299,8 @@ NMUuid *_nm_utils_uuid_generate_random (NMUuid *out_uuid); #define NM_UTILS_UUID_TYPE_VERSION3 3 #define NM_UTILS_UUID_TYPE_VERSION5 5 +NMUuid *nm_utils_uuid_generate_from_string_bin (NMUuid *uuid, const char *s, gssize slen, int uuid_type, gpointer type_args); + char *nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args); /* arbitrarily chosen namespace UUID for _nm_utils_uuid_generate_from_strings() */ diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 2cb5d851be..abdf80474a 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -2851,7 +2851,9 @@ nm_utils_uuid_generate (void) } /** - * nm_utils_uuid_generate_from_string: + * nm_utils_uuid_generate_from_string_bin: + * @uuid: the UUID to update inplace. This function cannot + * fail to succeed. * @s: a string to use as the seed for the UUID * @slen: if negative, treat @s as zero terminated C string. * Otherwise, assume the length as given (and allow @s to be @@ -2861,14 +2863,12 @@ nm_utils_uuid_generate (void) * * For a given @s, this function will always return the same UUID. * - * Returns: a newly allocated UUID suitable for use as the #NMSettingConnection - * object's #NMSettingConnection:id: property + * Returns: the input @uuid. This function cannot fail. **/ -char * -nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args) +NMUuid * +nm_utils_uuid_generate_from_string_bin (NMUuid *uuid, const char *s, gssize slen, int uuid_type, gpointer type_args) { - NMUuid uuid; - + g_return_val_if_fail (uuid, FALSE); g_return_val_if_fail (slen == 0 || s, FALSE); if (slen < 0) @@ -2881,8 +2881,8 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, g 0, (guint8 *) s, slen, - (guint8 *) &uuid, - sizeof (uuid)); + (guint8 *) uuid, + sizeof (*uuid)); break; case NM_UTILS_UUID_TYPE_VERSION3: case NM_UTILS_UUID_TYPE_VERSION5: { @@ -2899,8 +2899,8 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, g slen, (guint8 *) &ns_uuid, sizeof (ns_uuid), - (guint8 *) &uuid, - sizeof (uuid)); + (guint8 *) uuid, + sizeof (*uuid)); } else { nm_auto_free_checksum GChecksum *sum = NULL; guint8 buf[20]; @@ -2913,18 +2913,41 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, g g_checksum_get_digest (sum, buf, &len); nm_assert (len == sizeof (buf)); - G_STATIC_ASSERT_EXPR (sizeof (uuid) <= sizeof (buf)); - memcpy (&uuid, buf, sizeof (uuid)); + G_STATIC_ASSERT_EXPR (sizeof (*uuid) <= sizeof (buf)); + memcpy (uuid, buf, sizeof (*uuid)); } - uuid.uuid[6] = (uuid.uuid[6] & 0x0F) | (uuid_type << 4); - uuid.uuid[8] = (uuid.uuid[8] & 0x3F) | 0x80; + uuid->uuid[6] = (uuid->uuid[6] & 0x0F) | (uuid_type << 4); + uuid->uuid[8] = (uuid->uuid[8] & 0x3F) | 0x80; break; } default: g_return_val_if_reached (NULL); } + return uuid; +} + +/** + * nm_utils_uuid_generate_from_string: + * @s: a string to use as the seed for the UUID + * @slen: if negative, treat @s as zero terminated C string. + * Otherwise, assume the length as given (and allow @s to be + * non-null terminated or contain '\0'). + * @uuid_type: a type identifier which UUID format to generate. + * @type_args: additional arguments, depending on the uuid_type + * + * For a given @s, this function will always return the same UUID. + * + * Returns: a newly allocated UUID suitable for use as the #NMSettingConnection + * object's #NMSettingConnection:id: property + **/ +char * +nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args) +{ + NMUuid uuid; + + nm_utils_uuid_generate_from_string_bin (&uuid, s, slen, uuid_type, type_args); return _nm_utils_uuid_unparse (&uuid, NULL); } |