Skip to content

Commit

Permalink
lputil: Proper support for big-endian platforms with the various Read…
Browse files Browse the repository at this point in the history
…*() functions

Determine at configure-time whether we're building for a big-endian or a
little-endian machine, which in turn will enable or disable such things as
htons(3) from happening
  • Loading branch information
dressupgeekout committed Jul 20, 2020
1 parent 178953b commit 7eab951
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@ if(${LUNAPURPURA_DEBUG})
add_definitions(-DLUNAPURPURA_DEBUG)
endif()

include(TestBigEndian)
test_big_endian(LUNAPURPURA_BIG_ENDIAN)
if(${LUNAPURPURA_BIG_ENDIAN})
add_definitions(-DLUNAPURPURA_BIG_ENDIAN)
endif()

# Good to go.
add_subdirectory(src)
12 changes: 10 additions & 2 deletions src/lputil/lputil.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ size_t
ReadUint16(FILE *file, size_t count, uint16_t *dest)
{
size_t rv = fread(dest, sizeof(uint16_t), count, file);
#ifndef LUNAPURPURA_BIG_ENDIAN
*dest = htons(*dest);
#endif
return rv;
}

Expand All @@ -197,7 +199,9 @@ size_t
ReadUint16LE(FILE *file, size_t count, uint16_t *dest)
{
size_t rv = fread(dest, sizeof(uint16_t), count, file);
/* XXX do something to guarantee it was little-endian */
#ifdef LUNAPURPURA_BIG_ENDIAN
*dest = ntohs(*dest);
#endif
return rv;
}

Expand All @@ -210,7 +214,9 @@ size_t
ReadUint32(FILE *file, size_t count, uint32_t *dest)
{
size_t rv = fread(dest, sizeof(uint32_t), count, file);
#ifndef LUNAPURPURA_BIG_ENDIAN
*dest = htonl(*dest);
#endif
return rv;
}

Expand All @@ -223,6 +229,8 @@ size_t
ReadUint32LE(FILE *file, size_t count, uint32_t *dest)
{
size_t rv = fread(dest, sizeof(uint32_t), count, file);
/* XXX do something to guarantee it was little-endian */
#ifdef LUNAPURPURA_BIG_ENDIAN
*dest = ntohl(*dest);
#endif
return rv;
}

0 comments on commit 7eab951

Please sign in to comment.