Skip to content

Instantly share code, notes, and snippets.

@kazkansouh
Created February 23, 2019 08:56
Show Gist options
  • Save kazkansouh/51bd2f4a258f07d9efc356bbc0f20b9b to your computer and use it in GitHub Desktop.
Save kazkansouh/51bd2f4a258f07d9efc356bbc0f20b9b to your computer and use it in GitHub Desktop.
URL decode in C with only stdlib as dependency, written for use on an embedded device.
#include <stdio.h>
#include <stdlib.h>
void urldecode(char* pch_url) {
int i = 0;
char* ptr = pch_url;
while (*ptr != '\0') {
if (*ptr != '%') {
pch_url[i++] = *(ptr++);
} else {
char val[3] = {0,0,0};
if ((val[0] = *(ptr+1)) != '\0' &&
(val[1] = *(ptr+2)) != '\0') {
char* end = NULL;
long int x = strtol(val, &end, 16);
if (end != val+2) {
// invalid hex value
break;
}
pch_url[i++] = x & 0xFF;
ptr += 3;
} else {
// malformed
break;
}
}
}
pch_url[i] = '\0';
}
char str1[] = "kk%40kk.com%0a%7e";
int main(int argc, char* argv) {
printf("before %s\n", str1);
urldecode(str1);
printf("after %s\n", str1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment