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: #ifndef _SYS_ENDIAN_H_
35: #define _SYS_ENDIAN_H_
36:
37: #include <sys/featuretest.h>
38:
39: 40: 41: 42:
43: #define _LITTLE_ENDIAN 1234
44: #define _BIG_ENDIAN 4321
45: #define _PDP_ENDIAN 3412
46:
47:
48: #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
49: #ifndef _LOCORE
50:
51:
52:
53: #include <sys/ansi.h>
54: #include <sys/cdefs.h>
55: #include <sys/types.h>
56:
57: #ifndef in_addr_t
58: typedef __in_addr_t in_addr_t;
59: #define in_addr_t __in_addr_t
60: #endif
61:
62: #ifndef in_port_t
63: typedef __in_port_t in_port_t;
64: #define in_port_t __in_port_t
65: #endif
66:
67: __BEGIN_DECLS
68: uint32_t htonl(uint32_t) __attribute__((__const__));
69: uint16_t htons(uint16_t) __attribute__((__const__));
70: uint32_t ntohl(uint32_t) __attribute__((__const__));
71: uint16_t ntohs(uint16_t) __attribute__((__const__));
72: __END_DECLS
73:
74: #endif
75: #endif
76:
77:
78: #ifndef T2EX
79: #include <machine/endian_machdep.h>
80: #else
81: #include <sys/sysdepend/machine_common.h>
82: #if BIGENDIAN
83: #define _BYTE_ORDER _BIG_ENDIAN
84: #else
85: #define _BYTE_ORDER _LITTLE_ENDIAN
86: #endif
87: #endif
88:
89: 90: 91:
92: #if _BYTE_ORDER == _LITTLE_ENDIAN
93: #define _QUAD_HIGHWORD 1
94: #define _QUAD_LOWWORD 0
95: #endif
96:
97: #if _BYTE_ORDER == _BIG_ENDIAN
98: #define _QUAD_HIGHWORD 0
99: #define _QUAD_LOWWORD 1
100: #endif
101:
102:
103: #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
104: 105: 106: 107: 108:
109: #define LITTLE_ENDIAN 1234
110: #define BIG_ENDIAN 4321
111: #define PDP_ENDIAN 3412
112: #define BYTE_ORDER _BYTE_ORDER
113:
114: #ifndef _LOCORE
115:
116: #ifndef T2EX
117: #include <machine/bswap.h>
118: #else
119: #include <sys/bswap.h>
120: #endif
121:
122: 123: 124:
125: #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
126: #define ntohl(x) (x)
127: #define ntohs(x) (x)
128: #define htonl(x) (x)
129: #define htons(x) (x)
130:
131: #define NTOHL(x) (void) (x)
132: #define NTOHS(x) (void) (x)
133: #define HTONL(x) (void) (x)
134: #define HTONS(x) (void) (x)
135:
136: #else
137:
138: #define ntohl(x) bswap32((uint32_t)(x))
139: #define ntohs(x) bswap16((uint16_t)(x))
140: #define htonl(x) bswap32((uint32_t)(x))
141: #define htons(x) bswap16((uint16_t)(x))
142:
143: #define NTOHL(x) (x) = ntohl((uint32_t)(x))
144: #define NTOHS(x) (x) = ntohs((uint16_t)(x))
145: #define HTONL(x) (x) = htonl((uint32_t)(x))
146: #define HTONS(x) (x) = htons((uint16_t)(x))
147: #endif
148:
149: 150: 151:
152:
153: #if BYTE_ORDER == BIG_ENDIAN
154:
155: #define htobe16(x) (x)
156: #define htobe32(x) (x)
157: #define htobe64(x) (x)
158: #define htole16(x) bswap16((uint16_t)(x))
159: #define htole32(x) bswap32((uint32_t)(x))
160: #define htole64(x) bswap64((uint64_t)(x))
161:
162: #define HTOBE16(x) (void) (x)
163: #define HTOBE32(x) (void) (x)
164: #define HTOBE64(x) (void) (x)
165: #define HTOLE16(x) (x) = bswap16((uint16_t)(x))
166: #define HTOLE32(x) (x) = bswap32((uint32_t)(x))
167: #define HTOLE64(x) (x) = bswap64((uint64_t)(x))
168:
169: #else
170:
171: #define htobe16(x) bswap16((uint16_t)(x))
172: #define htobe32(x) bswap32((uint32_t)(x))
173: #define htobe64(x) bswap64((uint64_t)(x))
174: #define htole16(x) (x)
175: #define htole32(x) (x)
176: #define htole64(x) (x)
177:
178: #define HTOBE16(x) (x) = bswap16((uint16_t)(x))
179: #define HTOBE32(x) (x) = bswap32((uint32_t)(x))
180: #define HTOBE64(x) (x) = bswap64((uint64_t)(x))
181: #define HTOLE16(x) (void) (x)
182: #define HTOLE32(x) (void) (x)
183: #define HTOLE64(x) (void) (x)
184:
185: #endif
186:
187: #define be16toh(x) htobe16(x)
188: #define be32toh(x) htobe32(x)
189: #define be64toh(x) htobe64(x)
190: #define le16toh(x) htole16(x)
191: #define le32toh(x) htole32(x)
192: #define le64toh(x) htole64(x)
193:
194: #define BE16TOH(x) HTOBE16(x)
195: #define BE32TOH(x) HTOBE32(x)
196: #define BE64TOH(x) HTOBE64(x)
197: #define LE16TOH(x) HTOLE16(x)
198: #define LE32TOH(x) HTOLE32(x)
199: #define LE64TOH(x) HTOLE64(x)
200:
201: 202: 203: 204:
205:
206: #if __GNUC_PREREQ__(2, 95)
207:
208: #define __GEN_ENDIAN_ENC(bits, endian) \
209: static __inline __unused void \
210: endian ## bits ## enc(void *dst, uint ## bits ## _t u) \
211: { \
212: u = hto ## endian ## bits (u); \
213: __builtin_memcpy(dst, &u, sizeof(u)); \
214: }
215:
216: __GEN_ENDIAN_ENC(16, be)
217: __GEN_ENDIAN_ENC(32, be)
218: __GEN_ENDIAN_ENC(64, be)
219: __GEN_ENDIAN_ENC(16, le)
220: __GEN_ENDIAN_ENC(32, le)
221: __GEN_ENDIAN_ENC(64, le)
222: #undef __GEN_ENDIAN_ENC
223:
224: #define __GEN_ENDIAN_DEC(bits, endian) \
225: static __inline __unused uint ## bits ## _t \
226: endian ## bits ## dec(const void *buf) \
227: { \
228: uint ## bits ## _t u; \
229: __builtin_memcpy(&u, buf, sizeof(u)); \
230: return endian ## bits ## toh (u); \
231: }
232:
233: __GEN_ENDIAN_DEC(16, be)
234: __GEN_ENDIAN_DEC(32, be)
235: __GEN_ENDIAN_DEC(64, be)
236: __GEN_ENDIAN_DEC(16, le)
237: __GEN_ENDIAN_DEC(32, le)
238: __GEN_ENDIAN_DEC(64, le)
239: #undef __GEN_ENDIAN_DEC
240:
241: #else
242:
243: static __inline void __unused
244: be16enc(void *buf, uint16_t u)
245: {
246: uint8_t *p = (uint8_t *)buf;
247:
248: p[0] = (uint8_t)(((unsigned)u >> 8) & 0xff);
249: p[1] = (uint8_t)(u & 0xff);
250: }
251:
252: static __inline void __unused
253: le16enc(void *buf, uint16_t u)
254: {
255: uint8_t *p = (uint8_t *)buf;
256:
257: p[0] = (uint8_t)(u & 0xff);
258: p[1] = (uint8_t)(((unsigned)u >> 8) & 0xff);
259: }
260:
261: static __inline uint16_t __unused
262: be16dec(const void *buf)
263: {
264: const uint8_t *p = (const uint8_t *)buf;
265:
266: return (uint16_t)((p[0] << 8) | p[1]);
267: }
268:
269: static __inline uint16_t __unused
270: le16dec(const void *buf)
271: {
272: const uint8_t *p = (const uint8_t *)buf;
273:
274: return (uint16_t)((p[1] << 8) | p[0]);
275: }
276:
277: static __inline void __unused
278: be32enc(void *buf, uint32_t u)
279: {
280: uint8_t *p = (uint8_t *)buf;
281:
282: p[0] = (uint8_t)((u >> 24) & 0xff);
283: p[1] = (uint8_t)((u >> 16) & 0xff);
284: p[2] = (uint8_t)((u >> 8) & 0xff);
285: p[3] = (uint8_t)(u & 0xff);
286: }
287:
288: static __inline void __unused
289: le32enc(void *buf, uint32_t u)
290: {
291: uint8_t *p = (uint8_t *)buf;
292:
293: p[0] = (uint8_t)(u & 0xff);
294: p[1] = (uint8_t)((u >> 8) & 0xff);
295: p[2] = (uint8_t)((u >> 16) & 0xff);
296: p[3] = (uint8_t)((u >> 24) & 0xff);
297: }
298:
299: static __inline uint32_t __unused
300: be32dec(const void *buf)
301: {
302: const uint8_t *p = (const uint8_t *)buf;
303:
304: return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
305: }
306:
307: static __inline uint32_t __unused
308: le32dec(const void *buf)
309: {
310: const uint8_t *p = (const uint8_t *)buf;
311:
312: return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
313: }
314:
315: static __inline void __unused
316: be64enc(void *buf, uint64_t u)
317: {
318: uint8_t *p = (uint8_t *)buf;
319:
320: be32enc(p, (uint32_t)(u >> 32));
321: be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
322: }
323:
324: static __inline void __unused
325: le64enc(void *buf, uint64_t u)
326: {
327: uint8_t *p = (uint8_t *)buf;
328:
329: le32enc(p, (uint32_t)(u & 0xffffffffULL));
330: le32enc(p + 4, (uint32_t)(u >> 32));
331: }
332:
333: static __inline uint64_t __unused
334: be64dec(const void *buf)
335: {
336: const uint8_t *p = (const uint8_t *)buf;
337:
338: return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
339: }
340:
341: static __inline uint64_t __unused
342: le64dec(const void *buf)
343: {
344: const uint8_t *p = (const uint8_t *)buf;
345:
346: return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
347: }
348:
349: #endif
350:
351: #endif
352: #endif
353: #endif