Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions Syscaller/crt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#ifndef SYSCALL_CRT_HPP
#define SYSCALL_CRT_HPP

#include <cstdint>
#include <cwchar>

namespace syscall::crt
{
template<typename T, size_t N>
[[nodiscard]] constexpr size_t getCountOf(T(&)[N]) noexcept
{
return N;
}

namespace string
{
constexpr char toLower(char c) noexcept
{
return (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
}
constexpr wchar_t toLower(wchar_t c) noexcept
{
return (c >= L'A' && c <= L'Z') ? (c + (L'a' - L'A')) : c;
}

constexpr size_t getLength(const char* szStr) noexcept
{
const char* s = szStr;
while (*s)
++s;

return s - szStr;
}

constexpr size_t getLength(const wchar_t* wzStr) noexcept
{
const wchar_t* s = wzStr;
while (*s)
++s;

return s - wzStr;
}

[[nodiscard]] constexpr int compare(const char* szFirst, const char* szSecond) noexcept
{
while (*szFirst && (*szFirst == *szSecond))
{
szFirst++;
szSecond++;
}

return *(const unsigned char*)szFirst - *(const unsigned char*)szSecond;
}

[[nodiscard]] constexpr int compareIgnoreCase(const wchar_t* szFirst, const wchar_t* szSecond) noexcept
{
wchar_t c1, c2;
do {
c1 = toLower(*szFirst++);
c2 = toLower(*szSecond++);

if (c1 == L'\0')
return c1 - c2;
} while (c1 == c2);

return c1 - c2;
}

[[nodiscard]] constexpr const char* findChar(const char* str, int character) noexcept
{
while (*str != '\0')
{
if (*str == static_cast<char>(character))
return str;

str++;
}
return nullptr;
}

[[nodiscard]] inline char* findChar(char* str, int character) noexcept
{
return const_cast<char*>(findChar(static_cast<const char*>(str), character));
}

inline void copy(char* szDest, size_t uDestLength, const char* src) noexcept
{
if (!szDest || !uDestLength)
return;

const size_t uSourceLength = getLength(src);

const size_t uCount = (uSourceLength < uDestLength) ? uSourceLength : (uDestLength - 1);
std::copy_n(src, uCount, szDest);
szDest[uCount] = '\0';
}

inline void concat(wchar_t* pDest, size_t uSizeInElements, const wchar_t* pSource) noexcept
{
if (!pDest || !uSizeInElements)
return;

const size_t uDestLength = getLength(pDest);
if (uDestLength >= uSizeInElements - 1)
return;


const size_t uSourceLength = getLength(pSource);
const size_t uRemainingSpace = uSizeInElements - uDestLength - 1;
const size_t uCount = (uSourceLength < uRemainingSpace) ? uSourceLength : uRemainingSpace;

std::copy_n(pSource, uCount * sizeof(wchar_t), pDest + uDestLength);
pDest[uDestLength + uCount] = L'\0';
}

inline void mbToWcs(wchar_t* pDest, size_t uSizeInElements, const char* pSource) noexcept
{
if (!pDest || !uSizeInElements)
return;

const size_t uSourceLength = getLength(pSource);
const size_t uCount = (uSourceLength < uSizeInElements) ? uSourceLength : (uSizeInElements - 1);
for (size_t i = 0; i < uCount; ++i)
pDest[i] = static_cast<wchar_t>(static_cast<unsigned char>(pSource[i]));

pDest[uCount] = L'\0';
}
}
}

#endif
86 changes: 86 additions & 0 deletions Syscaller/hash.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef HASHING_HPP
#define HASHING_HPP

#include <cstdint>
#include <string>
#include <bit>

namespace syscall::hashing
{
using Hash_t = uint64_t;

constexpr Hash_t getCompileTimeSeed()
{
Hash_t seed = 0;
const char* szCurrentTime = __TIME__;
const char* szCurrentDate = __DATE__;

for (int i = 0; szCurrentTime[i] != '\0'; ++i)
seed = std::rotr(seed, 3) + szCurrentTime[i];

for (int i = 0; szCurrentDate[i] != '\0'; ++i)
seed = std::rotr(seed, 5) + szCurrentDate[i];

return seed;
}

constexpr Hash_t currentSeed = getCompileTimeSeed();
constexpr Hash_t polyKey1 = 0xAF6F01BD5B2D7583ULL ^ currentSeed;
constexpr Hash_t polyKey2 = 0xB4F281729182741DULL ^ std::rotr(currentSeed, 7);

consteval Hash_t calculateHash(const char* szData)
{
Hash_t hash = polyKey1;
while (*szData)
{
hash ^= static_cast<Hash_t>(*szData++);
hash += std::rotr(hash, 11) + polyKey2;
}
return hash;
}

consteval Hash_t calculateHash(const char* szData, size_t uLength)
{
Hash_t hash = polyKey1;
for (size_t i = 0; i < uLength && szData[i]; ++i)
{
hash ^= static_cast<Hash_t>(szData[i]);
hash += std::rotr(hash, 11) + polyKey2;
}
return hash;
}


inline Hash_t calculateHashRuntime(const char* szData)
{
Hash_t hash = polyKey1;
while (*szData)
{
hash ^= static_cast<Hash_t>(*szData++);
hash += std::rotr(hash, 11) + polyKey2;
}
return hash;
}

inline Hash_t calculateHashRuntime(const char* szData, size_t uLength)
{
Hash_t hash = polyKey1;
for (size_t i = 0; i < uLength && szData[i]; ++i)
{
hash ^= static_cast<Hash_t>(szData[i]);
hash += std::rotr(hash, 11) + polyKey2;
}
return hash;
}
}

#ifdef SYSCALLS_NO_HASH
#define SYSCALL_ID(str) (str)
#define SYSCALL_ID_RT(str) (str)
#else
#define SYSCALL_ID(str) (syscall::hashing::calculateHash(str))
#define SYSCALL_ID_RT(str) (syscall::hashing::calculateHashRuntime(str))

#endif

#endif
Loading