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