[network-manager-openvpn/nm-1-2] properties: reuse nm_utils_str_utf8safe_*() from shared/nm-utils



commit 9c4c914fcb2853885b3e43f0a2b26765917d274d
Author: Thomas Haller <thaller redhat com>
Date:   Fri May 19 11:34:45 2017 +0200

    properties: reuse nm_utils_str_utf8safe_*() from shared/nm-utils
    
    (cherry picked from commit 3b30176e41f279b8ba69a95e22268a11ec35385f)

 properties/import-export.c            |   20 +++---
 properties/tests/test-import-export.c |   77 -----------------------
 shared/utils.c                        |  111 ---------------------------------
 shared/utils.h                        |    5 --
 src/nm-openvpn-service.c              |   12 ++--
 5 files changed, 16 insertions(+), 209 deletions(-)
---
diff --git a/properties/import-export.c b/properties/import-export.c
index 7e48ab6..c796d35 100644
--- a/properties/import-export.c
+++ b/properties/import-export.c
@@ -140,7 +140,7 @@ setting_vpn_add_data_item_utf8safe (NMSettingVpn *setting,
        g_return_if_fail (value && value[0]);
 
        nm_setting_vpn_add_data_item (setting, key,
-                                     nmv_utils_str_utf8safe_escape_c (value, &s));
+                                     nm_utils_str_utf8safe_escape (value, 0, &s));
 }
 
 static void
@@ -165,7 +165,7 @@ setting_vpn_eq_data_item_utf8safe (NMSettingVpn *setting,
 
        if (!expected_value)
                return FALSE;
-       return nm_streq (expected_value, nmv_utils_str_utf8safe_unescape_c (s, &s_free));
+       return nm_streq (expected_value, nm_utils_str_utf8safe_unescape (s, &s_free));
 }
 
 /*****************************************************************************/
@@ -1577,7 +1577,7 @@ handle_line_error:
                const char *key_path;
 
                key_path = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_KEY);
-               if (is_encrypted (nmv_utils_str_utf8safe_unescape_c (key_path, &key_path_free))) {
+               if (is_encrypted (nm_utils_str_utf8safe_unescape (key_path, &key_path_free))) {
                        /* If there should be a private key password, default it to
                         * being agent-owned.
                         */
@@ -1840,18 +1840,18 @@ do_export_create (NMConnection *connection, const char *path, GError **error)
                                                   NM_OPENVPN_CONTYPE_PASSWORD_TLS)) {
                        value = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_CA);
                        if (_arg_is_set (value))
-                               cacert = nmv_utils_str_utf8safe_unescape_c (value, &cacert_free);
+                               cacert = nm_utils_str_utf8safe_unescape (value, &cacert_free);
                }
 
                if (NM_IN_STRSET (connection_type, NM_OPENVPN_CONTYPE_TLS,
                                                   NM_OPENVPN_CONTYPE_PASSWORD_TLS)) {
                        value = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_CERT);
                        if (_arg_is_set (value))
-                               user_cert = nmv_utils_str_utf8safe_unescape_c (value, &user_cert_free);
+                               user_cert = nm_utils_str_utf8safe_unescape (value, &user_cert_free);
 
                        value = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_KEY);
                        if (_arg_is_set (value))
-                               private_key = nmv_utils_str_utf8safe_unescape_c (value, &private_key_free);
+                               private_key = nm_utils_str_utf8safe_unescape (value, &private_key_free);
                }
 
                if (   cacert && user_cert && private_key
@@ -1879,7 +1879,7 @@ do_export_create (NMConnection *connection, const char *path, GError **error)
 
                        args_write_line (f,
                                         NMV_OVPN_TAG_SECRET,
-                                        nmv_utils_str_utf8safe_unescape_c (value, &s_free),
+                                        nm_utils_str_utf8safe_unescape (value, &s_free),
                                         _arg_is_set (nm_setting_vpn_get_data_item (s_vpn, 
NM_OPENVPN_KEY_STATIC_KEY_DIRECTION)));
                }
        }
@@ -1922,7 +1922,7 @@ do_export_create (NMConnection *connection, const char *path, GError **error)
 
                device_type = _arg_is_set (nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_DEV_TYPE));
                device = _arg_is_set (nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_DEV));
