Esempio n. 1
0
/**
 * Unserializes php variables without using the PHP userland
 */
void phalcon_unserialize(zval *return_value, zval *var) {

	const unsigned char *p;
	php_unserialize_data_t var_hash;

	if (Z_TYPE_P(var) != IS_STRING) {
		RETURN_FALSE;
	}

	if (Z_STRLEN_P(var) == 0) {
		RETURN_FALSE;
	}

	p = (const unsigned char*) Z_STRVAL_P(var);
	PHP_VAR_UNSERIALIZE_INIT(var_hash);
	if (!php_var_unserialize(return_value, &p, p + Z_STRLEN_P(var), &var_hash)) {
		PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
		zval_ptr_dtor(return_value);
		ZVAL_NULL(return_value);
		if (!EG(exception)) {
			php_error_docref(NULL, E_NOTICE, "Error at offset %ld of %d bytes", (long)((char*)p - Z_STRVAL_P(var)), Z_STRLEN_P(var));
		}
		RETURN_FALSE;
	}
	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);

}
Esempio n. 2
0
/* {{{ proto void SplDoublyLinkedList::unserialize(string serialized)
 Unserializes storage */
SPL_METHOD(SplDoublyLinkedList, unserialize)
{
	spl_dllist_object *intern = Z_SPLDLLIST_P(getThis());
	zval flags, elem;
	char *buf;
	size_t buf_len;
	const unsigned char *p, *s;
	php_unserialize_data_t var_hash;
	
	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &buf, &buf_len) == FAILURE) {
		return;
	}

	if (buf_len == 0) {
		return;
	}

	s = p = (const unsigned char*)buf;
	PHP_VAR_UNSERIALIZE_INIT(var_hash);

	/* flags */
	if (!php_var_unserialize(&flags, &p, s + buf_len, &var_hash)) {
		goto error;
	}

	if (Z_TYPE(flags) != IS_LONG) {
		zval_ptr_dtor(&flags);
		goto error;
	}

	intern->flags = (int)Z_LVAL(flags);
	zval_ptr_dtor(&flags);

	/* elements */
	while(*p == ':') {
		++p;
		if (!php_var_unserialize(&elem, &p, s + buf_len, &var_hash)) {
			goto error;
		}

		spl_ptr_llist_push(intern->llist, &elem);
		zval_ptr_dtor(&elem);
	}

	if (*p != '\0') {
		goto error;
	}

	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
	return;

error:
	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
	zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset %pd of %d bytes", (zend_long)((char*)p - buf), buf_len);
	return;

} /* }}} */
Esempio n. 3
0
/* {{{ php unserializer */
PHP_APCU_API int APC_UNSERIALIZER_NAME(php) (APC_UNSERIALIZER_ARGS) 
{
    const unsigned char *tmp = buf;
    php_unserialize_data_t var_hash;
    PHP_VAR_UNSERIALIZE_INIT(var_hash);
    if(!php_var_unserialize(value, &tmp, buf + buf_len, &var_hash)) {
        PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
        php_error_docref(NULL, E_NOTICE, "Error at offset %ld of %ld bytes", (zend_long)(tmp - buf), (zend_long)buf_len);
        ZVAL_NULL(value);
        return 0;
    }
    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
    return 1;
} /* }}} */
Esempio n. 4
0
/**
 * @brief Phalcon\Registry Phalcon\Registry::unserialize(string $str)
 */
