+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 && *chk_str == *sep_str && !memcmp(chk_str + 1, sep_str + 1, sep_len - 1);
+}
+
+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 (state->quotes || state->escape) {
+ return 0;
+ }
+
+ 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)
+{
+ 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;
+ }
+}
+
+HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts)
+{
+ php_http_params_state_t state = {{NULL,0}, {NULL,0}, {NULL,0}, {NULL,0}, {NULL,NULL,NULL}, 0, 0};
+
+ 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.escape) {
+ state.quotes = !state.quotes;
+ } else {
+ state.escape = (*state.input.str == '\\');
+ }
+
+ if (!state.param.str) {
+ /* initialize */
+ skip_sep(0, &state, opts->param, opts->arg, opts->val);
+ 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);
+
+ skip_sep(sep_len, &state, opts->param, opts->arg, opts->val);
+
+ /* 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;
+
+ continue;
+
+ } else
+ /* are we at an arg separator? */
+ if (0 < (sep_len = check_sep(&state, opts->arg))) {
+ push_param(params, &state, opts);
+
+ skip_sep(sep_len, &state, NULL, opts->arg, opts->val);
+
+ /* continue with a new arg */
+ state.arg.str = state.input.str;
+ state.arg.len = 0;
+ state.val.str = NULL;
+ state.val.len = 0;
+
+ continue;
+
+ } 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);
+
+ skip_sep(sep_len, &state, NULL, NULL, opts->val);
+
+ state.val.str = state.input.str;
+ state.val.len = 0;
+
+ continue;
+ }
+ }
+ }
+
+ if (state.input.len) {
+ ++state.input.str;
+ --state.input.len;
+ }
+ }
+ /* finalize */
+ push_param(params, &state, opts);
+
+ return params;
+}
+
+static inline void shift_key(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags)
+{
+ char *str;
+ size_t len;
+
+ if (buf->used) {
+ php_http_buffer_append(buf, ass, asl);
+ }
+
+ prepare_key(flags, key_str, key_len, &str, &len);
+ php_http_buffer_append(buf, str, len);
+ efree(str);
+}
+
+static inline void shift_rfc5987(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags)
+{
+ HashTable *ht = HASH_OF(zvalue);
+ zval *zdata, tmp;
+ zend_string *zs;
+ php_http_arrkey_t key = {0};
+
+ if ((zdata = zend_hash_get_current_data(ht))
+ && HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(ht, &key.key, &key.h)
+ ) {
+ php_http_arrkey_stringify(&key, NULL);
+ php_http_buffer_appendf(buf, "*%.*sutf-8'%.*s'",
+ (int) (vsl > INT_MAX ? INT_MAX : vsl), vss,
+ (int) (key.key->len > INT_MAX ? INT_MAX : key.key->len), key.key->val);
+ php_http_arrkey_dtor(&key);
+
+ if (Z_TYPE_P(zdata) == IS_INDIRECT) {
+ zdata = Z_INDIRECT_P(zdata);
+ }
+ zs = zval_get_string(zdata);
+ ZVAL_STR(&tmp, zs);
+ prepare_value(flags | PHP_HTTP_PARAMS_URLENCODED, &tmp);
+ php_http_buffer_append(buf, Z_STRVAL(tmp), Z_STRLEN(tmp));
+ zval_ptr_dtor(&tmp);
+ }
+}
+
+static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags)
+{
+ zval tmp;
+ zend_string *zs;
+
+ switch (Z_TYPE_P(zvalue)) {
+ case IS_TRUE:
+ break;
+
+ case IS_FALSE:
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_appends(buf, "0");
+ break;
+
+ default:
+ zs = zval_get_string(zvalue);
+
+ ZVAL_STR(&tmp, zs);
+ prepare_value(flags, &tmp);
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_append(buf, Z_STRVAL(tmp), Z_STRLEN(tmp));
+
+ zval_ptr_dtor(&tmp);
+ break;
+ }
+}
+
+static void shift_arg(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags)
+{
+ if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
+ php_http_arrkey_t key;
+ HashTable *ht = HASH_OF(zvalue);
+ zval *val;
+ zend_bool rfc5987 = !strcmp(key_str, "*rfc5987*");
+
+ if (!rfc5987) {
+ shift_key(buf, key_str, key_len, ass, asl, flags);
+ }
+ ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, val)
+ {
+ /* did you mean recursion? */
+ php_http_arrkey_stringify(&key, NULL);
+ if (rfc5987 && (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT)) {
+ shift_key(buf, key.key->val, key.key->len, ass, asl, flags);
+ shift_rfc5987(buf, val, vss, vsl, flags);
+ } else {
+ shift_arg(buf, key.key->val, key.key->len, val, ass, asl, vss, vsl, flags);
+ }
+ php_http_arrkey_dtor(&key);
+ }
+ ZEND_HASH_FOREACH_END();
+ } else {
+ shift_key(buf, key_str, key_len, ass, asl, flags);
+ shift_val(buf, zvalue, vss, vsl, flags);
+ }
+}
+
+static void shift_param(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags, zend_bool rfc5987)
+{
+ if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
+ /* treat as arguments, unless we care for dimensions or rfc5987 */
+ if (flags & PHP_HTTP_PARAMS_DIMENSION) {
+ php_http_buffer_t *keybuf = php_http_buffer_from_string(key_str, key_len);
+ prepare_dimension(buf, keybuf, zvalue, pss, psl, vss, vsl, flags);
+ php_http_buffer_free(&keybuf);
+ } else if (rfc5987) {
+ shift_key(buf, key_str, key_len, pss, psl, flags);
+ shift_rfc5987(buf, zvalue, vss, vsl, flags);
+ } else {
+ shift_arg(buf, key_str, key_len, zvalue, ass, asl, vss, vsl, flags);
+ }
+ } else {
+ shift_key(buf, key_str, key_len, pss, psl, flags);
+ shift_val(buf, zvalue, vss, vsl, flags);
+ }
+}
+
+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)
+{
+ zval *zparam;
+ php_http_arrkey_t key;
+ zend_bool rfc5987 = 0;
+
+ if (!buf) {
+ buf = php_http_buffer_init(NULL);
+ }
+
+ ZEND_HASH_FOREACH_KEY_VAL(params, key.h, key.key, zparam)
+ {
+ zval *zvalue, *zargs;
+
+ if (Z_TYPE_P(zparam) != IS_ARRAY) {
+ zvalue = zparam;
+ } else {
+ if (!(zvalue = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("value")))) {
+ if (!(zvalue = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("*rfc5987*")))) {
+ zvalue = zparam;