From: Jo-Philipp Wich Date: Thu, 7 Dec 2017 13:23:00 +0000 (+0100) Subject: file_util: implement urldecode_path() X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=71c27cb3aee45ba22d3c7806401baf8a0dee3d94;p=oweals%2Fopkg-lede.git 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 --- 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