diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2022-05-15 23:35:32 +0200 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2022-05-15 23:35:32 +0200 |
commit | b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038 (patch) | |
tree | e5351f85127dbbcce425baf79c3b13a07dd901a2 /lib.c | |
parent | initial commit (diff) | |
download | prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.gz prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.bz2 prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.lz prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.xz prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.tar.zst prijave-b6ecf4698e25b92b5b2c7bcf11d28e7ec9d64038.zip |
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 73 |
1 files changed, 73 insertions, 0 deletions
@@ -0,0 +1,73 @@ +#include <ctype.h> +// functions from http://git.sijanec.eu/sijanec/sear.c +static int urlencode (char * o, const char * i /* o must have at least strlen(i)*3+1 bytes of memory allocated */) { + if (!o || !i) + return -2; + size_t written = 0; /* o CANNOT be equal to i, unlike in urldecode */ + for (; *i; i++) { + if (isalnum(*i) || *i == '.' || *i == '_' || *i == '-' || *i == '~') { + o[written++] = *i; + } else { + sprintf(o+written, "%%%02X", (unsigned char) *i); + written += 3; + } + } + o[written++] = '\0'; + return 1; +} +__attribute__((unused)) static int urldecode (char * o, const char * i /* o must have at least strlen(i)+1 bytes memory allocated */) { + if (!o || !i) + return -2; + size_t written = 0; /* o can be equal to i for decoding in-place */ + char buf[] = "00"; + for (; *i; i++) { + if (*i == '%') { + buf[0] = *++i; + buf[1] = *++i; + if (!buf[0] || !buf[1]) { /* malformed */ + o[written++] = '\0'; + return -1; + } + o[written++] = strtol(buf, NULL, 16); + } else { + o[written++] = *i; + } + } + o[written++] = '\0'; + return 1; +} +static char * htmlspecialchars (const char * i) { /* remember to free the output */ + if (!i) + return NULL; + size_t s = 128; + char * o = malloc(s); + if (!o) + return NULL; + size_t w = 0; + for (; *i; i++) { + if (s - w <= 10) { + char * old = o; + o = realloc(o, (s *= 1.5)); + if (!o) { + free(old); + return NULL; + } + } + switch (*i) { + case '<': + w += sprintf(o+w, "<"); + break; + case '"': + w += sprintf(o+w, """); + break; + case '\'': + w += sprintf(o+w, "'"); + break; + default: + o[w++] = *i; + break; + } + } + o[w++] = '\0'; + return o; +} |