Merge branch 'master' into phpng
[m6w6/ext-http] / php_http_querystring.c
index d72337f..5f4eff8 100644 (file)
 
 #define QS_MERGE 1
 
-static inline void php_http_querystring_set(zval *instance, zval *params, int flags TSRMLS_DC)
+static inline void php_http_querystring_set(zval *instance, zval *params, int flags)
 {
-       zval *qa;
+       zval qa;
+
+       array_init(&qa);
 
        if (flags & QS_MERGE) {
-               qa = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0 TSRMLS_CC));
-       } else {
-               MAKE_STD_ZVAL(qa);
-               array_init(qa);
+               zval old_tmp, *old = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &old_tmp);
+
+               ZVAL_DEREF(old);
+               if (Z_TYPE_P(old) == IS_ARRAY) {
+                       array_copy(Z_ARRVAL_P(old), Z_ARRVAL(qa));
+               }
        }
 
-       php_http_querystring_update(qa, params, NULL TSRMLS_CC);
-       zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), qa TSRMLS_CC);
+       php_http_querystring_update(&qa, params, NULL);
+       zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), &qa);
        zval_ptr_dtor(&qa);
 }
 
-static inline void php_http_querystring_str(zval *instance, zval *return_value TSRMLS_DC)
+static inline void php_http_querystring_str(zval *instance, zval *return_value)
 {
-       zval *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qa_tmp);
 
+       ZVAL_DEREF(qa);
        if (Z_TYPE_P(qa) == IS_ARRAY) {
-               php_http_querystring_update(qa, NULL, return_value TSRMLS_CC);
+               php_http_querystring_update(qa, NULL, return_value);
        } else {
                RETURN_EMPTY_STRING();
        }
 }
 
-static inline void php_http_querystring_get(zval *this_ptr, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC)
+static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value)
 {
-       zval **arrval, *qarray = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       zval *arrval, qarray_tmp, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qarray_tmp);
+
+       ZVAL_DEREF(qarray);
+       if ((Z_TYPE_P(qarray) == IS_ARRAY) && (arrval = zend_symtable_str_find(Z_ARRVAL_P(qarray), name, name_len))) {
+               if (type && type != Z_TYPE_P(arrval)) {
+                       zval tmp;
 
-       if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) {
-               if (type) {
-                       zval *value = php_http_ztyp(type, *arrval);
-                       RETVAL_ZVAL(value, 1, 1);
+                       ZVAL_DUP(&tmp, arrval);
+                       convert_to_explicit_type(&tmp, type);
+                       RETVAL_ZVAL(&tmp, 0, 0);
                } else {
-                       RETVAL_ZVAL(*arrval, 1, 0);
+                       RETVAL_ZVAL(arrval, 1, 0);
                }
 
                if (del) {
-                       zval *delarr;
+                       zval delarr;
 
-                       MAKE_STD_ZVAL(delarr);
-                       array_init(delarr);
-                       add_assoc_null_ex(delarr, name, name_len + 1);
-                       php_http_querystring_set(this_ptr, delarr, QS_MERGE TSRMLS_CC);
+                       array_init(&delarr);
+                       add_assoc_null_ex(&delarr, name, name_len);
+                       php_http_querystring_set(instance, &delarr, QS_MERGE);
                        zval_ptr_dtor(&delarr);
                }
        } else if(defval) {
@@ -79,89 +87,85 @@ static inline void php_http_querystring_get(zval *this_ptr, int type, char *name
 }
 
 #ifdef PHP_HTTP_HAVE_ICONV
-ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie, const char *oe TSRMLS_DC)
+ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie, const char *oe)
 {
-       HashPosition pos;
-       zval **entry = NULL;
-       char *xlate_str = NULL, *xkey;
-       size_t xlate_len = 0, xlen;
-       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+       zval *entry;
+       zend_string *xkey, *xstr;
+       php_http_arrkey_t key;
        
-       FOREACH_KEYVAL(pos, src, key, entry) {
-               if (key.type == HASH_KEY_IS_STRING) {
-                       if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(key.str, key.len-1, &xkey, &xlen, oe, ie)) {
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", key.len-1, key.str, ie, oe);
+       ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(src), key.h, key.key, entry)
+       {
+               if (key.key) {
+                       if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(key.key->val, key.key->len, &xkey, oe, ie)) {
+                               php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", key.key->len, key.key->val, ie, oe);
                                return FAILURE;
                        }
                }
                
-               if (Z_TYPE_PP(entry) == IS_STRING) {
-                       if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry), &xlate_str, &xlate_len, oe, ie)) {
-                               if (key.type == HASH_KEY_IS_STRING) {
-                                       efree(xkey);
+               if (Z_TYPE_P(entry) == IS_STRING) {
+                       if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_P(entry), Z_STRLEN_P(entry), &xstr, oe, ie)) {
+                               if (key.key) {
+                                       zend_string_release(xkey);
                                }
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_PP(entry), Z_STRVAL_PP(entry), ie, oe);
+                               php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_P(entry), Z_STRVAL_P(entry), ie, oe);
                                return FAILURE;
                        }
