marshal: Detect the existence of endian.h and account of endianness.

If endian.h is available then the htobe etc functions will handle
endianness for us. If it's not available then we provide our own
endian conversion functions.

Signed-off-by: Philip Tricca <philip.b.tricca@intel.com>
diff --git a/marshal/base-types.c b/marshal/base-types.c
index 6c1e346..d14d407 100644
--- a/marshal/base-types.c
+++ b/marshal/base-types.c
@@ -102,3 +102,16 @@
 BASE_UNMARSHAL (UINT8,  BE_TO_HOST_8);
 BASE_MARSHAL   (UINT16, HOST_TO_BE_16);
 BASE_UNMARSHAL (UINT16, BE_TO_HOST_16);
+
+/*
+ * If we don't have endian.h then we need to fake it with our own endianness
+ * conversion functions.
+ */
+#if !defined(HAVE_ENDIAN_H) && !defined(WORDS_BIGENDIAN)
+UINT16
+endian_conv_16 (UINT16 value)
+{
+    return ((value & (0xff))      << 8) | \
+           ((value & (0xff << 8)) >> 8);
+}
+#endif /* HAVE_ENDIAN_H */
diff --git a/marshal/base-types.h b/marshal/base-types.h
index 78950ad..aa707f5 100644
--- a/marshal/base-types.h
+++ b/marshal/base-types.h
@@ -28,7 +28,6 @@
 #ifndef BASE_TYPES_H
 #define BASE_TYPES_H
 
-#include <endian.h>
 #include "sapi/tpm20.h"
 
 /*
@@ -37,9 +36,29 @@
  */
 static inline UINT8 noop8 (UINT8 value) { return value; }
 
+#if defined(HAVE_ENDIAN_H)
+
+#include <endian.h>
 #define HOST_TO_BE_8(value)  noop8   (value)
 #define HOST_TO_BE_16(value) htobe16 (value)
 #define BE_TO_HOST_8(value)  noop8   (value)
 #define BE_TO_HOST_16(value) be16toh (value)
 
+#elif defined(WORDS_BIGENDIAN)
+
+static inline UINT16 noop16 (UINT16 value) { return value; }
+#define HOST_TO_BE_8(value)  noop8  (value)
+#define HOST_TO_BE_16(value) noop16 (value)
+#define BE_TO_HOST_8(value)  noop8  (value)
+#define BE_TO_HOST_16(value) noop16 (value)
+
+#else
+
+UINT16 endian_conv_16 (UINT16 value);
+#define HOST_TO_BE_8(value)  noop8          (value)
+#define HOST_TO_BE_16(value) endian_conv_16 (value)
+#define BE_TO_HOST_8(value)  noop8          (value)
+#define BE_TO_HOST_16(value) endian_conv_16 (value)
+
+#endif /* HAVE_ENDIAN_H */
 #endif /* BASE_TYPES_H  */