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 <stdint.h>
23 
24 #if defined(__MACH__)
25 # include "clang_compat.h"
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /* ******************************* INTERFACE ******************************* */
33 
34 
40 typedef union __attribute__((packed)) {
41  uint16_t u16;
42  uint8_t u8[2];
43 } le_uint16_t;
44 
50 typedef union __attribute__((packed)) {
51  uint32_t u32;
52  uint8_t u8[4];
53  uint16_t u16[2];
54  le_uint16_t l16[2];
55 } le_uint32_t;
56 
62 typedef union __attribute__((packed)) {
63  uint64_t u64;
64  uint8_t u8[8];
65  uint16_t u16[4];
66  uint32_t u32[2];
67  le_uint16_t l16[4];
68  le_uint32_t l32[2];
69 } le_uint64_t;
70 
76 typedef union __attribute__((packed)) {
77  uint16_t u16;
78  uint8_t u8[2];
79 } be_uint16_t;
80 
86 typedef union __attribute__((packed)) {
87  uint32_t u32;
88  uint8_t u8[4];
89  uint16_t u16[2];
90  be_uint16_t b16[2];
91 } be_uint32_t;
92 
98 typedef union __attribute__((packed)) {
99  uint64_t u64;
100  uint8_t u8[8];
101  uint16_t u16[4];
102  uint32_t u32[2];
103  be_uint16_t b16[4];
104  be_uint32_t b32[2];
105 } be_uint64_t;
106 
111 
116 
121 
127 static inline be_uint16_t byteorder_ltobs(le_uint16_t v);
128 
134 static inline be_uint32_t byteorder_ltobl(le_uint32_t v);
135 
141 static inline be_uint64_t byteorder_ltobll(le_uint64_t v);
142 
148 static inline le_uint16_t byteorder_btols(be_uint16_t v);
149 
155 static inline le_uint32_t byteorder_btoll(be_uint32_t v);
156 
162 static inline le_uint64_t byteorder_btolll(be_uint64_t v);
163 
169 static inline network_uint16_t byteorder_htons(uint16_t v);
170 
176 static inline network_uint32_t byteorder_htonl(uint32_t v);
177 
183 static inline network_uint64_t byteorder_htonll(uint64_t v);
184 
190 static inline uint16_t byteorder_ntohs(network_uint16_t v);
191 
197 static inline uint32_t byteorder_ntohl(network_uint32_t v);
198 
204 static inline uint64_t byteorder_ntohll(network_uint64_t v);
205 
211 static inline uint16_t byteorder_swaps(uint16_t v);
212 
218 static inline uint32_t byteorder_swapl(uint32_t v);
219 
225 static inline uint64_t byteorder_swapll(uint64_t v);
226 
238 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf);
239 
250 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val);
251 
258 static inline uint16_t htons(uint16_t v);
259 
266 static inline uint32_t htonl(uint32_t v);
267 
274 static inline uint64_t htonll(uint64_t v);
275 
282 static inline uint16_t ntohs(uint16_t v);
283 
290 static inline uint32_t ntohl(uint32_t v);
291 
298 static inline uint64_t ntohll(uint64_t v);
299 
300 
301 /* **************************** IMPLEMENTATION ***************************** */
302 
303 #ifdef HAVE_NO_BUILTIN_BSWAP16
304 static inline unsigned short __builtin_bswap16(unsigned short a)
305 {
306  return (a << 8) | (a >> 8);
307 }
308 #endif
309 
310 static inline uint16_t byteorder_swaps(uint16_t v)
311 {
312 #ifndef MODULE_MSP430_COMMON
313  return __builtin_bswap16(v);
314 #else
315  network_uint16_t result = { v };
316  uint8_t tmp = result.u8[0];
317  result.u8[0] = result.u8[1];
318  result.u8[1] = tmp;
319  return result.u16;
320 #endif
321 }
322 
323 static inline uint32_t byteorder_swapl(uint32_t v)
324 {
325  return __builtin_bswap32(v);
326 }
327 
328 static inline uint64_t byteorder_swapll(uint64_t v)
329 {
330  return __builtin_bswap64(v);
331 }
332 
334 {
335  be_uint16_t result = { byteorder_swaps(v.u16) };
336 
337  return result;
338 }
339 
341 {
342  be_uint32_t result = { byteorder_swapl(v.u32) };
343 
344  return result;
345 }
346 
348 {
349  be_uint64_t result = { byteorder_swapll(v.u64) };
350 
351  return result;
352 }
353 
355 {
356  le_uint16_t result = { byteorder_swaps(v.u16) };
357 
358  return result;
359 }
360 
362 {
363  le_uint32_t result = { byteorder_swapl(v.u32) };
364 
365  return result;
366 }
367 
369 {
370  le_uint64_t result = { byteorder_swapll(v.u64) };
371 
372  return result;
373 }
374 
378 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
379 # define _byteorder_swap(V, T) (byteorder_swap ## T((V)))
380 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
381 # define _byteorder_swap(V, T) (V)
382 #else
383 # error "Byte order is neither little nor big!"
384 #endif
385 
386 static inline network_uint16_t byteorder_htons(uint16_t v)
387 {
388  network_uint16_t result = { _byteorder_swap(v, s) };
389 
390  return result;
391 }
392 
393 static inline network_uint32_t byteorder_htonl(uint32_t v)
394 {
395  network_uint32_t result = { _byteorder_swap(v, l) };
396 
397  return result;
398 }
399 
400 static inline network_uint64_t byteorder_htonll(uint64_t v)
401 {
402  network_uint64_t result = { _byteorder_swap(v, ll) };
403 
404  return result;
405 }
406 
407 static inline uint16_t byteorder_ntohs(network_uint16_t v)
408 {
409  return _byteorder_swap(v.u16, s);
410 }
411 
412 static inline uint32_t byteorder_ntohl(network_uint32_t v)
413 {
414  return _byteorder_swap(v.u32, l);
415 }
416 
417 static inline uint64_t byteorder_ntohll(network_uint64_t v)
418 {
419  return _byteorder_swap(v.u64, ll);
420 }
421 
422 static inline uint16_t htons(uint16_t v)
423 {
424  return byteorder_htons(v).u16;
425 }
426 
427 static inline uint32_t htonl(uint32_t v)
428 {
429  return byteorder_htonl(v).u32;
430 }
431 
432 static inline uint64_t htonll(uint64_t v)
433 {
434  return byteorder_htonll(v).u64;
435 }
436 
437 static inline uint16_t ntohs(uint16_t v)
438 {
439  network_uint16_t input = { v };
440 
441  return byteorder_ntohs(input);
442 }
443 
444 static inline uint32_t ntohl(uint32_t v)
445 {
446  network_uint32_t input = { v };
447 
448  return byteorder_ntohl(input);
449 }
450 
451 static inline uint64_t ntohll(uint64_t v)
452 {
453  network_uint64_t input = { v };
454 
455  return byteorder_ntohll(input);
456 }
457 
458 static inline uint16_t byteorder_bebuftohs(const uint8_t *buf)
459 {
460  return (uint16_t)((buf[0] << 8) | (buf[1] << 0));
461 }
462 
463 static inline void byteorder_htobebufs(uint8_t *buf, uint16_t val)
464 {
465  buf[0] = (uint8_t)(val >> 8);
466  buf[1] = (uint8_t)(val >> 0);
467 }
468 
469 #ifdef __cplusplus
470 }
471 #endif
472 
473 #endif /* BYTEORDER_H */
474 
#define _byteorder_swap(V, T)
Swaps the byteorder according to the endianness.
Definition: byteorder.h:379
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition: byteorder.h:323
static uint32_t ntohl(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:444
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition: byteorder.h:354
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:393
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:86
A 16 bit integer in big endian aka network byte order.
Definition: byteorder.h:76
uint32_t u32
32 bit representation
Definition: byteorder.h:51
uint32_t u32
32 bit representation
Definition: byteorder.h:87
A 64 bit integer in little endian.
Definition: byteorder.h:62
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:427
static le_uint32_t byteorder_btoll(be_uint32_t v)
Convert from big endian to little endian, 32 bit.
Definition: byteorder.h:361
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:422
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:458
A 32 bit integer in little endian.
Definition: byteorder.h:50
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition: byteorder.h:368
uint64_t u64
64 bit representation
Definition: byteorder.h:63
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:412
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition: byteorder.h:120
A 16 bit integer in little endian.
Definition: byteorder.h:40
uint16_t u16
16 bit representation
Definition: byteorder.h:77
static network_uint16_t byteorder_htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:386
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition: byteorder.h:115
static uint64_t htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:432
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition: byteorder.h:110
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:437
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition: byteorder.h:328
uint16_t u16
16 bit representation
Definition: byteorder.h:41
uint64_t u64
64 bit representation
Definition: byteorder.h:99
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:78
static network_uint64_t byteorder_htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:400
static uint64_t ntohll(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:451
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:407
A 64 bit integer in big endian aka network byte order.
Definition: byteorder.h:98
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition: byteorder.h:333
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition: byteorder.h:310
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:417
static be_uint32_t byteorder_ltobl(le_uint32_t v)
Convert from little endian to big endian, 32 bit.
Definition: byteorder.h:340
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition: byteorder.h:347
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:463