forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
endianness.h
135 lines (118 loc) · 4.66 KB
/
endianness.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (C) 2008 iptelorg GmbH
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/** @file
* Kamailio core :: endianness compile and runtime tests
* @author andrei
* @ingroup core
*
*
* Defines:
* - __IS_LITTLE_ENDIAN if the system is little endian and
* - __IS_BIG_ENDIAN if it's big endian
* Function/macros:
* - is_big_endian() - runtime test for big endian
* - is_little_endian() - runtime test for little endian
* - endianness_sanity_check() - returns 0 if the compile time
* detected endianness corresponds to
* the runtime detected one and -1 on
* error (recommended action: bail out)
*
* Implementation notes:
* Endian macro names/tests for different OSes:
* linux: __BYTE_ORDER == __LITTLE_ENDIAN | __BIG_ENDIAN
* BYTE_ORDER == LITTLE_ENDIAN | BIG_ENDIAN
* bsd: _BYTE_ORDER == _LITTLE_ENDIAN | _BIG_ENDIAN
* BYTE_ORDER == LITTLE_ENDIAN | BIG_ENDIAN
* solaris: _LITTLE_ENDIAN | _BIG_ENDIAN
*
* Include file for the endian macros:
* linux: <endian.h> (glibc), <sys/param.h>
* bsd: <sys/param.h>, <sys/endian.h>
* solaris: <sys/param.h>
*
* Note: BIG_ENDIAN, LITTLE_ENDIAN, _BIG_ENDIAN, _LITTLE_ENDIAN cannot be
* used always, some OSes define both of them for BYTE_ORDER use
* (e.g. linux defines both BIG_ENDIAN & LITTLE_ENDIAN, bsds define
* _BIG_ENDIAN, _LITTLE_ENDIAN, BIG_ENDIAN, LITTLE_ENDIAN)
*
*/
#ifndef _endianness_h
#define _endianness_h
/* use bsd includes: they work everywhere */
#include <sys/types.h>
#include <sys/param.h>
extern int _endian_test_int;
/* returns 1 for little endian, 0 for big endian */
#define endian_test() (*(char*)&_endian_test_int==1)
#define is_big_endian() (!endian_test())
#define is_little_endian() endian_test()
extern int endianness_sanity_check(void);
/* detect compile time endianess */
#if defined __BYTE_ORDER && defined __LITTLE_ENDIAN && defined __BIG_ENDIAN
/* linux */
#if __BYTE_ORDER == __LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
#define __IS_LITTLE_ENDIAN 0x01020304
#endif
#if __BYTE_ORDER == __BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
#define __IS_BIG_ENDIAN 0x01020304
#endif
#elif defined _BYTE_ORDER && defined _LITTLE_ENDIAN && defined _BIG_ENDIAN
/* bsd */
#if _BYTE_ORDER == _LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
#define __IS_LITTLE_ENDIAN 0x01020304
#endif
#if _BYTE_ORDER == _BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
#define __IS_BIG_ENDIAN 0x01020304
#endif
#elif defined BYTE_ORDER && defined LITTLE_ENDIAN && defined BIG_ENDIAN
/* bsd old/deprecated */
#if BYTE_ORDER == LITTLE_ENDIAN && ! defined __IS_LITTLE_ENDIAN
#define __IS_LITTLE_ENDIAN 0x01020304
#endif
#if BYTE_ORDER == BIG_ENDIAN && ! defined __IS_BIG_ENDIAN
#define __IS_BIG_ENDIAN 0x01020304
#endif
#elif !(defined _LITTLE_ENDIAN && defined _BIG_ENDIAN) && \
(defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)
/* OSes that don't define BYTE_ORDER (sanity check above makes sure
* little & big endian are not defined in the same time )*/
/* solaris */
#if defined _LITTLE_ENDIAN && !defined __IS_LITTLE_ENDIAN
#define __IS_LITTLE_ENDIAN 0x01020304
#endif
#if defined _BIG_ENDIAN && !defined __IS_BIG_ENDIAN
#define __IS_BIG_ENDIAN 0x04030201
#endif
#elif !(defined LITTLE_ENDIAN && defined BIG_ENDIAN) && \
(defined LITTLE_ENDIAN || defined BIG_ENDIAN)
/* OSes that don't define BYTE_ORDER (sanity check above makes sure
* little & big endian are not defined in the same time )*/
#if defined LITTLE_ENDIAN && !defined __IS_LITTLE_ENDIAN
#define __IS_LITTLE_ENDIAN 0x01020304
#endif
#if defined BIG_ENDIAN && !defined __IS_BIG_ENDIAN
#define __IS_BIG_ENDIAN 0x04030201
#endif
#else
#error could not detect endianess
#endif
#if !defined __IS_LITTLE_ENDIAN && !defined __IS_BIG_ENDIAN
#error BUG: could not detect endianess
#endif
#if defined __IS_LITTLE_ENDIAN && defined __IS_BIG_ENDIAN
#error BUG: both little & big endian detected in the same time
#endif
#endif /* _endianness_h */