PHP_METHOD(Phalcon_Registry, unserialize){
	zval data = {}, *str, zv = {};
	php_unserialize_data_t var_hash;
	const unsigned char *buf, *max;

	phalcon_fetch_params(0, 1, 0, &str);
	PHALCON_ENSURE_IS_STRING(str);

	phalcon_read_property(&data, getThis(), SL("_data"), PH_NOISY);

	if (zend_hash_num_elements(Z_ARRVAL(data))) {
		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call unserialize() on an already constructed object");
		return;
	}

	buf = (unsigned char*)(Z_STRVAL_P(str));
	max = buf + Z_STRLEN_P(str);

	PHP_VAR_UNSERIALIZE_INIT(var_hash);
	if (php_var_unserialize(&zv, &buf, max, &var_hash) && Z_TYPE(zv) == IS_ARRAY) {
		if (zend_hash_num_elements(Z_ARRVAL(zv)) != 0) {
			zend_hash_copy(Z_ARRVAL(data), Z_ARRVAL(zv), (copy_ctor_func_t) zval_add_ref);
		}
	} else {
		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Bad parameters passed to Phalcon\\Registry::unserialize()");
	}

	zval_dtor(&zv);
	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
}
/* {{{  proto string SolrUtils::digestJsonResponse(string jsonResponse)
   Digests the json response into a php serialize string. */
PHP_METHOD(SolrUtils, digestJsonResponse)
{
    solr_char_t *jsonResponse = NULL;
    long jsonResponse_len = 0;
    unsigned char *raw_resp = NULL, *str_end = NULL;

    solr_string_t buffer;
    php_unserialize_data_t var_hash;
    size_t raw_res_length;
    int successful = 1;
    int json_translation_result;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &jsonResponse, &jsonResponse_len) == FAILURE) {

        RETURN_FALSE;
    }


    memset(&buffer, 0, sizeof(solr_string_t));

    json_translation_result = solr_json_to_php_native(&buffer, jsonResponse, jsonResponse_len TSRMLS_CC);

    if (json_translation_result > 0)
    {
        solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, solr_get_json_error_msg(json_translation_result));

        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error in JSON->PHP conversion. JSON Error Code %d", json_translation_result);
    }else{
        solr_sarray_to_sobject(&buffer TSRMLS_CC);
    }


    memset(&var_hash, 0, sizeof(php_unserialize_data_t));

    PHP_VAR_UNSERIALIZE_INIT(var_hash);

    raw_resp = (unsigned char *) buffer.str;
    raw_res_length = buffer.len;
    str_end = (unsigned char *) (raw_resp + raw_res_length);
    if (!php_var_unserialize(
            &return_value, (const unsigned char **)&raw_resp,
            str_end, &var_hash TSRMLS_CC)
        )
    {
        solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG);

        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response.");

        successful = 0;
    }
    solr_string_free(&buffer);

    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);

    if (successful)
    {
        /* Overriding the default object handlers */
        Z_OBJ_HT_P(return_value) = &solr_object_handlers;
    }
}
/* {{{  proto string SolrUtils::digestXMLResponse(string xml_response [, int parse_mode])
   Digests the xml response into a php serialize string. */
