2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-10 22:16:34 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-10 22:16:34 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-10 22:16:34 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_BSWAP_H
|
|
|
|
|
2019-06-23 10:01:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
inline namespace util
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
2018-07-02 01:31:46 +02:00
|
|
|
template<class T> T &bswap(T *const &);
|
|
|
|
template<class T> T bswap(T);
|
|
|
|
|
|
|
|
// Host <-> network endian
|
2019-04-07 02:07:11 +02:00
|
|
|
template<class T> T &hton(T *const &);
|
|
|
|
template<class T> T &ntoh(T *const &);
|
2018-07-02 01:31:46 +02:00
|
|
|
template<class T> T hton(T);
|
|
|
|
template<class T> T ntoh(T);
|
|
|
|
|
|
|
|
// Convenience host to network interface for arpa/inet.h compat
|
|
|
|
uint32_t htonl(const uint32_t &);
|
|
|
|
uint16_t htons(const uint16_t &);
|
|
|
|
double htond(const double &);
|
|
|
|
float htonf(const float &);
|
|
|
|
|
|
|
|
// Convenience network to host interface for arpa/inet.h compat
|
|
|
|
uint32_t ntohl(const uint32_t &);
|
|
|
|
uint16_t ntohs(const uint16_t &);
|
|
|
|
double ntohd(const double &);
|
|
|
|
float ntohf(const float &);
|
2019-06-23 10:01:26 +02:00
|
|
|
}}
|
2018-07-02 01:31:46 +02:00
|
|
|
|
|
|
|
inline float
|
|
|
|
ircd::util::ntohf(const float &a)
|
|
|
|
{
|
|
|
|
return ntoh(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline double
|
|
|
|
ircd::util::ntohd(const double &a)
|
|
|
|
{
|
|
|
|
return ntoh(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t
|
|
|
|
ircd::util::ntohl(const uint32_t &a)
|
|
|
|
{
|
|
|
|
return ntoh(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t
|
|
|
|
ircd::util::ntohs(const uint16_t &a)
|
|
|
|
{
|
|
|
|
return ntoh(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float
|
|
|
|
ircd::util::htonf(const float &a)
|
|
|
|
{
|
|
|
|
return hton(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline double
|
|
|
|
ircd::util::htond(const double &a)
|
|
|
|
{
|
|
|
|
return hton(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t
|
|
|
|
ircd::util::htonl(const uint32_t &a)
|
|
|
|
{
|
|
|
|
return hton(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t
|
|
|
|
ircd::util::htons(const uint16_t &a)
|
|
|
|
{
|
|
|
|
return hton(a);
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-07-02 01:31:46 +02:00
|
|
|
T
|
|
|
|
ircd::util::hton(T a)
|
2019-04-07 02:07:11 +02:00
|
|
|
{
|
|
|
|
return hton<T>(&a);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
|
|
|
ircd::util::ntoh(T a)
|
|
|
|
{
|
|
|
|
return ntoh<T>(&a);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T &
|
|
|
|
ircd::util::hton(T *const &a)
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
2018-07-02 01:31:46 +02:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2019-04-07 02:07:11 +02:00
|
|
|
return bswap(a);
|
2018-07-02 01:31:46 +02:00
|
|
|
#else
|
2019-04-07 02:07:11 +02:00
|
|
|
assert(a);
|
|
|
|
return *a;
|
2018-07-02 01:31:46 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2019-04-07 02:07:11 +02:00
|
|
|
T &
|
|
|
|
ircd::util::ntoh(T *const &a)
|
2018-07-02 01:31:46 +02:00
|
|
|
{
|
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2019-04-07 02:07:11 +02:00
|
|
|
return bswap(a);
|
2018-07-02 01:31:46 +02:00
|
|
|
#else
|
2019-04-07 02:07:11 +02:00
|
|
|
assert(a);
|
|
|
|
return *a;
|
2018-07-02 01:31:46 +02:00
|
|
|
#endif
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reverse endian of T returning value copy
|
|
|
|
template<class T>
|
|
|
|
T
|
2018-07-02 01:31:46 +02:00
|
|
|
ircd::util::bswap(T ret)
|
|
|
|
{
|
|
|
|
return bswap(&ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reverse endian of data pointed to; return reference
|
|
|
|
template<class T>
|
|
|
|
T &
|
|
|
|
ircd::util::bswap(T *const &val)
|
2018-01-10 22:16:34 +01:00
|
|
|
{
|
2018-07-02 01:31:46 +02:00
|
|
|
assert(val != nullptr);
|
|
|
|
const auto &ptr
|
|
|
|
{
|
|
|
|
reinterpret_cast<uint8_t *const &>(val)
|
|
|
|
};
|
|
|
|
|
|
|
|
std::reverse(ptr, ptr + sizeof(T));
|
|
|
|
return *val;
|
2018-01-10 22:16:34 +01:00
|
|
|
}
|