/src/php-src/ext/standard/html.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | https://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Rasmus Lerdorf <rasmus@php.net> | |
14 | | | Jaakko Hyvätti <jaakko.hyvatti@iki.fi> | |
15 | | | Wez Furlong <wez@thebrainroom.com> | |
16 | | | Gustavo Lopes <cataphract@php.net> | |
17 | | +----------------------------------------------------------------------+ |
18 | | */ |
19 | | |
20 | | /* |
21 | | * HTML entity resources: |
22 | | * |
23 | | * http://www.unicode.org/Public/MAPPINGS/OBSOLETE/UNI2SGML.TXT |
24 | | * |
25 | | * XHTML 1.0 DTD |
26 | | * http://www.w3.org/TR/2002/REC-xhtml1-20020801/dtds.html#h-A2 |
27 | | * |
28 | | * From HTML 4.01 strict DTD: |
29 | | * http://www.w3.org/TR/html4/HTMLlat1.ent |
30 | | * http://www.w3.org/TR/html4/HTMLsymbol.ent |
31 | | * http://www.w3.org/TR/html4/HTMLspecial.ent |
32 | | * |
33 | | * HTML 5: |
34 | | * http://dev.w3.org/html5/spec/Overview.html#named-character-references |
35 | | */ |
36 | | |
37 | | #include "php.h" |
38 | | #ifdef PHP_WIN32 |
39 | | #include "config.w32.h" |
40 | | #else |
41 | | #include <php_config.h> |
42 | | #endif |
43 | | #include "php_standard.h" |
44 | | #include "SAPI.h" |
45 | | #include <locale.h> |
46 | | |
47 | | #include <zend_hash.h> |
48 | | #include "html_tables.h" |
49 | | |
50 | | /* Macro for disabling flag of translation of non-basic entities where this isn't supported. |
51 | | * Not appropriate for html_entity_decode/htmlspecialchars_decode */ |
52 | 836 | #define LIMIT_ALL(all, doctype, charset) do { \ |
53 | 836 | (all) = (all) && !CHARSET_PARTIAL_SUPPORT((charset)) && ((doctype) != ENT_HTML_DOC_XML1); \ |
54 | 836 | } while (0) |
55 | | |
56 | 720k | #define MB_FAILURE(pos, advance) do { \ |
57 | 1.31M | *cursor = pos + (advance); \ |
58 | 720k | *status = FAILURE; \ |
59 | 720k | return 0; \ |
60 | 720k | } while (0) |
61 | | |
62 | 1.67M | #define CHECK_LEN(pos, chars_need) ((str_len - (pos)) >= (chars_need)) |
63 | | |
64 | | /* valid as single byte character or leading byte */ |
65 | 45.4k | #define utf8_lead(c) ((c) < 0x80 || ((c) >= 0xC2 && (c) <= 0xF4)) |
66 | | /* whether it's actually valid depends on other stuff; |
67 | | * this macro cannot check for non-shortest forms, surrogates or |
68 | | * code points above 0x10FFFF */ |
69 | 406k | #define utf8_trail(c) ((c) >= 0x80 && (c) <= 0xBF) |
70 | | |
71 | 30.3k | #define gb2312_lead(c) ((c) != 0x8E && (c) != 0x8F && (c) != 0xA0 && (c) != 0xFF) |
72 | 1.04k | #define gb2312_trail(c) ((c) >= 0xA1 && (c) <= 0xFE) |
73 | | |
74 | 348 | #define sjis_lead(c) ((c) != 0x80 && (c) != 0xA0 && (c) < 0xFD) |
75 | 1.06k | #define sjis_trail(c) ((c) >= 0x40 && (c) != 0x7F && (c) < 0xFD) |
76 | | |
77 | | /* {{{ get_default_charset */ |
78 | 800 | static char *get_default_charset(void) { |
79 | 800 | if (PG(internal_encoding) && PG(internal_encoding)[0]) { |
80 | 0 | return PG(internal_encoding); |
81 | 800 | } else if (SG(default_charset) && SG(default_charset)[0] ) { |
82 | 800 | return SG(default_charset); |
83 | 800 | } |
84 | 0 | return NULL; |
85 | 800 | } |
86 | | /* }}} */ |
87 | | |
88 | | /* {{{ get_next_char */ |
89 | | static inline unsigned int get_next_char( |
90 | | enum entity_charset charset, |
91 | | const unsigned char *str, |
92 | | size_t str_len, |
93 | | size_t *cursor, |
94 | | zend_result *status) |
95 | 1.36M | { |
96 | 1.36M | size_t pos = *cursor; |
97 | 1.36M | unsigned int this_char = 0; |
98 | | |
99 | 1.36M | *status = SUCCESS; |
100 | 1.36M | assert(pos <= str_len); |
101 | | |
102 | 1.36M | if (!CHECK_LEN(pos, 1)) |
103 | 0 | MB_FAILURE(pos, 1); |
104 | | |
105 | 1.36M | switch (charset) { |
106 | 1.23M | case cs_utf_8: |
107 | 1.23M | { |
108 | | /* We'll follow strategy 2. from section 3.6.1 of UTR #36: |
109 | | * "In a reported illegal byte sequence, do not include any |
110 | | * non-initial byte that encodes a valid character or is a leading |
111 | | * byte for a valid sequence." */ |
112 | 1.23M | unsigned char c; |
113 | 1.23M | c = str[pos]; |
114 | 1.23M | if (c < 0x80) { |
115 | 445k | this_char = c; |
116 | 445k | pos++; |
117 | 792k | } else if (c < 0xc2) { |
118 | 342k | MB_FAILURE(pos, 1); |
119 | 449k | } else if (c < 0xe0) { |
120 | 306k | if (!CHECK_LEN(pos, 2)) |
121 | 20 | MB_FAILURE(pos, 1); |
122 | | |
123 | 306k | if (!utf8_trail(str[pos + 1])) { |
124 | 226k | MB_FAILURE(pos, utf8_lead(str[pos + 1]) ? 1 : 2); |
125 | 226k | } |
126 | 79.8k | this_char = ((c & 0x1f) << 6) | (str[pos + 1] & 0x3f); |
127 | 79.8k | if (this_char < 0x80) { /* non-shortest form */ |
128 | 0 | MB_FAILURE(pos, 2); |
129 | 0 | } |
130 | 79.8k | pos += 2; |
131 | 142k | } else if (c < 0xf0) { |
132 | 31.7k | size_t avail = str_len - pos; |
133 | | |
134 | 31.7k | if (avail < 3 || |
135 | 31.7k | !utf8_trail(str[pos + 1]) || !utf8_trail(str[pos + 2])) { |
136 | 30.5k | if (avail < 2 || utf8_lead(str[pos + 1])) |
137 | 27.4k | MB_FAILURE(pos, 1); |
138 | 3.18k | else if (avail < 3 || utf8_lead(str[pos + 2])) |
139 | 1.82k | MB_FAILURE(pos, 2); |
140 | 1.36k | else |
141 | 1.36k | MB_FAILURE(pos, 3); |
142 | 30.5k | } |
143 | | |
144 | 1.16k | this_char = ((c & 0x0f) << 12) | ((str[pos + 1] & 0x3f) << 6) | (str[pos + 2] & 0x3f); |
145 | 1.16k | if (this_char < 0x800) { /* non-shortest form */ |
146 | 41 | MB_FAILURE(pos, 3); |
147 | 1.12k | } else if (this_char >= 0xd800 && this_char <= 0xdfff) { /* surrogate */ |
148 | 63 | MB_FAILURE(pos, 3); |
149 | 63 | } |
150 | 1.06k | pos += 3; |
151 | 110k | } else if (c < 0xf5) { |
152 | 10.3k | size_t avail = str_len - pos; |
153 | | |
154 | 10.3k | if (avail < 4 || |
155 | 10.3k | !utf8_trail(str[pos + 1]) || !utf8_trail(str[pos + 2]) || |
156 | 10.3k | !utf8_trail(str[pos + 3])) { |
157 | 9.50k | if (avail < 2 || utf8_lead(str[pos + 1])) |
158 | 8.02k | MB_FAILURE(pos, 1); |
159 | 1.47k | else if (avail < 3 || utf8_lead(str[pos + 2])) |
160 | 738 | MB_FAILURE(pos, 2); |
161 | 738 | else if (avail < 4 || utf8_lead(str[pos + 3])) |
162 | 307 | MB_FAILURE(pos, 3); |
163 | 431 | else |
164 | 431 | MB_FAILURE(pos, 4); |
165 | 9.50k | } |
166 | | |
167 | 880 | this_char = ((c & 0x07) << 18) | ((str[pos + 1] & 0x3f) << 12) | ((str[pos + 2] & 0x3f) << 6) | (str[pos + 3] & 0x3f); |
168 | 880 | if (this_char < 0x10000 || this_char > 0x10FFFF) { /* non-shortest form or outside range */ |
169 | 43 | MB_FAILURE(pos, 4); |
170 | 43 | } |
171 | 837 | pos += 4; |
172 | 100k | } else { |
173 | 100k | MB_FAILURE(pos, 1); |
174 | 100k | } |
175 | 1.23M | } |
176 | 527k | break; |
177 | | |
178 | 527k | case cs_big5: |
179 | | /* reference http://demo.icu-project.org/icu-bin/convexp?conv=big5 */ |
180 | 24.7k | { |
181 | 24.7k | unsigned char c = str[pos]; |
182 | 24.7k | if (c >= 0x81 && c <= 0xFE) { |
183 | 3.08k | unsigned char next; |
184 | 3.08k | if (!CHECK_LEN(pos, 2)) |
185 | 0 | MB_FAILURE(pos, 1); |
186 | | |
187 | 3.08k | next = str[pos + 1]; |
188 | | |
189 | 3.08k | if ((next >= 0x40 && next <= 0x7E) || |
190 | 3.08k | (next >= 0xA1 && next <= 0xFE)) { |
191 | 1.62k | this_char = (c << 8) | next; |
192 | 1.62k | } else { |
193 | 1.45k | MB_FAILURE(pos, 1); |
194 | 1.45k | } |
195 | 1.62k | pos += 2; |
196 | 21.6k | } else { |
197 | 21.6k | this_char = c; |
198 | 21.6k | pos += 1; |
199 | 21.6k | } |
200 | 24.7k | } |
201 | 23.2k | break; |
202 | | |
203 | 23.2k | case cs_big5hkscs: |
204 | 0 | { |
205 | 0 | unsigned char c = str[pos]; |
206 | 0 | if (c >= 0x81 && c <= 0xFE) { |
207 | 0 | unsigned char next; |
208 | 0 | if (!CHECK_LEN(pos, 2)) |
209 | 0 | MB_FAILURE(pos, 1); |
210 | | |
211 | 0 | next = str[pos + 1]; |
212 | |
|
213 | 0 | if ((next >= 0x40 && next <= 0x7E) || |
214 | 0 | (next >= 0xA1 && next <= 0xFE)) { |
215 | 0 | this_char = (c << 8) | next; |
216 | 0 | } else if (next != 0x80 && next != 0xFF) { |
217 | 0 | MB_FAILURE(pos, 1); |
218 | 0 | } else { |
219 | 0 | MB_FAILURE(pos, 2); |
220 | 0 | } |
221 | 0 | pos += 2; |
222 | 0 | } else { |
223 | 0 | this_char = c; |
224 | 0 | pos += 1; |
225 | 0 | } |
226 | 0 | } |
227 | 0 | break; |
228 | | |
229 | 30.5k | case cs_gb2312: /* EUC-CN */ |
230 | 30.5k | { |
231 | 30.5k | unsigned char c = str[pos]; |
232 | 30.5k | if (c >= 0xA1 && c <= 0xFE) { |
233 | 1.10k | unsigned char next; |
234 | 1.10k | if (!CHECK_LEN(pos, 2)) |
235 | 57 | MB_FAILURE(pos, 1); |
236 | | |
237 | 1.04k | next = str[pos + 1]; |
238 | | |
239 | 1.04k | if (gb2312_trail(next)) { |
240 | 165 | this_char = (c << 8) | next; |
241 | 882 | } else if (gb2312_lead(next)) { |
242 | 604 | MB_FAILURE(pos, 1); |
243 | 604 | } else { |
244 | 278 | MB_FAILURE(pos, 2); |
245 | 278 | } |
246 | 165 | pos += 2; |
247 | 29.4k | } else if (gb2312_lead(c)) { |
248 | 26.1k | this_char = c; |
249 | 26.1k | pos += 1; |
250 | 26.1k | } else { |
251 | 3.27k | MB_FAILURE(pos, 1); |
252 | 3.27k | } |
253 | 30.5k | } |
254 | 26.3k | break; |
255 | | |
256 | 26.3k | case cs_sjis: |
257 | 25.1k | { |
258 | 25.1k | unsigned char c = str[pos]; |
259 | 25.1k | if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xFC)) { |
260 | 1.06k | unsigned char next; |
261 | 1.06k | if (!CHECK_LEN(pos, 2)) |
262 | 1 | MB_FAILURE(pos, 1); |
263 | | |
264 | 1.06k | next = str[pos + 1]; |
265 | | |
266 | 1.06k | if (sjis_trail(next)) { |
267 | 717 | this_char = (c << 8) | next; |
268 | 717 | } else if (sjis_lead(next)) { |
269 | 230 | MB_FAILURE(pos, 1); |
270 | 230 | } else { |
271 | 118 | MB_FAILURE(pos, 2); |
272 | 118 | } |
273 | 717 | pos += 2; |
274 | 24.0k | } else if (c < 0x80 || (c >= 0xA1 && c <= 0xDF)) { |
275 | 19.9k | this_char = c; |
276 | 19.9k | pos += 1; |
277 | 19.9k | } else { |
278 | 4.09k | MB_FAILURE(pos, 1); |
279 | 4.09k | } |
280 | 25.1k | } |
281 | 20.6k | break; |
282 | | |
283 | 20.6k | case cs_eucjp: |
284 | 0 | { |
285 | 0 | unsigned char c = str[pos]; |
286 | |
|
287 | 0 | if (c >= 0xA1 && c <= 0xFE) { |
288 | 0 | unsigned next; |
289 | 0 | if (!CHECK_LEN(pos, 2)) |
290 | 0 | MB_FAILURE(pos, 1); |
291 | 0 | next = str[pos + 1]; |
292 | |
|
293 | 0 | if (next >= 0xA1 && next <= 0xFE) { |
294 | | /* this a jis kanji char */ |
295 | 0 | this_char = (c << 8) | next; |
296 | 0 | } else { |
297 | 0 | MB_FAILURE(pos, (next != 0xA0 && next != 0xFF) ? 1 : 2); |
298 | 0 | } |
299 | 0 | pos += 2; |
300 | 0 | } else if (c == 0x8E) { |
301 | 0 | unsigned next; |
302 | 0 | if (!CHECK_LEN(pos, 2)) |
303 | 0 | MB_FAILURE(pos, 1); |
304 | | |
305 | 0 | next = str[pos + 1]; |
306 | 0 | if (next >= 0xA1 && next <= 0xDF) { |
307 | | /* JIS X 0201 kana */ |
308 | 0 | this_char = (c << 8) | next; |
309 | 0 | } else { |
310 | 0 | MB_FAILURE(pos, (next != 0xA0 && next != 0xFF) ? 1 : 2); |
311 | 0 | } |
312 | 0 | pos += 2; |
313 | 0 | } else if (c == 0x8F) { |
314 | 0 | size_t avail = str_len - pos; |
315 | |
|
316 | 0 | if (avail < 3 || !(str[pos + 1] >= 0xA1 && str[pos + 1] <= 0xFE) || |
317 | 0 | !(str[pos + 2] >= 0xA1 && str[pos + 2] <= 0xFE)) { |
318 | 0 | if (avail < 2 || (str[pos + 1] != 0xA0 && str[pos + 1] != 0xFF)) |
319 | 0 | MB_FAILURE(pos, 1); |
320 | 0 | else if (avail < 3 || (str[pos + 2] != 0xA0 && str[pos + 2] != 0xFF)) |
321 | 0 | MB_FAILURE(pos, 2); |
322 | 0 | else |
323 | 0 | MB_FAILURE(pos, 3); |
324 | 0 | } else { |
325 | | /* JIS X 0212 hojo-kanji */ |
326 | 0 | this_char = (c << 16) | (str[pos + 1] << 8) | str[pos + 2]; |
327 | 0 | } |
328 | 0 | pos += 3; |
329 | 0 | } else if (c != 0xA0 && c != 0xFF) { |
330 | | /* character encoded in 1 code unit */ |
331 | 0 | this_char = c; |
332 | 0 | pos += 1; |
333 | 0 | } else { |
334 | 0 | MB_FAILURE(pos, 1); |
335 | 0 | } |
336 | 0 | } |
337 | 0 | break; |
338 | 46.1k | default: |
339 | | /* single-byte charsets */ |
340 | 46.1k | this_char = str[pos++]; |
341 | 46.1k | break; |
342 | 1.36M | } |
343 | | |
344 | 643k | *cursor = pos; |
345 | 643k | return this_char; |
346 | 1.36M | } |
347 | | /* }}} */ |
348 | | |
349 | | /* {{{ php_next_utf8_char |
350 | | * Public interface for get_next_char used with UTF-8 */ |
351 | | PHPAPI unsigned int php_next_utf8_char( |
352 | | const unsigned char *str, |
353 | | size_t str_len, |
354 | | size_t *cursor, |
355 | | zend_result *status) |
356 | 81.9k | { |
357 | 81.9k | return get_next_char(cs_utf_8, str, str_len, cursor, status); |
358 | 81.9k | } |
359 | | /* }}} */ |
360 | | |
361 | | /* {{{ entity_charset determine_charset |
362 | | * Returns the charset identifier based on an explicitly provided charset, |
363 | | * the internal_encoding and default_charset ini settings, or UTF-8 by default. */ |
364 | | static enum entity_charset determine_charset(const char *charset_hint, bool quiet) |
365 | 1.47k | { |
366 | 1.47k | if (!charset_hint || !*charset_hint) { |
367 | 800 | charset_hint = get_default_charset(); |
368 | 800 | } |
369 | | |
370 | 1.47k | if (charset_hint && *charset_hint) { |
371 | 1.47k | size_t len = strlen(charset_hint); |
372 | | /* now walk the charset map and look for the codeset */ |
373 | 22.9k | for (size_t i = 0; i < sizeof(charset_map)/sizeof(charset_map[0]); i++) { |
374 | 22.5k | if (len == charset_map[i].codeset_len && |
375 | 22.5k | zend_binary_strcasecmp(charset_hint, len, charset_map[i].codeset, len) == 0) { |
376 | 1.08k | return charset_map[i].charset; |
377 | 1.08k | } |
378 | 22.5k | } |
379 | | |
380 | 390 | if (!quiet) { |
381 | 390 | php_error_docref(NULL, E_WARNING, "Charset \"%s\" is not supported, assuming UTF-8", |
382 | 390 | charset_hint); |
383 | 390 | } |
384 | 390 | } |
385 | | |
386 | 390 | return cs_utf_8; |
387 | 1.47k | } |
388 | | /* }}} */ |
389 | | |
390 | | /* {{{ php_utf32_utf8 */ |
391 | | static inline size_t php_utf32_utf8(unsigned char *buf, unsigned k) |
392 | 0 | { |
393 | 0 | size_t retval = 0; |
394 | | |
395 | | /* assert(0x0 <= k <= 0x10FFFF); */ |
396 | |
|
397 | 0 | if (k < 0x80) { |
398 | 0 | buf[0] = k; |
399 | 0 | retval = 1; |
400 | 0 | } else if (k < 0x800) { |
401 | 0 | buf[0] = 0xc0 | (k >> 6); |
402 | 0 | buf[1] = 0x80 | (k & 0x3f); |
403 | 0 | retval = 2; |
404 | 0 | } else if (k < 0x10000) { |
405 | 0 | buf[0] = 0xe0 | (k >> 12); |
406 | 0 | buf[1] = 0x80 | ((k >> 6) & 0x3f); |
407 | 0 | buf[2] = 0x80 | (k & 0x3f); |
408 | 0 | retval = 3; |
409 | 0 | } else { |
410 | 0 | buf[0] = 0xf0 | (k >> 18); |
411 | 0 | buf[1] = 0x80 | ((k >> 12) & 0x3f); |
412 | 0 | buf[2] = 0x80 | ((k >> 6) & 0x3f); |
413 | 0 | buf[3] = 0x80 | (k & 0x3f); |
414 | 0 | retval = 4; |
415 | 0 | } |
416 | | /* UTF-8 has been restricted to max 4 bytes since RFC 3629 */ |
417 | |
|
418 | 0 | return retval; |
419 | 0 | } |
420 | | /* }}} */ |
421 | | |
422 | | /* {{{ unimap_bsearc_cmp |
423 | | * Binary search of unicode code points in unicode <--> charset mapping. |
424 | | * Returns the code point in the target charset (whose mapping table was given) or 0 if |
425 | | * the unicode code point is not in the table. |
426 | | */ |
427 | | static inline unsigned char unimap_bsearch(const uni_to_enc *table, unsigned code_key_a, size_t num) |
428 | 0 | { |
429 | 0 | const uni_to_enc *l = table, |
430 | 0 | *h = &table[num-1], |
431 | 0 | *m; |
432 | 0 | unsigned short code_key; |
433 | | |
434 | | /* we have no mappings outside the BMP */ |
435 | 0 | if (code_key_a > 0xFFFFU) |
436 | 0 | return 0; |
437 | | |
438 | 0 | code_key = (unsigned short) code_key_a; |
439 | |
|
440 | 0 | while (l <= h) { |
441 | 0 | m = l + (h - l) / 2; |
442 | 0 | if (code_key < m->un_code_point) |
443 | 0 | h = m - 1; |
444 | 0 | else if (code_key > m->un_code_point) |
445 | 0 | l = m + 1; |
446 | 0 | else |
447 | 0 | return m->cs_code; |
448 | 0 | } |
449 | 0 | return 0; |
450 | 0 | } |
451 | | /* }}} */ |
452 | | |
453 | | /* {{{ map_from_unicode */ |
454 | | static inline zend_result map_from_unicode(unsigned code, enum entity_charset charset, unsigned *res) |
455 | 11.5k | { |
456 | 11.5k | unsigned char found; |
457 | 11.5k | const uni_to_enc *table; |
458 | 11.5k | size_t table_size; |
459 | | |
460 | 11.5k | switch (charset) { |
461 | 11.5k | case cs_8859_1: |
462 | | /* identity mapping of code points to unicode */ |
463 | 11.5k | if (code > 0xFF) { |
464 | 0 | return FAILURE; |
465 | 0 | } |
466 | 11.5k | *res = code; |
467 | 11.5k | break; |
468 | | |
469 | 0 | case cs_8859_5: |
470 | 0 | if (code <= 0xA0 || code == 0xAD /* soft hyphen */) { |
471 | 0 | *res = code; |
472 | 0 | } else if (code == 0x2116) { |
473 | 0 | *res = 0xF0; /* numero sign */ |
474 | 0 | } else if (code == 0xA7) { |
475 | 0 | *res = 0xFD; /* section sign */ |
476 | 0 | } else if (code >= 0x0401 && code <= 0x045F) { |
477 | 0 | if (code == 0x040D || code == 0x0450 || code == 0x045D) |
478 | 0 | return FAILURE; |
479 | 0 | *res = code - 0x360; |
480 | 0 | } else { |
481 | 0 | return FAILURE; |
482 | 0 | } |
483 | 0 | break; |
484 | | |
485 | 0 | case cs_8859_15: |
486 | 0 | if (code < 0xA4 || (code > 0xBE && code <= 0xFF)) { |
487 | 0 | *res = code; |
488 | 0 | } else { /* between A4 and 0xBE */ |
489 | 0 | found = unimap_bsearch(unimap_iso885915, |
490 | 0 | code, sizeof(unimap_iso885915) / sizeof(*unimap_iso885915)); |
491 | 0 | if (found) |
492 | 0 | *res = found; |
493 | 0 | else |
494 | 0 | return FAILURE; |
495 | 0 | } |
496 | 0 | break; |
497 | | |
498 | 0 | case cs_cp1252: |
499 | 0 | if (code <= 0x7F || (code >= 0xA0 && code <= 0xFF)) { |
500 | 0 | *res = code; |
501 | 0 | } else { |
502 | 0 | found = unimap_bsearch(unimap_win1252, |
503 | 0 | code, sizeof(unimap_win1252) / sizeof(*unimap_win1252)); |
504 | 0 | if (found) |
505 | 0 | *res = found; |
506 | 0 | else |
507 | 0 | return FAILURE; |
508 | 0 | } |
509 | 0 | break; |
510 | | |
511 | 0 | case cs_macroman: |
512 | 0 | if (code == 0x7F) |
513 | 0 | return FAILURE; |
514 | 0 | table = unimap_macroman; |
515 | 0 | table_size = sizeof(unimap_macroman) / sizeof(*unimap_macroman); |
516 | 0 | goto table_over_7F; |
517 | 0 | case cs_cp1251: |
518 | 0 | table = unimap_win1251; |
519 | 0 | table_size = sizeof(unimap_win1251) / sizeof(*unimap_win1251); |
520 | 0 | goto table_over_7F; |
521 | 0 | case cs_koi8r: |
522 | 0 | table = unimap_koi8r; |
523 | 0 | table_size = sizeof(unimap_koi8r) / sizeof(*unimap_koi8r); |
524 | 0 | goto table_over_7F; |
525 | 0 | case cs_cp866: |
526 | 0 | table = unimap_cp866; |
527 | 0 | table_size = sizeof(unimap_cp866) / sizeof(*unimap_cp866); |
528 | |
|
529 | 0 | table_over_7F: |
530 | 0 | if (code <= 0x7F) { |
531 | 0 | *res = code; |
532 | 0 | } else { |
533 | 0 | found = unimap_bsearch(table, code, table_size); |
534 | 0 | if (found) |
535 | 0 | *res = found; |
536 | 0 | else |
537 | 0 | return FAILURE; |
538 | 0 | } |
539 | 0 | break; |
540 | | |
541 | | /* from here on, only map the possible characters in the ASCII range. |
542 | | * to improve support here, it's a matter of building the unicode mappings. |
543 | | * See <http://www.unicode.org/Public/6.0.0/ucd/Unihan.zip> */ |
544 | 0 | case cs_sjis: |
545 | 0 | case cs_eucjp: |
546 | | /* we interpret 0x5C as the Yen symbol. This is not universal. |
547 | | * See <http://www.w3.org/Submission/japanese-xml/#ambiguity_of_yen> */ |
548 | 0 | if (code >= 0x20 && code <= 0x7D) { |
549 | 0 | if (code == 0x5C) |
550 | 0 | return FAILURE; |
551 | 0 | *res = code; |
552 | 0 | } else { |
553 | 0 | return FAILURE; |
554 | 0 | } |
555 | 0 | break; |
556 | | |
557 | 0 | case cs_big5: |
558 | 0 | case cs_big5hkscs: |
559 | 0 | case cs_gb2312: |
560 | 0 | if (code >= 0x20 && code <= 0x7D) { |
561 | 0 | *res = code; |
562 | 0 | } else { |
563 | 0 | return FAILURE; |
564 | 0 | } |
565 | 0 | break; |
566 | | |
567 | 0 | default: |
568 | 0 | return FAILURE; |
569 | 11.5k | } |
570 | | |
571 | 11.5k | return SUCCESS; |
572 | 11.5k | } |
573 | | /* }}} */ |
574 | | |
575 | | /* {{{ */ |
576 | | static inline void map_to_unicode(unsigned code, const enc_to_uni *table, unsigned *res) |
577 | 45.8k | { |
578 | | /* only single byte encodings are currently supported; assumed code <= 0xFF */ |
579 | 45.8k | *res = table->inner[ENT_ENC_TO_UNI_STAGE1(code)]->uni_cp[ENT_ENC_TO_UNI_STAGE2(code)]; |
580 | 45.8k | } |
581 | | /* }}} */ |
582 | | |
583 | | /* {{{ unicode_cp_is_allowed */ |
584 | | static inline int unicode_cp_is_allowed(unsigned uni_cp, int document_type) |
585 | 204k | { |
586 | | /* XML 1.0 HTML 4.01 HTML 5 |
587 | | * 0x09..0x0A 0x09..0x0A 0x09..0x0A |
588 | | * 0x0D 0x0D 0x0C..0x0D |
589 | | * 0x0020..0xD7FF 0x20..0x7E 0x20..0x7E |
590 | | * 0x00A0..0xD7FF 0x00A0..0xD7FF |
591 | | * 0xE000..0xFFFD 0xE000..0x10FFFF 0xE000..0xFDCF |
592 | | * 0x010000..0x10FFFF 0xFDF0..0x10FFFF (*) |
593 | | * |
594 | | * (*) exclude code points where ((code & 0xFFFF) >= 0xFFFE) |
595 | | * |
596 | | * References: |
597 | | * XML 1.0: <http://www.w3.org/TR/REC-xml/#charsets> |
598 | | * HTML 4.01: <http://www.w3.org/TR/1999/PR-html40-19990824/sgml/sgmldecl.html> |
599 | | * HTML 5: <http://dev.w3.org/html5/spec/Overview.html#preprocessing-the-input-stream> |
600 | | * |
601 | | * Not sure this is the relevant part for HTML 5, though. I opted to |
602 | | * disallow the characters that would result in a parse error when |
603 | | * preprocessing of the input stream. See also section 8.1.3. |
604 | | * |
605 | | * It's unclear if XHTML 1.0 allows C1 characters. I'll opt to apply to |
606 | | * XHTML 1.0 the same rules as for XML 1.0. |
607 | | * See <http://cmsmcq.com/2007/C1.xml>. |
608 | | */ |
609 | | |
610 | 204k | switch (document_type) { |
611 | 67.0k | case ENT_HTML_DOC_HTML401: |
612 | 67.0k | return (uni_cp >= 0x20 && uni_cp <= 0x7E) || |
613 | 67.0k | (uni_cp == 0x0A || uni_cp == 0x09 || uni_cp == 0x0D) || |
614 | 67.0k | (uni_cp >= 0xA0 && uni_cp <= 0xD7FF) || |
615 | 67.0k | (uni_cp >= 0xE000 && uni_cp <= 0x10FFFF); |
616 | 35.5k | case ENT_HTML_DOC_HTML5: |
617 | 35.5k | return (uni_cp >= 0x20 && uni_cp <= 0x7E) || |
618 | 35.5k | (uni_cp >= 0x09 && uni_cp <= 0x0D && uni_cp != 0x0B) || /* form feed U+0C allowed */ |
619 | 35.5k | (uni_cp >= 0xA0 && uni_cp <= 0xD7FF) || |
620 | 35.5k | (uni_cp >= 0xE000 && uni_cp <= 0x10FFFF && |
621 | 15.9k | ((uni_cp & 0xFFFF) < 0xFFFE) && /* last two of each plane (nonchars) disallowed */ |
622 | 15.9k | (uni_cp < 0xFDD0 || uni_cp > 0xFDEF)); /* U+FDD0-U+FDEF (nonchars) disallowed */ |
623 | 86.3k | case ENT_HTML_DOC_XHTML: |
624 | 101k | case ENT_HTML_DOC_XML1: |
625 | 101k | return (uni_cp >= 0x20 && uni_cp <= 0xD7FF) || |
626 | 101k | (uni_cp == 0x0A || uni_cp == 0x09 || uni_cp == 0x0D) || |
627 | 101k | (uni_cp >= 0xE000 && uni_cp <= 0x10FFFF && uni_cp != 0xFFFE && uni_cp != 0xFFFF); |
628 | 0 | default: |
629 | 0 | return 1; |
630 | 204k | } |
631 | 204k | } |
632 | | /* }}} */ |
633 | | |
634 | | /* {{{ unicode_cp_is_allowed */ |
635 | | static inline int numeric_entity_is_allowed(unsigned uni_cp, int document_type) |
636 | 0 | { |
637 | | /* less restrictive than unicode_cp_is_allowed */ |
638 | 0 | switch (document_type) { |
639 | 0 | case ENT_HTML_DOC_HTML401: |
640 | | /* all non-SGML characters (those marked with UNUSED in DESCSET) should be |
641 | | * representable with numeric entities */ |
642 | 0 | return uni_cp <= 0x10FFFF; |
643 | 0 | case ENT_HTML_DOC_HTML5: |
644 | | /* 8.1.4. The numeric character reference forms described above are allowed to |
645 | | * reference any Unicode code point other than U+0000, U+000D, permanently |
646 | | * undefined Unicode characters (noncharacters), and control characters other |
647 | | * than space characters (U+0009, U+000A, U+000C and U+000D) */ |
648 | | /* seems to allow surrogate characters, then */ |
649 | 0 | return (uni_cp >= 0x20 && uni_cp <= 0x7E) || |
650 | 0 | (uni_cp >= 0x09 && uni_cp <= 0x0C && uni_cp != 0x0B) || /* form feed U+0C allowed, but not U+0D */ |
651 | 0 | (uni_cp >= 0xA0 && uni_cp <= 0x10FFFF && |
652 | 0 | ((uni_cp & 0xFFFF) < 0xFFFE) && /* last two of each plane (nonchars) disallowed */ |
653 | 0 | (uni_cp < 0xFDD0 || uni_cp > 0xFDEF)); /* U+FDD0-U+FDEF (nonchars) disallowed */ |
654 | 0 | case ENT_HTML_DOC_XHTML: |
655 | 0 | case ENT_HTML_DOC_XML1: |
656 | | /* OTOH, XML 1.0 requires "character references to match the production for Char |
657 | | * See <http://www.w3.org/TR/REC-xml/#NT-CharRef> */ |
658 | 0 | return unicode_cp_is_allowed(uni_cp, document_type); |
659 | 0 | default: |
660 | 0 | return 1; |
661 | 0 | } |
662 | 0 | } |
663 | | /* }}} */ |
664 | | |
665 | | /* {{{ process_numeric_entity |
666 | | * Auxiliary function to traverse_for_entities. |
667 | | * On input, *buf should point to the first character after # and on output, it's the last |
668 | | * byte read, no matter if there was success or insuccess. |
669 | | */ |
670 | | static inline zend_result process_numeric_entity(const char **buf, unsigned *code_point) |
671 | 11 | { |
672 | 11 | zend_long code_l; |
673 | 11 | int hexadecimal = (**buf == 'x' || **buf == 'X'); /* TODO: XML apparently disallows "X" */ |
674 | 11 | char *endptr; |
675 | | |
676 | 11 | if (hexadecimal) |
677 | 0 | (*buf)++; |
678 | | |
679 | | /* strtol allows whitespace and other stuff in the beginning |
680 | | * we're not interested */ |
681 | 11 | if ((hexadecimal && !isxdigit(**buf)) || |
682 | 11 | (!hexadecimal && !isdigit(**buf))) { |
683 | 10 | return FAILURE; |
684 | 10 | } |
685 | | |
686 | 1 | code_l = ZEND_STRTOL(*buf, &endptr, hexadecimal ? 16 : 10); |
687 | | /* we're guaranteed there were valid digits, so *endptr > buf */ |
688 | 1 | *buf = endptr; |
689 | | |
690 | 1 | if (**buf != ';') |
691 | 0 | return FAILURE; |
692 | | |
693 | | /* many more are invalid, but that depends on whether it's HTML |
694 | | * (and which version) or XML. */ |
695 | 1 | if (code_l > Z_L(0x10FFFF)) |
696 | 0 | return FAILURE; |
697 | | |
698 | 1 | if (code_point != NULL) |
699 | 1 | *code_point = (unsigned)code_l; |
700 | | |
701 | 1 | return SUCCESS; |
702 | 1 | } |
703 | | /* }}} */ |
704 | | |
705 | | /* {{{ process_named_entity */ |
706 | | static inline zend_result process_named_entity_html(const char **buf, const char **start, size_t *length) |
707 | 2.52k | { |
708 | 2.52k | *start = *buf; |
709 | | |
710 | | /* "&" is represented by a 0x26 in all supported encodings. That means |
711 | | * the byte after represents a character or is the leading byte of a |
712 | | * sequence of 8-bit code units. If in the ranges below, it represents |
713 | | * necessarily an alpha character because none of the supported encodings |
714 | | * has an overlap with ASCII in the leading byte (only on the second one) */ |
715 | 5.89k | while ((**buf >= 'a' && **buf <= 'z') || |
716 | 5.89k | (**buf >= 'A' && **buf <= 'Z') || |
717 | 5.89k | (**buf >= '0' && **buf <= '9')) { |
718 | 3.37k | (*buf)++; |
719 | 3.37k | } |
720 | | |
721 | 2.52k | if (**buf != ';') |
722 | 2.42k | return FAILURE; |
723 | | |
724 | | /* cast to size_t OK as the quantity is always non-negative */ |
725 | 98 | *length = *buf - *start; |
726 | | |
727 | 98 | if (*length == 0) |
728 | 7 | return FAILURE; |
729 | | |
730 | 91 | return SUCCESS; |
731 | 98 | } |
732 | | /* }}} */ |
733 | | |
734 | | /* {{{ resolve_named_entity_html */ |
735 | | static zend_result resolve_named_entity_html(const char *start, size_t length, const entity_ht *ht, unsigned *uni_cp1, unsigned *uni_cp2) |
736 | 11.7k | { |
737 | 11.7k | const entity_cp_map *s; |
738 | 11.7k | zend_ulong hash = zend_inline_hash_func(start, length); |
739 | | |
740 | 11.7k | s = ht->buckets[hash % ht->num_elems]; |
741 | 11.9k | while (s->entity) { |
742 | 11.6k | if (s->entity_len == length) { |
743 | 11.5k | if (memcmp(start, s->entity, length) == 0) { |
744 | 11.5k | *uni_cp1 = s->codepoint1; |
745 | 11.5k | *uni_cp2 = s->codepoint2; |
746 | 11.5k | return SUCCESS; |
747 | 11.5k | } |
748 | 11.5k | } |
749 | 136 | s++; |
750 | 136 | } |
751 | 216 | return FAILURE; |
752 | 11.7k | } |
753 | | /* }}} */ |
754 | | |
755 | 11.5k | static inline size_t write_octet_sequence(unsigned char *buf, enum entity_charset charset, unsigned code) { |
756 | | /* code is not necessarily a unicode code point */ |
757 | 11.5k | switch (charset) { |
758 | 0 | case cs_utf_8: |
759 | 0 | return php_utf32_utf8(buf, code); |
760 | | |
761 | 11.5k | case cs_8859_1: |
762 | 11.5k | case cs_cp1252: |
763 | 11.5k | case cs_8859_15: |
764 | 11.5k | case cs_koi8r: |
765 | 11.5k | case cs_cp1251: |
766 | 11.5k | case cs_8859_5: |
767 | 11.5k | case cs_cp866: |
768 | 11.5k | case cs_macroman: |
769 | | /* single byte stuff */ |
770 | 11.5k | *buf = code; |
771 | 11.5k | return 1; |
772 | | |
773 | 0 | case cs_big5: |
774 | 0 | case cs_big5hkscs: |
775 | 0 | case cs_sjis: |
776 | 0 | case cs_gb2312: |
777 | | /* we don't have complete unicode mappings for these yet in entity_decode, |
778 | | * and we opt to pass through the octet sequences for these in htmlentities |
779 | | * instead of converting to an int and then converting back. */ |
780 | | #if 0 |
781 | | return php_mb2_int_to_char(buf, code); |
782 | | #else |
783 | 0 | ZEND_ASSERT(code <= 0xFFU); |
784 | 0 | *buf = code; |
785 | 0 | return 1; |
786 | 0 | #endif |
787 | | |
788 | 0 | case cs_eucjp: |
789 | | #if 0 /* idem */ |
790 | | return php_mb2_int_to_char(buf, code); |
791 | | #else |
792 | 0 | ZEND_ASSERT(code <= 0xFFU); |
793 | 0 | *buf = code; |
794 | 0 | return 1; |
795 | 0 | #endif |
796 | | |
797 | 0 | default: |
798 | 0 | assert(0); |
799 | 0 | return 0; |
800 | 11.5k | } |
801 | 11.5k | } |
802 | | |
803 | | /* {{{ traverse_for_entities |
804 | | * Auxiliary function to php_unescape_html_entities(). |
805 | | * - The argument "all" determines if all numeric entities are decode or only those |
806 | | * that correspond to quotes (depending on quote_style). |
807 | | */ |
808 | | /* maximum expansion (factor 1.2) for HTML 5 with ≫⃒ and ≪⃒ */ |
809 | | /* +2 is 1 because of rest (probably unnecessary), 1 because of terminating 0 */ |
810 | 512 | #define TRAVERSE_FOR_ENTITIES_EXPAND_SIZE(oldlen) ((oldlen) + (oldlen) / 5 + 2) |
811 | | static void traverse_for_entities( |
812 | | const zend_string *input, |
813 | | zend_string *output, /* should have allocated TRAVERSE_FOR_ENTITIES_EXPAND_SIZE(olden) */ |
814 | | const int all, |
815 | | const int flags, |
816 | | const entity_ht *inv_map, |
817 | | const enum entity_charset charset) |
818 | 512 | { |
819 | 512 | const char *current_ptr = ZSTR_VAL(input); |
820 | 512 | const char *input_end = current_ptr + ZSTR_LEN(input); /* terminator address */ |
821 | 512 | char *output_ptr = ZSTR_VAL(output); |
822 | 512 | const int doctype = flags & ENT_HTML_DOC_TYPE_MASK; |
823 | | |
824 | 12.2k | while (current_ptr < input_end) { |
825 | 12.2k | const char *ampersand_ptr = memchr(current_ptr, '&', input_end - current_ptr); |
826 | 12.2k | if (!ampersand_ptr) { |
827 | 512 | const size_t tail_len = input_end - current_ptr; |
828 | 512 | if (tail_len > 0) { |
829 | 512 | memcpy(output_ptr, current_ptr, tail_len); |
830 | 512 | output_ptr += tail_len; |
831 | 512 | } |
832 | 512 | break; |
833 | 512 | } |
834 | | |
835 | | /* Copy everything up to the found '&' */ |
836 | 11.7k | const size_t chunk_len = ampersand_ptr - current_ptr; |
837 | 11.7k | if (chunk_len > 0) { |
838 | 6.18k | memcpy(output_ptr, current_ptr, chunk_len); |
839 | 6.18k | output_ptr += chunk_len; |
840 | 6.18k | } |
841 | | |
842 | | /* Now current_ptr points to the '&' character. */ |
843 | 11.7k | current_ptr = ampersand_ptr; |
844 | | |
845 | | /* If there are less than 4 bytes remaining, there isn't enough for an entity - |
846 | | * copy '&' as a normal character. */ |
847 | 11.7k | if (input_end - current_ptr < 4) { |
848 | 0 | const size_t remaining = input_end - current_ptr; |
849 | 0 | memcpy(output_ptr, current_ptr, remaining); |
850 | 0 | output_ptr += remaining; |
851 | 0 | break; |
852 | 0 | } |
853 | | |
854 | 11.7k | unsigned code = 0, code2 = 0; |
855 | 11.7k | const char *entity_end_ptr = NULL; |
856 | | |
857 | 11.7k | if (current_ptr[1] == '#') { |
858 | | /* Processing numeric entity */ |
859 | 0 | const char *num_start = current_ptr + 2; |
860 | 0 | entity_end_ptr = num_start; |
861 | 0 | if (process_numeric_entity(&entity_end_ptr, &code) == FAILURE) { |
862 | 0 | goto invalid_incomplete_entity; |
863 | 0 | } |
864 | 0 | if (!all && (code > 63U || stage3_table_be_apos_00000[code].data.ent.entity == NULL)) { |
865 | | /* If we're in htmlspecialchars_decode, we're only decoding entities |
866 | | * that represent &, <, >, " and '. Is this one of them? */ |
867 | 0 | goto invalid_incomplete_entity; |
868 | 0 | } else if (!unicode_cp_is_allowed(code, doctype) || |
869 | 0 | (doctype == ENT_HTML_DOC_HTML5 && code == 0x0D)) { |
870 | | /* are we allowed to decode this entity in this document type? |
871 | | * HTML 5 is the only that has a character that cannot be used in |
872 | | * a numeric entity but is allowed literally (U+000D). The |
873 | | * unoptimized version would be ... || !numeric_entity_is_allowed(code) */ |
874 | 0 | goto invalid_incomplete_entity; |
875 | 0 | } |
876 | 11.7k | } else { |
877 | | /* Processing named entity */ |
878 | 11.7k | const char *name_start = current_ptr + 1; |
879 | | /* Search for ';' */ |
880 | 11.7k | const size_t max_search_len = MIN(LONGEST_ENTITY_LENGTH + 1, input_end - name_start); |
881 | 11.7k | const char *semi_colon_ptr = memchr(name_start, ';', max_search_len); |
882 | 11.7k | if (!semi_colon_ptr) { |
883 | 46 | goto invalid_incomplete_entity; |
884 | 11.6k | } else { |
885 | 11.6k | const size_t name_len = semi_colon_ptr - name_start; |
886 | 11.6k | if (name_len == 0) { |
887 | 0 | goto invalid_incomplete_entity; |
888 | 11.6k | } else { |
889 | 11.6k | if (resolve_named_entity_html(name_start, name_len, inv_map, &code, &code2) == FAILURE) { |
890 | 142 | if (doctype == ENT_HTML_DOC_XHTML && name_len == 4 && |
891 | 142 | name_start[0] == 'a' && name_start[1] == 'p' && |
892 | 142 | name_start[2] == 'o' && name_start[3] == 's') |
893 | 0 | { |
894 | | /* uses html4 inv_map, which doesn't include apos;. This is a |
895 | | * hack to support it */ |
896 | 0 | code = (unsigned)'\''; |
897 | 142 | } else { |
898 | 142 | goto invalid_incomplete_entity; |
899 | 142 | } |
900 | 142 | } |
901 | 11.5k | entity_end_ptr = semi_colon_ptr; |
902 | 11.5k | } |
903 | 11.6k | } |
904 | 11.7k | } |
905 | | |
906 | | /* At this stage the entity_end_ptr should be always set. */ |
907 | 11.5k | ZEND_ASSERT(entity_end_ptr != NULL); |
908 | | |
909 | | /* Check if quotes are allowed for entities representing ' or " */ |
910 | 11.5k | if ((code == '\'' && !(flags & ENT_HTML_QUOTE_SINGLE)) || |
911 | 11.5k | (code == '"' && !(flags & ENT_HTML_QUOTE_DOUBLE))) |
912 | 0 | { |
913 | 0 | goto invalid_complete_entity; |
914 | 0 | } |
915 | | |
916 | | /* UTF-8 doesn't need mapping (ISO-8859-1 doesn't either, but |
917 | | * the call is needed to ensure the codepoint <= U+00FF) */ |
918 | 11.5k | if (charset != cs_utf_8) { |
919 | | /* replace unicode code point */ |
920 | 11.5k | if (map_from_unicode(code, charset, &code) == FAILURE || code2 != 0) { |
921 | 0 | goto invalid_complete_entity; |
922 | 0 | } |
923 | 11.5k | } |
924 | | |
925 | | /* Write the parsed entity into the output buffer */ |
926 | 11.5k | output_ptr += write_octet_sequence((unsigned char*)output_ptr, charset, code); |
927 | 11.5k | if (code2) { |
928 | 0 | output_ptr += write_octet_sequence((unsigned char*)output_ptr, charset, code2); |
929 | 0 | } |
930 | | /* Move current_ptr past the semicolon */ |
931 | 11.5k | current_ptr = entity_end_ptr + 1; |
932 | 11.5k | continue; |
933 | | |
934 | 188 | invalid_incomplete_entity: |
935 | | /* If the entity is invalid at parse stage or entity_end_ptr was never found, copy '&' as normal */ |
936 | 188 | *output_ptr++ = *current_ptr++; |
937 | 188 | continue; |
938 | | |
939 | 0 | invalid_complete_entity: |
940 | | /* If the entity became invalid after we found entity_end_ptr */ |
941 | 0 | if (entity_end_ptr) { |
942 | 0 | const size_t len = entity_end_ptr - current_ptr; |
943 | 0 | memcpy(output_ptr, current_ptr, len); |
944 | 0 | output_ptr += len; |
945 | 0 | current_ptr = entity_end_ptr; |
946 | 0 | } else { |
947 | 0 | *output_ptr++ = *current_ptr++; |
948 | 0 | } |
949 | 0 | continue; |
950 | 11.5k | } |
951 | | |
952 | 512 | *output_ptr = '\0'; |
953 | 512 | ZSTR_LEN(output) = (size_t)(output_ptr - ZSTR_VAL(output)); |
954 | 512 | } |
955 | | /* }}} */ |
956 | | |
957 | | /* {{{ unescape_inverse_map */ |
958 | | static const entity_ht *unescape_inverse_map(int all, int flags) |
959 | 539 | { |
960 | 539 | int document_type = flags & ENT_HTML_DOC_TYPE_MASK; |
961 | | |
962 | 539 | if (all) { |
963 | 27 | switch (document_type) { |
964 | 25 | case ENT_HTML_DOC_HTML401: |
965 | 27 | case ENT_HTML_DOC_XHTML: /* but watch out for '...*/ |
966 | 27 | return &ent_ht_html4; |
967 | 0 | case ENT_HTML_DOC_HTML5: |
968 | 0 | return &ent_ht_html5; |
969 | 0 | default: |
970 | 0 | return &ent_ht_be_apos; |
971 | 27 | } |
972 | 512 | } else { |
973 | 512 | switch (document_type) { |
974 | 512 | case ENT_HTML_DOC_HTML401: |
975 | 512 | return &ent_ht_be_noapos; |
976 | 0 | default: |
977 | 0 | return &ent_ht_be_apos; |
978 | 512 | } |
979 | 512 | } |
980 | 539 | } |
981 | | /* }}} */ |
982 | | |
983 | | /* {{{ determine_entity_table |
984 | | * Entity table to use. Note that entity tables are defined in terms of |
985 | | * unicode code points */ |
986 | | static entity_table_opt determine_entity_table(int all, int doctype) |
987 | 1.47k | { |
988 | 1.47k | entity_table_opt retval = {0}; |
989 | | |
990 | 1.47k | assert(!(doctype == ENT_HTML_DOC_XML1 && all)); |
991 | | |
992 | 1.47k | if (all) { |
993 | 443 | retval.ms_table = (doctype == ENT_HTML_DOC_HTML5) ? |
994 | 267 | entity_ms_table_html5 : entity_ms_table_html4; |
995 | 1.02k | } else { |
996 | 1.02k | retval.table = (doctype == ENT_HTML_DOC_HTML401) ? |
997 | 625 | stage3_table_be_noapos_00000 : stage3_table_be_apos_00000; |
998 | 1.02k | } |
999 | 1.47k | return retval; |
1000 | 1.47k | } |
1001 | | /* }}} */ |
1002 | | |
1003 | | /* {{{ php_unescape_html_entities |
1004 | | * The parameter "all" should be true to decode all possible entities, false to decode |
1005 | | * only the basic ones, i.e., those in basic_entities_ex + the numeric entities |
1006 | | * that correspond to quotes. |
1007 | | */ |
1008 | | PHPAPI zend_string *php_unescape_html_entities(zend_string *str, int all, int flags, const char *hint_charset) |
1009 | 672 | { |
1010 | 672 | zend_string *ret; |
1011 | 672 | enum entity_charset charset; |
1012 | 672 | const entity_ht *inverse_map; |
1013 | 672 | size_t new_size; |
1014 | | |
1015 | 672 | if (!memchr(ZSTR_VAL(str), '&', ZSTR_LEN(str))) { |
1016 | 160 | return zend_string_copy(str); |
1017 | 160 | } |
1018 | | |
1019 | 512 | if (all) { |
1020 | 0 | charset = determine_charset(hint_charset, /* quiet */ 0); |
1021 | 512 | } else { |
1022 | 512 | charset = cs_8859_1; /* charset shouldn't matter, use ISO-8859-1 for performance */ |
1023 | 512 | } |
1024 | | |
1025 | | /* don't use LIMIT_ALL! */ |
1026 | | |
1027 | 512 | new_size = TRAVERSE_FOR_ENTITIES_EXPAND_SIZE(ZSTR_LEN(str)); |
1028 | 512 | if (ZSTR_LEN(str) > new_size) { |
1029 | | /* overflow, refuse to do anything */ |
1030 | 0 | return zend_string_copy(str); |
1031 | 0 | } |
1032 | | |
1033 | 512 | ret = zend_string_alloc(new_size, 0); |
1034 | | |
1035 | 512 | inverse_map = unescape_inverse_map(all, flags); |
1036 | | |
1037 | | /* replace numeric entities */ |
1038 | 512 | traverse_for_entities(str, ret, all, flags, inverse_map, charset); |
1039 | | |
1040 | 512 | return ret; |
1041 | 512 | } |
1042 | | /* }}} */ |
1043 | | |
1044 | | PHPAPI zend_string *php_escape_html_entities(const unsigned char *old, size_t oldlen, int all, int flags, const char *hint_charset) |
1045 | 0 | { |
1046 | 0 | return php_escape_html_entities_ex(old, oldlen, all, flags, hint_charset, 1, /* quiet */ 0); |
1047 | 0 | } |
1048 | | |
1049 | | /* {{{ find_entity_for_char */ |
1050 | | static inline void find_entity_for_char( |
1051 | | unsigned int k, |
1052 | | enum entity_charset charset, |
1053 | | const entity_stage1_row *table, |
1054 | | const unsigned char **entity, |
1055 | | size_t *entity_len, |
1056 | | const unsigned char *old, |
1057 | | size_t oldlen, |
1058 | | size_t *cursor) |
1059 | 143k | { |
1060 | 143k | unsigned stage1_idx = ENT_STAGE1_INDEX(k); |
1061 | 143k | const entity_stage3_row *c; |
1062 | | |
1063 | 143k | if (stage1_idx > 0x1D) { |
1064 | 328 | *entity = NULL; |
1065 | 328 | *entity_len = 0; |
1066 | 328 | return; |
1067 | 328 | } |
1068 | | |
1069 | 143k | c = &table[stage1_idx][ENT_STAGE2_INDEX(k)][ENT_STAGE3_INDEX(k)]; |
1070 | | |
1071 | 143k | if (!c->ambiguous) { |
1072 | 142k | *entity = (const unsigned char *)c->data.ent.entity; |
1073 | 142k | *entity_len = c->data.ent.entity_len; |
1074 | 142k | } else { |
1075 | | /* peek at next char */ |
1076 | 683 | size_t cursor_before = *cursor; |
1077 | 683 | zend_result status = SUCCESS; |
1078 | 683 | unsigned next_char; |
1079 | | |
1080 | 683 | if (!(*cursor < oldlen)) |
1081 | 12 | goto no_suitable_2nd; |
1082 | | |
1083 | 671 | next_char = get_next_char(charset, old, oldlen, cursor, &status); |
1084 | | |
1085 | 671 | if (status == FAILURE) |
1086 | 67 | goto no_suitable_2nd; |
1087 | | |
1088 | 604 | { |
1089 | 604 | const entity_multicodepoint_row *s, *e; |
1090 | | |
1091 | 604 | s = &c->data.multicodepoint_table[1]; |
1092 | 604 | e = s - 1 + c->data.multicodepoint_table[0].leading_entry.size; |
1093 | | /* we could do a binary search but it's not worth it since we have |
1094 | | * at most two entries... */ |
1095 | 1.20k | for ( ; s <= e; s++) { |
1096 | 604 | if (s->normal_entry.second_cp == next_char) { |
1097 | 0 | *entity = (const unsigned char *) s->normal_entry.entity; |
1098 | 0 | *entity_len = s->normal_entry.entity_len; |
1099 | 0 | return; |
1100 | 0 | } |
1101 | 604 | } |
1102 | 604 | } |
1103 | 683 | no_suitable_2nd: |
1104 | 683 | *cursor = cursor_before; |
1105 | 683 | *entity = (const unsigned char *) |
1106 | 683 | c->data.multicodepoint_table[0].leading_entry.default_entity; |
1107 | 683 | *entity_len = c->data.multicodepoint_table[0].leading_entry.default_entity_len; |
1108 | 683 | } |
1109 | 143k | } |
1110 | | /* }}} */ |
1111 | | |
1112 | | /* {{{ find_entity_for_char_basic */ |
1113 | | static inline void find_entity_for_char_basic( |
1114 | | unsigned int k, |
1115 | | const entity_stage3_row *table, |
1116 | | const unsigned char **entity, |
1117 | | size_t *entity_len) |
1118 | 482k | { |
1119 | 482k | if (k >= 64U) { |
1120 | 273k | *entity = NULL; |
1121 | 273k | *entity_len = 0; |
1122 | 273k | return; |
1123 | 273k | } |
1124 | | |
1125 | 209k | *entity = (const unsigned char *) table[k].data.ent.entity; |
1126 | 209k | *entity_len = table[k].data.ent.entity_len; |
1127 | 209k | } |
1128 | | /* }}} */ |
1129 | | |
1130 | | /* {{{ php_escape_html_entities */ |
1131 | | PHPAPI zend_string *php_escape_html_entities_ex(const unsigned char *old, size_t oldlen, int all, int flags, const char *hint_charset, bool double_encode, bool quiet) |
1132 | 1.47k | { |
1133 | 1.47k | size_t cursor, maxlen, len; |
1134 | 1.47k | zend_string *replaced; |
1135 | 1.47k | enum entity_charset charset = determine_charset(hint_charset, quiet); |
1136 | 1.47k | int doctype = flags & ENT_HTML_DOC_TYPE_MASK; |
1137 | 1.47k | entity_table_opt entity_table; |
1138 | 1.47k | const enc_to_uni *to_uni_table = NULL; |
1139 | 1.47k | const entity_ht *inv_map = NULL; /* used for !double_encode */ |
1140 | | /* only used if flags includes ENT_HTML_IGNORE_ERRORS or ENT_HTML_SUBSTITUTE_DISALLOWED_CHARS */ |
1141 | 1.47k | const unsigned char *replacement = NULL; |
1142 | 1.47k | size_t replacement_len = 0; |
1143 | | |
1144 | 1.47k | if (all) { /* replace with all named entities */ |
1145 | 836 | if (!quiet && CHARSET_PARTIAL_SUPPORT(charset)) { |
1146 | 175 | php_error_docref(NULL, E_NOTICE, "Only basic entities " |
1147 | 175 | "substitution is supported for multi-byte encodings other than UTF-8; " |
1148 | 175 | "functionality is equivalent to htmlspecialchars"); |
1149 | 175 | } |
1150 | 836 | LIMIT_ALL(all, doctype, charset); |
1151 | 836 | } |
1152 | 1.47k | entity_table = determine_entity_table(all, doctype); |
1153 | 1.47k | if (all && !CHARSET_UNICODE_COMPAT(charset)) { |
1154 | 106 | to_uni_table = enc_to_uni_index[charset]; |
1155 | 106 | } |
1156 | | |
1157 | 1.47k | if (!double_encode) { |
1158 | | /* first arg is 1 because we want to identify valid named entities |
1159 | | * even if we are only encoding the basic ones */ |
1160 | 27 | inv_map = unescape_inverse_map(1, flags); |
1161 | 27 | } |
1162 | | |
1163 | 1.47k | if (flags & (ENT_HTML_SUBSTITUTE_ERRORS | ENT_HTML_SUBSTITUTE_DISALLOWED_CHARS)) { |
1164 | 1.45k | if (charset == cs_utf_8) { |
1165 | 1.17k | replacement = (const unsigned char*)"\xEF\xBF\xBD"; |
1166 | 1.17k | replacement_len = sizeof("\xEF\xBF\xBD") - 1; |
1167 | 1.17k | } else { |
1168 | 278 | replacement = (const unsigned char*)"�"; |
1169 | 278 | replacement_len = sizeof("�") - 1; |
1170 | 278 | } |
1171 | 1.45k | } |
1172 | | |
1173 | | /* initial estimate */ |
1174 | 1.47k | if (oldlen < 64) { |
1175 | 98 | maxlen = 128; |
1176 | 1.37k | } else { |
1177 | 1.37k | maxlen = zend_safe_addmult(oldlen, 2, 0, "html_entities"); |
1178 | 1.37k | } |
1179 | | |
1180 | 1.47k | replaced = zend_string_alloc(maxlen, 0); |
1181 | 1.47k | len = 0; |
1182 | 1.47k | cursor = 0; |
1183 | 1.28M | while (cursor < oldlen) { |
1184 | 1.28M | const unsigned char *mbsequence = NULL; |
1185 | 1.28M | size_t mbseqlen = 0, |
1186 | 1.28M | cursor_before = cursor; |
1187 | 1.28M | zend_result status = SUCCESS; |
1188 | 1.28M | unsigned int this_char = get_next_char(charset, old, oldlen, &cursor, &status); |
1189 | | |
1190 | | /* guarantee we have at least 40 bytes to write. |
1191 | | * In HTML5, entities may take up to 33 bytes */ |
1192 | 1.28M | if (len > maxlen - 40) { /* maxlen can never be smaller than 128 */ |
1193 | 4.97k | replaced = zend_string_safe_realloc(replaced, maxlen, 1, 128, 0); |
1194 | 4.97k | maxlen += 128; |
1195 | 4.97k | } |
1196 | | |
1197 | 1.28M | if (status == FAILURE) { |
1198 | | /* invalid MB sequence */ |
1199 | 638k | if (flags & ENT_HTML_IGNORE_ERRORS) { |
1200 | 42.0k | continue; |
1201 | 596k | } else if (flags & ENT_HTML_SUBSTITUTE_ERRORS) { |
1202 | 596k | memcpy(&ZSTR_VAL(replaced)[len], replacement, replacement_len); |
1203 | 596k | len += replacement_len; |
1204 | 596k | continue; |
1205 | 596k | } else { |
1206 | 209 | zend_string_efree(replaced); |
1207 | 209 | return ZSTR_EMPTY_ALLOC(); |
1208 | 209 | } |
1209 | 643k | } else { /* SUCCESS */ |
1210 | 643k | mbsequence = &old[cursor_before]; |
1211 | 643k | mbseqlen = cursor - cursor_before; |
1212 | 643k | } |
1213 | | |
1214 | 643k | if (this_char != '&') { /* no entity on this position */ |
1215 | 628k | const unsigned char *rep = NULL; |
1216 | 628k | size_t rep_len = 0; |
1217 | | |
1218 | 628k | if (((this_char == '\'' && !(flags & ENT_HTML_QUOTE_SINGLE)) || |
1219 | 628k | (this_char == '"' && !(flags & ENT_HTML_QUOTE_DOUBLE)))) |
1220 | 1.86k | goto pass_char_through; |
1221 | | |
1222 | 626k | if (all) { /* false that CHARSET_PARTIAL_SUPPORT(charset) */ |
1223 | 143k | if (to_uni_table != NULL) { |
1224 | | /* !CHARSET_UNICODE_COMPAT therefore not UTF-8; since UTF-8 |
1225 | | * is the only multibyte encoding with !CHARSET_PARTIAL_SUPPORT, |
1226 | | * we're using a single byte encoding */ |
1227 | 45.8k | map_to_unicode(this_char, to_uni_table, &this_char); |
1228 | 45.8k | if (this_char == 0xFFFF) /* no mapping; pass through */ |
1229 | 0 | goto pass_char_through; |
1230 | 45.8k | } |
1231 | | /* the cursor may advance */ |
1232 | 143k | find_entity_for_char(this_char, charset, entity_table.ms_table, &rep, |
1233 | 143k | &rep_len, old, oldlen, &cursor); |
1234 | 482k | } else { |
1235 | 482k | find_entity_for_char_basic(this_char, entity_table.table, &rep, &rep_len); |
1236 | 482k | } |
1237 | | |
1238 | 626k | if (rep != NULL) { |
1239 | 26.1k | ZSTR_VAL(replaced)[len++] = '&'; |
1240 | 26.1k | memcpy(&ZSTR_VAL(replaced)[len], rep, rep_len); |
1241 | 26.1k | len += rep_len; |
1242 | 26.1k | ZSTR_VAL(replaced)[len++] = ';'; |
1243 | 600k | } else { |
1244 | | /* we did not find an entity for this char. |
1245 | | * check for its validity, if its valid pass it unchanged */ |
1246 | 600k | if (flags & ENT_HTML_SUBSTITUTE_DISALLOWED_CHARS) { |
1247 | 211k | if (CHARSET_UNICODE_COMPAT(charset)) { |
1248 | 108k | if (!unicode_cp_is_allowed(this_char, doctype)) { |
1249 | 35.1k | mbsequence = replacement; |
1250 | 35.1k | mbseqlen = replacement_len; |
1251 | 35.1k | } |
1252 | 108k | } else if (to_uni_table) { |
1253 | 35.1k | if (!all) /* otherwise we already did this */ |
1254 | 0 | map_to_unicode(this_char, to_uni_table, &this_char); |
1255 | 35.1k | if (!unicode_cp_is_allowed(this_char, doctype)) { |
1256 | 12.1k | mbsequence = replacement; |
1257 | 12.1k | mbseqlen = replacement_len; |
1258 | 12.1k | } |
1259 | 67.4k | } else { |
1260 | | /* not a unicode code point, unless, coincidentally, it's in |
1261 | | * the 0x20..0x7D range (except 0x5C in sjis). We know nothing |
1262 | | * about other code points, because we have no tables. Since |
1263 | | * Unicode code points in that range are not disallowed in any |
1264 | | * document type, we could do nothing. However, conversion |
1265 | | * tables frequently map 0x00-0x1F to the respective C0 code |
1266 | | * points. Let's play it safe and admit that's the case */ |
1267 | 67.4k | if (this_char <= 0x7D && |
1268 | 67.4k | !unicode_cp_is_allowed(this_char, doctype)) { |
1269 | 32.3k | mbsequence = replacement; |
1270 | 32.3k | mbseqlen = replacement_len; |
1271 | 32.3k | } |
1272 | 67.4k | } |
1273 | 211k | } |
1274 | 602k | pass_char_through: |
1275 | 602k | if (mbseqlen > 1) { |
1276 | 163k | memcpy(ZSTR_VAL(replaced) + len, mbsequence, mbseqlen); |
1277 | 163k | len += mbseqlen; |
1278 | 438k | } else { |
1279 | 438k | ZSTR_VAL(replaced)[len++] = mbsequence[0]; |
1280 | 438k | } |
1281 | 602k | } |
1282 | 626k | } else { /* this_char == '&' */ |
1283 | 14.5k | if (double_encode) { |
1284 | 14.4k | encode_amp: |
1285 | 14.4k | memcpy(&ZSTR_VAL(replaced)[len], "&", sizeof("&") - 1); |
1286 | 14.4k | len += sizeof("&") - 1; |
1287 | 14.4k | } else { /* no double encode */ |
1288 | | /* check if entity is valid */ |
1289 | 2.53k | size_t ent_len; /* not counting & or ; */ |
1290 | | /* peek at next char */ |
1291 | 2.53k | if (old[cursor] == '#') { /* numeric entity */ |
1292 | 11 | unsigned code_point; |
1293 | 11 | int valid; |
1294 | 11 | char *pos = (char*)&old[cursor+1]; |
1295 | 11 | valid = process_numeric_entity((const char **)&pos, &code_point); |
1296 | 11 | if (valid == FAILURE) |
1297 | 10 | goto encode_amp; |
1298 | 1 | if (flags & ENT_HTML_SUBSTITUTE_DISALLOWED_CHARS) { |
1299 | 0 | if (!numeric_entity_is_allowed(code_point, doctype)) |
1300 | 0 | goto encode_amp; |
1301 | 0 | } |
1302 | 1 | ent_len = pos - (char*)&old[cursor]; |
1303 | 2.52k | } else { /* named entity */ |
1304 | | /* check for vality of named entity */ |
1305 | 2.52k | const char *start = (const char *) &old[cursor], |
1306 | 2.52k | *next = start; |
1307 | 2.52k | unsigned dummy1, dummy2; |
1308 | | |
1309 | 2.52k | if (process_named_entity_html(&next, &start, &ent_len) == FAILURE) |
1310 | 2.43k | goto encode_amp; |
1311 | 91 | if (resolve_named_entity_html(start, ent_len, inv_map, &dummy1, &dummy2) == FAILURE) { |
1312 | 74 | if (!(doctype == ENT_HTML_DOC_XHTML && ent_len == 4 && start[0] == 'a' |
1313 | 74 | && start[1] == 'p' && start[2] == 'o' && start[3] == 's')) { |
1314 | | /* uses html4 inv_map, which doesn't include apos;. This is a |
1315 | | * hack to support it */ |
1316 | 74 | goto encode_amp; |
1317 | 74 | } |
1318 | 74 | } |
1319 | 91 | } |
1320 | | /* checks passed; copy entity to result */ |
1321 | | /* entity size is unbounded, we may need more memory */ |
1322 | | /* at this point maxlen - len >= 40 */ |
1323 | 18 | if (maxlen - len < ent_len + 2 /* & and ; */) { |
1324 | | /* ent_len < oldlen, which is certainly <= SIZE_MAX/2 */ |
1325 | 0 | replaced = zend_string_safe_realloc(replaced, maxlen, 1, ent_len + 128, 0); |
1326 | 0 | maxlen += ent_len + 128; |
1327 | 0 | } |
1328 | 18 | ZSTR_VAL(replaced)[len++] = '&'; |
1329 | 18 | memcpy(&ZSTR_VAL(replaced)[len], &old[cursor], ent_len); |
1330 | 18 | len += ent_len; |
1331 | 18 | ZSTR_VAL(replaced)[len++] = ';'; |
1332 | 18 | cursor += ent_len + 1; |
1333 | 18 | } |
1334 | 14.5k | } |
1335 | 643k | } |
1336 | 1.26k | ZSTR_VAL(replaced)[len] = '\0'; |
1337 | 1.26k | ZSTR_LEN(replaced) = len; |
1338 | | |
1339 | 1.26k | return replaced; |
1340 | 1.47k | } |
1341 | | /* }}} */ |
1342 | | |
1343 | | /* {{{ php_html_entities */ |
1344 | | static void php_html_entities(INTERNAL_FUNCTION_PARAMETERS, int all) |
1345 | 1.47k | { |
1346 | 1.47k | zend_string *str, *hint_charset = NULL; |
1347 | 1.47k | zend_long flags = ENT_QUOTES|ENT_SUBSTITUTE; |
1348 | 1.47k | zend_string *replaced; |
1349 | 1.47k | bool double_encode = 1; |
1350 | | |
1351 | 4.41k | ZEND_PARSE_PARAMETERS_START(1, 4) |
1352 | 5.88k | Z_PARAM_STR(str) |
1353 | 1.47k | Z_PARAM_OPTIONAL |
1354 | 4.71k | Z_PARAM_LONG(flags) |
1355 | 4.05k | Z_PARAM_STR_OR_NULL(hint_charset) |
1356 | 2.23k | Z_PARAM_BOOL(double_encode); |
1357 | 1.47k | ZEND_PARSE_PARAMETERS_END(); |
1358 | | |
1359 | 1.47k | replaced = php_escape_html_entities_ex( |
1360 | 1.47k | (unsigned char*)ZSTR_VAL(str), ZSTR_LEN(str), all, (int) flags, |
1361 | 1.47k | hint_charset ? ZSTR_VAL(hint_charset) : NULL, double_encode, /* quiet */ 0); |
1362 | 1.47k | RETVAL_STR(replaced); |
1363 | 1.47k | } |
1364 | | /* }}} */ |
1365 | | |
1366 | | /* {{{ Convert special characters to HTML entities */ |
1367 | | PHP_FUNCTION(htmlspecialchars) |
1368 | 635 | { |
1369 | 635 | php_html_entities(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0); |
1370 | 635 | } |
1371 | | /* }}} */ |
1372 | | |
1373 | | /* {{{ Convert special HTML entities back to characters */ |
1374 | | PHP_FUNCTION(htmlspecialchars_decode) |
1375 | 672 | { |
1376 | 672 | zend_string *str; |
1377 | 672 | zend_long quote_style = ENT_QUOTES|ENT_SUBSTITUTE; |
1378 | 672 | zend_string *replaced; |
1379 | | |
1380 | 2.01k | ZEND_PARSE_PARAMETERS_START(1, 2) |
1381 | 2.68k | Z_PARAM_STR(str) |
1382 | 672 | Z_PARAM_OPTIONAL |
1383 | 1.34k | Z_PARAM_LONG(quote_style) |
1384 | 672 | ZEND_PARSE_PARAMETERS_END(); |
1385 | | |
1386 | 672 | replaced = php_unescape_html_entities(str, 0 /*!all*/, (int)quote_style, NULL); |
1387 | 672 | RETURN_STR(replaced); |
1388 | 672 | } |
1389 | | /* }}} */ |
1390 | | |
1391 | | /* {{{ Convert all HTML entities to their applicable characters */ |
1392 | | PHP_FUNCTION(html_entity_decode) |
1393 | 0 | { |
1394 | 0 | zend_string *str, *hint_charset = NULL; |
1395 | 0 | zend_long quote_style = ENT_QUOTES|ENT_SUBSTITUTE; |
1396 | 0 | zend_string *replaced; |
1397 | |
|
1398 | 0 | ZEND_PARSE_PARAMETERS_START(1, 3) |
1399 | 0 | Z_PARAM_STR(str) |
1400 | 0 | Z_PARAM_OPTIONAL |
1401 | 0 | Z_PARAM_LONG(quote_style) |
1402 | 0 | Z_PARAM_STR_OR_NULL(hint_charset) |
1403 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1404 | | |
1405 | 0 | replaced = php_unescape_html_entities( |
1406 | 0 | str, 1 /*all*/, (int)quote_style, hint_charset ? ZSTR_VAL(hint_charset) : NULL); |
1407 | 0 | RETURN_STR(replaced); |
1408 | 0 | } |
1409 | | /* }}} */ |
1410 | | |
1411 | | |
1412 | | /* {{{ Convert all applicable characters to HTML entities */ |
1413 | | PHP_FUNCTION(htmlentities) |
1414 | 836 | { |
1415 | 836 | php_html_entities(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1); |
1416 | 836 | } |
1417 | | /* }}} */ |
1418 | | |
1419 | | /* {{{ write_s3row_data */ |
1420 | | static inline void write_s3row_data( |
1421 | | const entity_stage3_row *r, |
1422 | | unsigned orig_cp, |
1423 | | enum entity_charset charset, |
1424 | | zval *arr) |
1425 | 0 | { |
1426 | 0 | char key[9] = ""; /* two unicode code points in UTF-8 */ |
1427 | 0 | char entity[LONGEST_ENTITY_LENGTH + 2] = {'&'}; |
1428 | 0 | size_t written_k1; |
1429 | |
|
1430 | 0 | written_k1 = write_octet_sequence((unsigned char*)key, charset, orig_cp); |
1431 | |
|
1432 | 0 | if (!r->ambiguous) { |
1433 | 0 | size_t l = r->data.ent.entity_len; |
1434 | 0 | memcpy(&entity[1], r->data.ent.entity, l); |
1435 | 0 | entity[l + 1] = ';'; |
1436 | 0 | add_assoc_stringl_ex(arr, key, written_k1, entity, l + 2); |
1437 | 0 | } else { |
1438 | 0 | unsigned i, |
1439 | 0 | num_entries; |
1440 | 0 | const entity_multicodepoint_row *mcpr = r->data.multicodepoint_table; |
1441 | |
|
1442 | 0 | if (mcpr[0].leading_entry.default_entity != NULL) { |
1443 | 0 | size_t l = mcpr[0].leading_entry.default_entity_len; |
1444 | 0 | memcpy(&entity[1], mcpr[0].leading_entry.default_entity, l); |
1445 | 0 | entity[l + 1] = ';'; |
1446 | 0 | add_assoc_stringl_ex(arr, key, written_k1, entity, l + 2); |
1447 | 0 | } |
1448 | 0 | num_entries = mcpr[0].leading_entry.size; |
1449 | 0 | for (i = 1; i <= num_entries; i++) { |
1450 | 0 | size_t l, |
1451 | 0 | written_k2; |
1452 | 0 | unsigned uni_cp, |
1453 | 0 | spe_cp; |
1454 | |
|
1455 | 0 | uni_cp = mcpr[i].normal_entry.second_cp; |
1456 | 0 | l = mcpr[i].normal_entry.entity_len; |
1457 | |
|
1458 | 0 | if (!CHARSET_UNICODE_COMPAT(charset)) { |
1459 | 0 | if (map_from_unicode(uni_cp, charset, &spe_cp) == FAILURE) |
1460 | 0 | continue; /* non representable in this charset */ |
1461 | 0 | } else { |
1462 | 0 | spe_cp = uni_cp; |
1463 | 0 | } |
1464 | | |
1465 | 0 | written_k2 = write_octet_sequence((unsigned char*)&key[written_k1], charset, spe_cp); |
1466 | 0 | memcpy(&entity[1], mcpr[i].normal_entry.entity, l); |
1467 | 0 | entity[l + 1] = ';'; |
1468 | 0 | add_assoc_stringl_ex(arr, key, written_k1 + written_k2, entity, l + 2); |
1469 | 0 | } |
1470 | 0 | } |
1471 | 0 | } |
1472 | | /* }}} */ |
1473 | | |
1474 | | /* {{{ Returns the internal translation table used by htmlspecialchars and htmlentities */ |
1475 | | PHP_FUNCTION(get_html_translation_table) |
1476 | 0 | { |
1477 | 0 | zend_long all = PHP_HTML_SPECIALCHARS, |
1478 | 0 | flags = ENT_QUOTES|ENT_SUBSTITUTE; |
1479 | 0 | int doctype; |
1480 | 0 | entity_table_opt entity_table; |
1481 | 0 | const enc_to_uni *to_uni_table = NULL; |
1482 | 0 | char *charset_hint = NULL; |
1483 | 0 | size_t charset_hint_len; |
1484 | 0 | enum entity_charset charset; |
1485 | | |
1486 | | /* in this function we have to jump through some loops because we're |
1487 | | * getting the translated table from data structures that are optimized for |
1488 | | * random access, not traversal */ |
1489 | |
|
1490 | 0 | ZEND_PARSE_PARAMETERS_START(0, 3) |
1491 | 0 | Z_PARAM_OPTIONAL |
1492 | 0 | Z_PARAM_LONG(all) |
1493 | 0 | Z_PARAM_LONG(flags) |
1494 | 0 | Z_PARAM_STRING(charset_hint, charset_hint_len) |
1495 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1496 | | |
1497 | 0 | charset = determine_charset(charset_hint, /* quiet */ 0); |
1498 | 0 | doctype = flags & ENT_HTML_DOC_TYPE_MASK; |
1499 | 0 | LIMIT_ALL(all, doctype, charset); |
1500 | |
|
1501 | 0 | array_init(return_value); |
1502 | |
|
1503 | 0 | entity_table = determine_entity_table((int)all, doctype); |
1504 | 0 | if (all && !CHARSET_UNICODE_COMPAT(charset)) { |
1505 | 0 | to_uni_table = enc_to_uni_index[charset]; |
1506 | 0 | } |
1507 | |
|
1508 | 0 | if (all) { /* PHP_HTML_ENTITIES (actually, any non-zero value for 1st param) */ |
1509 | 0 | const entity_stage1_row *ms_table = entity_table.ms_table; |
1510 | |
|
1511 | 0 | if (CHARSET_UNICODE_COMPAT(charset)) { |
1512 | 0 | unsigned i, j, k, |
1513 | 0 | max_i, max_j, max_k; |
1514 | | /* no mapping to unicode required */ |
1515 | 0 | if (CHARSET_SINGLE_BYTE(charset)) { /* ISO-8859-1 */ |
1516 | 0 | max_i = 1; max_j = 4; max_k = 64; |
1517 | 0 | } else { |
1518 | 0 | max_i = 0x1E; max_j = 64; max_k = 64; |
1519 | 0 | } |
1520 | |
|
1521 | 0 | for (i = 0; i < max_i; i++) { |
1522 | 0 | if (ms_table[i] == empty_stage2_table) |
1523 | 0 | continue; |
1524 | 0 | for (j = 0; j < max_j; j++) { |
1525 | 0 | if (ms_table[i][j] == empty_stage3_table) |
1526 | 0 | continue; |
1527 | 0 | for (k = 0; k < max_k; k++) { |
1528 | 0 | const entity_stage3_row *r = &ms_table[i][j][k]; |
1529 | 0 | unsigned code; |
1530 | |
|
1531 | 0 | if (r->data.ent.entity == NULL) |
1532 | 0 | continue; |
1533 | | |
1534 | 0 | code = ENT_CODE_POINT_FROM_STAGES(i, j, k); |
1535 | 0 | if (((code == '\'' && !(flags & ENT_HTML_QUOTE_SINGLE)) || |
1536 | 0 | (code == '"' && !(flags & ENT_HTML_QUOTE_DOUBLE)))) |
1537 | 0 | continue; |
1538 | 0 | write_s3row_data(r, code, charset, return_value); |
1539 | 0 | } |
1540 | 0 | } |
1541 | 0 | } |
1542 | 0 | } else { |
1543 | | /* we have to iterate through the set of code points for this |
1544 | | * encoding and map them to unicode code points */ |
1545 | 0 | unsigned i; |
1546 | 0 | for (i = 0; i <= 0xFF; i++) { |
1547 | 0 | const entity_stage3_row *r; |
1548 | 0 | unsigned uni_cp; |
1549 | | |
1550 | | /* can be done before mapping, they're invariant */ |
1551 | 0 | if (((i == '\'' && !(flags & ENT_HTML_QUOTE_SINGLE)) || |
1552 | 0 | (i == '"' && !(flags & ENT_HTML_QUOTE_DOUBLE)))) |
1553 | 0 | continue; |
1554 | | |
1555 | 0 | map_to_unicode(i, to_uni_table, &uni_cp); |
1556 | 0 | r = &ms_table[ENT_STAGE1_INDEX(uni_cp)][ENT_STAGE2_INDEX(uni_cp)][ENT_STAGE3_INDEX(uni_cp)]; |
1557 | 0 | if (r->data.ent.entity == NULL) |
1558 | 0 | continue; |
1559 | | |
1560 | 0 | write_s3row_data(r, i, charset, return_value); |
1561 | 0 | } |
1562 | 0 | } |
1563 | 0 | } else { |
1564 | | /* we could use sizeof(stage3_table_be_apos_00000) as well */ |
1565 | 0 | unsigned j, |
1566 | 0 | numelems = sizeof(stage3_table_be_noapos_00000) / |
1567 | 0 | sizeof(*stage3_table_be_noapos_00000); |
1568 | |
|
1569 | 0 | for (j = 0; j < numelems; j++) { |
1570 | 0 | const entity_stage3_row *r = &entity_table.table[j]; |
1571 | 0 | if (r->data.ent.entity == NULL) |
1572 | 0 | continue; |
1573 | | |
1574 | 0 | if (((j == '\'' && !(flags & ENT_HTML_QUOTE_SINGLE)) || |
1575 | 0 | (j == '"' && !(flags & ENT_HTML_QUOTE_DOUBLE)))) |
1576 | 0 | continue; |
1577 | | |
1578 | | /* charset is indifferent, used cs_8859_1 for efficiency */ |
1579 | 0 | write_s3row_data(r, j, cs_8859_1, return_value); |
1580 | 0 | } |
1581 | 0 | } |
1582 | 0 | } |
1583 | | /* }}} */ |