all: move nm_utils_bin2hexstr_full() to shared

reuse++
This commit is contained in:
Beniamino Galvani
2019-01-28 16:56:46 +01:00
committed by Thomas Haller
parent d2144019d8
commit b5efcf08f4
7 changed files with 89 additions and 81 deletions

View File

@@ -4131,64 +4131,7 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length)
return buffer; 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: * nm_utils_bin2hexstr:
@@ -4214,7 +4157,7 @@ nm_utils_bin2hexstr (gconstpointer src, gsize len, int final_len)
result = g_malloc (buflen); 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 */ /* Cut converted key off at the correct length for this cipher type */
if (final_len >= 0 && (gsize) final_len < buflen) 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 (addr, g_strdup (""));
g_return_val_if_fail (length > 0, 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 * 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) if (buf_len < addr_len * 3)
g_return_val_if_reached (NULL); 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);
} }
/** /**

View File

@@ -2477,3 +2477,62 @@ nm_utils_memeqzero (gconstpointer data, gsize length)
/* Now we know that's zero, memcmp with self. */ /* Now we know that's zero, memcmp with self. */
return memcmp (data, p, length) == 0; 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;
}

View File

@@ -1122,4 +1122,10 @@ nm_strv_ptrarray_take_gstring (GPtrArray *cmd,
int nm_utils_getpagesize (void); 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__ */ #endif /* __NM_SHARED_UTILS_H__ */

View File

@@ -14553,7 +14553,7 @@ nm_device_spawn_iface_helper (NMDevice *self)
if (client_id) { if (client_id) {
g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid")); g_ptr_array_add (argv, g_strdup ("--dhcp4-clientid"));
g_ptr_array_add (argv, g_ptr_array_add (argv,
_nm_utils_bin2hexstr_full (g_bytes_get_data (client_id, NULL), nm_utils_bin2hexstr_full (g_bytes_get_data (client_id, NULL),
g_bytes_get_size (client_id), g_bytes_get_size (client_id),
':', ':',
FALSE, FALSE,
@@ -14595,7 +14595,7 @@ nm_device_spawn_iface_helper (NMDevice *self)
if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) { if (nm_device_get_ip_iface_identifier (self, &iid, FALSE)) {
g_ptr_array_add (argv, g_strdup ("--iid")); g_ptr_array_add (argv, g_strdup ("--iid"));
g_ptr_array_add (argv, g_ptr_array_add (argv,
_nm_utils_bin2hexstr_full (iid.id_u8, nm_utils_bin2hexstr_full (iid.id_u8,
sizeof (NMUtilsIPv6IfaceId), sizeof (NMUtilsIPv6IfaceId),
':', ':',
FALSE, FALSE,

View File

@@ -730,7 +730,7 @@ nm_dhcp_utils_duid_to_string (GBytes *duid)
g_return_val_if_fail (duid, NULL); g_return_val_if_fail (duid, NULL);
data = g_bytes_get_data (duid, &len); 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);
} }
/** /**

View File

@@ -2391,7 +2391,7 @@ _uuid_data_init (UuidData *uuid_data,
uuid_data->is_fake = is_fake; uuid_data->is_fake = is_fake;
if (packed) { if (packed) {
G_STATIC_ASSERT_EXPR (sizeof (uuid_data->str) >= (sizeof (*uuid) * 2 + 1)); G_STATIC_ASSERT_EXPR (sizeof (uuid_data->str) >= (sizeof (*uuid) * 2 + 1));
_nm_utils_bin2hexstr_full (uuid, nm_utils_bin2hexstr_full (uuid,
sizeof (*uuid), sizeof (*uuid),
'\0', '\0',
FALSE, FALSE,

View File

@@ -2012,7 +2012,7 @@ test_machine_id_read (void)
nmtst_logging_reenable (logstate); nmtst_logging_reenable (logstate);
g_assert (machine_id); g_assert (machine_id);
g_assert (_nm_utils_bin2hexstr_full (machine_id, g_assert (nm_utils_bin2hexstr_full (machine_id,
sizeof (NMUuid), sizeof (NMUuid),
'\0', '\0',
FALSE, FALSE,