summaryrefslogblamecommitdiffstats
path: root/src/url.c
blob: a3a29e7dea160ab7cf80175790c16ba45c055cbf (plain) (tree)





























                                                                                                       
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;
}