rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rendianness.h File Reference

Byte-order detection, byte swapping, and host/network integer conversion. More...

Go to the source code of this file.

Macros

#define R_LITTLE_ENDIAN   1234
 R_BYTE_ORDER value for little-endian targets.
 
#define R_BIG_ENDIAN   4321
 R_BYTE_ORDER value for big-endian targets.
 
#define RUINT16_BSWAP(val)   ((ruint16) (((ruint16)(val) >> 8) | ((ruint16)(val) << 8)))
 
#define RUINT32_BSWAP(val)
 
#define RUINT64_BSWAP(val)
 
#define R_BYTE_ORDER   R_LITTLE_ENDIAN
 Target byte order: R_LITTLE_ENDIAN or R_BIG_ENDIAN.
 
#define RINT16_TO_BE(val)   ((rint16) (val))
 
#define RUINT16_TO_BE(val)   ((ruint16) (val))
 
#define RINT16_TO_LE(val)   ((rint16) RUINT16_BSWAP (val))
 
#define RUINT16_TO_LE(val)   (RUINT16_BSWAP (val))
 
#define RINT32_TO_BE(val)   ((rint32) (val))
 
#define RUINT32_TO_BE(val)   ((ruint32) (val))
 
#define RINT32_TO_LE(val)   ((rint32) RUINT32_BSWAP (val))
 
#define RUINT32_TO_LE(val)   (RUINT32_BSWAP (val))
 
#define RINT64_TO_BE(val)   ((rint64) (val))
 
#define RUINT64_TO_BE(val)   ((ruint64) (val))
 
#define RINT64_TO_LE(val)   ((rint64) RUINT64_BSWAP (val))
 
#define RUINT64_TO_LE(val)   (RUINT64_BSWAP (val))
 
#define RINT16_FROM_LE(val)   (RINT16_TO_LE (val))
 
#define RUINT16_FROM_LE(val)   (RUINT16_TO_LE (val))
 
#define RINT16_FROM_BE(val)   (RINT16_TO_BE (val))
 
#define RUINT16_FROM_BE(val)   (RUINT16_TO_BE (val))
 
#define RINT32_FROM_LE(val)   (RINT32_TO_LE (val))
 
#define RUINT32_FROM_LE(val)   (RUINT32_TO_LE (val))
 
#define RINT32_FROM_BE(val)   (RINT32_TO_BE (val))
 
#define RUINT32_FROM_BE(val)   (RUINT32_TO_BE (val))
 
#define RINT64_FROM_LE(val)   (RINT64_TO_LE (val))
 
#define RUINT64_FROM_LE(val)   (RUINT64_TO_LE (val))
 
#define RINT64_FROM_BE(val)   (RINT64_TO_BE (val))
 
#define RUINT64_FROM_BE(val)   (RUINT64_TO_BE (val))
 
#define RLONG_FROM_LE(val)   (RLONG_TO_LE (val))
 
#define RULONG_FROM_LE(val)   (RULONG_TO_LE (val))
 
#define RLONG_FROM_BE(val)   (RLONG_TO_BE (val))
 
#define RULONG_FROM_BE(val)   (RULONG_TO_BE (val))
 
#define RINT_FROM_LE(val)   (RINT_TO_LE (val))
 
#define RUINT_FROM_LE(val)   (RUINT_TO_LE (val))
 
#define RINT_FROM_BE(val)   (RINT_TO_BE (val))
 
#define RUINT_FROM_BE(val)   (RUINT_TO_BE (val))
 
#define RSIZE_FROM_LE(val)   (RSIZE_TO_LE (val))
 
#define RSSIZE_FROM_LE(val)   (RSSIZE_TO_LE (val))
 
#define RSIZE_FROM_BE(val)   (RSIZE_TO_BE (val))
 
#define RSSIZE_FROM_BE(val)   (RSSIZE_TO_BE (val))
 
Network/host byte-order aliases (BSD @c ntoh / @c hton names)
#define r_ntohl(val)   (RUINT32_FROM_BE (val))
 Network-to-host, 32-bit (alias for RUINT32_FROM_BE).
 
#define r_ntohs(val)   (RUINT16_FROM_BE (val))
 Network-to-host, 16-bit (alias for RUINT16_FROM_BE).
 
#define r_htonl(val)   (RUINT32_TO_BE (val))
 Host-to-network, 32-bit (alias for RUINT32_TO_BE).
 
#define r_htons(val)   (RUINT16_TO_BE (val))
 Host-to-network, 16-bit (alias for RUINT16_TO_BE).
 

Functions

Alignment-safe load / store

Read or write a multi-byte integer through an arbitrary, possibly unaligned byte pointer (network buffers, packed structs). Casting a ruint8* to a wider pointer and dereferencing is undefined behaviour when alignment isn't met; these helpers go through memcpy, which compilers fold into a single (un)aligned access.

static ruint16 r_load_be16 (const void *p)
 Load a big-endian 16-bit integer from p.
 
static ruint32 r_load_be32 (const void *p)
 Load a big-endian 32-bit integer from p.
 
static ruint64 r_load_be64 (const void *p)
 Load a big-endian 64-bit integer from p.
 
static ruint16 r_load_le16 (const void *p)
 Load a little-endian 16-bit integer from p.
 
static ruint32 r_load_le32 (const void *p)
 Load a little-endian 32-bit integer from p.
 
static ruint64 r_load_le64 (const void *p)
 Load a little-endian 64-bit integer from p.
 
static void r_store_be16 (void *p, ruint16 v)
 Store v as a big-endian 16-bit integer at p.
 
static void r_store_be32 (void *p, ruint32 v)
 Store v as a big-endian 32-bit integer at p.
 
static void r_store_be64 (void *p, ruint64 v)
 Store v as a big-endian 64-bit integer at p.
 
static void r_store_le16 (void *p, ruint16 v)
 Store v as a little-endian 16-bit integer at p.
 
static void r_store_le32 (void *p, ruint32 v)
 Store v as a little-endian 32-bit integer at p.
 
static void r_store_le64 (void *p, ruint64 v)
 Store v as a little-endian 64-bit integer at p.
 

Detailed Description

Byte-order detection, byte swapping, and host/network integer conversion.