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