Synchronize with trunk revision 59636 (just before Alex's CreateProcess revamp).
[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 static int get_value_base_by_lctype( LCTYPE lctype )
758 {
759 return lctype == LOCALE_ILANGUAGE || lctype == LOCALE_IDEFAULTLANGUAGE ? 16 : 10;
760 }
761
762 /******************************************************************************
763 * GetLocaleInfoW (KERNEL32.@)
764 *
765 * See GetLocaleInfoA.
766 */
767 INT WINAPI GetLocaleInfoW( LCID lcid, LCTYPE lctype, LPWSTR buffer, INT len )
768 {
769 LANGID lang_id;
770 HRSRC hrsrc;
771 HGLOBAL hmem;
772 INT ret;
773 UINT lcflags;
774 const WCHAR *p;
775 unsigned int i;
776
777 if (len < 0 || (len && !buffer))
778 {
779 SetLastError( ERROR_INVALID_PARAMETER );
780 return 0;
781 }
782 if (lctype & LOCALE_RETURN_GENITIVE_NAMES &&
783 !is_genitive_name_supported( lctype ))
784 {
785 SetLastError( ERROR_INVALID_FLAGS );
786 return 0;
787 }
788
789 if (!len) buffer = NULL;
790
791 lcid = convert_default_lcid( lcid, lctype );
792
793 lcflags = lctype & LOCALE_LOCALEINFOFLAGSMASK;
794 lctype &= 0xffff;
795
796 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
797
798 /* first check for overrides in the registry */
799
800 if (!(lcflags & LOCALE_NOUSEROVERRIDE) &&
801 lcid == convert_default_lcid( LOCALE_USER_DEFAULT, lctype ))
802 {
803 const WCHAR *value = get_locale_value_name(lctype);
804
805 if (value)
806 {
807 if (lcflags & LOCALE_RETURN_NUMBER)
808 {
809 WCHAR tmp[16];
810 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
811 if (ret > 0)
812 {
813 WCHAR *end;
814 UINT number = strtolW( tmp, &end, get_value_base_by_lctype( lctype ) );
815 if (*end) /* invalid number */
816 {
817 SetLastError( ERROR_INVALID_FLAGS );
818 return 0;
819 }
820 ret = sizeof(UINT)/sizeof(WCHAR);
821 if (!buffer) return ret;
822 if (ret > len)
823 {
824 SetLastError( ERROR_INSUFFICIENT_BUFFER );
825 return 0;
826 }
827 memcpy( buffer, &number, sizeof(number) );
828 }
829 }
830 else ret = get_registry_locale_info( value, buffer, len );
831
832 if (ret != -1) return ret;
833 }
834 }
835
836 /* now load it from kernel resources */
837
838 lang_id = LANGIDFROMLCID( lcid );
839
840 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
841 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL)
842 lang_id = MAKELANGID(PRIMARYLANGID(lang_id), SUBLANG_DEFAULT);
843
844 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
845 ULongToPtr((lctype >> 4) + 1), lang_id )))
846 {
847 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
848 return 0;
849 }
850 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
851 return 0;
852
853 p = LockResource( hmem );
854 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
855
856 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
857 else if (is_genitive_name_supported( lctype ) && *p)
858 {
859 /* genitive form's stored after a null separator from a nominative */
860 for (i = 1; i <= *p; i++) if (!p[i]) break;
861
862 if (i <= *p && (lcflags & LOCALE_RETURN_GENITIVE_NAMES))
863 {
864 ret = *p - i + 1;
865 p += i;
866 }
867 else ret = i;
868 }
869 else
870 ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
871
872 if (!buffer) return ret;
873
874 if (ret > len)
875 {
876 SetLastError( ERROR_INSUFFICIENT_BUFFER );
877 return 0;
878 }
879
880 if (lcflags & LOCALE_RETURN_NUMBER)
881 {
882 UINT number;
883 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
884 if (!tmp) return 0;
885 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
886 tmp[*p] = 0;
887 number = strtolW( tmp, &end, get_value_base_by_lctype( lctype ) );
888 if (!*end)
889 memcpy( buffer, &number, sizeof(number) );
890 else /* invalid number */
891 {
892 SetLastError( ERROR_INVALID_FLAGS );
893 ret = 0;
894 }
895 HeapFree( GetProcessHeap(), 0, tmp );
896
897 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
898 lcid, lctype, buffer, len, number );
899 }
900 else
901 {
902 memcpy( buffer, p + 1, ret * sizeof(WCHAR) );
903 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
904
905 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
906 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
907 }
908 return ret;
909 }
910
911
912 /******************************************************************************
913 * SetLocaleInfoA [KERNEL32.@]
914 *
915 * Set information about an aspect of a locale.
916 *
917 * PARAMS
918 * lcid [I] LCID of the locale
919 * lctype [I] LCTYPE_ flags from "winnls.h"
920 * data [I] Information to set
921 *
922 * RETURNS
923 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
924 * whenever it is called without LOCALE_NOUSEROVERRIDE.
925 * Failure: FALSE. Use GetLastError() to determine the cause.
926 *
927 * NOTES
928 * - Values are only be set for the current user locale; the system locale
929 * settings cannot be changed.
930 * - Any settings changed by this call are lost when the locale is changed by
931 * the control panel (in Wine, this happens every time you change LANG).
932 * - The native implementation of this function does not check that lcid matches
933 * the current user locale, and simply sets the new values. Wine warns you in
934 * this case, but behaves the same.
935 */
936 BOOL WINAPI SetLocaleInfoA(LCID lcid, LCTYPE lctype, LPCSTR data)
937 {
938 UINT codepage = CP_ACP;
939 WCHAR *strW;
940 DWORD len;
941 BOOL ret;
942
943 if (!(lctype & LOCALE_USE_CP_ACP)) codepage = get_lcid_codepage( lcid );
944
945 if (!data)
946 {
947 SetLastError( ERROR_INVALID_PARAMETER );
948 return FALSE;
949 }
950 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
951 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
952 {
953 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
954 return FALSE;
955 }
956 MultiByteToWideChar( codepage, 0, data, -1, strW, len );
957 ret = SetLocaleInfoW( lcid, lctype, strW );
958 HeapFree( GetProcessHeap(), 0, strW );
959 return ret;
960 }
961
962
963 /******************************************************************************
964 * SetLocaleInfoW (KERNEL32.@)
965 *
966 * See SetLocaleInfoA.
967 */
968 BOOL WINAPI SetLocaleInfoW( LCID lcid, LCTYPE lctype, LPCWSTR data )
969 {
970 const WCHAR *value;
971 static const WCHAR intlW[] = {'i','n','t','l',0 };
972 UNICODE_STRING valueW;
973 NTSTATUS status;
974 HANDLE hkey;
975
976 lctype &= 0xffff;
977 value = get_locale_value_name( lctype );
978
979 if (!data || !value)
980 {
981 SetLastError( ERROR_INVALID_PARAMETER );
982 return FALSE;
983 }
984
985 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
986 {
987 SetLastError( ERROR_INVALID_FLAGS );
988 return FALSE;
989 }
990
991 TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value), debugstr_w(data) );
992
993 /* FIXME: should check that data to set is sane */
994
995 /* FIXME: profile functions should map to registry */
996 WriteProfileStringW( intlW, value, data );
997
998 if (!(hkey = create_registry_key())) return FALSE;
999 RtlInitUnicodeString( &valueW, value );
1000 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, data, (strlenW(data)+1)*sizeof(WCHAR) );
1001
1002 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
1003 {
1004 /* Set I-value from S value */
1005 WCHAR *lpD, *lpM, *lpY;
1006 WCHAR szBuff[2];
1007
1008 lpD = strrchrW(data, 'd');
1009 lpM = strrchrW(data, 'M');
1010 lpY = strrchrW(data, 'y');
1011
1012 if (lpD <= lpM)
1013 {
1014 szBuff[0] = '1'; /* D-M-Y */
1015 }
1016 else
1017 {
1018 if (lpY <= lpM)
1019 szBuff[0] = '2'; /* Y-M-D */
1020 else
1021 szBuff[0] = '0'; /* M-D-Y */
1022 }
1023
1024 szBuff[1] = '\0';
1025
1026 if (lctype == LOCALE_SSHORTDATE)
1027 lctype = LOCALE_IDATE;
1028 else
1029 lctype = LOCALE_ILDATE;
1030
1031 value = get_locale_value_name( lctype );
1032
1033 WriteProfileStringW( intlW, value, szBuff );
1034
1035 RtlInitUnicodeString( &valueW, value );
1036 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1037 }
1038
1039 NtClose( hkey );
1040
1041 if (status) SetLastError( RtlNtStatusToDosError(status) );
1042 return !status;
1043 }
1044
1045 /***********************************************************************
1046 * GetThreadLocale (KERNEL32.@)
1047 *
1048 * Get the current threads locale.
1049 *
1050 * PARAMS
1051 * None.
1052 *
1053 * RETURNS
1054 * The LCID currently associated with the calling thread.
1055 */
1056 LCID WINAPI GetThreadLocale(void)
1057 {
1058 LCID ret = NtCurrentTeb()->CurrentLocale;
1059 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
1060 return ret;
1061 }
1062
1063 /**********************************************************************
1064 * SetThreadLocale (KERNEL32.@)
1065 *
1066 * Set the current threads locale.
1067 *
1068 * PARAMS
1069 * lcid [I] LCID of the locale to set
1070 *
1071 * RETURNS
1072 * Success: TRUE. The threads locale is set to lcid.
1073 * Failure: FALSE. Use GetLastError() to determine the cause.
1074 */
1075 BOOL WINAPI SetThreadLocale( LCID lcid )
1076 {
1077 TRACE("(0x%04X)\n", lcid);
1078
1079 lcid = ConvertDefaultLocale(lcid);
1080
1081 if (lcid != GetThreadLocale())
1082 {
1083 if (!IsValidLocale(lcid, LCID_SUPPORTED))
1084 {
1085 SetLastError(ERROR_INVALID_PARAMETER);
1086 return FALSE;
1087 }
1088
1089 NtCurrentTeb()->CurrentLocale = lcid;
1090 }
1091 return TRUE;
1092 }
1093
1094 /******************************************************************************
1095 * ConvertDefaultLocale (KERNEL32.@)
1096 *
1097 * Convert a default locale identifier into a real identifier.
1098 *
1099 * PARAMS
1100 * lcid [I] LCID identifier of the locale to convert
1101 *
1102 * RETURNS
1103 * lcid unchanged, if not a default locale or its sublanguage is
1104 * not SUBLANG_NEUTRAL.
1105 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
1106 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
1107 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
1108 */
1109 LCID WINAPI ConvertDefaultLocale( LCID lcid )
1110 {
1111 LANGID langid;
1112
1113 switch (lcid)
1114 {
1115 case LOCALE_SYSTEM_DEFAULT:
1116 lcid = GetSystemDefaultLCID();
1117 break;
1118 case LOCALE_USER_DEFAULT:
1119 case LOCALE_NEUTRAL:
1120 lcid = GetUserDefaultLCID();
1121 break;
1122 default:
1123 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
1124 langid = LANGIDFROMLCID(lcid);
1125 if (SUBLANGID(langid) == SUBLANG_NEUTRAL)
1126 {
1127 langid = MAKELANGID(PRIMARYLANGID(langid), SUBLANG_DEFAULT);
1128 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
1129 }
1130 }
1131 return lcid;
1132 }
1133
1134
1135 /******************************************************************************
1136 * IsValidLocale (KERNEL32.@)
1137 *
1138 * Determine if a locale is valid.
1139 *
1140 * PARAMS
1141 * lcid [I] LCID of the locale to check
1142 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
1143 *
1144 * RETURNS
1145 * TRUE, if lcid is valid,
1146 * FALSE, otherwise.
1147 *
1148 * NOTES
1149 * Wine does not currently make the distinction between supported and installed. All
1150 * languages supported are installed by default.
1151 */
1152 BOOL WINAPI IsValidLocale( LCID lcid, DWORD flags )
1153 {
1154 /* check if language is registered in the kernel32 resources */
1155 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
1156 (LPCWSTR)LOCALE_ILANGUAGE, LANGIDFROMLCID(lcid)) != 0;
1157 }
1158
1159
1160 static BOOL CALLBACK enum_lang_proc_a( HMODULE hModule, LPCSTR type,
1161 LPCSTR name, WORD LangID, LONG_PTR lParam )
1162 {
1163 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
1164 char buf[20];
1165
1166 sprintf(buf, "%08x", (UINT)LangID);
1167 return lpfnLocaleEnum( buf );
1168 }
1169
1170 static BOOL CALLBACK enum_lang_proc_w( HMODULE hModule, LPCWSTR type,
1171 LPCWSTR name, WORD LangID, LONG_PTR lParam )
1172 {
1173 static const WCHAR formatW[] = {'%','0','8','x',0};
1174 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
1175 WCHAR buf[20];
1176 sprintfW( buf, formatW, (UINT)LangID );
1177 return lpfnLocaleEnum( buf );
1178 }
1179
1180 /******************************************************************************
1181 * EnumSystemLocalesA (KERNEL32.@)
1182 *
1183 * Call a users function for each locale available on the system.
1184 *
1185 * PARAMS
1186 * lpfnLocaleEnum [I] Callback function to call for each locale
1187 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
1188 *
1189 * RETURNS
1190 * Success: TRUE.
1191 * Failure: FALSE. Use GetLastError() to determine the cause.
1192 */
1193 BOOL WINAPI EnumSystemLocalesA( LOCALE_ENUMPROCA lpfnLocaleEnum, DWORD dwFlags )
1194 {
1195 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
1196 EnumResourceLanguagesA( kernel32_handle, (LPSTR)RT_STRING,
1197 (LPCSTR)LOCALE_ILANGUAGE, enum_lang_proc_a,
1198 (LONG_PTR)lpfnLocaleEnum);
1199 return TRUE;
1200 }
1201
1202
1203 /******************************************************************************
1204 * EnumSystemLocalesW (KERNEL32.@)
1205 *
1206 * See EnumSystemLocalesA.
1207 */
1208 BOOL WINAPI EnumSystemLocalesW( LOCALE_ENUMPROCW lpfnLocaleEnum, DWORD dwFlags )
1209 {
1210 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
1211 EnumResourceLanguagesW( kernel32_handle, (LPWSTR)RT_STRING,
1212 (LPCWSTR)LOCALE_ILANGUAGE, enum_lang_proc_w,
1213 (LONG_PTR)lpfnLocaleEnum);
1214 return TRUE;
1215 }
1216
1217 /***********************************************************************
1218 * VerLanguageNameA (KERNEL32.@)
1219 *
1220 * Get the name of a language.
1221 *
1222 * PARAMS
1223 * wLang [I] LANGID of the language
1224 * szLang [O] Destination for the language name
1225 *
1226 * RETURNS
1227 * Success: The size of the language name. If szLang is non-NULL, it is filled
1228 * with the name.
1229 * Failure: 0. Use GetLastError() to determine the cause.
1230 *
1231 */
1232 DWORD WINAPI VerLanguageNameA( DWORD wLang, LPSTR szLang, DWORD nSize )
1233 {
1234 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1235 }
1236
1237
1238 /***********************************************************************
1239 * VerLanguageNameW (KERNEL32.@)
1240 *
1241 * See VerLanguageNameA.
1242 */
1243 DWORD WINAPI VerLanguageNameW( DWORD wLang, LPWSTR szLang, DWORD nSize )
1244 {
1245 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
1246 }
1247
1248 /******************************************************************************
1249 * GetStringTypeW (KERNEL32.@)
1250 *
1251 * See GetStringTypeA.
1252 */
1253 BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1254 {
1255 static const unsigned char type2_map[16] =
1256 {
1257 C2_NOTAPPLICABLE, /* unassigned */
1258 C2_LEFTTORIGHT, /* L */
1259 C2_RIGHTTOLEFT, /* R */
1260 C2_EUROPENUMBER, /* EN */
1261 C2_EUROPESEPARATOR, /* ES */
1262 C2_EUROPETERMINATOR, /* ET */
1263 C2_ARABICNUMBER, /* AN */
1264 C2_COMMONSEPARATOR, /* CS */
1265 C2_BLOCKSEPARATOR, /* B */
1266 C2_SEGMENTSEPARATOR, /* S */
1267 C2_WHITESPACE, /* WS */
1268 C2_OTHERNEUTRAL, /* ON */
1269 C2_RIGHTTOLEFT, /* AL */
1270 C2_NOTAPPLICABLE, /* NSM */
1271 C2_NOTAPPLICABLE, /* BN */
1272 C2_OTHERNEUTRAL /* LRE, LRO, RLE, RLO, PDF */
1273 };
1274
1275 if (count == -1) count = strlenW(src) + 1;
1276 switch(type)
1277 {
1278 case CT_CTYPE1:
1279 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
1280 break;
1281 case CT_CTYPE2:
1282 while (count--) *chartype++ = type2_map[get_char_typeW( *src++ ) >> 12];
1283 break;
1284 case CT_CTYPE3:
1285 {
1286 WARN("CT_CTYPE3: semi-stub.\n");
1287 while (count--)
1288 {
1289 int c = *src;
1290 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
1291
1292 type1 = get_char_typeW( *src++ ) & 0xfff;
1293 /* try to construct type3 from type1 */
1294 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
1295 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
1296 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
1297 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
1298 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
1299 if ((c>=0x0600)&&(c<=0x06FF)) type3 |= C3_KASHIDA;
1300 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
1301
1302 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
1303 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
1304 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
1305 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
1306 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
1307 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
1308 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
1309 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
1310
1311 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
1312 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
1313 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
1314 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
1315 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
1316 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
1317 *chartype++ = type3;
1318 }
1319 break;
1320 }
1321 default:
1322 SetLastError( ERROR_INVALID_PARAMETER );
1323 return FALSE;
1324 }
1325 return TRUE;
1326 }
1327
1328
1329 /******************************************************************************
1330 * GetStringTypeExW (KERNEL32.@)
1331 *
1332 * See GetStringTypeExA.
1333 */
1334 BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
1335 {
1336 /* locale is ignored for Unicode */
1337 return GetStringTypeW( type, src, count, chartype );
1338 }
1339
1340
1341 /******************************************************************************
1342 * GetStringTypeA (KERNEL32.@)
1343 *
1344 * Get characteristics of the characters making up a string.
1345 *
1346 * PARAMS
1347 * locale [I] Locale Id for the string
1348 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1349 * src [I] String to analyse
1350 * count [I] Length of src in chars, or -1 if src is NUL terminated
1351 * chartype [O] Destination for the calculated characteristics
1352 *
1353 * RETURNS
1354 * Success: TRUE. chartype is filled with the requested characteristics of each char
1355 * in src.
1356 * Failure: FALSE. Use GetLastError() to determine the cause.
1357 */
1358 BOOL WINAPI GetStringTypeA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1359 {
1360 UINT cp;
1361 INT countW;
1362 LPWSTR srcW;
1363 BOOL ret = FALSE;
1364
1365 if(count == -1) count = strlen(src) + 1;
1366
1367 if (!(cp = get_lcid_codepage( locale )))
1368 {
1369 FIXME("For locale %04x using current ANSI code page\n", locale);
1370 cp = GetACP();
1371 }
1372
1373 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
1374 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
1375 {
1376 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
1377 /*
1378 * NOTE: the target buffer has 1 word for each CHARACTER in the source
1379 * string, with multibyte characters there maybe be more bytes in count
1380 * than character space in the buffer!
1381 */
1382 ret = GetStringTypeW(type, srcW, countW, chartype);
1383 HeapFree(GetProcessHeap(), 0, srcW);
1384 }
1385 return ret;
1386 }
1387
1388 /******************************************************************************
1389 * GetStringTypeExA (KERNEL32.@)
1390 *
1391 * Get characteristics of the characters making up a string.
1392 *
1393 * PARAMS
1394 * locale [I] Locale Id for the string
1395 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
1396 * src [I] String to analyse
1397 * count [I] Length of src in chars, or -1 if src is NUL terminated
1398 * chartype [O] Destination for the calculated characteristics
1399 *
1400 * RETURNS
1401 * Success: TRUE. chartype is filled with the requested characteristics of each char
1402 * in src.
1403 * Failure: FALSE. Use GetLastError() to determine the cause.
1404 */
1405 BOOL WINAPI GetStringTypeExA( LCID locale, DWORD type, LPCSTR src, INT count, LPWORD chartype )
1406 {
1407 return GetStringTypeA(locale, type, src, count, chartype);
1408 }
1409
1410
1411 /*************************************************************************
1412 * LCMapStringW (KERNEL32.@)
1413 *
1414 * See LCMapStringA.
1415 */
1416 INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen,
1417 LPWSTR dst, INT dstlen)
1418 {
1419 LPWSTR dst_ptr;
1420
1421 if (!src || !srclen || dstlen < 0)
1422 {
1423 SetLastError(ERROR_INVALID_PARAMETER);
1424 return 0;
1425 }
1426
1427 /* mutually exclusive flags */
1428 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
1429 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
1430 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
1431 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE))
1432 {
1433 SetLastError(ERROR_INVALID_FLAGS);
1434 return 0;
1435 }
1436
1437 if (!dstlen) dst = NULL;
1438
1439 lcid = ConvertDefaultLocale(lcid);
1440
1441 if (flags & LCMAP_SORTKEY)
1442 {
1443 INT ret;
1444 if (src == dst)
1445 {
1446 SetLastError(ERROR_INVALID_FLAGS);
1447 return 0;
1448 }
1449
1450 if (srclen < 0) srclen = strlenW(src);
1451
1452 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
1453 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1454
1455 ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
1456 if (ret == 0)
1457 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1458 else
1459 ret++;
1460 return ret;
1461 }
1462
1463 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
1464 if (flags & SORT_STRINGSORT)
1465 {
1466 SetLastError(ERROR_INVALID_FLAGS);
1467 return 0;
1468 }
1469
1470 if (srclen < 0) srclen = strlenW(src) + 1;
1471
1472 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
1473 lcid, flags, debugstr_wn(src, srclen), srclen, dst, dstlen);
1474
1475 if (!dst) /* return required string length */
1476 {
1477 INT len;
1478
1479 for (len = 0; srclen; src++, srclen--)
1480 {
1481 WCHAR wch = *src;
1482 /* tests show that win2k just ignores NORM_IGNORENONSPACE,
1483 * and skips white space and punctuation characters for
1484 * NORM_IGNORESYMBOLS.
1485 */
1486 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1487 continue;
1488 len++;
1489 }
1490 return len;
1491 }
1492
1493 if (flags & LCMAP_UPPERCASE)
1494 {
1495 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1496 {
1497 WCHAR wch = *src;
1498 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1499 continue;
1500 *dst_ptr++ = toupperW(wch);
1501 dstlen--;
1502 }
1503 }
1504 else if (flags & LCMAP_LOWERCASE)
1505 {
1506 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1507 {
1508 WCHAR wch = *src;
1509 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1510 continue;
1511 *dst_ptr++ = tolowerW(wch);
1512 dstlen--;
1513 }
1514 }
1515 else
1516 {
1517 if (src == dst)
1518 {
1519 SetLastError(ERROR_INVALID_FLAGS);
1520 return 0;
1521 }
1522 for (dst_ptr = dst; srclen && dstlen; src++, srclen--)
1523 {
1524 WCHAR wch = *src;
1525 if ((flags & NORM_IGNORESYMBOLS) && (get_char_typeW(wch) & (C1_PUNCT | C1_SPACE)))
1526 continue;
1527 *dst_ptr++ = wch;
1528 dstlen--;
1529 }
1530 }
1531
1532 if (srclen)
1533 {
1534 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1535 return 0;
1536 }
1537
1538 return dst_ptr - dst;
1539 }
1540
1541 /*************************************************************************
1542 * LCMapStringA (KERNEL32.@)
1543 *
1544 * Map characters in a locale sensitive string.
1545 *
1546 * PARAMS
1547 * lcid [I] LCID for the conversion.
1548 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
1549 * src [I] String to map
1550 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
1551 * dst [O] Destination for mapped string
1552 * dstlen [I] Length of dst in characters
1553 *
1554 * RETURNS
1555 * Success: The length of the mapped string in dst, including the NUL terminator.
1556 * Failure: 0. Use GetLastError() to determine the cause.
1557 */
1558 INT WINAPI LCMapStringA(LCID lcid, DWORD flags, LPCSTR src, INT srclen,
1559 LPSTR dst, INT dstlen)
1560 {
1561 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
1562 LPWSTR srcW, dstW;
1563 INT ret = 0, srclenW, dstlenW;
1564 UINT locale_cp = CP_ACP;
1565
1566 if (!src || !srclen || dstlen < 0)
1567 {
1568 SetLastError(ERROR_INVALID_PARAMETER);
1569 return 0;
1570 }
1571
1572 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
1573
1574 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
1575 if (srclenW)
1576 srcW = bufW;
1577 else
1578 {
1579 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
1580 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
1581 if (!srcW)
1582 {
1583 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1584 return 0;
1585 }
1586 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
1587 }
1588
1589 if (flags & LCMAP_SORTKEY)
1590 {
1591 if (src == dst)
1592 {
1593 SetLastError(ERROR_INVALID_FLAGS);
1594 goto map_string_exit;
1595 }
1596 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
1597 if (ret == 0)
1598 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1599 else
1600 ret++;
1601 goto map_string_exit;
1602 }
1603
1604 if (flags & SORT_STRINGSORT)
1605 {
1606 SetLastError(ERROR_INVALID_FLAGS);
1607 goto map_string_exit;
1608 }
1609
1610 dstlenW = LCMapStringW(lcid, flags, srcW, srclenW, NULL, 0);
1611 if (!dstlenW)
1612 goto map_string_exit;
1613
1614 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
1615 if (!dstW)
1616 {
1617 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1618 goto map_string_exit;
1619 }
1620
1621 LCMapStringW(lcid, flags, srcW, srclenW, dstW, dstlenW);
1622 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
1623 HeapFree(GetProcessHeap(), 0, dstW);
1624
1625 map_string_exit:
1626 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
1627 return ret;
1628 }
1629
1630 /*************************************************************************
1631 * FoldStringA (KERNEL32.@)
1632 *
1633 * Map characters in a string.
1634 *
1635 * PARAMS
1636 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
1637 * src [I] String to map
1638 * srclen [I] Length of src, or -1 if src is NUL terminated
1639 * dst [O] Destination for mapped string
1640 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
1641 *
1642 * RETURNS
1643 * Success: The length of the string written to dst, including the terminating NUL. If
1644 * dstlen is 0, the value returned is the same, but nothing is written to dst,
1645 * and dst may be NULL.
1646 * Failure: 0. Use GetLastError() to determine the cause.
1647 */
1648 INT WINAPI FoldStringA(DWORD dwFlags, LPCSTR src, INT srclen,
1649 LPSTR dst, INT dstlen)
1650 {
1651 INT ret = 0, srclenW = 0;
1652 WCHAR *srcW = NULL, *dstW = NULL;
1653
1654 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
1655 {
1656 SetLastError(ERROR_INVALID_PARAMETER);
1657 return 0;
1658 }
1659
1660 srclenW = MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
1661 src, srclen, NULL, 0);
1662 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
1663
1664 if (!srcW)
1665 {
1666 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1667 goto FoldStringA_exit;
1668 }
1669
1670 MultiByteToWideChar(CP_ACP, dwFlags & MAP_COMPOSITE ? MB_COMPOSITE : 0,
1671 src, srclen, srcW, srclenW);
1672
1673 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
1674
1675 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
1676 if (ret && dstlen)
1677 {
1678 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
1679
1680 if (!dstW)
1681 {
1682 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1683 goto FoldStringA_exit;
1684 }
1685
1686 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
1687 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
1688 {
1689 ret = 0;
1690 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1691 }
1692 }
1693
1694 HeapFree(GetProcessHeap(), 0, dstW);
1695
1696 FoldStringA_exit:
1697 HeapFree(GetProcessHeap(), 0, srcW);
1698 return ret;
1699 }
1700
1701 /*************************************************************************
1702 * FoldStringW (KERNEL32.@)
1703 *
1704 * See FoldStringA.
1705 */
1706 INT WINAPI FoldStringW(DWORD dwFlags, LPCWSTR src, INT srclen,
1707 LPWSTR dst, INT dstlen)
1708 {
1709 int ret;
1710
1711 switch (dwFlags & (MAP_COMPOSITE|MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES))
1712 {
1713 case 0:
1714 if (dwFlags)
1715 break;
1716 /* Fall through for dwFlags == 0 */
1717 case MAP_PRECOMPOSED|MAP_COMPOSITE:
1718 case MAP_PRECOMPOSED|MAP_EXPAND_LIGATURES:
1719 case MAP_COMPOSITE|MAP_EXPAND_LIGATURES:
1720 SetLastError(ERROR_INVALID_FLAGS);
1721 return 0;
1722 }
1723
1724 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
1725 {
1726 SetLastError(ERROR_INVALID_PARAMETER);
1727 return 0;
1728 }
1729
1730 ret = wine_fold_string(dwFlags, src, srclen, dst, dstlen);
1731 if (!ret)
1732 SetLastError(ERROR_INSUFFICIENT_BUFFER);
1733 return ret;
1734 }
1735
1736 /******************************************************************************
1737 * CompareStringW (KERNEL32.@)
1738 *
1739 * See CompareStringA.
1740 */
1741 INT WINAPI CompareStringW(LCID lcid, DWORD style,
1742 LPCWSTR str1, INT len1, LPCWSTR str2, INT len2)
1743 {
1744 INT ret;
1745
1746 if (!str1 || !str2)
1747 {
1748 SetLastError(ERROR_INVALID_PARAMETER);
1749 return 0;
1750 }
1751
1752 if( style & ~(NORM_IGNORECASE|NORM_IGNORENONSPACE|NORM_IGNORESYMBOLS|
1753 SORT_STRINGSORT|NORM_IGNOREKANATYPE|NORM_IGNOREWIDTH|LOCALE_USE_CP_ACP|0x10000000) )
1754 {
1755 SetLastError(ERROR_INVALID_FLAGS);
1756 return 0;
1757 }
1758
1759 /* this style is related to diacritics in Arabic, Japanese, and Hebrew */
1760 if (style & 0x10000000)
1761 WARN("Ignoring unknown style 0x10000000\n");
1762
1763 if (len1 < 0) len1 = strlenW(str1);
1764 if (len2 < 0) len2 = strlenW(str2);
1765
1766 ret = wine_compare_string(style, str1, len1, str2, len2);
1767
1768 if (ret) /* need to translate result */
1769 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
1770 return CSTR_EQUAL;
1771 }
1772
1773 /******************************************************************************
1774 * CompareStringA (KERNEL32.@)
1775 *
1776 * Compare two locale sensitive strings.
1777 *
1778 * PARAMS
1779 * lcid [I] LCID for the comparison
1780 * style [I] Flags for the comparison (NORM_ constants from "winnls.h").
1781 * str1 [I] First string to compare
1782 * len1 [I] Length of str1, or -1 if str1 is NUL terminated
1783 * str2 [I] Second string to compare
1784 * len2 [I] Length of str2, or -1 if str2 is NUL terminated
1785 *
1786 * RETURNS
1787 * Success: CSTR_LESS_THAN, CSTR_EQUAL or CSTR_GREATER_THAN depending on whether
1788 * str1 is less than, equal to or greater than str2 respectively.
1789 * Failure: FALSE. Use GetLastError() to determine the cause.
1790 */
1791 INT WINAPI CompareStringA(LCID lcid, DWORD style,
1792 LPCSTR str1, INT len1, LPCSTR str2, INT len2)
1793 {
1794 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
1795 WCHAR *buf2W = buf1W + 130;
1796 LPWSTR str1W, str2W;
1797 INT len1W, len2W, ret;
1798 UINT locale_cp = CP_ACP;
1799
1800 if (!str1 || !str2)
1801 {
1802 SetLastError(ERROR_INVALID_PARAMETER);
1803 return 0;
1804 }
1805 if (len1 < 0) len1 = strlen(str1);
1806 if (len2 < 0) len2 = strlen(str2);
1807
1808 if (!(style & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
1809
1810 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, buf1W, 130);
1811 if (len1W)
1812 str1W = buf1W;
1813 else
1814 {
1815 len1W = MultiByteToWideChar(locale_cp, 0, str1, len1, NULL, 0);
1816 str1W = HeapAlloc(GetProcessHeap(), 0, len1W * sizeof(WCHAR));
1817 if (!str1W)
1818 {
1819 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1820 return 0;
1821 }
1822 MultiByteToWideChar(locale_cp, 0, str1, len1, str1W, len1W);
1823 }
1824 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, buf2W, 130);
1825 if (len2W)
1826 str2W = buf2W;
1827 else
1828 {
1829 len2W = MultiByteToWideChar(locale_cp, 0, str2, len2, NULL, 0);
1830 str2W = HeapAlloc(GetProcessHeap(), 0, len2W * sizeof(WCHAR));
1831 if (!str2W)
1832 {
1833 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
1834 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
1835 return 0;
1836 }
1837 MultiByteToWideChar(locale_cp, 0, str2, len2, str2W, len2W);
1838 }
1839
1840 ret = CompareStringW(lcid, style, str1W, len1W, str2W, len2W);
1841
1842 if (str1W != buf1W) HeapFree(GetProcessHeap(), 0, str1W);
1843 if (str2W != buf2W) HeapFree(GetProcessHeap(), 0, str2W);
1844 return ret;
1845 }
1846
1847 static HANDLE NLS_RegOpenKey(HANDLE hRootKey, LPCWSTR szKeyName)
1848 {
1849 UNICODE_STRING keyName;
1850 OBJECT_ATTRIBUTES attr;
1851 HANDLE hkey;
1852
1853 RtlInitUnicodeString( &keyName, szKeyName );
1854 InitializeObjectAttributes(&attr, &keyName, OBJ_CASE_INSENSITIVE, hRootKey, NULL);
1855
1856 if (NtOpenKey( &hkey, KEY_READ, &attr ) != STATUS_SUCCESS)
1857 hkey = 0;
1858
1859 return hkey;
1860 }
1861
1862 static BOOL NLS_RegEnumSubKey(HANDLE hKey, UINT ulIndex, LPWSTR szKeyName,
1863 ULONG keyNameSize)
1864 {
1865 BYTE buffer[80];
1866 KEY_BASIC_INFORMATION *info = (KEY_BASIC_INFORMATION *)buffer;
1867 DWORD dwLen;
1868
1869 if (NtEnumerateKey( hKey, ulIndex, KeyBasicInformation, buffer,
1870 sizeof(buffer), &dwLen) != STATUS_SUCCESS ||
1871 info->NameLength > keyNameSize)
1872 {
1873 return FALSE;
1874 }
1875
1876 TRACE("info->Name %s info->NameLength %d\n", debugstr_w(info->Name), info->NameLength);
1877
1878 memcpy( szKeyName, info->Name, info->NameLength);
1879 szKeyName[info->NameLength / sizeof(WCHAR)] = '\0';
1880
1881 TRACE("returning %s\n", debugstr_w(szKeyName));
1882 return TRUE;
1883 }
1884
1885 static BOOL NLS_RegEnumValue(HANDLE hKey, UINT ulIndex,
1886 LPWSTR szValueName, ULONG valueNameSize,
1887 LPWSTR szValueData, ULONG valueDataSize)
1888 {
1889 BYTE buffer[80];
1890 KEY_VALUE_FULL_INFORMATION *info = (KEY_VALUE_FULL_INFORMATION *)buffer;
1891 DWORD dwLen;
1892
1893 if (NtEnumerateValueKey( hKey, ulIndex, KeyValueFullInformation,
1894 buffer, sizeof(buffer), &dwLen ) != STATUS_SUCCESS ||
1895 info->NameLength > valueNameSize ||
1896 info->DataLength > valueDataSize)
1897 {
1898 return FALSE;
1899 }
1900
1901 TRACE("info->Name %s info->DataLength %d\n", debugstr_w(info->Name), info->DataLength);
1902
1903 memcpy( szValueName, info->Name, info->NameLength);
1904 szValueName[info->NameLength / sizeof(WCHAR)] = '\0';
1905 memcpy( szValueData, buffer + info->DataOffset, info->DataLength );
1906 szValueData[info->DataLength / sizeof(WCHAR)] = '\0';
1907
1908 TRACE("returning %s %s\n", debugstr_w(szValueName), debugstr_w(szValueData));
1909 return TRUE;
1910 }
1911
1912 static BOOL NLS_RegGetDword(HANDLE hKey, LPCWSTR szValueName, DWORD *lpVal)
1913 {
1914 BYTE buffer[128];
1915 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buffer;
1916 DWORD dwSize = sizeof(buffer);
1917 UNICODE_STRING valueName;
1918
1919 RtlInitUnicodeString( &valueName, szValueName );
1920
1921 TRACE("%p, %s\n", hKey, debugstr_w(szValueName));
1922 if (NtQueryValueKey( hKey, &valueName, KeyValuePartialInformation,
1923 buffer, dwSize, &dwSize ) == STATUS_SUCCESS &&
1924 info->DataLength == sizeof(DWORD))
1925 {
1926 memcpy(lpVal, info->Data, sizeof(DWORD));
1927 return TRUE;
1928 }
1929
1930 return FALSE;
1931 }
1932
1933 static BOOL NLS_GetLanguageGroupName(LGRPID lgrpid, LPWSTR szName, ULONG nameSize)
1934 {
1935 LANGID langId;
1936 LPCWSTR szResourceName = MAKEINTRESOURCEW(((lgrpid + 0x2000) >> 4) + 1);
1937 HRSRC hResource;
1938 BOOL bRet = FALSE;
1939
1940 /* FIXME: Is it correct to use the system default langid? */
1941 langId = GetSystemDefaultLangID();
1942
1943 if (SUBLANGID(langId) == SUBLANG_NEUTRAL)
1944 langId = MAKELANGID( PRIMARYLANGID(langId), SUBLANG_DEFAULT );
1945
1946 hResource = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING, szResourceName, langId );
1947
1948 if (hResource)
1949 {
1950 HGLOBAL hResDir = LoadResource( kernel32_handle, hResource );
1951
1952 if (hResDir)
1953 {
1954 ULONG iResourceIndex = lgrpid & 0xf;
1955 LPCWSTR lpResEntry = LockResource( hResDir );
1956 ULONG i;
1957
1958 for (i = 0; i < iResourceIndex; i++)
1959 lpResEntry += *lpResEntry + 1;
1960
1961 if (*lpResEntry < nameSize)
1962 {
1963 memcpy( szName, lpResEntry + 1, *lpResEntry * sizeof(WCHAR) );
1964 szName[*lpResEntry] = '\0';
1965 bRet = TRUE;
1966 }
1967
1968 }
1969 FreeResource( hResource );
1970 }
1971 return bRet;
1972 }
1973
1974 /* Registry keys for NLS related information */
1975
1976 static const WCHAR szCountryListName[] = {
1977 '\\','R','e','g','i','s','t','r','y','\\',
1978 'M','a','c','h','i','n','e','\\','S','o','f','t','w','a','r','e','\\',
1979 'M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s','\\',
1980 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1981 'T','e','l','e','p','h','o','n','y','\\',
1982 'C','o','u','n','t','r','y',' ','L','i','s','t','\0'
1983 };
1984
1985
1986 /* Callback function ptrs for EnumSystemLanguageGroupsA/W */
1987 typedef struct
1988 {
1989 LANGUAGEGROUP_ENUMPROCA procA;
1990 LANGUAGEGROUP_ENUMPROCW procW;
1991 DWORD dwFlags;
1992 LONG_PTR lParam;
1993 } ENUMLANGUAGEGROUP_CALLBACKS;
1994
1995 /* Internal implementation of EnumSystemLanguageGroupsA/W */
1996 static BOOL NLS_EnumSystemLanguageGroups(ENUMLANGUAGEGROUP_CALLBACKS *lpProcs)
1997 {
1998 WCHAR szNumber[10], szValue[4];
1999 HANDLE hKey;
2000 BOOL bContinue = TRUE;
2001 ULONG ulIndex = 0;
2002
2003 if (!lpProcs)
2004 {
2005 SetLastError(ERROR_INVALID_PARAMETER);
2006 return FALSE;
2007 }
2008
2009 switch (lpProcs->dwFlags)
2010 {
2011 case 0:
2012 /* Default to LGRPID_INSTALLED */
2013 lpProcs->dwFlags = LGRPID_INSTALLED;
2014 /* Fall through... */
2015 case LGRPID_INSTALLED:
2016 case LGRPID_SUPPORTED:
2017 break;
2018 default:
2019 SetLastError(ERROR_INVALID_FLAGS);
2020 return FALSE;
2021 }
2022
2023 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
2024
2025 if (!hKey)
2026 FIXME("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
2027
2028 while (bContinue)
2029 {
2030 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2031 szValue, sizeof(szValue) ))
2032 {
2033 BOOL bInstalled = szValue[0] == '1' ? TRUE : FALSE;
2034 LGRPID lgrpid = strtoulW( szNumber, NULL, 16 );
2035
2036 TRACE("grpid %s (%sinstalled)\n", debugstr_w(szNumber),
2037 bInstalled ? "" : "not ");
2038
2039 if (lpProcs->dwFlags == LGRPID_SUPPORTED || bInstalled)
2040 {
2041 WCHAR szGrpName[48];
2042
2043 if (!NLS_GetLanguageGroupName( lgrpid, szGrpName, sizeof(szGrpName) / sizeof(WCHAR) ))
2044 szGrpName[0] = '\0';
2045
2046 if (lpProcs->procW)
2047 bContinue = lpProcs->procW( lgrpid, szNumber, szGrpName, lpProcs->dwFlags,
2048 lpProcs->lParam );
2049 else
2050 {
2051 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2052 char szGrpNameA[48];
2053
2054 /* FIXME: MSDN doesn't say which code page the W->A translation uses,
2055 * or whether the language names are ever localised. Assume CP_ACP.
2056 */
2057
2058 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2059 WideCharToMultiByte(CP_ACP, 0, szGrpName, -1, szGrpNameA, sizeof(szGrpNameA), 0, 0);
2060
2061 bContinue = lpProcs->procA( lgrpid, szNumberA, szGrpNameA, lpProcs->dwFlags,
2062 lpProcs->lParam );
2063 }
2064 }
2065
2066 ulIndex++;
2067 }
2068 else
2069 bContinue = FALSE;
2070
2071 if (!bContinue)
2072 break;
2073 }
2074
2075 if (hKey)
2076 NtClose( hKey );
2077
2078 return TRUE;
2079 }
2080
2081 /******************************************************************************
2082 * EnumSystemLanguageGroupsA (KERNEL32.@)
2083 *
2084 * Call a users function for each language group available on the system.
2085 *
2086 * PARAMS
2087 * pLangGrpEnumProc [I] Callback function to call for each language group
2088 * dwFlags [I] LGRPID_SUPPORTED=All Supported, LGRPID_INSTALLED=Installed only
2089 * lParam [I] User parameter to pass to pLangGrpEnumProc
2090 *
2091 * RETURNS
2092 * Success: TRUE.
2093 * Failure: FALSE. Use GetLastError() to determine the cause.
2094 */
2095 BOOL WINAPI EnumSystemLanguageGroupsA(LANGUAGEGROUP_ENUMPROCA pLangGrpEnumProc,
2096 DWORD dwFlags, LONG_PTR lParam)
2097 {
2098 ENUMLANGUAGEGROUP_CALLBACKS procs;
2099
2100 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2101
2102 procs.procA = pLangGrpEnumProc;
2103 procs.procW = NULL;
2104 procs.dwFlags = dwFlags;
2105 procs.lParam = lParam;
2106
2107 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2108 }
2109
2110 /******************************************************************************
2111 * EnumSystemLanguageGroupsW (KERNEL32.@)
2112 *
2113 * See EnumSystemLanguageGroupsA.
2114 */
2115 BOOL WINAPI EnumSystemLanguageGroupsW(LANGUAGEGROUP_ENUMPROCW pLangGrpEnumProc,
2116 DWORD dwFlags, LONG_PTR lParam)
2117 {
2118 ENUMLANGUAGEGROUP_CALLBACKS procs;
2119
2120 TRACE("(%p,0x%08X,0x%08lX)\n", pLangGrpEnumProc, dwFlags, lParam);
2121
2122 procs.procA = NULL;
2123 procs.procW = pLangGrpEnumProc;
2124 procs.dwFlags = dwFlags;
2125 procs.lParam = lParam;
2126
2127 return NLS_EnumSystemLanguageGroups( pLangGrpEnumProc ? &procs : NULL);
2128 }
2129
2130 /******************************************************************************
2131 * IsValidLanguageGroup (KERNEL32.@)
2132 *
2133 * Determine if a language group is supported and/or installed.
2134 *
2135 * PARAMS
2136 * lgrpid [I] Language Group Id (LGRPID_ values from "winnls.h")
2137 * dwFlags [I] LGRPID_SUPPORTED=Supported, LGRPID_INSTALLED=Installed
2138 *
2139 * RETURNS
2140 * TRUE, if lgrpid is supported and/or installed, according to dwFlags.
2141 * FALSE otherwise.
2142 */
2143 BOOL WINAPI IsValidLanguageGroup(LGRPID lgrpid, DWORD dwFlags)
2144 {
2145 static const WCHAR szFormat[] = { '%','x','\0' };
2146 WCHAR szValueName[16], szValue[2];
2147 BOOL bSupported = FALSE, bInstalled = FALSE;
2148 HANDLE hKey;
2149
2150
2151 switch (dwFlags)
2152 {
2153 case LGRPID_INSTALLED:
2154 case LGRPID_SUPPORTED:
2155
2156 hKey = NLS_RegOpenKey( 0, szLangGroupsKeyName );
2157
2158 sprintfW( szValueName, szFormat, lgrpid );
2159
2160 if (NLS_RegGetDword( hKey, szValueName, (LPDWORD)szValue ))
2161 {
2162 bSupported = TRUE;
2163
2164 if (szValue[0] == '1')
2165 bInstalled = TRUE;
2166 }
2167
2168 if (hKey)
2169 NtClose( hKey );
2170
2171 break;
2172 }
2173
2174 if ((dwFlags == LGRPID_SUPPORTED && bSupported) ||
2175 (dwFlags == LGRPID_INSTALLED && bInstalled))
2176 return TRUE;
2177
2178 return FALSE;
2179 }
2180
2181 /* Callback function ptrs for EnumLanguageGrouplocalesA/W */
2182 typedef struct
2183 {
2184 LANGGROUPLOCALE_ENUMPROCA procA;
2185 LANGGROUPLOCALE_ENUMPROCW procW;
2186 DWORD dwFlags;
2187 LGRPID lgrpid;
2188 LONG_PTR lParam;
2189 } ENUMLANGUAGEGROUPLOCALE_CALLBACKS;
2190
2191 /* Internal implementation of EnumLanguageGrouplocalesA/W */
2192 static BOOL NLS_EnumLanguageGroupLocales(ENUMLANGUAGEGROUPLOCALE_CALLBACKS *lpProcs)
2193 {
2194 static const WCHAR szAlternateSortsKeyName[] = {
2195 'A','l','t','e','r','n','a','t','e',' ','S','o','r','t','s','\0'
2196 };
2197 WCHAR szNumber[10], szValue[4];
2198 HANDLE hKey;
2199 BOOL bContinue = TRUE, bAlternate = FALSE;
2200 LGRPID lgrpid;
2201 ULONG ulIndex = 1; /* Ignore default entry of 1st key */
2202
2203 if (!lpProcs || !lpProcs->lgrpid || lpProcs->lgrpid > LGRPID_ARMENIAN)
2204 {
2205 SetLastError(ERROR_INVALID_PARAMETER);
2206 return FALSE;
2207 }
2208
2209 if (lpProcs->dwFlags)
2210 {
2211 SetLastError(ERROR_INVALID_FLAGS);
2212 return FALSE;
2213 }
2214
2215 hKey = NLS_RegOpenKey( 0, szLocaleKeyName );
2216
2217 if (!hKey)
2218 WARN("NLS registry key not found. Please apply the default registry file 'wine.inf'\n");
2219
2220 while (bContinue)
2221 {
2222 if (NLS_RegEnumValue( hKey, ulIndex, szNumber, sizeof(szNumber),
2223 szValue, sizeof(szValue) ))
2224 {
2225 lgrpid = strtoulW( szValue, NULL, 16 );
2226
2227 TRACE("lcid %s, grpid %d (%smatched)\n", debugstr_w(szNumber),
2228 lgrpid, lgrpid == lpProcs->lgrpid ? "" : "not ");
2229
2230 if (lgrpid == lpProcs->lgrpid)
2231 {
2232 LCID lcid;
2233
2234 lcid = strtoulW( szNumber, NULL, 16 );
2235
2236 /* FIXME: native returns extra text for a few (17/150) locales, e.g:
2237 * '00000437 ;Georgian'
2238 * At present we only pass the LCID string.
2239 */
2240
2241 if (lpProcs->procW)
2242 bContinue = lpProcs->procW( lgrpid, lcid, szNumber, lpProcs->lParam );
2243 else
2244 {
2245 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2246
2247 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2248
2249 bContinue = lpProcs->procA( lgrpid, lcid, szNumberA, lpProcs->lParam );
2250 }
2251 }
2252
2253 ulIndex++;
2254 }
2255 else
2256 {
2257 /* Finished enumerating this key */
2258 if (!bAlternate)
2259 {
2260 /* Enumerate alternate sorts also */
2261 hKey = NLS_RegOpenKey( hKey, szAlternateSortsKeyName );
2262 bAlternate = TRUE;
2263 ulIndex = 0;
2264 }
2265 else
2266 bContinue = FALSE; /* Finished both keys */
2267 }
2268
2269 if (!bContinue)
2270 break;
2271 }
2272
2273 if (hKey)
2274 NtClose( hKey );
2275
2276 return TRUE;
2277 }
2278
2279 /******************************************************************************
2280 * EnumLanguageGroupLocalesA (KERNEL32.@)
2281 *
2282 * Call a users function for every locale in a language group available on the system.
2283 *
2284 * PARAMS
2285 * pLangGrpLcEnumProc [I] Callback function to call for each locale
2286 * lgrpid [I] Language group (LGRPID_ values from "winnls.h")
2287 * dwFlags [I] Reserved, set to 0
2288 * lParam [I] User parameter to pass to pLangGrpLcEnumProc
2289 *
2290 * RETURNS
2291 * Success: TRUE.
2292 * Failure: FALSE. Use GetLastError() to determine the cause.
2293 */
2294 BOOL WINAPI EnumLanguageGroupLocalesA(LANGGROUPLOCALE_ENUMPROCA pLangGrpLcEnumProc,
2295 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2296 {
2297 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2298
2299 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2300
2301 callbacks.procA = pLangGrpLcEnumProc;
2302 callbacks.procW = NULL;
2303 callbacks.dwFlags = dwFlags;
2304 callbacks.lgrpid = lgrpid;
2305 callbacks.lParam = lParam;
2306
2307 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2308 }
2309
2310 /******************************************************************************
2311 * EnumLanguageGroupLocalesW (KERNEL32.@)
2312 *
2313 * See EnumLanguageGroupLocalesA.
2314 */
2315 BOOL WINAPI EnumLanguageGroupLocalesW(LANGGROUPLOCALE_ENUMPROCW pLangGrpLcEnumProc,
2316 LGRPID lgrpid, DWORD dwFlags, LONG_PTR lParam)
2317 {
2318 ENUMLANGUAGEGROUPLOCALE_CALLBACKS callbacks;
2319
2320 TRACE("(%p,0x%08X,0x%08X,0x%08lX)\n", pLangGrpLcEnumProc, lgrpid, dwFlags, lParam);
2321
2322 callbacks.procA = NULL;
2323 callbacks.procW = pLangGrpLcEnumProc;
2324 callbacks.dwFlags = dwFlags;
2325 callbacks.lgrpid = lgrpid;
2326 callbacks.lParam = lParam;
2327
2328 return NLS_EnumLanguageGroupLocales( pLangGrpLcEnumProc ? &callbacks : NULL );
2329 }
2330
2331 /* Callback function ptrs for EnumSystemCodePagesA/W */
2332 typedef struct
2333 {
2334 CODEPAGE_ENUMPROCA procA;
2335 CODEPAGE_ENUMPROCW procW;
2336 DWORD dwFlags;
2337 } ENUMSYSTEMCODEPAGES_CALLBACKS;
2338
2339 /* Internal implementation of EnumSystemCodePagesA/W */
2340 static BOOL NLS_EnumSystemCodePages(ENUMSYSTEMCODEPAGES_CALLBACKS *lpProcs)
2341 {
2342 WCHAR szNumber[5 + 1], szValue[MAX_PATH];
2343 HANDLE hKey;
2344 BOOL bContinue = TRUE;
2345 ULONG ulIndex = 0;
2346
2347 if (!lpProcs)
2348 {
2349 SetLastError(ERROR_INVALID_PARAMETER);
2350 return FALSE;
2351 }
2352
2353 switch (lpProcs->dwFlags)
2354 {
2355 case CP_INSTALLED:
2356 case CP_SUPPORTED:
2357 break;
2358 default:
2359 SetLastError(ERROR_INVALID_FLAGS);
2360 return FALSE;
2361 }
2362
2363 hKey = NLS_RegOpenKey(0, L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\CodePage");
2364 if (!hKey)
2365 {
2366 WARN("NLS_RegOpenKey() failed\n");
2367 return FALSE;
2368 }
2369
2370 while (bContinue)
2371 {
2372 if (NLS_RegEnumValue(hKey, ulIndex, szNumber, sizeof(szNumber),
2373 szValue, sizeof(szValue)))
2374 {
2375 if ((lpProcs->dwFlags == CP_SUPPORTED)||
2376 ((lpProcs->dwFlags == CP_INSTALLED)&&(wcslen(szValue) > 2)))
2377 {
2378 if (lpProcs->procW)
2379 {
2380 bContinue = lpProcs->procW(szNumber);
2381 }
2382 else
2383 {
2384 char szNumberA[sizeof(szNumber)/sizeof(WCHAR)];
2385
2386 WideCharToMultiByte(CP_ACP, 0, szNumber, -1, szNumberA, sizeof(szNumberA), 0, 0);
2387 bContinue = lpProcs->procA(szNumberA);
2388 }
2389 }
2390
2391 ulIndex++;
2392
2393 } else bContinue = FALSE;
2394
2395 if (!bContinue)
2396 break;
2397 }
2398
2399 if (hKey)
2400 NtClose(hKey);
2401
2402 return TRUE;
2403 }
2404
2405 /*
2406 * @implemented
2407 */
2408 BOOL
2409 WINAPI
2410 EnumSystemCodePagesW (
2411 CODEPAGE_ENUMPROCW lpCodePageEnumProc,
2412 DWORD dwFlags
2413 )
2414 {
2415 ENUMSYSTEMCODEPAGES_CALLBACKS procs;
2416
2417 TRACE("(%p,0x%08X,0x%08lX)\n", lpCodePageEnumProc, dwFlags);
2418
2419 procs.procA = NULL;
2420 procs.procW = lpCodePageEnumProc;
2421 procs.dwFlags = dwFlags;
2422
2423 return NLS_EnumSystemCodePages(lpCodePageEnumProc ? &procs : NULL);
2424 }
2425
2426
2427 /*
2428 * @implemented
2429 */
2430 BOOL
2431 WINAPI
2432 EnumSystemCodePagesA (
2433 CODEPAGE_ENUMPROCA lpCodePageEnumProc,
2434 DWORD dwFlags
2435 )
2436 {
2437 ENUMSYSTEMCODEPAGES_CALLBACKS procs;
2438
2439 TRACE("(%p,0x%08X,0x%08lX)\n", lpCodePageEnumProc, dwFlags);
2440
2441 procs.procA = lpCodePageEnumProc;
2442 procs.procW = NULL;
2443 procs.dwFlags = dwFlags;
2444
2445 return NLS_EnumSystemCodePages(lpCodePageEnumProc ? &procs : NULL);
2446 }
2447 /******************************************************************************
2448 * EnumSystemGeoID (KERNEL32.@)
2449 *
2450 * Call a users function for every location available on the system.
2451 *
2452 * PARAMS
2453 * geoclass [I] Type of information desired (SYSGEOTYPE enum from "winnls.h")
2454 * reserved [I] Reserved, set to 0
2455 * pGeoEnumProc [I] Callback function to call for each location
2456 *
2457 * RETURNS
2458 * Success: TRUE.
2459 * Failure: FALSE. Use GetLastError() to determine the cause.
2460 */
2461 BOOL WINAPI EnumSystemGeoID(GEOCLASS geoclass, GEOID reserved, GEO_ENUMPROC pGeoEnumProc)
2462 {
2463 static const WCHAR szCountryCodeValueName[] = {
2464 'C','o','u','n','t','r','y','C','o','d','e','\0'
2465 };
2466 WCHAR szNumber[10];
2467 HANDLE hKey;
2468 ULONG ulIndex = 0;
2469
2470 TRACE("(0x%08X,0x%08X,%p)\n", geoclass, reserved, pGeoEnumProc);
2471
2472 if (geoclass != GEOCLASS_NATION || reserved || !pGeoEnumProc)
2473 {
2474 SetLastError(ERROR_INVALID_PARAMETER);
2475 return FALSE;
2476 }
2477
2478 hKey = NLS_RegOpenKey( 0, szCountryListName );
2479
2480 while (NLS_RegEnumSubKey( hKey, ulIndex, szNumber, sizeof(szNumber) ))
2481 {
2482 BOOL bContinue = TRUE;
2483 DWORD dwGeoId;
2484 HANDLE hSubKey = NLS_RegOpenKey( hKey, szNumber );
2485
2486 if (hSubKey)
2487 {
2488 if (NLS_RegGetDword( hSubKey, szCountryCodeValueName, &dwGeoId ))
2489 {
2490 TRACE("Got geoid %d\n", dwGeoId);
2491
2492 if (!pGeoEnumProc( dwGeoId ))
2493 bContinue = FALSE;
2494 }
2495
2496 NtClose( hSubKey );
2497 }
2498
2499 if (!bContinue)
2500 break;
2501
2502 ulIndex++;
2503 }
2504
2505 if (hKey)
2506 NtClose( hKey );
2507
2508 return TRUE;
2509 }
2510
2511 /******************************************************************************
2512 * InvalidateNLSCache (KERNEL32.@)
2513 *
2514 * Invalidate the cache of NLS values.
2515 *
2516 * PARAMS
2517 * None.
2518 *
2519 * RETURNS
2520 * Success: TRUE.
2521 * Failure: FALSE.
2522 */
2523 BOOL WINAPI InvalidateNLSCache(void)
2524 {
2525 FIXME("() stub\n");
2526 return FALSE;
2527 }
2528
2529 /******************************************************************************
2530 * GetUserGeoID (KERNEL32.@)
2531 */
2532 GEOID WINAPI GetUserGeoID( GEOCLASS GeoClass )
2533 {
2534 GEOID ret = GEOID_NOT_AVAILABLE;
2535 static const WCHAR geoW[] = {'G','e','o',0};
2536 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
2537 WCHAR bufferW[40], *end;
2538 DWORD count;
2539 HANDLE hkey, hSubkey = 0;
2540 UNICODE_STRING keyW;
2541 const KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)bufferW;
2542 RtlInitUnicodeString( &keyW, nationW );
2543 count = sizeof(bufferW);
2544
2545 if(!(hkey = create_registry_key())) return ret;
2546
2547 switch( GeoClass ){
2548 case GEOCLASS_NATION:
2549 if ((hSubkey = NLS_RegOpenKey(hkey, geoW)))
2550 {
2551 if((NtQueryValueKey(hSubkey, &keyW, KeyValuePartialInformation,
2552 bufferW, count, &count) == STATUS_SUCCESS ) && info->DataLength)
2553 ret = strtolW((LPCWSTR)info->Data, &end, 10);
2554 }
2555 break;
2556 case GEOCLASS_REGION:
2557 FIXME("GEOCLASS_REGION not handled yet\n");
2558 break;
2559 }
2560
2561 NtClose(hkey);
2562 if (hSubkey) NtClose(hSubkey);
2563 return ret;
2564 }
2565
2566 /******************************************************************************
2567 * SetUserGeoID (KERNEL32.@)
2568 */
2569 BOOL WINAPI SetUserGeoID( GEOID GeoID )
2570 {
2571 static const WCHAR geoW[] = {'G','e','o',0};
2572 static const WCHAR nationW[] = {'N','a','t','i','o','n',0};
2573 static const WCHAR formatW[] = {'%','i',0};
2574 UNICODE_STRING nameW,keyW;
2575 WCHAR bufferW[10];
2576 OBJECT_ATTRIBUTES attr;
2577 HANDLE hkey;
2578
2579 if(!(hkey = create_registry_key())) return FALSE;
2580
2581 attr.Length = sizeof(attr);
2582 attr.RootDirectory = hkey;
2583 attr.ObjectName = &nameW;
2584 attr.Attributes = 0;
2585 attr.SecurityDescriptor = NULL;
2586 attr.SecurityQualityOfService = NULL;
2587 RtlInitUnicodeString( &nameW, geoW );
2588 RtlInitUnicodeString( &keyW, nationW );
2589
2590 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) != STATUS_SUCCESS)
2591
2592 {
2593 NtClose(attr.RootDirectory);
2594 return FALSE;
2595 }
2596
2597 sprintfW(bufferW, formatW, GeoID);
2598 NtSetValueKey(hkey, &keyW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR));
2599 NtClose(attr.RootDirectory);
2600 NtClose(hkey);
2601 return TRUE;
2602 }
2603
2604 typedef struct
2605 {
2606 union
2607 {
2608 UILANGUAGE_ENUMPROCA procA;
2609 UILANGUAGE_ENUMPROCW procW;
2610 } u;
2611 DWORD flags;
2612 LONG_PTR param;
2613 } ENUM_UILANG_CALLBACK;
2614
2615 static BOOL CALLBACK enum_uilang_proc_a( HMODULE hModule, LPCSTR type,
2616 LPCSTR name, WORD LangID, LONG_PTR lParam )
2617 {
2618 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
2619 char buf[20];
2620
2621 sprintf(buf, "%08x", (UINT)LangID);
2622 return enum_uilang->u.procA( buf, enum_uilang->param );
2623 }
2624
2625 static BOOL CALLBACK enum_uilang_proc_w( HMODULE hModule, LPCWSTR type,
2626 LPCWSTR name, WORD LangID, LONG_PTR lParam )
2627 {
2628 static const WCHAR formatW[] = {'%','0','8','x',0};
2629 ENUM_UILANG_CALLBACK *enum_uilang = (ENUM_UILANG_CALLBACK *)lParam;
2630 WCHAR buf[20];
2631
2632 sprintfW( buf, formatW, (UINT)LangID );
2633 return enum_uilang->u.procW( buf, enum_uilang->param );
2634 }
2635
2636 /******************************************************************************
2637 * EnumUILanguagesA (KERNEL32.@)
2638 */
2639 BOOL WINAPI EnumUILanguagesA(UILANGUAGE_ENUMPROCA pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
2640 {
2641 ENUM_UILANG_CALLBACK enum_uilang;
2642
2643 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
2644
2645 if(!pUILangEnumProc) {
2646 SetLastError(ERROR_INVALID_PARAMETER);
2647 return FALSE;
2648 }
2649 if(dwFlags) {
2650 SetLastError(ERROR_INVALID_FLAGS);
2651 return FALSE;
2652 }
2653
2654 enum_uilang.u.procA = pUILangEnumProc;
2655 enum_uilang.flags = dwFlags;
2656 enum_uilang.param = lParam;
2657
2658 EnumResourceLanguagesA( kernel32_handle, (LPCSTR)RT_STRING,
2659 (LPCSTR)LOCALE_ILANGUAGE, enum_uilang_proc_a,
2660 (LONG_PTR)&enum_uilang);
2661 return TRUE;
2662 }
2663
2664 /******************************************************************************
2665 * EnumUILanguagesW (KERNEL32.@)
2666 */
2667 BOOL WINAPI EnumUILanguagesW(UILANGUAGE_ENUMPROCW pUILangEnumProc, DWORD dwFlags, LONG_PTR lParam)
2668 {
2669 ENUM_UILANG_CALLBACK enum_uilang;
2670
2671 TRACE("%p, %x, %lx\n", pUILangEnumProc, dwFlags, lParam);
2672
2673
2674 if(!pUILangEnumProc) {
2675 SetLastError(ERROR_INVALID_PARAMETER);
2676 return FALSE;
2677 }
2678 if(dwFlags) {
2679 SetLastError(ERROR_INVALID_FLAGS);
2680 return FALSE;
2681 }
2682
2683 enum_uilang.u.procW = pUILangEnumProc;
2684 enum_uilang.flags = dwFlags;
2685 enum_uilang.param = lParam;
2686
2687 EnumResourceLanguagesW( kernel32_handle, (LPCWSTR)RT_STRING,
2688 (LPCWSTR)LOCALE_ILANGUAGE, enum_uilang_proc_w,
2689 (LONG_PTR)&enum_uilang);
2690 return TRUE;
2691 }
2692
2693 static int
2694 NLS_GetGeoFriendlyName(GEOID Location, LPWSTR szFriendlyName, int cchData)
2695 {
2696 HANDLE hKey;
2697 WCHAR szPath[MAX_PATH];
2698 UNICODE_STRING ValueName;
2699 KEY_VALUE_PARTIAL_INFORMATION *info;
2700 const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
2701 DWORD dwSize;
2702 NTSTATUS Status;
2703 int Ret;
2704
2705 swprintf(szPath, L"\\REGISTRY\\Machine\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Telephony\\Country List\\%d", Location);
2706
2707 hKey = NLS_RegOpenKey(0, szPath);
2708 if (!hKey)
2709 {
2710 WARN("NLS_RegOpenKey() failed\n");
2711 return 0;
2712 }
2713
2714 dwSize = info_size + cchData * sizeof(WCHAR);
2715
2716 if (!(info = HeapAlloc(GetProcessHeap(), 0, dwSize)))
2717 {
2718 NtClose(hKey);
2719 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
2720 return 0;
2721 }
2722
2723 RtlInitUnicodeString(&ValueName, L"Name");
2724
2725 Status = NtQueryValueKey(hKey, &ValueName, KeyValuePartialInformation,
2726 (LPBYTE)info, dwSize, &dwSize);
2727
2728 if (!Status)
2729 {
2730 Ret = (dwSize - info_size) / sizeof(WCHAR);
2731
2732 if (!Ret || ((WCHAR *)info->Data)[Ret-1])
2733 {
2734 if (Ret < cchData || !szFriendlyName) Ret++;
2735 else
2736 {
2737 WARN("ERROR_INSUFFICIENT_BUFFER\n");
2738 SetLastError(ERROR_INSUFFICIENT_BUFFER);
2739 Ret = 0;
2740 }
2741 }
2742
2743 if (Ret && szFriendlyName)
2744 {
2745 memcpy(szFriendlyName, info->Data, (Ret-1) * sizeof(WCHAR));
2746 szFriendlyName[Ret-1] = 0;
2747 }
2748 }
2749 else if (Status == STATUS_BUFFER_OVERFLOW && !szFriendlyName)
2750 {
2751 Ret = (dwSize - info_size) / sizeof(WCHAR) + 1;
2752 }
2753 else if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
2754 {
2755 Ret = -1;
2756 }
2757 else
2758 {
2759 SetLastError(RtlNtStatusToDosError(Status));
2760 Ret = 0;
2761 }
2762
2763 NtClose(hKey);
2764 HeapFree(GetProcessHeap(), 0, info);
2765
2766 return Ret;
2767 }
2768
2769 /*
2770 * @implemented
2771 */
2772 INT WINAPI GetGeoInfoW(GEOID GeoId, GEOTYPE GeoType, LPWSTR lpGeoData,
2773 int cchData, LANGID LangId)
2774 {
2775 TRACE("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, LangId);
2776
2777 switch (GeoType)
2778 {
2779 case GEO_FRIENDLYNAME:
2780 {
2781 return NLS_GetGeoFriendlyName(GeoId, lpGeoData, cchData);
2782 }
2783 case GEO_TIMEZONES:
2784 case GEO_NATION:
2785 case GEO_LATITUDE:
2786 case GEO_LONGITUDE:
2787 case GEO_ISO2:
2788 case GEO_ISO3:
2789 case GEO_RFC1766:
2790 case GEO_LCID:
2791 case GEO_OFFICIALNAME:
2792 case GEO_OFFICIALLANGUAGES:
2793 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, LangId);
2794 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2795 break;
2796 }
2797
2798 return 0;
2799 }
2800
2801 /*
2802 * @implemented
2803 */
2804 INT WINAPI GetGeoInfoA(GEOID GeoId, GEOTYPE GeoType, LPSTR lpGeoData,
2805 int cchData, LANGID LangId)
2806 {
2807 WCHAR szBuffer[MAX_PATH];
2808 char szBufferA[sizeof(szBuffer)/sizeof(WCHAR)];
2809 int Ret;
2810
2811 TRACE("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, LangId);
2812
2813 switch (GeoType)
2814 {
2815 case GEO_FRIENDLYNAME:
2816 {
2817 Ret = NLS_GetGeoFriendlyName(GeoId, szBuffer, cchData);
2818 WideCharToMultiByte(CP_ACP, 0, szBuffer, -1, szBufferA, sizeof(szBufferA), 0, 0);
2819 strcpy(lpGeoData, szBufferA);
2820 return Ret;
2821 }
2822 case GEO_TIMEZONES:
2823 case GEO_NATION:
2824 case GEO_LATITUDE:
2825 case GEO_LONGITUDE:
2826 case GEO_ISO2:
2827 case GEO_ISO3:
2828 case GEO_RFC1766:
2829 case GEO_LCID:
2830 case GEO_OFFICIALNAME:
2831 case GEO_OFFICIALLANGUAGES:
2832 FIXME("%d %d %p %d %d\n", GeoId, GeoType, lpGeoData, cchData, LangId);
2833 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2834 break;
2835 }
2836
2837 return 0;
2838 }