Added generic hton and ntoh functions
This commit is contained in:
parent
8568a325c0
commit
c8804cbe8e
@ -62,6 +62,10 @@ AC_C_CONST
|
|||||||
AC_C_RESTRICT
|
AC_C_RESTRICT
|
||||||
AC_C_INLINE
|
AC_C_INLINE
|
||||||
|
|
||||||
|
##
|
||||||
|
## Architecture features
|
||||||
|
AC_C_BIGENDIAN
|
||||||
|
|
||||||
##
|
##
|
||||||
## stdlib features
|
## stdlib features
|
||||||
AC_FUNC_MMAP
|
AC_FUNC_MMAP
|
||||||
|
1
test/.gitignore
vendored
1
test/.gitignore
vendored
@ -1,5 +1,6 @@
|
|||||||
/backtrace
|
/backtrace
|
||||||
/float
|
/float
|
||||||
|
/hton
|
||||||
/ip
|
/ip
|
||||||
/json-check
|
/json-check
|
||||||
/maths
|
/maths
|
||||||
|
@ -7,7 +7,7 @@ AM_CPPFLAGS = \
|
|||||||
|
|
||||||
AM_LDFLAGS = $(COMMON_LDFLAGS)
|
AM_LDFLAGS = $(COMMON_LDFLAGS)
|
||||||
|
|
||||||
TEST_BIN = backtrace float range maths matrix version ip
|
TEST_BIN = backtrace float range maths matrix version ip hton
|
||||||
TESTS = $(TEST_BIN) json.pl
|
TESTS = $(TEST_BIN) json.pl
|
||||||
check_PROGRAMS = $(TEST_BIN) json-check
|
check_PROGRAMS = $(TEST_BIN) json-check
|
||||||
EXTRA_DIST = json.pl
|
EXTRA_DIST = json.pl
|
||||||
@ -34,5 +34,8 @@ version_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
|||||||
ip_SOURCES = ip.cpp
|
ip_SOURCES = ip.cpp
|
||||||
ip_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
ip_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
||||||
|
|
||||||
|
hton_SOURCES = hton.cpp
|
||||||
|
hton_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
||||||
|
|
||||||
json_check_SOURCES = json-check.cpp
|
json_check_SOURCES = json-check.cpp
|
||||||
json_check_LDADD = $(builddir)/../libutil.la $(BOOST_FILESYSTEM_LIB)
|
json_check_LDADD = $(builddir)/../libutil.la $(BOOST_FILESYSTEM_LIB)
|
||||||
|
26
test/hton.cpp
Normal file
26
test/hton.cpp
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
#include "../types.hpp"
|
||||||
|
|
||||||
|
#include "../debug.hpp"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv) {
|
||||||
|
|
||||||
|
uint16_t u16 = 0x1358;
|
||||||
|
uint32_t u32 = 0x12345678;
|
||||||
|
|
||||||
|
check_eq (htons (u16), hton (u16));
|
||||||
|
check_eq (htonl (u32), hton (u32));
|
||||||
|
|
||||||
|
check_eq (ntohs (u16), hton (u16));
|
||||||
|
check_eq (ntohl (u32), hton (u32));
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
79
types.cpp
79
types.cpp
@ -1,5 +1,7 @@
|
|||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
@ -10,3 +12,80 @@ template <> std::string type_to_string <const T> (void) { return "const " #T; }
|
|||||||
do_type_to_string (float)
|
do_type_to_string (float)
|
||||||
do_type_to_string (double)
|
do_type_to_string (double)
|
||||||
|
|
||||||
|
#ifdef WORDS_BIGENDIAN
|
||||||
|
/* Big endian doesn't need swapping */
|
||||||
|
template <typename T>
|
||||||
|
T hton (T val) {
|
||||||
|
static_assert (is_fundamental <T>(), "hton implementation assumes fundamental types");
|
||||||
|
static_assert (is_unsigned <T>(), "hton implementation does not handle signed");
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T ntoh (T val) {
|
||||||
|
static_assert (is_fundamental <T>(), "ntoh implementation assumes fundamental types");
|
||||||
|
static_assert (is_unsigned <T>(), "ntoh implementation does not handle signed");
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <> uint8_t hton ( uint8_t);
|
||||||
|
template <> uint16_t hton (uint16_t);
|
||||||
|
template <> uint32_t hton (uint32_t);
|
||||||
|
template <> uint64_t hton (uint64_t);
|
||||||
|
|
||||||
|
template <> uint8_t ntoh ( uint8_t);
|
||||||
|
template <> uint16_t ntoh (uint16_t);
|
||||||
|
template <> uint32_t ntoh (uint32_t);
|
||||||
|
template <> uint64_t ntoh (uint64_t);
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
static void
|
||||||
|
byte_swap (uint8_t *restrict dst, uint8_t *restrict src, size_t len) {
|
||||||
|
for (unsigned int i = 0; i < len; ++i)
|
||||||
|
dst[len - i - 1] = src[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T
|
||||||
|
hton (T i) {
|
||||||
|
// Unsure if this will really be sensible for non-intrinsic types, or types larger than 8 bytes.
|
||||||
|
static_assert (is_fundamental <T>::value, "hton implementation assumes fundamental types");
|
||||||
|
static_assert (is_unsigned <T>::value, "hton implementation does not handle signed");
|
||||||
|
|
||||||
|
T swapped;
|
||||||
|
byte_swap (reinterpret_cast <uint8_t*>(&swapped),
|
||||||
|
reinterpret_cast <uint8_t*>(&i),
|
||||||
|
sizeof (T));
|
||||||
|
return swapped;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
T
|
||||||
|
ntoh (T i) {
|
||||||
|
// Unsure if this will really be sensible for non-intrinsic types, or types larger than 8 bytes.
|
||||||
|
static_assert (is_fundamental <T>::value, "ntoh implementation assumes fundamental types");
|
||||||
|
static_assert (is_unsigned <T>::value, "ntoh implementation does not handle signed");
|
||||||
|
|
||||||
|
T swapped;
|
||||||
|
byte_swap (reinterpret_cast <uint8_t*>(&swapped),
|
||||||
|
reinterpret_cast <uint8_t*>(&i),
|
||||||
|
sizeof (T));
|
||||||
|
return swapped;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <> uint8_t hton (uint8_t i) { return i; }
|
||||||
|
template <> uint8_t ntoh (uint8_t i) { return i; }
|
||||||
|
|
||||||
|
template uint16_t ntoh<uint16_t> (uint16_t);
|
||||||
|
template uint32_t ntoh<uint32_t> (uint32_t);
|
||||||
|
template uint64_t ntoh<uint64_t> (uint64_t);
|
||||||
|
|
||||||
|
template uint16_t hton<uint16_t> (uint16_t);
|
||||||
|
template uint32_t hton<uint32_t> (uint32_t);
|
||||||
|
template uint64_t hton<uint64_t> (uint64_t);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user