ossp-pkg/xds/regression-tests/xds-mystruct.c
/*
XDS - OSSP Extensible Data Serialization Library
Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
This file is part of OSSP XDS, an extensible data serialization
library which can be found at http://www.ossp.com/pkg/xds/.
Permission to use, copy, modify, and distribute this software for
any purpose with or without fee is hereby granted, provided that
the above copyright notice and this permission notice appear in all
copies.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include "../internal.h"
struct mystruct
{
xds_int32_t small;
xds_int64_t big;
xds_uint32_t positive;
};
static int encode_mystruct_engine(xds_t* xds, void* engine_context,
void* buffer, size_t buffer_size, size_t* used_buffer_size,
va_list* args)
{
struct mystruct* ms;
assert(xds != NULL);
assert(buffer != NULL);
assert(buffer_size != 0);
assert(used_buffer_size != NULL);
assert(args != NULL);
ms = va_arg(*args, struct mystruct*);
return xds_encode(xds, "int32 int64 uint32", ms->small, ms->big, ms->positive);
}
static int decode_mystruct_engine(xds_t* xds, void* engine_context,
void* buffer, size_t buffer_size, size_t* used_buffer_size,
va_list* args)
{
struct mystruct* ms;
assert(xds != NULL);
assert(buffer != NULL);
assert(buffer_size != 0);
assert(used_buffer_size != NULL);
assert(args != NULL);
ms = va_arg(*args, struct mystruct*);
return xds_decode(xds, "int32 int64 uint32", &(ms->small), &(ms->big), &(ms->positive));
}
int main()
{
xds_t* xds;
char* buffer;
size_t buffer_size;
struct mystruct ms, new_ms;
ms.small = -0x1234567;
ms.big = -0x1234567890abcdeLL;
ms.positive = 42;
/* Encode our copy of mystruct using our encoding callback. Then
get a the buffer and destroy the context again. */
xds = xds_init(XDS_ENCODE);
if (xds == NULL)
{
printf("Failed to initialize XDS context.\n");
return 1;
}
if (xds_register(xds, "mystruct", &encode_mystruct_engine, NULL) != XDS_OK ||
xds_register(xds, "int32", &xdr_encode_int32, NULL) != XDS_OK ||
xds_register(xds, "int64", &xdr_encode_int64, NULL) != XDS_OK ||
xds_register(xds, "uint32", &xdr_encode_uint32, NULL) != XDS_OK)
{
printf("Failed to register my encoding engines.\n");
return 1;
}
buffer_size = 4;
buffer = malloc(buffer_size);
if (buffer == NULL)
{
printf("Failed to allocate memory for buffer.\n");
return 1;
}
if (xds_setbuffer(xds, XDS_GIFT, buffer, buffer_size) != XDS_OK)
{
printf("xds_setbuffer() failed!\n");
return 1;
}
if (xds_encode(xds, "mystruct", &ms) != XDS_OK)
{
printf("xds_encode() failed!\n");
return 1;
}
if (xds->buffer_capacity <= buffer_size)
{
printf("Buffer should have been enlarged after xds_encode()!\n");
return 1;
}
if (xds_getbuffer(xds, XDS_GIFT, (void**)&buffer, &buffer_size) != XDS_OK)
{
printf("xds_getbuffer() failed!\n");
return 1;
}
xds_destroy(xds);
printf("The encoded representation is %u bytes long.\n", buffer_size);
/* Now create a decoding context and decode the whole thing again. */
xds = xds_init(XDS_DECODE);
if (xds == NULL)
{
printf("Failed to initialize XDS context.\n");
return 1;
}
if (xds_register(xds, "mystruct", &decode_mystruct_engine, NULL) != XDS_OK ||
xds_register(xds, "int32", &xdr_decode_int32, NULL) != XDS_OK ||
xds_register(xds, "int64", &xdr_decode_int64, NULL) != XDS_OK ||
xds_register(xds, "uint32", &xdr_decode_uint32, NULL) != XDS_OK)
{
printf("Failed to register my decoding engines.\n");
return 1;
}
if (xds_setbuffer(xds, XDS_GIFT, buffer, buffer_size) != XDS_OK)
{
printf("xds_setbuffer() failed!\n");
return 1;
}
if (xds_decode(xds, "mystruct", &new_ms) != XDS_OK)
{
printf("xds_decode() failed!\n");
return 1;
}
xds_destroy(xds);
/* Both structures must be identical. */
if (ms.small != new_ms.small || ms.big != new_ms.big || ms.positive != new_ms.positive)
{
printf("Decoded data does not match the original!\n");
return 1;
}
/* Everything went fine. */
return 0;
}