xref: /onnv-gate/usr/src/uts/common/sys/byteorder.h (revision 713:c30b3a99e7aa)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*713Swesolows  * Common Development and Distribution License (the "License").
6*713Swesolows  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*713Swesolows 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #ifndef _SYS_BYTEORDER_H
410Sstevel@tonic-gate #define	_SYS_BYTEORDER_H
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/isa_defs.h>
460Sstevel@tonic-gate #include <sys/int_types.h>
470Sstevel@tonic-gate 
48*713Swesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \
49*713Swesolows 	(defined(__i386) || defined(__amd64))
500Sstevel@tonic-gate #include <asm/byteorder.h>
510Sstevel@tonic-gate #endif
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #ifdef	__cplusplus
540Sstevel@tonic-gate extern "C" {
550Sstevel@tonic-gate #endif
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * macros for conversion between host and (internet) network byte order
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
620Sstevel@tonic-gate /* big-endian */
630Sstevel@tonic-gate #define	ntohl(x)	(x)
640Sstevel@tonic-gate #define	ntohs(x)	(x)
650Sstevel@tonic-gate #define	htonl(x)	(x)
660Sstevel@tonic-gate #define	htons(x)	(x)
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #elif !defined(ntohl) /* little-endian */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #ifndef	_IN_PORT_T
710Sstevel@tonic-gate #define	_IN_PORT_T
720Sstevel@tonic-gate typedef uint16_t in_port_t;
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #ifndef	_IN_ADDR_T
760Sstevel@tonic-gate #define	_IN_ADDR_T
770Sstevel@tonic-gate typedef uint32_t in_addr_t;
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
810Sstevel@tonic-gate extern	uint32_t htonl(uint32_t);
820Sstevel@tonic-gate extern	uint16_t htons(uint16_t);
830Sstevel@tonic-gate extern 	uint32_t ntohl(uint32_t);
840Sstevel@tonic-gate extern	uint16_t ntohs(uint16_t);
850Sstevel@tonic-gate #else
860Sstevel@tonic-gate extern	in_addr_t htonl(in_addr_t);
870Sstevel@tonic-gate extern	in_port_t htons(in_port_t);
880Sstevel@tonic-gate extern 	in_addr_t ntohl(in_addr_t);
890Sstevel@tonic-gate extern	in_port_t ntohs(in_port_t);
900Sstevel@tonic-gate #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Macros to reverse byte order
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate #define	BSWAP_8(x)	((x) & 0xff)
990Sstevel@tonic-gate #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
1000Sstevel@tonic-gate #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
1010Sstevel@tonic-gate #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate #define	BMASK_8(x)	((x) & 0xff)
1040Sstevel@tonic-gate #define	BMASK_16(x)	((x) & 0xffff)
1050Sstevel@tonic-gate #define	BMASK_32(x)	((x) & 0xffffffff)
1060Sstevel@tonic-gate #define	BMASK_64(x)	(x)
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Macros to convert from a specific byte order to/from native byte order
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1120Sstevel@tonic-gate #define	BE_8(x)		BMASK_8(x)
1130Sstevel@tonic-gate #define	BE_16(x)	BMASK_16(x)
1140Sstevel@tonic-gate #define	BE_32(x)	BMASK_32(x)
1150Sstevel@tonic-gate #define	BE_64(x)	BMASK_64(x)
1160Sstevel@tonic-gate #define	LE_8(x)		BSWAP_8(x)
1170Sstevel@tonic-gate #define	LE_16(x)	BSWAP_16(x)
1180Sstevel@tonic-gate #define	LE_32(x)	BSWAP_32(x)
1190Sstevel@tonic-gate #define	LE_64(x)	BSWAP_64(x)
1200Sstevel@tonic-gate #else
1210Sstevel@tonic-gate #define	LE_8(x)		BMASK_8(x)
1220Sstevel@tonic-gate #define	LE_16(x)	BMASK_16(x)
1230Sstevel@tonic-gate #define	LE_32(x)	BMASK_32(x)
1240Sstevel@tonic-gate #define	LE_64(x)	BMASK_64(x)
1250Sstevel@tonic-gate #define	BE_8(x)		BSWAP_8(x)
1260Sstevel@tonic-gate #define	BE_16(x)	BSWAP_16(x)
1270Sstevel@tonic-gate #define	BE_32(x)	BSWAP_32(x)
1280Sstevel@tonic-gate #define	BE_64(x)	BSWAP_64(x)
1290Sstevel@tonic-gate #endif
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #endif	/* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #ifdef	__cplusplus
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate #endif
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #endif /* _SYS_BYTEORDER_H */
138