- get rid of 1001 strict-aliasing warnings
[m6w6/ext-http] / http_querystring_object.c
index dab3dae504c61de0a75294d2262461faa23734fb..55d355d8e480bc6840de2d2c74aaf11c61717020 100644 (file)
@@ -21,8 +21,9 @@
 #include "zend_interfaces.h"
 
 #ifdef HAVE_ICONV
-#      include "ext/standard/url.h"
+#      undef PHP_ATOM_INC
 #      include "ext/iconv/php_iconv.h"
+#      include "ext/standard/url.h"
 #endif
 
 #include "php_http_api.h"
@@ -41,7 +42,7 @@ HTTP_BEGIN_ARGS(__construct, 0)
 HTTP_END_ARGS;
 
 #ifndef WONKY
-HTTP_BEGIN_ARGS(getInstance, 0)
+HTTP_BEGIN_ARGS(singleton, 0)
        HTTP_ARG_VAL(global, 0)
 HTTP_END_ARGS;
 #endif
@@ -67,7 +68,7 @@ HTTP_BEGIN_ARGS(__getter, 1)
 HTTP_END_ARGS;
 
 #ifdef HAVE_ICONV
-HTTP_BEGIN_ARGS(iconv, 2)
+HTTP_BEGIN_ARGS(xlate, 2)
        HTTP_ARG_VAL(from_encoding, 0)
        HTTP_ARG_VAL(to_encoding, 0)
 HTTP_END_ARGS;
@@ -78,11 +79,6 @@ HTTP_BEGIN_ARGS(unserialize, 1)
        HTTP_ARG_VAL(serialized, 0)
 HTTP_END_ARGS;
 
-#define http_querystring_object_declare_default_properties() _http_querystring_object_declare_default_properties(TSRMLS_C)
-static inline void _http_querystring_object_declare_default_properties(TSRMLS_D);
-
-#define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_querystring_object_ce, n)
-#define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_querystring_object_ce, n, v)
 #define OBJ_PROP_CE http_querystring_object_ce
 zend_class_entry *http_querystring_object_ce;
 zend_function_entry http_querystring_object_fe[] = {
@@ -95,10 +91,6 @@ zend_function_entry http_querystring_object_fe[] = {
        HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC)
        HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC)
        
-#ifndef WONKY
-       HTTP_QUERYSTRING_ME(getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
-#endif
-       
        HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC)
        HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC)
        HTTP_QUERYSTRING_GME(getFloat, ZEND_ACC_PUBLIC)
@@ -106,8 +98,11 @@ zend_function_entry http_querystring_object_fe[] = {
        HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC)
        HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC)
        
+#ifndef WONKY
+       HTTP_QUERYSTRING_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+#endif
 #ifdef HAVE_ICONV
-       HTTP_QUERYSTRING_ME(iconv, ZEND_ACC_PUBLIC)
+       HTTP_QUERYSTRING_ME(xlate, ZEND_ACC_PUBLIC)
 #endif
        
        /* Implements Serializable */
@@ -126,6 +121,19 @@ PHP_MINIT_FUNCTION(http_querystring_object)
        zend_class_implements(http_querystring_object_ce TSRMLS_CC, 1, zend_ce_serializable);
 #endif
        
+       DCL_STATIC_PROP_N(PRIVATE, instance);
+       DCL_PROP_N(PRIVATE, queryArray);
+       DCL_PROP(PRIVATE, string, queryString, "");
+       
+#ifndef WONKY
+       DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
+       DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
+       DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
+       DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
+       DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
+       DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
+#endif
+       
        HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
        HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
        HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
@@ -163,25 +171,6 @@ zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_que
        return ov;
 }
 
