[En-Nut-Discussion] NutHttpURLEncode update

Lloyd Bailey lbailey at pharmanord.com
Fri Jan 6 09:57:08 CET 2006


I have created a modified version of the NutHttpURLEncode See below.
Maybe this can be included in the next release?

Regards,

Lloyd.

/*!
 *  Brief: URLEncodes a string
 *
 *  Param: str String to encode
 *
 *  Returns: A new allocated encoded string, or NULL if
 * 	str is null, or if there's not enough RAM for the
 *  new string.
 *
 *  Note: no longer encode everything that isn't
 * 	alphanumeric. See RFC2396
 *  Remember to free() to the returned string.
 */
static char *hexdigits = "0123456789ABCDEF";

char *NutHttpURLEncode2(char *str)
{
    register char *ptr1, *ptr2;
    char *encstring;
    int numEncs = 0;

    if (!str)
        return 0;

    /* Calculate how many characters we need to encode */
    for (ptr1 = str; *ptr1; ptr1++) {
        if (!isalnum(*ptr1) || *ptr1 == '%' || *ptr1 == '&'|| *ptr1 == '+' || 
		*ptr1 == ',' || *ptr1 == ':' || *ptr1 == ';'|| *ptr1 == '='|| *ptr1 == '?'|| *ptr1 == '@')
            numEncs++;
    }
    /* Now we can calculate the encoded string length */
    encstring = (char *) NutHeapAlloc(strlen(str) + 2 * numEncs + 1);

    /* Encode the string. ptr1 refers to the original string,
     * and ptr2 refers to the new string. */
    ptr2 = encstring;
    for (ptr1 = str; *ptr1; ptr1++) {
		if (isalnum(*ptr1) || *ptr1 == '%' || *ptr1 == '&'|| *ptr1 == '+' || 
		*ptr1 == ',' || *ptr1 == ':' || *ptr1 == ';'|| *ptr1 == '='|| *ptr1 == '?'|| *ptr1 == '@')
            *ptr2++ = *ptr1;
        else {
            *ptr2++ = '%';
            *ptr2++ = hexdigits[(*ptr1 >> 4)];
            *ptr2++ = hexdigits[*ptr1 & 0x0F];
        }
    }
    *ptr2++ = 0;
    return encstring;
}




More information about the En-Nut-Discussion mailing list