-                       if (key.type == HASH_KEY_IS_STRING) {
-                               add_assoc_stringl_ex(dst, xkey, xlen+1, xlate_str, xlate_len, 0);
+                       if (key.key) {
+                               add_assoc_str_ex(dst, xkey->val, xkey->len, xstr);
                        } else {
-                               add_index_stringl(dst, key.num, xlate_str, xlate_len, 0);
+                               add_index_str(dst, key.h, xstr);
                        }
-               } else if (Z_TYPE_PP(entry) == IS_ARRAY) {
-                       zval *subarray;
+               } else if (Z_TYPE_P(entry) == IS_ARRAY) {
+                       zval subarray;
                        
-                       MAKE_STD_ZVAL(subarray);
-                       array_init(subarray);
-                       if (key.type == HASH_KEY_IS_STRING) {
-                               add_assoc_zval_ex(dst, xkey, xlen+1, subarray);
+                       array_init(&subarray);
+                       if (key.key) {
+                               add_assoc_zval_ex(dst, xkey->val, xkey->len, &subarray);
                        } else {
-                               add_index_zval(dst, key.num, subarray);
+                               add_index_zval(dst, key.h, &subarray);
                        }
-                       if (SUCCESS != php_http_querystring_xlate(subarray, *entry, ie, oe TSRMLS_CC)) {
-                               if (key.type == HASH_KEY_IS_STRING) {
-                                       efree(xkey);
+                       if (SUCCESS != php_http_querystring_xlate(&subarray, entry, ie, oe)) {
+                               if (key.key) {
+                                       zend_string_release(xkey);
                                }
                                return FAILURE;
                        }
                }
                
-               if (key.type == HASH_KEY_IS_STRING) {
-                       efree(xkey);
+               if (key.key) {
+                       zend_string_release(xkey);
                }
        }
+       ZEND_HASH_FOREACH_END();
+
        return SUCCESS;
 }
 #endif /* HAVE_ICONV */
 
-ZEND_RESULT_CODE php_http_querystring_ctor(zval *instance, zval *params TSRMLS_DC)
+ZEND_RESULT_CODE php_http_querystring_ctor(zval *instance, zval *params)
 {
-       php_http_querystring_set(instance, params, 0 TSRMLS_CC);
+       php_http_querystring_set(instance, params, 0);
        return SUCCESS;
 }
 
-static int apply_querystring(void *pData TSRMLS_DC)
+static int apply_querystring(zval *val)
 {
-       zval **val = pData;
+       if (Z_TYPE_P(val) == IS_ARRAY) {
+               zval *zvalue;
 
-       if (Z_TYPE_PP(val) == IS_ARRAY) {
-               zval **zvalue;
+               if ((zvalue = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("value")))) {
+                       zval tmp;
 
-               if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("value"), (void *) &zvalue)) {
-                       zval *tmp = *val;
-
-                       Z_ADDREF_PP(zvalue);
-                       *val = *zvalue;
-                       zval_dtor(tmp);
-                       Z_TYPE_P(tmp) = IS_NULL;
-                       zval_ptr_dtor(&tmp);
+                       ZVAL_COPY(&tmp, zvalue);
+                       zval_dtor(val);
+                       ZVAL_COPY_VALUE(val, &tmp);
                }
        }
 
        return ZEND_HASH_APPLY_KEEP;
 }
 
-ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size_t len TSRMLS_DC)
+ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size_t len)
 {
        ZEND_RESULT_CODE rv = FAILURE;
        php_http_params_opts_t opts;
@@ -177,26 +181,23 @@ ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size
        opts.val = vsepp;
        opts.flags = PHP_HTTP_PARAMS_QUERY;
 
-       if (SUCCESS == php_http_ini_entry(ZEND_STRL("arg_separator.input"), &asi_str, &asi_len, 0 TSRMLS_CC) && asi_len) {
-               zval *arr;
+       if (SUCCESS == php_http_ini_entry(ZEND_STRL("arg_separator.input"), &asi_str, &asi_len, 0) && asi_len) {
+               zval arr;
 
-               MAKE_STD_ZVAL(arr);
-               array_init_size(arr, asi_len);
+               array_init_size(&arr, asi_len);
 
                do {
-                       add_next_index_stringl(arr, asi_str++, 1, 1);
+                       add_next_index_stringl(&arr, asi_str++, 1);
                } while (*asi_str);
 
-               opts.param = php_http_params_separator_init(arr TSRMLS_CC);
-
+               opts.param = php_http_params_separator_init(&arr);
                zval_ptr_dtor(&arr);
        }
 
-       MAKE_STD_ZVAL(opts.defval);
-       ZVAL_NULL(opts.defval);
+       ZVAL_NULL(&opts.defval);
 
-       if (php_http_params_parse(ht, &opts TSRMLS_CC)) {
-               zend_hash_apply(ht, apply_querystring TSRMLS_CC);
+       if (php_http_params_parse(ht, &opts)) {
+               zend_hash_apply(ht, apply_querystring);
                rv = SUCCESS;
        }
 
@@ -209,7 +210,7 @@ ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size
        return rv;
 }
 
-ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *outstring TSRMLS_DC)
+ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *outstring)
 {
        /* enforce proper type */
        if (Z_TYPE_P(qarray) != IS_ARRAY) {
@@ -218,90 +219,91 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
 
        /* modify qarray */
        if (params) {
-               HashPosition pos;
-               HashTable *ptr;
-               php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-               zval **params_entry,  **qarray_entry;
-               zval zv, *zv_ptr = NULL;
+               HashTable *ht;
+               php_http_arrkey_t key;
+               zval zv, *params_entry, *qarray_entry;
 
-               INIT_PZVAL(&zv);
                ZVAL_NULL(&zv);
 
                /* squeeze the hash out of the zval */
-               if (Z_TYPE_P(params) == IS_OBJECT && instanceof_function(Z_OBJCE_P(params), php_http_querystring_class_entry TSRMLS_CC)) {
-                       zv_ptr = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0 TSRMLS_CC));
-                       ptr = Z_ARRVAL_P(zv_ptr);
+               if (Z_TYPE_P(params) == IS_OBJECT && instanceof_function(Z_OBJCE_P(params), php_http_querystring_class_entry)) {
+                       zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0, &qa_tmp);
+
+                       ZVAL_DEREF(qa);
+                       convert_to_array(qa);
+                       ht = Z_ARRVAL_P(qa);
                } else if (Z_TYPE_P(params) == IS_OBJECT || Z_TYPE_P(params) == IS_ARRAY) {
-                       ptr = HASH_OF(params);
+                       ht = HASH_OF(params);
                } else {
-                       zv_ptr = php_http_ztyp(IS_STRING, params);
+                       zend_string *zs = zval_get_string(params);
+
                        array_init(&zv);
-                       php_http_querystring_parse(Z_ARRVAL(zv), Z_STRVAL_P(zv_ptr), Z_STRLEN_P(zv_ptr) TSRMLS_CC);
-                       zval_ptr_dtor(&zv_ptr);
-                       zv_ptr = NULL;
-                       ptr = Z_ARRVAL(zv);
+                       php_http_querystring_parse(Z_ARRVAL(zv), zs->val, zs->len);
+                       zend_string_release(zs);
+
+                       ht = Z_ARRVAL(zv);
                }
 
-               FOREACH_HASH_KEYVAL(pos, ptr, key, params_entry) {
+               ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, params_entry)
+               {
                        /* only public properties */
-                       if (key.type != HASH_KEY_IS_STRING || *key.str) {
-                               if (Z_TYPE_PP(params_entry) == IS_NULL) {
+                       if (!key.key || *key.key->val) {
+                               if (Z_TYPE_P(params_entry) == IS_NULL) {
                                        /*
                                         * delete
                                         */
-                                       if (key.type == HASH_KEY_IS_STRING) {
-                                               zend_hash_del(Z_ARRVAL_P(qarray), key.str, key.len);
+                                       if (key.key) {
+                                               zend_hash_del(Z_ARRVAL_P(qarray), key.key);
                                        } else {
-                                               zend_hash_index_del(Z_ARRVAL_P(qarray), key.num);
+                                               zend_hash_index_del(Z_ARRVAL_P(qarray), key.h);
                                        }
-                               } else if (     ((key.type == HASH_KEY_IS_STRING) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key.str, key.len, (void *) &qarray_entry)))
-                                               ||      ((key.type == HASH_KEY_IS_LONG) && (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(qarray), key.num, (void *) &qarray_entry)))) {
+                               } else if (     ((key.key) && (qarray_entry = zend_hash_find(Z_ARRVAL_P(qarray), key.key)))
+                                               ||      ((!key.key) && (qarray_entry = zend_hash_index_find(Z_ARRVAL_P(qarray), key.h)))) {
                                        /*
                                         * update
                                         */
-                                       zval equal, *entry = NULL;
+                                       zval equal, tmp, *entry = &tmp;
 
+                                       ZVAL_UNDEF(&tmp);
                                        /* recursive */
-                                       if (Z_TYPE_PP(params_entry) == IS_ARRAY || Z_TYPE_PP(params_entry) == IS_OBJECT) {
-                                               entry = php_http_zsep(1, IS_ARRAY, *qarray_entry);
-                                               php_http_querystring_update(entry, *params_entry, NULL TSRMLS_CC);
-                                       } else if ((FAILURE == is_equal_function(&equal, *qarray_entry, *params_entry TSRMLS_CC)) || !Z_BVAL(equal)) {
-                                               Z_ADDREF_PP(params_entry);
-                                               entry = *params_entry;
+                                       if (Z_TYPE_P(params_entry) == IS_ARRAY || Z_TYPE_P(params_entry) == IS_OBJECT) {
+                                               ZVAL_DUP(entry, qarray_entry);
+                                               convert_to_array(entry);
+                                               php_http_querystring_update(entry, params_entry, NULL);
+                                       } else if ((FAILURE == is_equal_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) {
+                                               Z_TRY_ADDREF_P(params_entry);
+                                               entry = params_entry;
                                        }
 
                                        if (entry) {
-                                               if (key.type == HASH_KEY_IS_STRING) {
-                                                       zend_hash_update(Z_ARRVAL_P(qarray), key.str, key.len, (void *) &entry, sizeof(zval *), NULL);
+                                               if (key.key) {
+                                                       zend_hash_update(Z_ARRVAL_P(qarray), key.key, entry);
                                                } else {
-                                                       zend_hash_index_update(Z_ARRVAL_P(qarray), key.num, (void *) &entry, sizeof(zval *), NULL);
+                                                       zend_hash_index_update(Z_ARRVAL_P(qarray), key.h, entry);
                                                }
                                        }
                                } else {
-                                       zval *entry;
+                                       zval entry, *entry_ptr = &entry;
                                        /*
                                         * add
                                         */
-                                       if (Z_TYPE_PP(params_entry) == IS_OBJECT) {
-                                               MAKE_STD_ZVAL(entry);
-                                               array_init(entry);
-                                               php_http_querystring_update(entry, *params_entry, NULL TSRMLS_CC);
+                                       if (Z_TYPE_P(params_entry) == IS_OBJECT) {
+                                               array_init(&entry);
+                                               php_http_querystring_update(&entry, params_entry, NULL);
                                        } else {
-                                               Z_ADDREF_PP(params_entry);
-                                               entry = *params_entry;
+                                               Z_TRY_ADDREF_P(params_entry);
+                                               entry_ptr = params_entry;
                                        }
-                                       if (key.type == HASH_KEY_IS_STRING) {
-                                               add_assoc_zval_ex(qarray, key.str, key.len, entry);
+                                       if (key.key) {
+                                               add_assoc_zval_ex(qarray, key.key->val, key.key->len, entry_ptr);
                                        } else {
-                                               add_index_zval(qarray, key.num, entry);
+                                               add_index_zval(qarray, key.h, entry_ptr);
                                        }
                                }
                        }
                }
-               /* clean up */
-               if (zv_ptr) {
-                       zval_ptr_dtor(&zv_ptr);
-               }
+               ZEND_HASH_FOREACH_END();
+
                zval_dtor(&zv);
        }
 
@@ -310,11 +312,11 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
                char *s;
                size_t l;
 
-               if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qarray), NULL, 0, &s, &l TSRMLS_CC)) {
+               if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qarray), NULL, 0, &s, &l)) {
                        zval_dtor(outstring);
-                       ZVAL_STRINGL(outstring, s, l, 0);
+                       ZVAL_STR(outstring, php_http_cs2zs(s, l));
                } else {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to encode query string");
+                       php_error_docref(NULL, E_WARNING, "Failed to encode query string");
                        return FAILURE;
                }
        }
