I am writing an ELF analyzer, but I'm having some trouble converting endianness properly. I have functions to determine the endianness of the analyzer and the endiannness of the object file.
Basically, there are four possible scenarios:
- A big endian compiled analyzer run on a big endian object file
- nothing needs converted
- A big endian compiled analyzer run on a little endian object file
- the byte order needs swapped, but ntohs/l() and htons/l() are both null macros on a big endian machine, so they won't swap the byte order. This is the problem
- A little endian compiled analyzer run on a big endian object file
- the byte order needs swapped, so use htons() to swap the byte order
- A little endian compiled analyzer run on a little endian object file.
- nothing needs converted
Is there a function I can use to explicitly swap byte order/change endianness, since ntohs/l() and htons/l() take the host's endianness into account and sometimes don't convert? Or do I need to find/write my own swap byte order function?