$OpenBSD: patch-src_6model_serialization_c,v 1.2 2012/07/13 09:46:41 pascal Exp $

http://github.com/perl6/nqp/commit/b1226fb1bf4857662b9baa326b20ae410c464695

--- src/6model/serialization.c.orig	Sat Jun 23 14:50:50 2012
+++ src/6model/serialization.c	Sun Jul  8 11:07:48 2012
@@ -59,32 +59,57 @@ static INTVAL ctmthunk_id = 0;
 static INTVAL ownedhash_id = 0;
 static INTVAL ownedrpa_id = 0;
 
+/* Endian translation (file format is little endian, so on big endian we need
+ * to twiddle. */
+#if PARROT_BIGENDIAN
+static void switch_endian(char *bytes, size_t size)
+{
+    size_t low  = 0;
+    size_t high = size - 1;
+    while (high > low) {
+        char tmp    = bytes[low];
+        bytes[low]  = bytes[high];
+        bytes[high] = tmp;
+        low++;
+        high--;
+    }
+}
+#endif
+
 /* ***************************************************************************
  * Serialization (writing related)
  * ***************************************************************************/
 
 /* Writes an int64 into a buffer. */
 static void write_int64(char *buffer, size_t offset, Parrot_Int8 value) {
-    /* XXX: Big Endian Handling! */
     memcpy(buffer + offset, &value, 8);
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 8);
+#endif
 }
 
 /* Writes an int32 into a buffer. */
 static void write_int32(char *buffer, size_t offset, Parrot_Int4 value) {
-    /* XXX: Big Endian Handling! */
     memcpy(buffer + offset, &value, 4);
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 4);
+#endif
 }
 
 /* Writes an int16 into a buffer. */
 static void write_int16(char *buffer, size_t offset, Parrot_Int2 value) {
-    /* XXX: Big Endian Handling! */
     memcpy(buffer + offset, &value, 2);
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 2);
+#endif
 }
 
 /* Writes an double into a buffer. */
 static void write_double(char *buffer, size_t offset, double value) {
-    /* XXX: Big Endian Handling! */
     memcpy(buffer + offset, &value, 8);
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 8);
+#endif
 }
 
 /* Adds an item to the string heap if needed, and returns the index where
@@ -1022,7 +1047,9 @@ STRING * Serialization_serialize(PARROT_INTERP, PMC *s
 /* Reads an int64 from a buffer. */
 static Parrot_Int8 read_int64(char *buffer, size_t offset) {
     Parrot_Int8 value;
-    /* XXX: Big Endian Handling! */
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 8);
+#endif
     memcpy(&value, buffer + offset, 8);
     return value;
 }
@@ -1030,7 +1057,9 @@ static Parrot_Int8 read_int64(char *buffer, size_t off
 /* Reads an int32 from a buffer. */
 static Parrot_Int4 read_int32(char *buffer, size_t offset) {
     Parrot_Int4 value;
-    /* XXX: Big Endian Handling! */
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 4);
+#endif
     memcpy(&value, buffer + offset, 4);
     return value;
 }
@@ -1038,7 +1067,9 @@ static Parrot_Int4 read_int32(char *buffer, size_t off
 /* Reads an int16 from a buffer. */
 static Parrot_Int2 read_int16(char *buffer, size_t offset) {
     Parrot_Int2 value;
-    /* XXX: Big Endian Handling! */
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 2);
+#endif
     memcpy(&value, buffer + offset, 2);
     return value;
 }
@@ -1046,7 +1077,9 @@ static Parrot_Int2 read_int16(char *buffer, size_t off
 /* Reads double from a buffer. */
 static double read_double(char *buffer, size_t offset) {
     double value;
-    /* XXX: Big Endian Handling! */
+#if PARROT_BIGENDIAN
+    switch_endian(buffer + offset, 8);
+#endif
     memcpy(&value, buffer + offset, 8);
     return value;
 }
