2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-05-22 21:55:40 +00:00
|
|
|
#include "arch/ArchString.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/Arch.h"
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <climits>
|
|
|
|
|
#include <cstdlib>
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <cstring>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
static ArchMutex s_mutex = nullptr;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// use C library non-reentrant multibyte conversion with mutex
|
|
|
|
|
//
|
|
|
|
|
|
2025-05-22 21:55:40 +00:00
|
|
|
ArchString::~ArchString()
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
if (s_mutex != nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
ARCH->closeMutex(s_mutex);
|
2025-04-27 19:39:23 +00:00
|
|
|
s_mutex = nullptr;
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-22 21:55:40 +00:00
|
|
|
int ArchString::convStringWCToMB(char *dst, const wchar_t *src, uint32_t n, bool *errors) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ptrdiff_t len = 0;
|
|
|
|
|
|
|
|
|
|
bool dummyErrors;
|
2025-04-27 19:39:23 +00:00
|
|
|
if (errors == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
errors = &dummyErrors;
|
|
|
|
|
}
|
|
|
|
|
*errors = false;
|
|
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
if (s_mutex == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
s_mutex = ARCH->newMutex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARCH->lockMutex(s_mutex);
|
|
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
if (dst == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
char dummy[MB_LEN_MAX];
|
|
|
|
|
const wchar_t *scan = src;
|
|
|
|
|
for (; n > 0; --n) {
|
|
|
|
|
ptrdiff_t mblen = wctomb(dummy, *scan);
|
|
|
|
|
if (mblen == -1) {
|
|
|
|
|
*errors = true;
|
|
|
|
|
mblen = 1;
|
|
|
|
|
}
|
|
|
|
|
len += mblen;
|
|
|
|
|
++scan;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2025-05-02 12:03:44 +00:00
|
|
|
if (ptrdiff_t mblen = wctomb(dummy, L'\0'); mblen != -1) {
|
2024-07-02 19:07:06 +00:00
|
|
|
len += mblen - 1;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
2025-05-22 20:24:32 +00:00
|
|
|
const char *dst0 = dst;
|
2024-07-02 19:07:06 +00:00
|
|
|
const wchar_t *scan = src;
|
|
|
|
|
for (; n > 0; --n) {
|
2025-05-22 20:24:32 +00:00
|
|
|
if (ptrdiff_t mblen = wctomb(dst, *scan); mblen == -1) {
|
2024-07-02 19:07:06 +00:00
|
|
|
*errors = true;
|
|
|
|
|
*dst++ = '?';
|
|
|
|
|
} else {
|
|
|
|
|
dst += mblen;
|
|
|
|
|
}
|
|
|
|
|
++scan;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2025-05-02 12:03:44 +00:00
|
|
|
if (ptrdiff_t mblen = wctomb(dst, L'\0'); mblen != -1) {
|
2024-07-02 19:07:06 +00:00
|
|
|
// don't include nul terminator
|
|
|
|
|
dst += mblen - 1;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
len = dst - dst0;
|
|
|
|
|
}
|
|
|
|
|
ARCH->unlockMutex(s_mutex);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return static_cast<int>(len);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-22 21:55:40 +00:00
|
|
|
ArchString::EWideCharEncoding ArchString::getWideCharEncoding() const
|
|
|
|
|
{
|
|
|
|
|
#ifdef SYSAPI_WIN32
|
|
|
|
|
return EWideCharEncoding::kUTF16;
|
|
|
|
|
#else
|
|
|
|
|
return EWideCharEncoding::kUCS4;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ArchString::convStringMBToWC(wchar_t *dst, const char *src, uint32_t n, bool *errors) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ptrdiff_t len = 0;
|
|
|
|
|
wchar_t dummy;
|
|
|
|
|
|
|
|
|
|
bool dummyErrors;
|
2025-04-27 19:39:23 +00:00
|
|
|
if (errors == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
errors = &dummyErrors;
|
|
|
|
|
}
|
|
|
|
|
*errors = false;
|
|
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
if (s_mutex == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
s_mutex = ARCH->newMutex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ARCH->lockMutex(s_mutex);
|
|
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
if (dst == nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
const char *scan = src;
|
|
|
|
|
while (n > 0) {
|
2025-05-02 12:03:44 +00:00
|
|
|
switch (ptrdiff_t mblen = mbtowc(&dummy, scan, n); mblen) {
|
2024-07-02 19:07:06 +00:00
|
|
|
case -2:
|
|
|
|
|
// incomplete last character. convert to unknown character.
|
|
|
|
|
*errors = true;
|
|
|
|
|
len += 1;
|
|
|
|
|
n = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
|
// invalid character. count one unknown character and
|
|
|
|
|
// start at the next byte.
|
|
|
|
|
*errors = true;
|
|
|
|
|
len += 1;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
len += 1;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// normal character
|
|
|
|
|
len += 1;
|
|
|
|
|
scan += static_cast<int>(mblen);
|
|
|
|
|
n -= static_cast<int>(mblen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
2025-05-22 20:24:32 +00:00
|
|
|
const wchar_t *dst0 = dst;
|
2024-07-02 19:07:06 +00:00
|
|
|
const char *scan = src;
|
|
|
|
|
while (n > 0) {
|
2025-05-02 12:03:44 +00:00
|
|
|
switch (ptrdiff_t mblen = mbtowc(dst, scan, n); mblen) {
|
2024-07-02 19:07:06 +00:00
|
|
|
case -2:
|
|
|
|
|
// incomplete character. convert to unknown character.
|
|
|
|
|
*errors = true;
|
|
|
|
|
*dst = (wchar_t)0xfffd;
|
|
|
|
|
n = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
|
// invalid character. count one unknown character and
|
|
|
|
|
// start at the next byte.
|
|
|
|
|
*errors = true;
|
|
|
|
|
*dst = (wchar_t)0xfffd;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
|
*dst = (wchar_t)0x0000;
|
|
|
|
|
scan += 1;
|
|
|
|
|
n -= 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// normal character
|
|
|
|
|
scan += static_cast<int>(mblen);
|
|
|
|
|
n -= static_cast<int>(mblen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
++dst;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
len = dst - dst0;
|
|
|
|
|
}
|
|
|
|
|
ARCH->unlockMutex(s_mutex);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return static_cast<int>(len);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|