Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
byteorder.h File Reference

Byte order helpers. More...

#include <zephyr/types.h>
#include <stddef.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/toolchain.h>

Go to the source code of this file.

Macros

#define BSWAP_16(x)
 
#define BSWAP_24(x)
 
#define BSWAP_32(x)
 
#define BSWAP_40(x)
 
#define BSWAP_48(x)
 
#define BSWAP_64(x)
 
#define sys_le16_to_cpu(val)
 Convert 16-bit integer from little-endian to host endianness.
 
#define sys_cpu_to_le16(val)
 Convert 16-bit integer from host endianness to little-endian.
 
#define sys_le24_to_cpu(val)
 Convert 24-bit integer from little-endian to host endianness.
 
#define sys_cpu_to_le24(val)
 Convert 24-bit integer from host endianness to little-endian.
 
#define sys_le32_to_cpu(val)
 Convert 32-bit integer from little-endian to host endianness.
 
#define sys_cpu_to_le32(val)
 Convert 32-bit integer from host endianness to little-endian.
 
#define sys_le40_to_cpu(val)
 
#define sys_cpu_to_le40(val)
 
#define sys_le48_to_cpu(val)
 Convert 48-bit integer from little-endian to host endianness.
 
#define sys_cpu_to_le48(val)
 Convert 48-bit integer from host endianness to little-endian.
 
#define sys_le64_to_cpu(val)
 
#define sys_cpu_to_le64(val)
 
#define sys_be16_to_cpu(val)
 Convert 16-bit integer from big-endian to host endianness.
 
#define sys_cpu_to_be16(val)
 Convert 16-bit integer from host endianness to big-endian.
 
#define sys_be24_to_cpu(val)
 Convert 24-bit integer from big-endian to host endianness.
 
#define sys_cpu_to_be24(val)
 Convert 24-bit integer from host endianness to big-endian.
 
#define sys_be32_to_cpu(val)
 Convert 32-bit integer from big-endian to host endianness.
 
#define sys_cpu_to_be32(val)
 Convert 32-bit integer from host endianness to big-endian.
 
#define sys_be40_to_cpu(val)
 
#define sys_cpu_to_be40(val)
 
#define sys_be48_to_cpu(val)
 Convert 48-bit integer from big-endian to host endianness.
 
#define sys_cpu_to_be48(val)
 Convert 48-bit integer from host endianness to big-endian.
 
#define sys_be64_to_cpu(val)
 
#define sys_cpu_to_be64(val)
 
#define sys_uint16_to_array(val)
 Convert 16-bit unsigned integer to byte array.
 
#define sys_uint32_to_array(val)
 Convert 32-bit unsigned integer to byte array.
 
#define sys_uint64_to_array(val)
 Convert 64-bit unsigned integer to byte array.
 

Functions

static void sys_put_be16 (uint16_t val, uint8_t dst[2])
 Put a 16-bit integer as big-endian to arbitrary location.
 
static void sys_put_be24 (uint32_t val, uint8_t dst[3])
 Put a 24-bit integer as big-endian to arbitrary location.
 
static void sys_put_be32 (uint32_t val, uint8_t dst[4])
 Put a 32-bit integer as big-endian to arbitrary location.
 
static void sys_put_be40 (uint64_t val, uint8_t dst[5])
 Put a 40-bit integer as big-endian to arbitrary location.
 
static void sys_put_be48 (uint64_t val, uint8_t dst[6])
 Put a 48-bit integer as big-endian to arbitrary location.
 
static void sys_put_be64 (uint64_t val, uint8_t dst[8])
 Put a 64-bit integer as big-endian to arbitrary location.
 
static void sys_put_le16 (uint16_t val, uint8_t dst[2])
 Put a 16-bit integer as little-endian to arbitrary location.
 
static void sys_put_le24 (uint32_t val, uint8_t dst[3])
 Put a 24-bit integer as little-endian to arbitrary location.
 
