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