@@ -330,63 +332,53 @@ PHP_METHOD(HttpQueryString, __construct)
        zval *params = NULL;
        zend_error_handling zeh;
        
-       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &params), invalid_arg, return);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &params), invalid_arg, return);
 
-       zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh TSRMLS_CC);
-       php_http_querystring_set(getThis(), params, 0 TSRMLS_CC);
-       zend_restore_error_handling(&zeh TSRMLS_CC);
+       zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh);
+       php_http_querystring_set(getThis(), params, 0);
+       zend_restore_error_handling(&zeh);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getGlobalInstance, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, getGlobalInstance)
 {
-       zval *instance;
+       zval *instance, *_GET;
+       zend_string *zs;
 
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
-       instance = *zend_std_get_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), 0 PHP_HTTP_ZEND_LITERAL_CCN TSRMLS_CC);
-
-       if (Z_TYPE_P(instance) != IS_OBJECT) {
-               zval **_GET = NULL;
-
-               zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC);
+       zs = zend_string_init(ZEND_STRL("instance"), 0);
+       instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0);
+       zend_string_release(zs);
 
-               if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET))
-               &&      (Z_TYPE_PP(_GET) == IS_ARRAY)
-               ) {
-                       MAKE_STD_ZVAL(instance);
-                       ZVAL_OBJVAL(instance, php_http_querystring_object_new(php_http_querystring_class_entry TSRMLS_CC), 0);
+       if (Z_TYPE_P(instance) == IS_OBJECT) {
+               RETVAL_ZVAL(instance, 1, 0);
+       } else if ((_GET = php_http_env_get_superglobal(ZEND_STRL("_GET")))) {
+               ZVAL_OBJ(return_value, php_http_querystring_object_new(php_http_querystring_class_entry));
 
-                       SEPARATE_ZVAL_TO_MAKE_IS_REF(_GET);
-                       convert_to_array(*_GET);
-                       zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), *_GET TSRMLS_CC);
+               ZVAL_MAKE_REF(_GET);
+               zend_update_property(php_http_querystring_class_entry, return_value, ZEND_STRL("queryArray"), _GET);
 