-               device = nmv_utils_str_utf8safe_unescape_c (device, &device_free);
+               device = nm_utils_str_utf8safe_unescape (device, &device_free);
                args_write_line (f,
                                 NMV_OVPN_TAG_DEV,
                                 device ?:
@@ -1980,7 +1980,7 @@ do_export_create (NMConnection *connection, const char *path, GError **error)
                        gs_free char *s_free = NULL;
                        args_write_line (f,
                                         NMV_OVPN_TAG_TLS_AUTH,
-                                        nmv_utils_str_utf8safe_unescape_c (key, &s_free),
+                                        nm_utils_str_utf8safe_unescape (key, &s_free),
                                         _arg_is_set (nm_setting_vpn_get_data_item (s_vpn, 
NM_OPENVPN_KEY_TA_DIR)));
                }
 
@@ -1989,7 +1989,7 @@ do_export_create (NMConnection *connection, const char *path, GError **error)
                        gs_free char *s_free = NULL;
                        args_write_line (f,
                                         NMV_OVPN_TAG_TLS_CRYPT,
-                                        nmv_utils_str_utf8safe_unescape_c (key, &s_free));
+                                        nm_utils_str_utf8safe_unescape (key, &s_free));
                }
 
        }
diff --git a/properties/tests/test-import-export.c b/properties/tests/test-import-export.c
index 116c066..d541d08 100644
--- a/properties/tests/test-import-export.c
+++ b/properties/tests/test-import-export.c
@@ -1075,81 +1075,6 @@ test_args_parse_line (void)
 
 /*****************************************************************************/
 
-static void
-do_test_utils_str_utf8safe (const char *str, const char *expected)
-{
-       const char *str_safe, *s;
-       gs_free char *str2 = NULL;
-       gs_free char *str3 = NULL;
-
-       str_safe = nmv_utils_str_utf8safe_escape_c (str, &str2);
-
-       str3 = nmv_utils_str_utf8safe_escape (str);
-       g_assert_cmpstr (str3, ==, str_safe);
-       g_assert ((!str && !str3) || (str != str3));
-       g_clear_pointer (&str3, g_free);
-
-       if (expected == NULL) {
-               g_assert (str_safe == str);
-               g_assert (!str2);
-               if (str) {
-                       g_assert (!strchr (str, '\\'));
-                       g_assert (g_utf8_validate (str, -1, NULL));
-               }
-
-               g_assert (str == nmv_utils_str_utf8safe_unescape_c (str_safe, &str3));
-               g_assert (!str3);
-
-               str3 = nmv_utils_str_utf8safe_unescape (str_safe);
-               if (str) {
-                       g_assert (str3 != str);
-                       g_assert_cmpstr (str3, ==, str);
-               } else
-                       g_assert (!str3);
-               g_clear_pointer (&str3, g_free);
-               return;
-       }
-
-       g_assert (str);
-       g_assert (str_safe != str);
-       g_assert (str_safe == str2);
-       g_assert (strchr (str, '\\') || !g_utf8_validate (str, -1, NULL));
-       g_assert (g_utf8_validate (str_safe, -1, NULL));
-
-       str3 = g_strcompress (str_safe);
-       g_assert_cmpstr (str, ==, str3);
-       g_clear_pointer (&str3, g_free);
-
-       str3 = nmv_utils_str_utf8safe_unescape (str_safe);
-       g_assert (str3 != str);
-       g_assert_cmpstr (str3, ==, str);
-       g_clear_pointer (&str3, g_free);
-
-       s = nmv_utils_str_utf8safe_unescape_c (str_safe, &str3);
-       g_assert (str3 != str);
-       g_assert (s == str3);
-       g_assert_cmpstr (str3, ==, str);
-       g_clear_pointer (&str3, g_free);
-
-       g_assert_cmpstr (str_safe, ==, expected);
-}
-
-static void
-test_utils_str_utf8safe (void)
-{
-       do_test_utils_str_utf8safe (NULL, NULL);
-       do_test_utils_str_utf8safe ("", NULL);
-       do_test_utils_str_utf8safe ("a", NULL);
-       do_test_utils_str_utf8safe ("ab", NULL);
-       do_test_utils_str_utf8safe ("abäb", NULL);
-       do_test_utils_str_utf8safe ("㈞abä㈞b", NULL);
-       do_test_utils_str_utf8safe ("Äab\\äb", "Äab\\\\äb");
-       do_test_utils_str_utf8safe ("ÄÄab\\äb", "ÄÄab\\\\äb");
-       do_test_utils_str_utf8safe ("Ä\304ab\\äb", "Ä\\304ab\\\\äb");
-}
-
-/*****************************************************************************/
-
 NMTST_DEFINE ();
 
 int main (int argc, char **argv)
