Add base::HostToNetXX() & NetToHostXX(), and use them to replace htonX() & ntohX() in Chrome.

This primarily addresses issues with code using the OS-provided htonX() & ntohX() functions from within the Chrome sandbox.  Under Windows these functions are provided by ws2_32.dll, which is no longer available within Chrome's sandbox.

The new base::HostToNetXX() and NetToHostXX() functions are safe for use by sandboxed code on Windows, and provide a single place where future fixes for other platforms can be made.

BUG=117252

Review URL: http://codereview.chromium.org/9716020

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129476 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/sys_byteorder.h b/base/sys_byteorder.h
index 9fcdbba..80bb8319 100644
--- a/base/sys_byteorder.h
+++ b/base/sys_byteorder.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -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 @@
 
 // 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 @@
 
 // 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