Commit | Line | Data |
---|---|---|
eb71a0aa DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
eb71a0aa DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
eb71a0aa DG |
16 | */ |
17 | ||
f263b7fd | 18 | #ifndef _COMPAT_ENDIAN_H |
eb71a0aa DG |
19 | #define _COMPAT_ENDIAN_H |
20 | ||
07bb6d71 | 21 | #if defined(__linux__) || defined(__CYGWIN__) |
eb71a0aa | 22 | #include <endian.h> |
f263b7fd JD |
23 | |
24 | /* | |
25 | * htobe/betoh are not defined for glibc <2.9, so add them | |
26 | * explicitly if they are missing. | |
27 | */ | |
28 | #ifdef __USE_BSD | |
29 | /* Conversion interfaces. */ | |
30 | # include <byteswap.h> | |
31 | ||
32 | # if __BYTE_ORDER == __LITTLE_ENDIAN | |
33 | # ifndef htobe16 | |
34 | # define htobe16(x) __bswap_16(x) | |
35 | # endif | |
36 | # ifndef htole16 | |
37 | # define htole16(x) (x) | |
38 | # endif | |
39 | # ifndef be16toh | |
40 | # define be16toh(x) __bswap_16(x) | |
41 | # endif | |
42 | # ifndef le16toh | |
43 | # define le16toh(x) (x) | |
44 | # endif | |
45 | ||
46 | # ifndef htobe32 | |
47 | # define htobe32(x) __bswap_32(x) | |
48 | # endif | |
49 | # ifndef htole32 | |
50 | # define htole32(x) (x) | |
51 | # endif | |
52 | # ifndef be32toh | |
53 | # define be32toh(x) __bswap_32(x) | |
54 | # endif | |
55 | # ifndef le32toh | |
56 | # define le32toh(x) (x) | |
57 | # endif | |
58 | ||
59 | # ifndef htobe64 | |
60 | # define htobe64(x) __bswap_64(x) | |
61 | # endif | |
62 | # ifndef htole64 | |
63 | # define htole64(x) (x) | |
64 | # endif | |
65 | # ifndef be64toh | |
66 | # define be64toh(x) __bswap_64(x) | |
67 | # endif | |
68 | # ifndef le64toh | |
69 | # define le64toh(x) (x) | |
70 | # endif | |
71 | ||
72 | # else /* __BYTE_ORDER == __LITTLE_ENDIAN */ | |
73 | # ifndef htobe16 | |
74 | # define htobe16(x) (x) | |
75 | # endif | |
76 | # ifndef htole16 | |
77 | # define htole16(x) __bswap_16(x) | |
78 | # endif | |
79 | # ifndef be16toh | |
80 | # define be16toh(x) (x) | |
81 | # endif | |
82 | # ifndef le16toh | |
83 | # define le16toh(x) __bswap_16(x) | |
84 | # endif | |
85 | ||
86 | # ifndef htobe32 | |
87 | # define htobe32(x) (x) | |
88 | # endif | |
89 | # ifndef htole32 | |
90 | # define htole32(x) __bswap_32(x) | |
91 | # endif | |
92 | # ifndef be32toh | |
93 | # define be32toh(x) (x) | |
94 | # endif | |
95 | # ifndef le32toh | |
96 | # define le32toh(x) __bswap_32(x) | |
97 | # endif | |
98 | ||
99 | # ifndef htobe64 | |
100 | # define htobe64(x) (x) | |
101 | # endif | |
102 | # ifndef htole64 | |
103 | # define htole64(x) __bswap_64(x) | |
104 | # endif | |
105 | # ifndef be64toh | |
106 | # define be64toh(x) (x) | |
107 | # endif | |
108 | # ifndef le64toh | |
109 | # define le64toh(x) __bswap_64(x) | |
110 | # endif | |
111 | ||
112 | # endif /* __BYTE_ORDER == __LITTLE_ENDIAN */ | |
113 | #endif /* __USE_BSD */ | |
114 | ||
b2c3836f | 115 | #elif defined(__FreeBSD__) |
eb71a0aa | 116 | #include <machine/endian.h> |
0c97bd7a MJ |
117 | |
118 | #elif defined(__sun__) | |
119 | #include <sys/byteorder.h> | |
120 | #ifndef __BIG_ENDIAN | |
121 | #define __BIG_ENDIAN 4321 | |
122 | #endif /* __BIG_ENDIAN */ | |
123 | #ifndef __LITTLE_ENDIAN | |
124 | #define __LITTLE_ENDIAN 1234 | |
125 | #endif /* __LITTLE_ENDIAN */ | |
126 | ||
127 | #ifdef _LITTLE_ENDIAN | |
128 | #define __BYTE_ORDER __LITTLE_ENDIAN | |
129 | #endif /* _LITTLE_ENDIAN */ | |
130 | #ifdef _BIG_ENDIAN | |
131 | #define __BYTE_ORDER __BIG_ENDIAN | |
132 | #endif /* _BIG_ENDIAN */ | |
133 | ||
134 | #define LITTLE_ENDIAN __LITTLE_ENDIAN | |
135 | #define BIG_ENDIAN __BIG_ENDIAN | |
136 | #define PDP_ENDIAN __PDP_ENDIAN | |
137 | #define BYTE_ORDER __BYTE_ORDER | |
138 | ||
139 | #define betoh16(x) BE_16(x) | |
140 | #define letoh16(x) LE_16(x) | |
141 | #define betoh32(x) BE_32(x) | |
142 | #define letoh32(x) LE_32(x) | |
143 | #define betoh64(x) BE_64(x) | |
144 | #define letoh64(x) LE_64(x) | |
145 | #define htobe16(x) BE_16(x) | |
146 | #define be16toh(x) BE_16(x) | |
147 | #define htobe32(x) BE_32(x) | |
148 | #define be32toh(x) BE_32(x) | |
149 | #define htobe64(x) BE_64(x) | |
150 | #define be64toh(x) BE_64(x) | |
151 | ||
50ec4d1a MJ |
152 | #elif defined(__APPLE__) |
153 | # include <machine/endian.h> | |
154 | # include <libkern/OSByteOrder.h> | |
155 | ||
156 | # if BYTE_ORDER == LITTLE_ENDIAN | |
157 | # define htobe16(x) OSSwapConstInt16(x) | |
158 | # define htole16(x) (x) | |
159 | # define be16toh(x) OSSwapConstInt16(x) | |
160 | # define le16toh(x) (x) | |
161 | ||
162 | # define htobe32(x) OSSwapConstInt32(x) | |
163 | # define htole32(x) (x) | |
164 | # define be32toh(x) OSSwapConstInt32(x) | |
165 | # define le32toh(x) (x) | |
166 | ||
167 | # define htobe64(x) OSSwapConstInt64(x) | |
168 | # define htole64(x) (x) | |
169 | # define be64toh(x) OSSwapConstInt64(x) | |
170 | # define le64toh(x) (x) | |
171 | ||
172 | # else /* BYTE_ORDER == LITTLE_ENDIAN */ | |
173 | # define htobe16(x) (x) | |
174 | # define htole16(x) OSSwapConstInt16(x) | |
175 | # define be16toh(x) (x) | |
176 | # define le16toh(x) OSSwapConstInt16(x) | |
177 | ||
178 | # define htobe32(x) (x) | |
179 | # define htole32(x) OSSwapConstInt32(x) | |
180 | # define be32toh(x) (x) | |
181 | # define le32toh(x) OSSwapConstInt32(x) | |
182 | ||
183 | # define htobe64(x) (x) | |
184 | # define htole64(x) OSSwapConstInt64(x) | |
185 | # define be64toh(x) (x) | |
186 | # define le64toh(x) OSSwapConstInt64(x) | |
187 | # endif | |
188 | ||
eb71a0aa | 189 | #else |
b2c3836f | 190 | #error "Please add support for your OS." |
eb71a0aa DG |
191 | #endif |
192 | ||
193 | #endif /* _COMPAT_ENDIAN_H */ |