URL Encoding in C (urlencode / encodeURIComponent)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char* urlencode(char* originalText) | |
{ | |
// allocate memory for the worst possible case (all characters need to be encoded) | |
char *encodedText = (char *)malloc(sizeof(char)*strlen(originalText)*3+1); | |
const char *hex = "0123456789abcdef"; | |
int pos = 0; | |
for (int i = 0; i < strlen(originalText); i++) { | |
if (('a' <= originalText[i] && originalText[i] <= 'z') | |
|| ('A' <= originalText[i] && originalText[i] <= 'Z') | |
|| ('0' <= originalText[i] && originalText[i] <= '9')) { | |
encodedText[pos++] = originalText[i]; | |
} else { | |
encodedText[pos++] = '%'; | |
encodedText[pos++] = hex[originalText[i] >> 4]; | |
encodedText[pos++] = hex[originalText[i] & 15]; | |
} | |
} | |
encodedText[pos] = '\0'; | |
return encodedText; | |
} | |
int main() | |
{ | |
printf("%s", urlencode("Hello / world =")); | |
return 0; | |
} |
Incredible!
I think you should free encodedText
Thank you!
I'd just test the return value of "malloc()". It must not be NULL.
Also, I'd rather say : char* urlencode(const char* originalText)
This isn't a good approach at all.
First, URLs sometimes can be very large, and now malloc will have to allocate a chunk of memory in the heap. In the worst scenario, anyone can pass an arbitrarily large URL which may cause a buffer overflow.
thanks, short version:
static void urlencode(char * s, char * d)
{
const char *hex = "0123456789ABCDEF";
while(*s)
{
if (('a' <= *s && *s <= 'z')
|| ('A' <= *s && *s <= 'Z')
|| ('0' <= *s && *s <= '9')) {
*d++ = *s;
} else {
*d++ = '%';
*d++ = hex[(*s >> 4) & 0x0F];
*d++ = hex[*s & 0x0F];
}
s++;
}
*d = '\0';
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!