#include <libcork/core.h>
This section contains definitions for determining the endianness of the host system, and for byte-swapping integer values of various sizes.
The CORK_HOST_ENDIANNESS macro can be used to determine the endianness of the host system. It will be equal to either CORK_LITTLE_ENDIAN or CORK_BIG_ENDIAN. (The actual values don’t matter; you should always compare against the predefined constants.) The CORK_OTHER_endianness macro is defined to be the opposite endianness as CORK_HOST_ENDIANNESS. A common use case would be something like:
#if CORK_HOST_endianness == CORK_LITTLE_ENDIAN
/* do something to little-endian values */
#else
/* do something to big-endian values */
#endif
These macros give you a human-readable name of the host’s endianness. You can use this in debugging messages.
Note
You should not use these macros to detect the endianness of the system, since we might change their definitions at some point to support localization. For that, use CORK_LITTLE_ENDIAN and CORK_BIG_ENDIAN.
All of the macros in this section take in an rvalue (i.e., any arbitrary expression) as a parameter. The result of the swap is returned as the value of the macro.
These functions always perform a byte-swap, regardless of the endianness of the host system.
These functions convert a big-endian (or network-endian) value into host endianness. (I.e., they only perform a swap if the current host is little-endian.)
These functions convert a host-endian value into big (or network) endianness. (I.e., they only perform a swap if the current host is little-endian.)
The macros in this section swap an integer in place, which means that the original value is overwritten with the result of the swap. To support this, you must pass in an lvalue as the parameter to the macro. (Note that you don’t pass in a pointer to the original value; these operations are implemented as macros, and you just need to provide a reference to the variable to be swapped.)
These functions always perform a byte-swap, regardless of the endianness of the host system.
These functions convert a big-endian (or network-endian) value into host endianness, and vice versa. (I.e., they only perform a swap if the current host is little-endian.)
These functions convert a host-endian value into big (or network) endianness. (I.e., they only perform a swap if the current host is little-endian.)
These functions convert a little-endian value into host endianness, and vice versa. (I.e., they only perform a swap if the current host is big-endian.)
These functions convert a host-endian value into little endianness. (I.e., they only perform a swap if the current host is big-endian.)