PHP_METHOD(SolrUtils, digestXmlResponse)
{
	solr_char_t *xmlresponse = NULL;
	COMPAT_ARG_SIZE_T xmlresponse_len = 0;
	long int parse_mode = 0L;
	solr_string_t sbuilder;
	unsigned char *raw_resp = NULL, *str_end = NULL;
	size_t raw_res_length = 0;
	php_unserialize_data_t var_hash;
	int successful = 1;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &xmlresponse, &xmlresponse_len, &parse_mode) == FAILURE) {
		RETURN_FALSE;
	}

	if (!xmlresponse_len) {
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Raw response is empty");
		RETURN_NULL();
	}

	parse_mode = ((parse_mode < 0L) ? 0L : ((parse_mode > 1L) ? 1L : parse_mode));

	memset(&sbuilder, 0, sizeof(solr_string_t));

	solr_encode_generic_xml_response(&sbuilder, xmlresponse, xmlresponse_len, parse_mode TSRMLS_CC);

	if (sbuilder.str == NULL || sbuilder.len == 0)
	{
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Raw response was not valid");

		RETURN_NULL();
	}

	memset(&var_hash, 0, sizeof(php_unserialize_data_t));

	PHP_VAR_UNSERIALIZE_INIT(var_hash);

	raw_resp = (unsigned char *) sbuilder.str;
	raw_res_length = sbuilder.len;
	str_end = (unsigned char *) (raw_resp + raw_res_length);

	if (!php_var_unserialize(return_value, (const unsigned char **) &raw_resp, str_end, &var_hash TSRMLS_CC))
	{
		solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG);
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response.");
		successful = 0;
	}

	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
	solr_string_free(&sbuilder);

	if (successful)
	{
		/* Overriding the default object handlers */
		Z_OBJ_HT_P(return_value) = &solr_object_handlers;
	}
}
Esempio n. 7
0
int shmcache_php_unpack(char *content, size_t len, zval *rv)
{
	const unsigned char *p;
	php_unserialize_data_t var_hash;

	p = (const unsigned char*)content;
	ZVAL_FALSE(rv);
	PHP_VAR_UNSERIALIZE_INIT(var_hash);
	if (!php_var_unserialize(GET_ZVAL_PTR(rv), &p, p + len,  &var_hash)) {
		zval_ptr_dtor(GET_ZVAL_PTR(rv));
		PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
		logError("file: "__FILE__", line: %d, "
				"unpack error at offset %ld of %ld bytes",
				__LINE__, (long)((char*)p - content), len);
		return EINVAL;
	}
	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
	return 0;
}
Esempio n. 8
0
CPINLINE zval * cp_unserialize(char *data, int len) {
    zval *unser_value;
    ALLOC_INIT_ZVAL(unser_value);
    php_unserialize_data_t var_hash;
    PHP_VAR_UNSERIALIZE_INIT(var_hash);
    if (php_var_unserialize(&unser_value, (const unsigned char **) &data, (unsigned char *) data + len - 1, &var_hash TSRMLS_CC) != 1)
    {
//        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "unser data is corrupted");
    }
    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
    return unser_value;
}
Esempio n. 9
0
int php_ds_pair_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buffer, size_t length, zend_unserialize_data *data)
{
    zval *key, *value;
    php_unserialize_data_t unserialize_data = (php_unserialize_data_t) data;

    const unsigned char *pos = buffer;
    const unsigned char *max = buffer + length;

    PHP_VAR_UNSERIALIZE_INIT(unserialize_data);

    key = var_tmp_var(&unserialize_data);
    if (php_var_unserialize(key, &pos, max, &unserialize_data)) {
        var_push_dtor(&unserialize_data, key);
    } else {
        goto error;
    }

    value = var_tmp_var(&unserialize_data);
    if (php_var_unserialize(value, &pos, max, &unserialize_data)) {
        var_push_dtor(&unserialize_data, value);
    } else {
        goto error;
    }

    if (*(++pos) != '\0') {
        goto error;
    }

    ZVAL_DS_PAIR(object, ds_pair_ex(key, value));
    PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
    return SUCCESS;

error:
    PHP_VAR_UNSERIALIZE_DESTROY(unserialize_data);
    UNSERIALIZE_ERROR();
    return FAILURE;
}
Esempio n. 10
0
/* {{{ data_unserialize */
static zval data_unserialize(const char *filename)
{
    zval retval;
    zend_long len = 0;
    zend_stat_t sb;
    char *contents, *tmp;
    FILE *fp;
    php_unserialize_data_t var_hash = {0,};

    if(VCWD_STAT(filename, &sb) == -1) {
        return EG(uninitialized_zval);
    }

    fp = fopen(filename, "rb");

    len = sizeof(char)*sb.st_size;

    tmp = contents = malloc(len);

    if(!contents) {
        fclose(fp);
        return EG(uninitialized_zval);
    }

    if(fread(contents, 1, len, fp) < 1) {	
        fclose(fp);
        free(contents);
        return EG(uninitialized_zval);
    }

    ZVAL_UNDEF(&retval);

    PHP_VAR_UNSERIALIZE_INIT(var_hash);
    
    /* I wish I could use json */
    if(!php_var_unserialize(&retval, (const unsigned char**)&tmp, (const unsigned char*)(contents+len), &var_hash)) {
        fclose(fp);
        free(contents);
        return EG(uninitialized_zval);
    }

    PHP_VAR_UNSERIALIZE_DESTROY(var_hash);

    free(contents);
    fclose(fp);

    return retval;
}
/**
 * Digest solr server raw response
 * and return SolrObject or array
 */
