From 3c46c880df81a51ed44cfdd713a6734a245693a8 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 7 Dec 2017 14:23:00 +0100 Subject: [PATCH] file_util: implement urldecode_path() Introduce a new urldecode_path() helper to resolve percent-encoded URL portions back into the original binary form. Signed-off-by: Jo-Philipp Wich --- libopkg/file_util.c | 20 ++++++++++++++++++++ libopkg/file_util.h | 1 + 2 files changed, 21 insertions(+) diff --git a/libopkg/file_util.c b/libopkg/file_util.c index 759c21b..61ff736 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -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; +} diff --git a/libopkg/file_util.h b/libopkg/file_util.h index e078f41..279db82 100644 --- a/libopkg/file_util.h +++ b/libopkg/file_util.h @@ -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 -- 2.30.2