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