Moved the implementation of the pthread wrappers to dlls/kernel.
[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 <string.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"  /* for RT_STRINGW */
35 #include "winternl.h"
36 #include "wine/unicode.h"
37 #include "winnls.h"
38 #include "winerror.h"
39 #include "thread.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(nls);
43
44 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
45
46 extern void CODEPAGE_Init( UINT ansi_cp, UINT oem_cp, UINT mac_cp, UINT unix_cp, LCID lcid );
47
48 /* Charset to codepage map, sorted by name. */
49 static const struct charset_entry
50 {
51     const char *charset_name;
52     UINT        codepage;
53 } charset_names[] =
54 {
55     { "CP1250", 1250 },
56     { "CP1251", 1251 },
57     { "CP1252", 1252 },
58     { "CP1253", 1253 },
59     { "CP1254", 1254 },
60     { "CP1255", 1255 },
61     { "CP1256", 1256 },
62     { "CP1257", 1257 },
63     { "CP1258", 1258 },
64     { "IBM037", 37 },
65     { "IBM1026", 1026 },
66     { "IBM424", 424 },
67     { "IBM437", 437 },
68     { "IBM500", 500 },
69     { "IBM850", 850 },
70     { "IBM852", 852 },
71     { "IBM855", 855 },
72     { "IBM857", 857 },
73     { "IBM860", 860 },
74     { "IBM861", 861 },
75     { "IBM862", 862 },
76     { "IBM863", 863 },
77     { "IBM864", 864 },
78     { "IBM865", 865 },
79     { "IBM866", 866 },
80     { "IBM869", 869 },
81     { "IBM874", 874 },
82     { "IBM875", 875 },
83     { "ISO-8859-1", 28591 },
84     { "ISO-8859-10", 28600 },
85     { "ISO-8859-13", 28603 },
86     { "ISO-8859-14", 28604 },
87     { "ISO-8859-15", 28605 },
88     { "ISO-8859-2", 28592 },
89     { "ISO-8859-3", 28593 },
90     { "ISO-8859-4", 28594 },
91     { "ISO-8859-5", 28595 },
92     { "ISO-8859-6", 28596 },
93     { "ISO-8859-7", 28597 },
94     { "ISO-8859-8", 28598 },
95     { "ISO-8859-9", 28599 },
96     { "ISO_8859-1", 28591 },
97     { "ISO_8859-10", 28600 },
98     { "ISO_8859-13", 28603 },
99     { "ISO_8859-14", 28604 },
100     { "ISO_8859-15", 28605 },
101     { "ISO_8859-2", 28592 },
102     { "ISO_8859-3", 28593 },
103     { "ISO_8859-4", 28594 },
104     { "ISO_8859-5", 28595 },
105     { "ISO_8859-6", 28596 },
106     { "ISO_8859-7", 28597 },
107     { "ISO_8859-8", 28598 },
108     { "ISO_8859-9", 28599 },
109     { "KOI8-R", 20866 },
110     { "KOI8-U", 20866 },
111     { "UTF-8", CP_UTF8 }
112 };
113
114 #define NLS_MAX_LANGUAGES 20
115 typedef struct {
116     char lang[128];
117     char country[128];
118     LANGID found_lang_id[NLS_MAX_LANGUAGES];
119     char found_language[NLS_MAX_LANGUAGES][3];
120     char found_country[NLS_MAX_LANGUAGES][3];
121     int n_found;
122 } LANG_FIND_DATA;
123
124
125 /***********************************************************************
126  *              get_lcid_codepage
127  *
128  * Retrieve the ANSI codepage for a given locale.
129  */
130 inline static UINT get_lcid_codepage( LCID lcid )
131 {
132     UINT ret;
133     if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
134                          sizeof(ret)/sizeof(WCHAR) )) ret = 0;
135     return ret;
136 }
137
138
139 /***********************************************************************
140  *              create_registry_key
141  *
142  * Create the Control Panel\\International registry key.
143  */
144 inline static HKEY create_registry_key(void)
145 {
146     static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
147                                   'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
148     OBJECT_ATTRIBUTES attr;
149     UNICODE_STRING nameW;
150     HKEY hkey;
151
152     if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
153
154     attr.Length = sizeof(attr);
155     attr.RootDirectory = hkey;
156     attr.ObjectName = &nameW;
157     attr.Attributes = 0;
158     attr.SecurityDescriptor = NULL;
159     attr.SecurityQualityOfService = NULL;
160     RtlInitUnicodeString( &nameW, intlW );
161
162     if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
163     NtClose( attr.RootDirectory );
164     return hkey;
165 }
166
167
168 /***********************************************************************
169  *              update_registry
170  *
171  * Update registry contents on startup if the user locale has changed.
172  * This simulates the action of the Windows control panel.
173  */
174 inline static void update_registry( LCID lcid )
175 {
176     static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
177     UNICODE_STRING nameW;
178     char buffer[20];
179     WCHAR bufferW[80];
180     DWORD count = sizeof(buffer);
181     HKEY hkey;
182
183     if (!(hkey = create_registry_key()))
184         return;  /* don't do anything if we can't create the registry key */
185
186     RtlInitUnicodeString( &nameW, LocaleW );
187     count = sizeof(bufferW);
188     if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
189     {
190         KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
191         RtlUnicodeToMultiByteN( buffer, sizeof(buffer)-1, &count,
192                                 (WCHAR *)info->Data, info->DataLength );
193         buffer[count] = 0;
194         if (strtol( buffer, NULL, 16 ) == lcid)  /* already set correctly */
195         {
196             NtClose( hkey );
197             return;
198         }
199         TRACE( "updating registry, locale changed %s -> %08lx\n", buffer, lcid );
200     }
201     else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
202
203     sprintf( buffer, "%08lx", lcid );
204     RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, strlen(buffer)+1 );
205     NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW)+1) * sizeof(WCHAR) );
206     NtClose( hkey );
207
208 #define UPDATE_VALUE(lctype) do { \
209     GetLocaleInfoW( lcid, (lctype)|LOCALE_NOUSEROVERRIDE, bufferW, sizeof(bufferW)/sizeof(WCHAR) ); \
210     SetLocaleInfoW( lcid, (lctype), bufferW ); } while (0)
211
212     UPDATE_VALUE(LOCALE_SLANGUAGE);
213     UPDATE_VALUE(LOCALE_SCOUNTRY);
214     UPDATE_VALUE(LOCALE_ICOUNTRY);
215     UPDATE_VALUE(LOCALE_S1159);
216     UPDATE_VALUE(LOCALE_S2359);
217     UPDATE_VALUE(LOCALE_STIME);
218     UPDATE_VALUE(LOCALE_ITIME);
219     UPDATE_VALUE(LOCALE_ITLZERO);
220     UPDATE_VALUE(LOCALE_SSHORTDATE);
221     UPDATE_VALUE(LOCALE_IDATE);
222     UPDATE_VALUE(LOCALE_SLONGDATE);
223     UPDATE_VALUE(LOCALE_SDATE);
224     UPDATE_VALUE(LOCALE_SCURRENCY);
225     UPDATE_VALUE(LOCALE_ICURRENCY);
226     UPDATE_VALUE(LOCALE_INEGCURR);
227     UPDATE_VALUE(LOCALE_ICURRDIGITS);
228     UPDATE_VALUE(LOCALE_SDECIMAL);
229     UPDATE_VALUE(LOCALE_SLIST);
230     UPDATE_VALUE(LOCALE_STHOUSAND);
231     UPDATE_VALUE(LOCALE_IDIGITS);
232     UPDATE_VALUE(LOCALE_ILZERO);
233     UPDATE_VALUE(LOCALE_IMEASURE);
234 #undef UPDATE_VALUE
235 }
236
237
238 /***********************************************************************
239  *           find_language_id_proc
240  */
241 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCSTR type,
242                                             LPCSTR name, WORD LangID, LPARAM lParam )
243 {
244     LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
245     LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
246     char buf_language[128];
247     char buf_country[128];
248     char buf_en_language[128];
249
250     TRACE("%04X\n", (UINT)LangID);
251     if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
252         return TRUE; /* continue search */
253
254     buf_language[0] = 0;
255     buf_country[0] = 0;
256
257     GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
258                    buf_language, sizeof(buf_language));
259     TRACE("LOCALE_SISO639LANGNAME: %s\n", buf_language);
260
261     GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
262                    buf_country, sizeof(buf_country));
263     TRACE("LOCALE_SISO3166CTRYNAME: %s\n", buf_country);
264
265     if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_language))
266     {
267         if(l_data->country && strlen(l_data->country) > 0)
268         {
269             if(!strcasecmp(l_data->country, buf_country))
270             {
271                 l_data->found_lang_id[0] = LangID;
272                 l_data->n_found = 1;
273                 TRACE("Found lang_id %04X for %s_%s\n", LangID, l_data->lang, l_data->country);
274                 return FALSE; /* stop enumeration */
275             }
276         }
277         else /* l_data->country not specified */
278         {
279             if(l_data->n_found < NLS_MAX_LANGUAGES)
280             {
281                 l_data->found_lang_id[l_data->n_found] = LangID;
282                 strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
283                 strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
284                 l_data->n_found++;
285                 TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
286                 return TRUE; /* continue search */
287             }
288         }
289     }
290
291     /* Just in case, check LOCALE_SENGLANGUAGE too,
292      * in hope that possible alias name might have that value.
293      */
294     buf_en_language[0] = 0;
295     GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP,
296                    buf_en_language, sizeof(buf_en_language));
297     TRACE("LOCALE_SENGLANGUAGE: %s\n", buf_en_language);
298
299     if(l_data->lang && strlen(l_data->lang) > 0 && !strcasecmp(l_data->lang, buf_en_language))
300     {
301         l_data->found_lang_id[l_data->n_found] = LangID;
302         strncpy(l_data->found_country[l_data->n_found], buf_country, 3);
303         strncpy(l_data->found_language[l_data->n_found], buf_language, 3);
304         l_data->n_found++;
305         TRACE("Found lang_id %04X for %s\n", LangID, l_data->lang);
306     }
307
308     return TRUE; /* continue search */
309 }
310
311
312 /***********************************************************************
313  *           get_language_id
314  *
315  * INPUT:
316  *      Lang: a string whose two first chars are the iso name of a language.
317  *      Country: a string whose two first chars are the iso name of country
318  *      Charset: a string defining the chosen charset encoding
319  *      Dialect: a string defining a variation of the locale
320  *
321  *      all those values are from the standardized format of locale
322  *      name in unix which is: Lang[_Country][.Charset][@Dialect]
323  *
324  * RETURNS:
325  *      the numeric code of the language used by Windows
326  *
327  * FIXME: Charset and Dialect are not handled
328  */
329 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
330 {
331     LANG_FIND_DATA l_data;
332     char lang_string[256];
333
334     if(!Lang)
335     {
336         l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
337         goto END;
338     }
339
340     memset(&l_data, 0, sizeof(LANG_FIND_DATA));
341     strncpy(l_data.lang, Lang, sizeof(l_data.lang));
342
343     if(Country && strlen(Country) > 0)
344         strncpy(l_data.country, Country, sizeof(l_data.country));
345
346     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
347                            (LPCSTR)LOCALE_ILANGUAGE, find_language_id_proc, (LPARAM)&l_data);
348
349     strcpy(lang_string, l_data.lang);
350     if(l_data.country && strlen(l_data.country) > 0)
351     {
352         strcat(lang_string, "_");
353         strcat(lang_string, l_data.country);
354     }
355
356     if(!l_data.n_found)
357     {
358         if(l_data.country && strlen(l_data.country) > 0)
359         {
360             MESSAGE("Warning: Language '%s' was not found, retrying without country name...\n", lang_string);
361             l_data.country[0] = 0;
362             EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), RT_STRINGA,
363                                    (LPCSTR)LOCALE_ILANGUAGE, find_language_id_proc, (LONG)&l_data);
364         }
365     }
366
367     /* Re-evaluate lang_string */
368     strcpy(lang_string, l_data.lang);
369     if(l_data.country && strlen(l_data.country) > 0)
370     {
371         strcat(lang_string, "_");
372         strcat(lang_string, l_data.country);
373     }
374
375     if(!l_data.n_found)
376     {
377         MESSAGE("Warning: Language '%s' was not recognized, defaulting to English\n", lang_string);
378         l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
379     }
380     else
381     {
382         if(l_data.n_found == 1)
383             TRACE("For language '%s' lang_id %04X was found\n", lang_string, l_data.found_lang_id[0]);
384         else /* l_data->n_found > 1 */
385         {
386             int i;
387             MESSAGE("For language '%s' several language ids were found:\n", lang_string);
388             for(i = 0; i < l_data.n_found; i++)
389                 MESSAGE("%s_%s - %04X; ", l_data.found_language[i], l_data.found_country[i], l_data.found_lang_id[i]);
390
391             MESSAGE("\nInstead of using first in the list, suggest to define\n"
392                     "your LANG environment variable like this: LANG=%s_%s\n",
393                     l_data.found_language[0], l_data.found_country[0]);
394         }
395     }
396 END:
397     TRACE("Returning %04X\n", l_data.found_lang_id[0]);
398     return l_data.found_lang_id[0];
399 }
400
401
402 /***********************************************************************
403  *              charset_cmp (internal)
404  */
405 static int charset_cmp( const void *name, const void *entry )
406 {
407     const struct charset_entry *charset = (struct charset_entry *)entry;
408     return strcasecmp( (char *)name, charset->charset_name );
409 }
410
411 /***********************************************************************
412  *              init_default_lcid
413  */
414 static LCID init_default_lcid( UINT *unix_cp )
415 {
416     char buf[256];
417     char *lang,*country,*charset,*dialect,*next;
418     LCID ret = 0;
419
420     if (GetEnvironmentVariableA( "LC_ALL", buf, sizeof(buf) ) ||
421         GetEnvironmentVariableA( "LC_CTYPE", buf, sizeof(buf) ) ||
422         GetEnvironmentVariableA( "LANGUAGE", buf, sizeof(buf) ) ||
423         GetEnvironmentVariableA( "LC_MESSAGES", buf, sizeof(buf) ) ||
424         GetEnvironmentVariableA( "LANG", buf, sizeof(buf) ))
425     {
426         if (!strcmp(buf,"POSIX") || !strcmp(buf,"C")) goto done;
427
428         lang=buf;
429
430         do {
431             next=strchr(lang,':'); if (next) *next++='\0';
432             dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
433             charset=strchr(lang,'.'); if (charset) *charset++='\0';
434             country=strchr(lang,'_'); if (country) *country++='\0';
435
436             ret = get_language_id(lang, country, charset, dialect);
437             if (ret && charset)
438             {
439                 const struct charset_entry *entry;
440                 entry = bsearch( charset, charset_names, sizeof(charset_names)/sizeof(charset_names[0]),
441                                sizeof(charset_names[0]), charset_cmp );
442                 if (entry)
443                 {
444                     *unix_cp = entry->codepage;
445                     TRACE("charset %s was mapped to cp %u\n", charset, *unix_cp);
446                 }
447                 else
448                     FIXME("charset %s was not recognized\n", charset);
449             }
450
451             lang=next;
452         } while (lang && !ret);
453
454         if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
455     }
456
457  done:
458     if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
459     return ret;
460 }
461
462
463 /******************************************************************************
464  *              get_locale_value_name
465  *
466  * Gets the registry value name for a given lctype.
467  */
468 static const WCHAR *get_locale_value_name( DWORD lctype )
469 {
470     static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
471     static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
472     static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
473     static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
474     static const WCHAR iDateW[] = {'i','D','a','t','e',0};
475     static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
476     static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
477     static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
478     static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
479     static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
480     static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
481     static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
482     static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
483     static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
484     static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
485     static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
486     static const WCHAR s1159W[] = {'s','1','1','5','9',0};
487     static const WCHAR s2359W[] = {'s','2','3','5','9',0};
488     static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
489     static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
490     static const WCHAR sDateW[] = {'s','D','a','t','e',0};
491     static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
492     static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
493     static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
494     static const WCHAR sListW[] = {'s','L','i','s','t',0};
495     static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
496     static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
497     static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
498     static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
499     static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
500     static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
501     static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
502     static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
503     static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
504     static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
505     static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
506
507     switch (lctype & ~LOCALE_LOCALEINFOFLAGSMASK)
508     {
509     /* These values are used by SetLocaleInfo and GetLocaleInfo, and
510      * the values are stored in the registry, confirmed under Windows.
511      */
512     case LOCALE_ICALENDARTYPE:    return iCalendarTypeW;
513     case LOCALE_ICURRDIGITS:      return iCurrDigitsW;
514     case LOCALE_ICURRENCY:        return iCurrencyW;
515     case LOCALE_IDIGITS:          return iDigitsW;
516     case LOCALE_IFIRSTDAYOFWEEK:  return iFirstDayOfWeekW;
517     case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
518     case LOCALE_ILZERO:           return iLZeroW;
519     case LOCALE_IMEASURE:         return iMeasureW;
520     case LOCALE_INEGCURR:         return iNegCurrW;
521     case LOCALE_INEGNUMBER:       return iNegNumberW;
522     case LOCALE_IPAPERSIZE:       return iPaperSizeW;
523     case LOCALE_ITIME:            return iTimeW;
524     case LOCALE_S1159:            return s1159W;
525     case LOCALE_S2359:            return s2359W;
526     case LOCALE_SCURRENCY:        return sCurrencyW;
527     case LOCALE_SDATE:            return sDateW;
528     case LOCALE_SDECIMAL:         return sDecimalW;
529     case LOCALE_SGROUPING:        return sGroupingW;
530     case LOCALE_SLIST:            return sListW;
531     case LOCALE_SLONGDATE:        return sLongDateW;
532     case LOCALE_SMONDECIMALSEP:   return sMonDecimalSepW;
533     case LOCALE_SMONGROUPING:     return sMonGroupingW;
534     case LOCALE_SMONTHOUSANDSEP:  return sMonThousandSepW;
535     case LOCALE_SNEGATIVESIGN:    return sNegativeSignW;
536     case LOCALE_SPOSITIVESIGN:    return sPositiveSignW;
537     case LOCALE_SSHORTDATE:       return sShortDateW;
538     case LOCALE_STHOUSAND:        return sThousandW;
539     case LOCALE_STIME:            return sTimeW;
540     case LOCALE_STIMEFORMAT:      return sTimeFormatW;
541     case LOCALE_SYEARMONTH:       return sYearMonthW;
542
543     /* The following are not listed under MSDN as supported,
544      * but seem to be used and also stored in the registry.
545      */
546     case LOCALE_ICOUNTRY:         return iCountryW;
547     case LOCALE_IDATE:            return iDateW;
548     case LOCALE_ILDATE:           return iLDateW;
549     case LOCALE_ITLZERO:          return iTLZeroW;
550     case LOCALE_SCOUNTRY:         return sCountryW;
551     case LOCALE_SLANGUAGE:        return sLanguageW;
552     }
553     return NULL;
554 }
555
556
557 /******************************************************************************
558  *              get_registry_locale_info
559  *
560  * Retrieve user-modified locale info from the registry.
561  * Return length, 0 on error, -1 if not found.
562  */
563 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
564 {
565     DWORD size;
566     INT ret;
567     HKEY hkey;
568     NTSTATUS status;
569     UNICODE_STRING nameW;
570     KEY_VALUE_PARTIAL_INFORMATION *info;
571     static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
572
573     if (!(hkey = create_registry_key())) return -1;
574
575     RtlInitUnicodeString( &nameW, value );
576     size = info_size + len * sizeof(WCHAR);
577
578     if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
579     {
580         NtClose( hkey );
581         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
582         return 0;
583     }
584
585     status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
586     if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
587
588     if (!status)
589     {
590         ret = (size - info_size) / sizeof(WCHAR);
591         /* append terminating null if needed */
592         if (!ret || ((WCHAR *)info->Data)[ret-1])
593         {
594             if (ret < len || !buffer) ret++;
595             else
596             {
597                 SetLastError( ERROR_INSUFFICIENT_BUFFER );
598                 ret = 0;
599             }
600         }
601         if (ret && buffer)
602         {
603             memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
604             buffer[ret-1] = 0;
605         }
606     }
607     else
608     {
609         if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
610         else
611         {
612             SetLastError( RtlNtStatusToDosError(status) );
613             ret = 0;
614         }
615     }
616     NtClose( hkey );
617     HeapFree( GetProcessHeap(), 0, info );
618     return ret;
619 }
620
621
622 /******************************************************************************
623  *              GetLocaleInfoA (KERNEL32.@)
624  *
625  * NOTES
626  *  LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
627  *
628  *  MS online documentation states that the string returned is NULL terminated
629  *  except for LOCALE_FONTSIGNATURE  which "will return a non-NULL
630  *  terminated string".
631  */
632 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
633 {
634     WCHAR *bufferW;
635     INT lenW, ret;
636
637     if (len < 0 || (len && !buffer))
638     {
639         SetLastError( ERROR_INVALID_PARAMETER );
640         return 0;
641     }
642     if (!len) buffer = NULL;
643
644     if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
645
646     if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
647     {
648         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
649         return 0;
650     }
651     if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
652     {
653         if ((lctype & LOCALE_RETURN_NUMBER) ||
654             ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
655         {
656             /* it's not an ASCII string, just bytes */
657             ret *= sizeof(WCHAR);
658             if (buffer)
659             {
660                 if (ret <= len) memcpy( buffer, bufferW, ret );
661                 else
662                 {
663                     SetLastError( ERROR_INSUFFICIENT_BUFFER );
664                     ret = 0;
665                 }
666             }
667         }
668         else
669         {
670             UINT codepage = CP_ACP;
671             if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
672             ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
673         }
674     }
675     HeapFree( GetProcessHeap(), 0, bufferW );
676     return ret;
677 }
678
679
680 /******************************************************************************
681  *              GetLocaleInfoW (KERNEL32.@)
682  *
683  * NOTES
684  *  LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
685  *
686  *  MS online documentation states that the string returned is NULL terminated
687  *  except for LOCALE_FONTSIGNATURE  which "will return a non-NULL
688  *  terminated string".
689  */
690 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
691 {
692     LANGID lang_id;
693     HRSRC hrsrc;
694     HGLOBAL hmem;
695     HMODULE hModule;
696     INT ret;
697     UINT lcflags;
698     const WCHAR *p;
699     int i;
700
701     if (len < 0 || (len && !buffer))
702     {
703         SetLastError( ERROR_INVALID_PARAMETER );
704         return 0;
705     }
706     if (!len) buffer = NULL;
707
708     if (lcid == LOCALE_NEUTRAL || lcid == LOCALE_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
709     else if (lcid == LOCALE_USER_DEFAULT) lcid = GetUserDefaultLCID();
710
711     lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
712     lctype &= ~LOCALE_LOCALEINFOFLAGSMASK;
713
714     /* first check for overrides in the registry */
715
716     if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
717     {
718         const WCHAR *value = get_locale_value_name(lctype);
719
720         if (value && ((ret = get_registry_locale_info( value, buffer, len )) != -1)) return ret;
721     }
722
723     /* now load it from kernel resources */
724
725     lang_id = LANGIDFROMLCID( lcid );
726
727     /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
728     if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
729         lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
730
731     hModule = GetModuleHandleA( "kernel32.dll" );
732     if (!(hrsrc = FindResourceExW( hModule, RT_STRINGW, (LPCWSTR)((lctype >> 4) + 1), lang_id )))
733     {
734         SetLastError( ERROR_INVALID_FLAGS );  /* no such lctype */
735         return 0;
736     }
737     if (!(hmem = LoadResource( hModule, hrsrc )))
738         return 0;
739
740     p = LockResource( hmem );
741     for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
742
743     if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
744     else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
745
746     if (!buffer) return ret;
747
748     if (ret > len)
749     {
750         SetLastError( ERROR_INSUFFICIENT_BUFFER );
751         return 0;
752     }
753
754     if (lcflags & LOCALE_RETURN_NUMBER)
755     {
756         UINT number;
757         WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
758         if (!tmp) return 0;
759         memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
760         tmp[*p] = 0;
761         number = strtolW( tmp, &end, 10 );
762         if (!*end)
763             memcpy( buffer, &number, sizeof(number) );
764         else  /* invalid number */
765         {
766             SetLastError( ERROR_INVALID_FLAGS );
767             ret = 0;
768         }
769         HeapFree( GetProcessHeap(), 0, tmp );
770
771         TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
772                lcid, lctype, buffer, len, number );
773     }
774     else
775     {
776         memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
777         if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
778
779         TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
780                lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
781     }
782     return ret;
783 }
784
785
786 /******************************************************************************
787  *              SetLocaleInfoA  [KERNEL32.@]
788  */
789 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
790 {
791     UINT codepage = CP_ACP;
792     WCHAR *strW;
793     DWORD len;
794     BOOL ret;
795
796     if (lcid == LOCALE_NEUTRAL || lcid == LOCALE_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
797     else if (lcid == LOCALE_USER_DEFAULT) lcid = GetUserDefaultLCID();
798
799     if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
800     len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
801     if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
802     {
803         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
804         return FALSE;
805     }
806     MultiByteToWideChar( codepage, 0, data, -1, strW, len );
807     ret = SetLocaleInfoW( lcid, lctype, strW );
808     HeapFree( GetProcessHeap(), 0, strW );
809     return ret;
810 }
811
812
813 /******************************************************************************
814  *              SetLocaleInfoW  (KERNEL32.@)
815  */
816 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
817 {
818     const WCHAR *value;
819     const WCHAR intlW[] = {'i','n','t','l',0 };
820     UNICODE_STRING valueW;
821     NTSTATUS status;
822     HKEY hkey;
823
824     if (lcid == LOCALE_NEUTRAL || lcid == LOCALE_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
825     else if (lcid == LOCALE_USER_DEFAULT) lcid = GetUserDefaultLCID();
826
827     if (!(value = get_locale_value_name( lctype )))
828     {
829         SetLastError( ERROR_INVALID_PARAMETER );
830         return FALSE;
831     }
832     if (lcid != GetUserDefaultLCID()) return TRUE;  /* fake success */
833
834     TRACE("setting %lx to %s\n", lctype, debugstr_w(data) );
835
836     /* FIXME: should check that data to set is sane */
837
838     /* FIXME: profile functions should map to registry */
839     WriteProfileStringW( intlW, value, data );
840
841     if (!(hkey = create_registry_key())) return FALSE;
842     RtlInitUnicodeString( &valueW, value );
843     status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
844     NtClose( hkey );
845
846     if (status) SetLastError( RtlNtStatusToDosError(status) );
847     return !status;
848     return TRUE;
849 }
850
851
852 /***********************************************************************
853  *           GetThreadLocale    (KERNEL32.@)
854  */
855 LCID WINAPI GetThreadLocale(void)
856 {
857     LCID ret = NtCurrentTeb()->CurrentLocale;
858     if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
859     return ret;
860 }
861
862
863 /**********************************************************************
864  *           SetThreadLocale    (KERNEL32.@)
865  *
866  * FIXME
867  *  check if lcid is a valid cp
868  */
869 BOOL WINAPI SetThreadLocale( LCID lcid ) /* [in] Locale identifier */
870 {
871     if (lcid == LOCALE_NEUTRAL || lcid == LOCALE_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
872     else if (lcid == LOCALE_USER_DEFAULT) lcid = GetUserDefaultLCID();
873
874     NtCurrentTeb()->CurrentLocale = lcid;
875     NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
876     return TRUE;
877 }
878
879
880 /******************************************************************************
881  *              ConvertDefaultLocale (KERNEL32.@)
882  */
883 LCID WINAPI ConvertDefaultLocale( LCID lcid )
884 {
885     switch (lcid)
886     {
887     case LOCALE_SYSTEM_DEFAULT:
888         return GetSystemDefaultLCID();
889     case LOCALE_USER_DEFAULT:
890         return GetUserDefaultLCID();
891     case LOCALE_NEUTRAL:
892         return MAKELCID (LANG_NEUTRAL, SUBLANG_NEUTRAL);
893     }
894     return MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_NEUTRAL);
895 }
896
897
898 /******************************************************************************
899  *           IsValidLocale   (KERNEL32.@)
900  */
901 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
902 {
903     /* check if language is registered in the kernel32 resources */
904     return FindResourceExW( GetModuleHandleA("KERNEL32"), RT_STRINGW,
905                             (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
906 }
907
908
909 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
910                                        LPCSTR name, WORD LangID, LONG lParam )
911 {
912     LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
913     char buf[20];
914
915     sprintf(buf, "%08x", (UINT)LangID);
916     return lpfnLocaleEnum( buf );
917 }
918
919 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
920                                        LPCWSTR name, WORD LangID, LONG lParam )
921 {
922     static const WCHAR formatW[] = {'%','0','8','x',0};
923     LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
924     WCHAR buf[20];
925     sprintfW( buf, formatW, (UINT)LangID );
926     return lpfnLocaleEnum( buf );
927 }
928
929 /******************************************************************************
930  *           EnumSystemLocalesA  (KERNEL32.@)
931  */
932 BOOL WINAPI EnumSystemLocalesA(LOCALE_ENUMPROCA lpfnLocaleEnum,
933                                    DWORD flags)
934 {
935     TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
936     EnumResourceLanguagesA( GetModuleHandleA("KERNEL32"), RT_STRINGA,
937                             (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
938                             (LONG)lpfnLocaleEnum);
939     return TRUE;
940 }
941
942
943 /******************************************************************************
944  *           EnumSystemLocalesW  (KERNEL32.@)
945  */
946 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD flags )
947 {
948     TRACE("(%p,%08lx)\n", lpfnLocaleEnum,flags);
949     EnumResourceLanguagesW( GetModuleHandleA("KERNEL32"), RT_STRINGW,
950                             (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
951                             (LONG)lpfnLocaleEnum);
952     return TRUE;
953 }
954
955
956 /***********************************************************************
957  *           VerLanguageNameA  (KERNEL32.@)
958  */
959 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
960 {
961     return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
962 }
963
964
965 /***********************************************************************
966  *           VerLanguageNameW  (KERNEL32.@)
967  */
968 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
969 {
970     return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
971 }
972
973
974 /******************************************************************************
975  *           GetStringTypeW    (KERNEL32.@)
976  */
977 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
978 {
979     if (count == -1) count = strlenW(src) + 1;
980     switch(type)
981     {
982     case CT_CTYPE1:
983         while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
984         break;
985     case CT_CTYPE2:
986         while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
987         break;
988     case CT_CTYPE3:
989     {
990         WARN("CT_CTYPE3: semi-stub.\n");
991         while (count--)
992         {
993             int c = *src;
994             WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
995
996             type1 = get_char_typeW( *src++ ) & 0xfff;
997             /* try to construct type3 from type1 */
998             if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
999             if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1000             if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1001             if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1002             if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1003             if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1004             if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1005
1006             if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1007             if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1008             if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1009             if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1010             if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1011             if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1012             if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1013             if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1014
1015             if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1016             if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1017             if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1018             if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1019             if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1020             if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1021             *chartype++ = type3;
1022         }
1023         break;
1024     }
1025     default:
1026         SetLastError( ERROR_INVALID_PARAMETER );
1027         return FALSE;
1028     }
1029     return TRUE;
1030 }
1031
1032
1033 /******************************************************************************
1034  *           GetStringTypeExW    (KERNEL32.@)
1035  */
1036 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1037 {
1038     /* locale is ignored for Unicode */
1039     return GetStringTypeW( type, src, count, chartype );
1040 }
1041
1042
1043 /******************************************************************************
1044  *           GetStringTypeA    (KERNEL32.@)
1045  */
1046 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1047 {
1048     UINT cp;
1049     INT countW;
1050     LPWSTR srcW;
1051     BOOL ret = FALSE;
1052
1053     if(count == -1) count = strlen(src) + 1;
1054
1055     if (!(cp = get_lcid_codepage( locale )))
1056     {
1057         FIXME("For locale %04lx using current ANSI code page\n", locale);
1058         cp = GetACP();
1059     }
1060
1061     countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1062     if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1063     {
1064         MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1065     /*
1066      * NOTE: the target buffer has 1 word for each CHARACTER in the source
1067      * string, with multibyte characters there maybe be more bytes in count
1068      * than character space in the buffer!
1069      */
1070         ret = GetStringTypeW(type, srcW, countW, chartype);
1071         HeapFree(GetProcessHeap(), 0, srcW);
1072     }
1073     return ret;
1074 }
1075
1076 /******************************************************************************
1077  *           GetStringTypeExA    (KERNEL32.@)
1078  */
1079 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1080 {
1081     return GetStringTypeA(locale, type, src, count, chartype);
1082 }
1083
1084
1085 /*************************************************************************
1086  *           LCMapStringW    (KERNEL32.@)
1087  */
1088 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
1089                         LPWSTR dst, INT dstlen)
1090 {
1091     LPWSTR dst_ptr;
1092
1093     if (!src || !srclen || dstlen < 0)
1094     {
1095         SetLastError(ERROR_INVALID_PARAMETER);
1096         return 0;
1097     }
1098
1099     /* mutually exclusive flags */
1100     if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
1101         (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
1102         (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
1103         (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
1104     {
1105         SetLastError(ERROR_INVALID_FLAGS);
1106         return 0;
1107     }
1108
1109     if (!dstlen) dst = NULL;
1110
1111     if (lcid == LOCALE_NEUTRAL || lcid == LOCALE_SYSTEM_DEFAULT) lcid = GetSystemDefaultLCID();
1112     else if (lcid == LOCALE_USER_DEFAULT) lcid = GetUserDefaultLCID();
1113
1114     if (flags & LCMAP_SORTKEY)
1115     {
1116         if (src == dst)
1117         {
1118             SetLastError(ERROR_INVALID_FLAGS);
1119             return 0;
1120         }
1121
1122         if (srclen < 0) srclen = strlenW(src);
1123
1124         TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1125               lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1126
1127         return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
1128     }
1129
1130     /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
1131     if (flags & SORT_STRINGSORT)
1132     {
1133         SetLastError(ERROR_INVALID_FLAGS);
1134         return 0;
1135     }
1136
1137     if (srclen < 0) srclen = strlenW(src) + 1;
1138
1139     TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
1140           lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1141
1142     if (!dst) /* return required string length */
1143     {
1144         INT len;
1145
1146         for (len = 0; srclen; src++, srclen--)
1147         {
1148             WCHAR wch = *src;
1149             /* tests show that win2k just ignores NORM_IGNORENONSPACE,
1150              * and skips white space and punctuation characters for
1151              * NORM_IGNORESYMBOLS.
1152              */
1153             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1154                 continue;
1155             len++;
1156         }
1157         return len;
1158     }
1159
1160     if (flags & LCMAP_UPPERCASE)
1161     {
1162         for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1163         {
1164             WCHAR wch = *src;
1165             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1166                 continue;
1167             *dst_ptr++ = toupperW(wch);
1168             dstlen--;
1169         }
1170     }
1171     else if (flags & LCMAP_LOWERCASE)
1172     {
1173         for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1174         {
1175             WCHAR wch = *src;
1176             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1177                 continue;
1178             *dst_ptr++ = tolowerW(wch);
1179             dstlen--;
1180         }
1181     }
1182     else
1183     {
1184         if (src == dst)
1185         {
1186             SetLastError(ERROR_INVALID_FLAGS);
1187             return 0;
1188         }
1189         for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1190         {
1191             WCHAR wch = *src;
1192             if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1193                 continue;
1194             *dst_ptr++ = wch;
1195             dstlen--;
1196         }
1197     }
1198
1199     return dst_ptr - dst;
1200 }
1201
1202 /*************************************************************************
1203  *           LCMapStringA    (KERNEL32.@)
1204  */
1205 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
1206                         LPSTR dst, INT dstlen)
1207 {
1208     WCHAR bufW[128];
1209     LPWSTR srcW, dstW;
1210     INT ret = 0, srclenW, dstlenW;
1211     UINT locale_cp;
1212
1213     if (!src || !srclen || dstlen < 0)
1214     {
1215         SetLastError(ERROR_INVALID_PARAMETER);
1216         return 0;
1217     }
1218
1219     GetLocaleInfoW(lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
1220                    (WCHAR *)&locale_cp, (sizeof(locale_cp)/sizeof(WCHAR)));
1221
1222     srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 128);
1223     if (srclenW)
1224         srcW = bufW;
1225     else
1226     {
1227         srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
1228         srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
1229         if (!srcW)
1230         {
1231             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1232             return 0;
1233         }
1234         MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
1235     }
1236
1237     if (flags & LCMAP_SORTKEY)
1238     {
1239         if (src == dst)
1240         {
1241             SetLastError(ERROR_INVALID_FLAGS);
1242             goto map_string_exit;
1243         }
1244         ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
1245         goto map_string_exit;
1246     }
1247
1248     if (flags & SORT_STRINGSORT)
1249     {
1250         SetLastError(ERROR_INVALID_FLAGS);
1251         goto map_string_exit;
1252     }
1253
1254     dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
1255     dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
1256     if (!dstW)
1257     {
1258         SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1259         goto map_string_exit;
1260     }
1261
1262     LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
1263     ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
1264     HeapFree(GetProcessHeap(), 0, dstW);
1265
1266 map_string_exit:
1267     if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
1268     return ret;
1269 }
1270
1271 /******************************************************************************
1272  *           CompareStringW    (KERNEL32.@)
1273  */
1274 INT WINAPI CompareStringW(LCID lcid, DWORD style,
1275                           LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
1276 {
1277     INT ret, len;
1278
1279     if (!str1 || !str2)
1280     {
1281         SetLastError(ERROR_INVALID_PARAMETER);
1282         return 0;
1283     }
1284
1285     if (len1 < 0) len1 = lstrlenW(str1);
1286     if (len2 < 0) len2 = lstrlenW(str2);
1287
1288     len = (len1 < len2) ? len1 : len2;
1289     ret = (style & NORM_IGNORECASE) ? strncmpiW(str1, str2, len) :
1290                                       strncmpW(str1, str2, len);
1291
1292     if (ret) /* need to translate result */
1293         return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
1294
1295     if (len1 == len2) return CSTR_EQUAL;
1296     /* the longer one is lexically greater */
1297     return (len1 < len2) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
1298 }
1299
1300 /******************************************************************************
1301  *           CompareStringA    (KERNEL32.@)
1302  */
1303 INT WINAPI CompareStringA(LCID lcid, DWORD style,
1304                           LPCSTR str1, INT len1, LPCSTR str2, INT len2)
1305 {
1306     WCHAR buf1W[128], buf2W[128];
1307     LPWSTR str1W, str2W;
1308     INT len1W, len2W, ret;
1309     UINT locale_cp;
1310
1311     if (!str1 || !str2)
1312     {
1313         SetLastError(ERROR_INVALID_PARAMETER);
1314         return 0;
1315     }
1316
1317     if (len1 < 0) len1 = strlen(str1);
1318     if (len2 < 0) len2 = strlen(str2);
1319
1320     GetLocaleInfoW(lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
1321                    (WCHAR *)&locale_cp, (sizeof(locale_cp)/sizeof(WCHAR)));
1322
1323     len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 128);
1324     if (len1W)
1325         str1W = buf1W;
1326     else
1327     {
1328         len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
1329         str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
1330         if (!str1W)
1331         {
1332             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1333             return 0;
1334         }
1335         MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
1336     }
1337     len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 128);
1338     if (len2W)
1339         str2W = buf2W;
1340     else
1341     {
1342         len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
1343         str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
1344         if (!str2W)
1345         {
1346             if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
1347             SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1348             return 0;
1349         }
1350         MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
1351     }
1352
1353     ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
1354
1355     if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
1356     if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
1357     return ret;
1358 }
1359
1360 /*************************************************************************
1361  *           lstrcmp     (KERNEL32.@)
1362  *           lstrcmpA    (KERNEL32.@)
1363  */
1364 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
1365 {
1366     int ret = CompareStringA(GetThreadLocale(), 0, str1, -1, str2, -1);
1367     if (ret) ret -= 2;
1368     return ret;
1369 }
1370
1371 /*************************************************************************
1372  *           lstrcmpi     (KERNEL32.@)
1373  *           lstrcmpiA    (KERNEL32.@)
1374  */
1375 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
1376 {
1377     int ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
1378     if (ret) ret -= 2;
1379     return ret;
1380 }
1381
1382 /*************************************************************************
1383  *           lstrcmpW    (KERNEL32.@)
1384  */
1385 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
1386 {
1387     int ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
1388     if (ret) ret -= 2;
1389     return ret;
1390 }
1391
1392 /*************************************************************************
1393  *           lstrcmpiW    (KERNEL32.@)
1394  */
1395 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
1396 {
1397     int ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
1398     if (ret) ret -= 2;
1399     return ret;
1400 }
1401
1402 /******************************************************************************
1403  *              LOCALE_Init
1404  */
1405 void LOCALE_Init(void)
1406 {
1407     UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp = -1;
1408     LCID lcid = init_default_lcid( &unix_cp );
1409
1410     NtSetDefaultLocale( FALSE, lcid );
1411     NtSetDefaultLocale( TRUE, lcid );
1412
1413     GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
1414                     (LPWSTR)&ansi_cp, sizeof(ansi_cp)/sizeof(WCHAR) );
1415     GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
1416                     (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
1417     GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
1418                     (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
1419     if (unix_cp == -1)
1420         GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
1421                     (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
1422
1423     CODEPAGE_Init( ansi_cp, oem_cp, mac_cp, unix_cp, lcid );
1424     update_registry( lcid );
1425 }
1426
1427 /******************************************************************************
1428  *           EnumSystemLanguageGroupsA    (KERNEL32.@)
1429  */
1430 BOOL WINAPI EnumSystemLanguageGroupsA(
1431   LANGUAGEGROUP_ENUMPROCA pLangGroupEnumProc, /* [in] callback function */
1432   DWORD dwFlags,                              /* [in] language groups */
1433   LONG_PTR  lParam                            /* [in] callback parameter */
1434 )
1435 {
1436   FIXME("stub\n");
1437   SetLastError( ERROR_INVALID_PARAMETER );
1438   return FALSE;
1439 }
1440
1441 /******************************************************************************
1442  *           EnumSystemLanguageGroupsW    (KERNEL32.@)
1443  */
1444 BOOL WINAPI EnumSystemLanguageGroupsW(
1445   LANGUAGEGROUP_ENUMPROCW pLangGroupEnumProc, /* [in] callback function */
1446   DWORD dwFlags,                              /* [in] language groups */
1447   LONG_PTR  lParam                            /* [in] callback parameter */
1448 )
1449 {
1450   FIXME("stub\n");
1451   SetLastError( ERROR_INVALID_PARAMETER );
1452   return FALSE;
1453 }
1454
1455 /******************************************************************************
1456  *           InvalidateNLSCache           (KERNEL32.@)
1457  */
1458 BOOL WINAPI InvalidateNLSCache(void)
1459 {
1460   FIXME("stub\n");
1461   return FALSE;
1462 }