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 #ifndef CONFIG_PICOLIBC
88 #define htobe16(x) bswap16((x))
89 #define htobe32(x) bswap32((x))
90 #define htobe64(x) bswap64((x))
91 #define htole16(x) ((uint16_t)(x))
92 #define htole32(x) ((uint32_t)(x))
93 #define htole64(x) ((uint64_t)(x))
94
95 #define be16toh(x) bswap16((x))
96 #define be32toh(x) bswap32((x))
97 #define be64toh(x) bswap64((x))
98 #define le16toh(x) ((uint16_t)(x))
99 #define le32toh(x) ((uint32_t)(x))
100 #define le64toh(x) ((uint64_t)(x))
101 #endif /* CONFIG_PICOLIBC */
102 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
103 #define htobe16(x) ((uint16_t)(x))
104 #define htobe32(x) ((uint32_t)(x))
105 #define htobe64(x) ((uint64_t)(x))
106 #define htole16(x) bswap16((x))
107 #define htole32(x) bswap32((x))
108 #define htole64(x) bswap64((x))
109
110 #define be16toh(x) ((uint16_t)(x))
111 #define be32toh(x) ((uint32_t)(x))
112 #define be64toh(x) ((uint64_t)(x))
113 #define le16toh(x) bswap16((x))
114 #define le32toh(x) bswap32((x))
115 #define le64toh(x) bswap64((x))
116 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
117
118 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
119 #define INLINE __inline__
120
121 static INLINE uint16_t
be16dec(const void * pp)122 be16dec(const void *pp)
123 {
124 uint8_t const *p = (uint8_t const *)pp;
125
126 return ((p[0] << 8) | p[1]);
127 }
128
129 static INLINE uint32_t
be32dec(const void * pp)130 be32dec(const void *pp)
131 {
132 uint8_t const *p = (uint8_t const *)pp;
133
134 return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
135 }
136
137 static INLINE uint64_t
be64dec(const void * pp)138 be64dec(const void *pp)
139 {
140 uint8_t const *p = (uint8_t const *)pp;
141
142 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
143 }
144
145 static INLINE uint16_t
le16dec(const void * pp)146 le16dec(const void *pp)
147 {
148 uint8_t const *p = (uint8_t const *)pp;
149
150 return ((p[1] << 8) | p[0]);
151 }
152
153 static INLINE uint32_t
le32dec(const void * pp)154 le32dec(const void *pp)
155 {
156 uint8_t const *p = (uint8_t const *)pp;
157
158 return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
159 }
160
161 static INLINE uint64_t
le64dec(const void * pp)162 le64dec(const void *pp)
163 {
164 uint8_t const *p = (uint8_t const *)pp;
165
166 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
167 }
168
169 static INLINE void
be16enc(void * pp,uint16_t u)170 be16enc(void *pp, uint16_t u)
171 {
172 uint8_t *p = (uint8_t *)pp;
173
174 p[0] = (u >> 8) & 0xff;
175 p[1] = u & 0xff;
176 }
177
178 static INLINE void
be32enc(void * pp,uint32_t u)179 be32enc(void *pp, uint32_t u)
180 {
181 uint8_t *p = (uint8_t *)pp;
182
183 p[0] = (u >> 24) & 0xff;
184 p[1] = (u >> 16) & 0xff;
185 p[2] = (u >> 8) & 0xff;
186 p[3] = u & 0xff;
187 }
188
189 static INLINE void
be64enc(void * pp,uint64_t u)190 be64enc(void *pp, uint64_t u)
191 {
192 uint8_t *p = (uint8_t *)pp;
193
194 be32enc(p, (uint32_t)(u >> 32));
195 be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
196 }
197
198 static INLINE void
le16enc(void * pp,uint16_t u)199 le16enc(void *pp, uint16_t u)
200 {
201 uint8_t *p = (uint8_t *)pp;
202
203 p[0] = u & 0xff;
204 p[1] = (u >> 8) & 0xff;
205 }
206
207 static INLINE void
le32enc(void * pp,uint32_t u)208 le32enc(void *pp, uint32_t u)
209 {
210 uint8_t *p = (uint8_t *)pp;
211
212 p[0] = u & 0xff;
213 p[1] = (u >> 8) & 0xff;
214 p[2] = (u >> 16) & 0xff;
215 p[3] = (u >> 24) & 0xff;
216 }
217
218 static INLINE void
le64enc(void * pp,uint64_t u)219 le64enc(void *pp, uint64_t u)
220 {
221 uint8_t *p = (uint8_t *)pp;
222
223 le32enc(p, (uint32_t)(u & 0xffffffffU));
224 le32enc(p + 4, (uint32_t)(u >> 32));
225 }
226
227 #endif /* _ENDIAN_H_ */
228