Skip to content

Commit

Permalink
feat: REX
Browse files Browse the repository at this point in the history
  • Loading branch information
qudix committed Oct 19, 2024
1 parent 07d8f5d commit 4642125
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
65 changes: 65 additions & 0 deletions CommonLibF4/include/REX/REX.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
#pragma once

namespace REX
{
template <
class E,
class U = std::underlying_type_t<E>>
class Enum
{
public:
using enum_type = E;
using underlying_type = U;

static_assert(std::is_enum_v<E>, "Enum<E, ...> must be an enum");
static_assert(std::is_integral_v<U>, "Enum<..., U> must be an integral");

constexpr Enum() noexcept = default;
constexpr Enum(const Enum&) noexcept = default;
constexpr Enum(Enum&&) noexcept = default;

template <class U2> // NOLINTNEXTLINE(google-explicit-constructor)
constexpr Enum(Enum<E, U2> a_rhs) noexcept :
_impl(static_cast<U>(a_rhs.get()))
{}

constexpr Enum(E a_value) noexcept :
_impl(static_cast<U>(a_value))
{}

~Enum() noexcept = default;

constexpr Enum& operator=(const Enum&) noexcept = default;
constexpr Enum& operator=(Enum&&) noexcept = default;

template <class U2>
constexpr Enum& operator=(Enum<E, U2> a_rhs) noexcept
{
_impl = static_cast<U>(a_rhs.get());
}

constexpr Enum& operator=(E a_value) noexcept
{
_impl = static_cast<U>(a_value);
return *this;
}

public:
[[nodiscard]] explicit constexpr operator bool() const noexcept { return _impl != static_cast<U>(0); }
[[nodiscard]] constexpr E operator*() const noexcept { return get(); }
[[nodiscard]] constexpr E get() const noexcept { return static_cast<E>(_impl); }
[[nodiscard]] constexpr U underlying() const noexcept { return _impl; }

public:
friend constexpr bool operator==(Enum a_lhs, Enum a_rhs) noexcept { return a_lhs.underlying() == a_rhs.underlying(); }
friend constexpr bool operator==(Enum a_lhs, E a_rhs) noexcept { return a_lhs.underlying() == static_cast<U>(a_rhs); }
friend constexpr bool operator==(E a_lhs, Enum a_rhs) noexcept { return static_cast<U>(a_lhs) == a_rhs.underlying(); }

private:
U _impl{ 0 };
};
template <class... Args>
Enum(Args...) -> Enum<
std::common_type_t<Args...>,
std::underlying_type_t<
std::common_type_t<Args...>>>;
}

namespace REX
{
template <
Expand Down
1 change: 1 addition & 0 deletions CommonLibF4/include/REX/W32.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "REX/W32/DXGI_5.h"
#include "REX/W32/DXGI_6.h"
#include "REX/W32/KERNEL32.h"
#include "REX/W32/NT.h"
#include "REX/W32/OLE32.h"
#include "REX/W32/USER32.h"
#include "REX/W32/VERSION.h"
Expand Down
10 changes: 10 additions & 0 deletions CommonLibF4/include/REX/W32/BASE.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ namespace REX::W32
};
std::int64_t value;
};
static_assert(sizeof(LARGE_INTEGER) == 0x8);

union ULARGE_INTEGER
{
Expand All @@ -172,6 +173,15 @@ namespace REX::W32
};
std::uint64_t value;
};
static_assert(sizeof(ULARGE_INTEGER) == 0x8);

struct UNICODE_STRING
{
std::uint16_t length;
std::uint16_t maxLength;
wchar_t* buffer;
};
static_assert(sizeof(UNICODE_STRING) == 0x10);
}

namespace REX::W32
Expand Down
92 changes: 92 additions & 0 deletions CommonLibF4/include/REX/W32/NT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

#include "REX/W32/BASE.h"

namespace REX::W32
{
struct EXCEPTION_REGISTRATION_RECORD;
struct PEB_LDR_DATA;
struct RTL_USER_PROCESS_PARAMETERS;
struct UNICODE_STRING;

using PS_POST_PROCESS_INIT_ROUTINE = void (*)();

struct LIST_ENTRY
{
struct LIST_ENTRY* fLink;
struct LIST_ENTRY* bLink;
};

struct NT_TIB
{
EXCEPTION_REGISTRATION_RECORD* exceptionList;
void* stackBase;
void* stackLimit;
void* subSystemTib;
union
{
void* fiberData;
std::uint32_t version;
};
void* arbitraryUserPointer;
struct NT_TIB* self;
};

struct PEB
{
std::byte reserved1[2];
std::byte beingDebugged;
std::byte reserved2[1];
void* reserved3[2];
PEB_LDR_DATA* ldr;
RTL_USER_PROCESS_PARAMETERS* processParameters;
void* reserved4[3];
void* atlThunkSListPtr;
void* reserved5;
std::uint32_t reserved6;
void* reserved7;
std::uint32_t reserved8;
std::uint32_t atlThunkSListPtr32;
void* reserved9[45];
std::byte reserved10[96];
PS_POST_PROCESS_INIT_ROUTINE postProcessInitRoutine;
std::byte reserved11[128];
void* reserved12[1];
std::uint32_t sessionID;
};

struct PEB_LDR_DATA
{
std::byte reserved1[8];
void* reserved2[3];
LIST_ENTRY inMemoryOrderModuleList;
};

struct RTL_USER_PROCESS_PARAMETERS
{
std::byte reserved1[16];
void* reserved2[10];
UNICODE_STRING imagePathName;
UNICODE_STRING commandLine;
};

struct TEB
{
void* reserved1[11];
void* threadLocalStoragePointer;
PEB* processEnvironmentBlock;
void* reserved2[399];
std::byte reserved3[1952];
void* tlsSlots[64];
std::byte reserved4[8];
void* reserved5[26];
void* reservedForOle;
void* reserved6[4];
void* tlsExpansionSlots;
};
}

namespace REX::W32
{
TEB* NtCurrentTeb() noexcept;
}
11 changes: 11 additions & 0 deletions CommonLibF4/src/REX/W32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "REX/W32/DBGHELP.h"
#include "REX/W32/DXGI.h"
#include "REX/W32/KERNEL32.h"
#include "REX/W32/NT.h"
#include "REX/W32/OLE32.h"
#include "REX/W32/SHELL32.h"
#include "REX/W32/USER32.h"
Expand Down Expand Up @@ -796,6 +797,16 @@ namespace REX::W32
}
}

// NT

namespace REX::W32
{
TEB* NtCurrentTeb() noexcept
{
return reinterpret_cast<TEB*>(__readgsqword(offsetof(NT_TIB, self)));
}
}

// OLE32

REX_W32_IMPORT(void, CoTaskMemFree, void*);
Expand Down

0 comments on commit 4642125

Please sign in to comment.