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