public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/main] Cygwin: glob: perform correct utf-32 -> multibyte conversion
Date: Wed, 22 Feb 2023 11:21:14 +0000 (GMT)	[thread overview]
Message-ID: <20230222112114.AD4DB3858C30@sourceware.org> (raw)

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

                 reply	other threads:[~2023-02-22 11:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230222112114.AD4DB3858C30@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).