ucrt: Use multibyte functions from ucrt instead of libmingwex

Use a different definition of the mbstate_t type for ucrt, and
move related functions from libmingwex.a into libmsvcr*.a.

A few stdio functions in libmingwex use these functions; add an
override to them to, to allocate the larger form of the mbstate_t
struct, to make it work when combined with ucrt.

Signed-off-by: Martin Storsjö <martin@martin.st>
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 55e3624..8707b11 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -158,8 +158,15 @@
   libsrc/wspiapi/WspiapiFreeAddrInfo.c
 
 src_msvcrt_common=\
+  misc/mbrtowc.c \
+  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/acrt_iob_func.c \
   math/frexp.c
 
@@ -424,13 +431,13 @@
   misc/feraiseexcept.c   misc/fesetenv.c            misc/fesetexceptflag.c  misc/fesetround.c            misc/fetestexcept.c    \
   misc/feupdateenv.c     misc/ftruncate.c           misc/fwide.c            misc/getlogin.c              misc/getopt.c          \
   misc/gettimeofday.c    misc/imaxabs.c             misc/imaxdiv.c          misc/isblank.c               misc/iswblank.c        \
-  misc/mbrtowc.c         misc/mbsinit.c             misc/mempcpy.c          misc/mingw-aligned-malloc.c  misc/mingw-fseek.c     \
+  misc/mempcpy.c         misc/mingw-aligned-malloc.c misc/mingw-fseek.c     \
   misc/mingw_matherr.c   misc/mingw_mbwc_convert.c  misc/mingw_usleep.c     misc/mingw_wcstod.c          misc/mingw_wcstof.c    \
   misc/mingw_wcstold.c   misc/mkstemp.c             misc/seterrno.c         misc/sleep.c          \
   misc/strnlen.c         misc/strsafe.c         \
   misc/strtoimax.c       misc/strtold.c             misc/strtoumax.c        misc/tdelete.c               misc/tfind.c           \
-  misc/tsearch.c         misc/twalk.c               misc/uchar_c16rtomb.c   misc/uchar_c32rtomb.c        misc/uchar_mbrtoc16.c  \
-  misc/uchar_mbrtoc32.c  misc/wcrtomb.c             misc/wcsnlen.c          misc/wcstof.c          \
+  misc/tsearch.c         misc/twalk.c           \
+  misc/wcsnlen.c         misc/wcstof.c          \
   misc/wcstoimax.c       misc/wcstold.c             misc/wcstoumax.c        misc/wctob.c                 misc/wctrans.c         \
   misc/wctype.c          misc/wdirent.c             misc/winbs_uint64.c     misc/winbs_ulong.c           misc/winbs_ushort.c    \
   misc/wmemchr.c         misc/wmemcmp.c             misc/wmemcpy.c          misc/wmemmove.c              misc/wmempcpy.c        \
diff --git a/mingw-w64-crt/stdio/mingw_pformat.c b/mingw-w64-crt/stdio/mingw_pformat.c
index 7e24932..a68d0f6 100644
--- a/mingw-w64-crt/stdio/mingw_pformat.c
+++ b/mingw-w64-crt/stdio/mingw_pformat.c
@@ -51,6 +51,8 @@
  *
  */
 
+#define __LARGE_MBSTATE_T
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
diff --git a/mingw-w64-crt/stdio/mingw_vfscanf.c b/mingw-w64-crt/stdio/mingw_vfscanf.c
index 4bd9e91..6f2d5eb 100644
--- a/mingw-w64-crt/stdio/mingw_vfscanf.c
+++ b/mingw-w64-crt/stdio/mingw_vfscanf.c
@@ -42,6 +42,8 @@
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#define __LARGE_MBSTATE_T
+
 #include <limits.h>
 #include <stddef.h>
 #include <stdarg.h>
diff --git a/mingw-w64-crt/stdio/mingw_wvfscanf.c b/mingw-w64-crt/stdio/mingw_wvfscanf.c
index 07c9587..2f0a03f 100644
--- a/mingw-w64-crt/stdio/mingw_wvfscanf.c
+++ b/mingw-w64-crt/stdio/mingw_wvfscanf.c
@@ -42,6 +42,8 @@
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#define __LARGE_MBSTATE_T
+
 #include <limits.h>
 #include <stddef.h>
 #include <stdarg.h>
diff --git a/mingw-w64-headers/crt/wchar.h b/mingw-w64-headers/crt/wchar.h
index d1348bc..eaaadec 100644
--- a/mingw-w64-headers/crt/wchar.h
+++ b/mingw-w64-headers/crt/wchar.h
@@ -1414,7 +1414,15 @@
 
 #endif
 
+#if defined(_UCRT) || defined(__LARGE_MBSTATE_T)
+  typedef struct _Mbstatet {
+    unsigned long _Wchar;
+    unsigned short _Byte, _State;
+  } _Mbstatet;
+  typedef _Mbstatet mbstate_t;
+#else
   typedef int mbstate_t;
+#endif
   typedef wchar_t _Wint_t;
 
   wint_t __cdecl btowc(int);
@@ -1433,7 +1441,12 @@
   wchar_t * __cdecl wmempcpy (wchar_t *_Dst, const wchar_t *_Src, size_t _Size);
   wchar_t *__cdecl wmemmove(wchar_t *s1, const wchar_t *s2, size_t n) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
   int __cdecl fwide(FILE *stream,int mode);
+#if defined(_UCRT) || defined(__LARGE_MBSTATE_T)
+  /* With UCRT, mbsinit is only available as inline. */
+  __mingw_static_ovr int __cdecl mbsinit(const mbstate_t *_P) { return (!_P || _P->_Wchar == 0); }
+#else
   int __cdecl mbsinit(const mbstate_t *ps);
+#endif
   __MINGW_EXTENSION long long __cdecl wcstoll(const wchar_t * __restrict__ nptr,wchar_t ** __restrict__ endptr, int base);
   __MINGW_EXTENSION unsigned long long __cdecl wcstoull(const wchar_t * __restrict__ nptr,wchar_t ** __restrict__ endptr, int base);
 #endif /* __NO_ISOCEXT */
@@ -1442,7 +1455,9 @@
   void *__cdecl memcpy(void * __restrict__ _Dst,const void * __restrict__ _Src,size_t _MaxCount) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
 #ifndef __CRT__NO_INLINE
   __CRT_INLINE int __cdecl fwide(FILE *_F,int _M) { (void)_F; return (_M); }
+#if !defined(_UCRT) && !defined(__LARGE_MBSTATE_T)
   __CRT_INLINE int __cdecl mbsinit(const mbstate_t *_P) { return (!_P || *_P==0); }
+#endif
   __CRT_INLINE _CONST_RETURN wchar_t *__cdecl wmemchr(const wchar_t *_S,wchar_t _C,size_t _N) {
     if (_S) {
       for ( ; 0 < _N; ++_S, --_N)
diff --git a/mingw-w64-headers/crt/yvals.h b/mingw-w64-headers/crt/yvals.h
index 15130e4..ec929c7 100644
--- a/mingw-w64-headers/crt/yvals.h
+++ b/mingw-w64-headers/crt/yvals.h
@@ -261,7 +261,9 @@
 _C_STD_BEGIN
 _CRTIMP void __cdecl _Atexit(void (__cdecl *)(void));
 
+#if !defined(_UCRT) && !defined(__LARGE_MBSTATE_T)
 typedef int _Mbstatet;
+#endif
 
 #define _ATEXIT_T void
 #define _Mbstinit(x) mbstate_t x = {0}