-                       zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), instance TSRMLS_CC);
-                       zval_ptr_dtor(&instance);
-               } else {
-                       php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL);
-               }
+               zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), return_value);
+       } else {
+               php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL);
        }
 
-       RETVAL_ZVAL(instance, 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getIterator, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, getIterator)
 {
-       zval *retval = NULL, *qa;
+       zval qa_tmp, *qa;
 
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
 
        object_init_ex(return_value, spl_ce_RecursiveArrayIterator);
-       zend_call_method_with_1_params(&return_value, spl_ce_RecursiveArrayIterator, NULL, "__construct", &retval, qa);
-       if (retval) {
-               zval_ptr_dtor(&retval);
-       }
+       zend_call_method_with_1_params(return_value, spl_ce_RecursiveArrayIterator, NULL, "__construct", NULL, qa);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_toString, 0, 0, 0)
@@ -396,20 +388,20 @@ PHP_METHOD(HttpQueryString, toString)
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
-       php_http_querystring_str(getThis(), return_value TSRMLS_CC);
+       php_http_querystring_str(getThis(), return_value);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_toArray, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, toArray)
 {
-       zval *zqa;
+       zval zqa_tmp, *zqa;
 
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
 
-       zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &zqa_tmp);
        RETURN_ZVAL(zqa, 1, 0);
 }
 
