public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/main] Cygwin: glob: perform correct utf-32 -> multibyte conversion
@ 2023-02-22 11:21 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2023-02-22 11:21 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=f3f20038c480e43ae264b760f4d8b14b618ca973

commit f3f20038c480e43ae264b760f4d8b14b618ca973
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Wed Feb 22 10:03:03 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Wed Feb 22 10:03:03 2023 +0100

    Cygwin: glob: perform correct utf-32 -> multibyte conversion
    
    g_Ctoc, converting the UTF-32 filenames to multibyte, still
    used UTF-16 to  multibyte conversion.  Introduce a wirtomb
    helper and fix that.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/glob.cc                |  2 +-
 winsup/cygwin/local_includes/wchar.h |  4 ++++
 winsup/cygwin/strfuncs.cc            | 19 +++++++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/glob.cc b/winsup/cygwin/glob.cc
index 8bcdd61b2032..2f86e46985d7 100644
--- a/winsup/cygwin/glob.cc
+++ b/winsup/cygwin/glob.cc
@@ -1024,7 +1024,7 @@ g_Ctoc(const Char *str, char *buf, size_t len)
 
 	memset(&mbs, 0, sizeof(mbs));
 	while (len >= (size_t) MB_CUR_MAX) {
-		clen = wcrtomb(buf, *str, &mbs);
+		clen = wirtomb(buf, *str, &mbs);
 		if (clen == (size_t)-1)
 			return (1);
 		if (*str == L'\0')
diff --git a/winsup/cygwin/local_includes/wchar.h b/winsup/cygwin/local_includes/wchar.h
index 6f2a4ad10f20..3907732a260f 100644
--- a/winsup/cygwin/local_includes/wchar.h
+++ b/winsup/cygwin/local_includes/wchar.h
@@ -44,6 +44,10 @@ extern wctomb_f __utf8_wctomb;
    for surrogate pairs, plus a wchar_t NUL. */
 void wcintowcs (wchar_t *, wint_t *, size_t);
 
+/* replacement function for wcrtomb, converting a UTF-32 char to a
+   multibyte string. */
+size_t wirtomb (char *, wint_t, mbstate_t *);
+
 /* replacement function for mbrtowc, returning a wint_t representing
    a UTF-32 value. Defined in strfuncs.cc */
 extern size_t mbrtowi (wint_t *, const char *, size_t, mbstate_t *);
diff --git a/winsup/cygwin/strfuncs.cc b/winsup/cygwin/strfuncs.cc
index e343a2fcc6e8..80e3eb0ad6a8 100644
--- a/winsup/cygwin/strfuncs.cc
+++ b/winsup/cygwin/strfuncs.cc
@@ -129,6 +129,25 @@ wcintowcs (wchar_t *dest, wint_t *src, size_t len)
   *dest = '\0';
 }
 
+/* replacement function for wcrtomb, converting a UTF-32 char to a
+   multibyte string. */
+extern "C" size_t
+wirtomb (char *s, wint_t wi, mbstate_t *ps)
+{
+    wchar_t wc[3] = { (wchar_t) wi, '\0', '\0' };
+    const wchar_t *wcp = wc;
+    size_t nwc = 1;
+
+    if (wi >= 0x10000)
+      {
+	wi -= 0x10000;
+	wc[0] = (wi >> 10) + 0xd800;
+	wc[1] = (wi & 0x3ff) + 0xdc00;
+	nwc = 2;
+      }
+    return wcsnrtombs (s, &wcp, nwc, SIZE_MAX, ps);
+}
+
 /* replacement function for mbrtowc, returning a wint_t representing
    a UTF-32 value. */
 extern "C" size_t

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-02-22 11:21 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-22 11:21 [newlib-cygwin/main] Cygwin: glob: perform correct utf-32 -> multibyte conversion Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).