-static inline void _http_querystring_object_declare_default_properties(TSRMLS_D)
-{
-       zend_class_entry *ce = http_querystring_object_ce;
-
-       DCL_STATIC_PROP_N(PRIVATE, instance);
-       
-       DCL_PROP_N(PRIVATE, queryArray);
-       DCL_PROP(PRIVATE, string, queryString, "");
-       
-#ifndef WONKY
-       DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
-       DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
-       DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
-       DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
-       DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
-       DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
-#endif
-}
-
 void _http_querystring_object_free(zend_object *object TSRMLS_DC)
 {
        http_querystring_object *o = (http_querystring_object *) object;
@@ -204,6 +193,64 @@ static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRM
 static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC);
 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
 static inline void _http_querystring_get(zval *this_ptr, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC);
+#ifdef HAVE_ICONV
+#define http_querystring_xlate(a, p, ie, oe) _http_querystring_xlate((a), (p), (ie), (oe) TSRMLS_CC)
+static inline int _http_querystring_xlate(zval *array, zval *param, const char *ie, const char *oe TSRMLS_DC)
+{
+       HashPosition pos;
+       zval **entry = NULL;
+       char *xlate_str = NULL, *xkey, *kstr = NULL;
+       size_t xlate_len = 0, xlen;
+       uint klen = 0;
+       ulong kidx = 0;
+       
+       FOREACH_KEYLENVAL(pos, param, kstr, klen, kidx, entry) {
+               if (kstr) {
+                       if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(kstr, klen-1, &xkey, &xlen, oe, ie)) {
+                               http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", klen-1, kstr, 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 (kstr) {
+                                       efree(xkey);
+                               }
+                               http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_PP(entry), Z_STRVAL_PP(entry), ie, oe);
+                               return FAILURE;
+                       }
+                       if (kstr) {
+                               add_assoc_stringl_ex(array, xkey, xlen+1, xlate_str, xlate_len, 0);
+                       } else {
+                               add_index_stringl(array, kidx, xlate_str, xlate_len, 0);
+                       }
+               } else if (Z_TYPE_PP(entry) == IS_ARRAY) {
+                       zval *subarray;
+                       
+                       MAKE_STD_ZVAL(subarray);
+                       array_init(subarray);
+                       if (kstr) {
+                               add_assoc_zval_ex(array, xkey, xlen+1, subarray);
+                       } else {
+                               add_index_zval(array, kidx, subarray);
+                       }
+                       if (SUCCESS != http_querystring_xlate(subarray, *entry, ie, oe)) {
+                               if (kstr) {
+                                       efree(xkey);
+                               }
+                               return FAILURE;
+                       }
+               }
+               
+               if (kstr) {
+                       kstr = NULL;
+                       efree(xkey);
+               }
+       }
+       return SUCCESS;
+}
+#endif /* HAVE_ICONV */
 #ifndef WONKY
 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
 static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC)
@@ -248,7 +295,7 @@ static inline int _http_querystring_modify_array_ex(zval *qarray, char *key, int
        }
        
        /* update */
