xref: /onnv-gate/usr/src/stand/lib/xdr/byteorder.c (revision 7421:8b7f030a1d82)
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*7421SDaniel.Anderson@Sun.COM  * Common Development and Distribution License (the "License").
6*7421SDaniel.Anderson@Sun.COM  * 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  */
210Sstevel@tonic-gate /*
22*7421SDaniel.Anderson@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <netinet/in.h>
280Sstevel@tonic-gate 
29*7421SDaniel.Anderson@Sun.COM /*
30*7421SDaniel.Anderson@Sun.COM  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
31*7421SDaniel.Anderson@Sun.COM  *
32*7421SDaniel.Anderson@Sun.COM  * On little endian machines these functions reverse the byte order of the
33*7421SDaniel.Anderson@Sun.COM  * input parameter and returns the result.  This is to convert the byte order
34*7421SDaniel.Anderson@Sun.COM  * from host byte order (little endian) to network byte order (big endian),
35*7421SDaniel.Anderson@Sun.COM  * or vice versa.
36*7421SDaniel.Anderson@Sun.COM  *
37*7421SDaniel.Anderson@Sun.COM  * On big endian machines these functions just return the input parameter,
38*7421SDaniel.Anderson@Sun.COM  * as the host byte order is the same as the network byte order (big endian).
39*7421SDaniel.Anderson@Sun.COM  */
40*7421SDaniel.Anderson@Sun.COM 
41*7421SDaniel.Anderson@Sun.COM 
420Sstevel@tonic-gate #ifdef	_LITTLE_ENDIAN
43*7421SDaniel.Anderson@Sun.COM uint64_t
htonll(uint64_t in)44*7421SDaniel.Anderson@Sun.COM htonll(uint64_t in)
45*7421SDaniel.Anderson@Sun.COM {
46*7421SDaniel.Anderson@Sun.COM 	return ((uint64_t)htonl((in >> 32) & 0xffffffff) |
47*7421SDaniel.Anderson@Sun.COM 	    ((uint64_t)htonl(in & 0xffffffff) << 32));
48*7421SDaniel.Anderson@Sun.COM }
49*7421SDaniel.Anderson@Sun.COM 
50*7421SDaniel.Anderson@Sun.COM uint64_t
ntohll(uint64_t in)51*7421SDaniel.Anderson@Sun.COM ntohll(uint64_t in)
52*7421SDaniel.Anderson@Sun.COM {
53*7421SDaniel.Anderson@Sun.COM 	return ((uint64_t)ntohl((in >> 32) & 0xffffffff) |
54*7421SDaniel.Anderson@Sun.COM 	    ((uint64_t)ntohl(in & 0xffffffff) << 32));
55*7421SDaniel.Anderson@Sun.COM }
56*7421SDaniel.Anderson@Sun.COM 
570Sstevel@tonic-gate uint32_t
htonl(uint32_t in)580Sstevel@tonic-gate htonl(uint32_t in)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	uint32_t	i;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
630Sstevel@tonic-gate 	    (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
640Sstevel@tonic-gate 	    (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
650Sstevel@tonic-gate 	    (uint32_t)((in & (uint32_t)0x000000ff) << 24);
660Sstevel@tonic-gate 	return (i);
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate uint32_t
ntohl(uint32_t in)700Sstevel@tonic-gate ntohl(uint32_t in)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	return (htonl(in));
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate uint16_t
htons(uint16_t in)760Sstevel@tonic-gate htons(uint16_t in)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	register int arg = (int)in;
790Sstevel@tonic-gate 	uint16_t i;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
820Sstevel@tonic-gate 	i |= (uint16_t)((arg & 0xff) << 8);
830Sstevel@tonic-gate 	return ((uint16_t)i);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate uint16_t
ntohs(uint16_t in)870Sstevel@tonic-gate ntohs(uint16_t in)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	return (htons(in));
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #else	/* _LITTLE_ENDIAN */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate #if defined(lint)
95*7421SDaniel.Anderson@Sun.COM uint64_t
htonll(uint64_t in)96*7421SDaniel.Anderson@Sun.COM htonll(uint64_t in)
97*7421SDaniel.Anderson@Sun.COM {
98*7421SDaniel.Anderson@Sun.COM 	return (in);
99*7421SDaniel.Anderson@Sun.COM }
100*7421SDaniel.Anderson@Sun.COM 
101*7421SDaniel.Anderson@Sun.COM uint64_t
ntohll(uint64_t in)102*7421SDaniel.Anderson@Sun.COM ntohll(uint64_t in)
103*7421SDaniel.Anderson@Sun.COM {
104*7421SDaniel.Anderson@Sun.COM 	return (in);
105*7421SDaniel.Anderson@Sun.COM }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate uint32_t
htonl(uint32_t in)1080Sstevel@tonic-gate htonl(uint32_t in)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	return (in);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate uint32_t
ntohl(uint32_t in)1140Sstevel@tonic-gate ntohl(uint32_t in)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	return (in);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate uint16_t
htons(uint16_t in)1200Sstevel@tonic-gate htons(uint16_t in)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	return (in);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate uint16_t
ntohs(uint16_t in)1260Sstevel@tonic-gate ntohs(uint16_t in)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (in);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #endif	/* lint */
1320Sstevel@tonic-gate #endif	/* _LITTLE_ENDIAN */
133