-- Best regards, LIU Hao
From 544a196787132e0ceae01fe2f0f6c62e76da0e05 Mon Sep 17 00:00:00 2001 From: LIU Hao <[email protected]> Date: Fri, 24 Mar 2023 16:38:51 +0800 Subject: [PATCH] crt: Remove conversion functions between UTF-{16,32} characters from MSVCRT
Microsoft documentation says these functions operate between UTF-8 and
UTF-{16,32}. However, the function `mbrtoc16()` delegates to `mbrtowc()`
which handles only DBCS encodings; the legacy MSVCRT does not have UTF-8
support. `mbrtoc32()` on the other hand igores the `mbstate_t` argument
and is non-restartable. So neither conforms to the C standard.
This commit removes those incorrect functions from MSVCRT. In order to
get UTF-8 support, users should link against UCRT.
Reference: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mbrtoc16-mbrtoc323?view=msvc-170
Signed-off-by: LIU Hao <[email protected]>
---
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 -----------------------------
mingw-w64-headers/crt/uchar.h | 5 +-
6 files changed, 4 insertions(+), 201 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;
-}
-
diff --git a/mingw-w64-headers/crt/uchar.h b/mingw-w64-headers/crt/uchar.h
index 019f2694f..fbe1016fa 100644
--- a/mingw-w64-headers/crt/uchar.h
+++ b/mingw-w64-headers/crt/uchar.h
@@ -35,6 +35,8 @@ typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
#endif
+#ifdef _UCRT
+
#ifndef __STDC_UTF_16__
#define __STDC_UTF_16__ 1
#endif
@@ -64,10 +66,11 @@ size_t mbrtoc32 (char32_t *__restrict__ pc32,
size_t c32rtomb (char *__restrict__ s,
char32_t c32,
mbstate_t *__restrict__ ps);
-
#ifdef __cplusplus
}
#endif
+#endif /* _UCRT */
+
#endif /* __UCHAR_H */
--
2.34.1
OpenPGP_signature
Description: OpenPGP digital signature
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
