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 
19 #ifndef BYTEORDER_H
20 #define BYTEORDER_H
21 
22 #include <string.h>
23 #include <stdint.h>
24 #include <endian.h>
25 #include "unaligned.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* ******************************* INTERFACE ******************************* */
32 
38 typedef union __attribute__((packed)) {
39  uint16_t u16;
40  uint8_t u8[2];
41 } le_uint16_t;
42 
48 typedef union __attribute__((packed)) {
49  uint32_t u32;
50  uint8_t u8[4];
51  uint16_t u16[2];
52  le_uint16_t l16[2];
53 } le_uint32_t;
54 
60 typedef union __attribute__((packed)) {
61  uint64_t u64;
62  uint8_t u8[8];
63  uint16_t u16[4];
64  uint32_t u32[2];
65  le_uint16_t l16[4];
66  le_uint32_t l32[2];
67 } le_uint64_t;
68 
74 typedef union __attribute__((packed)) {
75  uint16_t u16;
76  uint8_t u8[2];
77 } be_uint16_t;
78 
84 typedef union __attribute__((packed)) {
85  uint32_t u32;
86  uint8_t u8[4];
87  uint16_t u16[2];
88  be_uint16_t b16[2];
89 } be_uint32_t;
90 
96 typedef union __attribute__((packed)) {
97  uint64_t u64;
98  uint8_t u8[8];
99  uint16_t u16[4];
100  uint32_t u32[2];
101  be_uint16_t b16[4];
102  be_uint32_t b32[2];
103 } be_uint64_t;
104 
109 
114 
119 
125 static inline uint16_t byteorder_ltohs(le_uint16_t v);
126 
132 static inline uint32_t byteorder_ltohl(le_uint32_t v);
133 
139 static inline uint64_t byteorder_ltohll(le_uint64_t v);
140 
146 static inline be_uint16_t byteorder_ltobs(le_uint16_t v);
147 
153 static inline be_uint32_t byteorder_ltobl(le_uint32_t v);
154 
160 static inline be_uint64_t byteorder_ltobll(le_uint64_t v);
161 
167 static inline le_uint16_t byteorder_btols(be_uint16_t v);
168 
174 static inline le_uint32_t byteorder_btoll(be_uint32_t v);
175 
181 static inline le_uint64_t byteorder_btolll(be_uint64_t v);
182 
188 static inline le_uint16_t byteorder_htols(uint16_t v);
189 
195 static inline le_uint32_t byteorder_htoll(uint32_t v);
196 
202 static inline le_uint64_t byteorder_htolll(uint64_t v);
203 
209 static inline network_uint16_t byteorder_htons(uint16_t v);
210 
216 static inline network_uint32_t byteorder_htonl(uint32_t v);
217 
223 static inline network_uint64_t byteorder_htonll(uint64_t v);
224 
230 static inline uint16_t byteorder_ntohs(network_uint16_t v);
231 
237 static inline uint32_t byteorder_ntohl(network_uint32_t v);
238 
244 static inline uint64_t byteorder_ntohll(network_uint64_t v);
245 
251 static inline uint16_t byteorder_swaps(uint16_t v);
252 
258 static inline uint32_t byteorder_swapl(uint32_t v);
259 
265 static inline uint64_t byteorder_swapll(uint64_t v);
266 
278 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
279 
291 static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
292 
304 static inline uint64_t byteorder_bebuftohll(const uint8_t *buf);
305 
316 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
317 
328 static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
329 
340 static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val);
341 
348 static inline uint16_t htons(uint16_t v);
349 
356 static inline uint32_t htonl(uint32_t v);
357 
364 static inline uint64_t htonll(uint64_t v);
365 
372 static inline uint16_t ntohs(uint16_t v);
373 
380 static inline uint32_t ntohl(uint32_t v);
381 
388 static inline uint64_t ntohll(uint64_t v);
389 
390 /* **************************** IMPLEMENTATION ***************************** */
391 
392 static inline uint16_t byteorder_swaps(uint16_t v)
393 {
394  return __builtin_bswap16(v);
395 }
396 
397 static inline uint32_t byteorder_swapl(uint32_t v)
398 {
399  return __builtin_bswap32(v);
400 }
401 
402 static inline uint64_t byteorder_swapll(uint64_t v)
403 {
404  return __builtin_bswap64(v);
405 }
406 
407 static inline uint16_t byteorder_ltohs(le_uint16_t v)
408 {
409  return le16toh(v.u16);
410 }
411 
412 static inline uint32_t byteorder_ltohl(le_uint32_t v)
413 {
414  return le32toh(v.u32);
415 }
416 
417 static inline uint64_t byteorder_ltohll(le_uint64_t v)
418 {
419  return le64toh(v.u64);
420 }
421 
423 {
424  be_uint16_t result = { byteorder_swaps(v.u16) };
425 
426  return result;
427 }
428 
430 {
431  be_uint32_t result = { byteorder_swapl(v.u32) };
432 
433  return result;
434 }
435 
437 {
438  be_uint64_t result = { byteorder_swapll(v.u64) };
439 
440  return result;
441 }
442 
444 {
445  le_uint16_t result = { byteorder_swaps(v.u16) };
446 
447  return result;
448 }
449 
451 {
452  le_uint32_t result = { byteorder_swapl(v.u32) };
453 
454  return result;
455 }
456 
458 {
459  le_uint64_t result = { byteorder_swapll(v.u64) };
460 
461  return result;
462 }
463 
464 static inline le_uint16_t byteorder_htols(uint16_t v)
465 {
466  le_uint16_t result = { .u16 = htole16(v) };
467 
468  return result;
469 }
470 
471 static inline le_uint32_t byteorder_htoll(uint32_t v)
472 {
473  le_uint32_t result = { .u32 = htole32(v) };
474 
475  return result;
476 }
477 
478 static inline le_uint64_t byteorder_htolll(uint64_t v)
479 {
480  le_uint64_t result = { .u64 = htole64(v) };
481 
482  return result;
483 }
484 
485 static inline network_uint16_t byteorder_htons(uint16_t v)
486 {
487  network_uint16_t result = { .u16 = htobe16(v) };
488 
489  return result;
490 }
491 
492 static inline network_uint32_t byteorder_htonl(uint32_t v)
493 {
494  network_uint32_t result = { .u32 = htobe32(v) };
495 
496  return result;
497 }
498 
499 static inline network_uint64_t byteorder_htonll(uint64_t v)
500 {
501  network_uint64_t result = { .u64 = htobe64(v) };
502 
503  return result;
504 }
505 
506 static inline uint16_t byteorder_ntohs(network_uint16_t v)
507 {
508  return be16toh(v.u16);
509 }
510 
511 static inline uint32_t byteorder_ntohl(network_uint32_t v)
512 {
513  return be32toh(v.u32);
514 }
515 
516 static inline uint64_t byteorder_ntohll(network_uint64_t v)
517 {
518  return be64toh(v.u64);
519 }
520 
521 static inline uint16_t htons(uint16_t v)
522 {
523  return htobe16(v);
524 }
525 
526 static inline uint32_t htonl(uint32_t v)
527 {
528  return htobe32(v);
529 }
530 
531 static inline uint64_t htonll(uint64_t v)
532 {
533  return htobe64(v);
534 }
535 
536 static inline uint16_t ntohs(uint16_t v)
537 {
538  return be16toh(v);
539 }
540 
541 static inline uint32_t ntohl(uint32_t v)
542 {
543  return be32toh(v);
544 }
545 
546 static inline uint64_t ntohll(uint64_t v)
547 {
548  return be64toh(v);
549 }
550 
551 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
552 {
553  return be16toh(unaligned_get_u16(buf));
554 }
555 
556 static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
557 {
558  return be32toh(unaligned_get_u32(buf));
559 }
560 
561 static inline uint64_t byteorder_bebuftohll(const uint8_t *buf)
562 {
563  return be64toh(unaligned_get_u64(buf));
564 }
565 
566 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
567 {
568  val = htobe16(val);
569  memcpy(buf, &val, sizeof(val));
570 }
571 
572 static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
573 {
574  val = htobe32(val);
575  memcpy(buf, &val, sizeof(val));
576 }
577 
578 static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
579 {
580  val = htobe64(val);
581  memcpy(buf, &val, sizeof(val));
582 }
583 
584 static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
585 {
586  return le16toh(unaligned_get_u16(buf));
587 }
588 
589 static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
590 {
591  return le32toh(unaligned_get_u32(buf));
592 }
593 
594 static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
595 {
596  return le64toh(unaligned_get_u64(buf));
597 }
598 
599 static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
600 {
601  val = htole16(val);
602  memcpy(buf, &val, sizeof(val));
603 }
604 
605 static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
606 {
607  val = htole32(val);
608  memcpy(buf, &val, sizeof(val));
609 }
610 
611 static inline void byteorder_htolebufll(uint8_t *buf, uint64_t val)
612 {
613  val = htole64(val);
614  memcpy(buf, &val, sizeof(val));
615 }
616 
617 #ifdef __cplusplus
618 }
619 #endif
620 
621 #endif /* BYTEORDER_H */
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition: byteorder.h:113
static uint64_t ntohll(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:546
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:511
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:536
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:566
static uint16_t byteorder_ltohs(le_uint16_t v)
Convert from little endian to host byte order, 16 bit.
Definition: byteorder.h:407
static uint64_t byteorder_ltohll(le_uint64_t v)
Convert from little endian to host byte order, 64 bit.
Definition: byteorder.h:417
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition: byteorder.h:457
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition: byteorder.h:402
static be_uint32_t byteorder_ltobl(le_uint32_t v)
Convert from little endian to big endian, 32 bit.
Definition: byteorder.h:429
static network_uint64_t byteorder_htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:499
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:516
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:561
static uint32_t byteorder_ltohl(le_uint32_t v)
Convert from little endian to host byte order, 32 bit.
Definition: byteorder.h:412
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition: byteorder.h:108
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:578
static network_uint16_t byteorder_htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:485
static uint64_t htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:531
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition: byteorder.h:422
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:506
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:526
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:551
static le_uint64_t byteorder_htolll(uint64_t v)
Convert from host byte order to little endian, 64 bit.
Definition: byteorder.h:478
static le_uint32_t byteorder_btoll(be_uint32_t v)
Convert from big endian to little endian, 32 bit.
Definition: byteorder.h:450
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition: byteorder.h:443
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition: byteorder.h:392
static le_uint16_t byteorder_htols(uint16_t v)
Convert from host byte order to little endian, 16 bit.
Definition: byteorder.h:464
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:572
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition: byteorder.h:436
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition: byteorder.h:397
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition: byteorder.h:118
static le_uint32_t byteorder_htoll(uint32_t v)
Convert from host byte order to little endian, 32 bit.
Definition: byteorder.h:471
static uint32_t ntohl(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:541
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:556
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:492
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:521
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:68
static uint64_t unaligned_get_u64(const void *ptr)
Get uint64_t from possibly unaligned pointer.
Definition: unaligned.h:94
static uint32_t unaligned_get_u32(const void *ptr)
Get uint32_t from possibly unaligned pointer.
Definition: unaligned.h:81
Unaligned but safe memory access functions.
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:74
uint16_t u16
16 bit representation
Definition: byteorder.h:75
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:84
uint32_t u32
32 bit representation
Definition: byteorder.h:85
A 64 bit integer in big endian aka network byte order.
Definition: byteorder.h:96
uint64_t u64
64 bit representation
Definition: byteorder.h:97
A 16 bit integer in little endian.
Definition: byteorder.h:38
uint16_t u16
16 bit representation
Definition: byteorder.h:39
A 32 bit integer in little endian.
Definition: byteorder.h:48
uint32_t u32
32 bit representation
Definition: byteorder.h:49
A 64 bit integer in little endian.
Definition: byteorder.h:60
uint64_t u64
64 bit representation
Definition: byteorder.h:61