diff --git a/CMakeLists.txt b/CMakeLists.txt index ac2024a..1a0c4ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,12 +19,14 @@ add_executable(elfloader3 src/stubs.cpp src/utils.cpp src/SieFs.cpp + src/charset.cpp # SWI src/swi.cpp src/swi/libc.cpp src/swi/fs.cpp src/swi/time.cpp + src/swi/ws.cpp # vova7890 & Ganster elfloader src/elfloader/debug.c diff --git a/src/SieFs.cpp b/src/SieFs.cpp index 939c073..c48f33e 100644 --- a/src/SieFs.cpp +++ b/src/SieFs.cpp @@ -30,7 +30,7 @@ std::string SieFs::path2sie(const std::string &unix_path) { } std::replace(siemens_path.begin(), siemens_path.end(), '/', '\\'); - LOGD("path2sie: %s -> %s\n", unix_path.c_str(), siemens_path.c_str()); + // LOGD("path2sie: %s -> %s\n", unix_path.c_str(), siemens_path.c_str()); if (!siemens_path.size()) { LOGE("Invalid unix path: %s\n", unix_path.c_str()); @@ -52,7 +52,7 @@ std::string SieFs::sie2path(const std::string &siemens_path) { if (m_drives.find(drive) != m_drives.end()) { std::string unix_path = m_drives[drive] + "/" + path; - fprintf(stderr, "sie2path: %s -> %s\n", siemens_path.c_str(), unix_path.c_str()); + // LOGD("sie2path: %s -> %s\n", siemens_path.c_str(), unix_path.c_str()); return unix_path; } } diff --git a/src/charset.cpp b/src/charset.cpp new file mode 100644 index 0000000..5d2d0ea --- /dev/null +++ b/src/charset.cpp @@ -0,0 +1,208 @@ +#include "charset.h" + +#include + +static constexpr uint16_t cp1251_to_utf16_table[] = { + 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, + 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, + 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, + 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, + 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, + 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, + 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, + 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, + 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, + 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, + 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, + 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, + 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, + 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, + 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, + 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F, + 0x0402, 0x0403, 0x201A, 0x0453, 0x201E, 0x2026, 0x2020, 0x2021, + 0x20AC, 0x2030, 0x0409, 0x2039, 0x040A, 0x040C, 0x040B, 0x040F, + 0x0452, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, + 0x0020, 0x2122, 0x0459, 0x203A, 0x045A, 0x045C, 0x045B, 0x045F, + 0x00A0, 0x040E, 0x045E, 0x0408, 0x00A4, 0x0490, 0x00A6, 0x00A7, + 0x0401, 0x00A9, 0x0404, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x0407, + 0x00B0, 0x00B1, 0x0406, 0x0456, 0x0491, 0x00B5, 0x00B6, 0x00B7, + 0x0451, 0x2116, 0x0454, 0x00BB, 0x0458, 0x0405, 0x0455, 0x0457, + 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, + 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F, + 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, + 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F, + 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, + 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F, + 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, + 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F, +}; + +static size_t _codepoint_len(uint32_t codepoint) { + assert(codepoint <= 0x1FFFFF); + + if (codepoint <= 0x7F) { + return 1; + } else if (codepoint <= 0x7FF) { + return 2; + } else if (codepoint <= 0xFFFF) { + return 3; + } else if (codepoint <= 0x1FFFFF) { + return 4; + } + return 0; +} + +static size_t _encode_codepoint(char *tmp, uint32_t codepoint) { + assert(codepoint <= 0x1FFFFF); + + if (codepoint <= 0x7F) { + // 0xxxxxxx + tmp[0] = codepoint; + return 1; + } else if (codepoint <= 0x7FF) { + // 110xxxxx 10xxxxxx + tmp[0] = ((codepoint >> 6) & 0x1F) | 0xC0; + tmp[1] = (codepoint & 0x3F) | 0x80; + return 2; + } else if (codepoint <= 0xFFFF) { + // 1110xxxx 10xxxxxx 10xxxxxx + tmp[0] = ((codepoint >> 12) & 0xF) | 0xE0; + tmp[1] = ((codepoint >> 6) & 0x3F) | 0x80; + tmp[2] = (codepoint & 0x3F) | 0x80; + return 3; + } else if (codepoint <= 0x1FFFFF) { + // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + tmp[0] = ((codepoint >> 18) & 0x7) | 0xF0; + tmp[1] = ((codepoint >> 12) & 0x3F) | 0x80; + tmp[2] = ((codepoint >> 6) & 0x3F) | 0x80; + tmp[3] = (codepoint & 0x3F) | 0x80; + return 4; + } + return 0; +} + +static bool _check_utf8_seq(const char *buffer, size_t len) { + for (size_t i = 1; i < len; i++) { + if ((buffer[i] & 0xC0) != 0x80) + return false; + } + return true; +} + +static size_t _decode_utf8(const char *buffer, size_t max_len, uint32_t *codepoint) { + if ((buffer[0] & 0xF8) == 0xF0 && max_len >= 4 && _check_utf8_seq(buffer, 4)) { + // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx + *codepoint = (buffer[0] & 0x7) << 18; + *codepoint |= (buffer[1] & 0x3F) << 12; + *codepoint |= (buffer[2] & 0x3F) << 6; + *codepoint |= (buffer[3] & 0x3F); + return 4; + } else if ((buffer[0] & 0xF0) == 0xE0 && max_len >= 3 && _check_utf8_seq(buffer, 3)) { + // 1110xxxx 10xxxxxx 10xxxxxx + *codepoint = (buffer[0] & 0xF) << 12; + *codepoint |= (buffer[1] & 0x3F) << 6; + *codepoint |= (buffer[2] & 0x3F); + return 3; + } else if ((buffer[0] & 0xE0) == 0xC0 && max_len >= 2 && _check_utf8_seq(buffer, 2)) { + // 110xxxxx 10xxxxxx + *codepoint = (buffer[0] & 0x1F) << 6; + *codepoint |= (buffer[1] & 0x3F); + return 2; + } else if ((buffer[0] & 0x80) == 0) { + // 0xxxxxxx + *codepoint = buffer[0]; + return 1; + } + *codepoint = 0xFFFD; + return 1; +} + +static size_t _decode_utf16(const uint16_t *buffer, size_t max_len, uint32_t *codepoint) { + if ((buffer[0] & 0xFC00) == 0xD800 && max_len >= 2 && (buffer[1] & 0xFC00) == 0xDC00) { + *codepoint = (buffer[0] & 0x3FF) << 10; + *codepoint |= (buffer[1] & 0x3FF); + *codepoint += 0x10000; + return 2; + } else if ((buffer[0] & 0xFC00) == 0xD800 || (buffer[0] & 0xFC00) == 0xDC00) { + *codepoint = 0xFFFD; + } else { + *codepoint = buffer[0]; + } + return 1; +} + +size_t utf8_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size) { + assert(buffer_size > 0); + size_t new_len = 0; + for (size_t i = 0; i < input.size(); ) { + uint32_t codepoint = 0; + i += _decode_utf8(&input[i], input.size() - i, &codepoint); + if ((codepoint >= 0x0000 && codepoint <= 0xD7FF) || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { + if ((new_len + 1) > (buffer_size - 1)) + break; + buffer[new_len++] = codepoint; + } else { + if ((new_len + 2) > (buffer_size - 1)) + break; + uint32_t double_codepoint = codepoint - 0x10000; + buffer[new_len++] = ((double_codepoint >> 10) & 0x3FF) | 0xD800; + buffer[new_len++] = (double_codepoint & 0x3FF) | 0xDC00; + } + } + buffer[new_len] = 0; + return new_len; +} + +size_t utf16_to_utf8(const std::vector &input, char *buffer, size_t buffer_size) { + assert(buffer_size > 0); + size_t new_len = 0; + for (size_t i = 0; i < input.size(); ) { + uint32_t codepoint = 0; + i += _decode_utf16(&input[i], input.size() - i, &codepoint); + size_t char_len = _codepoint_len(codepoint); + if (new_len + char_len > (buffer_size - 1)) + break; + new_len += _encode_codepoint(&buffer[new_len], codepoint); + } + buffer[new_len] = 0; + return new_len; +} + +std::string cp1251_to_utf8(const std::string &input) { + char tmp[4]; + std::string out; + for (auto &c: input) + out.append(tmp, _encode_codepoint(tmp, cp1251_to_utf16_table[c])); + return out; +} + +size_t cp1251_to_utf8(const std::string &input, char *buffer, size_t buffer_size) { + assert(buffer_size > 0); + size_t new_len = 0; + for (size_t i = 0; i < input.size(); i++) { + uint16_t codepoint = cp1251_to_utf16_table[input[i]]; + size_t char_len = _codepoint_len(codepoint); + if (new_len + char_len > (buffer_size - 1)) + break; + new_len += _encode_codepoint(&buffer[new_len], codepoint); + } + buffer[new_len] = 0; + return new_len; +} + +std::vector cp1251_to_utf16(const std::string &input) { + std::vector out; + out.resize(input.size()); + for (auto &c: input) + out.push_back(cp1251_to_utf16_table[c]); + return out; +} + +size_t cp1251_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size) { + assert(buffer_size > 0); + size_t new_len = std::min(buffer_size - 1, input.size()); + for (size_t i = 0; i < new_len; i++) + buffer[i] = cp1251_to_utf16_table[input[i]]; + buffer[new_len] = 0; + return new_len; +} diff --git a/src/charset.h b/src/charset.h new file mode 100644 index 0000000..7783c7c --- /dev/null +++ b/src/charset.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +// CP1251 -> UTF8 +std::string cp1251_to_utf8(const std::string &input); +size_t cp1251_to_utf8(const std::string &input, char *buffer, size_t buffer_size); + +// CP1251 -> UTF16 +std::vector cp1251_to_utf16(const std::string &input); +size_t cp1251_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size); + +// UTF16 -> UTF8 +size_t utf16_to_utf8(const std::vector &input, char *buffer, size_t buffer_size); +size_t utf8_to_utf16(const std::string &input, uint16_t *buffer, size_t buffer_size); diff --git a/src/main.cpp b/src/main.cpp index a537cfd..d20cc42 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -41,6 +41,7 @@ int main(int argc, char **argv) { filename = std::filesystem::canonical(filename); + std::string exe_dir = std::filesystem::canonical(std::filesystem::path(filename).parent_path()); std::string self_dir = std::filesystem::canonical(std::filesystem::path(argv[0]).parent_path()); std::string library_path_env; diff --git a/src/swi.cpp b/src/swi.cpp index 8bf1c3e..08023b2 100644 --- a/src/swi.cpp +++ b/src/swi.cpp @@ -87,6 +87,30 @@ void loader_init_switab() { switab_functions[0x00B5] = (void *) SWI_GetWeek; switab_functions[0x0230] = (void *) SWI_GetTimeZoneShift; + // Wide String + switab_functions[0x011F] = (void *) SWI_wstrcpy, + switab_functions[0x0120] = (void *) SWI_wstrncpy, + switab_functions[0x0121] = (void *) SWI_wstrcat, + switab_functions[0x0122] = (void *) SWI_wstrncat, + switab_functions[0x0123] = (void *) SWI_wstrlen, + switab_functions[0x0124] = (void *) SWI_wsprintf, + switab_functions[0x0125] = (void *) SWI_AllocWS, + switab_functions[0x0126] = (void *) SWI_CutWSTR, + switab_functions[0x0127] = (void *) SWI_CreateLocalWS, + switab_functions[0x0128] = (void *) SWI_CreateWS, + switab_functions[0x0129] = (void *) SWI_FreeWS, + switab_functions[0x016C] = (void *) SWI_str_2ws, + switab_functions[0x02E2] = (void *) SWI_wstrcmp, + switab_functions[0x0214] = (void *) SWI_wstrcpybypos, + switab_functions[0x0215] = (void *) SWI_wsRemoveChars, + switab_functions[0x01E2] = (void *) SWI_ws_2utf8, + switab_functions[0x01E3] = (void *) SWI_utf8_2ws, + switab_functions[0x01DC] = (void *) SWI_wstrchr, + switab_functions[0x01DD] = (void *) SWI_wstrrchr, + switab_functions[0x001C] = (void *) SWI_wsAppendChar, + switab_functions[0x001D] = (void *) SWI_wsInsertChar, + + // Other switab_functions[0x190] = (void *) SWI_MutexCreate; switab_functions[0x191] = (void *) SWI_MutexDestroy; switab_functions[0x2EE] = (void *) SWI_elfclose; diff --git a/src/swi.h b/src/swi.h index 0641eb1..bae6c0a 100644 --- a/src/swi.h +++ b/src/swi.h @@ -4,6 +4,7 @@ #include #define NEWSGOLD 1 +#define SWI_STRACE 0 /* * Internals @@ -166,3 +167,37 @@ void SWI_GetDateTime(TDate *param1, TTime *param2); char SWI_GetWeek(TDate *param1); int SWI_GetTimeZoneShift(TDate *param1, TTime *param2, int timeZone); TDateTimeSettings *SWI_RamDateTimeSettings(void); + +/* + * Wide String + * */ +typedef struct { + uint16_t *wsbody; + void *(*ws_malloc)(size_t); + void (*ws_mfree)(void *); + int isbody_allocated; + uint16_t maxlen; + uint16_t unk1; +} WSHDR; + +WSHDR *SWI_wstrcpy(WSHDR *wshdr_d, WSHDR *wshdr_s); +WSHDR *SWI_wstrncpy(WSHDR *param1, WSHDR * param2, int param3); +WSHDR *SWI_wstrcat(WSHDR *wshdr_d, WSHDR *wshdr_s); +WSHDR *SWI_wstrncat(WSHDR *wshdr_d, WSHDR *wshdr_s, int n); +int SWI_wstrlen(WSHDR *wshdr); +int SWI_wsprintf(WSHDR* param1, const char *format,...); +WSHDR *SWI_AllocWS(int len); +void SWI_CutWSTR(WSHDR *wshdr, int len); +WSHDR *SWI_CreateLocalWS(WSHDR *wshdr, uint16_t *wsbody, int len); +WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), int len); +void SWI_FreeWS(WSHDR *wshdr); +int SWI_str_2ws(WSHDR *ws, const char *str, uint32_t size); +int SWI_wstrcmp(WSHDR *ws1, WSHDR *ws2); +void SWI_wstrcpybypos(WSHDR *dest, WSHDR *src, int from, int len); +void SWI_wsRemoveChars(WSHDR *ws, int from, int to); +int SWI_ws_2utf8( WSHDR *from, char *to, int *result_length, uint32_t max_len); +int SWI_utf8_2ws(WSHDR *ws, const char *utf8_str, uint32_t maxLen); +short SWI_wstrchr(WSHDR *ws, uint32_t start_pos, uint32_t wchar); +short SWI_wstrrchr(WSHDR *ws, uint32_t max_pos, uint32_t wchar); +void SWI_wsAppendChar(WSHDR *ws, int wchar); +int SWI_wsInsertChar(WSHDR *ws, int wchar, int pos); diff --git a/src/swi/fs.cpp b/src/swi/fs.cpp index e0c7b99..97b9eb5 100644 --- a/src/swi/fs.cpp +++ b/src/swi/fs.cpp @@ -18,6 +18,14 @@ struct FileSearchCtx { size_t found_cnt; }; +static void _set_errno(int ret, uint32_t *errp) { + if (ret < 0) { + *errp = 1; + } else { + *errp = 0; + } +} + int SWI_open(const char *path, uint32_t f, uint32_t m, uint32_t *errp) { int flags = 0; mode_t mode = S_IRUSR | S_IWUSR; @@ -52,70 +60,47 @@ int SWI_open(const char *path, uint32_t f, uint32_t m, uint32_t *errp) { ret = open(SieFs::sie2path(path).c_str(), flags, mode); } - fprintf(stderr, "open(%s, %d, %d) = %d\n", SieFs::sie2path(path).c_str(), flags, mode, ret); - - if (ret < 0) { - perror("[SWI] open"); - if (errp) - *errp = 1; - } else { - if (errp) - *errp = 0; + if (SWI_STRACE) { + fprintf(stderr, "[strace] open(%s, %d, %d) = %d\n", SieFs::sie2path(path).c_str(), flags, mode, ret); } + + _set_errno(ret, errp); return ret; } int SWI_read(int fd, void *buff, int count, uint32_t *errp) { int ret = read(fd, buff, count); - if (ret < 0) { - perror("[SWI] read"); - if (errp) - *errp = 1; - } else { - if (errp) - *errp = 0; + if (SWI_STRACE) { + fprintf(stderr, "[strace] read(%d, %p, %d) = %d\n", fd, buff, count, ret); } + _set_errno(ret, errp); return ret; } int SWI_write(int fd, const void *buff, int count, uint32_t *errp) { int ret = write(fd, buff, count); - if (ret < 0) { - fprintf(stderr, "fd=%d\n", fd); - perror("[SWI] write"); - if (errp) - *errp = 1; - } else { - if (errp) - *errp = 0; + if (SWI_STRACE) { + fprintf(stderr, "[strace] write(%d, %p, %d) = %d\n", fd, buff, count, ret); } + _set_errno(ret, errp); return ret; } int SWI_close(int fd, uint32_t *errp) { int ret = close(fd); - // fprintf(stderr, "close(%d) = %d\n", fd, ret); - if (ret != 0) { - perror("[SWI] close"); - if (errp) - *errp = 1; - } else { - if (errp) - *errp = 0; + if (SWI_STRACE) { + fprintf(stderr, "[strace] close(%d) = %d\n", fd, ret); } + _set_errno(ret, errp); return ret; } int SWI_flush(int fd, uint32_t *errp) { int ret = fsync(fd); - if (ret != 0) { - perror("[SWI] flush"); - if (errp) - *errp = 1; - } else { - if (errp) - *errp = 0; + if (SWI_STRACE) { + fprintf(stderr, "[strace] fsync(%d) = %d\n", fd, ret); } + _set_errno(ret, errp); return ret; } @@ -127,14 +112,10 @@ long SWI_lseek(int fd, uint32_t offset, uint32_t origin, uint32_t *errp, uint32_ int SWI_mkdir(const char *pathname, uint32_t *errp) { int ret = mkdir(SieFs::sie2path(pathname).c_str(), 0755); - if (ret != 0) { - perror("[SWI] mkdir"); - if (errp) - *errp = 1; - } else { - if (errp) - *errp = 0; + if (SWI_STRACE) { + fprintf(stderr, "[strace] mkdir(%s) = %d\n", SieFs::sie2path(pathname).c_str(), ret); } + _set_errno(ret, errp); return ret; } @@ -186,7 +167,6 @@ int SWI_FindFirstFile(DIR_ENTRY *de, const char *pattern, uint32_t *errp) { int ret = glob(unix_path.c_str(), 0, NULL, &ctx->glob); if (ret != 0) { - perror("[SWI] glob"); if (errp) *errp = 1; delete ctx; @@ -295,7 +275,6 @@ int SWI_GetFileStats(const char *siemens_file, FSTATS *out_st, uint32_t *errp) { return 0; } else { - perror("[SWI] lstat"); if (errp) *errp = 1; } diff --git a/src/swi/ws.cpp b/src/swi/ws.cpp new file mode 100644 index 0000000..9d10fd4 --- /dev/null +++ b/src/swi/ws.cpp @@ -0,0 +1,180 @@ +#include "../swi.h" +#include "../charset.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +int SWI_wstrcmp(WSHDR *ws1 ,WSHDR *ws2) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +void SWI_wstrcpybypos(WSHDR *dest, WSHDR *src, int from, int len) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); +} + +void SWI_wsRemoveChars(WSHDR *ws, int from, int to) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); +} + +short SWI_wstrchr(WSHDR *ws, uint32_t start_pos, uint32_t wchar) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +short SWI_wstrrchr(WSHDR *ws, uint32_t max_pos, uint32_t wchar) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +void SWI_wsAppendChar(WSHDR *ws, int wchar) { + WSHDR wslocal = {}; + uint16_t wsbody[2]; + SWI_CreateLocalWS(&wslocal, wsbody, 2); + wsbody[0] = wchar; + SWI_wstrcat(ws, &wslocal); +} + +int SWI_wsInsertChar(WSHDR *ws,int wchar,int pos) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +WSHDR *SWI_wstrcpy(WSHDR *ws1, WSHDR *ws2) { + return SWI_wstrncpy(ws1, ws2, SWI_wstrlen(ws2)); +} + +WSHDR *SWI_wstrncpy(WSHDR *ws1, WSHDR *ws2, int n) { + uint16_t *s = ws1->wsbody; + uint16_t *s2 = ws2->wsbody; + + if (n >= ws2->maxlen) + n = ws2->maxlen - 1; + if (n >= ws1->maxlen) + n = ws1->maxlen - 1; + + while (n > 0 && *s2 != 0) { + *s++ = *s2++; + --n; + } + while (n > 0) { + *s++ = 0; + --n; + } + return ws1; +} + +WSHDR *SWI_wstrcat(WSHDR *s1, WSHDR *s2) { + WSHDR wslocal = {}; + size_t len1 = SWI_wstrlen(s1); + SWI_CreateLocalWS(&wslocal, &s1->wsbody[len1], s1->maxlen - len1); + SWI_wstrcpy(&wslocal, s2); + return s1; +} + +WSHDR *SWI_wstrncat(WSHDR *s1, WSHDR *s2, int n) { + size_t len1 = SWI_wstrlen(s1); + size_t len2 = SWI_wstrlen(s2); + if (len2 < n) { + WSHDR wslocal = {}; + SWI_CreateLocalWS(&wslocal, &s1->wsbody[len1], s1->maxlen - len1); + SWI_wstrcpy(&wslocal, s2); + } else { + WSHDR wslocal = {}; + SWI_CreateLocalWS(&wslocal, &s1->wsbody[len1], s1->maxlen - len1); + SWI_wstrncpy(&s1[len1], s2, n); + s1->wsbody[len1 + n] = 0; + } + return s1; +} + +int SWI_wstrlen(WSHDR *ws) { + uint16_t *ptr = ws->wsbody; + int len = 0; + while (*ptr) { + *ptr++; + len++; + } + return len; +} + +int SWI_wsprintf(WSHDR * param1,const char *format,...) { + fprintf(stderr, "%s not implemented!\n", __func__); + abort(); + return 0; +} + +WSHDR *SWI_AllocWS(int len) { + return SWI_CreateWS(SWI_malloc, SWI_free, len); +} + +void SWI_CutWSTR(WSHDR *ws, int len) { + if (len >= ws->maxlen) + len = ws->maxlen - 1; + ws->wsbody[len] = 0; +} + +WSHDR *SWI_CreateLocalWS(WSHDR *ws, uint16_t *wsbody, int maxlen) { + ws->ws_malloc = nullptr; + ws->ws_mfree = nullptr; + ws->isbody_allocated = 0; + ws->maxlen = maxlen; + ws->wsbody = wsbody; + ws->unk1 = 0; // wtf + memset(ws->wsbody, 0, ws->maxlen * sizeof(uint16_t)); + return ws; +} + +WSHDR *SWI_CreateWS(void *(*ws_malloc)(size_t), void (*ws_mfree)(void *), int len) { + WSHDR *ws = reinterpret_cast(ws_malloc(sizeof(WSHDR))); + ws->ws_malloc = ws_malloc; + ws->ws_mfree = ws_mfree; + ws->isbody_allocated = 1; + ws->maxlen = len + 1; + ws->wsbody = reinterpret_cast(ws_malloc(ws->maxlen * sizeof(uint16_t))); + ws->unk1 = 0; // wtf + memset(ws->wsbody, 0, ws->maxlen * sizeof(uint16_t)); + return ws; +} + +void SWI_FreeWS(WSHDR *ws) { + if (ws) { + auto ws_free = ws->ws_mfree; + if (ws->isbody_allocated) + ws_free(ws->wsbody); + ws_free(ws); + } +} + +int SWI_str_2ws(WSHDR *ws, const char *buffer, uint32_t max_size) { + if (max_size > ws->maxlen) + max_size = ws->maxlen; + return cp1251_to_utf16(buffer, ws->wsbody, max_size); +} + +int SWI_ws_2utf8(WSHDR *from, char *buffer, int *result_length, uint32_t max_size) { + std::vector tmp; + tmp.assign(from->wsbody, from->wsbody + SWI_wstrlen(from)); + size_t new_len = utf16_to_utf8(tmp, buffer, max_size); + if (result_length) + *result_length = new_len; + return new_len; +} + +int SWI_utf8_2ws(WSHDR *ws, const char *buffer, uint32_t max_size) { + if (max_size > ws->maxlen) + max_size = ws->maxlen; + return utf8_to_utf16(buffer, ws->wsbody, max_size); +}