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