summaryrefslogtreecommitdiffstats
path: root/src/url.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/url.c')
-rw-r--r--src/url.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/url.c b/src/url.c
new file mode 100644
index 0000000..a3a29e7
--- /dev/null
+++ b/src/url.c
@@ -0,0 +1,30 @@
+int urlencode (char * o, char * i /* o must have at least strlen(i)*3+1 bytes of memory allocated */) {
+ size_t written = 0;
+ for (; *i; i++) {
+ if (isalnum(*i) || *i == '.' || *i == '_' || *i == '-' || *i == '~') {
+ o[written++] = *i;
+ } else {
+ sprintf(o+written, "%%%02X", *i);
+ written += 3;
+ }
+ }
+ return 1;
+}
+int urldecode (char * o, char * i /* o must have at least strlen(i)+1 bytes memory allocated */) {
+ size_t written = 0;
+ char buf[] = "00";
+ for (; *i; i++) {
+ if (*i == '%') {
+ buf[0] = *++i;
+ buf[1] = *++i;
+ if (!buf[0] || !buf[1]) { /* malformed */
+ o[written++] = '\0';
+ return 0;
+ }
+ o[written++] = strtol(buf, NULL, 16);
+ } else {
+ o[written++] = *i;
+ }
+ }
+ return 1;
+}