mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
windows.c: fix wrong usage of wcslen() for utf8 conversation
wcslen() returns the number of characters in a wchar_t string. In the case of WideCharToMultiByte() an estimate for the size of the utf8 buffer is needed. Using wcslen() is incorrect for such a buffer, because for any non-ASCII character the estimate will be off by 1 byte. Call the following instead to obtain the proper UTF8 buffer size for the conversation: WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL); Also fix some missing "\n" in fprintf() calls. Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
parent
2dd0187fe8
commit
d4d9b840cc
1 changed files with 9 additions and 5 deletions
|
@ -50,16 +50,20 @@ static char *utf16_to_utf8_fl(const wchar_t *utf16, char *file, int line)
|
|||
assert(file != NULL);
|
||||
assert(line);
|
||||
/* estimate buffer size */
|
||||
const int sz = wcslen(utf16) + 1;
|
||||
const int sz = WideCharToMultiByte(CP_UTF8, 0, utf16, -1, NULL, 0, NULL, NULL);
|
||||
if (!sz) {
|
||||
fprintf(stderr, "%s:%d: cannot estimate buffer size\n", file, line);
|
||||
return NULL;
|
||||
}
|
||||
char *utf8 = (char *)malloc(sz);
|
||||
if (!utf8) {
|
||||
fprintf(stderr, "%s:%d: %s %d.", file, line, "cannot allocate buffer of size", sz);
|
||||
fprintf(stderr, "%s:%d: cannot allocate buffer of size: %d\n", file, line, sz);
|
||||
return NULL;
|
||||
}
|
||||
if (WideCharToMultiByte(CP_UTF8, 0, utf16, -1, utf8, sz, NULL, NULL)) {
|
||||
return utf8;
|
||||
}
|
||||
fprintf(stderr, "%s:%d: %s", file, line, "cannot convert string.");
|
||||
fprintf(stderr, "%s:%d: cannot convert string\n", file, line);
|
||||
free((void *)utf8);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -78,12 +82,12 @@ static wchar_t *utf8_to_utf16_fl(const char *utf8, char *file, int line)
|
|||
const int sz = strlen(utf8) + 1;
|
||||
wchar_t *utf16 = (wchar_t *)malloc(sizeof(wchar_t) * sz);
|
||||
if (!utf16) {
|
||||
fprintf(stderr, "%s:%d: %s %d.", file, line, "cannot allocate buffer of size", sz);
|
||||
fprintf(stderr, "%s:%d: cannot allocate buffer of size: %d\n", file, line, sz);
|
||||
return NULL;
|
||||
}
|
||||
if (MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, sz))
|
||||
return utf16;
|
||||
fprintf(stderr, "%s:%d: %s", file, line, "cannot convert string.");
|
||||
fprintf(stderr, "%s:%d: cannot convert string\n", file, line);
|
||||
free((void *)utf16);
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue