byteorder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 RenĂ© Kijewski
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
21 #include <string.h>
22 #include <stdint.h>
23 #include <endian.h>
24 #include "unaligned.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /* ******************************* INTERFACE ******************************* */
31 
37 typedef union __attribute__((packed)) {
38  uint16_t u16;
39  uint8_t u8[2];
40 } le_uint16_t;
41 
47 typedef union __attribute__((packed)) {
48  uint32_t u32;
49  uint8_t u8[4];
50  uint16_t u16[2];
51  le_uint16_t l16[2];
52 } le_uint32_t;
53 
59 typedef union __attribute__((packed)) {
60  uint64_t u64;
61  uint8_t u8[8];
62  uint16_t u16[4];
63  uint32_t u32[2];
64  le_uint16_t l16[4];
65  le_uint32_t l32[2];
66 } le_uint64_t;
67 
73 typedef union __attribute__((packed)) {
74  uint16_t u16;
75  uint8_t u8[2];
76 } be_uint16_t;
77 
83 typedef union __attribute__((packed)) {
84  uint32_t u32;
85  uint8_t u8[4];
86  uint16_t u16[2];
87  be_uint16_t b16[2];
88 } be_uint32_t;
89 
95 typedef union __attribute__((packed)) {
96  uint64_t u64;
97  uint8_t u8[8];
98  uint16_t u16[4];
99  uint32_t u32[2];
100  be_uint16_t b16[4];
101  be_uint32_t b32[2];
102 } be_uint64_t;
103 
108 
113 
118 
124 static inline uint16_t byteorder_ltohs(le_uint16_t v);
125 
131 static inline uint32_t byteorder_ltohl(le_uint32_t v);
132 
138 static inline uint64_t byteorder_ltohll(le_uint64_t v);
139 
145 static inline be_uint16_t byteorder_ltobs(le_uint16_t v);
146 
152 static inline be_uint32_t byteorder_ltobl(le_uint32_t v);
153 
159 static inline be_uint64_t byteorder_ltobll(le_uint64_t v);
160 
166 static inline le_uint16_t byteorder_btols(be_uint16_t v);
167 
173 static inline le_uint32_t byteorder_btoll(be_uint32_t v);
174 
180 static inline le_uint64_t byteorder_btolll(be_uint64_t v);
181 
187 static inline le_uint16_t byteorder_htols(uint16_t v);
188 
194 static inline le_uint32_t byteorder_htoll(uint32_t v);
195 
201 static inline le_uint64_t byteorder_htolll(uint64_t v);
202 
208 static inline network_uint16_t byteorder_htons(uint16_t v);
209 
215 static inline network_uint32_t byteorder_htonl(uint32_t v);
216 
222 static inline network_uint64_t byteorder_htonll(uint64_t v);
223 
229 static inline uint16_t byteorder_ntohs(network_uint16_t v);
230 
236 static inline uint32_t byteorder_ntohl(network_uint32_t v);
237 
243 static inline uint64_t byteorder_ntohll(network_uint64_t v);
244 
250 static inline uint16_t byteorder_swaps(uint16_t v);
251 
257 static inline uint32_t byteorder_swapl(uint32_t v);
258 
264 static inline uint64_t byteorder_swapll(uint64_t v);
265 
277 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
278 
290 static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
291 
303 static inline uint64_t byteorder_bebuftohll(const uint8_t *buf);
304 
315 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
316 
327 static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
328 
339 static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val);
340 
347 static inline uint16_t htons(uint16_t v);
348 
355 static inline uint32_t htonl(uint32_t v);
356 
363 static inline uint64_t htonll(uint64_t v);
364 
371 static inline uint16_t ntohs(uint16_t v);
372 
379 static inline uint32_t ntohl(uint32_t v);
380 
387 static inline uint64_t ntohll(uint64_t v);
388 
389 /* **************************** IMPLEMENTATION ***************************** */
390 
391 static inline uint16_t byteorder_swaps(uint16_t v)
392 {
393  return __builtin_bswap16(v);
394 }
395 
396 static inline uint32_t byteorder_swapl(uint32_t v)
397 {
398  return __builtin_bswap32(v);
399 }
400 
401 static inline uint64_t byteorder_swapll(uint64_t v)
402 {
403  return __builtin_bswap64(v);
404 }
405 
406 static inline uint16_t byteorder_ltohs(le_uint16_t v)
407 {
408  return le16toh(v.u16);
409 }
410 
411 static inline uint32_t byteorder_ltohl(le_uint32_t v)
412 {
413  return le32toh(v.u32);
414 }
415 
416 static inline uint64_t byteorder_ltohll(le_uint64_t v)
417 {
418  return le64toh(v.u64);
419 }
420 
422 {
423  be_uint16_t result = { byteorder_swaps(v.u16) };
424 
425  return result;
426 }
427 
429 {
430  be_uint32_t result = { byteorder_swapl(v.u32) };
431 
432  return result;
433 }
434 
436 {
437  be_uint64_t result = { byteorder_swapll(v.u64) };
438 
439  return result;
440 }
441 
443 {
444  le_uint16_t result = { byteorder_swaps(v.u16) };
445 
446  return result;
447 }
448 
450 {
451  le_uint32_t result = { byteorder_swapl(v.u32) };
452 
453  return result;
454 }
455 
457 {
458  le_uint64_t result = { byteorder_swapll(v.u64) };
459 
460  return result;
461 }
462 
463 static inline le_uint16_t byteorder_htols(uint16_t v)
464 {
465  le_uint16_t result = { .u16 = htole16(v) };
466 
467  return result;
468 }
469 
470 static inline le_uint32_t byteorder_htoll(uint32_t v)
471 {
472  le_uint32_t result = { .u32 = htole32(v) };
473 
474  return result;
475 }
476 
477 static inline le_uint64_t byteorder_htolll(uint64_t v)
478 {
479  le_uint64_t result = { .u64 = htole64(v) };
480 
481  return result;
482 }
483 
484 static inline network_uint16_t byteorder_htons(uint16_t v)
485 {
486  network_uint16_t result = { .u16 = htobe16(v) };
487 
488  return result;
489 }
490 
491 static inline network_uint32_t byteorder_htonl(uint32_t v)
492 {
493  network_uint32_t result = { .u32 = htobe32(v) };
494 
495  return result;
496 }
497 
498 static inline network_uint64_t byteorder_htonll(uint64_t v)
499 {
500  network_uint64_t result = { .u64 = htobe64(v) };
501 
502  return result;
503 }
504 
505 static inline uint16_t byteorder_ntohs(network_uint16_t v)
506 {
507  return be16toh(v.u16);
508 }
509 
510 static inline uint32_t byteorder_ntohl(network_uint32_t v)
511 {
512  return be32toh(v.u32);
513 }
514 
515 static inline uint64_t byteorder_ntohll(network_uint64_t v)
516 {
517  return be64toh(v.u64);
518 }
519 
520 static inline uint16_t htons(uint16_t v)
521 {
522  return htobe16(v);
523 }
524 
525 static inline uint32_t htonl(uint32_t v)
526 {
527  return htobe32(v);
528 }
529 
530 static inline uint64_t htonll(uint64_t v)
531 {
532  return htobe64(v);
533 }
534 
535 static inline uint16_t ntohs(uint16_t v)
536 {
537  return be16toh(v);
538 }
539 
540 static inline uint32_t ntohl(uint32_t v)
541 {
542  return be32toh(v);
543 }
544 
545 static inline uint64_t ntohll(uint64_t v)
546 {
547  return be64toh(v);
548 }
549 
550 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
551 {
552  return be16toh(unaligned_get_u16(buf));
553 }
554 
555 static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
556 {
557  return be32toh(unaligned_get_u32(buf));
558 }
559 
560 static inline uint64_t byteorder_bebuftohll(const uint8_t *buf)
561 {
562  return be64toh(unaligned_get_u64(buf));
563 }
564 
565 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
566 {
567  val = htobe16(val);
568  memcpy(buf, &val, sizeof(val));
569 }
570 
571 static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
572 {
573  val = htobe32(val);
574  memcpy(buf, &val, sizeof(val));
575 }
576 
577 static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
578 {
579  val = htobe64(val);
580  memcpy(buf, &val, sizeof(val));
581 }
582 
583 static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
584 {
585  return le16toh(unaligned_get_u16(buf));
586 }
587 
588 static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
589 {
590  return le32toh(unaligned_get_u32(buf));
591 }
592 
593 static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
594 {
595  return le64toh(unaligned_get_u64(buf));
596 }
597 
598 static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
599 {
600  val = htole16(val);
601  memcpy(buf, &val, sizeof(val));
602 }
603 
604 static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
605 {
606  val = htole32(val);
607  memcpy(buf, &val, sizeof(val));
608 }
609 
610 static inline void byteorder_htolebufll(uint8_t *buf, uint64_t val)
611 {
612  val = htole64(val);
613  memcpy(buf, &val, sizeof(val));
614 }
615 
616 #ifdef __cplusplus
617 }
618 #endif
619 
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition: byteorder.h:112
static uint64_t ntohll(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:545
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:510
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:535
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:565
static uint16_t byteorder_ltohs(le_uint16_t v)
Convert from little endian to host byte order, 16 bit.
Definition: byteorder.h:406
static uint64_t byteorder_ltohll(le_uint64_t v)
Convert from little endian to host byte order, 64 bit.
Definition: byteorder.h:416
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition: byteorder.h:456
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition: byteorder.h:401
static be_uint32_t byteorder_ltobl(le_uint32_t v)
Convert from little endian to big endian, 32 bit.
Definition: byteorder.h:428
static network_uint64_t byteorder_htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:498
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:515
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:560
static uint32_t byteorder_ltohl(le_uint32_t v)
Convert from little endian to host byte order, 32 bit.
Definition: byteorder.h:411
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition: byteorder.h:107
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:577
static network_uint16_t byteorder_htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:484
static uint64_t htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:530
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition: byteorder.h:421
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:505
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:525
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:550
static le_uint64_t byteorder_htolll(uint64_t v)
Convert from host byte order to little endian, 64 bit.
Definition: byteorder.h:477
static le_uint32_t byteorder_btoll(be_uint32_t v)
Convert from big endian to little endian, 32 bit.
Definition: byteorder.h:449
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition: byteorder.h:442
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition: byteorder.h:391
static le_uint16_t byteorder_htols(uint16_t v)
Convert from host byte order to little endian, 16 bit.
Definition: byteorder.h:463
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:571
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition: byteorder.h:435
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition: byteorder.h:396
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition: byteorder.h:117
static le_uint32_t byteorder_htoll(uint32_t v)
Convert from host byte order to little endian, 32 bit.
Definition: byteorder.h:470
static uint32_t ntohl(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:540
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:555
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:491
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:520
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:67
static uint64_t unaligned_get_u64(const void *ptr)
Get uint64_t from possibly unaligned pointer.
Definition: unaligned.h:93
static uint32_t unaligned_get_u32(const void *ptr)
Get uint32_t from possibly unaligned pointer.
Definition: unaligned.h:80
Unaligned but safe memory access functions.
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:73
uint16_t u16
16 bit representation
Definition: byteorder.h:74
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:83
uint32_t u32
32 bit representation
Definition: byteorder.h:84
A 64 bit integer in big endian aka network byte order.
Definition: byteorder.h:95
uint64_t u64
64 bit representation
Definition: byteorder.h:96
A 16 bit integer in little endian.
Definition: byteorder.h:37
uint16_t u16
16 bit representation
Definition: byteorder.h:38
A 32 bit integer in little endian.
Definition: byteorder.h:47
uint32_t u32
32 bit representation
Definition: byteorder.h:48
A 64 bit integer in little endian.
Definition: byteorder.h:59
uint64_t u64
64 bit representation
Definition: byteorder.h:60