	Unescape a URL

	Some characters in URLs are escaped, to prevent them from
	being interpreted. This function undoes the escapes. The
	result is written back into the string itself, since unescaped
	strings are always shorter than escaped ones. Escapes consist
	of a percent sign and two hexadecimal characters.

	The function returns a pointer to its argument.

<<*>>=
#include <config.h>
#include <str.h>

static char hextoc(char *s)
{
    char c;

    if ('0' <= *s && *s <= '9') c = 16 * (*s - '0');
    else if ('a' <= *s && *s <= 'f') c = 16 * (*s - 'a' + 10);
    else if ('A' <= *s && *s <= 'F') c = 16 * (*s - 'A' + 10);
    else return '%';				/* Error! */
    s++;
    if ('0' <= *s && *s <= '9') c += *s - '0';
    else if ('a' <= *s && *s <= 'f') c += *s - 'a' + 10;
    else if ('A' <= *s && *s <= 'F') c += *s - 'A' + 10;
    else return '%';				/* Error! */
    return c;
}

EXPORT char *URL_unescape(char *s)
{
    char *p = s, *q;

    if (! p) return NULL;

    while (*p && *p != '%' && *p != '+') p++;

    if (*p) {
	for (q = p; *p; q++) {
	    if (*p == '%' && isxdigit(*(p+1)) && isxdigit(*(p+2))) {
		*q = hextoc(p + 1);
		p += 3;
	    } else if (*p == '+') {
		*q = ' ';
		p++;
	    } else
		*q = *(p++);
	}
	*q = '\0';
    }
    return s;
}
