]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/_endian.h
xnu-792.tar.gz
[apple/xnu.git] / bsd / sys / _endian.h
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 /*
24 * Copyright (c) 1995 NeXT Computer, Inc. All rights reserved.
25 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
26 *
27 * @APPLE_LICENSE_HEADER_START@
28 *
29 * The contents of this file constitute Original Code as defined in and
30 * are subject to the Apple Public Source License Version 1.1 (the
31 * "License"). You may not use this file except in compliance with the
32 * License. Please obtain a copy of the License at
33 * http://www.apple.com/publicsource and read it before using this file.
34 *
35 * This Original Code and all software distributed under the License are
36 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
37 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
38 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
40 * License for the specific language governing rights and limitations
41 * under the License.
42 *
43 * @APPLE_LICENSE_HEADER_END@
44 */
45 /*
46 * Copyright (c) 1987, 1991, 1993
47 * The Regents of the University of California. All rights reserved.
48 *
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
51 * are met:
52 * 1. Redistributions of source code must retain the above copyright
53 * notice, this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright
55 * notice, this list of conditions and the following disclaimer in the
56 * documentation and/or other materials provided with the distribution.
57 * 3. All advertising materials mentioning features or use of this software
58 * must display the following acknowledgement:
59 * This product includes software developed by the University of
60 * California, Berkeley and its contributors.
61 * 4. Neither the name of the University nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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 #ifndef _SYS__ENDIAN_H_
79 #define _SYS__ENDIAN_H_
80
81 #include <sys/cdefs.h>
82
83 /*
84 * Macros for network/external number representation conversion.
85 */
86 #if __DARWIN_BYTE_ORDER == __DARWIN_BIG_ENDIAN && !defined(lint)
87 #define ntohl(x) (x)
88 #define ntohs(x) (x)
89 #define htonl(x) (x)
90 #define htons(x) (x)
91
92 #if defined(KERNEL) || !defined(_POSIX_C_SOURCE)
93 #define NTOHL(x) (x)
94 #define NTOHS(x) (x)
95 #define HTONL(x) (x)
96 #define HTONS(x) (x)
97 #endif /* defined(KERNEL) || !defined(_POSIX_C_SOURCE) */
98
99 #else
100
101 #if !defined(__ASSEMBLER__)
102
103 #include <stdint.h>
104 #include <machine/byte_order.h>
105
106 __BEGIN_DECLS
107 uint16_t ntohs(uint16_t);
108 uint16_t htons(uint16_t);
109 uint32_t ntohl(uint32_t);
110 uint32_t htonl(uint32_t);
111 __END_DECLS
112 #endif /* !defined(__ASSEMBLER__) */
113
114 #define ntohs(x) NXSwapBigShortToHost(x)
115 #define htons(x) NXSwapHostShortToBig(x)
116
117 #if defined(__LP64__)
118 #define ntohl(x) NXSwapBigIntToHost(x)
119 #define htonl(x) NXSwapHostIntToBig(x)
120 #else
121 #define ntohl(x) NXSwapBigLongToHost(x)
122 #define htonl(x) NXSwapHostLongToBig(x)
123 #endif /* defined(__LP64__) */
124
125 #if defined(KERNEL) || !defined(_POSIX_C_SOURCE)
126 #define NTOHL(x) (x) = ntohl((u_long)x)
127 #define NTOHS(x) (x) = ntohs((u_short)x)
128 #define HTONL(x) (x) = htonl((u_long)x)
129 #define HTONS(x) (x) = htons((u_short)x)
130 #endif /* defined(KERNEL) || !defined(_POSIX_C_SOURCE) */
131 #endif /* __DARWIN_BYTE_ORDER != __DARWIN_BIG_ENDIAN || defined(lint) */
132 #endif /* !_SYS__ENDIAN_H_ */