udev: add and use nm_udev_utils_property_decode() function

DRY.
This commit is contained in:
Thomas Haller 2017-03-20 16:04:29 +01:00
parent c7ccdf5fc8
commit 6808bf8195
4 changed files with 58 additions and 69 deletions

View file

@ -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));

View file

@ -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));

View file

@ -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

View file

@ -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;