Fix MultiByteToWideChar and WideCharToMultiByte when called with code
[wine] / dlls / kernel / locale.c
1 /*
2  * Locale support
3  *
4  * Copyright 1995 Martin von Loewis
5  * Copyright 1998 David Lee Lambert
6  * Copyright 2000 Julio César Gázquez
7  * Copyright 2002 Alexandre Julliard for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <assert.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33
34 #include "ntstatus.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winuser.h"  /* for RT_STRINGW */
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "wine/unicode.h"
41 #include "winnls.h"
42 #include "winerror.h"
43 #include "thread.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(nls);
47
48 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
49
50 static const WCHAR kernel32W[] = { 'K','E','R','N','E','L','3','2','\0' };
51
52 /* current code pages */
53 static const union cptable *ansi_cptable;
54 static const union cptable *oem_cptable;
55 static const union cptable *mac_cptable;
56 static const union cptable *unix_cptable;  /* NULL if UTF8 */
57
58 /* Charset to codepage map, sorted by name. */
59 static const struct charset_entry
60 {
61     const char *charset_name;
62     UINT        codepage;
63 } charset_names[] =
64 {
65     { "CP1250", 1250 },
66     { "CP1251", 1251 },
67     { "CP1252", 1252 },
68     { "CP1253", 1253 },
69     { "CP1254", 1254 },
70     { "CP1255", 1255 },
71     { "CP1256", 1256 },
72     { "CP1257", 1257 },
73     { "CP1258", 1258 },
74     { "EUCJP", 20932 },
75     { "IBM037", 37 },
76     { "IBM1026", 1026 },
77     { "IBM424", 424 },
78     { "IBM437", 437 },
79     { "IBM500", 500 },
80     { "IBM850", 850 },
81     { "IBM852", 852 },
82     { "IBM855", 855 },
83     { "IBM857", 857 },
84     { "IBM860", 860 },
85     { "IBM861", 861 },
86     { "IBM862", 862 },
87     { "IBM863", 863 },
88     { "IBM864", 864 },
89     { "IBM865", 865 },
90     { "IBM866", 866 },
91     { "IBM869", 869 },
92     { "IBM874", 874 },
93     { "IBM875", 875 },
94     { "ISO88591", 28591 },
95     { "ISO885910", 28600 },
96     { "ISO885913", 28603 },
97     { "ISO885914", 28604 },
98     { "ISO885915", 28605 },
99     { "ISO88592", 28592 },
100     { "ISO88593", 28593 },
101     { "ISO88594", 28594 },
102     { "ISO88595", 28595 },
103     { "ISO88596", 28596 },
104     { "ISO88597", 28597 },
105     { "ISO88598", 28598 },
106     { "ISO88599", 28599 },
107     { "KOI8R", 20866 },
108     { "KOI8U", 20866 },
109     { "UTF8", CP_UTF8 }
110 };
111
112 #define NLS_MAX_LANGUAGES 20
113 typedef struct {
114     WCHAR lang[128];
115     WCHAR country[4];
116     LANGID found_lang_id[NLS_MAX_LANGUAGES];
117     WCHAR found_language[NLS_MAX_LANGUAGES][3];
118     WCHAR found_country[NLS_MAX_LANGUAGES][3];
119     int n_found;
120 } LANG_FIND_DATA;
121
122
123 /* copy Unicode string to Ascii without using codepages */
124 static inline void strcpyWtoA( char *dst, const WCHAR *src )
125 {
126     while ((*dst++ = *src++));
127 }
128
129 /* Copy Ascii string to Unicode without using codepages */
130 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
131 {
132     while (n > 1 && *src)
133     {
134         *dst++ = (unsigned char)*src++;
135         n--;
136     }
137     if (n) *dst = 0;
138 }
139
140 /* return a printable string for a language id */
141 static const char *debugstr_lang( LANGID lang )
142 {
143     WCHAR langW[4], countryW[4];
144     char buffer[8];
145     LCID lcid = MAKELCID( lang, SORT_DEFAULT );
146
147     GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE, langW, sizeof(langW));
148     GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE, countryW, sizeof(countryW));
149     strcpyWtoA( buffer, langW );
150     strcat( buffer, "_" );
151     strcpyWtoA( buffer + strlen(buffer), countryW );
152     return wine_dbg_sprintf( "%s", buffer );
153 }
154
155 /***********************************************************************
156  *              get_lcid_codepage
157  *
158  * Retrieve the ANSI codepage for a given locale.
159  */
160 inline static UINT get_lcid_codepage( LCID lcid )
161 {
162     UINT ret;
163     if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
164                          sizeof(ret)/sizeof(WCHAR) )) ret = 0;
165     return ret;
166 }
167
168
169 /***********************************************************************
170  *              get_codepage_table
171  *
172  * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
173  */
174 static const union cptable *get_codepage_table( unsigned int codepage )
175 {
176     const union cptable *ret = NULL;
177
178     assert( ansi_cptable );  /* init must have been done already */
179
180     switch(codepage)
181     {
182     case CP_ACP:
183         return ansi_cptable;
184     case CP_OEMCP:
185         return oem_cptable;
186     case CP_MACCP:
187         return mac_cptable;
188     case CP_UTF7:
189     case CP_UTF8:
190         break;
191     case CP_THREAD_ACP:
192         if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
193         /* fall through */
194     default:
195         if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
196         if (codepage == oem_cptable->info.codepage) return oem_cptable;
197         if (codepage == mac_cptable->info.codepage) return mac_cptable;
198         ret = wine_cp_get_table( codepage );
199         break;
200     }
201     return ret;
202 }
203
204 /***********************************************************************
205  *              create_registry_key
206  *
207  * Create the Control Panel\\International registry key.
208  */
209 inline static HKEY create_registry_key(void)
210 {
211     static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
212                                   'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
213     OBJECT_ATTRIBUTES attr;
214     UNICODE_STRING nameW;
215     HKEY hkey;
216
217     if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
218
219     attr.Length = sizeof(attr);
220     attr.RootDirectory = hkey;
221     attr.ObjectName = &nameW;
222     attr.Attributes = 0;
223     attr.SecurityDescriptor = NULL;
224     attr.SecurityQualityOfService = NULL;
225     RtlInitUnicodeString( &nameW, intlW );
226
227     if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
228     NtClose( attr.RootDirectory );
229     return hkey;
230 }
231
232
233 /***********************************************************************
234  *              LOCALE_InitRegistry
235  *
236  * Update registry contents on startup if the user locale has changed.
237  * This simulates the action of the Windows control panel.
238  */
239 void LOCALE_InitRegistry(void)
240 {
241     static const USHORT updateValues[] = {
242       LOCALE_SLANGUAGE,
243       LOCALE_SCOUNTRY, LOCALE_ICOUNTRY,
244       LOCALE_S1159, LOCALE_S2359,
245       LOCALE_STIME, LOCALE_ITIME,
246       LOCALE_ITLZERO,
247       LOCALE_SSHORTDATE,
248       LOCALE_IDATE,
249       LOCALE_SLONGDATE,
250       LOCALE_SDATE,
251       LOCALE_SCURRENCY, LOCALE_ICURRENCY,
252       LOCALE_INEGCURR,
253       LOCALE_ICURRDIGITS,
254       LOCALE_SDECIMAL,
255       LOCALE_SLIST,
256       LOCALE_STHOUSAND,
257       LOCALE_IDIGITS,
258       LOCALE_IDIGITSUBSTITUTION,
259       LOCALE_SNATIVEDIGITS,
260       LOCALE_ITIMEMARKPOSN,
261       LOCALE_ICALENDARTYPE,
262       LOCALE_ILZERO,
263       LOCALE_IMEASURE
264     };
265     static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
266     UNICODE_STRING nameW;
267     char buffer[20];
268     WCHAR bufferW[80];
269     DWORD count, i;
270     HKEY hkey;
271     LCID lcid = GetUserDefaultLCID();
272
273     if (!(hkey = create_registry_key()))
274         return;  /* don't do anything if we can't create the registry key */
275
276     RtlInitUnicodeString( &nameW, LocaleW );
277     count = sizeof(bufferW);
278     if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
279     {
280         const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
281         LPCWSTR szValueText = (LPCWSTR)info->Data;
282
283         if (strtoulW( szValueText, NULL, 16 ) == lcid)  /* already set correctly */
284         {
285             NtClose( hkey );
286             return;
287         }
288         TRACE( "updating registry, locale changed %s -> %08lx\n", debugstr_w(szValueText), lcid );
289     }
290     else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
291
292     sprintf( buffer, "%08lx", lcid );
293     /* Note: '9' constant below is strlen(buffer) + 1 */
294     RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, 9 );
295     NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, 9 * sizeof(WCHAR) );
296     NtClose( hkey );
297
298     for (i = 0; i < sizeof(updateValues)/sizeof(updateValues[0]); i++)
299     {
300         GetLocaleInfoW( lcid, updateValues[i] | LOCALE_NOUSEROVERRIDE, bufferW,
301                         sizeof(bufferW)/sizeof(WCHAR) );
302         SetLocaleInfoW( lcid, updateValues[i], bufferW );
303     }
304 }
305
306
307 /***********************************************************************
308  *           find_language_id_proc
309  */
310 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCWSTR type,
311                                             LPCWSTR name, WORD LangID, LPARAM lParam )
312 {
313     LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
314     LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
315     WCHAR buf_language[128];
316     WCHAR buf_country[128];
317     WCHAR buf_en_language[128];
318
319     if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
320         return TRUE; /* continue search */
321
322     buf_language[0] = 0;
323     buf_country[0] = 0;
324
325     GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
326                    buf_language, sizeof(buf_language));
327     GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
328                    buf_country, sizeof(buf_country));
329
330     if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_language))
331     {
332         if(l_data->country[0])
333         {
334             if(!strcmpiW(l_data->country, buf_country))
335             {
336                 l_data->found_lang_id[0] = LangID;
337                 l_data->n_found = 1;
338                 TRACE("Found id %04X for lang %s country %s\n",
339                       LangID, debugstr_w(l_data->lang), debugstr_w(l_data->country));
340                 return FALSE; /* stop enumeration */
341             }
342         }
343         else goto found; /* l_data->country not specified */
344     }
345
346     /* Just in case, check LOCALE_SENGLANGUAGE too,
347      * in hope that possible alias name might have that value.
348      */
349     buf_en_language[0] = 0;
350     GetLocaleInfoW(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
351                    buf_en_language, sizeof(buf_en_language));
352
353     if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_en_language)) goto found;
354     return TRUE;  /* not found, continue search */
355
356 found:
357     l_data->found_lang_id[l_data->n_found] = LangID;
358     strncpyW(l_data->found_country[l_data->n_found], buf_country, 3);
359     strncpyW(l_data->found_language[l_data->n_found], buf_language, 3);
360     l_data->n_found++;
361     TRACE("Found id %04X for lang %s\n", LangID, debugstr_w(l_data->lang));
362     return (l_data->n_found < NLS_MAX_LANGUAGES); /* continue search, unless we have enough */
363 }
364
365
366 /***********************************************************************
367  *           get_language_id
368  *
369  * INPUT:
370  *      Lang: a string whose two first chars are the iso name of a language.
371  *      Country: a string whose two first chars are the iso name of country
372  *      Charset: a string defining the chosen charset encoding
373  *      Dialect: a string defining a variation of the locale
374  *
375  *      all those values are from the standardized format of locale
376  *      name in unix which is: Lang[_Country][.Charset][@Dialect]
377  *
378  * RETURNS:
379  *      the numeric code of the language used by Windows
380  *
381  * FIXME: Charset and Dialect are not handled
382  */
383 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
384 {
385     LANG_FIND_DATA l_data;
386     HMODULE hKernel32;
387
388     if(!Lang)
389     {
390         l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
391         goto END;
392     }
393
394     l_data.n_found = 0;
395     strcpynAtoW(l_data.lang, Lang, sizeof(l_data.lang));
396
397     if (Country) strcpynAtoW(l_data.country, Country, sizeof(l_data.country));
398     else l_data.country[0] = 0;
399
400     hKernel32 = GetModuleHandleW(kernel32W);
401
402     EnumResourceLanguagesW(hKernel32, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
403                            find_language_id_proc, (LPARAM)&l_data);
404
405     if (l_data.n_found == 1) goto END;
406
407     if(!l_data.n_found)
408     {
409         if(l_data.country[0])
410         {
411             /* retry without country name */
412             l_data.country[0] = 0;
413             EnumResourceLanguagesW(hKernel32, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
414                                    find_language_id_proc, (LONG)&l_data);
415             if (!l_data.n_found)
416             {
417                 MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to English.\n",
418                         Lang, Country);
419                 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
420             }
421             else MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to '%s'.\n",
422                          Lang, Country, debugstr_lang(l_data.found_lang_id[0]) );
423         }
424         else
425         {
426             MESSAGE("Warning: Language '%s' was not recognized, defaulting to English.\n", Lang);
427             l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
428         }
429     }
430     else
431     {
432         int i;
433
434         if (Country && Country[0])
435             MESSAGE("For language '%s_%s' several language ids were found:\n", Lang, Country);
436         else
437             MESSAGE("For language '%s' several language ids were found:\n", Lang);
438
439         /* print a list of languages with their description */
440         for (i = 0; i < l_data.n_found; i++)
441         {
442             WCHAR buffW[128];
443             char buffA[128];
444             GetLocaleInfoW( MAKELCID( l_data.found_lang_id[i], SORT_DEFAULT ),
445                            LOCALE_SLANGUAGE|LOCALE_NOUSEROVERRIDE, buffW, sizeof(buffW));
446             strcpyWtoA( buffA, buffW );
447             MESSAGE( "   %s (%04X) - %s\n", debugstr_lang(l_data.found_lang_id[i]),
448                      l_data.found_lang_id[i], buffA );
449         }
450         MESSAGE("Defaulting to '%s'. You should specify the exact language you want\n"
451                 "by defining your LANG environment variable like this: LANG=%s\n",
452                 debugstr_lang(l_data.found_lang_id[0]), debugstr_lang(l_data.found_lang_id[0]) );
453     }
454 END:
455     TRACE("Returning %04X (%s)\n", l_data.found_lang_id[0], debugstr_lang(l_data.found_lang_id[0]));
456     return l_data.found_lang_id[0];
457 }
458
459
460 /***********************************************************************
461  *              charset_cmp (internal)
462  */
463 static int charset_cmp( const void *name, const void *entry )
464 {
465     const struct charset_entry *charset = (struct charset_entry *)entry;
466     return strcasecmp( (char *)name, charset->charset_name );
467 }
468
469 /***********************************************************************
470  *              init_default_lcid
471  */
472 static LCID init_default_lcid( UINT *unix_cp )
473 {
474     char *buf, *lang,*country,*charset,*dialect,*next;
475     LCID ret = 0;
476
477     if ((lang = getenv( "LC_ALL" )) ||
478         (lang = getenv( "LC_CTYPE" )) ||
479         (lang = getenv( "LANGUAGE" )) ||
480         (lang = getenv( "LC_MESSAGES" )) ||
481         (lang = getenv( "LANG" )))
482     {
483         if (!strcmp(lang,"POSIX") || !strcmp(lang,"C")) goto done;
484
485         buf = RtlAllocateHeap( GetProcessHeap(), 0, strlen(lang) + 1 );
486         strcpy( buf, lang );
487         lang=buf;
488
489         do {
490             next=strchr(lang,':'); if (next) *next++='\0';
491             dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
492             charset=strchr(lang,'.'); if (charset) *charset++='\0';
493             country=strchr(lang,'_'); if (country) *country++='\0';
494
495             ret = get_language_id(lang, country, charset, dialect);
496             if (ret && charset)
497             {
498                 const struct charset_entry *entry;
499                 char charset_name[16];
500                 size_t i, j;
501
502                 /* remove punctuation characters from charset name */
503                 for (i = j = 0; charset[i] && j < sizeof(charset_name)-1; i++)
504                     if (isalnum(charset[i])) charset_name[j++] = charset[i];
505                 charset_name[j] = 0;
506
507                 entry = bsearch( charset_name, charset_names,
508                                  sizeof(charset_names)/sizeof(charset_names[0]),
509                                  sizeof(charset_names[0]), charset_cmp );
510                 if (entry)
511                 {
512                     *unix_cp = entry->codepage;
513                     TRACE("charset %s was mapped to cp %u\n", charset, *unix_cp);
514                 }
515                 else
516                     FIXME("charset %s was not recognized\n", charset);
517             }
518
519             lang=next;
520         } while (lang && !ret);
521
522         if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
523         RtlFreeHeap( GetProcessHeap(), 0, buf );
524     }
525
526  done:
527     if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
528     return ret;
529 }
530
531
532 /***********************************************************************
533  *              GetUserDefaultLangID (KERNEL32.@)
534  *
535  * Get the default language Id for the current user.
536  *
537  * PARAMS
538  *  None.
539  *
540  * RETURNS
541  *  The current LANGID of the default language for the current user.
542  */
543 LANGID WINAPI GetUserDefaultLangID(void)
544 {
545     return LANGIDFROMLCID(GetUserDefaultLCID());
546 }
547
548
549 /***********************************************************************
550  *              GetSystemDefaultLangID (KERNEL32.@)
551  *
552  * Get the default language Id for the system.
553  *
554  * PARAMS
555  *  None.
556  *
557  * RETURNS
558  *  The current LANGID of the default language for the system.
559  */
560 LANGID WINAPI GetSystemDefaultLangID(void)
561 {
562     return GetUserDefaultLangID();
563 }
564
565
566 /***********************************************************************
567  *              GetUserDefaultLCID (KERNEL32.@)
568  *
569  * Get the default locale Id for the current user.
570  *
571  * PARAMS
572  *  None.
573  *
574  * RETURNS
575  *  The current LCID of the default locale for the current user.
576  */
577 LCID WINAPI GetUserDefaultLCID(void)
578 {
579     LCID lcid;
580     NtQueryDefaultLocale( TRUE, &lcid );
581     return lcid;
582 }
583
584
585 /***********************************************************************
586  *              GetSystemDefaultLCID (KERNEL32.@)
587  *
588  * Get the default locale Id for the system.
589  *
590  * PARAMS
591  *  None.
592  *
593  * RETURNS
594  *  The current LCID of the default locale for the system.
595  */
596 LCID WINAPI GetSystemDefaultLCID(void)
597 {
598     LCID lcid;
599     NtQueryDefaultLocale( FALSE, &lcid );
600     return lcid;
601 }
602
603
604 /***********************************************************************
605  *              GetUserDefaultUILanguage (KERNEL32.@)
606  *
607  * Get the default user interface language Id for the current user.
608  *
609  * PARAMS
610  *  None.
611  *
612  * RETURNS
613  *  The current LANGID of the default UI language for the current user.
614  */
615 LANGID WINAPI GetUserDefaultUILanguage(void)
616 {
617     return GetUserDefaultLangID();
618 }
619
620
621 /***********************************************************************
622  *              GetSystemDefaultUILanguage (KERNEL32.@)
623  *
624  * Get the default user interface language Id for the system.
625  *
626  * PARAMS
627  *  None.
628  *
629  * RETURNS
630  *  The current LANGID of the default UI language for the system. This is
631  *  typically the same language used during the installation process.
632  */
633 LANGID WINAPI GetSystemDefaultUILanguage(void)
634 {
635     return GetSystemDefaultLangID();
636 }
637
638
639 /******************************************************************************
640  *              get_locale_value_name
641  *
642  * Gets the registry value name for a given lctype.
643  */
644 static const WCHAR *get_locale_value_name( DWORD lctype )
645 {
646     static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
647     static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
648     static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
649     static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
650     static const WCHAR iDateW[] = {'i','D','a','t','e',0};
651     static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
652     static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
653     static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
654     static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
655     static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
656     static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
657     static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
658     static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
659     static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
660     static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
661     static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
662     static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
663     static const WCHAR s1159W[] = {'s','1','1','5','9',0};
664     static const WCHAR s2359W[] = {'s','2','3','5','9',0};
665     static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
666     static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
667     static const WCHAR sDateW[] = {'s','D','a','t','e',0};
668     static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
669     static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
670     static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
671     static const WCHAR sListW[] = {'s','L','i','s','t',0};
672     static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
673     static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
674     static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
675     static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
676     static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
677     static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
678     static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
679     static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
680     static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
681     static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
682     static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
683     static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
684     static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
685
686     switch (lctype)
687     {
688     /* These values are used by SetLocaleInfo and GetLocaleInfo, and
689      * the values are stored in the registry, confirmed under Windows.
690      */
691     case LOCALE_ICALENDARTYPE:    return iCalendarTypeW;
692     case LOCALE_ICURRDIGITS:      return iCurrDigitsW;
693     case LOCALE_ICURRENCY:        return iCurrencyW;
694     case LOCALE_IDIGITS:          return iDigitsW;
695     case LOCALE_IFIRSTDAYOFWEEK:  return iFirstDayOfWeekW;
696     case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
697     case LOCALE_ILZERO:           return iLZeroW;
698     case LOCALE_IMEASURE:         return iMeasureW;
699     case LOCALE_INEGCURR:         return iNegCurrW;
700     case LOCALE_INEGNUMBER:       return iNegNumberW;
701     case LOCALE_IPAPERSIZE:       return iPaperSizeW;
702     case LOCALE_ITIME:            return iTimeW;
703     case LOCALE_S1159:            return s1159W;
704     case LOCALE_S2359:            return s2359W;
705     case LOCALE_SCURRENCY:        return sCurrencyW;
706     case LOCALE_SDATE:            return sDateW;
707     case LOCALE_SDECIMAL:         return sDecimalW;
708     case LOCALE_SGROUPING:        return sGroupingW;
709     case LOCALE_SLIST:            return sListW;
710     case LOCALE_SLONGDATE:        return sLongDateW;
711     case LOCALE_SMONDECIMALSEP:   return sMonDecimalSepW;
712     case LOCALE_SMONGROUPING:     return sMonGroupingW;
713     case LOCALE_SMONTHOUSANDSEP:  return sMonThousandSepW;
714     case LOCALE_SNEGATIVESIGN:    return sNegativeSignW;
715     case LOCALE_SPOSITIVESIGN:    return sPositiveSignW;
716     case LOCALE_SSHORTDATE:       return sShortDateW;
717     case LOCALE_STHOUSAND:        return sThousandW;
718     case LOCALE_STIME:            return sTimeW;
719     case LOCALE_STIMEFORMAT:      return sTimeFormatW;
720     case LOCALE_SYEARMONTH:       return sYearMonthW;
721
722     /* The following are not listed under MSDN as supported,
723      * but seem to be used and also stored in the registry.
724      */
725     case LOCALE_ICOUNTRY:         return iCountryW;
726     case LOCALE_IDATE:            return iDateW;
727     case LOCALE_ILDATE:           return iLDateW;
728     case LOCALE_ITLZERO:          return iTLZeroW;
729     case LOCALE_SCOUNTRY:         return sCountryW;
730     case LOCALE_SLANGUAGE:        return sLanguageW;
731
732     /* The following are used in XP and later */
733     case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
734     case LOCALE_SNATIVEDIGITS:      return sNativeDigitsW;
735     case LOCALE_ITIMEMARKPOSN:      return iTimePrefixW;
736     }
737     return NULL;
738 }
739
740
741 /******************************************************************************
742  *              get_registry_locale_info
743  *
744  * Retrieve user-modified locale info from the registry.
745  * Return length, 0 on error, -1 if not found.
746  */
747 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
748 {
749     DWORD size;
750     INT ret;
751     HKEY hkey;
752     NTSTATUS status;
753     UNICODE_STRING nameW;
754     KEY_VALUE_PARTIAL_INFORMATION *info;
755     static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
756
757     if (!(hkey = create_registry_key())) return -1;
758
759     RtlInitUnicodeString( &nameW, value );
760     size = info_size + len * sizeof(WCHAR);
761
762     if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
763     {
764         NtClose( hkey );
765         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
766         return 0;
767     }
768
769     status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
770     if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
771
772     if (!status)
773     {
774         ret = (size - info_size) / sizeof(WCHAR);
775         /* append terminating null if needed */
776         if (!ret || ((WCHAR *)info->Data)[ret-1])
777         {
778             if (ret < len || !buffer) ret++;
779             else
780             {
781                 SetLastError( ERROR_INSUFFICIENT_BUFFER );
782                 ret = 0;
783             }
784         }
785         if (ret && buffer)
786         {
787             memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
788             buffer[ret-1] = 0;
789         }
790     }
791     else
792     {
793         if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
794         else
795         {
796             SetLastError( RtlNtStatusToDosError(status) );
797             ret = 0;
798         }
799     }
800     NtClose( hkey );
801     HeapFree( GetProcessHeap(), 0, info );
802     return ret;
803 }
804
805
806 /******************************************************************************
807  *              GetLocaleInfoA (KERNEL32.@)
808  *
809  * Get information about an aspect of a locale.
810  *
811  * PARAMS
812  *  lcid   [I] LCID of the locale
813  *  lctype [I] LCTYPE_ flags from "winnls.h"
814  *  buffer [O] Destination for the information
815  *  len    [I] Length of buffer in characters
816  *
817  * RETURNS
818  *  Success: The size of the data requested. If buffer is non-NULL, it is filled
819  *           with the information.
820  *  Failure: 0. Use GetLastError() to determine the cause.
821  *
822  * NOTES
823  *  - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
824  *  - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
825  *    which is a bit string.
826  */
827 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
828 {
829     WCHAR *bufferW;
830     INT lenW, ret;
831
832     if (len < 0 || (len && !buffer))
833     {
834         SetLastError( ERROR_INVALID_PARAMETER );
835         return 0;
836     }
837     if (!len) buffer = NULL;
838
839     if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
840
841     if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
842     {
843         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
844         return 0;
845     }
846     if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
847     {
848         if ((lctype & LOCALE_RETURN_NUMBER) ||
849             ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
850         {
851             /* it's not an ASCII string, just bytes */
852             ret *= sizeof(WCHAR);
853             if (buffer)
854             {
855                 if (ret <= len) memcpy( buffer, bufferW, ret );
856                 else
857                 {
858                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
859                     ret = 0;
860                 }
861             }
862         }
863         else
864         {
865             UINT codepage = CP_ACP;
866             if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
867             ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
868         }
869     }
870     HeapFree( GetProcessHeap(), 0, bufferW );
871     return ret;
872 }
873
874
875 /******************************************************************************
876  *              GetLocaleInfoW (KERNEL32.@)
877  *
878  * See GetLocaleInfoA.
879  */
880 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
881 {
882     LANGID lang_id;
883     HRSRC hrsrc;
884     HGLOBAL hmem;
885     HMODULE hModule;
886     INT ret;
887     UINT lcflags;
888     const WCHAR *p;
889     unsigned int i;
890
891     if (len < 0 || (len && !buffer))
892     {
893         SetLastError( ERROR_INVALID_PARAMETER );
894         return 0;
895     }
896     if (!len) buffer = NULL;
897
898     lcid = ConvertDefaultLocale(lcid);
899
900     lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
901     lctype &= 0xffff;
902
903     /* first check for overrides in the registry */
904
905     if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
906     {
907         const WCHAR *value = get_locale_value_name(lctype);
908
909         if (value)
910         {
911             if (lcflags & LOCALE_RETURN_NUMBER)
912             {
913                 WCHAR tmp[16];
914                 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
915                 if (ret > 0)
916                 {
917                     WCHAR *end;
918                     UINT number = strtolW( tmp, &end, 10 );
919                     if (*end)  /* invalid number */
920                     {
921                         SetLastError( ERROR_INVALID_FLAGS );
922                         return 0;
923                     }
924                     ret = sizeof(UINT)/sizeof(WCHAR);
925                     if (!buffer) return ret;
926                     if (ret > len)
927                     {
928                         SetLastError( ERROR_INSUFFICIENT_BUFFER );
929                         return 0;
930                     }
931                     memcpy( buffer, &number, sizeof(number) );
932                 }
933             }
934             else ret = get_registry_locale_info( value, buffer, len );
935
936             if (ret != -1) return ret;
937         }
938     }
939
940     /* now load it from kernel resources */
941
942     lang_id = LANGIDFROMLCID( lcid );
943
944     /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
945     if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
946         lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
947
948     hModule = GetModuleHandleW( kernel32W );
949     if (!(hrsrc = FindResourceExW( hModule, (LPWSTR)RT_STRING, (LPCWSTR)((lctype >> 4) + 1), lang_id )))
950     {
951         SetLastError( ERROR_INVALID_FLAGS );  /* no such lctype */
952         return 0;
953     }
954     if (!(hmem = LoadResource( hModule, hrsrc )))
955         return 0;
956
957     p = LockResource( hmem );
958     for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
959
960     if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
961     else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
962
963     if (!buffer) return ret;
964
965     if (ret > len)
966     {
967         SetLastError( ERROR_INSUFFICIENT_BUFFER );
968         return 0;
969     }
970
971     if (lcflags & LOCALE_RETURN_NUMBER)
972     {
973         UINT number;
974         WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
975         if (!tmp) return 0;
976         memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
977         tmp[*p] = 0;
978         number = strtolW( tmp, &end, 10 );
979         if (!*end)
980             memcpy( buffer, &number, sizeof(number) );
981         else  /* invalid number */
982         {
983             SetLastError( ERROR_INVALID_FLAGS );
984             ret = 0;
985         }
986         HeapFree( GetProcessHeap(), 0, tmp );
987
988         TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
989                lcid, lctype, buffer, len, number );
990     }
991     else
992     {
993         memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
994         if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
995
996         TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
997                lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
998     }
999     return ret;
1000 }
1001
1002
1003 /******************************************************************************
1004  *              SetLocaleInfoA  [KERNEL32.@]
1005  *
1006  * Set information about an aspect of a locale.
1007  *
1008  * PARAMS
1009  *  lcid   [I] LCID of the locale
1010  *  lctype [I] LCTYPE_ flags from "winnls.h"
1011  *  data   [I] Information to set
1012  *
1013  * RETURNS
1014  *  Success: TRUE. The information given will be returned by GetLocaleInfoA()
1015  *           whenever it is called without LOCALE_NOUSEROVERRIDE.
1016  *  Failure: FALSE. Use GetLastError() to determine the cause.
1017  *
1018  * NOTES
1019  *  - Values are only be set for the current user locale; the system locale
1020  *  settings cannot be changed.
1021  *  - Any settings changed by this call are lost when the locale is changed by
1022  *  the control panel (in Wine, this happens every time you change LANG).
1023  *  - The native implementation of this function does not check that lcid matches
1024  *  the current user locale, and simply sets the new values. Wine warns you in
1025  *  this case, but behaves the same.
1026  */
1027 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1028 {
1029     UINT codepage = CP_ACP;
1030     WCHAR *strW;
1031     DWORD len;
1032     BOOL ret;
1033
1034     lcid = ConvertDefaultLocale(lcid);
1035
1036     if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1037     len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1038     if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1039     {
1040         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1041         return FALSE;
1042     }
1043     MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1044     ret = SetLocaleInfoW( lcid, lctype, strW );
1045     HeapFree( GetProcessHeap(), 0, strW );
1046     return ret;
1047 }
1048
1049
1050 /******************************************************************************
1051  *              SetLocaleInfoW  (KERNEL32.@)
1052  *
1053  * See SetLocaleInfoA.
1054  */
1055 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1056 {
1057     const WCHAR *value;
1058     const WCHAR intlW[] = {'i','n','t','l',0 };
1059     UNICODE_STRING valueW;
1060     NTSTATUS status;
1061     HKEY hkey;
1062
1063     lcid = ConvertDefaultLocale(lcid);
1064
1065     lctype &= 0xffff;
1066     value = get_locale_value_name( lctype );
1067
1068     if (!data || !value)
1069     {
1070         SetLastError( ERROR_INVALID_PARAMETER );
1071         return FALSE;
1072     }
1073
1074     if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1075     {
1076         SetLastError( ERROR_INVALID_FLAGS );
1077         return FALSE;
1078     }
1079
1080     if (lcid != GetUserDefaultLCID())
1081     {
1082         /* Windows does not check that the lcid matches the current lcid */
1083         WARN("locale 0x%08lx isn't the current locale (0x%08lx), setting anyway!\n",
1084              lcid, GetUserDefaultLCID());
1085     }
1086
1087     TRACE("setting %lx to %s\n", lctype, debugstr_w(data) );
1088
1089     /* FIXME: should check that data to set is sane */
1090
1091     /* FIXME: profile functions should map to registry */
1092     WriteProfileStringW( intlW, value, data );
1093
1094     if (!(hkey = create_registry_key())) return FALSE;
1095     RtlInitUnicodeString( &valueW, value );
1096     status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1097
1098     if (lctype == LOCALE_SDATE || lctype == LOCALE_SLONGDATE)
1099     {
1100       /* Set I-value from S value */
1101       WCHAR *lpD, *lpM, *lpY;
1102       WCHAR szBuff[2];
1103
1104       lpD = strchrW(data, 'd');
1105       lpM = strchrW(data, 'M');
1106       lpY = strchrW(data, 'y');
1107
1108       if (lpD <= lpM)
1109       {
1110         szBuff[0] = '1'; /* D-M-Y */
1111       }
1112       else
1113       {
1114         if (lpY <= lpM)
1115           szBuff[0] = '2'; /* Y-M-D */
1116         else
1117           szBuff[0] = '0'; /* M-D-Y */
1118       }
1119
1120       szBuff[1] = '\0';
1121
1122       if (lctype == LOCALE_SDATE)
1123         lctype = LOCALE_IDATE;
1124       else
1125         lctype = LOCALE_ILDATE;
1126
1127       value = get_locale_value_name( lctype );
1128
1129       WriteProfileStringW( intlW, value, szBuff );
1130
1131       RtlInitUnicodeString( &valueW, value );
1132       status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1133     }
1134
1135     NtClose( hkey );
1136
1137     if (status) SetLastError( RtlNtStatusToDosError(status) );
1138     return !status;
1139 }
1140
1141
1142 /******************************************************************************
1143  *              GetACP   (KERNEL32.@)
1144  *
1145  * Get the current Ansi code page Id for the system.
1146  *
1147  * PARAMS
1148  *  None.
1149  *
1150  * RETURNS
1151  *    The current Ansi code page identifier for the system.
1152  */
1153 UINT WINAPI GetACP(void)
1154 {
1155     assert( ansi_cptable );
1156     return ansi_cptable->info.codepage;
1157 }
1158
1159
1160 /***********************************************************************
1161  *              GetOEMCP   (KERNEL32.@)
1162  *
1163  * Get the current OEM code page Id for the system.
1164  *
1165  * PARAMS
1166  *  None.
1167  *
1168  * RETURNS
1169  *    The current OEM code page identifier for the system.
1170  */
1171 UINT WINAPI GetOEMCP(void)
1172 {
1173     assert( oem_cptable );
1174     return oem_cptable->info.codepage;
1175 }
1176
1177
1178 /***********************************************************************
1179  *           IsValidCodePage   (KERNEL32.@)
1180  *
1181  * Determine if a given code page identifier is valid.
1182  *
1183  * PARAMS
1184  *  codepage [I] Code page Id to verify.
1185  *
1186  * RETURNS
1187  *  TRUE, If codepage is valid and available on the system,
1188  *  FALSE otherwise.
1189  */
1190 BOOL WINAPI IsValidCodePage( UINT codepage )
1191 {
1192     switch(codepage) {
1193     case CP_UTF7:
1194     case CP_UTF8:
1195         return TRUE;
1196     default:
1197         return wine_cp_get_table( codepage ) != NULL;
1198     }
1199 }
1200
1201
1202 /***********************************************************************
1203  *           IsDBCSLeadByteEx   (KERNEL32.@)
1204  *
1205  * Determine if a character is a lead byte in a given code page.
1206  *
1207  * PARAMS
1208  *  codepage [I] Code page for the test.
1209  *  testchar [I] Character to test
1210  *
1211  * RETURNS
1212  *  TRUE, if testchar is a lead byte in codepage,
1213  *  FALSE otherwise.
1214  */
1215 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1216 {
1217     const union cptable *table = get_codepage_table( codepage );
1218     return table && is_dbcs_leadbyte( table, testchar );
1219 }
1220
1221
1222 /***********************************************************************
1223  *           IsDBCSLeadByte   (KERNEL32.@)
1224  *           IsDBCSLeadByte   (KERNEL.207)
1225  *
1226  * Determine if a character is a lead byte.
1227  *
1228  * PARAMS
1229  *  testchar [I] Character to test
1230  *
1231  * RETURNS
1232  *  TRUE, if testchar is a lead byte in the Ansii code page,
1233  *  FALSE otherwise.
1234  */
1235 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1236 {
1237     if (!ansi_cptable) return FALSE;
1238     return is_dbcs_leadbyte( ansi_cptable, testchar );
1239 }
1240
1241
1242 /***********************************************************************
1243  *           GetCPInfo   (KERNEL32.@)
1244  *
1245  * Get information about a code page.
1246  *
1247  * PARAMS
1248  *  codepage [I] Code page number
1249  *  cpinfo   [O] Destination for code page information
1250  *
1251  * RETURNS
1252  *  Success: TRUE. cpinfo is updated with the information about codepage.
1253  *  Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1254  */
1255 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1256 {
1257     const union cptable *table = get_codepage_table( codepage );
1258
1259     if (!table)
1260     {
1261         SetLastError( ERROR_INVALID_PARAMETER );
1262         return FALSE;
1263     }
1264     if (table->info.def_char & 0xff00)
1265     {
1266         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
1267         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1268     }
1269     else
1270     {
1271         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1272         cpinfo->DefaultChar[1] = 0;
1273     }
1274     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1275         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1276     else
1277         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1278
1279     return TRUE;
1280 }
1281
1282 /***********************************************************************
1283  *           GetCPInfoExA   (KERNEL32.@)
1284  *
1285  * Get extended information about a code page.
1286  *
1287  * PARAMS
1288  *  codepage [I] Code page number
1289  *  dwFlags  [I] Reserved, must to 0.
1290  *  cpinfo   [O] Destination for code page information
1291  *
1292  * RETURNS
1293  *  Success: TRUE. cpinfo is updated with the information about codepage.
1294  *  Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1295  */
1296 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1297 {
1298     const union cptable *table = get_codepage_table( codepage );
1299
1300     if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1301       return FALSE;
1302
1303     cpinfo->CodePage = codepage;
1304     cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1305     strcpy(cpinfo->CodePageName, table->info.name);
1306     return TRUE;
1307 }
1308
1309 /***********************************************************************
1310  *           GetCPInfoExW   (KERNEL32.@)
1311  *
1312  * Unicode version of GetCPInfoExA.
1313  */
1314 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1315 {
1316     const union cptable *table = get_codepage_table( codepage );
1317
1318     if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1319       return FALSE;
1320
1321     cpinfo->CodePage = codepage;
1322     cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1323     MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1324                          sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1325     return TRUE;
1326 }
1327
1328 /***********************************************************************
1329  *              EnumSystemCodePagesA   (KERNEL32.@)
1330  *
1331  * Call a user defined function for every code page installed on the system.
1332  *
1333  * PARAMS
1334  *   lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1335  *   flags            [I] Reserved, set to 0.
1336  *
1337  * RETURNS
1338  *  TRUE, If all code pages have been enumerated, or
1339  *  FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1340  */
1341 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1342 {
1343     const union cptable *table;
1344     char buffer[10];
1345     int index = 0;
1346
1347     for (;;)
1348     {
1349         if (!(table = wine_cp_enum_table( index++ ))) break;
1350         sprintf( buffer, "%d", table->info.codepage );
1351         if (!lpfnCodePageEnum( buffer )) break;
1352     }
1353     return TRUE;
1354 }
1355
1356
1357 /***********************************************************************
1358  *              EnumSystemCodePagesW   (KERNEL32.@)
1359  *
1360  * See EnumSystemCodePagesA.
1361  */
1362 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1363 {
1364     const union cptable *table;
1365     WCHAR buffer[10], *p;
1366     int page, index = 0;
1367
1368     for (;;)
1369     {
1370         if (!(table = wine_cp_enum_table( index++ ))) break;
1371         p = buffer + sizeof(buffer)/sizeof(WCHAR);
1372         *--p = 0;
1373         page = table->info.codepage;
1374         do
1375         {
1376             *--p = '0' + (page % 10);
1377             page /= 10;
1378         } while( page );
1379         if (!lpfnCodePageEnum( p )) break;
1380     }
1381     return TRUE;
1382 }
1383
1384
1385 /***********************************************************************
1386  *              MultiByteToWideChar   (KERNEL32.@)
1387  *
1388  * Convert a multibyte character string into a Unicode string.
1389  *
1390  * PARAMS
1391  *   page   [I] Codepage character set to convert from
1392  *   flags  [I] Character mapping flags
1393  *   src    [I] Source string buffer
1394  *   srclen [I] Length of src, or -1 if src is NUL terminated
1395  *   dst    [O] Destination buffer
1396  *   dstlen [I] Length of dst, or 0 to compute the required length
1397  *
1398  * RETURNS
1399  *   Success: If dstlen > 0, the number of characters written to dst.
1400  *            If dstlen == 0, the number of characters needed to perform the
1401  *            conversion. In both cases the count includes the terminating NUL.
1402  *   Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1403  *            ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1404  *            and dstlen != 0; ERROR_INVALID_PARAMETER,  if an invalid parameter
1405  *            is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1406  *            possible for src.
1407  */
1408 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1409                                 LPWSTR dst, INT dstlen )
1410 {
1411     const union cptable *table;
1412     int ret;
1413
1414     if (!src || (!dst && dstlen))
1415     {
1416         SetLastError( ERROR_INVALID_PARAMETER );
1417         return 0;
1418     }
1419
1420     if (srclen < 0) srclen = strlen(src) + 1;
1421
1422     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
1423
1424     switch(page)
1425     {
1426     case CP_SYMBOL:
1427         if( flags)
1428         {
1429             SetLastError( ERROR_INVALID_PARAMETER );
1430             return 0;
1431         }
1432         ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1433         break;
1434     case CP_UTF7:
1435         FIXME("UTF-7 not supported\n");
1436         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1437         return 0;
1438     case CP_UNIXCP:
1439         if (unix_cptable)
1440         {
1441             ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1442             break;
1443         }
1444         /* fall through */
1445     case CP_UTF8:
1446         ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1447         break;
1448     default:
1449         if (!(table = get_codepage_table( page )))
1450         {
1451             SetLastError( ERROR_INVALID_PARAMETER );
1452             return 0;
1453         }
1454         ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1455         break;
1456     }
1457
1458     if (ret < 0)
1459     {
1460         switch(ret)
1461         {
1462         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1463         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1464         }
1465         ret = 0;
1466     }
1467     return ret;
1468 }
1469
1470
1471 /***********************************************************************
1472  *              WideCharToMultiByte   (KERNEL32.@)
1473  *
1474  * Convert a Unicode character string into a multibyte string.
1475  *
1476  * PARAMS
1477  *   page    [I] Code page character set to convert to
1478  *   flags   [I] Mapping Flags (MB_ constants from "winnls.h").
1479  *   src     [I] Source string buffer
1480  *   srclen  [I] Length of src, or -1 if src is NUL terminated
1481  *   dst     [O] Destination buffer
1482  *   dstlen  [I] Length of dst, or 0 to compute the required length
1483  *   defchar [I] Default character to use for conversion if no exact
1484  *                  conversion can be made
1485  *   used    [O] Set if default character was used in the conversion
1486  *
1487  * RETURNS
1488  *   Success: If dstlen > 0, the number of characters written to dst.
1489  *            If dstlen == 0, number of characters needed to perform the
1490  *            conversion. In both cases the count includes the terminating NUL.
1491  *   Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1492  *            ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1493  *            and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1494  *            parameter was given.
1495  */
1496 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1497                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1498 {
1499     const union cptable *table;
1500     int ret, used_tmp;
1501
1502     if (!src || (!dst && dstlen))
1503     {
1504         SetLastError( ERROR_INVALID_PARAMETER );
1505         return 0;
1506     }
1507
1508     if (srclen < 0) srclen = strlenW(src) + 1;
1509
1510     switch(page)
1511     {
1512     case CP_SYMBOL:
1513         if( flags || defchar || used)
1514         {
1515             SetLastError( ERROR_INVALID_PARAMETER );
1516             return 0;
1517         }
1518         ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1519         break;
1520     case CP_UTF7:
1521         FIXME("UTF-7 not supported\n");
1522         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1523         return 0;
1524     case CP_UNIXCP:
1525         if (unix_cptable)
1526         {
1527             ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1528                                     defchar, used ? &used_tmp : NULL );
1529             break;
1530         }
1531         /* fall through */
1532     case CP_UTF8:
1533         ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
1534         break;
1535     default:
1536         if (!(table = get_codepage_table( page )))
1537         {
1538             SetLastError( ERROR_INVALID_PARAMETER );
1539             return 0;
1540         }
1541         ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1542                                 defchar, used ? &used_tmp : NULL );
1543         if (used) *used = used_tmp;
1544         break;
1545     }
1546
1547     if (ret < 0)
1548     {
1549         switch(ret)
1550         {
1551         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1552         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1553         }
1554         ret = 0;
1555     }
1556     TRACE("cp %d %s -> %s\n", page, debugstr_w(src), debugstr_a(dst));
1557     return ret;
1558 }
1559
1560
1561 /***********************************************************************
1562  *           GetThreadLocale    (KERNEL32.@)
1563  *
1564  * Get the current threads locale.
1565  *
1566  * PARAMS
1567  *  None.
1568  *
1569  * RETURNS
1570  *  The LCID currently assocated with the calling thread.
1571  */
1572 LCID WINAPI GetThreadLocale(void)
1573 {
1574     LCID ret = NtCurrentTeb()->CurrentLocale;
1575     if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1576     return ret;
1577 }
1578
1579 /**********************************************************************
1580  *           SetThreadLocale    (KERNEL32.@)
1581  *
1582  * Set the current threads locale.
1583  *
1584  * PARAMS
1585  *  lcid [I] LCID of the locale to set
1586  *
1587  * RETURNS
1588  *  Success: TRUE. The threads locale is set to lcid.
1589  *  Failure: FALSE. Use GetLastError() to determine the cause.
1590  */
1591 BOOL WINAPI SetThreadLocale( LCID lcid )
1592 {
1593     TRACE("(0x%04lX)\n", lcid);
1594
1595     lcid = ConvertDefaultLocale(lcid);
1596
1597     if (lcid != GetThreadLocale())
1598     {
1599         if (!IsValidLocale(lcid, LCID_SUPPORTED))
1600         {
1601             SetLastError(ERROR_INVALID_PARAMETER);
1602             return FALSE;
1603         }
1604
1605         NtCurrentTeb()->CurrentLocale = lcid;
1606         NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
1607     }
1608     return TRUE;
1609 }
1610
1611 /******************************************************************************
1612  *              ConvertDefaultLocale (KERNEL32.@)
1613  *
1614  * Convert a default locale identifier into a real identifier.
1615  *
1616  * PARAMS
1617  *  lcid [I] LCID identifier of the locale to convert
1618  *
1619  * RETURNS
1620  *  lcid unchanged, if not a default locale or its sublanguage is
1621  *   not SUBLANG_NEUTRAL.
1622  *  GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
1623  *  GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
1624  *  Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
1625  */
1626 LCID WINAPI ConvertDefaultLocale( LCID lcid )
1627 {
1628     LANGID langid;
1629
1630     switch (lcid)
1631     {
1632     case LOCALE_SYSTEM_DEFAULT:
1633         lcid = GetSystemDefaultLCID();
1634         break;
1635     case LOCALE_USER_DEFAULT:
1636     case LOCALE_NEUTRAL:
1637         lcid = GetUserDefaultLCID();
1638         break;
1639     default:
1640         /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
1641         langid = LANGIDFROMLCID(lcid);
1642         if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
1643         {
1644           langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
1645           lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
1646         }
1647     }
1648     return lcid;
1649 }
1650
1651
1652 /******************************************************************************
1653  *           IsValidLocale   (KERNEL32.@)
1654  *
1655  * Determine if a locale is valid.
1656  *
1657  * PARAMS
1658  *  lcid  [I] LCID of the locale to check
1659  *  flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
1660  *
1661  * RETURN
1662  *  TRUE,  if lcid is valid,
1663  *  FALSE, otherwise.
1664  *
1665  * NOTES
1666  *  Wine does not currently make the distinction between supported and installed. All
1667  *  languages supported are installed by default.
1668  */
1669 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
1670 {
1671     /* check if language is registered in the kernel32 resources */
1672     return FindResourceExW( GetModuleHandleW(kernel32W), (LPWSTR)RT_STRING,
1673                             (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
1674 }
1675
1676
1677 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
1678                                        LPCSTR name, WORD LangID, LONG lParam )
1679 {
1680     LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
1681     char buf[20];
1682
1683     sprintf(buf, "%08x", (UINT)LangID);
1684     return lpfnLocaleEnum( buf );
1685 }
1686
1687 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
1688                                        LPCWSTR name, WORD LangID, LONG lParam )
1689 {
1690     static const WCHAR formatW[] = {'%','0','8','x',0};
1691     LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
1692     WCHAR buf[20];
1693     sprintfW( buf, formatW, (UINT)LangID );
1694     return lpfnLocaleEnum( buf );
1695 }
1696
1697 /******************************************************************************
1698  *           EnumSystemLocalesA  (KERNEL32.@)
1699  *
1700  * Call a users function for each locale available on the system.
1701  *
1702  * PARAMS
1703  *  lpfnLocaleEnum [I] Callback function to call for each locale
1704  *  dwFlags        [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
1705  *
1706  * RETURNS
1707  *  Success: TRUE.
1708  *  Failure: FALSE. Use GetLastError() to determine the cause.
1709  */
1710 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
1711 {
1712     TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1713     EnumResourceLanguagesA( GetModuleHandleW(kernel32W), (LPSTR)RT_STRING,
1714                             (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
1715                             (LONG)lpfnLocaleEnum);
1716     return TRUE;
1717 }
1718
1719
1720 /******************************************************************************
1721  *           EnumSystemLocalesW  (KERNEL32.@)
1722  *
1723  * See EnumSystemLocalesA.
1724  */
1725 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
1726 {
1727     TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1728     EnumResourceLanguagesW( GetModuleHandleW(kernel32W), (LPWSTR)RT_STRING,
1729                             (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
1730                             (LONG)lpfnLocaleEnum);
1731     return TRUE;
1732 }
1733
1734
1735 /***********************************************************************
1736  *           VerLanguageNameA  (KERNEL32.@)
1737  *
1738  * Get the name of a language.
1739  *
1740  * PARAMS
1741  *  wLang  [I] LANGID of the language
1742  *  szLang [O] Destination for the language name
1743  *
1744  * RETURNS
1745  *  Success: The size of the language name. If szLang is non-NULL, it is filled
1746  *           with the name.
1747  *  Failure: 0. Use GetLastError() to determine the cause.
1748  *
1749  */
1750 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1751 {
1752     return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1753 }
1754
1755
1756 /***********************************************************************
1757  *           VerLanguageNameW  (KERNEL32.@)
1758  *
1759  * See VerLanguageNameA.
1760  */
1761 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1762 {
1763     return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1764 }
1765
1766
1767 /******************************************************************************
1768  *           GetStringTypeW    (KERNEL32.@)
1769  *
1770  * See GetStringTypeA.
1771  */
1772 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1773 {
1774     if (count == -1) count = strlenW(src) + 1;
1775     switch(type)
1776     {
1777     case CT_CTYPE1:
1778         while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
1779         break;
1780     case CT_CTYPE2:
1781         while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
1782         break;
1783     case CT_CTYPE3:
1784     {
1785         WARN("CT_CTYPE3: semi-stub.\n");
1786         while (count--)
1787         {
1788             int c = *src;
1789             WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
1790
1791             type1 = get_char_typeW( *src++ ) & 0xfff;
1792             /* try to construct type3 from type1 */
1793             if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
1794             if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1795             if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1796             if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1797             if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1798             if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1799             if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1800
1801             if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1802             if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1803             if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1804             if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1805             if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1806             if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1807             if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1808             if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1809
1810             if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1811             if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1812             if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1813             if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1814             if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1815             if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1816             *chartype++ = type3;
1817         }
1818         break;
1819     }
1820     default:
1821         SetLastError( ERROR_INVALID_PARAMETER );
1822         return FALSE;
1823     }
1824     return TRUE;
1825 }
1826
1827
1828 /******************************************************************************
1829  *           GetStringTypeExW    (KERNEL32.@)
1830  *
1831  * See GetStringTypeExA.
1832  */
1833 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1834 {
1835     /* locale is ignored for Unicode */
1836     return GetStringTypeW( type, src, count, chartype );
1837 }
1838
1839
1840 /******************************************************************************
1841  *           GetStringTypeA    (KERNEL32.@)
1842  *
1843  * Get characteristics of the characters making up a string.
1844  *
1845  * PARAMS
1846  *  locale   [I] Locale Id for the string
1847  *  type     [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1848  *  src      [I] String to analyse
1849  *  count    [I] Length of src in chars, or -1 if src is NUL terminated
1850  *  chartype [O] Destination for the calculated characteristics
1851  *
1852  * RETURNS
1853  *  Success: TRUE. chartype is filled with the requested characteristics of each char
1854  *           in src.
1855  *  Failure: FALSE. Use GetLastError() to determine the cause.
1856  */
1857 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1858 {
1859     UINT cp;
1860     INT countW;
1861     LPWSTR srcW;
1862     BOOL ret = FALSE;
1863
1864     if(count == -1) count = strlen(src) + 1;
1865
1866     if (!(cp = get_lcid_codepage( locale )))
1867     {
1868         FIXME("For locale %04lx using current ANSI code page\n", locale);
1869         cp = GetACP();
1870     }
1871
1872     countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1873     if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1874     {
1875         MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1876     /*
1877      * NOTE: the target buffer has 1 word for each CHARACTER in the source
1878      * string, with multibyte characters there maybe be more bytes in count
1879      * than character space in the buffer!
1880      */
1881         ret = GetStringTypeW(type, srcW, countW, chartype);
1882         HeapFree(GetProcessHeap(), 0, srcW);
1883     }
1884     return ret;
1885 }
1886
1887 /******************************************************************************
1888  *           GetStringTypeExA    (KERNEL32.@)
1889  *
1890  * Get characteristics of the characters making up a string.
1891  *
1892  * PARAMS
1893  *  locale   [I] Locale Id for the string
1894  *  type     [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1895  *  src      [I] String to analyse
1896  *  count    [I] Length of src in chars, or -1 if src is NUL terminated
1897  *  chartype [O] Destination for the calculated characteristics
1898  *
1899  * RETURNS
1900  *  Success: TRUE. chartype is filled with the requested characteristics of each char
1901  *           in src.
1902  *  Failure: FALSE. Use GetLastError() to determine the cause.
1903  */
1904 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1905 {
1906     return GetStringTypeA(locale, type, src, count, chartype);
1907 }
1908
1909
1910 /*************************************************************************
1911  *           LCMapStringW    (KERNEL32.@)
1912  *
1913  * See LCMapStringA.
1914  */
1915 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
1916                         LPWSTR dst, INT dstlen)
1917 {
1918     LPWSTR dst_ptr;
1919
1920     if (!src || !srclen || dstlen < 0)
1921     {
1922         SetLastError(ERROR_INVALID_PARAMETER);
1923         return 0;
1924     }
1925
1926     /* mutually exclusive flags */
1927     if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
1928         (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
1929         (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
1930         (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
1931     {
1932         SetLastError(ERROR_INVALID_FLAGS);
1933         return 0;
1934     }
1935
1936     if (!dstlen) dst = NULL;
1937
1938     lcid = ConvertDefaultLocale(lcid);
1939
1940     if (flags & LCMAP_SORTKEY)
1941     {
1942         if (src == dst)
1943         {
1944             SetLastError(ERROR_INVALID_FLAGS);
1945             return 0;
1946         }
1947
1948         if (srclen < 0) srclen = strlenW(src);
1949
1950         TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1951               lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1952
1953         return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
1954     }
1955
1956     /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
1957     if (flags & SORT_STRINGSORT)
1958     {
1959         SetLastError(ERROR_INVALID_FLAGS);
1960         return 0;
1961     }
1962
1963     if (srclen < 0) srclen = strlenW(src) + 1;
1964
1965     TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1966           lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1967
1968     if (!dst) /* return required string length */
1969     {
1970         INT len;
1971
1972         for (len = 0; srclen; src++, srclen--)
1973         {
1974             WCHAR wch = *src;
1975             /* tests show that win2k just ignores NORM_IGNORENONSPACE,
1976              * and skips white space and punctuation characters for
1977              * NORM_IGNORESYMBOLS.
1978              */
1979             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1980                 continue;
1981             len++;
1982         }
1983         return len;
1984     }
1985
1986     if (flags & LCMAP_UPPERCASE)
1987     {
1988         for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1989         {
1990             WCHAR wch = *src;
1991             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1992                 continue;
1993             *dst_ptr++ = toupperW(wch);
1994             dstlen--;
1995         }
1996     }
1997     else if (flags & LCMAP_LOWERCASE)
1998     {
1999         for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2000         {
2001             WCHAR wch = *src;
2002             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2003                 continue;
2004             *dst_ptr++ = tolowerW(wch);
2005             dstlen--;
2006         }
2007     }
2008     else
2009     {
2010         if (src == dst)
2011         {
2012             SetLastError(ERROR_INVALID_FLAGS);
2013             return 0;
2014         }
2015         for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2016         {
2017             WCHAR wch = *src;
2018             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2019                 continue;
2020             *dst_ptr++ = wch;
2021             dstlen--;
2022         }
2023     }
2024
2025     if (srclen)
2026     {
2027         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2028         return 0;
2029     }
2030
2031     return dst_ptr - dst;
2032 }
2033
2034 /*************************************************************************
2035  *           LCMapStringA    (KERNEL32.@)
2036  *
2037  * Map characters in a locale sensitive string.
2038  *
2039  * PARAMS
2040  *  lcid   [I] LCID for the conversion.
2041  *  flags  [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2042  *  src    [I] String to map
2043  *  srclen [I] Length of src in chars, or -1 if src is NUL terminated
2044  *  dst    [O] Destination for mapped string
2045  *  dstlen [I] Length of dst in characters
2046  *
2047  * RETURNS
2048  *  Success: The length of the mapped string in dst, including the NUL terminator.
2049  *  Failure: 0. Use GetLastError() to determine the cause.
2050  */
2051 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2052                         LPSTR dst, INT dstlen)
2053 {
2054     WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2055     LPWSTR srcW, dstW;
2056     INT ret = 0, srclenW, dstlenW;
2057     UINT locale_cp;
2058
2059     if (!src || !srclen || dstlen < 0)
2060     {
2061         SetLastError(ERROR_INVALID_PARAMETER);
2062         return 0;
2063     }
2064
2065     locale_cp = get_lcid_codepage(lcid);
2066
2067     srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2068     if (srclenW)
2069         srcW = bufW;
2070     else
2071     {
2072         srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2073         srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2074         if (!srcW)
2075         {
2076             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2077             return 0;
2078         }
2079         MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2080     }
2081
2082     if (flags & LCMAP_SORTKEY)
2083     {
2084         if (src == dst)
2085         {
2086             SetLastError(ERROR_INVALID_FLAGS);
2087             goto map_string_exit;
2088         }
2089         ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2090         goto map_string_exit;
2091     }
2092
2093     if (flags & SORT_STRINGSORT)
2094     {
2095         SetLastError(ERROR_INVALID_FLAGS);
2096         goto map_string_exit;
2097     }
2098
2099     dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2100     if (!dstlenW)
2101         goto map_string_exit;
2102
2103     dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2104     if (!dstW)
2105     {
2106         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2107         goto map_string_exit;
2108     }
2109
2110     LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2111     ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2112     HeapFree(GetProcessHeap(), 0, dstW);
2113
2114 map_string_exit:
2115     if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2116     return ret;
2117 }
2118
2119 /*************************************************************************
2120  *           FoldStringA    (KERNEL32.@)
2121  *
2122  * Map characters in a string.
2123  *
2124  * PARAMS
2125  *  dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2126  *  src     [I] String to map
2127  *  srclen  [I] Length of src, or -1 if src is NUL terminated
2128  *  dst     [O] Destination for mapped string
2129  *  dstlen  [I] Length of dst, or 0 to find the required length for the mapped string
2130  *
2131  * RETURNS
2132  *  Success: The length of the string written to dst, including the terminating NUL. If
2133  *           dstlen is 0, the value returned is the same, but nothing is written to dst,
2134  *           and dst may be NULL.
2135  *  Failure: 0. Use GetLastError() to determine the cause.
2136  */
2137 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2138                        LPSTR dst, INT dstlen)
2139 {
2140     INT ret = 0, srclenW = 0;
2141     WCHAR *srcW = NULL, *dstW = NULL;
2142
2143     if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2144     {
2145         SetLastError(ERROR_INVALID_PARAMETER);
2146         return 0;
2147     }
2148
2149     srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2150                                   src, srclen, NULL, 0);
2151     srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2152
2153     if (!srcW)
2154     {
2155         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2156         goto FoldStringA_exit;
2157     }
2158
2159     MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2160                         src, srclen, srcW, srclenW);
2161
2162     dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2163
2164     ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2165     if (ret && dstlen)
2166     {
2167         dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2168
2169         if (!dstW)
2170         {
2171             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2172             goto FoldStringA_exit;
2173         }
2174
2175         ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2176         if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2177         {
2178             ret = 0;
2179             SetLastError(ERROR_INSUFFICIENT_BUFFER);
2180         }
2181     }
2182
2183     if (dstW)
2184         HeapFree(GetProcessHeap(), 0, dstW);
2185
2186 FoldStringA_exit:
2187     if (srcW)
2188         HeapFree(GetProcessHeap(), 0, srcW);
2189     return ret;
2190 }
2191
2192 /*************************************************************************
2193  *           FoldStringW    (KERNEL32.@)
2194  *
2195  * See FoldStringA.
2196  */
2197 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2198                        LPWSTR dst, INT dstlen)
2199 {
2200     int ret;
2201
2202     switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2203     {
2204     case 0:
2205         if (dwFlags)
2206           break;
2207         /* Fall through for dwFlags == 0 */
2208     case MAP_PRECOMPOSED|MAP_COMPOSITE:
2209     case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2210     case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2211         SetLastError(ERROR_INVALID_FLAGS);
2212         return 0;
2213     }
2214
2215     if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2216     {
2217         SetLastError(ERROR_INVALID_PARAMETER);
2218         return 0;
2219     }
2220
2221     ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2222     if (!ret)
2223         SetLastError(ERROR_INSUFFICIENT_BUFFER);
2224     return ret;
2225 }
2226
2227 /******************************************************************************
2228  *           CompareStringW    (KERNEL32.@)
2229  *
2230  * See CompareStringA.
2231  */
2232 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2233                           LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2234 {
2235     INT ret;
2236
2237     if (!str1 || !str2)
2238     {
2239         SetLastError(ERROR_INVALID_PARAMETER);
2240         return 0;
2241     }
2242
2243     if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2244         SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|0x10000000) )
2245     {
2246         SetLastError(ERROR_INVALID_FLAGS);
2247         return 0;
2248     }
2249
2250     if (style & 0x10000000)
2251         FIXME("Ignoring unknown style 0x10000000\n");
2252
2253     if (len1 < 0) len1 = strlenW(str1);
2254     if (len2 < 0) len2 = strlenW(str2);
2255
2256     ret = wine_compare_string(style, str1, len1, str2, len2);
2257
2258     if (ret) /* need to translate result */
2259         return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2260     return CSTR_EQUAL;
2261 }
2262
2263 /******************************************************************************
2264  *           CompareStringA    (KERNEL32.@)
2265  *
2266  * Compare two locale sensitive strings.
2267  *
2268  * PARAMS
2269  *  lcid  [I] LCID for the comparason
2270  *  style [I] Flags for the comparason (NORM_ constants from "winnls.h").
2271  *  str1  [I] First string to compare
2272  *  len1  [I] Length of str1, or -1 if str1 is NUL terminated
2273  *  str2  [I] Second string to compare
2274  *  len2  [I] Length of str2, or -1 if str2 is NUL terminated
2275  *
2276  * RETURNS
2277  *  Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2278  *           str2 is less than, equal to or greater than str1 respectively.
2279  *  Failure: FALSE. Use GetLastError() to determine the cause.
2280  */
2281 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2282                           LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2283 {
2284     WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2285     WCHAR *buf2W = buf1W + 130;
2286     LPWSTR str1W, str2W;
2287     INT len1W, len2W, ret;
2288     UINT locale_cp;
2289
2290     if (!str1 || !str2)
2291     {
2292         SetLastError(ERROR_INVALID_PARAMETER);
2293         return 0;
2294     }
2295     if (len1 < 0) len1 = strlen(str1);
2296     if (len2 < 0) len2 = strlen(str2);
2297
2298     locale_cp = get_lcid_codepage(lcid);
2299
2300     len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2301     if (len1W)
2302         str1W = buf1W;
2303     else
2304     {
2305         len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2306         str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2307         if (!str1W)
2308         {
2309             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2310             return 0;
2311         }
2312         MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2313     }
2314     len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2315     if (len2W)
2316         str2W = buf2W;
2317     else
2318     {
2319         len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2320         str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2321         if (!str2W)
2322         {
2323             if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2324             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2325             return 0;
2326         }
2327         MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2328     }
2329
2330     ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2331
2332     if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2333     if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2334     return ret;
2335 }
2336
2337 /*************************************************************************
2338  *           lstrcmp     (KERNEL32.@)
2339  *           lstrcmpA    (KERNEL32.@)
2340  *
2341  * Compare two strings using the current thread locale.
2342  *
2343  * PARAMS
2344  *  str1  [I] First string to compare
2345  *  str2  [I] Second string to compare
2346  *
2347  * RETURNS
2348  *  Success: A number less than, equal to or greater than 0 depending on whether
2349  *           str2 is less than, equal to or greater than str1 respectively.
2350  *  Failure: FALSE. Use GetLastError() to determine the cause.
2351  */
2352 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2353 {
2354     int ret = CompareStringA(GetThreadLocale(), 0, str1, -1, str2, -1);
2355     if (ret) ret -= 2;
2356     return ret;
2357 }
2358
2359 /*************************************************************************
2360  *           lstrcmpi     (KERNEL32.@)
2361  *           lstrcmpiA    (KERNEL32.@)
2362  *
2363  * Compare two strings using the current thread locale, ignoring case.
2364  *
2365  * PARAMS
2366  *  str1  [I] First string to compare
2367  *  str2  [I] Second string to compare
2368  *
2369  * RETURNS
2370  *  Success: A number less than, equal to or greater than 0 depending on whether
2371  *           str2 is less than, equal to or greater than str1 respectively.
2372  *  Failure: FALSE. Use GetLastError() to determine the cause.
2373  */
2374 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2375 {
2376     int ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2377     if (ret) ret -= 2;
2378     return ret;
2379 }
2380
2381 /*************************************************************************
2382  *           lstrcmpW    (KERNEL32.@)
2383  *
2384  * See lstrcmpA.
2385  */
2386 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2387 {
2388     int ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2389     if (ret) ret -= 2;
2390     return ret;
2391 }
2392
2393 /*************************************************************************
2394  *           lstrcmpiW    (KERNEL32.@)
2395  *
2396  * See lstrcmpiA.
2397  */
2398 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2399 {
2400     int ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2401     if (ret) ret -= 2;
2402     return ret;
2403 }
2404
2405 /******************************************************************************
2406  *              LOCALE_Init
2407  */
2408 void LOCALE_Init(void)
2409 {
2410     extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2411                                        const union cptable *unix_cp );
2412
2413     UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp = ~0U;
2414     LCID lcid = init_default_lcid( &unix_cp );
2415
2416     NtSetDefaultLocale( FALSE, lcid );
2417     NtSetDefaultLocale( TRUE, lcid );
2418
2419     ansi_cp = get_lcid_codepage(lcid);
2420     GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2421                     (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2422     GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2423                     (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2424     if (unix_cp == ~0U)
2425         GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2426                     (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2427
2428     if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2429         ansi_cptable = wine_cp_get_table( 1252 );
2430     if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2431         oem_cptable  = wine_cp_get_table( 437 );
2432     if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2433         mac_cptable  = wine_cp_get_table( 10000 );
2434     if (unix_cp != CP_UTF8)
2435     {
2436         if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2437             unix_cptable  = wine_cp_get_table( 28591 );
2438     }
2439
2440     __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2441
2442     TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2443            ansi_cptable->info.codepage, oem_cptable->info.codepage,
2444            mac_cptable->info.codepage, unix_cp );
2445 }
2446
2447 static HKEY NLS_RegOpenKey(HKEY hRootKey, LPCWSTR szKeyName)
2448 {
2449     UNICODE_STRING keyName;
2450     OBJECT_ATTRIBUTES attr;
2451     HKEY hkey;
2452
2453     RtlInitUnicodeString( &keyName, szKeyName );
2454     InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2455
2456     if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2457         hkey = 0;
2458
2459     return hkey;
2460 }
2461
2462 static HKEY NLS_RegOpenSubKey(HKEY hRootKey, LPCWSTR szKeyName)
2463 {
2464     HKEY hKey = NLS_RegOpenKey(hRootKey, szKeyName);
2465
2466     if (hRootKey)
2467         NtClose( hRootKey );
2468
2469     return hKey;
2470 }
2471
2472 static BOOL NLS_RegEnumSubKey(HKEY hKey, UINT ulIndex, LPWSTR szKeyName,
2473                               ULONG keyNameSize)
2474 {
2475     BYTE buffer[80];
2476     KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2477     DWORD dwLen;
2478
2479     if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2480                         sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2481         info->NameLength > keyNameSize)
2482     {
2483         return FALSE;
2484     }
2485
2486     TRACE("info->Name %s info->NameLength %ld\n", debugstr_w(info->Name), info->NameLength);
2487
2488     memcpy( szKeyName, info->Name, info->NameLength);
2489     szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2490
2491     TRACE("returning %s\n", debugstr_w(szKeyName));
2492     return TRUE;
2493 }
2494
2495 static BOOL NLS_RegEnumValue(HKEY hKey, UINT ulIndex,
2496                              LPWSTR szValueName, ULONG valueNameSize,
2497                              LPWSTR szValueData, ULONG valueDataSize)
2498 {
2499     BYTE buffer[80];
2500     KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2501     DWORD dwLen;
2502
2503     if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2504         buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2505         info->NameLength > valueNameSize ||
2506         info->DataLength > valueDataSize)
2507     {
2508         return FALSE;
2509     }
2510
2511     TRACE("info->Name %s info->DataLength %ld\n", debugstr_w(info->Name), info->DataLength);
2512
2513     memcpy( szValueName, info->Name, info->NameLength);
2514     szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
2515     memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
2516     szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
2517
2518     TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
2519     return TRUE;
2520 }
2521
2522 static BOOL NLS_RegGetDword(HKEY hKey, LPCWSTR szValueName, DWORD *lpVal)
2523 {
2524     BYTE buffer[128];
2525     const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
2526     DWORD dwSize = sizeof(buffer);
2527     UNICODE_STRING valueName;
2528
2529     RtlInitUnicodeString( &valueName, szValueName );
2530
2531     TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
2532     if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
2533                          buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
2534         info->DataLength == sizeof(DWORD))
2535     {
2536         memcpy(lpVal, info->Data, sizeof(DWORD));
2537         return TRUE;
2538     }
2539
2540     return FALSE;
2541 }
2542
2543 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
2544 {
2545     HMODULE hModule = GetModuleHandleW(kernel32W);
2546     LANGID  langId;
2547     LPCWSTR szResourceName = (LPCWSTR)(((lgrpid + 0x2000) >> 4) + 1);
2548     HRSRC   hResource;
2549     BOOL    bRet = FALSE;
2550
2551     /* FIXME: Is it correct to use the system default langid? */
2552     langId = GetSystemDefaultLangID();
2553
2554     if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
2555         langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
2556
2557     hResource = FindResourceExW( hModule, (LPWSTR)RT_STRING, szResourceName, langId );
2558
2559     if (hResource)
2560     {
2561         HGLOBAL hResDir = LoadResource( hModule, hResource );
2562
2563         if (hResDir)
2564         {
2565             ULONG   iResourceIndex = lgrpid & 0xf;
2566             LPCWSTR lpResEntry = LockResource( hResDir );
2567             ULONG   i;
2568
2569             for (i = 0; i < iResourceIndex; i++)
2570                 lpResEntry += *lpResEntry + 1;
2571
2572             if (*lpResEntry < nameSize)
2573             {
2574                 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
2575                 szName[*lpResEntry] = '\0';
2576                 bRet = TRUE;
2577             }
2578
2579         }
2580         FreeResource( hResource );
2581     }
2582     return bRet;
2583 }
2584
2585 /* Registry keys for NLS related information */
2586 static const WCHAR szNlsKeyName[] = {
2587     'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
2588     'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
2589     'C','o','n','t','r','o','l','\\','N','l','s','\0'
2590 };
2591
2592 static const WCHAR szLangGroupsKeyName[] = {
2593     'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s','\0'
2594 };
2595
2596 static const WCHAR szCountryListName[] = {
2597     'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
2598     'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
2599     'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2600     'T','e','l','e','p','h','o','n','y','\\',
2601     'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
2602 };
2603
2604
2605 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
2606 typedef struct
2607 {
2608   LANGUAGEGROUP_ENUMPROCA procA;
2609   LANGUAGEGROUP_ENUMPROCW procW;
2610   DWORD    dwFlags;
2611   LONG_PTR lParam;
2612 } ENUMLANGUAGEGROUP_CALLBACKS;
2613
2614 /* Internal implementation of EnumSystemLanguageGroupsA/W */
2615 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
2616 {
2617     WCHAR szNumber[10], szValue[4];
2618     HKEY hKey;
2619     BOOL bContinue = TRUE;
2620     ULONG ulIndex = 0;
2621
2622     if (!lpProcs)
2623     {
2624         SetLastError(ERROR_INVALID_PARAMETER);
2625         return FALSE;
2626     }
2627
2628     switch (lpProcs->dwFlags)
2629     {
2630     case 0:
2631         /* Default to LGRPID_INSTALLED */
2632         lpProcs->dwFlags = LGRPID_INSTALLED;
2633         /* Fall through... */
2634     case LGRPID_INSTALLED:
2635     case LGRPID_SUPPORTED:
2636         break;
2637     default:
2638         SetLastError(ERROR_INVALID_FLAGS);
2639         return FALSE;
2640     }
2641
2642     hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2643
2644     if (!hKey)
2645       WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2646
2647     while (bContinue)
2648     {
2649         if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2650                               szValue, sizeof(szValue) ))
2651         {
2652             BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
2653             LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
2654
2655             TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
2656                    bInstalled ? "" : "not ");
2657
2658             if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
2659             {
2660                 WCHAR szGrpName[48];
2661
2662                 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
2663                     szGrpName[0] = '\0';
2664
2665                 if (lpProcs->procW)
2666                     bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
2667                                                 lpProcs->lParam );
2668                 else
2669                 {
2670                     char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2671                     char szGrpNameA[48];
2672
2673                     /* FIXME: MSDN doesn't say which code page the W->A translation uses,
2674                      *        or whether the language names are ever localised. Assume CP_ACP.
2675                      */
2676
2677                     WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2678                     WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
2679
2680                     bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
2681                                                 lpProcs->lParam );
2682                 }
2683             }
2684
2685             ulIndex++;
2686         }
2687         else
2688             bContinue = FALSE;
2689
2690         if (!bContinue)
2691             break;
2692     }
2693
2694     if (hKey)
2695         NtClose( hKey );
2696
2697     return TRUE;
2698 }
2699
2700 /******************************************************************************
2701  *           EnumSystemLanguageGroupsA    (KERNEL32.@)
2702  *
2703  * Call a users function for each language group available on the system.
2704  *
2705  * PARAMS
2706  *  pLangGrpEnumProc [I] Callback function to call for each language group
2707  *  dwFlags          [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
2708  *  lParam           [I] User parameter to pass to pLangGrpEnumProc
2709  *
2710  * RETURNS
2711  *  Success: TRUE.
2712  *  Failure: FALSE. Use GetLastError() to determine the cause.
2713  */
2714 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
2715                                       DWORD dwFlags, LONG_PTR lParam)
2716 {
2717     ENUMLANGUAGEGROUP_CALLBACKS procs;
2718
2719     TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2720
2721     procs.procA = pLangGrpEnumProc;
2722     procs.procW = NULL;
2723     procs.dwFlags = dwFlags;
2724     procs.lParam = lParam;
2725
2726     return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2727 }
2728
2729 /******************************************************************************
2730  *           EnumSystemLanguageGroupsW    (KERNEL32.@)
2731  *
2732  * See EnumSystemLanguageGroupsA.
2733  */
2734 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
2735                                       DWORD dwFlags, LONG_PTR lParam)
2736 {
2737     ENUMLANGUAGEGROUP_CALLBACKS procs;
2738
2739     TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2740
2741     procs.procA = NULL;
2742     procs.procW = pLangGrpEnumProc;
2743     procs.dwFlags = dwFlags;
2744     procs.lParam = lParam;
2745
2746     return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2747 }
2748
2749 /******************************************************************************
2750  *           IsValidLanguageGroup    (KERNEL32.@)
2751  *
2752  * Determine if a language group is supported and/or installed.
2753  *
2754  * PARAMS
2755  *  lgrpid  [I] Language Group Id (LGRPID_ values from "winnls.h")
2756  *  dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
2757  *
2758  * RETURNS
2759  *  TRUE, if lgrpid is supported and/or installed, according to dwFlags.
2760  *  FALSE otherwise.
2761  */
2762 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
2763 {
2764     static const WCHAR szFormat[] = { '%','x','\0' };
2765     WCHAR szValueName[16], szValue[2];
2766     BOOL bSupported = FALSE, bInstalled = FALSE;
2767     HKEY hKey;
2768
2769
2770     switch (dwFlags)
2771     {
2772     case LGRPID_INSTALLED:
2773     case LGRPID_SUPPORTED:
2774
2775         hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2776
2777         sprintfW( szValueName, szFormat, lgrpid );
2778
2779         if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
2780         {
2781             bSupported = TRUE;
2782
2783             if (szValue[0] == '1')
2784                 bInstalled = TRUE;
2785         }
2786
2787         if (hKey)
2788             NtClose( hKey );
2789
2790         break;
2791     }
2792
2793     if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
2794         (dwFlags == LGRPID_INSTALLED && bInstalled))
2795         return TRUE;
2796
2797     return FALSE;
2798 }
2799
2800 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
2801 typedef struct
2802 {
2803   LANGGROUPLOCALE_ENUMPROCA procA;
2804   LANGGROUPLOCALE_ENUMPROCW procW;
2805   DWORD    dwFlags;
2806   LGRPID   lgrpid;
2807   LONG_PTR lParam;
2808 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
2809
2810 /* Internal implementation of EnumLanguageGrouplocalesA/W */
2811 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
2812 {
2813     static const WCHAR szLocaleKeyName[] = {
2814       'L','o','c','a','l','e','\0'
2815     };
2816     static const WCHAR szAlternateSortsKeyName[] = {
2817       'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
2818     };
2819     WCHAR szNumber[10], szValue[4];
2820     HKEY hKey;
2821     BOOL bContinue = TRUE, bAlternate = FALSE;
2822     LGRPID lgrpid;
2823     ULONG ulIndex = 1;  /* Ignore default entry of 1st key */
2824
2825     if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
2826     {
2827         SetLastError(ERROR_INVALID_PARAMETER);
2828         return FALSE;
2829     }
2830
2831     if (lpProcs->dwFlags)
2832     {
2833         SetLastError(ERROR_INVALID_FLAGS);
2834         return FALSE;
2835     }
2836
2837     hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLocaleKeyName );
2838
2839     if (!hKey)
2840       WARN("NLS registry key not found. Please apply the default registry file 'winedefault.reg'\n");
2841
2842     while (bContinue)
2843     {
2844         if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2845                               szValue, sizeof(szValue) ))
2846         {
2847             lgrpid = strtoulW( szValue, NULL, 16 );
2848
2849             TRACE("lcid %s, grpid %ld (%smatched)\n", debugstr_w(szNumber),
2850                    lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
2851
2852             if (lgrpid == lpProcs->lgrpid)
2853             {
2854                 LCID lcid;
2855
2856                 lcid = strtoulW( szNumber, NULL, 16 );
2857
2858                 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
2859                  * '00000437          ;Georgian'
2860                  * At present we only pass the LCID string.
2861                  */
2862
2863                 if (lpProcs->procW)
2864                     bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
2865                 else
2866                 {
2867                     char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2868
2869                     WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2870
2871                     bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
2872                 }
2873             }
2874
2875             ulIndex++;
2876         }
2877         else
2878         {
2879             /* Finished enumerating this key */
2880             if (!bAlternate)
2881             {
2882                 /* Enumerate alternate sorts also */
2883                 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
2884                 bAlternate = TRUE;
2885                 ulIndex = 0;
2886             }
2887             else
2888                 bContinue = FALSE; /* Finished both keys */
2889         }
2890
2891         if (!bContinue)
2892             break;
2893     }
2894
2895     if (hKey)
2896         NtClose( hKey );
2897
2898     return TRUE;
2899 }
2900
2901 /******************************************************************************
2902  *           EnumLanguageGroupLocalesA    (KERNEL32.@)
2903  *
2904  * Call a users function for every locale in a language group available on the system.
2905  *
2906  * PARAMS
2907  *  pLangGrpLcEnumProc [I] Callback function to call for each locale
2908  *  lgrpid             [I] Language group (LGRPID_ values from "winnls.h")
2909  *  dwFlags            [I] Reserved, set to 0
2910  *  lParam             [I] User parameter to pass to pLangGrpLcEnumProc
2911  *
2912  * RETURNS
2913  *  Success: TRUE.
2914  *  Failure: FALSE. Use GetLastError() to determine the cause.
2915  */
2916 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
2917                                       LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2918 {
2919     ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2920
2921     TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2922
2923     callbacks.procA   = pLangGrpLcEnumProc;
2924     callbacks.procW   = NULL;
2925     callbacks.dwFlags = dwFlags;
2926     callbacks.lgrpid  = lgrpid;
2927     callbacks.lParam  = lParam;
2928
2929     return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2930 }
2931
2932 /******************************************************************************
2933  *           EnumLanguageGroupLocalesW    (KERNEL32.@)
2934  *
2935  * See EnumLanguageGroupLocalesA.
2936  */
2937 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
2938                                       LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2939 {
2940     ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2941
2942     TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2943
2944     callbacks.procA   = NULL;
2945     callbacks.procW   = pLangGrpLcEnumProc;
2946     callbacks.dwFlags = dwFlags;
2947     callbacks.lgrpid  = lgrpid;
2948     callbacks.lParam  = lParam;
2949
2950     return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2951 }
2952
2953 /******************************************************************************
2954  *           EnumSystemGeoID    (KERNEL32.@)
2955  *
2956  * Call a users function for every location available on the system.
2957  *
2958  * PARAMS
2959  *  geoclass     [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
2960  *  reserved     [I] Reserved, set to 0
2961  *  pGeoEnumProc [I] Callback function to call for each location
2962  *
2963  * RETURNS
2964  *  Success: TRUE.
2965  *  Failure: FALSE. Use GetLastError() to determine the cause.
2966  */
2967 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
2968 {
2969     static const WCHAR szCountryCodeValueName[] = {
2970       'C','o','u','n','t','r','y','C','o','d','e','\0'
2971     };
2972     WCHAR szNumber[10];
2973     HKEY hKey;
2974     ULONG ulIndex = 0;
2975
2976     TRACE("(0x%08lX,0x%08lX,%p)\n", geoclass, reserved, pGeoEnumProc);
2977
2978     if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
2979     {
2980         SetLastError(ERROR_INVALID_PARAMETER);
2981         return FALSE;
2982     }
2983
2984     hKey = NLS_RegOpenKey( 0, szCountryListName );
2985
2986     while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
2987     {
2988         BOOL bContinue = TRUE;
2989         DWORD dwGeoId;
2990         HKEY hSubKey = NLS_RegOpenKey( hKey, szNumber );
2991
2992         if (hSubKey)
2993         {
2994             if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
2995             {
2996                 TRACE("Got geoid %ld\n", dwGeoId);
2997
2998                 if (!pGeoEnumProc( dwGeoId ))
2999                     bContinue = FALSE;
3000             }
3001
3002             NtClose( hSubKey );
3003         }
3004
3005         if (!bContinue)
3006             break;
3007
3008         ulIndex++;
3009     }
3010
3011     if (hKey)
3012         NtClose( hKey );
3013
3014     return TRUE;
3015 }
3016
3017 /******************************************************************************
3018  *           InvalidateNLSCache           (KERNEL32.@)
3019  *
3020  * Invalidate the cache of NLS values.
3021  *
3022  * PARAMS
3023  *  None.
3024  *
3025  * RETURNS
3026  *  Success: TRUE.
3027  *  Failure: FALSE.
3028  */
3029 BOOL WINAPI InvalidateNLSCache(void)
3030 {
3031   FIXME("() stub\n");
3032   return FALSE;
3033 }
3034
3035 /******************************************************************************
3036  *           GetUserGeoID (KERNEL32.@)
3037  */
3038 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3039 {
3040     FIXME("%ld\n",GeoClass);
3041     return GEOID_NOT_AVAILABLE;
3042 }
3043
3044 /******************************************************************************
3045  *           SetUserGeoID (KERNEL32.@)
3046  */
3047 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3048 {
3049     FIXME("%ld\n",GeoID);
3050     return FALSE;
3051 }