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