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_lebuftohs(const uint8_t *buf);
239 
250 static inline void byteorder_htolebufs(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  return result;
337 }
338 
340 {
341  be_uint32_t result = { byteorder_swapl(v.u32) };
342  return result;
343 }
344 
346 {
347  be_uint64_t result = { byteorder_swapll(v.u64) };
348  return result;
349 }
350 
352 {
353  le_uint16_t result = { byteorder_swaps(v.u16) };
354  return result;
355 }
356 
358 {
359  le_uint32_t result = { byteorder_swapl(v.u32) };
360  return result;
361 }
362 
364 {
365  le_uint64_t result = { byteorder_swapll(v.u64) };
366  return result;
367 }
368 
372 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
373 # define _byteorder_swap(V, T) (byteorder_swap##T((V)))
374 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
375 # define _byteorder_swap(V, T) (V)
376 #else
377 # error "Byte order is neither little nor big!"
378 #endif
379 
380 static inline network_uint16_t byteorder_htons(uint16_t v)
381 {
382  network_uint16_t result = { _byteorder_swap(v, s) };
383  return result;
384 }
385 
386 static inline network_uint32_t byteorder_htonl(uint32_t v)
387 {
388  network_uint32_t result = { _byteorder_swap(v, l) };
389  return result;
390 }
391 
392 static inline network_uint64_t byteorder_htonll(uint64_t v)
393 {
394  network_uint64_t result = { _byteorder_swap(v, ll) };
395  return result;
396 }
397 
398 static inline uint16_t byteorder_ntohs(network_uint16_t v)
399 {
400  return _byteorder_swap(v.u16, s);
401 }
402 
403 static inline uint32_t byteorder_ntohl(network_uint32_t v)
404 {
405  return _byteorder_swap(v.u32, l);
406 }
407 
408 static inline uint64_t byteorder_ntohll(network_uint64_t v)
409 {
410  return _byteorder_swap(v.u64, ll);
411 }
412 
413 static inline uint16_t htons(uint16_t v)
414 {
415  return byteorder_htons(v).u16;
416 }
417 
418 static inline uint32_t htonl(uint32_t v)
419 {
420  return byteorder_htonl(v).u32;
421 }
422 
423 static inline uint64_t htonll(uint64_t v)
424 {
425  return byteorder_htonll(v).u64;
426 }
427 
428 static inline uint16_t ntohs(uint16_t v)
429 {
430  network_uint16_t input = { v };
431  return byteorder_ntohs(input);
432 }
433 
434 static inline uint32_t ntohl(uint32_t v)
435 {
436  network_uint32_t input = { v };
437  return byteorder_ntohl(input);
438 }
439 
440 static inline uint64_t ntohll(uint64_t v)
441 {
442  network_uint64_t input = { v };
443  return byteorder_ntohll(input);
444 }
445 
446 static inline uint16_t byteorder_lebuftohs(const uint8_t *buf)
447 {
448 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
449  return (uint16_t)((buf[0] << 8) | buf[1]);
450 #else
451  return (uint16_t)((buf[1] << 8) | buf[0]);
452 #endif
453 }
454 
455 static inline void byteorder_htolebufs(uint8_t *buf, uint16_t val)
456 {
457 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
458  buf[0] = (uint8_t)(val >> 8);
459  buf[1] = (uint8_t)(val & 0xff);
460 #else
461  buf[0] = (uint8_t)(val & 0xff);
462  buf[1] = (uint8_t)(val >> 8);
463 #endif
464 }
465 
466 #ifdef __cplusplus
467 }
468 #endif
469 
470 #endif /* BYTEORDER_H */
471 
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition: byteorder.h:110
static uint64_t ntohll(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:440
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition: byteorder.h:351
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:408
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:86
static uint64_t htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:423
static uint16_t byteorder_lebuftohs(const uint8_t *buf)
Read a little endian encoded unsigned integer from a buffer into host byte order encoded variable...
Definition: byteorder.h:446
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition: byteorder.h:333
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 be_uint32_t byteorder_ltobl(le_uint32_t v)
Convert from little endian to big endian, 32 bit.
Definition: byteorder.h:339
static uint32_t htonl(uint32_t v)
Convert from host byte order to network byte order, 32 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:398
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition: byteorder.h:328
#define _byteorder_swap(V, T)
Swaps the byteorder according to the endianess.
Definition: byteorder.h:373
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition: byteorder.h:310
A 32 bit integer in little endian.
Definition: byteorder.h:50
static le_uint32_t byteorder_btoll(be_uint32_t v)
Convert from big endian to little endian, 32 bit.
Definition: byteorder.h:357
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition: byteorder.h:345
uint64_t u64
64 bit representation
Definition: byteorder.h:63
A 16 bit integer in little endian.
Definition: byteorder.h:40
static void byteorder_htolebufs(uint8_t *buf, uint16_t val)
Write a host byte order encoded unsigned integer as little endian encoded value into a buffer...
Definition: byteorder.h:455
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition: byteorder.h:363
uint16_t u16
16 bit representation
Definition: byteorder.h:77
static uint32_t ntohl(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:434
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:403
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:386
uint16_t u16
16 bit representation
Definition: byteorder.h:41
be_uint32_t network_uint32_t
A 32 bit integer in network byte order.
Definition: byteorder.h:115
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition: byteorder.h:323
uint64_t u64
64 bit representation
Definition: byteorder.h:99
static uint16_t htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:413
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:78
A 64 bit integer in big endian aka network byte order.
Definition: byteorder.h:98
static network_uint16_t byteorder_htons(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:380
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition: byteorder.h:120
static uint16_t ntohs(uint16_t v)
Convert from network byte order to host byte order, 16 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:392