xref: /illumos-gate/usr/src/stand/lib/xdr/byteorder.c (revision 4b56a003)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4b56a003SDaniel Anderson  * Common Development and Distribution License (the "License").
6*4b56a003SDaniel Anderson  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4b56a003SDaniel Anderson  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <netinet/in.h>
287c478bd9Sstevel@tonic-gate 
29*4b56a003SDaniel Anderson /*
30*4b56a003SDaniel Anderson  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
31*4b56a003SDaniel Anderson  *
32*4b56a003SDaniel Anderson  * On little endian machines these functions reverse the byte order of the
33*4b56a003SDaniel Anderson  * input parameter and returns the result.  This is to convert the byte order
34*4b56a003SDaniel Anderson  * from host byte order (little endian) to network byte order (big endian),
35*4b56a003SDaniel Anderson  * or vice versa.
36*4b56a003SDaniel Anderson  *
37*4b56a003SDaniel Anderson  * On big endian machines these functions just return the input parameter,
38*4b56a003SDaniel Anderson  * as the host byte order is the same as the network byte order (big endian).
39*4b56a003SDaniel Anderson  */
40*4b56a003SDaniel Anderson 
41*4b56a003SDaniel Anderson 
427c478bd9Sstevel@tonic-gate #ifdef	_LITTLE_ENDIAN
43*4b56a003SDaniel Anderson uint64_t
htonll(uint64_t in)44*4b56a003SDaniel Anderson htonll(uint64_t in)
45*4b56a003SDaniel Anderson {
46*4b56a003SDaniel Anderson 	return ((uint64_t)htonl((in >> 32) & 0xffffffff) |
47*4b56a003SDaniel Anderson 	    ((uint64_t)htonl(in & 0xffffffff) << 32));
48*4b56a003SDaniel Anderson }
49*4b56a003SDaniel Anderson 
50*4b56a003SDaniel Anderson uint64_t
ntohll(uint64_t in)51*4b56a003SDaniel Anderson ntohll(uint64_t in)
52*4b56a003SDaniel Anderson {
53*4b56a003SDaniel Anderson 	return ((uint64_t)ntohl((in >> 32) & 0xffffffff) |
54*4b56a003SDaniel Anderson 	    ((uint64_t)ntohl(in & 0xffffffff) << 32));
55*4b56a003SDaniel Anderson }
56*4b56a003SDaniel Anderson 
577c478bd9Sstevel@tonic-gate uint32_t
htonl(uint32_t in)587c478bd9Sstevel@tonic-gate htonl(uint32_t in)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	uint32_t	i;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
637c478bd9Sstevel@tonic-gate 	    (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
647c478bd9Sstevel@tonic-gate 	    (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
657c478bd9Sstevel@tonic-gate 	    (uint32_t)((in & (uint32_t)0x000000ff) << 24);
667c478bd9Sstevel@tonic-gate 	return (i);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate uint32_t
ntohl(uint32_t in)707c478bd9Sstevel@tonic-gate ntohl(uint32_t in)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	return (htonl(in));
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate uint16_t
htons(uint16_t in)767c478bd9Sstevel@tonic-gate htons(uint16_t in)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	register int arg = (int)in;
797c478bd9Sstevel@tonic-gate 	uint16_t i;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
827c478bd9Sstevel@tonic-gate 	i |= (uint16_t)((arg & 0xff) << 8);
837c478bd9Sstevel@tonic-gate 	return ((uint16_t)i);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate uint16_t
ntohs(uint16_t in)877c478bd9Sstevel@tonic-gate ntohs(uint16_t in)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	return (htons(in));
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #else	/* _LITTLE_ENDIAN */
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #if defined(lint)
95*4b56a003SDaniel Anderson uint64_t
htonll(uint64_t in)96*4b56a003SDaniel Anderson htonll(uint64_t in)
97*4b56a003SDaniel Anderson {
98*4b56a003SDaniel Anderson 	return (in);
99*4b56a003SDaniel Anderson }
100*4b56a003SDaniel Anderson 
101*4b56a003SDaniel Anderson uint64_t
ntohll(uint64_t in)102*4b56a003SDaniel Anderson ntohll(uint64_t in)
103*4b56a003SDaniel Anderson {
104*4b56a003SDaniel Anderson 	return (in);
105*4b56a003SDaniel Anderson }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate uint32_t
htonl(uint32_t in)1087c478bd9Sstevel@tonic-gate htonl(uint32_t in)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	return (in);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate uint32_t
ntohl(uint32_t in)1147c478bd9Sstevel@tonic-gate ntohl(uint32_t in)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	return (in);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate uint16_t
htons(uint16_t in)1207c478bd9Sstevel@tonic-gate htons(uint16_t in)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	return (in);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate uint16_t
ntohs(uint16_t in)1267c478bd9Sstevel@tonic-gate ntohs(uint16_t in)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	return (in);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #endif	/* lint */
1327c478bd9Sstevel@tonic-gate #endif	/* _LITTLE_ENDIAN */
133