Avoid copying invalid data on error.
[wine/wine-kai.git] / dlls / kernel / locale.c
blobfb1f992e53bee744848986b48115c619dcf84f7b
1 /*
2 * Locale support
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
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.
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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "config.h"
25 #include "wine/port.h"
27 #include <assert.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <stdlib.h>
34 #include "ntstatus.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winuser.h" /* for RT_STRINGW */
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "wine/unicode.h"
41 #include "winnls.h"
42 #include "winerror.h"
43 #include "winver.h"
44 #include "thread.h"
45 #include "kernel_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(nls);
50 #define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|LOCALE_RETURN_NUMBER)
52 /* current code pages */
53 static const union cptable *ansi_cptable;
54 static const union cptable *oem_cptable;
55 static const union cptable *mac_cptable;
56 static const union cptable *unix_cptable; /* NULL if UTF8 */
58 static HKEY NLS_RegOpenKey(HKEY hRootKey, LPCWSTR szKeyName);
59 static HKEY NLS_RegOpenSubKey(HKEY hRootKey, LPCWSTR szKeyName);
61 static const WCHAR szNlsKeyName[] = {
62 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
63 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
64 'C','o','n','t','r','o','l','\\','N','l','s','\0'
67 /* Charset to codepage map, sorted by name. */
68 static const struct charset_entry
70 const char *charset_name;
71 UINT codepage;
72 } charset_names[] =
74 { "BIG5", 950 },
75 { "CP1250", 1250 },
76 { "CP1251", 1251 },
77 { "CP1252", 1252 },
78 { "CP1253", 1253 },
79 { "CP1254", 1254 },
80 { "CP1255", 1255 },
81 { "CP1256", 1256 },
82 { "CP1257", 1257 },
83 { "CP1258", 1258 },
84 { "CP932", 932 },
85 { "CP936", 936 },
86 { "CP949", 949 },
87 { "CP950", 950 },
88 { "EUCJP", 20932 },
89 { "GB2312", 936 },
90 { "IBM037", 37 },
91 { "IBM1026", 1026 },
92 { "IBM424", 424 },
93 { "IBM437", 437 },
94 { "IBM500", 500 },
95 { "IBM850", 850 },
96 { "IBM852", 852 },
97 { "IBM855", 855 },
98 { "IBM857", 857 },
99 { "IBM860", 860 },
100 { "IBM861", 861 },
101 { "IBM862", 862 },
102 { "IBM863", 863 },
103 { "IBM864", 864 },
104 { "IBM865", 865 },
105 { "IBM866", 866 },
106 { "IBM869", 869 },
107 { "IBM874", 874 },
108 { "IBM875", 875 },
109 { "ISO88591", 28591 },
110 { "ISO885910", 28600 },
111 { "ISO885913", 28603 },
112 { "ISO885914", 28604 },
113 { "ISO885915", 28605 },
114 { "ISO88592", 28592 },
115 { "ISO88593", 28593 },
116 { "ISO88594", 28594 },
117 { "ISO88595", 28595 },
118 { "ISO88596", 28596 },
119 { "ISO88597", 28597 },
120 { "ISO88598", 28598 },
121 { "ISO88599", 28599 },
122 { "KOI8R", 20866 },
123 { "KOI8U", 20866 },
124 { "UTF8", CP_UTF8 }
127 #define NLS_MAX_LANGUAGES 20
128 typedef struct {
129 WCHAR lang[128];
130 WCHAR country[4];
131 LANGID found_lang_id[NLS_MAX_LANGUAGES];
132 WCHAR found_language[NLS_MAX_LANGUAGES][3];
133 WCHAR found_country[NLS_MAX_LANGUAGES][3];
134 int n_found;
135 } LANG_FIND_DATA;
138 /* copy Unicode string to Ascii without using codepages */
139 static inline void strcpyWtoA( char *dst, const WCHAR *src )
141 while ((*dst++ = *src++));
144 /* Copy Ascii string to Unicode without using codepages */
145 static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
147 while (n > 1 && *src)
149 *dst++ = (unsigned char)*src++;
150 n--;
152 if (n) *dst = 0;
155 /* return a printable string for a language id */
156 static const char *debugstr_lang( LANGID lang )
158 WCHAR langW[4], countryW[4];
159 char buffer[8];
160 LCID lcid = MAKELCID( lang, SORT_DEFAULT );
162 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE, langW, sizeof(langW)/sizeof(WCHAR));
163 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE, countryW, sizeof(countryW)/sizeof(WCHAR));
164 strcpyWtoA( buffer, langW );
165 strcat( buffer, "_" );
166 strcpyWtoA( buffer + strlen(buffer), countryW );
167 return wine_dbg_sprintf( "%s", buffer );
170 /***********************************************************************
171 * get_lcid_codepage
173 * Retrieve the ANSI codepage for a given locale.
175 inline static UINT get_lcid_codepage( LCID lcid )
177 UINT ret;
178 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
179 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
180 return ret;
184 /***********************************************************************
185 * get_codepage_table
187 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
189 static const union cptable *get_codepage_table( unsigned int codepage )
191 const union cptable *ret = NULL;
193 assert( ansi_cptable ); /* init must have been done already */
195 switch(codepage)
197 case CP_ACP:
198 return ansi_cptable;
199 case CP_OEMCP:
200 return oem_cptable;
201 case CP_MACCP:
202 return mac_cptable;
203 case CP_UTF7:
204 case CP_UTF8:
205 break;
206 case CP_THREAD_ACP:
207 if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
208 /* fall through */
209 default:
210 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
211 if (codepage == oem_cptable->info.codepage) return oem_cptable;
212 if (codepage == mac_cptable->info.codepage) return mac_cptable;
213 ret = wine_cp_get_table( codepage );
214 break;
216 return ret;
219 /***********************************************************************
220 * create_registry_key
222 * Create the Control Panel\\International registry key.
224 inline static HKEY create_registry_key(void)
226 static const WCHAR intlW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
227 'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
228 OBJECT_ATTRIBUTES attr;
229 UNICODE_STRING nameW;
230 HKEY hkey;
232 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
234 attr.Length = sizeof(attr);
235 attr.RootDirectory = hkey;
236 attr.ObjectName = &nameW;
237 attr.Attributes = 0;
238 attr.SecurityDescriptor = NULL;
239 attr.SecurityQualityOfService = NULL;
240 RtlInitUnicodeString( &nameW, intlW );
242 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS) hkey = 0;
243 NtClose( attr.RootDirectory );
244 return hkey;
248 /***********************************************************************
249 * LOCALE_InitRegistry
251 * Update registry contents on startup if the user locale has changed.
252 * This simulates the action of the Windows control panel.
254 void LOCALE_InitRegistry(void)
256 static const WCHAR CodepageW[] = {'C','o','d','e','p','a','g','e',0};
257 static const WCHAR acpW[] = {'A','C','P',0};
258 static const WCHAR oemcpW[] = {'O','E','M','C','P',0};
259 static const WCHAR maccpW[] = {'M','A','C','C','P',0};
260 static const struct
262 LPCWSTR name;
263 USHORT value;
264 } update_cp_values[] = {
265 { acpW, LOCALE_IDEFAULTANSICODEPAGE },
266 { oemcpW, LOCALE_IDEFAULTCODEPAGE },
267 { maccpW, LOCALE_IDEFAULTMACCODEPAGE }
269 static const USHORT updateValues[] = {
270 LOCALE_SLANGUAGE,
271 LOCALE_SCOUNTRY, LOCALE_ICOUNTRY,
272 LOCALE_S1159, LOCALE_S2359,
273 LOCALE_STIME, LOCALE_ITIME,
274 LOCALE_ITLZERO,
275 LOCALE_SSHORTDATE,
276 LOCALE_SLONGDATE,
277 LOCALE_SDATE,
278 LOCALE_SCURRENCY, LOCALE_ICURRENCY,
279 LOCALE_INEGCURR,
280 LOCALE_ICURRDIGITS,
281 LOCALE_SDECIMAL,
282 LOCALE_SLIST,
283 LOCALE_STHOUSAND,
284 LOCALE_IDIGITS,
285 LOCALE_IDIGITSUBSTITUTION,
286 LOCALE_SNATIVEDIGITS,
287 LOCALE_ITIMEMARKPOSN,
288 LOCALE_ICALENDARTYPE,
289 LOCALE_ILZERO,
290 LOCALE_IMEASURE
292 static const WCHAR LocaleW[] = {'L','o','c','a','l','e',0};
293 UNICODE_STRING nameW;
294 char buffer[20];
295 WCHAR bufferW[80];
296 DWORD count, i;
297 HKEY hkey;
298 LCID lcid = GetUserDefaultLCID();
300 if (!(hkey = create_registry_key()))
301 return; /* don't do anything if we can't create the registry key */
303 RtlInitUnicodeString( &nameW, LocaleW );
304 count = sizeof(bufferW);
305 if (!NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation, (LPBYTE)bufferW, count, &count))
307 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
308 LPCWSTR szValueText = (LPCWSTR)info->Data;
310 if (strtoulW( szValueText, NULL, 16 ) == lcid) /* already set correctly */
312 NtClose( hkey );
313 return;
315 TRACE( "updating registry, locale changed %s -> %08lx\n", debugstr_w(szValueText), lcid );
317 else TRACE( "updating registry, locale changed none -> %08lx\n", lcid );
319 sprintf( buffer, "%08lx", lcid );
320 /* Note: '9' constant below is strlen(buffer) + 1 */
321 RtlMultiByteToUnicodeN( bufferW, sizeof(bufferW), NULL, buffer, 9 );
322 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, 9 * sizeof(WCHAR) );
323 NtClose( hkey );
325 for (i = 0; i < sizeof(updateValues)/sizeof(updateValues[0]); i++)
327 GetLocaleInfoW( lcid, updateValues[i] | LOCALE_NOUSEROVERRIDE, bufferW,
328 sizeof(bufferW)/sizeof(WCHAR) );
329 SetLocaleInfoW( lcid, updateValues[i], bufferW );
332 hkey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), CodepageW );
334 for (i = 0; i < sizeof(update_cp_values)/sizeof(update_cp_values[0]); i++)
336 count = GetLocaleInfoW( lcid, update_cp_values[i].value | LOCALE_NOUSEROVERRIDE,
337 bufferW, sizeof(bufferW)/sizeof(WCHAR) );
338 RtlInitUnicodeString( &nameW, update_cp_values[i].name );
339 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, count * sizeof(WCHAR) );
342 NtClose( hkey );
346 /***********************************************************************
347 * find_language_id_proc
349 static BOOL CALLBACK find_language_id_proc( HMODULE hModule, LPCWSTR type,
350 LPCWSTR name, WORD LangID, LPARAM lParam )
352 LANG_FIND_DATA *l_data = (LANG_FIND_DATA *)lParam;
353 LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
354 WCHAR buf_language[128];
355 WCHAR buf_country[128];
356 WCHAR buf_en_language[128];
358 if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
359 return TRUE; /* continue search */
361 buf_language[0] = 0;
362 buf_country[0] = 0;
364 GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME|LOCALE_NOUSEROVERRIDE,
365 buf_language, sizeof(buf_language)/sizeof(WCHAR));
366 GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME|LOCALE_NOUSEROVERRIDE,
367 buf_country, sizeof(buf_country)/sizeof(WCHAR));
369 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_language))
371 if(l_data->country[0])
373 if(!strcmpiW(l_data->country, buf_country))
375 l_data->found_lang_id[0] = LangID;
376 l_data->n_found = 1;
377 TRACE("Found id %04X for lang %s country %s\n",
378 LangID, debugstr_w(l_data->lang), debugstr_w(l_data->country));
379 return FALSE; /* stop enumeration */
382 else goto found; /* l_data->country not specified */
385 /* Just in case, check LOCALE_SENGLANGUAGE too,
386 * in hope that possible alias name might have that value.
388 buf_en_language[0] = 0;
389 GetLocaleInfoW(lcid, LOCALE_SENGLANGUAGE|LOCALE_NOUSEROVERRIDE,
390 buf_en_language, sizeof(buf_en_language)/sizeof(WCHAR));
392 if(l_data->lang[0] && !strcmpiW(l_data->lang, buf_en_language)) goto found;
393 return TRUE; /* not found, continue search */
395 found:
396 l_data->found_lang_id[l_data->n_found] = LangID;
397 strncpyW(l_data->found_country[l_data->n_found], buf_country, 3);
398 strncpyW(l_data->found_language[l_data->n_found], buf_language, 3);
399 l_data->n_found++;
400 TRACE("Found id %04X for lang %s\n", LangID, debugstr_w(l_data->lang));
401 return (l_data->n_found < NLS_MAX_LANGUAGES); /* continue search, unless we have enough */
405 /***********************************************************************
406 * get_language_id
408 * INPUT:
409 * Lang: a string whose two first chars are the iso name of a language.
410 * Country: a string whose two first chars are the iso name of country
411 * Charset: a string defining the chosen charset encoding
412 * Dialect: a string defining a variation of the locale
414 * all those values are from the standardized format of locale
415 * name in unix which is: Lang[_Country][.Charset][@Dialect]
417 * RETURNS:
418 * the numeric code of the language used by Windows
420 * FIXME: Charset and Dialect are not handled
422 static LANGID get_language_id(LPCSTR Lang, LPCSTR Country, LPCSTR Charset, LPCSTR Dialect)
424 LANG_FIND_DATA l_data;
426 if(!Lang)
428 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
429 goto END;
432 l_data.n_found = 0;
433 strcpynAtoW(l_data.lang, Lang, sizeof(l_data.lang));
435 if (Country) strcpynAtoW(l_data.country, Country, sizeof(l_data.country));
436 else l_data.country[0] = 0;
438 EnumResourceLanguagesW(kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
439 find_language_id_proc, (LPARAM)&l_data);
441 if (l_data.n_found == 1) goto END;
443 if(!l_data.n_found)
445 if(l_data.country[0])
447 /* retry without country name */
448 l_data.country[0] = 0;
449 EnumResourceLanguagesW(kernel32_handle, (LPCWSTR)RT_STRING, (LPCWSTR)LOCALE_ILANGUAGE,
450 find_language_id_proc, (LONG)&l_data);
451 if (!l_data.n_found)
453 MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to English.\n",
454 Lang, Country);
455 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
457 else MESSAGE("Warning: Language '%s_%s' was not recognized, defaulting to '%s'.\n",
458 Lang, Country, debugstr_lang(l_data.found_lang_id[0]) );
460 else
462 MESSAGE("Warning: Language '%s' was not recognized, defaulting to English.\n", Lang);
463 l_data.found_lang_id[0] = MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT);
466 else
468 int i;
470 if (Country && Country[0])
471 MESSAGE("For language '%s_%s' several language ids were found:\n", Lang, Country);
472 else
473 MESSAGE("For language '%s' several language ids were found:\n", Lang);
475 /* print a list of languages with their description */
476 for (i = 0; i < l_data.n_found; i++)
478 WCHAR buffW[128];
479 char buffA[128];
480 GetLocaleInfoW( MAKELCID( l_data.found_lang_id[i], SORT_DEFAULT ),
481 LOCALE_SLANGUAGE|LOCALE_NOUSEROVERRIDE, buffW, sizeof(buffW)/sizeof(WCHAR));
482 strcpyWtoA( buffA, buffW );
483 MESSAGE( " %s (%04X) - %s\n", debugstr_lang(l_data.found_lang_id[i]),
484 l_data.found_lang_id[i], buffA );
486 MESSAGE("Defaulting to '%s'. You should specify the exact language you want\n"
487 "by defining your LANG environment variable like this: LANG=%s\n",
488 debugstr_lang(l_data.found_lang_id[0]), debugstr_lang(l_data.found_lang_id[0]) );
490 END:
491 TRACE("Returning %04X (%s)\n", l_data.found_lang_id[0], debugstr_lang(l_data.found_lang_id[0]));
492 return l_data.found_lang_id[0];
496 /***********************************************************************
497 * charset_cmp (internal)
499 static int charset_cmp( const void *name, const void *entry )
501 const struct charset_entry *charset = (const struct charset_entry *)entry;
502 return strcasecmp( (const char *)name, charset->charset_name );
505 /***********************************************************************
506 * get_env_lcid
508 static LCID get_env_lcid( UINT *unix_cp, const char *env_str )
510 char *buf, *lang,*country,*charset,*dialect,*next;
511 LCID ret = 0;
513 if ((lang = getenv( "LC_ALL" )) ||
514 (env_str && (lang = getenv( env_str ))) ||
515 (lang = getenv( "LANG" )))
517 if (!strcmp(lang,"POSIX") || !strcmp(lang,"C")) goto done;
519 buf = RtlAllocateHeap( GetProcessHeap(), 0, strlen(lang) + 1 );
520 strcpy( buf, lang );
521 lang=buf;
523 do {
524 next=strchr(lang,':'); if (next) *next++='\0';
525 dialect=strchr(lang,'@'); if (dialect) *dialect++='\0';
526 charset=strchr(lang,'.'); if (charset) *charset++='\0';
527 country=strchr(lang,'_'); if (country) *country++='\0';
529 ret = get_language_id(lang, country, charset, dialect);
530 if (ret && charset && unix_cp)
532 const struct charset_entry *entry;
533 char charset_name[16];
534 size_t i, j;
536 /* remove punctuation characters from charset name */
537 for (i = j = 0; charset[i] && j < sizeof(charset_name)-1; i++)
538 if (isalnum(charset[i])) charset_name[j++] = charset[i];
539 charset_name[j] = 0;
541 entry = bsearch( charset_name, charset_names,
542 sizeof(charset_names)/sizeof(charset_names[0]),
543 sizeof(charset_names[0]), charset_cmp );
544 if (entry)
546 *unix_cp = entry->codepage;
547 TRACE("charset %s was mapped to cp %u\n", charset, *unix_cp);
549 else
550 FIXME("charset %s was not recognized\n", charset);
553 lang=next;
554 } while (lang && !ret);
556 if (!ret) MESSAGE("Warning: language '%s' not recognized, defaulting to English\n", buf);
557 RtlFreeHeap( GetProcessHeap(), 0, buf );
560 done:
561 if (!ret) ret = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT) ;
562 return ret;
566 /***********************************************************************
567 * GetUserDefaultLangID (KERNEL32.@)
569 * Get the default language Id for the current user.
571 * PARAMS
572 * None.
574 * RETURNS
575 * The current LANGID of the default language for the current user.
577 LANGID WINAPI GetUserDefaultLangID(void)
579 return LANGIDFROMLCID(GetUserDefaultLCID());
583 /***********************************************************************
584 * GetSystemDefaultLangID (KERNEL32.@)
586 * Get the default language Id for the system.
588 * PARAMS
589 * None.
591 * RETURNS
592 * The current LANGID of the default language for the system.
594 LANGID WINAPI GetSystemDefaultLangID(void)
596 return LANGIDFROMLCID(GetSystemDefaultLCID());
600 /***********************************************************************
601 * GetUserDefaultLCID (KERNEL32.@)
603 * Get the default locale Id for the current user.
605 * PARAMS
606 * None.
608 * RETURNS
609 * The current LCID of the default locale for the current user.
611 LCID WINAPI GetUserDefaultLCID(void)
613 LCID lcid;
614 NtQueryDefaultLocale( TRUE, &lcid );
615 return lcid;
619 /***********************************************************************
620 * GetSystemDefaultLCID (KERNEL32.@)
622 * Get the default locale Id for the system.
624 * PARAMS
625 * None.
627 * RETURNS
628 * The current LCID of the default locale for the system.
630 LCID WINAPI GetSystemDefaultLCID(void)
632 LCID lcid;
633 NtQueryDefaultLocale( FALSE, &lcid );
634 return lcid;
638 /***********************************************************************
639 * GetUserDefaultUILanguage (KERNEL32.@)
641 * Get the default user interface language Id for the current user.
643 * PARAMS
644 * None.
646 * RETURNS
647 * The current LANGID of the default UI language for the current user.
649 LANGID WINAPI GetUserDefaultUILanguage(void)
651 LANGID lang;
652 NtQueryDefaultUILanguage( &lang );
653 return lang;
657 /***********************************************************************
658 * GetSystemDefaultUILanguage (KERNEL32.@)
660 * Get the default user interface language Id for the system.
662 * PARAMS
663 * None.
665 * RETURNS
666 * The current LANGID of the default UI language for the system. This is
667 * typically the same language used during the installation process.
669 LANGID WINAPI GetSystemDefaultUILanguage(void)
671 LANGID lang;
672 NtQueryInstallUILanguage( &lang );
673 return lang;
677 /******************************************************************************
678 * get_locale_value_name
680 * Gets the registry value name for a given lctype.
682 static const WCHAR *get_locale_value_name( DWORD lctype )
684 static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
685 static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
686 static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
687 static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
688 static const WCHAR iDateW[] = {'i','D','a','t','e',0};
689 static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
690 static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
691 static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
692 static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
693 static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
694 static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
695 static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
696 static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
697 static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
698 static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
699 static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
700 static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
701 static const WCHAR s1159W[] = {'s','1','1','5','9',0};
702 static const WCHAR s2359W[] = {'s','2','3','5','9',0};
703 static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
704 static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
705 static const WCHAR sDateW[] = {'s','D','a','t','e',0};
706 static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
707 static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
708 static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
709 static const WCHAR sListW[] = {'s','L','i','s','t',0};
710 static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
711 static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
712 static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
713 static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
714 static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
715 static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
716 static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
717 static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
718 static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
719 static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
720 static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
721 static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
722 static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
724 switch (lctype)
726 /* These values are used by SetLocaleInfo and GetLocaleInfo, and
727 * the values are stored in the registry, confirmed under Windows.
729 case LOCALE_ICALENDARTYPE: return iCalendarTypeW;
730 case LOCALE_ICURRDIGITS: return iCurrDigitsW;
731 case LOCALE_ICURRENCY: return iCurrencyW;
732 case LOCALE_IDIGITS: return iDigitsW;
733 case LOCALE_IFIRSTDAYOFWEEK: return iFirstDayOfWeekW;
734 case LOCALE_IFIRSTWEEKOFYEAR: return iFirstWeekOfYearW;
735 case LOCALE_ILZERO: return iLZeroW;
736 case LOCALE_IMEASURE: return iMeasureW;
737 case LOCALE_INEGCURR: return iNegCurrW;
738 case LOCALE_INEGNUMBER: return iNegNumberW;
739 case LOCALE_IPAPERSIZE: return iPaperSizeW;
740 case LOCALE_ITIME: return iTimeW;
741 case LOCALE_S1159: return s1159W;
742 case LOCALE_S2359: return s2359W;
743 case LOCALE_SCURRENCY: return sCurrencyW;
744 case LOCALE_SDATE: return sDateW;
745 case LOCALE_SDECIMAL: return sDecimalW;
746 case LOCALE_SGROUPING: return sGroupingW;
747 case LOCALE_SLIST: return sListW;
748 case LOCALE_SLONGDATE: return sLongDateW;
749 case LOCALE_SMONDECIMALSEP: return sMonDecimalSepW;
750 case LOCALE_SMONGROUPING: return sMonGroupingW;
751 case LOCALE_SMONTHOUSANDSEP: return sMonThousandSepW;
752 case LOCALE_SNEGATIVESIGN: return sNegativeSignW;
753 case LOCALE_SPOSITIVESIGN: return sPositiveSignW;
754 case LOCALE_SSHORTDATE: return sShortDateW;
755 case LOCALE_STHOUSAND: return sThousandW;
756 case LOCALE_STIME: return sTimeW;
757 case LOCALE_STIMEFORMAT: return sTimeFormatW;
758 case LOCALE_SYEARMONTH: return sYearMonthW;
760 /* The following are not listed under MSDN as supported,
761 * but seem to be used and also stored in the registry.
763 case LOCALE_ICOUNTRY: return iCountryW;
764 case LOCALE_IDATE: return iDateW;
765 case LOCALE_ILDATE: return iLDateW;
766 case LOCALE_ITLZERO: return iTLZeroW;
767 case LOCALE_SCOUNTRY: return sCountryW;
768 case LOCALE_SLANGUAGE: return sLanguageW;
770 /* The following are used in XP and later */
771 case LOCALE_IDIGITSUBSTITUTION: return NumShapeW;
772 case LOCALE_SNATIVEDIGITS: return sNativeDigitsW;
773 case LOCALE_ITIMEMARKPOSN: return iTimePrefixW;
775 return NULL;
779 /******************************************************************************
780 * get_registry_locale_info
782 * Retrieve user-modified locale info from the registry.
783 * Return length, 0 on error, -1 if not found.
785 static INT get_registry_locale_info( LPCWSTR value, LPWSTR buffer, INT len )
787 DWORD size;
788 INT ret;
789 HKEY hkey;
790 NTSTATUS status;
791 UNICODE_STRING nameW;
792 KEY_VALUE_PARTIAL_INFORMATION *info;
793 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
795 if (!(hkey = create_registry_key())) return -1;
797 RtlInitUnicodeString( &nameW, value );
798 size = info_size + len * sizeof(WCHAR);
800 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
802 NtClose( hkey );
803 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
804 return 0;
807 status = NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, info, size, &size );
808 if (status == STATUS_BUFFER_OVERFLOW && !buffer) status = 0;
810 if (!status)
812 ret = (size - info_size) / sizeof(WCHAR);
813 /* append terminating null if needed */
814 if (!ret || ((WCHAR *)info->Data)[ret-1])
816 if (ret < len || !buffer) ret++;
817 else
819 SetLastError( ERROR_INSUFFICIENT_BUFFER );
820 ret = 0;
823 if (ret && buffer)
825 memcpy( buffer, info->Data, (ret-1) * sizeof(WCHAR) );
826 buffer[ret-1] = 0;
829 else
831 if (status == STATUS_OBJECT_NAME_NOT_FOUND) ret = -1;
832 else
834 SetLastError( RtlNtStatusToDosError(status) );
835 ret = 0;
838 NtClose( hkey );
839 HeapFree( GetProcessHeap(), 0, info );
840 return ret;
844 /******************************************************************************
845 * GetLocaleInfoA (KERNEL32.@)
847 * Get information about an aspect of a locale.
849 * PARAMS
850 * lcid [I] LCID of the locale
851 * lctype [I] LCTYPE_ flags from "winnls.h"
852 * buffer [O] Destination for the information
853 * len [I] Length of buffer in characters
855 * RETURNS
856 * Success: The size of the data requested. If buffer is non-NULL, it is filled
857 * with the information.
858 * Failure: 0. Use GetLastError() to determine the cause.
860 * NOTES
861 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
862 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
863 * which is a bit string.
865 INT WINAPI GetLocaleInfoA( LCID lcid, LCTYPE lctype, LPSTR buffer, INT len )
867 WCHAR *bufferW;
868 INT lenW, ret;
870 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d)\n", lcid, lctype, buffer, len );
872 if (len < 0 || (len && !buffer))
874 SetLastError( ERROR_INVALID_PARAMETER );
875 return 0;
877 if (!len) buffer = NULL;
879 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
881 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
883 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
884 return 0;
886 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
888 if ((lctype & LOCALE_RETURN_NUMBER) ||
889 ((lctype & ~LOCALE_LOCALEINFOFLAGSMASK) == LOCALE_FONTSIGNATURE))
891 /* it's not an ASCII string, just bytes */
892 ret *= sizeof(WCHAR);
893 if (buffer)
895 if (ret <= len) memcpy( buffer, bufferW, ret );
896 else
898 SetLastError( ERROR_INSUFFICIENT_BUFFER );
899 ret = 0;
903 else
905 UINT codepage = CP_ACP;
906 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
907 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
910 HeapFree( GetProcessHeap(), 0, bufferW );
911 return ret;
915 /******************************************************************************
916 * GetLocaleInfoW (KERNEL32.@)
918 * See GetLocaleInfoA.
920 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
922 LANGID lang_id;
923 HRSRC hrsrc;
924 HGLOBAL hmem;
925 INT ret;
926 UINT lcflags;
927 const WCHAR *p;
928 unsigned int i;
930 if (len < 0 || (len && !buffer))
932 SetLastError( ERROR_INVALID_PARAMETER );
933 return 0;
935 if (!len) buffer = NULL;
937 lcid = ConvertDefaultLocale(lcid);
939 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
940 lctype &= 0xffff;
942 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d)\n", lcid, lctype, buffer, len );
944 /* first check for overrides in the registry */
946 if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == GetUserDefaultLCID())
948 const WCHAR *value = get_locale_value_name(lctype);
950 if (value)
952 if (lcflags & LOCALE_RETURN_NUMBER)
954 WCHAR tmp[16];
955 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
956 if (ret > 0)
958 WCHAR *end;
959 UINT number = strtolW( tmp, &end, 10 );
960 if (*end) /* invalid number */
962 SetLastError( ERROR_INVALID_FLAGS );
963 return 0;
965 ret = sizeof(UINT)/sizeof(WCHAR);
966 if (!buffer) return ret;
967 if (ret > len)
969 SetLastError( ERROR_INSUFFICIENT_BUFFER );
970 return 0;
972 memcpy( buffer, &number, sizeof(number) );
975 else ret = get_registry_locale_info( value, buffer, len );
977 if (ret != -1) return ret;
981 /* now load it from kernel resources */
983 lang_id = LANGIDFROMLCID( lcid );
985 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
986 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
987 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
989 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
990 (LPCWSTR)((lctype >> 4) + 1), lang_id )))
992 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
993 return 0;
995 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
996 return 0;
998 p = LockResource( hmem );
999 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
1001 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
1002 else ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
1004 if (!buffer) return ret;
1006 if (ret > len)
1008 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1009 return 0;
1012 if (lcflags & LOCALE_RETURN_NUMBER)
1014 UINT number;
1015 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
1016 if (!tmp) return 0;
1017 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
1018 tmp[*p] = 0;
1019 number = strtolW( tmp, &end, 10 );
1020 if (!*end)
1021 memcpy( buffer, &number, sizeof(number) );
1022 else /* invalid number */
1024 SetLastError( ERROR_INVALID_FLAGS );
1025 ret = 0;
1027 HeapFree( GetProcessHeap(), 0, tmp );
1029 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning number %d\n",
1030 lcid, lctype, buffer, len, number );
1032 else
1034 memcpy( buffer, p + 1, *p * sizeof(WCHAR) );
1035 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
1037 TRACE( "(lcid=0x%lx,lctype=0x%lx,%p,%d) returning %d %s\n",
1038 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1040 return ret;
1044 /******************************************************************************
1045 * SetLocaleInfoA [KERNEL32.@]
1047 * Set information about an aspect of a locale.
1049 * PARAMS
1050 * lcid [I] LCID of the locale
1051 * lctype [I] LCTYPE_ flags from "winnls.h"
1052 * data [I] Information to set
1054 * RETURNS
1055 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1056 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1057 * Failure: FALSE. Use GetLastError() to determine the cause.
1059 * NOTES
1060 * - Values are only be set for the current user locale; the system locale
1061 * settings cannot be changed.
1062 * - Any settings changed by this call are lost when the locale is changed by
1063 * the control panel (in Wine, this happens every time you change LANG).
1064 * - The native implementation of this function does not check that lcid matches
1065 * the current user locale, and simply sets the new values. Wine warns you in
1066 * this case, but behaves the same.
1068 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
1070 UINT codepage = CP_ACP;
1071 WCHAR *strW;
1072 DWORD len;
1073 BOOL ret;
1075 lcid = ConvertDefaultLocale(lcid);
1077 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
1079 if (!data)
1081 SetLastError( ERROR_INVALID_PARAMETER );
1082 return FALSE;
1084 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1085 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1087 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1088 return FALSE;
1090 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
1091 ret = SetLocaleInfoW( lcid, lctype, strW );
1092 HeapFree( GetProcessHeap(), 0, strW );
1093 return ret;
1097 /******************************************************************************
1098 * SetLocaleInfoW (KERNEL32.@)
1100 * See SetLocaleInfoA.
1102 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
1104 const WCHAR *value;
1105 static const WCHAR intlW[] = {'i','n','t','l',0 };
1106 UNICODE_STRING valueW;
1107 NTSTATUS status;
1108 HKEY hkey;
1110 lcid = ConvertDefaultLocale(lcid);
1112 lctype &= 0xffff;
1113 value = get_locale_value_name( lctype );
1115 if (!data || !value)
1117 SetLastError( ERROR_INVALID_PARAMETER );
1118 return FALSE;
1121 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
1123 SetLastError( ERROR_INVALID_FLAGS );
1124 return FALSE;
1127 if (lcid != GetUserDefaultLCID())
1129 /* Windows does not check that the lcid matches the current lcid */
1130 WARN("locale 0x%08lx isn't the current locale (0x%08lx), setting anyway!\n",
1131 lcid, GetUserDefaultLCID());
1134 TRACE("setting %lx (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
1136 /* FIXME: should check that data to set is sane */
1138 /* FIXME: profile functions should map to registry */
1139 WriteProfileStringW( intlW, value, data );
1141 if (!(hkey = create_registry_key())) return FALSE;
1142 RtlInitUnicodeString( &valueW, value );
1143 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1145 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1147 /* Set I-value from S value */
1148 WCHAR *lpD, *lpM, *lpY;
1149 WCHAR szBuff[2];
1151 lpD = strrchrW(data, 'd');
1152 lpM = strrchrW(data, 'M');
1153 lpY = strrchrW(data, 'y');
1155 if (lpD <= lpM)
1157 szBuff[0] = '1'; /* D-M-Y */
1159 else
1161 if (lpY <= lpM)
1162 szBuff[0] = '2'; /* Y-M-D */
1163 else
1164 szBuff[0] = '0'; /* M-D-Y */
1167 szBuff[1] = '\0';
1169 if (lctype == LOCALE_SSHORTDATE)
1170 lctype = LOCALE_IDATE;
1171 else
1172 lctype = LOCALE_ILDATE;
1174 value = get_locale_value_name( lctype );
1176 WriteProfileStringW( intlW, value, szBuff );
1178 RtlInitUnicodeString( &valueW, value );
1179 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1182 NtClose( hkey );
1184 if (status) SetLastError( RtlNtStatusToDosError(status) );
1185 return !status;
1189 /******************************************************************************
1190 * GetACP (KERNEL32.@)
1192 * Get the current Ansi code page Id for the system.
1194 * PARAMS
1195 * None.
1197 * RETURNS
1198 * The current Ansi code page identifier for the system.
1200 UINT WINAPI GetACP(void)
1202 assert( ansi_cptable );
1203 return ansi_cptable->info.codepage;
1207 /***********************************************************************
1208 * GetOEMCP (KERNEL32.@)
1210 * Get the current OEM code page Id for the system.
1212 * PARAMS
1213 * None.
1215 * RETURNS
1216 * The current OEM code page identifier for the system.
1218 UINT WINAPI GetOEMCP(void)
1220 assert( oem_cptable );
1221 return oem_cptable->info.codepage;
1225 /***********************************************************************
1226 * IsValidCodePage (KERNEL32.@)
1228 * Determine if a given code page identifier is valid.
1230 * PARAMS
1231 * codepage [I] Code page Id to verify.
1233 * RETURNS
1234 * TRUE, If codepage is valid and available on the system,
1235 * FALSE otherwise.
1237 BOOL WINAPI IsValidCodePage( UINT codepage )
1239 switch(codepage) {
1240 case CP_UTF7:
1241 case CP_UTF8:
1242 return TRUE;
1243 default:
1244 return wine_cp_get_table( codepage ) != NULL;
1249 /***********************************************************************
1250 * IsDBCSLeadByteEx (KERNEL32.@)
1252 * Determine if a character is a lead byte in a given code page.
1254 * PARAMS
1255 * codepage [I] Code page for the test.
1256 * testchar [I] Character to test
1258 * RETURNS
1259 * TRUE, if testchar is a lead byte in codepage,
1260 * FALSE otherwise.
1262 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
1264 const union cptable *table = get_codepage_table( codepage );
1265 return table && is_dbcs_leadbyte( table, testchar );
1269 /***********************************************************************
1270 * IsDBCSLeadByte (KERNEL32.@)
1271 * IsDBCSLeadByte (KERNEL.207)
1273 * Determine if a character is a lead byte.
1275 * PARAMS
1276 * testchar [I] Character to test
1278 * RETURNS
1279 * TRUE, if testchar is a lead byte in the Ansii code page,
1280 * FALSE otherwise.
1282 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
1284 if (!ansi_cptable) return FALSE;
1285 return is_dbcs_leadbyte( ansi_cptable, testchar );
1289 /***********************************************************************
1290 * GetCPInfo (KERNEL32.@)
1292 * Get information about a code page.
1294 * PARAMS
1295 * codepage [I] Code page number
1296 * cpinfo [O] Destination for code page information
1298 * RETURNS
1299 * Success: TRUE. cpinfo is updated with the information about codepage.
1300 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1302 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
1304 const union cptable *table = get_codepage_table( codepage );
1306 if (!table)
1308 switch(codepage)
1310 case CP_UTF7:
1311 case CP_UTF8:
1312 cpinfo->DefaultChar[0] = 0x3f;
1313 cpinfo->DefaultChar[1] = 0;
1314 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1315 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
1316 return TRUE;
1319 SetLastError( ERROR_INVALID_PARAMETER );
1320 return FALSE;
1322 if (table->info.def_char & 0xff00)
1324 cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
1325 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
1327 else
1329 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
1330 cpinfo->DefaultChar[1] = 0;
1332 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
1333 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
1334 else
1335 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
1337 return TRUE;
1340 /***********************************************************************
1341 * GetCPInfoExA (KERNEL32.@)
1343 * Get extended information about a code page.
1345 * PARAMS
1346 * codepage [I] Code page number
1347 * dwFlags [I] Reserved, must to 0.
1348 * cpinfo [O] Destination for code page information
1350 * RETURNS
1351 * Success: TRUE. cpinfo is updated with the information about codepage.
1352 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
1354 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
1356 CPINFOEXW cpinfoW;
1358 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
1359 return FALSE;
1361 /* the layout is the same except for CodePageName */
1362 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
1363 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
1364 return TRUE;
1367 /***********************************************************************
1368 * GetCPInfoExW (KERNEL32.@)
1370 * Unicode version of GetCPInfoExA.
1372 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
1374 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
1375 return FALSE;
1377 switch(codepage)
1379 case CP_UTF7:
1381 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
1383 cpinfo->CodePage = CP_UTF7;
1384 cpinfo->UnicodeDefaultChar = 0x3f;
1385 strcpyW(cpinfo->CodePageName, utf7);
1386 break;
1389 case CP_UTF8:
1391 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
1393 cpinfo->CodePage = CP_UTF8;
1394 cpinfo->UnicodeDefaultChar = 0x3f;
1395 strcpyW(cpinfo->CodePageName, utf8);
1396 break;
1399 default:
1401 const union cptable *table = get_codepage_table( codepage );
1403 cpinfo->CodePage = table->info.codepage;
1404 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
1405 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
1406 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
1407 break;
1410 return TRUE;
1413 /***********************************************************************
1414 * EnumSystemCodePagesA (KERNEL32.@)
1416 * Call a user defined function for every code page installed on the system.
1418 * PARAMS
1419 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
1420 * flags [I] Reserved, set to 0.
1422 * RETURNS
1423 * TRUE, If all code pages have been enumerated, or
1424 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
1426 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
1428 const union cptable *table;
1429 char buffer[10];
1430 int index = 0;
1432 for (;;)
1434 if (!(table = wine_cp_enum_table( index++ ))) break;
1435 sprintf( buffer, "%d", table->info.codepage );
1436 if (!lpfnCodePageEnum( buffer )) break;
1438 return TRUE;
1442 /***********************************************************************
1443 * EnumSystemCodePagesW (KERNEL32.@)
1445 * See EnumSystemCodePagesA.
1447 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
1449 const union cptable *table;
1450 WCHAR buffer[10], *p;
1451 int page, index = 0;
1453 for (;;)
1455 if (!(table = wine_cp_enum_table( index++ ))) break;
1456 p = buffer + sizeof(buffer)/sizeof(WCHAR);
1457 *--p = 0;
1458 page = table->info.codepage;
1461 *--p = '0' + (page % 10);
1462 page /= 10;
1463 } while( page );
1464 if (!lpfnCodePageEnum( p )) break;
1466 return TRUE;
1470 /***********************************************************************
1471 * MultiByteToWideChar (KERNEL32.@)
1473 * Convert a multibyte character string into a Unicode string.
1475 * PARAMS
1476 * page [I] Codepage character set to convert from
1477 * flags [I] Character mapping flags
1478 * src [I] Source string buffer
1479 * srclen [I] Length of src, or -1 if src is NUL terminated
1480 * dst [O] Destination buffer
1481 * dstlen [I] Length of dst, or 0 to compute the required length
1483 * RETURNS
1484 * Success: If dstlen > 0, the number of characters written to dst.
1485 * If dstlen == 0, the number of characters needed to perform the
1486 * conversion. In both cases the count includes the terminating NUL.
1487 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1488 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1489 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
1490 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
1491 * possible for src.
1493 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
1494 LPWSTR dst, INT dstlen )
1496 const union cptable *table;
1497 int ret;
1499 if (!src || (!dst && dstlen))
1501 SetLastError( ERROR_INVALID_PARAMETER );
1502 return 0;
1505 if (srclen < 0) srclen = strlen(src) + 1;
1507 if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
1509 switch(page)
1511 case CP_SYMBOL:
1512 if( flags)
1514 SetLastError( ERROR_INVALID_PARAMETER );
1515 return 0;
1517 ret = wine_cpsymbol_mbstowcs( src, srclen, dst, dstlen );
1518 break;
1519 case CP_UTF7:
1520 FIXME("UTF-7 not supported\n");
1521 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1522 return 0;
1523 case CP_UNIXCP:
1524 if (unix_cptable)
1526 ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
1527 break;
1529 /* fall through */
1530 case CP_UTF8:
1531 ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
1532 break;
1533 default:
1534 if (!(table = get_codepage_table( page )))
1536 SetLastError( ERROR_INVALID_PARAMETER );
1537 return 0;
1539 ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
1540 break;
1543 if (ret < 0)
1545 switch(ret)
1547 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1548 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1550 ret = 0;
1552 return ret;
1556 /***********************************************************************
1557 * WideCharToMultiByte (KERNEL32.@)
1559 * Convert a Unicode character string into a multibyte string.
1561 * PARAMS
1562 * page [I] Code page character set to convert to
1563 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
1564 * src [I] Source string buffer
1565 * srclen [I] Length of src, or -1 if src is NUL terminated
1566 * dst [O] Destination buffer
1567 * dstlen [I] Length of dst, or 0 to compute the required length
1568 * defchar [I] Default character to use for conversion if no exact
1569 * conversion can be made
1570 * used [O] Set if default character was used in the conversion
1572 * RETURNS
1573 * Success: If dstlen > 0, the number of characters written to dst.
1574 * If dstlen == 0, number of characters needed to perform the
1575 * conversion. In both cases the count includes the terminating NUL.
1576 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
1577 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
1578 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
1579 * parameter was given.
1581 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
1582 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
1584 const union cptable *table;
1585 int ret, used_tmp;
1587 if (!src || (!dst && dstlen))
1589 SetLastError( ERROR_INVALID_PARAMETER );
1590 return 0;
1593 if (srclen < 0) srclen = strlenW(src) + 1;
1595 switch(page)
1597 case CP_SYMBOL:
1598 if( flags || defchar || used)
1600 SetLastError( ERROR_INVALID_PARAMETER );
1601 return 0;
1603 ret = wine_cpsymbol_wcstombs( src, srclen, dst, dstlen );
1604 break;
1605 case CP_UTF7:
1606 FIXME("UTF-7 not supported\n");
1607 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
1608 return 0;
1609 case CP_UNIXCP:
1610 if (unix_cptable)
1612 ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
1613 defchar, used ? &used_tmp : NULL );
1614 break;
1616 /* fall through */
1617 case CP_UTF8:
1618 if (used) *used = FALSE; /* all chars are valid for UTF-8 */
1619 ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
1620 break;
1621 default:
1622 if (!(table = get_codepage_table( page )))
1624 SetLastError( ERROR_INVALID_PARAMETER );
1625 return 0;
1627 ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
1628 defchar, used ? &used_tmp : NULL );
1629 if (used) *used = used_tmp;
1630 break;
1633 if (ret < 0)
1635 switch(ret)
1637 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
1638 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
1640 ret = 0;
1642 TRACE("cp %d %s -> %s\n", page, debugstr_w(src), debugstr_a(dst));
1643 return ret;
1647 /***********************************************************************
1648 * GetThreadLocale (KERNEL32.@)
1650 * Get the current threads locale.
1652 * PARAMS
1653 * None.
1655 * RETURNS
1656 * The LCID currently assocated with the calling thread.
1658 LCID WINAPI GetThreadLocale(void)
1660 LCID ret = NtCurrentTeb()->CurrentLocale;
1661 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1662 return ret;
1665 /**********************************************************************
1666 * SetThreadLocale (KERNEL32.@)
1668 * Set the current threads locale.
1670 * PARAMS
1671 * lcid [I] LCID of the locale to set
1673 * RETURNS
1674 * Success: TRUE. The threads locale is set to lcid.
1675 * Failure: FALSE. Use GetLastError() to determine the cause.
1677 BOOL WINAPI SetThreadLocale( LCID lcid )
1679 TRACE("(0x%04lX)\n", lcid);
1681 lcid = ConvertDefaultLocale(lcid);
1683 if (lcid != GetThreadLocale())
1685 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1687 SetLastError(ERROR_INVALID_PARAMETER);
1688 return FALSE;
1691 NtCurrentTeb()->CurrentLocale = lcid;
1692 NtCurrentTeb()->code_page = get_lcid_codepage( lcid );
1694 return TRUE;
1697 /******************************************************************************
1698 * ConvertDefaultLocale (KERNEL32.@)
1700 * Convert a default locale identifier into a real identifier.
1702 * PARAMS
1703 * lcid [I] LCID identifier of the locale to convert
1705 * RETURNS
1706 * lcid unchanged, if not a default locale or its sublanguage is
1707 * not SUBLANG_NEUTRAL.
1708 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
1709 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
1710 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
1712 LCID WINAPI ConvertDefaultLocale( LCID lcid )
1714 LANGID langid;
1716 switch (lcid)
1718 case LOCALE_SYSTEM_DEFAULT:
1719 lcid = GetSystemDefaultLCID();
1720 break;
1721 case LOCALE_USER_DEFAULT:
1722 case LOCALE_NEUTRAL:
1723 lcid = GetUserDefaultLCID();
1724 break;
1725 default:
1726 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
1727 langid = LANGIDFROMLCID(lcid);
1728 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
1730 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
1731 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
1734 return lcid;
1738 /******************************************************************************
1739 * IsValidLocale (KERNEL32.@)
1741 * Determine if a locale is valid.
1743 * PARAMS
1744 * lcid [I] LCID of the locale to check
1745 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
1747 * RETURN
1748 * TRUE, if lcid is valid,
1749 * FALSE, otherwise.
1751 * NOTES
1752 * Wine does not currently make the distinction between supported and installed. All
1753 * languages supported are installed by default.
1755 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
1757 /* check if language is registered in the kernel32 resources */
1758 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1759 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
1763 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
1764 LPCSTR name, WORD LangID, LONG lParam )
1766 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
1767 char buf[20];
1769 sprintf(buf, "%08x", (UINT)LangID);
1770 return lpfnLocaleEnum( buf );
1773 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
1774 LPCWSTR name, WORD LangID, LONG lParam )
1776 static const WCHAR formatW[] = {'%','0','8','x',0};
1777 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
1778 WCHAR buf[20];
1779 sprintfW( buf, formatW, (UINT)LangID );
1780 return lpfnLocaleEnum( buf );
1783 /******************************************************************************
1784 * EnumSystemLocalesA (KERNEL32.@)
1786 * Call a users function for each locale available on the system.
1788 * PARAMS
1789 * lpfnLocaleEnum [I] Callback function to call for each locale
1790 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
1792 * RETURNS
1793 * Success: TRUE.
1794 * Failure: FALSE. Use GetLastError() to determine the cause.
1796 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
1798 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1799 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
1800 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
1801 (LONG)lpfnLocaleEnum);
1802 return TRUE;
1806 /******************************************************************************
1807 * EnumSystemLocalesW (KERNEL32.@)
1809 * See EnumSystemLocalesA.
1811 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
1813 TRACE("(%p,%08lx)\n", lpfnLocaleEnum, dwFlags);
1814 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
1815 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
1816 (LONG)lpfnLocaleEnum);
1817 return TRUE;
1821 /***********************************************************************
1822 * VerLanguageNameA (KERNEL32.@)
1824 * Get the name of a language.
1826 * PARAMS
1827 * wLang [I] LANGID of the language
1828 * szLang [O] Destination for the language name
1830 * RETURNS
1831 * Success: The size of the language name. If szLang is non-NULL, it is filled
1832 * with the name.
1833 * Failure: 0. Use GetLastError() to determine the cause.
1836 DWORD WINAPI VerLanguageNameA( UINT wLang, LPSTR szLang, UINT nSize )
1838 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1842 /***********************************************************************
1843 * VerLanguageNameW (KERNEL32.@)
1845 * See VerLanguageNameA.
1847 DWORD WINAPI VerLanguageNameW( UINT wLang, LPWSTR szLang, UINT nSize )
1849 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1853 /******************************************************************************
1854 * GetStringTypeW (KERNEL32.@)
1856 * See GetStringTypeA.
1858 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1860 if (count == -1) count = strlenW(src) + 1;
1861 switch(type)
1863 case CT_CTYPE1:
1864 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
1865 break;
1866 case CT_CTYPE2:
1867 while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
1868 break;
1869 case CT_CTYPE3:
1871 WARN("CT_CTYPE3: semi-stub.\n");
1872 while (count--)
1874 int c = *src;
1875 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
1877 type1 = get_char_typeW( *src++ ) & 0xfff;
1878 /* try to construct type3 from type1 */
1879 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
1880 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1881 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1882 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1883 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1884 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1885 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1887 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1888 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1889 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1890 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1891 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1892 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1893 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1894 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1896 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1897 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1898 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1899 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1900 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1901 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1902 *chartype++ = type3;
1904 break;
1906 default:
1907 SetLastError( ERROR_INVALID_PARAMETER );
1908 return FALSE;
1910 return TRUE;
1914 /******************************************************************************
1915 * GetStringTypeExW (KERNEL32.@)
1917 * See GetStringTypeExA.
1919 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1921 /* locale is ignored for Unicode */
1922 return GetStringTypeW( type, src, count, chartype );
1926 /******************************************************************************
1927 * GetStringTypeA (KERNEL32.@)
1929 * Get characteristics of the characters making up a string.
1931 * PARAMS
1932 * locale [I] Locale Id for the string
1933 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1934 * src [I] String to analyse
1935 * count [I] Length of src in chars, or -1 if src is NUL terminated
1936 * chartype [O] Destination for the calculated characteristics
1938 * RETURNS
1939 * Success: TRUE. chartype is filled with the requested characteristics of each char
1940 * in src.
1941 * Failure: FALSE. Use GetLastError() to determine the cause.
1943 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1945 UINT cp;
1946 INT countW;
1947 LPWSTR srcW;
1948 BOOL ret = FALSE;
1950 if(count == -1) count = strlen(src) + 1;
1952 if (!(cp = get_lcid_codepage( locale )))
1954 FIXME("For locale %04lx using current ANSI code page\n", locale);
1955 cp = GetACP();
1958 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1959 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1961 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1963 * NOTE: the target buffer has 1 word for each CHARACTER in the source
1964 * string, with multibyte characters there maybe be more bytes in count
1965 * than character space in the buffer!
1967 ret = GetStringTypeW(type, srcW, countW, chartype);
1968 HeapFree(GetProcessHeap(), 0, srcW);
1970 return ret;
1973 /******************************************************************************
1974 * GetStringTypeExA (KERNEL32.@)
1976 * Get characteristics of the characters making up a string.
1978 * PARAMS
1979 * locale [I] Locale Id for the string
1980 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1981 * src [I] String to analyse
1982 * count [I] Length of src in chars, or -1 if src is NUL terminated
1983 * chartype [O] Destination for the calculated characteristics
1985 * RETURNS
1986 * Success: TRUE. chartype is filled with the requested characteristics of each char
1987 * in src.
1988 * Failure: FALSE. Use GetLastError() to determine the cause.
1990 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1992 return GetStringTypeA(locale, type, src, count, chartype);
1996 /*************************************************************************
1997 * LCMapStringW (KERNEL32.@)
1999 * See LCMapStringA.
2001 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
2002 LPWSTR dst, INT dstlen)
2004 LPWSTR dst_ptr;
2006 if (!src || !srclen || dstlen < 0)
2008 SetLastError(ERROR_INVALID_PARAMETER);
2009 return 0;
2012 /* mutually exclusive flags */
2013 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
2014 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
2015 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
2016 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
2018 SetLastError(ERROR_INVALID_FLAGS);
2019 return 0;
2022 if (!dstlen) dst = NULL;
2024 lcid = ConvertDefaultLocale(lcid);
2026 if (flags & LCMAP_SORTKEY)
2028 if (src == dst)
2030 SetLastError(ERROR_INVALID_FLAGS);
2031 return 0;
2034 if (srclen < 0) srclen = strlenW(src);
2036 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
2037 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2039 return wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
2042 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
2043 if (flags & SORT_STRINGSORT)
2045 SetLastError(ERROR_INVALID_FLAGS);
2046 return 0;
2049 if (srclen < 0) srclen = strlenW(src) + 1;
2051 TRACE("(0x%04lx,0x%08lx,%s,%d,%p,%d)\n",
2052 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
2054 if (!dst) /* return required string length */
2056 INT len;
2058 for (len = 0; srclen; src++, srclen--)
2060 WCHAR wch = *src;
2061 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
2062 * and skips white space and punctuation characters for
2063 * NORM_IGNORESYMBOLS.
2065 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2066 continue;
2067 len++;
2069 return len;
2072 if (flags & LCMAP_UPPERCASE)
2074 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2076 WCHAR wch = *src;
2077 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2078 continue;
2079 *dst_ptr++ = toupperW(wch);
2080 dstlen--;
2083 else if (flags & LCMAP_LOWERCASE)
2085 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2087 WCHAR wch = *src;
2088 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2089 continue;
2090 *dst_ptr++ = tolowerW(wch);
2091 dstlen--;
2094 else
2096 if (src == dst)
2098 SetLastError(ERROR_INVALID_FLAGS);
2099 return 0;
2101 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
2103 WCHAR wch = *src;
2104 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
2105 continue;
2106 *dst_ptr++ = wch;
2107 dstlen--;
2111 if (srclen)
2113 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2114 return 0;
2117 return dst_ptr - dst;
2120 /*************************************************************************
2121 * LCMapStringA (KERNEL32.@)
2123 * Map characters in a locale sensitive string.
2125 * PARAMS
2126 * lcid [I] LCID for the conversion.
2127 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
2128 * src [I] String to map
2129 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
2130 * dst [O] Destination for mapped string
2131 * dstlen [I] Length of dst in characters
2133 * RETURNS
2134 * Success: The length of the mapped string in dst, including the NUL terminator.
2135 * Failure: 0. Use GetLastError() to determine the cause.
2137 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
2138 LPSTR dst, INT dstlen)
2140 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
2141 LPWSTR srcW, dstW;
2142 INT ret = 0, srclenW, dstlenW;
2143 UINT locale_cp;
2145 if (!src || !srclen || dstlen < 0)
2147 SetLastError(ERROR_INVALID_PARAMETER);
2148 return 0;
2151 locale_cp = get_lcid_codepage(lcid);
2153 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
2154 if (srclenW)
2155 srcW = bufW;
2156 else
2158 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
2159 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2160 if (!srcW)
2162 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2163 return 0;
2165 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
2168 if (flags & LCMAP_SORTKEY)
2170 if (src == dst)
2172 SetLastError(ERROR_INVALID_FLAGS);
2173 goto map_string_exit;
2175 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
2176 goto map_string_exit;
2179 if (flags & SORT_STRINGSORT)
2181 SetLastError(ERROR_INVALID_FLAGS);
2182 goto map_string_exit;
2185 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
2186 if (!dstlenW)
2187 goto map_string_exit;
2189 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
2190 if (!dstW)
2192 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2193 goto map_string_exit;
2196 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
2197 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
2198 HeapFree(GetProcessHeap(), 0, dstW);
2200 map_string_exit:
2201 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
2202 return ret;
2205 /*************************************************************************
2206 * FoldStringA (KERNEL32.@)
2208 * Map characters in a string.
2210 * PARAMS
2211 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
2212 * src [I] String to map
2213 * srclen [I] Length of src, or -1 if src is NUL terminated
2214 * dst [O] Destination for mapped string
2215 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
2217 * RETURNS
2218 * Success: The length of the string written to dst, including the terminating NUL. If
2219 * dstlen is 0, the value returned is the same, but nothing is written to dst,
2220 * and dst may be NULL.
2221 * Failure: 0. Use GetLastError() to determine the cause.
2223 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
2224 LPSTR dst, INT dstlen)
2226 INT ret = 0, srclenW = 0;
2227 WCHAR *srcW = NULL, *dstW = NULL;
2229 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2231 SetLastError(ERROR_INVALID_PARAMETER);
2232 return 0;
2235 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2236 src, srclen, NULL, 0);
2237 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
2239 if (!srcW)
2241 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2242 goto FoldStringA_exit;
2245 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
2246 src, srclen, srcW, srclenW);
2248 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
2250 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
2251 if (ret && dstlen)
2253 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
2255 if (!dstW)
2257 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2258 goto FoldStringA_exit;
2261 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
2262 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
2264 ret = 0;
2265 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2269 if (dstW)
2270 HeapFree(GetProcessHeap(), 0, dstW);
2272 FoldStringA_exit:
2273 if (srcW)
2274 HeapFree(GetProcessHeap(), 0, srcW);
2275 return ret;
2278 /*************************************************************************
2279 * FoldStringW (KERNEL32.@)
2281 * See FoldStringA.
2283 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
2284 LPWSTR dst, INT dstlen)
2286 int ret;
2288 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
2290 case 0:
2291 if (dwFlags)
2292 break;
2293 /* Fall through for dwFlags == 0 */
2294 case MAP_PRECOMPOSED|MAP_COMPOSITE:
2295 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
2296 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
2297 SetLastError(ERROR_INVALID_FLAGS);
2298 return 0;
2301 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
2303 SetLastError(ERROR_INVALID_PARAMETER);
2304 return 0;
2307 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
2308 if (!ret)
2309 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2310 return ret;
2313 /******************************************************************************
2314 * CompareStringW (KERNEL32.@)
2316 * See CompareStringA.
2318 INT WINAPI CompareStringW(LCID lcid, DWORD style,
2319 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
2321 INT ret;
2323 if (!str1 || !str2)
2325 SetLastError(ERROR_INVALID_PARAMETER);
2326 return 0;
2329 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
2330 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|0x10000000) )
2332 SetLastError(ERROR_INVALID_FLAGS);
2333 return 0;
2336 if (style & 0x10000000)
2337 FIXME("Ignoring unknown style 0x10000000\n");
2339 if (len1 < 0) len1 = strlenW(str1);
2340 if (len2 < 0) len2 = strlenW(str2);
2342 ret = wine_compare_string(style, str1, len1, str2, len2);
2344 if (ret) /* need to translate result */
2345 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
2346 return CSTR_EQUAL;
2349 /******************************************************************************
2350 * CompareStringA (KERNEL32.@)
2352 * Compare two locale sensitive strings.
2354 * PARAMS
2355 * lcid [I] LCID for the comparison
2356 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
2357 * str1 [I] First string to compare
2358 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
2359 * str2 [I] Second string to compare
2360 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
2362 * RETURNS
2363 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
2364 * str2 is less than, equal to or greater than str1 respectively.
2365 * Failure: FALSE. Use GetLastError() to determine the cause.
2367 INT WINAPI CompareStringA(LCID lcid, DWORD style,
2368 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
2370 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
2371 WCHAR *buf2W = buf1W + 130;
2372 LPWSTR str1W, str2W;
2373 INT len1W, len2W, ret;
2374 UINT locale_cp;
2376 if (!str1 || !str2)
2378 SetLastError(ERROR_INVALID_PARAMETER);
2379 return 0;
2381 if (len1 < 0) len1 = strlen(str1);
2382 if (len2 < 0) len2 = strlen(str2);
2384 locale_cp = get_lcid_codepage(lcid);
2386 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
2387 if (len1W)
2388 str1W = buf1W;
2389 else
2391 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
2392 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
2393 if (!str1W)
2395 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2396 return 0;
2398 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
2400 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
2401 if (len2W)
2402 str2W = buf2W;
2403 else
2405 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
2406 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
2407 if (!str2W)
2409 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2410 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2411 return 0;
2413 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
2416 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
2418 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
2419 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
2420 return ret;
2423 /*************************************************************************
2424 * lstrcmp (KERNEL32.@)
2425 * lstrcmpA (KERNEL32.@)
2427 * Compare two strings using the current thread locale.
2429 * PARAMS
2430 * str1 [I] First string to compare
2431 * str2 [I] Second string to compare
2433 * RETURNS
2434 * Success: A number less than, equal to or greater than 0 depending on whether
2435 * str2 is less than, equal to or greater than str1 respectively.
2436 * Failure: FALSE. Use GetLastError() to determine the cause.
2438 int WINAPI lstrcmpA(LPCSTR str1, LPCSTR str2)
2440 int ret;
2442 if ((str1 == NULL) && (str2 == NULL)) return 0;
2443 if (str1 == NULL) return -1;
2444 if (str2 == NULL) return 1;
2446 ret = CompareStringA(GetThreadLocale(), 0, str1, -1, str2, -1);
2447 if (ret) ret -= 2;
2449 return ret;
2452 /*************************************************************************
2453 * lstrcmpi (KERNEL32.@)
2454 * lstrcmpiA (KERNEL32.@)
2456 * Compare two strings using the current thread locale, ignoring case.
2458 * PARAMS
2459 * str1 [I] First string to compare
2460 * str2 [I] Second string to compare
2462 * RETURNS
2463 * Success: A number less than, equal to or greater than 0 depending on whether
2464 * str2 is less than, equal to or greater than str1 respectively.
2465 * Failure: FALSE. Use GetLastError() to determine the cause.
2467 int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
2469 int ret;
2471 if ((str1 == NULL) && (str2 == NULL)) return 0;
2472 if (str1 == NULL) return -1;
2473 if (str2 == NULL) return 1;
2475 ret = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2476 if (ret) ret -= 2;
2478 return ret;
2481 /*************************************************************************
2482 * lstrcmpW (KERNEL32.@)
2484 * See lstrcmpA.
2486 int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
2488 int ret;
2490 if ((str1 == NULL) && (str2 == NULL)) return 0;
2491 if (str1 == NULL) return -1;
2492 if (str2 == NULL) return 1;
2494 ret = CompareStringW(GetThreadLocale(), 0, str1, -1, str2, -1);
2495 if (ret) ret -= 2;
2497 return ret;
2500 /*************************************************************************
2501 * lstrcmpiW (KERNEL32.@)
2503 * See lstrcmpiA.
2505 int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
2507 int ret;
2509 if ((str1 == NULL) && (str2 == NULL)) return 0;
2510 if (str1 == NULL) return -1;
2511 if (str2 == NULL) return 1;
2513 ret = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1);
2514 if (ret) ret -= 2;
2516 return ret;
2519 /******************************************************************************
2520 * LOCALE_Init
2522 void LOCALE_Init(void)
2524 extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp,
2525 const union cptable *unix_cp );
2527 UINT ansi_cp = 1252, oem_cp = 437, mac_cp = 10000, unix_cp = ~0U;
2528 LCID lcid;
2530 lcid = get_env_lcid( NULL, NULL );
2531 NtSetDefaultLocale( TRUE, lcid );
2533 lcid = get_env_lcid( NULL, "LC_MESSAGES" );
2534 NtSetDefaultUILanguage( LANGIDFROMLCID(lcid) );
2536 lcid = get_env_lcid( &unix_cp, "LC_CTYPE" );
2537 NtSetDefaultLocale( FALSE, lcid );
2539 ansi_cp = get_lcid_codepage(lcid);
2540 GetLocaleInfoW( lcid, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
2541 (LPWSTR)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
2542 GetLocaleInfoW( lcid, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
2543 (LPWSTR)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
2544 if (unix_cp == ~0U)
2545 GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
2546 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) );
2548 if (!(ansi_cptable = wine_cp_get_table( ansi_cp )))
2549 ansi_cptable = wine_cp_get_table( 1252 );
2550 if (!(oem_cptable = wine_cp_get_table( oem_cp )))
2551 oem_cptable = wine_cp_get_table( 437 );
2552 if (!(mac_cptable = wine_cp_get_table( mac_cp )))
2553 mac_cptable = wine_cp_get_table( 10000 );
2554 if (unix_cp != CP_UTF8)
2556 if (!(unix_cptable = wine_cp_get_table( unix_cp )))
2557 unix_cptable = wine_cp_get_table( 28591 );
2560 __wine_init_codepages( ansi_cptable, oem_cptable, unix_cptable );
2562 TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
2563 ansi_cptable->info.codepage, oem_cptable->info.codepage,
2564 mac_cptable->info.codepage, unix_cp );
2567 static HKEY NLS_RegOpenKey(HKEY hRootKey, LPCWSTR szKeyName)
2569 UNICODE_STRING keyName;
2570 OBJECT_ATTRIBUTES attr;
2571 HKEY hkey;
2573 RtlInitUnicodeString( &keyName, szKeyName );
2574 InitializeObjectAttributes(&attr, &keyName, 0, hRootKey, NULL);
2576 if (NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) != STATUS_SUCCESS)
2577 hkey = 0;
2579 return hkey;
2582 static HKEY NLS_RegOpenSubKey(HKEY hRootKey, LPCWSTR szKeyName)
2584 HKEY hKey = NLS_RegOpenKey(hRootKey, szKeyName);
2586 if (hRootKey)
2587 NtClose( hRootKey );
2589 return hKey;
2592 static BOOL NLS_RegEnumSubKey(HKEY hKey, UINT ulIndex, LPWSTR szKeyName,
2593 ULONG keyNameSize)
2595 BYTE buffer[80];
2596 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
2597 DWORD dwLen;
2599 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
2600 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
2601 info->NameLength > keyNameSize)
2603 return FALSE;
2606 TRACE("info->Name %s info->NameLength %ld\n", debugstr_w(info->Name), info->NameLength);
2608 memcpy( szKeyName, info->Name, info->NameLength);
2609 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
2611 TRACE("returning %s\n", debugstr_w(szKeyName));
2612 return TRUE;
2615 static BOOL NLS_RegEnumValue(HKEY hKey, UINT ulIndex,
2616 LPWSTR szValueName, ULONG valueNameSize,
2617 LPWSTR szValueData, ULONG valueDataSize)
2619 BYTE buffer[80];
2620 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
2621 DWORD dwLen;
2623 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
2624 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
2625 info->NameLength > valueNameSize ||
2626 info->DataLength > valueDataSize)
2628 return FALSE;
2631 TRACE("info->Name %s info->DataLength %ld\n", debugstr_w(info->Name), info->DataLength);
2633 memcpy( szValueName, info->Name, info->NameLength);
2634 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
2635 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
2636 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
2638 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
2639 return TRUE;
2642 static BOOL NLS_RegGetDword(HKEY hKey, LPCWSTR szValueName, DWORD *lpVal)
2644 BYTE buffer[128];
2645 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
2646 DWORD dwSize = sizeof(buffer);
2647 UNICODE_STRING valueName;
2649 RtlInitUnicodeString( &valueName, szValueName );
2651 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
2652 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
2653 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
2654 info->DataLength == sizeof(DWORD))
2656 memcpy(lpVal, info->Data, sizeof(DWORD));
2657 return TRUE;
2660 return FALSE;
2663 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
2665 LANGID langId;
2666 LPCWSTR szResourceName = (LPCWSTR)(((lgrpid + 0x2000) >> 4) + 1);
2667 HRSRC hResource;
2668 BOOL bRet = FALSE;
2670 /* FIXME: Is it correct to use the system default langid? */
2671 langId = GetSystemDefaultLangID();
2673 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
2674 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
2676 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
2678 if (hResource)
2680 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
2682 if (hResDir)
2684 ULONG iResourceIndex = lgrpid & 0xf;
2685 LPCWSTR lpResEntry = LockResource( hResDir );
2686 ULONG i;
2688 for (i = 0; i < iResourceIndex; i++)
2689 lpResEntry += *lpResEntry + 1;
2691 if (*lpResEntry < nameSize)
2693 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
2694 szName[*lpResEntry] = '\0';
2695 bRet = TRUE;
2699 FreeResource( hResource );
2701 return bRet;
2704 /* Registry keys for NLS related information */
2705 static const WCHAR szLangGroupsKeyName[] = {
2706 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s','\0'
2709 static const WCHAR szCountryListName[] = {
2710 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
2711 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
2712 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2713 'T','e','l','e','p','h','o','n','y','\\',
2714 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
2718 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
2719 typedef struct
2721 LANGUAGEGROUP_ENUMPROCA procA;
2722 LANGUAGEGROUP_ENUMPROCW procW;
2723 DWORD dwFlags;
2724 LONG_PTR lParam;
2725 } ENUMLANGUAGEGROUP_CALLBACKS;
2727 /* Internal implementation of EnumSystemLanguageGroupsA/W */
2728 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
2730 WCHAR szNumber[10], szValue[4];
2731 HKEY hKey;
2732 BOOL bContinue = TRUE;
2733 ULONG ulIndex = 0;
2735 if (!lpProcs)
2737 SetLastError(ERROR_INVALID_PARAMETER);
2738 return FALSE;
2741 switch (lpProcs->dwFlags)
2743 case 0:
2744 /* Default to LGRPID_INSTALLED */
2745 lpProcs->dwFlags = LGRPID_INSTALLED;
2746 /* Fall through... */
2747 case LGRPID_INSTALLED:
2748 case LGRPID_SUPPORTED:
2749 break;
2750 default:
2751 SetLastError(ERROR_INVALID_FLAGS);
2752 return FALSE;
2755 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2757 if (!hKey)
2758 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
2760 while (bContinue)
2762 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2763 szValue, sizeof(szValue) ))
2765 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
2766 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
2768 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
2769 bInstalled ? "" : "not ");
2771 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
2773 WCHAR szGrpName[48];
2775 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
2776 szGrpName[0] = '\0';
2778 if (lpProcs->procW)
2779 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
2780 lpProcs->lParam );
2781 else
2783 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2784 char szGrpNameA[48];
2786 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
2787 * or whether the language names are ever localised. Assume CP_ACP.
2790 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2791 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
2793 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
2794 lpProcs->lParam );
2798 ulIndex++;
2800 else
2801 bContinue = FALSE;
2803 if (!bContinue)
2804 break;
2807 if (hKey)
2808 NtClose( hKey );
2810 return TRUE;
2813 /******************************************************************************
2814 * EnumSystemLanguageGroupsA (KERNEL32.@)
2816 * Call a users function for each language group available on the system.
2818 * PARAMS
2819 * pLangGrpEnumProc [I] Callback function to call for each language group
2820 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
2821 * lParam [I] User parameter to pass to pLangGrpEnumProc
2823 * RETURNS
2824 * Success: TRUE.
2825 * Failure: FALSE. Use GetLastError() to determine the cause.
2827 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
2828 DWORD dwFlags, LONG_PTR lParam)
2830 ENUMLANGUAGEGROUP_CALLBACKS procs;
2832 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2834 procs.procA = pLangGrpEnumProc;
2835 procs.procW = NULL;
2836 procs.dwFlags = dwFlags;
2837 procs.lParam = lParam;
2839 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2842 /******************************************************************************
2843 * EnumSystemLanguageGroupsW (KERNEL32.@)
2845 * See EnumSystemLanguageGroupsA.
2847 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
2848 DWORD dwFlags, LONG_PTR lParam)
2850 ENUMLANGUAGEGROUP_CALLBACKS procs;
2852 TRACE("(%p,0x%08lX,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2854 procs.procA = NULL;
2855 procs.procW = pLangGrpEnumProc;
2856 procs.dwFlags = dwFlags;
2857 procs.lParam = lParam;
2859 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2862 /******************************************************************************
2863 * IsValidLanguageGroup (KERNEL32.@)
2865 * Determine if a language group is supported and/or installed.
2867 * PARAMS
2868 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
2869 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
2871 * RETURNS
2872 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
2873 * FALSE otherwise.
2875 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
2877 static const WCHAR szFormat[] = { '%','x','\0' };
2878 WCHAR szValueName[16], szValue[2];
2879 BOOL bSupported = FALSE, bInstalled = FALSE;
2880 HKEY hKey;
2883 switch (dwFlags)
2885 case LGRPID_INSTALLED:
2886 case LGRPID_SUPPORTED:
2888 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLangGroupsKeyName );
2890 sprintfW( szValueName, szFormat, lgrpid );
2892 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)&szValue ))
2894 bSupported = TRUE;
2896 if (szValue[0] == '1')
2897 bInstalled = TRUE;
2900 if (hKey)
2901 NtClose( hKey );
2903 break;
2906 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
2907 (dwFlags == LGRPID_INSTALLED && bInstalled))
2908 return TRUE;
2910 return FALSE;
2913 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
2914 typedef struct
2916 LANGGROUPLOCALE_ENUMPROCA procA;
2917 LANGGROUPLOCALE_ENUMPROCW procW;
2918 DWORD dwFlags;
2919 LGRPID lgrpid;
2920 LONG_PTR lParam;
2921 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
2923 /* Internal implementation of EnumLanguageGrouplocalesA/W */
2924 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
2926 static const WCHAR szLocaleKeyName[] = {
2927 'L','o','c','a','l','e','\0'
2929 static const WCHAR szAlternateSortsKeyName[] = {
2930 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
2932 WCHAR szNumber[10], szValue[4];
2933 HKEY hKey;
2934 BOOL bContinue = TRUE, bAlternate = FALSE;
2935 LGRPID lgrpid;
2936 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
2938 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
2940 SetLastError(ERROR_INVALID_PARAMETER);
2941 return FALSE;
2944 if (lpProcs->dwFlags)
2946 SetLastError(ERROR_INVALID_FLAGS);
2947 return FALSE;
2950 hKey = NLS_RegOpenSubKey( NLS_RegOpenKey( 0, szNlsKeyName ), szLocaleKeyName );
2952 if (!hKey)
2953 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
2955 while (bContinue)
2957 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2958 szValue, sizeof(szValue) ))
2960 lgrpid = strtoulW( szValue, NULL, 16 );
2962 TRACE("lcid %s, grpid %ld (%smatched)\n", debugstr_w(szNumber),
2963 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
2965 if (lgrpid == lpProcs->lgrpid)
2967 LCID lcid;
2969 lcid = strtoulW( szNumber, NULL, 16 );
2971 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
2972 * '00000437 ;Georgian'
2973 * At present we only pass the LCID string.
2976 if (lpProcs->procW)
2977 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
2978 else
2980 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2982 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2984 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
2988 ulIndex++;
2990 else
2992 /* Finished enumerating this key */
2993 if (!bAlternate)
2995 /* Enumerate alternate sorts also */
2996 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
2997 bAlternate = TRUE;
2998 ulIndex = 0;
3000 else
3001 bContinue = FALSE; /* Finished both keys */
3004 if (!bContinue)
3005 break;
3008 if (hKey)
3009 NtClose( hKey );
3011 return TRUE;
3014 /******************************************************************************
3015 * EnumLanguageGroupLocalesA (KERNEL32.@)
3017 * Call a users function for every locale in a language group available on the system.
3019 * PARAMS
3020 * pLangGrpLcEnumProc [I] Callback function to call for each locale
3021 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
3022 * dwFlags [I] Reserved, set to 0
3023 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
3025 * RETURNS
3026 * Success: TRUE.
3027 * Failure: FALSE. Use GetLastError() to determine the cause.
3029 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
3030 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3032 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3034 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3036 callbacks.procA = pLangGrpLcEnumProc;
3037 callbacks.procW = NULL;
3038 callbacks.dwFlags = dwFlags;
3039 callbacks.lgrpid = lgrpid;
3040 callbacks.lParam = lParam;
3042 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3045 /******************************************************************************
3046 * EnumLanguageGroupLocalesW (KERNEL32.@)
3048 * See EnumLanguageGroupLocalesA.
3050 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
3051 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
3053 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
3055 TRACE("(%p,0x%08lX,0x%08lX,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
3057 callbacks.procA = NULL;
3058 callbacks.procW = pLangGrpLcEnumProc;
3059 callbacks.dwFlags = dwFlags;
3060 callbacks.lgrpid = lgrpid;
3061 callbacks.lParam = lParam;
3063 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
3066 /******************************************************************************
3067 * EnumSystemGeoID (KERNEL32.@)
3069 * Call a users function for every location available on the system.
3071 * PARAMS
3072 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
3073 * reserved [I] Reserved, set to 0
3074 * pGeoEnumProc [I] Callback function to call for each location
3076 * RETURNS
3077 * Success: TRUE.
3078 * Failure: FALSE. Use GetLastError() to determine the cause.
3080 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
3082 static const WCHAR szCountryCodeValueName[] = {
3083 'C','o','u','n','t','r','y','C','o','d','e','\0'
3085 WCHAR szNumber[10];
3086 HKEY hKey;
3087 ULONG ulIndex = 0;
3089 TRACE("(0x%08lX,0x%08lX,%p)\n", geoclass, reserved, pGeoEnumProc);
3091 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
3093 SetLastError(ERROR_INVALID_PARAMETER);
3094 return FALSE;
3097 hKey = NLS_RegOpenKey( 0, szCountryListName );
3099 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
3101 BOOL bContinue = TRUE;
3102 DWORD dwGeoId;
3103 HKEY hSubKey = NLS_RegOpenKey( hKey, szNumber );
3105 if (hSubKey)
3107 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
3109 TRACE("Got geoid %ld\n", dwGeoId);
3111 if (!pGeoEnumProc( dwGeoId ))
3112 bContinue = FALSE;
3115 NtClose( hSubKey );
3118 if (!bContinue)
3119 break;
3121 ulIndex++;
3124 if (hKey)
3125 NtClose( hKey );
3127 return TRUE;
3130 /******************************************************************************
3131 * InvalidateNLSCache (KERNEL32.@)
3133 * Invalidate the cache of NLS values.
3135 * PARAMS
3136 * None.
3138 * RETURNS
3139 * Success: TRUE.
3140 * Failure: FALSE.
3142 BOOL WINAPI InvalidateNLSCache(void)
3144 FIXME("() stub\n");
3145 return FALSE;
3148 /******************************************************************************
3149 * GetUserGeoID (KERNEL32.@)
3151 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
3153 FIXME("%ld\n",GeoClass);
3154 return GEOID_NOT_AVAILABLE;
3157 /******************************************************************************
3158 * SetUserGeoID (KERNEL32.@)
3160 BOOL WINAPI SetUserGeoID( GEOID GeoID )
3162 FIXME("%ld\n",GeoID);
3163 return FALSE;
3166 /******************************************************************************
3167 * EnumUILanguagesA (KERNEL32.@)
3169 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3171 static char value[] = "0409";
3173 if(!pUILangEnumProc) return FALSE;
3175 FIXME("%p, %lx, %lx calling pUILangEnumProc with %s\n",
3176 pUILangEnumProc, dwFlags, lParam, debugstr_a(value));
3178 pUILangEnumProc( value, lParam );
3179 return(TRUE);
3182 /******************************************************************************
3183 * EnumUILanguagesW (KERNEL32.@)
3185 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
3187 static WCHAR value[] = {'0','4','0','9',0};
3189 if(!pUILangEnumProc) return FALSE;
3191 FIXME("%p, %lx, %lx calling pUILangEnumProc with %s\n",
3192 pUILangEnumProc, dwFlags, lParam, debugstr_w(value));
3194 pUILangEnumProc( value, lParam );
3195 return(TRUE);