1 /*
2  * All the code below is a rework of
3  * https://github.com/freebsd/freebsd/blob/master/sys/sys/endian.h
4  * to import symbols defining non-standard endian handling functions.
5  * The aforementioned source code license terms are included here.
6  * For further license info, please look at https://github.com/freebsd/freebsd
7  */
8 
9 /*-
10  * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
11  * SPDX-FileCopyrightText: 2020 Francesco Giancane <francesco.giancane@accenture.com>
12  * SPDX-FileCopyrightText: 2002 Thomas Moestl <tmm@FreeBSD.org>
13  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND Apache-2.0
14  *
15  * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * $FreeBSD$
40  */
41 
42 #pragma once
43 
44 #include <stdint.h>
45 
46 /*
47  * This is a compatibility header for <endian.h>.
48  * In xtensa-newlib distribution it is located in <machine/endian.h>
49  * but most program expect to be plain <endian.h>.
50  */
51 #include <machine/endian.h>
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 /*
58  * General byte order swapping functions.
59  */
60 #define	bswap16(x)	__bswap16(x)
61 #define	bswap32(x)	__bswap32(x)
62 #define	bswap64(x)	__bswap64(x)
63 
64 /*
65  * Host to big endian, host to little endian, big endian to host, and little
66  * endian to host byte order functions as detailed in byteorder(9).
67  */
68 #if _BYTE_ORDER == _LITTLE_ENDIAN
69 #define	htobe16(x)	bswap16((x))
70 #define	htobe32(x)	bswap32((x))
71 #define	htobe64(x)	bswap64((x))
72 #define	htole16(x)	((uint16_t)(x))
73 #define	htole32(x)	((uint32_t)(x))
74 #define	htole64(x)	((uint64_t)(x))
75 
76 #define	be16toh(x)	bswap16((x))
77 #define	be32toh(x)	bswap32((x))
78 #define	be64toh(x)	bswap64((x))
79 #define	le16toh(x)	((uint16_t)(x))
80 #define	le32toh(x)	((uint32_t)(x))
81 #define	le64toh(x)	((uint64_t)(x))
82 #else /* _BYTE_ORDER != _LITTLE_ENDIAN */
83 #define	htobe16(x)	((uint16_t)(x))
84 #define	htobe32(x)	((uint32_t)(x))
85 #define	htobe64(x)	((uint64_t)(x))
86 #define	htole16(x)	bswap16((x))
87 #define	htole32(x)	bswap32((x))
88 #define	htole64(x)	bswap64((x))
89 
90 #define	be16toh(x)	((uint16_t)(x))
91 #define	be32toh(x)	((uint32_t)(x))
92 #define	be64toh(x)	((uint64_t)(x))
93 #define	le16toh(x)	bswap16((x))
94 #define	le32toh(x)	bswap32((x))
95 #define	le64toh(x)	bswap64((x))
96 #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
97 
98 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
99 
100 static __inline uint16_t
be16dec(const void * pp)101 be16dec(const void *pp)
102 {
103 	uint8_t const *p = (uint8_t const *)pp;
104 
105 	return ((p[0] << 8) | p[1]);
106 }
107 
108 static __inline uint32_t
be32dec(const void * pp)109 be32dec(const void *pp)
110 {
111 	uint8_t const *p = (uint8_t const *)pp;
112 
113 	return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
114 }
115 
116 static __inline uint64_t
be64dec(const void * pp)117 be64dec(const void *pp)
118 {
119 	uint8_t const *p = (uint8_t const *)pp;
120 
121 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
122 }
123 
124 static __inline uint16_t
le16dec(const void * pp)125 le16dec(const void *pp)
126 {
127 	uint8_t const *p = (uint8_t const *)pp;
128 
129 	return ((p[1] << 8) | p[0]);
130 }
131 
132 static __inline uint32_t
le32dec(const void * pp)133 le32dec(const void *pp)
134 {
135 	uint8_t const *p = (uint8_t const *)pp;
136 
137 	return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
138 }
139 
140 static __inline uint64_t
le64dec(const void * pp)141 le64dec(const void *pp)
142 {
143 	uint8_t const *p = (uint8_t const *)pp;
144 
145 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
146 }
147 
148 static __inline void
be16enc(void * pp,uint16_t u)149 be16enc(void *pp, uint16_t u)
150 {
151 	uint8_t *p = (uint8_t *)pp;
152 
153 	p[0] = (u >> 8) & 0xff;
154 	p[1] = u & 0xff;
155 }
156 
157 static __inline void
be32enc(void * pp,uint32_t u)158 be32enc(void *pp, uint32_t u)
159 {
160 	uint8_t *p = (uint8_t *)pp;
161 
162 	p[0] = (u >> 24) & 0xff;
163 	p[1] = (u >> 16) & 0xff;
164 	p[2] = (u >> 8) & 0xff;
165 	p[3] = u & 0xff;
166 }
167 
168 static __inline void
be64enc(void * pp,uint64_t u)169 be64enc(void *pp, uint64_t u)
170 {
171 	uint8_t *p = (uint8_t *)pp;
172 
173 	be32enc(p, (uint32_t)(u >> 32));
174 	be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
175 }
176 
177 static __inline void
le16enc(void * pp,uint16_t u)178 le16enc(void *pp, uint16_t u)
179 {
180 	uint8_t *p = (uint8_t *)pp;
181 
182 	p[0] = u & 0xff;
183 	p[1] = (u >> 8) & 0xff;
184 }
185 
186 static __inline void
le32enc(void * pp,uint32_t u)187 le32enc(void *pp, uint32_t u)
188 {
189 	uint8_t *p = (uint8_t *)pp;
190 
191 	p[0] = u & 0xff;
192 	p[1] = (u >> 8) & 0xff;
193 	p[2] = (u >> 16) & 0xff;
194 	p[3] = (u >> 24) & 0xff;
195 }
196 
197 static __inline void
le64enc(void * pp,uint64_t u)198 le64enc(void *pp, uint64_t u)
199 {
200 	uint8_t *p = (uint8_t *)pp;
201 
202 	le32enc(p, (uint32_t)(u & 0xffffffffU));
203 	le32enc(p + 4, (uint32_t)(u >> 32));
204 }
205 
206 #ifdef __cplusplus
207 }
208 #endif
209