file_util: implement urldecode_path()
authorJo-Philipp Wich <jo@mein.io>
Thu, 7 Dec 2017 13:23:00 +0000 (14:23 +0100)
committerRafał Miłecki <rafal@milecki.pl>
Fri, 8 Dec 2017 12:46:46 +0000 (13:46 +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 759c21b3ae76d206f717022c38dc8538fecf0d52..61ff736cd2c82a224cb10f48d14532b8224bd792 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 e078f41fd63143fc118077cb0798c33e34bc69f4..279db82bf5e720ce58072a6a98c0e7432e9d3510 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