From: VMware, Inc <> Date: Tue, 17 Nov 2009 22:05:48 +0000 (-0800) Subject: XdrUtil: Add optional string, opaque field macros. X-Git-Tag: 2009.11.16-210370~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f553580a421f9b233d57cb7221ec810e41e81576;p=thirdparty%2Fopen-vm-tools.git XdrUtil: Add optional string, opaque field macros. During the NicInfoV3 mess, it was a fairly common operation to have to assign arbitrary data to optional string, opaque, and optional opaque fields. This change introduces a few macros which make it a little easier to do just that. Not sure that I'm happy with them, but they get the job done for initializers used in test code. Signed-off-by: Marcelo Vanzin --- diff --git a/open-vm-tools/lib/include/xdrutil.h b/open-vm-tools/lib/include/xdrutil.h index d64be4d36..095a52445 100644 --- a/open-vm-tools/lib/include/xdrutil.h +++ b/open-vm-tools/lib/include/xdrutil.h @@ -27,6 +27,7 @@ #include #include "vm_basic_types.h" +#include "util.h" /* * Helper macros for iterating over an rpcgen-generated array. Given a struct S: @@ -73,6 +74,48 @@ (cnt)) #endif +/* + * Macros for assigning to XDR optional strings, opaque fields, and + * optional opaque fields. + * + * Usage: + * // XDR + * struct MyFoo { string *foo; }; + * struct MyBar { opaque bar; }; + * struct MyOptBar { opaque *bar; }; + * + * // C + * char buf[] = { 0xca, 0xfe, 0xba, 0xbe, 0x80, 0x08 }; + * + * MyFoo foo; + * XDRUTIL_STRING_OPT(&foo.foo, "Hello, world!"); + * + * MyBar bar; + * XDRUTIL_OPAQUE(&bar.bar, buf, sizeof buf); + * + * MyOptBar obar; + * XDRUTIL_OPAQUE_OPT(&obar.bar, buf, sizeof buf); + */ + +#define XDRUTIL_STRING_OPT(ptr, src) do { \ + (ptr) = Util_SafeMalloc(sizeof *(ptr)); \ + *(ptr) = Util_SafeStrdup((src)); \ +} while (0) + +#define XDRUTIL_OPAQUE(ptr, src, srcSize) do { \ + struct { u_int len; char *val; } __opaque_temp = {(srcSize), NULL}; \ + ASSERT_ON_COMPILE(sizeof(*(ptr)) == sizeof(__opaque_temp)); \ + \ + __opaque_temp.val = Util_SafeMalloc((srcSize)); \ + memcpy(__opaque_temp.val, (src), (srcSize)); \ + memcpy(ptr, &__opaque_temp, sizeof __opaque_temp); \ +} while (0) + +#define XDRUTIL_OPAQUE_OPT(ptr, src, srcSize) do { \ + *(ptr) = Util_SafeMalloc(sizeof (struct { u_len; void*; })); \ + XDRUTIL_OPAQUE(*(ptr), (src), (srcSize)); \ +} while(0) + void * XdrUtil_ArrayAppend(void **array, u_int *arrayLen, size_t elemSz, u_int elemCnt);