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