From: Luca Bacci Date: Thu, 29 Dec 2022 13:58:49 +0000 (+0100) Subject: Add two functions for converting between UTF-16 and UTF-8 on Windows X-Git-Tag: archive/raspbian/1%0.1.106-3+rpi1^2~15^2^2~43 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=4d3c12980f46a9511853c638ea5dd93836a6e965;p=babl.git Add two functions for converting between UTF-16 and UTF-8 on Windows --- diff --git a/babl/babl-util.c b/babl/babl-util.c index d446a43..cf1d8c6 100644 --- a/babl/babl-util.c +++ b/babl/babl-util.c @@ -150,6 +150,74 @@ _babl_file_get_contents (const char *path, #ifdef _WIN32 +wchar_t * +babl_convert_utf8_to_utf16 (const char *str) +{ + int wchar_count = 0; + wchar_t *wstr = NULL; + + if (!str) + return NULL; + + wchar_count = MultiByteToWideChar (CP_UTF8, + MB_ERR_INVALID_CHARS, + str, -1, + NULL, 0); + if (wchar_count <= 0) + return NULL; + + wstr = babl_malloc (wchar_count * sizeof (wchar_t)); + if (!wstr) + return NULL; + + wchar_count = MultiByteToWideChar (CP_UTF8, + MB_ERR_INVALID_CHARS, + str, -1, + wstr, wchar_count); + if (wchar_count <= 0) + { + babl_free (wstr); + return NULL; + } + + return wstr; +} + +char * +babl_convert_utf16_to_utf8 (const wchar_t *wstr) +{ + int char_count = 0; + char *str = NULL; + + if (!wstr) + return NULL; + + char_count = WideCharToMultiByte (CP_UTF8, + WC_ERR_INVALID_CHARS, + wstr, -1, + NULL, 0, + NULL, NULL); + if (char_count <= 0) + return NULL; + + str = babl_malloc (char_count); + if (!str) + return NULL; + + char_count = WideCharToMultiByte (CP_UTF8, + WC_ERR_INVALID_CHARS, + wstr, -1, + str, char_count, + NULL, NULL); + if (char_count <= 0) + { + babl_free (str); + return NULL; + } + + return str; +} + extern IMAGE_DOS_HEADER __ImageBase; void * diff --git a/babl/babl-util.h b/babl/babl-util.h index f24101d..82c421a 100644 --- a/babl/babl-util.h +++ b/babl/babl-util.h @@ -19,6 +19,8 @@ #ifndef _BABL_UTIL_H #define _BABL_UTIL_H +#include + long babl_ticks (void); @@ -29,6 +31,12 @@ babl_rel_avg_error (const double *imgA, #ifdef _WIN32 +wchar_t * +babl_convert_utf8_to_utf16 (const char *str); + +char * +babl_convert_utf16_to_utf8 (const wchar_t *wstr); + void * get_libbabl_module (void); diff --git a/babl/babl.c b/babl/babl.c index 57bb916..8a6f29a 100644 --- a/babl/babl.c +++ b/babl/babl.c @@ -44,21 +44,16 @@ babl_dir_list (void) { #ifdef _WIN32 /* Figure it out from the location of this DLL */ - char *filename; - int filename_size; - char *sep1, *sep2; - wchar_t w_filename[MAX_PATH]; + char *filename = NULL; + char *sep1, *sep2; DWORD nSize = sizeof (w_filename) / sizeof ((w_filename)[0]); if (GetModuleFileNameW (get_libbabl_module (), w_filename, nSize) == 0) babl_fatal ("GetModuleFilenameW failed"); - filename_size = WideCharToMultiByte (CP_UTF8, 0, w_filename, -1, NULL, 0, - NULL, NULL); - filename = babl_malloc (sizeof (char) * filename_size); - if (!WideCharToMultiByte (CP_UTF8, 0, w_filename, -1, - filename, filename_size, NULL, NULL)) + filename = babl_convert_utf16_to_utf8 (w_filename); + if (!filename) babl_fatal ("Converting module filename to UTF-8 failed"); /* If the DLL file name is of the format