-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.c
executable file
·295 lines (253 loc) · 5.81 KB
/
filter.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. If you did not receive
* a copy of the license it is available through the world-wide-web at the
* following url: https://docs.zephir-lang.com/en/latest/license
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include "php.h"
#include "php_ext.h"
#include "php_main.h"
#include <ext/standard/php_smart_string.h>
#include <ext/standard/php_math.h>
#include <ext/standard/html.h>
#include "kernel/main.h"
#include "kernel/memory.h"
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>
#include <zend_smart_str.h>
/**
* Filter alphanum string
*/
void zephir_filter_alphanum(zval *return_value, zval *param)
{
unsigned int i;
unsigned char ch;
smart_str filtered_str = {0};
zval copy;
int use_copy = 0;
if (Z_TYPE_P(param) != IS_STRING) {
use_copy = zend_make_printable_zval(param, ©);
if (use_copy) {
param = ©
}
}
for (i = 0; i < Z_STRLEN_P(param); i++) {
ch = Z_STRVAL_P(param)[i];
if (ch == '\0') {
break;
}
if (isalnum(ch)) {
smart_str_appendc(&filtered_str, ch);
}
}
if (use_copy) {
zval_dtor(param);
}
smart_str_0(&filtered_str);
if (filtered_str.s) {
RETURN_STR(filtered_str.s);
} else {
RETURN_EMPTY_STRING();
}
}
/**
* Check if a string is encoded with ASCII or ISO-8859-1
*/
void zephir_is_basic_charset(zval *return_value, const zval *param)
{
unsigned int i;
unsigned int ch;
int iso88591 = 0;
for (i = 0; i < Z_STRLEN_P(param); i++) {
ch = Z_STRVAL_P(param)[i];
if (ch != '\0') {
if (ch == 172 || (ch >= 128 && ch <= 159)) {
continue;
}
if (ch >= 160 && ch <= 255) {
iso88591 = 1;
continue;
}
}
RETURN_FALSE;
}
if (!iso88591) {
RETURN_STRING("ASCII");
}
RETURN_STRING("ISO-8859-1");
}
static long zephir_unpack(char *data, int size, int issigned, int *map)
{
long result;
char *cresult = (char *) &result;
int i;
result = issigned ? -1 : 0;
for (i = 0; i < size; i++) {
cresult[map[i]] = *data++;
}
return result;
}
/**
* Converts an unsigned long to a char*
*/
static inline char *zephir_longtohex(unsigned long value)
{
static char digits[] = "0123456789abcdef";
char buf[(sizeof(unsigned long) << 3) + 1];
char *ptr, *end;
end = ptr = buf + sizeof(buf) - 1;
*ptr = '\0';
do {
*--ptr = digits[value & 0x0F];
value >>= 4;
} while (ptr > buf && value);
return estrndup(ptr, end - ptr);
}
/**
* Perform escaping of non-alphanumeric characters to different formats
*/
void zephir_escape_multi(zval *return_value, zval *param, const char *escape_char, unsigned int escape_length, char escape_extra, int use_whitelist)
{
unsigned int i;
zval copy;
smart_str escaped_str = {0};
char machine_little_endian, *hex;
int big_endian_long_map[4];
int use_copy = 0, machine_endian_check = 1;
int issigned = 0;
long value;
if (Z_TYPE_P(param) != IS_STRING) {
use_copy = zend_make_printable_zval(param, ©);
if (use_copy) {
param = ©
}
}
if (Z_STRLEN_P(param) <= 0) {
RETURN_FALSE;
}
/**
* This is how the big_ending_long_map is calculated as in 'pack'
*/
machine_little_endian = ((char *) &machine_endian_check)[0];
if (machine_little_endian) {
big_endian_long_map[0] = 3;
big_endian_long_map[1] = 2;
big_endian_long_map[2] = 1;
big_endian_long_map[3] = 0;
} else {
int size = sizeof(Z_LVAL_P(param));
big_endian_long_map[0] = size - 4;
big_endian_long_map[1] = size - 3;
big_endian_long_map[2] = size - 2;
big_endian_long_map[3] = size - 1;
}
/**
* The input must be a valid UTF-32 string
*/
if ((Z_STRLEN_P(param) % 4) != 0) {
RETURN_FALSE;
}
for (i = 0; i < Z_STRLEN_P(param); i += 4) {
issigned = Z_STRVAL_P(param)[i] & 0x80;
value = 0;
if (sizeof(long) > 4 && issigned) {
value = ~INT_MAX;
}
value |= zephir_unpack(&Z_STRVAL_P(param)[i], 4, issigned, big_endian_long_map);
if (sizeof(long) > 4) {
value = (unsigned int) value;
}
/**
* CSS 2.1 section 4.1.3: "It is undefined in CSS 2.1 what happens if a
* style sheet does contain a character with Unicode codepoint zero."
*/
if (value == '\0') {
RETURN_FALSE;
}
/**
* Alphanumeric characters are not escaped
*/
if (value < 123 && isalnum(value)) {
smart_str_appendc(&escaped_str, (unsigned char) value);
continue;
}
/**
* Chararters in the whitelist are left as they are
*/
if (use_whitelist) {
switch (value) {
case ' ':
case '/':
case '*':
case '+':
case '-':
case '\t':
case '\n':
case '^':
case '$':
case '!':
case '?':
case '\\':
case '#':
case '}':
case '{':
case ')':
case '(':
case ']':
case '[':
case '.':
case ',':
case ':':
case ';':
case '_':
case '|':
smart_str_appendc(&escaped_str, (unsigned char) value);
continue;
}
}
/**
* Convert character to hexadecimal
*/
hex = zephir_longtohex(value);
/**
* Append the escaped character
*/
smart_str_appendl(&escaped_str, escape_char, escape_length);
smart_str_appendl(&escaped_str, hex, strlen(hex));
if (escape_extra != '\0') {
smart_str_appendc(&escaped_str, escape_extra);
}
efree(hex);
}
if (use_copy) {
zval_dtor(param);
}
smart_str_0(&escaped_str);
if (escaped_str.s) {
RETURN_STR(escaped_str.s);
} else {
RETURN_EMPTY_STRING();
}
}
/**
* Escapes non-alphanumeric characters to \HH+space
*/
void zephir_escape_css(zval *return_value, zval *param)
{
zephir_escape_multi(return_value, param, "\\", sizeof("\\")-1, ' ', 0);
}
/**
* Escapes non-alphanumeric characters to \xHH+
*/
void zephir_escape_js(zval *return_value, zval *param)
{
zephir_escape_multi(return_value, param, "\\x", sizeof("\\x")-1, '\0', 1);
}