2 * Copyright © 2011 Guillem Jover <guillem@hadrons.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include_next <endian.h>
33 #ifndef LIBBSD_SYS_ENDIAN_H
34 #define LIBBSD_SYS_ENDIAN_H
37 #define _BYTE_ORDER __BYTE_ORDER
40 #ifndef _LITTLE_ENDIAN
41 #define _LITTLE_ENDIAN __LITTLE_ENDIAN
45 #define _BIG_ENDIAN __BIG_ENDIAN
49 #define _PDP_ENDIAN __PDP_ENDIAN
53 * Copyright © 2002 Thomas Moestl <tmm@FreeBSD.org>
54 * All rights reserved.
56 * Redistribution and use in source and binary forms, with or without
57 * modification, are permitted provided that the following conditions
59 * 1. Redistributions of source code must retain the above copyright
60 * notice, this list of conditions and the following disclaimer.
61 * 2. Redistributions in binary form must reproduce the above copyright
62 * notice, this list of conditions and the following disclaimer in the
63 * documentation and/or other materials provided with the distribution.
65 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
82 static __inline uint16_t
83 be16dec(const void *pp)
85 uint8_t const *p = (uint8_t const *)pp;
87 return ((p[0] << 8) | p[1]);
90 static __inline uint32_t
91 be32dec(const void *pp)
93 uint8_t const *p = (uint8_t const *)pp;
95 return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
98 static __inline uint64_t
99 be64dec(const void *pp)
101 uint8_t const *p = (uint8_t const *)pp;
103 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
106 static __inline uint16_t
107 le16dec(const void *pp)
109 uint8_t const *p = (uint8_t const *)pp;
111 return ((p[1] << 8) | p[0]);
114 static __inline uint32_t
115 le32dec(const void *pp)
117 uint8_t const *p = (uint8_t const *)pp;
119 return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
122 static __inline uint64_t
123 le64dec(const void *pp)
125 uint8_t const *p = (uint8_t const *)pp;
127 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
131 be16enc(void *pp, uint16_t u)
133 uint8_t *p = (uint8_t *)pp;
135 p[0] = (u >> 8) & 0xff;
140 be32enc(void *pp, uint32_t u)
142 uint8_t *p = (uint8_t *)pp;
144 p[0] = (u >> 24) & 0xff;
145 p[1] = (u >> 16) & 0xff;
146 p[2] = (u >> 8) & 0xff;
151 be64enc(void *pp, uint64_t u)
153 uint8_t *p = (uint8_t *)pp;
155 be32enc(p, (uint32_t)(u >> 32));
156 be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
160 le16enc(void *pp, uint16_t u)
162 uint8_t *p = (uint8_t *)pp;
165 p[1] = (u >> 8) & 0xff;
169 le32enc(void *pp, uint32_t u)
171 uint8_t *p = (uint8_t *)pp;
174 p[1] = (u >> 8) & 0xff;
175 p[2] = (u >> 16) & 0xff;
176 p[3] = (u >> 24) & 0xff;
180 le64enc(void *pp, uint64_t u)
182 uint8_t *p = (uint8_t *)pp;
184 le32enc(p, (uint32_t)(u & 0xffffffffU));
185 le32enc(p + 4, (uint32_t)(u >> 32));