@@ -422,12 +414,12 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, get)
 {
        char *name_str = NULL;
-       int name_len = 0;
-       long type = 0;
+       size_t name_len = 0;
+       zend_long type = 0;
        zend_bool del = 0;
        zval *ztype = NULL, *defval = NULL;
        
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name_str, &name_len, &ztype, &defval, &del)) {
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|szzb", &name_str, &name_len, &ztype, &defval, &del)) {
                if (name_str && name_len) {
                        if (ztype) {
                                if (Z_TYPE_P(ztype) == IS_LONG) {
@@ -453,9 +445,9 @@ PHP_METHOD(HttpQueryString, get)
                                        }
                                }
                        }
-                       php_http_querystring_get(getThis(), type, name_str, name_len, defval, del, return_value TSRMLS_CC);
+                       php_http_querystring_get(getThis(), type, name_str, name_len, defval, del, return_value);
                } else {
-                       php_http_querystring_str(getThis(), return_value TSRMLS_CC);
+                       php_http_querystring_str(getThis(), return_value);
                }
        }
 }
@@ -467,11 +459,11 @@ PHP_METHOD(HttpQueryString, set)
 {
        zval *params;
        
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "z", &params)) {
                return;
        }
        
-       php_http_querystring_set(getThis(), params, QS_MERGE TSRMLS_CC);
+       php_http_querystring_set(getThis(), params, QS_MERGE);
        RETVAL_ZVAL(getThis(), 1, 0);
 }
 
