line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#include |
2
|
|
|
|
|
|
|
#include |
3
|
|
|
|
|
|
|
#include |
4
|
|
|
|
|
|
|
#include "uri.h" |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
/* |
7
|
|
|
|
|
|
|
* The next file is generated automatically with program "encode". |
8
|
|
|
|
|
|
|
*/ |
9
|
|
|
|
|
|
|
#include "uri_tables.h" |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#define SET_ENCODE_VALUE(var, pos, flag) \ |
12
|
|
|
|
|
|
|
do { \ |
13
|
|
|
|
|
|
|
if (flag) { \ |
14
|
|
|
|
|
|
|
sprintf(var[pos], "%%%02X", pos); \ |
15
|
|
|
|
|
|
|
} else { \ |
16
|
|
|
|
|
|
|
var[pos][0] = 0; \ |
17
|
|
|
|
|
|
|
} \ |
18
|
|
|
|
|
|
|
} while (0) |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
static void fill_matrix(const Buffer* escape, char matrix[256][4]); |
21
|
|
|
|
|
|
|
|
22
|
12
|
|
|
|
|
|
Buffer* uri_decode(Buffer* src, int length, |
23
|
|
|
|
|
|
|
Buffer* tgt) |
24
|
|
|
|
|
|
|
{ |
25
|
12
|
|
|
|
|
|
int s = src->pos; |
26
|
12
|
|
|
|
|
|
int t = tgt->pos; |
27
|
|
|
|
|
|
|
|
28
|
12
|
50
|
|
|
|
|
if (length < 0) { |
29
|
0
|
|
|
|
|
|
length = src->size; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
/* check and maybe increase space in target */ |
33
|
14
|
100
|
|
|
|
|
buffer_ensure_unused(tgt, length); |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
156
|
100
|
|
|
|
|
while (s < (src->pos + length)) { |
36
|
192
|
|
|
|
|
|
if (src->data[s] == '%' && |
37
|
96
|
50
|
|
|
|
|
isxdigit(src->data[s+1]) && |
38
|
48
|
|
|
|
|
|
isxdigit(src->data[s+2])) { |
39
|
|
|
|
|
|
|
/* put a byte together from the next two hex digits */ |
40
|
48
|
|
|
|
|
|
tgt->data[t++] = MAKE_BYTE(uri_decode_tbl[(int)src->data[s+1]], |
41
|
|
|
|
|
|
|
uri_decode_tbl[(int)src->data[s+2]]); |
42
|
|
|
|
|
|
|
/* we used up 3 characters (%XY) from source */ |
43
|
48
|
|
|
|
|
|
s += 3; |
44
|
|
|
|
|
|
|
} else { |
45
|
96
|
|
|
|
|
|
tgt->data[t++] = src->data[s++]; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
/* null-terminate target and return src as was left */ |
50
|
12
|
|
|
|
|
|
src->pos = s; |
51
|
12
|
|
|
|
|
|
tgt->pos = t; |
52
|
12
|
50
|
|
|
|
|
buffer_terminate(tgt); |
53
|
12
|
|
|
|
|
|
return src; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
31
|
|
|
|
|
|
Buffer* uri_encode(Buffer* src, int length, |
57
|
|
|
|
|
|
|
Buffer* tgt) |
58
|
|
|
|
|
|
|
{ |
59
|
31
|
|
|
|
|
|
int s = src->pos; |
60
|
31
|
|
|
|
|
|
int t = tgt->pos; |
61
|
|
|
|
|
|
|
|
62
|
31
|
50
|
|
|
|
|
if (length < 0) { |
63
|
0
|
|
|
|
|
|
length = src->size; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
/* check and maybe increase space in target */ |
67
|
45
|
100
|
|
|
|
|
buffer_ensure_unused(tgt, 3 * length); |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
525
|
100
|
|
|
|
|
while (s < (src->pos + length)) { |
70
|
494
|
|
|
|
|
|
unsigned char u = (unsigned char) src->data[s]; |
71
|
494
|
|
|
|
|
|
char* v = uri_encode_tbl[(int)u]; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
/* if current source character doesn't need to be encoded, |
74
|
|
|
|
|
|
|
just copy it to target*/ |
75
|
494
|
100
|
|
|
|
|
if (!v) { |
76
|
313
|
|
|
|
|
|
tgt->data[t++] = src->data[s++]; |
77
|
313
|
|
|
|
|
|
continue; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
/* copy encoded character from our table */ |
81
|
181
|
|
|
|
|
|
memcpy(tgt->data + t, v, 3); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
/* we used up 3 characters (%XY) in target |
84
|
|
|
|
|
|
|
* and 1 character from source */ |
85
|
181
|
|
|
|
|
|
t += 3; |
86
|
181
|
|
|
|
|
|
++s; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
/* null-terminate target and return src as was left */ |
90
|
31
|
|
|
|
|
|
src->pos = s; |
91
|
31
|
|
|
|
|
|
tgt->pos = t; |
92
|
31
|
50
|
|
|
|
|
buffer_terminate(tgt); |
93
|
31
|
|
|
|
|
|
return src; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
104
|
|
|
|
|
|
Buffer* uri_encode_matrix(Buffer* src, int length, |
97
|
|
|
|
|
|
|
Buffer* escape, |
98
|
|
|
|
|
|
|
Buffer* tgt) |
99
|
|
|
|
|
|
|
{ |
100
|
104
|
|
|
|
|
|
int s = src->pos; |
101
|
104
|
|
|
|
|
|
int t = tgt->pos; |
102
|
|
|
|
|
|
|
char uri_encode_tbl[256][4]; |
103
|
|
|
|
|
|
|
|
104
|
104
|
50
|
|
|
|
|
if (length < 0) { |
105
|
0
|
|
|
|
|
|
length = src->size; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
/* check and maybe increase space in target */ |
109
|
132
|
100
|
|
|
|
|
buffer_ensure_unused(tgt, 3 * length); |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
104
|
|
|
|
|
|
fill_matrix(escape, uri_encode_tbl); |
112
|
|
|
|
|
|
|
|
113
|
1422
|
100
|
|
|
|
|
while (s < (src->pos + length)) { |
114
|
1318
|
|
|
|
|
|
unsigned char u = (unsigned char) src->data[s]; |
115
|
1318
|
|
|
|
|
|
char* v = uri_encode_tbl[(int)u]; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
/* if current source character doesn't need to be encoded, |
118
|
|
|
|
|
|
|
just copy it to target*/ |
119
|
1318
|
100
|
|
|
|
|
if (!v[0]) { |
120
|
1088
|
|
|
|
|
|
tgt->data[t++] = src->data[s++]; |
121
|
1088
|
|
|
|
|
|
continue; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
/* copy encoded character from our table */ |
125
|
230
|
|
|
|
|
|
memcpy(tgt->data + t, v, 3); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
/* we used up 3 characters (%XY) in target |
128
|
|
|
|
|
|
|
* and 1 character from source */ |
129
|
230
|
|
|
|
|
|
t += 3; |
130
|
230
|
|
|
|
|
|
++s; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
/* null-terminate target and return src as was left */ |
134
|
104
|
|
|
|
|
|
src->pos = s; |
135
|
104
|
|
|
|
|
|
tgt->pos = t; |
136
|
104
|
50
|
|
|
|
|
buffer_terminate(tgt); |
137
|
104
|
|
|
|
|
|
return src; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
104
|
|
|
|
|
|
static void fill_matrix(const Buffer* escape, char matrix[256][4]) |
141
|
|
|
|
|
|
|
{ |
142
|
|
|
|
|
|
|
/* |
143
|
|
|
|
|
|
|
* Table has a 0 if that character doesn't need to be encoded; |
144
|
|
|
|
|
|
|
* otherwise it has a string with the character encoded in hex digits. |
145
|
|
|
|
|
|
|
*/ |
146
|
104
|
|
|
|
|
|
int pos = 0; |
147
|
104
|
|
|
|
|
|
int flag = 0; |
148
|
104
|
|
|
|
|
|
int beg = escape->pos; |
149
|
104
|
100
|
|
|
|
|
if (escape->data[beg] == '^') { |
150
|
44
|
|
|
|
|
|
flag = 1; |
151
|
44
|
|
|
|
|
|
++beg; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
/* printf("Using flag %d, beg %d, size %d\n", flag, beg, escape->size); */ |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
/* Set default values and flip flag */ |
156
|
26728
|
100
|
|
|
|
|
for (pos = 0; pos < 256; ++pos) { |
157
|
26624
|
100
|
|
|
|
|
SET_ENCODE_VALUE(matrix, pos, flag); |
158
|
|
|
|
|
|
|
} |
159
|
104
|
|
|
|
|
|
flag = !flag; |
160
|
|
|
|
|
|
|
|
161
|
1616
|
100
|
|
|
|
|
for (pos = beg; pos < escape->size; ++pos) { |
162
|
1512
|
|
|
|
|
|
int p = 0; |
163
|
1512
|
100
|
|
|
|
|
if (escape->data[pos] != '-' || |
|
|
50
|
|
|
|
|
|
164
|
152
|
100
|
|
|
|
|
(pos == beg || pos == (escape->size - 1))) { |
165
|
|
|
|
|
|
|
/* printf("Found char %c\n", escape->data[pos]); */ |
166
|
1416
|
|
|
|
|
|
int p = escape->data[pos]; |
167
|
1416
|
100
|
|
|
|
|
SET_ENCODE_VALUE(matrix, p, flag); |
168
|
1416
|
|
|
|
|
|
continue; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
/* printf("Found range %c %c\n", escape->data[pos-1] + 1, escape->data[pos+1]); */ |
171
|
1856
|
100
|
|
|
|
|
for (p = escape->data[pos-1] + 1; p <= escape->data[pos+1]; ++p) { |
172
|
1760
|
100
|
|
|
|
|
SET_ENCODE_VALUE(matrix, p, flag); |
173
|
|
|
|
|
|
|
} |
174
|
96
|
|
|
|
|
|
++pos; /* need to skip over the '-' and the next character */ |
175
|
|
|
|
|
|
|
} |
176
|
104
|
|
|
|
|
|
} |