Add two functions for converting between UTF-16 and UTF-8 on Windows
authorLuca Bacci <luca.bacci982@gmail.com>
Thu, 29 Dec 2022 13:58:49 +0000 (14:58 +0100)
committerØyvind "pippin" Kolås <pippin@gimp.org>
Tue, 3 Jan 2023 10:33:37 +0000 (10:33 +0000)
babl/babl-util.c
babl/babl-util.h
babl/babl.c

index d446a430ba6b0e9313969f0793967fc6133f82bb..cf1d8c64fcd9ef72920aa2e62de688884041745d 100644 (file)
@@ -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 *
index f24101dadbbf47b7b78273f7a9ef4f07095ceaca..82c421aa8c703eb4124435ff40cac58c9191dea7 100644 (file)
@@ -19,6 +19,8 @@
 #ifndef _BABL_UTIL_H
 #define _BABL_UTIL_H
 
+#include <stddef.h>
+
 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);
 
index 57bb916e3267a4f0ac5700e2342d12be0b4e55d9..8a6f29ad663b11f558cb014c001727abde956ec4 100644 (file)
@@ -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