1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20178414Sjb *
21178414Sjb * $FreeBSD$
22168404Spjd */
23168404Spjd
24168404Spjd/*
25168404Spjd * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26168404Spjd * Use is subject to license terms.
27168404Spjd */
28168404Spjd
29168404Spjd/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
30168404Spjd/*	  All Rights Reserved  	*/
31168404Spjd
32168404Spjd/*
33168404Spjd * University Copyright- Copyright (c) 1982, 1986, 1988
34168404Spjd * The Regents of the University of California
35168404Spjd * All Rights Reserved
36168404Spjd *
37168404Spjd * University Acknowledgment- Portions of this document are derived from
38168404Spjd * software developed by the University of California, Berkeley, and its
39168404Spjd * contributors.
40168404Spjd */
41168404Spjd
42168404Spjd#ifndef _OPENSOLARIS_SYS_BYTEORDER_H_
43168404Spjd#define	_OPENSOLARIS_SYS_BYTEORDER_H_
44168404Spjd
45247309Sdelphij/* for htonl() */
46247309Sdelphij#ifndef _KERNEL
47247309Sdelphij#include <netinet/in.h>
48247309Sdelphij#endif
49247309Sdelphij
50168404Spjd/*
51168404Spjd * Macros to reverse byte order
52168404Spjd */
53168404Spjd#define	BSWAP_8(x)	((x) & 0xff)
54168404Spjd#define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
55168404Spjd#define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
56168404Spjd#define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
57168404Spjd
58168404Spjd#define	BMASK_8(x)	((x) & 0xff)
59168404Spjd#define	BMASK_16(x)	((x) & 0xffff)
60168404Spjd#define	BMASK_32(x)	((x) & 0xffffffff)
61168404Spjd#define	BMASK_64(x)	(x)
62168404Spjd
63168404Spjd/*
64168404Spjd * Macros to convert from a specific byte order to/from native byte order
65168404Spjd */
66168404Spjd#if _BYTE_ORDER == _BIG_ENDIAN
67219089Spjd#define	BE_8(x)		BMASK_8(x)
68219089Spjd#define	BE_16(x)	BMASK_16(x)
69219089Spjd#define	BE_32(x)	BMASK_32(x)
70219089Spjd#define	BE_64(x)	BMASK_64(x)
71219089Spjd#define	LE_8(x)		BSWAP_8(x)
72219089Spjd#define	LE_16(x)	BSWAP_16(x)
73219089Spjd#define	LE_32(x)	BSWAP_32(x)
74168404Spjd#define	LE_64(x)	BSWAP_64(x)
75168404Spjd#else
76219089Spjd#define	LE_8(x)		BMASK_8(x)
77219089Spjd#define	LE_16(x)	BMASK_16(x)
78219089Spjd#define	LE_32(x)	BMASK_32(x)
79168404Spjd#define	LE_64(x)	BMASK_64(x)
80219089Spjd#define	BE_8(x)		BSWAP_8(x)
81219089Spjd#define	BE_16(x)	BSWAP_16(x)
82219089Spjd#define	BE_32(x)	BSWAP_32(x)
83219089Spjd#define	BE_64(x)	BSWAP_64(x)
84168404Spjd#endif
85168404Spjd
86219089Spjd#if _BYTE_ORDER == _BIG_ENDIAN
87219089Spjd#define	htonll(x)	BMASK_64(x)
88219089Spjd#define	ntohll(x)	BMASK_64(x)
89219089Spjd#else
90219089Spjd#define	htonll(x)	BSWAP_64(x)
91219089Spjd#define	ntohll(x)	BSWAP_64(x)
92219089Spjd#endif
93219089Spjd
94247309Sdelphij#define BE_IN32(xa)	htonl(*((uint32_t *)(void *)(xa)))
95247309Sdelphij
96168404Spjd#endif /* _OPENSOLARIS_SYS_BYTEORDER_H_ */
97