Index: ossp-pkg/xds/docs/decode.c RCS File: /v/ossp/cvs/ossp-pkg/xds/docs/decode.c,v co -q -kk -p'1.1' '/v/ossp/cvs/ossp-pkg/xds/docs/decode.c,v' | diff -u /dev/null - -L'ossp-pkg/xds/docs/decode.c' 2>/dev/null --- ossp-pkg/xds/docs/decode.c +++ - 2025-04-04 22:39:10.792298334 +0200 @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include + +static void error_exit(int rc, const char* msg, ...) + { + va_list args; + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + exit(rc); + } + +int main() + { + xds_t* xds; + char buffer[1024]; + size_t buffer_len; + int rc; + + xds_int32_t int32; + xds_uint32_t uint32; + char* string; + + buffer_len = 0; + do + { + rc = read(STDIN_FILENO, buffer + buffer_len, sizeof(buffer) - buffer_len); + if (rc < 0) + error_exit(1, "read() failed: %s\n", strerror(errno)); + else if (rc > 0) + buffer_len += rc; + } + while (rc > 0 && buffer_len < sizeof(buffer)); + + if (buffer_len >= sizeof(buffer)) + error_exit(1, "Too much input data for our buffer.\n"); + + xds = xds_init(XDS_DECODE); + if (xds == NULL) + error_exit(1, "Failed to initialize XDS context: %s\n", strerror(errno)); + + if (xds_register(xds, "int32", &xdr_decode_int32, NULL) != XDS_OK || + xds_register(xds, "uint32", &xdr_decode_uint32, NULL) != XDS_OK || + xds_register(xds, "string", &xdr_decode_string, NULL) != XDS_OK) + error_exit(1, "Failed to register my decoding engines!\n"); + + if (xds_setbuffer(xds, XDS_LOAN, buffer, buffer_len) != XDS_OK) + error_exit(1, "setbuffer() failed.\n"); + + if (xds_decode(xds, "int32 uint32 string", &int32, &uint32, &string) != XDS_OK) + error_exit(1, "xds_decode() failed!\n"); + + xds_destroy(xds); + + fprintf(stderr, "Decoded data:\n"); + fprintf(stderr, "\tint32 = %d\n", int32); + fprintf(stderr, "\tuint32 = 0x%x\n", uint32); + fprintf(stderr, "\tstring = \"%s\"\n", string); + + free(string); + + return 0; + } Index: ossp-pkg/xds/docs/encode.c RCS File: /v/ossp/cvs/ossp-pkg/xds/docs/encode.c,v co -q -kk -p'1.1' '/v/ossp/cvs/ossp-pkg/xds/docs/encode.c,v' | diff -u /dev/null - -L'ossp-pkg/xds/docs/encode.c' 2>/dev/null --- ossp-pkg/xds/docs/encode.c +++ - 2025-04-04 22:39:10.794886552 +0200 @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +static void error_exit(int rc, const char* msg, ...) + { + va_list args; + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + exit(rc); + } + +int main() + { + xds_t* xds; + char* buffer; + size_t buffer_size; + + xds_int32_t int32 = -42; + xds_uint32_t uint32 = 0x12345678; + const char* string = "This is a test."; + + xds = xds_init(XDS_ENCODE); + if (xds == NULL) + error_exit(1, "Failed to initialize XDS context: %s\n", strerror(errno)); + + if (xds_register(xds, "int32", &xdr_encode_int32, NULL) != XDS_OK || + xds_register(xds, "uint32", &xdr_encode_uint32, NULL) != XDS_OK || + xds_register(xds, "string", &xdr_encode_string, NULL) != XDS_OK) + error_exit(1, "Failed to register my encoding engines!\n"); + + if (xds_encode(xds, "int32 uint32 string", int32, uint32, string) != XDS_OK) + error_exit(1, "xds_encode() failed!\n"); + + if (xds_getbuffer(xds, XDS_GIFT, (void**)&buffer, &buffer_size) != XDS_OK) + error_exit(1, "getbuffer() failed.\n"); + + xds_destroy(xds); + + write(STDOUT_FILENO, buffer, buffer_size); + + fprintf(stderr, "Encoded data:\n"); + fprintf(stderr, "\tint32 = %d\n", int32); + fprintf(stderr, "\tuint32 = 0x%x\n", uint32); + fprintf(stderr, "\tstring = \"%s\"\n", string); + + return 0; + }