include unistd include strings static int hexdigit char if return if r

  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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <unistd.h>
#include <strings.h>
static int hexdigit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
return -1;
}
enum states { ST_SYM, ST_PERCENT, ST_PERCENT_AND_SYM };
typedef struct decoder_state_s
{
int state;
char sym;
} decoder_state;
int is_unreserved(char c)
{
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || index("-_.~!*'();:@&=+$,/?%#[]", c);
}
/**
Decodes URL-encoded data. Because encoded data is always bigger conversion is
done in-place.
@return Number of decoded bytes written to data. Negative integer if data is
not valid URL-encoded sequence.
*/
ssize_t urldecode(decoder_state *state, char *data, size_t size)
{
size_t inpos = 0, outpos = 0;
int d1, d2;
while (inpos < size)
{
char in = data[inpos++];
switch (in)
{
case '%':
switch (state->state)
{
case ST_SYM:
state->state = ST_PERCENT;
break;
default:
return -1;
}
break;
case '+':
switch (state->state)
{
case ST_SYM:
data[outpos++] = ' ';
break;
default:
return -1;
}
break;
default:
switch (state->state)
{
case ST_PERCENT_AND_SYM:
d1 = hexdigit(state->sym), d2 = hexdigit(in);
if (d1 >= 0 && d2 >= 0)
{
data[outpos++] = (d1 << 4) | d2;
}
else
{
return -1;
}
state->state = ST_SYM;
break;
case ST_PERCENT:
state->sym = in;
state->state = ST_PERCENT_AND_SYM;
break;
case ST_SYM:
if (is_unreserved(in))
{
data[outpos++] = in;
}
else
{
return -1;
}
break;
}
}
}
return outpos;
}
int main(int argc, char *argv)
{
char data[4096];
ssize_t bytes;
decoder_state state;
state.state = ST_SYM;
while ((bytes = read(0, data, sizeof(data))) > 0)
{
bytes = urldecode(&state, data, bytes);
if (bytes > 0)
{
write(1, data, bytes);
}
else
{
return 1;
}
}
return 0;
}