From 2e95d9434add41b90900aca83c8fa8f83c966816 Mon Sep 17 00:00:00 2001 From: FutureRave Date: Wed, 29 Mar 2023 17:47:57 +0100 Subject: [PATCH] maint(string): update --- src/client/component/console.cpp | 3 ++ src/client/dllmain.cpp | 1 + src/client/std_include.hpp | 11 ++++--- src/common/utils/memory.cpp | 10 ++++--- src/common/utils/memory.hpp | 12 ++++---- src/common/utils/string.cpp | 2 +- src/common/utils/string.hpp | 49 +++++++++++++++++--------------- 7 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/client/component/console.cpp b/src/client/component/console.cpp index 512d98c..dbbcc31 100644 --- a/src/client/component/console.cpp +++ b/src/client/component/console.cpp @@ -9,6 +9,9 @@ #include "console.hpp" #include "command.hpp" +#include +#include + namespace console { namespace { using message_queue = std::queue; diff --git a/src/client/dllmain.cpp b/src/client/dllmain.cpp index 1be93ff..232ab3a 100644 --- a/src/client/dllmain.cpp +++ b/src/client/dllmain.cpp @@ -51,6 +51,7 @@ BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, ) { if (ul_reason_for_call == DLL_PROCESS_ATTACH) { AddVectoredExceptionHandler(0, exception_handler); + SetProcessDEPPolicy(PROCESS_DEP_ENABLE); std::srand(std::uint32_t(time(nullptr))); diff --git a/src/client/std_include.hpp b/src/client/std_include.hpp index 3b8907c..cd2cf42 100644 --- a/src/client/std_include.hpp +++ b/src/client/std_include.hpp @@ -8,21 +8,20 @@ #include #include -#include -#include - #include #include #include +#include +#include +#include #include #include #include +#include +#include #include #include -#include -#include -#include #pragma comment(lib, "ntdll.lib") #pragma comment(lib, "ws2_32.lib") diff --git a/src/common/utils/memory.cpp b/src/common/utils/memory.cpp index ad459c4..5b5c163 100644 --- a/src/common/utils/memory.cpp +++ b/src/common/utils/memory.cpp @@ -30,7 +30,7 @@ void memory::allocator::free(const void* data) { this->free(const_cast(data)); } -void* memory::allocator::allocate(const size_t length) { +void* memory::allocator::allocate(const std::size_t length) { std::lock_guard _(this->mutex_); const auto data = memory::allocate(length); @@ -48,7 +48,9 @@ char* memory::allocator::duplicate_string(const std::string& string) { return data; } -void* memory::allocate(const size_t length) { return std::calloc(length, 1); } +void* memory::allocate(const std::size_t length) { + return std::calloc(1, length); +} char* memory::duplicate_string(const std::string& string) { const auto new_string = allocate_array(string.size() + 1); @@ -60,10 +62,10 @@ void memory::free(void* data) { std::free(data); } void memory::free(const void* data) { free(const_cast(data)); } -bool memory::is_set(const void* mem, const char chr, const size_t length) { +bool memory::is_set(const void* mem, const char chr, const std::size_t length) { const auto mem_arr = static_cast(mem); - for (size_t i = 0; i < length; ++i) { + for (std::size_t i = 0; i < length; ++i) { if (mem_arr[i] != chr) { return false; } diff --git a/src/common/utils/memory.hpp b/src/common/utils/memory.hpp index cacf25f..3e335d8 100644 --- a/src/common/utils/memory.hpp +++ b/src/common/utils/memory.hpp @@ -16,15 +16,15 @@ public: void free(const void* data); - void* allocate(size_t length); + void* allocate(std::size_t length); template T* allocate() { return this->allocate_array(1); } - template T* allocate_array(const size_t count = 1) { + template T* allocate_array(const std::size_t count = 1) { return static_cast(this->allocate(count * sizeof(T))); } - bool empty() const; + [[nodiscard]] bool empty() const; char* duplicate_string(const std::string& string); @@ -33,11 +33,11 @@ public: std::vector pool_; }; - static void* allocate(size_t length); + static void* allocate(std::size_t length); template static T* allocate() { return allocate_array(1); } - template static T* allocate_array(const size_t count = 1) { + template static T* allocate_array(const std::size_t count = 1) { return static_cast(allocate(count * sizeof(T))); } @@ -46,7 +46,7 @@ public: static void free(void* data); static void free(const void* data); - static bool is_set(const void* mem, char chr, size_t length); + static bool is_set(const void* mem, char chr, std::size_t length); static bool is_bad_read_ptr(const void* ptr); static bool is_bad_code_ptr(const void* ptr); diff --git a/src/common/utils/string.cpp b/src/common/utils/string.cpp index d882f61..11d3354 100644 --- a/src/common/utils/string.cpp +++ b/src/common/utils/string.cpp @@ -7,7 +7,7 @@ namespace utils::string { const char* va(const char* fmt, ...) { - static thread_local va_provider<8, 256> provider; + static thread_local va_provider<8, 1024> provider; va_list ap; va_start(ap, fmt); diff --git a/src/common/utils/string.hpp b/src/common/utils/string.hpp index a3de846..7b2639f 100644 --- a/src/common/utils/string.hpp +++ b/src/common/utils/string.hpp @@ -2,14 +2,17 @@ #include "memory.hpp" #ifndef ARRAYSIZE -template size_t ARRAYSIZE(Type (&)[n]) { return n; } +template std::size_t ARRAYSIZE(Type (&)[n]) { + return n; +} #endif namespace utils::string { -template class va_provider final { +template +class va_provider final { public: - static_assert(Buffers != 0 && MinBufferSize != 0, - "Buffers and MinBufferSize mustn't be 0"); + static_assert(buffers != 0 && min_buffer_size != 0, + "buffers and min_buffer_size mustn't be 0"); va_provider() : current_buffer_(0) {} @@ -17,13 +20,13 @@ public: ++this->current_buffer_ %= ARRAYSIZE(this->string_pool_); auto entry = &this->string_pool_[this->current_buffer_]; - if (!entry->size || !entry->buffer) { + if (!entry->size_ || !entry->buffer_) { throw std::runtime_error("String pool not initialized"); } while (true) { const int res = - _vsnprintf_s(entry->buffer, entry->size, _TRUNCATE, format, ap); + vsnprintf_s(entry->buffer_, entry->size_, _TRUNCATE, format, ap); if (res > 0) break; // Success if (res == 0) @@ -32,44 +35,44 @@ public: entry->double_size(); } - return entry->buffer; + return entry->buffer_; } private: class entry final { public: - explicit entry(const size_t _size = MinBufferSize) - : size(_size), buffer(nullptr) { - if (this->size < MinBufferSize) - this->size = MinBufferSize; + explicit entry(const std::size_t size = min_buffer_size) + : size_(size), buffer_(nullptr) { + if (this->size_ < min_buffer_size) + this->size_ = min_buffer_size; this->allocate(); } ~entry() { - if (this->buffer) - memory::get_allocator()->free(this->buffer); - this->size = 0; - this->buffer = nullptr; + if (this->buffer_) + memory::get_allocator()->free(this->buffer_); + this->size_ = 0; + this->buffer_ = nullptr; } void allocate() { - if (this->buffer) - memory::get_allocator()->free(this->buffer); - this->buffer = - memory::get_allocator()->allocate_array(this->size + 1); + if (this->buffer_) + memory::get_allocator()->free(this->buffer_); + this->buffer_ = + memory::get_allocator()->allocate_array(this->size_ + 1); } void double_size() { - this->size *= 2; + this->size_ *= 2; this->allocate(); } - size_t size; - char* buffer; + size_t size_; + char* buffer_; }; size_t current_buffer_; - entry string_pool_[Buffers]; + entry string_pool_[buffers]; }; const char* va(const char* fmt, ...);