@@ -1230,8 +1155,6 @@ int main (int argc, char **argv)
 
        _add_test_func_simple (test_args_parse_line);
 
-       _add_test_func_simple (test_utils_str_utf8safe);
-
        result = g_test_run ();
        if (result != EXIT_SUCCESS)
                return result;
diff --git a/shared/utils.c b/shared/utils.c
index f61cd42..5565d4d 100644
--- a/shared/utils.c
+++ b/shared/utils.c
@@ -92,114 +92,3 @@ is_encrypted (const char *filename)
 }
 
 /*****************************************************************************/
-
-/**
- * nmv_utils_str_utf8safe_escape:
- * @str: NUL terminated input string, possibly in utf-8 encoding
- *
- * Does something similar like g_strescape(), where the operation
- * can be reverted by g_strcompress(). However, the UTF-8 characters
- * are not escaped at all (except the escape character '\\'). It only
- * escapes non-UTF-8 characters. This way it is possible to transfer
- * the string as UTF-8 via D-Bus.
- * Also, it can be directly displayed to the user and will show as
- * UTF-8, with exception of the escape character and characters in
- * different encodings.
- *
- * Returns: the escaped input string in UTF-8 encoding. The returned
- *   value should be freed with g_free().
- *   The escaping can be reverted by g_strcompress().
- **/
-char *
-nmv_utils_str_utf8safe_escape (const char *str)
-{
-       char *s = NULL;
-
-       nmv_utils_str_utf8safe_escape_c (str, &s);
-       return s ? : g_strdup (str);
-}
-
-/**
- * nmv_utils_str_utf8safe_escape_c:
- * @str: NUL terminated input string, possibly in utf-8 encoding
- * @str_free: (out): return the pointer location of the string
- *   if a copying was necessary.
- *
- * Like nmv_utils_str_utf8safe_escape(), except that the string
- * is only copied if it is actually necessary. In that case,
- * @str_free will contain the allocated string which must be
- * freed with g_free().
- * Otherwise, @str_free is %NULL and the input string is returned.
- *
- * Returns: the escaped input string. If no escaping is necessary,
- *   it returns @str. Otherwise, an allocated string @str_free is
- *   returned.
- *   The escaping can be reverted by g_strcompress().
- **/
-const char *
-nmv_utils_str_utf8safe_escape_c (const char *str, char **str_free)
-{
-       const char *p = NULL;
-       guchar ch;
-       GString *s;
-
-       g_return_val_if_fail (str_free, NULL);
-
-       *str_free = NULL;
-       if (!str || !str[0])
-               return str;
-
-       if (g_utf8_validate (str, -1, &p)) {
-               if (!strchr (str, '\\'))
-                       return str;
-       }
-
-       s = g_string_sized_new (30);
-
-       do {
-               for (; str < p; str++) {
-                       if (str[0] == '\\')
-                               g_string_append (s, "\\\\");
-                       else
-                               g_string_append_c (s, str[0]);
-               }
-
-               ch = p[0];
-               if (ch == '\0')
-                       break;
-               g_string_append_c (s, '\\');
-               g_string_append_c (s, '0' + ((ch >> 6) & 07));
-               g_string_append_c (s, '0' + ((ch >> 3) & 07));
-               g_string_append_c (s, '0' + ( ch       & 07));
-
-               str = &p[1];
-               g_utf8_validate (str, -1, &p);
-       } while (TRUE);
-
-       *str_free = g_string_free (s, FALSE);
-       return *str_free;
-}
-
-char *
-nmv_utils_str_utf8safe_unescape (const char *str)
-{
-       if (!str)
-               return NULL;
-       return g_strcompress (str);
-}
-
-const char *
-nmv_utils_str_utf8safe_unescape_c (const char *str, char **str_free)
-{
-       g_return_val_if_fail (str_free, NULL);
-
-       if (!str || !strchr (str, '\\')) {
-               *str_free = NULL;
-               return str;
-       }
-       *str_free = g_strcompress (str);
-       return *str_free;
-}
-
-/*****************************************************************************/
-
diff --git a/shared/utils.h b/shared/utils.h
index cb16d32..ed4ae98 100644
--- a/shared/utils.h
+++ b/shared/utils.h
@@ -80,9 +80,4 @@ gboolean is_pkcs12 (const char *filepath);
 
 gboolean is_encrypted (const char *filename);
 
