1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 2018-2020 Alex Richardson <arichardson@FreeBSD.org>
5 *
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory (Department of Computer Science and
8 * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
9 * DARPA SSITH research programme.
10 *
11 * This software was developed by SRI International and the University of
12 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
13 * ("CTSRD"), as part of the DARPA CRASH research programme.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36 #pragma once
37
38 #include <machine/endian.h>
39
40 #include <stdint.h>
41
42 /*
43 * General byte order swapping functions.
44 */
45 #define bswap16(x) __builtin_bswap16(x)
46 #define bswap32(x) __builtin_bswap32(x)
47 #define bswap64(x) __builtin_bswap64(x)
48
49 #define _BYTE_ORDER __DARWIN_BYTE_ORDER
50 #define _LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
51 #define _BIG_ENDIAN __DARWIN_BIG_ENDIAN
52 /*
53 * Host to big endian, host to little endian, big endian to host, and little
54 * endian to host byte order functions as detailed in byteorder(9).
55 */
56 #if _BYTE_ORDER == _LITTLE_ENDIAN
57 #define htobe16(x) bswap16((x))
58 #define htobe32(x) bswap32((x))
59 #define htobe64(x) bswap64((x))
60 #define htole16(x) ((uint16_t)(x))
61 #define htole32(x) ((uint32_t)(x))
62 #define htole64(x) ((uint64_t)(x))
63
64 #define be16toh(x) bswap16((x))
65 #define be32toh(x) bswap32((x))
66 #define be64toh(x) bswap64((x))
67 #define le16toh(x) ((uint16_t)(x))
68 #define le32toh(x) ((uint32_t)(x))
69 #define le64toh(x) ((uint64_t)(x))
70 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
71 #define htobe16(x) ((uint16_t)(x))
72 #define htobe32(x) ((uint32_t)(x))
73 #define htobe64(x) ((uint64_t)(x))
74 #define htole16(x) bswap16((x))
75 #define htole32(x) bswap32((x))
76 #define htole64(x) bswap64((x))
77
78 #define be16toh(x) ((uint16_t)(x))
79 #define be32toh(x) ((uint32_t)(x))
80 #define be64toh(x) ((uint64_t)(x))
81 #define le16toh(x) bswap16((x))
82 #define le32toh(x) bswap32((x))
83 #define le64toh(x) bswap64((x))
84 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
85
86 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
87
88 static __inline uint16_t
be16dec(const void * pp)89 be16dec(const void *pp)
90 {
91 uint8_t const *p = (uint8_t const *)pp;
92
93 return ((p[0] << 8) | p[1]);
94 }
95
96 static __inline uint32_t
be32dec(const void * pp)97 be32dec(const void *pp)
98 {
99 uint8_t const *p = (uint8_t const *)pp;
100
101 return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
102 }
103
104 static __inline uint64_t
be64dec(const void * pp)105 be64dec(const void *pp)
106 {
107 uint8_t const *p = (uint8_t const *)pp;
108
109 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
110 }
111
112 static __inline uint16_t
le16dec(const void * pp)113 le16dec(const void *pp)
114 {
115 uint8_t const *p = (uint8_t const *)pp;
116
117 return ((p[1] << 8) | p[0]);
118 }
119
120 static __inline uint32_t
le32dec(const void * pp)121 le32dec(const void *pp)
122 {
123 uint8_t const *p = (uint8_t const *)pp;
124
125 return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
126 }
127
128 static __inline uint64_t
le64dec(const void * pp)129 le64dec(const void *pp)
130 {
131 uint8_t const *p = (uint8_t const *)pp;
132
133 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
134 }
135
136 static __inline void
be16enc(void * pp,uint16_t u)137 be16enc(void *pp, uint16_t u)
138 {
139 uint8_t *p = (uint8_t *)pp;
140
141 p[0] = (u >> 8) & 0xff;
142 p[1] = u & 0xff;
143 }
144
145 static __inline void
be32enc(void * pp,uint32_t u)146 be32enc(void *pp, uint32_t u)
147 {
148 uint8_t *p = (uint8_t *)pp;
149
150 p[0] = (u >> 24) & 0xff;
151 p[1] = (u >> 16) & 0xff;
152 p[2] = (u >> 8) & 0xff;
153 p[3] = u & 0xff;
154 }
155
156 static __inline void
be64enc(void * pp,uint64_t u)157 be64enc(void *pp, uint64_t u)
158 {
159 uint8_t *p = (uint8_t *)pp;
160
161 be32enc(p, (uint32_t)(u >> 32));
162 be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
163 }
164
165 static __inline void
le16enc(void * pp,uint16_t u)166 le16enc(void *pp, uint16_t u)
167 {
168 uint8_t *p = (uint8_t *)pp;
169
170 p[0] = u & 0xff;
171 p[1] = (u >> 8) & 0xff;
172 }
173
174 static __inline void
le32enc(void * pp,uint32_t u)175 le32enc(void *pp, uint32_t u)
176 {
177 uint8_t *p = (uint8_t *)pp;
178
179 p[0] = u & 0xff;
180 p[1] = (u >> 8) & 0xff;
181 p[2] = (u >> 16) & 0xff;
182 p[3] = (u >> 24) & 0xff;
183 }
184
185 static __inline void
le64enc(void * pp,uint64_t u)186 le64enc(void *pp, uint64_t u)
187 {
188 uint8_t *p = (uint8_t *)pp;
189
190 le32enc(p, (uint32_t)(u & 0xffffffffU));
191 le32enc(p + 4, (uint32_t)(u >> 32));
192 }
193