Created
February 23, 2019 08:56
-
-
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.
This file contains 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> | |
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