static void sys_put_le32 (uint32_t val, uint8_t dst[4])
 Put a 32-bit integer as little-endian to arbitrary location.
 
static void sys_put_le40 (uint64_t val, uint8_t dst[5])
 Put a 40-bit integer as little-endian to arbitrary location.
 
static void sys_put_le48 (uint64_t val, uint8_t dst[6])
 Put a 48-bit integer as little-endian to arbitrary location.
 
static void sys_put_le64 (uint64_t val, uint8_t dst[8])
 Put a 64-bit integer as little-endian to arbitrary location.
 
static uint16_t sys_get_be16 (const uint8_t src[2])
 Get a 16-bit integer stored in big-endian format.
 
static uint32_t sys_get_be24 (const uint8_t src[3])
 Get a 24-bit integer stored in big-endian format.
 
static uint32_t sys_get_be32 (const uint8_t src[4])
 Get a 32-bit integer stored in big-endian format.
 
static uint64_t sys_get_be40 (const uint8_t src[5])
 Get a 40-bit integer stored in big-endian format.
 
static uint64_t sys_get_be48 (const uint8_t src[6])
 Get a 48-bit integer stored in big-endian format.
 
static uint64_t sys_get_be64 (const uint8_t src[8])
 Get a 64-bit integer stored in big-endian format.
 
static uint16_t sys_get_le16 (const uint8_t src[2])
 Get a 16-bit integer stored in little-endian format.
 
static uint32_t sys_get_le24 (const uint8_t src[3])
 Get a 24-bit integer stored in little-endian format.
 
static uint32_t sys_get_le32 (const uint8_t src[4])
 Get a 32-bit integer stored in little-endian format.
 
static uint64_t sys_get_le40 (const uint8_t src[5])
 Get a 40-bit integer stored in little-endian format.
 
static uint64_t sys_get_le48 (const uint8_t src[6])
 Get a 48-bit integer stored in little-endian format.
 
static uint64_t sys_get_le64 (const uint8_t src[8])
 Get a 64-bit integer stored in little-endian format.
 
static void sys_memcpy_swap (void *dst, const void *src, size_t length)
 Swap one buffer content into another.
 
static void sys_mem_swap (void *buf, size_t length)
 Swap buffer content.
 

Detailed Description

Byte order helpers.

Macro Definition Documentation

◆ BSWAP_16