-       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key, keylen, (void **) &qarray_entry)) {
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key, keylen, (void *) &qarray_entry)) {
                zval equal;
                
                /* recursive */
@@ -323,7 +370,7 @@ static inline void _http_querystring_get(zval *this_ptr, int type, char *name, u
 {
        zval **arrval, *qarray = GET_PROP(queryArray);
                
-       if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void **) &arrval))) {
+       if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) {
                RETVAL_ZVAL(*arrval, 1, 0);
                
                if (type) {
@@ -350,15 +397,22 @@ PHP_METHOD(HttpQueryString, __construct)
        zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
        
        SET_EH_THROW_HTTP();
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
+       if (!sapi_module.treat_data) {
+               http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered");
+       } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
                if (global) {
-                       if (    (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) &&
+#ifdef ZEND_ENGINE_2
+                       zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
+#endif
+                       if (    (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER)) &&
                                        (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
-                                       (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING))) {
+                                       (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING))) {
                                
                                qstring = *QUERY_STRING;
-                               
-                               if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
+#ifdef ZEND_ENGINE_2
+                               zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC);
+#endif
+                               if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
                                        qarray = *_GET;
                                } else {
                                        http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
@@ -377,9 +431,10 @@ PHP_METHOD(HttpQueryString, __construct)
                                GET_PROP(queryArray)->is_ref = 1;
                                GET_PROP(queryString)->is_ref = 1;
                                
-                               if (params && http_querystring_modify(GET_PROP(queryArray), params)) {
-                                       http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
+                               if (params) {
+                                       http_querystring_modify(GET_PROP(queryArray), params);
                                }
+                               http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
                        }
                } else {
                        qarray = ecalloc(1, sizeof(zval));
@@ -487,11 +542,11 @@ PHP_METHOD(HttpQueryString, set)
 /* }}} */
 
 #ifndef WONKY
-/* {{{ proto HttpQueryString HttpQueryString::getInstance([bool global = true])
+/* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
  *
  * Get a single instance (differentiates between the global setting).
  */
-PHP_METHOD(HttpQueryString, getInstance)
+PHP_METHOD(HttpQueryString, singleton)
 {
        zend_bool global = 1;
        zval *instance = GET_STATIC_PROP(instance);
@@ -501,7 +556,7 @@ PHP_METHOD(HttpQueryString, getInstance)
                zval **zobj_ptr = NULL, *zobj = NULL;
                
                if (Z_TYPE_P(instance) == IS_ARRAY) {
-                       if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void **) &zobj_ptr)) {
+                       if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) {
                                RETVAL_ZVAL(*zobj_ptr, 1, 0);
                        } else {
                                zobj = http_querystring_instantiate(global);
@@ -546,18 +601,19 @@ HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
 /* }}} */
 
 #ifdef HAVE_ICONV
-/* {{{ proto bool HttpQueryString::iconv(string ie, string oe)
+/* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
  *
  * Converts the query string from the source encoding ie to the target encoding oe.
+ * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
  *
  * Returns TRUE on success or FALSE on failure.
  */
-PHP_METHOD(HttpQueryString, iconv)
+PHP_METHOD(HttpQueryString, xlate)
 {
-       char *ie, *oe, *er = NULL;
+       char *ie, *oe;
        int ie_len, oe_len;
-       size_t er_len = 0;
-       zval *qa, *qs;
+       zval xa, *qa, *qs;
+       STATUS rs;
        
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
                RETURN_FALSE;
@@ -565,17 +621,17 @@ PHP_METHOD(HttpQueryString, iconv)
        
        qa = GET_PROP(queryArray);
        qs = GET_PROP(queryString);
-       Z_STRLEN_P(qs) = php_url_decode(Z_STRVAL_P(qs), Z_STRLEN_P(qs));
-       if (PHP_ICONV_ERR_SUCCESS == php_iconv_string(Z_STRVAL_P(qs), (size_t) Z_STRLEN_P(qs), &er, &er_len, oe, ie)) {
-               efree(Z_STRVAL_P(qs));
-               ZVAL_STRINGL(qs, er, er_len, 0);
-               http_querystring_modify(qa, qs);
-               RETVAL_TRUE;
-       } else {
-               http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_P(qs), Z_STRVAL_P(qs), ie, oe);
-               RETVAL_FALSE;
+       INIT_PZVAL(&xa);
+       array_init(&xa);
+       
+       if (SUCCESS == (rs = http_querystring_xlate(&xa, qa, ie, oe))) {
+               zend_hash_clean(Z_ARRVAL_P(qa));
+               zend_hash_copy(Z_ARRVAL_P(qa), Z_ARRVAL(xa), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+               http_querystring_update(qa, qs);
        }
-       http_querystring_update(qa, qs);
+       zval_dtor(&xa);
+       
+       RETURN_SUCCESS(rs);
 }
 /* }}} */
 #endif /* HAVE_ICONV */