-char *      nmv_utils_str_utf8safe_escape     (const char *str);
-const char *nmv_utils_str_utf8safe_escape_c   (const char *str, char **out_clone);
-char *      nmv_utils_str_utf8safe_unescape   (const char *str);
-const char *nmv_utils_str_utf8safe_unescape_c (const char *str, char **str_free);
-
 #endif  /* UTILS_H */
diff --git a/src/nm-openvpn-service.c b/src/nm-openvpn-service.c
index aa12475..ced6f9d 100644
--- a/src/nm-openvpn-service.c
+++ b/src/nm-openvpn-service.c
@@ -1061,7 +1061,7 @@ add_openvpn_arg_utf8safe (GPtrArray *args, const char *arg)
        g_return_val_if_fail (args, NULL);
        g_return_val_if_fail (arg, NULL);
 
-       arg_unescaped = nmv_utils_str_utf8safe_unescape (arg);
+       arg_unescaped = nm_utils_str_utf8safe_unescape_cp (arg);
        g_ptr_array_add (args, arg_unescaped);
        return arg_unescaped;
 }
@@ -1098,9 +1098,9 @@ add_cert_args (GPtrArray *args, NMSettingVpn *s_vpn)
        cert = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_CERT);
        key  = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_KEY);
 
-       ca   = nmv_utils_str_utf8safe_unescape_c (ca,   &ca_free);
-       cert = nmv_utils_str_utf8safe_unescape_c (cert, &cert_free);
-       key  = nmv_utils_str_utf8safe_unescape_c (key,  &key_free);
+       ca   = nm_utils_str_utf8safe_unescape (ca,   &ca_free);
+       cert = nm_utils_str_utf8safe_unescape (cert, &cert_free);
+       key  = nm_utils_str_utf8safe_unescape (key,  &key_free);
 
        if (   ca && strlen (ca)
            && cert && strlen (cert)
@@ -1949,7 +1949,7 @@ check_need_secrets (NMSettingVpn *s_vpn, gboolean *need_secrets)
        if (!strcmp (ctype, NM_OPENVPN_CONTYPE_PASSWORD_TLS)) {
                /* Will require a password and maybe private key password */
                key = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_KEY);
-               key = nmv_utils_str_utf8safe_unescape_c (key, &key_free);
+               key = nm_utils_str_utf8safe_unescape (key, &key_free);
                if (is_encrypted (key) && !nm_setting_vpn_get_secret (s_vpn, NM_OPENVPN_KEY_CERTPASS))
                        *need_secrets = TRUE;
 
@@ -1972,7 +1972,7 @@ check_need_secrets (NMSettingVpn *s_vpn, gboolean *need_secrets)
        } else if (!strcmp (ctype, NM_OPENVPN_CONTYPE_TLS)) {
                /* May require private key password */
                key = nm_setting_vpn_get_data_item (s_vpn, NM_OPENVPN_KEY_KEY);
-               key = nmv_utils_str_utf8safe_unescape_c (key, &key_free);
+               key = nm_utils_str_utf8safe_unescape (key, &key_free);
                if (is_encrypted (key) && !nm_setting_vpn_get_secret (s_vpn, NM_OPENVPN_KEY_CERTPASS))
                        *need_secrets = TRUE;
        } else {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]