all: move nm_utils_bin2hexstr_full() to shared
reuse++
This commit is contained in:

committed by
Thomas Haller

parent
d2144019d8
commit
b5efcf08f4
@@ -4131,64 +4131,7 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* _nm_utils_bin2hexstr_full:
|
||||
* @addr: pointer of @length bytes.
|
||||
* @length: number of bytes in @addr
|
||||
* @delimiter: either '\0', otherwise the output string will have the
|
||||
* given delimiter character between each two hex numbers.
|
||||
* @upper_case: if TRUE, use upper case ASCII characters for hex.
|
||||
* @out: if %NULL, the function will allocate a new buffer of
|
||||
* either (@length*2+1) or (@length*3) bytes, depending on whether
|
||||
* a @delimiter is specified. In that case, the allocated buffer will
|
||||
* be returned and must be freed by the caller.
|
||||
* If not %NULL, the buffer must already be preallocated and contain
|
||||
* at least (@length*2+1) or (@length*3) bytes, depending on the delimiter.
|
||||
*
|
||||
* Returns: the binary value converted to a hex string. If @out is given,
|
||||
* this always returns @out. If @out is %NULL, a newly allocated string
|
||||
* is returned.
|
||||
*/
|
||||
char *
|
||||
_nm_utils_bin2hexstr_full (gconstpointer addr,
|
||||
gsize length,
|
||||
char delimiter,
|
||||
gboolean upper_case,
|
||||
char *out)
|
||||
{
|
||||
const guint8 *in = addr;
|
||||
const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
|
||||
char *out0;
|
||||
|
||||
nm_assert (addr);
|
||||
nm_assert (length > 0);
|
||||
|
||||
if (out)
|
||||
out0 = out;
|
||||
else {
|
||||
out0 = out = g_new (char, delimiter == '\0'
|
||||
? length * 2 + 1
|
||||
: length * 3);
|
||||
}
|
||||
|
||||
/* @out must contain at least @length*3 bytes if @delimiter is set,
|
||||
* otherwise, @length*2+1. */
|
||||
|
||||
for (;;) {
|
||||
const guint8 v = *in++;
|
||||
|
||||
*out++ = LOOKUP[v >> 4];
|
||||
*out++ = LOOKUP[v & 0x0F];
|
||||
length--;
|
||||
if (!length)
|
||||
break;
|
||||
if (delimiter)
|
||||
*out++ = delimiter;
|
||||
}
|
||||
|
||||
*out = 0;
|
||||
return out0;
|
||||
}
|
||||
|
||||
/**
|
||||
* nm_utils_bin2hexstr:
|
||||
@@ -4214,7 +4157,7 @@ nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len)
|
||||
|
||||
result = g_malloc (buflen);
|
||||
|
||||
_nm_utils_bin2hexstr_full (src, len, '\0', FALSE, result);
|
||||
nm_utils_bin2hexstr_full (src, len, '\0', FALSE, result);
|
||||
|
||||
/* Cut converted key off at the correct length for this cipher type */
|
||||
if (final_len >= 0 && (gsize) final_len < buflen)
|
||||
@@ -4238,7 +4181,7 @@ nm_utils_hwaddr_ntoa (gconstpointer addr, gsize length)
|
||||
g_return_val_if_fail (addr, g_strdup (""));
|
||||
g_return_val_if_fail (length > 0, g_strdup (""));
|
||||
|
||||
return _nm_utils_bin2hexstr_full (addr, length, ':', TRUE, NULL);
|
||||
return nm_utils_bin2hexstr_full (addr, length, ':', TRUE, NULL);
|
||||
}
|
||||
|
||||
const char *
|
||||
@@ -4250,7 +4193,7 @@ nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_cas
|
||||
if (buf_len < addr_len * 3)
|
||||
g_return_val_if_reached (NULL);
|
||||
|
||||
return _nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
|
||||
return nm_utils_bin2hexstr_full (addr, addr_len, ':', upper_case, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -2477,3 +2477,62 @@ nm_utils_memeqzero (gconstpointer data, gsize length)
|
||||
/* Now we know that's zero, memcmp with self. */
|
||||
return memcmp (data, p, length) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* nm_utils_bin2hexstr_full:
|
||||
* @addr: pointer of @length bytes.
|
||||
* @length: number of bytes in @addr
|
||||
* @delimiter: either '\0', otherwise the output string will have the
|
||||
* given delimiter character between each two hex numbers.
|
||||
* @upper_case: if TRUE, use upper case ASCII characters for hex.
|
||||
* @out: if %NULL, the function will allocate a new buffer of
|
||||
* either (@length*2+1) or (@length*3) bytes, depending on whether
|
||||
* a @delimiter is specified. In that case, the allocated buffer will
|
||||
* be returned and must be freed by the caller.
|
||||
* If not %NULL, the buffer must already be preallocated and contain
|
||||
* at least (@length*2+1) or (@length*3) bytes, depending on the delimiter.
|
||||
*
|
||||
* Returns: the binary value converted to a hex string. If @out is given,
|
||||
* this always returns @out. If @out is %NULL, a newly allocated string
|
||||
* is returned.
|
||||
*/
|
||||
char *
|
||||
nm_utils_bin2hexstr_full (gconstpointer addr,
|
||||
gsize length,
|
||||
char delimiter,
|
||||
gboolean upper_case,
|
||||
char *out)
|
||||
{
|
||||
const guint8 *in = addr;
|
||||
const char *LOOKUP = upper_case ? "0123456789ABCDEF" : "0123456789abcdef";
|
||||
char *out0;
|
||||
|
||||
nm_assert (addr);
|
||||
nm_assert (length > 0);
|
||||
|
||||
if (out)
|
||||
out0 = out;
|
||||
else {
|
||||
out0 = out = g_new (char, delimiter == '\0'
|
||||
? length * 2 + 1
|
||||
: length * 3);
|
||||
}
|
||||
|
||||
/* @out must contain at least @length*3 bytes if @delimiter is set,
|
||||
* otherwise, @length*2+1. */
|
||||
|
||||
for (;;) {
|
||||
const guint8 v = *in++;
|
||||
|
||||
*out++ = LOOKUP[v >> 4];
|
||||
*out++ = LOOKUP[v & 0x0F];
|
||||
length--;
|
||||
if (!length)
|
||||
break;
|
||||
if (delimiter)
|
||||
*out++ = delimiter;
|
||||
}
|
||||
|
||||
*out = 0;
|
||||
return out0;
|
||||
}
|
||||
|
@@ -1122,4 +1122,10 @@ nm_strv_ptrarray_take_gstring (GPtrArray *cmd,
|
||||
|
||||
int nm_utils_getpagesize (void);
|
||||
|
||||
char *nm_utils_bin2hexstr_full (gconstpointer addr,
|
||||
gsize length,
|
||||
char delimiter,
|
||||
gboolean upper_case,
|
||||
char *out);
|
||||
|
||||
#endif /* __NM_SHARED_UTILS_H__ */
|
||||
|
@@ -14553,11 +14553,11 @@ nm_device_spawn_iface_helper (NMDevice *self)
|
||||
if (client_id) {
|
||||
g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid"));
|
||||
g_ptr_array_add (argv,
|
||||
_nm_utils_bin2hexstr_full (g_bytes_get_data (client_id, NULL),
|
||||
g_bytes_get_size (client_id),
|
||||
':',
|
||||
FALSE,
|
||||
NULL));
|
||||
nm_utils_bin2hexstr_full (g_bytes_get_data (client_id, NULL),
|
||||
g_bytes_get_size (client_id),
|
||||
':',
|
||||
FALSE,
|
||||
NULL));
|
||||
}
|
||||
|
||||
hostname = nm_dhcp_client_get_hostname (priv->dhcp4.client);
|
||||
@@ -14595,11 +14595,11 @@ nm_device_spawn_iface_helper (NMDevice *self)
|
||||
if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) {
|
||||
g_ptr_array_add (argv, g_strdup ("--iid"));
|
||||
g_ptr_array_add (argv,
|
||||
_nm_utils_bin2hexstr_full (iid.id_u8,
|
||||
sizeof (NMUtilsIPv6IfaceId),
|
||||
':',
|
||||
FALSE,
|
||||
NULL));
|
||||
nm_utils_bin2hexstr_full (iid.id_u8,
|
||||
sizeof (NMUtilsIPv6IfaceId),
|
||||
':',
|
||||
FALSE,
|
||||
NULL));
|
||||
}
|
||||
|
||||
g_ptr_array_add (argv, g_strdup ("--addr-gen-mode"));
|
||||
|
@@ -730,7 +730,7 @@ nm_dhcp_utils_duid_to_string (GBytes *duid)
|
||||
g_return_val_if_fail (duid, NULL);
|
||||
|
||||
data = g_bytes_get_data (duid, &len);
|
||||
return _nm_utils_bin2hexstr_full (data, len, ':', FALSE, NULL);
|
||||
return nm_utils_bin2hexstr_full (data, len, ':', FALSE, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -2391,11 +2391,11 @@ _uuid_data_init (UuidData *uuid_data,
|
||||
uuid_data->is_fake = is_fake;
|
||||
if (packed) {
|
||||
G_STATIC_ASSERT_EXPR (sizeof (uuid_data->str) >= (sizeof (*uuid) * 2 + 1));
|
||||
_nm_utils_bin2hexstr_full (uuid,
|
||||
sizeof (*uuid),
|
||||
'\0',
|
||||
FALSE,
|
||||
uuid_data->str);
|
||||
nm_utils_bin2hexstr_full (uuid,
|
||||
sizeof (*uuid),
|
||||
'\0',
|
||||
FALSE,
|
||||
uuid_data->str);
|
||||
} else {
|
||||
G_STATIC_ASSERT_EXPR (sizeof (uuid_data->str) >= 37);
|
||||
_nm_utils_uuid_unparse (uuid, uuid_data->str);
|
||||
|
@@ -2012,11 +2012,11 @@ test_machine_id_read (void)
|
||||
nmtst_logging_reenable (logstate);
|
||||
|
||||
g_assert (machine_id);
|
||||
g_assert (_nm_utils_bin2hexstr_full (machine_id,
|
||||
sizeof (NMUuid),
|
||||
'\0',
|
||||
FALSE,
|
||||
machine_id_str) == machine_id_str);
|
||||
g_assert (nm_utils_bin2hexstr_full (machine_id,
|
||||
sizeof (NMUuid),
|
||||
'\0',
|
||||
FALSE,
|
||||
machine_id_str) == machine_id_str);
|
||||
g_assert (strlen (machine_id_str) == 32);
|
||||
g_assert_cmpstr (machine_id_str, ==, nm_utils_machine_id_str ());
|
||||
|
||||
|
Reference in New Issue
Block a user