kabufuda/include/kabufuda/Util.hpp

215 lines
8.5 KiB
C++
Raw Normal View History

2018-10-06 20:41:40 -07:00
#pragma once
2016-03-26 11:34:03 -07:00
#ifndef _WIN32
#include <cerrno>
2017-12-29 00:07:49 -08:00
#include <cstdlib>
2016-03-26 11:34:03 -07:00
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
2016-03-26 11:34:03 -07:00
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <algorithm>
#include <cstdint>
2019-08-31 01:58:06 -07:00
#include <cstring>
2016-03-26 11:34:03 -07:00
#include <string>
#include <type_traits>
2016-03-26 11:34:03 -07:00
#undef bswap16
#undef bswap32
#undef bswap64
2016-06-30 02:53:13 -07:00
#ifndef ENABLE_BITWISE_ENUM
2016-09-05 13:56:09 -07:00
#define ENABLE_BITWISE_ENUM(type) \
2018-12-07 21:20:24 -08:00
constexpr type operator|(type a, type b) { \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) | static_cast<T>(b)); \
} \
constexpr type operator&(type a, type b) { \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) & static_cast<T>(b)); \
} \
constexpr type& operator|=(type& a, type b) { \
2018-12-07 21:20:24 -08:00
using T = std::underlying_type_t<type>; \
a = type(static_cast<T>(a) | static_cast<T>(b)); \
return a; \
} \
constexpr type& operator&=(type& a, type b) { \
2018-12-07 21:20:24 -08:00
using T = std::underlying_type_t<type>; \
a = type(static_cast<T>(a) & static_cast<T>(b)); \
return a; \
} \
2018-12-15 22:30:54 -08:00
constexpr type operator~(type key) { \
2018-12-07 21:20:24 -08:00
using T = std::underlying_type_t<type>; \
return type(~static_cast<T>(key)); \
} \
constexpr bool True(type key) { \
using T = std::underlying_type_t<type>; \
return static_cast<T>(key) != 0; \
} \
constexpr bool False(type key) { \
using T = std::underlying_type_t<type>; \
return static_cast<T>(key) == 0; \
2018-12-07 21:20:24 -08:00
}
2016-06-30 02:53:13 -07:00
#endif
2018-12-07 21:20:24 -08:00
namespace kabufuda {
2016-03-26 11:34:03 -07:00
/* Type-sensitive byte swappers */
template <typename T>
2019-07-19 21:26:25 -07:00
constexpr T bswap16(T val) {
2016-03-26 11:34:03 -07:00
#if __GNUC__
2018-12-07 21:20:24 -08:00
return __builtin_bswap16(val);
2016-03-26 11:34:03 -07:00
#elif _WIN32
2018-12-07 21:20:24 -08:00
return _byteswap_ushort(val);
2016-03-26 11:34:03 -07:00
#else
2018-12-07 21:20:24 -08:00
return (val = (val << 8) | ((val >> 8) & 0xFF));
2016-03-26 11:34:03 -07:00
#endif
}
template <typename T>
2019-07-19 21:26:25 -07:00
constexpr T bswap32(T val) {
2016-03-26 11:34:03 -07:00
#if __GNUC__
2018-12-07 21:20:24 -08:00
return __builtin_bswap32(val);
2016-03-26 11:34:03 -07:00
#elif _WIN32
2018-12-07 21:20:24 -08:00
return _byteswap_ulong(val);
2016-03-26 11:34:03 -07:00
#else
2018-12-07 21:20:24 -08:00
val = (val & 0x0000FFFF) << 16 | (val & 0xFFFF0000) >> 16;
val = (val & 0x00FF00FF) << 8 | (val & 0xFF00FF00) >> 8;
return val;
2016-03-26 11:34:03 -07:00
#endif
}
template <typename T>
2019-07-19 21:26:25 -07:00
constexpr T bswap64(T val) {
2016-03-26 11:34:03 -07:00
#if __GNUC__
2018-12-07 21:20:24 -08:00
return __builtin_bswap64(val);
2016-03-26 11:34:03 -07:00
#elif _WIN32
2018-12-07 21:20:24 -08:00
return _byteswap_uint64(val);
2016-03-26 11:34:03 -07:00
#else
2018-12-07 21:20:24 -08:00
return ((val & 0xFF00000000000000ULL) >> 56) | ((val & 0x00FF000000000000ULL) >> 40) |
((val & 0x0000FF0000000000ULL) >> 24) | ((val & 0x000000FF00000000ULL) >> 8) |
((val & 0x00000000FF000000ULL) << 8) | ((val & 0x0000000000FF0000ULL) << 24) |
((val & 0x000000000000FF00ULL) << 40) | ((val & 0x00000000000000FFULL) << 56);
2016-03-26 11:34:03 -07:00
#endif
}
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
2019-07-19 21:26:25 -07:00
constexpr int16_t SBig(int16_t val) { return bswap16(val); }
constexpr uint16_t SBig(uint16_t val) { return bswap16(val); }
constexpr int32_t SBig(int32_t val) { return bswap32(val); }
constexpr uint32_t SBig(uint32_t val) { return bswap32(val); }
constexpr int64_t SBig(int64_t val) { return bswap64(val); }
constexpr uint64_t SBig(uint64_t val) { return bswap64(val); }
constexpr float SBig(float val) {
2019-06-11 19:04:15 -07:00
union { float f; int32_t i; } uval1 = {val};
union { int32_t i; float f; } uval2 = {bswap32(uval1.i)};
return uval2.f;
2016-03-26 11:34:03 -07:00
}
2019-07-19 21:26:25 -07:00
constexpr double SBig(double val) {
2019-06-11 19:04:15 -07:00
union { double f; int64_t i; } uval1 = {val};
union { int64_t i; double f; } uval2 = {bswap64(uval1.i)};
return uval2.f;
2016-03-26 11:34:03 -07:00
}
#ifndef SBIG
2016-09-05 13:56:09 -07:00
#define SBIG(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
#endif
2016-03-26 11:34:03 -07:00
2019-07-19 21:26:25 -07:00
constexpr int16_t SLittle(int16_t val) { return val; }
constexpr uint16_t SLittle(uint16_t val) { return val; }
constexpr int32_t SLittle(int32_t val) { return val; }
constexpr uint32_t SLittle(uint32_t val) { return val; }
constexpr int64_t SLittle(int64_t val) { return val; }
constexpr uint64_t SLittle(uint64_t val) { return val; }
constexpr float SLittle(float val) { return val; }
constexpr double SLittle(double val) { return val; }
#ifndef SLITTLE
2016-03-26 11:34:03 -07:00
#define SLITTLE(q) (q)
#endif
2016-03-26 11:34:03 -07:00
#else
2019-07-19 21:26:25 -07:00
constexpr int16_t SLittle(int16_t val) { return bswap16(val); }
constexpr uint16_t SLittle(uint16_t val) { return bswap16(val); }
constexpr int32_t SLittle(int32_t val) { return bswap32(val); }
constexpr uint32_t SLittle(uint32_t val) { return bswap32(val); }
constexpr int64_t SLittle(int64_t val) { return bswap64(val); }
constexpr uint64_t SLittle(uint64_t val) { return bswap64(val); }
constexpr float SLittle(float val) {
2018-12-07 21:20:24 -08:00
int32_t ival = bswap32(*((int32_t*)(&val)));
return *((float*)(&ival));
2016-03-26 11:34:03 -07:00
}
2019-07-19 21:26:25 -07:00
constexpr double SLittle(double val) {
2018-12-07 21:20:24 -08:00
int64_t ival = bswap64(*((int64_t*)(&val)));
return *((double*)(&ival));
2016-03-26 11:34:03 -07:00
}
#ifndef SLITTLE
2016-09-05 13:56:09 -07:00
#define SLITTLE(q) (((q)&0x000000FF) << 24 | ((q)&0x0000FF00) << 8 | ((q)&0x00FF0000) >> 8 | ((q)&0xFF000000) >> 24)
#endif
2016-03-26 11:34:03 -07:00
2019-07-19 21:26:25 -07:00
constexpr int16_t SBig(int16_t val) { return val; }
constexpr uint16_t SBig(uint16_t val) { return val; }
constexpr int32_t SBig(int32_t val) { return val; }
constexpr uint32_t SBig(uint32_t val) { return val; }
constexpr int64_t SBig(int64_t val) { return val; }
constexpr uint64_t SBig(uint64_t val) { return val; }
constexpr float SBig(float val) { return val; }
constexpr double SBig(double val) { return val; }
#ifndef SBIG
2016-03-26 11:34:03 -07:00
#define SBIG(q) (q)
#endif
#endif
2016-03-26 11:34:03 -07:00
2021-06-28 15:58:22 -07:00
#if _WIN32
using Sstat = struct ::_stat64;
2016-03-26 11:34:03 -07:00
#else
2021-06-30 13:21:37 -07:00
using Sstat = struct stat;
2016-03-26 11:34:03 -07:00
#endif
uint64_t getGCTime();
2016-12-28 14:05:48 -08:00
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
2018-12-07 21:20:24 -08:00
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG)
2016-12-28 14:05:48 -08:00
#endif
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
2018-12-07 21:20:24 -08:00
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
2016-12-28 14:05:48 -08:00
#endif
2021-06-28 15:58:22 -07:00
#if _WIN32
int Stat(const char* path, Sstat* statOut);
2016-03-26 11:34:03 -07:00
#else
inline int Stat(const char* path, Sstat* statOut) { return stat(path, statOut); }
2016-03-26 11:34:03 -07:00
#endif
/**
* @brief calculateChecksum
* @param data
* @param len
* @param checksum
* @param checksumInv
*/
void calculateChecksumBE(const uint16_t* data, size_t len, uint16_t* checksum, uint16_t* checksumInv);
2018-02-06 01:34:01 -08:00
#undef NOFILE
2018-12-07 21:20:24 -08:00
enum class ECardResult {
CRC_MISMATCH = -1003, /* Extension enum for Retro's CRC check */
FATAL_ERROR = -128,
ENCODING = -13,
NAMETOOLONG = -12,
INSSPACE = -9,
NOENT = -8,
EXIST = -7,
BROKEN = -6,
IOERROR = -5,
NOFILE = -4,
NOCARD = -3,
WRONGDEVICE = -2,
BUSY = -1,
READY = 0
2018-02-06 01:34:01 -08:00
};
2018-12-07 21:20:24 -08:00
} // namespace kabufuda