+}
+
+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;
+}
+
+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.param.str) {
+ /* initialize */
+ 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);
+
+ /* start off with a new param */
+ state.param.str = state.input.str + sep_len;
+ 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);
+
+ /* continue with a new arg */
+ state.arg.str = state.input.str + sep_len;
+ 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);
+
+ state.val.str = state.input.str + sep_len;
+ state.val.len = 0;
+ }
+ }
+ }
+
+ ++state.input.str;
+ --state.input.len;
+ }
+ /* finalize */
+ push_param(params, &state, opts TSRMLS_CC);
+
+ return params;
+}
+
+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 TSRMLS_DC)
+{
+ zval **zparam;
+ HashPosition pos1, pos2;
+ php_http_array_hashkey_t key1 = php_http_array_hashkey_init(0), key2 = php_http_array_hashkey_init(0);
+
+ if (!buf) {
+ buf = php_http_buffer_init(NULL);
+ }
+
+ FOREACH_HASH_KEYVAL(pos1, params, key1, zparam) {
+ /* new param ? */
+ if (PHP_HTTP_BUFFER_LEN(buf)) {
+ php_http_buffer_append(buf, pss, psl);
+ }
+
+ /* add name */
+ if (key1.type == HASH_KEY_IS_STRING) {
+ php_http_buffer_append(buf, key1.str, key1.len - 1);
+ } else {
+ php_http_buffer_appendf(buf, "%lu", key1.num);
+ }
+
+ if (Z_TYPE_PP(zparam) == IS_ARRAY) {
+ zval **zvalue, **zargs, **zarg;
+
+ /* got a value? */
+ if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) {
+ if (Z_TYPE_PP(zvalue) != IS_BOOL) {
+ zval *tmp = php_http_ztyp(IS_STRING, *zvalue);
+
+ 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");
+ }
+ }
+ /* add arguments */
+ if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("arguments"), (void *) &zargs)) {
+ zargs = zparam;
+ }
+
+ if (Z_TYPE_PP(zargs) == IS_ARRAY) {
+ FOREACH_KEYVAL(pos2, *zargs, key2, zarg) {
+ /* skip "value" if zargs == zparam */
+ if (zargs == zparam && key2.type == HASH_KEY_IS_STRING && !strcmp(key2.str, "value")) {
+ continue;