shared: move _nm_utils_hwaddr_aton() to shared/

_nm_utils_hwaddr_aton() is only a wrapper around nm_utils_hexstr2bin_full().
But it abstracts the "right" parameters for what we consider a valid MAC
address and what not. As such, this function is useful.

Move it to "shared/" and replace the dupicate macro hwaddr_aton() with
it.
This commit is contained in:
Thomas Haller 2020-09-23 12:02:49 +02:00
parent d5d45c8ce6
commit 74c03da086
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 37 additions and 40 deletions

View file

@ -339,7 +339,6 @@ guint nm_setting_ethtool_init_features (NMSettingEthtool *setting,
#define NM_UTILS_HWADDR_LEN_MAX_STR (NM_UTILS_HWADDR_LEN_MAX * 3)
guint8 *_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length);
const char *nm_utils_hwaddr_ntoa_buf (gconstpointer addr, gsize addr_len, gboolean upper_case, char *buf, gsize buf_len);
gboolean nm_utils_is_valid_iface_name_utf8safe (const char *utf8safe_name);

View file

@ -4041,8 +4041,6 @@ nm_utils_hexstr2bin (const char *hex)
return g_bytes_new_take (buffer, len);
}
#define hwaddr_aton(asc, buffer, buffer_len, out_len) nm_utils_hexstr2bin_full ((asc), FALSE, TRUE, FALSE, ":-", 0, (buffer), (buffer_len), (out_len))
/**
* nm_utils_hwaddr_atoba:
* @asc: the ASCII representation of a hardware address
@ -4065,7 +4063,7 @@ nm_utils_hwaddr_atoba (const char *asc, gsize length)
ba = g_byte_array_sized_new (length);
g_byte_array_set_size (ba, length);
if (!hwaddr_aton (asc, ba->data, length, &l))
if (!_nm_utils_hwaddr_aton (asc, ba->data, length, &l))
goto fail;
if (length != l)
goto fail;
@ -4076,36 +4074,6 @@ fail:
return NULL;
}
/**
* _nm_utils_hwaddr_aton:
* @asc: the ASCII representation of a hardware address
* @buffer: buffer to store the result into. Must have
* at least a size of @buffer_length.
* @buffer_length: the length of the input buffer @buffer.
* The result must fit into that buffer, otherwise
* the function fails and returns %NULL.
* @out_length: the output length in case of success.
*
* Parses @asc and converts it to binary form in @buffer.
* Bytes in @asc can be separated by colons (:), or hyphens (-), but not mixed.
*
* It is like nm_utils_hwaddr_aton(), but contrary to that it
* can parse addresses of any length. That is, you don't need
* to know the length before-hand.
*
* Return value: @buffer, or %NULL if @asc couldn't be parsed.
*/
guint8 *
_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length)
{
g_return_val_if_fail (asc, NULL);
g_return_val_if_fail (buffer, NULL);
g_return_val_if_fail (buffer_length > 0, NULL);
g_return_val_if_fail (out_length, NULL);
return hwaddr_aton (asc, buffer, buffer_length, out_length);
}
/**
* nm_utils_hwaddr_aton:
* @asc: the ASCII representation of a hardware address
@ -4128,7 +4096,7 @@ nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize length)
g_return_val_if_fail (buffer, NULL);
g_return_val_if_fail (length > 0 && length <= NM_UTILS_HWADDR_LEN_MAX, NULL);
if (!hwaddr_aton (asc, buffer, length, &l))
if (!_nm_utils_hwaddr_aton (asc, buffer, length, &l))
return NULL;
if (length != l)
return NULL;
@ -4223,7 +4191,7 @@ nm_utils_hwaddr_valid (const char *asc, gssize length)
if (length == 0)
return FALSE;
if (!hwaddr_aton (asc, buf, sizeof (buf), &l))
if (!_nm_utils_hwaddr_aton (asc, buf, sizeof (buf), &l))
return FALSE;
return length == -1
@ -4254,7 +4222,7 @@ nm_utils_hwaddr_canonical (const char *asc, gssize length)
|| ( length > 0
&& length <= NM_UTILS_HWADDR_LEN_MAX), NULL);
if (!hwaddr_aton (asc, buf, sizeof (buf), &l))
if (!_nm_utils_hwaddr_aton (asc, buf, sizeof (buf), &l))
return NULL;
if ( length != -1
&& length != (gssize) l)
@ -4363,7 +4331,7 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1,
if (hwaddr1_len == -1) {
if (hwaddr1 == NULL) {
hwaddr1_len = 0;
} else if (hwaddr_aton (hwaddr1, buf1, sizeof (buf1), &l)) {
} else if (_nm_utils_hwaddr_aton (hwaddr1, buf1, sizeof (buf1), &l)) {
hwaddr1 = buf1;
hwaddr1_len = l;
} else {
@ -4383,7 +4351,7 @@ nm_utils_hwaddr_matches (gconstpointer hwaddr1,
if (hwaddr2_len == -1) {
if (hwaddr2 == NULL)
l = 0;
else if (!hwaddr_aton (hwaddr2, buf2, sizeof (buf2), &l))
else if (!_nm_utils_hwaddr_aton (hwaddr2, buf2, sizeof (buf2), &l))
return FALSE;
if (l != hwaddr1_len)
return FALSE;
@ -4427,7 +4395,7 @@ _nm_utils_hwaddr_to_dbus_impl (const char *str)
if (!str)
return NULL;
if (!hwaddr_aton (str, buf, sizeof (buf), &len))
if (!_nm_utils_hwaddr_aton (str, buf, sizeof (buf), &len))
return NULL;
return g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, buf, len, 1);

View file

@ -2037,6 +2037,36 @@ guint8 *nm_utils_hexstr2bin_alloc (const char *hexstr,
gsize required_len,
gsize *out_len);
/**
* _nm_utils_hwaddr_aton:
* @asc: the ASCII representation of a hardware address
* @buffer: buffer to store the result into. Must have
* at least a size of @buffer_length.
* @buffer_length: the length of the input buffer @buffer.
* The result must fit into that buffer, otherwise
* the function fails and returns %NULL.
* @out_length: the output length in case of success.
*
* Parses @asc and converts it to binary form in @buffer.
* Bytes in @asc can be separated by colons (:), or hyphens (-), but not mixed.
*
* It is like nm_utils_hwaddr_aton(), but contrary to that it
* can parse addresses of any length. That is, you don't need
* to know the length before-hand.
*
* Return value: @buffer, or %NULL if @asc couldn't be parsed.
*/
static inline guint8 *
_nm_utils_hwaddr_aton (const char *asc, gpointer buffer, gsize buffer_length, gsize *out_length)
{
g_return_val_if_fail (asc, NULL);
g_return_val_if_fail (buffer, NULL);
g_return_val_if_fail (buffer_length > 0, NULL);
g_return_val_if_fail (out_length, NULL);
return nm_utils_hexstr2bin_full (asc, FALSE, TRUE, FALSE, ":-", 0, buffer, buffer_length, out_length);
}
/*****************************************************************************/
#define _NM_UTILS_STRING_TABLE_LOOKUP_DEFINE(fcn_name, \