dracoblue.net

urlencode in Pawn

When creating new REST-ful API for GTAT (based upon my pwncurl and a lua backend) I had to implement the method urlencode in pawn.

So I took a look at the specification in RFC 1738 to get information about what characters need to be encoded.

RFC 1738: Only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

Since I really want to be sure that all is encoded properly and %xx encoding is

<!--more-->allowed, too - even if not neceassary - my urlencode function encodes every character except A-Z,a-z,0-9,'-','_' and '.'.

/**
 *  Url encodes a specific string
 *  @param string string
 */
stock urlencode(string[]) {
    new ret[MAX_STRING];
    ret[0] = 0;
    new i = 0;
    new p = 0;
    new s = 0;
    while (string[i] != 0) {
        if  (
                (string[i] >= 'A' && string[i] &lt;='Z')
                || (string[i] >= 'a' && string[i] &lt;='z')
                || (string[i] >= '0' && string[i] &lt;='9')
                || (string[i] == '-')
                || (string[i] == '_')
                || (string[i] == '.')
            ) {
                ret[p] = string[i];
            } else {
                //
                ret[p] = '%';
                p++;
                s = (string[i] % 16); //
                ret[p+1] = (s>9) ? (55+s) : (48+s); // 64 - 9 = 55
                s = floatround((string[i] - s)/16);
                ret[p] = (s>9) ? (55+s) : (48+s); // 64 - 9 = 55
                p++;
            }
        p++;
        i++;
    }
    return ret;
}

This function will also be part of next release of dutils.

In dutils, pawn, pwncurl by
@ 20 Jun 2009, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About