3 // ----------------------------------------------------------------------------
5 // code from: http://www.geekhideout.com/urlcode.shtml
7 /* Converts a hex character to its integer value */
8 char from_hex(char ch) {
9 return (char)(isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10);
12 /* Converts an integer value to its hex character*/
13 char to_hex(char code) {
14 static char hex[] = "0123456789abcdef";
15 return hex[code & 15];
18 /* Returns a url-encoded version of str */
19 /* IMPORTANT: be sure to free() the returned string after use */
20 char *url_encode(char *str) {
23 pbuf = buf = malloc(strlen(str) * 3 + 1);
26 fatal("Cannot allocate memory.");
29 if (isalnum(*str) || *str == '-' || *str == '_' || *str == '.' || *str == '~')
36 *pbuf++ = '%', *pbuf++ = to_hex(*str >> 4), *pbuf++ = to_hex(*str & 15);
42 // FIX: I think this is prudent. URLs can be as long as 2 KiB or more.
43 // We allocated 3 times more space to accomodate %NN encoding of
44 // non ASCII chars. If URL has none of these kind of chars we will
45 // end up with a big unused buffer.
47 // Try to shrink the buffer...
48 if (!!(pbuf = (char *)realloc(buf, strlen(buf)+1)))
54 /* Returns a url-decoded version of str */
55 /* IMPORTANT: be sure to free() the returned string after use */
56 char *url_decode(char *str) {
57 size_t size = strlen(str) + 1;
59 char *buf = malloc(size);
61 fatal("Cannot allocate %zu bytes of memory.", size);
63 return url_decode_r(buf, str, size);
66 char *url_decode_r(char *to, char *url, size_t size) {
67 char *s = url, // source
68 *d = to, // destination
69 *e = &to[size - 1]; // destination end
72 if(unlikely(*s == '%')) {
73 if(likely(s[1] && s[2])) {
74 *d++ = from_hex(s[1]) << 4 | from_hex(s[2]);
78 else if(unlikely(*s == '+'))