diff options
author | Felix Fietkau <nbd@openwrt.org> | 2012-12-30 22:09:34 +0100 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2012-12-30 22:09:34 +0100 |
commit | b660d584ca45e74f15711471be3ce9618cdc8d7e (patch) | |
tree | 9d1000e8bea304024cbc827cbda0602b97f6ce73 /utils.c | |
parent | 5ddf71b3638788f8d1dd1d312ec79acfac7f3e74 (diff) |
clean up uh_urldecode, null-terminate string
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 31 |
1 files changed, 9 insertions, 22 deletions
@@ -99,31 +99,18 @@ int uh_urldecode(char *buf, int blen, const char *src, int slen) for (i = 0; (i < slen) && (len < blen); i++) { - if (src[i] == '%') - { - if (((i+2) < slen) && isxdigit(src[i+1]) && isxdigit(src[i+2])) - { - buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2])); - i += 2; - } - else - { - /* Encoding error: it's hard to think of a - ** scenario in which returning an incorrect - ** 'decoding' of the malformed string is - ** preferable to signaling an error condition. */ - #if 0 /* WORSE_IS_BETTER */ - buf[len++] = '%'; - #else - return -2; - #endif - } - } - else - { + if (src[i] != '%') { buf[len++] = src[i]; + continue; } + + if (i + 2 >= slen || !isxdigit(src[i + 1]) || !isxdigit(src[i + 2])) + return -2; + + buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2])); + i += 2; } + buf[len] = 0; return (i == slen) ? len : -1; } |