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