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 
18 #include <string.h>
19 #include <stdint.h>
20 #include <endian.h>
21 #include "unaligned.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* ******************************* INTERFACE ******************************* */
28 
34 typedef union __attribute__((packed)) {
35  uint16_t u16;
36  uint8_t u8[2];
37 } le_uint16_t;
38 
44 typedef union __attribute__((packed)) {
45  uint32_t u32;
46  uint8_t u8[4];
47  uint16_t u16[2];
48  le_uint16_t l16[2];
49 } le_uint32_t;
50 
56 typedef union __attribute__((packed)) {
57  uint64_t u64;
58  uint8_t u8[8];
59  uint16_t u16[4];
60  uint32_t u32[2];
61  le_uint16_t l16[4];
62  le_uint32_t l32[2];
63 } le_uint64_t;
64 
70 typedef union __attribute__((packed)) {
71  uint16_t u16;
72  uint8_t u8[2];
73 } be_uint16_t;
74 
80 typedef union __attribute__((packed)) {
81  uint32_t u32;
82  uint8_t u8[4];
83  uint16_t u16[2];
84  be_uint16_t b16[2];
85 } be_uint32_t;
86 
92 typedef union __attribute__((packed)) {
93  uint64_t u64;
94  uint8_t u8[8];
95  uint16_t u16[4];
96  uint32_t u32[2];
97  be_uint16_t b16[4];
98  be_uint32_t b32[2];
99 } be_uint64_t;
100 
105 
110 
115 
121 static inline uint16_t byteorder_ltohs(le_uint16_t v);
122 
128 static inline uint32_t byteorder_ltohl(le_uint32_t v);
129 
135 static inline uint64_t byteorder_ltohll(le_uint64_t v);
136 
142 static inline be_uint16_t byteorder_ltobs(le_uint16_t v);
143 
149 static inline be_uint32_t byteorder_ltobl(le_uint32_t v);
150 
156 static inline be_uint64_t byteorder_ltobll(le_uint64_t v);
157 
163 static inline le_uint16_t byteorder_btols(be_uint16_t v);
164 
170 static inline le_uint32_t byteorder_btoll(be_uint32_t v);
171 
177 static inline le_uint64_t byteorder_btolll(be_uint64_t v);
178 
184 static inline le_uint16_t byteorder_htols(uint16_t v);
185 
191 static inline le_uint32_t byteorder_htoll(uint32_t v);
192 
198 static inline le_uint64_t byteorder_htolll(uint64_t v);
199 
205 static inline network_uint16_t byteorder_htons(uint16_t v);
206 
212 static inline network_uint32_t byteorder_htonl(uint32_t v);
213 
219 static inline network_uint64_t byteorder_htonll(uint64_t v);
220 
226 static inline uint16_t byteorder_ntohs(network_uint16_t v);
227 
233 static inline uint32_t byteorder_ntohl(network_uint32_t v);
234 
240 static inline uint64_t byteorder_ntohll(network_uint64_t v);
241 
247 static inline uint16_t byteorder_swaps(uint16_t v);
248 
254 static inline uint32_t byteorder_swapl(uint32_t v);
255 
261 static inline uint64_t byteorder_swapll(uint64_t v);
262 
274 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
275 
287 static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
288 
300 static inline uint64_t byteorder_bebuftohll(const uint8_t *buf);
301 
312 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
313 
324 static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
325 
336 static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val);
337 
344 static inline uint16_t htons(uint16_t v);
345 
352 static inline uint32_t htonl(uint32_t v);
353 
360 static inline uint64_t htonll(uint64_t v);
361 
368 static inline uint16_t ntohs(uint16_t v);
369 
376 static inline uint32_t ntohl(uint32_t v);
377 
384 static inline uint64_t ntohll(uint64_t v);
385 
386 /* **************************** IMPLEMENTATION ***************************** */
387 
388 static inline uint16_t byteorder_swaps(uint16_t v)
389 {
390  return __builtin_bswap16(v);
391 }
392 
393 static inline uint32_t byteorder_swapl(uint32_t v)
394 {
395  return __builtin_bswap32(v);
396 }
397 
398 static inline uint64_t byteorder_swapll(uint64_t v)
399 {
400  return __builtin_bswap64(v);
401 }
402 
403 static inline uint16_t byteorder_ltohs(le_uint16_t v)
404 {
405  return le16toh(v.u16);
406 }
407 
408 static inline uint32_t byteorder_ltohl(le_uint32_t v)
409 {
410  return le32toh(v.u32);
411 }
412 
413 static 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 
460 static inline le_uint16_t byteorder_htols(uint16_t v)
461 {
462  le_uint16_t result = { .u16 = htole16(v) };
463 
464  return result;
465 }
466 
467 static inline le_uint32_t byteorder_htoll(uint32_t v)
468 {
469  le_uint32_t result = { .u32 = htole32(v) };
470 
471  return result;
472 }
473 
474 static inline le_uint64_t byteorder_htolll(uint64_t v)
475 {
476  le_uint64_t result = { .u64 = htole64(v) };
477 
478  return result;
479 }
480 
481 static inline network_uint16_t byteorder_htons(uint16_t v)
482 {
483  network_uint16_t result = { .u16 = htobe16(v) };
484 
485  return result;
486 }
487 
488 static inline network_uint32_t byteorder_htonl(uint32_t v)
489 {
490  network_uint32_t result = { .u32 = htobe32(v) };
491 
492  return result;
493 }
494 
495 static inline network_uint64_t byteorder_htonll(uint64_t v)
496 {
497  network_uint64_t result = { .u64 = htobe64(v) };
498 
499  return result;
500 }
501 
502 static inline uint16_t byteorder_ntohs(network_uint16_t v)
503 {
504  return be16toh(v.u16);
505 }
506 
507 static inline uint32_t byteorder_ntohl(network_uint32_t v)
508 {
509  return be32toh(v.u32);
510 }
511 
512 static inline uint64_t byteorder_ntohll(network_uint64_t v)
513 {
514  return be64toh(v.u64);
515 }
516 
517 static inline uint16_t htons(uint16_t v)
518 {
519  return htobe16(v);
520 }
521 
522 static inline uint32_t htonl(uint32_t v)
523 {
524  return htobe32(v);
525 }
526 
527 static inline uint64_t htonll(uint64_t v)
528 {
529  return htobe64(v);
530 }
531 
532 static inline uint16_t ntohs(uint16_t v)
533 {
534  return be16toh(v);
535 }
536 
537 static inline uint32_t ntohl(uint32_t v)
538 {
539  return be32toh(v);
540 }
541 
542 static inline uint64_t ntohll(uint64_t v)
543 {
544  return be64toh(v);
545 }
546 
547 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
548 {
549  return be16toh(unaligned_get_u16(buf));
550 }
551 
552 static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
553 {
554  return be32toh(unaligned_get_u32(buf));
555 }
556 
557 static inline uint64_t byteorder_bebuftohll(const uint8_t *buf)
558 {
559  return be64toh(unaligned_get_u64(buf));
560 }
561 
562 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
563 {
564  val = htobe16(val);
565  memcpy(buf, &val, sizeof(val));
566 }
567 
568 static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
569 {
570  val = htobe32(val);
571  memcpy(buf, &val, sizeof(val));
572 }
573 
574 static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
575 {
576  val = htobe64(val);
577  memcpy(buf, &val, sizeof(val));
578 }
579 
580 static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
581 {
582  return le16toh(unaligned_get_u16(buf));
583 }
584 
585 static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
586 {
587  return le32toh(unaligned_get_u32(buf));
588 }
589 
590 static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
591 {
592  return le64toh(unaligned_get_u64(buf));
593 }
594 
595 static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
596 {
597  val = htole16(val);
598  memcpy(buf, &val, sizeof(val));
599 }
600 
601 static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
602 {
603  val = htole32(val);
604  memcpy(buf, &val, sizeof(val));
605 }
606 
607 static 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
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
uint32_t u32
32 bit representation
Definition: byteorder.h:81
A 64 bit integer in big endian aka network byte order.
Definition: byteorder.h:92
uint64_t u64
64 bit representation
Definition: byteorder.h:93
A 16 bit integer in little endian.
Definition: byteorder.h:34
uint16_t u16
16 bit representation
Definition: byteorder.h:35
A 32 bit integer in little endian.
Definition: byteorder.h:44
uint32_t u32
32 bit representation
Definition: byteorder.h:45
A 64 bit integer in little endian.
Definition: byteorder.h:56
uint64_t u64
64 bit representation
Definition: byteorder.h:57