I wrote this to also take care of the encoding of the request string of the space character
Usage: UrlEncode (" http://www.example.com/index.html?Hello=World ", ": /", buffer, buf_size)
url The url string to encode. May be a string literal or a string array
encode : a character string with a null character to encode. This is a good reason why you can determine at runtime how many URLs to encode.
buffer : buffer for storing a new line
size : buffer size
return Returns the size of the new line if the buffer is large enough or returns the required buffer size if the buffer is not large enough. You can double-tap this function if you want to select the desired size.
int UrlEncode(char* url, char* encode, char* buffer, unsigned int size) { char chars[127] = {0}; unsigned int length = 0; if(!url || !encode || !buffer) return 0; //Create an array to hold ascii chars, loop through encode string //and assign to place in array. I used this construct instead of a large if statement for speed. while(*encode) chars[*encode++] = *encode; //Loop through url, if we find an encode char, replace with % and add hex //as ascii chars. Move buffer up by 2 and track the length needed. //If we reach the query string (?), move to query string encoding URLENCODE_BASE_URL: while(size && (*buffer = *url)) { if(*url == '?') goto URLENCODE_QUERY_STRING; if(chars[*url] && size > 2) { *buffer++ = '%'; itoa(*url, buffer, 16); buffer++; size-=2; length+=2; } url++, buffer++, size--; length++; } goto URLENCODE_RETURN; //Same as above but on spaces (' '), replace with plus ('+') and convert //to hex ascii. I moved this out into a separate loop for speed. URLENCODE_QUERY_STRING: while(size && (*buffer = *url)) { if(chars[*url] && size > 2) { *buffer++ = '%'; if(*url == ' ') itoa('+', buffer, 16); else itoa(*url, buffer, 16); buffer++; size-=2; length+=2; } url++, buffer++, size--; length++; } //Terminate the end of the buffer, and if the buffer wasn't large enough //calc the rest of the url length and return URLENCODE_RETURN: *buffer = '\0'; if(*url) while(*url) { if(chars[*url]) length+=2; url++; length++; } return length; }
This function pretty much handles most (if not all) of the URL encoders you need. Best of all - it's really fast!
source share