summaryrefslogtreecommitdiffstats
path: root/src/cvtendian.h
diff options
context:
space:
mode:
authorTimothy Pearson <[email protected]>2014-12-27 08:13:20 -0600
committerTimothy Pearson <[email protected]>2014-12-27 08:13:20 -0600
commit9b92536e6c51b66406d593745a938975e226f95e (patch)
treea40b0066dc774827f602f5e372c2f53656007553 /src/cvtendian.h
downloadlibr-9b92536e6c51b66406d593745a938975e226f95e.tar.gz
libr-9b92536e6c51b66406d593745a938975e226f95e.zip
Initial import
Diffstat (limited to 'src/cvtendian.h')
-rw-r--r--src/cvtendian.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/cvtendian.h b/src/cvtendian.h
new file mode 100644
index 0000000..d69158b
--- /dev/null
+++ b/src/cvtendian.h
@@ -0,0 +1,48 @@
+#ifndef __CVTENDIAN_H
+#define __CVTENDIAN_H
+
+/* Support for swapping bytes (endian conversion) */
+#include <byteswap.h>
+
+/* For obtaining the host endian type */
+#include <endian.h>
+#if (__BYTE_ORDER == __LITTLE_ENDIAN)
+# define HOST_ENDIAN ELFDATA2LSB
+#elif (__BYTE_ORDER == __BIG_ENDIAN)
+# define HOST_ENDIAN ELFDATA2MSB
+#else
+# error "Failed to detect host endian type"
+#endif
+
+/*
+ * Convert the endian of a parameter
+ */
+static int ConvertEndian(void *ptr, int bytes)
+{
+ switch(bytes)
+ {
+ case 2:
+ {
+ uint16_t *value = (uint16_t *) ptr;
+
+ *value = bswap_16(*value);
+ } return 1;
+ case 4:
+ {
+ uint32_t *value = (uint32_t *) ptr;
+
+ *value = bswap_32(*value);
+ } return 1;
+ case 8:
+ {
+ uint64_t *value = (uint64_t *) ptr;
+
+ *value = bswap_64(*value);
+ } return 1;
+ default:
+ break;
+ }
+ return 0;
+}
+
+#endif /* __CVTENDIAN_H */