aboutsummaryrefslogtreecommitdiff
path: root/query.c
diff options
context:
space:
mode:
Diffstat (limited to 'query.c')
-rw-r--r--query.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/query.c b/query.c
index 27770e4..96cfbcb 100644
--- a/query.c
+++ b/query.c
@@ -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