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