]> git.draconx.ca Git - liblbx.git/blobdiff - src/byteorder.h
Starting implementation of image parsing code.
[liblbx.git] / src / byteorder.h
index c997b10f1dba24ef4de7891a81145dfea54d854d..4cf2854424d68514d31981f11c29ca196befad02 100644 (file)
@@ -5,12 +5,32 @@
 #      include "config.h"
 #endif
 
+#include <stdint.h>
+
+static inline uint16_t _flip16(uint16_t val)
+{
+       uint16_t hostval;
+       int i;
+
+       for (i = 0; i < sizeof val; i++)
+               ((uint8_t *)&hostval)[i] = ((uint8_t *)&val)[sizeof val - i];
+}
+
+static inline uint32_t _flip32(uint32_t val)
+{
+       uint32_t hostval;
+       int i;
+
+       for (i = 0; i < sizeof val; i++)
+               ((uint8_t *)&hostval)[i] = ((uint8_t *)&val)[sizeof val - i];
+}
+
 #ifdef WORDS_BIGENDIAN
-       uint16_t letohs(uint16_t);
-       uint32_t letohl(uint32_t);
+#      define letohs(x) _flip16(x)
+#      define letohl(x) _flip32(x)
 #else
-#      define letohs(x) (x)
-#      define letohl(x) (x)
+#      define letohs(x) ((uint16_t)(x))
+#      define letohl(x) ((uint16_t)(x))
 #endif
 
 #endif