Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2014 René Kijewski
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
17
18#include <string.h>
19#include <stdint.h>
20#include <endian.h>
21#include "unaligned.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/* ******************************* INTERFACE ******************************* */
28
34typedef union __attribute__((packed)) {
35 uint16_t u16;
36 uint8_t u8[2];
38
44typedef union __attribute__((packed)) {
45 uint32_t u32;
46 uint8_t u8[4];
48
54typedef union __attribute__((packed)) {
55 uint64_t u64;
56 uint8_t u8[8];
58
64typedef union __attribute__((packed)) {
65 uint16_t u16;
66 uint8_t u8[2];
68
74typedef union __attribute__((packed)) {
75 uint32_t u32;
76 uint8_t u8[4];
78
84typedef union __attribute__((packed)) {
85 uint64_t u64;
86 uint8_t u8[8];
88
93
98
103
109static inline uint16_t byteorder_ltohs(le_uint16_t v);
110
116static inline uint32_t byteorder_ltohl(le_uint32_t v);
117
123static inline uint64_t byteorder_ltohll(le_uint64_t v);
124
131
138
145
152
159
166
172static inline le_uint16_t byteorder_htols(uint16_t v);
173
179static inline le_uint32_t byteorder_htoll(uint32_t v);
180
186static inline le_uint64_t byteorder_htolll(uint64_t v);
187
193static inline network_uint16_t byteorder_htons(uint16_t v);
194
200static inline network_uint32_t byteorder_htonl(uint32_t v);
201
207static inline network_uint64_t byteorder_htonll(uint64_t v);
208
214static inline uint16_t byteorder_ntohs(network_uint16_t v);
215
221static inline uint32_t byteorder_ntohl(network_uint32_t v);
222
228static inline uint64_t byteorder_ntohll(network_uint64_t v);
229
235static inline uint16_t byteorder_swaps(uint16_t v);
236
242static inline uint32_t byteorder_swapl(uint32_t v);
243
249static inline uint64_t byteorder_swapll(uint64_t v);
250
262static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
263
275static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
276
288static inline uint64_t byteorder_bebuftohll(const uint8_t *buf);
289
300static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
301
312static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
313
324static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val);
325
332static inline uint16_t htons(uint16_t v);
333
340static inline uint32_t htonl(uint32_t v);
341
348static inline uint64_t htonll(uint64_t v);
349
356static inline uint16_t ntohs(uint16_t v);
357
364static inline uint32_t ntohl(uint32_t v);
365
372static inline uint64_t ntohll(uint64_t v);
373
374/* **************************** IMPLEMENTATION ***************************** */
375
376static inline uint16_t byteorder_swaps(uint16_t v)
377{
378 return __builtin_bswap16(v);
379}
380
381static inline uint32_t byteorder_swapl(uint32_t v)
382{
383 return __builtin_bswap32(v);
384}
385
386static inline uint64_t byteorder_swapll(uint64_t v)
387{
388 return __builtin_bswap64(v);
389}
390
391static inline uint16_t byteorder_ltohs(le_uint16_t v)
392{
393 return le16toh(v.u16);
394}
395
396static inline uint32_t byteorder_ltohl(le_uint32_t v)
397{
398 return le32toh(v.u32);
399}
400
401static inline uint64_t byteorder_ltohll(le_uint64_t v)
402{
403 return le64toh(v.u64);
404}
405
407{
408 be_uint16_t result = { byteorder_swaps(v.u16) };
409
410 return result;
411}
412
414{
415 be_uint32_t result = { byteorder_swapl(v.u32) };
416
417 return result;
418}
419
421{
422 be_uint64_t result = { byteorder_swapll(v.u64) };
423
424 return result;
425}
426
428{
429 le_uint16_t result = { byteorder_swaps(v.u16) };
430
431 return result;
432}
433
435{
436 le_uint32_t result = { byteorder_swapl(v.u32) };
437
438 return result;
439}
440
442{
443 le_uint64_t result = { byteorder_swapll(v.u64) };
444
445 return result;
446}
447
448static inline le_uint16_t byteorder_htols(uint16_t v)
449{
450 le_uint16_t result = { .u16 = htole16(v) };
451
452 return result;
453}
454
455static inline le_uint32_t byteorder_htoll(uint32_t v)
456{
457 le_uint32_t result = { .u32 = htole32(v) };
458
459 return result;
460}
461
462static inline le_uint64_t byteorder_htolll(uint64_t v)
463{
464 le_uint64_t result = { .u64 = htole64(v) };
465
466 return result;
467}
468
469static inline network_uint16_t byteorder_htons(uint16_t v)
470{
471 network_uint16_t result = { .u16 = htobe16(v) };
472
473 return result;
474}
475
476static inline network_uint32_t byteorder_htonl(uint32_t v)
477{
478 network_uint32_t result = { .u32 = htobe32(v) };
479
480 return result;
481}
482
483static inline network_uint64_t byteorder_htonll(uint64_t v)
484{
485 network_uint64_t result = { .u64 = htobe64(v) };
486
487 return result;
488}
489
490static inline uint16_t byteorder_ntohs(network_uint16_t v)
491{
492 return be16toh(v.u16);
493}
494
495static inline uint32_t byteorder_ntohl(network_uint32_t v)
496{
497 return be32toh(v.u32);
498}
499
500static inline uint64_t byteorder_ntohll(network_uint64_t v)
501{
502 return be64toh(v.u64);
503}
504
505static inline uint16_t htons(uint16_t v)
506{
507 return htobe16(v);
508}
509
510static inline uint32_t htonl(uint32_t v)
511{
512 return htobe32(v);
513}
514
515static inline uint64_t htonll(uint64_t v)
516{
517 return htobe64(v);
518}
519
520static inline uint16_t ntohs(uint16_t v)
521{
522 return be16toh(v);
523}
524
525static inline uint32_t ntohl(uint32_t v)
526{
527 return be32toh(v);
528}
529
530static inline uint64_t ntohll(uint64_t v)
531{
532 return be64toh(v);
533}
534
535static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
536{
537 return be16toh(unaligned_get_u16(buf));
538}
539
540static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
541{
542 return be32toh(unaligned_get_u32(buf));
543}
544
545static inline uint64_t byteorder_bebuftohll(const uint8_t *buf)
546{
547 return be64toh(unaligned_get_u64(buf));
548}
549
550static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
551{
552 val = htobe16(val);
553 memcpy(buf, &val, sizeof(val));
554}
555
556static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
557{
558 val = htobe32(val);
559 memcpy(buf, &val, sizeof(val));
560}
561
562static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
563{
564 val = htobe64(val);
565 memcpy(buf, &val, sizeof(val));
566}
567
568static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
569{
570 return le16toh(unaligned_get_u16(buf));
571}
572
573static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
574{
575 return le32toh(unaligned_get_u32(buf));
576}
577
578static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
579{
580 return le64toh(unaligned_get_u64(buf));
581}
582
583static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
584{
585 val = htole16(val);
586 memcpy(buf, &val, sizeof(val));
587}
588
589static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
590{
591 val = htole32(val);
592 memcpy(buf, &val, sizeof(val));
593}
594
595static inline void byteorder_htolebufll(uint8_t *buf, uint64_t val)
596{
597 val = htole64(val);
598 memcpy(buf, &val, sizeof(val));
599}
600
601#ifdef __cplusplus
602}
603#endif
604
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition byteorder.h:97
static uint64_t ntohll(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition byteorder.h:530
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition byteorder.h:495
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:520
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,...
Definition byteorder.h:550
static uint16_t byteorder_ltohs(le_uint16_t v)
Convert from little endian to host byte order, 16 bit.
Definition byteorder.h:391
static uint64_t byteorder_ltohll(le_uint64_t v)
Convert from little endian to host byte order, 64 bit.
Definition byteorder.h:401
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition byteorder.h:441
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition byteorder.h:386
static be_uint32_t byteorder_ltobl(le_uint32_t v)
Convert from little endian to big endian, 32 bit.
Definition byteorder.h:413
static network_uint64_t byteorder_htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition byteorder.h:483
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition byteorder.h:500
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,...
Definition byteorder.h:545
static uint32_t byteorder_ltohl(le_uint32_t v)
Convert from little endian to host byte order, 32 bit.
Definition byteorder.h:396
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition byteorder.h:92
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,...
Definition byteorder.h:562
static network_uint16_t byteorder_htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:469
static uint64_t htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition byteorder.h:515
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition byteorder.h:406
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:490
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition byteorder.h:510
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,...
Definition byteorder.h:535
static le_uint64_t byteorder_htolll(uint64_t v)
Convert from host byte order to little endian, 64 bit.
Definition byteorder.h:462
static le_uint32_t byteorder_btoll(be_uint32_t v)
Convert from big endian to little endian, 32 bit.
Definition byteorder.h:434
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition byteorder.h:427
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition byteorder.h:376
static le_uint16_t byteorder_htols(uint16_t v)
Convert from host byte order to little endian, 16 bit.
Definition byteorder.h:448
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,...
Definition byteorder.h:556
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition byteorder.h:420
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition byteorder.h:381
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition byteorder.h:102
static le_uint32_t byteorder_htoll(uint32_t v)
Convert from host byte order to little endian, 32 bit.
Definition byteorder.h:455
static uint32_t ntohl(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition byteorder.h:525
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,...
Definition byteorder.h:540
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition byteorder.h:476
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:505
libc header for endian conversion
uint16_t be16toh(uint16_t big_endian_16bits)
big endian to host, 16 bit
uint16_t htobe16(uint16_t host_16bits)
host to big endian, 16 bit
uint32_t le32toh(uint32_t little_endian_32bits)
little endian to host, 32 bit
uint32_t htobe32(uint32_t host_32bits)
host to big endian, 32 bit
uint16_t le16toh(uint16_t little_endian_16bits)
little endian to host, 16 bit
uint64_t htobe64(uint64_t host_64bits)
host to big endian, 64 bit
uint64_t be64toh(uint64_t big_endian_64bits)
big endian to host, 64 bit
uint32_t be32toh(uint32_t big_endian_32bits)
big endian to host, 32 bit
uint64_t htole64(uint64_t host_64bits)
host to little endian, 64 bit
uint32_t htole32(uint32_t host_32bits)
host to little endian, 32 bit
uint16_t htole16(uint16_t host_16bits)
host to little endian, 16 bit
uint64_t le64toh(uint64_t little_endian_64bits)
little endian to host, 64 bit
static uint16_t unaligned_get_u16(const void *ptr)
Get uint16_t from possibly unaligned pointer.
Definition unaligned.h:64
static uint64_t unaligned_get_u64(const void *ptr)
Get uint64_t from possibly unaligned pointer.
Definition unaligned.h:90
static uint32_t unaligned_get_u32(const void *ptr)
Get uint32_t from possibly unaligned pointer.
Definition unaligned.h:77
Unaligned but safe memory access functions.
A 16 bit integer in big endian aka network byte order.
Definition byteorder.h:64
uint8_t u8[2]
8 bit representation
Definition byteorder.h:66
uint16_t u16
16 bit representation
Definition byteorder.h:65
A 32 bit integer in big endian aka network byte order.
Definition byteorder.h:74
uint32_t u32
32 bit representation
Definition byteorder.h:75
uint8_t u8[4]
8 bit representation
Definition byteorder.h:76
A 64 bit integer in big endian aka network byte order.
Definition byteorder.h:84
uint8_t u8[8]
8 bit representation
Definition byteorder.h:86
uint64_t u64
64 bit representation
Definition byteorder.h:85
A 16 bit integer in little endian.
Definition byteorder.h:34
uint16_t u16
16 bit representation
Definition byteorder.h:35
uint8_t u8[2]
8 bit representation
Definition byteorder.h:36
A 32 bit integer in little endian.
Definition byteorder.h:44
uint32_t u32
32 bit representation
Definition byteorder.h:45
uint8_t u8[4]
8 bit representation
Definition byteorder.h:46
A 64 bit integer in little endian.
Definition byteorder.h:54
uint8_t u8[8]
8 bit representation
Definition byteorder.h:56
uint64_t u64
64 bit representation
Definition byteorder.h:55