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
22/*
23 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28/*	  All Rights Reserved  	*/
29
30/*
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
34 *
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
38 */
39
40#ifndef _SYS_BYTEORDER_H
41#define	_SYS_BYTEORDER_H
42
43#if defined(__GNUC__) && defined(_ASM_INLINES) && \
44	(defined(__i386) || defined(__amd64))
45#include <asm/byteorder.h>
46#endif
47
48#include <sys/isa_defs.h>
49#include <inttypes.h>
50
51#ifdef	__cplusplus
52extern "C" {
53#endif
54
55/*
56 * macros for conversion between host and (internet) network byte order
57 */
58
59#if defined(_ZFS_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
60/* big-endian */
61#define	ntohl(x)	(x)
62#define	ntohs(x)	(x)
63#define	htonl(x)	(x)
64#define	htons(x)	(x)
65
66#elif !defined(ntohl) /* little-endian */
67
68#ifndef	_IN_PORT_T
69#define	_IN_PORT_T
70typedef uint16_t in_port_t;
71#endif
72
73#ifndef	_IN_ADDR_T
74#define	_IN_ADDR_T
75typedef uint32_t in_addr_t;
76#endif
77
78#if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
79extern	uint32_t htonl(uint32_t);
80extern	uint16_t htons(uint16_t);
81extern 	uint32_t ntohl(uint32_t);
82extern	uint16_t ntohs(uint16_t);
83#else
84extern	in_addr_t htonl(in_addr_t);
85extern	in_port_t htons(in_port_t);
86extern 	in_addr_t ntohl(in_addr_t);
87extern	in_port_t ntohs(in_port_t);
88#endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
89#endif
90
91#if !defined(_XPG4_2) || defined(__EXTENSIONS__)
92
93#ifdef __COVERITY__
94/*
95 * Coverity's taint warnings from byteswapping are false positives for us.
96 * Suppress them by hiding byteswapping from Coverity.
97 */
98#define	BSWAP_8(x)	((x) & 0xff)
99#define	BSWAP_16(x)	((x) & 0xffff)
100#define	BSWAP_32(x)	((x) & 0xffffffff)
101#define	BSWAP_64(x)	(x)
102
103#else /* __COVERITY__ */
104
105/*
106 * Macros to reverse byte order
107 */
108#define	BSWAP_8(x)	((x) & 0xff)
109#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
110#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
111#define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
112
113#endif /* __COVERITY__ */
114
115#define	BMASK_8(x)	((x) & 0xff)
116#define	BMASK_16(x)	((x) & 0xffff)
117#define	BMASK_32(x)	((x) & 0xffffffff)
118#define	BMASK_64(x)	(x)
119
120/*
121 * Macros to convert from a specific byte order to/from native byte order
122 */
123#ifdef _ZFS_BIG_ENDIAN
124#define	BE_8(x)		BMASK_8(x)
125#define	BE_16(x)	BMASK_16(x)
126#define	BE_32(x)	BMASK_32(x)
127#define	BE_64(x)	BMASK_64(x)
128#define	LE_8(x)		BSWAP_8(x)
129#define	LE_16(x)	BSWAP_16(x)
130#define	LE_32(x)	BSWAP_32(x)
131#define	LE_64(x)	BSWAP_64(x)
132#else
133#define	LE_8(x)		BMASK_8(x)
134#define	LE_16(x)	BMASK_16(x)
135#define	LE_32(x)	BMASK_32(x)
136#define	LE_64(x)	BMASK_64(x)
137#define	BE_8(x)		BSWAP_8(x)
138#define	BE_16(x)	BSWAP_16(x)
139#define	BE_32(x)	BSWAP_32(x)
140#define	BE_64(x)	BSWAP_64(x)
141#endif
142
143#ifdef _ZFS_BIG_ENDIAN
144static __inline__ uint64_t
145htonll(uint64_t n)
146{
147	return (n);
148}
149
150static __inline__ uint64_t
151ntohll(uint64_t n)
152{
153	return (n);
154}
155#else
156static __inline__ uint64_t
157htonll(uint64_t n)
158{
159	return ((((uint64_t)htonl(n)) << 32) + htonl(n >> 32));
160}
161
162static __inline__ uint64_t
163ntohll(uint64_t n)
164{
165	return ((((uint64_t)ntohl(n)) << 32) + ntohl(n >> 32));
166}
167#endif
168
169/*
170 * Macros to read unaligned values from a specific byte order to
171 * native byte order
172 */
173
174#define	BE_IN8(xa) \
175	*((uint8_t *)(xa))
176
177#define	BE_IN16(xa) \
178	(((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1))
179
180#define	BE_IN32(xa) \
181	(((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2))
182
183#define	BE_IN64(xa) \
184	(((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4))
185
186#define	LE_IN8(xa) \
187	*((uint8_t *)(xa))
188
189#define	LE_IN16(xa) \
190	(((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
191
192#define	LE_IN32(xa) \
193	(((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
194
195#define	LE_IN64(xa) \
196	(((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
197
198/*
199 * Macros to write unaligned values from native byte order to a specific byte
200 * order.
201 */
202
203#define	BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
204
205#define	BE_OUT16(xa, yv) \
206	BE_OUT8((uint8_t *)(xa) + 1, yv); \
207	BE_OUT8((uint8_t *)(xa), (yv) >> 8);
208
209#define	BE_OUT32(xa, yv) \
210	BE_OUT16((uint8_t *)(xa) + 2, yv); \
211	BE_OUT16((uint8_t *)(xa), (yv) >> 16);
212
213#define	BE_OUT64(xa, yv) \
214	BE_OUT32((uint8_t *)(xa) + 4, yv); \
215	BE_OUT32((uint8_t *)(xa), (yv) >> 32);
216
217#define	LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
218
219#define	LE_OUT16(xa, yv) \
220	LE_OUT8((uint8_t *)(xa), yv); \
221	LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
222
223#define	LE_OUT32(xa, yv) \
224	LE_OUT16((uint8_t *)(xa), yv); \
225	LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
226
227#define	LE_OUT64(xa, yv) \
228	LE_OUT32((uint8_t *)(xa), yv); \
229	LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
230
231#endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
232
233#ifdef	__cplusplus
234}
235#endif
236
237#endif /* _SYS_BYTEORDER_H */
238