diff options
author | wez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-28 20:19:31 +0000 |
---|---|---|
committer | wez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-28 20:19:31 +0000 |
commit | 9eb7b11b614e987d7608678d0396873f37ca817e (patch) | |
tree | 7f222f4ae73bd3c3edc33d39f3bf7050d10f4254 /base/sys_byteorder.h | |
parent | 97f37d558f60a245190b17ce14d3d9039fc83767 (diff) | |
download | chromium_src-9eb7b11b614e987d7608678d0396873f37ca817e.zip chromium_src-9eb7b11b614e987d7608678d0396873f37ca817e.tar.gz chromium_src-9eb7b11b614e987d7608678d0396873f37ca817e.tar.bz2 |
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
Diffstat (limited to 'base/sys_byteorder.h')
-rw-r--r-- | base/sys_byteorder.h | 56 |
1 files changed, 53 insertions, 3 deletions
diff --git a/base/sys_byteorder.h b/base/sys_byteorder.h index 9fcdbba..80bb831 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 @@ 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 |