@@ -480,15 +472,17 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_mod, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, mod)
 {
-       zval *params;
+       zval qa_tmp, *params, *instance = getThis();
        zend_error_handling zeh;
 
-       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params), invalid_arg, return);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &params), invalid_arg, return);
        
-       zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh TSRMLS_CC);
-       ZVAL_OBJVAL(return_value, Z_OBJ_HT_P(getThis())->clone_obj(getThis() TSRMLS_CC), 0);
-       php_http_querystring_set(return_value, params, QS_MERGE TSRMLS_CC);
-       zend_restore_error_handling(&zeh TSRMLS_CC);
+       zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh);
+       ZVAL_OBJ(return_value, Z_OBJ_HT_P(instance)->clone_obj(instance));
+       /* make sure we do not inherit the reference to _GET */
+       SEPARATE_ZVAL(zend_read_property(Z_OBJCE_P(return_value), return_value, ZEND_STRL("queryArray"), 0, &qa_tmp));
+       php_http_querystring_set(return_value, params, QS_MERGE);
+       zend_restore_error_handling(&zeh);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString___getter, 0, 0, 1)
@@ -500,14 +494,14 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, method) \
 { \
        char *name; \
-       int name_len; \
+       size_t name_len; \
        zval *defval = NULL; \
        zend_bool del = 0; \
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
-               php_http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value TSRMLS_CC); \
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|zb", &name, &name_len, &defval, &del)) { \
+               php_http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
        } \
 }
-PHP_HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
+PHP_HTTP_QUERYSTRING_GETTER(getBool, _IS_BOOL);
 PHP_HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
 PHP_HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
 PHP_HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
