]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
XdrUtil: Add optional string, opaque field macros.
authorVMware, Inc <>
Tue, 17 Nov 2009 22:05:48 +0000 (14:05 -0800)
committerMarcelo Vanzin <mvanzin@vmware.com>
Tue, 17 Nov 2009 22:05:48 +0000 (14:05 -0800)
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 <mvanzin@vmware.com>
open-vm-tools/lib/include/xdrutil.h

index d64be4d36ccfc5d3d6a9d78695230a964a8f2764..095a524459bb7896da6dceb98e30bed1893b2e20 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <rpc/rpc.h>
 #include "vm_basic_types.h"
+#include "util.h"
 
 /*
  * Helper macros for iterating over an rpcgen-generated array. Given a struct S:
                            (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);