Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Forked from sudar/url-encode.c
Created January 22, 2019 21:21
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesobreira/4ba48d1699b7527a4a514bfa1d70f61a to your computer and use it in GitHub Desktop.
Save jesobreira/4ba48d1699b7527a4a514bfa1d70f61a to your computer and use it in GitHub Desktop.
URL Encoding in C (urlencode / encodeURIComponent)
#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;
}
@4anonz
Copy link

4anonz commented Jun 8, 2021

Thanks!

@harieamjari
Copy link

Incredible!

@ThePedroo
Copy link

I think you should free encodedText

@denis-beurive
Copy link

denis-beurive commented Feb 3, 2023

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)

@rilysh
Copy link

rilysh commented Mar 3, 2023

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.

@redwinner
Copy link

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