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  * $FreeBSD$
37  */
38 #pragma once
39 
40 #include <machine/endian.h>
41 
42 #include <stdint.h>
43 
44 /*
45  * General byte order swapping functions.
46  */
47 #define bswap16(x) __builtin_bswap16(x)
48 #define bswap32(x) __builtin_bswap32(x)
49 #define bswap64(x) __builtin_bswap64(x)
50 
51 #define _BYTE_ORDER __DARWIN_BYTE_ORDER
52 #define _LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
53 #define _BIG_ENDIAN __DARWIN_BIG_ENDIAN
54 /*
55  * Host to big endian, host to little endian, big endian to host, and little
56  * endian to host byte order functions as detailed in byteorder(9).
57  */
58 #if _BYTE_ORDER == _LITTLE_ENDIAN
59 #define htobe16(x) bswap16((x))
60 #define htobe32(x) bswap32((x))
61 #define htobe64(x) bswap64((x))
62 #define htole16(x) ((uint16_t)(x))
63 #define htole32(x) ((uint32_t)(x))
64 #define htole64(x) ((uint64_t)(x))
65 
66 #define be16toh(x) bswap16((x))
67 #define be32toh(x) bswap32((x))
68 #define be64toh(x) bswap64((x))
69 #define le16toh(x) ((uint16_t)(x))
70 #define le32toh(x) ((uint32_t)(x))
71 #define le64toh(x) ((uint64_t)(x))
72 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
73 #define htobe16(x) ((uint16_t)(x))
74 #define htobe32(x) ((uint32_t)(x))
75 #define htobe64(x) ((uint64_t)(x))
76 #define htole16(x) bswap16((x))
77 #define htole32(x) bswap32((x))
78 #define htole64(x) bswap64((x))
79 
80 #define be16toh(x) ((uint16_t)(x))
81 #define be32toh(x) ((uint32_t)(x))
82 #define be64toh(x) ((uint64_t)(x))
83 #define le16toh(x) bswap16((x))
84 #define le32toh(x) bswap32((x))
85 #define le64toh(x) bswap64((x))
86 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
87 
88 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
89 
90 static __inline uint16_t
91 be16dec(const void *pp)
92 {
93 	uint8_t const *p = (uint8_t const *)pp;
94 
95 	return ((p[0] << 8) | p[1]);
96 }
97 
98 static __inline uint32_t
99 be32dec(const void *pp)
100 {
101 	uint8_t const *p = (uint8_t const *)pp;
102 
103 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
104 }
105 
106 static __inline uint64_t
107 be64dec(const void *pp)
108 {
109 	uint8_t const *p = (uint8_t const *)pp;
110 
111 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
112 }
113 
114 static __inline uint16_t
115 le16dec(const void *pp)
116 {
117 	uint8_t const *p = (uint8_t const *)pp;
118 
119 	return ((p[1] << 8) | p[0]);
120 }
121 
122 static __inline uint32_t
123 le32dec(const void *pp)
124 {
125 	uint8_t const *p = (uint8_t const *)pp;
126 
127 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
128 }
129 
130 static __inline uint64_t
131 le64dec(const void *pp)
132 {
133 	uint8_t const *p = (uint8_t const *)pp;
134 
135 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
136 }
137 
138 static __inline void
139 be16enc(void *pp, uint16_t u)
140 {
141 	uint8_t *p = (uint8_t *)pp;
142 
143 	p[0] = (u >> 8) & 0xff;
144 	p[1] = u & 0xff;
145 }
146 
147 static __inline void
148 be32enc(void *pp, uint32_t u)
149 {
150 	uint8_t *p = (uint8_t *)pp;
151 
152 	p[0] = (u >> 24) & 0xff;
153 	p[1] = (u >> 16) & 0xff;
154 	p[2] = (u >> 8) & 0xff;
155 	p[3] = u & 0xff;
156 }
157 
158 static __inline void
159 be64enc(void *pp, uint64_t u)
160 {
161 	uint8_t *p = (uint8_t *)pp;
162 
163 	be32enc(p, (uint32_t)(u >> 32));
164 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
165 }
166 
167 static __inline void
168 le16enc(void *pp, uint16_t u)
169 {
170 	uint8_t *p = (uint8_t *)pp;
171 
172 	p[0] = u & 0xff;
173 	p[1] = (u >> 8) & 0xff;
174 }
175 
176 static __inline void
177 le32enc(void *pp, uint32_t u)
178 {
179 	uint8_t *p = (uint8_t *)pp;
180 
181 	p[0] = u & 0xff;
182 	p[1] = (u >> 8) & 0xff;
183 	p[2] = (u >> 16) & 0xff;
184 	p[3] = (u >> 24) & 0xff;
185 }
186 
187 static __inline void
188 le64enc(void *pp, uint64_t u)
189 {
190 	uint8_t *p = (uint8_t *)pp;
191 
192 	le32enc(p, (uint32_t)(u & 0xffffffffU));
193 	le32enc(p + 4, (uint32_t)(u >> 32));
194 }
195