Ejemplo n.º 1
0
static void escape_string(smart_str *buf, zend_string *str) /* {{{ */
{
	size_t str_len = ZSTR_LEN(str);

    if (!str_len) {
        return;
    }

    // preallocate
    smart_str_alloc(buf, str_len + 2, 0);

    char *ptr = ZSTR_VAL(str);
    char *ptr_max = ptr + str_len;

    char *start = ptr;
    char ch = *ptr;

    for (; ptr < ptr_max; ptr++, ch = *ptr) {
        if ('"' == ch || '\\' == ch) {
            if (ptr > start) {
                smart_str_appendl(buf, start, ptr - start);
            }
            start = ptr;

            smart_str_appendc(buf, '\\');
        }
    }

    if (ptr > start) {
        smart_str_appendl(buf, start, ptr - start);
    }
}
Ejemplo n.º 2
0
/* {{{ proto mixed hstore_encode(array hstore)
   Decodes the hStore representation into a PHP value */
static PHP_FUNCTION(hstore_encode)
{
    zval *array, *value;
	zend_ulong num_idx;
    zend_string *str_idx;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
        return;
    }

    smart_str buf = {};
    smart_str_alloc(&buf, 2048, 0);

    zend_bool need_comma = 0;

    ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, value) {
        if (need_comma) {
            smart_str_appendl(&buf, ", ", 2);
        } else {
            need_comma = 1;
        }

        smart_str_appendc(&buf, '"');

        if (str_idx) {
            escape_string(&buf, str_idx);
        } else {
            smart_str_append_long(&buf, (long) num_idx);
        }

        smart_str_appendl(&buf, "\"=>", 3);

        if (Z_TYPE_P(value) == IS_NULL) {
            smart_str_appendl(&buf, "NULL", 4);
        } else {
            convert_to_string_ex(value);

            smart_str_appendc(&buf, '"');
            zend_string *str = zval_get_string(value);
            escape_string(&buf, str);
            zend_string_release(str);
            smart_str_appendc(&buf, '"');
        }
    } ZEND_HASH_FOREACH_END();

	smart_str_0(&buf); /* copy? */
	ZVAL_NEW_STR(return_value, buf.s);
}
Ejemplo n.º 3
0
zend_string *php_yar_packager_pack(char *packager_name, zval *pzval, char **msg) /* {{{ */ {
	char header[8];
	smart_str buf = {0};
	const yar_packager_t *packager = packager_name ?
		php_yar_packager_get(packager_name, strlen(packager_name)) : YAR_G(packager);

	if (!packager) {
		php_error_docref(NULL, E_ERROR, "unsupported packager %s", packager_name);
		return 0;
	}
	memcpy(header, packager->name, 8);
	smart_str_alloc(&buf, YAR_PACKAGER_BUFFER_SIZE /* 1M */, 0);
	smart_str_appendl(&buf, header, 8);
    packager->pack(packager, pzval, &buf, msg); 

	if (buf.s) {
		smart_str_0(&buf);
		return buf.s;
	}

	smart_str_free(&buf);

	return NULL;
} /* }}} */
inline void mmc_buffer_alloc(mmc_buffer_t *buffer, unsigned int size)  /*
	ensures space for an additional size bytes {{{ */
{
	register size_t newlen;
	smart_str_alloc((&(buffer->value)), size, 0);
}
Ejemplo n.º 5
0
/**
 * Writes the log to the stream itself
 *
 * @param string $message
 * @param int $type
 * @param int $time
 * @param array $context
 * @see http://www.firephp.org/Wiki/Reference/Protocol
 */
