-
Notifications
You must be signed in to change notification settings - Fork 0
/
urldecode.c
49 lines (41 loc) · 827 Bytes
/
urldecode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <assert.h>
#define BUF_SIZE 255
void urldecode (const char *in, char *out) {
// zeroes memory, better play it safe
bzero((void*)out, sizeof(out));
//while ( *out++ = *in++ )
while ( *in )
{
if ( *in == '%' && isxdigit(*(in+1)) && isxdigit (*(in+2)) )
{
// need to fix size because %320 would behave strangely
char buf[2];
buf[0] = *(in+1);
buf[1] = *(in+2);
sscanf(buf, "%x", out);
in +=3;
out += 1;
continue;
}
if ( *in == '+' )
{
*out++ = ' ';
in++;
continue;
}
*out++ = *in++;
}
}
int main ( int argc, char **argv ) {
assert(argc > 1 );
char in[BUF_SIZE];
char out[BUF_SIZE];
sprintf(in, "%s", argv[1]);
printf("in: %s\n", in);
urldecode(in, out);
printf("out: %s\n", out);
return 0;
}