summaryrefslogtreecommitdiffstats
path: root/src/url.c
diff options
context:
space:
mode:
authorsijanec <anton@sijanec.eu>2021-04-01 23:30:37 +0200
committersijanec <anton@sijanec.eu>2021-04-01 23:30:37 +0200
commit579048eaf89784ec1da8592d96311fafd49aea1a (patch)
tree61bf0c50c656f2b16ed8901ec3b07fb468ffb916 /src/url.c
downloadsear.c-579048eaf89784ec1da8592d96311fafd49aea1a.tar
sear.c-579048eaf89784ec1da8592d96311fafd49aea1a.tar.gz
sear.c-579048eaf89784ec1da8592d96311fafd49aea1a.tar.bz2
sear.c-579048eaf89784ec1da8592d96311fafd49aea1a.tar.lz
sear.c-579048eaf89784ec1da8592d96311fafd49aea1a.tar.xz
sear.c-579048eaf89784ec1da8592d96311fafd49aea1a.tar.zst
sear.c-579048eaf89784ec1da8592d96311fafd49aea1a.zip
Diffstat (limited to '')
-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;
+}