#define BSWAP_16 ( x)
Value:
((uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
__UINT16_TYPE__ uint16_t
Definition stdint.h:89

◆ BSWAP_24

#define BSWAP_24 ( x)
Value:
((uint32_t) ((((x) >> 16) & 0xff) | \
(((x)) & 0xff00) | \
(((x) & 0xff) << 16)))
__UINT32_TYPE__ uint32_t
Definition stdint.h:90

◆ BSWAP_32

#define BSWAP_32 ( x)
Value:
((uint32_t) ((((x) >> 24) & 0xff) | \
(((x) >> 8) & 0xff00) | \
(((x) & 0xff00) << 8) | \
(((x) & 0xff) << 24)))

◆ BSWAP_40

#define BSWAP_40 ( x)
Value:
((uint64_t) ((((x) >> 32) & 0xff) | \
(((x) >> 16) & 0xff00) | \
(((x)) & 0xff0000) | \
(((x) & 0xff00) << 16) | \
(((x) & 0xff) << 32)))
__UINT64_TYPE__ uint64_t
Definition stdint.h:91

◆ BSWAP_48

#define BSWAP_48 ( x)
Value:
((uint64_t) ((((x) >> 40) & 0xff) | \
(((x) >> 24) & 0xff00) | \
(((x) >> 8) & 0xff0000) | \
(((x) & 0xff0000) << 8) | \
(((x) & 0xff00) << 24) | \
(((x) & 0xff) << 40)))

◆ BSWAP_64

#define BSWAP_64 ( x)
Value:
((uint64_t) ((((x) >> 56) & 0xff) | \
(((x) >> 40) & 0xff00) | \
(((x) >> 24) & 0xff0000) | \
(((x) >> 8) & 0xff000000) | \
(((x) & 0xff000000) << 8) | \
(((x) & 0xff0000) << 24) | \
(((x) & 0xff00) << 40) | \
(((x) & 0xff) << 56)))

◆ sys_be16_to_cpu

#define sys_be16_to_cpu ( val)
Value:
(val)

Convert 16-bit integer from big-endian to host endianness.

Parameters
val16-bit integer in big-endian format.
Returns
16-bit integer in host endianness.

◆ sys_be24_to_cpu

#define sys_be24_to_cpu ( val)
Value:
(val)

Convert 24-bit integer from big-endian to host endianness.

Parameters
val24-bit integer in big-endian format.
Returns
24-bit integer in host endianness.

◆ sys_be32_to_cpu

#define sys_be32_to_cpu ( val)
Value:
(val)

Convert 32-bit integer from big-endian to host endianness.

Parameters
val32-bit integer in big-endian format.
Returns
32-bit integer in host endianness.

◆ sys_be40_to_cpu

#define sys_be40_to_cpu ( val)
Value:
(val)

◆ sys_be48_to_cpu

#define sys_be48_to_cpu ( val)
Value:
(val)

Convert 48-bit integer from big-endian to host endianness.

Parameters
val48-bit integer in big-endian format.
Returns
48-bit integer in host endianness.

◆ sys_be64_to_cpu

#define sys_be64_to_cpu ( val)
Value:
(val)

◆ sys_cpu_to_be16

#define sys_cpu_to_be16 ( val)
Value:
(val)

Convert 16-bit integer from host endianness to big-endian.

Parameters
val16-bit integer in host endianness.
Returns
16-bit integer in big-endian format.

◆ sys_cpu_to_be24

#define sys_cpu_to_be24 ( val)
Value:
(val)

Convert 24-bit integer from host endianness to big-endian.

Parameters
val24-bit integer in host endianness.
Returns
24-bit integer in big-endian format.

◆ sys_cpu_to_be32

#define sys_cpu_to_be32 ( val)
Value:
(val)

Convert 32-bit integer from host endianness to big-endian.

Parameters
val32-bit integer in host endianness.
Returns
32-bit integer in big-endian format.

◆ sys_cpu_to_be40

#define sys_cpu_to_be40 ( val)
Value:
(val)

◆ sys_cpu_to_be48

#define sys_cpu_to_be48 ( val)
Value:
(val)

Convert 48-bit integer from host endianness to big-endian.

Parameters
val48-bit integer in host endianness.
Returns
48-bit integer in big-endian format.

◆ sys_cpu_to_be64

#define sys_cpu_to_be64 ( val)
Value:
(val)

◆ sys_cpu_to_le16

#define sys_cpu_to_le16 ( val)
Value:
#define BSWAP_16(x)
Definition byteorder.h:19

Convert 16-bit integer from host endianness to little-endian.

Parameters
val16-bit integer in host endianness.
Returns
16-bit integer in little-endian format.

◆ sys_cpu_to_le24

#define sys_cpu_to_le24 ( val)
Value:
#define BSWAP_24(x)
Definition byteorder.h:20

Convert 24-bit integer from host endianness to little-endian.

Parameters
val24-bit integer in host endianness.
Returns
24-bit integer in little-endian format.

◆ sys_cpu_to_le32

#define sys_cpu_to_le32 ( val)
Value:
#define BSWAP_32(x)
Definition byteorder.h:23

Convert 32-bit integer from host endianness to little-endian.

Parameters
val32-bit integer in host endianness.
Returns
32-bit integer in little-endian format.

◆ sys_cpu_to_le40

#define sys_cpu_to_le40 ( val)
Value:
#define BSWAP_40(x)
Definition byteorder.h:27

◆ sys_cpu_to_le48

#define sys_cpu_to_le48 ( val)
Value:
#define BSWAP_48(x)
Definition byteorder.h:32

Convert 48-bit integer from host endianness to little-endian.

Parameters
val48-bit integer in host endianness.
Returns
48-bit integer in little-endian format.

◆ sys_cpu_to_le64

#define sys_cpu_to_le64 ( val)
Value:
#define BSWAP_64(x)
Definition byteorder.h:38

◆ sys_le16_to_cpu

#define sys_le16_to_cpu ( val)
Value:

Convert 16-bit integer from little-endian to host endianness.

Parameters
val16-bit integer in little-endian format.
Returns
16-bit integer in host endianness.

◆ sys_le24_to_cpu

#define sys_le24_to_cpu ( val)
Value:

Convert 24-bit integer from little-endian to host endianness.

Parameters
val24-bit integer in little-endian format.
Returns
24-bit integer in host endianness.

◆ sys_le32_to_cpu

#define sys_le32_to_cpu ( val)
Value:

Convert 32-bit integer from little-endian to host endianness.

Parameters
val32-bit integer in little-endian format.
Returns
32-bit integer in host endianness.

◆ sys_le40_to_cpu

#define sys_le40_to_cpu ( val)
Value:

◆ sys_le48_to_cpu

#define sys_le48_to_cpu ( val)
Value:

Convert 48-bit integer from little-endian to host endianness.

Parameters
val48-bit integer in little-endian format.
Returns
48-bit integer in host endianness.

◆ sys_le64_to_cpu

#define sys_le64_to_cpu ( val)
Value:

◆ sys_uint16_to_array

#define sys_uint16_to_array ( val)
Value:
{ \
(((val) >> 8) & 0xff), \
((val) & 0xff)}

Convert 16-bit unsigned integer to byte array.

Byte order aware macro to treat an unsigned integer as an array, rather than an integer literal. For example, 0x0123 would be converted to {0x01, 0x23} for big endian machines, and {0x23, 0x01} for little endian machines.

Parameters
val16-bit unsigned integer.
Returns
16-bit unsigned integer as byte array.

◆ sys_uint32_to_array

#define sys_uint32_to_array ( val)
Value:
{ \
(((val) >> 24) & 0xff), \
(((val) >> 16) & 0xff), \
(((val) >> 8) & 0xff), \
((val) & 0xff)}

Convert 32-bit unsigned integer to byte array.

Byte order aware macro to treat an unsigned integer as an array, rather than an integer literal. For example, 0x01234567 would be converted to {0x01, 0x23, 0x45, 0x67} for big endian machines, and {0x67, 0x45, 0x23, 0x01} for little endian machines.

Parameters
val32-bit unsigned integer.
Returns
32-bit unsigned integer as byte array.

◆ sys_uint64_to_array

#define sys_uint64_to_array ( val)
Value:
{ \
(((val) >> 56) & 0xff), \
(((val) >> 48) & 0xff), \
(((val) >> 40) & 0xff), \
(((val) >> 32) & 0xff), \
(((val) >> 24) & 0xff), \
(((val) >> 16) & 0xff), \
(((val) >> 8) & 0xff), \
((val) & 0xff)}

Convert 64-bit unsigned integer to byte array.

Byte order aware macro to treat an unsigned integer as an array, rather than an integer literal. For example, 0x0123456789abcdef would be converted to {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef} for big endian machines, and {0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01} for little endian machines.

Parameters
val64-bit unsigned integer.
Returns
64-bit unsigned integer as byte array.

Function Documentation

◆ sys_get_be16()

static uint16_t sys_get_be16 ( const uint8_t src[2])
inlinestatic

Get a 16-bit integer stored in big-endian format.

Get a 16-bit integer, stored in big-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the big-endian 16-bit integer to get.
Returns
16-bit integer in host endianness.

◆ sys_get_be24()

static uint32_t sys_get_be24 ( const uint8_t src[3])
inlinestatic

Get a 24-bit integer stored in big-endian format.

Get a 24-bit integer, stored in big-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the big-endian 24-bit integer to get.
Returns
24-bit integer in host endianness.

◆ sys_get_be32()

static uint32_t sys_get_be32 ( const uint8_t src[4])
inlinestatic

Get a 32-bit integer stored in big-endian format.

Get a 32-bit integer, stored in big-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the big-endian 32-bit integer to get.
Returns
32-bit integer in host endianness.

◆ sys_get_be40()

static uint64_t sys_get_be40 ( const uint8_t src[5])
inlinestatic

Get a 40-bit integer stored in big-endian format.

Get a 40-bit integer, stored in big-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the big-endian 40-bit integer to get.
Returns
40-bit integer in host endianness.

◆ sys_get_be48()

static uint64_t sys_get_be48 ( const uint8_t src[6])
inlinestatic

Get a 48-bit integer stored in big-endian format.

Get a 48-bit integer, stored in big-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the big-endian 48-bit integer to get.
Returns
48-bit integer in host endianness.

◆ sys_get_be64()

static uint64_t sys_get_be64 ( const uint8_t src[8])
inlinestatic

Get a 64-bit integer stored in big-endian format.

Get a 64-bit integer, stored in big-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the big-endian 64-bit integer to get.
Returns
64-bit integer in host endianness.

◆ sys_get_le16()

static uint16_t sys_get_le16 ( const uint8_t src[2])
inlinestatic

Get a 16-bit integer stored in little-endian format.

Get a 16-bit integer, stored in little-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the little-endian 16-bit integer to get.
Returns
16-bit integer in host endianness.

◆ sys_get_le24()

static uint32_t sys_get_le24 ( const uint8_t src[3])
inlinestatic

Get a 24-bit integer stored in little-endian format.

Get a 24-bit integer, stored in little-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the little-endian 24-bit integer to get.
Returns
24-bit integer in host endianness.

◆ sys_get_le32()

static uint32_t sys_get_le32 ( const uint8_t src[4])
inlinestatic

Get a 32-bit integer stored in little-endian format.

Get a 32-bit integer, stored in little-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the little-endian 32-bit integer to get.
Returns
32-bit integer in host endianness.

◆ sys_get_le40()

static uint64_t sys_get_le40 ( const uint8_t src[5])
inlinestatic

Get a 40-bit integer stored in little-endian format.

Get a 40-bit integer, stored in little-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the little-endian 40-bit integer to get.
Returns
40-bit integer in host endianness.

◆ sys_get_le48()

static uint64_t sys_get_le48 ( const uint8_t src[6])
inlinestatic

Get a 48-bit integer stored in little-endian format.

Get a 48-bit integer, stored in little-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the little-endian 48-bit integer to get.
Returns
48-bit integer in host endianness.

◆ sys_get_le64()

static uint64_t sys_get_le64 ( const uint8_t src[8])
inlinestatic

Get a 64-bit integer stored in little-endian format.

Get a 64-bit integer, stored in little-endian format in a potentially unaligned memory location, and convert it to the host endianness.

Parameters
srcLocation of the little-endian 64-bit integer to get.
Returns
64-bit integer in host endianness.

◆ sys_mem_swap()

static void sys_mem_swap ( void * buf,
size_t length )
inlinestatic

Swap buffer content.

In-place memory swap, where final content will be reversed. I.e.: buf[n] will be put in buf[end-n] Where n is an index and 'end' the last index of buf.

Parameters
bufA valid pointer on a memory area to swap
lengthSize of buf memory area

◆ sys_memcpy_swap()

static void sys_memcpy_swap ( void * dst,
const void * src,
size_t length )
inlinestatic

Swap one buffer content into another.

Copy the content of src buffer into dst buffer in reversed order, i.e.: src[n] will be put in dst[end-n] Where n is an index and 'end' the last index in both arrays. The 2 memory pointers must be pointing to different areas, and have a minimum size of given length.

Parameters
dstA valid pointer on a memory area where to copy the data in
srcA valid pointer on a memory area where to copy the data from
lengthSize of both dst and src memory areas

◆ sys_put_be16()

static void sys_put_be16 ( uint16_t val,
uint8_t dst[2] )
inlinestatic

Put a 16-bit integer as big-endian to arbitrary location.

Put a 16-bit integer, originally in host endianness, to a potentially unaligned memory location in big-endian format.

Parameters
val16-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_be24()

static void sys_put_be24 ( uint32_t val,
uint8_t dst[3] )
inlinestatic

Put a 24-bit integer as big-endian to arbitrary location.

Put a 24-bit integer, originally in host endianness, to a potentially unaligned memory location in big-endian format.

Parameters
val24-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_be32()

static void sys_put_be32 ( uint32_t val,
uint8_t dst[4] )
inlinestatic

Put a 32-bit integer as big-endian to arbitrary location.

Put a 32-bit integer, originally in host endianness, to a potentially unaligned memory location in big-endian format.

Parameters
val32-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_be40()

static void sys_put_be40 ( uint64_t val,
uint8_t dst[5] )
inlinestatic

Put a 40-bit integer as big-endian to arbitrary location.

Put a 40-bit integer, originally in host endianness, to a potentially unaligned memory location in big-endian format.

Parameters
val40-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_be48()

static void sys_put_be48 ( uint64_t val,
uint8_t dst[6] )
inlinestatic

Put a 48-bit integer as big-endian to arbitrary location.

Put a 48-bit integer, originally in host endianness, to a potentially unaligned memory location in big-endian format.

Parameters
val48-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_be64()

static void sys_put_be64 ( uint64_t val,
uint8_t dst[8] )
inlinestatic

Put a 64-bit integer as big-endian to arbitrary location.

Put a 64-bit integer, originally in host endianness, to a potentially unaligned memory location in big-endian format.

Parameters
val64-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_le16()

static void sys_put_le16 ( uint16_t val,
uint8_t dst[2] )
inlinestatic

Put a 16-bit integer as little-endian to arbitrary location.

Put a 16-bit integer, originally in host endianness, to a potentially unaligned memory location in little-endian format.

Parameters
val16-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_le24()

static void sys_put_le24 ( uint32_t val,
uint8_t dst[3] )
inlinestatic

Put a 24-bit integer as little-endian to arbitrary location.

Put a 24-bit integer, originally in host endianness, to a potentially unaligned memory location in little-endian format.

Parameters
val24-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_le32()

static void sys_put_le32 ( uint32_t val,
uint8_t dst[4] )
inlinestatic

Put a 32-bit integer as little-endian to arbitrary location.

Put a 32-bit integer, originally in host endianness, to a potentially unaligned memory location in little-endian format.

Parameters
val32-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_le40()

static void sys_put_le40 ( uint64_t val,
uint8_t dst[5] )
inlinestatic

Put a 40-bit integer as little-endian to arbitrary location.

Put a 40-bit integer, originally in host endianness, to a potentially unaligned memory location in little-endian format.

Parameters
val40-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_le48()

static void sys_put_le48 ( uint64_t val,
uint8_t dst[6] )
inlinestatic

Put a 48-bit integer as little-endian to arbitrary location.

Put a 48-bit integer, originally in host endianness, to a potentially unaligned memory location in little-endian format.

Parameters
val48-bit integer in host endianness.
dstDestination memory address to store the result.

◆ sys_put_le64()

static void sys_put_le64 ( uint64_t val,
uint8_t dst[8] )
inlinestatic

Put a 64-bit integer as little-endian to arbitrary location.

Put a 64-bit integer, originally in host endianness, to a potentially unaligned memory location in little-endian format.

Parameters
val64-bit integer in host endianness.
dstDestination memory address to store the result.