]> granicus.if.org Git - p11-kit/commitdiff
common: Add path encoding functions
authorDaiki Ueno <dueno@redhat.com>
Wed, 25 Jan 2017 14:54:40 +0000 (15:54 +0100)
committerDaiki Ueno <ueno@gnu.org>
Thu, 16 Feb 2017 15:56:40 +0000 (16:56 +0100)
This adds p11_path_{encode,decode}(), following the escaping rule
described in:
https://dbus.freedesktop.org/doc/dbus-specification.html#addresses

Although they are merely a wrapper around p11_url_{decode,encode}(),
having dedicated functions hides the implementation details.

common/path.c
common/path.h
common/test-path.c

index 8b8b66ce0aa74511e957f5e4796dd03e382a1b20..5cf0e1ab8152f12641f06571a68fbc0d07e8a4f5 100644 (file)
 
 #include "config.h"
 
+#include "buffer.h"
 #include "debug.h"
 #include "message.h"
 #include "path.h"
+#include "url.h"
 
 #include <assert.h>
 #include <errno.h>
@@ -325,3 +327,34 @@ p11_path_canon (char *name)
                        name[i] = '_';
        }
 }
+
+char *
+p11_path_encode (const char *path)
+{
+       static const char *VALID =
+               "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_/\\";
+       p11_buffer buf;
+       char *result;
+
+       return_val_if_fail (path != NULL, NULL);
+
+       if (!p11_buffer_init_null (&buf, strlen (path)))
+               return_val_if_reached (NULL);
+
+       p11_url_encode ((unsigned char *)path,
+                       (unsigned char *)path + strlen (path),
+                       VALID,
+                       &buf);
+       return_val_if_fail (p11_buffer_ok (&buf), NULL);
+
+       result = p11_buffer_steal (&buf, NULL);
+       p11_buffer_uninit (&buf);
+
+       return result;
+}
+
+char *
+p11_path_decode (const char *path)
+{
+       return (char *) p11_url_decode (path, path + strlen (path), "", NULL);
+}
index 0b19a5d71998d72038ebbb008e62c0f8c96ddce9..243c14f5d10d414a685cbaad61bd9ec01efafdcf 100644 (file)
@@ -66,4 +66,8 @@ bool         p11_path_prefix    (const char *string,
 
 void         p11_path_canon     (char *name);
 
+char *       p11_path_encode    (const char *path);
+
+char *       p11_path_decode    (const char *path);
+
 #endif /* P11_PATH_H__ */
index 1394e0fc314404af92177443675d2d696419f63a..c77acf20918caaec3e6d7a7aa7aed16e3ef40ce5 100644 (file)
@@ -200,6 +200,26 @@ test_canon (void)
        free (test);
 }
 
+static void
+test_encode (void)
+{
+       char *test;
+
+       test = p11_path_encode ("2309haonutb;/AOE@#$O ");
+       assert_str_eq (test, "2309haonutb%3b/AOE%40%23%24O%20");
+       free (test);
+}
+
+static void
+test_decode (void)
+{
+       char *test;
+
+       test = p11_path_decode ("2309haonutb%3b/AOE%40%23%24O%20");
+       assert_str_eq (test, "2309haonutb;/AOE@#$O ");
+       free (test);
+}
+
 int
 main (int argc,
       char *argv[])
@@ -211,6 +231,8 @@ main (int argc,
        p11_test (test_parent, "/path/parent");
        p11_test (test_prefix, "/path/prefix");
        p11_test (test_canon, "/path/canon");
+       p11_test (test_encode, "/path/encode");
+       p11_test (test_decode, "/path/decode");
 
        return p11_test_run (argc, argv);
 }