Initial Commit (Not working)

This commit is contained in:
2025-06-04 16:29:53 -05:00
commit 62c7a653a0
5 changed files with 201 additions and 0 deletions

0
CMakeLists.txt Normal file
View File

0
README.md Normal file
View File

139
include/Endianness.hpp Normal file
View File

@@ -0,0 +1,139 @@
#pragma once
#include <algorithm>
#include <cstdint>
#include <bit>
#include <concepts>
#include <ranges>
namespace IntegerLiterals
{
using u8 = uint8_t; using ub = u8;
using u16 = uint16_t; using us = u16;
using u32 = uint32_t; //using ul = u32;
using u64 = uint64_t; using ud = u64;
using s8 = int8_t; using sb = s8;
using s16 = int16_t; using ss = s16;
using s32 = int32_t; using sl = s32;
using s64 = int64_t; using sd = s64;
constexpr inline u8 operator ""_u8(unsigned long long int value) { return value;}
constexpr inline ub operator ""_ub(unsigned long long int value) { return value;}
constexpr inline u16 operator ""_u16(unsigned long long int value) { return value;}
constexpr inline us operator ""_us(unsigned long long int value) { return value;}
constexpr inline u32 operator ""_u32(unsigned long long int value) { return value;}
constexpr inline u32 operator ""_ul(unsigned long long int value) { return value;}
constexpr inline u64 operator ""_u64(unsigned long long int value) { return value;}
constexpr inline u64 operator ""_ud(unsigned long long int value) { return value;}
constexpr inline s8 operator ""_s8(long double value) { return value;}
constexpr inline s8 operator ""_sb(long double value) { return value;}
constexpr inline s16 operator ""_s16(long double value) { return value;}
constexpr inline s16 operator ""_ss(long double value) { return value;}
constexpr inline s32 operator ""_s32(long double value) { return value;}
constexpr inline s32 operator ""_sl(long double value) { return value;}
constexpr inline s64 operator ""_s64(long double value) { return value;}
constexpr inline s64 operator ""_sd(long double value) { return value;}
}
using namespace IntegerLiterals;
/// https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
/// Platform-independent functions for swapping the byte order of common data types.
namespace Endianness
{
// TODO: It seems these can't have their implementation put into
// a separate .cpp file else the compiler throws undefined reference
// is it because they're constexpr?
constexpr bool IsBigEndian() { return (std::endian::native == std::endian::big); }
constexpr bool IsLittleEndian() { return (std::endian::native == std::endian::little); }
template <typename T>
T ReverseByteOrder(T val)
{
T retVal;
char* pVal = (char*)&val;
char* pRetVal = (char*)&retVal;
int size = sizeof(T);
for (int i = 0; i < size; i++)
{
pRetVal[size - 1 - i] = pVal[i];
}
return retVal;
//return byteswap(val);
}
template <typename T>
T ReverseByteOrderIfLittleEndian(T val)
{
if (IsLittleEndian())
return ReverseByteOrder(val);
else return val;
}
/// Returns the given values converted into network byte order.
/// Generally on x86, numbers are stored in 'little-endian' order.
/// Network order is 'big-endian'. Supposing a big-endian host machine,
/// no conversion will take place and the original value will be returned.
u16 HostToNetworkOrder(u16 host);
u32 HostToNetworkOrder(u32 host);
u64 HostToNetworkOrder(u64 host);
s16 HostToNetworkOrder(s16 host);
s32 HostToNetworkOrder(s32 host);
s64 HostToNetworkOrder(s64 host);
float HostToNetworkOrder(float host);
double HostToNetworkOrder(double host);
template <typename T> T HostToNetworkOrder(T host) { return HostToNetworkOrder(host);}
template<> inline u16 HostToNetworkOrder<u16>(u16 host) { return HostToNetworkOrder(host);}
template<> inline u32 HostToNetworkOrder<u32>(u32 host) { return HostToNetworkOrder(host);}
template<> inline u64 HostToNetworkOrder<u64>(u64 host) { return HostToNetworkOrder(host);}
template<> inline s16 HostToNetworkOrder<s16>(s16 host) { return HostToNetworkOrder(host);}
template<> inline s32 HostToNetworkOrder<s32>(s32 host) { return HostToNetworkOrder(host);}
template<> inline s64 HostToNetworkOrder<s64>(s64 host) { return HostToNetworkOrder(host);}
template<> inline float HostToNetworkOrder<float>(float host) { return HostToNetworkOrder(host);}
template<> inline double HostToNetworkOrder<double>(double host) { return HostToNetworkOrder(host);}
/// Returns the given values converted into host-byte order.
/// On x86, this will usually be 'little-endian'
/// On ARM (and other RISC architectures), it is often big-endian.
/// Supposing a big-endian host machine, no conversion will take place and the original value will be returned.
u16 NetworkToHostOrder(u16 network);
u32 NetworkToHostOrder(u32 network);
u64 NetworkToHostOrder(u64 network);
s16 NetworkToHostOrder(s16 network);
s32 NetworkToHostOrder(s32 network);
s64 NetworkToHostOrder(s64 network);
float NetworkToHostOrder(float network);
double NetworkToHostOrder(double network);
template <typename T> T NetworkToHostOrder(T network) { return NetworkToHostOrder(network);}
template<> inline u16 NetworkToHostOrder<u16>(u16 network) { return NetworkToHostOrder(network);}
template<> inline u32 NetworkToHostOrder<u32>(u32 network) { return NetworkToHostOrder(network);}
template<> inline u64 NetworkToHostOrder<u64>(u64 network) { return NetworkToHostOrder(network);}
template<> inline s16 NetworkToHostOrder<s16>(s16 network) { return NetworkToHostOrder(network);}
template<> inline s32 NetworkToHostOrder<s32>(s32 network) { return NetworkToHostOrder(network);}
template<> inline s64 NetworkToHostOrder<s64>(s64 network) { return NetworkToHostOrder(network);}
template<> inline float NetworkToHostOrder<float>(float network) { return NetworkToHostOrder(network);}
template<> inline double NetworkToHostOrder<double>(double network) { return NetworkToHostOrder(network);}
}