PHP_METHOD(Phalcon_Logger_Adapter_Firephp, logInternal){

	zval *message, *type, *time, *context, *formatter = NULL, *applied_format = NULL;
	zval *initialized, *index;
	sapi_header_line h = { NULL, 0, 0 };
	smart_str str      = { 0 };
	int size, offset;
	int separate_index = 0;
	size_t num_bytes;
	const int chunk = 4500;

	/* If headers has already been sent, we can do nothing. Exit early. */
	if (SG(headers_sent)) {
		RETURN_FALSE;
	}

	PHALCON_MM_GROW();

	phalcon_fetch_params(1, 4, 0, &message, &type, &time, &context);

	PHALCON_CALL_METHOD(&formatter, getThis(), "getformatter");

	initialized = phalcon_read_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_initialized"));
	if (!zend_is_true(initialized)) {
		/**
		 * Send the required initialization headers.
		 * Use Zend API here so that the user can see the progress and because
		 * if we delegate this to Phalcon and there will be a fatal errors,
		 * chances are that the headers will never ne sent.
		 */
		h.line     = "X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2";
		h.line_len = sizeof("X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2")-1;
		sapi_header_op(SAPI_HEADER_REPLACE, &h);

		h.line     = "X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3";
		h.line_len = sizeof("X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3")-1;
		sapi_header_op(SAPI_HEADER_REPLACE, &h);

		h.line     = "X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1";
		h.line_len = sizeof("X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1")-1;
		sapi_header_op(SAPI_HEADER_REPLACE, &h);

		ZVAL_TRUE(initialized); /* This will also update the property because "initialized" was not separated */
	}

	PHALCON_CALL_METHOD(&applied_format, formatter, "format", message, type, time, context);
	convert_to_string(applied_format);

	index = phalcon_read_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_index"));
	assert(Z_TYPE_P(index) == IS_LONG);

	size   = Z_STRLEN_P(applied_format);
	offset = 0;

	/**
	 * We need to send the data in chunks not exceeding 5,000 bytes.
	 * Allocate the smart string once to avoid performance penalties.
	 */
	num_bytes = smart_str_alloc(&str, (uint)(size > chunk ? chunk : size), 0);

	while (size > 0) {
		smart_str_appends(&str, "X-Wf-1-1-1-");
		smart_str_append_long(&str, Z_LVAL_P(index));
		smart_str_appends(&str, ": ");
		num_bytes = size > chunk ? chunk : size;

		if (offset) {
			/* This is not the first chunk, prepend the payload with "|" */
			smart_str_appendc(&str, '|');
		}

		/* Grab the chunk from the encoded string */
		smart_str_appendl(&str, Z_STRVAL_P(applied_format) + offset, num_bytes);

		size   -= num_bytes;
		offset += num_bytes;

		if (size) {
			/* If we have more data to send, append "|/" */
			smart_str_appendl(&str, "|\\", 2);
		}

		smart_str_0(&str); /* Not strictly necessary but just to be safe */

		/* Send the result */
		h.line     = str.s->val;
		h.line_len = str.s->len;
		sapi_header_op(SAPI_HEADER_REPLACE, &h);

		ZVAL_LONG(index, Z_LVAL_P(index)+1);

		/**
		 * Do not free and then reallocate memory. Just pretend the string
		 * is empty. We will take care of deallocation later.
		 */
		str.s->len = 0;
	}

	if (separate_index) {
		phalcon_update_static_property_ce(phalcon_logger_adapter_firephp_ce, SL("_index"), index);
	}

	/* Deallocate the smnart string if it is not empty */
	smart_str_free(&str);

	PHALCON_MM_RESTORE();
}
Ejemplo n.º 6
0
static inline void hstore_parse(HSParser *state, zval *result) /* {{{ */
{
    #define WKEY  0
    #define WVAL  1
    #define WEQ   2
    #define WDEL  4

    state->error   = 0;

    int st = WKEY;

    smart_str buf = {};
    smart_str_alloc(&buf, 1024, 0);

	zend_string *key;

    while (state->ptr < state->ptr_max) {
        char ch = *(state->ptr);

        if (isspace((unsigned char) ch)) {
            ++state->ptr;
            continue;
        }

		if (st == WKEY) {
            if (!hstore_parse_value(state, &buf, 0)) {
                goto free;
            }
            key = zval_get_string(&state->value);

            st = WEQ;
        } else if (st == WEQ) {
            if (state->ptr + 2 > state->ptr_max) {
                state->error = ERROR_UNEXPECTED_END;
                goto free;
            }

            if ('=' == *(state->ptr) && '>' == *(state->ptr + 1)) {
                st          = WVAL;
                state->ptr += 2;

                continue;
            }

            state->error = ERROR_SYNTAX;
            return;
        } else if (st == WVAL) {
            if (!hstore_parse_value(state, &buf, 1)) {
                goto free;
            }

            zend_symtable_update(Z_ARRVAL_P(result), key, &state->value);
            zend_string_release(key);
            zend_string_release(key);

            st = WDEL;
        } else if (st == WDEL) {
            if (',' == ch) {
                st = WKEY;
            } else {
                state->error = ERROR_SYNTAX;
                goto free;
            }
        } else {
            state->error = ERROR_UNKNOWN_STATE;
            goto free;
        }

        state->ptr++;
    }

    if (st != WKEY && st != WDEL) {
        state->error = ERROR_UNEXPECTED_END;
    }

free:
    smart_str_free(&buf);
}