Initial commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
extern "C"
|
||||
{
|
||||
extern ulong __fastcall sc_asm_alloc_virtual_memory(
|
||||
HANDLE ProcessHandle,
|
||||
PVOID* BaseAddress,
|
||||
ULONG_PTR ZeroBits,
|
||||
PSIZE_T RegionSize,
|
||||
ULONG AllocationType,
|
||||
ULONG Protect
|
||||
);
|
||||
|
||||
extern ulong __fastcall sc_asm_query_virtual_memory(
|
||||
HANDLE ProcessHandle,
|
||||
PVOID BaseAddress,
|
||||
k::MEMORY_INFORMATION_CLASS MemoryInformationClass,
|
||||
PVOID MemoryInformation,
|
||||
SIZE_T MemoryInformationLength,
|
||||
PSIZE_T ReturnLength
|
||||
);
|
||||
|
||||
extern ulong __fastcall sc_asm_delay_execution(
|
||||
BOOLEAN Alertable,
|
||||
PLARGE_INTEGER DelayInterval
|
||||
);
|
||||
|
||||
extern ulong __fastcall sc_asm_get_context_thread(
|
||||
HANDLE Thread,
|
||||
PCONTEXT Context
|
||||
);
|
||||
|
||||
extern ulong __fastcall sc_asm_open_file(
|
||||
PHANDLE FileHandle,
|
||||
ACCESS_MASK DesiredAccess,
|
||||
POBJECT_ATTRIBUTES ObjectAttributes,
|
||||
PIO_STATUS_BLOCK IoStatusBlock,
|
||||
ULONG ShareAccess,
|
||||
ULONG OpenOptions
|
||||
);
|
||||
|
||||
extern ulong __fastcall sc_asm_read_file(
|
||||
HANDLE FileHandle,
|
||||
HANDLE Event,
|
||||
PIO_APC_ROUTINE ApcRoutine,
|
||||
PVOID ApcContext,
|
||||
PIO_STATUS_BLOCK IoStatusBlock,
|
||||
PVOID Buffer,
|
||||
ULONG Length,
|
||||
PLARGE_INTEGER ByteOffset,
|
||||
PULONG Key
|
||||
);
|
||||
|
||||
extern void __fastcall asm_capture_context(
|
||||
PCONTEXT Context
|
||||
);
|
||||
}
|
||||
+1704
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
enum button_states
|
||||
{
|
||||
button_state_invalid = -1,
|
||||
button_state_unassigned = 0,
|
||||
|
||||
button_state_released,
|
||||
button_state_busy,
|
||||
|
||||
button_state_count,
|
||||
};
|
||||
|
||||
struct button_t
|
||||
{
|
||||
int m_state = -1;
|
||||
bool m_pressed = false;
|
||||
};
|
||||
|
||||
class c_synced_input
|
||||
{
|
||||
public:
|
||||
void sync(std::array<button_t, 255>& buttons);
|
||||
bool is_button_down(uintptr_t key_code) noexcept;
|
||||
bool was_button_down(uintptr_t key_code) noexcept;
|
||||
private:
|
||||
std::array<button_t, 255> m_buttons;
|
||||
std::array<button_t, 255> m_buttons_prev;
|
||||
};
|
||||
|
||||
class c_input_manager
|
||||
{
|
||||
public:
|
||||
void update();
|
||||
void window_proc(UINT msg, WPARAM w_param, LPARAM l_param);
|
||||
void sync(c_synced_input* si);
|
||||
public:
|
||||
bool is_button_down(uintptr_t key_code) noexcept;
|
||||
math::position_t<int> get_cursor_pos() noexcept;
|
||||
private:
|
||||
void internal_handler_key_down(uintptr_t key_code);
|
||||
void internal_handler_key_up(uintptr_t key_code);
|
||||
private:
|
||||
math::position_t<int> m_cursor_pos;
|
||||
|
||||
std::array<button_t, 255> m_buttons;
|
||||
std::array<button_t, 255> m_buttons_prev;
|
||||
};
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
#pragma once
|
||||
|
||||
#include <pshpack1.h>
|
||||
|
||||
namespace instr
|
||||
{
|
||||
struct jump_t
|
||||
{
|
||||
size_t length() const
|
||||
{
|
||||
return sizeof((*this));
|
||||
}
|
||||
|
||||
void assign_data(uint8_t mov_register = 0xB8,
|
||||
uint8_t jmp_register = 0xE0)
|
||||
{
|
||||
memset(this, 0, sizeof(jump_t));
|
||||
|
||||
// mov rax, qword ptr (or something?!) <address>
|
||||
m_mov[0] = 0x48;
|
||||
m_mov[1] = mov_register;
|
||||
|
||||
// jmp rax
|
||||
m_jmp[0] = 0xFF;
|
||||
m_jmp[1] = jmp_register;
|
||||
}
|
||||
|
||||
jump_t()
|
||||
{
|
||||
assign_data();
|
||||
};
|
||||
|
||||
~jump_t() = default;
|
||||
|
||||
jump_t(uintptr_t destination)
|
||||
{
|
||||
assign_data();
|
||||
|
||||
memcpy(&m_dst[0], &destination, sizeof(uintptr_t));
|
||||
}
|
||||
|
||||
jump_t(uintptr_t destination,
|
||||
uint8_t mov_register, uint8_t jmp_register)
|
||||
{
|
||||
assign_data(mov_register, jmp_register);
|
||||
|
||||
memcpy(&m_dst[0], &destination, sizeof(uintptr_t));
|
||||
}
|
||||
|
||||
inline void set_registers(uint8_t mov_register, uint8_t jmp_register)
|
||||
{
|
||||
m_mov[1] = mov_register;
|
||||
m_jmp[1] = jmp_register;
|
||||
}
|
||||
|
||||
UINT8 m_mov[2];
|
||||
UINT8 m_dst[8];
|
||||
UINT8 m_jmp[2];
|
||||
};
|
||||
|
||||
struct jump_safe_t
|
||||
{
|
||||
void assign_data()
|
||||
{
|
||||
memset(this, 0, sizeof(jump_safe_t));
|
||||
|
||||
uint8_t pre[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
memcpy(m_pre, pre, sizeof(pre));
|
||||
}
|
||||
|
||||
jump_safe_t()
|
||||
{
|
||||
assign_data();
|
||||
};
|
||||
|
||||
~jump_safe_t() = default;
|
||||
|
||||
jump_safe_t(uintptr_t destination)
|
||||
{
|
||||
assign_data();
|
||||
|
||||
memcpy(&m_dst[0], &destination, sizeof(uintptr_t));
|
||||
}
|
||||
uint8_t m_pre[6];
|
||||
uint8_t m_dst[8];
|
||||
};
|
||||
|
||||
struct jump6_t
|
||||
{
|
||||
static jump6_t build(void* dst)
|
||||
{
|
||||
return jump6_t
|
||||
{
|
||||
0x50,
|
||||
{ 0x48, 0xB8 },
|
||||
dst,
|
||||
{ 0x48, 0x87, 0x04, 0x24 },
|
||||
0xC3
|
||||
};
|
||||
}
|
||||
|
||||
uint8_t m_push;
|
||||
uint8_t m_mov[2];
|
||||
void* m_dst;
|
||||
uint8_t m_rstor[4];
|
||||
uint8_t m_ret;
|
||||
};
|
||||
|
||||
struct system_call_t
|
||||
{
|
||||
system_call_t() = default;
|
||||
|
||||
system_call_t(uint32_t id)
|
||||
{
|
||||
memset(this, 0, sizeof(system_call_t));
|
||||
|
||||
// mov r10, rcx ;
|
||||
m_mov_r10[0] = 0x49;
|
||||
m_mov_r10[1] = 0x89;
|
||||
m_mov_r10[2] = 0xCA;
|
||||
|
||||
// mov eax, <id> ;
|
||||
m_mov_eax = 0xB8;
|
||||
|
||||
memcpy(&m_id, &id, sizeof(id));
|
||||
|
||||
// sycall ;
|
||||
m_call[0] = 0x0F;
|
||||
m_call[1] = 0x05;
|
||||
|
||||
// retn ;
|
||||
m_return = 0xC3;
|
||||
}
|
||||
|
||||
void* stub() noexcept;
|
||||
|
||||
std::uint8_t m_mov_r10[0x3];
|
||||
std::uint8_t m_mov_eax;
|
||||
std::uint8_t m_id[0x4];
|
||||
std::uint8_t m_call[0x2];
|
||||
std::uint8_t m_return;
|
||||
};
|
||||
}
|
||||
|
||||
#include <poppack.h>
|
||||
+474
@@ -0,0 +1,474 @@
|
||||
#pragma once
|
||||
#define JSMN_STATIC
|
||||
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2010 Serge Zaitsev
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#ifndef JSMN_H
|
||||
#define JSMN_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef JSMN_STATIC
|
||||
#define JSMN_API inline_block static
|
||||
#else
|
||||
#define JSMN_API extern
|
||||
#endif
|
||||
|
||||
/**
|
||||
* JSON type identifier. Basic types are:
|
||||
* o Object
|
||||
* o Array
|
||||
* o String
|
||||
* o Other primitive: number, boolean (true/false) or null
|
||||
*/
|
||||
typedef enum {
|
||||
JSMN_UNDEFINED = 0,
|
||||
JSMN_OBJECT = 1 << 0,
|
||||
JSMN_ARRAY = 1 << 1,
|
||||
JSMN_STRING = 1 << 2,
|
||||
JSMN_PRIMITIVE = 1 << 3
|
||||
} jsmntype_t;
|
||||
|
||||
enum jsmnerr {
|
||||
/* Not enough tokens were provided */
|
||||
JSMN_ERROR_NOMEM = -1,
|
||||
/* Invalid character inside JSON string */
|
||||
JSMN_ERROR_INVAL = -2,
|
||||
/* The string is not a full JSON packet, more bytes expected */
|
||||
JSMN_ERROR_PART = -3
|
||||
};
|
||||
|
||||
/**
|
||||
* JSON token description.
|
||||
* type type (object, array, string etc.)
|
||||
* start start position in JSON data string
|
||||
* end end position in JSON data string
|
||||
*/
|
||||
typedef struct jsmntok {
|
||||
jsmntype_t type;
|
||||
int start;
|
||||
int end;
|
||||
int size;
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
int parent;
|
||||
#endif
|
||||
} jsmntok_t;
|
||||
|
||||
/**
|
||||
* JSON parser. Contains an array of token blocks available. Also stores
|
||||
* the string being parsed now and current position in that string.
|
||||
*/
|
||||
typedef struct jsmn_parser {
|
||||
unsigned int pos; /* offset in the JSON string */
|
||||
unsigned int toknext; /* next token to allocate */
|
||||
int toksuper; /* superior token node, e.g. parent object or array */
|
||||
} jsmn_parser;
|
||||
|
||||
/**
|
||||
* Create JSON parser over an array of tokens
|
||||
*/
|
||||
JSMN_API void jsmn_init(jsmn_parser* parser);
|
||||
|
||||
/**
|
||||
* Run JSON parser. It parses a JSON data string into and array of tokens, each
|
||||
* describing
|
||||
* a single JSON object.
|
||||
*/
|
||||
JSMN_API int jsmn_parse(jsmn_parser* parser, const char* js, const size_t len,
|
||||
jsmntok_t* tokens, const unsigned int num_tokens);
|
||||
|
||||
#ifndef JSMN_HEADER
|
||||
/**
|
||||
* Allocates a fresh unused token from the token pool.
|
||||
*/
|
||||
static jsmntok_t* jsmn_alloc_token(jsmn_parser* parser, jsmntok_t* tokens,
|
||||
const size_t num_tokens) {
|
||||
jsmntok_t* tok;
|
||||
if (parser->toknext >= num_tokens) {
|
||||
return NULL;
|
||||
}
|
||||
tok = &tokens[parser->toknext++];
|
||||
tok->start = tok->end = -1;
|
||||
tok->size = 0;
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
tok->parent = -1;
|
||||
#endif
|
||||
return tok;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills token type and boundaries.
|
||||
*/
|
||||
static void jsmn_fill_token(jsmntok_t* token, const jsmntype_t type,
|
||||
const int start, const int end) {
|
||||
token->type = type;
|
||||
token->start = start;
|
||||
token->end = end;
|
||||
token->size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills next available token with JSON primitive.
|
||||
*/
|
||||
static int jsmn_parse_primitive(jsmn_parser* parser, const char* js,
|
||||
const size_t len, jsmntok_t* tokens,
|
||||
const size_t num_tokens) {
|
||||
jsmntok_t* token;
|
||||
int start;
|
||||
|
||||
start = parser->pos;
|
||||
|
||||
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
|
||||
switch (js[parser->pos]) {
|
||||
#ifndef JSMN_STRICT
|
||||
/* In strict mode primitive must be followed by "," or "}" or "]" */
|
||||
case ':':
|
||||
#endif
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
case ' ':
|
||||
case ',':
|
||||
case ']':
|
||||
case '}':
|
||||
goto found;
|
||||
default:
|
||||
/* to quiet a warning from gcc*/
|
||||
break;
|
||||
}
|
||||
if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
}
|
||||
#ifdef JSMN_STRICT
|
||||
/* In strict mode primitive must be followed by a comma/object/array */
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_PART;
|
||||
#endif
|
||||
|
||||
found:
|
||||
if (tokens == NULL) {
|
||||
parser->pos--;
|
||||
return 0;
|
||||
}
|
||||
token = jsmn_alloc_token(parser, tokens, num_tokens);
|
||||
if (token == NULL) {
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_NOMEM;
|
||||
}
|
||||
jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
token->parent = parser->toksuper;
|
||||
#endif
|
||||
parser->pos--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills next token with JSON string.
|
||||
*/
|
||||
static int jsmn_parse_string(jsmn_parser* parser, const char* js,
|
||||
const size_t len, jsmntok_t* tokens,
|
||||
const size_t num_tokens) {
|
||||
jsmntok_t* token;
|
||||
|
||||
int start = parser->pos;
|
||||
|
||||
/* Skip starting quote */
|
||||
parser->pos++;
|
||||
|
||||
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
|
||||
char c = js[parser->pos];
|
||||
|
||||
/* Quote: end of string */
|
||||
if (c == '\"') {
|
||||
if (tokens == NULL) {
|
||||
return 0;
|
||||
}
|
||||
token = jsmn_alloc_token(parser, tokens, num_tokens);
|
||||
if (token == NULL) {
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_NOMEM;
|
||||
}
|
||||
jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
token->parent = parser->toksuper;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Backslash: Quoted symbol expected */
|
||||
if (c == '\\' && parser->pos + 1 < len) {
|
||||
int i;
|
||||
parser->pos++;
|
||||
switch (js[parser->pos]) {
|
||||
/* Allowed escaped symbols */
|
||||
case '\"':
|
||||
case '/':
|
||||
case '\\':
|
||||
case 'b':
|
||||
case 'f':
|
||||
case 'r':
|
||||
case 'n':
|
||||
case 't':
|
||||
break;
|
||||
/* Allows escaped symbol \uXXXX */
|
||||
case 'u':
|
||||
parser->pos++;
|
||||
for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0';
|
||||
i++) {
|
||||
/* If it isn't a hex character we have an error */
|
||||
if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
|
||||
(js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
|
||||
(js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
parser->pos++;
|
||||
}
|
||||
parser->pos--;
|
||||
break;
|
||||
/* Unexpected symbol */
|
||||
default:
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
parser->pos = start;
|
||||
return JSMN_ERROR_PART;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse JSON string and fill tokens.
|
||||
*/
|
||||
JSMN_API int jsmn_parse(jsmn_parser* parser, const char* js, const size_t len,
|
||||
jsmntok_t* tokens, const unsigned int num_tokens) {
|
||||
int r;
|
||||
int i;
|
||||
jsmntok_t* token;
|
||||
int count = parser->toknext;
|
||||
|
||||
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
|
||||
char c;
|
||||
jsmntype_t type;
|
||||
|
||||
c = js[parser->pos];
|
||||
switch (c) {
|
||||
case '{':
|
||||
case '[':
|
||||
count++;
|
||||
if (tokens == NULL) {
|
||||
break;
|
||||
}
|
||||
token = jsmn_alloc_token(parser, tokens, num_tokens);
|
||||
if (token == NULL) {
|
||||
return JSMN_ERROR_NOMEM;
|
||||
}
|
||||
if (parser->toksuper != -1) {
|
||||
jsmntok_t* t = &tokens[parser->toksuper];
|
||||
#ifdef JSMN_STRICT
|
||||
/* In strict mode an object or array can't become a key */
|
||||
if (t->type == JSMN_OBJECT) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
#endif
|
||||
t->size++;
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
token->parent = parser->toksuper;
|
||||
#endif
|
||||
}
|
||||
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
|
||||
token->start = parser->pos;
|
||||
parser->toksuper = parser->toknext - 1;
|
||||
break;
|
||||
case '}':
|
||||
case ']':
|
||||
if (tokens == NULL) {
|
||||
break;
|
||||
}
|
||||
type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
if (parser->toknext < 1) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
token = &tokens[parser->toknext - 1];
|
||||
for (;;) {
|
||||
if (token->start != -1 && token->end == -1) {
|
||||
if (token->type != type) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
token->end = parser->pos + 1;
|
||||
parser->toksuper = token->parent;
|
||||
break;
|
||||
}
|
||||
if (token->parent == -1) {
|
||||
if (token->type != type || parser->toksuper == -1) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
break;
|
||||
}
|
||||
token = &tokens[token->parent];
|
||||
}
|
||||
#else
|
||||
for (i = parser->toknext - 1; i >= 0; i--) {
|
||||
token = &tokens[i];
|
||||
if (token->start != -1 && token->end == -1) {
|
||||
if (token->type != type) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
parser->toksuper = -1;
|
||||
token->end = parser->pos + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Error if unmatched closing bracket */
|
||||
if (i == -1) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
for (; i >= 0; i--) {
|
||||
token = &tokens[i];
|
||||
if (token->start != -1 && token->end == -1) {
|
||||
parser->toksuper = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case '\"':
|
||||
r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
|
||||
if (r < 0) {
|
||||
return r;
|
||||
}
|
||||
count++;
|
||||
if (parser->toksuper != -1 && tokens != NULL) {
|
||||
tokens[parser->toksuper].size++;
|
||||
}
|
||||
break;
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
case ' ':
|
||||
break;
|
||||
case ':':
|
||||
parser->toksuper = parser->toknext - 1;
|
||||
break;
|
||||
case ',':
|
||||
if (tokens != NULL && parser->toksuper != -1 &&
|
||||
tokens[parser->toksuper].type != JSMN_ARRAY &&
|
||||
tokens[parser->toksuper].type != JSMN_OBJECT) {
|
||||
#ifdef JSMN_PARENT_LINKS
|
||||
parser->toksuper = tokens[parser->toksuper].parent;
|
||||
#else
|
||||
for (i = parser->toknext - 1; i >= 0; i--) {
|
||||
if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
|
||||
if (tokens[i].start != -1 && tokens[i].end == -1) {
|
||||
parser->toksuper = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
#ifdef JSMN_STRICT
|
||||
/* In strict mode primitives are: numbers and booleans */
|
||||
case '-':
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
case 't':
|
||||
case 'f':
|
||||
case 'n':
|
||||
/* And they must not be keys of the object */
|
||||
if (tokens != NULL && parser->toksuper != -1) {
|
||||
const jsmntok_t* t = &tokens[parser->toksuper];
|
||||
if (t->type == JSMN_OBJECT ||
|
||||
(t->type == JSMN_STRING && t->size != 0)) {
|
||||
return JSMN_ERROR_INVAL;
|
||||
}
|
||||
}
|
||||
#else
|
||||
/* In non-strict mode every unquoted value is a primitive */
|
||||
default:
|
||||
#endif
|
||||
r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
|
||||
if (r < 0) {
|
||||
return r;
|
||||
}
|
||||
count++;
|
||||
if (parser->toksuper != -1 && tokens != NULL) {
|
||||
tokens[parser->toksuper].size++;
|
||||
}
|
||||
break;
|
||||
|
||||
#ifdef JSMN_STRICT
|
||||
/* Unexpected char in strict mode */
|
||||
default:
|
||||
return JSMN_ERROR_INVAL;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (tokens != NULL) {
|
||||
for (i = parser->toknext - 1; i >= 0; i--) {
|
||||
/* Unmatched opened object or array */
|
||||
if (tokens[i].start != -1 && tokens[i].end == -1) {
|
||||
return JSMN_ERROR_PART;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new parser based over a given buffer with an array of tokens
|
||||
* available.
|
||||
*/
|
||||
JSMN_API void jsmn_init(jsmn_parser* parser) {
|
||||
parser->pos = 0;
|
||||
parser->toknext = 0;
|
||||
parser->toksuper = -1;
|
||||
}
|
||||
|
||||
#endif /* JSMN_HEADER */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* JSMN_H */
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(disable : 4996)
|
||||
|
||||
class c_json
|
||||
{
|
||||
public:
|
||||
c_json() = default;
|
||||
~c_json();
|
||||
|
||||
void reset() noexcept;
|
||||
|
||||
bool parse(const char* data) noexcept;
|
||||
bool is_equal(int index, const char* text);
|
||||
|
||||
const auto count() { return m_count; }
|
||||
|
||||
int get_value_integer(const char* name);
|
||||
bool get_value_boolean(const char* name);
|
||||
double get_value_double(const char* name);
|
||||
private:
|
||||
void get_value_internal(int index, char* buffer, size_t length);
|
||||
private:
|
||||
int m_count;
|
||||
char* m_json;
|
||||
jsmn_parser m_parser;
|
||||
jsmntok_t m_tok[1024];
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
namespace k
|
||||
{
|
||||
typedef struct _SYSTEM_MODULE_ENTRY
|
||||
{
|
||||
HANDLE Section;
|
||||
PVOID MappedBase;
|
||||
PVOID ImageBase;
|
||||
ULONG ImageSize;
|
||||
ULONG Flags;
|
||||
USHORT LoadOrderIndex;
|
||||
USHORT InitOrderIndex;
|
||||
USHORT LoadCount;
|
||||
USHORT OffsetToFileName;
|
||||
UCHAR FullPathName[256];
|
||||
} SYSTEM_MODULE_ENTRY, * PSYSTEM_MODULE_ENTRY;
|
||||
|
||||
typedef enum _MEMORY_INFORMATION_CLASS {
|
||||
MemoryBasicInformation
|
||||
} MEMORY_INFORMATION_CLASS;
|
||||
}
|
||||
@@ -0,0 +1,700 @@
|
||||
/*
|
||||
* Copyright 2018-2020 Justas Masiulis
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// documentation is available at https://github.com/JustasMasiulis/lazy_importer
|
||||
|
||||
#ifndef LAZY_IMPORTER_HPP
|
||||
#define LAZY_IMPORTER_HPP
|
||||
|
||||
#define LI_FN(name) \
|
||||
::li::detail::lazy_function<::li::detail::khash(#name), decltype(&name)>()
|
||||
|
||||
#define LI_FN_DEF(name) ::li::detail::lazy_function<::li::detail::khash(#name), name>()
|
||||
|
||||
#define LI_MODULE(name) ::li::detail::lazy_module<::li::detail::khash(name)>()
|
||||
|
||||
// NOTE only std::forward is used from this header.
|
||||
// If there is a need to eliminate this dependency the function itself is very small.
|
||||
|
||||
#include <utility>
|
||||
#include <cstddef>
|
||||
#include <intrin.h>
|
||||
|
||||
#ifndef LAZY_IMPORTER_NO_FORCEINLINE
|
||||
#if defined(_MSC_VER)
|
||||
#define LAZY_IMPORTER_FORCEINLINE inline
|
||||
#elif defined(__GNUC__) && __GNUC__ > 3
|
||||
#define LAZY_IMPORTER_FORCEINLINE inline __attribute__((__always_inline__))
|
||||
#else
|
||||
#define LAZY_IMPORTER_FORCEINLINE inline
|
||||
#endif
|
||||
#else
|
||||
#define LAZY_IMPORTER_FORCEINLINE inline
|
||||
#endif
|
||||
|
||||
#ifdef LAZY_IMPORTER_CASE_INSENSITIVE
|
||||
#define LAZY_IMPORTER_TOLOWER(c) (c >= 'A' && c <= 'Z' ? (c | (1 << 5)) : c)
|
||||
#else
|
||||
#define LAZY_IMPORTER_TOLOWER(c) (c)
|
||||
#endif
|
||||
|
||||
namespace li {
|
||||
namespace detail {
|
||||
|
||||
template<class First, class Second>
|
||||
struct pair {
|
||||
First first;
|
||||
Second second;
|
||||
};
|
||||
|
||||
namespace win {
|
||||
|
||||
struct LIST_ENTRY_T {
|
||||
const char* Flink;
|
||||
const char* Blink;
|
||||
};
|
||||
|
||||
struct UNICODE_STRING_T {
|
||||
unsigned short Length;
|
||||
unsigned short MaximumLength;
|
||||
wchar_t* Buffer;
|
||||
};
|
||||
|
||||
struct PEB_LDR_DATA_T {
|
||||
unsigned long Length;
|
||||
unsigned long Initialized;
|
||||
const char* SsHandle;
|
||||
LIST_ENTRY_T InLoadOrderModuleList;
|
||||
};
|
||||
|
||||
struct PEB_T {
|
||||
unsigned char Reserved1[2];
|
||||
unsigned char BeingDebugged;
|
||||
unsigned char Reserved2[1];
|
||||
const char* Reserved3[2];
|
||||
PEB_LDR_DATA_T* Ldr;
|
||||
};
|
||||
|
||||
struct LDR_DATA_TABLE_ENTRY_T {
|
||||
LIST_ENTRY_T InLoadOrderLinks;
|
||||
LIST_ENTRY_T InMemoryOrderLinks;
|
||||
LIST_ENTRY_T InInitializationOrderLinks;
|
||||
const char* DllBase;
|
||||
const char* EntryPoint;
|
||||
union {
|
||||
unsigned long SizeOfImage;
|
||||
const char* _dummy;
|
||||
};
|
||||
UNICODE_STRING_T FullDllName;
|
||||
UNICODE_STRING_T BaseDllName;
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const LDR_DATA_TABLE_ENTRY_T*
|
||||
load_order_next() const noexcept
|
||||
{
|
||||
return reinterpret_cast<const LDR_DATA_TABLE_ENTRY_T*>(
|
||||
InLoadOrderLinks.Flink);
|
||||
}
|
||||
};
|
||||
|
||||
struct IMAGE_DOS_HEADER { // DOS .EXE header
|
||||
unsigned short e_magic; // Magic number
|
||||
unsigned short e_cblp; // Bytes on last page of file
|
||||
unsigned short e_cp; // Pages in file
|
||||
unsigned short e_crlc; // Relocations
|
||||
unsigned short e_cparhdr; // Size of header in paragraphs
|
||||
unsigned short e_minalloc; // Minimum extra paragraphs needed
|
||||
unsigned short e_maxalloc; // Maximum extra paragraphs needed
|
||||
unsigned short e_ss; // Initial (relative) SS value
|
||||
unsigned short e_sp; // Initial SP value
|
||||
unsigned short e_csum; // Checksum
|
||||
unsigned short e_ip; // Initial IP value
|
||||
unsigned short e_cs; // Initial (relative) CS value
|
||||
unsigned short e_lfarlc; // File address of relocation table
|
||||
unsigned short e_ovno; // Overlay number
|
||||
unsigned short e_res[4]; // Reserved words
|
||||
unsigned short e_oemid; // OEM identifier (for e_oeminfo)
|
||||
unsigned short e_oeminfo; // OEM information; e_oemid specific
|
||||
unsigned short e_res2[10]; // Reserved words
|
||||
long e_lfanew; // File address of new exe header
|
||||
};
|
||||
|
||||
struct IMAGE_FILE_HEADER {
|
||||
unsigned short Machine;
|
||||
unsigned short NumberOfSections;
|
||||
unsigned long TimeDateStamp;
|
||||
unsigned long PointerToSymbolTable;
|
||||
unsigned long NumberOfSymbols;
|
||||
unsigned short SizeOfOptionalHeader;
|
||||
unsigned short Characteristics;
|
||||
};
|
||||
|
||||
struct IMAGE_EXPORT_DIRECTORY {
|
||||
unsigned long Characteristics;
|
||||
unsigned long TimeDateStamp;
|
||||
unsigned short MajorVersion;
|
||||
unsigned short MinorVersion;
|
||||
unsigned long Name;
|
||||
unsigned long Base;
|
||||
unsigned long NumberOfFunctions;
|
||||
unsigned long NumberOfNames;
|
||||
unsigned long AddressOfFunctions; // RVA from base of image
|
||||
unsigned long AddressOfNames; // RVA from base of image
|
||||
unsigned long AddressOfNameOrdinals; // RVA from base of image
|
||||
};
|
||||
|
||||
struct IMAGE_DATA_DIRECTORY {
|
||||
unsigned long VirtualAddress;
|
||||
unsigned long Size;
|
||||
};
|
||||
|
||||
struct IMAGE_OPTIONAL_HEADER64 {
|
||||
unsigned short Magic;
|
||||
unsigned char MajorLinkerVersion;
|
||||
unsigned char MinorLinkerVersion;
|
||||
unsigned long SizeOfCode;
|
||||
unsigned long SizeOfInitializedData;
|
||||
unsigned long SizeOfUninitializedData;
|
||||
unsigned long AddressOfEntryPoint;
|
||||
unsigned long BaseOfCode;
|
||||
unsigned long long ImageBase;
|
||||
unsigned long SectionAlignment;
|
||||
unsigned long FileAlignment;
|
||||
unsigned short MajorOperatingSystemVersion;
|
||||
unsigned short MinorOperatingSystemVersion;
|
||||
unsigned short MajorImageVersion;
|
||||
unsigned short MinorImageVersion;
|
||||
unsigned short MajorSubsystemVersion;
|
||||
unsigned short MinorSubsystemVersion;
|
||||
unsigned long Win32VersionValue;
|
||||
unsigned long SizeOfImage;
|
||||
unsigned long SizeOfHeaders;
|
||||
unsigned long CheckSum;
|
||||
unsigned short Subsystem;
|
||||
unsigned short DllCharacteristics;
|
||||
unsigned long long SizeOfStackReserve;
|
||||
unsigned long long SizeOfStackCommit;
|
||||
unsigned long long SizeOfHeapReserve;
|
||||
unsigned long long SizeOfHeapCommit;
|
||||
unsigned long LoaderFlags;
|
||||
unsigned long NumberOfRvaAndSizes;
|
||||
IMAGE_DATA_DIRECTORY DataDirectory[16];
|
||||
};
|
||||
|
||||
struct IMAGE_OPTIONAL_HEADER32 {
|
||||
unsigned short Magic;
|
||||
unsigned char MajorLinkerVersion;
|
||||
unsigned char MinorLinkerVersion;
|
||||
unsigned long SizeOfCode;
|
||||
unsigned long SizeOfInitializedData;
|
||||
unsigned long SizeOfUninitializedData;
|
||||
unsigned long AddressOfEntryPoint;
|
||||
unsigned long BaseOfCode;
|
||||
unsigned long BaseOfData;
|
||||
unsigned long ImageBase;
|
||||
unsigned long SectionAlignment;
|
||||
unsigned long FileAlignment;
|
||||
unsigned short MajorOperatingSystemVersion;
|
||||
unsigned short MinorOperatingSystemVersion;
|
||||
unsigned short MajorImageVersion;
|
||||
unsigned short MinorImageVersion;
|
||||
unsigned short MajorSubsystemVersion;
|
||||
unsigned short MinorSubsystemVersion;
|
||||
unsigned long Win32VersionValue;
|
||||
unsigned long SizeOfImage;
|
||||
unsigned long SizeOfHeaders;
|
||||
unsigned long CheckSum;
|
||||
unsigned short Subsystem;
|
||||
unsigned short DllCharacteristics;
|
||||
unsigned long SizeOfStackReserve;
|
||||
unsigned long SizeOfStackCommit;
|
||||
unsigned long SizeOfHeapReserve;
|
||||
unsigned long SizeOfHeapCommit;
|
||||
unsigned long LoaderFlags;
|
||||
unsigned long NumberOfRvaAndSizes;
|
||||
IMAGE_DATA_DIRECTORY DataDirectory[16];
|
||||
};
|
||||
|
||||
struct IMAGE_NT_HEADERS {
|
||||
unsigned long Signature;
|
||||
IMAGE_FILE_HEADER FileHeader;
|
||||
#ifdef _WIN64
|
||||
IMAGE_OPTIONAL_HEADER64 OptionalHeader;
|
||||
#else
|
||||
IMAGE_OPTIONAL_HEADER32 OptionalHeader;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace win
|
||||
|
||||
// hashing stuff
|
||||
struct hash_t {
|
||||
using value_type = unsigned long;
|
||||
constexpr static value_type offset = 2166136261;
|
||||
constexpr static value_type prime = 16777619;
|
||||
constexpr static unsigned long long prime64 = prime;
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE constexpr static value_type single(value_type value,
|
||||
char c) noexcept
|
||||
{
|
||||
return static_cast<hash_t::value_type>(
|
||||
(value ^ LAZY_IMPORTER_TOLOWER(c)) *
|
||||
static_cast<unsigned long long>(prime));
|
||||
}
|
||||
};
|
||||
|
||||
template<class CharT = char>
|
||||
LAZY_IMPORTER_FORCEINLINE constexpr hash_t::value_type
|
||||
khash(const CharT* str, hash_t::value_type value = hash_t::offset) noexcept
|
||||
{
|
||||
return (*str ? khash(str + 1, hash_t::single(value, *str)) : value);
|
||||
}
|
||||
|
||||
template<class CharT = char>
|
||||
LAZY_IMPORTER_FORCEINLINE hash_t::value_type hash(const CharT* str) noexcept
|
||||
{
|
||||
hash_t::value_type value = hash_t::offset;
|
||||
|
||||
for (;;) {
|
||||
char c = *str++;
|
||||
if (!c)
|
||||
return value;
|
||||
value = hash_t::single(value, c);
|
||||
}
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE hash_t::value_type hash(
|
||||
const win::UNICODE_STRING_T& str) noexcept
|
||||
{
|
||||
auto first = str.Buffer;
|
||||
const auto last = first + (str.Length / sizeof(wchar_t));
|
||||
auto value = hash_t::offset;
|
||||
for (; first != last; ++first)
|
||||
value = hash_t::single(value, static_cast<char>(*first));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE pair<hash_t::value_type, hash_t::value_type> hash_forwarded(
|
||||
const char* str) noexcept
|
||||
{
|
||||
pair<hash_t::value_type, hash_t::value_type> module_and_function{
|
||||
hash_t::offset, hash_t::offset
|
||||
};
|
||||
|
||||
for (; *str != '.'; ++str)
|
||||
module_and_function.first = hash_t::single(module_and_function.first, *str);
|
||||
|
||||
++str;
|
||||
|
||||
for (; *str; ++str)
|
||||
module_and_function.second = hash_t::single(module_and_function.second, *str);
|
||||
|
||||
return module_and_function;
|
||||
}
|
||||
|
||||
|
||||
// some helper functions
|
||||
LAZY_IMPORTER_FORCEINLINE const win::PEB_T* peb() noexcept
|
||||
{
|
||||
#if defined(_M_X64) || defined(__amd64__)
|
||||
return reinterpret_cast<const win::PEB_T*>(__readgsqword(0x60));
|
||||
#elif defined(_M_IX86) || defined(__i386__)
|
||||
return reinterpret_cast<const win::PEB_T*>(__readfsdword(0x30));
|
||||
#elif defined(_M_ARM) || defined(__arm__)
|
||||
return *reinterpret_cast<const win::PEB_T**>(_MoveFromCoprocessor(15, 0, 13, 0, 2) + 0x30);
|
||||
#elif defined(_M_ARM64) || defined(__aarch64__)
|
||||
return *reinterpret_cast<const win::PEB_T**>(__getReg(18) + 0x60);
|
||||
#elif defined(_M_IA64) || defined(__ia64__)
|
||||
return *reinterpret_cast<const win::PEB_T**>(static_cast<char*>(_rdteb()) + 0x60);
|
||||
#else
|
||||
#error Unsupported platform. Open an issue and I'll probably add support.
|
||||
#endif
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const win::PEB_LDR_DATA_T* ldr()
|
||||
{
|
||||
return reinterpret_cast<const win::PEB_LDR_DATA_T*>(peb()->Ldr);
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const win::IMAGE_NT_HEADERS* nt_headers(
|
||||
const char* base) noexcept
|
||||
{
|
||||
return reinterpret_cast<const win::IMAGE_NT_HEADERS*>(
|
||||
base + reinterpret_cast<const win::IMAGE_DOS_HEADER*>(base)->e_lfanew);
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const win::IMAGE_EXPORT_DIRECTORY* image_export_dir(
|
||||
const char* base) noexcept
|
||||
{
|
||||
return reinterpret_cast<const win::IMAGE_EXPORT_DIRECTORY*>(
|
||||
base + nt_headers(base)->OptionalHeader.DataDirectory->VirtualAddress);
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const win::LDR_DATA_TABLE_ENTRY_T* ldr_data_entry() noexcept
|
||||
{
|
||||
return reinterpret_cast<const win::LDR_DATA_TABLE_ENTRY_T*>(
|
||||
ldr()->InLoadOrderModuleList.Flink);
|
||||
}
|
||||
|
||||
struct exports_directory {
|
||||
const char* _base;
|
||||
const win::IMAGE_EXPORT_DIRECTORY* _ied;
|
||||
unsigned long _ied_size;
|
||||
|
||||
public:
|
||||
using size_type = unsigned long;
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE
|
||||
exports_directory(const char* base) noexcept : _base(base)
|
||||
{
|
||||
const auto ied_data_dir = nt_headers(base)->OptionalHeader.DataDirectory[0];
|
||||
_ied = reinterpret_cast<const win::IMAGE_EXPORT_DIRECTORY*>(
|
||||
base + ied_data_dir.VirtualAddress);
|
||||
_ied_size = ied_data_dir.Size;
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE explicit operator bool() const noexcept
|
||||
{
|
||||
return reinterpret_cast<const char*>(_ied) != _base;
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE size_type size() const noexcept
|
||||
{
|
||||
return _ied->NumberOfNames;
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const char* base() const noexcept { return _base; }
|
||||
LAZY_IMPORTER_FORCEINLINE const win::IMAGE_EXPORT_DIRECTORY* ied() const noexcept
|
||||
{
|
||||
return _ied;
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const char* name(size_type index) const noexcept
|
||||
{
|
||||
return reinterpret_cast<const char*>(
|
||||
_base + reinterpret_cast<const unsigned long*>(
|
||||
_base + _ied->AddressOfNames)[index]);
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE const char* address(size_type index) const noexcept
|
||||
{
|
||||
const auto* const rva_table =
|
||||
reinterpret_cast<const unsigned long*>(_base + _ied->AddressOfFunctions);
|
||||
|
||||
const auto* const ord_table = reinterpret_cast<const unsigned short*>(
|
||||
_base + _ied->AddressOfNameOrdinals);
|
||||
|
||||
return _base + rva_table[ord_table[index]];
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE bool is_forwarded(
|
||||
const char* export_address) const noexcept
|
||||
{
|
||||
const auto ui_ied = reinterpret_cast<const char*>(_ied);
|
||||
return (export_address > ui_ied && export_address < ui_ied + _ied_size);
|
||||
}
|
||||
};
|
||||
|
||||
struct safe_module_enumerator {
|
||||
using value_type = const detail::win::LDR_DATA_TABLE_ENTRY_T;
|
||||
value_type* value;
|
||||
value_type* head;
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE safe_module_enumerator() noexcept
|
||||
: safe_module_enumerator(ldr_data_entry())
|
||||
{}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE
|
||||
safe_module_enumerator(const detail::win::LDR_DATA_TABLE_ENTRY_T* ldr) noexcept
|
||||
: value(ldr->load_order_next()), head(value)
|
||||
{}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE void reset() noexcept
|
||||
{
|
||||
value = head->load_order_next();
|
||||
}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE bool next() noexcept
|
||||
{
|
||||
value = value->load_order_next();
|
||||
|
||||
return value != head && value->DllBase;
|
||||
}
|
||||
};
|
||||
|
||||
struct unsafe_module_enumerator {
|
||||
using value_type = const detail::win::LDR_DATA_TABLE_ENTRY_T*;
|
||||
value_type value;
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE unsafe_module_enumerator() noexcept
|
||||
: value(ldr_data_entry())
|
||||
{}
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE void reset() noexcept { value = ldr_data_entry(); }
|
||||
|
||||
LAZY_IMPORTER_FORCEINLINE bool next() noexcept
|
||||
{
|
||||
value = value->load_order_next();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// provides the cached functions which use Derive classes methods
|
||||
template<class Derived, class DefaultType = void*>
|
||||
class lazy_base {
|
||||
protected:
|
||||
// This function is needed because every templated function
|
||||
// with different args has its own static buffer
|
||||
LAZY_IMPORTER_FORCEINLINE static void*& _cache() noexcept
|
||||
{
|
||||
static void* value = nullptr;
|
||||
return value;
|
||||
}
|
||||
|
||||
public:
|
||||
template<class T = DefaultType>
|
||||
LAZY_IMPORTER_FORCEINLINE static T safe() noexcept
|
||||
{
|
||||
return Derived::template get<T, safe_module_enumerator>();
|
||||
}
|
||||
|
||||
template<class T = DefaultType, class Enum = unsafe_module_enumerator>
|
||||
LAZY_IMPORTER_FORCEINLINE static T cached() noexcept
|
||||
{
|
||||
auto& cached = _cache();
|
||||
if (!cached)
|
||||
cached = Derived::template get<void*, Enum>();
|
||||
|
||||
return (T)(cached);
|
||||
}
|
||||
|
||||
template<class T = DefaultType>
|
||||
LAZY_IMPORTER_FORCEINLINE static T safe_cached() noexcept
|
||||
{
|
||||
return cached<T, safe_module_enumerator>();
|
||||
}
|
||||
};
|
||||
|
||||
template<hash_t::value_type Hash>
|
||||
struct lazy_module : lazy_base<lazy_module<Hash>> {
|
||||
template<class T = void*, class Enum = unsafe_module_enumerator>
|
||||
LAZY_IMPORTER_FORCEINLINE static T get() noexcept
|
||||
{
|
||||
Enum e;
|
||||
do {
|
||||
if (hash(e.value->BaseDllName) == Hash)
|
||||
return (T)(e.value->DllBase);
|
||||
} while (e.next());
|
||||
return {};
|
||||
}
|
||||
|
||||
template<class T = void*, class Ldr>
|
||||
LAZY_IMPORTER_FORCEINLINE static T in(Ldr ldr) noexcept
|
||||
{
|
||||
safe_module_enumerator e((const detail::win::LDR_DATA_TABLE_ENTRY_T*)(ldr));
|
||||
do {
|
||||
if (hash(e.value->BaseDllName) == Hash)
|
||||
return (T)(e.value->DllBase);
|
||||
} while (e.next());
|
||||
return {};
|
||||
}
|
||||
|
||||
template<class T = void*, class Ldr>
|
||||
LAZY_IMPORTER_FORCEINLINE static T in_cached(Ldr ldr) noexcept
|
||||
{
|
||||
auto& cached = lazy_base<lazy_module<Hash>>::_cache();
|
||||
if (!cached)
|
||||
cached = in(ldr);
|
||||
|
||||
return (T)(cached);
|
||||
}
|
||||
};
|
||||
|
||||
template<hash_t::value_type Hash, class T>
|
||||
struct lazy_function : lazy_base<lazy_function<Hash, T>, T> {
|
||||
using base_type = lazy_base<lazy_function<Hash, T>, T>;
|
||||
|
||||
template<class... Args>
|
||||
LAZY_IMPORTER_FORCEINLINE decltype(auto) operator()(Args&&... args) const
|
||||
{
|
||||
#ifndef LAZY_IMPORTER_CACHE_OPERATOR_PARENS
|
||||
return get()(std::forward<Args>(args)...);
|
||||
#else
|
||||
return this->cached()(std::forward<Args>(args)...);
|
||||
#endif
|
||||
}
|
||||
|
||||
template<class F = T, class Enum = unsafe_module_enumerator>
|
||||
LAZY_IMPORTER_FORCEINLINE static F get() noexcept
|
||||
{
|
||||
// for backwards compatability.
|
||||
// Before 2.0 it was only possible to resolve forwarded exports when
|
||||
// this macro was enabled
|
||||
#ifdef LAZY_IMPORTER_RESOLVE_FORWARDED_EXPORTS
|
||||
return forwarded<F, Enum>();
|
||||
#else
|
||||
|
||||
Enum e;
|
||||
|
||||
do {
|
||||
#ifdef LAZY_IMPORTER_HARDENED_MODULE_CHECKS
|
||||
if (!e.value->DllBase || !e.value->FullDllName.Length)
|
||||
continue;
|
||||
#endif
|
||||
|
||||
const exports_directory exports(e.value->DllBase);
|
||||
|
||||
if (exports) {
|
||||
auto export_index = exports.size();
|
||||
while (export_index--)
|
||||
if (hash(exports.name(export_index)) == Hash)
|
||||
return (F)(exports.address(export_index));
|
||||
}
|
||||
} while (e.next());
|
||||
return {};
|
||||
#endif
|
||||
}
|
||||
|
||||
template<class F = T, class Enum = unsafe_module_enumerator>
|
||||
LAZY_IMPORTER_FORCEINLINE static F forwarded() noexcept
|
||||
{
|
||||
detail::win::UNICODE_STRING_T name;
|
||||
hash_t::value_type module_hash = 0;
|
||||
auto function_hash = Hash;
|
||||
|
||||
Enum e;
|
||||
do {
|
||||
name = e.value->BaseDllName;
|
||||
name.Length -= 8; // get rid of .dll extension
|
||||
|
||||
if (!module_hash || hash(name) == module_hash) {
|
||||
const exports_directory exports(e.value->DllBase);
|
||||
|
||||
if (exports) {
|
||||
auto export_index = exports.size();
|
||||
while (export_index--)
|
||||
if (hash(exports.name(export_index)) == function_hash) {
|
||||
const auto addr = exports.address(export_index);
|
||||
|
||||
if (exports.is_forwarded(addr)) {
|
||||
auto hashes = hash_forwarded(
|
||||
reinterpret_cast<const char*>(addr));
|
||||
|
||||
function_hash = hashes.second;
|
||||
module_hash = hashes.first;
|
||||
|
||||
e.reset();
|
||||
break;
|
||||
}
|
||||
return (F)(addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (e.next());
|
||||
return {};
|
||||
}
|
||||
|
||||
template<class F = T>
|
||||
LAZY_IMPORTER_FORCEINLINE static F forwarded_safe() noexcept
|
||||
{
|
||||
return forwarded<F, safe_module_enumerator>();
|
||||
}
|
||||
|
||||
template<class F = T, class Enum = unsafe_module_enumerator>
|
||||
LAZY_IMPORTER_FORCEINLINE static F forwarded_cached() noexcept
|
||||
{
|
||||
auto& value = base_type::_cache();
|
||||
if (!value)
|
||||
value = forwarded<void*, Enum>();
|
||||
return (F)(value);
|
||||
}
|
||||
|
||||
template<class F = T>
|
||||
LAZY_IMPORTER_FORCEINLINE static F forwarded_safe_cached() noexcept
|
||||
{
|
||||
return forwarded_cached<F, safe_module_enumerator>();
|
||||
}
|
||||
|
||||
template<class F = T, bool IsSafe = false, class Module>
|
||||
LAZY_IMPORTER_FORCEINLINE static F in(Module m) noexcept
|
||||
{
|
||||
if (IsSafe && !m)
|
||||
return {};
|
||||
|
||||
const exports_directory exports((const char*)(m));
|
||||
if (IsSafe && !exports)
|
||||
return {};
|
||||
|
||||
for (unsigned long i{};; ++i) {
|
||||
if (IsSafe && i == exports.size())
|
||||
break;
|
||||
|
||||
if (hash(exports.name(i)) == Hash)
|
||||
return (F)(exports.address(i));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
template<class F = T, class Module>
|
||||
LAZY_IMPORTER_FORCEINLINE static F in_safe(Module m) noexcept
|
||||
{
|
||||
return in<F, true>(m);
|
||||
}
|
||||
|
||||
template<class F = T, bool IsSafe = false, class Module>
|
||||
LAZY_IMPORTER_FORCEINLINE static F in_cached(Module m) noexcept
|
||||
{
|
||||
auto& value = base_type::_cache();
|
||||
if (!value)
|
||||
value = in<void*, IsSafe>(m);
|
||||
return (F)(value);
|
||||
}
|
||||
|
||||
template<class F = T, class Module>
|
||||
LAZY_IMPORTER_FORCEINLINE static F in_safe_cached(Module m) noexcept
|
||||
{
|
||||
return in_cached<F, true>(m);
|
||||
}
|
||||
|
||||
template<class F = T>
|
||||
LAZY_IMPORTER_FORCEINLINE static F nt() noexcept
|
||||
{
|
||||
return in<F>(ldr_data_entry()->load_order_next()->DllBase);
|
||||
}
|
||||
|
||||
template<class F = T>
|
||||
LAZY_IMPORTER_FORCEINLINE static F nt_safe() noexcept
|
||||
{
|
||||
return in_safe<F>(ldr_data_entry()->load_order_next()->DllBase);
|
||||
}
|
||||
|
||||
template<class F = T>
|
||||
LAZY_IMPORTER_FORCEINLINE static F nt_cached() noexcept
|
||||
{
|
||||
return in_cached<F>(ldr_data_entry()->load_order_next()->DllBase);
|
||||
}
|
||||
|
||||
template<class F = T>
|
||||
LAZY_IMPORTER_FORCEINLINE static F nt_safe_cached() noexcept
|
||||
{
|
||||
return in_safe_cached<F>(ldr_data_entry()->load_order_next()->DllBase);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace li::detail
|
||||
|
||||
#endif // include guard
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
inline_block void log_internal(const char* fmt, ...)
|
||||
{
|
||||
#if !defined(HS_DISABLE_LOGS)
|
||||
|
||||
char cad[512];
|
||||
va_list list{ };
|
||||
|
||||
ZERO_MEM(cad, sizeof(cad));
|
||||
|
||||
va_start(list, fmt);
|
||||
LI_FN(wvsprintfA).cached()(cad, fmt, list);
|
||||
va_end(list);
|
||||
|
||||
win_api::output_debug_string(cad);
|
||||
#else
|
||||
__noop();
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
extern void __log_monitor_call(void* ret_addr, const char* funcname, const char* format, T&&... arguments);
|
||||
|
||||
template <typename... T>
|
||||
extern void __log_monitor_call_nofilter(void* ret_addr, const char* funcname, const char* format, T&&... arguments);
|
||||
|
||||
#define log_mon_call(X, ...) __log_monitor_call(_ReturnAddress(), _XS(__FUNCTION__), _XS(X), __VA_ARGS__)
|
||||
#define log_mon_call_nf(X, ...) __log_monitor_call_nofilter(_ReturnAddress(), _XS(__FUNCTION__), _XS(X), __VA_ARGS__)
|
||||
|
||||
#define log_legacy(...) log_internal(__VA_ARGS__)
|
||||
|
||||
|
||||
#define log_info_(X, ...) log_legacy(_XS(X))
|
||||
#define log_info(...) log_legacy(__VA_ARGS__)
|
||||
|
||||
|
||||
#define log_call() log_info("LOG_CALL '%s'", _XS(__FUNCTION__))
|
||||
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
template <typename... T>
|
||||
void __log_monitor_call(void* ret_addr, const char* funcname, const char* format, T&&... arguments)
|
||||
{
|
||||
//
|
||||
// ignore calls from within our own image.
|
||||
//
|
||||
if (r5::is_within_self(ret_addr))
|
||||
return;
|
||||
|
||||
//
|
||||
// only log API calls outside of valid images.
|
||||
//
|
||||
if (util::file_header(ret_addr) != nullptr)
|
||||
return;
|
||||
|
||||
size_t length = 0;
|
||||
char cad[512];
|
||||
|
||||
length += LI_FN(wsprintfA).cached()(cad + length, _XS("MON_CALL @ #%p '%s' "), ret_addr, funcname);
|
||||
length += LI_FN(wsprintfA).cached()(cad + length, format, arguments...);
|
||||
length += LI_FN(wsprintfA).cached()(cad + length, _XS("\n"));
|
||||
|
||||
win_api::output_debug_string(cad);
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
void __log_monitor_call_nofilter(void* ret_addr, const char* funcname, const char* format, T&&... arguments)
|
||||
{
|
||||
//
|
||||
// ignore calls from within our own image.
|
||||
//
|
||||
if (r5::is_within_self(ret_addr))
|
||||
return;
|
||||
|
||||
//
|
||||
// only log API calls outside of valid images.
|
||||
//
|
||||
if (util::file_header(ret_addr) != nullptr)
|
||||
return;
|
||||
|
||||
size_t length = 0;
|
||||
char cad[512];
|
||||
|
||||
length += LI_FN(wsprintfA).cached()(cad + length, _XS("MON_CALL @ #%p '%s' "), ret_addr, funcname);
|
||||
length += LI_FN(wsprintfA).cached()(cad + length, format, arguments...);
|
||||
length += LI_FN(wsprintfA).cached()(cad + length, _XS("\n"));
|
||||
|
||||
win_api::output_debug_string(cad);
|
||||
}
|
||||
+812
@@ -0,0 +1,812 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(push, 0)
|
||||
|
||||
#define __HI(x) *(1+(int*)&x)
|
||||
#define __LO(x) *(int*)&x
|
||||
#define __HIp(x) *(1+(int*)x)
|
||||
#define __LOp(x) *(int*)x
|
||||
|
||||
#define POLYNOM1(x, a) ((a)[1]*(x)+(a)[0])
|
||||
#define POLYNOM2(x, a) (POLYNOM1((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM3(x, a) (POLYNOM2((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM4(x, a) (POLYNOM3((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM5(x, a) (POLYNOM4((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM6(x, a) (POLYNOM5((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM7(x, a) (POLYNOM6((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM8(x, a) (POLYNOM7((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM9(x, a) (POLYNOM8((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM10(x, a) (POLYNOM9((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM11(x, a) (POLYNOM10((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM12(x, a) (POLYNOM11((x),(a)+1)*(x)+(a)[0])
|
||||
#define POLYNOM13(x, a) (POLYNOM12((x),(a)+1)*(x)+(a)[0])
|
||||
|
||||
#define M_LN_MAX_D (M_LN2 * DBL_MAX_EXP)
|
||||
#define M_LN_MIN_D (M_LN2 * (DBL_MIN_EXP - 1))
|
||||
|
||||
struct f64
|
||||
{
|
||||
uint32_t low_word;
|
||||
uint32_t high_word;
|
||||
};
|
||||
|
||||
#define F64_SIGN_SHIFT 31
|
||||
#define F64_SIGN_MASK 1
|
||||
|
||||
#define F64_EXP_SHIFT 20
|
||||
#define F64_EXP_MASK 0x7ff
|
||||
#define F64_EXP_BIAS 1023
|
||||
#define F64_EXP_MAX 2047
|
||||
|
||||
#define F64_MANT_SHIFT 0
|
||||
#define F64_MANT_MASK 0xfffff
|
||||
|
||||
#define F64_GET_SIGN(fp) (((fp)->high_word >> F64_SIGN_SHIFT) & \
|
||||
F64_SIGN_MASK)
|
||||
#define F64_GET_EXP(fp) (((fp)->high_word >> F64_EXP_SHIFT) & \
|
||||
F64_EXP_MASK)
|
||||
#define F64_SET_EXP(fp, val) ((fp)->high_word= ((fp)->high_word & \
|
||||
~(F64_EXP_MASK << F64_EXP_SHIFT)) | \
|
||||
(((val) & F64_EXP_MASK) << F64_EXP_SHIFT))
|
||||
|
||||
#define F64_GET_MANT_LOW(fp) ((fp)->low_word)
|
||||
#define F64_SET_MANT_LOW(fp, val) ((fp)->low_word= (val))
|
||||
#define F64_GET_MANT_HIGH(fp) (((fp)->high_word >> F64_MANT_SHIFT) & \
|
||||
F64_MANT_MASK)
|
||||
#define F64_SET_MANT_HIGH(fp, val) ((fp)->high_word= ((fp)->high_word & \
|
||||
~(F64_MANT_MASK << F64_MANT_SHIFT)) | \
|
||||
(((val) & F64_MANT_MASK) << F64_MANT_SHIFT))
|
||||
|
||||
namespace math
|
||||
{
|
||||
static const double huge = 1.0e300;
|
||||
|
||||
template <typename _t>
|
||||
struct position_t
|
||||
{
|
||||
_t x;
|
||||
_t y;
|
||||
|
||||
_t length()
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
_t distance_to(const position_t<_t>& other)
|
||||
{
|
||||
return length() - other.length();
|
||||
}
|
||||
|
||||
template <typename _n>
|
||||
_n static_type_convert()
|
||||
{
|
||||
return position_t<_n>{static_cast<_n>(x), static_cast<_n>(y)};
|
||||
}
|
||||
};
|
||||
|
||||
template <typename _t>
|
||||
struct dimension_t
|
||||
{
|
||||
_t w;
|
||||
_t h;
|
||||
};
|
||||
|
||||
constexpr double calculate_percentage(const double current, const double max)
|
||||
{
|
||||
if (max == 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if (current > max) {
|
||||
return 100.0;
|
||||
}
|
||||
|
||||
// G = max
|
||||
// W = value
|
||||
return ((current * 100.0) / max);
|
||||
}
|
||||
|
||||
template < typename _t >
|
||||
struct range_t {
|
||||
_t min;
|
||||
_t max;
|
||||
};
|
||||
|
||||
template < typename _t >
|
||||
struct floating_point_holder2_t {
|
||||
_t x;
|
||||
_t y;
|
||||
};
|
||||
|
||||
template < typename _t >
|
||||
struct floating_point_holder3_t {
|
||||
_t x;
|
||||
_t y;
|
||||
_t z;
|
||||
};
|
||||
|
||||
template < typename _t >
|
||||
struct floating_point_holder4_t {
|
||||
_t x;
|
||||
_t y;
|
||||
_t z;
|
||||
_t h;
|
||||
};
|
||||
|
||||
inline float sqrtf(float _X) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(_X))); }
|
||||
inline float expf(float _X) { return _mm_cvtss_f32(_mm_exp_ps(_mm_set_ss(_X))); }
|
||||
|
||||
inline float asinf(float _X) { return _mm_cvtss_f32(_mm_asin_ps(_mm_set_ss(_X))); }
|
||||
|
||||
inline float fabsf(float x)
|
||||
{
|
||||
union
|
||||
{
|
||||
float f;
|
||||
uint32_t i;
|
||||
} u = { x };
|
||||
u.i &= 0x7fffffff;
|
||||
return u.f;
|
||||
}
|
||||
|
||||
inline int isinf(double x)
|
||||
{
|
||||
union { uint64_t u; double f; } ieee754;
|
||||
ieee754.f = x;
|
||||
return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) == 0x7ff00000 &&
|
||||
((unsigned)ieee754.u == 0);
|
||||
}
|
||||
|
||||
inline int isnan(double x)
|
||||
{
|
||||
union { uint64_t u; double f; } ieee754;
|
||||
ieee754.f = x;
|
||||
return ((unsigned)(ieee754.u >> 32) & 0x7fffffff) +
|
||||
((unsigned)ieee754.u != 0) > 0x7ff00000;
|
||||
}
|
||||
|
||||
inline int signbit(double x)
|
||||
{
|
||||
union {
|
||||
double d;
|
||||
uint64_t i;
|
||||
} y = { x };
|
||||
return y.i >> 63;
|
||||
}
|
||||
|
||||
inline float fminf(float x, float y)
|
||||
{
|
||||
if (isnan(x))
|
||||
return y;
|
||||
|
||||
if (isnan(y))
|
||||
return x;
|
||||
|
||||
if (signbit(x) != signbit(y))
|
||||
return signbit(x) ? x : y;
|
||||
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
inline float fmaxf(float x, float y)
|
||||
{
|
||||
if (isnan(x))
|
||||
return y;
|
||||
|
||||
if (isnan(y))
|
||||
return x;
|
||||
|
||||
if (signbit(x) != signbit(y))
|
||||
return signbit(x) ? y : x;
|
||||
|
||||
return x < y ? y : x;
|
||||
}
|
||||
|
||||
inline float atan2f(float y, float x)
|
||||
{
|
||||
float a, r, s, t, c, q, ax, ay, mx, mn;
|
||||
ax = fabsf(x);
|
||||
ay = fabsf(y);
|
||||
mx = fmaxf(ay, ax);
|
||||
mn = fminf(ay, ax);
|
||||
a = mn / mx;
|
||||
|
||||
s = a * a;
|
||||
c = s * a;
|
||||
q = s * s;
|
||||
r = 0.024840285f * q + 0.18681418f;
|
||||
t = -0.094097948f * q - 0.33213072f;
|
||||
r = r * s + t;
|
||||
r = r * c + a;
|
||||
|
||||
if (ay > ax) r = 1.57079637f - r;
|
||||
if (x < 0) r = 3.14159274f - r;
|
||||
if (y < 0) r = -r;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
inline static float FOPI = (float)1.27323954473516;
|
||||
|
||||
inline static float DP1 = (float)0.78515625;
|
||||
inline static float DP2 = (float)2.4187564849853515625e-4;
|
||||
inline static float DP3 = (float)3.77489497744594108e-8;
|
||||
inline static float lossth = (float)8192.;
|
||||
inline static float T24M1 = (float)16777215.;
|
||||
|
||||
inline static float sincof[] = {
|
||||
(float)-1.9515295891E-4,
|
||||
(float)8.3321608736E-3,
|
||||
(float)-1.6666654611E-1
|
||||
};
|
||||
inline static float coscof[] = {
|
||||
(float)2.443315711809948E-005,
|
||||
(float)-1.388731625493765E-003,
|
||||
(float)4.166664568298827E-002
|
||||
};
|
||||
|
||||
inline float acos(float x) {
|
||||
float negate = float(x < 0);
|
||||
x = abs(x);
|
||||
float ret = -0.0187293;
|
||||
ret = ret * x;
|
||||
ret = ret + 0.0742610;
|
||||
ret = ret * x;
|
||||
ret = ret - 0.2121144;
|
||||
ret = ret * x;
|
||||
ret = ret + 1.5707288;
|
||||
ret = ret * sqrtf(1.0 - x);
|
||||
ret = ret - 2 * negate * ret;
|
||||
return negate * 3.14159265358979 + ret;
|
||||
}
|
||||
|
||||
inline float sinf(float xx)
|
||||
{
|
||||
float* p;
|
||||
float x, y, z;
|
||||
unsigned long j;
|
||||
int sign;
|
||||
|
||||
sign = 1;
|
||||
x = xx;
|
||||
if (xx < 0)
|
||||
{
|
||||
sign = -1;
|
||||
x = -xx;
|
||||
}
|
||||
if (x > T24M1)
|
||||
{
|
||||
return(0.0);
|
||||
}
|
||||
j = FOPI * x;
|
||||
y = j;
|
||||
if (j & 1)
|
||||
{
|
||||
j += 1;
|
||||
y += 1.0;
|
||||
}
|
||||
j &= 7;
|
||||
if (j > 3)
|
||||
{
|
||||
sign = -sign;
|
||||
j -= 4;
|
||||
}
|
||||
|
||||
if (x > lossth)
|
||||
{
|
||||
x = x - y * M_PI_4;
|
||||
}
|
||||
else
|
||||
{
|
||||
x = ((x - y * DP1) - y * DP2) - y * DP3;
|
||||
}
|
||||
z = x * x;
|
||||
if ((j == 1) || (j == 2))
|
||||
{
|
||||
p = coscof;
|
||||
y = *p++;
|
||||
y = y * z + *p++;
|
||||
y = y * z + *p++;
|
||||
y *= z * z;
|
||||
y -= 0.5 * z;
|
||||
y += 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
p = sincof;
|
||||
y = *p++;
|
||||
y = y * z + *p++;
|
||||
y = y * z + *p++;
|
||||
y *= z * x;
|
||||
y += x;
|
||||
}
|
||||
|
||||
if (sign < 0)
|
||||
y = -y;
|
||||
return(y);
|
||||
}
|
||||
|
||||
inline float cosf(float xx)
|
||||
{
|
||||
float x, y, z;
|
||||
int j, sign;
|
||||
|
||||
sign = 1;
|
||||
x = xx;
|
||||
if (x < 0)
|
||||
x = -x;
|
||||
|
||||
if (x > T24M1)
|
||||
{
|
||||
return(0.0);
|
||||
}
|
||||
|
||||
j = FOPI * x;
|
||||
y = j;
|
||||
|
||||
if (j & 1)
|
||||
{
|
||||
j += 1;
|
||||
y += 1.0;
|
||||
}
|
||||
j &= 7;
|
||||
if (j > 3)
|
||||
{
|
||||
j -= 4;
|
||||
sign = -sign;
|
||||
}
|
||||
|
||||
if (j > 1)
|
||||
sign = -sign;
|
||||
|
||||
if (x > lossth)
|
||||
{
|
||||
x = x - y * M_PI_4;
|
||||
}
|
||||
else
|
||||
x = ((x - y * DP1) - y * DP2) - y * DP3;
|
||||
|
||||
z = x * x;
|
||||
|
||||
if ((j == 1) || (j == 2))
|
||||
{
|
||||
y = (((-1.9515295891E-4 * z
|
||||
+ 8.3321608736E-3) * z
|
||||
- 1.6666654611E-1) * z * x)
|
||||
+ x;
|
||||
}
|
||||
else
|
||||
{
|
||||
y = ((2.443315711809948E-005 * z
|
||||
- 1.388731625493765E-003) * z
|
||||
+ 4.166664568298827E-002) * z * z;
|
||||
y -= 0.5 * z;
|
||||
y += 1.0;
|
||||
}
|
||||
if (sign < 0)
|
||||
y = -y;
|
||||
return(y);
|
||||
}
|
||||
|
||||
inline double floor(double x)
|
||||
{
|
||||
int i0, i1, j0;
|
||||
unsigned i, j;
|
||||
i0 = __HI(x);
|
||||
i1 = __LO(x);
|
||||
j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
|
||||
if (j0 < 20) {
|
||||
if (j0 < 0) { /* raise inexact if x != 0 */
|
||||
if (huge + x > 0.0) {/* return 0*sign(x) if |x|<1 */
|
||||
if (i0 >= 0) { i0 = i1 = 0; }
|
||||
else if (((i0 & 0x7fffffff) | i1) != 0)
|
||||
{
|
||||
i0 = 0xbff00000; i1 = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
i = (0x000fffff) >> j0;
|
||||
if (((i0 & i) | i1) == 0) return x; /* x is integral */
|
||||
if (huge + x > 0.0) { /* raise inexact flag */
|
||||
if (i0 < 0) i0 += (0x00100000) >> j0;
|
||||
i0 &= (~i); i1 = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (j0 > 51) {
|
||||
if (j0 == 0x400) return x + x; /* inf or NaN */
|
||||
else return x; /* x is integral */
|
||||
}
|
||||
else {
|
||||
i = ((unsigned)(0xffffffff)) >> (j0 - 20);
|
||||
if ((i1 & i) == 0) return x; /* x is integral */
|
||||
if (huge + x > 0.0) { /* raise inexact flag */
|
||||
if (i0 < 0) {
|
||||
if (j0 == 20) i0 += 1;
|
||||
else {
|
||||
j = i1 + (1 << (52 - j0));
|
||||
if (j < i1) i0 += 1; /* got a carry */
|
||||
i1 = j;
|
||||
}
|
||||
}
|
||||
i1 &= (~i);
|
||||
}
|
||||
}
|
||||
__HI(x) = i0;
|
||||
__LO(x) = i1;
|
||||
return x;
|
||||
}
|
||||
|
||||
inline float floorf(float f)
|
||||
{
|
||||
return (float)floor((double)f);
|
||||
}
|
||||
|
||||
inline float roundf(float x)
|
||||
{
|
||||
float t;
|
||||
|
||||
if (x >= 0.0) {
|
||||
t = floorf(x);
|
||||
if (t - x <= -0.5)
|
||||
t += 1.0;
|
||||
return t;
|
||||
}
|
||||
else {
|
||||
t = floorf(-x);
|
||||
if (t + x <= -0.5)
|
||||
t += 1.0;
|
||||
return -t;
|
||||
}
|
||||
}
|
||||
|
||||
inline double modf(double value, double* iptr)
|
||||
{
|
||||
struct f64* f64p;
|
||||
double tmp;
|
||||
int exp;
|
||||
int mask_bits;
|
||||
uint32_t mant;
|
||||
|
||||
f64p = (struct f64*)&value;
|
||||
|
||||
exp = F64_GET_EXP(f64p);
|
||||
exp -= F64_EXP_BIAS;
|
||||
if (exp < 0)
|
||||
{
|
||||
*iptr = 0;
|
||||
return value;
|
||||
}
|
||||
mask_bits = 52 - exp;
|
||||
if (mask_bits <= 0)
|
||||
{
|
||||
*iptr = value;
|
||||
return 0;
|
||||
}
|
||||
tmp = value;
|
||||
if (mask_bits >= 32)
|
||||
{
|
||||
F64_SET_MANT_LOW(f64p, 0);
|
||||
mask_bits -= 32;
|
||||
mant = F64_GET_MANT_HIGH(f64p);
|
||||
mant &= ~((1 << mask_bits) - 1);
|
||||
F64_SET_MANT_HIGH(f64p, mant);
|
||||
}
|
||||
else
|
||||
{
|
||||
mant = F64_GET_MANT_LOW(f64p);
|
||||
mant &= ~((1 << mask_bits) - 1);
|
||||
F64_SET_MANT_LOW(f64p, mant);
|
||||
}
|
||||
*iptr = value;
|
||||
return tmp - value;
|
||||
}
|
||||
|
||||
inline double
|
||||
tan(double x)
|
||||
{
|
||||
/* Algorithm and coefficients from:
|
||||
"Software manual for the elementary functions"
|
||||
by W.J. Cody and W. Waite, Prentice-Hall, 1980
|
||||
*/
|
||||
|
||||
int negative = x < 0;
|
||||
int invert = 0;
|
||||
double y;
|
||||
static double p[] = {
|
||||
1.0,
|
||||
-0.13338350006421960681e+0,
|
||||
0.34248878235890589960e-2,
|
||||
-0.17861707342254426711e-4
|
||||
};
|
||||
static double q[] = {
|
||||
1.0,
|
||||
-0.46671683339755294240e+0,
|
||||
0.25663832289440112864e-1,
|
||||
-0.31181531907010027307e-3,
|
||||
0.49819433993786512270e-6
|
||||
};
|
||||
|
||||
if (math::isnan(x)) {
|
||||
return x;
|
||||
}
|
||||
|
||||
if (negative) x = -x;
|
||||
|
||||
/* ??? avoid loss of significance, error if x is too large ??? */
|
||||
|
||||
y = x * M_2_PI + 0.5;
|
||||
|
||||
if (y >= DBL_MAX / M_PI_2) return 0.0;
|
||||
|
||||
/* Use extended precision to calculate reduced argument.
|
||||
Here we used 12 bits of the mantissa for a1.
|
||||
Also split x in integer part x1 and fraction part x2.
|
||||
*/
|
||||
#define A1 1.57080078125
|
||||
#define A2 -4.454455103380768678308e-6
|
||||
{
|
||||
double x1, x2;
|
||||
|
||||
modf(y, &y);
|
||||
if (modf(0.5 * y, &x1)) invert = 1;
|
||||
x2 = modf(x, &x1);
|
||||
x = x1 - y * A1;
|
||||
x += x2;
|
||||
x -= y * A2;
|
||||
#undef A1
|
||||
#undef A2
|
||||
}
|
||||
|
||||
/* ??? avoid underflow ??? */
|
||||
y = x * x;
|
||||
x += x * y * POLYNOM2(y, p + 1);
|
||||
y = POLYNOM4(y, q);
|
||||
if (negative) x = -x;
|
||||
return invert ? -y / x : x / y;
|
||||
}
|
||||
|
||||
inline float tanf(float x)
|
||||
{
|
||||
return tan(x);
|
||||
}
|
||||
|
||||
static int maxExponent = 511; /* Largest possible base 10 exponent. Any
|
||||
* exponent larger than this will already
|
||||
* produce underflow or overflow, so there's
|
||||
* no need to worry about additional digits.
|
||||
*/
|
||||
static double powersOf10[] = { /* Table giving binary powers of 10. Entry */
|
||||
10., /* is 10^2^i. Used to convert decimal */
|
||||
100., /* exponents into floating-point numbers. */
|
||||
1.0e4,
|
||||
1.0e8,
|
||||
1.0e16,
|
||||
1.0e32,
|
||||
1.0e64,
|
||||
1.0e128,
|
||||
1.0e256
|
||||
};
|
||||
|
||||
inline int isspace(int c)
|
||||
{
|
||||
return (c == '\t' || c == '\n' ||
|
||||
c == '\v' || c == '\f' || c == '\r' || c == ' ' ? 1 : 0);
|
||||
}
|
||||
|
||||
inline char isdigit(unsigned char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline int isalpha(int c)
|
||||
{
|
||||
/*
|
||||
* Depends on ASCII-like character ordering.
|
||||
*/
|
||||
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? 1 : 0);
|
||||
}
|
||||
|
||||
inline char isupper(unsigned char c)
|
||||
{
|
||||
if (c >= UC('A') && c <= UC('Z'))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline double atof(char* string) /* A decimal ASCII floating-point number,
|
||||
* optionally preceded by white space.
|
||||
* Must have form "-I.FE-X", where I is the
|
||||
* integer part of the mantissa, F is the
|
||||
* fractional part of the mantissa, and X
|
||||
* is the exponent. Either of the signs
|
||||
* may be "+", "-", or omitted. Either I
|
||||
* or F may be omitted, or both. The decimal
|
||||
* point isn't necessary unless F is present.
|
||||
* The "E" may actually be an "e". E and X
|
||||
* may both be omitted (but not just one).
|
||||
*/
|
||||
{
|
||||
int sign, expSign = FALSE;
|
||||
double fraction, dblExp, * d;
|
||||
register char* p, c;
|
||||
int exp = 0; /* Exponent read from "EX" field. */
|
||||
int fracExp = 0; /* Exponent that derives from the fractional
|
||||
* part. Under normal circumstatnces, it is
|
||||
* the negative of the number of digits in F.
|
||||
* However, if I is very long, the last digits
|
||||
* of I get dropped (otherwise a long I with a
|
||||
* large negative exponent could cause an
|
||||
* unnecessary overflow on I alone). In this
|
||||
* case, fracExp is incremented one for each
|
||||
* dropped digit.
|
||||
*/
|
||||
int mantSize; /* Number of digits in mantissa. */
|
||||
int decPt; /* Number of mantissa digits BEFORE decimal
|
||||
* point.
|
||||
*/
|
||||
char* pExp; /* Temporarily holds location of exponent
|
||||
* in string.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Strip off leading blanks and check for a sign.
|
||||
*/
|
||||
|
||||
p = string;
|
||||
while (isspace(*p)) {
|
||||
p += 1;
|
||||
}
|
||||
if (*p == '-') {
|
||||
sign = TRUE;
|
||||
p += 1;
|
||||
}
|
||||
else {
|
||||
if (*p == '+') {
|
||||
p += 1;
|
||||
}
|
||||
sign = FALSE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Count the number of digits in the mantissa (including the decimal
|
||||
* point), and also locate the decimal point.
|
||||
*/
|
||||
|
||||
decPt = -1;
|
||||
for (mantSize = 0; ; mantSize += 1)
|
||||
{
|
||||
c = *p;
|
||||
if (!isdigit(c)) {
|
||||
if ((c != '.') || (decPt >= 0)) {
|
||||
break;
|
||||
}
|
||||
decPt = mantSize;
|
||||
}
|
||||
p += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now suck up the digits in the mantissa. Use two integers to
|
||||
* collect 9 digits each (this is faster than using floating-point).
|
||||
* If the mantissa has more than 18 digits, ignore the extras, since
|
||||
* they can't affect the value anyway.
|
||||
*/
|
||||
|
||||
pExp = p;
|
||||
p -= mantSize;
|
||||
if (decPt < 0) {
|
||||
decPt = mantSize;
|
||||
}
|
||||
else {
|
||||
mantSize -= 1; /* One of the digits was the point. */
|
||||
}
|
||||
if (mantSize > 18) {
|
||||
fracExp = decPt - 18;
|
||||
mantSize = 18;
|
||||
}
|
||||
else {
|
||||
fracExp = decPt - mantSize;
|
||||
}
|
||||
if (mantSize == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
else {
|
||||
int frac1, frac2;
|
||||
frac1 = 0;
|
||||
for (; mantSize > 9; mantSize -= 1)
|
||||
{
|
||||
c = *p;
|
||||
p += 1;
|
||||
if (c == '.') {
|
||||
c = *p;
|
||||
p += 1;
|
||||
}
|
||||
frac1 = 10 * frac1 + (c - '0');
|
||||
}
|
||||
frac2 = 0;
|
||||
for (; mantSize > 0; mantSize -= 1)
|
||||
{
|
||||
c = *p;
|
||||
p += 1;
|
||||
if (c == '.') {
|
||||
c = *p;
|
||||
p += 1;
|
||||
}
|
||||
frac2 = 10 * frac2 + (c - '0');
|
||||
}
|
||||
fraction = (1.0e9 * frac1) + frac2;
|
||||
}
|
||||
|
||||
/*
|
||||
* Skim off the exponent.
|
||||
*/
|
||||
|
||||
p = pExp;
|
||||
if ((*p == 'E') || (*p == 'e')) {
|
||||
p += 1;
|
||||
if (*p == '-') {
|
||||
expSign = TRUE;
|
||||
p += 1;
|
||||
}
|
||||
else {
|
||||
if (*p == '+') {
|
||||
p += 1;
|
||||
}
|
||||
expSign = FALSE;
|
||||
}
|
||||
while (isdigit(*p)) {
|
||||
exp = exp * 10 + (*p - '0');
|
||||
p += 1;
|
||||
}
|
||||
}
|
||||
if (expSign) {
|
||||
exp = fracExp - exp;
|
||||
}
|
||||
else {
|
||||
exp = fracExp + exp;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate a floating-point number that represents the exponent.
|
||||
* Do this by processing the exponent one bit at a time to combine
|
||||
* many powers of 2 of 10. Then combine the exponent with the
|
||||
* fraction.
|
||||
*/
|
||||
|
||||
if (exp < 0) {
|
||||
expSign = TRUE;
|
||||
exp = -exp;
|
||||
}
|
||||
else {
|
||||
expSign = FALSE;
|
||||
}
|
||||
if (exp > maxExponent) {
|
||||
exp = maxExponent;
|
||||
}
|
||||
dblExp = 1.0;
|
||||
for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
|
||||
if (exp & 01) {
|
||||
dblExp *= *d;
|
||||
}
|
||||
}
|
||||
if (expSign) {
|
||||
fraction /= dblExp;
|
||||
}
|
||||
else {
|
||||
fraction *= dblExp;
|
||||
}
|
||||
|
||||
if (sign) {
|
||||
return -fraction;
|
||||
}
|
||||
return fraction;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning(pop)
|
||||
@@ -0,0 +1,130 @@
|
||||
#pragma once
|
||||
|
||||
namespace mem
|
||||
{
|
||||
struct range {
|
||||
uintptr_t m_begin{};
|
||||
uintptr_t m_end{};
|
||||
uintptr_t m_size{};
|
||||
};
|
||||
|
||||
class addr
|
||||
{
|
||||
public:
|
||||
addr() = default;
|
||||
~addr() = default;
|
||||
|
||||
bool operator==(const DWORD_PTR& other) const
|
||||
{
|
||||
return (m_base == other);
|
||||
}
|
||||
|
||||
bool operator==(const DWORD_PTR& lhs)
|
||||
{
|
||||
return (lhs == m_base);
|
||||
}
|
||||
|
||||
addr(const DWORD_PTR base)
|
||||
{
|
||||
m_base = base;
|
||||
}
|
||||
|
||||
addr(void* const ptr)
|
||||
{
|
||||
m_base = reinterpret_cast<DWORD_PTR>(ptr);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Get() const
|
||||
{
|
||||
return addr(m_base);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Base() const
|
||||
{
|
||||
return m_base;
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Valid() const
|
||||
{
|
||||
return !(m_base == 0);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Add(const DWORD_PTR t) const
|
||||
{
|
||||
return addr(m_base + t);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Sub(const DWORD_PTR t) const
|
||||
{
|
||||
return addr(m_base - t);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Deref() const
|
||||
{
|
||||
return addr(*reinterpret_cast<DWORD_PTR*>(m_base));
|
||||
}
|
||||
|
||||
[[nodiscard]] auto Ptr() const
|
||||
{
|
||||
return reinterpret_cast<PVOID>(m_base);
|
||||
}
|
||||
|
||||
template < class T >
|
||||
[[nodiscard]] auto As()
|
||||
{
|
||||
return reinterpret_cast<T>(m_base);
|
||||
}
|
||||
|
||||
template < class T >
|
||||
[[nodiscard]] auto Retrieve_as()
|
||||
{
|
||||
return *reinterpret_cast<T*>(m_base);
|
||||
}
|
||||
|
||||
template < class T >
|
||||
void Set(const T& value)
|
||||
{
|
||||
*As<T*>() = value;
|
||||
}
|
||||
private:
|
||||
DWORD_PTR m_base;
|
||||
};
|
||||
|
||||
class bare_pointer
|
||||
{
|
||||
public:
|
||||
bare_pointer(void* const ptr)
|
||||
{
|
||||
m_instance = ptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] addr base() const
|
||||
{
|
||||
return addr(m_instance);
|
||||
}
|
||||
|
||||
[[nodiscard]] auto deref() const
|
||||
{
|
||||
return *static_cast<void**>(m_instance);
|
||||
}
|
||||
private:
|
||||
void* m_instance;
|
||||
};
|
||||
|
||||
INLINE DWORD_PTR ResolveAddressToRelative(DWORD_PTR dwInitial, DWORD_PTR dwInstructionOffset, DWORD_PTR dwPostOffset)
|
||||
{
|
||||
if (dwInitial == NULL)
|
||||
return NULL;
|
||||
|
||||
CONST AUTO dwRelativeAddyOffset = (dwInitial + dwInstructionOffset);
|
||||
CONST AUTO dwRelativeAddy = *reinterpret_cast<LONG*>(dwRelativeAddyOffset);
|
||||
CONST AUTO dwRelativeResult = dwInitial + dwRelativeAddy;
|
||||
|
||||
if (dwPostOffset != NULL)
|
||||
return dwRelativeResult + dwPostOffset;
|
||||
|
||||
return dwRelativeResult;
|
||||
}
|
||||
}
|
||||
|
||||
#define RESOLVE_ADDRESS_TO_RELATIVE(INITIAL, INSTR_OFFS, OPCODE_LENGTH) mem::ResolveAddressToRelative(INITIAL, INSTR_OFFS, OPCODE_LENGTH)
|
||||
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
namespace mh
|
||||
{
|
||||
__forceinline auto get_minhook_data_for_hook(void* target, void* handler, void** original, uint8_t* buffer)
|
||||
{
|
||||
MH_STATUS status;
|
||||
|
||||
if ((status = MH_CreateHook(target, handler, original, 0)) != MH_OK)
|
||||
{
|
||||
log_info("failed to create minhook request => %i", status);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((status = MH_EnableHook(target, buffer)) != MH_OK)
|
||||
{
|
||||
log_info("failed to enable minhook request => %i", status);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline auto stealth_patch_bytes(uintptr_t target, uint8_t* patch, size_t size)
|
||||
{
|
||||
if (!hvi::write_virt_memory(win_api::get_current_process_id(), target, patch, size, true))
|
||||
{
|
||||
log_info_("stealth_patch_bytes() -> failed to write memory. \n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// just a regular stealth patch.
|
||||
//
|
||||
template < typename T >
|
||||
__forceinline auto stealth_patch(uintptr_t target, void* handler, T* original)
|
||||
{
|
||||
uint8_t patch[64];
|
||||
|
||||
memset(patch, 0, sizeof(patch));
|
||||
|
||||
if (!target)
|
||||
return false;
|
||||
|
||||
if (!get_minhook_data_for_hook(mem::addr(target).Ptr(), handler, (void**)original, patch))
|
||||
{
|
||||
log_info_("stealth_patch_api() -> failed to get data from MH engine. \n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hvi::write_virt_memory(win_api::get_current_process_id(), target, patch, 5, true))
|
||||
{
|
||||
log_info_("stealth_patch_api() -> failed to write memory. \n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// this in addition triggers CoW (copy-on-write) so the page gets it's unique physical address.
|
||||
// that's necessary and very handy for shared DLLs which are mapped to the same physical memory,
|
||||
// like system DLLs kernel32.dll ntdll.dll etc. we don't want a system wide hook, that'd be fatal.
|
||||
// access violations, undefined behavior and insane performance loss as well so yeah no don't try that.
|
||||
//
|
||||
template < typename T >
|
||||
__forceinline auto stealth_patch_api(uintptr_t target, void* handler, T* original)
|
||||
{
|
||||
uint8_t patch[64];
|
||||
|
||||
memset(patch, 0, sizeof(patch));
|
||||
|
||||
if (!target)
|
||||
return false;
|
||||
|
||||
if (!hv::trigger_cow(target))
|
||||
{
|
||||
//return false;
|
||||
}
|
||||
|
||||
if (!get_minhook_data_for_hook(mem::addr(target).Ptr(), handler, (void**)original, patch))
|
||||
{
|
||||
log_info_("stealth_patch_api() -> failed to get data from MH engine. \n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!hvi::write_virt_memory(win_api::get_current_process_id(), target, patch, 5, true))
|
||||
{
|
||||
log_info_("stealth_patch_api() -> failed to write memory. \n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
namespace pe
|
||||
{
|
||||
template < typename _pt, typename _nth >
|
||||
__forceinline _pt* get_data_directory_ptr(uintptr_t base, _nth nt_hdr, int index)
|
||||
{
|
||||
const auto offset =
|
||||
nt_hdr->OptionalHeader.DataDirectory[index].VirtualAddress;
|
||||
|
||||
return reinterpret_cast<_pt*>((base + offset));
|
||||
}
|
||||
|
||||
template < typename _nth >
|
||||
__forceinline bool get_data_directory_info(uintptr_t base, _nth nt_hdr, int index, IMAGE_DATA_DIRECTORY& out)
|
||||
{
|
||||
out = nt_hdr->OptionalHeader.DataDirectory[index];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
extern bool get_headers(void* image, _pimage_dos_header* out_dh, _pimage_nt_header* out_nh);
|
||||
extern uintptr_t find_export_address(uintptr_t base, _pimage_dos_header dos_hdr, _pimage_nt_header nt_hdr, const char* exp_name);
|
||||
|
||||
template < typename T >
|
||||
__forceinline auto find_export(void* image, const char* name)
|
||||
{
|
||||
const auto base = mem::addr(image).Base();
|
||||
|
||||
_pimage_dos_header dh = nullptr;
|
||||
_pimage_nt_header nh = nullptr;
|
||||
|
||||
if (!get_headers(image, &dh, &nh))
|
||||
return T();
|
||||
|
||||
return reinterpret_cast<T>(find_export_address(base, dh, nh, name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
#include <type_traits>
|
||||
|
||||
namespace detail
|
||||
{
|
||||
extern "C" void* _spoofer_stub();
|
||||
|
||||
template <typename Ret, typename... Args>
|
||||
static inline auto shellcode_stub_helper(
|
||||
const void* shell,
|
||||
Args... args
|
||||
) -> Ret
|
||||
{
|
||||
auto fn = (Ret(*)(Args...))(shell);
|
||||
return fn(args...);
|
||||
}
|
||||
|
||||
template <std::size_t Argc, typename>
|
||||
struct argument_remapper
|
||||
{
|
||||
// At least 5 params
|
||||
template<
|
||||
typename Ret,
|
||||
typename First,
|
||||
typename Second,
|
||||
typename Third,
|
||||
typename Fourth,
|
||||
typename... Pack
|
||||
>
|
||||
static auto do_call(
|
||||
const void* shell,
|
||||
void* shell_param,
|
||||
First first,
|
||||
Second second,
|
||||
Third third,
|
||||
Fourth fourth,
|
||||
Pack... pack
|
||||
) -> Ret
|
||||
{
|
||||
return shellcode_stub_helper<
|
||||
Ret,
|
||||
First,
|
||||
Second,
|
||||
Third,
|
||||
Fourth,
|
||||
void*,
|
||||
void*,
|
||||
Pack...
|
||||
>(
|
||||
shell,
|
||||
first,
|
||||
second,
|
||||
third,
|
||||
fourth,
|
||||
shell_param,
|
||||
nullptr,
|
||||
pack...
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
template <std::size_t Argc>
|
||||
struct argument_remapper<Argc, std::enable_if_t<Argc <= 4>>
|
||||
{
|
||||
// 4 or less params
|
||||
template<
|
||||
typename Ret,
|
||||
typename First = void*,
|
||||
typename Second = void*,
|
||||
typename Third = void*,
|
||||
typename Fourth = void*
|
||||
>
|
||||
static auto do_call(
|
||||
const void* shell,
|
||||
void* shell_param,
|
||||
First first = First{},
|
||||
Second second = Second{},
|
||||
Third third = Third{},
|
||||
Fourth fourth = Fourth{}
|
||||
) -> Ret
|
||||
{
|
||||
return shellcode_stub_helper<
|
||||
Ret,
|
||||
First,
|
||||
Second,
|
||||
Third,
|
||||
Fourth,
|
||||
void*,
|
||||
void*
|
||||
>(
|
||||
shell,
|
||||
first,
|
||||
second,
|
||||
third,
|
||||
fourth,
|
||||
shell_param,
|
||||
nullptr
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
template <typename Ret, typename... Args>
|
||||
static inline auto spoof_call(
|
||||
Ret(*fn)(Args...),
|
||||
Args... args
|
||||
) -> Ret
|
||||
{
|
||||
struct shell_params
|
||||
{
|
||||
const void* trampoline;
|
||||
void* function;
|
||||
void* rbx;
|
||||
};
|
||||
|
||||
shell_params p{ (void*)g::return_address_gadget, reinterpret_cast<void*>(fn) };
|
||||
using mapper = detail::argument_remapper<sizeof...(Args), void>;
|
||||
return mapper::template do_call<Ret, Args...>((const void*)&detail::_spoofer_stub, &p, args...);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "defs.h"
|
||||
#include "ia32.h"
|
||||
#include "kernel.h"
|
||||
#include "lazy_importer.h"
|
||||
#include "math.h"
|
||||
#include "xor.h"
|
||||
#include "assembly.h"
|
||||
#include "memory.h"
|
||||
#include "pe.h"
|
||||
#include "crc.hpp"
|
||||
#include "winapi.h"
|
||||
#include "instr.h"
|
||||
#include "return_spoofer.h"
|
||||
#include "vfunc.h"
|
||||
#include "stl.h"
|
||||
#include "stl_uni.h"
|
||||
#include "util.h"
|
||||
#include "syscall.h"
|
||||
#include "hvi.h"
|
||||
#include "signature.h"
|
||||
#include "log.h"
|
||||
#include "vmt.h"
|
||||
#include "hv.h"
|
||||
#include "min_hook.h"
|
||||
#include "input.h"
|
||||
#include "jsmn.h"
|
||||
#include "json.h"
|
||||
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#define IN_RANGE(x,a,b) (x >= a && x <= b)
|
||||
#define GET_BITS( x ) (IN_RANGE(x,'0','9') ? (x - '0') : ((x&(~0x20)) - 'A' + 0xa))
|
||||
#define GET_BYTE( x ) (GET_BITS(x[0]) << 4 | GET_BITS(x[1]))
|
||||
|
||||
namespace shared::sig {
|
||||
inline BYTE GetBits(BYTE x) {
|
||||
if (IN_RANGE(x, '0', '9'))
|
||||
{
|
||||
return x - '0';
|
||||
}
|
||||
|
||||
return ((x & (~0x20)) - 'A' + 0xA);
|
||||
}
|
||||
|
||||
inline BYTE GetByte(PCWSTR x) {
|
||||
return (GetBits((BYTE)x[0]) << 4 | GetBits((BYTE)x[1]));
|
||||
}
|
||||
|
||||
extern DWORD_PTR FindPattern(const DWORD_PTR baseAddress, const DWORD_PTR length, const PCWSTR pattern);
|
||||
|
||||
inline DWORD_PTR FindInImage(const DWORD_PTR imageBaseAddress, const SIZE_T imageSize, const PCWSTR pattern, const DWORD offset = NULL)
|
||||
{
|
||||
auto dwResult = FindPattern(imageBaseAddress, imageSize, pattern);
|
||||
|
||||
if (offset)
|
||||
dwResult += offset;
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
inline DWORD_PTR FindInImageEx(const DWORD_PTR imageBaseAddress, const SIZE_T imageSize, const PCWSTR section, const PCWSTR pattern, const DWORD offset = NULL)
|
||||
{
|
||||
auto* const pDosHdr = (PIMAGE_DOS_HEADER)imageBaseAddress;
|
||||
auto* const pNtHdr = (PIMAGE_NT_HEADERS)(imageBaseAddress + pDosHdr->e_lfanew);
|
||||
|
||||
if (pDosHdr->e_magic != IMAGE_DOS_SIGNATURE || pNtHdr->Signature != IMAGE_NT_SIGNATURE)
|
||||
return 0;
|
||||
|
||||
auto* pSecHdr = IMAGE_FIRST_SECTION(pNtHdr);
|
||||
auto nSecCnt = pNtHdr->FileHeader.NumberOfSections;
|
||||
|
||||
for (size_t i = 0; i < nSecCnt; i++)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
auto dwResult = FindPattern(imageBaseAddress, imageSize, pattern);
|
||||
|
||||
if (offset)
|
||||
dwResult += offset;
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
inline DWORD_PTR FindInImagePtr(const DWORD_PTR imageBaseAddress, const SIZE_T imageSize, const PCWSTR pattern, const DWORD offset,
|
||||
const DWORD offsetToInstr, const DWORD instrLength)
|
||||
{
|
||||
const auto dwInitialAddr = FindInImage(imageBaseAddress, imageSize, pattern, offset);
|
||||
|
||||
if (!dwInitialAddr)
|
||||
return NULL;
|
||||
|
||||
return RESOLVE_ADDRESS_TO_RELATIVE(dwInitialAddr, offsetToInstr, instrLength);
|
||||
}
|
||||
|
||||
inline BOOLEAN FindSection(PIMAGE_DOS_HEADER pDosHdr, PIMAGE_NT_HEADERS pNtHdr, IMAGE_SECTION_HEADER* pBuffer, LPCSTR pszName)
|
||||
{
|
||||
auto* pSecHdr = IMAGE_FIRST_SECTION(pNtHdr);
|
||||
|
||||
const auto nSecCnt = pNtHdr->FileHeader.NumberOfSections;
|
||||
|
||||
for (size_t i = 0; i < nSecCnt; i++)
|
||||
{
|
||||
const auto& sEntry = pSecHdr[i];
|
||||
|
||||
if (!stl::strcmp((PCSTR)sEntry.Name, pszName))
|
||||
{
|
||||
memcpy(pBuffer, &sEntry, sizeof(sEntry));
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
inline DWORD_PTR FindInImageEx(const DWORD_PTR imageBaseAddress, const PCSTR section, const PCWSTR pattern, const DWORD offset = NULL)
|
||||
{
|
||||
auto* const pDosHdr = (PIMAGE_DOS_HEADER)imageBaseAddress;
|
||||
auto* const pNtHdr = (PIMAGE_NT_HEADERS)(imageBaseAddress + pDosHdr->e_lfanew);
|
||||
|
||||
if (pDosHdr->e_magic != IMAGE_DOS_SIGNATURE || pNtHdr->Signature != IMAGE_NT_SIGNATURE)
|
||||
return 0;
|
||||
|
||||
auto sTargetSection = IMAGE_SECTION_HEADER{};
|
||||
|
||||
if (!FindSection(pDosHdr, pNtHdr, &sTargetSection, section))
|
||||
return 0;
|
||||
|
||||
auto dwResult = FindPattern(
|
||||
imageBaseAddress + sTargetSection.VirtualAddress,
|
||||
sTargetSection.Misc.VirtualSize,
|
||||
pattern
|
||||
);
|
||||
|
||||
if (offset)
|
||||
dwResult += offset;
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
extern uintptr_t find_offset(uintptr_t start_addr, const uint8_t* pattern_buf, size_t pattern_len, size_t offset, size_t opcode_len) noexcept;
|
||||
extern DWORD_PTR FindPatternSafe(const DWORD_PTR baseAddress, const DWORD_PTR length, const PCWSTR pattern);
|
||||
}
|
||||
|
||||
+1118
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,413 @@
|
||||
#pragma once
|
||||
|
||||
namespace stl
|
||||
{
|
||||
struct out_of_range
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
template<class T> class vector
|
||||
{
|
||||
public:
|
||||
/* ----- Constructors ----- */
|
||||
|
||||
// Default constructor
|
||||
vector();
|
||||
|
||||
explicit vector(int s);
|
||||
|
||||
// Copy constructor
|
||||
vector(const vector& arg);
|
||||
|
||||
// Copy Assingment
|
||||
vector<T>& operator=(const vector<T>& arg);
|
||||
|
||||
// Destructor
|
||||
~vector();
|
||||
|
||||
/*----------------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* -------- ITERATORS --------*/
|
||||
|
||||
class iterator;
|
||||
|
||||
iterator begin();
|
||||
|
||||
const iterator begin() const;
|
||||
|
||||
iterator end();
|
||||
|
||||
const iterator end() const;
|
||||
|
||||
const iterator cbegin() const;
|
||||
|
||||
const iterator cend() const;
|
||||
|
||||
/*----------------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* -------- CAPACITY -------- */
|
||||
|
||||
bool empty() const;
|
||||
|
||||
// Returns size of allocated storate capacity
|
||||
size_t capacity() const;
|
||||
|
||||
// Requests a change in capacity
|
||||
// reserve() will never decrase the capacity.
|
||||
void reserve(size_t newmalloc);
|
||||
|
||||
// Changes the Vector's size.
|
||||
// If the newsize is smaller, the last elements will be lost.
|
||||
// Has a default value param for custom values when resizing.
|
||||
void resize(size_t newsize, T val = T());
|
||||
|
||||
// Returns the size of the Vector (number of elements).
|
||||
size_t size() const;
|
||||
|
||||
// Returns the maximum number of elements the Vector can hold
|
||||
size_t max_size() const;
|
||||
|
||||
// Reduces capcity to fit the size
|
||||
void shrink_to_fit();
|
||||
|
||||
/*----------------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* -------- MODIFIERS --------*/
|
||||
|
||||
// Removes all elements from the Vector
|
||||
// Capacity is not changed.
|
||||
void clear();
|
||||
|
||||
// Inserts element at the back
|
||||
void push_back(const T& d);
|
||||
|
||||
// Removes the last element from the Vector
|
||||
void pop_back();
|
||||
|
||||
/*----------------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* ----- ELEMENT ACCESS ----- */
|
||||
|
||||
// Access elements with bounds checking
|
||||
T& at(int n);
|
||||
|
||||
// Access elements with bounds checking for constant Vectors.
|
||||
const T& at(int n) const;
|
||||
|
||||
// Access elements, no bounds checking
|
||||
T& operator[](int i);
|
||||
|
||||
// Access elements, no bounds checking
|
||||
const T& operator[](int i) const;
|
||||
|
||||
// Returns a reference to the first element
|
||||
T& front();
|
||||
|
||||
// Returns a reference to the first element
|
||||
const T& front() const;
|
||||
|
||||
// Returns a reference to the last element
|
||||
T& back();
|
||||
|
||||
// Returns a reference to the last element
|
||||
const T& back() const;
|
||||
|
||||
// Returns a pointer to the array used by Vector
|
||||
T* data();
|
||||
|
||||
// Returns a pointer to the array used by Vector
|
||||
const T* data() const;
|
||||
|
||||
/*----------------------------*/
|
||||
|
||||
private:
|
||||
size_t _size; // Number of elements in Vector
|
||||
T* _elements; // Pointer to first element of Vector
|
||||
size_t _space; // Total space used by Vector including
|
||||
// elements and free space.
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class T> class vector<T>::iterator
|
||||
{
|
||||
public:
|
||||
iterator(T* p)
|
||||
:_curr(p)
|
||||
{}
|
||||
|
||||
iterator& operator++()
|
||||
{
|
||||
_curr++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator& operator--()
|
||||
{
|
||||
_curr--;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*()
|
||||
{
|
||||
return *_curr;
|
||||
}
|
||||
|
||||
bool operator==(const iterator& b) const
|
||||
{
|
||||
return *_curr == *b._curr;
|
||||
}
|
||||
|
||||
bool operator!=(const iterator& b) const
|
||||
{
|
||||
return *_curr != *b._curr;
|
||||
}
|
||||
|
||||
private:
|
||||
T* _curr;
|
||||
};
|
||||
|
||||
|
||||
|
||||
// Constructors/Destructor
|
||||
template<class T>
|
||||
vector<T>::vector()
|
||||
:_size(0), _elements(0), _space(0)
|
||||
{}
|
||||
|
||||
template<class T>
|
||||
inline vector<T>::vector(int s)
|
||||
: _size(s), _elements(new T[s]), _space(s)
|
||||
{
|
||||
for (int index = 0; index < _size; ++index)
|
||||
_elements[index] = T();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline vector<T>::vector(const vector& arg)
|
||||
:_size(arg._size), _elements(new T[arg._size])
|
||||
{
|
||||
for (int index = 0; index < arg._size; ++index)
|
||||
_elements[index] = arg._elements[index];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline vector<T>& vector<T>::operator=(const vector<T>& a)
|
||||
{
|
||||
if (this == &a) return *this; // Self-assingment not work needed
|
||||
|
||||
// Current Vector has enough space, so there is no need for new allocation
|
||||
if (a._size <= _space)
|
||||
{
|
||||
for (int index = 0; index < a._size; ++index)
|
||||
{
|
||||
_elements[index] = a._elements[index];
|
||||
_size = a._size;
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
T* p = new T[a._size];
|
||||
|
||||
for (int index = 0; index < a._size; ++index)
|
||||
p[index] = a._elements[index];
|
||||
|
||||
delete[] _elements;
|
||||
_size = a._size;
|
||||
_space = a._size;
|
||||
_elements = p;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
vector<T>::~vector()
|
||||
{
|
||||
delete[] _elements;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Iterators
|
||||
template<class T>
|
||||
inline typename vector<T>::iterator vector<T>::begin()
|
||||
{
|
||||
return vector<T>::iterator(&_elements[0]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline typename const vector<T>::iterator vector<T>::begin() const
|
||||
{
|
||||
return vector<T>::iterator(&_elements[0]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline typename vector<T>::iterator vector<T>::end()
|
||||
{
|
||||
return vector<T>::iterator(&_elements[_size]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline typename const vector<T>::iterator vector<T>::end() const
|
||||
{
|
||||
return vector<T>::iterator(&_elements[_size]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline typename const vector<T>::iterator vector<T>::cbegin() const
|
||||
{
|
||||
return vector<T>::iterator(&_elements[0]);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline typename const vector<T>::iterator vector<T>::cend() const
|
||||
{
|
||||
return vector<T>::iterator(&_elements[_size]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Capacity
|
||||
template<class T>
|
||||
inline bool vector<T>::empty() const
|
||||
{
|
||||
return (_size == 0);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline size_t vector<T>::capacity() const
|
||||
{
|
||||
return _space;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void vector<T>::reserve(size_t newalloc)
|
||||
{
|
||||
if (newalloc <= _space) return;
|
||||
|
||||
T* p = new T[newalloc];
|
||||
|
||||
for (int i = 0; i < _size; ++i)
|
||||
p[i] = _elements[i];
|
||||
|
||||
delete[] _elements;
|
||||
|
||||
_elements = p;
|
||||
|
||||
_space = newalloc;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void vector<T>::resize(size_t newsize, T val)
|
||||
{
|
||||
reserve(newsize);
|
||||
|
||||
for (auto index = _size; index < newsize; ++index)
|
||||
_elements[index] = T();
|
||||
|
||||
_size = newsize;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline size_t vector<T>::size() const
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Modifiers
|
||||
template<class T>
|
||||
inline void vector<T>::push_back(const T& d)
|
||||
{
|
||||
if (_space == 0)
|
||||
reserve(8);
|
||||
else if (_size == _space)
|
||||
reserve(2 * _space);
|
||||
|
||||
_elements[_size] = d;
|
||||
|
||||
++_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Accessors
|
||||
template<class T>
|
||||
inline T& vector<T>::at(int n)
|
||||
{
|
||||
if (n < 0 || _size <= n) throw out_of_range();
|
||||
return _elements[n];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline const T& vector<T>::at(int n) const
|
||||
{
|
||||
if (n < 0 || _size <= n) throw out_of_range();
|
||||
return _elements[n];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T& vector<T>::operator[](int i)
|
||||
{
|
||||
return _elements[i];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline const T& vector<T>::operator[](int i) const
|
||||
{
|
||||
return _elements[i];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T& vector<T>::front()
|
||||
{
|
||||
return _elements[0];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline const T& vector<T>::front() const
|
||||
{
|
||||
return _elements[0];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T& vector<T>::back()
|
||||
{
|
||||
return _elements[_size - 1];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline const T& vector<T>::back() const
|
||||
{
|
||||
return _elements[_size - 1];
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline T* vector<T>::data()
|
||||
{
|
||||
return _elements;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline const T* vector<T>::data() const
|
||||
{
|
||||
return _elements;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4244 4066 4309 4305 4244)
|
||||
|
||||
namespace stl
|
||||
{
|
||||
inline_block char wtoc(wchar_t w)
|
||||
{
|
||||
if (w == L'Ё') return 'Ё';
|
||||
if (w == L'ё') return 'ё';
|
||||
if (w >= L'А' && w <= L'Я') return w - L'А' + 'А';
|
||||
if (w >= L'а' && w <= L'я') return w - L'а' + 'а';
|
||||
|
||||
return (char)w;
|
||||
}
|
||||
|
||||
inline_block wchar_t ctow(char w)
|
||||
{
|
||||
if (w == 'Ё') return L'Ё';
|
||||
if (w == 'ё') return L'ё';
|
||||
if (w >= 'А' && w <= 'Я') return w - 'А' + L'А';
|
||||
if (w >= 'а' && w <= 'я') return w - 'а' + L'а';
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
inline_block int mbstowcs(wchar_t* Dest, const char* Source, unsigned __int64 Length, unsigned __int64 MaxCount)
|
||||
{
|
||||
int result = -1;
|
||||
|
||||
if (!Dest)
|
||||
return 0;
|
||||
|
||||
if (Dest && !MaxCount)
|
||||
return 0;
|
||||
|
||||
if (MaxCount > 0x7FFFFFFF)
|
||||
return -1;
|
||||
|
||||
if (!Length)
|
||||
{
|
||||
Length = strlen(Source);
|
||||
|
||||
if (!Length)
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (result = 0; (result < Length && result < MaxCount); result++)
|
||||
{
|
||||
Dest[result] = ctow(Source[result]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning(pop)
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace sc
|
||||
{
|
||||
extern UINT32 find_id_by_name(LPCSTR pszName);
|
||||
}
|
||||
+296
@@ -0,0 +1,296 @@
|
||||
#pragma once
|
||||
|
||||
namespace util
|
||||
{
|
||||
inline unsigned int mmh_32(const void* key, int len, unsigned int seed)
|
||||
{
|
||||
const unsigned int m = 0x5bd1e995;
|
||||
const int r = 24;
|
||||
|
||||
unsigned int h = seed ^ len;
|
||||
|
||||
const unsigned char* data = (const unsigned char*)key;
|
||||
|
||||
while (len >= 4)
|
||||
{
|
||||
unsigned int k = *(unsigned int*)data;
|
||||
|
||||
k *= m;
|
||||
k ^= k >> r;
|
||||
k *= m;
|
||||
|
||||
h *= m;
|
||||
h ^= k;
|
||||
|
||||
data += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
switch (len)
|
||||
{
|
||||
case 3: h ^= data[2] << 16;
|
||||
case 2: h ^= data[1] << 8;
|
||||
case 1: h ^= data[0];
|
||||
h *= m;
|
||||
};
|
||||
|
||||
h ^= h >> 13;
|
||||
h *= m;
|
||||
h ^= h >> 15;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
inline int string_hash(const char* text, size_t length)
|
||||
{
|
||||
return mmh_32((void*)text, static_cast<int>(length), 6);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
inline auto find_export(const char* mod, const char* proc)
|
||||
{
|
||||
auto handle = LI_FN(GetModuleHandleA)(mod);
|
||||
return (T)LI_FN(GetProcAddress)(handle, proc);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
inline auto find_export(_hmodule mod, const char* proc)
|
||||
{
|
||||
return (T)LI_FN(GetProcAddress)(mod, proc);
|
||||
}
|
||||
|
||||
__forceinline void* get_vtable_func(void* class_base, size_t index)
|
||||
{
|
||||
if (!class_base || index < 0)
|
||||
return nullptr;
|
||||
|
||||
auto** const class_vtable = *(void***)class_base;
|
||||
|
||||
if (class_vtable == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return class_vtable[index];
|
||||
}
|
||||
|
||||
__forceinline auto protect_virtual_memory(void* address, size_t length, u_long prot, u_long* prot_old = nullptr)
|
||||
{
|
||||
u_long unused_0{};
|
||||
u_long* prot_old_act = (!(prot_old == nullptr) ? prot_old : &unused_0);
|
||||
|
||||
return win_api::virtual_protect(address, length, prot, prot_old_act);
|
||||
}
|
||||
|
||||
__forceinline auto file_header(void* pc) -> void*
|
||||
{
|
||||
void* result = nullptr;
|
||||
|
||||
if (!win_api::pc_to_file_header(pc, &result))
|
||||
return nullptr;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
__forceinline void format_string(char* buffer, size_t length, const char* fmt, T&&... args)
|
||||
{
|
||||
LI_FN(wsprintfA).cached()(buffer, fmt, args...);
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
__forceinline void format_wstring(wchar_t* buffer, size_t length, const wchar_t* fmt, T&&... args)
|
||||
{
|
||||
LI_FN(wsprintfW).cached()(buffer, fmt, args...);
|
||||
}
|
||||
|
||||
__forceinline bool is_valid_address(void* p)
|
||||
{
|
||||
auto region_info = MEMORY_BASIC_INFORMATION{};
|
||||
|
||||
if (!win_api::rebuilt::virtual_query(p, ®ion_info, sizeof(region_info)))
|
||||
return false;
|
||||
|
||||
if (region_info.Protect == PAGE_GUARD)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline bool read_memory(void* dst, uintptr_t src, size_t len)
|
||||
{
|
||||
size_t processed = 0;
|
||||
|
||||
const auto result = win_api::read_process_memory(
|
||||
win_api::get_current_process(),
|
||||
(LPCVOID)src,
|
||||
dst,
|
||||
len,
|
||||
&processed
|
||||
);
|
||||
|
||||
if (!result && win_api::get_last_error() != ERROR_PARTIAL_COPY)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline bool write_data(const char* data, const wchar_t* filename)
|
||||
{
|
||||
HANDLE hFile;
|
||||
SIZE_T dwBytesToWrite = stl::strlen(data);
|
||||
DWORD dwBytesWritten;
|
||||
BOOL bErrorFlag = FALSE;
|
||||
|
||||
hFile = LI_FN(CreateFileW).cached()(filename, // name of the write
|
||||
FILE_APPEND_DATA, // open for appending
|
||||
FILE_SHARE_READ, // share for reading only
|
||||
NULL, // default security
|
||||
OPEN_ALWAYS, // open existing file or create new file
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file
|
||||
NULL); // no attr. template
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
|
||||
while (dwBytesToWrite > 0)
|
||||
{
|
||||
bErrorFlag = LI_FN(WriteFile).cached()(
|
||||
hFile, // open file handle
|
||||
data, // start of data to write
|
||||
dwBytesToWrite, // number of bytes to write
|
||||
&dwBytesWritten, // number of bytes that were written
|
||||
NULL); // no overlapped structure
|
||||
|
||||
if (!bErrorFlag)
|
||||
break;
|
||||
|
||||
data += dwBytesWritten;
|
||||
dwBytesToWrite -= dwBytesWritten;
|
||||
}
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
__forceinline bool write_data_reg(const char* data, size_t length, const char* filename)
|
||||
{
|
||||
HANDLE hFile;
|
||||
SIZE_T dwBytesToWrite = length;
|
||||
DWORD dwBytesWritten;
|
||||
BOOL bErrorFlag = FALSE;
|
||||
|
||||
hFile = LI_FN(CreateFileA).cached()(filename, // name of the write
|
||||
FILE_APPEND_DATA, // open for appending
|
||||
FILE_SHARE_READ, // share for reading only
|
||||
NULL, // default security
|
||||
OPEN_ALWAYS, // open existing file or create new file
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file
|
||||
NULL); // no attr. template
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE)
|
||||
return false;
|
||||
|
||||
while (dwBytesToWrite > 0)
|
||||
{
|
||||
bErrorFlag = LI_FN(WriteFile).cached()(
|
||||
hFile, // open file handle
|
||||
data, // start of data to write
|
||||
dwBytesToWrite, // number of bytes to write
|
||||
&dwBytesWritten, // number of bytes that were written
|
||||
NULL); // no overlapped structure
|
||||
|
||||
if (!bErrorFlag)
|
||||
break;
|
||||
|
||||
data += dwBytesWritten;
|
||||
dwBytesToWrite -= dwBytesWritten;
|
||||
}
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline BOOL file_exists(LPCSTR szPath)
|
||||
{
|
||||
DWORD dwAttrib = LI_FN(GetFileAttributesA).cached()(szPath);
|
||||
|
||||
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
|
||||
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
||||
}
|
||||
|
||||
inline BOOL file_exists_wide(LPCWSTR szPath)
|
||||
{
|
||||
DWORD dwAttrib = LI_FN(GetFileAttributesW).cached()(szPath);
|
||||
|
||||
return (dwAttrib != INVALID_FILE_ATTRIBUTES &&
|
||||
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
|
||||
}
|
||||
|
||||
inline BOOL read_file(LPCSTR pszPath, PVOID pBuffer, DWORD uLength)
|
||||
{
|
||||
HANDLE hFile = LI_FN(CreateFileA).cached()(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile)
|
||||
{
|
||||
BOOL bResult = LI_FN(ReadFile).cached()(hFile, pBuffer, uLength, &uLength, nullptr);
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
if (bResult)
|
||||
{
|
||||
((PCHAR)pBuffer)[uLength] = '\0';
|
||||
}
|
||||
|
||||
return bResult;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
extern BOOL read_file_ex(LPCWSTR pszPath, PVOID pBuffer, DWORD uLength);
|
||||
extern PVOID get_file_ptr(LPCWSTR pszPath);
|
||||
|
||||
inline DWORD get_file_size(LPCSTR pszPath)
|
||||
{
|
||||
HANDLE hFile = LI_FN(CreateFileA).cached()(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile)
|
||||
{
|
||||
DWORD dwResult = LI_FN(GetFileSize).cached()(hFile, nullptr);
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline DWORD get_file_size_wide(LPCWSTR pszPath)
|
||||
{
|
||||
HANDLE hFile = LI_FN(CreateFileW).cached()(pszPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile)
|
||||
{
|
||||
DWORD dwResult = LI_FN(GetFileSize).cached()(hFile, nullptr);
|
||||
|
||||
LI_FN(CloseHandle).cached()(hFile);
|
||||
|
||||
return dwResult;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline bool get_desktop_directory(char* buffer, size_t length)
|
||||
{
|
||||
if (!buffer || !length)
|
||||
return false;
|
||||
|
||||
if (!LI_FN(SHGetSpecialFolderPathA).cached()(HWND_DESKTOP, buffer, CSIDL_DESKTOP, FALSE))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
namespace vfunc
|
||||
{
|
||||
template <size_t Index, typename ReturnType, typename... Args>
|
||||
__forceinline ReturnType call(void* instance, Args... args)
|
||||
{
|
||||
using Fn = ReturnType(__thiscall*)(void*, Args...);
|
||||
|
||||
auto function = (*reinterpret_cast<Fn**>(instance))[Index];
|
||||
|
||||
return spoof_call(function, instance, args...);
|
||||
}
|
||||
}
|
||||
|
||||
#define VIRTUAL_FUNC(NAME, RETURN_VALUE, INDEX, ARGS, ...) RETURN_VALUE NAME ARGS { return vfunc::call<INDEX, RETURN_VALUE>(__VA_ARGS__); }
|
||||
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class protect_guard
|
||||
{
|
||||
public:
|
||||
protect_guard(void* base, size_t len, uint32_t flags)
|
||||
{
|
||||
_base = base;
|
||||
_length = len;
|
||||
if (!win_api::virtual_protect(base, len, flags, reinterpret_cast<PDWORD>(&_old)))
|
||||
{
|
||||
log_info_("failed to protect virtual memory.");
|
||||
EXIT_PROCESS(NULL);
|
||||
}
|
||||
}
|
||||
~protect_guard()
|
||||
{
|
||||
win_api::virtual_protect(_base, _length, _old, reinterpret_cast<PDWORD>(&_old));
|
||||
}
|
||||
|
||||
private:
|
||||
void* _base;
|
||||
size_t _length;
|
||||
uint32_t _old{};
|
||||
};
|
||||
}
|
||||
|
||||
class c_vfunc_hook
|
||||
{
|
||||
public:
|
||||
bool setup(void* base);
|
||||
size_t estimate_vftbl_length(std::uintptr_t* vftbl_start);
|
||||
|
||||
template<typename T>
|
||||
void hook_index(const int index, T fun)
|
||||
{
|
||||
new_vftb1[index + 1] = reinterpret_cast<std::uintptr_t>(fun);
|
||||
}
|
||||
|
||||
void unhook_index(const int index) const
|
||||
{
|
||||
new_vftb1[index] = old_vftbl[index];
|
||||
}
|
||||
|
||||
void unhook_all()
|
||||
{
|
||||
if (old_vftbl != nullptr) {
|
||||
auto guard = detail::protect_guard{ class_base, sizeof(uintptr_t), PAGE_READWRITE };
|
||||
*static_cast<uintptr_t**>(class_base) = old_vftbl;
|
||||
old_vftbl = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T get_original(const int index)
|
||||
{
|
||||
return reinterpret_cast<T>(old_vftbl[index]);
|
||||
}
|
||||
|
||||
private:
|
||||
void* class_base;
|
||||
size_t vftbl_len;
|
||||
uintptr_t* new_vftb1;
|
||||
uintptr_t* old_vftbl;
|
||||
LPCVOID search_base = nullptr;
|
||||
bool wasAllocated = false;
|
||||
};
|
||||
@@ -0,0 +1,179 @@
|
||||
#pragma once
|
||||
|
||||
#define FIND_EXPORT(MOD, EXP, TYPE) win_api::find_export<TYPE>(_XS(MOD), _XS(EXP))
|
||||
#define DECLARE_FUNC(MOD, EXP, TYPE, ID) if(!table[ID]) table[ID] = FIND_EXPORT(MOD, EXP, TYPE); \
|
||||
auto* const fn = (TYPE*)table[ID]
|
||||
|
||||
namespace win_api
|
||||
{
|
||||
#pragma data_seg("data")
|
||||
__declspec(allocate("data")) inline void* table[8192];
|
||||
#pragma data_seg()
|
||||
|
||||
template < typename T >
|
||||
T* find_export(const char* mod_name, const char* exp_name)
|
||||
{
|
||||
auto mod_handle = LI_FN(GetModuleHandleA)(mod_name);
|
||||
|
||||
if (mod_handle == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return pe::find_export<T*>(mod_handle, exp_name);
|
||||
}
|
||||
|
||||
inline_block auto get_last_error() -> DWORD
|
||||
{
|
||||
return *(DWORD*)(__readgsqword(0x30u) + 104);
|
||||
}
|
||||
|
||||
inline_block auto get_current_process() -> HANDLE
|
||||
{
|
||||
return (HANDLE)-1;
|
||||
}
|
||||
|
||||
inline_block auto get_current_thread() -> HANDLE
|
||||
{
|
||||
return (HANDLE)-2;
|
||||
}
|
||||
|
||||
inline_block auto get_current_process_id() -> u_long
|
||||
{
|
||||
return *(DWORD*)(__readgsqword(0x30u) + 64);
|
||||
}
|
||||
|
||||
inline_block auto get_process_heap() -> HANDLE
|
||||
{
|
||||
return *(HANDLE*)(__readgsqword(0x60u) + 48);
|
||||
}
|
||||
|
||||
inline_block auto get_module_handle(const char* name) -> HMODULE
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "GetModuleHandleA", decltype(get_module_handle), __COUNTER__);
|
||||
|
||||
return fn(name);
|
||||
}
|
||||
|
||||
inline_block auto get_module_handle_wide(const wchar_t* name) -> HMODULE
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "GetModuleHandleA", decltype(get_module_handle_wide), __COUNTER__);
|
||||
|
||||
return fn(name);
|
||||
}
|
||||
|
||||
inline_block auto virtual_alloc(
|
||||
void* address, size_t size,
|
||||
u_long alloc_type, u_long protect
|
||||
) -> void*
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "VirtualAlloc", decltype(virtual_alloc), __COUNTER__);
|
||||
|
||||
return fn(
|
||||
address, size,
|
||||
alloc_type, protect
|
||||
);
|
||||
}
|
||||
|
||||
inline_block auto virtual_query(const void* address, PMEMORY_BASIC_INFORMATION buffer, size_t size) -> size_t
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "VirtualQuery", decltype(virtual_query), __COUNTER__);
|
||||
|
||||
return fn(address, buffer, size);
|
||||
}
|
||||
|
||||
inline_block auto virtual_free(void* address, size_t size, u_long free_type) -> int
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "VirtualFree", decltype(virtual_free), __COUNTER__);
|
||||
|
||||
return fn(address, size, free_type);
|
||||
}
|
||||
|
||||
inline_block auto read_process_memory(_handle process, const void* base_address, void* buffer, size_t size, size_t* cnt) -> BOOL
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "ReadProcessMemory", decltype(read_process_memory), __COUNTER__);
|
||||
|
||||
return fn(process, base_address, buffer, size, cnt);
|
||||
}
|
||||
|
||||
inline_block auto output_debug_string(const char* text) -> void
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "OutputDebugStringA", decltype(output_debug_string), __COUNTER__);
|
||||
|
||||
return fn(text);
|
||||
}
|
||||
|
||||
inline_block auto suspend_thread(HANDLE handle) -> DWORD
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "SuspendThread", decltype(suspend_thread), __COUNTER__);
|
||||
|
||||
return fn(handle);
|
||||
}
|
||||
|
||||
inline_block auto heap_create(DWORD options, SIZE_T initial_size, SIZE_T maximum_size) -> HANDLE
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "HeapCreate", decltype(heap_create), __COUNTER__);
|
||||
|
||||
return fn(options, initial_size, maximum_size);
|
||||
}
|
||||
|
||||
inline_block auto heap_destroy(HANDLE handle) -> BOOL
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "HeapDestroy", decltype(heap_destroy), __COUNTER__);
|
||||
|
||||
return fn(handle);
|
||||
}
|
||||
|
||||
inline_block auto pc_to_file_header(PVOID value, PVOID* base_of_image) -> PVOID
|
||||
{
|
||||
DECLARE_FUNC("kernel32.dll", "RtlPcToFileHeader", decltype(pc_to_file_header), __COUNTER__);
|
||||
|
||||
return fn(value, base_of_image);
|
||||
}
|
||||
|
||||
inline_block auto heap_alloc(HANDLE handle, DWORD flags, SIZE_T size) -> PVOID
|
||||
{
|
||||
DECLARE_FUNC("ntdll.dll", "RtlAllocateHeap", decltype(heap_alloc), __COUNTER__);
|
||||
|
||||
return fn(handle, flags, size);
|
||||
}
|
||||
|
||||
inline_block auto heap_re_alloc(HANDLE handle, DWORD flags, PVOID ptr, SIZE_T size) -> PVOID
|
||||
{
|
||||
DECLARE_FUNC("ntdll.dll", "RtlReAllocateHeap", decltype(heap_re_alloc), __COUNTER__);
|
||||
|
||||
return fn(handle, flags, ptr, size);
|
||||
}
|
||||
|
||||
extern bool virtual_protect(void* address, size_t size, u_long new_protect, u_long* old_protect);
|
||||
|
||||
namespace rebuilt
|
||||
{
|
||||
inline_block auto virtual_alloc(void* address, size_t size, u_long alloc_type, u_long protect) -> void*
|
||||
{
|
||||
if (!NT_SUCCESS(sc_asm_alloc_virtual_memory(get_current_process(), &address, 0, &size, alloc_type, protect)))
|
||||
return nullptr;
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
inline_block auto virtual_query(const void* address, PMEMORY_BASIC_INFORMATION info_buffer, size_t length) -> size_t
|
||||
{
|
||||
if (!NT_SUCCESS(sc_asm_query_virtual_memory(get_current_process(), (void*)address, k::MemoryBasicInformation, info_buffer, length, &length)))
|
||||
return 0;
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
inline_block auto get_thread_context(_handle thread, PCONTEXT context) -> bool
|
||||
{
|
||||
return NT_SUCCESS(sc_asm_get_context_thread(thread, context));
|
||||
}
|
||||
|
||||
inline_block auto sleep(int miliseconds)
|
||||
{
|
||||
LARGE_INTEGER delay;
|
||||
delay.QuadPart = -(miliseconds * 10000);
|
||||
|
||||
sc_asm_delay_execution(FALSE, &delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
|
||||
#pragma warning(disable : 4244)
|
||||
|
||||
// =============================================================================
|
||||
namespace crypt
|
||||
{
|
||||
// =============================================================================
|
||||
|
||||
// convert __TIME__ == "hh:mm:ss" to a sum of seconds this gives us a compile-time seed
|
||||
// Note: in some weird cases I've seen the seed being different from encryption
|
||||
// to decryption so it's safer to not use time and set the seed manually
|
||||
#if 0
|
||||
#define TBX_XSTR_SEED ((__TIME__[7] - '0') * 1ull + (__TIME__[6] - '0') * 10ull + \
|
||||
(__TIME__[4] - '0') * 60ull + (__TIME__[3] - '0') * 600ull + \
|
||||
(__TIME__[1] - '0') * 3600ull + (__TIME__[0] - '0') * 36000ull)
|
||||
#else
|
||||
#define TBX_XSTR_SEED (3600ull)
|
||||
#endif
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// @return a pseudo random number clamped at 0xFFFFFFFF
|
||||
constexpr unsigned long long linear_congruent_generator(unsigned rounds)
|
||||
{
|
||||
return 1013904223ull + (1664525ull * ((rounds > 0) ? linear_congruent_generator(rounds - 1) : (TBX_XSTR_SEED)))
|
||||
% 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#define Random() linear_congruent_generator(10)
|
||||
#define XSTR_RANDOM_NUMBER(Min, Max) (Min + (Random() % (Max - Min + 1)))
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
constexpr const unsigned long long XORKEY = XSTR_RANDOM_NUMBER(0, 0xFF);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
template <typename Char>
|
||||
constexpr Char encrypt_character(const Char character, int index)
|
||||
{
|
||||
return character ^ (static_cast<Char>(XORKEY) + index);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
template <unsigned size, typename Char>
|
||||
class Xor_string
|
||||
{
|
||||
public:
|
||||
const unsigned _nb_chars = (size - 1);
|
||||
Char _string[size];
|
||||
|
||||
// if every goes alright this constructor should be executed at compile time
|
||||
constexpr Xor_string(const Char* string)
|
||||
: _string{}
|
||||
{
|
||||
for (unsigned i = 0u; i < size; ++i)
|
||||
_string[i] = encrypt_character<Char>(string[i], i);
|
||||
}
|
||||
|
||||
// This is executed at runtime.
|
||||
// HACK: although decrypt() is const we modify '_string' in place
|
||||
const Char* decrypt() const
|
||||
{
|
||||
Char* string = const_cast<Char*>(_string);
|
||||
for (unsigned t = 0; t < _nb_chars; t++)
|
||||
{
|
||||
string[t] = string[t] ^ (static_cast<Char>(XORKEY) + t);
|
||||
}
|
||||
string[_nb_chars] = '\0';
|
||||
return string;
|
||||
}
|
||||
};
|
||||
|
||||
template <unsigned size, typename uint64_t>
|
||||
class Xor_Addr
|
||||
{
|
||||
public:
|
||||
const unsigned _nb_chars = (size - 1);
|
||||
uint64_t _addr;
|
||||
|
||||
// if every goes alright this constructor should be executed at compile time
|
||||
constexpr Xor_Addr(const uint64_t addr)
|
||||
: _addr{}
|
||||
{
|
||||
_addr = (addr ^ XORKEY);
|
||||
}
|
||||
|
||||
// This is executed at runtime.
|
||||
// HACK: although decrypt() is const we modify '_string' in place
|
||||
const uint64_t decrypt() const
|
||||
{
|
||||
return (_addr ^ XORKEY);
|
||||
}
|
||||
};
|
||||
} // END crypt NAMESPACE ========================================================
|
||||
|
||||
#define XOR_S(name, my_string) constexpr crypt::Xor_string<(sizeof(my_string)/sizeof(char)), char> name(my_string)
|
||||
// Because of a limitation/bug in msvc 2017 we need to declare crypt::Xor_string() as a constexpr
|
||||
// otherwise the constructor is not evaluated at compile time. The lambda function is here to allow this declaration inside the macro
|
||||
// because there is no such thing as casting to 'constexpr' (and casting to const does not solve this bug).
|
||||
#define _XS(my_string) []()[[msvc::forceinline]]{ constexpr crypt::Xor_string<(sizeof(my_string)/sizeof(char)), char> expr(my_string); return expr; }().decrypt()
|
||||
#define _XA(my_addr)[]{ constexpr crypt::Xor_Addr<(sizeof(my_addr)/sizeof(uint64_t)), uint64_t> expr(my_addr); return expr; }().decrypt()
|
||||
|
||||
// Crypt normal string char*
|
||||
#define XOR_C( string ) XorString( string )
|
||||
|
||||
#define XOR_WS(name, my_string) constexpr crypt::Xor_string<(sizeof(my_string)/sizeof(wchar_t)), wchar_t> name(my_string)
|
||||
#define _XSW(my_string) []()[[msvc::forceinline]]{ constexpr crypt::Xor_string<(sizeof(my_string)/sizeof(wchar_t)), wchar_t> expr(my_string); return expr; }().decrypt()
|
||||
|
||||
// crypt wide characters
|
||||
#define XOR_W( string ) XorWideString( string )
|
||||
#define XOR_STD_STRING(X) XOR_STRING(X)
|
||||
|
||||
constexpr uint64_t __xor_constant_key = 0xF2E1062D448B;
|
||||
#define _XC(X) (X ^ __xor_constant_key)
|
||||
|
||||
Reference in New Issue
Block a user