diff options
author | Daiki Ueno <dueno@redhat.com> | 2017-01-25 15:54:40 +0100 |
---|---|---|
committer | Daiki Ueno <ueno@gnu.org> | 2017-02-16 16:56:40 +0100 |
commit | 3bab48000c4e61104b30ac379806cad3e1376ea6 (patch) | |
tree | d307f95c4583079949421791185667e6846d941e /common/path.c | |
parent | 5442b1cfa13da9307cc38a8fd289a67a05fe26ad (diff) |
common: Add path encoding functions
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.
Diffstat (limited to 'common/path.c')
-rw-r--r-- | common/path.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/common/path.c b/common/path.c index 8b8b66c..5cf0e1a 100644 --- a/common/path.c +++ b/common/path.c @@ -38,9 +38,11 @@ #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); +} |