summaryrefslogtreecommitdiffstats
path: root/lib.c
blob: 207cc7ff9738dda66abf58375763db2245420f1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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)	// output will not be longer than strlen(i)*6
		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, "&lt;");
				break;
			case '"':
				w += sprintf(o+w, "&quot;");
				break;
			case '\'':
				w += sprintf(o+w, "&apos;");
				break;
			default:
				o[w++] = *i;
				break;
		}
	}
	o[w++] = '\0';
	return o;
}
static char * hscf (char * i) { // htmlspecialchars and free
	char * hsc = htmlspecialchars(i);
	free(i);
	return hsc;
}