+}
+
+static inline zend_bool check_str(const char *chk_str, size_t chk_len, const char *sep_str, size_t sep_len) {
+ return 0 < sep_len && chk_len >= sep_len && !memcmp(chk_str, sep_str, sep_len);
+}
+
+static size_t check_sep(php_http_params_state_t *state, php_http_params_token_t **separators)
+{
+ php_http_params_token_t **sep = separators;
+
+ if (sep) while (*sep) {
+ if (check_str(state->input.str, state->input.len, (*sep)->str, (*sep)->len)) {
+ return (*sep)->len;
+ }
+ ++sep;
+ }
+ return 0;
+}
+
+static void skip_sep(size_t skip, php_http_params_state_t *state, php_http_params_token_t **param, php_http_params_token_t **arg, php_http_params_token_t **val TSRMLS_DC)
+{
+ size_t sep_len;
+
+ state->input.str += skip;
+ state->input.len -= skip;
+
+ while ( (param && (sep_len = check_sep(state, param)))
+ || (arg && (sep_len = check_sep(state, arg)))
+ || (val && (sep_len = check_sep(state, val)))
+ ) {
+ state->input.str += sep_len;
+ state->input.len -= sep_len;
+ }
+}
+
+PHP_HTTP_API HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts TSRMLS_DC)
+{
+ php_http_params_state_t state = {{NULL,0}, {NULL,0}, {NULL,0}, {NULL,0}, {NULL,NULL,NULL}};
+
+ state.input.str = opts->input.str;
+ state.input.len = opts->input.len;
+
+ if (!params) {
+ ALLOC_HASHTABLE(params);
+ ZEND_INIT_SYMTABLE(params);
+ }
+
+ while (state.input.len) {
+ if (*state.input.str == '\\') {
+ ++state.input.str;
+ --state.input.len;
+ } else if (!state.param.str) {
+ /* initialize */
+ skip_sep(0, &state, opts->param, opts->arg, opts->val TSRMLS_CC);
+ state.param.str = state.input.str;
+ } else {
+ size_t sep_len;
+ /* are we at a param separator? */
+ if (0 < (sep_len = check_sep(&state, opts->param))) {
+ push_param(params, &state, opts TSRMLS_CC);
+
+ skip_sep(sep_len, &state, opts->param, opts->arg, opts->val TSRMLS_CC);
+
+ /* start off with a new param */
+ state.param.str = state.input.str;
+ state.param.len = 0;
+ state.arg.str = NULL;
+ state.arg.len = 0;
+ state.val.str = NULL;
+ state.val.len = 0;
+ } else
+ /* are we at an arg separator? */
+ if (0 < (sep_len = check_sep(&state, opts->arg))) {
+ push_param(params, &state, opts TSRMLS_CC);
+
+ skip_sep(sep_len, &state, NULL, opts->arg, opts->val TSRMLS_CC);
+
+ /* continue with a new arg */
+ state.arg.str = state.input.str;
+ state.arg.len = 0;
+ state.val.str = NULL;
+ state.val.len = 0;
+ } else
+ /* are we at a val separator? */
+ if (0 < (sep_len = check_sep(&state, opts->val))) {
+ /* only handle separator if we're not already reading in a val */
+ if (!state.val.str) {
+ push_param(params, &state, opts TSRMLS_CC);
+
+ skip_sep(sep_len, &state, NULL, NULL, opts->val TSRMLS_CC);
+
+ state.val.str = state.input.str;
+ state.val.len = 0;
+ }
+ }
+ }
+
+ if (state.input.len) {
+ ++state.input.str;
+ --state.input.len;
+ }
+ }
+ /* finalize */
+ push_param(params, &state, opts TSRMLS_CC);
+
+ return params;
+}
+
+static void shift_param(php_http_buffer_t *buf, char *key_str, size_t key_len, zval **zvalue, const char *css, size_t csl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+ if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
+ zval *tmp = php_http_zsep(1, IS_ARRAY, *zvalue);
+
+ do {
+ char *str;
+ size_t len;
+ zval *tmp2;
+
+ if (PHP_HTTP_BUFFER_LEN(buf)) {
+ php_http_buffer_append(buf, css, csl);
+ }
+
+ prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC);
+ php_http_buffer_append(buf, str, len);
+ efree(str);
+
+ tmp2 = php_http_zsep(1, IS_ARRAY, tmp);
+ prepare_value(flags, tmp2 TSRMLS_CC);
+ php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2));
+ zval_ptr_dtor(&tmp2);
+
+ zvalue = &tmp;
+ while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zvalue), (void *) &zvalue) && Z_TYPE_PP(zvalue) == IS_ARRAY);
+
+ if (Z_TYPE_PP(zvalue) != IS_BOOL) {
+ php_http_buffer_append(buf, vss, vsl);
+
+ tmp2 = php_http_ztyp(IS_STRING, *zvalue);
+ prepare_value(flags, tmp2 TSRMLS_CC);
+ php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2));
+ zval_ptr_dtor(&tmp2);
+ } else if (!Z_BVAL_PP(zvalue)) {
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_appends(buf, "0");
+ }
+
+ } while (SUCCESS == zend_hash_move_forward(Z_ARRVAL_P(tmp)) && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void *) &zvalue));
+
+ zval_ptr_dtor(&tmp);
+
+ } else {
+ zval *tmp;
+ char *str;
+ size_t len;
+
+ if (PHP_HTTP_BUFFER_LEN(buf)) {
+ php_http_buffer_append(buf, css, csl);
+ }
+
+ prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC);
+ php_http_buffer_append(buf, str, len);
+ efree(str);
+
+ if (Z_TYPE_PP(zvalue) != IS_BOOL) {
+ tmp = php_http_ztyp(IS_STRING, *zvalue);
+ prepare_value(flags, tmp TSRMLS_CC);
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
+ zval_ptr_dtor(&tmp);
+ } else if (!Z_BVAL_PP(zvalue)) {
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_appends(buf, "0");
+ }
+ }
+}
+
+PHP_HTTP_API php_http_buffer_t *php_http_params_to_string(php_http_buffer_t *buf, HashTable *params, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+ zval **zparam;
+ HashPosition pos, pos1;
+ php_http_array_hashkey_t key = php_http_array_hashkey_init(0), key1 = php_http_array_hashkey_init(0);
+
+ if (!buf) {
+ buf = php_http_buffer_init(NULL);
+ }
+
+ FOREACH_HASH_KEYVAL(pos, params, key, zparam) {
+ zval **zvalue, **zargs;
+
+ if (Z_TYPE_PP(zparam) != IS_ARRAY || SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) {
+ zvalue = zparam;
+ }
+
+ php_http_array_hashkey_stringify(&key);
+ shift_param(buf, key.str, key.len - 1, zvalue, pss, psl, vss, vsl, flags TSRMLS_CC);
+ php_http_array_hashkey_stringfree(&key);
+
+ if (Z_TYPE_PP(zparam) == IS_ARRAY && SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("arguments"), (void *) &zvalue)) {
+ zvalue = zparam;
+ }
+
+ FOREACH_KEYVAL(pos1, *zvalue, key1, zargs) {
+ if (zvalue == zparam && key1.type == HASH_KEY_IS_STRING && !strcmp(key1.str, "value")) {
+ continue;
+ }
+
+ php_http_array_hashkey_stringify(&key1);
+ shift_param(buf, key1.str, key1.len - 1, zargs, ass, asl, vss, vsl, flags TSRMLS_CC);
+ php_http_array_hashkey_stringfree(&key1);
+ }
+ }
+/*
+ FOREACH_HASH_KEYVAL(pos1, params, key1, zparam) {
+ if (PHP_HTTP_BUFFER_LEN(buf)) {
+ php_http_buffer_append(buf, pss, psl);
+ }
+
+ if (key1.type == HASH_KEY_IS_STRING) {
+ char *key;
+ size_t len;
+
+ prepare_key(flags, key1.str, key1.len - 1, &key, &len TSRMLS_CC);
+ php_http_buffer_append(buf, key, len);
+ efree(key);
+ } else {
+ php_http_buffer_appendf(buf, "%lu", key1.num);
+ }
+
+ if (Z_TYPE_PP(zparam) != IS_ARRAY) {
+ zval *tmp = php_http_ztyp(IS_STRING, *zparam);
+
+ prepare_value(flags, tmp TSRMLS_CC);
+
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
+ zval_ptr_dtor(&tmp);
+ } else {
+ zval **zvalue, **zargs, **zarg;
+
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) {
+ if (Z_TYPE_PP(zvalue) != IS_BOOL) {
+ zval *tmp, *tmp2;
+
+ if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
+ tmp = php_http_zsep(1, IS_ARRAY, *zvalue);
+ do {
+ if (key1.type == HASH_KEY_IS_STRING) {
+ char *key;
+ size_t len;
+
+ prepare_key(flags, key1.str, key1.len - 1, &key, &len TSRMLS_CC);
+ php_http_buffer_append(buf, key, len);
+ efree(key);
+ } else {
+ php_http_buffer_appendf(buf, "%lu", key1.num);
+ }
+
+ tmp2 = php_http_zsep(1, IS_ARRAY, tmp);
+ prepare_value(flags, tmp2 TSRMLS_CC);
+ php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2));
+ zval_ptr_dtor(&tmp2);
+
+ while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void *) &zvalue) && Z_TYPE_PP(zvalue) == IS_ARRAY);
+
+ tmp2 = php_http_ztyp(IS_STRING, *zvalue);
+ prepare_value(flags, tmp2 TSRMLS_CC);
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2));
+ zval_ptr_dtor(&tmp2);
+ } while (SUCCESS == zend_hash_move_forward(Z_ARRVAL_P(tmp)));
+ zval_ptr_dtor(&tmp);