--- lib/toutf8.c~0	2014-08-10 22:21:41 +0300
+++ lib/toutf8.c	2014-12-11 10:31:39 +0200
@@ -58,6 +58,71 @@
 # include <langinfo.h>
 #endif
 
+#if defined _WIN32 && !defined __CYGWIN__
+
+# define WIN32_LEAN_AND_MEAN  /* avoid including junk */
+# include <windows.h>
+
+/* Return the codeset of the current locale, if this is easily deducible.
+   Otherwise, return "".  */
+static char *
+ctype_codeset (void)
+{
+  static char buf[2 + 10 + 1];
+  size_t buflen = 0;
+  char const *locale = setlocale (LC_CTYPE, NULL);
+
+  if (locale && locale[0])
+    {
+      /* If the locale name contains an encoding after the dot, return it.  */
+      char *dot = strchr (locale, '.');
+
+      if (dot)
+        {
+          /* Look for the possible @... trailer and remove it, if any.  */
+          char const *modifier = strchr (++dot, '@');
+
+          if (! modifier)
+            return dot;
+          if (modifier - dot < sizeof buf)
+            {
+              buflen = modifier - dot;
+              memcpy (buf, dot, buflen);
+            }
+        }
+    }
+
+  buf[buflen] = '\0';
+  return buf;
+}
+
+static char *
+w32_charset (void)
+{
+  static char nlbuf[100];
+  char *codeset = ctype_codeset ();
+
+  if (*codeset)
+    {
+      /* On MS-Windows, ctype_codeset returns the number of the
+	 codepage, as a string.  We need to prepend the "CP" prefix to
+	 make it a valid codeset name.  */
+      sprintf (nlbuf, "CP%s", codeset);
+    }
+  else
+    {
+      /* The Windows API has a function returning the locale's
+	 codepage as a number, but the value doesn't change according
+	 to what the 'setlocale' call specified.  So use it as a last
+	 resort, in case the string returned by 'setlocale' doesn't
+	 specify the codepage.  */
+      sprintf (nlbuf, "CP%u", GetACP ());
+    }
+  return nlbuf;
+}
+
+#endif	/* _WIN32 && !__CYGWIN__ */
+
 #ifdef _LIBC
 # define stringprep_locale_charset() nl_langinfo (CODESET)
 #else
@@ -95,6 +160,12 @@ stringprep_locale_charset (void)
   if (charset && *charset)
     return charset;
 # endif
+
+# if defined _WIN32 && !defined __CYGWIN__
+  charset = w32_charset ();
+  if (charset && *charset)
+    return charset;
+# endif
 
   return "ASCII";
 }
