• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

The MinGW.org Windows System Libraries


Commit MetaInfo

修订版cc40bc6c69615bc8b936d966f457d31bbc9cabc8 (tree)
时间2020-03-04 06:58:11
作者Keith Marshall <keith@user...>
CommiterKeith Marshall

Log Message

Reimplement btowc(), and wctob() functions.

更改概述

差异

--- a/mingwrt/ChangeLog
+++ b/mingwrt/ChangeLog
@@ -1,3 +1,25 @@
1+2020-03-03 Keith Marshall <keith@users.osdn.me>
2+
3+ Reimplement btowc(), and wctob() functions.
4+
5+ * include/wchar.h [__MSVCRT_VERSION__ < __MSVCR80_DLL]
6+ (btowc, wctob): Implement them as static inline redirects...
7+ (__msvcrt_btowc, __msvcrt_wctob): ...to these; declare them, with
8+ additional MinGW specific alternatives declared...
9+ (__mingw_btowc, __mingw_wctob): ...thus.
10+
11+ * mingwex/btowc.c: Rewrite as new; it now implements...
12+ (__mingw_btowc, __msvcrt_btowc): ...these public API functions, either
13+ of which serves as a suitable replacement entry point for...
14+ (btowc): ...this, with ultimate fallback served by...
15+ (__mingw_btowc_fallback): ...this private function.
16+
17+ * mingwex/wctob.c: Rewrite as new; it now implements...
18+ (__mingw_wctob, __msvcrt_wctob): ...these public API functions, either
19+ of which serves as a suitable replacement entry point for...
20+ (wctob): ...this, with ultimate fallback served by...
21+ (__mingw_wctob_fallback): ...this private function.
22+
123 2020-03-02 Keith Marshall <keith@users.osdn.me>
224
325 Reimplement mbrlen(), mbrtowc(), and mbsrtowcs() functions.
--- a/mingwrt/include/wchar.h
+++ b/mingwrt/include/wchar.h
@@ -560,6 +560,8 @@ __cdecl __MINGW_NOTHROW size_t wcsrtombs
560560 * it exists in the process address space; otherwise, execution
561561 * will fall back to a MinGW implementation...
562562 */
563+__cdecl __MINGW_NOTHROW wint_t __msvcrt_btowc (int);
564+
563565 __cdecl __MINGW_NOTHROW size_t __msvcrt_mbrlen
564566 (const char *__restrict__, size_t, mbstate_t *__restrict__);
565567
@@ -569,6 +571,8 @@ __cdecl __MINGW_NOTHROW size_t __msvcrt_mbrtowc
569571 __cdecl __MINGW_NOTHROW size_t __msvcrt_mbsrtowcs
570572 (wchar_t *__restrict__, const char **__restrict__, size_t, mbstate_t *__restrict__);
571573
574+__cdecl __MINGW_NOTHROW int __msvcrt_wctob (wint_t);
575+
572576 __cdecl __MINGW_NOTHROW size_t __msvcrt_wcrtomb
573577 (char * __restrict__, wchar_t, mbstate_t *__restrict__);
574578
@@ -579,6 +583,8 @@ __cdecl __MINGW_NOTHROW size_t __msvcrt_wcsrtombs
579583 * fall back implementations, without considering any possible
580584 * reference to MSVCRT.DLL or MSVCR80.DLL implementations.
581585 */
586+__cdecl __MINGW_NOTHROW wint_t __mingw_btowc (int);
587+
582588 __cdecl __MINGW_NOTHROW size_t __mingw_mbrlen
583589 (const char *__restrict__, size_t, mbstate_t *__restrict__);
584590
@@ -588,6 +594,8 @@ __cdecl __MINGW_NOTHROW size_t __mingw_mbrtowc
588594 __cdecl __MINGW_NOTHROW size_t __mingw_mbsrtowcs
589595 (wchar_t *__restrict__, const char **__restrict__, size_t, mbstate_t *__restrict__);
590596
597+__cdecl __MINGW_NOTHROW int __mingw_wctob (wint_t);
598+
591599 __cdecl __MINGW_NOTHROW size_t __mingw_wcrtomb
592600 (char * __restrict__, wchar_t, mbstate_t *__restrict__);
593601
@@ -603,6 +611,9 @@ __cdecl __MINGW_NOTHROW size_t __mingw_wcsrtombs
603611 * the libmingwex.a implementations, (which will delegate the calls
604612 * to the Microsoft DLL implementations, when they are available).
605613 */
614+__CRT_ALIAS __cdecl __MINGW_NOTHROW wint_t btowc (int __c)
615+{ return __msvcrt_btowc( __c ); }
616+
606617 __CRT_ALIAS __cdecl __MINGW_NOTHROW size_t mbrlen
607618 (const char *__mbc, size_t __n, mbstate_t *__ps)
608619 { return __msvcrt_mbrlen( __mbc, __n, __ps ); }
@@ -615,6 +626,9 @@ __CRT_ALIAS __cdecl __MINGW_NOTHROW size_t mbsrtowcs
615626 (wchar_t *__wcs, const char **__mbs, size_t __n, mbstate_t *__ps)
616627 { return __msvcrt_mbsrtowcs( __wcs, __mbs, __n, __ps ); }
617628
629+__CRT_ALIAS __cdecl __MINGW_NOTHROW int wctob (wint_t __wc)
630+{ return __msvcrt_wctob( __wc ); }
631+
618632 __CRT_ALIAS __cdecl __MINGW_NOTHROW size_t wcrtomb
619633 (char * __mbc, wchar_t __wc, mbstate_t *__ps)
620634 { return __msvcrt_wcrtomb(__mbc, __wc, __ps); }
--- a/mingwrt/mingwex/btowc.c
+++ b/mingwrt/mingwex/btowc.c
@@ -1,19 +1,120 @@
1-#include "mb_wc_common.h"
2-#include <wchar.h>
1+/*
2+ * btowc.c
3+ *
4+ * Implementation of an ISO-C99 conforming btowc() function; note that,
5+ * since this considers only one byte for conversion, and a single byte
6+ * can never convert to a surrogate pair, this is not susceptible to the
7+ * potential wchar_t overflow error, which may occur with functions such
8+ * as mbrtowc(), which may need to return surrogate pairs.
9+ *
10+ * $Id$
11+ *
12+ * Written by Keith Marshall <keith@users.osdn.me>
13+ * Copyright (C) 2020, MinGW.org Project
14+ *
15+ *
16+ * Permission is hereby granted, free of charge, to any person obtaining a
17+ * copy of this software and associated documentation files (the "Software"),
18+ * to deal in the Software without restriction, including without limitation
19+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20+ * and/or sell copies of the Software, and to permit persons to whom the
21+ * Software is furnished to do so, subject to the following conditions:
22+ *
23+ * The above copyright notice, this permission notice, and the following
24+ * disclaimer shall be included in all copies or substantial portions of
25+ * the Software.
26+ *
27+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
33+ * DEALINGS IN THE SOFTWARE.
34+ *
35+ */
36+#include "wcharmap.h"
37+
38+/* For runtime delegation, we need a mechanism for detection of an
39+ * implementation, within the default C runtime DLL; we may use the
40+ * MinGW dlfcn emulation, to facilitate this.
41+ */
42+#include <dlfcn.h>
43+
44+/* We also need <limits.h>, for UCHAR_MAX, and <stdio.h>, for EOF.
45+ */
46+#include <limits.h>
347 #include <stdio.h>
4-#define WIN32_LEAN_AND_MEAN
5-#include <windows.h>
648
7-wint_t btowc (int c)
49+/* We need to look up the effective working codeset, before choosing
50+ * between MSVCRT.DLL and MinGW fallback implementations; to avoid a
51+ * need to look it up again, within the MinGW fallback, we store the
52+ * result of the initial look up in this file-global variable.
53+ */
54+static __thread unsigned int cs;
55+
56+static wint_t __mingw_btowc_fallback( int c )
57+{
58+ /* Fallback function, providing an implementation of the btowc()
59+ * function, when none is available within the Microsoft runtime.
60+ * This performs an MBCS to wchar_t conversion on the given single
61+ * character argument, (expressed as an int), returning WEOF in
62+ * the event that conversion fails.
63+ */
64+ wint_t wc = WEOF;
65+
66+ if( c != EOF )
67+ { if( (cs == 0) && (UCHAR_MAX >= (unsigned int)(c)) ) return (wchar_t)(c);
68+ MultiByteToWideChar( cs, MB_ERR_INVALID_CHARS, (char *)(&c), 1, &wc, 1 );
69+ }
70+ return wc;
71+}
72+
73+wint_t __mingw_btowc( int c )
874 {
9- if (c == EOF)
10- return (WEOF);
11- else
12- {
13- unsigned char ch = c;
14- wchar_t wc = WEOF;
15- MultiByteToWideChar (get_codepage(), MB_ERR_INVALID_CHARS,
16- (char*)&ch, 1, &wc, 1);
17- return wc;
18- }
75+ /* Wrapper for the btowc() function; this will unconditionally
76+ * delegate the call to the MinGW fallback implementation, (as
77+ * implemented above), after initialization of the effective
78+ * codeset file-global variable.
79+ */
80+ cs = __mb_codeset_for_locale();
81+ return __mingw_btowc_fallback( c );
1982 }
83+
84+wint_t __msvcrt_btowc( int c )
85+{
86+ /* Wrapper for the btowc() function; it will initially attempt
87+ * to delegate the call to a Microsoft-provided implementation,
88+ * but if no such implementation can be found, fall back to the
89+ * MinGW substitute (defined above).
90+ */
91+ static wint_t (*redirector_hook)( int ) = NULL;
92+
93+ /* MSVCRT.DLL's setlocale() cannot reliably handle code pages with
94+ * more than two bytes per code point, (e.g. UTF-7 and UTF-8); thus,
95+ * Microsoft's btowc() is likely to be similarly unreliable, so we
96+ * always use the MinGW fallback with such code pages.
97+ */
98+ if( __mb_cur_max_for_codeset( cs = __mb_codeset_for_locale() ) > 2 )
99+ return __mingw_btowc_fallback( c );
100+
101+ /* On first time call, we don't know which implementation is to be
102+ * selected; look for a Microsoft implementation, which, if available,
103+ * may be registered for immediate use on this, and any subsequent,
104+ * calls to this function wrapper...
105+ */
106+ if( (redirector_hook == NULL)
107+ && ((redirector_hook = dlsym( RTLD_DEFAULT, "btowc" )) == NULL) )
108+
109+ /* ...but when no Microsoft implementation can be found, register
110+ * the MinGW fall back in its stead.
111+ */
112+ redirector_hook = __mingw_btowc_fallback;
113+
114+ /* Finally, delegate the call to whichever implementation has been
115+ * registered on first-time call.
116+ */
117+ return redirector_hook( c );
118+}
119+
120+/* $RCSfile$: end of file */
--- a/mingwrt/mingwex/wctob.c
+++ b/mingwrt/mingwex/wctob.c
@@ -1,21 +1,103 @@
1-#include "mb_wc_common.h"
2-#include <wchar.h>
1+/*
2+ * wctob.c
3+ *
4+ * Implementation of ISO-C99 wctob() function, supporting it on legacy
5+ * Windows versions, for which MSVCRT.DLL doesn't provide it, otherwise
6+ * delegating to the Microsoft implementation, except in specific cases
7+ * when that implementation may not support the active MBCS codeset.
8+ *
9+ *
10+ * $Id$
11+ *
12+ * Written by Keith Marshall <keith@users.osdn.me>
13+ * Copyright (C) 2020, MinGW.org Project
14+ *
15+ *
16+ * Permission is hereby granted, free of charge, to any person obtaining a
17+ * copy of this software and associated documentation files (the "Software"),
18+ * to deal in the Software without restriction, including without limitation
19+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20+ * and/or sell copies of the Software, and to permit persons to whom the
21+ * Software is furnished to do so, subject to the following conditions:
22+ *
23+ * The above copyright notice, this permission notice, and the following
24+ * disclaimer shall be included in all copies or substantial portions of
25+ * the Software.
26+ *
27+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
33+ * DEALINGS IN THE SOFTWARE.
34+ *
35+ */
36+#include "wcharmap.h"
37+
38+/* For runtime delegation, we need a mechanism for detection of an
39+ * implementation, within the default C runtime DLL; we may use the
40+ * MinGW dlfcn emulation, to facilitate this.
41+ */
42+#include <dlfcn.h>
343 #include <stdio.h>
4-#include <stdlib.h>
5-#include <errno.h>
6-#define WIN32_LEAN_AND_MEAN
7-#include <windows.h>
8-
9-/* Return just the first byte after translating to multibyte. */
10-int wctob (wint_t wc )
11-{
12- wchar_t w = wc;
13- char c;
14- int invalid_char = 0;
15- if (!WideCharToMultiByte (get_codepage(),
16- 0 /* Is this correct flag? */,
17- &w, 1, &c, 1, NULL, &invalid_char)
18- || invalid_char)
19- return EOF;
20- return (int) c;
44+
45+static int __mingw_wctob_fallback( wint_t wc )
46+{ /* Fallback function, providing an implementation of the wctob()
47+ * function, when none is available within the Microsoft runtime.
48+ * This performs a wchar_t to MBCS conversion on the given single
49+ * wide character argument, capturing the conversion into a local
50+ * buffer, checks that the result occupies exactly one byte, for
51+ * which the coercion of that byte value to int is returned, or
52+ * otherwise returns EOF.
53+ */
54+ union { unsigned char u; char c; } retval;
55+ return (__mingw_wctomb_convert( &retval.c, 1, &wc, 1 ) == 1)
56+ ? (int)(retval.u) : EOF;
57+}
58+
59+int __mingw_wctob( wint_t wc )
60+{ /* Wrapper for the wctob() function; this variant will unconditionally
61+ * delegate the call to the MinGW fallback implementation, after first
62+ * storing the effective codeset index.
63+ */
64+ (void)(__mingw_wctomb_codeset_init());
65+ return __mingw_wctob_fallback( wc );
2166 }
67+
68+int __msvcrt_wctob( wint_t wc )
69+{ /* Wrapper for the wctob() function; it will initially attempt
70+ * to delegate the call to a Microsoft-provided implementation,
71+ * but if no such implementation can be found, fall back to the
72+ * MinGW substitute (defined above).
73+ */
74+ static int (*redirector_hook)( wchar_t ) = NULL;
75+
76+ /* MSVCRT.DLL's setlocale() cannot reliably handle code pages with
77+ * more than two bytes per code point, (e.g. UTF-7 and UTF-8); thus,
78+ * Microsoft's wctob() is likely to be similarly unreliable, so we
79+ * always use the MinGW fallback with such code pages.
80+ */
81+ if( __mingw_wctomb_cur_max_init(__mingw_wctomb_codeset_init()) > 2 )
82+ return __mingw_wctob_fallback( wc );
83+
84+ /* On first time call, we don't know which implementation is to be
85+ * selected; look for a Microsoft implementation, which, if available,
86+ * may be registered for immediate use on this, and any subsequent,
87+ * calls to this function wrapper...
88+ */
89+ if( (redirector_hook == NULL)
90+ && ((redirector_hook = dlsym( RTLD_DEFAULT, "wctob" )) == NULL) )
91+
92+ /* ...but when no Microsoft implementation can be found, register
93+ * the MinGW fall back in its stead.
94+ */
95+ redirector_hook = __mingw_wctob_fallback;
96+
97+ /* Finally, delegate the call to whichever implementation has been
98+ * registered on first-time call.
99+ */
100+ return redirector_hook( wc );
101+}
102+
103+/* $RCSfile$: end of file */