[Utils]: Re-order namespaces (#576)

* [Utils]: Re-order namespaces

* [Steam]: Fix comp
This commit is contained in:
Edo 2022-11-22 14:09:25 +00:00 committed by GitHub
parent 6a8088281f
commit e8e1543cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 619 additions and 636 deletions

View File

@ -19,7 +19,7 @@
#include <ShlObj.h>
#include <timeapi.h>
#include <shellapi.h>
#include <WinInet.h>
#include <wininet.h>
#include <d3d9.h>
#include <AclAPI.h>
#include <Psapi.h>

View File

@ -1,8 +1,6 @@
#include <STDInclude.hpp>
namespace Utils
{
namespace Compression
namespace Utils::Compression
{
std::string ZLib::Compress(const std::string& data)
{
@ -16,7 +14,7 @@ namespace Utils
char* buffer = allocator.allocateArray<char>(length);
if (compress2(reinterpret_cast<Bytef*>(buffer), &length, reinterpret_cast<Bytef*>(const_cast<char*>(data.data())), data.size(), Z_BEST_COMPRESSION) != Z_OK)
{
return "";
return {};
}
return std::string(buffer, length);
@ -30,13 +28,13 @@ namespace Utils
if (inflateInit(&stream) != Z_OK)
{
return "";
return {};
}
int ret;
Memory::Allocator allocator;
uint8_t* dest = allocator.allocateArray<uint8_t>(CHUNK);
std::uint8_t* dest = allocator.allocateArray<uint8_t>(CHUNK);
const char* dataPtr = data.data();
do
@ -54,7 +52,7 @@ namespace Utils
if (ret != Z_OK && ret != Z_STREAM_END)
{
inflateEnd(&stream);
return "";
return {};
}
buffer.append(reinterpret_cast<const char*>(dest), CHUNK - stream.avail_out);
@ -67,4 +65,3 @@ namespace Utils
return buffer;
}
}
}

View File

@ -4,9 +4,7 @@
#define DEFLATE_ZLIB false
#define DEFLATE_ZSTD true
namespace Utils
{
namespace Compression
namespace Utils::Compression
{
class ZLib
{
@ -14,5 +12,4 @@ namespace Utils
static std::string Compress(const std::string& data);
static std::string Decompress(const std::string& data);
};
};
}

View File

@ -19,17 +19,17 @@ namespace Utils
std::string Rand::GenerateChallenge()
{
std::string challenge;
challenge.append(Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()));
challenge.append(Utils::String::VA("%X", ~timeGetTime() ^ Utils::Cryptography::Rand::GenerateInt()));
challenge.append(Utils::String::VA("%X", Utils::Cryptography::Rand::GenerateInt()));
challenge.append(String::VA("%X", GenerateInt()));
challenge.append(String::VA("%X", ~timeGetTime() ^ GenerateInt()));
challenge.append(String::VA("%X", GenerateInt()));
return challenge;
}
uint32_t Rand::GenerateInt()
std::uint32_t Rand::GenerateInt()
{
uint32_t number = 0;
fortuna_read(reinterpret_cast<uint8_t*>(&number), sizeof(number), &Rand::State);
std::uint32_t number = 0;
fortuna_read(reinterpret_cast<std::uint8_t*>(&number), sizeof(number), &Rand::State);
return number;
}
@ -46,7 +46,7 @@ namespace Utils
ECC::Key ECC::GenerateKey(int bits)
{
ECC::Key key;
Key key;
ltc_mp = ltm_desc;
register_prng(&sprng_desc);
@ -57,16 +57,16 @@ namespace Utils
std::string ECC::SignMessage(Key key, const std::string& message)
{
if (!key.isValid()) return "";
if (!key.isValid()) return {};
uint8_t buffer[512];
std::uint8_t buffer[512];
DWORD length = sizeof(buffer);
ltc_mp = ltm_desc;
register_prng(&sprng_desc);
ecc_sign_hash(reinterpret_cast<const uint8_t*>(message.data()), message.size(), buffer, &length, nullptr, find_prng("sprng"), key.getKeyPtr());
ecc_sign_hash(reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), buffer, &length, nullptr, find_prng("sprng"), key.getKeyPtr());
return std::string(reinterpret_cast<char*>(buffer), length);
return {reinterpret_cast<char*>(buffer), length};
}
bool ECC::VerifyMessage(Key key, const std::string& message, const std::string& signature)
@ -76,7 +76,7 @@ namespace Utils
ltc_mp = ltm_desc;
int result = 0;
return (ecc_verify_hash(reinterpret_cast<const uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const uint8_t*>(message.data()), message.size(), &result, key.getKeyPtr()) == CRYPT_OK && result != 0);
return (ecc_verify_hash(reinterpret_cast<const std::uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), &result, key.getKeyPtr()) == CRYPT_OK && result != 0);
}
#pragma endregion
@ -85,7 +85,7 @@ namespace Utils
RSA::Key RSA::GenerateKey(int bits)
{
RSA::Key key;
Key key;
register_prng(&sprng_desc);
register_hash(&sha1_desc);
@ -97,11 +97,11 @@ namespace Utils
return key;
}
std::string RSA::SignMessage(RSA::Key key, const std::string& message)
std::string RSA::SignMessage(Key key, const std::string& message)
{
if (!key.isValid()) return "";
if (!key.isValid()) return {};
uint8_t buffer[512];
std::uint8_t buffer[512];
DWORD length = sizeof(buffer);
register_prng(&sprng_desc);
@ -109,9 +109,9 @@ namespace Utils
ltc_mp = ltm_desc;
rsa_sign_hash(reinterpret_cast<const uint8_t*>(message.data()), message.size(), buffer, &length, NULL, find_prng("sprng"), find_hash("sha1"), 0, key.getKeyPtr());
rsa_sign_hash(reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), buffer, &length, NULL, find_prng("sprng"), find_hash("sha1"), 0, key.getKeyPtr());
return std::string(reinterpret_cast<char*>(buffer), length);
return {reinterpret_cast<char*>(buffer), length};
}
bool RSA::VerifyMessage(Key key, const std::string& message, const std::string& signature)
@ -123,7 +123,7 @@ namespace Utils
ltc_mp = ltm_desc;
int result = 0;
return (rsa_verify_hash(reinterpret_cast<const uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const uint8_t*>(message.data()), message.size(), find_hash("sha1"), 0, &result, key.getKeyPtr()) == CRYPT_OK && result != 0);
return (rsa_verify_hash(reinterpret_cast<const std::uint8_t*>(signature.data()), signature.size(), reinterpret_cast<const std::uint8_t*>(message.data()), message.size(), find_hash("sha1"), 0, &result, key.getKeyPtr()) == CRYPT_OK && result != 0);
}
#pragma endregion
@ -143,8 +143,8 @@ namespace Utils
symmetric_CBC cbc;
int des3 = find_cipher("3des");
cbc_start(des3, reinterpret_cast<const uint8_t*>(iv.data()), reinterpret_cast<const uint8_t*>(key.data()), key.size(), 0, &cbc);
cbc_encrypt(reinterpret_cast<const uint8_t*>(text.data()), reinterpret_cast<uint8_t*>(const_cast<char*>(encData.data())), text.size(), &cbc);
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), 0, &cbc);
cbc_encrypt(reinterpret_cast<const std::uint8_t*>(text.data()), reinterpret_cast<uint8_t*>(encData.data()), text.size(), &cbc);
cbc_done(&cbc);
return encData;
@ -158,8 +158,8 @@ namespace Utils
symmetric_CBC cbc;
int des3 = find_cipher("3des");
cbc_start(des3, reinterpret_cast<const uint8_t*>(iv.data()), reinterpret_cast<const uint8_t*>(key.data()), key.size(), 0, &cbc);
cbc_decrypt(reinterpret_cast<const uint8_t*>(data.data()), reinterpret_cast<uint8_t*>(const_cast<char*>(decData.data())), data.size(), &cbc);
cbc_start(des3, reinterpret_cast<const std::uint8_t*>(iv.data()), reinterpret_cast<const std::uint8_t*>(key.data()), key.size(), 0, &cbc);
cbc_decrypt(reinterpret_cast<const std::uint8_t*>(data.data()), reinterpret_cast<std::uint8_t*>(decData.data()), data.size(), &cbc);
cbc_done(&cbc);
return decData;
@ -171,12 +171,12 @@ namespace Utils
std::string Tiger::Compute(const std::string& data, bool hex)
{
return Tiger::Compute(reinterpret_cast<const uint8_t*>(data.data()), data.size(), hex);
return Compute(reinterpret_cast<const std::uint8_t*>(data.data()), data.size(), hex);
}
std::string Tiger::Compute(const uint8_t* data, size_t length, bool hex)
std::string Tiger::Compute(const std::uint8_t* data, std::size_t length, bool hex)
{
uint8_t buffer[24] = { 0 };
std::uint8_t buffer[24]{};
hash_state state;
tiger_init(&state);
@ -186,7 +186,7 @@ namespace Utils
std::string hash(reinterpret_cast<char*>(buffer), sizeof(buffer));
if (!hex) return hash;
return Utils::String::DumpHex(hash, "");
return String::DumpHex(hash, "");
}
#pragma endregion
@ -195,12 +195,12 @@ namespace Utils
std::string SHA1::Compute(const std::string& data, bool hex)
{
return SHA1::Compute(reinterpret_cast<const uint8_t*>(data.data()), data.size(), hex);
return Compute(reinterpret_cast<const std::uint8_t*>(data.data()), data.size(), hex);
}
std::string SHA1::Compute(const uint8_t* data, size_t length, bool hex)
std::string SHA1::Compute(const std::uint8_t* data, std::size_t length, bool hex)
{
uint8_t buffer[20] = { 0 };
std::uint8_t buffer[20]{};
hash_state state;
sha1_init(&state);
@ -210,7 +210,7 @@ namespace Utils
std::string hash(reinterpret_cast<char*>(buffer), sizeof(buffer));
if (!hex) return hash;
return Utils::String::DumpHex(hash, "");
return String::DumpHex(hash, "");
}
#pragma endregion
@ -219,12 +219,12 @@ namespace Utils
std::string SHA256::Compute(const std::string& data, bool hex)
{
return SHA256::Compute(reinterpret_cast<const uint8_t*>(data.data()), data.size(), hex);
return Compute(reinterpret_cast<const std::uint8_t*>(data.data()), data.size(), hex);
}
std::string SHA256::Compute(const uint8_t* data, size_t length, bool hex)
std::string SHA256::Compute(const std::uint8_t* data, std::size_t length, bool hex)
{
uint8_t buffer[32] = { 0 };
std::uint8_t buffer[32]{};
hash_state state;
sha256_init(&state);
@ -234,7 +234,7 @@ namespace Utils
std::string hash(reinterpret_cast<char*>(buffer), sizeof(buffer));
if (!hex) return hash;
return Utils::String::DumpHex(hash, "");
return String::DumpHex(hash, "");
}
#pragma endregion
@ -243,12 +243,12 @@ namespace Utils
std::string SHA512::Compute(const std::string& data, bool hex)
{
return SHA512::Compute(reinterpret_cast<const uint8_t*>(data.data()), data.size(), hex);
return Compute(reinterpret_cast<const std::uint8_t*>(data.data()), data.size(), hex);
}
std::string SHA512::Compute(const uint8_t* data, size_t length, bool hex)
std::string SHA512::Compute(const std::uint8_t* data, std::size_t length, bool hex)
{
uint8_t buffer[64] = { 0 };
std::uint8_t buffer[64]{};
hash_state state;
sha512_init(&state);
@ -258,7 +258,7 @@ namespace Utils
std::string hash(reinterpret_cast<char*>(buffer), sizeof(buffer));
if (!hex) return hash;
return Utils::String::DumpHex(hash, "");
return String::DumpHex(hash, "");
}
#pragma endregion
@ -267,7 +267,7 @@ namespace Utils
unsigned int JenkinsOneAtATime::Compute(const std::string& data)
{
return JenkinsOneAtATime::Compute(data.data(), data.size());
return Compute(data.data(), data.size());
}
unsigned int JenkinsOneAtATime::Compute(const char *key, size_t len)
@ -286,6 +286,5 @@ namespace Utils
}
#pragma endregion
}
}

