]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libbsd/lib/contrib/include/bsd/sys/endian.h
update
[l4.git] / l4 / pkg / libbsd / lib / contrib / include / bsd / sys / endian.h
1 /*
2  * Copyright © 2011 Guillem Jover <guillem@hadrons.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
14  *
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.
25  */
26
27 #ifdef LIBBSD_OVERLAY
28 #include_next <endian.h>
29 #else
30 #include <endian.h>
31 #endif
32
33 #ifndef LIBBSD_SYS_ENDIAN_H
34 #define LIBBSD_SYS_ENDIAN_H
35
36 #ifndef _BYTE_ORDER
37 #define _BYTE_ORDER __BYTE_ORDER
38 #endif
39
40 #ifndef _LITTLE_ENDIAN
41 #define _LITTLE_ENDIAN __LITTLE_ENDIAN
42 #endif
43
44 #ifndef _BIG_ENDIAN
45 #define _BIG_ENDIAN __BIG_ENDIAN
46 #endif
47
48 #ifndef _PDP_ENDIAN
49 #define _PDP_ENDIAN __PDP_ENDIAN
50 #endif
51
52 /*
53  * Copyright © 2002 Thomas Moestl <tmm@FreeBSD.org>
54  * All rights reserved.
55  *
56  * Redistribution and use in source and binary forms, with or without
57  * modification, are permitted provided that the following conditions
58  * are met:
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.
64  *
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
75  * SUCH DAMAGE.
76  */
77
78 #include <stdint.h>
79
80 /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
81
82 static __inline uint16_t
83 be16dec(const void *pp)
84 {
85         uint8_t const *p = (uint8_t const *)pp;
86
87         return ((p[0] << 8) | p[1]);
88 }
89
90 static __inline uint32_t
91 be32dec(const void *pp)
92 {
93         uint8_t const *p = (uint8_t const *)pp;
94
95         return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
96 }
97
98 static __inline uint64_t
99 be64dec(const void *pp)
100 {
101         uint8_t const *p = (uint8_t const *)pp;
102
103         return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
104 }
105
106 static __inline uint16_t
107 le16dec(const void *pp)
108 {
109         uint8_t const *p = (uint8_t const *)pp;
110
111         return ((p[1] << 8) | p[0]);
112 }
113
114 static __inline uint32_t
115 le32dec(const void *pp)
116 {
117         uint8_t const *p = (uint8_t const *)pp;
118
119         return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
120 }
121
122 static __inline uint64_t
123 le64dec(const void *pp)
124 {
125         uint8_t const *p = (uint8_t const *)pp;
126
127         return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
128 }
129
130 static __inline void
131 be16enc(void *pp, uint16_t u)
132 {
133         uint8_t *p = (uint8_t *)pp;
134
135         p[0] = (u >> 8) & 0xff;
136         p[1] = u & 0xff;
137 }
138
139 static __inline void
140 be32enc(void *pp, uint32_t u)
141 {
142         uint8_t *p = (uint8_t *)pp;
143
144         p[0] = (u >> 24) & 0xff;
145         p[1] = (u >> 16) & 0xff;
146         p[2] = (u >> 8) & 0xff;
147         p[3] = u & 0xff;
148 }
149
150 static __inline void
151 be64enc(void *pp, uint64_t u)
152 {
153         uint8_t *p = (uint8_t *)pp;
154
155         be32enc(p, (uint32_t)(u >> 32));
156         be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
157 }
158
159 static __inline void
160 le16enc(void *pp, uint16_t u)
161 {
162         uint8_t *p = (uint8_t *)pp;
163
164         p[0] = u & 0xff;
165         p[1] = (u >> 8) & 0xff;
166 }
167
168 static __inline void
169 le32enc(void *pp, uint32_t u)
170 {
171         uint8_t *p = (uint8_t *)pp;
172
173         p[0] = u & 0xff;
174         p[1] = (u >> 8) & 0xff;
175         p[2] = (u >> 16) & 0xff;
176         p[3] = (u >> 24) & 0xff;
177 }
178
179 static __inline void
180 le64enc(void *pp, uint64_t u)
181 {
182         uint8_t *p = (uint8_t *)pp;
183
184         le32enc(p, (uint32_t)(u & 0xffffffffU));
185         le32enc(p + 4, (uint32_t)(u >> 32));
186 }
187
188 #endif