PageRenderTime 135ms CodeModel.GetById 37ms RepoModel.GetById 5ms app.codeStats 1ms

/ext/standard/string.c

http://github.com/php/php-src
C | 6164 lines | 4888 code | 692 blank | 584 comment | 1224 complexity | 55ac1c61da69a0feb623e86f03d5457b MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. | http://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. | Stig Sæther Bakken <ssb@php.net> |
  15. | Zeev Suraski <zeev@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdio.h>
  19. #include "php.h"
  20. #include "php_rand.h"
  21. #include "php_string.h"
  22. #include "php_variables.h"
  23. #include <locale.h>
  24. #ifdef HAVE_LANGINFO_H
  25. # include <langinfo.h>
  26. #endif
  27. #ifdef HAVE_MONETARY_H
  28. # include <monetary.h>
  29. #endif
  30. /*
  31. * This define is here because some versions of libintl redefine setlocale
  32. * to point to libintl_setlocale. That's a ridiculous thing to do as far
  33. * as I am concerned, but with this define and the subsequent undef we
  34. * limit the damage to just the actual setlocale() call in this file
  35. * without turning zif_setlocale into zif_libintl_setlocale. -Rasmus
  36. */
  37. #define php_my_setlocale setlocale
  38. #ifdef HAVE_LIBINTL
  39. # include <libintl.h> /* For LC_MESSAGES */
  40. #ifdef setlocale
  41. # undef setlocale
  42. #endif
  43. #endif
  44. #include "scanf.h"
  45. #include "zend_API.h"
  46. #include "zend_execute.h"
  47. #include "php_globals.h"
  48. #include "basic_functions.h"
  49. #include "zend_smart_str.h"
  50. #include <Zend/zend_exceptions.h>
  51. #ifdef ZTS
  52. #include "TSRM.h"
  53. #endif
  54. /* For str_getcsv() support */
  55. #include "ext/standard/file.h"
  56. /* For php_next_utf8_char() */
  57. #include "ext/standard/html.h"
  58. #define STR_PAD_LEFT 0
  59. #define STR_PAD_RIGHT 1
  60. #define STR_PAD_BOTH 2
  61. #define PHP_PATHINFO_DIRNAME 1
  62. #define PHP_PATHINFO_BASENAME 2
  63. #define PHP_PATHINFO_EXTENSION 4
  64. #define PHP_PATHINFO_FILENAME 8
  65. #define PHP_PATHINFO_ALL (PHP_PATHINFO_DIRNAME | PHP_PATHINFO_BASENAME | PHP_PATHINFO_EXTENSION | PHP_PATHINFO_FILENAME)
  66. #define STR_STRSPN 0
  67. #define STR_STRCSPN 1
  68. /* {{{ register_string_constants
  69. */
  70. void register_string_constants(INIT_FUNC_ARGS)
  71. {
  72. REGISTER_LONG_CONSTANT("STR_PAD_LEFT", STR_PAD_LEFT, CONST_CS | CONST_PERSISTENT);
  73. REGISTER_LONG_CONSTANT("STR_PAD_RIGHT", STR_PAD_RIGHT, CONST_CS | CONST_PERSISTENT);
  74. REGISTER_LONG_CONSTANT("STR_PAD_BOTH", STR_PAD_BOTH, CONST_CS | CONST_PERSISTENT);
  75. REGISTER_LONG_CONSTANT("PATHINFO_DIRNAME", PHP_PATHINFO_DIRNAME, CONST_CS | CONST_PERSISTENT);
  76. REGISTER_LONG_CONSTANT("PATHINFO_BASENAME", PHP_PATHINFO_BASENAME, CONST_CS | CONST_PERSISTENT);
  77. REGISTER_LONG_CONSTANT("PATHINFO_EXTENSION", PHP_PATHINFO_EXTENSION, CONST_CS | CONST_PERSISTENT);
  78. REGISTER_LONG_CONSTANT("PATHINFO_FILENAME", PHP_PATHINFO_FILENAME, CONST_CS | CONST_PERSISTENT);
  79. /* If last members of struct lconv equal CHAR_MAX, no grouping is done */
  80. REGISTER_LONG_CONSTANT("CHAR_MAX", CHAR_MAX, CONST_CS | CONST_PERSISTENT);
  81. REGISTER_LONG_CONSTANT("LC_CTYPE", LC_CTYPE, CONST_CS | CONST_PERSISTENT);
  82. REGISTER_LONG_CONSTANT("LC_NUMERIC", LC_NUMERIC, CONST_CS | CONST_PERSISTENT);
  83. REGISTER_LONG_CONSTANT("LC_TIME", LC_TIME, CONST_CS | CONST_PERSISTENT);
  84. REGISTER_LONG_CONSTANT("LC_COLLATE", LC_COLLATE, CONST_CS | CONST_PERSISTENT);
  85. REGISTER_LONG_CONSTANT("LC_MONETARY", LC_MONETARY, CONST_CS | CONST_PERSISTENT);
  86. REGISTER_LONG_CONSTANT("LC_ALL", LC_ALL, CONST_CS | CONST_PERSISTENT);
  87. # ifdef LC_MESSAGES
  88. REGISTER_LONG_CONSTANT("LC_MESSAGES", LC_MESSAGES, CONST_CS | CONST_PERSISTENT);
  89. # endif
  90. }
  91. /* }}} */
  92. int php_tag_find(char *tag, size_t len, const char *set);
  93. /* this is read-only, so it's ok */
  94. ZEND_SET_ALIGNED(16, static char hexconvtab[]) = "0123456789abcdef";
  95. /* localeconv mutex */
  96. #ifdef ZTS
  97. static MUTEX_T locale_mutex = NULL;
  98. #endif
  99. /* {{{ php_bin2hex
  100. */
  101. static zend_string *php_bin2hex(const unsigned char *old, const size_t oldlen)
  102. {
  103. zend_string *result;
  104. size_t i, j;
  105. result = zend_string_safe_alloc(oldlen, 2 * sizeof(char), 0, 0);
  106. for (i = j = 0; i < oldlen; i++) {
  107. ZSTR_VAL(result)[j++] = hexconvtab[old[i] >> 4];
  108. ZSTR_VAL(result)[j++] = hexconvtab[old[i] & 15];
  109. }
  110. ZSTR_VAL(result)[j] = '\0';
  111. return result;
  112. }
  113. /* }}} */
  114. /* {{{ php_hex2bin
  115. */
  116. static zend_string *php_hex2bin(const unsigned char *old, const size_t oldlen)
  117. {
  118. size_t target_length = oldlen >> 1;
  119. zend_string *str = zend_string_alloc(target_length, 0);
  120. unsigned char *ret = (unsigned char *)ZSTR_VAL(str);
  121. size_t i, j;
  122. for (i = j = 0; i < target_length; i++) {
  123. unsigned char c = old[j++];
  124. unsigned char l = c & ~0x20;
  125. int is_letter = ((unsigned int) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(unsigned int) - 1);
  126. unsigned char d;
  127. /* basically (c >= '0' && c <= '9') || (l >= 'A' && l <= 'F') */
  128. if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(unsigned int) - 1)) | is_letter)) {
  129. d = (l - 0x10 - 0x27 * is_letter) << 4;
  130. } else {
  131. zend_string_efree(str);
  132. return NULL;
  133. }
  134. c = old[j++];
  135. l = c & ~0x20;
  136. is_letter = ((unsigned int) ((l - 'A') ^ (l - 'F' - 1))) >> (8 * sizeof(unsigned int) - 1);
  137. if (EXPECTED((((c ^ '0') - 10) >> (8 * sizeof(unsigned int) - 1)) | is_letter)) {
  138. d |= l - 0x10 - 0x27 * is_letter;
  139. } else {
  140. zend_string_efree(str);
  141. return NULL;
  142. }
  143. ret[i] = d;
  144. }
  145. ret[i] = '\0';
  146. return str;
  147. }
  148. /* }}} */
  149. /* {{{ localeconv_r
  150. * glibc's localeconv is not reentrant, so lets make it so ... sorta */
  151. PHPAPI struct lconv *localeconv_r(struct lconv *out)
  152. {
  153. #ifdef ZTS
  154. tsrm_mutex_lock( locale_mutex );
  155. #endif
  156. /* cur->locinfo is struct __crt_locale_info which implementation is
  157. hidden in vc14. TODO revisit this and check if a workaround available
  158. and needed. */
  159. #if defined(PHP_WIN32) && _MSC_VER < 1900 && defined(ZTS)
  160. {
  161. /* Even with the enabled per thread locale, localeconv
  162. won't check any locale change in the master thread. */
  163. _locale_t cur = _get_current_locale();
  164. *out = *cur->locinfo->lconv;
  165. _free_locale(cur);
  166. }
  167. #else
  168. /* localeconv doesn't return an error condition */
  169. *out = *localeconv();
  170. #endif
  171. #ifdef ZTS
  172. tsrm_mutex_unlock( locale_mutex );
  173. #endif
  174. return out;
  175. }
  176. /* }}} */
  177. #ifdef ZTS
  178. /* {{{ PHP_MINIT_FUNCTION
  179. */
  180. PHP_MINIT_FUNCTION(localeconv)
  181. {
  182. locale_mutex = tsrm_mutex_alloc();
  183. return SUCCESS;
  184. }
  185. /* }}} */
  186. /* {{{ PHP_MSHUTDOWN_FUNCTION
  187. */
  188. PHP_MSHUTDOWN_FUNCTION(localeconv)
  189. {
  190. tsrm_mutex_free( locale_mutex );
  191. locale_mutex = NULL;
  192. return SUCCESS;
  193. }
  194. /* }}} */
  195. #endif
  196. /* {{{ proto string bin2hex(string data)
  197. Converts the binary representation of data to hex */
  198. PHP_FUNCTION(bin2hex)
  199. {
  200. zend_string *result;
  201. zend_string *data;
  202. ZEND_PARSE_PARAMETERS_START(1, 1)
  203. Z_PARAM_STR(data)
  204. ZEND_PARSE_PARAMETERS_END();
  205. result = php_bin2hex((unsigned char *)ZSTR_VAL(data), ZSTR_LEN(data));
  206. RETURN_STR(result);
  207. }
  208. /* }}} */
  209. /* {{{ proto string|false hex2bin(string data)
  210. Converts the hex representation of data to binary */
  211. PHP_FUNCTION(hex2bin)
  212. {
  213. zend_string *result, *data;
  214. ZEND_PARSE_PARAMETERS_START(1, 1)
  215. Z_PARAM_STR(data)
  216. ZEND_PARSE_PARAMETERS_END();
  217. if (ZSTR_LEN(data) % 2 != 0) {
  218. php_error_docref(NULL, E_WARNING, "Hexadecimal input string must have an even length");
  219. RETURN_FALSE;
  220. }
  221. result = php_hex2bin((unsigned char *)ZSTR_VAL(data), ZSTR_LEN(data));
  222. if (!result) {
  223. php_error_docref(NULL, E_WARNING, "Input string must be hexadecimal string");
  224. RETURN_FALSE;
  225. }
  226. RETVAL_STR(result);
  227. }
  228. /* }}} */
  229. static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */
  230. {
  231. zend_string *s11, *s22;
  232. zend_long start = 0, len = 0;
  233. ZEND_PARSE_PARAMETERS_START(2, 4)
  234. Z_PARAM_STR(s11)
  235. Z_PARAM_STR(s22)
  236. Z_PARAM_OPTIONAL
  237. Z_PARAM_LONG(start)
  238. Z_PARAM_LONG(len)
  239. ZEND_PARSE_PARAMETERS_END();
  240. if (ZEND_NUM_ARGS() < 4) {
  241. len = ZSTR_LEN(s11);
  242. }
  243. /* look at substr() function for more information */
  244. if (start < 0) {
  245. start += (zend_long)ZSTR_LEN(s11);
  246. if (start < 0) {
  247. start = 0;
  248. }
  249. } else if ((size_t)start > ZSTR_LEN(s11)) {
  250. RETURN_FALSE;
  251. }
  252. if (len < 0) {
  253. len += (ZSTR_LEN(s11) - start);
  254. if (len < 0) {
  255. len = 0;
  256. }
  257. }
  258. if (len > (zend_long)ZSTR_LEN(s11) - start) {
  259. len = ZSTR_LEN(s11) - start;
  260. }
  261. if(len == 0) {
  262. RETURN_LONG(0);
  263. }
  264. if (behavior == STR_STRSPN) {
  265. RETURN_LONG(php_strspn(ZSTR_VAL(s11) + start /*str1_start*/,
  266. ZSTR_VAL(s22) /*str2_start*/,
  267. ZSTR_VAL(s11) + start + len /*str1_end*/,
  268. ZSTR_VAL(s22) + ZSTR_LEN(s22) /*str2_end*/));
  269. } else if (behavior == STR_STRCSPN) {
  270. RETURN_LONG(php_strcspn(ZSTR_VAL(s11) + start /*str1_start*/,
  271. ZSTR_VAL(s22) /*str2_start*/,
  272. ZSTR_VAL(s11) + start + len /*str1_end*/,
  273. ZSTR_VAL(s22) + ZSTR_LEN(s22) /*str2_end*/));
  274. }
  275. }
  276. /* }}} */
  277. /* {{{ proto int|false strspn(string str, string mask [, int start [, int len]])
  278. Finds length of initial segment consisting entirely of characters found in mask. If start or/and length is provided works like strspn(substr($s,$start,$len),$good_chars) */
  279. PHP_FUNCTION(strspn)
  280. {
  281. php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, STR_STRSPN);
  282. }
  283. /* }}} */
  284. /* {{{ proto int|false strcspn(string str, string mask [, int start [, int len]])
  285. Finds length of initial segment consisting entirely of characters not found in mask. If start or/and length is provide works like strcspn(substr($s,$start,$len),$bad_chars) */
  286. PHP_FUNCTION(strcspn)
  287. {
  288. php_spn_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, STR_STRCSPN);
  289. }
  290. /* }}} */
  291. /* {{{ PHP_MINIT_FUNCTION(nl_langinfo) */
  292. #if HAVE_NL_LANGINFO
  293. PHP_MINIT_FUNCTION(nl_langinfo)
  294. {
  295. #define REGISTER_NL_LANGINFO_CONSTANT(x) REGISTER_LONG_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
  296. #ifdef ABDAY_1
  297. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_1);
  298. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_2);
  299. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_3);
  300. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_4);
  301. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_5);
  302. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_6);
  303. REGISTER_NL_LANGINFO_CONSTANT(ABDAY_7);
  304. #endif
  305. #ifdef DAY_1
  306. REGISTER_NL_LANGINFO_CONSTANT(DAY_1);
  307. REGISTER_NL_LANGINFO_CONSTANT(DAY_2);
  308. REGISTER_NL_LANGINFO_CONSTANT(DAY_3);
  309. REGISTER_NL_LANGINFO_CONSTANT(DAY_4);
  310. REGISTER_NL_LANGINFO_CONSTANT(DAY_5);
  311. REGISTER_NL_LANGINFO_CONSTANT(DAY_6);
  312. REGISTER_NL_LANGINFO_CONSTANT(DAY_7);
  313. #endif
  314. #ifdef ABMON_1
  315. REGISTER_NL_LANGINFO_CONSTANT(ABMON_1);
  316. REGISTER_NL_LANGINFO_CONSTANT(ABMON_2);
  317. REGISTER_NL_LANGINFO_CONSTANT(ABMON_3);
  318. REGISTER_NL_LANGINFO_CONSTANT(ABMON_4);
  319. REGISTER_NL_LANGINFO_CONSTANT(ABMON_5);
  320. REGISTER_NL_LANGINFO_CONSTANT(ABMON_6);
  321. REGISTER_NL_LANGINFO_CONSTANT(ABMON_7);
  322. REGISTER_NL_LANGINFO_CONSTANT(ABMON_8);
  323. REGISTER_NL_LANGINFO_CONSTANT(ABMON_9);
  324. REGISTER_NL_LANGINFO_CONSTANT(ABMON_10);
  325. REGISTER_NL_LANGINFO_CONSTANT(ABMON_11);
  326. REGISTER_NL_LANGINFO_CONSTANT(ABMON_12);
  327. #endif
  328. #ifdef MON_1
  329. REGISTER_NL_LANGINFO_CONSTANT(MON_1);
  330. REGISTER_NL_LANGINFO_CONSTANT(MON_2);
  331. REGISTER_NL_LANGINFO_CONSTANT(MON_3);
  332. REGISTER_NL_LANGINFO_CONSTANT(MON_4);
  333. REGISTER_NL_LANGINFO_CONSTANT(MON_5);
  334. REGISTER_NL_LANGINFO_CONSTANT(MON_6);
  335. REGISTER_NL_LANGINFO_CONSTANT(MON_7);
  336. REGISTER_NL_LANGINFO_CONSTANT(MON_8);
  337. REGISTER_NL_LANGINFO_CONSTANT(MON_9);
  338. REGISTER_NL_LANGINFO_CONSTANT(MON_10);
  339. REGISTER_NL_LANGINFO_CONSTANT(MON_11);
  340. REGISTER_NL_LANGINFO_CONSTANT(MON_12);
  341. #endif
  342. #ifdef AM_STR
  343. REGISTER_NL_LANGINFO_CONSTANT(AM_STR);
  344. #endif
  345. #ifdef PM_STR
  346. REGISTER_NL_LANGINFO_CONSTANT(PM_STR);
  347. #endif
  348. #ifdef D_T_FMT
  349. REGISTER_NL_LANGINFO_CONSTANT(D_T_FMT);
  350. #endif
  351. #ifdef D_FMT
  352. REGISTER_NL_LANGINFO_CONSTANT(D_FMT);
  353. #endif
  354. #ifdef T_FMT
  355. REGISTER_NL_LANGINFO_CONSTANT(T_FMT);
  356. #endif
  357. #ifdef T_FMT_AMPM
  358. REGISTER_NL_LANGINFO_CONSTANT(T_FMT_AMPM);
  359. #endif
  360. #ifdef ERA
  361. REGISTER_NL_LANGINFO_CONSTANT(ERA);
  362. #endif
  363. #ifdef ERA_YEAR
  364. REGISTER_NL_LANGINFO_CONSTANT(ERA_YEAR);
  365. #endif
  366. #ifdef ERA_D_T_FMT
  367. REGISTER_NL_LANGINFO_CONSTANT(ERA_D_T_FMT);
  368. #endif
  369. #ifdef ERA_D_FMT
  370. REGISTER_NL_LANGINFO_CONSTANT(ERA_D_FMT);
  371. #endif
  372. #ifdef ERA_T_FMT
  373. REGISTER_NL_LANGINFO_CONSTANT(ERA_T_FMT);
  374. #endif
  375. #ifdef ALT_DIGITS
  376. REGISTER_NL_LANGINFO_CONSTANT(ALT_DIGITS);
  377. #endif
  378. #ifdef INT_CURR_SYMBOL
  379. REGISTER_NL_LANGINFO_CONSTANT(INT_CURR_SYMBOL);
  380. #endif
  381. #ifdef CURRENCY_SYMBOL
  382. REGISTER_NL_LANGINFO_CONSTANT(CURRENCY_SYMBOL);
  383. #endif
  384. #ifdef CRNCYSTR
  385. REGISTER_NL_LANGINFO_CONSTANT(CRNCYSTR);
  386. #endif
  387. #ifdef MON_DECIMAL_POINT
  388. REGISTER_NL_LANGINFO_CONSTANT(MON_DECIMAL_POINT);
  389. #endif
  390. #ifdef MON_THOUSANDS_SEP
  391. REGISTER_NL_LANGINFO_CONSTANT(MON_THOUSANDS_SEP);
  392. #endif
  393. #ifdef MON_GROUPING
  394. REGISTER_NL_LANGINFO_CONSTANT(MON_GROUPING);
  395. #endif
  396. #ifdef POSITIVE_SIGN
  397. REGISTER_NL_LANGINFO_CONSTANT(POSITIVE_SIGN);
  398. #endif
  399. #ifdef NEGATIVE_SIGN
  400. REGISTER_NL_LANGINFO_CONSTANT(NEGATIVE_SIGN);
  401. #endif
  402. #ifdef INT_FRAC_DIGITS
  403. REGISTER_NL_LANGINFO_CONSTANT(INT_FRAC_DIGITS);
  404. #endif
  405. #ifdef FRAC_DIGITS
  406. REGISTER_NL_LANGINFO_CONSTANT(FRAC_DIGITS);
  407. #endif
  408. #ifdef P_CS_PRECEDES
  409. REGISTER_NL_LANGINFO_CONSTANT(P_CS_PRECEDES);
  410. #endif
  411. #ifdef P_SEP_BY_SPACE
  412. REGISTER_NL_LANGINFO_CONSTANT(P_SEP_BY_SPACE);
  413. #endif
  414. #ifdef N_CS_PRECEDES
  415. REGISTER_NL_LANGINFO_CONSTANT(N_CS_PRECEDES);
  416. #endif
  417. #ifdef N_SEP_BY_SPACE
  418. REGISTER_NL_LANGINFO_CONSTANT(N_SEP_BY_SPACE);
  419. #endif
  420. #ifdef P_SIGN_POSN
  421. REGISTER_NL_LANGINFO_CONSTANT(P_SIGN_POSN);
  422. #endif
  423. #ifdef N_SIGN_POSN
  424. REGISTER_NL_LANGINFO_CONSTANT(N_SIGN_POSN);
  425. #endif
  426. #ifdef DECIMAL_POINT
  427. REGISTER_NL_LANGINFO_CONSTANT(DECIMAL_POINT);
  428. #endif
  429. #ifdef RADIXCHAR
  430. REGISTER_NL_LANGINFO_CONSTANT(RADIXCHAR);
  431. #endif
  432. #ifdef THOUSANDS_SEP
  433. REGISTER_NL_LANGINFO_CONSTANT(THOUSANDS_SEP);
  434. #endif
  435. #ifdef THOUSEP
  436. REGISTER_NL_LANGINFO_CONSTANT(THOUSEP);
  437. #endif
  438. #ifdef GROUPING
  439. REGISTER_NL_LANGINFO_CONSTANT(GROUPING);
  440. #endif
  441. #ifdef YESEXPR
  442. REGISTER_NL_LANGINFO_CONSTANT(YESEXPR);
  443. #endif
  444. #ifdef NOEXPR
  445. REGISTER_NL_LANGINFO_CONSTANT(NOEXPR);
  446. #endif
  447. #ifdef YESSTR
  448. REGISTER_NL_LANGINFO_CONSTANT(YESSTR);
  449. #endif
  450. #ifdef NOSTR
  451. REGISTER_NL_LANGINFO_CONSTANT(NOSTR);
  452. #endif
  453. #ifdef CODESET
  454. REGISTER_NL_LANGINFO_CONSTANT(CODESET);
  455. #endif
  456. #undef REGISTER_NL_LANGINFO_CONSTANT
  457. return SUCCESS;
  458. }
  459. /* }}} */
  460. /* {{{ proto string|false nl_langinfo(int item)
  461. Query language and locale information */
  462. PHP_FUNCTION(nl_langinfo)
  463. {
  464. zend_long item;
  465. char *value;
  466. ZEND_PARSE_PARAMETERS_START(1, 1)
  467. Z_PARAM_LONG(item)
  468. ZEND_PARSE_PARAMETERS_END();
  469. switch(item) { /* {{{ */
  470. #ifdef ABDAY_1
  471. case ABDAY_1:
  472. case ABDAY_2:
  473. case ABDAY_3:
  474. case ABDAY_4:
  475. case ABDAY_5:
  476. case ABDAY_6:
  477. case ABDAY_7:
  478. #endif
  479. #ifdef DAY_1
  480. case DAY_1:
  481. case DAY_2:
  482. case DAY_3:
  483. case DAY_4:
  484. case DAY_5:
  485. case DAY_6:
  486. case DAY_7:
  487. #endif
  488. #ifdef ABMON_1
  489. case ABMON_1:
  490. case ABMON_2:
  491. case ABMON_3:
  492. case ABMON_4:
  493. case ABMON_5:
  494. case ABMON_6:
  495. case ABMON_7:
  496. case ABMON_8:
  497. case ABMON_9:
  498. case ABMON_10:
  499. case ABMON_11:
  500. case ABMON_12:
  501. #endif
  502. #ifdef MON_1
  503. case MON_1:
  504. case MON_2:
  505. case MON_3:
  506. case MON_4:
  507. case MON_5:
  508. case MON_6:
  509. case MON_7:
  510. case MON_8:
  511. case MON_9:
  512. case MON_10:
  513. case MON_11:
  514. case MON_12:
  515. #endif
  516. #ifdef AM_STR
  517. case AM_STR:
  518. #endif
  519. #ifdef PM_STR
  520. case PM_STR:
  521. #endif
  522. #ifdef D_T_FMT
  523. case D_T_FMT:
  524. #endif
  525. #ifdef D_FMT
  526. case D_FMT:
  527. #endif
  528. #ifdef T_FMT
  529. case T_FMT:
  530. #endif
  531. #ifdef T_FMT_AMPM
  532. case T_FMT_AMPM:
  533. #endif
  534. #ifdef ERA
  535. case ERA:
  536. #endif
  537. #ifdef ERA_YEAR
  538. case ERA_YEAR:
  539. #endif
  540. #ifdef ERA_D_T_FMT
  541. case ERA_D_T_FMT:
  542. #endif
  543. #ifdef ERA_D_FMT
  544. case ERA_D_FMT:
  545. #endif
  546. #ifdef ERA_T_FMT
  547. case ERA_T_FMT:
  548. #endif
  549. #ifdef ALT_DIGITS
  550. case ALT_DIGITS:
  551. #endif
  552. #ifdef INT_CURR_SYMBOL
  553. case INT_CURR_SYMBOL:
  554. #endif
  555. #ifdef CURRENCY_SYMBOL
  556. case CURRENCY_SYMBOL:
  557. #endif
  558. #ifdef CRNCYSTR
  559. case CRNCYSTR:
  560. #endif
  561. #ifdef MON_DECIMAL_POINT
  562. case MON_DECIMAL_POINT:
  563. #endif
  564. #ifdef MON_THOUSANDS_SEP
  565. case MON_THOUSANDS_SEP:
  566. #endif
  567. #ifdef MON_GROUPING
  568. case MON_GROUPING:
  569. #endif
  570. #ifdef POSITIVE_SIGN
  571. case POSITIVE_SIGN:
  572. #endif
  573. #ifdef NEGATIVE_SIGN
  574. case NEGATIVE_SIGN:
  575. #endif
  576. #ifdef INT_FRAC_DIGITS
  577. case INT_FRAC_DIGITS:
  578. #endif
  579. #ifdef FRAC_DIGITS
  580. case FRAC_DIGITS:
  581. #endif
  582. #ifdef P_CS_PRECEDES
  583. case P_CS_PRECEDES:
  584. #endif
  585. #ifdef P_SEP_BY_SPACE
  586. case P_SEP_BY_SPACE:
  587. #endif
  588. #ifdef N_CS_PRECEDES
  589. case N_CS_PRECEDES:
  590. #endif
  591. #ifdef N_SEP_BY_SPACE
  592. case N_SEP_BY_SPACE:
  593. #endif
  594. #ifdef P_SIGN_POSN
  595. case P_SIGN_POSN:
  596. #endif
  597. #ifdef N_SIGN_POSN
  598. case N_SIGN_POSN:
  599. #endif
  600. #ifdef DECIMAL_POINT
  601. case DECIMAL_POINT:
  602. #elif defined(RADIXCHAR)
  603. case RADIXCHAR:
  604. #endif
  605. #ifdef THOUSANDS_SEP
  606. case THOUSANDS_SEP:
  607. #elif defined(THOUSEP)
  608. case THOUSEP:
  609. #endif
  610. #ifdef GROUPING
  611. case GROUPING:
  612. #endif
  613. #ifdef YESEXPR
  614. case YESEXPR:
  615. #endif
  616. #ifdef NOEXPR
  617. case NOEXPR:
  618. #endif
  619. #ifdef YESSTR
  620. case YESSTR:
  621. #endif
  622. #ifdef NOSTR
  623. case NOSTR:
  624. #endif
  625. #ifdef CODESET
  626. case CODESET:
  627. #endif
  628. break;
  629. default:
  630. php_error_docref(NULL, E_WARNING, "Item '" ZEND_LONG_FMT "' is not valid", item);
  631. RETURN_FALSE;
  632. }
  633. /* }}} */
  634. value = nl_langinfo(item);
  635. if (value == NULL) {
  636. RETURN_FALSE;
  637. } else {
  638. RETURN_STRING(value);
  639. }
  640. }
  641. #endif
  642. /* }}} */
  643. /* {{{ proto int strcoll(string str1, string str2)
  644. Compares two strings using the current locale */
  645. PHP_FUNCTION(strcoll)
  646. {
  647. zend_string *s1, *s2;
  648. ZEND_PARSE_PARAMETERS_START(2, 2)
  649. Z_PARAM_STR(s1)
  650. Z_PARAM_STR(s2)
  651. ZEND_PARSE_PARAMETERS_END();
  652. RETURN_LONG(strcoll((const char *) ZSTR_VAL(s1),
  653. (const char *) ZSTR_VAL(s2)));
  654. }
  655. /* }}} */
  656. /* {{{ php_charmask
  657. * Fills a 256-byte bytemask with input. You can specify a range like 'a..z',
  658. * it needs to be incrementing.
  659. * Returns: FAILURE/SUCCESS whether the input was correct (i.e. no range errors)
  660. */
  661. static inline int php_charmask(const unsigned char *input, size_t len, char *mask)
  662. {
  663. const unsigned char *end;
  664. unsigned char c;
  665. int result = SUCCESS;
  666. memset(mask, 0, 256);
  667. for (end = input+len; input < end; input++) {
  668. c=*input;
  669. if ((input+3 < end) && input[1] == '.' && input[2] == '.'
  670. && input[3] >= c) {
  671. memset(mask+c, 1, input[3] - c + 1);
  672. input+=3;
  673. } else if ((input+1 < end) && input[0] == '.' && input[1] == '.') {
  674. /* Error, try to be as helpful as possible:
  675. (a range ending/starting with '.' won't be captured here) */
  676. if (end-len >= input) { /* there was no 'left' char */
  677. php_error_docref(NULL, E_WARNING, "Invalid '..'-range, no character to the left of '..'");
  678. result = FAILURE;
  679. continue;
  680. }
  681. if (input+2 >= end) { /* there is no 'right' char */
  682. php_error_docref(NULL, E_WARNING, "Invalid '..'-range, no character to the right of '..'");
  683. result = FAILURE;
  684. continue;
  685. }
  686. if (input[-1] > input[2]) { /* wrong order */
  687. php_error_docref(NULL, E_WARNING, "Invalid '..'-range, '..'-range needs to be incrementing");
  688. result = FAILURE;
  689. continue;
  690. }
  691. /* FIXME: better error (a..b..c is the only left possibility?) */
  692. php_error_docref(NULL, E_WARNING, "Invalid '..'-range");
  693. result = FAILURE;
  694. continue;
  695. } else {
  696. mask[c]=1;
  697. }
  698. }
  699. return result;
  700. }
  701. /* }}} */
  702. /* {{{ php_trim_int()
  703. * mode 1 : trim left
  704. * mode 2 : trim right
  705. * mode 3 : trim left and right
  706. * what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
  707. */
  708. static zend_always_inline zend_string *php_trim_int(zend_string *str, char *what, size_t what_len, int mode)
  709. {
  710. const char *start = ZSTR_VAL(str);
  711. const char *end = start + ZSTR_LEN(str);
  712. char mask[256];
  713. if (what) {
  714. if (what_len == 1) {
  715. char p = *what;
  716. if (mode & 1) {
  717. while (start != end) {
  718. if (*start == p) {
  719. start++;
  720. } else {
  721. break;
  722. }
  723. }
  724. }
  725. if (mode & 2) {
  726. while (start != end) {
  727. if (*(end-1) == p) {
  728. end--;
  729. } else {
  730. break;
  731. }
  732. }
  733. }
  734. } else {
  735. php_charmask((unsigned char*)what, what_len, mask);
  736. if (mode & 1) {
  737. while (start != end) {
  738. if (mask[(unsigned char)*start]) {
  739. start++;
  740. } else {
  741. break;
  742. }
  743. }
  744. }
  745. if (mode & 2) {
  746. while (start != end) {
  747. if (mask[(unsigned char)*(end-1)]) {
  748. end--;
  749. } else {
  750. break;
  751. }
  752. }
  753. }
  754. }
  755. } else {
  756. if (mode & 1) {
  757. while (start != end) {
  758. unsigned char c = (unsigned char)*start;
  759. if (c <= ' ' &&
  760. (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0')) {
  761. start++;
  762. } else {
  763. break;
  764. }
  765. }
  766. }
  767. if (mode & 2) {
  768. while (start != end) {
  769. unsigned char c = (unsigned char)*(end-1);
  770. if (c <= ' ' &&
  771. (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\0')) {
  772. end--;
  773. } else {
  774. break;
  775. }
  776. }
  777. }
  778. }
  779. if (ZSTR_LEN(str) == end - start) {
  780. return zend_string_copy(str);
  781. } else if (end - start == 0) {
  782. return ZSTR_EMPTY_ALLOC();
  783. } else {
  784. return zend_string_init(start, end - start, 0);
  785. }
  786. }
  787. /* }}} */
  788. /* {{{ php_trim_int()
  789. * mode 1 : trim left
  790. * mode 2 : trim right
  791. * mode 3 : trim left and right
  792. * what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0')
  793. */
  794. PHPAPI zend_string *php_trim(zend_string *str, char *what, size_t what_len, int mode)
  795. {
  796. return php_trim_int(str, what, what_len, mode);
  797. }
  798. /* }}} */
  799. /* {{{ php_do_trim
  800. * Base for trim(), rtrim() and ltrim() functions.
  801. */
  802. static zend_always_inline void php_do_trim(INTERNAL_FUNCTION_PARAMETERS, int mode)
  803. {
  804. zend_string *str;
  805. zend_string *what = NULL;
  806. ZEND_PARSE_PARAMETERS_START(1, 2)
  807. Z_PARAM_STR(str)
  808. Z_PARAM_OPTIONAL
  809. Z_PARAM_STR(what)
  810. ZEND_PARSE_PARAMETERS_END();
  811. ZVAL_STR(return_value, php_trim_int(str, (what ? ZSTR_VAL(what) : NULL), (what ? ZSTR_LEN(what) : 0), mode));
  812. }
  813. /* }}} */
  814. /* {{{ proto string trim(string str [, string character_mask])
  815. Strips whitespace from the beginning and end of a string */
  816. PHP_FUNCTION(trim)
  817. {
  818. php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  819. }
  820. /* }}} */
  821. /* {{{ proto string rtrim(string str [, string character_mask])
  822. Removes trailing whitespace */
  823. PHP_FUNCTION(rtrim)
  824. {
  825. php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
  826. }
  827. /* }}} */
  828. /* {{{ proto string ltrim(string str [, string character_mask])
  829. Strips whitespace from the beginning of a string */
  830. PHP_FUNCTION(ltrim)
  831. {
  832. php_do_trim(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  833. }
  834. /* }}} */
  835. /* {{{ proto string wordwrap(string str [, int width [, string break [, bool cut]]])
  836. Wraps buffer to selected number of characters using string break char */
  837. PHP_FUNCTION(wordwrap)
  838. {
  839. zend_string *text;
  840. char *breakchar = "\n";
  841. size_t newtextlen, chk, breakchar_len = 1;
  842. size_t alloced;
  843. zend_long current = 0, laststart = 0, lastspace = 0;
  844. zend_long linelength = 75;
  845. zend_bool docut = 0;
  846. zend_string *newtext;
  847. ZEND_PARSE_PARAMETERS_START(1, 4)
  848. Z_PARAM_STR(text)
  849. Z_PARAM_OPTIONAL
  850. Z_PARAM_LONG(linelength)
  851. Z_PARAM_STRING(breakchar, breakchar_len)
  852. Z_PARAM_BOOL(docut)
  853. ZEND_PARSE_PARAMETERS_END();
  854. if (ZSTR_LEN(text) == 0) {
  855. RETURN_EMPTY_STRING();
  856. }
  857. if (breakchar_len == 0) {
  858. zend_argument_value_error(3, "cannot be empty");
  859. RETURN_THROWS();
  860. }
  861. if (linelength == 0 && docut) {
  862. zend_argument_value_error(4, "cannot be true when argument #2 ($width) is 0");
  863. RETURN_THROWS();
  864. }
  865. /* Special case for a single-character break as it needs no
  866. additional storage space */
  867. if (breakchar_len == 1 && !docut) {
  868. newtext = zend_string_init(ZSTR_VAL(text), ZSTR_LEN(text), 0);
  869. laststart = lastspace = 0;
  870. for (current = 0; current < (zend_long)ZSTR_LEN(text); current++) {
  871. if (ZSTR_VAL(text)[current] == breakchar[0]) {
  872. laststart = lastspace = current + 1;
  873. } else if (ZSTR_VAL(text)[current] == ' ') {
  874. if (current - laststart >= linelength) {
  875. ZSTR_VAL(newtext)[current] = breakchar[0];
  876. laststart = current + 1;
  877. }
  878. lastspace = current;
  879. } else if (current - laststart >= linelength && laststart != lastspace) {
  880. ZSTR_VAL(newtext)[lastspace] = breakchar[0];
  881. laststart = lastspace + 1;
  882. }
  883. }
  884. RETURN_NEW_STR(newtext);
  885. } else {
  886. /* Multiple character line break or forced cut */
  887. if (linelength > 0) {
  888. chk = (size_t)(ZSTR_LEN(text)/linelength + 1);
  889. newtext = zend_string_safe_alloc(chk, breakchar_len, ZSTR_LEN(text), 0);
  890. alloced = ZSTR_LEN(text) + chk * breakchar_len + 1;
  891. } else {
  892. chk = ZSTR_LEN(text);
  893. alloced = ZSTR_LEN(text) * (breakchar_len + 1) + 1;
  894. newtext = zend_string_safe_alloc(ZSTR_LEN(text), breakchar_len + 1, 0, 0);
  895. }
  896. /* now keep track of the actual new text length */
  897. newtextlen = 0;
  898. laststart = lastspace = 0;
  899. for (current = 0; current < (zend_long)ZSTR_LEN(text); current++) {
  900. if (chk == 0) {
  901. alloced += (size_t) (((ZSTR_LEN(text) - current + 1)/linelength + 1) * breakchar_len) + 1;
  902. newtext = zend_string_extend(newtext, alloced, 0);
  903. chk = (size_t) ((ZSTR_LEN(text) - current)/linelength) + 1;
  904. }
  905. /* when we hit an existing break, copy to new buffer, and
  906. * fix up laststart and lastspace */
  907. if (ZSTR_VAL(text)[current] == breakchar[0]
  908. && current + breakchar_len < ZSTR_LEN(text)
  909. && !strncmp(ZSTR_VAL(text) + current, breakchar, breakchar_len)) {
  910. memcpy(ZSTR_VAL(newtext) + newtextlen, ZSTR_VAL(text) + laststart, current - laststart + breakchar_len);
  911. newtextlen += current - laststart + breakchar_len;
  912. current += breakchar_len - 1;
  913. laststart = lastspace = current + 1;
  914. chk--;
  915. }
  916. /* if it is a space, check if it is at the line boundary,
  917. * copy and insert a break, or just keep track of it */
  918. else if (ZSTR_VAL(text)[current] == ' ') {
  919. if (current - laststart >= linelength) {
  920. memcpy(ZSTR_VAL(newtext) + newtextlen, ZSTR_VAL(text) + laststart, current - laststart);
  921. newtextlen += current - laststart;
  922. memcpy(ZSTR_VAL(newtext) + newtextlen, breakchar, breakchar_len);
  923. newtextlen += breakchar_len;
  924. laststart = current + 1;
  925. chk--;
  926. }
  927. lastspace = current;
  928. }
  929. /* if we are cutting, and we've accumulated enough
  930. * characters, and we haven't see a space for this line,
  931. * copy and insert a break. */
  932. else if (current - laststart >= linelength
  933. && docut && laststart >= lastspace) {
  934. memcpy(ZSTR_VAL(newtext) + newtextlen, ZSTR_VAL(text) + laststart, current - laststart);
  935. newtextlen += current - laststart;
  936. memcpy(ZSTR_VAL(newtext) + newtextlen, breakchar, breakchar_len);
  937. newtextlen += breakchar_len;
  938. laststart = lastspace = current;
  939. chk--;
  940. }
  941. /* if the current word puts us over the linelength, copy
  942. * back up until the last space, insert a break, and move
  943. * up the laststart */
  944. else if (current - laststart >= linelength
  945. && laststart < lastspace) {
  946. memcpy(ZSTR_VAL(newtext) + newtextlen, ZSTR_VAL(text) + laststart, lastspace - laststart);
  947. newtextlen += lastspace - laststart;
  948. memcpy(ZSTR_VAL(newtext) + newtextlen, breakchar, breakchar_len);
  949. newtextlen += breakchar_len;
  950. laststart = lastspace = lastspace + 1;
  951. chk--;
  952. }
  953. }
  954. /* copy over any stragglers */
  955. if (laststart != current) {
  956. memcpy(ZSTR_VAL(newtext) + newtextlen, ZSTR_VAL(text) + laststart, current - laststart);
  957. newtextlen += current - laststart;
  958. }
  959. ZSTR_VAL(newtext)[newtextlen] = '\0';
  960. /* free unused memory */
  961. newtext = zend_string_truncate(newtext, newtextlen, 0);
  962. RETURN_NEW_STR(newtext);
  963. }
  964. }
  965. /* }}} */
  966. /* {{{ php_explode
  967. */
  968. PHPAPI void php_explode(const zend_string *delim, zend_string *str, zval *return_value, zend_long limit)
  969. {
  970. const char *p1 = ZSTR_VAL(str);
  971. const char *endp = ZSTR_VAL(str) + ZSTR_LEN(str);
  972. const char *p2 = php_memnstr(ZSTR_VAL(str), ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
  973. zval tmp;
  974. if (p2 == NULL) {
  975. ZVAL_STR_COPY(&tmp, str);
  976. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
  977. } else {
  978. do {
  979. size_t l = p2 - p1;
  980. if (l == 0) {
  981. ZVAL_EMPTY_STRING(&tmp);
  982. } else if (l == 1) {
  983. ZVAL_INTERNED_STR(&tmp, ZSTR_CHAR((zend_uchar)(*p1)));
  984. } else {
  985. ZVAL_STRINGL(&tmp, p1, p2 - p1);
  986. }
  987. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
  988. p1 = p2 + ZSTR_LEN(delim);
  989. p2 = php_memnstr(p1, ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
  990. } while (p2 != NULL && --limit > 1);
  991. if (p1 <= endp) {
  992. ZVAL_STRINGL(&tmp, p1, endp - p1);
  993. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
  994. }
  995. }
  996. }
  997. /* }}} */
  998. /* {{{ php_explode_negative_limit
  999. */
  1000. PHPAPI void php_explode_negative_limit(const zend_string *delim, zend_string *str, zval *return_value, zend_long limit)
  1001. {
  1002. #define EXPLODE_ALLOC_STEP 64
  1003. const char *p1 = ZSTR_VAL(str);
  1004. const char *endp = ZSTR_VAL(str) + ZSTR_LEN(str);
  1005. const char *p2 = php_memnstr(ZSTR_VAL(str), ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
  1006. zval tmp;
  1007. if (p2 == NULL) {
  1008. /*
  1009. do nothing since limit <= -1, thus if only one chunk - 1 + (limit) <= 0
  1010. by doing nothing we return empty array
  1011. */
  1012. } else {
  1013. size_t allocated = EXPLODE_ALLOC_STEP, found = 0;
  1014. zend_long i, to_return;
  1015. const char **positions = emalloc(allocated * sizeof(char *));
  1016. positions[found++] = p1;
  1017. do {
  1018. if (found >= allocated) {
  1019. allocated = found + EXPLODE_ALLOC_STEP;/* make sure we have enough memory */
  1020. positions = erealloc(positions, allocated*sizeof(char *));
  1021. }
  1022. positions[found++] = p1 = p2 + ZSTR_LEN(delim);
  1023. p2 = php_memnstr(p1, ZSTR_VAL(delim), ZSTR_LEN(delim), endp);
  1024. } while (p2 != NULL);
  1025. to_return = limit + found;
  1026. /* limit is at least -1 therefore no need of bounds checking : i will be always less than found */
  1027. for (i = 0; i < to_return; i++) { /* this checks also for to_return > 0 */
  1028. ZVAL_STRINGL(&tmp, positions[i], (positions[i+1] - ZSTR_LEN(delim)) - positions[i]);
  1029. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
  1030. }
  1031. efree((void *)positions);
  1032. }
  1033. #undef EXPLODE_ALLOC_STEP
  1034. }
  1035. /* }}} */
  1036. /* {{{ proto array explode(string separator, string str [, int limit])
  1037. Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned. */
  1038. PHP_FUNCTION(explode)
  1039. {
  1040. zend_string *str, *delim;
  1041. zend_long limit = ZEND_LONG_MAX; /* No limit */
  1042. zval tmp;
  1043. ZEND_PARSE_PARAMETERS_START(2, 3)
  1044. Z_PARAM_STR(delim)
  1045. Z_PARAM_STR(str)
  1046. Z_PARAM_OPTIONAL
  1047. Z_PARAM_LONG(limit)
  1048. ZEND_PARSE_PARAMETERS_END();
  1049. if (ZSTR_LEN(delim) == 0) {
  1050. zend_argument_value_error(1, "cannot be empty");
  1051. RETURN_THROWS();
  1052. }
  1053. array_init(return_value);
  1054. if (ZSTR_LEN(str) == 0) {
  1055. if (limit >= 0) {
  1056. ZVAL_EMPTY_STRING(&tmp);
  1057. zend_hash_index_add_new(Z_ARRVAL_P(return_value), 0, &tmp);
  1058. }
  1059. return;
  1060. }
  1061. if (limit > 1) {
  1062. php_explode(delim, str, return_value, limit);
  1063. } else if (limit < 0) {
  1064. php_explode_negative_limit(delim, str, return_value, limit);
  1065. } else {
  1066. ZVAL_STR_COPY(&tmp, str);
  1067. zend_hash_index_add_new(Z_ARRVAL_P(return_value), 0, &tmp);
  1068. }
  1069. }
  1070. /* }}} */
  1071. /* {{{ proto string join([string glue,] array pieces)
  1072. An alias for implode */
  1073. /* }}} */
  1074. /* {{{ php_implode
  1075. */
  1076. PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return_value)
  1077. {
  1078. zval *tmp;
  1079. int numelems;
  1080. zend_string *str;
  1081. char *cptr;
  1082. size_t len = 0;
  1083. struct {
  1084. zend_string *str;
  1085. zend_long lval;
  1086. } *strings, *ptr;
  1087. ALLOCA_FLAG(use_heap)
  1088. numelems = zend_hash_num_elements(pieces);
  1089. if (numelems == 0) {
  1090. RETURN_EMPTY_STRING();
  1091. } else if (numelems == 1) {
  1092. /* loop to search the first not undefined element... */
  1093. ZEND_HASH_FOREACH_VAL_IND(pieces, tmp) {
  1094. RETURN_STR(zval_get_string(tmp));
  1095. } ZEND_HASH_FOREACH_END();
  1096. }
  1097. ptr = strings = do_alloca((sizeof(*strings)) * numelems, use_heap);
  1098. ZEND_HASH_FOREACH_VAL_IND(pieces, tmp) {
  1099. if (EXPECTED(Z_TYPE_P(tmp) == IS_STRING)) {
  1100. ptr->str = Z_STR_P(tmp);
  1101. len += ZSTR_LEN(ptr->str);
  1102. ptr->lval = 0;
  1103. ptr++;
  1104. } else if (UNEXPECTED(Z_TYPE_P(tmp) == IS_LONG)) {
  1105. zend_long val = Z_LVAL_P(tmp);
  1106. ptr->str = NULL;
  1107. ptr->lval = val;
  1108. ptr++;
  1109. if (val <= 0) {
  1110. len++;
  1111. }
  1112. while (val) {
  1113. val /= 10;
  1114. len++;
  1115. }
  1116. } else {
  1117. ptr->str = zval_get_string_func(tmp);
  1118. len += ZSTR_LEN(ptr->str);
  1119. ptr->lval = 1;
  1120. ptr++;
  1121. }
  1122. } ZEND_HASH_FOREACH_END();
  1123. /* numelems can not be 0, we checked above */
  1124. str = zend_string_safe_alloc(numelems - 1, ZSTR_LEN(glue), len, 0);
  1125. cptr = ZSTR_VAL(str) + ZSTR_LEN(str);
  1126. *cptr = 0;
  1127. while (1) {
  1128. ptr--;
  1129. if (EXPECTED(ptr->str)) {
  1130. cptr -= ZSTR_LEN(ptr->str);
  1131. memcpy(cptr, ZSTR_VAL(ptr->str), ZSTR_LEN(ptr->str));
  1132. if (ptr->lval) {
  1133. zend_string_release_ex(ptr->str, 0);
  1134. }
  1135. } else {
  1136. char *oldPtr = cptr;
  1137. char oldVal = *cptr;
  1138. cptr = zend_print_long_to_buf(cptr, ptr->lval);
  1139. *oldPtr = oldVal;
  1140. }
  1141. if (ptr == strings) {
  1142. break;
  1143. }
  1144. cptr -= ZSTR_LEN(glue);
  1145. memcpy(cptr, ZSTR_VAL(glue), ZSTR_LEN(glue));
  1146. }
  1147. free_alloca(strings, use_heap);
  1148. RETURN_NEW_STR(str);
  1149. }
  1150. /* }}} */
  1151. /* {{{ proto string implode([string glue,] array pieces)
  1152. Joins array elements placing glue string between items and return one string */
  1153. PHP_FUNCTION(implode)
  1154. {
  1155. zend_string *arg1_str = NULL;
  1156. HashTable *arg1_array = NULL;
  1157. zend_array *pieces = NULL;
  1158. ZEND_PARSE_PARAMETERS_START(1, 2)
  1159. Z_PARAM_STR_OR_ARRAY_HT(arg1_str, arg1_array)
  1160. Z_PARAM_OPTIONAL
  1161. Z_PARAM_ARRAY_HT(pieces)
  1162. ZEND_PARSE_PARAMETERS_END();
  1163. if (pieces == NULL) {
  1164. if (arg1_array == NULL) {
  1165. zend_type_error("%s(): Argument #1 ($pieces) must be of type array, string given", get_active_function_name());
  1166. RETURN_THROWS();
  1167. }
  1168. arg1_str = ZSTR_EMPTY_ALLOC();
  1169. pieces = arg1_array;
  1170. } else {
  1171. if (arg1_str == NULL) {
  1172. zend_argument_type_error(1, "must be of type string, array given");
  1173. RETURN_THROWS();
  1174. }
  1175. }
  1176. php_implode(arg1_str, pieces, return_value);
  1177. }
  1178. /* }}} */
  1179. #define STRTOK_TABLE(p) BG(strtok_table)[(unsigned char) *p]
  1180. /* {{{ proto string|false strtok([string str,] string token)
  1181. Tokenize a string */
  1182. PHP_FUNCTION(strtok)
  1183. {
  1184. zend_string *str, *tok = NULL;
  1185. char *token;
  1186. char *token_end;
  1187. char *p;
  1188. char *pe;
  1189. size_t skipped = 0;
  1190. ZEND_PARSE_PARAMETERS_START(1, 2)
  1191. Z_PARAM_STR(str)
  1192. Z_PARAM_OPTIONAL
  1193. Z_PARAM_STR(tok)
  1194. ZEND_PARSE_PARAMETERS_END();
  1195. if (ZEND_NUM_ARGS() == 1) {
  1196. tok = str;
  1197. } else {
  1198. zval_ptr_dtor(&BG(strtok_zval));
  1199. ZVAL_STRINGL(&BG(strtok_zval), ZSTR_VAL(str), ZSTR_LEN(str));
  1200. BG(strtok_last) = BG(strtok_string) = Z_STRVAL(BG(strtok_zval));
  1201. BG(strtok_len) = ZSTR_LEN(str);
  1202. }
  1203. p = BG(strtok_last); /* Where we start to search */
  1204. pe = BG(strtok_string) + BG(strtok_len);
  1205. if (!p || p >= pe) {
  1206. RETURN_FALSE;
  1207. }
  1208. token = ZSTR_VAL(tok);
  1209. token_end = token + ZSTR_LEN(tok);
  1210. while (token < token_end) {
  1211. STRTOK_TABLE(token++) = 1;
  1212. }
  1213. /* Skip leading delimiters */
  1214. while (STRTOK_TABLE(p)) {
  1215. if (++p >= pe) {
  1216. /* no other chars left */
  1217. BG(strtok_last) = NULL;
  1218. RETVAL_FALSE;
  1219. goto restore;
  1220. }
  1221. skipped++;
  1222. }
  1223. /* We know at this place that *p is no delimiter, so skip it */
  1224. while (++p < pe) {
  1225. if (STRTOK_TABLE(p)) {
  1226. goto return_token;
  1227. }
  1228. }
  1229. if (p - BG(strtok_last)) {
  1230. return_token:
  1231. RETVAL_STRINGL(BG(strtok_last) + skipped, (p - BG(strtok_last)) - skipped);
  1232. BG(strtok_last) = p + 1;
  1233. } else {
  1234. RETVAL_FALSE;
  1235. BG(strtok_last) = NULL;
  1236. }
  1237. /* Restore table -- usually faster then memset'ing the table on every invocation */
  1238. restore:
  1239. token = ZSTR_VAL(tok);
  1240. while (token < token_end) {
  1241. STRTOK_TABLE(token++) = 0;
  1242. }
  1243. }
  1244. /* }}} */
  1245. /* {{{ php_strtoupper
  1246. */
  1247. PHPAPI char *php_strtoupper(char *s, size_t len)
  1248. {
  1249. unsigned char *c;
  1250. const unsigned char *e;
  1251. c = (unsigned char *)s;
  1252. e = (unsigned char *)c+len;
  1253. while (c < e) {
  1254. *c = toupper(*c);
  1255. c++;
  1256. }
  1257. return s;
  1258. }
  1259. /* }}} */
  1260. /* {{{ php_string_toupper
  1261. */
  1262. PHPAPI zend_string *php_string_toupper(zend_string *s)
  1263. {
  1264. unsigned char *c;
  1265. const unsigned char *e;
  1266. c = (unsigned char *)ZSTR_VAL(s);
  1267. e = c + ZSTR_LEN(s);
  1268. while (c < e) {
  1269. if (islower(*c)) {
  1270. register unsigned char *r;
  1271. zend_string *res = zend_string_alloc(ZSTR_LEN(s), 0);
  1272. if (c != (unsigned char*)ZSTR_VAL(s)) {
  1273. memcpy(ZSTR_VAL(res), ZSTR_VAL(s), c - (unsigned char*)ZSTR_VAL(s));
  1274. }
  1275. r = c + (ZSTR_VAL(res) - ZSTR_VAL(s));
  1276. while (c < e) {
  1277. *r = toupper(*c);
  1278. r++;
  1279. c++;
  1280. }
  1281. *r = '\0';
  1282. return res;
  1283. }
  1284. c++;
  1285. }
  1286. return zend_string_copy(s);
  1287. }
  1288. /* }}} */
  1289. /* {{{ proto string strtoupper(string str)
  1290. Makes a string uppercase */
  1291. PHP_FUNCTION(strtoupper)
  1292. {
  1293. zend_string *arg;
  1294. ZEND_PARSE_PARAMETERS_START(1, 1)
  1295. Z_PARAM_STR(arg)
  1296. ZEND_PARSE_PARAMETERS_END();
  1297. RETURN_STR(php_string_toupper(arg));
  1298. }
  1299. /* }}} */
  1300. /* {{{ php_strtolower
  1301. */
  1302. PHPAPI char *php_strtolower(char *s, size_t len)
  1303. {
  1304. unsigned char *c;
  1305. const unsigned char *e;
  1306. c = (unsigned char *)s;
  1307. e = c+len;
  1308. while (c < e) {
  1309. *c = tolower(*c);
  1310. c++;
  1311. }
  1312. return s;
  1313. }
  1314. /* }}} */
  1315. /* {{{ php_string_tolower
  1316. */
  1317. PHPAPI zend_string *php_string_tolower(zend_string *s)
  1318. {
  1319. unsigned char *c;
  1320. const unsigned char *e;
  1321. if (EXPECTED(!BG(ctype_string))) {
  1322. return zend_string_tolower(s);
  1323. } else {
  1324. c = (unsigned char *)ZSTR_VAL(s);
  1325. e = c + ZSTR_LEN(s);
  1326. while (c < e) {
  1327. if (isupper(*c)) {
  1328. register unsigned char *r;
  1329. zend_string *res = zend_string_alloc(ZSTR_LEN(s), 0);
  1330. if (c != (unsigned char*)ZSTR_VAL(s)) {
  1331. memcpy(ZSTR_VAL(res), ZSTR_VAL(s), c - (unsigned char*)ZSTR_VAL(s));
  1332. }
  1333. r = c + (ZSTR_VAL(res) - ZSTR_VAL(s));
  1334. while (c < e) {
  1335. *r = tolower(*c);
  1336. r++;
  1337. c++;
  1338. }
  1339. *r = '\0';
  1340. return res;
  1341. }
  1342. c++;
  1343. }
  1344. return zend_string_copy(s);
  1345. }
  1346. }
  1347. /* }}} */
  1348. /* {{{ proto string strtolower(string str)
  1349. Makes a string lowercase */
  1350. PHP_FUNCTION(strtolower)
  1351. {
  1352. zend_string *str;
  1353. ZEND_PARSE_PARAMETERS_START(1, 1)
  1354. Z_PARAM_STR(str)
  1355. ZEND_PARSE_PARAMETERS_END();
  1356. RETURN_STR(php_string_tolower(str));
  1357. }
  1358. /* }}} */
  1359. /* {{{ php_basename
  1360. */
  1361. PHPAPI zend_string *php_basename(const char *s, size_t len, char *suffix, size_t suffix_len)
  1362. {
  1363. /* State 0 is directly after a directory separator (or at the start of the string).
  1364. * State 1 is everything else. */
  1365. int state = 0;
  1366. const char *basename_start = s;
  1367. const char *basename_end = s;
  1368. while (len > 0) {
  1369. int inc_len = (*s == '\0' ? 1 : php_mblen(s, len));
  1370. switch (inc_len) {
  1371. case 0:
  1372. goto quit_loop;
  1373. case 1:
  1374. #if defined(PHP_WIN32)
  1375. if (*s == '/' || *s == '\\') {
  1376. #else
  1377. if (*s == '/') {
  1378. #endif
  1379. if (state == 1) {
  1380. state = 0;
  1381. basename_end = s;
  1382. }
  1383. #if defined(PHP_WIN32)
  1384. /* Catch relative paths in c:file.txt style. They're not to confuse
  1385. with the NTFS streams. This part ensures also, that no drive
  1386. letter traversing happens. */
  1387. } else if ((*s == ':' && (s - basename_start == 1))) {
  1388. if (state == 0) {
  1389. basename_start = s;
  1390. state = 1;
  1391. } else {
  1392. basename_end = s;
  1393. state = 0;
  1394. }
  1395. #endif
  1396. } else {
  1397. if (state == 0) {
  1398. basename_start = s;
  1399. state = 1;
  1400. }
  1401. }
  1402. break;
  1403. default:
  1404. if (inc_len < 0) {
  1405. /* If character is invalid, treat it like other non-significant characters. */
  1406. inc_len = 1;
  1407. php_mb_reset();
  1408. }
  1409. if (state == 0) {
  1410. basename_start = s;
  1411. state = 1;
  1412. }
  1413. break;
  1414. }
  1415. s += inc_len;
  1416. len -= inc_len;
  1417. }
  1418. quit_loop:
  1419. if (state == 1) {
  1420. basename_end = s;
  1421. }
  1422. if (suffix != NULL && suffix_len < (size_t)(basename_end - basename_start) &&
  1423. memcmp(basename_end - suffix_len, suffix, suffix_len) == 0) {
  1424. basename_end -= suffix_len;
  1425. }
  1426. return zend_string_init(basename_start, basename_end - basename_start, 0);
  1427. }
  1428. /* }}} */
  1429. /* {{{ proto string basename(string path [, string suffix])
  1430. Returns the filename component of the path */
  1431. PHP_FUNCTION(basename)
  1432. {
  1433. char *string, *suffix = NULL;
  1434. size_t string_len, suffix_len = 0;
  1435. ZEND_PARSE_PARAMETERS_START(1, 2)
  1436. Z_PARAM_STRING(string, string_len)
  1437. Z_PARAM_OPTIONAL
  1438. Z_PARAM_STRING(suffix, suffix_len)
  1439. ZEND_PARSE_PARAMETERS_END();
  1440. RETURN_STR(php_basename(string, string_len, suffix, suffix_len));
  1441. }
  1442. /* }}} */
  1443. /* {{{ php_dirname
  1444. Returns directory name component of path */
  1445. PHPAPI size_t php_dirname(char *path, size_t len)
  1446. {
  1447. return zend_dirname(path, len);
  1448. }
  1449. /* }}} */
  1450. /* {{{ proto string dirname(string path[, int levels])
  1451. Returns the directory name component of the path */
  1452. PHP_FUNCTION(dirname)
  1453. {
  1454. char *str;
  1455. size_t str_len;
  1456. zend_string *ret;
  1457. zend_long levels = 1;
  1458. ZEND_PARSE_PARAMETERS_START(1, 2)
  1459. Z_PARAM_STRING(str, str_len)
  1460. Z_PARAM_OPTIONAL
  1461. Z_PARAM_LONG(levels)
  1462. ZEND_PARSE_PARAMETERS_END();
  1463. ret = zend_string_init(str, str_len, 0);
  1464. if (levels == 1) {
  1465. /* Default case */
  1466. #ifdef PHP_WIN32
  1467. ZSTR_LEN(ret) = php_win32_ioutil_dirname(ZSTR_VAL(ret), str_len);
  1468. #else
  1469. ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len);
  1470. #endif
  1471. } else if (levels < 1) {
  1472. zend_argument_value_error(2, "must be greater than or equal to 1");
  1473. zend_string_efree(ret);
  1474. RETURN_THROWS();
  1475. } else {
  1476. /* Some levels up */
  1477. do {
  1478. #ifdef PHP_WIN32
  1479. ZSTR_LEN(ret) = php_win32_ioutil_dirname(ZSTR_VAL(ret), str_len = ZSTR_LEN(ret));
  1480. #else
  1481. ZSTR_LEN(ret) = zend_dirname(ZSTR_VAL(ret), str_len = ZSTR_LEN(ret));
  1482. #endif
  1483. } while (ZSTR_LEN(ret) < str_len && --levels);
  1484. }
  1485. RETURN_NEW_STR(ret);
  1486. }
  1487. /* }}} */
  1488. /* {{{ proto array|string pathinfo(string path[, int options])
  1489. Returns information about a certain string */
  1490. PHP_FUNCTION(pathinfo)
  1491. {
  1492. zval tmp;
  1493. char *path, *dirname;
  1494. size_t path_len;
  1495. int have_basename;
  1496. zend_long opt = PHP_PATHINFO_ALL;
  1497. zend_string *ret = NULL;
  1498. ZEND_PARSE_PARAMETERS_START(1, 2)
  1499. Z_PARAM_STRING(path, path_len)
  1500. Z_PARAM_OPTIONAL
  1501. Z_PARAM_LONG(opt)
  1502. ZEND_PARSE_PARAMETERS_END();
  1503. have_basename = ((opt & PHP_PATHINFO_BASENAME) == PHP_PATHINFO_BASENAME);
  1504. array_init(&tmp);
  1505. if ((opt & PHP_PATHINFO_DIRNAME) == PHP_PATHINFO_DIRNAME) {
  1506. dirname = estrndup(path, path_len);
  1507. php_dirname(dirname, path_len);
  1508. if (*dirname) {
  1509. add_assoc_string(&tmp, "dirname", dirname);
  1510. }
  1511. efree(dirname);
  1512. }
  1513. if (have_basename) {
  1514. ret = php_basename(path, path_len, NULL, 0);
  1515. add_assoc_str(&tmp, "basename", zend_string_copy(ret));
  1516. }
  1517. if ((opt & PHP_PATHINFO_EXTENSION) == PHP_PATHINFO_EXTENSION) {
  1518. const char *p;
  1519. ptrdiff_t idx;
  1520. if (!have_basename) {
  1521. ret = php_basename(path, path_len, NULL, 0);
  1522. }
  1523. p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
  1524. if (p) {
  1525. idx = p - ZSTR_VAL(ret);
  1526. add_assoc_stringl(&tmp, "extension", ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
  1527. }
  1528. }
  1529. if ((opt & PHP_PATHINFO_FILENAME) == PHP_PATHINFO_FILENAME) {
  1530. const char *p;
  1531. ptrdiff_t idx;
  1532. /* Have we already looked up the basename? */
  1533. if (!have_basename && !ret) {
  1534. ret = php_basename(path, path_len, NULL, 0);
  1535. }
  1536. p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
  1537. idx = p ? (p - ZSTR_VAL(ret)) : (ptrdiff_t)ZSTR_LEN(ret);
  1538. add_assoc_stringl(&tmp, "filename", ZSTR_VAL(ret), idx);
  1539. }
  1540. if (ret) {
  1541. zend_string_release_ex(ret, 0);
  1542. }
  1543. if (opt == PHP_PATHINFO_ALL) {
  1544. ZVAL_COPY_VALUE(return_value, &tmp);
  1545. } else {
  1546. zval *element;
  1547. if ((element = zend_hash_get_current_data(Z_ARRVAL(tmp))) != NULL) {
  1548. ZVAL_COPY_DEREF(return_value, element);
  1549. } else {
  1550. ZVAL_EMPTY_STRING(return_value);
  1551. }
  1552. zval_ptr_dtor(&tmp);
  1553. }
  1554. }
  1555. /* }}} */
  1556. /* {{{ php_stristr
  1557. case insensitive strstr */
  1558. PHPAPI char *php_stristr(char *s, char *t, size_t s_len, size_t t_len)
  1559. {
  1560. php_strtolower(s, s_len);
  1561. php_strtolower(t, t_len);
  1562. return (char*)php_memnstr(s, t, t_len, s + s_len);
  1563. }
  1564. /* }}} */
  1565. /* {{{ php_strspn
  1566. */
  1567. PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end)
  1568. {
  1569. register const char *p = s1, *spanp;
  1570. register char c = *p;
  1571. cont:
  1572. for (spanp = s2; p != s1_end && spanp != s2_end;) {
  1573. if (*spanp++ == c) {
  1574. c = *(++p);
  1575. goto cont;
  1576. }
  1577. }
  1578. return (p - s1);
  1579. }
  1580. /* }}} */
  1581. /* {{{ php_strcspn
  1582. */
  1583. PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end)
  1584. {
  1585. register const char *p, *spanp;
  1586. register char c = *s1;
  1587. for (p = s1;;) {
  1588. spanp = s2;
  1589. do {
  1590. if (*spanp == c || p == s1_end) {
  1591. return p - s1;
  1592. }
  1593. } while (spanp++ < (s2_end - 1));
  1594. c = *++p;
  1595. }
  1596. /* NOTREACHED */
  1597. }
  1598. /* }}} */
  1599. /* {{{ proto string|false stristr(string haystack, string needle[, bool part])
  1600. Finds first occurrence of a string within another, case insensitive */
  1601. PHP_FUNCTION(stristr)
  1602. {
  1603. zend_string *haystack, *needle;
  1604. const char *found = NULL;
  1605. size_t found_offset;
  1606. char *haystack_dup;
  1607. char *orig_needle;
  1608. zend_bool part = 0;
  1609. ZEND_PARSE_PARAMETERS_START(2, 3)
  1610. Z_PARAM_STR(haystack)
  1611. Z_PARAM_STR(needle)
  1612. Z_PARAM_OPTIONAL
  1613. Z_PARAM_BOOL(part)
  1614. ZEND_PARSE_PARAMETERS_END();
  1615. haystack_dup = estrndup(ZSTR_VAL(haystack), ZSTR_LEN(haystack));
  1616. orig_needle = estrndup(ZSTR_VAL(needle), ZSTR_LEN(needle));
  1617. found = php_stristr(haystack_dup, orig_needle, ZSTR_LEN(haystack), ZSTR_LEN(needle));
  1618. efree(orig_needle);
  1619. if (found) {
  1620. found_offset = found - haystack_dup;
  1621. if (part) {
  1622. RETVAL_STRINGL(ZSTR_VAL(haystack), found_offset);
  1623. } else {
  1624. RETVAL_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset);
  1625. }
  1626. } else {
  1627. RETVAL_FALSE;
  1628. }
  1629. efree(haystack_dup);
  1630. }
  1631. /* }}} */
  1632. /* {{{ proto string|false strstr(string haystack, string needle[, bool part])
  1633. Finds first occurrence of a string within another */
  1634. PHP_FUNCTION(strstr)
  1635. {
  1636. zend_string *haystack, *needle;
  1637. const char *found = NULL;
  1638. zend_long found_offset;
  1639. zend_bool part = 0;
  1640. ZEND_PARSE_PARAMETERS_START(2, 3)
  1641. Z_PARAM_STR(haystack)
  1642. Z_PARAM_STR(needle)
  1643. Z_PARAM_OPTIONAL
  1644. Z_PARAM_BOOL(part)
  1645. ZEND_PARSE_PARAMETERS_END();
  1646. found = php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
  1647. if (found) {
  1648. found_offset = found - ZSTR_VAL(haystack);
  1649. if (part) {
  1650. RETURN_STRINGL(ZSTR_VAL(haystack), found_offset);
  1651. } else {
  1652. RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
  1653. }
  1654. }
  1655. RETURN_FALSE;
  1656. }
  1657. /* }}} */
  1658. /* {{{ proto bool str_contains(string haystack, string needle)
  1659. Checks if a string contains another */
  1660. PHP_FUNCTION(str_contains)
  1661. {
  1662. zend_string *haystack, *needle;
  1663. ZEND_PARSE_PARAMETERS_START(2, 2)
  1664. Z_PARAM_STR(haystack)
  1665. Z_PARAM_STR(needle)
  1666. ZEND_PARSE_PARAMETERS_END();
  1667. RETURN_BOOL(php_memnstr(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack)));
  1668. }
  1669. /* }}} */
  1670. /* {{{ proto bool str_starts_with(string haystack, string needle)
  1671. Checks if haystack starts with needle */
  1672. PHP_FUNCTION(str_starts_with)
  1673. {
  1674. zend_string *haystack, *needle;
  1675. ZEND_PARSE_PARAMETERS_START(2, 2)
  1676. Z_PARAM_STR(haystack)
  1677. Z_PARAM_STR(needle)
  1678. ZEND_PARSE_PARAMETERS_END();
  1679. if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
  1680. RETURN_FALSE;
  1681. }
  1682. RETURN_BOOL(memcmp(ZSTR_VAL(haystack), ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0);
  1683. }
  1684. /* }}} */
  1685. /* {{{ proto bool str_ends_with(string haystack, string needle)
  1686. Checks if haystack ends with needle */
  1687. PHP_FUNCTION(str_ends_with)
  1688. {
  1689. zend_string *haystack, *needle;
  1690. ZEND_PARSE_PARAMETERS_START(2, 2)
  1691. Z_PARAM_STR(haystack)
  1692. Z_PARAM_STR(needle)
  1693. ZEND_PARSE_PARAMETERS_END();
  1694. if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
  1695. RETURN_FALSE;
  1696. }
  1697. RETURN_BOOL(memcmp(
  1698. ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - ZSTR_LEN(needle),
  1699. ZSTR_VAL(needle), ZSTR_LEN(needle)) == 0);
  1700. }
  1701. /* }}} */
  1702. /* {{{ proto string strchr(string haystack, string needle)
  1703. An alias for strstr */
  1704. /* }}} */
  1705. /* {{{ proto int|false strpos(string haystack, string needle [, int offset])
  1706. Finds position of first occurrence of a string within another */
  1707. PHP_FUNCTION(strpos)
  1708. {
  1709. zend_string *haystack, *needle;
  1710. const char *found = NULL;
  1711. zend_long offset = 0;
  1712. ZEND_PARSE_PARAMETERS_START(2, 3)
  1713. Z_PARAM_STR(haystack)
  1714. Z_PARAM_STR(needle)
  1715. Z_PARAM_OPTIONAL
  1716. Z_PARAM_LONG(offset)
  1717. ZEND_PARSE_PARAMETERS_END();
  1718. if (offset < 0) {
  1719. offset += (zend_long)ZSTR_LEN(haystack);
  1720. }
  1721. if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
  1722. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1723. RETURN_THROWS();
  1724. }
  1725. found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset,
  1726. ZSTR_VAL(needle), ZSTR_LEN(needle),
  1727. ZSTR_VAL(haystack) + ZSTR_LEN(haystack));
  1728. if (found) {
  1729. RETURN_LONG(found - ZSTR_VAL(haystack));
  1730. } else {
  1731. RETURN_FALSE;
  1732. }
  1733. }
  1734. /* }}} */
  1735. /* {{{ proto int|false stripos(string haystack, string needle [, int offset])
  1736. Finds position of first occurrence of a string within another, case insensitive */
  1737. PHP_FUNCTION(stripos)
  1738. {
  1739. const char *found = NULL;
  1740. zend_string *haystack, *needle;
  1741. zend_long offset = 0;
  1742. zend_string *needle_dup = NULL, *haystack_dup;
  1743. ZEND_PARSE_PARAMETERS_START(2, 3)
  1744. Z_PARAM_STR(haystack)
  1745. Z_PARAM_STR(needle)
  1746. Z_PARAM_OPTIONAL
  1747. Z_PARAM_LONG(offset)
  1748. ZEND_PARSE_PARAMETERS_END();
  1749. if (offset < 0) {
  1750. offset += (zend_long)ZSTR_LEN(haystack);
  1751. }
  1752. if (offset < 0 || (size_t)offset > ZSTR_LEN(haystack)) {
  1753. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1754. RETURN_THROWS();
  1755. }
  1756. if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
  1757. RETURN_FALSE;
  1758. }
  1759. haystack_dup = php_string_tolower(haystack);
  1760. needle_dup = php_string_tolower(needle);
  1761. found = (char*)php_memnstr(ZSTR_VAL(haystack_dup) + offset,
  1762. ZSTR_VAL(needle_dup), ZSTR_LEN(needle_dup), ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack));
  1763. if (found) {
  1764. RETVAL_LONG(found - ZSTR_VAL(haystack_dup));
  1765. } else {
  1766. RETVAL_FALSE;
  1767. }
  1768. zend_string_release_ex(haystack_dup, 0);
  1769. zend_string_release_ex(needle_dup, 0);
  1770. }
  1771. /* }}} */
  1772. /* {{{ proto int|false strrpos(string haystack, string needle [, int offset])
  1773. Finds position of last occurrence of a string within another string */
  1774. PHP_FUNCTION(strrpos)
  1775. {
  1776. zend_string *needle;
  1777. zend_string *haystack;
  1778. zend_long offset = 0;
  1779. const char *p, *e, *found;
  1780. ZEND_PARSE_PARAMETERS_START(2, 3)
  1781. Z_PARAM_STR(haystack)
  1782. Z_PARAM_STR(needle)
  1783. Z_PARAM_OPTIONAL
  1784. Z_PARAM_LONG(offset)
  1785. ZEND_PARSE_PARAMETERS_END();
  1786. if (offset >= 0) {
  1787. if ((size_t)offset > ZSTR_LEN(haystack)) {
  1788. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1789. RETURN_THROWS();
  1790. }
  1791. p = ZSTR_VAL(haystack) + (size_t)offset;
  1792. e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
  1793. } else {
  1794. if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
  1795. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1796. RETURN_THROWS();
  1797. }
  1798. p = ZSTR_VAL(haystack);
  1799. if ((size_t)-offset < ZSTR_LEN(needle)) {
  1800. e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
  1801. } else {
  1802. e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) + offset + ZSTR_LEN(needle);
  1803. }
  1804. }
  1805. if ((found = zend_memnrstr(p, ZSTR_VAL(needle), ZSTR_LEN(needle), e))) {
  1806. RETURN_LONG(found - ZSTR_VAL(haystack));
  1807. }
  1808. RETURN_FALSE;
  1809. }
  1810. /* }}} */
  1811. /* {{{ proto int|false strripos(string haystack, string needle [, int offset])
  1812. Finds position of last occurrence of a string within another string */
  1813. PHP_FUNCTION(strripos)
  1814. {
  1815. zend_string *needle;
  1816. zend_string *haystack;
  1817. zend_long offset = 0;
  1818. const char *p, *e, *found;
  1819. zend_string *needle_dup, *haystack_dup;
  1820. ZEND_PARSE_PARAMETERS_START(2, 3)
  1821. Z_PARAM_STR(haystack)
  1822. Z_PARAM_STR(needle)
  1823. Z_PARAM_OPTIONAL
  1824. Z_PARAM_LONG(offset)
  1825. ZEND_PARSE_PARAMETERS_END();
  1826. if (ZSTR_LEN(needle) == 1) {
  1827. /* Single character search can shortcut memcmps
  1828. Can also avoid tolower emallocs */
  1829. char lowered;
  1830. if (offset >= 0) {
  1831. if ((size_t)offset > ZSTR_LEN(haystack)) {
  1832. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1833. RETURN_THROWS();
  1834. }
  1835. p = ZSTR_VAL(haystack) + (size_t)offset;
  1836. e = ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - 1;
  1837. } else {
  1838. p = ZSTR_VAL(haystack);
  1839. if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
  1840. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1841. RETURN_THROWS();
  1842. }
  1843. e = ZSTR_VAL(haystack) + (ZSTR_LEN(haystack) + (size_t)offset);
  1844. }
  1845. /* Borrow that ord_needle buffer to avoid repeatedly tolower()ing needle */
  1846. lowered = tolower(*ZSTR_VAL(needle));
  1847. while (e >= p) {
  1848. if (tolower(*e) == lowered) {
  1849. RETURN_LONG(e - p + (offset > 0 ? offset : 0));
  1850. }
  1851. e--;
  1852. }
  1853. RETURN_FALSE;
  1854. }
  1855. haystack_dup = php_string_tolower(haystack);
  1856. if (offset >= 0) {
  1857. if ((size_t)offset > ZSTR_LEN(haystack)) {
  1858. zend_string_release_ex(haystack_dup, 0);
  1859. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1860. RETURN_THROWS();
  1861. }
  1862. p = ZSTR_VAL(haystack_dup) + offset;
  1863. e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack);
  1864. } else {
  1865. if (offset < -ZEND_LONG_MAX || (size_t)(-offset) > ZSTR_LEN(haystack)) {
  1866. zend_string_release_ex(haystack_dup, 0);
  1867. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  1868. RETURN_THROWS();
  1869. }
  1870. p = ZSTR_VAL(haystack_dup);
  1871. if ((size_t)-offset < ZSTR_LEN(needle)) {
  1872. e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack);
  1873. } else {
  1874. e = ZSTR_VAL(haystack_dup) + ZSTR_LEN(haystack) + offset + ZSTR_LEN(needle);
  1875. }
  1876. }
  1877. needle_dup = php_string_tolower(needle);
  1878. if ((found = (char *)zend_memnrstr(p, ZSTR_VAL(needle_dup), ZSTR_LEN(needle_dup), e))) {
  1879. RETVAL_LONG(found - ZSTR_VAL(haystack_dup));
  1880. zend_string_release_ex(needle_dup, 0);
  1881. zend_string_release_ex(haystack_dup, 0);
  1882. } else {
  1883. zend_string_release_ex(needle_dup, 0);
  1884. zend_string_release_ex(haystack_dup, 0);
  1885. RETURN_FALSE;
  1886. }
  1887. }
  1888. /* }}} */
  1889. /* {{{ proto string|false strrchr(string haystack, string needle)
  1890. Finds the last occurrence of a character in a string within another */
  1891. PHP_FUNCTION(strrchr)
  1892. {
  1893. zend_string *haystack, *needle;
  1894. const char *found = NULL;
  1895. zend_long found_offset;
  1896. ZEND_PARSE_PARAMETERS_START(2, 2)
  1897. Z_PARAM_STR(haystack)
  1898. Z_PARAM_STR(needle)
  1899. ZEND_PARSE_PARAMETERS_END();
  1900. found = zend_memrchr(ZSTR_VAL(haystack), *ZSTR_VAL(needle), ZSTR_LEN(haystack));
  1901. if (found) {
  1902. found_offset = found - ZSTR_VAL(haystack);
  1903. RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset);
  1904. } else {
  1905. RETURN_FALSE;
  1906. }
  1907. }
  1908. /* }}} */
  1909. /* {{{ php_chunk_split
  1910. */
  1911. static zend_string *php_chunk_split(const char *src, size_t srclen, const char *end, size_t endlen, size_t chunklen)
  1912. {
  1913. char *q;
  1914. const char *p;
  1915. size_t chunks;
  1916. size_t restlen;
  1917. zend_string *dest;
  1918. chunks = srclen / chunklen;
  1919. restlen = srclen - chunks * chunklen; /* srclen % chunklen */
  1920. if (restlen) {
  1921. /* We want chunks to be rounded up rather than rounded down.
  1922. * Increment can't overflow because chunks <= SIZE_MAX/2 at this point. */
  1923. chunks++;
  1924. }
  1925. dest = zend_string_safe_alloc(chunks, endlen, srclen, 0);
  1926. for (p = src, q = ZSTR_VAL(dest); p < (src + srclen - chunklen + 1); ) {
  1927. memcpy(q, p, chunklen);
  1928. q += chunklen;
  1929. memcpy(q, end, endlen);
  1930. q += endlen;
  1931. p += chunklen;
  1932. }
  1933. if (restlen) {
  1934. memcpy(q, p, restlen);
  1935. q += restlen;
  1936. memcpy(q, end, endlen);
  1937. q += endlen;
  1938. }
  1939. *q = '\0';
  1940. ZEND_ASSERT(q - ZSTR_VAL(dest) == ZSTR_LEN(dest));
  1941. return dest;
  1942. }
  1943. /* }}} */
  1944. /* {{{ proto string chunk_split(string str [, int chunklen [, string ending]])
  1945. Returns split line */
  1946. PHP_FUNCTION(chunk_split)
  1947. {
  1948. zend_string *str;
  1949. char *end = "\r\n";
  1950. size_t endlen = 2;
  1951. zend_long chunklen = 76;
  1952. zend_string *result;
  1953. ZEND_PARSE_PARAMETERS_START(1, 3)
  1954. Z_PARAM_STR(str)
  1955. Z_PARAM_OPTIONAL
  1956. Z_PARAM_LONG(chunklen)
  1957. Z_PARAM_STRING(end, endlen)
  1958. ZEND_PARSE_PARAMETERS_END();
  1959. if (chunklen <= 0) {
  1960. zend_argument_value_error(2, "must be greater than 0");
  1961. RETURN_THROWS();
  1962. }
  1963. if ((size_t)chunklen > ZSTR_LEN(str)) {
  1964. /* to maintain BC, we must return original string + ending */
  1965. result = zend_string_safe_alloc(ZSTR_LEN(str), 1, endlen, 0);
  1966. memcpy(ZSTR_VAL(result), ZSTR_VAL(str), ZSTR_LEN(str));
  1967. memcpy(ZSTR_VAL(result) + ZSTR_LEN(str), end, endlen);
  1968. ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';
  1969. RETURN_NEW_STR(result);
  1970. }
  1971. if (!ZSTR_LEN(str)) {
  1972. RETURN_EMPTY_STRING();
  1973. }
  1974. result = php_chunk_split(ZSTR_VAL(str), ZSTR_LEN(str), end, endlen, (size_t)chunklen);
  1975. RETURN_STR(result);
  1976. }
  1977. /* }}} */
  1978. /* {{{ proto string|false substr(string str, int start [, int length])
  1979. Returns part of a string */
  1980. PHP_FUNCTION(substr)
  1981. {
  1982. zend_string *str;
  1983. zend_long l = 0, f;
  1984. zend_bool len_is_null = 1;
  1985. ZEND_PARSE_PARAMETERS_START(2, 3)
  1986. Z_PARAM_STR(str)
  1987. Z_PARAM_LONG(f)
  1988. Z_PARAM_OPTIONAL
  1989. Z_PARAM_LONG_OR_NULL(l, len_is_null)
  1990. ZEND_PARSE_PARAMETERS_END();
  1991. if (f > (zend_long)ZSTR_LEN(str)) {
  1992. RETURN_FALSE;
  1993. } else if (f < 0) {
  1994. /* if "from" position is negative, count start position from the end
  1995. * of the string
  1996. */
  1997. if ((size_t)-f > ZSTR_LEN(str)) {
  1998. f = 0;
  1999. } else {
  2000. f = (zend_long)ZSTR_LEN(str) + f;
  2001. }
  2002. if (!len_is_null) {
  2003. if (l < 0) {
  2004. /* if "length" position is negative, set it to the length
  2005. * needed to stop that many chars from the end of the string
  2006. */
  2007. if ((size_t)(-l) > ZSTR_LEN(str) - (size_t)f) {
  2008. if ((size_t)(-l) > ZSTR_LEN(str)) {
  2009. RETURN_FALSE;
  2010. } else {
  2011. l = 0;
  2012. }
  2013. } else {
  2014. l = (zend_long)ZSTR_LEN(str) - f + l;
  2015. }
  2016. } else if ((size_t)l > ZSTR_LEN(str) - (size_t)f) {
  2017. goto truncate_len;
  2018. }
  2019. } else {
  2020. goto truncate_len;
  2021. }
  2022. } else if (!len_is_null) {
  2023. if (l < 0) {
  2024. /* if "length" position is negative, set it to the length
  2025. * needed to stop that many chars from the end of the string
  2026. */
  2027. if ((size_t)(-l) > ZSTR_LEN(str) - (size_t)f) {
  2028. RETURN_FALSE;
  2029. } else {
  2030. l = (zend_long)ZSTR_LEN(str) - f + l;
  2031. }
  2032. } else if ((size_t)l > ZSTR_LEN(str) - (size_t)f) {
  2033. goto truncate_len;
  2034. }
  2035. } else {
  2036. truncate_len:
  2037. l = (zend_long)ZSTR_LEN(str) - f;
  2038. }
  2039. if (l == 0) {
  2040. RETURN_EMPTY_STRING();
  2041. } else if (l == 1) {
  2042. RETURN_INTERNED_STR(ZSTR_CHAR((zend_uchar)(ZSTR_VAL(str)[f])));
  2043. } else if (l == ZSTR_LEN(str)) {
  2044. RETURN_STR_COPY(str);
  2045. }
  2046. RETURN_STRINGL(ZSTR_VAL(str) + f, l);
  2047. }
  2048. /* }}} */
  2049. /* {{{ proto string|array|false substr_replace(mixed str, mixed repl, mixed start [, mixed length])
  2050. Replaces part of a string with another string */
  2051. PHP_FUNCTION(substr_replace)
  2052. {
  2053. zend_string *str, *repl_str;
  2054. HashTable *str_ht, *repl_ht;
  2055. zval *from;
  2056. zval *len = NULL;
  2057. zend_long l = 0;
  2058. zend_long f;
  2059. int argc = ZEND_NUM_ARGS();
  2060. zend_string *result;
  2061. HashPosition from_idx, repl_idx, len_idx;
  2062. zval *tmp_str = NULL, *tmp_repl, *tmp_from = NULL, *tmp_len= NULL;
  2063. ZEND_PARSE_PARAMETERS_START(3, 4)
  2064. Z_PARAM_STR_OR_ARRAY_HT(str, str_ht)
  2065. Z_PARAM_STR_OR_ARRAY_HT(repl_str, repl_ht)
  2066. Z_PARAM_ZVAL(from)
  2067. Z_PARAM_OPTIONAL
  2068. Z_PARAM_ZVAL(len)
  2069. ZEND_PARSE_PARAMETERS_END();
  2070. if (Z_TYPE_P(from) != IS_ARRAY) {
  2071. convert_to_long_ex(from);
  2072. if (EG(exception)) {
  2073. RETURN_THROWS();
  2074. }
  2075. }
  2076. if (argc > 3) {
  2077. if (Z_TYPE_P(len) != IS_ARRAY) {
  2078. convert_to_long_ex(len);
  2079. l = Z_LVAL_P(len);
  2080. }
  2081. } else {
  2082. if (str) {
  2083. l = ZSTR_LEN(str);
  2084. }
  2085. }
  2086. if (str) {
  2087. if (
  2088. (argc == 3 && Z_TYPE_P(from) == IS_ARRAY) ||
  2089. (argc == 4 && Z_TYPE_P(from) != Z_TYPE_P(len))
  2090. ) {
  2091. php_error_docref(NULL, E_WARNING, "'start' and 'length' should be of same type - numerical or array ");
  2092. RETURN_STR_COPY(str);
  2093. }
  2094. if (argc == 4 && Z_TYPE_P(from) == IS_ARRAY) {
  2095. if (zend_hash_num_elements(Z_ARRVAL_P(from)) != zend_hash_num_elements(Z_ARRVAL_P(len))) {
  2096. php_error_docref(NULL, E_WARNING, "'start' and 'length' should have the same number of elements");
  2097. RETURN_STR_COPY(str);
  2098. }
  2099. }
  2100. }
  2101. if (str) {
  2102. if (Z_TYPE_P(from) != IS_ARRAY) {
  2103. f = Z_LVAL_P(from);
  2104. /* if "from" position is negative, count start position from the end
  2105. * of the string
  2106. */
  2107. if (f < 0) {
  2108. f = (zend_long)ZSTR_LEN(str) + f;
  2109. if (f < 0) {
  2110. f = 0;
  2111. }
  2112. } else if ((size_t)f > ZSTR_LEN(str)) {
  2113. f = ZSTR_LEN(str);
  2114. }
  2115. /* if "length" position is negative, set it to the length
  2116. * needed to stop that many chars from the end of the string
  2117. */
  2118. if (l < 0) {
  2119. l = ((zend_long)ZSTR_LEN(str) - f) + l;
  2120. if (l < 0) {
  2121. l = 0;
  2122. }
  2123. }
  2124. if ((size_t)l > ZSTR_LEN(str) || (l < 0 && (size_t)(-l) > ZSTR_LEN(str))) {
  2125. l = ZSTR_LEN(str);
  2126. }
  2127. if ((f + l) > (zend_long)ZSTR_LEN(str)) {
  2128. l = ZSTR_LEN(str) - f;
  2129. }
  2130. zend_string *tmp_repl_str = NULL;
  2131. if (repl_ht) {
  2132. repl_idx = 0;
  2133. while (repl_idx < repl_ht->nNumUsed) {
  2134. tmp_repl = &repl_ht->arData[repl_idx].val;
  2135. if (Z_TYPE_P(tmp_repl) != IS_UNDEF) {
  2136. break;
  2137. }
  2138. repl_idx++;
  2139. }
  2140. if (repl_idx < repl_ht->nNumUsed) {
  2141. repl_str = zval_get_tmp_string(tmp_repl, &tmp_repl_str);
  2142. } else {
  2143. repl_str = STR_EMPTY_ALLOC();
  2144. }
  2145. }
  2146. result = zend_string_safe_alloc(1, ZSTR_LEN(str) - l + ZSTR_LEN(repl_str), 0, 0);
  2147. memcpy(ZSTR_VAL(result), ZSTR_VAL(str), f);
  2148. if (ZSTR_LEN(repl_str)) {
  2149. memcpy((ZSTR_VAL(result) + f), ZSTR_VAL(repl_str), ZSTR_LEN(repl_str));
  2150. }
  2151. memcpy((ZSTR_VAL(result) + f + ZSTR_LEN(repl_str)), ZSTR_VAL(str) + f + l, ZSTR_LEN(str) - f - l);
  2152. ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';
  2153. zend_tmp_string_release(tmp_repl_str);
  2154. RETURN_NEW_STR(result);
  2155. } else {
  2156. php_error_docref(NULL, E_WARNING, "Functionality of 'start' and 'length' as arrays is not implemented");
  2157. RETURN_STR_COPY(str);
  2158. }
  2159. } else { /* str is array of strings */
  2160. zend_string *str_index = NULL;
  2161. size_t result_len;
  2162. zend_ulong num_index;
  2163. array_init(return_value);
  2164. from_idx = len_idx = repl_idx = 0;
  2165. ZEND_HASH_FOREACH_KEY_VAL(str_ht, num_index, str_index, tmp_str) {
  2166. zend_string *tmp_orig_str;
  2167. zend_string *orig_str = zval_get_tmp_string(tmp_str, &tmp_orig_str);
  2168. if (Z_TYPE_P(from) == IS_ARRAY) {
  2169. while (from_idx < Z_ARRVAL_P(from)->nNumUsed) {
  2170. tmp_from = &Z_ARRVAL_P(from)->arData[from_idx].val;
  2171. if (Z_TYPE_P(tmp_from) != IS_UNDEF) {
  2172. break;
  2173. }
  2174. from_idx++;
  2175. }
  2176. if (from_idx < Z_ARRVAL_P(from)->nNumUsed) {
  2177. f = zval_get_long(tmp_from);
  2178. if (f < 0) {
  2179. f = (zend_long)ZSTR_LEN(orig_str) + f;
  2180. if (f < 0) {
  2181. f = 0;
  2182. }
  2183. } else if (f > (zend_long)ZSTR_LEN(orig_str)) {
  2184. f = ZSTR_LEN(orig_str);
  2185. }
  2186. from_idx++;
  2187. } else {
  2188. f = 0;
  2189. }
  2190. } else {
  2191. f = Z_LVAL_P(from);
  2192. if (f < 0) {
  2193. f = (zend_long)ZSTR_LEN(orig_str) + f;
  2194. if (f < 0) {
  2195. f = 0;
  2196. }
  2197. } else if (f > (zend_long)ZSTR_LEN(orig_str)) {
  2198. f = ZSTR_LEN(orig_str);
  2199. }
  2200. }
  2201. if (argc > 3 && Z_TYPE_P(len) == IS_ARRAY) {
  2202. while (len_idx < Z_ARRVAL_P(len)->nNumUsed) {
  2203. tmp_len = &Z_ARRVAL_P(len)->arData[len_idx].val;
  2204. if (Z_TYPE_P(tmp_len) != IS_UNDEF) {
  2205. break;
  2206. }
  2207. len_idx++;
  2208. }
  2209. if (len_idx < Z_ARRVAL_P(len)->nNumUsed) {
  2210. l = zval_get_long(tmp_len);
  2211. len_idx++;
  2212. } else {
  2213. l = ZSTR_LEN(orig_str);
  2214. }
  2215. } else if (argc > 3) {
  2216. l = Z_LVAL_P(len);
  2217. } else {
  2218. l = ZSTR_LEN(orig_str);
  2219. }
  2220. if (l < 0) {
  2221. l = (ZSTR_LEN(orig_str) - f) + l;
  2222. if (l < 0) {
  2223. l = 0;
  2224. }
  2225. }
  2226. if ((f + l) > (zend_long)ZSTR_LEN(orig_str)) {
  2227. l = ZSTR_LEN(orig_str) - f;
  2228. }
  2229. result_len = ZSTR_LEN(orig_str) - l;
  2230. if (repl_ht) {
  2231. while (repl_idx < repl_ht->nNumUsed) {
  2232. tmp_repl = &repl_ht->arData[repl_idx].val;
  2233. if (repl_ht != IS_UNDEF) {
  2234. break;
  2235. }
  2236. repl_idx++;
  2237. }
  2238. if (repl_idx < repl_ht->nNumUsed) {
  2239. zend_string *tmp_repl_str;
  2240. zend_string *repl_str = zval_get_tmp_string(tmp_repl, &tmp_repl_str);
  2241. result_len += ZSTR_LEN(repl_str);
  2242. repl_idx++;
  2243. result = zend_string_safe_alloc(1, result_len, 0, 0);
  2244. memcpy(ZSTR_VAL(result), ZSTR_VAL(orig_str), f);
  2245. memcpy((ZSTR_VAL(result) + f), ZSTR_VAL(repl_str), ZSTR_LEN(repl_str));
  2246. memcpy((ZSTR_VAL(result) + f + ZSTR_LEN(repl_str)), ZSTR_VAL(orig_str) + f + l, ZSTR_LEN(orig_str) - f - l);
  2247. zend_tmp_string_release(tmp_repl_str);
  2248. } else {
  2249. result = zend_string_safe_alloc(1, result_len, 0, 0);
  2250. memcpy(ZSTR_VAL(result), ZSTR_VAL(orig_str), f);
  2251. memcpy((ZSTR_VAL(result) + f), ZSTR_VAL(orig_str) + f + l, ZSTR_LEN(orig_str) - f - l);
  2252. }
  2253. } else {
  2254. result_len += ZSTR_LEN(repl_str);
  2255. result = zend_string_safe_alloc(1, result_len, 0, 0);
  2256. memcpy(ZSTR_VAL(result), ZSTR_VAL(orig_str), f);
  2257. memcpy((ZSTR_VAL(result) + f), ZSTR_VAL(repl_str), ZSTR_LEN(repl_str));
  2258. memcpy((ZSTR_VAL(result) + f + ZSTR_LEN(repl_str)), ZSTR_VAL(orig_str) + f + l, ZSTR_LEN(orig_str) - f - l);
  2259. }
  2260. ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';
  2261. if (str_index) {
  2262. zval tmp;
  2263. ZVAL_NEW_STR(&tmp, result);
  2264. zend_symtable_update(Z_ARRVAL_P(return_value), str_index, &tmp);
  2265. } else {
  2266. add_index_str(return_value, num_index, result);
  2267. }
  2268. zend_tmp_string_release(tmp_orig_str);
  2269. } ZEND_HASH_FOREACH_END();
  2270. } /* if */
  2271. }
  2272. /* }}} */
  2273. /* {{{ proto string quotemeta(string str)
  2274. Quotes meta characters */
  2275. PHP_FUNCTION(quotemeta)
  2276. {
  2277. zend_string *old;
  2278. const char *old_end, *p;
  2279. char *q;
  2280. char c;
  2281. zend_string *str;
  2282. ZEND_PARSE_PARAMETERS_START(1, 1)
  2283. Z_PARAM_STR(old)
  2284. ZEND_PARSE_PARAMETERS_END();
  2285. old_end = ZSTR_VAL(old) + ZSTR_LEN(old);
  2286. if (ZSTR_LEN(old) == 0) {
  2287. RETURN_EMPTY_STRING();
  2288. }
  2289. str = zend_string_safe_alloc(2, ZSTR_LEN(old), 0, 0);
  2290. for (p = ZSTR_VAL(old), q = ZSTR_VAL(str); p != old_end; p++) {
  2291. c = *p;
  2292. switch (c) {
  2293. case '.':
  2294. case '\\':
  2295. case '+':
  2296. case '*':
  2297. case '?':
  2298. case '[':
  2299. case '^':
  2300. case ']':
  2301. case '$':
  2302. case '(':
  2303. case ')':
  2304. *q++ = '\\';
  2305. /* break is missing _intentionally_ */
  2306. default:
  2307. *q++ = c;
  2308. }
  2309. }
  2310. *q = '\0';
  2311. RETURN_NEW_STR(zend_string_truncate(str, q - ZSTR_VAL(str), 0));
  2312. }
  2313. /* }}} */
  2314. /* {{{ proto int ord(string character)
  2315. Returns ASCII value of character
  2316. Warning: This function is special-cased by zend_compile.c and so is bypassed for constant string argument */
  2317. PHP_FUNCTION(ord)
  2318. {
  2319. zend_string *str;
  2320. ZEND_PARSE_PARAMETERS_START(1, 1)
  2321. Z_PARAM_STR(str)
  2322. ZEND_PARSE_PARAMETERS_END();
  2323. RETURN_LONG((unsigned char) ZSTR_VAL(str)[0]);
  2324. }
  2325. /* }}} */
  2326. /* {{{ proto string chr(int ascii)
  2327. Converts ASCII code to a character
  2328. Warning: This function is special-cased by zend_compile.c and so is bypassed for constant integer argument */
  2329. PHP_FUNCTION(chr)
  2330. {
  2331. zend_long c;
  2332. ZEND_PARSE_PARAMETERS_START(1, 1)
  2333. Z_PARAM_LONG(c)
  2334. ZEND_PARSE_PARAMETERS_END();
  2335. c &= 0xff;
  2336. ZVAL_INTERNED_STR(return_value, ZSTR_CHAR(c));
  2337. }
  2338. /* }}} */
  2339. /* {{{ php_ucfirst
  2340. Uppercase the first character of the word in a native string */
  2341. static zend_string* php_ucfirst(zend_string *str)
  2342. {
  2343. unsigned char r = toupper(ZSTR_VAL(str)[0]);
  2344. if (r == ZSTR_VAL(str)[0]) {
  2345. return zend_string_copy(str);
  2346. } else {
  2347. zend_string *s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
  2348. ZSTR_VAL(s)[0] = r;
  2349. return s;
  2350. }
  2351. }
  2352. /* }}} */
  2353. /* {{{ proto string ucfirst(string str)
  2354. Makes a string's first character uppercase */
  2355. PHP_FUNCTION(ucfirst)
  2356. {
  2357. zend_string *str;
  2358. ZEND_PARSE_PARAMETERS_START(1, 1)
  2359. Z_PARAM_STR(str)
  2360. ZEND_PARSE_PARAMETERS_END();
  2361. if (!ZSTR_LEN(str)) {
  2362. RETURN_EMPTY_STRING();
  2363. }
  2364. RETURN_STR(php_ucfirst(str));
  2365. }
  2366. /* }}} */
  2367. /* {{{
  2368. Lowercase the first character of the word in a native string */
  2369. static zend_string* php_lcfirst(zend_string *str)
  2370. {
  2371. unsigned char r = tolower(ZSTR_VAL(str)[0]);
  2372. if (r == ZSTR_VAL(str)[0]) {
  2373. return zend_string_copy(str);
  2374. } else {
  2375. zend_string *s = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
  2376. ZSTR_VAL(s)[0] = r;
  2377. return s;
  2378. }
  2379. }
  2380. /* }}} */
  2381. /* {{{ proto string lcfirst(string str)
  2382. Make a string's first character lowercase */
  2383. PHP_FUNCTION(lcfirst)
  2384. {
  2385. zend_string *str;
  2386. ZEND_PARSE_PARAMETERS_START(1, 1)
  2387. Z_PARAM_STR(str)
  2388. ZEND_PARSE_PARAMETERS_END();
  2389. if (!ZSTR_LEN(str)) {
  2390. RETURN_EMPTY_STRING();
  2391. }
  2392. RETURN_STR(php_lcfirst(str));
  2393. }
  2394. /* }}} */
  2395. /* {{{ proto string ucwords(string str [, string delims])
  2396. Uppercase the first character of every word in a string */
  2397. PHP_FUNCTION(ucwords)
  2398. {
  2399. zend_string *str;
  2400. char *delims = " \t\r\n\f\v";
  2401. register char *r;
  2402. register const char *r_end;
  2403. size_t delims_len = 6;
  2404. char mask[256];
  2405. ZEND_PARSE_PARAMETERS_START(1, 2)
  2406. Z_PARAM_STR(str)
  2407. Z_PARAM_OPTIONAL
  2408. Z_PARAM_STRING(delims, delims_len)
  2409. ZEND_PARSE_PARAMETERS_END();
  2410. if (!ZSTR_LEN(str)) {
  2411. RETURN_EMPTY_STRING();
  2412. }
  2413. php_charmask((unsigned char *)delims, delims_len, mask);
  2414. ZVAL_STRINGL(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
  2415. r = Z_STRVAL_P(return_value);
  2416. *r = toupper((unsigned char) *r);
  2417. for (r_end = r + Z_STRLEN_P(return_value) - 1; r < r_end; ) {
  2418. if (mask[(unsigned char)*r++]) {
  2419. *r = toupper((unsigned char) *r);
  2420. }
  2421. }
  2422. }
  2423. /* }}} */
  2424. /* {{{ php_strtr
  2425. */
  2426. PHPAPI char *php_strtr(char *str, size_t len, const char *str_from, const char *str_to, size_t trlen)
  2427. {
  2428. size_t i;
  2429. if (UNEXPECTED(trlen < 1)) {
  2430. return str;
  2431. } else if (trlen == 1) {
  2432. char ch_from = *str_from;
  2433. char ch_to = *str_to;
  2434. for (i = 0; i < len; i++) {
  2435. if (str[i] == ch_from) {
  2436. str[i] = ch_to;
  2437. }
  2438. }
  2439. } else {
  2440. unsigned char xlat[256], j = 0;
  2441. do { xlat[j] = j; } while (++j != 0);
  2442. for (i = 0; i < trlen; i++) {
  2443. xlat[(size_t)(unsigned char) str_from[i]] = str_to[i];
  2444. }
  2445. for (i = 0; i < len; i++) {
  2446. str[i] = xlat[(size_t)(unsigned char) str[i]];
  2447. }
  2448. }
  2449. return str;
  2450. }
  2451. /* }}} */
  2452. /* {{{ php_strtr_ex
  2453. */
  2454. static zend_string *php_strtr_ex(zend_string *str, const char *str_from, const char *str_to, size_t trlen)
  2455. {
  2456. zend_string *new_str = NULL;
  2457. size_t i;
  2458. if (UNEXPECTED(trlen < 1)) {
  2459. return zend_string_copy(str);
  2460. } else if (trlen == 1) {
  2461. char ch_from = *str_from;
  2462. char ch_to = *str_to;
  2463. for (i = 0; i < ZSTR_LEN(str); i++) {
  2464. if (ZSTR_VAL(str)[i] == ch_from) {
  2465. new_str = zend_string_alloc(ZSTR_LEN(str), 0);
  2466. memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), i);
  2467. ZSTR_VAL(new_str)[i] = ch_to;
  2468. break;
  2469. }
  2470. }
  2471. for (; i < ZSTR_LEN(str); i++) {
  2472. ZSTR_VAL(new_str)[i] = (ZSTR_VAL(str)[i] != ch_from) ? ZSTR_VAL(str)[i] : ch_to;
  2473. }
  2474. } else {
  2475. unsigned char xlat[256], j = 0;
  2476. do { xlat[j] = j; } while (++j != 0);
  2477. for (i = 0; i < trlen; i++) {
  2478. xlat[(size_t)(unsigned char) str_from[i]] = str_to[i];
  2479. }
  2480. for (i = 0; i < ZSTR_LEN(str); i++) {
  2481. if (ZSTR_VAL(str)[i] != xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]]) {
  2482. new_str = zend_string_alloc(ZSTR_LEN(str), 0);
  2483. memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), i);
  2484. ZSTR_VAL(new_str)[i] = xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]];
  2485. break;
  2486. }
  2487. }
  2488. for (;i < ZSTR_LEN(str); i++) {
  2489. ZSTR_VAL(new_str)[i] = xlat[(size_t)(unsigned char) ZSTR_VAL(str)[i]];
  2490. }
  2491. }
  2492. if (!new_str) {
  2493. return zend_string_copy(str);
  2494. }
  2495. ZSTR_VAL(new_str)[ZSTR_LEN(new_str)] = 0;
  2496. return new_str;
  2497. }
  2498. /* }}} */
  2499. /* {{{ php_strtr_array */
  2500. static void php_strtr_array(zval *return_value, zend_string *input, HashTable *pats)
  2501. {
  2502. const char *str = ZSTR_VAL(input);
  2503. size_t slen = ZSTR_LEN(input);
  2504. zend_ulong num_key;
  2505. zend_string *str_key;
  2506. size_t len, pos, old_pos;
  2507. int num_keys = 0;
  2508. size_t minlen = 128*1024;
  2509. size_t maxlen = 0;
  2510. HashTable str_hash;
  2511. zval *entry;
  2512. const char *key;
  2513. smart_str result = {0};
  2514. zend_ulong bitset[256/sizeof(zend_ulong)];
  2515. zend_ulong *num_bitset;
  2516. /* we will collect all possible key lengths */
  2517. num_bitset = ecalloc((slen + sizeof(zend_ulong)) / sizeof(zend_ulong), sizeof(zend_ulong));
  2518. memset(bitset, 0, sizeof(bitset));
  2519. /* check if original array has numeric keys */
  2520. ZEND_HASH_FOREACH_STR_KEY(pats, str_key) {
  2521. if (UNEXPECTED(!str_key)) {
  2522. num_keys = 1;
  2523. } else {
  2524. len = ZSTR_LEN(str_key);
  2525. if (UNEXPECTED(len < 1)) {
  2526. php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
  2527. continue;
  2528. } else if (UNEXPECTED(len > slen)) {
  2529. /* skip long patterns */
  2530. continue;
  2531. }
  2532. if (len > maxlen) {
  2533. maxlen = len;
  2534. }
  2535. if (len < minlen) {
  2536. minlen = len;
  2537. }
  2538. /* remember possible key length */
  2539. num_bitset[len / sizeof(zend_ulong)] |= Z_UL(1) << (len % sizeof(zend_ulong));
  2540. bitset[((unsigned char)ZSTR_VAL(str_key)[0]) / sizeof(zend_ulong)] |= Z_UL(1) << (((unsigned char)ZSTR_VAL(str_key)[0]) % sizeof(zend_ulong));
  2541. }
  2542. } ZEND_HASH_FOREACH_END();
  2543. if (UNEXPECTED(num_keys)) {
  2544. zend_string *key_used;
  2545. /* we have to rebuild HashTable with numeric keys */
  2546. zend_hash_init(&str_hash, zend_hash_num_elements(pats), NULL, NULL, 0);
  2547. ZEND_HASH_FOREACH_KEY_VAL(pats, num_key, str_key, entry) {
  2548. if (UNEXPECTED(!str_key)) {
  2549. key_used = zend_long_to_str(num_key);
  2550. len = ZSTR_LEN(key_used);
  2551. if (UNEXPECTED(len > slen)) {
  2552. /* skip long patterns */
  2553. zend_string_release(key_used);
  2554. continue;
  2555. }
  2556. if (len > maxlen) {
  2557. maxlen = len;
  2558. }
  2559. if (len < minlen) {
  2560. minlen = len;
  2561. }
  2562. /* remember possible key length */
  2563. num_bitset[len / sizeof(zend_ulong)] |= Z_UL(1) << (len % sizeof(zend_ulong));
  2564. bitset[((unsigned char)ZSTR_VAL(key_used)[0]) / sizeof(zend_ulong)] |= Z_UL(1) << (((unsigned char)ZSTR_VAL(key_used)[0]) % sizeof(zend_ulong));
  2565. } else {
  2566. key_used = str_key;
  2567. len = ZSTR_LEN(key_used);
  2568. if (UNEXPECTED(len > slen)) {
  2569. /* skip long patterns */
  2570. continue;
  2571. }
  2572. }
  2573. zend_hash_add(&str_hash, key_used, entry);
  2574. if (UNEXPECTED(!str_key)) {
  2575. zend_string_release_ex(key_used, 0);
  2576. }
  2577. } ZEND_HASH_FOREACH_END();
  2578. pats = &str_hash;
  2579. }
  2580. if (UNEXPECTED(minlen > maxlen)) {
  2581. /* return the original string */
  2582. if (pats == &str_hash) {
  2583. zend_hash_destroy(&str_hash);
  2584. }
  2585. efree(num_bitset);
  2586. RETURN_STR_COPY(input);
  2587. }
  2588. old_pos = pos = 0;
  2589. while (pos <= slen - minlen) {
  2590. key = str + pos;
  2591. if (bitset[((unsigned char)key[0]) / sizeof(zend_ulong)] & (Z_UL(1) << (((unsigned char)key[0]) % sizeof(zend_ulong)))) {
  2592. len = maxlen;
  2593. if (len > slen - pos) {
  2594. len = slen - pos;
  2595. }
  2596. while (len >= minlen) {
  2597. if ((num_bitset[len / sizeof(zend_ulong)] & (Z_UL(1) << (len % sizeof(zend_ulong))))) {
  2598. entry = zend_hash_str_find(pats, key, len);
  2599. if (entry != NULL) {
  2600. zend_string *tmp;
  2601. zend_string *s = zval_get_tmp_string(entry, &tmp);
  2602. smart_str_appendl(&result, str + old_pos, pos - old_pos);
  2603. smart_str_append(&result, s);
  2604. old_pos = pos + len;
  2605. pos = old_pos - 1;
  2606. zend_tmp_string_release(tmp);
  2607. break;
  2608. }
  2609. }
  2610. len--;
  2611. }
  2612. }
  2613. pos++;
  2614. }
  2615. if (result.s) {
  2616. smart_str_appendl(&result, str + old_pos, slen - old_pos);
  2617. smart_str_0(&result);
  2618. RETVAL_NEW_STR(result.s);
  2619. } else {
  2620. smart_str_free(&result);
  2621. RETVAL_STR_COPY(input);
  2622. }
  2623. if (pats == &str_hash) {
  2624. zend_hash_destroy(&str_hash);
  2625. }
  2626. efree(num_bitset);
  2627. }
  2628. /* }}} */
  2629. /* {{{ php_char_to_str_ex
  2630. */
  2631. static zend_string* php_char_to_str_ex(zend_string *str, char from, char *to, size_t to_len, int case_sensitivity, zend_long *replace_count)
  2632. {
  2633. zend_string *result;
  2634. size_t char_count = 0;
  2635. char lc_from = 0;
  2636. const char *source, *source_end= ZSTR_VAL(str) + ZSTR_LEN(str);
  2637. char *target;
  2638. if (case_sensitivity) {
  2639. char *p = ZSTR_VAL(str), *e = p + ZSTR_LEN(str);
  2640. while ((p = memchr(p, from, (e - p)))) {
  2641. char_count++;
  2642. p++;
  2643. }
  2644. } else {
  2645. lc_from = tolower(from);
  2646. for (source = ZSTR_VAL(str); source < source_end; source++) {
  2647. if (tolower(*source) == lc_from) {
  2648. char_count++;
  2649. }
  2650. }
  2651. }
  2652. if (char_count == 0) {
  2653. return zend_string_copy(str);
  2654. }
  2655. if (to_len > 0) {
  2656. result = zend_string_safe_alloc(char_count, to_len - 1, ZSTR_LEN(str), 0);
  2657. } else {
  2658. result = zend_string_alloc(ZSTR_LEN(str) - char_count, 0);
  2659. }
  2660. target = ZSTR_VAL(result);
  2661. if (case_sensitivity) {
  2662. char *p = ZSTR_VAL(str), *e = p + ZSTR_LEN(str), *s = ZSTR_VAL(str);
  2663. while ((p = memchr(p, from, (e - p)))) {
  2664. memcpy(target, s, (p - s));
  2665. target += p - s;
  2666. memcpy(target, to, to_len);
  2667. target += to_len;
  2668. p++;
  2669. s = p;
  2670. if (replace_count) {
  2671. *replace_count += 1;
  2672. }
  2673. }
  2674. if (s < e) {
  2675. memcpy(target, s, (e - s));
  2676. target += e - s;
  2677. }
  2678. } else {
  2679. for (source = ZSTR_VAL(str); source < source_end; source++) {
  2680. if (tolower(*source) == lc_from) {
  2681. if (replace_count) {
  2682. *replace_count += 1;
  2683. }
  2684. memcpy(target, to, to_len);
  2685. target += to_len;
  2686. } else {
  2687. *target = *source;
  2688. target++;
  2689. }
  2690. }
  2691. }
  2692. *target = 0;
  2693. return result;
  2694. }
  2695. /* }}} */
  2696. /* {{{ php_str_to_str_ex
  2697. */
  2698. static zend_string *php_str_to_str_ex(zend_string *haystack,
  2699. const char *needle, size_t needle_len, const char *str, size_t str_len, zend_long *replace_count)
  2700. {
  2701. zend_string *new_str;
  2702. if (needle_len < ZSTR_LEN(haystack)) {
  2703. const char *end;
  2704. const char *p, *r;
  2705. char *e;
  2706. if (needle_len == str_len) {
  2707. new_str = NULL;
  2708. end = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
  2709. for (p = ZSTR_VAL(haystack); (r = (char*)php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
  2710. if (!new_str) {
  2711. new_str = zend_string_init(ZSTR_VAL(haystack), ZSTR_LEN(haystack), 0);
  2712. }
  2713. memcpy(ZSTR_VAL(new_str) + (r - ZSTR_VAL(haystack)), str, str_len);
  2714. (*replace_count)++;
  2715. }
  2716. if (!new_str) {
  2717. goto nothing_todo;
  2718. }
  2719. return new_str;
  2720. } else {
  2721. size_t count = 0;
  2722. const char *o = ZSTR_VAL(haystack);
  2723. const char *n = needle;
  2724. const char *endp = o + ZSTR_LEN(haystack);
  2725. while ((o = (char*)php_memnstr(o, n, needle_len, endp))) {
  2726. o += needle_len;
  2727. count++;
  2728. }
  2729. if (count == 0) {
  2730. /* Needle doesn't occur, shortcircuit the actual replacement. */
  2731. goto nothing_todo;
  2732. }
  2733. if (str_len > needle_len) {
  2734. new_str = zend_string_safe_alloc(count, str_len - needle_len, ZSTR_LEN(haystack), 0);
  2735. } else {
  2736. new_str = zend_string_alloc(count * (str_len - needle_len) + ZSTR_LEN(haystack), 0);
  2737. }
  2738. e = ZSTR_VAL(new_str);
  2739. end = ZSTR_VAL(haystack) + ZSTR_LEN(haystack);
  2740. for (p = ZSTR_VAL(haystack); (r = (char*)php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
  2741. memcpy(e, p, r - p);
  2742. e += r - p;
  2743. memcpy(e, str, str_len);
  2744. e += str_len;
  2745. (*replace_count)++;
  2746. }
  2747. if (p < end) {
  2748. memcpy(e, p, end - p);
  2749. e += end - p;
  2750. }
  2751. *e = '\0';
  2752. return new_str;
  2753. }
  2754. } else if (needle_len > ZSTR_LEN(haystack) || memcmp(ZSTR_VAL(haystack), needle, ZSTR_LEN(haystack))) {
  2755. nothing_todo:
  2756. return zend_string_copy(haystack);
  2757. } else {
  2758. if (str_len == 0) {
  2759. new_str = ZSTR_EMPTY_ALLOC();
  2760. } else if (str_len == 1) {
  2761. new_str = ZSTR_CHAR((zend_uchar)(*str));
  2762. } else {
  2763. new_str = zend_string_init(str, str_len, 0);
  2764. }
  2765. (*replace_count)++;
  2766. return new_str;
  2767. }
  2768. }
  2769. /* }}} */
  2770. /* {{{ php_str_to_str_i_ex
  2771. */
  2772. static zend_string *php_str_to_str_i_ex(zend_string *haystack, const char *lc_haystack,
  2773. zend_string *needle, const char *str, size_t str_len, zend_long *replace_count)
  2774. {
  2775. zend_string *new_str = NULL;
  2776. zend_string *lc_needle;
  2777. if (ZSTR_LEN(needle) < ZSTR_LEN(haystack)) {
  2778. const char *end;
  2779. const char *p, *r;
  2780. char *e;
  2781. if (ZSTR_LEN(needle) == str_len) {
  2782. lc_needle = php_string_tolower(needle);
  2783. end = lc_haystack + ZSTR_LEN(haystack);
  2784. for (p = lc_haystack; (r = (char*)php_memnstr(p, ZSTR_VAL(lc_needle), ZSTR_LEN(lc_needle), end)); p = r + ZSTR_LEN(lc_needle)) {
  2785. if (!new_str) {
  2786. new_str = zend_string_init(ZSTR_VAL(haystack), ZSTR_LEN(haystack), 0);
  2787. }
  2788. memcpy(ZSTR_VAL(new_str) + (r - lc_haystack), str, str_len);
  2789. (*replace_count)++;
  2790. }
  2791. zend_string_release_ex(lc_needle, 0);
  2792. if (!new_str) {
  2793. goto nothing_todo;
  2794. }
  2795. return new_str;
  2796. } else {
  2797. size_t count = 0;
  2798. const char *o = lc_haystack;
  2799. const char *n;
  2800. const char *endp = o + ZSTR_LEN(haystack);
  2801. lc_needle = php_string_tolower(needle);
  2802. n = ZSTR_VAL(lc_needle);
  2803. while ((o = (char*)php_memnstr(o, n, ZSTR_LEN(lc_needle), endp))) {
  2804. o += ZSTR_LEN(lc_needle);
  2805. count++;
  2806. }
  2807. if (count == 0) {
  2808. /* Needle doesn't occur, shortcircuit the actual replacement. */
  2809. zend_string_release_ex(lc_needle, 0);
  2810. goto nothing_todo;
  2811. }
  2812. if (str_len > ZSTR_LEN(lc_needle)) {
  2813. new_str = zend_string_safe_alloc(count, str_len - ZSTR_LEN(lc_needle), ZSTR_LEN(haystack), 0);
  2814. } else {
  2815. new_str = zend_string_alloc(count * (str_len - ZSTR_LEN(lc_needle)) + ZSTR_LEN(haystack), 0);
  2816. }
  2817. e = ZSTR_VAL(new_str);
  2818. end = lc_haystack + ZSTR_LEN(haystack);
  2819. for (p = lc_haystack; (r = (char*)php_memnstr(p, ZSTR_VAL(lc_needle), ZSTR_LEN(lc_needle), end)); p = r + ZSTR_LEN(lc_needle)) {
  2820. memcpy(e, ZSTR_VAL(haystack) + (p - lc_haystack), r - p);
  2821. e += r - p;
  2822. memcpy(e, str, str_len);
  2823. e += str_len;
  2824. (*replace_count)++;
  2825. }
  2826. if (p < end) {
  2827. memcpy(e, ZSTR_VAL(haystack) + (p - lc_haystack), end - p);
  2828. e += end - p;
  2829. }
  2830. *e = '\0';
  2831. zend_string_release_ex(lc_needle, 0);
  2832. return new_str;
  2833. }
  2834. } else if (ZSTR_LEN(needle) > ZSTR_LEN(haystack)) {
  2835. nothing_todo:
  2836. return zend_string_copy(haystack);
  2837. } else {
  2838. lc_needle = php_string_tolower(needle);
  2839. if (memcmp(lc_haystack, ZSTR_VAL(lc_needle), ZSTR_LEN(lc_needle))) {
  2840. zend_string_release_ex(lc_needle, 0);
  2841. goto nothing_todo;
  2842. }
  2843. zend_string_release_ex(lc_needle, 0);
  2844. new_str = zend_string_init(str, str_len, 0);
  2845. (*replace_count)++;
  2846. return new_str;
  2847. }
  2848. }
  2849. /* }}} */
  2850. /* {{{ php_str_to_str
  2851. */
  2852. PHPAPI zend_string *php_str_to_str(const char *haystack, size_t length, const char *needle, size_t needle_len, const char *str, size_t str_len)
  2853. {
  2854. zend_string *new_str;
  2855. if (needle_len < length) {
  2856. const char *end;
  2857. const char *s, *p;
  2858. char *e, *r;
  2859. if (needle_len == str_len) {
  2860. new_str = zend_string_init(haystack, length, 0);
  2861. end = ZSTR_VAL(new_str) + length;
  2862. for (p = ZSTR_VAL(new_str); (r = (char*)php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
  2863. memcpy(r, str, str_len);
  2864. }
  2865. return new_str;
  2866. } else {
  2867. if (str_len < needle_len) {
  2868. new_str = zend_string_alloc(length, 0);
  2869. } else {
  2870. size_t count = 0;
  2871. const char *o = haystack;
  2872. const char *n = needle;
  2873. const char *endp = o + length;
  2874. while ((o = (char*)php_memnstr(o, n, needle_len, endp))) {
  2875. o += needle_len;
  2876. count++;
  2877. }
  2878. if (count == 0) {
  2879. /* Needle doesn't occur, shortcircuit the actual replacement. */
  2880. new_str = zend_string_init(haystack, length, 0);
  2881. return new_str;
  2882. } else {
  2883. if (str_len > needle_len) {
  2884. new_str = zend_string_safe_alloc(count, str_len - needle_len, length, 0);
  2885. } else {
  2886. new_str = zend_string_alloc(count * (str_len - needle_len) + length, 0);
  2887. }
  2888. }
  2889. }
  2890. s = e = ZSTR_VAL(new_str);
  2891. end = haystack + length;
  2892. for (p = haystack; (r = (char*)php_memnstr(p, needle, needle_len, end)); p = r + needle_len) {
  2893. memcpy(e, p, r - p);
  2894. e += r - p;
  2895. memcpy(e, str, str_len);
  2896. e += str_len;
  2897. }
  2898. if (p < end) {
  2899. memcpy(e, p, end - p);
  2900. e += end - p;
  2901. }
  2902. *e = '\0';
  2903. new_str = zend_string_truncate(new_str, e - s, 0);
  2904. return new_str;
  2905. }
  2906. } else if (needle_len > length || memcmp(haystack, needle, length)) {
  2907. new_str = zend_string_init(haystack, length, 0);
  2908. return new_str;
  2909. } else {
  2910. new_str = zend_string_init(str, str_len, 0);
  2911. return new_str;
  2912. }
  2913. }
  2914. /* }}} */
  2915. /* {{{ proto string|false strtr(string str, string from[, string to])
  2916. Translates characters in str using given translation tables */
  2917. PHP_FUNCTION(strtr)
  2918. {
  2919. zend_string *str, *from_str = NULL;
  2920. HashTable *from_ht = NULL;
  2921. char *to = NULL;
  2922. size_t to_len = 0;
  2923. int ac = ZEND_NUM_ARGS();
  2924. ZEND_PARSE_PARAMETERS_START(2, 3)
  2925. Z_PARAM_STR(str)
  2926. Z_PARAM_STR_OR_ARRAY_HT(from_str, from_ht)
  2927. Z_PARAM_OPTIONAL
  2928. Z_PARAM_STRING(to, to_len)
  2929. ZEND_PARSE_PARAMETERS_END();
  2930. if (ac == 2 && from_ht == NULL) {
  2931. zend_argument_type_error(2, "must be of type array, string given");
  2932. RETURN_THROWS();
  2933. } else if (ac != 2 && from_str == NULL) {
  2934. zend_argument_type_error(2, "must be of type string, array given");
  2935. RETURN_THROWS();
  2936. }
  2937. /* shortcut for empty string */
  2938. if (ZSTR_LEN(str) == 0) {
  2939. RETURN_EMPTY_STRING();
  2940. }
  2941. if (ac == 2) {
  2942. if (zend_hash_num_elements(from_ht) < 1) {
  2943. RETURN_STR_COPY(str);
  2944. } else if (zend_hash_num_elements(from_ht) == 1) {
  2945. zend_long num_key;
  2946. zend_string *str_key, *tmp_str, *replace, *tmp_replace;
  2947. zval *entry;
  2948. ZEND_HASH_FOREACH_KEY_VAL(from_ht, num_key, str_key, entry) {
  2949. tmp_str = NULL;
  2950. if (UNEXPECTED(!str_key)) {
  2951. str_key = tmp_str = zend_long_to_str(num_key);
  2952. }
  2953. replace = zval_get_tmp_string(entry, &tmp_replace);
  2954. if (ZSTR_LEN(str_key) < 1) {
  2955. php_error_docref(NULL, E_WARNING, "Ignoring replacement of empty string");
  2956. RETVAL_STR_COPY(str);
  2957. } else if (ZSTR_LEN(str_key) == 1) {
  2958. RETVAL_STR(php_char_to_str_ex(str,
  2959. ZSTR_VAL(str_key)[0],
  2960. ZSTR_VAL(replace),
  2961. ZSTR_LEN(replace),
  2962. 1,
  2963. NULL));
  2964. } else {
  2965. zend_long dummy;
  2966. RETVAL_STR(php_str_to_str_ex(str,
  2967. ZSTR_VAL(str_key), ZSTR_LEN(str_key),
  2968. ZSTR_VAL(replace), ZSTR_LEN(replace), &dummy));
  2969. }
  2970. zend_tmp_string_release(tmp_str);
  2971. zend_tmp_string_release(tmp_replace);
  2972. return;
  2973. } ZEND_HASH_FOREACH_END();
  2974. } else {
  2975. php_strtr_array(return_value, str, from_ht);
  2976. }
  2977. } else {
  2978. RETURN_STR(php_strtr_ex(str,
  2979. ZSTR_VAL(from_str),
  2980. to,
  2981. MIN(ZSTR_LEN(from_str), to_len)));
  2982. }
  2983. }
  2984. /* }}} */
  2985. /* {{{ proto string strrev(string str)
  2986. Reverse a string */
  2987. #if ZEND_INTRIN_SSSE3_NATIVE
  2988. #include <tmmintrin.h>
  2989. #elif defined(__aarch64__)
  2990. #include <arm_neon.h>
  2991. #endif
  2992. PHP_FUNCTION(strrev)
  2993. {
  2994. zend_string *str;
  2995. const char *s, *e;
  2996. char *p;
  2997. zend_string *n;
  2998. ZEND_PARSE_PARAMETERS_START(1, 1)
  2999. Z_PARAM_STR(str)
  3000. ZEND_PARSE_PARAMETERS_END();
  3001. n = zend_string_alloc(ZSTR_LEN(str), 0);
  3002. p = ZSTR_VAL(n);
  3003. s = ZSTR_VAL(str);
  3004. e = s + ZSTR_LEN(str);
  3005. --e;
  3006. #if ZEND_INTRIN_SSSE3_NATIVE
  3007. if (e - s > 15) {
  3008. const __m128i map = _mm_set_epi8(
  3009. 0, 1, 2, 3,
  3010. 4, 5, 6, 7,
  3011. 8, 9, 10, 11,
  3012. 12, 13, 14, 15);
  3013. do {
  3014. const __m128i str = _mm_loadu_si128((__m128i *)(e - 15));
  3015. _mm_storeu_si128((__m128i *)p, _mm_shuffle_epi8(str, map));
  3016. p += 16;
  3017. e -= 16;
  3018. } while (e - s > 15);
  3019. }
  3020. #elif defined(__aarch64__)
  3021. if (e - s > 15) {
  3022. do {
  3023. const uint8x16_t str = vld1q_u8((uint8_t *)(e - 15));
  3024. /* Synthesize rev128 with a rev64 + ext. */
  3025. const uint8x16_t rev = vrev64q_u8(str);
  3026. const uint8x16_t ext = (uint8x16_t)
  3027. vextq_u64((uint64x2_t)rev, (uint64x2_t)rev, 1);
  3028. vst1q_u8((uint8_t *)p, ext);
  3029. p += 16;
  3030. e -= 16;
  3031. } while (e - s > 15);
  3032. }
  3033. #endif
  3034. while (e >= s) {
  3035. *p++ = *e--;
  3036. }
  3037. *p = '\0';
  3038. RETVAL_NEW_STR(n);
  3039. }
  3040. /* }}} */
  3041. /* {{{ php_similar_str
  3042. */
  3043. static void php_similar_str(const char *txt1, size_t len1, const char *txt2, size_t len2, size_t *pos1, size_t *pos2, size_t *max, size_t *count)
  3044. {
  3045. const char *p, *q;
  3046. const char *end1 = (char *) txt1 + len1;
  3047. const char *end2 = (char *) txt2 + len2;
  3048. size_t l;
  3049. *max = 0;
  3050. *count = 0;
  3051. for (p = (char *) txt1; p < end1; p++) {
  3052. for (q = (char *) txt2; q < end2; q++) {
  3053. for (l = 0; (p + l < end1) && (q + l < end2) && (p[l] == q[l]); l++);
  3054. if (l > *max) {
  3055. *max = l;
  3056. *count += 1;
  3057. *pos1 = p - txt1;
  3058. *pos2 = q - txt2;
  3059. }
  3060. }
  3061. }
  3062. }
  3063. /* }}} */
  3064. /* {{{ php_similar_char
  3065. */
  3066. static size_t php_similar_char(const char *txt1, size_t len1, const char *txt2, size_t len2)
  3067. {
  3068. size_t sum;
  3069. size_t pos1 = 0, pos2 = 0, max, count;
  3070. php_similar_str(txt1, len1, txt2, len2, &pos1, &pos2, &max, &count);
  3071. if ((sum = max)) {
  3072. if (pos1 && pos2 && count > 1) {
  3073. sum += php_similar_char(txt1, pos1,
  3074. txt2, pos2);
  3075. }
  3076. if ((pos1 + max < len1) && (pos2 + max < len2)) {
  3077. sum += php_similar_char(txt1 + pos1 + max, len1 - pos1 - max,
  3078. txt2 + pos2 + max, len2 - pos2 - max);
  3079. }
  3080. }
  3081. return sum;
  3082. }
  3083. /* }}} */
  3084. /* {{{ proto int similar_text(string str1, string str2 [, float percent])
  3085. Calculates the similarity between two strings */
  3086. PHP_FUNCTION(similar_text)
  3087. {
  3088. zend_string *t1, *t2;
  3089. zval *percent = NULL;
  3090. int ac = ZEND_NUM_ARGS();
  3091. size_t sim;
  3092. ZEND_PARSE_PARAMETERS_START(2, 3)
  3093. Z_PARAM_STR(t1)
  3094. Z_PARAM_STR(t2)
  3095. Z_PARAM_OPTIONAL
  3096. Z_PARAM_ZVAL(percent)
  3097. ZEND_PARSE_PARAMETERS_END();
  3098. if (ZSTR_LEN(t1) + ZSTR_LEN(t2) == 0) {
  3099. if (ac > 2) {
  3100. ZEND_TRY_ASSIGN_REF_DOUBLE(percent, 0);
  3101. }
  3102. RETURN_LONG(0);
  3103. }
  3104. sim = php_similar_char(ZSTR_VAL(t1), ZSTR_LEN(t1), ZSTR_VAL(t2), ZSTR_LEN(t2));
  3105. if (ac > 2) {
  3106. ZEND_TRY_ASSIGN_REF_DOUBLE(percent, sim * 200.0 / (ZSTR_LEN(t1) + ZSTR_LEN(t2)));
  3107. }
  3108. RETURN_LONG(sim);
  3109. }
  3110. /* }}} */
  3111. /* {{{ proto string addcslashes(string str, string charlist)
  3112. Escapes all chars mentioned in charlist with backslash. It creates octal representations if asked to backslash characters with 8th bit set or with ASCII<32 (except '\n', '\r', '\t' etc...) */
  3113. PHP_FUNCTION(addcslashes)
  3114. {
  3115. zend_string *str, *what;
  3116. ZEND_PARSE_PARAMETERS_START(2, 2)
  3117. Z_PARAM_STR(str)
  3118. Z_PARAM_STR(what)
  3119. ZEND_PARSE_PARAMETERS_END();
  3120. if (ZSTR_LEN(str) == 0) {
  3121. RETURN_EMPTY_STRING();
  3122. }
  3123. if (ZSTR_LEN(what) == 0) {
  3124. RETURN_STR_COPY(str);
  3125. }
  3126. RETURN_STR(php_addcslashes_str(ZSTR_VAL(str), ZSTR_LEN(str), ZSTR_VAL(what), ZSTR_LEN(what)));
  3127. }
  3128. /* }}} */
  3129. /* {{{ proto string addslashes(string str)
  3130. Escapes single quote, double quotes and backslash characters in a string with backslashes */
  3131. PHP_FUNCTION(addslashes)
  3132. {
  3133. zend_string *str;
  3134. ZEND_PARSE_PARAMETERS_START(1, 1)
  3135. Z_PARAM_STR(str)
  3136. ZEND_PARSE_PARAMETERS_END();
  3137. if (ZSTR_LEN(str) == 0) {
  3138. RETURN_EMPTY_STRING();
  3139. }
  3140. RETURN_STR(php_addslashes(str));
  3141. }
  3142. /* }}} */
  3143. /* {{{ proto string stripcslashes(string str)
  3144. Strips backslashes from a string. Uses C-style conventions */
  3145. PHP_FUNCTION(stripcslashes)
  3146. {
  3147. zend_string *str;
  3148. ZEND_PARSE_PARAMETERS_START(1, 1)
  3149. Z_PARAM_STR(str)
  3150. ZEND_PARSE_PARAMETERS_END();
  3151. ZVAL_STRINGL(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
  3152. php_stripcslashes(Z_STR_P(return_value));
  3153. }
  3154. /* }}} */
  3155. /* {{{ proto string stripslashes(string str)
  3156. Strips backslashes from a string */
  3157. PHP_FUNCTION(stripslashes)
  3158. {
  3159. zend_string *str;
  3160. ZEND_PARSE_PARAMETERS_START(1, 1)
  3161. Z_PARAM_STR(str)
  3162. ZEND_PARSE_PARAMETERS_END();
  3163. ZVAL_STRINGL(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
  3164. php_stripslashes(Z_STR_P(return_value));
  3165. }
  3166. /* }}} */
  3167. /* {{{ php_stripcslashes
  3168. */
  3169. PHPAPI void php_stripcslashes(zend_string *str)
  3170. {
  3171. const char *source, *end;
  3172. char *target;
  3173. size_t nlen = ZSTR_LEN(str), i;
  3174. char numtmp[4];
  3175. for (source = (char*)ZSTR_VAL(str), end = source + ZSTR_LEN(str), target = ZSTR_VAL(str); source < end; source++) {
  3176. if (*source == '\\' && source + 1 < end) {
  3177. source++;
  3178. switch (*source) {
  3179. case 'n': *target++='\n'; nlen--; break;
  3180. case 'r': *target++='\r'; nlen--; break;
  3181. case 'a': *target++='\a'; nlen--; break;
  3182. case 't': *target++='\t'; nlen--; break;
  3183. case 'v': *target++='\v'; nlen--; break;
  3184. case 'b': *target++='\b'; nlen--; break;
  3185. case 'f': *target++='\f'; nlen--; break;
  3186. case '\\': *target++='\\'; nlen--; break;
  3187. case 'x':
  3188. if (source+1 < end && isxdigit((int)(*(source+1)))) {
  3189. numtmp[0] = *++source;
  3190. if (source+1 < end && isxdigit((int)(*(source+1)))) {
  3191. numtmp[1] = *++source;
  3192. numtmp[2] = '\0';
  3193. nlen-=3;
  3194. } else {
  3195. numtmp[1] = '\0';
  3196. nlen-=2;
  3197. }
  3198. *target++=(char)strtol(numtmp, NULL, 16);
  3199. break;
  3200. }
  3201. /* break is left intentionally */
  3202. default:
  3203. i=0;
  3204. while (source < end && *source >= '0' && *source <= '7' && i<3) {
  3205. numtmp[i++] = *source++;
  3206. }
  3207. if (i) {
  3208. numtmp[i]='\0';
  3209. *target++=(char)strtol(numtmp, NULL, 8);
  3210. nlen-=i;
  3211. source--;
  3212. } else {
  3213. *target++=*source;
  3214. nlen--;
  3215. }
  3216. }
  3217. } else {
  3218. *target++=*source;
  3219. }
  3220. }
  3221. if (nlen != 0) {
  3222. *target='\0';
  3223. }
  3224. ZSTR_LEN(str) = nlen;
  3225. }
  3226. /* }}} */
  3227. /* {{{ php_addcslashes_str
  3228. */
  3229. PHPAPI zend_string *php_addcslashes_str(const char *str, size_t len, char *what, size_t wlength)
  3230. {
  3231. char flags[256];
  3232. char *target;
  3233. const char *source, *end;
  3234. char c;
  3235. size_t newlen;
  3236. zend_string *new_str = zend_string_safe_alloc(4, len, 0, 0);
  3237. php_charmask((unsigned char *)what, wlength, flags);
  3238. for (source = str, end = source + len, target = ZSTR_VAL(new_str); source < end; source++) {
  3239. c = *source;
  3240. if (flags[(unsigned char)c]) {
  3241. if ((unsigned char) c < 32 || (unsigned char) c > 126) {
  3242. *target++ = '\\';
  3243. switch (c) {
  3244. case '\n': *target++ = 'n'; break;
  3245. case '\t': *target++ = 't'; break;
  3246. case '\r': *target++ = 'r'; break;
  3247. case '\a': *target++ = 'a'; break;
  3248. case '\v': *target++ = 'v'; break;
  3249. case '\b': *target++ = 'b'; break;
  3250. case '\f': *target++ = 'f'; break;
  3251. default: target += sprintf(target, "%03o", (unsigned char) c);
  3252. }
  3253. continue;
  3254. }
  3255. *target++ = '\\';
  3256. }
  3257. *target++ = c;
  3258. }
  3259. *target = 0;
  3260. newlen = target - ZSTR_VAL(new_str);
  3261. if (newlen < len * 4) {
  3262. new_str = zend_string_truncate(new_str, newlen, 0);
  3263. }
  3264. return new_str;
  3265. }
  3266. /* }}} */
  3267. /* {{{ php_addcslashes
  3268. */
  3269. PHPAPI zend_string *php_addcslashes(zend_string *str, char *what, size_t wlength)
  3270. {
  3271. return php_addcslashes_str(ZSTR_VAL(str), ZSTR_LEN(str), what, wlength);
  3272. }
  3273. /* }}} */
  3274. /* {{{ php_addslashes */
  3275. #if ZEND_INTRIN_SSE4_2_NATIVE
  3276. # include <nmmintrin.h>
  3277. # include "Zend/zend_bitset.h"
  3278. #elif ZEND_INTRIN_SSE4_2_RESOLVER
  3279. # include <nmmintrin.h>
  3280. # include "Zend/zend_bitset.h"
  3281. # include "Zend/zend_cpuinfo.h"
  3282. ZEND_INTRIN_SSE4_2_FUNC_DECL(zend_string *php_addslashes_sse42(zend_string *str));
  3283. zend_string *php_addslashes_default(zend_string *str);
  3284. ZEND_INTRIN_SSE4_2_FUNC_DECL(void php_stripslashes_sse42(zend_string *str));
  3285. void php_stripslashes_default(zend_string *str);
  3286. # if ZEND_INTRIN_SSE4_2_FUNC_PROTO
  3287. PHPAPI zend_string *php_addslashes(zend_string *str) __attribute__((ifunc("resolve_addslashes")));
  3288. PHPAPI void php_stripslashes(zend_string *str) __attribute__((ifunc("resolve_stripslashes")));
  3289. typedef zend_string *(*php_addslashes_func_t)(zend_string *);
  3290. typedef void (*php_stripslashes_func_t)(zend_string *);
  3291. ZEND_NO_SANITIZE_ADDRESS
  3292. ZEND_ATTRIBUTE_UNUSED /* clang mistakenly warns about this */
  3293. static php_addslashes_func_t resolve_addslashes() {
  3294. if (zend_cpu_supports_sse42()) {
  3295. return php_addslashes_sse42;
  3296. }
  3297. return php_addslashes_default;
  3298. }
  3299. ZEND_NO_SANITIZE_ADDRESS
  3300. ZEND_ATTRIBUTE_UNUSED /* clang mistakenly warns about this */
  3301. static php_stripslashes_func_t resolve_stripslashes() {
  3302. if (zend_cpu_supports_sse42()) {
  3303. return php_stripslashes_sse42;
  3304. }
  3305. return php_stripslashes_default;
  3306. }
  3307. # else /* ZEND_INTRIN_SSE4_2_FUNC_PTR */
  3308. static zend_string *(*php_addslashes_ptr)(zend_string *str) = NULL;
  3309. static void (*php_stripslashes_ptr)(zend_string *str) = NULL;
  3310. PHPAPI zend_string *php_addslashes(zend_string *str) {
  3311. return php_addslashes_ptr(str);
  3312. }
  3313. PHPAPI void php_stripslashes(zend_string *str) {
  3314. php_stripslashes_ptr(str);
  3315. }
  3316. /* {{{ PHP_MINIT_FUNCTION
  3317. */
  3318. PHP_MINIT_FUNCTION(string_intrin)
  3319. {
  3320. if (zend_cpu_supports(ZEND_CPU_FEATURE_SSE42)) {
  3321. php_addslashes_ptr = php_addslashes_sse42;
  3322. php_stripslashes_ptr = php_stripslashes_sse42;
  3323. } else {
  3324. php_addslashes_ptr = php_addslashes_default;
  3325. php_stripslashes_ptr = php_stripslashes_default;
  3326. }
  3327. return SUCCESS;
  3328. }
  3329. /* }}} */
  3330. # endif
  3331. #endif
  3332. #if ZEND_INTRIN_SSE4_2_NATIVE || ZEND_INTRIN_SSE4_2_RESOLVER
  3333. # if ZEND_INTRIN_SSE4_2_NATIVE
  3334. PHPAPI zend_string *php_addslashes(zend_string *str) /* {{{ */
  3335. # elif ZEND_INTRIN_SSE4_2_RESOLVER
  3336. zend_string *php_addslashes_sse42(zend_string *str)
  3337. # endif
  3338. {
  3339. ZEND_SET_ALIGNED(16, static const char slashchars[16]) = "\'\"\\\0";
  3340. __m128i w128, s128;
  3341. uint32_t res = 0;
  3342. /* maximum string length, worst case situation */
  3343. char *target;
  3344. const char *source, *end;
  3345. size_t offset;
  3346. zend_string *new_str;
  3347. if (!str) {
  3348. return ZSTR_EMPTY_ALLOC();
  3349. }
  3350. source = ZSTR_VAL(str);
  3351. end = source + ZSTR_LEN(str);
  3352. if (ZSTR_LEN(str) > 15) {
  3353. w128 = _mm_load_si128((__m128i *)slashchars);
  3354. do {
  3355. s128 = _mm_loadu_si128((__m128i *)source);
  3356. res = _mm_cvtsi128_si32(_mm_cmpestrm(w128, 4, s128, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK));
  3357. if (res) {
  3358. goto do_escape;
  3359. }
  3360. source += 16;
  3361. } while ((end - source) > 15);
  3362. }
  3363. while (source < end) {
  3364. switch (*source) {
  3365. case '\0':
  3366. case '\'':
  3367. case '\"':
  3368. case '\\':
  3369. goto do_escape;
  3370. default:
  3371. source++;
  3372. break;
  3373. }
  3374. }
  3375. return zend_string_copy(str);
  3376. do_escape:
  3377. offset = source - (char *)ZSTR_VAL(str);
  3378. new_str = zend_string_safe_alloc(2, ZSTR_LEN(str) - offset, offset, 0);
  3379. memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), offset);
  3380. target = ZSTR_VAL(new_str) + offset;
  3381. if (res) {
  3382. int pos = 0;
  3383. do {
  3384. int i, n = zend_ulong_ntz(res);
  3385. for (i = 0; i < n; i++) {
  3386. *target++ = source[pos + i];
  3387. }
  3388. pos += n;
  3389. *target++ = '\\';
  3390. if (source[pos] == '\0') {
  3391. *target++ = '0';
  3392. } else {
  3393. *target++ = source[pos];
  3394. }
  3395. pos++;
  3396. res = res >> (n + 1);
  3397. } while (res);
  3398. for (; pos < 16; pos++) {
  3399. *target++ = source[pos];
  3400. }
  3401. source += 16;
  3402. } else if (end - source > 15) {
  3403. w128 = _mm_load_si128((__m128i *)slashchars);
  3404. }
  3405. for (; end - source > 15; source += 16) {
  3406. int pos = 0;
  3407. s128 = _mm_loadu_si128((__m128i *)source);
  3408. res = _mm_cvtsi128_si32(_mm_cmpestrm(w128, 4, s128, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK));
  3409. if (res) {
  3410. do {
  3411. int i, n = zend_ulong_ntz(res);
  3412. for (i = 0; i < n; i++) {
  3413. *target++ = source[pos + i];
  3414. }
  3415. pos += n;
  3416. *target++ = '\\';
  3417. if (source[pos] == '\0') {
  3418. *target++ = '0';
  3419. } else {
  3420. *target++ = source[pos];
  3421. }
  3422. pos++;
  3423. res = res >> (n + 1);
  3424. } while (res);
  3425. for (; pos < 16; pos++) {
  3426. *target++ = source[pos];
  3427. }
  3428. } else {
  3429. _mm_storeu_si128((__m128i*)target, s128);
  3430. target += 16;
  3431. }
  3432. }
  3433. while (source < end) {
  3434. switch (*source) {
  3435. case '\0':
  3436. *target++ = '\\';
  3437. *target++ = '0';
  3438. break;
  3439. case '\'':
  3440. case '\"':
  3441. case '\\':
  3442. *target++ = '\\';
  3443. /* break is missing *intentionally* */
  3444. default:
  3445. *target++ = *source;
  3446. break;
  3447. }
  3448. source++;
  3449. }
  3450. *target = '\0';
  3451. if (ZSTR_LEN(new_str) - (target - ZSTR_VAL(new_str)) > 16) {
  3452. new_str = zend_string_truncate(new_str, target - ZSTR_VAL(new_str), 0);
  3453. } else {
  3454. ZSTR_LEN(new_str) = target - ZSTR_VAL(new_str);
  3455. }
  3456. return new_str;
  3457. }
  3458. /* }}} */
  3459. #endif
  3460. #ifdef __aarch64__
  3461. typedef union {
  3462. uint8_t mem[16];
  3463. uint64_t dw[2];
  3464. } quad_word;
  3465. static zend_always_inline quad_word aarch64_contains_slash_chars(uint8x16_t x) {
  3466. uint8x16_t s0 = vceqq_u8(x, vdupq_n_u8('\0'));
  3467. uint8x16_t s1 = vceqq_u8(x, vdupq_n_u8('\''));
  3468. uint8x16_t s2 = vceqq_u8(x, vdupq_n_u8('\"'));
  3469. uint8x16_t s3 = vceqq_u8(x, vdupq_n_u8('\\'));
  3470. uint8x16_t s01 = vorrq_u8(s0, s1);
  3471. uint8x16_t s23 = vorrq_u8(s2, s3);
  3472. uint8x16_t s0123 = vorrq_u8(s01, s23);
  3473. quad_word qw;
  3474. vst1q_u8(qw.mem, s0123);
  3475. return qw;
  3476. }
  3477. static zend_always_inline char *aarch64_add_slashes(quad_word res, const char *source, char *target)
  3478. {
  3479. int i = 0;
  3480. for (; i < 16; i++) {
  3481. char s = source[i];
  3482. if (res.mem[i] == 0)
  3483. *target++ = s;
  3484. else {
  3485. *target++ = '\\';
  3486. if (s == '\0')
  3487. *target++ = '0';
  3488. else
  3489. *target++ = s;
  3490. }
  3491. }
  3492. return target;
  3493. }
  3494. #endif /* __aarch64__ */
  3495. #if !ZEND_INTRIN_SSE4_2_NATIVE
  3496. # if ZEND_INTRIN_SSE4_2_RESOLVER
  3497. zend_string *php_addslashes_default(zend_string *str) /* {{{ */
  3498. # else
  3499. PHPAPI zend_string *php_addslashes(zend_string *str)
  3500. # endif
  3501. {
  3502. /* maximum string length, worst case situation */
  3503. char *target;
  3504. const char *source, *end;
  3505. size_t offset;
  3506. zend_string *new_str;
  3507. if (!str) {
  3508. return ZSTR_EMPTY_ALLOC();
  3509. }
  3510. source = ZSTR_VAL(str);
  3511. end = source + ZSTR_LEN(str);
  3512. # ifdef __aarch64__
  3513. quad_word res = {0};
  3514. if (ZSTR_LEN(str) > 15) {
  3515. do {
  3516. res = aarch64_contains_slash_chars(vld1q_u8((uint8_t *)source));
  3517. if (res.dw[0] | res.dw[1])
  3518. goto do_escape;
  3519. source += 16;
  3520. } while ((end - source) > 15);
  3521. }
  3522. /* Finish the last 15 bytes or less with the scalar loop. */
  3523. # endif /* __aarch64__ */
  3524. while (source < end) {
  3525. switch (*source) {
  3526. case '\0':
  3527. case '\'':
  3528. case '\"':
  3529. case '\\':
  3530. goto do_escape;
  3531. default:
  3532. source++;
  3533. break;
  3534. }
  3535. }
  3536. return zend_string_copy(str);
  3537. do_escape:
  3538. offset = source - (char *)ZSTR_VAL(str);
  3539. new_str = zend_string_safe_alloc(2, ZSTR_LEN(str) - offset, offset, 0);
  3540. memcpy(ZSTR_VAL(new_str), ZSTR_VAL(str), offset);
  3541. target = ZSTR_VAL(new_str) + offset;
  3542. # ifdef __aarch64__
  3543. if (res.dw[0] | res.dw[1]) {
  3544. target = aarch64_add_slashes(res, source, target);
  3545. source += 16;
  3546. }
  3547. for (; end - source > 15; source += 16) {
  3548. uint8x16_t x = vld1q_u8((uint8_t *)source);
  3549. res = aarch64_contains_slash_chars(x);
  3550. if (res.dw[0] | res.dw[1]) {
  3551. target = aarch64_add_slashes(res, source, target);
  3552. } else {
  3553. vst1q_u8((uint8_t*)target, x);
  3554. target += 16;
  3555. }
  3556. }
  3557. /* Finish the last 15 bytes or less with the scalar loop. */
  3558. # endif /* __aarch64__ */
  3559. while (source < end) {
  3560. switch (*source) {
  3561. case '\0':
  3562. *target++ = '\\';
  3563. *target++ = '0';
  3564. break;
  3565. case '\'':
  3566. case '\"':
  3567. case '\\':
  3568. *target++ = '\\';
  3569. /* break is missing *intentionally* */
  3570. default:
  3571. *target++ = *source;
  3572. break;
  3573. }
  3574. source++;
  3575. }
  3576. *target = '\0';
  3577. if (ZSTR_LEN(new_str) - (target - ZSTR_VAL(new_str)) > 16) {
  3578. new_str = zend_string_truncate(new_str, target - ZSTR_VAL(new_str), 0);
  3579. } else {
  3580. ZSTR_LEN(new_str) = target - ZSTR_VAL(new_str);
  3581. }
  3582. return new_str;
  3583. }
  3584. #endif
  3585. /* }}} */
  3586. /* }}} */
  3587. /* {{{ php_stripslashes
  3588. *
  3589. * be careful, this edits the string in-place */
  3590. static zend_always_inline char *php_stripslashes_impl(const char *str, char *out, size_t len)
  3591. {
  3592. #ifdef __aarch64__
  3593. while (len > 15) {
  3594. uint8x16_t x = vld1q_u8((uint8_t *)str);
  3595. quad_word q;
  3596. vst1q_u8(q.mem, vceqq_u8(x, vdupq_n_u8('\\')));
  3597. if (q.dw[0] | q.dw[1]) {
  3598. int i = 0;
  3599. for (; i < 16; i++) {
  3600. if (q.mem[i] == 0) {
  3601. *out++ = str[i];
  3602. continue;
  3603. }
  3604. i++; /* skip the slash */
  3605. char s = str[i];
  3606. if (s == '0')
  3607. *out++ = '\0';
  3608. else
  3609. *out++ = s; /* preserve the next character */
  3610. }
  3611. str += i;
  3612. len -= i;
  3613. } else {
  3614. vst1q_u8((uint8_t*)out, x);
  3615. out += 16;
  3616. str += 16;
  3617. len -= 16;
  3618. }
  3619. }
  3620. /* Finish the last 15 bytes or less with the scalar loop. */
  3621. #endif /* __aarch64__ */
  3622. while (len > 0) {
  3623. if (*str == '\\') {
  3624. str++; /* skip the slash */
  3625. len--;
  3626. if (len > 0) {
  3627. if (*str == '0') {
  3628. *out++='\0';
  3629. str++;
  3630. } else {
  3631. *out++ = *str++; /* preserve the next character */
  3632. }
  3633. len--;
  3634. }
  3635. } else {
  3636. *out++ = *str++;
  3637. len--;
  3638. }
  3639. }
  3640. return out;
  3641. }
  3642. #if ZEND_INTRIN_SSE4_2_NATIVE || ZEND_INTRIN_SSE4_2_RESOLVER
  3643. # if ZEND_INTRIN_SSE4_2_NATIVE
  3644. PHPAPI void php_stripslashes(zend_string *str)
  3645. # elif ZEND_INTRIN_SSE4_2_RESOLVER
  3646. void php_stripslashes_sse42(zend_string *str)
  3647. # endif
  3648. {
  3649. const char *s = ZSTR_VAL(str);
  3650. char *t = ZSTR_VAL(str);
  3651. size_t l = ZSTR_LEN(str);
  3652. if (l > 15) {
  3653. const __m128i slash = _mm_set1_epi8('\\');
  3654. do {
  3655. __m128i in = _mm_loadu_si128((__m128i *)s);
  3656. __m128i any_slash = _mm_cmpeq_epi8(in, slash);
  3657. uint32_t res = _mm_movemask_epi8(any_slash);
  3658. if (res) {
  3659. int i, n = zend_ulong_ntz(res);
  3660. const char *e = s + 15;
  3661. l -= n;
  3662. for (i = 0; i < n; i++) {
  3663. *t++ = *s++;
  3664. }
  3665. for (; s < e; s++) {
  3666. if (*s == '\\') {
  3667. s++;
  3668. l--;
  3669. if (*s == '0') {
  3670. *t = '\0';
  3671. } else {
  3672. *t = *s;
  3673. }
  3674. } else {
  3675. *t = *s;
  3676. }
  3677. t++;
  3678. l--;
  3679. }
  3680. } else {
  3681. _mm_storeu_si128((__m128i *)t, in);
  3682. s += 16;
  3683. t += 16;
  3684. l -= 16;
  3685. }
  3686. } while (l > 15);
  3687. }
  3688. t = php_stripslashes_impl(s, t, l);
  3689. if (t != (ZSTR_VAL(str) + ZSTR_LEN(str))) {
  3690. ZSTR_LEN(str) = t - ZSTR_VAL(str);
  3691. ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
  3692. }
  3693. }
  3694. #endif
  3695. #if !ZEND_INTRIN_SSE4_2_NATIVE
  3696. # if ZEND_INTRIN_SSE4_2_RESOLVER
  3697. void php_stripslashes_default(zend_string *str) /* {{{ */
  3698. # else
  3699. PHPAPI void php_stripslashes(zend_string *str)
  3700. # endif
  3701. {
  3702. const char *t = php_stripslashes_impl(ZSTR_VAL(str), ZSTR_VAL(str), ZSTR_LEN(str));
  3703. if (t != (ZSTR_VAL(str) + ZSTR_LEN(str))) {
  3704. ZSTR_LEN(str) = t - ZSTR_VAL(str);
  3705. ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
  3706. }
  3707. }
  3708. /* }}} */
  3709. #endif
  3710. /* }}} */
  3711. #define _HEB_BLOCK_TYPE_ENG 1
  3712. #define _HEB_BLOCK_TYPE_HEB 2
  3713. #define isheb(c) (((((unsigned char) c) >= 224) && (((unsigned char) c) <= 250)) ? 1 : 0)
  3714. #define _isblank(c) (((((unsigned char) c) == ' ' || ((unsigned char) c) == '\t')) ? 1 : 0)
  3715. #define _isnewline(c) (((((unsigned char) c) == '\n' || ((unsigned char) c) == '\r')) ? 1 : 0)
  3716. /* {{{ php_str_replace_in_subject
  3717. */
  3718. static zend_long php_str_replace_in_subject(zval *search, zval *replace, zend_string *subject_str, zval *result, int case_sensitivity)
  3719. {
  3720. zval *search_entry,
  3721. *replace_entry = NULL;
  3722. zend_string *tmp_result,
  3723. *tmp_replace_entry_str = NULL,
  3724. *replace_entry_str;
  3725. char *replace_value = NULL;
  3726. size_t replace_len = 0;
  3727. zend_long replace_count = 0;
  3728. zend_string *lc_subject_str = NULL;
  3729. uint32_t replace_idx;
  3730. if (ZSTR_LEN(subject_str) == 0) {
  3731. ZVAL_EMPTY_STRING(result);
  3732. return 0;
  3733. }
  3734. /* If search is an array */
  3735. if (Z_TYPE_P(search) == IS_ARRAY) {
  3736. /* Duplicate subject string for repeated replacement */
  3737. zend_string_addref(subject_str);
  3738. if (Z_TYPE_P(replace) == IS_ARRAY) {
  3739. replace_idx = 0;
  3740. } else {
  3741. /* Set replacement value to the passed one */
  3742. replace_value = Z_STRVAL_P(replace);
  3743. replace_len = Z_STRLEN_P(replace);
  3744. }
  3745. /* For each entry in the search array, get the entry */
  3746. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(search), search_entry) {
  3747. /* Make sure we're dealing with strings. */
  3748. zend_string *tmp_search_str;
  3749. zend_string *search_str = zval_get_tmp_string(search_entry, &tmp_search_str);
  3750. /* If replace is an array. */
  3751. if (Z_TYPE_P(replace) == IS_ARRAY) {
  3752. /* Get current entry */
  3753. while (replace_idx < Z_ARRVAL_P(replace)->nNumUsed) {
  3754. replace_entry = &Z_ARRVAL_P(replace)->arData[replace_idx].val;
  3755. if (Z_TYPE_P(replace_entry) != IS_UNDEF) {
  3756. break;
  3757. }
  3758. replace_idx++;
  3759. }
  3760. if (replace_idx < Z_ARRVAL_P(replace)->nNumUsed) {
  3761. /* Make sure we're dealing with strings. */
  3762. replace_entry_str = zval_get_tmp_string(replace_entry, &tmp_replace_entry_str);
  3763. /* Set replacement value to the one we got from array */
  3764. replace_value = ZSTR_VAL(replace_entry_str);
  3765. replace_len = ZSTR_LEN(replace_entry_str);
  3766. replace_idx++;
  3767. } else {
  3768. /* We've run out of replacement strings, so use an empty one. */
  3769. replace_value = "";
  3770. replace_len = 0;
  3771. }
  3772. }
  3773. if (ZSTR_LEN(search_str) == 1) {
  3774. zend_long old_replace_count = replace_count;
  3775. tmp_result = php_char_to_str_ex(subject_str,
  3776. ZSTR_VAL(search_str)[0],
  3777. replace_value,
  3778. replace_len,
  3779. case_sensitivity,
  3780. &replace_count);
  3781. if (lc_subject_str && replace_count != old_replace_count) {
  3782. zend_string_release_ex(lc_subject_str, 0);
  3783. lc_subject_str = NULL;
  3784. }
  3785. } else if (ZSTR_LEN(search_str) > 1) {
  3786. if (case_sensitivity) {
  3787. tmp_result = php_str_to_str_ex(subject_str,
  3788. ZSTR_VAL(search_str), ZSTR_LEN(search_str),
  3789. replace_value, replace_len, &replace_count);
  3790. } else {
  3791. zend_long old_replace_count = replace_count;
  3792. if (!lc_subject_str) {
  3793. lc_subject_str = php_string_tolower(subject_str);
  3794. }
  3795. tmp_result = php_str_to_str_i_ex(subject_str, ZSTR_VAL(lc_subject_str),
  3796. search_str, replace_value, replace_len, &replace_count);
  3797. if (replace_count != old_replace_count) {
  3798. zend_string_release_ex(lc_subject_str, 0);
  3799. lc_subject_str = NULL;
  3800. }
  3801. }
  3802. } else {
  3803. zend_tmp_string_release(tmp_search_str);
  3804. continue;
  3805. }
  3806. zend_tmp_string_release(tmp_search_str);
  3807. if (tmp_replace_entry_str) {
  3808. zend_string_release_ex(tmp_replace_entry_str, 0);
  3809. tmp_replace_entry_str = NULL;
  3810. }
  3811. if (subject_str == tmp_result) {
  3812. zend_string_delref(subject_str);
  3813. } else {
  3814. zend_string_release_ex(subject_str, 0);
  3815. subject_str = tmp_result;
  3816. if (ZSTR_LEN(subject_str) == 0) {
  3817. zend_string_release_ex(subject_str, 0);
  3818. ZVAL_EMPTY_STRING(result);
  3819. if (lc_subject_str) {
  3820. zend_string_release_ex(lc_subject_str, 0);
  3821. }
  3822. return replace_count;
  3823. }
  3824. }
  3825. } ZEND_HASH_FOREACH_END();
  3826. ZVAL_STR(result, subject_str);
  3827. if (lc_subject_str) {
  3828. zend_string_release_ex(lc_subject_str, 0);
  3829. }
  3830. } else {
  3831. ZEND_ASSERT(Z_TYPE_P(search) == IS_STRING);
  3832. if (Z_STRLEN_P(search) == 1) {
  3833. ZVAL_STR(result,
  3834. php_char_to_str_ex(subject_str,
  3835. Z_STRVAL_P(search)[0],
  3836. Z_STRVAL_P(replace),
  3837. Z_STRLEN_P(replace),
  3838. case_sensitivity,
  3839. &replace_count));
  3840. } else if (Z_STRLEN_P(search) > 1) {
  3841. if (case_sensitivity) {
  3842. ZVAL_STR(result, php_str_to_str_ex(subject_str,
  3843. Z_STRVAL_P(search), Z_STRLEN_P(search),
  3844. Z_STRVAL_P(replace), Z_STRLEN_P(replace), &replace_count));
  3845. } else {
  3846. lc_subject_str = php_string_tolower(subject_str);
  3847. ZVAL_STR(result, php_str_to_str_i_ex(subject_str, ZSTR_VAL(lc_subject_str),
  3848. Z_STR_P(search),
  3849. Z_STRVAL_P(replace), Z_STRLEN_P(replace), &replace_count));
  3850. zend_string_release_ex(lc_subject_str, 0);
  3851. }
  3852. } else {
  3853. ZVAL_STR_COPY(result, subject_str);
  3854. }
  3855. }
  3856. return replace_count;
  3857. }
  3858. /* }}} */
  3859. /* {{{ php_str_replace_common
  3860. */
  3861. static void php_str_replace_common(INTERNAL_FUNCTION_PARAMETERS, int case_sensitivity)
  3862. {
  3863. zend_string *subject_str;
  3864. HashTable *subject_ht;
  3865. zval *search, *replace, *subject_entry, *zcount = NULL;
  3866. zval result;
  3867. zend_string *string_key;
  3868. zend_ulong num_key;
  3869. zend_long count = 0;
  3870. int argc = ZEND_NUM_ARGS();
  3871. ZEND_PARSE_PARAMETERS_START(3, 4)
  3872. Z_PARAM_ZVAL(search)
  3873. Z_PARAM_ZVAL(replace)
  3874. Z_PARAM_STR_OR_ARRAY_HT(subject_str, subject_ht)
  3875. Z_PARAM_OPTIONAL
  3876. Z_PARAM_ZVAL(zcount)
  3877. ZEND_PARSE_PARAMETERS_END();
  3878. /* Make sure we're dealing with strings and do the replacement. */
  3879. if (Z_TYPE_P(search) != IS_ARRAY) {
  3880. convert_to_string_ex(search);
  3881. if (Z_TYPE_P(replace) != IS_STRING) {
  3882. convert_to_string_ex(replace);
  3883. }
  3884. } else if (Z_TYPE_P(replace) != IS_ARRAY) {
  3885. convert_to_string_ex(replace);
  3886. }
  3887. if (EG(exception)) {
  3888. RETURN_THROWS();
  3889. }
  3890. /* if subject is an array */
  3891. if (subject_ht) {
  3892. array_init(return_value);
  3893. /* For each subject entry, convert it to string, then perform replacement
  3894. and add the result to the return_value array. */
  3895. ZEND_HASH_FOREACH_KEY_VAL(subject_ht, num_key, string_key, subject_entry) {
  3896. zend_string *tmp_subject_str;
  3897. ZVAL_DEREF(subject_entry);
  3898. subject_str = zval_get_tmp_string(subject_entry, &tmp_subject_str);
  3899. count += php_str_replace_in_subject(search, replace, subject_str, &result, case_sensitivity);
  3900. zend_tmp_string_release(tmp_subject_str);
  3901. /* Add to return array */
  3902. if (string_key) {
  3903. zend_hash_add_new(Z_ARRVAL_P(return_value), string_key, &result);
  3904. } else {
  3905. zend_hash_index_add_new(Z_ARRVAL_P(return_value), num_key, &result);
  3906. }
  3907. } ZEND_HASH_FOREACH_END();
  3908. } else { /* if subject is not an array */
  3909. count = php_str_replace_in_subject(search, replace, subject_str, return_value, case_sensitivity);
  3910. }
  3911. if (argc > 3) {
  3912. ZEND_TRY_ASSIGN_REF_LONG(zcount, count);
  3913. }
  3914. }
  3915. /* }}} */
  3916. /* {{{ proto mixed str_replace(mixed search, mixed replace, mixed subject [, int &replace_count])
  3917. Replaces all occurrences of search in haystack with replace */
  3918. PHP_FUNCTION(str_replace)
  3919. {
  3920. php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3921. }
  3922. /* }}} */
  3923. /* {{{ proto mixed str_ireplace(mixed search, mixed replace, mixed subject [, int &replace_count])
  3924. Replaces all occurrences of search in haystack with replace / case-insensitive */
  3925. PHP_FUNCTION(str_ireplace)
  3926. {
  3927. php_str_replace_common(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3928. }
  3929. /* }}} */
  3930. /* {{{ proto string hebrev(string str [, int max_chars_per_line])
  3931. Converts logical Hebrew text to visual text */
  3932. PHP_FUNCTION(hebrev)
  3933. {
  3934. char *str, *heb_str, *target;
  3935. const char *tmp;
  3936. size_t block_start, block_end, block_type, block_length, i;
  3937. zend_long max_chars=0, char_count;
  3938. size_t begin, end, orig_begin;
  3939. size_t str_len;
  3940. zend_string *broken_str;
  3941. ZEND_PARSE_PARAMETERS_START(1, 2)
  3942. Z_PARAM_STRING(str, str_len)
  3943. Z_PARAM_OPTIONAL
  3944. Z_PARAM_LONG(max_chars)
  3945. ZEND_PARSE_PARAMETERS_END();
  3946. if (str_len == 0) {
  3947. RETURN_EMPTY_STRING();
  3948. }
  3949. tmp = str;
  3950. block_start=block_end=0;
  3951. heb_str = (char *) emalloc(str_len+1);
  3952. target = heb_str+str_len;
  3953. *target = 0;
  3954. target--;
  3955. block_length=0;
  3956. if (isheb(*tmp)) {
  3957. block_type = _HEB_BLOCK_TYPE_HEB;
  3958. } else {
  3959. block_type = _HEB_BLOCK_TYPE_ENG;
  3960. }
  3961. do {
  3962. if (block_type == _HEB_BLOCK_TYPE_HEB) {
  3963. while ((isheb((int)*(tmp+1)) || _isblank((int)*(tmp+1)) || ispunct((int)*(tmp+1)) || (int)*(tmp+1)=='\n' ) && block_end<str_len-1) {
  3964. tmp++;
  3965. block_end++;
  3966. block_length++;
  3967. }
  3968. for (i = block_start+1; i<= block_end+1; i++) {
  3969. *target = str[i-1];
  3970. switch (*target) {
  3971. case '(':
  3972. *target = ')';
  3973. break;
  3974. case ')':
  3975. *target = '(';
  3976. break;
  3977. case '[':
  3978. *target = ']';
  3979. break;
  3980. case ']':
  3981. *target = '[';
  3982. break;
  3983. case '{':
  3984. *target = '}';
  3985. break;
  3986. case '}':
  3987. *target = '{';
  3988. break;
  3989. case '<':
  3990. *target = '>';
  3991. break;
  3992. case '>':
  3993. *target = '<';
  3994. break;
  3995. case '\\':
  3996. *target = '/';
  3997. break;
  3998. case '/':
  3999. *target = '\\';
  4000. break;
  4001. default:
  4002. break;
  4003. }
  4004. target--;
  4005. }
  4006. block_type = _HEB_BLOCK_TYPE_ENG;
  4007. } else {
  4008. while (!isheb(*(tmp+1)) && (int)*(tmp+1)!='\n' && block_end < str_len-1) {
  4009. tmp++;
  4010. block_end++;
  4011. block_length++;
  4012. }
  4013. while ((_isblank((int)*tmp) || ispunct((int)*tmp)) && *tmp!='/' && *tmp!='-' && block_end > block_start) {
  4014. tmp--;
  4015. block_end--;
  4016. }
  4017. for (i = block_end+1; i >= block_start+1; i--) {
  4018. *target = str[i-1];
  4019. target--;
  4020. }
  4021. block_type = _HEB_BLOCK_TYPE_HEB;
  4022. }
  4023. block_start=block_end+1;
  4024. } while (block_end < str_len-1);
  4025. broken_str = zend_string_alloc(str_len, 0);
  4026. begin = end = str_len-1;
  4027. target = ZSTR_VAL(broken_str);
  4028. while (1) {
  4029. char_count=0;
  4030. while ((!max_chars || (max_chars > 0 && char_count < max_chars)) && begin > 0) {
  4031. char_count++;
  4032. begin--;
  4033. if (_isnewline(heb_str[begin])) {
  4034. while (begin > 0 && _isnewline(heb_str[begin-1])) {
  4035. begin--;
  4036. char_count++;
  4037. }
  4038. break;
  4039. }
  4040. }
  4041. if (max_chars >= 0 && char_count == max_chars) { /* try to avoid breaking words */
  4042. size_t new_char_count=char_count, new_begin=begin;
  4043. while (new_char_count > 0) {
  4044. if (_isblank(heb_str[new_begin]) || _isnewline(heb_str[new_begin])) {
  4045. break;
  4046. }
  4047. new_begin++;
  4048. new_char_count--;
  4049. }
  4050. if (new_char_count > 0) {
  4051. begin=new_begin;
  4052. }
  4053. }
  4054. orig_begin=begin;
  4055. if (_isblank(heb_str[begin])) {
  4056. heb_str[begin]='\n';
  4057. }
  4058. while (begin <= end && _isnewline(heb_str[begin])) { /* skip leading newlines */
  4059. begin++;
  4060. }
  4061. for (i = begin; i <= end; i++) { /* copy content */
  4062. *target = heb_str[i];
  4063. target++;
  4064. }
  4065. for (i = orig_begin; i <= end && _isnewline(heb_str[i]); i++) {
  4066. *target = heb_str[i];
  4067. target++;
  4068. }
  4069. begin=orig_begin;
  4070. if (begin == 0) {
  4071. *target = 0;
  4072. break;
  4073. }
  4074. begin--;
  4075. end=begin;
  4076. }
  4077. efree(heb_str);
  4078. RETURN_NEW_STR(broken_str);
  4079. }
  4080. /* }}} */
  4081. /* {{{ proto string nl2br(string str [, bool is_xhtml])
  4082. Converts newlines to HTML line breaks */
  4083. PHP_FUNCTION(nl2br)
  4084. {
  4085. /* in brief this inserts <br /> or <br> before matched regexp \n\r?|\r\n? */
  4086. const char *tmp, *end;
  4087. zend_string *str;
  4088. char *target;
  4089. size_t repl_cnt = 0;
  4090. zend_bool is_xhtml = 1;
  4091. zend_string *result;
  4092. ZEND_PARSE_PARAMETERS_START(1, 2)
  4093. Z_PARAM_STR(str)
  4094. Z_PARAM_OPTIONAL
  4095. Z_PARAM_BOOL(is_xhtml)
  4096. ZEND_PARSE_PARAMETERS_END();
  4097. tmp = ZSTR_VAL(str);
  4098. end = ZSTR_VAL(str) + ZSTR_LEN(str);
  4099. /* it is really faster to scan twice and allocate mem once instead of scanning once
  4100. and constantly reallocing */
  4101. while (tmp < end) {
  4102. if (*tmp == '\r') {
  4103. if (*(tmp+1) == '\n') {
  4104. tmp++;
  4105. }
  4106. repl_cnt++;
  4107. } else if (*tmp == '\n') {
  4108. if (*(tmp+1) == '\r') {
  4109. tmp++;
  4110. }
  4111. repl_cnt++;
  4112. }
  4113. tmp++;
  4114. }
  4115. if (repl_cnt == 0) {
  4116. RETURN_STR_COPY(str);
  4117. }
  4118. {
  4119. size_t repl_len = is_xhtml ? (sizeof("<br />") - 1) : (sizeof("<br>") - 1);
  4120. result = zend_string_safe_alloc(repl_cnt, repl_len, ZSTR_LEN(str), 0);
  4121. target = ZSTR_VAL(result);
  4122. }
  4123. tmp = ZSTR_VAL(str);
  4124. while (tmp < end) {
  4125. switch (*tmp) {
  4126. case '\r':
  4127. case '\n':
  4128. *target++ = '<';
  4129. *target++ = 'b';
  4130. *target++ = 'r';
  4131. if (is_xhtml) {
  4132. *target++ = ' ';
  4133. *target++ = '/';
  4134. }
  4135. *target++ = '>';
  4136. if ((*tmp == '\r' && *(tmp+1) == '\n') || (*tmp == '\n' && *(tmp+1) == '\r')) {
  4137. *target++ = *tmp++;
  4138. }
  4139. /* lack of a break; is intentional */
  4140. default:
  4141. *target++ = *tmp;
  4142. }
  4143. tmp++;
  4144. }
  4145. *target = '\0';
  4146. RETURN_NEW_STR(result);
  4147. }
  4148. /* }}} */
  4149. /* {{{ proto string strip_tags(string str [, string allowable_tags])
  4150. Strips HTML and PHP tags from a string */
  4151. PHP_FUNCTION(strip_tags)
  4152. {
  4153. zend_string *buf;
  4154. zend_string *str;
  4155. zval *allow=NULL;
  4156. const char *allowed_tags=NULL;
  4157. size_t allowed_tags_len=0;
  4158. smart_str tags_ss = {0};
  4159. ZEND_PARSE_PARAMETERS_START(1, 2)
  4160. Z_PARAM_STR(str)
  4161. Z_PARAM_OPTIONAL
  4162. Z_PARAM_ZVAL(allow)
  4163. ZEND_PARSE_PARAMETERS_END();
  4164. if (allow) {
  4165. if (Z_TYPE_P(allow) == IS_ARRAY) {
  4166. zval *tmp;
  4167. zend_string *tag;
  4168. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(allow), tmp) {
  4169. tag = zval_get_string(tmp);
  4170. smart_str_appendc(&tags_ss, '<');
  4171. smart_str_append(&tags_ss, tag);
  4172. smart_str_appendc(&tags_ss, '>');
  4173. zend_string_release(tag);
  4174. } ZEND_HASH_FOREACH_END();
  4175. if (tags_ss.s) {
  4176. smart_str_0(&tags_ss);
  4177. allowed_tags = ZSTR_VAL(tags_ss.s);
  4178. allowed_tags_len = ZSTR_LEN(tags_ss.s);
  4179. }
  4180. } else {
  4181. /* To maintain a certain BC, we allow anything for the second parameter and return original string */
  4182. convert_to_string(allow);
  4183. allowed_tags = Z_STRVAL_P(allow);
  4184. allowed_tags_len = Z_STRLEN_P(allow);
  4185. }
  4186. }
  4187. buf = zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0);
  4188. ZSTR_LEN(buf) = php_strip_tags_ex(ZSTR_VAL(buf), ZSTR_LEN(str), allowed_tags, allowed_tags_len, 0);
  4189. smart_str_free(&tags_ss);
  4190. RETURN_NEW_STR(buf);
  4191. }
  4192. /* }}} */
  4193. /* {{{ proto string|false setlocale(int category, string locale [, string ...])
  4194. Set locale information */
  4195. PHP_FUNCTION(setlocale)
  4196. {
  4197. zval *args = NULL;
  4198. zval *plocale;
  4199. zend_string *loc;
  4200. const char *retval;
  4201. zend_long cat;
  4202. int num_args, i = 0;
  4203. uint32_t idx;
  4204. ZEND_PARSE_PARAMETERS_START(2, -1)
  4205. Z_PARAM_LONG(cat)
  4206. Z_PARAM_VARIADIC('+', args, num_args)
  4207. ZEND_PARSE_PARAMETERS_END();
  4208. idx = 0;
  4209. while (1) {
  4210. if (Z_TYPE(args[0]) == IS_ARRAY) {
  4211. while (idx < Z_ARRVAL(args[0])->nNumUsed) {
  4212. plocale = &Z_ARRVAL(args[0])->arData[idx].val;
  4213. if (Z_TYPE_P(plocale) != IS_UNDEF) {
  4214. break;
  4215. }
  4216. idx++;
  4217. }
  4218. if (idx >= Z_ARRVAL(args[0])->nNumUsed) {
  4219. break;
  4220. }
  4221. } else {
  4222. plocale = &args[i];
  4223. }
  4224. loc = zval_try_get_string(plocale);
  4225. if (UNEXPECTED(!loc)) {
  4226. return;
  4227. }
  4228. if (!strcmp("0", ZSTR_VAL(loc))) {
  4229. zend_string_release_ex(loc, 0);
  4230. loc = NULL;
  4231. } else {
  4232. if (ZSTR_LEN(loc) >= 255) {
  4233. php_error_docref(NULL, E_WARNING, "Specified locale name is too long");
  4234. zend_string_release_ex(loc, 0);
  4235. break;
  4236. }
  4237. }
  4238. # ifndef PHP_WIN32
  4239. retval = php_my_setlocale(cat, loc ? ZSTR_VAL(loc) : NULL);
  4240. # else
  4241. if (loc) {
  4242. /* BC: don't try /^[a-z]{2}_[A-Z]{2}($|\..*)/ except for /^u[ks]_U[KS]$/ */
  4243. char *locp = ZSTR_VAL(loc);
  4244. if (ZSTR_LEN(loc) >= 5 && locp[2] == '_'
  4245. && locp[0] >= 'a' && locp[0] <= 'z' && locp[1] >= 'a' && locp[1] <= 'z'
  4246. && locp[3] >= 'A' && locp[3] <= 'Z' && locp[4] >= 'A' && locp[4] <= 'Z'
  4247. && (locp[5] == '\0' || locp[5] == '.')
  4248. && !(locp[0] == 'u' && (locp[1] == 'k' || locp[1] == 's')
  4249. && locp[3] == 'U' && (locp[4] == 'K' || locp[4] == 'S')
  4250. && locp[5] == '\0')
  4251. ) {
  4252. retval = NULL;
  4253. } else {
  4254. retval = php_my_setlocale(cat, ZSTR_VAL(loc));
  4255. }
  4256. } else {
  4257. retval = php_my_setlocale(cat, NULL);
  4258. }
  4259. # endif
  4260. zend_update_current_locale();
  4261. if (retval) {
  4262. if (loc) {
  4263. /* Remember if locale was changed */
  4264. size_t len = strlen(retval);
  4265. BG(locale_changed) = 1;
  4266. if (cat == LC_CTYPE || cat == LC_ALL) {
  4267. if (BG(ctype_string)) {
  4268. zend_string_release_ex(BG(ctype_string), 0);
  4269. }
  4270. if (len == 1 && *retval == 'C') {
  4271. /* C locale is represented as NULL. */
  4272. BG(ctype_string) = NULL;
  4273. zend_string_release_ex(loc, 0);
  4274. RETURN_INTERNED_STR(ZSTR_CHAR('C'));
  4275. } else if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) {
  4276. BG(ctype_string) = zend_string_copy(loc);
  4277. RETURN_STR(BG(ctype_string));
  4278. } else {
  4279. BG(ctype_string) = zend_string_init(retval, len, 0);
  4280. zend_string_release_ex(loc, 0);
  4281. RETURN_STR_COPY(BG(ctype_string));
  4282. }
  4283. } else if (len == ZSTR_LEN(loc) && !memcmp(ZSTR_VAL(loc), retval, len)) {
  4284. RETURN_STR(loc);
  4285. }
  4286. zend_string_release_ex(loc, 0);
  4287. }
  4288. RETURN_STRING(retval);
  4289. }
  4290. if (loc) {
  4291. zend_string_release_ex(loc, 0);
  4292. }
  4293. if (Z_TYPE(args[0]) == IS_ARRAY) {
  4294. idx++;
  4295. } else {
  4296. if (++i >= num_args) break;
  4297. }
  4298. }
  4299. RETURN_FALSE;
  4300. }
  4301. /* }}} */
  4302. /* {{{ proto void parse_str(string encoded_string, array &result)
  4303. Parses GET/POST/COOKIE data and sets global variables */
  4304. PHP_FUNCTION(parse_str)
  4305. {
  4306. char *arg;
  4307. zval *arrayArg = NULL;
  4308. char *res = NULL;
  4309. size_t arglen;
  4310. ZEND_PARSE_PARAMETERS_START(2, 2)
  4311. Z_PARAM_STRING(arg, arglen)
  4312. Z_PARAM_ZVAL(arrayArg)
  4313. ZEND_PARSE_PARAMETERS_END();
  4314. arrayArg = zend_try_array_init(arrayArg);
  4315. if (!arrayArg) {
  4316. RETURN_THROWS();
  4317. }
  4318. res = estrndup(arg, arglen);
  4319. sapi_module.treat_data(PARSE_STRING, res, arrayArg);
  4320. }
  4321. /* }}} */
  4322. #define PHP_TAG_BUF_SIZE 1023
  4323. /* {{{ php_tag_find
  4324. *
  4325. * Check if tag is in a set of tags
  4326. *
  4327. * states:
  4328. *
  4329. * 0 start tag
  4330. * 1 first non-whitespace char seen
  4331. */
  4332. int php_tag_find(char *tag, size_t len, const char *set) {
  4333. char c, *n;
  4334. const char *t;
  4335. int state=0, done=0;
  4336. char *norm;
  4337. if (len == 0) {
  4338. return 0;
  4339. }
  4340. norm = emalloc(len+1);
  4341. n = norm;
  4342. t = tag;
  4343. c = tolower(*t);
  4344. /*
  4345. normalize the tag removing leading and trailing whitespace
  4346. and turn any <a whatever...> into just <a> and any </tag>
  4347. into <tag>
  4348. */
  4349. while (!done) {
  4350. switch (c) {
  4351. case '<':
  4352. *(n++) = c;
  4353. break;
  4354. case '>':
  4355. done =1;
  4356. break;
  4357. default:
  4358. if (!isspace((int)c)) {
  4359. if (state == 0) {
  4360. state=1;
  4361. }
  4362. if (c != '/' || (*(t-1) != '<' && *(t+1) != '>')) {
  4363. *(n++) = c;
  4364. }
  4365. } else {
  4366. if (state == 1)
  4367. done=1;
  4368. }
  4369. break;
  4370. }
  4371. c = tolower(*(++t));
  4372. }
  4373. *(n++) = '>';
  4374. *n = '\0';
  4375. if (strstr(set, norm)) {
  4376. done=1;
  4377. } else {
  4378. done=0;
  4379. }
  4380. efree(norm);
  4381. return done;
  4382. }
  4383. /* }}} */
  4384. PHPAPI size_t php_strip_tags(char *rbuf, size_t len, const char *allow, size_t allow_len) /* {{{ */
  4385. {
  4386. return php_strip_tags_ex(rbuf, len, allow, allow_len, 0);
  4387. }
  4388. /* }}} */
  4389. /* {{{ php_strip_tags
  4390. A simple little state-machine to strip out html and php tags
  4391. State 0 is the output state, State 1 means we are inside a
  4392. normal html tag and state 2 means we are inside a php tag.
  4393. The state variable is passed in to allow a function like fgetss
  4394. to maintain state across calls to the function.
  4395. lc holds the last significant character read and br is a bracket
  4396. counter.
  4397. When an allow string is passed in we keep track of the string
  4398. in state 1 and when the tag is closed check it against the
  4399. allow string to see if we should allow it.
  4400. swm: Added ability to strip <?xml tags without assuming it PHP
  4401. code.
  4402. */
  4403. PHPAPI size_t php_strip_tags_ex(char *rbuf, size_t len, const char *allow, size_t allow_len, zend_bool allow_tag_spaces)
  4404. {
  4405. char *tbuf, *tp, *rp, c, lc;
  4406. const char *buf, *p, *end;
  4407. int br, depth=0, in_q = 0;
  4408. uint8_t state = 0;
  4409. size_t pos;
  4410. char *allow_free = NULL;
  4411. char is_xml = 0;
  4412. buf = estrndup(rbuf, len);
  4413. end = buf + len;
  4414. lc = '\0';
  4415. p = buf;
  4416. rp = rbuf;
  4417. br = 0;
  4418. if (allow) {
  4419. allow_free = zend_str_tolower_dup_ex(allow, allow_len);
  4420. allow = allow_free ? allow_free : allow;
  4421. tbuf = emalloc(PHP_TAG_BUF_SIZE + 1);
  4422. tp = tbuf;
  4423. } else {
  4424. tbuf = tp = NULL;
  4425. }
  4426. state_0:
  4427. if (p >= end) {
  4428. goto finish;
  4429. }
  4430. c = *p;
  4431. switch (c) {
  4432. case '\0':
  4433. break;
  4434. case '<':
  4435. if (in_q) {
  4436. break;
  4437. }
  4438. if (isspace(*(p + 1)) && !allow_tag_spaces) {
  4439. *(rp++) = c;
  4440. break;
  4441. }
  4442. lc = '<';
  4443. state = 1;
  4444. if (allow) {
  4445. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4446. pos = tp - tbuf;
  4447. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4448. tp = tbuf + pos;
  4449. }
  4450. *(tp++) = '<';
  4451. }
  4452. p++;
  4453. goto state_1;
  4454. case '>':
  4455. if (depth) {
  4456. depth--;
  4457. break;
  4458. }
  4459. if (in_q) {
  4460. break;
  4461. }
  4462. *(rp++) = c;
  4463. break;
  4464. default:
  4465. *(rp++) = c;
  4466. break;
  4467. }
  4468. p++;
  4469. goto state_0;
  4470. state_1:
  4471. if (p >= end) {
  4472. goto finish;
  4473. }
  4474. c = *p;
  4475. switch (c) {
  4476. case '\0':
  4477. break;
  4478. case '<':
  4479. if (in_q) {
  4480. break;
  4481. }
  4482. if (isspace(*(p + 1)) && !allow_tag_spaces) {
  4483. goto reg_char_1;
  4484. }
  4485. depth++;
  4486. break;
  4487. case '>':
  4488. if (depth) {
  4489. depth--;
  4490. break;
  4491. }
  4492. if (in_q) {
  4493. break;
  4494. }
  4495. lc = '>';
  4496. if (is_xml && p >= buf + 1 && *(p -1) == '-') {
  4497. break;
  4498. }
  4499. in_q = state = is_xml = 0;
  4500. if (allow) {
  4501. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4502. pos = tp - tbuf;
  4503. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4504. tp = tbuf + pos;
  4505. }
  4506. *(tp++) = '>';
  4507. *tp='\0';
  4508. if (php_tag_find(tbuf, tp-tbuf, allow)) {
  4509. memcpy(rp, tbuf, tp-tbuf);
  4510. rp += tp-tbuf;
  4511. }
  4512. tp = tbuf;
  4513. }
  4514. p++;
  4515. goto state_0;
  4516. case '"':
  4517. case '\'':
  4518. if (p != buf && (!in_q || *p == in_q)) {
  4519. if (in_q) {
  4520. in_q = 0;
  4521. } else {
  4522. in_q = *p;
  4523. }
  4524. }
  4525. goto reg_char_1;
  4526. case '!':
  4527. /* JavaScript & Other HTML scripting languages */
  4528. if (p >= buf + 1 && *(p-1) == '<') {
  4529. state = 3;
  4530. lc = c;
  4531. p++;
  4532. goto state_3;
  4533. } else {
  4534. goto reg_char_1;
  4535. }
  4536. break;
  4537. case '?':
  4538. if (p >= buf + 1 && *(p-1) == '<') {
  4539. br=0;
  4540. state = 2;
  4541. p++;
  4542. goto state_2;
  4543. } else {
  4544. goto reg_char_1;
  4545. }
  4546. break;
  4547. default:
  4548. reg_char_1:
  4549. if (allow) {
  4550. if (tp - tbuf >= PHP_TAG_BUF_SIZE) {
  4551. pos = tp - tbuf;
  4552. tbuf = erealloc(tbuf, (tp - tbuf) + PHP_TAG_BUF_SIZE + 1);
  4553. tp = tbuf + pos;
  4554. }
  4555. *(tp++) = c;
  4556. }
  4557. break;
  4558. }
  4559. p++;
  4560. goto state_1;
  4561. state_2:
  4562. if (p >= end) {
  4563. goto finish;
  4564. }
  4565. c = *p;
  4566. switch (c) {
  4567. case '(':
  4568. if (lc != '"' && lc != '\'') {
  4569. lc = '(';
  4570. br++;
  4571. }
  4572. break;
  4573. case ')':
  4574. if (lc != '"' && lc != '\'') {
  4575. lc = ')';
  4576. br--;
  4577. }
  4578. break;
  4579. case '>':
  4580. if (depth) {
  4581. depth--;
  4582. break;
  4583. }
  4584. if (in_q) {
  4585. break;
  4586. }
  4587. if (!br && p >= buf + 1 && lc != '\"' && *(p-1) == '?') {
  4588. in_q = state = 0;
  4589. tp = tbuf;
  4590. p++;
  4591. goto state_0;
  4592. }
  4593. break;
  4594. case '"':
  4595. case '\'':
  4596. if (p >= buf + 1 && *(p-1) != '\\') {
  4597. if (lc == c) {
  4598. lc = '\0';
  4599. } else if (lc != '\\') {
  4600. lc = c;
  4601. }
  4602. if (p != buf && (!in_q || *p == in_q)) {
  4603. if (in_q) {
  4604. in_q = 0;
  4605. } else {
  4606. in_q = *p;
  4607. }
  4608. }
  4609. }
  4610. break;
  4611. case 'l':
  4612. case 'L':
  4613. /* swm: If we encounter '<?xml' then we shouldn't be in
  4614. * state == 2 (PHP). Switch back to HTML.
  4615. */
  4616. if (state == 2 && p > buf+4
  4617. && (*(p-1) == 'm' || *(p-1) == 'M')
  4618. && (*(p-2) == 'x' || *(p-2) == 'X')
  4619. && *(p-3) == '?'
  4620. && *(p-4) == '<') {
  4621. state = 1; is_xml=1;
  4622. p++;
  4623. goto state_1;
  4624. }
  4625. break;
  4626. default:
  4627. break;
  4628. }
  4629. p++;
  4630. goto state_2;
  4631. state_3:
  4632. if (p >= end) {
  4633. goto finish;
  4634. }
  4635. c = *p;
  4636. switch (c) {
  4637. case '>':
  4638. if (depth) {
  4639. depth--;
  4640. break;
  4641. }
  4642. if (in_q) {
  4643. break;
  4644. }
  4645. in_q = state = 0;
  4646. tp = tbuf;
  4647. p++;
  4648. goto state_0;
  4649. case '"':
  4650. case '\'':
  4651. if (p != buf && *(p-1) != '\\' && (!in_q || *p == in_q)) {
  4652. if (in_q) {
  4653. in_q = 0;
  4654. } else {
  4655. in_q = *p;
  4656. }
  4657. }
  4658. break;
  4659. case '-':
  4660. if (p >= buf + 2 && *(p-1) == '-' && *(p-2) == '!') {
  4661. state = 4;
  4662. p++;
  4663. goto state_4;
  4664. }
  4665. break;
  4666. case 'E':
  4667. case 'e':
  4668. /* !DOCTYPE exception */
  4669. if (p > buf+6
  4670. && (*(p-1) == 'p' || *(p-1) == 'P')
  4671. && (*(p-2) == 'y' || *(p-2) == 'Y')
  4672. && (*(p-3) == 't' || *(p-3) == 'T')
  4673. && (*(p-4) == 'c' || *(p-4) == 'C')
  4674. && (*(p-5) == 'o' || *(p-5) == 'O')
  4675. && (*(p-6) == 'd' || *(p-6) == 'D')) {
  4676. state = 1;
  4677. p++;
  4678. goto state_1;
  4679. }
  4680. break;
  4681. default:
  4682. break;
  4683. }
  4684. p++;
  4685. goto state_3;
  4686. state_4:
  4687. while (p < end) {
  4688. c = *p;
  4689. if (c == '>' && !in_q) {
  4690. if (p >= buf + 2 && *(p-1) == '-' && *(p-2) == '-') {
  4691. in_q = state = 0;
  4692. tp = tbuf;
  4693. p++;
  4694. goto state_0;
  4695. }
  4696. }
  4697. p++;
  4698. }
  4699. finish:
  4700. if (rp < rbuf + len) {
  4701. *rp = '\0';
  4702. }
  4703. efree((void *)buf);
  4704. if (tbuf) {
  4705. efree(tbuf);
  4706. }
  4707. if (allow_free) {
  4708. efree(allow_free);
  4709. }
  4710. return (size_t)(rp - rbuf);
  4711. }
  4712. /* }}} */
  4713. /* {{{ proto array str_getcsv(string input[, string delimiter[, string enclosure[, string escape]]])
  4714. Parse a CSV string into an array */
  4715. PHP_FUNCTION(str_getcsv)
  4716. {
  4717. zend_string *str;
  4718. char delim = ',', enc = '"';
  4719. int esc = (unsigned char) '\\';
  4720. char *delim_str = NULL, *enc_str = NULL, *esc_str = NULL;
  4721. size_t delim_len = 0, enc_len = 0, esc_len = 0;
  4722. ZEND_PARSE_PARAMETERS_START(1, 4)
  4723. Z_PARAM_STR(str)
  4724. Z_PARAM_OPTIONAL
  4725. Z_PARAM_STRING(delim_str, delim_len)
  4726. Z_PARAM_STRING(enc_str, enc_len)
  4727. Z_PARAM_STRING(esc_str, esc_len)
  4728. ZEND_PARSE_PARAMETERS_END();
  4729. delim = delim_len ? delim_str[0] : delim;
  4730. enc = enc_len ? enc_str[0] : enc;
  4731. if (esc_str != NULL) {
  4732. esc = esc_len ? (unsigned char) esc_str[0] : PHP_CSV_NO_ESCAPE;
  4733. }
  4734. php_fgetcsv(NULL, delim, enc, esc, ZSTR_LEN(str), ZSTR_VAL(str), return_value);
  4735. }
  4736. /* }}} */
  4737. /* {{{ proto string str_repeat(string input, int mult)
  4738. Returns the input string repeat mult times */
  4739. PHP_FUNCTION(str_repeat)
  4740. {
  4741. zend_string *input_str; /* Input string */
  4742. zend_long mult; /* Multiplier */
  4743. zend_string *result; /* Resulting string */
  4744. size_t result_len; /* Length of the resulting string */
  4745. ZEND_PARSE_PARAMETERS_START(2, 2)
  4746. Z_PARAM_STR(input_str)
  4747. Z_PARAM_LONG(mult)
  4748. ZEND_PARSE_PARAMETERS_END();
  4749. if (mult < 0) {
  4750. zend_argument_value_error(2, "must be greater than or equal to 0");
  4751. RETURN_THROWS();
  4752. }
  4753. /* Don't waste our time if it's empty */
  4754. /* ... or if the multiplier is zero */
  4755. if (ZSTR_LEN(input_str) == 0 || mult == 0)
  4756. RETURN_EMPTY_STRING();
  4757. /* Initialize the result string */
  4758. result = zend_string_safe_alloc(ZSTR_LEN(input_str), mult, 0, 0);
  4759. result_len = ZSTR_LEN(input_str) * mult;
  4760. /* Heavy optimization for situations where input string is 1 byte long */
  4761. if (ZSTR_LEN(input_str) == 1) {
  4762. memset(ZSTR_VAL(result), *ZSTR_VAL(input_str), mult);
  4763. } else {
  4764. const char *s, *ee;
  4765. char *e;
  4766. ptrdiff_t l=0;
  4767. memcpy(ZSTR_VAL(result), ZSTR_VAL(input_str), ZSTR_LEN(input_str));
  4768. s = ZSTR_VAL(result);
  4769. e = ZSTR_VAL(result) + ZSTR_LEN(input_str);
  4770. ee = ZSTR_VAL(result) + result_len;
  4771. while (e<ee) {
  4772. l = (e-s) < (ee-e) ? (e-s) : (ee-e);
  4773. memmove(e, s, l);
  4774. e += l;
  4775. }
  4776. }
  4777. ZSTR_VAL(result)[result_len] = '\0';
  4778. RETURN_NEW_STR(result);
  4779. }
  4780. /* }}} */
  4781. /* {{{ proto array|string count_chars(string input [, int mode])
  4782. Returns info about what characters are used in input */
  4783. PHP_FUNCTION(count_chars)
  4784. {
  4785. zend_string *input;
  4786. int chars[256];
  4787. zend_long mymode=0;
  4788. const unsigned char *buf;
  4789. int inx;
  4790. char retstr[256];
  4791. size_t retlen=0;
  4792. size_t tmp = 0;
  4793. ZEND_PARSE_PARAMETERS_START(1, 2)
  4794. Z_PARAM_STR(input)
  4795. Z_PARAM_OPTIONAL
  4796. Z_PARAM_LONG(mymode)
  4797. ZEND_PARSE_PARAMETERS_END();
  4798. if (mymode < 0 || mymode > 4) {
  4799. zend_argument_value_error(2, "must be between 1 and 4 (inclusive)");
  4800. RETURN_THROWS();
  4801. }
  4802. buf = (const unsigned char *) ZSTR_VAL(input);
  4803. memset((void*) chars, 0, sizeof(chars));
  4804. while (tmp < ZSTR_LEN(input)) {
  4805. chars[*buf]++;
  4806. buf++;
  4807. tmp++;
  4808. }
  4809. if (mymode < 3) {
  4810. array_init(return_value);
  4811. }
  4812. for (inx = 0; inx < 256; inx++) {
  4813. switch (mymode) {
  4814. case 0:
  4815. add_index_long(return_value, inx, chars[inx]);
  4816. break;
  4817. case 1:
  4818. if (chars[inx] != 0) {
  4819. add_index_long(return_value, inx, chars[inx]);
  4820. }
  4821. break;
  4822. case 2:
  4823. if (chars[inx] == 0) {
  4824. add_index_long(return_value, inx, chars[inx]);
  4825. }
  4826. break;
  4827. case 3:
  4828. if (chars[inx] != 0) {
  4829. retstr[retlen++] = inx;
  4830. }
  4831. break;
  4832. case 4:
  4833. if (chars[inx] == 0) {
  4834. retstr[retlen++] = inx;
  4835. }
  4836. break;
  4837. }
  4838. }
  4839. if (mymode >= 3 && mymode <= 4) {
  4840. RETURN_STRINGL(retstr, retlen);
  4841. }
  4842. }
  4843. /* }}} */
  4844. /* {{{ php_strnatcmp
  4845. */
  4846. static void php_strnatcmp(INTERNAL_FUNCTION_PARAMETERS, int fold_case)
  4847. {
  4848. zend_string *s1, *s2;
  4849. ZEND_PARSE_PARAMETERS_START(2, 2)
  4850. Z_PARAM_STR(s1)
  4851. Z_PARAM_STR(s2)
  4852. ZEND_PARSE_PARAMETERS_END();
  4853. RETURN_LONG(strnatcmp_ex(ZSTR_VAL(s1), ZSTR_LEN(s1),
  4854. ZSTR_VAL(s2), ZSTR_LEN(s2),
  4855. fold_case));
  4856. }
  4857. /* }}} */
  4858. PHPAPI int string_natural_compare_function_ex(zval *result, zval *op1, zval *op2, zend_bool case_insensitive) /* {{{ */
  4859. {
  4860. zend_string *tmp_str1, *tmp_str2;
  4861. zend_string *str1 = zval_get_tmp_string(op1, &tmp_str1);
  4862. zend_string *str2 = zval_get_tmp_string(op2, &tmp_str2);
  4863. ZVAL_LONG(result, strnatcmp_ex(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2), case_insensitive));
  4864. zend_tmp_string_release(tmp_str1);
  4865. zend_tmp_string_release(tmp_str2);
  4866. return SUCCESS;
  4867. }
  4868. /* }}} */
  4869. PHPAPI int string_natural_case_compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
  4870. {
  4871. return string_natural_compare_function_ex(result, op1, op2, 1);
  4872. }
  4873. /* }}} */
  4874. PHPAPI int string_natural_compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
  4875. {
  4876. return string_natural_compare_function_ex(result, op1, op2, 0);
  4877. }
  4878. /* }}} */
  4879. /* {{{ proto int strnatcmp(string s1, string s2)
  4880. Returns the result of string comparison using 'natural' algorithm */
  4881. PHP_FUNCTION(strnatcmp)
  4882. {
  4883. php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  4884. }
  4885. /* }}} */
  4886. /* {{{ proto array localeconv(void)
  4887. Returns numeric formatting information based on the current locale */
  4888. PHP_FUNCTION(localeconv)
  4889. {
  4890. zval grouping, mon_grouping;
  4891. int len, i;
  4892. ZEND_PARSE_PARAMETERS_NONE();
  4893. array_init(return_value);
  4894. array_init(&grouping);
  4895. array_init(&mon_grouping);
  4896. {
  4897. struct lconv currlocdata;
  4898. localeconv_r( &currlocdata );
  4899. /* Grab the grouping data out of the array */
  4900. len = (int)strlen(currlocdata.grouping);
  4901. for (i = 0; i < len; i++) {
  4902. add_index_long(&grouping, i, currlocdata.grouping[i]);
  4903. }
  4904. /* Grab the monetary grouping data out of the array */
  4905. len = (int)strlen(currlocdata.mon_grouping);
  4906. for (i = 0; i < len; i++) {
  4907. add_index_long(&mon_grouping, i, currlocdata.mon_grouping[i]);
  4908. }
  4909. add_assoc_string(return_value, "decimal_point", currlocdata.decimal_point);
  4910. add_assoc_string(return_value, "thousands_sep", currlocdata.thousands_sep);
  4911. add_assoc_string(return_value, "int_curr_symbol", currlocdata.int_curr_symbol);
  4912. add_assoc_string(return_value, "currency_symbol", currlocdata.currency_symbol);
  4913. add_assoc_string(return_value, "mon_decimal_point", currlocdata.mon_decimal_point);
  4914. add_assoc_string(return_value, "mon_thousands_sep", currlocdata.mon_thousands_sep);
  4915. add_assoc_string(return_value, "positive_sign", currlocdata.positive_sign);
  4916. add_assoc_string(return_value, "negative_sign", currlocdata.negative_sign);
  4917. add_assoc_long( return_value, "int_frac_digits", currlocdata.int_frac_digits);
  4918. add_assoc_long( return_value, "frac_digits", currlocdata.frac_digits);
  4919. add_assoc_long( return_value, "p_cs_precedes", currlocdata.p_cs_precedes);
  4920. add_assoc_long( return_value, "p_sep_by_space", currlocdata.p_sep_by_space);
  4921. add_assoc_long( return_value, "n_cs_precedes", currlocdata.n_cs_precedes);
  4922. add_assoc_long( return_value, "n_sep_by_space", currlocdata.n_sep_by_space);
  4923. add_assoc_long( return_value, "p_sign_posn", currlocdata.p_sign_posn);
  4924. add_assoc_long( return_value, "n_sign_posn", currlocdata.n_sign_posn);
  4925. }
  4926. zend_hash_str_update(Z_ARRVAL_P(return_value), "grouping", sizeof("grouping")-1, &grouping);
  4927. zend_hash_str_update(Z_ARRVAL_P(return_value), "mon_grouping", sizeof("mon_grouping")-1, &mon_grouping);
  4928. }
  4929. /* }}} */
  4930. /* {{{ proto int strnatcasecmp(string s1, string s2)
  4931. Returns the result of case-insensitive string comparison using 'natural' algorithm */
  4932. PHP_FUNCTION(strnatcasecmp)
  4933. {
  4934. php_strnatcmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  4935. }
  4936. /* }}} */
  4937. /* {{{ proto int|false substr_count(string haystack, string needle [, int offset [, int length]])
  4938. Returns the number of times a substring occurs in the string */
  4939. PHP_FUNCTION(substr_count)
  4940. {
  4941. char *haystack, *needle;
  4942. zend_long offset = 0, length = 0;
  4943. zend_bool length_is_null = 1;
  4944. zend_long count = 0;
  4945. size_t haystack_len, needle_len;
  4946. const char *p, *endp;
  4947. char cmp;
  4948. ZEND_PARSE_PARAMETERS_START(2, 4)
  4949. Z_PARAM_STRING(haystack, haystack_len)
  4950. Z_PARAM_STRING(needle, needle_len)
  4951. Z_PARAM_OPTIONAL
  4952. Z_PARAM_LONG(offset)
  4953. Z_PARAM_LONG_OR_NULL(length, length_is_null)
  4954. ZEND_PARSE_PARAMETERS_END();
  4955. if (needle_len == 0) {
  4956. zend_argument_value_error(2, "must be a non-empty string");
  4957. RETURN_THROWS();
  4958. }
  4959. p = haystack;
  4960. endp = p + haystack_len;
  4961. if (offset < 0) {
  4962. offset += (zend_long)haystack_len;
  4963. }
  4964. if ((offset < 0) || ((size_t)offset > haystack_len)) {
  4965. zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
  4966. RETURN_THROWS();
  4967. }
  4968. p += offset;
  4969. if (!length_is_null) {
  4970. if (length < 0) {
  4971. length += (haystack_len - offset);
  4972. }
  4973. if (length < 0 || ((size_t)length > (haystack_len - offset))) {
  4974. php_error_docref(NULL, E_WARNING, "Invalid length value");
  4975. RETURN_FALSE;
  4976. }
  4977. endp = p + length;
  4978. }
  4979. if (needle_len == 1) {
  4980. cmp = needle[0];
  4981. while ((p = memchr(p, cmp, endp - p))) {
  4982. count++;
  4983. p++;
  4984. }
  4985. } else {
  4986. while ((p = (char*)php_memnstr(p, needle, needle_len, endp))) {
  4987. p += needle_len;
  4988. count++;
  4989. }
  4990. }
  4991. RETURN_LONG(count);
  4992. }
  4993. /* }}} */
  4994. /* {{{ proto string str_pad(string input, int pad_length [, string pad_string [, int pad_type]])
  4995. Returns input string padded on the left or right to specified length with pad_string */
  4996. PHP_FUNCTION(str_pad)
  4997. {
  4998. /* Input arguments */
  4999. zend_string *input; /* Input string */
  5000. zend_long pad_length; /* Length to pad to */
  5001. /* Helper variables */
  5002. size_t num_pad_chars; /* Number of padding characters (total - input size) */
  5003. char *pad_str = " "; /* Pointer to padding string */
  5004. size_t pad_str_len = 1;
  5005. zend_long pad_type_val = STR_PAD_RIGHT; /* The padding type value */
  5006. size_t i, left_pad=0, right_pad=0;
  5007. zend_string *result = NULL; /* Resulting string */
  5008. ZEND_PARSE_PARAMETERS_START(2, 4)
  5009. Z_PARAM_STR(input)
  5010. Z_PARAM_LONG(pad_length)
  5011. Z_PARAM_OPTIONAL
  5012. Z_PARAM_STRING(pad_str, pad_str_len)
  5013. Z_PARAM_LONG(pad_type_val)
  5014. ZEND_PARSE_PARAMETERS_END();
  5015. /* If resulting string turns out to be shorter than input string,
  5016. we simply copy the input and return. */
  5017. if (pad_length < 0 || (size_t)pad_length <= ZSTR_LEN(input)) {
  5018. RETURN_STR_COPY(input);
  5019. }
  5020. if (pad_str_len == 0) {
  5021. zend_argument_value_error(3, "must be a non-empty string");
  5022. RETURN_THROWS();
  5023. }
  5024. if (pad_type_val < STR_PAD_LEFT || pad_type_val > STR_PAD_BOTH) {
  5025. zend_argument_value_error(4, "must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH");
  5026. RETURN_THROWS();
  5027. }
  5028. num_pad_chars = pad_length - ZSTR_LEN(input);
  5029. result = zend_string_safe_alloc(1, ZSTR_LEN(input), num_pad_chars, 0);
  5030. ZSTR_LEN(result) = 0;
  5031. /* We need to figure out the left/right padding lengths. */
  5032. switch (pad_type_val) {
  5033. case STR_PAD_RIGHT:
  5034. left_pad = 0;
  5035. right_pad = num_pad_chars;
  5036. break;
  5037. case STR_PAD_LEFT:
  5038. left_pad = num_pad_chars;
  5039. right_pad = 0;
  5040. break;
  5041. case STR_PAD_BOTH:
  5042. left_pad = num_pad_chars / 2;
  5043. right_pad = num_pad_chars - left_pad;
  5044. break;
  5045. }
  5046. /* First we pad on the left. */
  5047. for (i = 0; i < left_pad; i++)
  5048. ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
  5049. /* Then we copy the input string. */
  5050. memcpy(ZSTR_VAL(result) + ZSTR_LEN(result), ZSTR_VAL(input), ZSTR_LEN(input));
  5051. ZSTR_LEN(result) += ZSTR_LEN(input);
  5052. /* Finally, we pad on the right. */
  5053. for (i = 0; i < right_pad; i++)
  5054. ZSTR_VAL(result)[ZSTR_LEN(result)++] = pad_str[i % pad_str_len];
  5055. ZSTR_VAL(result)[ZSTR_LEN(result)] = '\0';
  5056. RETURN_NEW_STR(result);
  5057. }
  5058. /* }}} */
  5059. /* {{{ proto array|int|null sscanf(string str, string format [, string ...])
  5060. Implements an ANSI C compatible sscanf */
  5061. PHP_FUNCTION(sscanf)
  5062. {
  5063. zval *args = NULL;
  5064. char *str, *format;
  5065. size_t str_len, format_len;
  5066. int result, num_args = 0;
  5067. ZEND_PARSE_PARAMETERS_START(2, -1)
  5068. Z_PARAM_STRING(str, str_len)
  5069. Z_PARAM_STRING(format, format_len)
  5070. Z_PARAM_VARIADIC('*', args, num_args)
  5071. ZEND_PARSE_PARAMETERS_END();
  5072. result = php_sscanf_internal(str, format, num_args, args, 0, return_value);
  5073. if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
  5074. WRONG_PARAM_COUNT;
  5075. }
  5076. }
  5077. /* }}} */
  5078. /* static zend_string *php_str_rot13(zend_string *str) {{{ */
  5079. #ifdef __SSE2__
  5080. #include <emmintrin.h>
  5081. #endif
  5082. static zend_string *php_str_rot13(zend_string *str)
  5083. {
  5084. zend_string *ret;
  5085. const char *p, *e;
  5086. char *target;
  5087. if (UNEXPECTED(ZSTR_LEN(str) == 0)) {
  5088. return ZSTR_EMPTY_ALLOC();
  5089. }
  5090. ret = zend_string_alloc(ZSTR_LEN(str), 0);
  5091. p = ZSTR_VAL(str);
  5092. e = p + ZSTR_LEN(str);
  5093. target = ZSTR_VAL(ret);
  5094. #ifdef __SSE2__
  5095. if (e - p > 15) {
  5096. const __m128i a_minus_1 = _mm_set1_epi8('a' - 1);
  5097. const __m128i m_plus_1 = _mm_set1_epi8('m' + 1);
  5098. const __m128i n_minus_1 = _mm_set1_epi8('n' - 1);
  5099. const __m128i z_plus_1 = _mm_set1_epi8('z' + 1);
  5100. const __m128i A_minus_1 = _mm_set1_epi8('A' - 1);
  5101. const __m128i M_plus_1 = _mm_set1_epi8('M' + 1);
  5102. const __m128i N_minus_1 = _mm_set1_epi8('N' - 1);
  5103. const __m128i Z_plus_1 = _mm_set1_epi8('Z' + 1);
  5104. const __m128i add = _mm_set1_epi8(13);
  5105. const __m128i sub = _mm_set1_epi8(-13);
  5106. do {
  5107. __m128i in, gt, lt, cmp, delta;
  5108. delta = _mm_setzero_si128();
  5109. in = _mm_loadu_si128((__m128i *)p);
  5110. gt = _mm_cmpgt_epi8(in, a_minus_1);
  5111. lt = _mm_cmplt_epi8(in, m_plus_1);
  5112. cmp = _mm_and_si128(lt, gt);
  5113. if (_mm_movemask_epi8(cmp)) {
  5114. cmp = _mm_and_si128(cmp, add);
  5115. delta = _mm_or_si128(delta, cmp);
  5116. }
  5117. gt = _mm_cmpgt_epi8(in, n_minus_1);
  5118. lt = _mm_cmplt_epi8(in, z_plus_1);
  5119. cmp = _mm_and_si128(lt, gt);
  5120. if (_mm_movemask_epi8(cmp)) {
  5121. cmp = _mm_and_si128(cmp, sub);
  5122. delta = _mm_or_si128(delta, cmp);
  5123. }
  5124. gt = _mm_cmpgt_epi8(in, A_minus_1);
  5125. lt = _mm_cmplt_epi8(in, M_plus_1);
  5126. cmp = _mm_and_si128(lt, gt);
  5127. if (_mm_movemask_epi8(cmp)) {
  5128. cmp = _mm_and_si128(cmp, add);
  5129. delta = _mm_or_si128(delta, cmp);
  5130. }
  5131. gt = _mm_cmpgt_epi8(in, N_minus_1);
  5132. lt = _mm_cmplt_epi8(in, Z_plus_1);
  5133. cmp = _mm_and_si128(lt, gt);
  5134. if (_mm_movemask_epi8(cmp)) {
  5135. cmp = _mm_and_si128(cmp, sub);
  5136. delta = _mm_or_si128(delta, cmp);
  5137. }
  5138. in = _mm_add_epi8(in, delta);
  5139. _mm_storeu_si128((__m128i *)target, in);
  5140. p += 16;
  5141. target += 16;
  5142. } while (e - p > 15);
  5143. }
  5144. #endif
  5145. while (p < e) {
  5146. if (*p >= 'a' && *p <= 'z') {
  5147. *target++ = 'a' + (((*p++ - 'a') + 13) % 26);
  5148. } else if (*p >= 'A' && *p <= 'Z') {
  5149. *target++ = 'A' + (((*p++ - 'A') + 13) % 26);
  5150. } else {
  5151. *target++ = *p++;
  5152. }
  5153. }
  5154. *target = '\0';
  5155. return ret;
  5156. }
  5157. /* }}} */
  5158. /* {{{ proto string str_rot13(string str)
  5159. Perform the rot13 transform on a string */
  5160. PHP_FUNCTION(str_rot13)
  5161. {
  5162. zend_string *arg;
  5163. ZEND_PARSE_PARAMETERS_START(1, 1)
  5164. Z_PARAM_STR(arg)
  5165. ZEND_PARSE_PARAMETERS_END();
  5166. RETURN_STR(php_str_rot13(arg));
  5167. }
  5168. /* }}} */
  5169. static void php_string_shuffle(char *str, zend_long len) /* {{{ */
  5170. {
  5171. zend_long n_elems, rnd_idx, n_left;
  5172. char temp;
  5173. /* The implementation is stolen from array_data_shuffle */
  5174. /* Thus the characteristics of the randomization are the same */
  5175. n_elems = len;
  5176. if (n_elems <= 1) {
  5177. return;
  5178. }
  5179. n_left = n_elems;
  5180. while (--n_left) {
  5181. rnd_idx = php_mt_rand_range(0, n_left);
  5182. if (rnd_idx != n_left) {
  5183. temp = str[n_left];
  5184. str[n_left] = str[rnd_idx];
  5185. str[rnd_idx] = temp;
  5186. }
  5187. }
  5188. }
  5189. /* }}} */
  5190. /* {{{ proto string str_shuffle(string str)
  5191. Shuffles string. One permutation of all possible is created */
  5192. PHP_FUNCTION(str_shuffle)
  5193. {
  5194. zend_string *arg;
  5195. ZEND_PARSE_PARAMETERS_START(1, 1)
  5196. Z_PARAM_STR(arg)
  5197. ZEND_PARSE_PARAMETERS_END();
  5198. RETVAL_STRINGL(ZSTR_VAL(arg), ZSTR_LEN(arg));
  5199. if (Z_STRLEN_P(return_value) > 1) {
  5200. php_string_shuffle(Z_STRVAL_P(return_value), (zend_long) Z_STRLEN_P(return_value));
  5201. }
  5202. }
  5203. /* }}} */
  5204. /* {{{ proto array|int str_word_count(string str, [int format [, string charlist]])
  5205. Counts the number of words inside a string. If format of 1 is specified,
  5206. then the function will return an array containing all the words
  5207. found inside the string. If format of 2 is specified, then the function
  5208. will return an associated array where the position of the word is the key
  5209. and the word itself is the value.
  5210. For the purpose of this function, 'word' is defined as a locale dependent
  5211. string containing alphabetic characters, which also may contain, but not start
  5212. with "'" and "-" characters.
  5213. */
  5214. PHP_FUNCTION(str_word_count)
  5215. {
  5216. zend_string *str;
  5217. char *char_list = NULL, ch[256];
  5218. const char *p, *e, *s;
  5219. size_t char_list_len = 0, word_count = 0;
  5220. zend_long type = 0;
  5221. ZEND_PARSE_PARAMETERS_START(1, 3)
  5222. Z_PARAM_STR(str)
  5223. Z_PARAM_OPTIONAL
  5224. Z_PARAM_LONG(type)
  5225. Z_PARAM_STRING(char_list, char_list_len)
  5226. ZEND_PARSE_PARAMETERS_END();
  5227. switch(type) {
  5228. case 1:
  5229. case 2:
  5230. array_init(return_value);
  5231. if (!ZSTR_LEN(str)) {
  5232. return;
  5233. }
  5234. break;
  5235. case 0:
  5236. if (!ZSTR_LEN(str)) {
  5237. RETURN_LONG(0);
  5238. }
  5239. /* nothing to be done */
  5240. break;
  5241. default:
  5242. zend_argument_value_error(2, "must be a valid format value");
  5243. RETURN_THROWS();
  5244. }
  5245. if (char_list) {
  5246. php_charmask((unsigned char *)char_list, char_list_len, ch);
  5247. }
  5248. p = ZSTR_VAL(str);
  5249. e = ZSTR_VAL(str) + ZSTR_LEN(str);
  5250. /* first character cannot be ' or -, unless explicitly allowed by the user */
  5251. if ((*p == '\'' && (!char_list || !ch['\''])) || (*p == '-' && (!char_list || !ch['-']))) {
  5252. p++;
  5253. }
  5254. /* last character cannot be -, unless explicitly allowed by the user */
  5255. if (*(e - 1) == '-' && (!char_list || !ch['-'])) {
  5256. e--;
  5257. }
  5258. while (p < e) {
  5259. s = p;
  5260. while (p < e && (isalpha((unsigned char)*p) || (char_list && ch[(unsigned char)*p]) || *p == '\'' || *p == '-')) {
  5261. p++;
  5262. }
  5263. if (p > s) {
  5264. switch (type)
  5265. {
  5266. case 1:
  5267. add_next_index_stringl(return_value, s, p - s);
  5268. break;
  5269. case 2:
  5270. add_index_stringl(return_value, (s - ZSTR_VAL(str)), s, p - s);
  5271. break;
  5272. default:
  5273. word_count++;
  5274. break;
  5275. }
  5276. }
  5277. p++;
  5278. }
  5279. if (!type) {
  5280. RETURN_LONG(word_count);
  5281. }
  5282. }
  5283. /* }}} */
  5284. /* {{{ proto array str_split(string str [, int split_length])
  5285. Convert a string to an array. If split_length is specified, break the string down into chunks each split_length characters long. */
  5286. PHP_FUNCTION(str_split)
  5287. {
  5288. zend_string *str;
  5289. zend_long split_length = 1;
  5290. const char *p;
  5291. size_t n_reg_segments;
  5292. ZEND_PARSE_PARAMETERS_START(1, 2)
  5293. Z_PARAM_STR(str)
  5294. Z_PARAM_OPTIONAL
  5295. Z_PARAM_LONG(split_length)
  5296. ZEND_PARSE_PARAMETERS_END();
  5297. if (split_length <= 0) {
  5298. zend_argument_value_error(2, "must be greater than 0");
  5299. RETURN_THROWS();
  5300. }
  5301. if (0 == ZSTR_LEN(str) || (size_t)split_length >= ZSTR_LEN(str)) {
  5302. array_init_size(return_value, 1);
  5303. add_next_index_stringl(return_value, ZSTR_VAL(str), ZSTR_LEN(str));
  5304. return;
  5305. }
  5306. array_init_size(return_value, (uint32_t)(((ZSTR_LEN(str) - 1) / split_length) + 1));
  5307. n_reg_segments = ZSTR_LEN(str) / split_length;
  5308. p = ZSTR_VAL(str);
  5309. while (n_reg_segments-- > 0) {
  5310. add_next_index_stringl(return_value, p, split_length);
  5311. p += split_length;
  5312. }
  5313. if (p != (ZSTR_VAL(str) + ZSTR_LEN(str))) {
  5314. add_next_index_stringl(return_value, p, (ZSTR_VAL(str) + ZSTR_LEN(str) - p));
  5315. }
  5316. }
  5317. /* }}} */
  5318. /* {{{ proto string|false strpbrk(string haystack, string char_list)
  5319. Search a string for any of a set of characters */
  5320. PHP_FUNCTION(strpbrk)
  5321. {
  5322. zend_string *haystack, *char_list;
  5323. const char *haystack_ptr, *cl_ptr;
  5324. ZEND_PARSE_PARAMETERS_START(2, 2)
  5325. Z_PARAM_STR(haystack)
  5326. Z_PARAM_STR(char_list)
  5327. ZEND_PARSE_PARAMETERS_END();
  5328. if (!ZSTR_LEN(char_list)) {
  5329. zend_argument_value_error(2, "must be a non-empty string");
  5330. RETURN_THROWS();
  5331. }
  5332. for (haystack_ptr = ZSTR_VAL(haystack); haystack_ptr < (ZSTR_VAL(haystack) + ZSTR_LEN(haystack)); ++haystack_ptr) {
  5333. for (cl_ptr = ZSTR_VAL(char_list); cl_ptr < (ZSTR_VAL(char_list) + ZSTR_LEN(char_list)); ++cl_ptr) {
  5334. if (*cl_ptr == *haystack_ptr) {
  5335. RETURN_STRINGL(haystack_ptr, (ZSTR_VAL(haystack) + ZSTR_LEN(haystack) - haystack_ptr));
  5336. }
  5337. }
  5338. }
  5339. RETURN_FALSE;
  5340. }
  5341. /* }}} */
  5342. /* {{{ proto int|false substr_compare(string main_str, string str, int offset [, int length [, bool case_sensitivity]])
  5343. Binary safe optionally case insensitive comparison of 2 strings from an offset, up to length characters */
  5344. PHP_FUNCTION(substr_compare)
  5345. {
  5346. zend_string *s1, *s2;
  5347. zend_long offset, len=0;
  5348. zend_bool len_is_default=1;
  5349. zend_bool cs=0;
  5350. size_t cmp_len;
  5351. ZEND_PARSE_PARAMETERS_START(3, 5)
  5352. Z_PARAM_STR(s1)
  5353. Z_PARAM_STR(s2)
  5354. Z_PARAM_LONG(offset)
  5355. Z_PARAM_OPTIONAL
  5356. Z_PARAM_LONG_OR_NULL(len, len_is_default)
  5357. Z_PARAM_BOOL(cs)
  5358. ZEND_PARSE_PARAMETERS_END();
  5359. if (!len_is_default && len <= 0) {
  5360. if (len == 0) {
  5361. RETURN_LONG(0L);
  5362. } else {
  5363. zend_argument_value_error(4, "must be greater than or equal to 0");
  5364. RETURN_THROWS();
  5365. }
  5366. }
  5367. if (offset < 0) {
  5368. offset = ZSTR_LEN(s1) + offset;
  5369. offset = (offset < 0) ? 0 : offset;
  5370. }
  5371. if ((size_t)offset > ZSTR_LEN(s1)) {
  5372. php_error_docref(NULL, E_WARNING, "The start position cannot exceed initial string length");
  5373. RETURN_FALSE;
  5374. }
  5375. cmp_len = len ? (size_t)len : MAX(ZSTR_LEN(s2), (ZSTR_LEN(s1) - offset));
  5376. if (!cs) {
  5377. RETURN_LONG(zend_binary_strncmp(ZSTR_VAL(s1) + offset, (ZSTR_LEN(s1) - offset), ZSTR_VAL(s2), ZSTR_LEN(s2), cmp_len));
  5378. } else {
  5379. RETURN_LONG(zend_binary_strncasecmp_l(ZSTR_VAL(s1) + offset, (ZSTR_LEN(s1) - offset), ZSTR_VAL(s2), ZSTR_LEN(s2), cmp_len));
  5380. }
  5381. }
  5382. /* }}} */
  5383. /* {{{ */
  5384. static zend_string *php_utf8_encode(const char *s, size_t len)
  5385. {
  5386. size_t pos = len;
  5387. zend_string *str;
  5388. unsigned char c;
  5389. str = zend_string_safe_alloc(len, 2, 0, 0);
  5390. ZSTR_LEN(str) = 0;
  5391. while (pos > 0) {
  5392. /* The lower 256 codepoints of Unicode are identical to Latin-1,
  5393. * so we don't need to do any mapping here. */
  5394. c = (unsigned char)(*s);
  5395. if (c < 0x80) {
  5396. ZSTR_VAL(str)[ZSTR_LEN(str)++] = (char) c;
  5397. /* We only account for the single-byte and two-byte cases because
  5398. * we're only dealing with the first 256 Unicode codepoints. */
  5399. } else {
  5400. ZSTR_VAL(str)[ZSTR_LEN(str)++] = (0xc0 | (c >> 6));
  5401. ZSTR_VAL(str)[ZSTR_LEN(str)++] = (0x80 | (c & 0x3f));
  5402. }
  5403. pos--;
  5404. s++;
  5405. }
  5406. ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
  5407. str = zend_string_truncate(str, ZSTR_LEN(str), 0);
  5408. return str;
  5409. }
  5410. /* }}} */
  5411. /* {{{ */
  5412. static zend_string *php_utf8_decode(const char *s, size_t len)
  5413. {
  5414. size_t pos = 0;
  5415. unsigned int c;
  5416. zend_string *str;
  5417. str = zend_string_alloc(len, 0);
  5418. ZSTR_LEN(str) = 0;
  5419. while (pos < len) {
  5420. int status = FAILURE;
  5421. c = php_next_utf8_char((const unsigned char*)s, (size_t) len, &pos, &status);
  5422. /* The lower 256 codepoints of Unicode are identical to Latin-1,
  5423. * so we don't need to do any mapping here beyond replacing non-Latin-1
  5424. * characters. */
  5425. if (status == FAILURE || c > 0xFFU) {
  5426. c = '?';
  5427. }
  5428. ZSTR_VAL(str)[ZSTR_LEN(str)++] = c;
  5429. }
  5430. ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
  5431. if (ZSTR_LEN(str) < len) {
  5432. str = zend_string_truncate(str, ZSTR_LEN(str), 0);
  5433. }
  5434. return str;
  5435. }
  5436. /* }}} */
  5437. /* {{{ proto string utf8_encode(string data)
  5438. Encodes an ISO-8859-1 string to UTF-8 */
  5439. PHP_FUNCTION(utf8_encode)
  5440. {
  5441. char *arg;
  5442. size_t arg_len;
  5443. ZEND_PARSE_PARAMETERS_START(1, 1)
  5444. Z_PARAM_STRING(arg, arg_len)
  5445. ZEND_PARSE_PARAMETERS_END();
  5446. RETURN_STR(php_utf8_encode(arg, arg_len));
  5447. }
  5448. /* }}} */
  5449. /* {{{ proto string utf8_decode(string data)
  5450. Converts a UTF-8 encoded string to ISO-8859-1 */
  5451. PHP_FUNCTION(utf8_decode)
  5452. {
  5453. char *arg;
  5454. size_t arg_len;
  5455. ZEND_PARSE_PARAMETERS_START(1, 1)
  5456. Z_PARAM_STRING(arg, arg_len)
  5457. ZEND_PARSE_PARAMETERS_END();
  5458. RETURN_STR(php_utf8_decode(arg, arg_len));
  5459. }
  5460. /* }}} */