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];
47 uint16_t u16[2];
50
56typedef union __attribute__((packed)) {
57 uint64_t u64;
58 uint8_t u8[8];
59 uint16_t u16[4];
60 uint32_t u32[2];
64
70typedef union __attribute__((packed)) {
71 uint16_t u16;
72 uint8_t u8[2];
74
80typedef union __attribute__((packed)) {
81 uint32_t u32;
82 uint8_t u8[4];
83 uint16_t u16[2];
86
92typedef union __attribute__((packed)) {
93 uint64_t u64;
94 uint8_t u8[8];
95 uint16_t u16[4];
96 uint32_t u32[2];
100
105
110
115
121static inline uint16_t byteorder_ltohs(le_uint16_t v);
122
128static inline uint32_t byteorder_ltohl(le_uint32_t v);
129
135static inline uint64_t byteorder_ltohll(le_uint64_t v);
136
143
150
157
164
171
178
184static inline le_uint16_t byteorder_htols(uint16_t v);
185
191static inline le_uint32_t byteorder_htoll(uint32_t v);
192
198static inline le_uint64_t byteorder_htolll(uint64_t v);
199
205static inline network_uint16_t byteorder_htons(uint16_t v);
206
212static inline network_uint32_t byteorder_htonl(uint32_t v);
213
219static inline network_uint64_t byteorder_htonll(uint64_t v);
220
226static inline uint16_t byteorder_ntohs(network_uint16_t v);
227
233static inline uint32_t byteorder_ntohl(network_uint32_t v);
234
240static inline uint64_t byteorder_ntohll(network_uint64_t v);
241
247static inline uint16_t byteorder_swaps(uint16_t v);
248
254static inline uint32_t byteorder_swapl(uint32_t v);
255
261static inline uint64_t byteorder_swapll(uint64_t v);
262
274static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
275
287static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
288
300static inline uint64_t byteorder_bebuftohll(const uint8_t *buf);
301
312static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
313
324static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
325
336static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val);
337
344static inline uint16_t htons(uint16_t v);
345
352static inline uint32_t htonl(uint32_t v);
353
360static inline uint64_t htonll(uint64_t v);
361
368static inline uint16_t ntohs(uint16_t v);
369
376static inline uint32_t ntohl(uint32_t v);
377
384static inline uint64_t ntohll(uint64_t v);
385
386/* **************************** IMPLEMENTATION ***************************** */
387
388static inline uint16_t byteorder_swaps(uint16_t v)
389{
390 return __builtin_bswap16(v);
391}
392
393static inline uint32_t byteorder_swapl(uint32_t v)
394{
395 return __builtin_bswap32(v);
396}
397
398static inline uint64_t byteorder_swapll(uint64_t v)
399{
400 return __builtin_bswap64(v);
401}
402
403static inline uint16_t byteorder_ltohs(le_uint16_t v)
404{
405 return le16toh(v.u16);
406}
407
408static inline uint32_t byteorder_ltohl(le_uint32_t v)
409{
410 return le32toh(v.u32);
411}
412
413static inline uint64_t byteorder_ltohll(le_uint64_t v)
414{
415 return le64toh(v.u64);
416}
417
419{
420 be_uint16_t result = { byteorder_swaps(v.u16) };
421
422 return result;
423}
424
426{
427 be_uint32_t result = { byteorder_swapl(v.u32) };
428
429 return result;
430}
431
433{
434 be_uint64_t result = { byteorder_swapll(v.u64) };
435
436 return result;
437}
438
440{
441 le_uint16_t result = { byteorder_swaps(v.u16) };
442
443 return result;
444}
445
447{
448 le_uint32_t result = { byteorder_swapl(v.u32) };
449
450 return result;
451}
452
454{
455 le_uint64_t result = { byteorder_swapll(v.u64) };
456
457 return result;
458}
459
460static inline le_uint16_t byteorder_htols(uint16_t v)
461{
462 le_uint16_t result = { .u16 = htole16(v) };
463
464 return result;
465}
466
467static inline le_uint32_t byteorder_htoll(uint32_t v)
468{
469 le_uint32_t result = { .u32 = htole32(v) };
470
471 return result;
472}
473
474static inline le_uint64_t byteorder_htolll(uint64_t v)
475{
476 le_uint64_t result = { .u64 = htole64(v) };
477
478 return result;
479}
480
481static inline network_uint16_t byteorder_htons(uint16_t v)
482{
483 network_uint16_t result = { .u16 = htobe16(v) };
484
485 return result;
486}
487
488static inline network_uint32_t byteorder_htonl(uint32_t v)
489{
490 network_uint32_t result = { .u32 = htobe32(v) };
491
492 return result;
493}
494
495static inline network_uint64_t byteorder_htonll(uint64_t v)
496{
497 network_uint64_t result = { .u64 = htobe64(v) };
498
499 return result;
500}
501
502static inline uint16_t byteorder_ntohs(network_uint16_t v)
503{
504 return be16toh(v.u16);
505}
506
507static inline uint32_t byteorder_ntohl(network_uint32_t v)
508{
509 return be32toh(v.u32);
510}
511
512static inline uint64_t byteorder_ntohll(network_uint64_t v)
513{
514 return be64toh(v.u64);
515}
516
517static inline uint16_t htons(uint16_t v)
518{
519 return htobe16(v);
520}
521
522static inline uint32_t htonl(uint32_t v)
523{
524 return htobe32(v);
525}
526
527static inline uint64_t htonll(uint64_t v)
528{
529 return htobe64(v);
530}
531
532static inline uint16_t ntohs(uint16_t v)
533{
534 return be16toh(v);
535}
536
537static inline uint32_t ntohl(uint32_t v)
538{
539 return be32toh(v);
540}
541
542static inline uint64_t ntohll(uint64_t v)
543{
544 return be64toh(v);
545}
546
547static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
548{
549 return be16toh(unaligned_get_u16(buf));
550}
551
552static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
553{
554 return be32toh(unaligned_get_u32(buf));
555}
556
557static inline uint64_t byteorder_bebuftohll(const uint8_t *buf)
558{
559 return be64toh(unaligned_get_u64(buf));
560}
561
562static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
563{
564 val = htobe16(val);
565 memcpy(buf, &val, sizeof(val));
566}
567
568static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
569{
570 val = htobe32(val);
571 memcpy(buf, &val, sizeof(val));
572}
573
574static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
575{
576 val = htobe64(val);
577 memcpy(buf, &val, sizeof(val));
578}
579
580static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
581{
582 return le16toh(unaligned_get_u16(buf));
583}
584
585static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
586{
587 return le32toh(unaligned_get_u32(buf));
588}
589
590static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
591{
592 return le64toh(unaligned_get_u64(buf));
593}
594
595static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
596{
597 val = htole16(val);
598 memcpy(buf, &val, sizeof(val));
599}
600
601static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
602{
603 val = htole32(val);
604 memcpy(buf, &val, sizeof(val));
605}
606
607static inline void byteorder_htolebufll(uint8_t *buf, uint64_t val)
608{
609 val = htole64(val);
610 memcpy(buf, &val, sizeof(val));
611}
612
613#ifdef __cplusplus
614}
615#endif
616
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition byteorder.h:109
static uint64_t ntohll(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition byteorder.h:542
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition byteorder.h:507
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:532
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:562
static uint16_t byteorder_ltohs(le_uint16_t v)
Convert from little endian to host byte order, 16 bit.
Definition byteorder.h:403
static uint64_t byteorder_ltohll(le_uint64_t v)
Convert from little endian to host byte order, 64 bit.
Definition byteorder.h:413
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition byteorder.h:453
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition byteorder.h:398
static be_uint32_t byteorder_ltobl(le_uint32_t v)
Convert from little endian to big endian, 32 bit.
Definition byteorder.h:425
static network_uint64_t byteorder_htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition byteorder.h:495
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition byteorder.h:512
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:557
static uint32_t byteorder_ltohl(le_uint32_t v)
Convert from little endian to host byte order, 32 bit.
Definition byteorder.h:408
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition byteorder.h:104
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:574
static network_uint16_t byteorder_htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:481
static uint64_t htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition byteorder.h:527
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition byteorder.h:418
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition byteorder.h:502
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition byteorder.h:522
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:547
static le_uint64_t byteorder_htolll(uint64_t v)
Convert from host byte order to little endian, 64 bit.
Definition byteorder.h:474
static le_uint32_t byteorder_btoll(be_uint32_t v)
Convert from big endian to little endian, 32 bit.
Definition byteorder.h:446
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition byteorder.h:439
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition byteorder.h:388
static le_uint16_t byteorder_htols(uint16_t v)
Convert from host byte order to little endian, 16 bit.
Definition byteorder.h:460
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:568
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition byteorder.h:432
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition byteorder.h:393
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition byteorder.h:114
static le_uint32_t byteorder_htoll(uint32_t v)
Convert from host byte order to little endian, 32 bit.
Definition byteorder.h:467
static uint32_t ntohl(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition byteorder.h:537
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:552
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition byteorder.h:488
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition byteorder.h:517
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:70
uint8_t u8[2]
8 bit representation
Definition byteorder.h:72
uint16_t u16
16 bit representation
Definition byteorder.h:71
A 32 bit integer in big endian aka network byte order.
Definition byteorder.h:80
be_uint16_t b16[2]
big endian 16 bit representation
Definition byteorder.h:84
uint32_t u32
32 bit representation
Definition byteorder.h:81
uint16_t u16[2]
16 bit representation
Definition byteorder.h:83
uint8_t u8[4]
8 bit representation
Definition byteorder.h:82
A 64 bit integer in big endian aka network byte order.
Definition byteorder.h:92
uint8_t u8[8]
8 bit representation
Definition byteorder.h:94
uint32_t u32[2]
32 bit representation
Definition byteorder.h:96
be_uint16_t b16[4]
big endian 16 bit representation
Definition byteorder.h:97
uint16_t u16[4]
16 bit representation
Definition byteorder.h:95
uint64_t u64
64 bit representation
Definition byteorder.h:93
be_uint32_t b32[2]
big endian 32 bit representation
Definition byteorder.h:98
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
le_uint16_t l16[2]
little endian 16 bit representation
Definition byteorder.h:48
uint16_t u16[2]
16 bit representation
Definition byteorder.h:47
A 64 bit integer in little endian.
Definition byteorder.h:56
le_uint16_t l16[4]
little endian 16 bit representation
Definition byteorder.h:61
le_uint32_t l32[2]
little endian 32 bit representation
Definition byteorder.h:62
uint32_t u32[2]
32 bit representation
Definition byteorder.h:60
uint8_t u8[8]
8 bit representation
Definition byteorder.h:58
uint64_t u64
64 bit representation
Definition byteorder.h:57
uint16_t u16[4]
16 bit representation
Definition byteorder.h:59