byteorder.h File Reference

Functions to work with different byte orders. More...

Detailed Description

Functions to work with different byte orders.

Author
René Kijewski rene..nosp@m.kije.nosp@m.wski@.nosp@m.fu-b.nosp@m.erlin.nosp@m..de

Definition in file byteorder.h.

#include <stdint.h>
#include "unaligned.h"
+ Include dependency graph for byteorder.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

union  le_uint16_t
 A 16 bit integer in little endian. More...
 
union  le_uint32_t
 A 32 bit integer in little endian. More...
 
union  le_uint64_t
 A 64 bit integer in little endian. More...
 
union  be_uint16_t
 A 16 bit integer in big endian aka network byte order. More...
 
union  be_uint32_t
 A 32 bit integer in big endian aka network byte order. More...
 
union  be_uint64_t
 A 64 bit integer in big endian aka network byte order. More...
 
#define _byteorder_swap(V, T)   (byteorder_swap ## T((V)))
 Swaps the byteorder according to the endianness.
 
typedef be_uint16_t network_uint16_t
 A 16 bit integer in network byte order.
 
typedef be_uint32_t network_uint32_t
 A 32 bit integer in network byte order.
 
typedef be_uint64_t network_uint64_t
 A 64 bit integer in network byte order.
 
static be_uint16_t byteorder_ltobs (le_uint16_t v)
 Convert from little endian to big endian, 16 bit. More...
 
static be_uint32_t byteorder_ltobl (le_uint32_t v)
 Convert from little endian to big endian, 32 bit. More...
 
static be_uint64_t byteorder_ltobll (le_uint64_t v)
 Convert from little endian to big endian, 64 bit. More...
 
static le_uint16_t byteorder_btols (be_uint16_t v)
 Convert from big endian to little endian, 16 bit. More...
 
static le_uint32_t byteorder_btoll (be_uint32_t v)
 Convert from big endian to little endian, 32 bit. More...
 
static le_uint64_t byteorder_btolll (be_uint64_t v)
 Convert from big endian to little endian, 64 bit. More...
 
static network_uint16_t byteorder_htons (uint16_t v)
 Convert from host byte order to network byte order, 16 bit. More...
 
static network_uint32_t byteorder_htonl (uint32_t v)
 Convert from host byte order to network byte order, 32 bit. More...
 
static network_uint64_t byteorder_htonll (uint64_t v)
 Convert from host byte order to network byte order, 64 bit. More...
 
static uint16_t byteorder_ntohs (network_uint16_t v)
 Convert from network byte order to host byte order, 16 bit. More...
 
static uint32_t byteorder_ntohl (network_uint32_t v)
 Convert from network byte order to host byte order, 32 bit. More...
 
static uint64_t byteorder_ntohll (network_uint64_t v)
 Convert from network byte order to host byte order, 64 bit. More...
 
static uint16_t byteorder_swaps (uint16_t v)
 Swap byte order, 16 bit. More...
 
static uint32_t byteorder_swapl (uint32_t v)
 Swap byte order, 32 bit. More...
 
static uint64_t byteorder_swapll (uint64_t v)
 Swap byte order, 64 bit. More...
 
static uint16_t byteorder_bebuftohs (const uint8_t *buf)
 Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable, 16-bit. More...
 
static uint32_t byteorder_bebuftohl (const uint8_t *buf)
 Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable, 32-bit. More...
 
static uint64_t byteorder_bebuftohll (const uint8_t *buf)
 Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable, 64-bit. More...
 
static void byteorder_htobebufs (uint8_t *buf, uint16_t val)
 Write a host byte order encoded unsigned integer as big endian encoded value into a buffer, 16-bit. More...
 
static void byteorder_htobebufl (uint8_t *buf, uint32_t val)
 Write a host byte order encoded unsigned integer as big endian encoded value into a buffer, 32-bit. More...
 
static void byteorder_htobebufll (uint8_t *buf, uint64_t val)
 Write a host byte order encoded unsigned integer as big endian encoded value into a buffer, 64-bit. More...
 
static uint16_t htons (uint16_t v)
 Convert from host byte order to network byte order, 16 bit. More...
 
static uint32_t htonl (uint32_t v)
 Convert from host byte order to network byte order, 32 bit. More...
 
static uint64_t htonll (uint64_t v)
 Convert from host byte order to network byte order, 64 bit. More...
 
static uint16_t ntohs (uint16_t v)
 Convert from network byte order to host byte order, 16 bit. More...
 
static uint32_t ntohl (uint32_t v)
 Convert from network byte order to host byte order, 32 bit. More...
 
static uint64_t ntohll (uint64_t v)
 Convert from network byte order to host byte order, 64 bit. More...
 

Function Documentation

◆ byteorder_bebuftohl()

static uint32_t byteorder_bebuftohl ( const uint8_t *  buf)
inlinestatic

Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable, 32-bit.

Note
This function is agnostic to the alignment of the target value in the given buffer
Parameters
[in]bufposition in a buffer holding the target value
Returns
32-bit unsigned integer in host byte order

Definition at line 520 of file byteorder.h.

◆ byteorder_bebuftohll()

static uint64_t byteorder_bebuftohll ( const uint8_t *  buf)
inlinestatic

Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable, 64-bit.

Note
This function is agnostic to the alignment of the target value in the given buffer
Parameters
[in]bufposition in a buffer holding the target value
Returns
64-bit unsigned integer in host byte order

Definition at line 534 of file byteorder.h.

◆ byteorder_bebuftohs()

static uint16_t byteorder_bebuftohs ( const uint8_t *  buf)
inlinestatic

Read a big endian encoded unsigned integer from a buffer into host byte order encoded variable, 16-bit.

Note
This function is agnostic to the alignment of the target value in the given buffer
Parameters
[in]bufposition in a buffer holding the target value
Returns
16-bit unsigned integer in host byte order

Definition at line 509 of file byteorder.h.

◆ byteorder_btoll()

static le_uint32_t byteorder_btoll ( be_uint32_t  v)
inlinestatic

Convert from big endian to little endian, 32 bit.

Parameters
[in]vThe integer in big endian.
Returns
v converted to little endian.

Definition at line 412 of file byteorder.h.

◆ byteorder_btolll()

static le_uint64_t byteorder_btolll ( be_uint64_t  v)
inlinestatic

Convert from big endian to little endian, 64 bit.

Parameters
[in]vThe integer in big endian.
Returns
v converted to little endian.

Definition at line 419 of file byteorder.h.

◆ byteorder_btols()

static le_uint16_t byteorder_btols ( be_uint16_t  v)
inlinestatic

Convert from big endian to little endian, 16 bit.

Parameters
[in]vThe integer in big endian.
Returns
v converted to little endian.

Definition at line 405 of file byteorder.h.

◆ byteorder_htobebufl()

static void byteorder_htobebufl ( uint8_t *  buf,
uint32_t  val 
)
inlinestatic

Write a host byte order encoded unsigned integer as big endian encoded value into a buffer, 32-bit.

Note
This function is alignment agnostic and works with any given memory location of the buffer
Parameters
[out]buftarget buffer, must be able to accept 4 bytes
[in]valvalue written to the buffer, in host byte order

Definition at line 564 of file byteorder.h.

◆ byteorder_htobebufll()

static void byteorder_htobebufll ( uint8_t *  buf,
uint64_t  val 
)
inlinestatic

Write a host byte order encoded unsigned integer as big endian encoded value into a buffer, 64-bit.

Note
This function is alignment agnostic and works with any given memory location of the buffer
Parameters
[out]buftarget buffer, must be able to accept 8 bytes
[in]valvalue written to the buffer, in host byte order

Definition at line 578 of file byteorder.h.

◆ byteorder_htobebufs()

static void byteorder_htobebufs ( uint8_t *  buf,
uint16_t  val 
)
inlinestatic

Write a host byte order encoded unsigned integer as big endian encoded value into a buffer, 16-bit.

Note
This function is alignment agnostic and works with any given memory location of the buffer
Parameters
[out]buftarget buffer, must be able to accept 2 bytes
[in]valvalue written to the buffer, in host byte order

Definition at line 552 of file byteorder.h.

◆ byteorder_htonl()

static network_uint32_t byteorder_htonl ( uint32_t  v)
inlinestatic

Convert from host byte order to network byte order, 32 bit.

Parameters
[in]vThe integer in host byte order.
Returns
v converted to network byte order.

Definition at line 444 of file byteorder.h.

◆ byteorder_htonll()

static network_uint64_t byteorder_htonll ( uint64_t  v)
inlinestatic

Convert from host byte order to network byte order, 64 bit.

Parameters
[in]vThe integer in host byte order.
Returns
v converted to network byte order.

Definition at line 451 of file byteorder.h.

◆ byteorder_htons()

static network_uint16_t byteorder_htons ( uint16_t  v)
inlinestatic

Convert from host byte order to network byte order, 16 bit.

Parameters
[in]vThe integer in host byte order.
Returns
v converted to network byte order.

Definition at line 437 of file byteorder.h.

◆ byteorder_ltobl()

static be_uint32_t byteorder_ltobl ( le_uint32_t  v)
inlinestatic

Convert from little endian to big endian, 32 bit.

Parameters
[in]vThe integer in little endian.
Returns
v converted to big endian.

Definition at line 391 of file byteorder.h.

◆ byteorder_ltobll()

static be_uint64_t byteorder_ltobll ( le_uint64_t  v)
inlinestatic

Convert from little endian to big endian, 64 bit.

Parameters
[in]vThe integer in little endian.
Returns
v converted to big endian.

Definition at line 398 of file byteorder.h.

◆ byteorder_ltobs()

static be_uint16_t byteorder_ltobs ( le_uint16_t  v)
inlinestatic

Convert from little endian to big endian, 16 bit.

Parameters
[in]vThe integer in little endian.
Returns
v converted to big endian.

Definition at line 384 of file byteorder.h.

◆ byteorder_ntohl()

static uint32_t byteorder_ntohl ( network_uint32_t  v)
inlinestatic

Convert from network byte order to host byte order, 32 bit.

Parameters
[in]vThe integer in network byte order.
Returns
v converted to host byte order.

Definition at line 463 of file byteorder.h.

◆ byteorder_ntohll()

static uint64_t byteorder_ntohll ( network_uint64_t  v)
inlinestatic

Convert from network byte order to host byte order, 64 bit.

Parameters
[in]vThe integer in network byte order.
Returns
v converted to host byte order.

Definition at line 468 of file byteorder.h.

◆ byteorder_ntohs()

static uint16_t byteorder_ntohs ( network_uint16_t  v)
inlinestatic

Convert from network byte order to host byte order, 16 bit.

Parameters
[in]vThe integer in network byte order.
Returns
v converted to host byte order.

Definition at line 458 of file byteorder.h.

◆ byteorder_swapl()

static uint32_t byteorder_swapl ( uint32_t  v)
inlinestatic

Swap byte order, 32 bit.

Parameters
[in]vThe integer to swap.
Returns
The swapped integer.

Definition at line 374 of file byteorder.h.

◆ byteorder_swapll()

static uint64_t byteorder_swapll ( uint64_t  v)
inlinestatic

Swap byte order, 64 bit.

Parameters
[in]vThe integer to swap.
Returns
The swapped integer.

Definition at line 379 of file byteorder.h.

◆ byteorder_swaps()

static uint16_t byteorder_swaps ( uint16_t  v)
inlinestatic

Swap byte order, 16 bit.

Parameters
[in]vThe integer to swap.
Returns
The swapped integer.

Definition at line 361 of file byteorder.h.

◆ htonl()

static uint32_t htonl ( uint32_t  v)
inlinestatic

Convert from host byte order to network byte order, 32 bit.

See also
byteorder_htonl()
Parameters
[in]vThe integer to convert.
Returns
Converted integer.

Definition at line 478 of file byteorder.h.

◆ htonll()

static uint64_t htonll ( uint64_t  v)
inlinestatic

Convert from host byte order to network byte order, 64 bit.

See also
byteorder_htonll()
Parameters
[in]vThe integer to convert.
Returns
Converted integer.

Definition at line 483 of file byteorder.h.

◆ htons()

static uint16_t htons ( uint16_t  v)
inlinestatic

Convert from host byte order to network byte order, 16 bit.

See also
byteorder_htons()
Parameters
[in]vThe integer to convert.
Returns
Converted integer.

Definition at line 473 of file byteorder.h.

◆ ntohl()

static uint32_t ntohl ( uint32_t  v)
inlinestatic

Convert from network byte order to host byte order, 32 bit.

See also
byteorder_ntohl()
Parameters
[in]vThe integer to convert.
Returns
Converted integer.

Definition at line 495 of file byteorder.h.

◆ ntohll()

static uint64_t ntohll ( uint64_t  v)
inlinestatic

Convert from network byte order to host byte order, 64 bit.

See also
byteorder_ntohll()
Parameters
[in]vThe integer to convert.
Returns
Converted integer.

Definition at line 502 of file byteorder.h.

◆ ntohs()

static uint16_t ntohs ( uint16_t  v)
inlinestatic

Convert from network byte order to host byte order, 16 bit.

See also
byteorder_ntohs()
Parameters
[in]vThe integer to convert.
Returns
Converted integer.

Definition at line 488 of file byteorder.h.