rlib
Convenience library for useful things
Loading...
Searching...
No Matches

Byte-order detection, byte-swapping, host/network conversion and alignment-safe load / store of multi-byte integers. More...

Files

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

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))
 

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.
 

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).
 

Detailed Description

Byte-order detection, byte-swapping, host/network conversion and alignment-safe load / store of multi-byte integers.

R_BYTE_ORDER resolves to R_LITTLE_ENDIAN or R_BIG_ENDIAN for the target. The conversion macros follow the R<TYPE>_TO_<ORDER> / R<TYPE>_FROM_<ORDER> convention (for each integer type and LE / BE order) and are no-ops when the host order already matches, a byte swap otherwise. The RUINT16_BSWAP family below is the unconditional swap the conversions build on.

For reading or writing integers out of arbitrary (possibly unaligned) byte pointers — network buffers, packed structs — use the r_load_be32 / r_store_le32 inline helpers, which go through memcpy to avoid undefined behaviour.

Macro Definition Documentation

◆ RUINT32_BSWAP

#define RUINT32_BSWAP (   val)
Value:
((ruint32) ( \
((((ruint32)(val)) ) >> 24) | \
((((ruint32)(val)) & 0x00FF0000) >> 8) | \
((((ruint32)(val)) & 0x0000FF00) << 8) | \
((((ruint32)(val)) ) << 24)))
unsigned int ruint32
Unsigned 32-bit integer.
Definition rtypes.h:192

◆ RUINT64_BSWAP

#define RUINT64_BSWAP (   val)
Value:
((ruint64) ( \
(((ruint64)(val) & RUINT64_CONSTANT (0x00000000000000FF)) << 56) | \
(((ruint64)(val) & RUINT64_CONSTANT (0x000000000000FF00)) << 40) | \
(((ruint64)(val) & RUINT64_CONSTANT (0x0000000000FF0000)) << 24) | \
(((ruint64)(val) & RUINT64_CONSTANT (0x00000000FF000000)) << 8) | \
(((ruint64)(val) & RUINT64_CONSTANT (0x000000FF00000000)) >> 8) | \
(((ruint64)(val) & RUINT64_CONSTANT (0x0000FF0000000000)) >> 24) | \
(((ruint64)(val) & RUINT64_CONSTANT (0x00FF000000000000)) >> 40) | \
(((ruint64)(val) & RUINT64_CONSTANT (0xFF00000000000000)) >> 56)))
#define RUINT64_CONSTANT(val)
Suffix an integer literal as a 64-bit unsigned (ruint64) constant.
Definition rtypes.h:270
unsigned long ruint64
Unsigned 64-bit integer.
Definition rtypes.h:193