diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2019-01-28 16:56:46 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-02-21 09:36:17 +0100 |
commit | b5efcf08f4598004606181fe005e8a22127df61c (patch) | |
tree | 105fbe2b262422b55ca3fefb4ff9a542fc5a47d4 | |
parent | d2144019d84c3706446f28220c84b678c105d164 (diff) | |
download | NetworkManager-b5efcf08f4598004606181fe005e8a22127df61c.tar.gz |
all: move nm_utils_bin2hexstr_full() to shared
reuse++
-rw-r--r-- | libnm-core/nm-utils.c | 63 | ||||
-rw-r--r-- | shared/nm-utils/nm-shared-utils.c | 59 | ||||
-rw-r--r-- | shared/nm-utils/nm-shared-utils.h | 6 | ||||
-rw-r--r-- | src/devices/nm-device.c | 20 | ||||
-rw-r--r-- | src/dhcp/nm-dhcp-utils.c | 2 | ||||
-rw-r--r-- | src/nm-core-utils.c | 10 | ||||
-rw-r--r-- | src/tests/test-general.c | 10 |
7 files changed, 89 insertions, 81 deletions
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index c5855da196..cd354d036e 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -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); } /** diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index b22251afc8..f48513e02f 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -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; +} diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 4d9f20d073..c70ca86e86 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -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__ */ diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 2d4ae32cd9..6aa6da8e5c 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -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")); diff --git a/src/dhcp/nm-dhcp-utils.c b/src/dhcp/nm-dhcp-utils.c index 3d347427ca..5227eea777 100644 --- a/src/dhcp/nm-dhcp-utils.c +++ b/src/dhcp/nm-dhcp-utils.c @@ -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); } /** diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 6141c5f1cf..340bbcdb15 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -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); diff --git a/src/tests/test-general.c b/src/tests/test-general.c index f4bb820d9c..0dee566eaa 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -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 ()); |