This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
383 lines (318 loc) · 8.39 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
+------------------------------------------------------------------------+
| Zephir Language |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Zephir Team (http://www.zephir-lang.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
+------------------------------------------------------------------------+
*/
#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_str.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"
/**
* 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) {
zend_make_printable_zval(param, ©, &use_copy);
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.c) {
RETURN_STRINGL(filtered_str.c, filtered_str.len, 0);
} else {
RETURN_EMPTY_STRING();
}
}
/**
* Filter identifiers string like variables or database columns/tables
*/
void zephir_filter_identifier(zval *return_value, zval *param){
unsigned int i;
unsigned char ch;
zval copy;
smart_str filtered_str = {0};
int use_copy = 0;
if (Z_TYPE_P(param) != IS_STRING) {
zend_make_printable_zval(param, ©, &use_copy);
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) || ch == '_') {
smart_str_appendc(&filtered_str, ch);
}
}
if (use_copy) {
zval_dtor(param);
}
smart_str_0(&filtered_str);
if (filtered_str.c) {
RETURN_STRINGL(filtered_str.c, filtered_str.len, 0);
} 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", 1);
}
RETURN_STRING("ISO-8859-1", 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) {
zend_make_printable_zval(param, ©, &use_copy);
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 < 256 && 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.c) {
RETURN_STRINGL(escaped_str.c, escaped_str.len, 0);
} 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);
}
/**
* Escapes non-alphanumeric characters to &xHH;
*/
void zephir_escape_htmlattr(zval *return_value, zval *param) {
zephir_escape_multi(return_value, param, "&#x", sizeof("&#x")-1, ';', 1);
}
/**
* Escapes HTML replacing special chars by entities
*/
void zephir_escape_html(zval *return_value, zval *str, zval *quote_style, zval *charset TSRMLS_DC) {
#if PHP_VERSION_ID < 50400
int length;
#else
size_t length;
#endif
char *escaped;
if (Z_TYPE_P(str) != IS_STRING) {
/* Nothing to escape */
RETURN_ZVAL(str, 1, 0);
}
if (Z_TYPE_P(quote_style) != IS_LONG) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid quote_style supplied for zephir_escape_html()");
RETURN_ZVAL(str, 1, 0);
}
if (Z_TYPE_P(charset) != IS_STRING) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid charset supplied for zephir_escape_html()");
RETURN_ZVAL(str, 1, 0);
}
escaped = php_escape_html_entities((unsigned char*) Z_STRVAL_P(str), Z_STRLEN_P(str), &length, 0, Z_LVAL_P(quote_style), Z_STRVAL_P(charset) TSRMLS_CC);
RETURN_STRINGL(escaped, length, 0);
}