file_util: implement urldecode_path()
authorJo-Philipp Wich <jo@mein.io>
Thu, 7 Dec 2017 13:23:00 +0000 (14:23 +0100)
committerJo-Philipp Wich <jo@mein.io>
Thu, 7 Dec 2017 16:59:44 +0000 (17:59 +0100)
Introduce a new urldecode_path() helper to resolve percent-encoded URL
portions back into the original binary form.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
libopkg/file_util.c
libopkg/file_util.h

index 759c21b..61ff736 100644 (file)
@@ -395,3 +395,23 @@ char *urlencode_path(const char *filename)
 
        return (char *)copy;
 }
+
+char *urldecode_path(const char *filename)
+{
+       unsigned char *copy = (unsigned char *)xstrdup(filename);
+       unsigned char *in, *out;
+
+       for (in = copy, out = copy; *in != 0; in++) {
+               if (*in == '%' && isxdigit(in[1]) && isxdigit(in[2])) {
+                       *out++ = hex2bin(in[1]) * 16 + hex2bin(in[2]);
+                       in += 2;
+               }
+               else {
+                       *out++ = *in;
+               }
+       }
+
+       *out = 0;
+
+       return (char *)copy;
+}
index e078f41..279db82 100644 (file)
@@ -32,5 +32,6 @@ char *checksum_bin2hex(const char *src, size_t len);
 char *checksum_hex2bin(const char *src, size_t *len);
 
 char *urlencode_path(const char *filename);
+char *urldecode_path(const char *filename);
 
 #endif