1f9a21cf8SJérôme Duval/*-
2f9a21cf8SJérôme Duval * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
3f9a21cf8SJérôme Duval * All rights reserved.
4f9a21cf8SJérôme Duval *
5f9a21cf8SJérôme Duval * Redistribution and use in source and binary forms, with or without
6f9a21cf8SJérôme Duval * modification, are permitted provided that the following conditions
7f9a21cf8SJérôme Duval * are met:
8f9a21cf8SJérôme Duval * 1. Redistributions of source code must retain the above copyright
9f9a21cf8SJérôme Duval *    notice, this list of conditions and the following disclaimer.
10f9a21cf8SJérôme Duval * 2. Redistributions in binary form must reproduce the above copyright
11f9a21cf8SJérôme Duval *    notice, this list of conditions and the following disclaimer in the
12f9a21cf8SJérôme Duval *    documentation and/or other materials provided with the distribution.
13f9a21cf8SJérôme Duval *
14f9a21cf8SJérôme Duval * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15f9a21cf8SJérôme Duval * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f9a21cf8SJérôme Duval * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f9a21cf8SJérôme Duval * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f9a21cf8SJérôme Duval * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f9a21cf8SJérôme Duval * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f9a21cf8SJérôme Duval * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f9a21cf8SJérôme Duval * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f9a21cf8SJérôme Duval * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f9a21cf8SJérôme Duval * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f9a21cf8SJérôme Duval * SUCH DAMAGE.
25f9a21cf8SJérôme Duval *
26f9a21cf8SJérôme Duval * $FreeBSD: src/sys/sys/endian.h,v 1.6 2003/10/15 20:05:57 obrien Exp $
27f9a21cf8SJérôme Duval */
28f9a21cf8SJérôme Duval#ifndef _FBSD_COMPAT_SYS_ENDIAN_H_
29f9a21cf8SJérôme Duval#define _FBSD_COMPAT_SYS_ENDIAN_H_
30f9a21cf8SJérôme Duval
31f9a21cf8SJérôme Duval#include <stdint.h>
32f9a21cf8SJérôme Duval#include <posix/endian.h>
33f9a21cf8SJérôme Duval
34f9a21cf8SJérôme Duval#include <support/ByteOrder.h>
35f9a21cf8SJérôme Duval
36f9a21cf8SJérôme Duval#include <sys/cdefs.h>
37f9a21cf8SJérôme Duval#include <sys/_types.h>
38f9a21cf8SJérôme Duval#include <machine/endian.h>
39f9a21cf8SJérôme Duval
40f9a21cf8SJérôme Duval#define _BYTE_ORDER		BYTE_ORDER
41f9a21cf8SJérôme Duval#define _LITTLE_ENDIAN	LITTLE_ENDIAN
42f9a21cf8SJérôme Duval#define _BIG_ENDIAN		BIG_ENDIAN
43f9a21cf8SJérôme Duval
44f9a21cf8SJérôme Duval#define __bswap16(x)	__swap_int16(x)
45f9a21cf8SJérôme Duval#define __bswap32(x)	__swap_int32(x)
46f9a21cf8SJérôme Duval#define __bswap64(x)	__swap_int64(x)
47f9a21cf8SJérôme Duval
48f9a21cf8SJérôme Duval/*
49f9a21cf8SJérôme Duval * General byte order swapping functions.
50f9a21cf8SJérôme Duval */
51f9a21cf8SJérôme Duval#define	bswap16(x)	__bswap16(x)
52f9a21cf8SJérôme Duval#define	bswap32(x)	__bswap32(x)
53f9a21cf8SJérôme Duval#define	bswap64(x)	__bswap64(x)
54f9a21cf8SJérôme Duval
55f9a21cf8SJérôme Duval/*
56f9a21cf8SJérôme Duval * Host to big endian, host to little endian, big endian to host, and little
57f9a21cf8SJérôme Duval * endian to host byte order functions as detailed in byteorder(9).
58f9a21cf8SJérôme Duval */
59f9a21cf8SJérôme Duval#if _BYTE_ORDER == _LITTLE_ENDIAN
60f9a21cf8SJérôme Duval#define	htobe16(x)	bswap16((x))
61f9a21cf8SJérôme Duval#define	htobe32(x)	bswap32((x))
62f9a21cf8SJérôme Duval#define	htobe64(x)	bswap64((x))
63f9a21cf8SJérôme Duval#define	htole16(x)	((uint16_t)(x))
64f9a21cf8SJérôme Duval#define	htole32(x)	((uint32_t)(x))
65f9a21cf8SJérôme Duval#define	htole64(x)	((uint64_t)(x))
66f9a21cf8SJérôme Duval
67f9a21cf8SJérôme Duval#define	be16toh(x)	bswap16((x))
68f9a21cf8SJérôme Duval#define	be32toh(x)	bswap32((x))
69f9a21cf8SJérôme Duval#define	be64toh(x)	bswap64((x))
70f9a21cf8SJérôme Duval#define	le16toh(x)	((uint16_t)(x))
71f9a21cf8SJérôme Duval#define	le32toh(x)	((uint32_t)(x))
72f9a21cf8SJérôme Duval#define	le64toh(x)	((uint64_t)(x))
73f9a21cf8SJérôme Duval#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
74f9a21cf8SJérôme Duval#define	htobe16(x)	((uint16_t)(x))
75f9a21cf8SJérôme Duval#define	htobe32(x)	((uint32_t)(x))
76f9a21cf8SJérôme Duval#define	htobe64(x)	((uint64_t)(x))
77f9a21cf8SJérôme Duval#define	htole16(x)	bswap16((x))
78f9a21cf8SJérôme Duval#define	htole32(x)	bswap32((x))
79f9a21cf8SJérôme Duval#define	htole64(x)	bswap64((x))
80f9a21cf8SJérôme Duval
81f9a21cf8SJérôme Duval#define	be16toh(x)	((uint16_t)(x))
82f9a21cf8SJérôme Duval#define	be32toh(x)	((uint32_t)(x))
83f9a21cf8SJérôme Duval#define	be64toh(x)	((uint64_t)(x))
84f9a21cf8SJérôme Duval#define	le16toh(x)	bswap16((x))
85f9a21cf8SJérôme Duval#define	le32toh(x)	bswap32((x))
86f9a21cf8SJérôme Duval#define	le64toh(x)	bswap64((x))
87f9a21cf8SJérôme Duval#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
88f9a21cf8SJérôme Duval
89f9a21cf8SJérôme Duval/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
90f9a21cf8SJérôme Duval
91f9a21cf8SJérôme Duvalstatic __inline uint16_t
92f9a21cf8SJérôme Duvalbe16dec(const void *pp)
93f9a21cf8SJérôme Duval{
94f9a21cf8SJérôme Duval	unsigned char const *p = (unsigned char const *)pp;
95f9a21cf8SJérôme Duval
96f9a21cf8SJérôme Duval	return ((p[0] << 8) | p[1]);
97f9a21cf8SJérôme Duval}
98f9a21cf8SJérôme Duval
99f9a21cf8SJérôme Duvalstatic __inline uint32_t
100f9a21cf8SJérôme Duvalbe32dec(const void *pp)
101f9a21cf8SJérôme Duval{
102f9a21cf8SJérôme Duval	unsigned char const *p = (unsigned char const *)pp;
103f9a21cf8SJérôme Duval
104f9a21cf8SJérôme Duval	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
105f9a21cf8SJérôme Duval}
106f9a21cf8SJérôme Duval
107f9a21cf8SJérôme Duvalstatic __inline uint64_t
108f9a21cf8SJérôme Duvalbe64dec(const void *pp)
109f9a21cf8SJérôme Duval{
110f9a21cf8SJérôme Duval	unsigned char const *p = (unsigned char const *)pp;
111f9a21cf8SJérôme Duval
112f9a21cf8SJérôme Duval	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
113f9a21cf8SJérôme Duval}
114f9a21cf8SJérôme Duval
115f9a21cf8SJérôme Duvalstatic __inline uint16_t
116f9a21cf8SJérôme Duvalle16dec(const void *pp)
117f9a21cf8SJérôme Duval{
118f9a21cf8SJérôme Duval	unsigned char const *p = (unsigned char const *)pp;
119f9a21cf8SJérôme Duval
120f9a21cf8SJérôme Duval	return ((p[1] << 8) | p[0]);
121f9a21cf8SJérôme Duval}
122f9a21cf8SJérôme Duval
123f9a21cf8SJérôme Duvalstatic __inline uint32_t
124f9a21cf8SJérôme Duvalle32dec(const void *pp)
125f9a21cf8SJérôme Duval{
126f9a21cf8SJérôme Duval	unsigned char const *p = (unsigned char const *)pp;
127f9a21cf8SJérôme Duval
128f9a21cf8SJérôme Duval	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
129f9a21cf8SJérôme Duval}
130f9a21cf8SJérôme Duval
131f9a21cf8SJérôme Duvalstatic __inline uint64_t
132f9a21cf8SJérôme Duvalle64dec(const void *pp)
133f9a21cf8SJérôme Duval{
134f9a21cf8SJérôme Duval	unsigned char const *p = (unsigned char const *)pp;
135f9a21cf8SJérôme Duval
136f9a21cf8SJérôme Duval	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
137f9a21cf8SJérôme Duval}
138f9a21cf8SJérôme Duval
139f9a21cf8SJérôme Duvalstatic __inline void
140f9a21cf8SJérôme Duvalbe16enc(void *pp, uint16_t u)
141f9a21cf8SJérôme Duval{
142f9a21cf8SJérôme Duval	unsigned char *p = (unsigned char *)pp;
143f9a21cf8SJérôme Duval
144f9a21cf8SJérôme Duval	p[0] = (u >> 8) & 0xff;
145f9a21cf8SJérôme Duval	p[1] = u & 0xff;
146f9a21cf8SJérôme Duval}
147f9a21cf8SJérôme Duval
148f9a21cf8SJérôme Duvalstatic __inline void
149f9a21cf8SJérôme Duvalbe32enc(void *pp, uint32_t u)
150f9a21cf8SJérôme Duval{
151f9a21cf8SJérôme Duval	unsigned char *p = (unsigned char *)pp;
152f9a21cf8SJérôme Duval
153f9a21cf8SJérôme Duval	p[0] = (u >> 24) & 0xff;
154f9a21cf8SJérôme Duval	p[1] = (u >> 16) & 0xff;
155f9a21cf8SJérôme Duval	p[2] = (u >> 8) & 0xff;
156f9a21cf8SJérôme Duval	p[3] = u & 0xff;
157f9a21cf8SJérôme Duval}
158f9a21cf8SJérôme Duval
159f9a21cf8SJérôme Duvalstatic __inline void
160f9a21cf8SJérôme Duvalbe64enc(void *pp, uint64_t u)
161f9a21cf8SJérôme Duval{
162f9a21cf8SJérôme Duval	unsigned char *p = (unsigned char *)pp;
163f9a21cf8SJérôme Duval
164f9a21cf8SJérôme Duval	be32enc(p, u >> 32);
165f9a21cf8SJérôme Duval	be32enc(p + 4, u & 0xffffffff);
166f9a21cf8SJérôme Duval}
167f9a21cf8SJérôme Duval
168f9a21cf8SJérôme Duvalstatic __inline void
169f9a21cf8SJérôme Duvalle16enc(void *pp, uint16_t u)
170f9a21cf8SJérôme Duval{
171f9a21cf8SJérôme Duval	unsigned char *p = (unsigned char *)pp;
172f9a21cf8SJérôme Duval
173f9a21cf8SJérôme Duval	p[0] = u & 0xff;
174f9a21cf8SJérôme Duval	p[1] = (u >> 8) & 0xff;
175f9a21cf8SJérôme Duval}
176f9a21cf8SJérôme Duval
177f9a21cf8SJérôme Duvalstatic __inline void
178f9a21cf8SJérôme Duvalle32enc(void *pp, uint32_t u)
179f9a21cf8SJérôme Duval{
180f9a21cf8SJérôme Duval	unsigned char *p = (unsigned char *)pp;
181f9a21cf8SJérôme Duval
182f9a21cf8SJérôme Duval	p[0] = u & 0xff;
183f9a21cf8SJérôme Duval	p[1] = (u >> 8) & 0xff;
184f9a21cf8SJérôme Duval	p[2] = (u >> 16) & 0xff;
185f9a21cf8SJérôme Duval	p[3] = (u >> 24) & 0xff;
186f9a21cf8SJérôme Duval}
187f9a21cf8SJérôme Duval
188f9a21cf8SJérôme Duvalstatic __inline void
189f9a21cf8SJérôme Duvalle64enc(void *pp, uint64_t u)
190f9a21cf8SJérôme Duval{
191f9a21cf8SJérôme Duval	unsigned char *p = (unsigned char *)pp;
192f9a21cf8SJérôme Duval
193f9a21cf8SJérôme Duval	le32enc(p, u & 0xffffffff);
194f9a21cf8SJérôme Duval	le32enc(p + 4, u >> 32);
195f9a21cf8SJérôme Duval}
196f9a21cf8SJérôme Duval
197f9a21cf8SJérôme Duval#endif	/* _SYS_ENDIAN_H_ */
198