[headers] Add byte order swapping support
authorShao Miller <Shao.Miller@yrdsb.edu.on.ca>
Thu, 3 Jun 2010 01:51:01 +0000 (21:51 -0400)
committerShao Miller <Shao.Miller@yrdsb.edu.on.ca>
Thu, 3 Jun 2010 02:10:00 +0000 (22:10 -0400)
Simply for convenience, we now have a function
byte__order_swap() and a macro
byte__rev_array_union() and a type
byte__array_union which are all just conveniences
for changing byte order.

WinVBlock.dev
src/include/byte.h [new file with mode: 0644]

index cc9fd5a..5f391df 100644 (file)
@@ -1,7 +1,7 @@
 [Project]\r
 FileName=WinVBlock.dev\r
 Name=WinVBlock\r
-UnitCount=56\r
+UnitCount=57\r
 PchHead=-1\r
 PchSource=-1\r
 Ver=3\r
@@ -641,3 +641,13 @@ Priority=1000
 OverrideBuildCmd=0\r
 BuildCmd=\r
 \r
+[Unit57]\r
+FileName=src\include\byte.h\r
+CompileCpp=1\r
+Folder=Include\r
+Compile=1\r
+Link=1\r
+Priority=1000\r
+OverrideBuildCmd=0\r
+BuildCmd=\r
+\r
diff --git a/src/include/byte.h b/src/include/byte.h
new file mode 100644 (file)
index 0000000..3fc4581
--- /dev/null
@@ -0,0 +1,61 @@
+/**
+ * Copyright (C) 2010, Shao Miller <shao.miller@yrdsb.edu.on.ca>.
+ *
+ * This file is part of WinVBlock, derived from WinAoE.
+ *
+ * WinVBlock is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WinVBlock is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WinVBlock.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef _byte_h
+#  define _byte_h
+
+/**
+ * @file
+ *
+ * Byte order (endian) specifics
+ *
+ */
+
+#  define byte__array_union( type, name ) \
+  union                                 \
+    {                                   \
+      type val;                         \
+      char bytes[sizeof ( type )];      \
+    }                                   \
+    name
+
+/* Function body in header so user-land utility links without WinVBlock */
+static void STDCALL
+byte__order_swap (
+  IN OUT char *bytes,
+  int count
+ )
+{
+  /*
+   * Just for fun
+   */
+  while ( --count > 0 )
+    {
+      bytes[0] ^= bytes[count];
+      bytes[count] ^= bytes[0];
+      bytes[0] ^= bytes[count];
+      bytes++;
+      count--;
+    }
+}
+
+/* Expects a byte_array_union only! */
+#  define byte__rev_array_union( x ) \
+  byte__order_swap ( x.bytes, sizeof(x.bytes) )
+
+#endif                         /* _byte_h */