@@ -522,26 +516,25 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, xlate)
 {
        char *ie, *oe;
-       int ie_len, oe_len;
-       zval *na, *qa;
+       size_t ie_len, oe_len;
+       zval na, qa_tmp, *qa;
 
-       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len), invalid_arg, return);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &ie, &ie_len, &oe, &oe_len), invalid_arg, return);
 
-       MAKE_STD_ZVAL(na);
-       array_init(na);
-       qa = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC));
+       array_init(&na);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
+       ZVAL_DEREF(qa);
+       convert_to_array(qa);
 
-       php_http_expect(SUCCESS == php_http_querystring_xlate(na, qa, ie, oe TSRMLS_CC), bad_conversion,
+       php_http_expect(SUCCESS == php_http_querystring_xlate(&na, qa, ie, oe), bad_conversion,
                        zval_ptr_dtor(&na);
-                       zval_ptr_dtor(&qa);
                        return;
        );
 
-       php_http_querystring_set(getThis(), na, 0 TSRMLS_CC);
+       php_http_querystring_set(getThis(), &na, 0);
        RETVAL_ZVAL(getThis(), 1, 0);
        
        zval_ptr_dtor(&na);
-       zval_ptr_dtor(&qa);
 }
 #endif /* HAVE_ICONV */
 
@@ -552,7 +545,7 @@ PHP_METHOD(HttpQueryString, serialize)
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
-       php_http_querystring_str(getThis(), return_value TSRMLS_CC);
+       php_http_querystring_str(getThis(), return_value);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_unserialize, 0, 0, 1)
@@ -562,14 +555,14 @@ PHP_METHOD(HttpQueryString, unserialize)
 {
        zval *serialized;
        
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "z", &serialized)) {
                return;
        }
 
        if (Z_TYPE_P(serialized) == IS_STRING) {
-               php_http_querystring_set(getThis(), serialized, 0 TSRMLS_CC);
+               php_http_querystring_set(getThis(), serialized, 0);
        } else {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected a string as parameter");
+               php_error_docref(NULL, E_WARNING, "Expected a string as parameter");
        }
 }
 
@@ -578,19 +571,19 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_offsetGet, 0, 0, 1)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetGet)
 {
-       char *offset_str;
-       int offset_len;
-       zval **value, *qa;
+       zend_string *offset;
+       zval *value, qa_tmp, *qa;
 
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
        
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
+       ZVAL_DEREF(qa);
 
        if (Z_TYPE_P(qa) == IS_ARRAY) {
-               if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(qa), offset_str, offset_len + 1, (void *) &value)) {
-                       RETVAL_ZVAL(*value, 1, 0);
+               if ((value = zend_symtable_find(Z_ARRVAL_P(qa), offset))) {
+                       RETVAL_ZVAL(value, 1, 0);
                }
        }
 }
@@ -601,27 +594,22 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_offsetSet, 0, 0, 2)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetSet)
 {
-       char *offset_str;
-       int offset_len;
-       zval *value, *param;
+       zend_string *offset;
+       zval *value, param, znull;
        
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &offset_str, &offset_len, &value)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &offset, &value)) {
                return;
        }
 
-       param = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC);
-
-       if (Z_TYPE_P(param) == IS_ARRAY && zend_symtable_exists(Z_ARRVAL_P(param), offset_str, offset_len + 1)) {
-               Z_ADDREF_P(value);
-               zend_symtable_update(Z_ARRVAL_P(param), offset_str, offset_len + 1, (void *) &value, sizeof(zval *), NULL);
-               Z_ADDREF_P(param);
-       } else {
-               MAKE_STD_ZVAL(param);
-               array_init(param);
-               Z_ADDREF_P(value);
-               add_assoc_zval_ex(param, offset_str, offset_len + 1, value);
-       }
-       php_http_querystring_set(getThis(), param, QS_MERGE TSRMLS_CC);
+       array_init_size(&param, 1);
+       /* unset first */
+       ZVAL_NULL(&znull);
+       zend_symtable_update(Z_ARRVAL(param), offset, &znull);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
+       /* then update, else QS_MERGE would merge sub-arrrays */
+       Z_TRY_ADDREF_P(value);
+       zend_symtable_update(Z_ARRVAL(param), offset, value);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
        zval_ptr_dtor(&param);
 }
 
