From c4c4d2c5cb62c87e6741542f0148fe0a08527820 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 24 Sep 2024 08:46:39 +0100 Subject: [PATCH] dup_mb_to_wc, dup_wc_to_mb: remove the 'flags' parameter. This parameter was undocumented, and Windows-specific: its semantics date from before PuTTY was cross-platform, and are "Pass this flags parameter straight through to the Win32 API's conversion functions". So in Windows platform code you can pass flags like MB_USEGLYPHCHARS, but in cross-platform code, you dare not pass anything nonzero at all because the Unix frontend won't recognise it (or, likely, even compile). I've kept the flag for now in the underlying mb_to_wc / wc_to_mb functions. Partly that's because there's one place in the Windows code where the parameter _is_ used; mostly, it's because I'm about to replace those functions anyway, so there's no point in editing all the call sites twice. --- misc.h | 8 ++++---- unix/window.c | 4 ++-- utils/dup_mb_to_wc.c | 8 ++++---- utils/dup_wc_to_mb.c | 9 ++++----- windows/console.c | 4 ++-- windows/controls.c | 2 +- windows/gss.c | 2 +- windows/utils/filename.c | 8 ++++---- windows/utils/message_box.c | 4 ++-- windows/window.c | 14 +++++++------- 10 files changed, 31 insertions(+), 32 deletions(-) diff --git a/misc.h b/misc.h index 3fc2e448..7d94e7e6 100644 --- a/misc.h +++ b/misc.h @@ -71,12 +71,12 @@ void strbuf_finalise_agent_query(strbuf *buf); /* String-to-Unicode converters that auto-allocate the destination and * work around the rather deficient interface of mb_to_wc. */ -wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, +wchar_t *dup_mb_to_wc_c(int codepage, const char *string, size_t len, size_t *outlen_p); -wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string); -char *dup_wc_to_mb_c(int codepage, int flags, const wchar_t *string, +wchar_t *dup_mb_to_wc(int codepage, const char *string); +char *dup_wc_to_mb_c(int codepage, const wchar_t *string, size_t len, const char *defchr, size_t *outlen_p); -char *dup_wc_to_mb(int codepage, int flags, const wchar_t *string, +char *dup_wc_to_mb(int codepage, const wchar_t *string, const char *defchr); static inline int toint(unsigned u) diff --git a/unix/window.c b/unix/window.c index f8dbda74..27696212 100644 --- a/unix/window.c +++ b/unix/window.c @@ -3440,7 +3440,7 @@ static void gtkwin_set_title(TermWin *tw, const char *title, int codepage) GtkFrontend *inst = container_of(tw, GtkFrontend, termwin); sfree(inst->wintitle); if (codepage != CP_UTF8) { - wchar_t *title_w = dup_mb_to_wc(codepage, 0, title); + wchar_t *title_w = dup_mb_to_wc(codepage, title); inst->wintitle = encode_wide_string_as_utf8(title_w); sfree(title_w); } else { @@ -3454,7 +3454,7 @@ static void gtkwin_set_icon_title(TermWin *tw, const char *title, int codepage) GtkFrontend *inst = container_of(tw, GtkFrontend, termwin); sfree(inst->icontitle); if (codepage != CP_UTF8) { - wchar_t *title_w = dup_mb_to_wc(codepage, 0, title); + wchar_t *title_w = dup_mb_to_wc(codepage, title); inst->icontitle = encode_wide_string_as_utf8(title_w); sfree(title_w); } else { diff --git a/utils/dup_mb_to_wc.c b/utils/dup_mb_to_wc.c index f6c48975..6317ca93 100644 --- a/utils/dup_mb_to_wc.c +++ b/utils/dup_mb_to_wc.c @@ -9,14 +9,14 @@ #include "putty.h" #include "misc.h" -wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, +wchar_t *dup_mb_to_wc_c(int codepage, const char *string, size_t inlen, size_t *outlen_p) { assert(inlen <= INT_MAX); size_t mult; for (mult = 1 ;; mult++) { wchar_t *ret = snewn(mult*inlen + 2, wchar_t); - size_t outlen = mb_to_wc(codepage, flags, string, inlen, ret, + size_t outlen = mb_to_wc(codepage, 0, string, inlen, ret, mult*inlen + 1); if (outlen < mult*inlen+1) { if (outlen_p) @@ -28,7 +28,7 @@ wchar_t *dup_mb_to_wc_c(int codepage, int flags, const char *string, } } -wchar_t *dup_mb_to_wc(int codepage, int flags, const char *string) +wchar_t *dup_mb_to_wc(int codepage, const char *string) { - return dup_mb_to_wc_c(codepage, flags, string, strlen(string), NULL); + return dup_mb_to_wc_c(codepage, string, strlen(string), NULL); } diff --git a/utils/dup_wc_to_mb.c b/utils/dup_wc_to_mb.c index 4a55803c..42780f73 100644 --- a/utils/dup_wc_to_mb.c +++ b/utils/dup_wc_to_mb.c @@ -11,7 +11,7 @@ #include "putty.h" #include "misc.h" -char *dup_wc_to_mb_c(int codepage, int flags, const wchar_t *string, +char *dup_wc_to_mb_c(int codepage, const wchar_t *string, size_t inlen, const char *defchr, size_t *outlen_p) { assert(inlen <= INT_MAX); @@ -20,7 +20,7 @@ char *dup_wc_to_mb_c(int codepage, int flags, const wchar_t *string, char *out = snewn(outsize, char); while (true) { - size_t outlen = wc_to_mb(codepage, flags, string, inlen, out, outsize, + size_t outlen = wc_to_mb(codepage, 0, string, inlen, out, outsize, defchr); /* We can only be sure we've consumed the whole input if the * output is not within a multibyte-character-length of the @@ -36,9 +36,8 @@ char *dup_wc_to_mb_c(int codepage, int flags, const wchar_t *string, } } -char *dup_wc_to_mb(int codepage, int flags, const wchar_t *string, +char *dup_wc_to_mb(int codepage, const wchar_t *string, const char *defchr) { - return dup_wc_to_mb_c(codepage, flags, string, wcslen(string), - defchr, NULL); + return dup_wc_to_mb_c(codepage, string, wcslen(string), defchr, NULL); } diff --git a/windows/console.c b/windows/console.c index 89021a9f..be49dd89 100644 --- a/windows/console.c +++ b/windows/console.c @@ -148,7 +148,7 @@ static void console_write(BinarySink *bs, const void *data, size_t len) * Convert the UTF-8 input into a wide string. */ size_t wlen; - wchar_t *wide = dup_mb_to_wc_c(CP_UTF8, 0, data, len, &wlen); + wchar_t *wide = dup_mb_to_wc_c(CP_UTF8, data, len, &wlen); if (conio->hout_is_console) { /* * To write UTF-8 to a console, use WriteConsoleW on the @@ -168,7 +168,7 @@ static void console_write(BinarySink *bs, const void *data, size_t len) * what else can you do? */ size_t clen; - char *sys_cp = dup_wc_to_mb_c(CP_ACP, 0, wide, wlen, "?", &clen); + char *sys_cp = dup_wc_to_mb_c(CP_ACP, wide, wlen, "?", &clen); size_t pos = 0; DWORD nwritten; diff --git a/windows/controls.c b/windows/controls.c index 84708168..54f0f3d0 100644 --- a/windows/controls.c +++ b/windows/controls.c @@ -2012,7 +2012,7 @@ bool winctrl_handle_command(struct dlgparam *dp, UINT msg, of.nMaxFile = lenof(filename); of.lpstrFileTitle = NULL; of.lpstrTitle = title_to_free = dup_mb_to_wc( - DEFAULT_CODEPAGE, 0, ctrl->fileselect.title); + DEFAULT_CODEPAGE, ctrl->fileselect.title); of.Flags = 0; if (request_file_w(NULL, &of, false, ctrl->fileselect.for_writing)) { diff --git a/windows/gss.c b/windows/gss.c index bb1e914a..4f21400c 100644 --- a/windows/gss.c +++ b/windows/gss.c @@ -144,7 +144,7 @@ struct ssh_gss_liblist *ssh_gss_setup(Conf *conf) if (p_AddDllDirectory) { /* Add MIT Kerberos' path to the DLL search path, * it loads its own DLLs further down the road */ - wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, bindir); + wchar_t *dllPath = dup_mb_to_wc(DEFAULT_CODEPAGE, bindir); p_AddDllDirectory(dllPath); sfree(dllPath); } diff --git a/windows/utils/filename.c b/windows/utils/filename.c index eb358b1f..5c1a0d12 100644 --- a/windows/utils/filename.c +++ b/windows/utils/filename.c @@ -10,7 +10,7 @@ Filename *filename_from_str(const char *str) { Filename *fn = snew(Filename); fn->cpath = dupstr(str); - fn->wpath = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, fn->cpath); + fn->wpath = dup_mb_to_wc(DEFAULT_CODEPAGE, fn->cpath); fn->utf8path = encode_wide_string_as_utf8(fn->wpath); return fn; } @@ -19,7 +19,7 @@ Filename *filename_from_wstr(const wchar_t *str) { Filename *fn = snew(Filename); fn->wpath = dupwcs(str); - fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, 0, fn->wpath, "?"); + fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?"); fn->utf8path = encode_wide_string_as_utf8(fn->wpath); return fn; } @@ -29,7 +29,7 @@ Filename *filename_from_utf8(const char *ustr) Filename *fn = snew(Filename); fn->utf8path = dupstr(ustr); fn->wpath = decode_utf8_to_wide_string(fn->utf8path); - fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, 0, fn->wpath, "?"); + fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?"); return fn; } @@ -86,7 +86,7 @@ char filename_char_sanitise(char c) FILE *f_open(const Filename *fn, const char *mode, bool isprivate) { - wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, mode); + wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, mode); return _wfopen(fn->wpath, wmode); sfree(wmode); } diff --git a/windows/utils/message_box.c b/windows/utils/message_box.c index 11b69794..37e22b0a 100644 --- a/windows/utils/message_box.c +++ b/windows/utils/message_box.c @@ -50,8 +50,8 @@ int message_box(HWND owner, LPCTSTR text, LPCTSTR caption, DWORD style, wtext = decode_utf8_to_wide_string(text); wcaption = decode_utf8_to_wide_string(caption); } else { - wtext = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, text); - wcaption = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, caption); + wtext = dup_mb_to_wc(DEFAULT_CODEPAGE, text); + wcaption = dup_mb_to_wc(DEFAULT_CODEPAGE, caption); } mbox.lpszText = wtext; mbox.lpszCaption = wcaption; diff --git a/windows/window.c b/windows/window.c index 14bab0ba..8423a571 100644 --- a/windows/window.c +++ b/windows/window.c @@ -390,7 +390,7 @@ static void sw_SetWindowText(HWND hwnd, wchar_t *text) if (unicode_window) { SetWindowTextW(hwnd, text); } else { - char *mb = dup_wc_to_mb(DEFAULT_CODEPAGE, 0, text, "?"); + char *mb = dup_wc_to_mb(DEFAULT_CODEPAGE, text, "?"); SetWindowTextA(hwnd, mb); sfree(mb); } @@ -417,7 +417,7 @@ wchar_t *terminal_window_class_w(void) { static wchar_t *classname = NULL; if (!classname) - classname = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, appname); + classname = dup_mb_to_wc(DEFAULT_CODEPAGE, appname); if (!hprev) { WNDCLASSW wndclassw; SETUP_WNDCLASS(wndclassw, classname); @@ -549,9 +549,9 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) bool resize_forbidden = false; if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN) resize_forbidden = true; - wchar_t *uappname = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, appname); - wgs->window_name = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, appname); - wgs->icon_name = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, appname); + wchar_t *uappname = dup_mb_to_wc(DEFAULT_CODEPAGE, appname); + wgs->window_name = dup_mb_to_wc(DEFAULT_CODEPAGE, appname); + wgs->icon_name = dup_mb_to_wc(DEFAULT_CODEPAGE, appname); if (!conf_get_bool(wgs->conf, CONF_scrollbar)) winmode &= ~(WS_VSCROLL); if (conf_get_int(wgs->conf, CONF_resize_action) == RESIZE_DISABLED || @@ -4826,7 +4826,7 @@ static int TranslateKey(WinGuiSeat *wgs, UINT message, WPARAM wParam, static void wintw_set_title(TermWin *tw, const char *title, int codepage) { WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin); - wchar_t *new_window_name = dup_mb_to_wc(codepage, 0, title); + wchar_t *new_window_name = dup_mb_to_wc(codepage, title); if (!wcscmp(new_window_name, wgs->window_name)) { sfree(new_window_name); return; @@ -4841,7 +4841,7 @@ static void wintw_set_title(TermWin *tw, const char *title, int codepage) static void wintw_set_icon_title(TermWin *tw, const char *title, int codepage) { WinGuiSeat *wgs = container_of(tw, WinGuiSeat, termwin); - wchar_t *new_icon_name = dup_mb_to_wc(codepage, 0, title); + wchar_t *new_icon_name = dup_mb_to_wc(codepage, title); if (!wcscmp(new_icon_name, wgs->icon_name)) { sfree(new_icon_name); return;