diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-19 01:20:30 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-19 01:20:30 +0200 |
commit | 4b838fced8cd5fd2c24300e3f9132a1474051a76 (patch) | |
tree | 62be70f1da57b87c863f03eb9054056f9b841aa2 /query.c | |
parent | 7708b5f493bf3057af331624d29664ad17a87dbc (diff) | |
download | cforum-4b838fced8cd5fd2c24300e3f9132a1474051a76.tar.gz |
Implement URL decode and HTML escape functions
Diffstat (limited to 'query.c')
-rw-r--r-- | query.c | 27 |
1 files changed, 25 insertions, 2 deletions
@@ -115,8 +115,31 @@ split(char *param) return param+n+1; } -void -urldecode(char *s) +/* + * Decode a URL-encoded string in place. If len is negative, the string + * is assumed to be NUL-terminated. The length of the new string is + * returned. Note that the new string is NOT NUL-terminated! To produce + * a NUL-terminated string, use s[urldecode(s, -1)] = 0. + */ +int +urldecode(char *s, int len) { + unsigned int code, i, j; + + if(len < 0) + len = strlen(s); + + for(i = j = 0; i < len; i++, j++){ + if(s[i] == '+') + s[j] = ' '; + else if(s[i] == '%'){ + if(!sscanf(s+i+1, "%2x", &code)) + code = '?'; + s[j] = code; + i += 2; + }else + s[j] = s[i]; + } + return j; }
\ No newline at end of file |