libnm: don't allocate temporary buffer in nm_utils_uuid_generate_from_strings()

crypto_md5_sum() already accepts two separate strings: salt and password.
No need to allocate a temporary buffer. Just pass @ns_uuid and @s
separately.
This commit is contained in:
Thomas Haller
2014-12-04 17:37:04 +01:00
parent 440b9d85b4
commit 69860e5d3a

View File

@@ -1967,38 +1967,30 @@ char *
nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args) nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args)
{ {
uuid_t uuid; uuid_t uuid;
char *buf = NULL; char *buf;
g_return_val_if_fail (slen == 0 || s, FALSE); g_return_val_if_fail (slen == 0 || s, FALSE);
g_return_val_if_fail (uuid_type == NM_UTILS_UUID_TYPE_LEGACY || uuid_type == NM_UTILS_UUID_TYPE_VARIANT3, NULL); g_return_val_if_fail (uuid_type == NM_UTILS_UUID_TYPE_LEGACY || uuid_type == NM_UTILS_UUID_TYPE_VARIANT3, NULL);
g_return_val_if_fail (!type_args || uuid_type == NM_UTILS_UUID_TYPE_VARIANT3, NULL); g_return_val_if_fail (!type_args || uuid_type == NM_UTILS_UUID_TYPE_VARIANT3, NULL);
if (slen < 0)
slen = strlen (s);
else if (slen == 0)
s = "";
switch (uuid_type) { switch (uuid_type) {
case NM_UTILS_UUID_TYPE_LEGACY: case NM_UTILS_UUID_TYPE_LEGACY:
crypto_md5_hash (NULL, 0, s, slen, (char *) uuid, sizeof (uuid)); crypto_md5_hash (NULL, 0, s, slen, (char *) uuid, sizeof (uuid));
break; break;
case NM_UTILS_UUID_TYPE_VARIANT3: { case NM_UTILS_UUID_TYPE_VARIANT3: {
uuid_t ns_uuid = { 0 }; uuid_t ns_uuid = { 0 };
char *str;
if (type_args) { if (type_args) {
/* type_args can be a name space UUID. Interpret it as (char *) */ /* type_args can be a name space UUID. Interpret it as (char *) */
if (uuid_parse ((char *) type_args, ns_uuid) != 0) if (uuid_parse ((char *) type_args, ns_uuid) != 0)
g_return_val_if_reached (NULL); g_return_val_if_reached (NULL);
} }
str = g_new (char, sizeof (ns_uuid) + slen);
memcpy (&str[0], ns_uuid, sizeof (ns_uuid)); crypto_md5_hash (s, slen, (char *) ns_uuid, sizeof (ns_uuid), (char *) uuid, sizeof (uuid));
memcpy (&str[sizeof (ns_uuid)], s, slen);
crypto_md5_hash (NULL, 0, str, sizeof (ns_uuid) + slen, (char *) uuid, sizeof (uuid));
uuid[6] = (uuid[6] & 0x0F) | 0x30; uuid[6] = (uuid[6] & 0x0F) | 0x30;
uuid[8] = (uuid[8] & 0x3F) | 0x80; uuid[8] = (uuid[8] & 0x3F) | 0x80;
g_free (str);
break; break;
} }
default: default: