Beispiel #1
0
static void test_copyEqualBuffer(void **state)
{
    char element0[] = "element0";
    unsigned int element0size = strlen(element0);
    char element1[] = "element1";
    unsigned int element1size = strlen(element1);

    Buffer *buffer0 = NULL;
    Buffer *buffer1 = NULL;
    Buffer *buffer2 = NULL;

    // Empty buffers, all empty buffers are the same
    assert_int_equal(0, BufferNew(&buffer0));
    assert_int_equal(0, BufferNew(&buffer1));
    assert_true(BufferEqual(buffer0, buffer0));
    assert_true(BufferEqual(buffer0, buffer1));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_true(BufferEqual(buffer0, buffer2));

    // Add some flavour
    assert_int_equal(0, BufferDestroy(&buffer2));
    assert_int_equal(element0size, BufferSet(buffer0, element0, element0size));
    assert_int_equal(element1size, BufferSet(buffer1, element1, element1size));
    assert_true(BufferEqual(buffer0, buffer0));
    assert_false(BufferEqual(buffer0, buffer1));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_true(BufferEqual(buffer0, buffer2));

    // Destroy the buffers
    assert_int_equal(0, BufferDestroy(&buffer0));
    assert_int_equal(0, BufferDestroy(&buffer1));
    assert_int_equal(0, BufferDestroy(&buffer2));
}
Beispiel #2
0
static void _WorkerReadHandler(Event event, Connection* source, Worker* worker) {
    Buffer* bufferCopy = NULL;
    Event eventToProcess = AIO4C_OUTBOUND_DATA_EVENT;

    ProbeTimeStart(AIO4C_TIME_PROBE_DATA_PROCESS);

    if (event != AIO4C_INBOUND_DATA_EVENT || source->state == AIO4C_CONNECTION_STATE_CLOSED) {
        return;
    }

    bufferCopy = AllocateBuffer(worker->pool);

    if (event == AIO4C_INBOUND_DATA_EVENT) {
        BufferFlip(source->readBuffer);

        BufferCopy(bufferCopy, source->readBuffer);

        BufferReset(source->readBuffer);

        eventToProcess = AIO4C_READ_EVENT;
    }

    if (!EnqueueTaskItem(worker->queue, eventToProcess, source, bufferCopy)) {
        ReleaseBuffer(&bufferCopy);
        return;
    }

    ProbeTimeEnd(AIO4C_TIME_PROBE_DATA_PROCESS);
}
Beispiel #3
0
static void test_copyCompareBuffer(void)
{
    char *element0 = xstrdup("element0");
    unsigned int element0size = strlen(element0);
    char *element1 = xstrdup("element1");
    unsigned int element1size = strlen(element1);

    Buffer *buffer0 = NULL;
    Buffer *buffer1 = NULL;
    Buffer *buffer2 = NULL;

    assert_int_equal(0, BufferCompare(buffer0, buffer1));
    buffer0 = BufferNew();
    assert_int_equal(-1, BufferCompare(NULL, buffer0));
    assert_int_equal(1, BufferCompare(buffer0, NULL));
    buffer1 = BufferNew();
    assert_int_equal(0, BufferCompare(buffer0, buffer0));
    assert_int_equal(0, BufferCompare(buffer0, buffer1));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_int_equal(0, BufferCompare(buffer0, buffer2));

    // Add some flavour
    assert_int_equal(0, BufferDestroy(&buffer2));
    assert_int_equal(element0size, BufferSet(buffer0, element0, element0size));
    assert_int_equal(element1size, BufferSet(buffer1, element1, element1size));
    assert_int_equal(0, BufferCompare(buffer0, buffer0));
    assert_int_equal(-1, BufferCompare(buffer0, buffer1));
    assert_int_equal(1, BufferCompare(buffer1, buffer0));
    assert_int_equal(0, BufferCopy(buffer0, &buffer2));
    assert_int_equal(0, BufferCompare(buffer0, buffer2));

    // Destroy the buffers
    assert_int_equal(0, BufferDestroy(&buffer0));
    assert_int_equal(0, BufferDestroy(&buffer1));
    assert_int_equal(0, BufferDestroy(&buffer2));

    free (element0);
    free (element1);
}
Beispiel #4
0
void TestCopyBuffer(CuTest* tc)
{
    buffer_t *buf = BufferCreate();
    uint8_t data[4] = { 1, 2, 3, 4 };
    BufferAppend(buf, data, sizeof(data));
    uint8_t *data_out;
    size_t size_out;
    BufferCopy(buf, 2, &data_out, &size_out);
    CuAssertIntEquals(tc, 2, size_out);
    CuAssertIntEquals(tc, 0, memcmp(data, data_out, 2));
    free(data_out);
    BufferDestroy(&buf);
}
Beispiel #5
0
ps_image* PICAPI ps_image_create_from_data(ps_byte* data, ps_color_format fmt, int w, int h, int p)
{
    if (!picasso::is_valid_system_device()) {
        global_status = STATUS_DEVICE_ERROR;
        return NULL;
    }

    if (!data || w <= 0 || h <= 0 || p <= 0) {
        global_status = STATUS_INVALID_ARGUMENT;
        return NULL;
    }

    ps_image* img = (ps_image*)mem_malloc(sizeof(ps_image));
    if (img) {
        img->refcount = 1;
        img->fmt = fmt;
        img->host = NULL;
        new ((void*)&(img->buffer)) picasso::rendering_buffer;
        int pitch = picasso::_bytes_per_color(fmt) * w;
        byte* buf = NULL;
        if ((buf = (byte*)BufferAlloc(h * pitch))) {
            for (int i = 0; i < h; i++) {
                BufferCopy(buf + (pitch * i), data + (i * p), pitch);
            }
            img->flage = buffer_alloc_surface;
            img->buffer.attach(buf, w, h, pitch);
            global_status = STATUS_SUCCEED;
            return img;
        } else if ((buf = (byte*)mem_malloc(h * pitch))) {
            for (int i = 0; i < h; i++) {
                mem_copy(buf + (pitch * i), data + (i * p), pitch);
            }
            img->flage = buffer_alloc_malloc;
            img->buffer.attach(buf, w, h, pitch);
            global_status = STATUS_SUCCEED;
            return img;
        } else {
            (&img->buffer)->picasso::rendering_buffer::~rendering_buffer();
            mem_free(img);
            global_status = STATUS_OUT_OF_MEMORY;
            return NULL;
        }
    } else {
        global_status = STATUS_OUT_OF_MEMORY;
        return NULL;
    }
}
void SSLBufferTryWrite(struct sslbuffer_t *sslbuffer)
{
	int res;
	unsigned long error;

	if (!sslbuffer->fl_writing)
	{
		if (BufferSize(sslbuffer->write_buffer_1) > 0)
			BufferCopy(sslbuffer->write_buffer_2, sslbuffer->write_buffer_1);
	}
	if (BufferSize(sslbuffer->write_buffer_2) > 0)
	{
		res = SSL_write(
			sslbuffer->ssl,
			BufferStart(sslbuffer->write_buffer_2),
			BufferSize(sslbuffer->write_buffer_2));
		if (res <= 0)
		{
			error = SSL_get_error(sslbuffer->ssl, res);
			sslbuffer->fl_writing = 1;
			if (error == SSL_ERROR_WANT_READ)
				sslbuffer->fl_want_read = 1;
			else if (error == SSL_ERROR_WANT_WRITE)
				sslbuffer->fl_want_write = 1;
			else
			{
				printf("%s:%d: unknown error %lu\n", __FILE__, __LINE__, error);
				print_errors();
				event_base_loopbreak(sslbuffer->base);
			}
			return;
		}

		BufferRemove(sslbuffer->write_buffer_2, (size_t) res);
		sslbuffer->fl_writing = 0;
		sslbuffer->fl_want_write = 0;
		sslbuffer->fl_want_read = 0;
	}

	if ( (BufferSize(sslbuffer->write_buffer_1) == 0) &&
		 (BufferSize(sslbuffer->write_buffer_2) == 0))
	{
		event_del(sslbuffer->ev_write);
	}
}
Beispiel #7
0
/**
 * Expand a #string into Buffer #out, returning the pointer to the string
 * itself, inside the Buffer #out. If #out is NULL then the buffer will be
 * created and destroyed internally.
 *
 * @retval NULL something went wrong
 */
char *ExpandScalar(const EvalContext *ctx, const char *ns, const char *scope,
                   const char *string, Buffer *out)
{
    bool out_belongs_to_us = false;

    if (out == NULL)
    {
        out               = BufferNew();
        out_belongs_to_us = true;
    }

    assert(string != NULL);
    assert(out != NULL);
    Buffer *current_item = BufferNew();

    for (const char *sp = string; *sp != '\0'; sp++)
    {
        BufferClear(current_item);
        ExtractScalarPrefix(current_item, sp, strlen(sp));

        BufferAppend(out, BufferData(current_item), BufferSize(current_item));
        sp += BufferSize(current_item);
        if (*sp == '\0')
        {
            break;
        }

        BufferClear(current_item);
        char varstring = sp[1];
        ExtractScalarReference(current_item,  sp, strlen(sp), true);
        sp += BufferSize(current_item) + 2;

        if (IsCf3VarString(BufferData(current_item)))
        {
            Buffer *temp = BufferCopy(current_item);
            BufferClear(current_item);
            ExpandScalar(ctx, ns, scope, BufferData(temp), current_item);
            BufferDestroy(temp);
        }

        if (!IsExpandable(BufferData(current_item)))
        {
            VarRef *ref = VarRefParseFromNamespaceAndScope(
                BufferData(current_item),
                ns, scope, CF_NS, '.');
            DataType value_type;
            const void *value = EvalContextVariableGet(ctx, ref, &value_type);
            VarRefDestroy(ref);

            switch (DataTypeToRvalType(value_type))
            {
            case RVAL_TYPE_SCALAR:
                assert(value != NULL);
                BufferAppendString(out, value);
                continue;
                break;

            case RVAL_TYPE_CONTAINER:
            {
                assert(value != NULL);
                const JsonElement *jvalue = value;      /* instead of casts */
                if (JsonGetElementType(jvalue) == JSON_ELEMENT_TYPE_PRIMITIVE)
                {
                    BufferAppendString(out, JsonPrimitiveGetAsString(jvalue));
                    continue;
                }
                break;
            }
            default:
                /* TODO Log() */
                break;
            }
        }

        if (varstring == '{')
        {
            BufferAppendF(out, "${%s}", BufferData(current_item));
        }
        else
        {
            BufferAppendF(out, "$(%s)", BufferData(current_item));
        }
    }

    BufferDestroy(current_item);

    LogDebug(LOG_MOD_EXPAND, "ExpandScalar( %s : %s . %s )  =>  %s",
             SAFENULL(ns), SAFENULL(scope), string, BufferData(out));

    return out_belongs_to_us ? BufferClose(out) : BufferGet(out);
}
Beispiel #8
0
bool ExpandScalar(const EvalContext *ctx,
                  const char *ns, const char *scope, const char *string,
                  Buffer *out)
{
    assert(string);
    if (strlen(string) == 0)
    {
        return true;
    }

    bool fully_expanded = true;

    Buffer *current_item = BufferNew();
    for (const char *sp = string; *sp != '\0'; sp++)
    {
        BufferClear(current_item);
        ExtractScalarPrefix(current_item, sp, strlen(sp));

        BufferAppend(out, BufferData(current_item), BufferSize(current_item));
        sp += BufferSize(current_item);
        if (*sp == '\0')
        {
            break;
        }

        BufferClear(current_item);
        char varstring = sp[1];
        ExtractScalarReference(current_item,  sp, strlen(sp), true);
        sp += BufferSize(current_item) + 2;

        if (IsCf3VarString(BufferData(current_item)))
        {
            Buffer *temp = BufferCopy(current_item);
            BufferClear(current_item);
            ExpandScalar(ctx, ns, scope, BufferData(temp), current_item);
            BufferDestroy(temp);
        }

        if (!IsExpandable(BufferData(current_item)))
        {
            DataType type = CF_DATA_TYPE_NONE;
            const void *value = NULL;
            {
                VarRef *ref = VarRefParseFromNamespaceAndScope(BufferData(current_item), ns, scope, CF_NS, '.');
                value = EvalContextVariableGet(ctx, ref, &type);
                VarRefDestroy(ref);
            }

            if (value)
            {
                switch (DataTypeToRvalType(type))
                {
                case RVAL_TYPE_SCALAR:
                    BufferAppendString(out, value);
                    continue;

                case RVAL_TYPE_CONTAINER:
                    if (JsonGetElementType((JsonElement*)value) == JSON_ELEMENT_TYPE_PRIMITIVE)
                    {
                        BufferAppendString(out, JsonPrimitiveGetAsString((JsonElement*)value));
                        continue;
                    }
                    break;

                default:
                    break;
                }
            }
        }

        if (varstring == '{')
        {
            BufferAppendF(out, "${%s}", BufferData(current_item));
        }
        else
        {
            BufferAppendF(out, "$(%s)", BufferData(current_item));
        }
    }

    BufferDestroy(current_item);

    return fully_expanded;
}