@@ -630,19 +618,19 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_offsetExists, 0, 0, 1)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetExists)
 {
-       char *offset_str;
-       int offset_len;
-       zval **value, *qa;
+       zend_string *offset;
+       zval *value, qa_tmp, *qa;
 
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
        
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
+       ZVAL_DEREF(qa);
 
        if (Z_TYPE_P(qa) == IS_ARRAY) {
-               if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(qa), offset_str, offset_len + 1, (void *) &value)) {
-                       RETURN_BOOL(Z_TYPE_PP(value) != IS_NULL);
+               if ((value = zend_symtable_find(Z_ARRVAL_P(qa), offset))) {
+                       RETURN_BOOL(Z_TYPE_P(value) != IS_NULL);
                }
        }
        RETURN_FALSE;
@@ -653,18 +641,17 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_offsetUnset, 0, 0, 1)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetUnset)
 {
-       char *offset_str;
-       int offset_len;
-       zval *param;
+       zend_string *offset;
+       zval param, znull;
        
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
 
-       MAKE_STD_ZVAL(param);
-       array_init(param);
-       add_assoc_null_ex(param, offset_str, offset_len + 1);
-       php_http_querystring_set(getThis(), param, QS_MERGE TSRMLS_CC);
+       array_init(&param);
+       ZVAL_NULL(&znull);
+       zend_symtable_update(Z_ARRVAL(param), offset, &znull);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
        zval_ptr_dtor(&param);
 }
 
@@ -713,19 +700,19 @@ PHP_MINIT_FUNCTION(http_querystring)
        zend_class_entry ce = {0};
 
        INIT_NS_CLASS_ENTRY(ce, "http", "QueryString", php_http_querystring_methods);
-       php_http_querystring_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
+       php_http_querystring_class_entry = zend_register_internal_class(&ce);
        php_http_querystring_class_entry->create_object = php_http_querystring_object_new;
-       zend_class_implements(php_http_querystring_class_entry TSRMLS_CC, 3, zend_ce_serializable, zend_ce_arrayaccess, zend_ce_aggregate);
+       zend_class_implements(php_http_querystring_class_entry, 3, zend_ce_serializable, zend_ce_arrayaccess, zend_ce_aggregate);
 
-       zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("instance"), (ZEND_ACC_STATIC|ZEND_ACC_PRIVATE) TSRMLS_CC);
-       zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("queryArray"), ZEND_ACC_PRIVATE TSRMLS_CC);
+       zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("instance"), (ZEND_ACC_STATIC|ZEND_ACC_PRIVATE));
+       zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("queryArray"), ZEND_ACC_PRIVATE);
 
-       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_BOOL"), PHP_HTTP_QUERYSTRING_TYPE_BOOL TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_INT"), PHP_HTTP_QUERYSTRING_TYPE_INT TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_FLOAT"), PHP_HTTP_QUERYSTRING_TYPE_FLOAT TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_STRING"), PHP_HTTP_QUERYSTRING_TYPE_STRING TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_ARRAY"), PHP_HTTP_QUERYSTRING_TYPE_ARRAY TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_OBJECT"), PHP_HTTP_QUERYSTRING_TYPE_OBJECT TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_BOOL"), PHP_HTTP_QUERYSTRING_TYPE_BOOL);
+       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_INT"), PHP_HTTP_QUERYSTRING_TYPE_INT);
+       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_FLOAT"), PHP_HTTP_QUERYSTRING_TYPE_FLOAT);
+       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_STRING"), PHP_HTTP_QUERYSTRING_TYPE_STRING);
+       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_ARRAY"), PHP_HTTP_QUERYSTRING_TYPE_ARRAY);
+       zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_OBJECT"), PHP_HTTP_QUERYSTRING_TYPE_OBJECT);
 
        return SUCCESS;
 }