3
main.cpp Normal file
View File

@@ -0,0 +1,3 @@
//
// Created by josh on 6/4/2025.
//

59
src/Endianness.cpp Normal file
View File

@@ -0,0 +1,59 @@
#ifdef WIN32
#pragma comment(lib, "Ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#include <winsock.h>
#endif
#ifdef __linux__
#include <socket.h>
#endif
#include <ReArchive/Endianness.h>
namespace Endianness
{
// TODO: Manually swap bytes so we can depend less on clunky include directives.
u16 HostToNetworkOrder(u16 host) { return htons(host); }
u16 NetworkToHostOrder(u16 network) { return ntohs(network); }
s16 HostToNetworkOrder(s16 host) { return htons( host); }
s16 NetworkToHostOrder(s16 network) { return ntohs(network);}
u32 HostToNetworkOrder(u32 host) { return htonl(host); }
u32 NetworkToHostOrder(u32 network) { return ntohl(network); }
s32 HostToNetworkOrder(s32 host) { return htonl(host);}
s32 NetworkToHostOrder(s32 network) { return ntohl(network);}
u64 HostToNetworkOrder(u64 host) { return ReverseByteOrderIfLittleEndian(host); }
u64 NetworkToHostOrder(u64 network) { return ReverseByteOrderIfLittleEndian(network); }
s64 HostToNetworkOrder(s64 host) { return ReverseByteOrderIfLittleEndian(host); }
s64 NetworkToHostOrder(s64 network) { return ReverseByteOrderIfLittleEndian(network); }
float HostToNetworkOrder(float host) {
if (IsLittleEndian())
return ReverseByteOrder(host);
else
return host;
}
float NetworkToHostOrder(float network) {
if (IsLittleEndian())
return ReverseByteOrder(network);
else
return network;
}
double HostToNetworkOrder(double host) {
if (IsLittleEndian())
return ReverseByteOrder(host);
else
return host;
}
double NetworkToHostOrder(double network) {
if (IsLittleEndian())
return ReverseByteOrder(network);
else
return network;
}
}