Skip to content

Instantly share code, notes, and snippets.

@imba-tjd
Last active July 8, 2023 13:52
Show Gist options
  • Save imba-tjd/7fe6583c63bc2453eca931537fa025fc to your computer and use it in GitHub Desktop.
Save imba-tjd/7fe6583c63bc2453eca931537fa025fc to your computer and use it in GitHub Desktop.
// https://github.com/abejfehr/URLDecode
// Compile: gcc -Ofast -mtune=native -D__USE_MINGW_ANSI_STDIO -o urld.exe
// When decoding UTF8-encoded data, work with [xargsu2a](https://gist.github.com/imba-tjd/0a5a41df029babc6c814efe5d9296593)
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char *urlDecode(const char *str) {
int d = 0;
char *dStr = malloc(strlen(str) + 1);
char eStr[] = "00";
strcpy(dStr, str);
while(!d) {
d = 1;
int i;
for(i=0;i<strlen(dStr);++i) {
if(dStr[i] == '%') {
if(dStr[i+1] == 0)
return dStr;
if(isxdigit(dStr[i+1]) && isxdigit(dStr[i+2])) {
d = 0;
eStr[0] = dStr[i+1];
eStr[1] = dStr[i+2];
long int x = strtol(eStr, NULL, 16);
memmove(&dStr[i+1], &dStr[i+3], strlen(&dStr[i+3])+1);
dStr[i] = x;
}
}
}
}
return dStr;
}
int main(){
char* s;
scanf("%ms", &s);
puts(urlDecode(s));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment