Loading...
Searching...
No Matches
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 <string.h>
#include <stdint.h>
#include <endian.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...
 
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 uint16_t byteorder_ltohs (le_uint16_t v)
 Convert from little endian to host byte order, 16 bit.
 
static uint32_t byteorder_ltohl (le_uint32_t v)
 Convert from little endian to host byte order, 32 bit.
 
static uint64_t byteorder_ltohll (le_uint64_t v)
 Convert from little endian to host byte order, 64 bit.
 
static be_uint16_t byteorder_ltobs (le_uint16_t v)
 Convert from little endian to big endian, 16 bit.
 
static be_uint32_t byteorder_ltobl (le_uint32_t v)
 Convert from little endian to big endian, 32 bit.
 
static be_uint64_t byteorder_ltobll (le_uint64_t v)
 Convert from little endian to big endian, 64 bit.
 
static le_uint16_t byteorder_btols (be_uint16_t v)
 Convert from big endian to little endian, 16 bit.
 
static le_uint32_t byteorder_btoll (be_uint32_t v)
 Convert from big endian to little endian, 32 bit.
 
static le_uint64_t byteorder_btolll (be_uint64_t v)
 Convert from big endian to little endian, 64 bit.
 
static le_uint16_t byteorder_htols (uint16_t v)
 Convert from host byte order to little endian, 16 bit.
 
static le_uint32_t byteorder_htoll (uint32_t v)
 Convert from host byte order to little endian, 32 bit.
 
static le_uint64_t byteorder_htolll (uint64_t v)
 Convert from host byte order to little endian, 64 bit.
 
static network_uint16_t byteorder_htons (uint16_t v)
 Convert from host byte order to network byte order, 16 bit.
 
static network_uint32_t byteorder_htonl (uint32_t v)
 Convert from host byte order to network byte order, 32 bit.
 
static network_uint64_t byteorder_htonll (uint64_t v)
 Convert from host byte order to network byte order, 64 bit.
 
static uint16_t byteorder_ntohs (network_uint16_t v)
 Convert from network byte order to host byte order, 16 bit.
 
static uint32_t byteorder_ntohl (network_uint32_t v)
 Convert from network byte order to host byte order, 32 bit.
 
static uint64_t byteorder_ntohll (network_uint64_t v)
 Convert from network byte order to host byte order, 64 bit.
 
static uint16_t byteorder_swaps (uint16_t v)
 Swap byte order, 16 bit.
 
static uint32_t byteorder_swapl (uint32_t v)
 Swap byte order, 32 bit.
 
static uint64_t byteorder_swapll (uint64_t v)
 Swap byte order, 64 bit.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
static uint16_t htons (uint16_t v)
 Convert from host byte order to network byte order, 16 bit.
 
static uint32_t htonl (uint32_t v)
 Convert from host byte order to network byte order, 32 bit.
 
static uint64_t htonll (uint64_t v)
 Convert from host byte order to network byte order, 64 bit.
 
static uint16_t ntohs (uint16_t v)
 Convert from network byte order to host byte order, 16 bit.
 
static uint32_t ntohl (uint32_t v)
 Convert from network byte order to host byte order, 32 bit.
 
static uint64_t ntohll (uint64_t v)
 Convert from network byte order to host byte order, 64 bit.
 
static uint16_t byteorder_lebuftohs (const uint8_t *buf)
 
static uint32_t byteorder_lebuftohl (const uint8_t *buf)
 
static uint64_t byteorder_lebuftohll (const uint8_t *buf)
 
static void byteorder_htolebufs (uint8_t *buf, uint16_t val)
 
static void byteorder_htolebufl (uint8_t *buf, uint32_t val)
 
static void byteorder_htolebufll (uint8_t *buf, uint64_t val)
 

Typedef Documentation

◆ network_uint16_t

A 16 bit integer in network byte order.

Definition at line 108 of file byteorder.h.

◆ network_uint32_t

A 32 bit integer in network byte order.

Definition at line 113 of file byteorder.h.

◆ network_uint64_t

A 64 bit integer in network byte order.

Definition at line 118 of file byteorder.h.

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 556 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 561 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 551 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 450 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 457 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 443 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 572 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 566 of file byteorder.h.

◆ byteorder_htolebufl()

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

Definition at line 605 of file byteorder.h.

◆ byteorder_htolebufll()

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

Definition at line 611 of file byteorder.h.

◆ byteorder_htolebufs()

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

Definition at line 599 of file byteorder.h.

◆ byteorder_htoll()

static le_uint32_t byteorder_htoll ( uint32_t  v)
inlinestatic

Convert from host byte order to little endian, 32 bit.

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

Definition at line 471 of file byteorder.h.

◆ byteorder_htolll()

static le_uint64_t byteorder_htolll ( uint64_t  v)
inlinestatic

Convert from host byte order to little endian, 64 bit.

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

Definition at line 478 of file byteorder.h.

◆ byteorder_htols()

static le_uint16_t byteorder_htols ( uint16_t  v)
inlinestatic

Convert from host byte order to little endian, 16 bit.

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

Definition at line 464 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 492 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 499 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 485 of file byteorder.h.

◆ byteorder_lebuftohl()

static uint32_t byteorder_lebuftohl ( const uint8_t *  buf)
inlinestatic

Definition at line 589 of file byteorder.h.

◆ byteorder_lebuftohll()

static uint64_t byteorder_lebuftohll ( const uint8_t *  buf)
inlinestatic

Definition at line 594 of file byteorder.h.

◆ byteorder_lebuftohs()

static uint16_t byteorder_lebuftohs ( const uint8_t *  buf)
inlinestatic

Definition at line 584 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 429 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 436 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 422 of file byteorder.h.

◆ byteorder_ltohl()

static uint32_t byteorder_ltohl ( le_uint32_t  v)
inlinestatic

Convert from little endian to host byte order, 32 bit.

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

Definition at line 412 of file byteorder.h.

◆ byteorder_ltohll()

static uint64_t byteorder_ltohll ( le_uint64_t  v)
inlinestatic

Convert from little endian to host byte order, 64 bit.

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

Definition at line 417 of file byteorder.h.

◆ byteorder_ltohs()

static uint16_t byteorder_ltohs ( le_uint16_t  v)
inlinestatic

Convert from little endian to host byte order, 16 bit.

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

Definition at line 407 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 511 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 516 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 506 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 397 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 402 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 392 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 526 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 531 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 521 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 541 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 546 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 536 of file byteorder.h.