html.c 48 KB

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