View File

@ -31,7 +31,7 @@ namespace Utils
if (!i)
{
// Prepend here, as /dev/urandom says so ;) https://github.com/IW4x/iw4x-client-node/wikis/technical-information#incrementing-the-token
this->tokenString = std::basic_string<uint8_t>(reinterpret_cast<uint8_t*>(const_cast<char*>("\0")), 1) + this->tokenString;
this->tokenString = std::basic_string<std::uint8_t>(reinterpret_cast<std::uint8_t*>(const_cast<char*>("\0")), 1) + this->tokenString;
break;
}
}
@ -253,16 +253,18 @@ namespace Utils
Key() : keyStorage(new rsa_key)
{
ZeroMemory(this->getKeyPtr(), sizeof(*this->getKeyPtr()));
};
Key(rsa_key* key) : Key() { if (key) std::memmove(this->getKeyPtr(), key, sizeof(*key)); };
Key(rsa_key key) : Key(&key) {};
}
Key(rsa_key* key) : Key() { if (key) std::memmove(this->getKeyPtr(), key, sizeof(*key)); }
Key(rsa_key key) : Key(&key) {}
~Key()
{
if (this->keyStorage.use_count() <= 1)
{
this->free();
}
};
}
rsa_key* getKeyPtr()
{
@ -271,7 +273,7 @@ namespace Utils
bool isValid()
{
return (!Utils::Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
return (!Memory::IsSet(this->getKeyPtr(), 0, sizeof(*this->getKeyPtr())));
}
void free()
@ -305,35 +307,35 @@ namespace Utils
{
public:
static std::string Compute(const std::string& data, bool hex = false);
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
static std::string Compute(const std::uint8_t* data, std::size_t length, bool hex = false);
};
class SHA1
{
public:
static std::string Compute(const std::string& data, bool hex = false);
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
static std::string Compute(const std::uint8_t* data, std::size_t length, bool hex = false);
};
class SHA256
{
public:
static std::string Compute(const std::string& data, bool hex = false);
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
static std::string Compute(const std::uint8_t* data, std::size_t length, bool hex = false);
};
class SHA512
{
public:
static std::string Compute(const std::string& data, bool hex = false);
static std::string Compute(const uint8_t* data, size_t length, bool hex = false);
static std::string Compute(const std::uint8_t* data, std::size_t length, bool hex = false);
};
class JenkinsOneAtATime
{
public:
static unsigned int Compute(const std::string& data);
static unsigned int Compute(const char *key, size_t len);
static unsigned int Compute(const char *key, std::size_t len);
};
}
}

