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 #if __has_include_next(<sys/endian.h>)
41 #include_next <sys/endian.h>
42 #endif
43 
44 #if __has_include(<endian.h>)
45 #include <endian.h>
46 #endif
47 
48 /* Linux uses a double underscore, FreeBSD a single one */
49 #define _LITTLE_ENDIAN __LITTLE_ENDIAN
50 #define _BIG_ENDIAN __BIG_ENDIAN
51 #define _BYTE_ORDER __BYTE_ORDER
52 
53 /*
54  * Ensure all these are constant expressions (which is not the case for some
55  * of the glibc versions depending on compiler optimization level)
56  */
57 
58 #undef bswap64
59 #define bswap64(a) __builtin_bswap64(a)
60 
61 #undef bswap32
62 #define bswap32(a) __builtin_bswap32(a)
63 
64 #undef bswap16
65 #define bswap16(a) __builtin_bswap16(a)
66 
67 #undef __bswap_64
68 #define __bswap_64(a) __builtin_bswap64(a)
69 
70 #undef __bswap_32
71 #define __bswap_32(a) __builtin_bswap32(a)
72 
73 #undef __bswap_16
74 #define __bswap_16(a) __builtin_bswap16(a)
75 
76 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
77 
78 static __inline uint16_t
79 be16dec(const void *pp)
80 {
81 	uint8_t const *p = (uint8_t const *)pp;
82 
83 	return ((p[0] << 8) | p[1]);
84 }
85 
86 static __inline uint32_t
87 be32dec(const void *pp)
88 {
89 	uint8_t const *p = (uint8_t const *)pp;
90 
91 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
92 }
93 
94 static __inline uint64_t
95 be64dec(const void *pp)
96 {
97 	uint8_t const *p = (uint8_t const *)pp;
98 
99 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
100 }
101 
102 static __inline uint16_t
103 le16dec(const void *pp)
104 {
105 	uint8_t const *p = (uint8_t const *)pp;
106 
107 	return ((p[1] << 8) | p[0]);
108 }
109 
110 static __inline uint32_t
111 le32dec(const void *pp)
112 {
113 	uint8_t const *p = (uint8_t const *)pp;
114 
115 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
116 }
117 
118 static __inline uint64_t
119 le64dec(const void *pp)
120 {
121 	uint8_t const *p = (uint8_t const *)pp;
122 
123 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
124 }
125 
126 static __inline void
127 be16enc(void *pp, uint16_t u)
128 {
129 	uint8_t *p = (uint8_t *)pp;
130 
131 	p[0] = (u >> 8) & 0xff;
132 	p[1] = u & 0xff;
133 }
134 
135 static __inline void
136 be32enc(void *pp, uint32_t u)
137 {
138 	uint8_t *p = (uint8_t *)pp;
139 
140 	p[0] = (u >> 24) & 0xff;
141 	p[1] = (u >> 16) & 0xff;
142 	p[2] = (u >> 8) & 0xff;
143 	p[3] = u & 0xff;
144 }
145 
146 static __inline void
147 be64enc(void *pp, uint64_t u)
148 {
149 	uint8_t *p = (uint8_t *)pp;
150 
151 	be32enc(p, (uint32_t)(u >> 32));
152 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
153 }
154 
155 static __inline void
156 le16enc(void *pp, uint16_t u)
157 {
158 	uint8_t *p = (uint8_t *)pp;
159 
160 	p[0] = u & 0xff;
161 	p[1] = (u >> 8) & 0xff;
162 }
163 
164 static __inline void
165 le32enc(void *pp, uint32_t u)
166 {
167 	uint8_t *p = (uint8_t *)pp;
168 
169 	p[0] = u & 0xff;
170 	p[1] = (u >> 8) & 0xff;
171 	p[2] = (u >> 16) & 0xff;
172 	p[3] = (u >> 24) & 0xff;
173 }
174 
175 static __inline void
176 le64enc(void *pp, uint64_t u)
177 {
178 	uint8_t *p = (uint8_t *)pp;
179 
180 	le32enc(p, (uint32_t)(u & 0xffffffffU));
181 	le32enc(p + 4, (uint32_t)(u >> 32));
182 }
183