blob: dfe013fa40c35565f25a0f7af6e8e8cea849042f [file] [log] [blame]
Vishal Bhoj82c80712015-12-15 21:13:33 +05301/** @file
2 Byte order related definitions and declarations.
3
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License that accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Copyright (c) 1990, 1993
14 The Regents of the University of California. All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
19 - Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 - Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 - Neither the name of the University nor the names of its contributors
25 may be used to endorse or promote products derived from this software
26 without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE.
39
40 NetBSD: endian.h,v 1.24 2006/05/05 15:08:11 christos Exp
41 endian.h 8.1 (Berkeley) 6/11/93
42**/
43#ifndef _SYS_ENDIAN_H_
44#define _SYS_ENDIAN_H_
45
46#include <sys/EfiCdefs.h>
47
48/*
49 * Definitions for byte order, according to byte significance from low
50 * address to high.
51 */
52#define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
53#define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
54#define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
55
56
57#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
58#ifndef _LOCORE
59
60/* C-family endian-ness definitions */
61
62#include <sys/ansi.h>
63#include <sys/types.h>
64
65#ifndef in_addr_t
66typedef __in_addr_t in_addr_t;
67#define in_addr_t __in_addr_t
68#endif
69
70#ifndef in_port_t
71typedef __in_port_t in_port_t;
72#define in_port_t __in_port_t
73#endif
74
75__BEGIN_DECLS
76uint32_t htonl(uint32_t) __attribute__((__const__));
77uint16_t htons(uint16_t) __attribute__((__const__));
78uint32_t ntohl(uint32_t) __attribute__((__const__));
79uint16_t ntohs(uint16_t) __attribute__((__const__));
80__END_DECLS
81
82#endif /* !_LOCORE */
83#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
84
85
86#include <machine/endian_machdep.h>
87
88/*
89 * Define the order of 32-bit words in 64-bit words.
90 */
91#if _BYTE_ORDER == _LITTLE_ENDIAN
92#define _QUAD_HIGHWORD 1
93#define _QUAD_LOWWORD 0
94#endif
95
96#if _BYTE_ORDER == _BIG_ENDIAN
97#define _QUAD_HIGHWORD 0
98#define _QUAD_LOWWORD 1
99#endif
100
101
102#if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
103/*
104 * Traditional names for byteorder. These are defined as the numeric
105 * sequences so that third party code can "#define XXX_ENDIAN" and not
106 * cause errors.
107 */
108#define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
109#define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
110#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
111#define BYTE_ORDER _BYTE_ORDER
112
113#ifndef _LOCORE
114
115#include <machine/bswap.h>
116
117/*
118 * Macros for network/external number representation conversion.
119 */
120#if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
121#define ntohl(x) (x)
122#define ntohs(x) (x)
123#define htonl(x) (x)
124#define htons(x) (x)
125
126#define NTOHL(x) (void) (x)
127#define NTOHS(x) (void) (x)
128#define HTONL(x) (void) (x)
129#define HTONS(x) (void) (x)
130
131#else /* LITTLE_ENDIAN || !defined(__lint__) */
132
133#define ntohl(x) bswap32((uint32_t)(x))
134#define ntohs(x) bswap16((uint16_t)(x))
135#define htonl(x) bswap32((uint32_t)(x))
136#define htons(x) bswap16((uint16_t)(x))
137
138#define NTOHL(x) (x) = ntohl((uint32_t)(x))
139#define NTOHS(x) (x) = ntohs((uint16_t)(x))
140#define HTONL(x) (x) = htonl((uint32_t)(x))
141#define HTONS(x) (x) = htons((uint16_t)(x))
142#endif /* LITTLE_ENDIAN || !defined(__lint__) */
143
144/*
145 * Macros to convert to a specific endianness.
146 */
147
148#if BYTE_ORDER == BIG_ENDIAN
149
150#define htobe16(x) (x)
151#define htobe32(x) (x)
152#define htobe64(x) (x)
153#define htole16(x) bswap16((uint16_t)(x))
154#define htole32(x) bswap32((uint32_t)(x))
155#define htole64(x) bswap64((uint64_t)(x))
156
157#define HTOBE16(x) (void) (x)
158#define HTOBE32(x) (void) (x)
159#define HTOBE64(x) (void) (x)
160#define HTOLE16(x) (x) = bswap16((uint16_t)(x))
161#define HTOLE32(x) (x) = bswap32((uint32_t)(x))
162#define HTOLE64(x) (x) = bswap64((uint64_t)(x))
163
164#else /* LITTLE_ENDIAN */
165
166#define htobe16(x) bswap16((uint16_t)(x))
167#define htobe32(x) bswap32((uint32_t)(x))
168#define htobe64(x) bswap64((uint64_t)(x))
169#define htole16(x) (x)
170#define htole32(x) (x)
171#define htole64(x) (x)
172
173#define HTOBE16(x) (x) = bswap16((uint16_t)(x))
174#define HTOBE32(x) (x) = bswap32((uint32_t)(x))
175#define HTOBE64(x) (x) = bswap64((uint64_t)(x))
176#define HTOLE16(x) (void) (x)
177#define HTOLE32(x) (void) (x)
178#define HTOLE64(x) (void) (x)
179
180#endif /* LITTLE_ENDIAN */
181
182#define be16toh(x) htobe16(x)
183#define be32toh(x) htobe32(x)
184#define be64toh(x) htobe64(x)
185#define le16toh(x) htole16(x)
186#define le32toh(x) htole32(x)
187#define le64toh(x) htole64(x)
188
189#define BE16TOH(x) HTOBE16(x)
190#define BE32TOH(x) HTOBE32(x)
191#define BE64TOH(x) HTOBE64(x)
192#define LE16TOH(x) HTOLE16(x)
193#define LE32TOH(x) HTOLE32(x)
194#define LE64TOH(x) HTOLE64(x)
195
196/*
197 * Routines to encode/decode big- and little-endian multi-octet values
198 * to/from an octet stream.
199 */
200
201static __inline void __unused
202be16enc(void *buf, uint16_t u)
203{
204 uint8_t *p = (uint8_t *)buf;
205
206 p[0] = (uint8_t)(((unsigned)u >> 8) & 0xff);
207 p[1] = (uint8_t)(u & 0xff);
208}
209
210static __inline void __unused
211le16enc(void *buf, uint16_t u)
212{
213 uint8_t *p = (uint8_t *)buf;
214
215 p[0] = (uint8_t)(u & 0xff);
216 p[1] = (uint8_t)(((unsigned)u >> 8) & 0xff);
217}
218
219static __inline uint16_t __unused
220be16dec(const void *buf)
221{
222 const uint8_t *p = (const uint8_t *)buf;
223
224 return ((p[0] << 8) | p[1]);
225}
226
227static __inline uint16_t __unused
228le16dec(const void *buf)
229{
230 const uint8_t *p = (const uint8_t *)buf;
231
232 return ((p[1] << 8) | p[0]);
233}
234
235static __inline void __unused
236be32enc(void *buf, uint32_t u)
237{
238 uint8_t *p = (uint8_t *)buf;
239
240 p[0] = (uint8_t)((u >> 24) & 0xff);
241 p[1] = (uint8_t)((u >> 16) & 0xff);
242 p[2] = (uint8_t)((u >> 8) & 0xff);
243 p[3] = (uint8_t)(u & 0xff);
244}
245
246static __inline void __unused
247le32enc(void *buf, uint32_t u)
248{
249 uint8_t *p = (uint8_t *)buf;
250
251 p[0] = (uint8_t)(u & 0xff);
252 p[1] = (uint8_t)((u >> 8) & 0xff);
253 p[2] = (uint8_t)((u >> 16) & 0xff);
254 p[3] = (uint8_t)((u >> 24) & 0xff);
255}
256
257static __inline uint32_t __unused
258be32dec(const void *buf)
259{
260 const uint8_t *p = (const uint8_t *)buf;
261
262 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
263}
264
265static __inline uint32_t __unused
266le32dec(const void *buf)
267{
268 const uint8_t *p = (const uint8_t *)buf;
269
270 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
271}
272
273static __inline void __unused
274be64enc(void *buf, uint64_t u)
275{
276 uint8_t *p = (uint8_t *)buf;
277
278 be32enc(p, (uint32_t)(u >> 32));
279 be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
280}
281
282static __inline void __unused
283le64enc(void *buf, uint64_t u)
284{
285 uint8_t *p = (uint8_t *)buf;
286
287 le32enc(p, (uint32_t)(u & 0xffffffffULL));
288 le32enc(p + 4, (uint32_t)(u >> 32));
289}
290
291static __inline uint64_t __unused
292be64dec(const void *buf)
293{
294 const uint8_t *p = (const uint8_t *)buf;
295
296 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
297}
298
299static __inline uint64_t __unused
300le64dec(const void *buf)
301{
302 const uint8_t *p = (const uint8_t *)buf;
303
304 return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
305}
306
307#endif /* !_LOCORE */
308#endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
309#endif /* !_SYS_ENDIAN_H_ */