Split byte order routines out of utils.h

This commit is contained in:
Vinnie Falco
2013-05-25 16:32:08 -07:00
parent 4e8d4c28c2
commit 61fb6538d7
8 changed files with 122 additions and 73 deletions

View File

@@ -0,0 +1,41 @@
#ifdef WIN32
//#include <windows.h>
// from: http://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c
// but we don't need to check the endianness
uint64_t htobe64(uint64_t value)
{
// The answer is 42
//static const int num = 42;
// Check the endianness
//if (*reinterpret_cast<const char*>(&num) == num)
//{
const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));
return (static_cast<uint64_t>(low_part) << 32) | high_part;
//} else
//{
// return value;
//}
}
uint64_t be64toh(uint64_t value)
{
return(_byteswap_uint64(value));
}
uint32_t htobe32(uint32_t value)
{
return(htonl(value));
}
uint32_t be32toh(uint32_t value)
{
return( _byteswap_ulong(value));
}
#endif
// vim:ts=4

View File

@@ -0,0 +1,46 @@
#ifndef RIPPLE_BYTEORDER_H
#define RIPPLE_BYTEORDER_H
// Routines for converting endianness
// Reference: http://www.mail-archive.com/licq-commits@googlegroups.com/msg02334.html
// VFALCO: TODO, use VFLIB_* platform macros instead of hard-coded compiler specific ones
#ifdef WIN32
extern uint64_t htobe64(uint64_t value);
extern uint64_t be64toh(uint64_t value);
extern uint32_t htobe32(uint32_t value);
extern uint32_t be32toh(uint32_t value);
#elif __APPLE__
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#elif defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/endian.h>
#elif defined(__OpenBSD__)
#include <sys/types.h>
#define be16toh(x) betoh16(x)
#define be32toh(x) betoh32(x)
#define be64toh(x) betoh64(x)
#endif
#endif
// vim:ts=4

View File

@@ -44,6 +44,12 @@
#include "events/ripple_UptimeTimer.cpp"
#ifdef WIN32
// Winsock #defines 'max' and does other stupid things so put it last
#include "Winsock2.h" // for ripple_ByteOrder.cpp
#endif
#include "memory/ripple_ByteOrder.cpp"
#ifdef _MSC_VER
//#pragma warning (pop)
#endif

View File

@@ -68,6 +68,17 @@ namespace boost {
#include <boost/ref.hpp>
#include <boost/make_shared.hpp>
// ByteOrder
#ifdef WIN32
// (nothing)
#elif __APPLE__
# include <libkern/OSByteOrder.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/endian.h>
#elif defined(__OpenBSD__)
# include <sys/types.h>
#endif
#include "../ripple_json/ripple_json.h"
@@ -81,6 +92,8 @@ namespace boost {
#include "containers/ripple_SecureAllocator.h"
#include "containers/ripple_TaggedCache.h"
#include "memory/ripple_ByteOrder.h"
#include "events/ripple_UptimeTimer.h"
#endif

View File

@@ -197,6 +197,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="modules\ripple_basics\memory\ripple_ByteOrder.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="modules\ripple_basics\ripple_basics.cpp" />
<ClCompile Include="modules\ripple_client\ripple_client.cpp" />
<ClCompile Include="modules\ripple_db\ripple_db.cpp" />
@@ -1181,6 +1187,7 @@
<ClInclude Include="modules\ripple_basics\containers\ripple_TaggedCache.h" />
<ClInclude Include="modules\ripple_basics\diagnostic\ripple_Log.h" />
<ClInclude Include="modules\ripple_basics\events\ripple_UptimeTimer.h" />
<ClInclude Include="modules\ripple_basics\memory\ripple_ByteOrder.h" />
<ClInclude Include="modules\ripple_basics\ripple_basics.h" />
<ClInclude Include="modules\ripple_basics\types\ripple_IntegerTypes.h" />
<ClInclude Include="modules\ripple_client\ripple_client.h" />

View File

@@ -133,6 +133,9 @@
<Filter Include="1. Modules\ripple_net\sockets\_OLD">
<UniqueIdentifier>{1194480b-fa6b-4d92-8656-2470f45f2753}</UniqueIdentifier>
</Filter>
<Filter Include="1. Modules\ripple_basics\memory">
<UniqueIdentifier>{49faa808-02f3-44ba-a17c-43dce213672e}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\cpp\database\sqlite3.c">
@@ -753,6 +756,9 @@
<ClCompile Include="modules\ripple_basics\containers\ripple_RangeSet.cpp">
<Filter>1. Modules\ripple_basics\containers</Filter>
</ClCompile>
<ClCompile Include="modules\ripple_basics\memory\ripple_ByteOrder.cpp">
<Filter>1. Modules\ripple_basics\memory</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="database\sqlite3ext.h">
@@ -1400,6 +1406,9 @@
<ClInclude Include="modules\ripple_basics\containers\ripple_TaggedCache.h">
<Filter>1. Modules\ripple_basics\containers</Filter>
</ClInclude>
<ClInclude Include="modules\ripple_basics\memory\ripple_ByteOrder.h">
<Filter>1. Modules\ripple_basics\memory</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="SConstruct" />

View File

@@ -344,46 +344,6 @@ int strIPtoInt(std::string& ipStr)
}
*/
#ifdef WIN32
//#include "Winsock2.h"
//#include <windows.h>
// from: http://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c
// but we don't need to check the endianness
uint64_t htobe64(uint64_t value)
{
// The answer is 42
//static const int num = 42;
// Check the endianness
//if (*reinterpret_cast<const char*>(&num) == num)
//{
const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));
return (static_cast<uint64_t>(low_part) << 32) | high_part;
//} else
//{
// return value;
//}
}
uint64_t be64toh(uint64_t value)
{
return(_byteswap_uint64(value));
}
uint32_t htobe32(uint32_t value)
{
return(htonl(value));
}
uint32_t be32toh(uint32_t value)
{
return( _byteswap_ulong(value));
}
#endif
#ifdef PR_SET_NAME
#define HAVE_NAME_THREAD

View File

@@ -21,39 +21,6 @@
#define isSetBit(x,y) (!!((x) & (y)))
// maybe use http://www.mail-archive.com/licq-commits@googlegroups.com/msg02334.html
#ifdef WIN32
extern uint64_t htobe64(uint64_t value);
extern uint64_t be64toh(uint64_t value);
extern uint32_t htobe32(uint32_t value);
extern uint32_t be32toh(uint32_t value);
#elif __APPLE__
#include <libkern/OSByteOrder.h>
#define htobe16(x) OSSwapHostToBigInt16(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define be16toh(x) OSSwapBigToHostInt16(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#define htobe32(x) OSSwapHostToBigInt32(x)
#define htole32(x) OSSwapHostToLittleInt32(x)
#define be32toh(x) OSSwapBigToHostInt32(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define htobe64(x) OSSwapHostToBigInt64(x)
#define htole64(x) OSSwapHostToLittleInt64(x)
#define be64toh(x) OSSwapBigToHostInt64(x)
#define le64toh(x) OSSwapLittleToHostInt64(x)
#elif defined(__FreeBSD__) || defined(__NetBSD__)
#include <sys/endian.h>
#elif defined(__OpenBSD__)
#include <sys/types.h>
#define be16toh(x) betoh16(x)
#define be32toh(x) betoh32(x)
#define be64toh(x) betoh64(x)
#endif
#define vt_f_black "\033[30m"
#define vt_f_red "\033[31m"
#define vt_f_green "\033[32m"