1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy *
21eda14cbcSMatt Macy * $FreeBSD$
22eda14cbcSMatt Macy */
23eda14cbcSMatt Macy
24eda14cbcSMatt Macy /*
25eda14cbcSMatt Macy * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26eda14cbcSMatt Macy * Use is subject to license terms.
27eda14cbcSMatt Macy */
28eda14cbcSMatt Macy
29eda14cbcSMatt Macy /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
30eda14cbcSMatt Macy /* All Rights Reserved */
31eda14cbcSMatt Macy
32eda14cbcSMatt Macy /*
33eda14cbcSMatt Macy * University Copyright- Copyright (c) 1982, 1986, 1988
34eda14cbcSMatt Macy * The Regents of the University of California
35eda14cbcSMatt Macy * All Rights Reserved
36eda14cbcSMatt Macy *
37eda14cbcSMatt Macy * University Acknowledgment- Portions of this document are derived from
38eda14cbcSMatt Macy * software developed by the University of California, Berkeley, and its
39eda14cbcSMatt Macy * contributors.
40eda14cbcSMatt Macy */
41eda14cbcSMatt Macy
42eda14cbcSMatt Macy #ifndef _OPENSOLARIS_SYS_BYTEORDER_H_
43eda14cbcSMatt Macy #define _OPENSOLARIS_SYS_BYTEORDER_H_
44eda14cbcSMatt Macy
45eda14cbcSMatt Macy #include <sys/endian.h>
46eda14cbcSMatt Macy
47be181ee2SMartin Matuska #ifdef __COVERITY__
48be181ee2SMartin Matuska /*
49be181ee2SMartin Matuska * Coverity's taint warnings from byteswapping are false positives for us.
50be181ee2SMartin Matuska * Suppress them by hiding byteswapping from Coverity.
51be181ee2SMartin Matuska */
52be181ee2SMartin Matuska #define BSWAP_8(x) ((x) & 0xff)
53be181ee2SMartin Matuska #define BSWAP_16(x) ((x) & 0xffff)
54be181ee2SMartin Matuska #define BSWAP_32(x) ((x) & 0xffffffff)
55be181ee2SMartin Matuska #define BSWAP_64(x) (x)
56be181ee2SMartin Matuska
57be181ee2SMartin Matuska #else /* __COVERITY__ */
58be181ee2SMartin Matuska
59eda14cbcSMatt Macy /*
60eda14cbcSMatt Macy * Macros to reverse byte order
61eda14cbcSMatt Macy */
62eda14cbcSMatt Macy #define BSWAP_8(x) ((x) & 0xff)
63eda14cbcSMatt Macy #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
64eda14cbcSMatt Macy #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
65eda14cbcSMatt Macy #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
66eda14cbcSMatt Macy
67be181ee2SMartin Matuska #endif /* __COVERITY__ */
68be181ee2SMartin Matuska
69eda14cbcSMatt Macy #define BMASK_8(x) ((x) & 0xff)
70eda14cbcSMatt Macy #define BMASK_16(x) ((x) & 0xffff)
71eda14cbcSMatt Macy #define BMASK_32(x) ((x) & 0xffffffff)
72eda14cbcSMatt Macy #define BMASK_64(x) (x)
73eda14cbcSMatt Macy
74eda14cbcSMatt Macy /*
75eda14cbcSMatt Macy * Macros to convert from a specific byte order to/from native byte order
76eda14cbcSMatt Macy */
77eda14cbcSMatt Macy #if BYTE_ORDER == _BIG_ENDIAN
78eda14cbcSMatt Macy #define BE_8(x) BMASK_8(x)
79eda14cbcSMatt Macy #define BE_16(x) BMASK_16(x)
80eda14cbcSMatt Macy #define BE_32(x) BMASK_32(x)
81eda14cbcSMatt Macy #define BE_64(x) BMASK_64(x)
82eda14cbcSMatt Macy #define LE_8(x) BSWAP_8(x)
83eda14cbcSMatt Macy #define LE_16(x) BSWAP_16(x)
84eda14cbcSMatt Macy #define LE_32(x) BSWAP_32(x)
85eda14cbcSMatt Macy #define LE_64(x) BSWAP_64(x)
86eda14cbcSMatt Macy #else
87eda14cbcSMatt Macy #define LE_8(x) BMASK_8(x)
88eda14cbcSMatt Macy #define LE_16(x) BMASK_16(x)
89eda14cbcSMatt Macy #define LE_32(x) BMASK_32(x)
90eda14cbcSMatt Macy #define LE_64(x) BMASK_64(x)
91eda14cbcSMatt Macy #define BE_8(x) BSWAP_8(x)
92eda14cbcSMatt Macy #define BE_16(x) BSWAP_16(x)
93eda14cbcSMatt Macy #define BE_32(x) BSWAP_32(x)
94eda14cbcSMatt Macy #define BE_64(x) BSWAP_64(x)
95eda14cbcSMatt Macy #endif
96eda14cbcSMatt Macy
972fec3ae8SWarner Losh #if !defined(_STANDALONE)
98eda14cbcSMatt Macy #if BYTE_ORDER == _BIG_ENDIAN
99eda14cbcSMatt Macy #define htonll(x) BMASK_64(x)
100eda14cbcSMatt Macy #define ntohll(x) BMASK_64(x)
1012fec3ae8SWarner Losh #else /* BYTE_ORDER == _LITTLE_ENDIAN */
102eda14cbcSMatt Macy #ifndef __LP64__
103eda14cbcSMatt Macy static __inline__ uint64_t
htonll(uint64_t n)104eda14cbcSMatt Macy htonll(uint64_t n)
105eda14cbcSMatt Macy {
106eda14cbcSMatt Macy return ((((uint64_t)htonl(n)) << 32) + htonl(n >> 32));
107eda14cbcSMatt Macy }
108eda14cbcSMatt Macy
109eda14cbcSMatt Macy static __inline__ uint64_t
ntohll(uint64_t n)110eda14cbcSMatt Macy ntohll(uint64_t n)
111eda14cbcSMatt Macy {
112eda14cbcSMatt Macy return ((((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32));
113eda14cbcSMatt Macy }
1142fec3ae8SWarner Losh #else /* !__LP64__ */
115eda14cbcSMatt Macy #define htonll(x) BSWAP_64(x)
116eda14cbcSMatt Macy #define ntohll(x) BSWAP_64(x)
1172fec3ae8SWarner Losh #endif /* __LP64__ */
1182fec3ae8SWarner Losh #endif /* BYTE_ORDER */
1192fec3ae8SWarner Losh #endif /* _STANDALONE */
120eda14cbcSMatt Macy
121eda14cbcSMatt Macy #define BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa)))
122eda14cbcSMatt Macy
123eda14cbcSMatt Macy #endif /* _OPENSOLARIS_SYS_BYTEORDER_H_ */
124