diff --git a/common.h b/common.h index 4201116..74e0d70 100644 --- a/common.h +++ b/common.h @@ -1,3 +1,5 @@ +#pragma once + #include #include #include diff --git a/files.h b/files.h index d86e927..74e6d30 100644 --- a/files.h +++ b/files.h @@ -1,3 +1,5 @@ +#pragma once + #include #include diff --git a/handles.h b/handles.h index f751845..af96c3f 100644 --- a/handles.h +++ b/handles.h @@ -1,4 +1,6 @@ -#include +#pragma once + +#include namespace handles { enum Type { diff --git a/processes.h b/processes.h index 4bcad09..0e6ecca 100644 --- a/processes.h +++ b/processes.h @@ -1,3 +1,5 @@ +#pragma once + #include #include @@ -9,4 +11,4 @@ namespace processes { void *allocProcessHandle(pid_t pid); Process* processFromHandle(void* hHandle, bool pop); -} \ No newline at end of file +} diff --git a/resources.h b/resources.h index 5612743..3296e78 100644 --- a/resources.h +++ b/resources.h @@ -1,5 +1,6 @@ #pragma once +#include namespace wibo { @@ -11,4 +12,4 @@ bool resourceEntryBelongsToExecutable(const Executable &exe, const ImageResource ResourceIdentifier resourceIdentifierFromAnsi(const char *id); ResourceIdentifier resourceIdentifierFromWide(const uint16_t *id); -} +} // namespace wibo diff --git a/strutil.cpp b/strutil.cpp index 24b00ff..16f1738 100644 --- a/strutil.cpp +++ b/strutil.cpp @@ -1,208 +1,225 @@ +#include "strutil.h" #include "common.h" -#include "strings.h" #include -#include -#include -#include -#include #include +#include +#include size_t wstrlen(const uint16_t *str) { - if(!str) return 0; - + if (!str) + return 0; + size_t len = 0; while (str[len] != 0) ++len; return len; } -size_t wstrnlen(const uint16_t* str, size_t numberOfElements){ +size_t wstrnlen(const uint16_t *str, size_t numberOfElements) { + if (!str) + return 0; size_t len = 0; - while (str[len] != 0 && len < numberOfElements) + while (len < numberOfElements && str[len] != 0) ++len; - return len; + return len; } -int wstrncmp(const uint16_t *string1, const uint16_t *string2, size_t count){ - const uint16_t* ptr1 = string1; - const uint16_t* ptr2 = string2; - for(size_t i = 0; i < count; i++){ +int wstrncmp(const uint16_t *string1, const uint16_t *string2, size_t count) { + const uint16_t *ptr1 = string1; + const uint16_t *ptr2 = string2; + for (size_t i = 0; i < count; i++) { uint16_t c1 = *ptr1++; uint16_t c2 = *ptr2++; if (c1 != c2) { - return (c1 > c2) ? 1 : -1; - } + return (c1 > c2) ? 1 : -1; + } } return 0; } -const uint16_t* wstrstr(const uint16_t *dest, const uint16_t *src){ - if (!*src) return dest; +const uint16_t *wstrstr(const uint16_t *dest, const uint16_t *src) { + if (!*src) + return dest; - for (; *dest != 0; dest++) { - const uint16_t* d = dest; - const uint16_t* s = src; + for (; *dest != 0; dest++) { + const uint16_t *d = dest; + const uint16_t *s = src; - while (*d != 0 && *s != 0 && *d == *s) { - d++; - s++; - } + while (*d != 0 && *s != 0 && *d == *s) { + d++; + s++; + } - if (*s == 0) { - return dest; - } - } + if (*s == 0) { + return dest; + } + } - return nullptr; + return nullptr; } -uint16_t* wstrchr(const uint16_t* str, uint16_t c) { - for (; *str != 0; str++) { - if (*str == c) { - return (uint16_t*)str; - } - } - // If searching for '\0', return pointer to terminator - if (c == 0) { - return (uint16_t*)str; - } - return nullptr; +uint16_t *wstrchr(const uint16_t *str, uint16_t c) { + if (!str) + return nullptr; + for (; *str != 0; str++) { + if (*str == c) { + return (uint16_t *)str; + } + } + // If searching for '\0', return pointer to terminator + if (c == 0) { + return (uint16_t *)str; + } + return nullptr; } -uint16_t* wstrrchr(const uint16_t* str, uint16_t c){ - const uint16_t* last = nullptr; - for (; *str != 0; str++) { - if (*str == c) { - last = str; - } - } - return (uint16_t*)last; +uint16_t *wstrrchr(const uint16_t *str, uint16_t c) { + if (!str) + return nullptr; + const uint16_t *last = nullptr; + const uint16_t *it = str; + for (; *it != 0; ++it) { + if (*it == c) { + last = it; + } + } + if (c == 0) + return (uint16_t *)it; + return (uint16_t *)last; } -uint16_t* wstrcat(uint16_t* dest, const uint16_t* src){ - uint16_t* d = dest; - while (*d) d++; - while ((*d++ = *src++) != 0); +uint16_t *wstrcat(uint16_t *dest, const uint16_t *src) { + uint16_t *d = dest; + while (*d) + d++; + while ((*d++ = *src++) != 0) + ; return dest; } -uint16_t* wstrncat(uint16_t* dest, const uint16_t* src, size_t count){ - uint16_t* d = dest; - while (*d) d++; - for(size_t i = 0; i < count && src[i] != 0; i++){ +uint16_t *wstrncat(uint16_t *dest, const uint16_t *src, size_t count) { + uint16_t *d = dest; + while (*d) + d++; + for (size_t i = 0; i < count && src[i] != 0; i++) { *d++ = src[i]; } *d = 0; - return dest; + return dest; } -uint16_t* wstrcpy(uint16_t* dest, const uint16_t* src){ - uint16_t* d = dest; - while ((*d++ = *src++) != 0); - return dest; +uint16_t *wstrcpy(uint16_t *dest, const uint16_t *src) { + uint16_t *d = dest; + while ((*d++ = *src++) != 0) + ; + return dest; } size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n) { + if (!dst || !src || n == 0) + return 0; size_t i = 0; - while (i < n && src[i] != 0) { + for (; i < n && src[i] != 0; ++i) { dst[i] = src[i]; - ++i; } - if (i < n) - dst[i] = 0; + for (size_t j = i; j < n; ++j) { + dst[j] = 0; + } return i; } -std::string wideStringToString(const uint16_t *src, int len = -1) { - if(!src) return std::string(); - if (len < 0) { - len = src ? wstrlen(src) : 0; +std::string wideStringToString(const uint16_t *src, int len) { + if (!src) + return {}; + + size_t count; + if (len >= 0) { + count = static_cast(len); + } else { + count = wstrlen(src); } - // std::u16string u16str; - // for(const uint16_t* p = src; *p != 0; p++){ - // u16str.push_back(*p); - // } +#ifndef NDEBUG + std::stringstream hexDump; + hexDump << std::hex; + bool sawWide = false; +#endif - // std::wstring_convert, char16_t> convert; - // return convert.to_bytes(reinterpret_cast(u16str.data())); + std::string result(count, '\0'); + for (size_t i = 0; i < count; ++i) { + uint16_t value = src[i]; + if (i > 0) + hexDump << ' '; + hexDump << "0x" << value; + if (value > 0xFF) + sawWide = true; + result[i] = static_cast(value & 0xFF); + } - // the old implementation - std::string res(len, '\0'); - std::string debug_wstr; - std::stringstream ss; - bool is_wide = false; - for (int i = 0; i < len; i++) { - ss << "0x" << std::hex << src[i] << " "; - // debug_wstr += std::format("0x%X ", src[i]); - if(src[i] > 255){ - // DEBUG_LOG("Encountered wide char with value 0x%X!\n", src[i]); - // assert(src[i] <= 255); - is_wide = true; - } - res[i] = src[i] & 0xFF; +#ifndef NDEBUG + if (sawWide) { + size_t loggedLength = (len >= 0) ? count : wstrlen(src); + DEBUG_LOG("wideString (%zu): %s\n", loggedLength, hexDump.str().c_str()); } - if(is_wide){ - debug_wstr += ss.str(); - DEBUG_LOG("wideString (%d): %s\n", wstrlen(src), debug_wstr.c_str()); - } - return res; +#endif + + return result; } std::vector stringToWideString(const char *src) { - int len = strlen(src); + if (!src) + return std::vector{0}; + size_t len = strlen(src); std::vector res(len + 1); - for (size_t i = 0; i < res.size(); i++) { - res[i] = src[i] & 0xFF; + res[i] = static_cast(src[i] & 0xFF); } res[len] = 0; // NUL terminate - return res; } -long wstrtol(const uint16_t* string, uint16_t** end_ptr, int base){ - if(!string){ - if(end_ptr) *end_ptr = nullptr; +long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base) { + if (!string) { + if (end_ptr) + *end_ptr = nullptr; return 0; } std::string normal_str = wideStringToString(string); - char* normal_end = nullptr; + char *normal_end = nullptr; long res = std::strtol(normal_str.c_str(), &normal_end, base); - if(end_ptr){ - if(normal_end && *normal_end){ + if (end_ptr) { + if (normal_end && *normal_end) { size_t offset = normal_end - normal_str.c_str(); - *end_ptr = (uint16_t*)(string + offset); - } - else { - *end_ptr = (uint16_t*)(string + normal_str.size()); + *end_ptr = (uint16_t *)(string + offset); + } else { + *end_ptr = (uint16_t *)(string + normal_str.size()); } } return res; } -unsigned long wstrtoul(const uint16_t* string, uint16_t** end_ptr, int base){ - if(!string){ - if(end_ptr) *end_ptr = nullptr; +unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base) { + if (!string) { + if (end_ptr) + *end_ptr = nullptr; return 0; } std::string normal_str = wideStringToString(string); - char* normal_end = nullptr; + char *normal_end = nullptr; unsigned long res = std::strtoul(normal_str.c_str(), &normal_end, base); - if(end_ptr){ - if(normal_end && *normal_end){ + if (end_ptr) { + if (normal_end && *normal_end) { size_t offset = normal_end - normal_str.c_str(); - *end_ptr = (uint16_t*)(string + offset); - } - else { - *end_ptr = (uint16_t*)(string + normal_str.size()); + *end_ptr = (uint16_t *)(string + offset); + } else { + *end_ptr = (uint16_t *)(string + normal_str.size()); } } return res; -} \ No newline at end of file +} diff --git a/strutil.h b/strutil.h index 4aa4728..897ae4a 100644 --- a/strutil.h +++ b/strutil.h @@ -1,17 +1,20 @@ +#pragma once + +#include #include #include size_t wstrlen(const uint16_t *str); -size_t wstrnlen(const uint16_t* str, size_t numberOfElements); +size_t wstrnlen(const uint16_t *str, size_t numberOfElements); int wstrncmp(const uint16_t *string1, const uint16_t *string2, size_t count); -const uint16_t* wstrstr(const uint16_t *dest, const uint16_t *src); -uint16_t* wstrchr(const uint16_t* str, uint16_t c); -uint16_t* wstrrchr(const uint16_t* str, uint16_t c); -uint16_t* wstrcat(uint16_t* dest, const uint16_t* src); -uint16_t* wstrncat(uint16_t* dest, const uint16_t* src, size_t count); -uint16_t* wstrcpy(uint16_t* dest, const uint16_t* src); +const uint16_t *wstrstr(const uint16_t *dest, const uint16_t *src); +uint16_t *wstrchr(const uint16_t *str, uint16_t c); +uint16_t *wstrrchr(const uint16_t *str, uint16_t c); +uint16_t *wstrcat(uint16_t *dest, const uint16_t *src); +uint16_t *wstrncat(uint16_t *dest, const uint16_t *src, size_t count); +uint16_t *wstrcpy(uint16_t *dest, const uint16_t *src); size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n); std::string wideStringToString(const uint16_t *src, int len = -1); std::vector stringToWideString(const char *src); -long wstrtol(const uint16_t* string, uint16_t** end_ptr, int base); -unsigned long wstrtoul(const uint16_t* strSource, uint16_t** end_ptr, int base); \ No newline at end of file +long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base); +unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base);