#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; }