From e0bbaf6a397aea4980437088ad11f6402c504d73 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Thu, 9 Aug 2018 17:53:59 +0200 Subject: [PATCH] shared: add space escape functions --- libnm-core/tests/test-general.c | 49 ++++++++++++++++++++++++++ shared/nm-utils/nm-shared-utils.c | 57 +++++++++++++++++++++++++++++++ shared/nm-utils/nm-shared-utils.h | 3 ++ 3 files changed, 109 insertions(+) diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index db1d1c1447..16bb5d7113 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -7074,6 +7074,53 @@ test_ethtool_offload (void) g_assert_cmpstr (d->optname, ==, NM_ETHTOOL_OPTNAME_FEATURE_RXHASH); } +static void +test_nm_utils_escape_spaces (void) +{ + char *to_free; + + g_assert_cmpstr (_nm_utils_escape_spaces (NULL, &to_free), ==, NULL); + g_free (to_free); + + g_assert_cmpstr (_nm_utils_escape_spaces ("", &to_free), ==, ""); + g_free (to_free); + + g_assert_cmpstr (_nm_utils_escape_spaces (" ", &to_free), ==, "\\ "); + g_free (to_free); + + g_assert_cmpstr (_nm_utils_escape_spaces ("\t ", &to_free), ==, "\\\t\\ "); + g_free (to_free); + + g_assert_cmpstr (_nm_utils_escape_spaces ("abc", &to_free), ==, "abc"); + g_free (to_free); + + g_assert_cmpstr (_nm_utils_escape_spaces ("abc def", &to_free), ==, "abc\\ def"); + g_free (to_free); + + g_assert_cmpstr (_nm_utils_escape_spaces ("abc\tdef", &to_free), ==, "abc\\\tdef"); + g_free (to_free); +} + +static void +test_nm_utils_unescape_spaces (void) +{ +#define CHECK_STR(in, out) \ + G_STMT_START { \ + gs_free char *str = g_strdup (in); \ + \ + g_assert_cmpstr (_nm_utils_unescape_spaces (str), ==, out); \ + } G_STMT_END + + CHECK_STR ("\\a", "\\a"); + CHECK_STR ("foobar", "foobar"); + CHECK_STR ("foo bar", "foo bar"); + CHECK_STR ("foo\\ bar", "foo bar"); + CHECK_STR ("foo\\", "foo\\"); + CHECK_STR ("\\\\\t", "\\\t"); + +#undef CHECK_STR +} + /*****************************************************************************/ NMTST_DEFINE (); @@ -7224,6 +7271,8 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/_nm_utils_dns_option_find_idx", test_nm_utils_dns_option_find_idx); g_test_add_func ("/core/general/_nm_utils_validate_json", test_nm_utils_check_valid_json); g_test_add_func ("/core/general/_nm_utils_team_config_equal", test_nm_utils_team_config_equal); + g_test_add_func ("/core/general/_nm_utils_escape_spaces", test_nm_utils_escape_spaces); + g_test_add_func ("/core/general/_nm_utils_unescape_spaces", test_nm_utils_unescape_spaces); g_test_add_func ("/core/general/test_nm_utils_enum", test_nm_utils_enum); g_test_add_func ("/core/general/nm-set-out", test_nm_set_out); g_test_add_func ("/core/general/route_attributes/parse", test_route_attributes_parse); diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 34c93c8108..4e2cdbb98a 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -1702,3 +1702,60 @@ _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...) g_slice_free1 (((gsize) nargs) * sizeof (gconstpointer), user_data); } + +/*****************************************************************************/ + +#define IS_SPACE(c) NM_IN_SET ((c), ' ', '\t') + +const char * +_nm_utils_escape_spaces (const char *str, char **to_free) +{ + const char *ptr = str; + char *ret, *r; + + *to_free = NULL; + + if (!str) + return NULL; + + while (TRUE) { + if (!*ptr) + return str; + if (IS_SPACE (*ptr)) + break; + ptr++; + } + + ptr = str; + ret = g_new (char, strlen (str) * 2 + 1); + r = ret; + *to_free = ret; + while (*ptr) { + if (IS_SPACE (*ptr)) + *r++ = '\\'; + *r++ = *ptr++; + } + *r = '\0'; + + return ret; +} + +char * +_nm_utils_unescape_spaces (char *str) +{ + guint i, j = 0; + + if (!str) + return NULL; + + for (i = 0; str[i]; i++) { + if (str[i] == '\\' && IS_SPACE (str[i+1])) + i++; + str[j++] = str[i]; + } + str[j] = '\0'; + + return str; +} + +#undef IS_SPACE diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 6ee77b989f..ef7225e1bb 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -718,4 +718,7 @@ void _nm_utils_user_data_unpack (gpointer user_data, int nargs, ...); /*****************************************************************************/ +const char *_nm_utils_escape_spaces (const char *str, char **to_free); +char *_nm_utils_unescape_spaces (char *str); + #endif /* __NM_SHARED_UTILS_H__ */