PageRenderTime 83ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/ext/standard/string.c

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