xref: /titanic_44/usr/src/stand/lib/xdr/byteorder.c (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
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 http://www.opensolaris.org/os/licensing.
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <netinet/in.h>
28 
29 /*
30  * htonll(), ntohll(), htonl(), ntohl(), htons(), ntohs()
31  *
32  * On little endian machines these functions reverse the byte order of the
33  * input parameter and returns the result.  This is to convert the byte order
34  * from host byte order (little endian) to network byte order (big endian),
35  * or vice versa.
36  *
37  * On big endian machines these functions just return the input parameter,
38  * as the host byte order is the same as the network byte order (big endian).
39  */
40 
41 
42 #ifdef	_LITTLE_ENDIAN
43 uint64_t
44 htonll(uint64_t in)
45 {
46 	return ((uint64_t)htonl((in >> 32) & 0xffffffff) |
47 	    ((uint64_t)htonl(in & 0xffffffff) << 32));
48 }
49 
50 uint64_t
51 ntohll(uint64_t in)
52 {
53 	return ((uint64_t)ntohl((in >> 32) & 0xffffffff) |
54 	    ((uint64_t)ntohl(in & 0xffffffff) << 32));
55 }
56 
57 uint32_t
58 htonl(uint32_t in)
59 {
60 	uint32_t	i;
61 
62 	i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
63 	    (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
64 	    (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
65 	    (uint32_t)((in & (uint32_t)0x000000ff) << 24);
66 	return (i);
67 }
68 
69 uint32_t
70 ntohl(uint32_t in)
71 {
72 	return (htonl(in));
73 }
74 
75 uint16_t
76 htons(uint16_t in)
77 {
78 	register int arg = (int)in;
79 	uint16_t i;
80 
81 	i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
82 	i |= (uint16_t)((arg & 0xff) << 8);
83 	return ((uint16_t)i);
84 }
85 
86 uint16_t
87 ntohs(uint16_t in)
88 {
89 	return (htons(in));
90 }
91 
92 #else	/* _LITTLE_ENDIAN */
93 
94 #if defined(lint)
95 uint64_t
96 htonll(uint64_t in)
97 {
98 	return (in);
99 }
100 
101 uint64_t
102 ntohll(uint64_t in)
103 {
104 	return (in);
105 }
106 
107 uint32_t
108 htonl(uint32_t in)
109 {
110 	return (in);
111 }
112 
113 uint32_t
114 ntohl(uint32_t in)
115 {
116 	return (in);
117 }
118 
119 uint16_t
120 htons(uint16_t in)
121 {
122 	return (in);
123 }
124 
125 uint16_t
126 ntohs(uint16_t in)
127 {
128 	return (in);
129 }
130 
131 #endif	/* lint */
132 #endif	/* _LITTLE_ENDIAN */
133