Commit 73e6c815 authored by Jo-Philipp Wich's avatar Jo-Philipp Wich
Browse files

file_util: implement urlencode_path() helper



Introduce a helper function urlencode_path() which can be used to encode
problematic characters in package file names.

Signed-off-by: default avatarJo-Philipp Wich <jo@mein.io>
Tested-by: default avatarRafał Miłecki <rafal@milecki.pl>
parent 468158f0
......@@ -346,3 +346,63 @@ int rm_r(const char *path)
return ret;
}
static int urlencode_is_specialchar(char c)
{
switch (c) {
case ':':
case '?':
case '#':
case '[':
case ']':
case '@':
case '!':
case '$':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
case '%':
return 1;
default:
return 0;
}
}
char *urlencode_path(const char *filename)
{
static const char bin2hex[16] = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
size_t len = 0;
const char *in;
char *copy, *out;
for (in = filename; *in != 0; in++)
len += urlencode_is_specialchar(*in) ? 3 : 1;
copy = xcalloc(1, len + 1);
for (in = filename, out = copy; *in != 0; in++) {
if (urlencode_is_specialchar(*in)) {
*out++ = '%';
*out++ = bin2hex[*in / 16];
*out++ = bin2hex[*in % 16];
}
else {
*out++ = *in;
}
}
return copy;
}
......@@ -31,4 +31,6 @@ int rm_r(const char *path);
char *checksum_bin2hex(const char *src, size_t len);
char *checksum_hex2bin(const char *src, size_t *len);
char *urlencode_path(const char *filename);
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment