]> git.draconx.ca Git - liblbx.git/blob - src/byteorder.h
5eb4daf51ddfc32f2728816c4acd1d6fb60cb389
[liblbx.git] / src / byteorder.h
1 #ifndef BYTEORDER_H_
2 #define BYTEORDER_H_
3
4 #include <stdint.h>
5
6 #ifdef HAVE_CONFIG_H
7 #       include "config.h"
8 #endif
9
10 static inline uint16_t _flip16(uint16_t val)
11 {
12         uint16_t hostval;
13         int i;
14
15         for (i = 0; i < sizeof val; i++)
16                 ((uint8_t *)&hostval)[i] = ((uint8_t *)&val)[sizeof val - i];
17 }
18
19 static inline uint32_t _flip32(uint32_t val)
20 {
21         uint32_t hostval;
22         int i;
23
24         for (i = 0; i < sizeof val; i++)
25                 ((uint8_t *)&hostval)[i] = ((uint8_t *)&val)[sizeof val - i];
26 }
27
28 #ifdef WORDS_BIGENDIAN
29 #       define letohs(x) _flip16(x)
30 #       define letohl(x) _flip32(x)
31 #else
32 #       define letohs(x) (x)
33 #       define letohl(x) (x)
34 #endif
35
36 #endif