The friendly Operating System for the Internet of Things
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 
233 static inline uint16_t HTONS(uint16_t v);
234 
241 static inline uint32_t HTONL(uint32_t v);
242 
249 static inline uint64_t HTONLL(uint64_t v);
250 
257 static inline uint16_t NTOHS(uint16_t v);
258 
265 static inline uint32_t NTOHL(uint32_t v);
266 
273 static inline uint64_t NTOHLL(uint64_t v);
274 
275 
276 /* **************************** IMPLEMENTATION ***************************** */
277 
278 #ifdef HAVE_NO_BUILTIN_BSWAP16
279 static inline unsigned short __builtin_bswap16(unsigned short a)
280 {
281  return (a<<8)|(a>>8);
282 }
283 #endif
284 
285 static inline uint16_t byteorder_swaps(uint16_t v)
286 {
287 #ifndef MODULE_MSP430_COMMON
288  return __builtin_bswap16(v);
289 #else
290  network_uint16_t result = { v };
291  uint8_t tmp = result.u8[0];
292  result.u8[0] = result.u8[1];
293  result.u8[1] = tmp;
294  return result.u16;
295 #endif
296 }
297 
298 static inline uint32_t byteorder_swapl(uint32_t v)
299 {
300  return __builtin_bswap32(v);
301 }
302 
303 static inline uint64_t byteorder_swapll(uint64_t v)
304 {
305  return __builtin_bswap64(v);
306 }
307 
309 {
310  be_uint16_t result = { byteorder_swaps(v.u16) };
311  return result;
312 }
313 
315 {
316  be_uint32_t result = { byteorder_swapl(v.u32) };
317  return result;
318 }
319 
321 {
322  be_uint64_t result = { byteorder_swapll(v.u64) };
323  return result;
324 }
325 
327 {
328  le_uint16_t result = { byteorder_swaps(v.u16) };
329  return result;
330 }
331 
333 {
334  le_uint32_t result = { byteorder_swapl(v.u32) };
335  return result;
336 }
337 
339 {
340  le_uint64_t result = { byteorder_swapll(v.u64) };
341  return result;
342 }
343 
347 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
348 # define _byteorder_swap(V, T) (byteorder_swap##T((V)))
349 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
350 # define _byteorder_swap(V, T) (V)
351 #else
352 # error "Byte order is neither little nor big!"
353 #endif
354 
355 static inline network_uint16_t byteorder_htons(uint16_t v)
356 {
357  network_uint16_t result = { _byteorder_swap(v, s) };
358  return result;
359 }
360 
361 static inline network_uint32_t byteorder_htonl(uint32_t v)
362 {
363  network_uint32_t result = { _byteorder_swap(v, l) };
364  return result;
365 }
366 
367 static inline network_uint64_t byteorder_htonll(uint64_t v)
368 {
369  network_uint64_t result = { _byteorder_swap(v, ll) };
370  return result;
371 }
372 
373 static inline uint16_t byteorder_ntohs(network_uint16_t v)
374 {
375  return _byteorder_swap(v.u16, s);
376 }
377 
378 static inline uint32_t byteorder_ntohl(network_uint32_t v)
379 {
380  return _byteorder_swap(v.u32, l);
381 }
382 
383 static inline uint64_t byteorder_ntohll(network_uint64_t v)
384 {
385  return _byteorder_swap(v.u64, ll);
386 }
387 
388 static inline uint16_t HTONS(uint16_t v)
389 {
390  return byteorder_htons(v).u16;
391 }
392 
393 static inline uint32_t HTONL(uint32_t v)
394 {
395  return byteorder_htonl(v).u32;
396 }
397 
398 static inline uint64_t HTONLL(uint64_t v)
399 {
400  return byteorder_htonll(v).u64;
401 }
402 
403 static inline uint16_t NTOHS(uint16_t v)
404 {
405  network_uint16_t input = { v };
406  return byteorder_ntohs(input);
407 }
408 
409 static inline uint32_t NTOHL(uint32_t v)
410 {
411  network_uint32_t input = { v };
412  return byteorder_ntohl(input);
413 }
414 
415 static inline uint64_t NTOHLL(uint64_t v)
416 {
417  network_uint64_t input = { v };
418  return byteorder_ntohll(input);
419 }
420 
421 #ifdef __cplusplus
422 }
423 #endif
424 
425 #endif /* BYTEORDER_H */
426 
be_uint16_t network_uint16_t
A 16 bit integer in network byte order.
Definition: byteorder.h:110
static le_uint16_t byteorder_btols(be_uint16_t v)
Convert from big endian to little endian, 16 bit.
Definition: byteorder.h:326
static uint64_t byteorder_ntohll(network_uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:383
A 32 bit integer in big endian aka network byte order.
Definition: byteorder.h:86
static be_uint16_t byteorder_ltobs(le_uint16_t v)
Convert from little endian to big endian, 16 bit.
Definition: byteorder.h:308
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:314
static uint16_t byteorder_ntohs(network_uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:373
static uint64_t byteorder_swapll(uint64_t v)
Swap byte order, 64 bit.
Definition: byteorder.h:303
static uint32_t HTONL(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:393
#define _byteorder_swap(V, T)
Swaps the byteorder according to the endianess.
Definition: byteorder.h:348
static uint64_t HTONLL(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:398
static uint16_t byteorder_swaps(uint16_t v)
Swap byte order, 16 bit.
Definition: byteorder.h:285
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:332
static be_uint64_t byteorder_ltobll(le_uint64_t v)
Convert from little endian to big endian, 64 bit.
Definition: byteorder.h:320
static uint16_t NTOHS(uint16_t v)
Convert from network byte order to host byte order, 16 bit.
Definition: byteorder.h:403
uint64_t u64
64 bit representation
Definition: byteorder.h:63
A 16 bit integer in little endian.
Definition: byteorder.h:40
static le_uint64_t byteorder_btolll(be_uint64_t v)
Convert from big endian to little endian, 64 bit.
Definition: byteorder.h:338
uint16_t u16
16 bit representation
Definition: byteorder.h:77
static uint32_t byteorder_ntohl(network_uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:378
static network_uint32_t byteorder_htonl(uint32_t v)
Convert from host byte order to network byte order, 32 bit.
Definition: byteorder.h:361
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 uint16_t HTONS(uint16_t v)
Convert from host byte order to network byte order, 16 bit.
Definition: byteorder.h:388
static uint64_t NTOHLL(uint64_t v)
Convert from network byte order to host byte order, 64 bit.
Definition: byteorder.h:415
static uint32_t byteorder_swapl(uint32_t v)
Swap byte order, 32 bit.
Definition: byteorder.h:298
uint64_t u64
64 bit representation
Definition: byteorder.h:99
uint8_t u8[2]
8 bit representation
Definition: byteorder.h:78
static uint32_t NTOHL(uint32_t v)
Convert from network byte order to host byte order, 32 bit.
Definition: byteorder.h:409
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:355
be_uint64_t network_uint64_t
A 64 bit integer in network byte order.
Definition: byteorder.h:120
static network_uint64_t byteorder_htonll(uint64_t v)
Convert from host byte order to network byte order, 64 bit.
Definition: byteorder.h:367