From 8985ee2114ddeb7e65148843f592f3270053e269 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 5 Oct 2018 20:54:59 +0000 Subject: Better implementations of Tcl_WinTCharToUtf() and Tcl_WinUtfToTChar(). They don't make any assumption on whether encodings are initialized, or specific win32 handling of special characters. --- generic/tclStubInit.c | 49 ++++++++--------------------------------------- win/tclWin32Dll.c | 53 ++++----------------------------------------------- 2 files changed, 12 insertions(+), 90 deletions(-) diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index b28d501..50bb4a2 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -12,6 +12,10 @@ #include "tclInt.h" #include "tommath.h" +#ifdef __CYGWIN__ +# include +#endif + #ifdef __GNUC__ #pragma GCC dependency "tcl.decls" #pragma GCC dependency "tclInt.decls" @@ -106,10 +110,6 @@ static unsigned short TclWinNToHS(unsigned short ns) { # define TclWinFlushDirtyChannels doNothing # define TclWinResetInterfaces doNothing -#if TCL_UTF_MAX < 4 -static Tcl_Encoding winTCharEncoding; -#endif - static int TclpIsAtty(int fd) { @@ -188,25 +188,8 @@ Tcl_WinUtfToTChar( int len, Tcl_DString *dsPtr) { -#if TCL_UTF_MAX > 3 - WCHAR *wp; - int size = MultiByteToWideChar(CP_UTF8, 0, string, len, 0, 0); - Tcl_DStringInit(dsPtr); - Tcl_DStringSetLength(dsPtr, 2*size+2); - wp = (WCHAR *)Tcl_DStringValue(dsPtr); - MultiByteToWideChar(CP_UTF8, 0, string, len, wp, size+1); - if (len == -1) --size; /* account for 0-byte at string end */ - Tcl_DStringSetLength(dsPtr, 2*size); - wp[size] = 0; - return (char *)wp; -#else - if (!winTCharEncoding) { - winTCharEncoding = Tcl_GetEncoding(0, "unicode"); - } - return Tcl_UtfToExternalDString(winTCharEncoding, - string, len, dsPtr); -#endif + return (char *)Tcl_UtfToUniCharDString(string, len, dsPtr); } char * @@ -215,29 +198,13 @@ Tcl_WinTCharToUtf( int len, Tcl_DString *dsPtr) { -#if TCL_UTF_MAX > 3 - char *p; - int size; - if (len > 0) { len /= 2; + } else if (len == -1) { + len = wcslen((wchar_t *)string); } - size = WideCharToMultiByte(CP_UTF8, 0, string, len, 0, 0, NULL, NULL); Tcl_DStringInit(dsPtr); - Tcl_DStringSetLength(dsPtr, size+1); - p = (char *)Tcl_DStringValue(dsPtr); - WideCharToMultiByte(CP_UTF8, 0, string, len, p, size, NULL, NULL); - if (len == -1) --size; /* account for 0-byte at string end */ - Tcl_DStringSetLength(dsPtr, size); - p[size] = 0; - return p; -#else - if (!winTCharEncoding) { - winTCharEncoding = Tcl_GetEncoding(0, "unicode"); - } - return Tcl_ExternalToUtfDString(winTCharEncoding, - string, len, dsPtr); -#endif + return Tcl_UniCharToUtfDString((Tcl_UniChar *)string, len, dsPtr); } #if defined(TCL_WIDE_INT_IS_LONG) diff --git a/win/tclWin32Dll.c b/win/tclWin32Dll.c index a85dd0c..ca1c856 100644 --- a/win/tclWin32Dll.c +++ b/win/tclWin32Dll.c @@ -33,10 +33,6 @@ static int platformId; /* Running under NT, or 95/98? */ #define cpuid __asm __emit 0fh __asm __emit 0a2h #endif -#if TCL_UTF_MAX < 4 -static Tcl_Encoding winTCharEncoding = NULL; -#endif - /* * The following declaration is for the VC++ DLL entry point. */ @@ -201,8 +197,6 @@ TclWinInit( if (platformId == VER_PLATFORM_WIN32_WINDOWS) { Tcl_Panic("Windows 9x is not a supported platform"); } - - TclWinResetInterfaces(); } /* @@ -282,10 +276,6 @@ TclWinNoBackslash( void TclpSetInterfaces(void) { -#if TCL_UTF_MAX < 4 - TclWinResetInterfaces(); - winTCharEncoding = Tcl_GetEncoding(NULL, "unicode"); -#endif } /* @@ -313,8 +303,6 @@ TclWinEncodingsCleanup(void) { MountPointMap *dlIter, *dlIter2; - TclWinResetInterfaces(); - /* * Clean up the mount point map. */ @@ -348,12 +336,6 @@ TclWinEncodingsCleanup(void) void TclWinResetInterfaces(void) { -#if TCL_UTF_MAX < 4 - if (winTCharEncoding != NULL) { - Tcl_FreeEncoding(winTCharEncoding); - winTCharEncoding = NULL; - } -#endif } /* @@ -565,22 +547,8 @@ Tcl_WinUtfToTChar( Tcl_DString *dsPtr) /* Uninitialized or free DString in which the * converted string is stored. */ { -#if TCL_UTF_MAX > 3 - TCHAR *wp; - int size = MultiByteToWideChar(CP_UTF8, 0, string, len, 0, 0); - Tcl_DStringInit(dsPtr); - Tcl_DStringSetLength(dsPtr, 2*size+2); - wp = (TCHAR *)Tcl_DStringValue(dsPtr); - MultiByteToWideChar(CP_UTF8, 0, string, len, wp, size+1); - if (len == -1) --size; /* account for 0-byte at string end */ - Tcl_DStringSetLength(dsPtr, 2*size); - wp[size] = 0; - return wp; -#else - return (TCHAR *) Tcl_UtfToExternalDString(winTCharEncoding, - string, len, dsPtr); -#endif + return Tcl_UtfToUniCharDString(string, len, dsPtr); } char * @@ -591,26 +559,13 @@ Tcl_WinTCharToUtf( Tcl_DString *dsPtr) /* Uninitialized or free DString in which the * converted string is stored. */ { -#if TCL_UTF_MAX > 3 - char *p; - int size; - if (len > 0) { len /= 2; + } else if (len < 0) { + len = wcslen(string); } - size = WideCharToMultiByte(CP_UTF8, 0, string, len, 0, 0, NULL, NULL); Tcl_DStringInit(dsPtr); - Tcl_DStringSetLength(dsPtr, size+1); - p = (char *)Tcl_DStringValue(dsPtr); - WideCharToMultiByte(CP_UTF8, 0, string, len, p, size, NULL, NULL); - if (len == -1) --size; /* account for 0-byte at string end */ - Tcl_DStringSetLength(dsPtr, size); - p[size] = 0; - return p; -#else - return Tcl_ExternalToUtfDString(winTCharEncoding, - (const char *) string, len, dsPtr); -#endif + return Tcl_UniCharToUtfDString(string, len, dsPtr); } /* -- cgit v0.12 From cc4dae5c0f418d304a44bb10c87c624b478b6441 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Sun, 7 Oct 2018 20:26:33 +0000 Subject: Streamline: Tcl_WinTCharToUtf/Tcl_WinUtfToTChar(): Always initialize DString, and let it return NULL when imput is NULL. That's what seem to assume the callers. --- generic/tclStubInit.c | 14 ++++++++++---- win/tclWin32Dll.c | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/generic/tclStubInit.c b/generic/tclStubInit.c index 50bb4a2..cbf4084 100644 --- a/generic/tclStubInit.c +++ b/generic/tclStubInit.c @@ -189,6 +189,9 @@ Tcl_WinUtfToTChar( Tcl_DString *dsPtr) { Tcl_DStringInit(dsPtr); + if (!string) { + return NULL; + } return (char *)Tcl_UtfToUniCharDString(string, len, dsPtr); } @@ -198,12 +201,15 @@ Tcl_WinTCharToUtf( int len, Tcl_DString *dsPtr) { - if (len > 0) { - len /= 2; - } else if (len == -1) { + Tcl_DStringInit(dsPtr); + if (!string) { + return NULL; + } + if (len < 0) { len = wcslen((wchar_t *)string); + } else { + len /= 2; } - Tcl_DStringInit(dsPtr); return Tcl_UniCharToUtfDString((Tcl_UniChar *)string, len, dsPtr); } diff --git a/win/tclWin32Dll.c b/win/tclWin32Dll.c index ca1c856..6fc2401 100644 --- a/win/tclWin32Dll.c +++ b/win/tclWin32Dll.c @@ -548,6 +548,9 @@ Tcl_WinUtfToTChar( * converted string is stored. */ { Tcl_DStringInit(dsPtr); + if (!string) { + return NULL; + } return Tcl_UtfToUniCharDString(string, len, dsPtr); } @@ -559,12 +562,15 @@ Tcl_WinTCharToUtf( Tcl_DString *dsPtr) /* Uninitialized or free DString in which the * converted string is stored. */ { - if (len > 0) { - len /= 2; - } else if (len < 0) { + Tcl_DStringInit(dsPtr); + if (!string) { + return NULL; + } + if (len < 0) { len = wcslen(string); + } else { + len /= 2; } - Tcl_DStringInit(dsPtr); return Tcl_UniCharToUtfDString(string, len, dsPtr); } -- cgit v0.12