PHP_SOLR_API void solr_response_get_response_impl(
        INTERNAL_FUNCTION_PARAMETERS,
        int return_array)
{
    zend_bool silent = 0;
    zval *objptr = getThis();

    if (return_value_used)
    {
        zval *response_writer = solr_read_response_object_property(objptr, "response_writer", silent);
        zval *raw_response = solr_read_response_object_property(objptr, "http_raw_response", silent);
        zval *success = solr_read_response_object_property(objptr, "success", silent);
        zval *parser_mode = solr_read_response_object_property(objptr, "parser_mode", silent);

        if (Z_BVAL_P(success) && Z_STRLEN_P(raw_response))
        {
            solr_string_t buffer;
            php_unserialize_data_t var_hash;
            const unsigned char *raw_resp;
            size_t raw_res_length;
            const unsigned char *str_end;
            int successful = 1;

            memset(&buffer, 0, sizeof(solr_string_t));

            if (Z_STRLEN_P(response_writer))
            {
                if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_XML_RESPONSE_WRITER))
                {
                    /* SOLR_XML_RESPONSE_WRITER */

                    /* Convert from XML serialization to PHP serialization format */
                    solr_encode_generic_xml_response(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response), Z_LVAL_P(parser_mode) TSRMLS_CC);
                    if(return_array)
                    {
                        solr_sobject_to_sarray(&buffer TSRMLS_CC);
                    }
                } else if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_PHP_NATIVE_RESPONSE_WRITER) || 0 == strcmp(Z_STRVAL_P(response_writer), SOLR_PHP_SERIALIZED_RESPONSE_WRITER)) {

                    /* SOLR_PHP_NATIVE_RESPONSE_WRITER */

                    /* Response string is already in Native PHP serialization format */
                    solr_string_set(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response));

                    if(!return_array)
                    {
                        solr_sarray_to_sobject(&buffer TSRMLS_CC);
                    }

                } else if (0 == strcmp(Z_STRVAL_P(response_writer), SOLR_JSON_RESPONSE_WRITER)) {

                    int json_translation_result = solr_json_to_php_native(&buffer, Z_STRVAL_P(raw_response), Z_STRLEN_P(raw_response) TSRMLS_CC);

                    /* SOLR_JSON_RESPONSE_WRITER */

                    /* Convert from JSON serialization to PHP serialization format */
                    if (json_translation_result > 0)
                    {
                        solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, solr_get_json_error_msg(json_translation_result));

                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error in JSON->PHP conversion. JSON Error Code %d", json_translation_result);
                    }

                    if(!return_array)
                    {
                        solr_sarray_to_sobject(&buffer TSRMLS_CC);
                    }
                }
            }

            if (buffer.len)
            {
                zend_update_property_stringl(Z_OBJCE_P(objptr), objptr, "http_digested_response", sizeof("http_digested_response")-1, buffer.str, buffer.len TSRMLS_CC);
            }

            memset(&var_hash, 0, sizeof(php_unserialize_data_t));

            PHP_VAR_UNSERIALIZE_INIT(var_hash);

            raw_resp = (unsigned char *) buffer.str;
            raw_res_length = buffer.len;
            str_end = (unsigned char *) (raw_resp + raw_res_length);

            if (!php_var_unserialize(&return_value, &raw_resp, str_end, &var_hash TSRMLS_CC))
            {
                successful = 0;

                solr_throw_exception_ex(solr_ce_SolrException, SOLR_ERROR_1000 TSRMLS_CC, SOLR_FILE_LINE_FUNC, SOLR_ERROR_1000_MSG);

                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error unserializing raw response.");
            }

            PHP_VAR_UNSERIALIZE_DESTROY(var_hash);

            solr_string_free(&buffer);

            if (successful)
            {
                /* Overriding the default object handlers */
                Z_OBJ_HT_P(return_value) = &solr_object_handlers;
            }

            return ;
        }

        RETURN_NULL();

    } else {

        php_error_docref(NULL TSRMLS_CC, E_NOTICE, SOLR_ERROR_4002_MSG);
    }
}