Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/stand/lib/xdr/byteorder.c
+++ new/usr/src/stand/lib/xdr/byteorder.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 - * Common Development and Distribution License, Version 1.0 only
6 - * (the "License"). You may not use this file except in compliance
7 - * with the License.
5 + * Common Development and Distribution License (the "License").
6 + * You may not use this file except in compliance with the License.
8 7 *
9 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 9 * or http://www.opensolaris.org/os/licensing.
11 10 * See the License for the specific language governing permissions
12 11 * and limitations under the License.
13 12 *
14 13 * When distributing Covered Code, include this CDDL HEADER in each
15 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 15 * If applicable, add the following below this CDDL HEADER, with the
17 16 * fields enclosed by brackets "[]" replaced with your own identifying
18 17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 18 *
20 19 * CDDL HEADER END
21 20 */
22 21 /*
23 - * Copyright 1991-2000, 2003 Sun Microsystems, Inc. All rights reserved.
22 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 23 * Use is subject to license terms.
25 24 */
26 25
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 26 #include <sys/types.h>
30 27 #include <netinet/in.h>
31 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 +
32 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 +
33 57 uint32_t
34 58 htonl(uint32_t in)
35 59 {
36 60 uint32_t i;
37 61
38 62 i = (uint32_t)((in & (uint32_t)0xff000000) >> 24) +
39 63 (uint32_t)((in & (uint32_t)0x00ff0000) >> 8) +
40 64 (uint32_t)((in & (uint32_t)0x0000ff00) << 8) +
41 65 (uint32_t)((in & (uint32_t)0x000000ff) << 24);
42 66 return (i);
43 67 }
44 68
45 69 uint32_t
46 70 ntohl(uint32_t in)
47 71 {
48 72 return (htonl(in));
49 73 }
50 74
51 75 uint16_t
52 76 htons(uint16_t in)
53 77 {
54 78 register int arg = (int)in;
55 79 uint16_t i;
56 80
57 81 i = (uint16_t)(((arg & 0xff00) >> 8) & 0xff);
58 82 i |= (uint16_t)((arg & 0xff) << 8);
59 83 return ((uint16_t)i);
60 84 }
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
61 85
62 86 uint16_t
63 87 ntohs(uint16_t in)
64 88 {
65 89 return (htons(in));
66 90 }
67 91
68 92 #else /* _LITTLE_ENDIAN */
69 93
70 94 #if defined(lint)
95 +uint64_t
96 +htonll(uint64_t in)
97 +{
98 + return (in);
99 +}
71 100
101 +uint64_t
102 +ntohll(uint64_t in)
103 +{
104 + return (in);
105 +}
106 +
72 107 uint32_t
73 108 htonl(uint32_t in)
74 109 {
75 110 return (in);
76 111 }
77 112
78 113 uint32_t
79 114 ntohl(uint32_t in)
80 115 {
81 116 return (in);
82 117 }
83 118
84 119 uint16_t
85 120 htons(uint16_t in)
86 121 {
87 122 return (in);
88 123 }
89 124
90 125 uint16_t
91 126 ntohs(uint16_t in)
92 127 {
93 128 return (in);
94 129 }
95 130
96 131 #endif /* lint */
97 132 #endif /* _LITTLE_ENDIAN */
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX