void php_explode(lua_State *L, char *delim, size_t delim_len, char *str, size_t str_len, long limit) { size_t i = 1; char *p1, *p2, *endp; endp = str + str_len; p1 = str; p2 = php_memnstr(str, delim, delim_len, endp); lua_newtable(L); if (p2 == NULL) { lua_pushinteger(L, i++); lua_pushlstring(L, p1, str_len); lua_settable(L, -3); } else { do { lua_pushinteger(L, i++); lua_pushlstring(L, p1, p2 - p1); lua_settable(L, -3); p1 = p2 + delim_len; } while ((p2 = php_memnstr(p1, delim, delim_len, endp)) != NULL && --limit > 1); if (p1 <= endp) { lua_pushinteger(L, i++); lua_pushlstring(L, p1, endp-p1); lua_settable(L, -3); } } }
/** {{{ split str to array */ static void monip_split(zval *return_value, char *str, uint str_len TSRMLS_DC){ char *p1, *p2, *endp; array_init(return_value); endp = str + str_len; //end of ip_addr p1 = str; p2 = php_memnstr(p1, ZEND_STRL("\t"), endp); if(p2 == NULL){ add_next_index_stringl(return_value, p1, str_len, 1); }else{ do{ add_next_index_stringl(return_value, p1, p2 - p1, 1); p1 = p2 + 1; }while((p2 = php_memnstr(p1, ZEND_STRL("\t"), endp)) != NULL); if (p1 <= endp){ add_next_index_stringl(return_value, p1, endp-p1, 1); } } }
/** * Find the position of the first occurrence of a substring in a string */ int php_can_strpos(char *haystack, char *needle, int offset) { if (!haystack) { return FAILURE; } int haystack_len = strlen(haystack) + 1; if (offset < 0 || offset > haystack_len) { return FAILURE; } char * found = php_memnstr( haystack + offset, needle, strlen(needle), haystack + haystack_len ); if (!found) { return FAILURE; } return found - haystack; }
/* {{{ strstr_common_handler */ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_case) { char *haystack, *needle; const char *found; size_t haystack_len, needle_len; int32_t ret_pos, uchar_pos; zend_bool part = 0; if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|b", &haystack, &haystack_len, &needle, &needle_len, &part) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strstr: unable to parse input param", 0 ); RETURN_FALSE; } if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 ); RETURN_FALSE; } if ( !f_ignore_case ) { /* ASCII optimization: quick check to see if the string might be there * I realize that 'offset' is 'grapheme count offset' but will work in spite of that */ found = php_memnstr(haystack, needle, needle_len, haystack + haystack_len); /* if it isn't there the we are done */ if ( !found ) { RETURN_FALSE; } /* if it is there, and if the haystack is ascii, we are all done */ if ( grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0 ) { size_t found_offset = found - haystack; if (part) { RETURN_STRINGL(haystack, found_offset); } else { RETURN_STRINGL(found, haystack_len - found_offset); } } } /* need to work in utf16 */ ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, 0, &uchar_pos, f_ignore_case, 0 /*last */ ); if ( ret_pos < 0 ) { RETURN_FALSE; } /* uchar_pos is the 'nth' Unicode character position of the needle */ ret_pos = 0; U8_FWD_N(haystack, ret_pos, haystack_len, uchar_pos); if (part) { RETURN_STRINGL(haystack, ret_pos); } else { RETURN_STRINGL(haystack + ret_pos, haystack_len - ret_pos); } }
PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err, int arg_start) /* {{{ */ { static int optchr = 0; static int dash = 0; /* have already seen the - */ static char **prev_optarg = NULL; php_optidx = -1; if(prev_optarg && prev_optarg != optarg) { /* reset the state */ optchr = 0; dash = 0; } prev_optarg = optarg; if (*optind >= argc) { return(EOF); } if (!dash) { if ((argv[*optind][0] != '-')) { return(EOF); } else { if (!argv[*optind][1]) { /* * use to specify stdin. Need to let pgm process this and * the following args */ return(EOF); } } } if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) { char *pos; int arg_end = strlen(argv[*optind])-1; /* '--' indicates end of args if not followed by a known long option name */ if (argv[*optind][2] == '\0') { (*optind)++; return(EOF); } arg_start = 2; /* Check for <arg>=<val> */ if ((pos = php_memnstr(&argv[*optind][arg_start], "=", 1, argv[*optind]+arg_end)) != NULL) { arg_end = pos-&argv[*optind][arg_start]; arg_start++; } else { arg_end--; } while (1) { php_optidx++; if (opts[php_optidx].opt_char == '-') { (*optind)++; return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err)); } else if (opts[php_optidx].opt_name && !strncmp(&argv[*optind][2], opts[php_optidx].opt_name, arg_end) && arg_end == strlen(opts[php_optidx].opt_name)) { break; } } optchr = 0; dash = 0; arg_start += strlen(opts[php_optidx].opt_name); } else { if (!dash) { dash = 1; optchr = 1; } /* Check if the guy tries to do a -: kind of flag */ if (argv[*optind][optchr] == ':') { dash = 0; (*optind)++; return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err)); } arg_start = 1 + optchr; } if (php_optidx < 0) { while (1) { php_optidx++; if (opts[php_optidx].opt_char == '-') { int errind = *optind; int errchr = optchr; if (!argv[*optind][optchr+1]) { dash = 0; (*optind)++; } else { optchr++; arg_start++; } return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err)); } else if (argv[*optind][optchr] == opts[php_optidx].opt_char) { break; } } } if (opts[php_optidx].need_param) { /* Check for cases where the value of the argument is in the form -<arg> <val>, -<arg>=<varl> or -<arg><val> */ dash = 0; if (!argv[*optind][arg_start]) { (*optind)++; if (*optind == argc) { /* Was the value required or is it optional? */ if (opts[php_optidx].need_param == 1) { return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err)); } /* Optional value is not supported with -<arg> <val> style */ } else if (opts[php_optidx].need_param == 1) { *optarg = argv[(*optind)++]; } } else if (argv[*optind][arg_start] == '=') { arg_start++; *optarg = &argv[*optind][arg_start]; (*optind)++; } else { *optarg = &argv[*optind][arg_start]; (*optind)++; } return opts[php_optidx].opt_char; } else { /* multiple options specified as one (exclude long opts) */ if (arg_start >= 2 && !((argv[*optind][0] == '-') && (argv[*optind][1] == '-'))) { if (!argv[*optind][optchr+1]) { dash = 0; (*optind)++; } else { optchr++; } } else { (*optind)++; } return opts[php_optidx].opt_char; } assert(0); return(0); /* never reached */ }
static inline int php_url_scanner_reset_var_impl(zend_string *name, int encode, int type) { char *start, *end, *limit; size_t separator_len; smart_str sname = {0}; smart_str hname = {0}; smart_str url_app = {0}; smart_str form_app = {0}; zend_string *encoded; int ret = SUCCESS; zend_bool sep_removed = 0; url_adapt_state_ex_t *url_state; if (type) { url_state = &BG(url_adapt_session_ex); } else { url_state = &BG(url_adapt_output_ex); } /* Short circuit check. Only check url_app. */ if (!url_state->url_app.s || !ZSTR_LEN(url_state->url_app.s)) { return SUCCESS; } if (encode) { encoded = php_raw_url_encode(ZSTR_VAL(name), ZSTR_LEN(name)); smart_str_appendl(&sname, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); zend_string_free(encoded); encoded = php_escape_html_entities_ex((unsigned char *)ZSTR_VAL(name), ZSTR_LEN(name), 0, ENT_QUOTES|ENT_SUBSTITUTE, SG(default_charset), 0); smart_str_appendl(&hname, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); zend_string_free(encoded); } else { smart_str_appendl(&sname, ZSTR_VAL(name), ZSTR_LEN(name)); smart_str_appendl(&hname, ZSTR_VAL(name), ZSTR_LEN(name)); } smart_str_0(&sname); smart_str_0(&hname); smart_str_append_smart_str(&url_app, &sname); smart_str_appendc(&url_app, '='); smart_str_0(&url_app); smart_str_appends(&form_app, "<input type=\"hidden\" name=\""); smart_str_append_smart_str(&form_app, &hname); smart_str_appends(&form_app, "\" value=\""); smart_str_0(&form_app); /* Short circuit check. Only check url_app. */ start = (char *) php_memnstr(ZSTR_VAL(url_state->url_app.s), ZSTR_VAL(url_app.s), ZSTR_LEN(url_app.s), ZSTR_VAL(url_state->url_app.s) + ZSTR_LEN(url_state->url_app.s)); if (!start) { ret = FAILURE; goto finish; } /* Get end of url var */ limit = ZSTR_VAL(url_state->url_app.s) + ZSTR_LEN(url_state->url_app.s); end = start + ZSTR_LEN(url_app.s); separator_len = strlen(PG(arg_separator).output); while (end < limit) { if (!memcmp(end, PG(arg_separator).output, separator_len)) { end += separator_len; sep_removed = 1; break; } end++; } /* Remove all when this is the only rewrite var */ if (ZSTR_LEN(url_state->url_app.s) == end - start) { php_url_scanner_reset_vars_impl(type); goto finish; } /* Check preceeding separator */ if (!sep_removed && start - PG(arg_separator).output >= separator_len && !memcmp(start - separator_len, PG(arg_separator).output, separator_len)) { start -= separator_len; } /* Remove partially */ memmove(start, end, ZSTR_LEN(url_state->url_app.s) - (end - ZSTR_VAL(url_state->url_app.s))); ZSTR_LEN(url_state->url_app.s) -= end - start; ZSTR_VAL(url_state->url_app.s)[ZSTR_LEN(url_state->url_app.s)] = '\0'; /* Remove form var */ start = (char *) php_memnstr(ZSTR_VAL(url_state->form_app.s), ZSTR_VAL(form_app.s), ZSTR_LEN(form_app.s), ZSTR_VAL(url_state->form_app.s) + ZSTR_LEN(url_state->form_app.s)); if (!start) { /* Should not happen */ ret = FAILURE; php_url_scanner_reset_vars_impl(type); goto finish; } /* Get end of form var */ limit = ZSTR_VAL(url_state->form_app.s) + ZSTR_LEN(url_state->form_app.s); end = start + ZSTR_LEN(form_app.s); while (end < limit) { if (*end == '>') { end += 1; break; } end++; } /* Remove partially */ memmove(start, end, ZSTR_LEN(url_state->form_app.s) - (end - ZSTR_VAL(url_state->form_app.s))); ZSTR_LEN(url_state->form_app.s) -= end - start; ZSTR_VAL(url_state->form_app.s)[ZSTR_LEN(url_state->form_app.s)] = '\0'; finish: smart_str_free(&url_app); smart_str_free(&form_app); smart_str_free(&sname); smart_str_free(&hname); return ret; }