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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/chromeos/web_socket_proxy.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/sys_byteorder.h
diff --git a/base/sys_byteorder.h b/base/sys_byteorder.h
index 9fcdbba38703cbf0f6dbb43c65a8728ada79837d..1f317714d2529b3d887b9c6bc8b581407575a263 100644
--- a/base/sys_byteorder.h
+++ b/base/sys_byteorder.h
@@ -37,6 +37,28 @@
namespace base {
// Returns a value with all bytes in |x| swapped, i.e. reverses the endianness.
+inline uint16 ByteSwap(uint16 x) {
+#if defined(COMPILER_MSVC)
+ return _byteswap_ushort(x);
+#elif defined(OS_MACOSX)
+ return OSSwapInt16(x);
+#elif defined(OS_OPENBSD)
+ return swap16(x);
+#else
+ return bswap_16(x);
+#endif
+}
+inline uint32 ByteSwap(uint32 x) {
+#if defined(COMPILER_MSVC)
+ return _byteswap_ulong(x);
+#elif defined(OS_MACOSX)
+ return OSSwapInt32(x);
+#elif defined(OS_OPENBSD)
+ return swap32(x);
+#else
+ return bswap_32(x);
+#endif
+}
inline uint64 ByteSwap(uint64 x) {
#if defined(COMPILER_MSVC)
return _byteswap_uint64(x);
@@ -51,7 +73,21 @@ inline uint64 ByteSwap(uint64 x) {
// Converts the bytes in |x| from network to host order (endianness), and
// returns the result.
-inline uint64 ntohll(uint64 x) {
+inline uint16 NetToHost16(uint16 x) {
+#if defined(ARCH_CPU_LITTLE_ENDIAN)
+ return ByteSwap(x);
+#else
+ return x;
+#endif
+}
+inline uint32 NetToHost32(uint32 x) {
+#if defined(ARCH_CPU_LITTLE_ENDIAN)
+ return ByteSwap(x);
+#else
+ return x;
+#endif
+}
+inline uint64 NetToHost64(uint64 x) {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
return ByteSwap(x);
#else
@@ -61,7 +97,21 @@ inline uint64 ntohll(uint64 x) {
// Converts the bytes in |x| from host to network order (endianness), and
// returns the result.
-inline uint64 htonll(uint64 x) {
+inline uint16 HostToNet16(uint16 x) {
+#if defined(ARCH_CPU_LITTLE_ENDIAN)
+ return ByteSwap(x);
+#else
+ return x;
+#endif
+}
+inline uint32 HostToNet32(uint32 x) {
+#if defined(ARCH_CPU_LITTLE_ENDIAN)
+ return ByteSwap(x);
+#else
+ return x;
+#endif
+}
+inline uint64 HostToNet64(uint64 x) {
#if defined(ARCH_CPU_LITTLE_ENDIAN)
return ByteSwap(x);
#else
« 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