diff options
author | Thomas Haller <thaller@redhat.com> | 2017-03-20 16:04:29 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-03-22 12:41:06 +0100 |
commit | 6808bf8195d427975638610781f8c5384228218d (patch) | |
tree | 7b89193b8f4602e266ca3411681535cf19b1eb0d | |
parent | c7ccdf5fc893fd41f23349f298bf2c3ea383487a (diff) | |
download | NetworkManager-6808bf8195d427975638610781f8c5384228218d.tar.gz |
udev: add and use nm_udev_utils_property_decode() function
DRY.
-rw-r--r-- | libnm-glib/nm-device.c | 29 | ||||
-rw-r--r-- | libnm/nm-device.c | 43 | ||||
-rw-r--r-- | shared/nm-utils/nm-udev-utils.c | 53 | ||||
-rw-r--r-- | shared/nm-utils/nm-udev-utils.h | 2 |
4 files changed, 58 insertions, 69 deletions
diff --git a/libnm-glib/nm-device.c b/libnm-glib/nm-device.c index 81af5b6cff..be86a2464e 100644 --- a/libnm-glib/nm-device.c +++ b/libnm-glib/nm-device.c @@ -1503,33 +1503,6 @@ nm_device_get_available_connections (NMDevice *device) return handle_ptr_array_return (NM_DEVICE_GET_PRIVATE (device)->available_connections); } -static char * -get_decoded_property (struct udev_device *device, const char *property) -{ - const char *orig, *p; - char *unescaped, *n; - guint len; - - p = orig = udev_device_get_property_value (device, property); - if (!orig) - return NULL; - - len = strlen (orig); - n = unescaped = g_malloc0 (len + 1); - while (*p) { - if ((len >= 4) && (*p == '\\') && (*(p+1) == 'x')) { - *n++ = (char) nm_utils_hex2byte (p + 2); - p += 4; - len -= 4; - } else { - *n++ = *p++; - len--; - } - } - - return unescaped; -} - static gboolean ensure_udev_client (NMDevice *device) { @@ -1572,7 +1545,7 @@ _get_udev_property (NMDevice *device, tmpdev = udev_device; while ((count++ < 3) && tmpdev && !enc_value) { if (!enc_value) - enc_value = get_decoded_property (tmpdev, enc_prop); + enc_value = nm_udev_utils_property_decode_cp (udev_device_get_property_value (tmpdev, enc_prop)); if (!db_value) db_value = g_strdup (udev_device_get_property_value (tmpdev, db_prop)); diff --git a/libnm/nm-device.c b/libnm/nm-device.c index 5fba2e2000..9cbdbd0069 100644 --- a/libnm/nm-device.c +++ b/libnm/nm-device.c @@ -40,6 +40,7 @@ #include "nm-dbus-helpers.h" #include "nm-device-tun.h" #include "nm-setting-connection.h" +#include "shared/nm-utils/nm-udev-utils.h" #include "introspection/org.freedesktop.NetworkManager.Device.h" @@ -1292,46 +1293,6 @@ nm_device_get_available_connections (NMDevice *device) return NM_DEVICE_GET_PRIVATE (device)->available_connections; } -static inline guint8 -hex2byte (const char *hex) -{ - int a, b; - a = g_ascii_xdigit_value (*hex++); - if (a < 0) - return -1; - b = g_ascii_xdigit_value (*hex++); - if (b < 0) - return -1; - return (a << 4) | b; -} - -static char * -get_decoded_property (struct udev_device *device, const char *property) -{ - const char *orig, *p; - char *unescaped, *n; - guint len; - - p = orig = udev_device_get_property_value (device, property); - if (!orig) - return NULL; - - len = strlen (orig); - n = unescaped = g_malloc0 (len + 1); - while (*p) { - if ((len >= 4) && (*p == '\\') && (*(p+1) == 'x')) { - *n++ = (char) hex2byte (p + 2); - p += 4; - len -= 4; - } else { - *n++ = *p++; - len--; - } - } - - return unescaped; -} - void _nm_device_set_udev (NMDevice *device, struct udev *udev) { @@ -1377,7 +1338,7 @@ _get_udev_property (NMDevice *device, tmpdev = udev_device; while ((count++ < 3) && tmpdev && !enc_value) { if (!enc_value) - enc_value = get_decoded_property (tmpdev, enc_prop); + enc_value = nm_udev_utils_property_decode_cp (udev_device_get_property_value (tmpdev, enc_prop)); if (!db_value) db_value = g_strdup (udev_device_get_property_value (tmpdev, db_prop)); diff --git a/shared/nm-utils/nm-udev-utils.c b/shared/nm-utils/nm-udev-utils.c index 857954b1dd..5552d592db 100644 --- a/shared/nm-utils/nm-udev-utils.c +++ b/shared/nm-utils/nm-udev-utils.c @@ -48,6 +48,59 @@ nm_udev_utils_property_as_boolean (const char *uproperty) return FALSE; } +const char * +nm_udev_utils_property_decode (const char *uproperty, char **to_free) +{ + const char *p; + char *unescaped = NULL; + char *n = NULL; + + if (!uproperty) { + *to_free = NULL; + return NULL; + } + + p = uproperty; + while (*p) { + int a, b; + + if ( p[0] == '\\' + && p[1] == 'x' + && (a = g_ascii_xdigit_value (p[2])) >= 0 + && (b = g_ascii_xdigit_value (p[3])) >= 0) { + if (!unescaped) { + gssize l = p - uproperty; + + unescaped = g_malloc (l + strlen (p) + 1 - 3); + memcpy (unescaped, uproperty, l); + n = &unescaped[l]; + } + *n++ = (a << 4) | b; + p += 4; + } else { + if (n) + *n++ = *p; + p++; + } + } + + if (!unescaped) { + *to_free = NULL; + return uproperty; + } + + return (*to_free = unescaped); +} + +char * +nm_udev_utils_property_decode_cp (const char *uproperty) +{ + char *cpy; + + uproperty = nm_udev_utils_property_decode (uproperty, &cpy); + return cpy ?: g_strdup (uproperty); +} + /*****************************************************************************/ static void diff --git a/shared/nm-utils/nm-udev-utils.h b/shared/nm-utils/nm-udev-utils.h index 5f6e383f15..911e8a2702 100644 --- a/shared/nm-utils/nm-udev-utils.h +++ b/shared/nm-utils/nm-udev-utils.h @@ -26,6 +26,8 @@ struct udev_device; struct udev_enumerate; gboolean nm_udev_utils_property_as_boolean (const char *uproperty); +const char *nm_udev_utils_property_decode (const char *uproperty, char **to_free); +char *nm_udev_utils_property_decode_cp (const char *uproperty); typedef struct _NMPUdevClient NMUdevClient; |