Basing on some discussion on IRC, I have spit this into two patches. The actual removal of these functions will be postponed after this release.

--
Best regards,
LIU Hao
From cb01a7df771e7a48d3246cc772674c470a92ff3f Mon Sep 17 00:00:00 2001
From: LIU Hao <lh_mo...@126.com>
Date: Thu, 27 Apr 2023 22:35:22 +0800
Subject: [PATCH 1/2] headers: Hide UTF-16 and UTF-32 functions from libmsvcrt

While these are standard C11 functions, Microsoft docs say they operate
on strings in UTF-8, instead of strings in the encoding determined by the
current locale. The MSVCRT `mbstate_t`, being an `int`, is too small for
implementations of these functions.

This commit ensures these functions are only declared for UCRT; when
targeting MSVCRT only the typedefs are available. Those incorrect
implementations will be removed in the future.

Reference: 
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/c16rtomb-c32rtomb1?view=msvc-170
Signed-off-by: LIU Hao <lh_mo...@126.com>
---
 mingw-w64-headers/crt/uchar.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mingw-w64-headers/crt/uchar.h b/mingw-w64-headers/crt/uchar.h
index 019f2694f..7c32e7025 100644
--- a/mingw-w64-headers/crt/uchar.h
+++ b/mingw-w64-headers/crt/uchar.h
@@ -47,6 +47,8 @@ typedef uint_least32_t char32_t;
 extern "C" {
 #endif
 
+#ifdef _UCRT
+
 size_t mbrtoc16 (char16_t *__restrict__ pc16,
                 const char *__restrict__ s,
                 size_t n,
@@ -65,6 +67,8 @@ size_t c32rtomb (char *__restrict__ s,
                 char32_t c32,
                 mbstate_t *__restrict__ ps);
 
+#endif  /* _UCRT */
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.40.1

From 84dded03b548786ac65acec873ee36d26133dd20 Mon Sep 17 00:00:00 2001
From: LIU Hao <lh_mo...@126.com>
Date: Thu, 27 Apr 2023 22:45:45 +0800
Subject: [PATCH 2/2] crt: Remove incorrect implementations of UTF-16 and
 UTF-32 functions

As described in cb01a7df771e7a48d3246cc772674c470a92ff3f.

Signed-off-by: LIU Hao <lh_mo...@126.com>
---
 mingw-w64-crt/Makefile.am           |  4 --
 mingw-w64-crt/misc/uchar_c16rtomb.c | 32 -------------
 mingw-w64-crt/misc/uchar_c32rtomb.c | 59 -----------------------
 mingw-w64-crt/misc/uchar_mbrtoc16.c | 33 -------------
 mingw-w64-crt/misc/uchar_mbrtoc32.c | 72 -----------------------------
 5 files changed, 200 deletions(-)
 delete mode 100644 mingw-w64-crt/misc/uchar_c16rtomb.c
 delete mode 100644 mingw-w64-crt/misc/uchar_c32rtomb.c
 delete mode 100644 mingw-w64-crt/misc/uchar_mbrtoc16.c
 delete mode 100644 mingw-w64-crt/misc/uchar_mbrtoc32.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 3cf7203e9..0454ecec3 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -174,10 +174,6 @@ src_msvcrt_common=\
   misc/mbsinit.c \
   misc/onexit_table.c \
   misc/register_tls_atexit.c \
-  misc/uchar_c16rtomb.c \
-  misc/uchar_c32rtomb.c \
-  misc/uchar_mbrtoc16.c \
-  misc/uchar_mbrtoc32.c \
   misc/wcrtomb.c \
   stdio/_getc_nolock.c \
   stdio/_getwc_nolock.c \
diff --git a/mingw-w64-crt/misc/uchar_c16rtomb.c 
b/mingw-w64-crt/misc/uchar_c16rtomb.c
deleted file mode 100644
index 94a2aaad0..000000000
--- a/mingw-w64-crt/misc/uchar_c16rtomb.c
+++ /dev/null
@@ -1,32 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-/* ISO C1x Unicode utilities
- * Based on ISO/IEC SC22/WG14 9899 TR 19769 (SC22 N1326)
- *
- *  THIS SOFTWARE IS NOT COPYRIGHTED
- *
- *  This source code is offered for use in the public domain. You may
- *  use, modify or distribute it freely.
- *
- *  This code is distributed in the hope that it will be useful but
- *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
- *  DISCLAIMED. This includes but is not limited to warranties of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- *  Date: 2011-09-27
- */
-
-#include <errno.h>
-#include <uchar.h>
-
-size_t c16rtomb (char *__restrict__ s,
-                char16_t c16,
-                mbstate_t *__restrict__ state)
-{
-/* wchar_t should compatible to char16_t on Windows */
-    return wcrtomb(s, c16, state);
-}
-
diff --git a/mingw-w64-crt/misc/uchar_c32rtomb.c 
b/mingw-w64-crt/misc/uchar_c32rtomb.c
deleted file mode 100644
index d05e6326f..000000000
--- a/mingw-w64-crt/misc/uchar_c32rtomb.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-/* ISO C1x Unicode utilities
- * Based on ISO/IEC SC22/WG14 9899 TR 19769 (SC22 N1326)
- *
- *  THIS SOFTWARE IS NOT COPYRIGHTED
- *
- *  This source code is offered for use in the public domain. You may
- *  use, modify or distribute it freely.
- *
- *  This code is distributed in the hope that it will be useful but
- *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
- *  DISCLAIMED. This includes but is not limited to warranties of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- *  Date: 2011-09-27
- */
-
-#include <errno.h>
-#include <uchar.h>
-
-size_t c32rtomb (char *__restrict__ s,
-                char32_t c32,
-                mbstate_t *__restrict__ __UNUSED_PARAM(ps))
-{
-    if (c32 <= 0x7F) /* 7 bits needs 1 byte */
-    {
-       *s = (char)c32 & 0x7F;
-       return 1;
-    }
-    else if (c32 <= 0x7FF) /* 11 bits needs 2 bytes */
-    {
-       s[1] = 0x80 | (char)(c32 & 0x3F);
-       s[0] = 0xC0 | (char)(c32 >> 6);
-       return 2;
-    }
-    else if (c32 <= 0xFFFF) /* 16 bits needs 3 bytes */
-    {
-       s[2] = 0x80 | (char)(c32 & 0x3F);
-       s[1] = 0x80 | (char)((c32 >> 6) & 0x3F);
-       s[0] = 0xE0 | (char)(c32 >> 12);
-       return 3;
-    }
-    else if (c32 <= 0x1FFFFF) /* 21 bits needs 4 bytes */
-    {
-       s[3] = 0x80 | (char)(c32 & 0x3F);
-       s[2] = 0x80 | (char)((c32 >> 6) & 0x3F);
-       s[1] = 0x80 | (char)((c32 >> 12) & 0x3F);
-       s[0] = 0xF0 | (char)(c32 >> 18);
-       return 4;
-    }
-
-    errno = EILSEQ;
-    return (size_t)-1;
-}
-
diff --git a/mingw-w64-crt/misc/uchar_mbrtoc16.c 
b/mingw-w64-crt/misc/uchar_mbrtoc16.c
deleted file mode 100644
index 9de35fe07..000000000
--- a/mingw-w64-crt/misc/uchar_mbrtoc16.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-/* ISO C1x Unicode utilities
- * Based on ISO/IEC SC22/WG14 9899 TR 19769 (SC22 N1326)
- *
- *  THIS SOFTWARE IS NOT COPYRIGHTED
- *
- *  This source code is offered for use in the public domain. You may
- *  use, modify or distribute it freely.
- *
- *  This code is distributed in the hope that it will be useful but
- *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
- *  DISCLAIMED. This includes but is not limited to warranties of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- *  Date: 2011-09-27
- */
-
-#include <errno.h>
-#include <uchar.h>
-
-size_t mbrtoc16 (char16_t *__restrict__ pc16,
-                const char *__restrict__ s,
-                size_t n,
-                mbstate_t *__restrict__ state)
-{
-/* wchar_t should compatible to char16_t on Windows */
-    return mbrtowc((wchar_t *)pc16, s, n, state);
-}
-
diff --git a/mingw-w64-crt/misc/uchar_mbrtoc32.c 
b/mingw-w64-crt/misc/uchar_mbrtoc32.c
deleted file mode 100644
index 825fc5268..000000000
--- a/mingw-w64-crt/misc/uchar_mbrtoc32.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-/* ISO C1x Unicode utilities
- * Based on ISO/IEC SC22/WG14 9899 TR 19769 (SC22 N1326)
- *
- *  THIS SOFTWARE IS NOT COPYRIGHTED
- *
- *  This source code is offered for use in the public domain. You may
- *  use, modify or distribute it freely.
- *
- *  This code is distributed in the hope that it will be useful but
- *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
- *  DISCLAIMED. This includes but is not limited to warranties of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- *  Date: 2011-09-27
- */
-
-#include <errno.h>
-#include <uchar.h>
-
-size_t mbrtoc32 (char32_t *__restrict__ pc32,
-                const char *__restrict__ s,
-                size_t n,
-                mbstate_t *__restrict__ __UNUSED_PARAM(ps))
-{
-    if (*s == 0)
-    {
-       *pc32 = 0;
-       return 0;
-    }
-
-    /* ASCII character - high bit unset */
-    if ((*s & 0x80) == 0)
-    {
-       *pc32 = *s;
-       return 1;
-    }
-
-    /* Multibyte chars */
-    if ((*s & 0xE0) == 0xC0) /* 110xxxxx needs 2 bytes */
-    {
-       if (n < 2)
-           return (size_t)-2;
-
-       *pc32 = ((s[0] & 31) << 6) | (s[1] & 63);
-       return 2;
-    }
-    else if ((*s & 0xf0) == 0xE0) /* 1110xxxx needs 3 bytes */
-    {
-       if (n < 3)
-           return (size_t)-2;
-
-       *pc32 = ((s[0] & 15) << 12) | ((s[1] & 63) << 6) | (s[2] & 63);
-       return 3;
-    }
-    else if ((*s & 0xF8) == 0xF0) /* 11110xxx needs 4 bytes */
-    {
-       if (n < 4)
-           return (size_t)-2;
-
-       *pc32 = ((s[0] & 7) << 18) | ((s[1] & 63) << 12) | ((s[2] & 63) << 6) | 
(s[3] & 63);
-       return 4;
-    }
-
-    errno = EILSEQ;
-    return (size_t)-1;
-}
-
-- 
2.40.1

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to