Loading...
Searching...
No Matches
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
20
21#include <string.h>
22#include <stdint.h>
23#include <endian.h>
24#include "unaligned.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/* ******************************* INTERFACE ******************************* */
31
37typedef union __attribute__((packed)) {
38 uint16_t u16;
39 uint8_t u8[2];
41
47typedef union __attribute__((packed)) {
48 uint32_t u32;
49 uint8_t u8[4];
50 uint16_t u16[2];
53
59typedef union __attribute__((packed)) {
60 uint64_t u64;
61 uint8_t u8[8];
62 uint16_t u16[4];
63 uint32_t u32[2];
67
73typedef union __attribute__((packed)) {
74 uint16_t u16;
75 uint8_t u8[2];
77
83typedef union __attribute__((packed)) {
84 uint32_t u32;
85 uint8_t u8[4];
86 uint16_t u16[2];
89
95typedef union __attribute__((packed)) {
96 uint64_t u64;
97 uint8_t u8[8];
98 uint16_t u16[4];
99 uint32_t u32[2];
103
108
113
118
124static inline uint16_t byteorder_ltohs(le_uint16_t v);
125
131static inline uint32_t byteorder_ltohl(le_uint32_t v);
132
138static inline uint64_t byteorder_ltohll(le_uint64_t v);
139
146
153
160
167
174
181
187static inline le_uint16_t byteorder_htols(uint16_t v);
188
194static inline le_uint32_t byteorder_htoll(uint32_t v);
195
201static inline le_uint64_t byteorder_htolll(uint64_t v);
202
208static inline network_uint16_t byteorder_htons(uint16_t v);
209
215static inline network_uint32_t byteorder_htonl(uint32_t v);
216
222static inline network_uint64_t byteorder_htonll(uint64_t v);
223
229static inline uint16_t byteorder_ntohs(network_uint16_t v);
230
236static inline uint32_t byteorder_ntohl(network_uint32_t v);
237
243static inline uint64_t byteorder_ntohll(network_uint64_t v);
244
250static inline uint16_t byteorder_swaps(uint16_t v);
251
257static inline uint32_t byteorder_swapl(uint32_t v);
258
264static inline uint64_t byteorder_swapll(uint64_t v);
265
277static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
278
290static inline uint32_t byteorder_bebuftohl(const uint8_t *buf);
291
303static inline uint64_t byteorder_bebuftohll(const uint8_t *buf);
304
315static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
316
327static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val);
328
339static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val);
340
347static inline uint16_t htons(uint16_t v);
348
355static inline uint32_t htonl(uint32_t v);
356
363static inline uint64_t htonll(uint64_t v);
364
371static inline uint16_t ntohs(uint16_t v);
372
379static inline uint32_t ntohl(uint32_t v);
380
387static inline uint64_t ntohll(uint64_t v);
388
389/* **************************** IMPLEMENTATION ***************************** */
390
391static inline uint16_t byteorder_swaps(uint16_t v)
392{
393 return __builtin_bswap16(v);
394}
395
396static inline uint32_t byteorder_swapl(uint32_t v)
397{
398 return __builtin_bswap32(v);
399}
400
401static inline uint64_t byteorder_swapll(uint64_t v)
402{
403 return __builtin_bswap64(v);
404}
405
406static inline uint16_t byteorder_ltohs(le_uint16_t v)
407{
408 return le16toh(v.u16);
409}
410
411static inline uint32_t byteorder_ltohl(le_uint32_t v)
412{
413 return le32toh(v.u32);
414}
415
416static 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
463static inline le_uint16_t byteorder_htols(uint16_t v)
464{
465 le_uint16_t result = { .u16 = htole16(v) };
466
467 return result;
468}
469
470static inline le_uint32_t byteorder_htoll(uint32_t v)
471{
472 le_uint32_t result = { .u32 = htole32(v) };
473
474 return result;
475}
476
477static inline le_uint64_t byteorder_htolll(uint64_t v)
478{
479 le_uint64_t result = { .u64 = htole64(v) };
480
481 return result;
482}
483
484static inline network_uint16_t byteorder_htons(uint16_t v)
485{
486 network_uint16_t result = { .u16 = htobe16(v) };
487
488 return result;
489}
490
491static inline network_uint32_t byteorder_htonl(uint32_t v)
492{
493 network_uint32_t result = { .u32 = htobe32(v) };
494
495 return result;
496}
497
498static inline network_uint64_t byteorder_htonll(uint64_t v)
499{
500 network_uint64_t result = { .u64 = htobe64(v) };
501
502 return result;
503}
504
505static inline uint16_t byteorder_ntohs(network_uint16_t v)
506{
507 return be16toh(v.u16);
508}
509
510static inline uint32_t byteorder_ntohl(network_uint32_t v)
511{
512 return be32toh(v.u32);
513}
514
515static inline uint64_t byteorder_ntohll(network_uint64_t v)
516{
517 return be64toh(v.u64);
518}
519
520static inline uint16_t htons(uint16_t v)
521{
522 return htobe16(v);
523}
524
525static inline uint32_t htonl(uint32_t v)
526{
527 return htobe32(v);
528}
529
530static inline uint64_t htonll(uint64_t v)
531{
532 return htobe64(v);
533}
534
535static inline uint16_t ntohs(uint16_t v)
536{
537 return be16toh(v);
538}
539
540static inline uint32_t ntohl(uint32_t v)
541{
542 return be32toh(v);
543}
544
545static inline uint64_t ntohll(uint64_t v)
546{
547 return be64toh(v);
548}
549
550static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
551{
552 return be16toh(unaligned_get_u16(buf));
553}
554
555static inline uint32_t byteorder_bebuftohl(const uint8_t *buf)
556{
557 return be32toh(unaligned_get_u32(buf));
558}
559
560static inline uint64_t byteorder_bebuftohll(const uint8_t *buf)
561{
562 return be64toh(unaligned_get_u64(buf));
563}
564
565static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
566{
567 val = htobe16(val);
568 memcpy(buf, &val, sizeof(val));
569}
570
571static inline void byteorder_htobebufl(uint8_t *buf, uint32_t val)
572{
573 val = htobe32(val);
574 memcpy(buf, &val, sizeof(val));
575}
576
577static inline void byteorder_htobebufll(uint8_t *buf, uint64_t val)
578{
579 val = htobe64(val);
580 memcpy(buf, &val, sizeof(val));
581}
582
583static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
584{
585 return le16toh(unaligned_get_u16(buf));
586}
587
588static inline uint32_t byteorder_lebuftohl(const uint8_t *buf)
589{
590 return le32toh(unaligned_get_u32(buf));
591}
592
593static inline uint64_t byteorder_lebuftohll(const uint8_t *buf)
594{
595 return le64toh(unaligned_get_u64(buf));
596}
597
598static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
599{
600 val = htole16(val);
601 memcpy(buf, &val, sizeof(val));
602}
603
604static inline void byteorder_htolebufl(uint8_t *buf, uint32_t val)
605{
606 val = htole32(val);
607 memcpy(buf, &val, sizeof(val));
608}
609
610static 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
uint8_t u8[2]
8 bit representation
Definition byteorder.h:75
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
be_uint16_t b16[2]
big endian 16 bit representation
Definition byteorder.h:87
uint32_t u32
32 bit representation
Definition byteorder.h:84
uint16_t u16[2]
16 bit representation
Definition byteorder.h:86
uint8_t u8[4]
8 bit representation
Definition byteorder.h:85
A 64 bit integer in big endian aka network byte order.
Definition byteorder.h:95
uint8_t u8[8]
8 bit representation
Definition byteorder.h:97
uint32_t u32[2]
32 bit representation
Definition byteorder.h:99
be_uint16_t b16[4]
big endian 16 bit representation
Definition byteorder.h:100
uint16_t u16[4]
16 bit representation
Definition byteorder.h:98
uint64_t u64
64 bit representation
Definition byteorder.h:96
be_uint32_t b32[2]
big endian 32 bit representation
Definition byteorder.h:101
A 16 bit integer in little endian.
Definition byteorder.h:37
uint16_t u16
16 bit representation
Definition byteorder.h:38
uint8_t u8[2]
8 bit representation
Definition byteorder.h:39
A 32 bit integer in little endian.
Definition byteorder.h:47
uint32_t u32
32 bit representation
Definition byteorder.h:48
uint8_t u8[4]
8 bit representation
Definition byteorder.h:49
le_uint16_t l16[2]
little endian 16 bit representation
Definition byteorder.h:51
uint16_t u16[2]
16 bit representation
Definition byteorder.h:50
A 64 bit integer in little endian.
Definition byteorder.h:59
le_uint16_t l16[4]
little endian 16 bit representation
Definition byteorder.h:64
le_uint32_t l32[2]
little endian 32 bit representation
Definition byteorder.h:65
uint32_t u32[2]
32 bit representation
Definition byteorder.h:63
uint8_t u8[8]
8 bit representation
Definition byteorder.h:61
uint64_t u64
64 bit representation
Definition byteorder.h:60
uint16_t u16[4]
16 bit representation
Definition byteorder.h:62