View File

@ -2,7 +2,7 @@
namespace Utils
{
std::string Entities::build()
std::string Entities::build() const
{
std::string entityString;
@ -31,9 +31,9 @@ namespace Utils
for (auto& entity : this->entities)
{
if (entity.find("model") != entity.end())
if (const auto itr = entity.find("model"); itr != entity.end())
{
std::string model = entity["model"];
const auto& model = itr->second;
if (!model.empty() && model[0] != '*' && model[0] != '?') // Skip brushmodels
{
@ -55,7 +55,7 @@ namespace Utils
if (i->find("classname") != i->end())
{
std::string classname = (*i)["classname"];
if (Utils::String::StartsWith(classname, "trigger_"))
if (String::StartsWith(classname, "trigger_"))
{
i = this->entities.erase(i);
continue;
@ -105,9 +105,9 @@ namespace Utils
std::string value;
std::unordered_map<std::string, std::string> entity;
for (unsigned int i = 0; i < buffer.size(); ++i)
for (std::size_t i = 0; i < buffer.size(); ++i)
{
char character = buffer[i];
const auto character = buffer[i];
if (character == '{')
{
entity.clear();
@ -146,7 +146,7 @@ namespace Utils
}
else if (parseState == PARSE_READ_VALUE)
{
entity[Utils::String::ToLower(key)] = value;
entity[String::ToLower(key)] = value;
parseState = PARSE_AWAIT_KEY;
}
else

View File

@ -5,12 +5,12 @@ namespace Utils
class Entities
{
public:
Entities() {};
Entities(const char* string, size_t lenPlusOne) : Entities(std::string(string, lenPlusOne - 1)) {}
Entities(const std::string& buffer) : Entities() { this->parse(buffer); };
Entities(const Entities &obj) : entities(obj.entities) {};
Entities() = default;
Entities(const std::string& buffer) : Entities() { this->parse(buffer); }
Entities(const char* string, std::size_t lenPlusOne) : Entities(std::string(string, lenPlusOne - 1)) {}
Entities(const Entities& obj) = default;
std::string build();
[[nodiscard]] std::string build() const;
std::vector<std::string> getModels();
void deleteTriggers();

View File

@ -1,8 +1,6 @@
#include <STDInclude.hpp>
namespace Utils
{
namespace IO
namespace Utils::IO
{
bool FileExists(const std::string& file)
{
@ -12,7 +10,7 @@ namespace Utils
bool WriteFile(const std::string& file, const std::string& data, bool append)
{
auto pos = file.find_last_of("/\\");
const auto pos = file.find_last_of("/\\");
if (pos != std::string::npos)
{
CreateDir(file.substr(0, pos));
@ -22,7 +20,7 @@ namespace Utils
if (stream.is_open())
{
stream.write(data.data(), data.size());
stream.write(data.data(), static_cast<std::streamsize>(data.size()));
stream.close();
return true;
}
@ -111,4 +109,3 @@ namespace Utils
return files;
}
}
}

View File

@ -6,22 +6,22 @@ namespace Utils
void* Memory::AllocateAlign(size_t length, size_t alignment)
{
void* data = _aligned_malloc(length, alignment);
assert(data != nullptr);
auto* data = _aligned_malloc(length, alignment);
assert(data);
if (data) ZeroMemory(data, length);
return data;
}
void* Memory::Allocate(size_t length)
{
void* data = calloc(length, 1);
assert(data != nullptr);
auto* data = calloc(length, 1);
assert(data);
return data;
}
char* Memory::DuplicateString(const std::string& string)
{
char* newString = Memory::AllocateArray<char>(string.size() + 1);
auto* newString = Memory::AllocateArray<char>(string.size() + 1);
std::memcpy(newString, string.data(), string.size());
return newString;
}
@ -30,13 +30,13 @@ namespace Utils
{
if (data)
{
free(data);
::free(data);
}
}
void Memory::Free(const void* data)
{
Memory::Free(const_cast<void*>(data));
Free(const_cast<void*>(data));
}
void Memory::FreeAlign(void* data)
@ -49,13 +49,13 @@ namespace Utils
void Memory::FreeAlign(const void* data)
{
Memory::FreeAlign(const_cast<void*>(data));
FreeAlign(const_cast<void*>(data));
}
// Complementary function for memset, which checks if memory is filled with a char
bool Memory::IsSet(void* mem, char chr, size_t length)
{
char* memArr = reinterpret_cast<char*>(mem);
auto* memArr = static_cast<char*>(mem);
for (size_t i = 0; i < length; ++i)
{
@ -98,7 +98,7 @@ namespace Utils
return true;
}
Utils::Memory::Allocator* Memory::GetAllocator()
Memory::Allocator* Memory::GetAllocator()
{
return &Memory::MemAllocator;
}

View File

@ -22,7 +22,7 @@ namespace Utils
void clear()
{
std::lock_guard<std::mutex> _(this->mutex);
std::lock_guard _(this->mutex);
for (auto i = this->refMemory.begin(); i != this->refMemory.end(); ++i)
{
@ -36,7 +36,7 @@ namespace Utils
for (const auto& data : this->pool)
{
Memory::Free(data);
Free(data);
}
this->pool.clear();
@ -44,7 +44,7 @@ namespace Utils
void free(void* data)
{
std::lock_guard<std::mutex> _(this->mutex);
std::lock_guard _(this->mutex);
auto i = this->refMemory.find(data);
if (i != this->refMemory.end())
@ -56,7 +56,7 @@ namespace Utils
auto j = std::find(this->pool.begin(), this->pool.end(), data);
if (j != this->pool.end())
{
Memory::Free(data);
Free(data);
this->pool.erase(j);
}
}
@ -68,45 +68,45 @@ namespace Utils
void reference(void* memory, FreeCallback callback)
{
std::lock_guard<std::mutex> _(this->mutex);
std::lock_guard _(this->mutex);
this->refMemory[memory] = callback;
}
void* allocate(size_t length)
{
std::lock_guard<std::mutex> _(this->mutex);
std::lock_guard _(this->mutex);
void* data = Memory::Allocate(length);
void* data = Allocate(length);
this->pool.push_back(data);
return data;
}
template <typename T> inline T* allocate()
template <typename T> T* allocate()
{
return this->allocateArray<T>(1);
}
template <typename T> inline T* allocateArray(size_t count = 1)
template <typename T> T* allocateArray(size_t count = 1)
{
return static_cast<T*>(this->allocate(count * sizeof(T)));
}
bool empty()
bool empty() const
{
return (this->pool.empty() && this->refMemory.empty());
}
char* duplicateString(const std::string& string)
{
std::lock_guard<std::mutex> _(this->mutex);
std::lock_guard _(this->mutex);
char* data = Memory::DuplicateString(string);
char* data = DuplicateString(string);
this->pool.push_back(data);
return data;
}
bool isPointerMapped(void* ptr)
bool isPointerMapped(void* ptr) const
{
return this->ptrMap.contains(ptr);
}
@ -115,7 +115,7 @@ namespace Utils
{
if (this->isPointerMapped(oldPtr))
{
return reinterpret_cast<T*>(this->ptrMap[oldPtr]);
return static_cast<T*>(this->ptrMap[oldPtr]);
}
return nullptr;
@ -135,16 +135,16 @@ namespace Utils
static void* AllocateAlign(size_t length, size_t alignment);
static void* Allocate(size_t length);
template <typename T> static inline T* Allocate()
template <typename T> static T* Allocate()
{
return AllocateArray<T>(1);
}
template <typename T> static inline T* AllocateArray(size_t count = 1)
template <typename T> static T* AllocateArray(size_t count = 1)
{
return static_cast<T*>(Allocate(count * sizeof(T)));
}
template <typename T> static inline T* Duplicate(T* original)
template <typename T> static T* Duplicate(T* original)
{
T* data = Memory::Allocate<T>();
std::memcpy(data, original, sizeof(T));
@ -164,9 +164,9 @@ namespace Utils
static bool IsBadReadPtr(const void* ptr);
static bool IsBadCodePtr(const void* ptr);
static Utils::Memory::Allocator* GetAllocator();
static Allocator* GetAllocator();
private:
static Utils::Memory::Allocator MemAllocator;
static Allocator MemAllocator;
};
}

View File

@ -29,7 +29,7 @@ namespace Utils
throw std::runtime_error("Reading past the buffer");
}
void* Stream::Reader::read(size_t size, size_t count)
void* Stream::Reader::read(size_t size, std::size_t count)
{
size_t bytes = size * count;
@ -112,17 +112,17 @@ namespace Utils
}
};
size_t Stream::length()
std::size_t Stream::length() const
{
return this->buffer.length();
}
size_t Stream::capacity()
std::size_t Stream::capacity() const
{
return this->buffer.capacity();
}
void Stream::assertPointer(const void* pointer, size_t length)
void Stream::assertPointer(const void* pointer, std::size_t length)
{
if (!this->ptrAssertion) return;
@ -131,22 +131,24 @@ namespace Utils
unsigned int ePtr = reinterpret_cast<unsigned int>(entry.first);
unsigned int tPtr = reinterpret_cast<unsigned int>(pointer);
if (Utils::HasIntercection(ePtr, entry.second, tPtr, length))
if (HasIntersection(ePtr, entry.second, tPtr, length))
{
MessageBoxA(nullptr, "Duplicate data written!", "ERROR", MB_ICONERROR);
#ifdef _DEBUG
__debugbreak();
#endif
}
}
this->ptrList.push_back({ pointer, length });
}
char* Stream::save(const void* _str, size_t size, size_t count)
char* Stream::save(const void* _str, std::size_t size, std::size_t count)
{
return this->save(this->getCurrentBlock(), _str, size, count);
}
char* Stream::save(Game::XFILE_BLOCK_TYPES stream, const void * _str, size_t size, size_t count)
char* Stream::save(Game::XFILE_BLOCK_TYPES stream, const void * _str, std::size_t size, std::size_t count)
{
// Only those seem to actually write data.
// everything else is allocated at runtime but XFILE_BLOCK_RUNTIME is the only one that actually allocates anything
@ -179,7 +181,7 @@ namespace Utils
return this->at() - (size * count);
}
char* Stream::save(Game::XFILE_BLOCK_TYPES stream, int value, size_t count)
char* Stream::save(Game::XFILE_BLOCK_TYPES stream, int value, std::size_t count)
{
auto ret = this->length();
@ -201,7 +203,7 @@ namespace Utils
return this->saveString(string, strlen(string));
}
char* Stream::saveString(const char* string, size_t len)
char* Stream::saveString(const char* string, std::size_t len)
{
auto ret = this->length();
@ -220,7 +222,7 @@ namespace Utils
return this->save(string.data(), string.length());
}
char* Stream::saveByte(unsigned char byte, size_t count)
char* Stream::saveByte(unsigned char byte, std::size_t count)
{
auto ret = this->length();

View File

@ -16,7 +16,7 @@ namespace Utils
{
private:
bool ptrAssertion;
std::vector<std::pair<const void*, size_t>> ptrList;
std::vector<std::pair<const void*, std::size_t>> ptrList;
int criticalSectionState;
unsigned int blockSize[Game::MAX_XFILE_COUNT];
@ -27,21 +27,21 @@ namespace Utils
class Reader
{
public:
Reader(Utils::Memory::Allocator* _allocator, const std::string& _buffer) : position(0), buffer(_buffer), allocator(_allocator) {}
Reader(Memory::Allocator* _allocator, const std::string& _buffer) : position(0), buffer(_buffer), allocator(_allocator) {}
std::string readString();
const char* readCString();
char readByte();
void* read(size_t size, size_t count = 1);
template <typename T> inline T* readObject()
void* read(std::size_t size, std::size_t count = 1);
template <typename T> T* readObject()
{
return readArray<T>(1);
}
template <typename T> inline T* readArray(size_t count = 1)
template <typename T> T* readArray(std::size_t count = 1)
{
return reinterpret_cast<T*>(this->read(sizeof(T), count));
return static_cast<T*>(this->read(sizeof(T), count));
}
template <typename T> T read()
{
@ -89,25 +89,25 @@ namespace Utils
Stream(size_t size);
~Stream();
size_t length();
size_t capacity();
std::size_t length() const;
std::size_t capacity() const;
char* save(const void * _str, size_t size, size_t count = 1);
char* save(Game::XFILE_BLOCK_TYPES stream, const void * _str, size_t size, size_t count);
char* save(Game::XFILE_BLOCK_TYPES stream, int value, size_t count);
char* save(const void * _str, std::size_t size, std::size_t count = 1);
char* save(Game::XFILE_BLOCK_TYPES stream, const void * _str, std::size_t size, std::size_t count);
char* save(Game::XFILE_BLOCK_TYPES stream, int value, std::size_t count);
template <typename T> inline char* save(T* object)
{
return saveArray<T>(object, 1);
}
template <typename T> inline char* saveArray(T* array, size_t count)
template <typename T> inline char* saveArray(T* array, std::size_t count)
{
return save(array, sizeof(T), count);
}
char* saveString(const std::string& string);
char* saveString(const char* string);
char* saveString(const char* string, size_t len);
char* saveByte(unsigned char byte, size_t count = 1);
char* saveString(const char* string, std::size_t len);
char* saveByte(unsigned char byte, std::size_t count = 1);
char* saveNull(size_t count = 1);
char* saveMax(size_t count = 1);
@ -140,7 +140,7 @@ namespace Utils
{
this->ptrAssertion = value;
}
void assertPointer(const void* pointer, size_t length);
void assertPointer(const void* pointer, std::size_t length);
void toBuffer(std::string& outBuffer);
std::string toBuffer();

View File

@ -3,9 +3,7 @@
#include "base128.h"
#endif
namespace Utils
{
namespace String
namespace Utils::String
{
const char *VA(const char *fmt, ...)
{
@ -55,14 +53,14 @@ namespace Utils
{
std::string result;
for (unsigned int i = 0; i < data.size(); ++i)
for (std::size_t i = 0; i < data.size(); ++i)
{
if (i > 0)
{
result.append(separator);
}
result.append(Utils::String::VA("%02X", data[i] & 0xFF));
result.append(VA("%02X", data[i] & 0xFF));
}
return result;
@ -70,9 +68,9 @@ namespace Utils
std::string XOR(std::string str, char value)
{
for (unsigned int i = 0; i < str.size(); ++i)
for (std::size_t i = 0; i < str.size(); ++i)
{
str[i] ^= value;
str[i] ^= static_cast<unsigned char>(value);
}
return str;
@ -116,8 +114,10 @@ namespace Utils
bool IsNumber(const std::string& str)
{
return !str.empty() && std::find_if(str.begin(),
str.end(), [](unsigned char input) { return !std::isdigit(input); }) == str.end();
return !str.empty() && std::find_if(str.begin(), str.end(), [](unsigned char input)
{
return !std::isdigit(input);
}) == str.end();
}
// Trim from start
@ -182,10 +182,10 @@ namespace Utils
int minutes = minutesTotal % 60;
int hoursTotal = minutesTotal / 60;
return Utils::String::VA("%02d:%02d:%02d", hoursTotal, minutes, seconds);
return VA("%02d:%02d:%02d", hoursTotal, minutes, seconds);
}
std::string FormatBandwidth(size_t bytes, int milliseconds)
std::string FormatBandwidth(std::size_t bytes, int milliseconds)
{
static const char* sizes[] =
{
@ -193,20 +193,20 @@ namespace Utils
"KB",
"MB",
"GB",
"TB"
"TB",
};
if (!milliseconds) return "0.00 B/s";
double bytesPerSecond = (1000.0 / milliseconds) * bytes;
int i;
std::size_t i;
for (i = 0; bytesPerSecond > 1000 && i < ARRAYSIZE(sizes); ++i) // 1024 or 1000?
{
bytesPerSecond /= 1000;
}
return Utils::String::VA("%.2f %s/s", static_cast<float>(bytesPerSecond), sizes[i]);
return VA("%.2f %s/s", static_cast<float>(bytesPerSecond), sizes[i]);
}
#ifdef ENABLE_BASE64
@ -246,4 +246,3 @@ namespace Utils
}
#endif
}
}

View File

@ -1,8 +1,6 @@
#pragma once
namespace Utils
{
namespace String
namespace Utils::String
{
template <std::size_t Buffers, std::size_t MinBufferSize>
class VAProvider
@ -39,7 +37,7 @@ namespace Utils
class Entry
{
public:
Entry(size_t _size = MinBufferSize) : size(_size), buffer(nullptr)
Entry(std::size_t _size = MinBufferSize) : size(_size), buffer(nullptr)
{
if (this->size < MinBufferSize) this->size = MinBufferSize;
this->allocate();
@ -64,11 +62,11 @@ namespace Utils
this->allocate();
}
size_t size;
std::size_t size;
char* buffer;
};
size_t currentBuffer;
std::size_t currentBuffer;
Entry stringPool[Buffers];
};
@ -91,7 +89,7 @@ namespace Utils
std::wstring Convert(const std::string& str);
std::string FormatTimeSpan(int milliseconds);
std::string FormatBandwidth(size_t bytes, int milliseconds);
std::string FormatBandwidth(std::size_t bytes, int milliseconds);
std::string DumpHex(const std::string& data, const std::string& separator = " ");
@ -102,4 +100,3 @@ namespace Utils
std::string EncodeBase128(const std::string& input);
}
}

View File

@ -1,15 +1,13 @@
#include <STDInclude.hpp>
namespace Utils
{
namespace Time
namespace Utils::Time
{
void Interval::update()
{
this->lastPoint = std::chrono::high_resolution_clock::now();
}
bool Interval::elapsed(std::chrono::nanoseconds nsecs)
bool Interval::elapsed(std::chrono::nanoseconds nsecs) const
{
return ((std::chrono::high_resolution_clock::now() - this->lastPoint) >= nsecs);
}
@ -24,19 +22,18 @@ namespace Utils
this->lastPoint = Game::Sys_Milliseconds();
}
int Point::diff(Point point)
int Point::diff(Point point) const
{
return point.lastPoint - this->lastPoint;
}
bool Point::after(Point point)
bool Point::after(Point point) const
{
return this->diff(point) < 0;
}
bool Point::elapsed(int milliseconds)
bool Point::elapsed(int milliseconds) const
{
return (Game::Sys_Milliseconds() - this->lastPoint) >= milliseconds;
}
}
}

View File

@ -1,8 +1,6 @@
#pragma once
namespace Utils
{
namespace Time
namespace Utils::Time
{
class Interval
{
@ -13,7 +11,7 @@ namespace Utils
Interval() : lastPoint(std::chrono::high_resolution_clock::now()) {}
void update();
bool elapsed(std::chrono::nanoseconds nsecs);
bool elapsed(std::chrono::nanoseconds nsecs) const;
};
class Point
@ -22,12 +20,11 @@ namespace Utils
Point();
void update();
int diff(Point point);
bool after(Point point);
bool elapsed(int milliseconds);
int diff(Point point) const;
bool after(Point point) const;
bool elapsed(int milliseconds) const;
private:
int lastPoint;
};
}
}

View File

@ -72,7 +72,7 @@ namespace Utils
return 0;
}
size_t GetModuleSize(HMODULE module)
std::size_t GetModuleSize(HMODULE module)
{
PIMAGE_DOS_HEADER header = PIMAGE_DOS_HEADER(module);
PIMAGE_NT_HEADERS ntHeader = PIMAGE_NT_HEADERS(DWORD(module) + header->e_lfanew);
@ -158,7 +158,7 @@ namespace Utils
SafeShellExecute(nullptr, "open", url.data(), nullptr, nullptr, SW_SHOWNORMAL);
}
bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2)
bool HasIntersection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2)
{
return !(base1 + len1 <= base2 || base2 + len2 <= base1);
}

View File

@ -15,7 +15,7 @@ namespace Utils
bool IsWineEnvironment();
unsigned long GetParentProcessId();
size_t GetModuleSize(HMODULE);
std::size_t GetModuleSize(HMODULE);
void* GetThreadStartAddress(HANDLE hThread);
HMODULE GetNTDLL();
@ -23,7 +23,7 @@ namespace Utils
void OpenUrl(const std::string& url);
bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2);
bool HasIntersection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2);
template <typename T> void RotLeft(T& object, size_t bits)
{

View File

@ -10,7 +10,7 @@ namespace Utils
this->setURL(url);
}
WebIO::WebIO(const std::string& useragent) : cancel(false), timeout(5000), hSession(nullptr) // 5 seconds timeout by default
WebIO::WebIO(const std::string& useragent) : cancel(false), hSession(nullptr), timeout(5000) // 5 seconds timeout by default
{
this->openSession(useragent);
}
@ -91,7 +91,7 @@ namespace Utils
server = server.substr(2);
}
size_t pos = server.find("/");
size_t pos = server.find('/');
if (pos == std::string::npos)
{
this->url.server = server;
@ -105,7 +105,7 @@ namespace Utils
this->url.port.clear();
pos = this->url.server.find(":");
pos = this->url.server.find(':');
if (pos != std::string::npos)
{
this->url.port = this->url.server.substr(pos + 1);
@ -128,7 +128,7 @@ namespace Utils
this->isFTP = (this->url.protocol == "ftp");
}
std::string WebIO::buildPostBody(WebIO::Params params)
std::string WebIO::buildPostBody(Params params)
{
std::string body;
@ -157,15 +157,15 @@ namespace Utils
std::string WebIO::postFile(const std::string& data, std::string fieldName, std::string fileName)
{
WebIO::Params headers;
Params headers;
std::string boundary = "----WebKitFormBoundaryHoLVocRsBxs71fU6";
headers["Content-Type"] = "multipart/form-data, boundary=" + boundary;
Utils::String::Replace(fieldName, "\"", "\\\"");
Utils::String::Replace(fieldName, "\\", "\\\\");
Utils::String::Replace(fileName, "\"", "\\\"");
Utils::String::Replace(fileName, "\\", "\\\\");
String::Replace(fieldName, "\"", "\\\"");
String::Replace(fieldName, "\\", "\\\\");
String::Replace(fileName, "\"", "\\\"");
String::Replace(fileName, "\\", "\\\\");
std::string body = "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name=\"";
@ -177,7 +177,7 @@ namespace Utils
body += data + "\r\n";
body += "--" + boundary + "--\r\n";
headers["Content-Length"] = Utils::String::VA("%u", body.size());
headers["Content-Length"] = String::VA("%u", body.size());
return this->execute("POST", body, headers);
}
@ -188,13 +188,13 @@ namespace Utils
return this->post(body, success);
}
std::string WebIO::post(const std::string& _url, WebIO::Params params, bool* success)
std::string WebIO::post(const std::string& _url, Params params, bool* success)
{
this->setURL(_url);
return this->post(params, success);
}
std::string WebIO::post(WebIO::Params params, bool* success)
std::string WebIO::post(Params params, bool* success)
{
return this->post(this->buildPostBody(params), success);
}
@ -259,7 +259,7 @@ namespace Utils
std::string WebIO::execute(const char* command, const std::string& body, WebIO::Params headers, bool* success)
{
if (success) *success = false;
if (!this->openConnection()) return "";
if (!this->openConnection()) return {};
const char *acceptTypes[] = { "application/x-www-form-urlencoded", nullptr };
@ -276,12 +276,12 @@ namespace Utils
if (!this->hFile || this->hFile == INVALID_HANDLE_VALUE)
{
this->closeConnection();
return "";
return {};
}
if (headers.find("Content-Type") == headers.end())
if (const auto itr = headers.find("Content-Type"); itr == headers.end())
{
headers["Content-Type"] = "application/x-www-form-urlencoded";
itr->second = "application/x-www-form-urlencoded";
}
std::string finalHeaders;
@ -296,20 +296,20 @@ namespace Utils
if (HttpSendRequestA(this->hFile, finalHeaders.data(), finalHeaders.size(), const_cast<char*>(body.data()), body.size() + 1) == FALSE)
{
return "";
return {};
}
DWORD statusCode = 404;
DWORD length = sizeof(statusCode);
if (HttpQueryInfo(this->hFile, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &statusCode, &length, nullptr) == FALSE || (statusCode != 200 && statusCode != 201))
if (HttpQueryInfoA(this->hFile, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &statusCode, &length, nullptr) == FALSE || (statusCode != 200 && statusCode != 201))
{
this->closeConnection();
return "";
return {};
}
DWORD contentLength = 0;
length = sizeof(statusCode);
if (HttpQueryInfo(this->hFile, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &contentLength, &length, nullptr) == FALSE)
if (HttpQueryInfoA(this->hFile, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &contentLength, &length, nullptr) == FALSE)
{
contentLength = 0;
}
@ -317,15 +317,15 @@ namespace Utils
std::string returnBuffer;
returnBuffer.reserve(contentLength);
DWORD size = 0;
char buffer[0x2001] = { 0 };
DWORD size{};
char buffer[0x2001]{};
while (InternetReadFile(this->hFile, buffer, 0x2000, &size))
while (InternetReadFile(this->hFile, buffer, sizeof(buffer) - 1, &size))
{
if (this->cancel)
{
this->closeConnection();
return "";
return {};
}
returnBuffer.append(buffer, size);
@ -339,9 +339,9 @@ namespace Utils
return returnBuffer;
}
bool WebIO::isSecuredConnection()
bool WebIO::isSecuredConnection() const
{
return (this->url.protocol == "https");
return this->url.protocol == "https"s;
}
bool WebIO::connect()
@ -368,7 +368,7 @@ namespace Utils
this->formatPath(directory, true);
this->formatPath(currentDir, true);
char path[MAX_PATH] = { 0 };
char path[MAX_PATH]{};
PathCombineA(path, currentDir.data(), directory.data());
std::string newPath(path);
@ -384,8 +384,8 @@ namespace Utils
{
directory.clear();
DWORD size = MAX_PATH;
char currentDir[MAX_PATH] = { 0 };
char currentDir[MAX_PATH]{};
DWORD size = sizeof(currentDir);
if (FtpGetCurrentDirectoryA(this->hConnect, currentDir, &size) == TRUE)
{
@ -398,7 +398,7 @@ namespace Utils
void WebIO::formatPath(std::string& path, bool win)
{
size_t nPos;
std::size_t nPos;
std::string find = "\\";
std::string replace = "/";
@ -451,7 +451,7 @@ namespace Utils
WIN32_FIND_DATAA findFileData;
bool result = false;
DWORD dwAttribute = (files ? FILE_ATTRIBUTE_NORMAL : FILE_ATTRIBUTE_DIRECTORY);
const DWORD dwAttribute = (files ? FILE_ATTRIBUTE_NORMAL : FILE_ATTRIBUTE_DIRECTORY);
// Any filename.
std::string tempDir;
@ -469,8 +469,7 @@ namespace Utils
if (findFileData.dwFileAttributes == dwAttribute) // No bitwise flag check, as it might return archives/offline/hidden or other files/dirs
{
//printf("%s: %X\n", findFileData.cFileName, findFileData.dwFileAttributes);
list.push_back(findFileData.cFileName);
list.emplace_back(findFileData.cFileName);
result = true;
}
} while (InternetFindNextFileA(this->hFile, &findFileData));
@ -556,7 +555,7 @@ namespace Utils
return false;
}
void WebIO::setProgressCallback(Utils::Slot<void(size_t, size_t)> callback)
void WebIO::setProgressCallback(const Slot<void(std::size_t, std::size_t)>& callback)
{
this->progressCallback = callback;
}

View File

@ -30,9 +30,9 @@ namespace Utils
std::string postFile(const std::string& url, const std::string& data, const std::string& fieldName, const std::string& fileName);
std::string postFile(const std::string& data, std::string fieldName, std::string fileName);
std::string post(const std::string& url, WebIO::Params params, bool* success = nullptr);
std::string post(const std::string& url, Params params, bool* success = nullptr);
std::string post(const std::string& url, const std::string& body, bool* success = nullptr);
std::string post(WebIO::Params params, bool* success = nullptr);
std::string post(Params params, bool* success = nullptr);
std::string post(const std::string& body, bool* success = nullptr);
std::string get(const std::string& url, bool* success = nullptr);
@ -62,7 +62,7 @@ namespace Utils
bool uploadFileData(const std::string& file,const std::string& data);
bool downloadFileData(const std::string& file, std::string& data);
void setProgressCallback(Utils::Slot<void(size_t, size_t)> callback);
void setProgressCallback(const Slot<void(std::size_t, std::size_t)>& callback);
void cancelDownload() { this->cancel = true; }
private:
@ -96,13 +96,13 @@ namespace Utils
DWORD timeout;
Utils::Slot<void(size_t, size_t)> progressCallback;
Slot<void(size_t, size_t)> progressCallback;
std::string buildPostBody(WebIO::Params params);
static std::string buildPostBody(Params params);
bool isSecuredConnection();
bool isSecuredConnection() const;
std::string execute(const char* command, const std::string& body, WebIO::Params headers = WebIO::Params(), bool* success = nullptr);
std::string execute(const char* command, const std::string& body, Params headers = {}, bool* success = nullptr);
bool listElements(const std::string& directory, std::vector<std::string>& list, bool files);
@ -112,6 +112,6 @@ namespace Utils
bool openConnection();
void closeConnection();
void formatPath(std::string& path, bool win); /* if (win == true): / -> \\ */
static void formatPath(std::string& path, bool win); /* if (win == true): / -> \\ */
};
}