1 /*
2 * Copyright (c) 2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1987, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * @(#)endian.h 8.1 (Berkeley) 6/11/93
57 */
58
59 #ifndef _SYS_ENDIAN_H_
60 #define _SYS_ENDIAN_H_
61
62 #include <sys/cdefs.h>
63 #include <sys/_types.h>
64 #include <sys/_endian.h>
65
66 #ifndef DRIVERKIT
67 #include <libkern/_OSByteOrder.h>
68 #define __bswap16(x) __DARWIN_OSSwapInt16(x)
69 #define __bswap32(x) __DARWIN_OSSwapInt32(x)
70 #define __bswap64(x) __DARWIN_OSSwapInt64(x)
71 #else /* DRIVERKIT */
72 #define __bswap16(x) __builtin_bswap16(x)
73 #define __bswap32(x) __builtin_bswap32(x)
74 #define __bswap64(x) __builtin_bswap64(x)
75 #endif /* DRIVERKIT */
76
77 /*
78 * General byte order swapping functions.
79 */
80 #define bswap16(x) __bswap16(x)
81 #define bswap32(x) __bswap32(x)
82 #define bswap64(x) __bswap64(x)
83
84 /*
85 * Macros to convert to a specific endianness.
86 */
87 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
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 #else /* __DARWIN_BYTE_ORDER != __DARWIN_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 /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
116
117 /*
118 * Routines to encode/decode big- and little-endian multi-octet values
119 * to/from an octet stream.
120 */
121 static __inline __uint16_t
be16dec(const void * __sized_by (sizeof (__uint16_t))pp)122 be16dec(const void *__sized_by(sizeof(__uint16_t)) pp)
123 {
124 const __uint8_t *p = (const __uint8_t *)pp;
125
126 return (__uint16_t)(p[0] << 8) | p[1];
127 }
128
129 static __inline __uint32_t
be32dec(const void * __sized_by (sizeof (__uint32_t))pp)130 be32dec(const void *__sized_by(sizeof(__uint32_t)) pp)
131 {
132 const __uint8_t *p = (const __uint8_t *)pp;
133
134 return ((__uint32_t)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
135 }
136
137 static __inline __uint64_t
be64dec(const void * __sized_by (sizeof (__uint64_t))pp)138 be64dec(const void *__sized_by(sizeof(__uint64_t)) pp)
139 {
140 const __uint8_t *p = (const __uint8_t *)pp;
141
142 return ((__uint64_t)be32dec(p) << 32) | be32dec(p + 4);
143 }
144
145 static __inline __uint16_t
le16dec(const void * __sized_by (sizeof (__uint16_t))pp)146 le16dec(const void *__sized_by(sizeof(__uint16_t)) pp)
147 {
148 const __uint8_t *p = (const __uint8_t *)pp;
149
150 return (__uint16_t)(p[1] << 8) | p[0];
151 }
152
153 static __inline __uint32_t
le32dec(const void * __sized_by (sizeof (__uint32_t))pp)154 le32dec(const void *__sized_by(sizeof(__uint32_t)) pp)
155 {
156 const __uint8_t *p = (const __uint8_t *)pp;
157
158 return ((__uint32_t)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
159 }
160
161 static __inline __uint64_t
le64dec(const void * __sized_by (sizeof (__uint64_t))pp)162 le64dec(const void *__sized_by(sizeof(__uint64_t)) pp)
163 {
164 const __uint8_t *p = (const __uint8_t *)pp;
165
166 return ((__uint64_t)le32dec(p + 4) << 32) | le32dec(p);
167 }
168
169 static __inline void
be16enc(void * __sized_by (sizeof (__uint16_t))pp,__uint16_t u)170 be16enc(void *__sized_by(sizeof(__uint16_t)) 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 * __sized_by (sizeof (__uint32_t))pp,__uint32_t u)179 be32enc(void *__sized_by(sizeof(__uint32_t)) 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 * __sized_by (sizeof (__uint64_t))pp,__uint64_t u)190 be64enc(void *__sized_by(sizeof(__uint64_t)) 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 * __sized_by (sizeof (__uint16_t))pp,__uint16_t u)199 le16enc(void *__sized_by(sizeof(__uint16_t)) 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 * __sized_by (sizeof (__uint32_t))pp,__uint32_t u)208 le32enc(void *__sized_by(sizeof(__uint32_t)) 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 * __sized_by (sizeof (__uint64_t))pp,__uint64_t u)219 le64enc(void *__sized_by(sizeof(__uint64_t)) 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 /* _SYS_ENDIAN_H_ */
228