From fc527a237cc9619a12e8abe14757087487b2e588 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 25 May 2016 10:46:48 +0200 Subject: [PATCH] libnm: add internal util function _nm_utils_hwaddr_length() --- libnm-core/nm-core-internal.h | 2 ++ libnm-core/nm-utils.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index 7ae126c1bc..96624d05b8 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -124,6 +124,8 @@ guint32 _nm_setting_get_setting_priority (NMSetting *setting); gboolean _nm_setting_get_property (NMSetting *setting, const char *name, GValue *value); +guint _nm_utils_hwaddr_length (const char *asc); + GSList * _nm_utils_hash_values_to_slist (GHashTable *hash); GHashTable *_nm_utils_copy_strdict (GHashTable *strdict); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 4bcde68474..21cbe337a1 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -3114,6 +3114,33 @@ hwaddr_binary_len (const char *asc) return octets; } +/** + * _nm_utils_hwaddr_length: + * @asc: the ASCII representation of the hardware address + * + * Validates that @asc is a valid representation of a hardware + * address up to (including) %NM_UTILS_HWADDR_LEN_MAX bytes. + * + * Returns: binary length of the hardware address @asc or + * 0 on error. + */ +guint +_nm_utils_hwaddr_length (const char *asc) +{ + int l; + + if (!asc) + return 0; + + l = hwaddr_binary_len (asc); + if (l <= 0 || l > NM_UTILS_HWADDR_LEN_MAX) + return 0; + + if (!nm_utils_hwaddr_valid (asc, l)) + return 0; + return l; +} + /** * nm_utils_hwaddr_valid: * @asc: the ASCII representation of a hardware address