Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Try to unbust things on big endian platforms. Untested, but should do…
… the trick.
  • Loading branch information
jnthn committed Jun 11, 2012
1 parent d82864d commit b1226fb
Showing 1 changed file with 41 additions and 8 deletions.
49 changes: 41 additions & 8 deletions src/6model/serialization.c
Expand Up @@ -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
Expand Down Expand Up @@ -1022,31 +1047,39 @@ STRING * Serialization_serialize(PARROT_INTERP, PMC *sc, PMC *empty_string_heap)
/* 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;
}

/* 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;
}

/* 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;
}

/* 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;
}
Expand Down

0 comments on commit b1226fb

Please sign in to comment.