--- xds-mystruct.c 2001/07/23 15:33:43 1.4
+++ xds-mystruct.c 2001/07/24 15:44:18 1.5
@@ -47,17 +47,12 @@
assert(buffer_size != 0);
assert(args != NULL);
- if (buffer_size >= sizeof(struct mystruct))
- {
- ms = va_arg(*args, struct mystruct*);
- rc = xds_encode(xds, "int32 int64 uint32", ms->small, ms->big, ms->positive);
- if (rc != XDS_OK)
- return rc;
- }
+ ms = va_arg(*args, struct mystruct*);
+ rc = xds_encode(xds, "int32 int64 uint32", ms->small, ms->big, ms->positive);
+ if (rc == XDS_OK)
+ return 0;
else
- return sizeof(struct mystruct);
-
- return 0;
+ return rc;
}
static int decode_mystruct_engine(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
@@ -70,17 +65,12 @@
assert(buffer_size != 0);
assert(args != NULL);
- if (buffer_size >= sizeof(struct mystruct))
- {
- ms = va_arg(*args, struct mystruct*);
- rc = xds_decode(xds, "int32 int64 uint32", &(ms->small), &(ms->big), &(ms->positive));
- if (rc != XDS_OK)
- return rc;
- }
+ ms = va_arg(*args, struct mystruct*);
+ rc = xds_decode(xds, "int32 int64 uint32", &(ms->small), &(ms->big), &(ms->positive));
+ if (rc == XDS_OK)
+ return 0;
else
- return XDS_ERR_UNDERFLOW;
-
- return 0;
+ return rc;
}
int main()
@@ -111,17 +101,35 @@
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_setbuffer() failed!\n");
+ 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. */
@@ -153,7 +161,7 @@
/* Both structures must be identical. */
- if (memcmp(&ms, &new_ms, sizeof(struct mystruct)) != 0)
+ 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;
|