Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1108)

Side by Side Diff: base/sys_byteorder.h

Issue 9716020: Add base::HostToNetXX() & NetToHostXX(), and use them to replace htonX() & ntohX() in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/chromeos/web_socket_proxy.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This is a convenience header to pull in the platform-specific 5 // This is a convenience header to pull in the platform-specific
6 // headers that define functions for byte-order conversion, 6 // headers that define functions for byte-order conversion,
7 // particularly: ntohs(), htons(), ntohl(), htonl(). Prefer including 7 // particularly: ntohs(), htons(), ntohl(), htonl(). Prefer including
8 // this file instead of directly writing the #if / #else, since it 8 // this file instead of directly writing the #if / #else, since it
9 // avoids duplicating the platform-specific selections. 9 // avoids duplicating the platform-specific selections.
10 // This header also provides ntohll() and htonll() for byte-order conversion 10 // This header also provides ntohll() and htonll() for byte-order conversion
(...skipping 19 matching lines...) Expand all
30 #elif defined(OS_OPENBSD) 30 #elif defined(OS_OPENBSD)
31 #include <sys/endian.h> 31 #include <sys/endian.h>
32 #else 32 #else
33 #include <byteswap.h> 33 #include <byteswap.h>
34 #endif 34 #endif
35 35
36 36
37 namespace base { 37 namespace base {
38 38
39 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness. 39 // Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
40 inline uint16 ByteSwap(uint16 x) {
41 #if defined(COMPILER_MSVC)
42 return _byteswap_ushort(x);
43 #elif defined(OS_MACOSX)
44 return OSSwapInt16(x);
45 #elif defined(OS_OPENBSD)
46 return swap16(x);
47 #else
48 return bswap_16(x);
49 #endif
50 }
51 inline uint32 ByteSwap(uint32 x) {
52 #if defined(COMPILER_MSVC)
53 return _byteswap_ulong(x);
54 #elif defined(OS_MACOSX)
55 return OSSwapInt32(x);
56 #elif defined(OS_OPENBSD)
57 return swap32(x);
58 #else
59 return bswap_32(x);
60 #endif
61 }
40 inline uint64 ByteSwap(uint64 x) { 62 inline uint64 ByteSwap(uint64 x) {
41 #if defined(COMPILER_MSVC) 63 #if defined(COMPILER_MSVC)
42 return _byteswap_uint64(x); 64 return _byteswap_uint64(x);
43 #elif defined(OS_MACOSX) 65 #elif defined(OS_MACOSX)
44 return OSSwapInt64(x); 66 return OSSwapInt64(x);
45 #elif defined(OS_OPENBSD) 67 #elif defined(OS_OPENBSD)
46 return swap64(x); 68 return swap64(x);
47 #else 69 #else
48 return bswap_64(x); 70 return bswap_64(x);
49 #endif 71 #endif
50 } 72 }
51 73
52 // Converts the bytes in |x| from network to host order (endianness), and 74 // Converts the bytes in |x| from network to host order (endianness), and
53 // returns the result. 75 // returns the result.
54 inline uint64 ntohll(uint64 x) { 76 inline uint16 NetToHost16(uint16 x) {
55 #if defined(ARCH_CPU_LITTLE_ENDIAN) 77 #if defined(ARCH_CPU_LITTLE_ENDIAN)
56 return ByteSwap(x); 78 return ByteSwap(x);
57 #else 79 #else
80 return x;
81 #endif
82 }
83 inline uint32 NetToHost32(uint32 x) {
84 #if defined(ARCH_CPU_LITTLE_ENDIAN)
85 return ByteSwap(x);
86 #else
87 return x;
88 #endif
89 }
90 inline uint64 NetToHost64(uint64 x) {
91 #if defined(ARCH_CPU_LITTLE_ENDIAN)
92 return ByteSwap(x);
93 #else
58 return x; 94 return x;
59 #endif 95 #endif
60 } 96 }
61 97
62 // Converts the bytes in |x| from host to network order (endianness), and 98 // Converts the bytes in |x| from host to network order (endianness), and
63 // returns the result. 99 // returns the result.
64 inline uint64 htonll(uint64 x) { 100 inline uint16 HostToNet16(uint16 x) {
65 #if defined(ARCH_CPU_LITTLE_ENDIAN) 101 #if defined(ARCH_CPU_LITTLE_ENDIAN)
66 return ByteSwap(x); 102 return ByteSwap(x);
67 #else 103 #else
104 return x;
105 #endif
106 }
107 inline uint32 HostToNet32(uint32 x) {
108 #if defined(ARCH_CPU_LITTLE_ENDIAN)
109 return ByteSwap(x);
110 #else
111 return x;
112 #endif
113 }
114 inline uint64 HostToNet64(uint64 x) {
115 #if defined(ARCH_CPU_LITTLE_ENDIAN)
116 return ByteSwap(x);
117 #else
68 return x; 118 return x;
69 #endif 119 #endif
70 } 120 }
71 121
72 } // namespace base 122 } // namespace base
73 123
74 124
75 #endif // BASE_SYS_BYTEORDER_H_ 125 #endif // BASE_SYS_BYTEORDER_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/web_socket_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698