ReactOS 0.4.15-dev-8219-ge8b88cf
locale.c
Go to the documentation of this file.
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 <k32.h>
25
26#define NDEBUG
27#include <debug.h>
29
30#include "lcformat_private.h"
31#ifdef __REACTOS__
32 #include "japanese.h"
33#endif
34
37
38#undef WINVER
39#define WINVER DLL_EXPORT_VERSION
40
41/* From winnls.h */
42#define LOCALE_NAME_USER_DEFAULT NULL
43
44#define REG_SZ 1
45extern int wine_fold_string(int flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen);
46extern int wine_get_sortkey(int flags, const WCHAR *src, int srclen, char *dst, int dstlen);
47extern int wine_compare_string(int flags, const WCHAR *str1, int len1, const WCHAR *str2, int len2);
48#ifdef __REACTOS__
50#else
52#endif
53#define NLSRC_OFFSET 5000 /* FIXME */
54
56
57#define LOCALE_LOCALEINFOFLAGSMASK (LOCALE_NOUSEROVERRIDE|LOCALE_USE_CP_ACP|\
58 LOCALE_RETURN_NUMBER|LOCALE_RETURN_GENITIVE_NAMES)
59#define MB_FLAGSMASK (MB_PRECOMPOSED|MB_COMPOSITE|MB_USEGLYPHCHARS|MB_ERR_INVALID_CHARS)
60#define WC_FLAGSMASK (WC_DISCARDNS|WC_SEPCHARS|WC_DEFAULTCHAR|WC_ERR_INVALID_CHARS|\
61 WC_COMPOSITECHECK|WC_NO_BEST_FIT_CHARS)
62
63/* current code pages */
64static const union cptable *ansi_cptable;
65static const union cptable *oem_cptable;
66static const union cptable *mac_cptable;
67static const union cptable *unix_cptable; /* NULL if UTF8 */
68
69static const WCHAR szLocaleKeyName[] = {
70 '\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
71 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
72 'C','o','n','t','r','o','l','\\','N','l','s','\\','L','o','c','a','l','e',0
73};
74
75static const WCHAR szLangGroupsKeyName[] = {
76 '\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
77 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
78 'C','o','n','t','r','o','l','\\','N','l','s','\\',
79 'L','a','n','g','u','a','g','e',' ','G','r','o','u','p','s',0
80};
81
82#if (WINVER >= 0x0600)
83/* Charset to codepage map, sorted by name. */
84static const struct charset_entry
85{
86 const char *charset_name;
88} charset_names[] =
89{
90 { "BIG5", 950 },
91 { "CP1250", 1250 },
92 { "CP1251", 1251 },
93 { "CP1252", 1252 },
94 { "CP1253", 1253 },
95 { "CP1254", 1254 },
96 { "CP1255", 1255 },
97 { "CP1256", 1256 },
98 { "CP1257", 1257 },
99 { "CP1258", 1258 },
100 { "CP932", 932 },
101 { "CP936", 936 },
102 { "CP949", 949 },
103 { "CP950", 950 },
104 { "EUCJP", 20932 },
105 { "GB2312", 936 },
106 { "IBM037", 37 },
107 { "IBM1026", 1026 },
108 { "IBM424", 424 },
109 { "IBM437", 437 },
110 { "IBM500", 500 },
111 { "IBM850", 850 },
112 { "IBM852", 852 },
113 { "IBM855", 855 },
114 { "IBM857", 857 },
115 { "IBM860", 860 },
116 { "IBM861", 861 },
117 { "IBM862", 862 },
118 { "IBM863", 863 },
119 { "IBM864", 864 },
120 { "IBM865", 865 },
121 { "IBM866", 866 },
122 { "IBM869", 869 },
123 { "IBM874", 874 },
124 { "IBM875", 875 },
125 { "ISO88591", 28591 },
126 { "ISO885910", 28600 },
127 { "ISO885913", 28603 },
128 { "ISO885914", 28604 },
129 { "ISO885915", 28605 },
130 { "ISO885916", 28606 },
131 { "ISO88592", 28592 },
132 { "ISO88593", 28593 },
133 { "ISO88594", 28594 },
134 { "ISO88595", 28595 },
135 { "ISO88596", 28596 },
136 { "ISO88597", 28597 },
137 { "ISO88598", 28598 },
138 { "ISO88599", 28599 },
139 { "KOI8R", 20866 },
140 { "KOI8U", 21866 },
141 { "UTF8", CP_UTF8 }
142};
143#endif
144
145
147{
148 WCHAR win_name[128]; /* Windows name ("en-US") */
149 WCHAR lang[128]; /* language ("en") (note: buffer contains the other strings too) */
150 WCHAR *country; /* country ("US") */
151 WCHAR *charset; /* charset ("UTF-8") for Unix format only */
152 WCHAR *script; /* script ("Latn") for Windows format only */
153 WCHAR *modifier; /* modifier or sort order */
154 LCID lcid; /* corresponding LCID */
155 int matches; /* number of elements matching LCID (0..4) */
156 UINT codepage; /* codepage corresponding to charset */
157};
158
159/* locale ids corresponding to the various Unix locale parameters */
169
170static const WCHAR iCalendarTypeW[] = {'i','C','a','l','e','n','d','a','r','T','y','p','e',0};
171static const WCHAR iCountryW[] = {'i','C','o','u','n','t','r','y',0};
172static const WCHAR iCurrDigitsW[] = {'i','C','u','r','r','D','i','g','i','t','s',0};
173static const WCHAR iCurrencyW[] = {'i','C','u','r','r','e','n','c','y',0};
174static const WCHAR iDateW[] = {'i','D','a','t','e',0};
175static const WCHAR iDigitsW[] = {'i','D','i','g','i','t','s',0};
176static const WCHAR iFirstDayOfWeekW[] = {'i','F','i','r','s','t','D','a','y','O','f','W','e','e','k',0};
177static const WCHAR iFirstWeekOfYearW[] = {'i','F','i','r','s','t','W','e','e','k','O','f','Y','e','a','r',0};
178static const WCHAR iLDateW[] = {'i','L','D','a','t','e',0};
179static const WCHAR iLZeroW[] = {'i','L','Z','e','r','o',0};
180static const WCHAR iMeasureW[] = {'i','M','e','a','s','u','r','e',0};
181static const WCHAR iNegCurrW[] = {'i','N','e','g','C','u','r','r',0};
182static const WCHAR iNegNumberW[] = {'i','N','e','g','N','u','m','b','e','r',0};
183static const WCHAR iPaperSizeW[] = {'i','P','a','p','e','r','S','i','z','e',0};
184static const WCHAR iTLZeroW[] = {'i','T','L','Z','e','r','o',0};
185static const WCHAR iTimePrefixW[] = {'i','T','i','m','e','P','r','e','f','i','x',0};
186static const WCHAR iTimeW[] = {'i','T','i','m','e',0};
187static const WCHAR s1159W[] = {'s','1','1','5','9',0};
188static const WCHAR s2359W[] = {'s','2','3','5','9',0};
189static const WCHAR sCountryW[] = {'s','C','o','u','n','t','r','y',0};
190static const WCHAR sCurrencyW[] = {'s','C','u','r','r','e','n','c','y',0};
191static const WCHAR sDateW[] = {'s','D','a','t','e',0};
192static const WCHAR sDecimalW[] = {'s','D','e','c','i','m','a','l',0};
193static const WCHAR sGroupingW[] = {'s','G','r','o','u','p','i','n','g',0};
194static const WCHAR sLanguageW[] = {'s','L','a','n','g','u','a','g','e',0};
195static const WCHAR sListW[] = {'s','L','i','s','t',0};
196static const WCHAR sLongDateW[] = {'s','L','o','n','g','D','a','t','e',0};
197static const WCHAR sMonDecimalSepW[] = {'s','M','o','n','D','e','c','i','m','a','l','S','e','p',0};
198static const WCHAR sMonGroupingW[] = {'s','M','o','n','G','r','o','u','p','i','n','g',0};
199static const WCHAR sMonThousandSepW[] = {'s','M','o','n','T','h','o','u','s','a','n','d','S','e','p',0};
200static const WCHAR sNativeDigitsW[] = {'s','N','a','t','i','v','e','D','i','g','i','t','s',0};
201static const WCHAR sNegativeSignW[] = {'s','N','e','g','a','t','i','v','e','S','i','g','n',0};
202static const WCHAR sPositiveSignW[] = {'s','P','o','s','i','t','i','v','e','S','i','g','n',0};
203static const WCHAR sShortDateW[] = {'s','S','h','o','r','t','D','a','t','e',0};
204static const WCHAR sThousandW[] = {'s','T','h','o','u','s','a','n','d',0};
205static const WCHAR sTimeFormatW[] = {'s','T','i','m','e','F','o','r','m','a','t',0};
206static const WCHAR sTimeW[] = {'s','T','i','m','e',0};
207static const WCHAR sYearMonthW[] = {'s','Y','e','a','r','M','o','n','t','h',0};
208static const WCHAR NumShapeW[] = {'N','u','m','s','h','a','p','e',0};
209
210static struct registry_value
211{
213 const WCHAR *name;
215} registry_values[] =
216{
227 { LOCALE_IPAPERSIZE, iPaperSizeW },
228 { LOCALE_ITIME, iTimeW },
229 { LOCALE_S1159, s1159W },
230 { LOCALE_S2359, s2359W },
232 { LOCALE_SDATE, sDateW },
235 { LOCALE_SLIST, sListW },
244 { LOCALE_STIME, sTimeW },
246 { LOCALE_SYEARMONTH, sYearMonthW },
247 /* The following are not listed under MSDN as supported,
248 * but seem to be used and also stored in the registry.
249 */
251 { LOCALE_IDATE, iDateW },
256 /* The following are used in XP and later */
257 { LOCALE_IDIGITSUBSTITUTION, NumShapeW },
261
262static RTL_CRITICAL_SECTION cache_section = { NULL, -1, 0, 0, 0, 0 };
263
264#ifndef __REACTOS__
265/* Copy Ascii string to Unicode without using codepages */
266static inline void strcpynAtoW( WCHAR *dst, const char *src, size_t n )
267{
268 while (n > 1 && *src)
269 {
270 *dst++ = (unsigned char)*src++;
271 n--;
272 }
273 if (n) *dst = 0;
274}
275
276static inline unsigned short get_table_entry( const unsigned short *table, WCHAR ch )
277{
278 return table[table[table[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0xf)];
279}
280#endif // !__REACTOS__
281
282/***********************************************************************
283 * get_lcid_codepage
284 *
285 * Retrieve the ANSI codepage for a given locale.
286 */
287static inline UINT get_lcid_codepage( LCID lcid )
288{
289 UINT ret;
290 if (!GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE|LOCALE_RETURN_NUMBER, (WCHAR *)&ret,
291 sizeof(ret)/sizeof(WCHAR) )) ret = 0;
292 return ret;
293}
294
295#ifndef __REACTOS__
296/***********************************************************************
297 * get_codepage_table
298 *
299 * Find the table for a given codepage, handling CP_ACP etc. pseudo-codepages
300 */
301static const union cptable *get_codepage_table( unsigned int codepage )
302{
303 const union cptable *ret = NULL;
304
305 assert( ansi_cptable ); /* init must have been done already */
306
307 switch(codepage)
308 {
309 case CP_ACP:
310 return ansi_cptable;
311 case CP_OEMCP:
312 return oem_cptable;
313 case CP_MACCP:
314 return mac_cptable;
315 case CP_UTF7:
316 case CP_UTF8:
317 break;
318 case CP_THREAD_ACP:
319 if (NtCurrentTeb()->CurrentLocale == GetUserDefaultLCID()) return ansi_cptable;
320 codepage = get_lcid_codepage( NtCurrentTeb()->CurrentLocale );
321 if (!codepage) return ansi_cptable;
322 /* fall through */
323 default:
324 if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
325 if (codepage == oem_cptable->info.codepage) return oem_cptable;
326 if (codepage == mac_cptable->info.codepage) return mac_cptable;
328 break;
329 }
330 return ret;
331}
332#endif // !__REACTOS__
333
334#if (WINVER >= 0x0600)
335/***********************************************************************
336 * charset_cmp (internal)
337 */
338static int charset_cmp( const void *name, const void *entry )
339{
340 const struct charset_entry *charset = entry;
341 return strcasecmp( name, charset->charset_name );
342}
343
344/***********************************************************************
345 * find_charset
346 */
347static UINT find_charset( const WCHAR *name )
348{
349 const struct charset_entry *entry;
350 char charset_name[16];
351 size_t i, j;
352
353 /* remove punctuation characters from charset name */
354 for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
355 if (isalnum((unsigned char)name[i])) charset_name[j++] = name[i];
356 charset_name[j] = 0;
357
358 entry = bsearch( charset_name, charset_names,
359 sizeof(charset_names)/sizeof(charset_names[0]),
360 sizeof(charset_names[0]), charset_cmp );
361 if (entry) return entry->codepage;
362 return 0;
363}
364#endif // (WINVER >= 0x0600)
365
367{
368 switch (lang)
369 {
379 }
381 return lang;
382}
383
384#if (WINVER >= 0x0600)
385/***********************************************************************
386 * find_locale_id_callback
387 */
388static BOOL CALLBACK find_locale_id_callback( HMODULE hModule, LPCWSTR type,
390{
391 struct locale_name *data = (struct locale_name *)lParam;
392 WCHAR buffer[128];
393 int matches = 0;
394 LCID lcid = MAKELCID( lang, SORT_DEFAULT ); /* FIXME: handle sort order */
395
396 if (PRIMARYLANGID(lang) == LANG_NEUTRAL) return TRUE; /* continue search */
397
398 /* first check exact name */
399 if (data->win_name[0] &&
401 buffer, sizeof(buffer)/sizeof(WCHAR) ))
402 {
403 if (!strcmpiW( data->win_name, buffer ))
404 {
405 matches = 4; /* everything matches */
406 goto done;
407 }
408 }
409
411 buffer, sizeof(buffer)/sizeof(WCHAR) ))
412 return TRUE;
413 if (strcmpiW( buffer, data->lang )) return TRUE;
414 matches++; /* language name matched */
415
416 if (data->script)
417 {
418 if (GetLocaleInfoW( lcid, LOCALE_SSCRIPTS | LOCALE_NOUSEROVERRIDE,
419 buffer, sizeof(buffer)/sizeof(WCHAR) ))
420 {
421 const WCHAR *p = buffer;
422 unsigned int len = strlenW( data->script );
423 while (*p)
424 {
425 if (!strncmpiW( p, data->script, len ) && (!p[len] || p[len] == ';')) break;
426 if (!(p = strchrW( p, ';'))) goto done;
427 p++;
428 }
429 if (!*p) goto done;
430 matches++; /* script matched */
431 }
432 }
433
434 if (data->country)
435 {
437 buffer, sizeof(buffer)/sizeof(WCHAR) ))
438 {
439 if (strcmpiW( buffer, data->country )) goto done;
440 matches++; /* country name matched */
441 }
442 }
443 else /* match default language */
444 {
445 LANGID def_lang = data->script ? lang : MAKELANGID( PRIMARYLANGID(lang), LANG_NEUTRAL );
446 if (lang == get_default_sublang( def_lang )) matches++;
447 }
448
449 if (data->codepage)
450 {
451 UINT unix_cp;
452 if (GetLocaleInfoW( lcid, LOCALE_IDEFAULTUNIXCODEPAGE | LOCALE_RETURN_NUMBER,
453 (LPWSTR)&unix_cp, sizeof(unix_cp)/sizeof(WCHAR) ))
454 {
455 if (unix_cp == data->codepage) matches++;
456 }
457 }
458
459 /* FIXME: check sort order */
460
461done:
462 if (matches > data->matches)
463 {
464 data->lcid = lcid;
465 data->matches = matches;
466 }
467 return (data->matches < 4); /* no need to continue for perfect match */
468}
469
470
471/***********************************************************************
472 * parse_locale_name
473 *
474 * Parse a locale name into a struct locale_name, handling both Windows and Unix formats.
475 * Unix format is: lang[_country][.charset][@modifier]
476 * Windows format is: lang[-script][-country][_modifier]
477 */
478static void parse_locale_name( const WCHAR *str, struct locale_name *name )
479{
480 static const WCHAR sepW[] = {'-','_','.','@',0};
481 static const WCHAR winsepW[] = {'-','_',0};
482 static const WCHAR posixW[] = {'P','O','S','I','X',0};
483 static const WCHAR cW[] = {'C',0};
484 static const WCHAR latinW[] = {'l','a','t','i','n',0};
485 static const WCHAR latnW[] = {'-','L','a','t','n',0};
486 WCHAR *p;
487
488 TRACE("%s\n", debugstr_w(str));
489
490 name->country = name->charset = name->script = name->modifier = NULL;
492 name->matches = 0;
493 name->codepage = 0;
494 name->win_name[0] = 0;
495 lstrcpynW( name->lang, str, sizeof(name->lang)/sizeof(WCHAR) );
496
497 if (!*name->lang)
498 {
499 name->lcid = LOCALE_INVARIANT;
500 name->matches = 4;
501 return;
502 }
503
504 if (!(p = strpbrkW( name->lang, sepW )))
505 {
506 if (!strcmpW( name->lang, posixW ) || !strcmpW( name->lang, cW ))
507 {
508 name->matches = 4; /* perfect match for default English lcid */
509 return;
510 }
511 strcpyW( name->win_name, name->lang );
512 }
513 else if (*p == '-') /* Windows format */
514 {
515 strcpyW( name->win_name, name->lang );
516 *p++ = 0;
517 name->country = p;
518 if ((p = strpbrkW( p, winsepW )) && *p == '-')
519 {
520 *p++ = 0;
521 name->script = name->country;
522 name->country = p;
523 p = strpbrkW( p, winsepW );
524 }
525 if (p)
526 {
527 *p++ = 0;
528 name->modifier = p;
529 }
530 /* second value can be script or country, check length to resolve the ambiguity */
531 if (!name->script && strlenW( name->country ) == 4)
532 {
533 name->script = name->country;
534 name->country = NULL;
535 }
536 }
537 else /* Unix format */
538 {
539 if (*p == '_')
540 {
541 *p++ = 0;
542 name->country = p;
543 p = strpbrkW( p, sepW + 2 );
544 }
545 if (p && *p == '.')
546 {
547 *p++ = 0;
548 name->charset = p;
549 p = strchrW( p, '@' );
550 }
551 if (p)
552 {
553 *p++ = 0;
554 name->modifier = p;
555 }
556
557 if (name->charset)
558 name->codepage = find_charset( name->charset );
559
560 /* rebuild a Windows name if possible */
561
562 if (name->charset) goto done; /* can't specify charset in Windows format */
563 if (name->modifier && strcmpW( name->modifier, latinW ))
564 goto done; /* only Latn script supported for now */
565 strcpyW( name->win_name, name->lang );
566 if (name->modifier) strcatW( name->win_name, latnW );
567 if (name->country)
568 {
569 p = name->win_name + strlenW(name->win_name);
570 *p++ = '-';
571 strcpyW( p, name->country );
572 }
573 }
574done:
576 find_locale_id_callback, (LPARAM)name );
577}
578#endif
579
580
581/***********************************************************************
582 * convert_default_lcid
583 *
584 * Get the default LCID to use for a given lctype in GetLocaleInfo.
585 */
587{
591 {
592 LCID default_id = 0;
593
594 switch(lctype & 0xffff)
595 {
596 case LOCALE_SSORTNAME:
597 default_id = lcid_LC_COLLATE;
598 break;
599
603 case LOCALE_IDEFAULTEBCDICCODEPAGE:
606 default_id = lcid_LC_CTYPE;
607 break;
608
610 case LOCALE_ICURRENCY:
612 case LOCALE_INEGCURR:
619 case LOCALE_SCURRENCY:
624 case LOCALE_SNATIVECURRNAME:
625 default_id = lcid_LC_MONETARY;
626 break;
627
628 case LOCALE_IDIGITS:
629 case LOCALE_IDIGITSUBSTITUTION:
630 case LOCALE_ILZERO:
632 case LOCALE_SDECIMAL:
633 case LOCALE_SGROUPING:
634 //case LOCALE_SNAN:
637 //case LOCALE_SNEGINFINITY:
638 //case LOCALE_SPOSINFINITY:
640 case LOCALE_STHOUSAND:
641 default_id = lcid_LC_NUMERIC;
642 break;
643
645 case LOCALE_ICENTURY:
646 case LOCALE_IDATE:
647 case LOCALE_IDAYLZERO:
650 case LOCALE_ILDATE:
651 case LOCALE_IMONLZERO:
653 case LOCALE_ITIME:
655 case LOCALE_ITLZERO:
656 case LOCALE_S1159:
657 case LOCALE_S2359:
678 case LOCALE_SDATE:
679 case LOCALE_SDAYNAME1:
680 case LOCALE_SDAYNAME2:
681 case LOCALE_SDAYNAME3:
682 case LOCALE_SDAYNAME4:
683 case LOCALE_SDAYNAME5:
684 case LOCALE_SDAYNAME6:
685 case LOCALE_SDAYNAME7:
686 //case LOCALE_SDURATION:
687 case LOCALE_SLONGDATE:
702 //case LOCALE_SSHORTESTDAYNAME1:
703 //case LOCALE_SSHORTESTDAYNAME2:
704 //case LOCALE_SSHORTESTDAYNAME3:
705 //case LOCALE_SSHORTESTDAYNAME4:
706 //case LOCALE_SSHORTESTDAYNAME5:
707 //case LOCALE_SSHORTESTDAYNAME6:
708 //case LOCALE_SSHORTESTDAYNAME7:
709 case LOCALE_STIME:
711 case LOCALE_SYEARMONTH:
712 default_id = lcid_LC_TIME;
713 break;
714
715 case LOCALE_IPAPERSIZE:
716 default_id = lcid_LC_PAPER;
717 break;
718
719 case LOCALE_IMEASURE:
720 default_id = lcid_LC_MEASUREMENT;
721 break;
722
723 case LOCALE_ICOUNTRY:
724 default_id = lcid_LC_TELEPHONE;
725 break;
726 }
727 if (default_id) lcid = default_id;
728 }
729 return ConvertDefaultLocale( lcid );
730}
731
732/***********************************************************************
733 * is_genitive_name_supported
734 *
735 * Determine could LCTYPE basically support genitive name form or not.
736 */
738{
739 switch(lctype & 0xffff)
740 {
754 return TRUE;
755 default:
756 return FALSE;
757 }
758}
759
760/***********************************************************************
761 * create_registry_key
762 *
763 * Create the Control Panel\\International registry key.
764 */
765static inline HANDLE create_registry_key(void)
766{
767 static const WCHAR cplW[] = {'C','o','n','t','r','o','l',' ','P','a','n','e','l',0};
768 static const WCHAR intlW[] = {'I','n','t','e','r','n','a','t','i','o','n','a','l',0};
771 HANDLE cpl_key, hkey = 0;
772
773 if (RtlOpenCurrentUser( KEY_ALL_ACCESS, &hkey ) != STATUS_SUCCESS) return 0;
774
775 attr.Length = sizeof(attr);
776 attr.RootDirectory = hkey;
777 attr.ObjectName = &nameW;
778 attr.Attributes = 0;
779 attr.SecurityDescriptor = NULL;
780 attr.SecurityQualityOfService = NULL;
781 RtlInitUnicodeString( &nameW, cplW );
782
783 if (!NtCreateKey( &cpl_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
784 {
785 NtClose( attr.RootDirectory );
786 attr.RootDirectory = cpl_key;
787 RtlInitUnicodeString( &nameW, intlW );
788 if (NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) hkey = 0;
789 }
790 NtClose( attr.RootDirectory );
791 return hkey;
792}
793
794
795#ifndef __REACTOS__
796/* update the registry settings for a given locale parameter */
797/* return TRUE if an update was needed */
799 const LCTYPE *values, UINT nb_values )
800{
801 static const WCHAR formatW[] = { '%','0','8','x',0 };
802 WCHAR bufferW[40];
804 DWORD count, i;
805
807 count = sizeof(bufferW);
809 {
811 LPCWSTR text = (LPCWSTR)info->Data;
812
813 if (strtoulW( text, NULL, 16 ) == lcid) return FALSE; /* already set correctly */
814 TRACE( "updating registry, locale %s changed %s -> %08x\n",
816 }
817 else TRACE( "updating registry, locale %s changed none -> %08x\n", debugstr_w(name), lcid );
818 sprintfW( bufferW, formatW, lcid );
819 NtSetValueKey( hkey, &nameW, 0, REG_SZ, bufferW, (strlenW(bufferW) + 1) * sizeof(WCHAR) );
820
821 for (i = 0; i < nb_values; i++)
822 {
824 sizeof(bufferW)/sizeof(WCHAR) );
825 SetLocaleInfoW( lcid, values[i], bufferW );
826 }
827 return TRUE;
828}
829
830
831/***********************************************************************
832 * LOCALE_InitRegistry
833 *
834 * Update registry contents on startup if the user locale has changed.
835 * This simulates the action of the Windows control panel.
836 */
838{
839 static const WCHAR acpW[] = {'A','C','P',0};
840 static const WCHAR oemcpW[] = {'O','E','M','C','P',0};
841 static const WCHAR maccpW[] = {'M','A','C','C','P',0};
842 static const WCHAR localeW[] = {'L','o','c','a','l','e',0};
843 static const WCHAR lc_ctypeW[] = { 'L','C','_','C','T','Y','P','E',0 };
844 static const WCHAR lc_monetaryW[] = { 'L','C','_','M','O','N','E','T','A','R','Y',0 };
845 static const WCHAR lc_numericW[] = { 'L','C','_','N','U','M','E','R','I','C',0 };
846 static const WCHAR lc_timeW[] = { 'L','C','_','T','I','M','E',0 };
847 static const WCHAR lc_measurementW[] = { 'L','C','_','M','E','A','S','U','R','E','M','E','N','T',0 };
848 static const WCHAR lc_telephoneW[] = { 'L','C','_','T','E','L','E','P','H','O','N','E',0 };
849 static const WCHAR lc_paperW[] = { 'L','C','_','P','A','P','E','R',0};
850 static const struct
851 {
854 } update_cp_values[] = {
856 { oemcpW, LOCALE_IDEFAULTCODEPAGE },
858 };
859 static const LCTYPE lc_messages_values[] = {
862 LOCALE_SLIST };
863 static const LCTYPE lc_monetary_values[] = {
872 static const LCTYPE lc_numeric_values[] = {
876 LOCALE_IDIGITSUBSTITUTION,
882 static const LCTYPE lc_time_values[] = {
896 LOCALE_SYEARMONTH,
897 LOCALE_IDATE };
898 static const LCTYPE lc_measurement_values[] = { LOCALE_IMEASURE };
899 static const LCTYPE lc_telephone_values[] = { LOCALE_ICOUNTRY };
900 static const LCTYPE lc_paper_values[] = { LOCALE_IPAPERSIZE };
901
903 WCHAR bufferW[80];
904 DWORD count, i;
905 HANDLE hkey;
907
908 if (!(hkey = create_registry_key()))
909 return; /* don't do anything if we can't create the registry key */
910
911 locale_update_registry( hkey, localeW, lcid_LC_MESSAGES, lc_messages_values,
912 sizeof(lc_messages_values)/sizeof(lc_messages_values[0]) );
913 locale_update_registry( hkey, lc_monetaryW, lcid_LC_MONETARY, lc_monetary_values,
914 sizeof(lc_monetary_values)/sizeof(lc_monetary_values[0]) );
915 locale_update_registry( hkey, lc_numericW, lcid_LC_NUMERIC, lc_numeric_values,
916 sizeof(lc_numeric_values)/sizeof(lc_numeric_values[0]) );
917 locale_update_registry( hkey, lc_timeW, lcid_LC_TIME, lc_time_values,
918 sizeof(lc_time_values)/sizeof(lc_time_values[0]) );
919 locale_update_registry( hkey, lc_measurementW, lcid_LC_MEASUREMENT, lc_measurement_values,
920 sizeof(lc_measurement_values)/sizeof(lc_measurement_values[0]) );
921 locale_update_registry( hkey, lc_telephoneW, lcid_LC_TELEPHONE, lc_telephone_values,
922 sizeof(lc_telephone_values)/sizeof(lc_telephone_values[0]) );
923 locale_update_registry( hkey, lc_paperW, lcid_LC_PAPER, lc_paper_values,
924 sizeof(lc_paper_values)/sizeof(lc_paper_values[0]) );
925
926 if (locale_update_registry( hkey, lc_ctypeW, lcid_LC_CTYPE, NULL, 0 ))
927 {
928 static const WCHAR codepageW[] =
929 {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
930 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
931 'C','o','n','t','r','o','l','\\','N','l','s','\\','C','o','d','e','p','a','g','e',0};
932
934 HANDLE nls_key;
935 DWORD len = 14;
936
937 RtlInitUnicodeString( &nameW, codepageW );
939 while (codepageW[len])
940 {
941 nameW.Length = len * sizeof(WCHAR);
942 if (NtCreateKey( &nls_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL )) break;
943 NtClose( nls_key );
944 len++;
945 while (codepageW[len] && codepageW[len] != '\\') len++;
946 }
947 nameW.Length = len * sizeof(WCHAR);
948 if (!NtCreateKey( &nls_key, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ))
949 {
950 for (i = 0; i < sizeof(update_cp_values)/sizeof(update_cp_values[0]); i++)
951 {
952 count = GetLocaleInfoW( lcid, update_cp_values[i].value | LOCALE_NOUSEROVERRIDE,
953 bufferW, sizeof(bufferW)/sizeof(WCHAR) );
954 RtlInitUnicodeString( &nameW, update_cp_values[i].name );
955 NtSetValueKey( nls_key, &nameW, 0, REG_SZ, bufferW, count * sizeof(WCHAR) );
956 }
957 NtClose( nls_key );
958 }
959 }
960
961 NtClose( hkey );
962}
963
964
965#ifdef __APPLE__
966/***********************************************************************
967 * get_mac_locale
968 *
969 * Return a locale identifier string reflecting the Mac locale, in a form
970 * that parse_locale_name() will understand. So, strip out unusual
971 * things like script, variant, etc. Or, rather, just construct it as
972 * <lang>[_<country>].UTF-8.
973 */
974static const char* get_mac_locale(void)
975{
976 static char mac_locale[50];
977
978 if (!mac_locale[0])
979 {
980 CFLocaleRef locale = CFLocaleCopyCurrent();
981 CFStringRef lang = CFLocaleGetValue( locale, kCFLocaleLanguageCode );
982 CFStringRef country = CFLocaleGetValue( locale, kCFLocaleCountryCode );
983 CFStringRef locale_string;
984
985 if (country)
986 locale_string = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@_%@"), lang, country);
987 else
988 locale_string = CFStringCreateCopy(NULL, lang);
989
990 CFStringGetCString(locale_string, mac_locale, sizeof(mac_locale), kCFStringEncodingUTF8);
991 strcat(mac_locale, ".UTF-8");
992
993 CFRelease(locale);
994 CFRelease(locale_string);
995 }
996
997 return mac_locale;
998}
999
1000
1001/***********************************************************************
1002 * has_env
1003 */
1004static BOOL has_env(const char* name)
1005{
1006 const char* value = getenv( name );
1007 return value && value[0];
1008}
1009#endif
1010
1011
1012/***********************************************************************
1013 * get_locale
1014 *
1015 * Get the locale identifier for a given category. On most platforms,
1016 * this is just a thin wrapper around setlocale(). On OS X, though, it
1017 * is common for the Mac locale settings to not be supported by the C
1018 * library. So, we sometimes override the result with the Mac locale.
1019 */
1020static const char* get_locale(int category, const char* category_name)
1021{
1022 const char* ret = setlocale(category, NULL);
1023
1024#ifdef __ANDROID__
1025 if (!strcmp(ret, "C"))
1026 {
1027 ret = getenv( category_name );
1028 if (!ret || !ret[0]) ret = getenv( "LC_ALL" );
1029 if (!ret || !ret[0]) ret = "C";
1030 }
1031#endif
1032
1033#ifdef __APPLE__
1034 /* If LC_ALL is set, respect it as a user override.
1035 If LC_* is set, respect it as a user override, except if it's LC_CTYPE
1036 and equal to UTF-8. That's because, when the Mac locale isn't supported
1037 by the C library, Terminal.app sets LC_CTYPE=UTF-8 and doesn't set LANG.
1038 parse_locale_name() doesn't handle that properly, so we override that
1039 with the Mac locale (which uses UTF-8 for the charset, anyway).
1040 Otherwise:
1041 For LC_MESSAGES, we override the C library because the user language
1042 setting is separate from the locale setting on which LANG was based.
1043 If the C library didn't get anything better from LANG than C or POSIX,
1044 override that. That probably means the Mac locale isn't supported by
1045 the C library. */
1046 if (!has_env( "LC_ALL" ) &&
1047 ((category == LC_CTYPE && !strcmp( ret, "UTF-8" )) ||
1048 (!has_env( category_name ) &&
1049 (category == LC_MESSAGES || !strcmp( ret, "C" ) || !strcmp( ret, "POSIX" )))))
1050 {
1051 const char* override = get_mac_locale();
1052
1053 if (category == LC_MESSAGES)
1054 {
1055 /* Retrieve the preferred language as chosen in System Preferences. */
1056 static char messages_locale[50];
1057
1058 if (!messages_locale[0])
1059 {
1060 CFArrayRef preferred_langs = CFLocaleCopyPreferredLanguages();
1061 if (preferred_langs && CFArrayGetCount( preferred_langs ))
1062 {
1063 CFStringRef preferred_lang = CFArrayGetValueAtIndex( preferred_langs, 0 );
1064 CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier( NULL, preferred_lang );
1065 if (components)
1066 {
1067 CFStringRef lang = CFDictionaryGetValue( components, kCFLocaleLanguageCode );
1068 CFStringRef country = CFDictionaryGetValue( components, kCFLocaleCountryCode );
1069 CFLocaleRef locale = NULL;
1070 CFStringRef locale_string;
1071
1072 if (!country)
1073 {
1074 locale = CFLocaleCopyCurrent();
1075 country = CFLocaleGetValue( locale, kCFLocaleCountryCode );
1076 }
1077
1078 if (country)
1079 locale_string = CFStringCreateWithFormat( NULL, NULL, CFSTR("%@_%@"), lang, country );
1080 else
1081 locale_string = CFStringCreateCopy( NULL, lang );
1082 CFStringGetCString( locale_string, messages_locale, sizeof(messages_locale), kCFStringEncodingUTF8 );
1083 strcat( messages_locale, ".UTF-8" );
1084
1085 CFRelease( locale_string );
1086 if (locale) CFRelease( locale );
1087 CFRelease( components );
1088 }
1089 }
1090 if (preferred_langs)
1091 CFRelease( preferred_langs );
1092 }
1093
1094 if (messages_locale[0])
1095 override = messages_locale;
1096 }
1097
1098 TRACE( "%s is %s; overriding with %s\n", category_name, debugstr_a(ret), debugstr_a(override) );
1099 ret = override;
1100 }
1101#endif
1102
1103 return ret;
1104}
1105
1106
1107/***********************************************************************
1108 * setup_unix_locales
1109 */
1111{
1112 struct locale_name locale_name;
1113 WCHAR buffer[128], ctype_buff[128];
1114 const char *locale;
1115 UINT unix_cp = 0;
1116
1117 if ((locale = get_locale( LC_CTYPE, "LC_CTYPE" )))
1118 {
1119 strcpynAtoW( ctype_buff, locale, sizeof(ctype_buff)/sizeof(WCHAR) );
1120 parse_locale_name( ctype_buff, &locale_name );
1122 unix_cp = locale_name.codepage;
1123 }
1124 if (!lcid_LC_CTYPE) /* this one needs a default value */
1126
1127 TRACE( "got lcid %04x (%d matches) for LC_CTYPE=%s\n",
1129
1130#define GET_UNIX_LOCALE(cat) do \
1131 if ((locale = get_locale( cat, #cat ))) \
1132 { \
1133 strcpynAtoW( buffer, locale, sizeof(buffer)/sizeof(WCHAR) ); \
1134 if (!strcmpW( buffer, ctype_buff )) lcid_##cat = lcid_LC_CTYPE; \
1135 else { \
1136 parse_locale_name( buffer, &locale_name ); \
1137 lcid_##cat = locale_name.lcid; \
1138 TRACE( "got lcid %04x (%d matches) for " #cat "=%s\n", \
1139 locale_name.lcid, locale_name.matches, debugstr_a(locale) ); \
1140 } \
1141 } while (0)
1142
1144 GET_UNIX_LOCALE( LC_MESSAGES );
1148#ifdef LC_PAPER
1149 GET_UNIX_LOCALE( LC_PAPER );
1150#endif
1151#ifdef LC_MEASUREMENT
1152 GET_UNIX_LOCALE( LC_MEASUREMENT );
1153#endif
1154#ifdef LC_TELEPHONE
1155 GET_UNIX_LOCALE( LC_TELEPHONE );
1156#endif
1157
1158#undef GET_UNIX_LOCALE
1159
1160 return unix_cp;
1161}
1162#endif // !__REACTOS__
1163
1164
1165/***********************************************************************
1166 * GetUserDefaultLangID (KERNEL32.@)
1167 *
1168 * Get the default language Id for the current user.
1169 *
1170 * PARAMS
1171 * None.
1172 *
1173 * RETURNS
1174 * The current LANGID of the default language for the current user.
1175 */
1177{
1179}
1180
1181
1182/***********************************************************************
1183 * GetSystemDefaultLangID (KERNEL32.@)
1184 *
1185 * Get the default language Id for the system.
1186 *
1187 * PARAMS
1188 * None.
1189 *
1190 * RETURNS
1191 * The current LANGID of the default language for the system.
1192 */
1194{
1196}
1197
1198
1199/***********************************************************************
1200 * GetUserDefaultLCID (KERNEL32.@)
1201 *
1202 * Get the default locale Id for the current user.
1203 *
1204 * PARAMS
1205 * None.
1206 *
1207 * RETURNS
1208 * The current LCID of the default locale for the current user.
1209 */
1211{
1212 LCID lcid;
1214 return lcid;
1215}
1216
1217
1218/***********************************************************************
1219 * GetSystemDefaultLCID (KERNEL32.@)
1220 *
1221 * Get the default locale Id for the system.
1222 *
1223 * PARAMS
1224 * None.
1225 *
1226 * RETURNS
1227 * The current LCID of the default locale for the system.
1228 */
1230{
1231 LCID lcid;
1233 return lcid;
1234}
1235
1236#ifndef __REACTOS__
1237/***********************************************************************
1238 * GetSystemDefaultLocaleName (KERNEL32.@)
1239 */
1241{
1243 return LCIDToLocaleName(lcid, localename, len, 0);
1244}
1245
1247{
1248 LCTYPE type;
1249 int lsize;
1250
1251 FIXME("(0x%x %p %p %p) returning a dummy value (current locale)\n", flags, count, buffer, size);
1252
1253 if (flags & MUI_LANGUAGE_ID)
1255 else
1257
1259 if (!lsize)
1260 {
1261 /* keep last error from callee */
1262 return FALSE;
1263 }
1264 lsize++;
1265 if (!*size)
1266 {
1267 *size = lsize;
1268 *count = 1;
1269 return TRUE;
1270 }
1271
1272 if (lsize > *size)
1273 {
1275 return FALSE;
1276 }
1277
1279 {
1280 /* keep last error from callee */
1281 return FALSE;
1282 }
1283
1284 buffer[lsize-1] = 0;
1285 *size = lsize;
1286 *count = 1;
1287 TRACE("returned variable content: %d, \"%s\", %d\n", *count, debugstr_w(buffer), *size);
1288 return TRUE;
1289
1290}
1291
1292/***********************************************************************
1293 * GetSystemPreferredUILanguages (KERNEL32.@)
1294 */
1296{
1297 if (flags & ~(MUI_LANGUAGE_NAME | MUI_LANGUAGE_ID | MUI_MACHINE_LANGUAGE_SETTINGS))
1298 {
1300 return FALSE;
1301 }
1302 if ((flags & MUI_LANGUAGE_NAME) && (flags & MUI_LANGUAGE_ID))
1303 {
1305 return FALSE;
1306 }
1307 if (*size && !buffer)
1308 {
1310 return FALSE;
1311 }
1312
1314}
1315
1316/***********************************************************************
1317 * SetThreadPreferredUILanguages (KERNEL32.@)
1318 */
1320{
1321 FIXME( "%u, %p, %p\n", flags, buffer, count );
1322 return TRUE;
1323}
1324
1325/***********************************************************************
1326 * GetThreadPreferredUILanguages (KERNEL32.@)
1327 */
1329{
1330 FIXME( "%08x, %p, %p %p\n", flags, count, buf, size );
1332}
1333
1334#if (WINVER >= 0x0600)
1335/******************************************************************************
1336 * GetUserPreferredUILanguages (KERNEL32.@)
1337 */
1339{
1340 TRACE( "%u %p %p %p\n", flags, count, buffer, size );
1341
1342 if (flags & ~(MUI_LANGUAGE_NAME | MUI_LANGUAGE_ID))
1343 {
1345 return FALSE;
1346 }
1347 if ((flags & MUI_LANGUAGE_NAME) && (flags & MUI_LANGUAGE_ID))
1348 {
1350 return FALSE;
1351 }
1352 if (*size && !buffer)
1353 {
1355 return FALSE;
1356 }
1357
1359}
1360#endif // (WINVER >= 0x0600)
1361#endif // !__REACTOS__
1362
1363/***********************************************************************
1364 * GetUserDefaultUILanguage (KERNEL32.@)
1365 *
1366 * Get the default user interface language Id for the current user.
1367 *
1368 * PARAMS
1369 * None.
1370 *
1371 * RETURNS
1372 * The current LANGID of the default UI language for the current user.
1373 */
1375{
1376 LANGID lang;
1378 return lang;
1379}
1380
1381
1382/***********************************************************************
1383 * GetSystemDefaultUILanguage (KERNEL32.@)
1384 *
1385 * Get the default user interface language Id for the system.
1386 *
1387 * PARAMS
1388 * None.
1389 *
1390 * RETURNS
1391 * The current LANGID of the default UI language for the system. This is
1392 * typically the same language used during the installation process.
1393 */
1395{
1396 LANGID lang;
1398 return lang;
1399}
1400
1401#if (WINVER >= 0x0600)
1402/***********************************************************************
1403 * LocaleNameToLCID (KERNEL32.@)
1404 */
1405LCID WINAPI LocaleNameToLCID( LPCWSTR name, DWORD flags )
1406{
1407 struct locale_name locale_name;
1408
1409 if (flags) FIXME( "unsupported flags %x\n", flags );
1410
1412 return GetUserDefaultLCID();
1413
1414 /* string parsing */
1415 parse_locale_name( name, &locale_name );
1416
1417 TRACE( "found lcid %x for %s, matches %d\n",
1419
1420 if (!locale_name.matches)
1421 {
1423 return 0;
1424 }
1425
1426 if (locale_name.matches == 1)
1427 WARN( "locale %s not recognized, defaulting to %s\n",
1429
1430 return locale_name.lcid;
1431}
1432
1433
1434/***********************************************************************
1435 * LCIDToLocaleName (KERNEL32.@)
1436 */
1437INT WINAPI LCIDToLocaleName( LCID lcid, LPWSTR name, INT count, DWORD flags )
1438{
1439 if (flags) FIXME( "unsupported flags %x\n", flags );
1440
1442}
1443#endif
1444
1445
1446/******************************************************************************
1447 * get_locale_registry_value
1448 *
1449 * Gets the registry value name and cache for a given lctype.
1450 */
1452{
1453 int i;
1454 for (i=0; i < sizeof(registry_values)/sizeof(registry_values[0]); i++)
1456 return &registry_values[i];
1457 return NULL;
1458}
1459
1460
1461/******************************************************************************
1462 * get_registry_locale_info
1463 *
1464 * Retrieve user-modified locale info from the registry.
1465 * Return length, 0 on error, -1 if not found.
1466 */
1468{
1469 DWORD size;
1470 INT ret;
1471 HANDLE hkey;
1475 static const int info_size = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data);
1476
1478
1480 {
1481 if (!(hkey = create_registry_key()))
1482 {
1484 return -1;
1485 }
1486
1488 size = info_size + len * sizeof(WCHAR);
1489
1490 if (!(info = HeapAlloc( GetProcessHeap(), 0, size )))
1491 {
1492 NtClose( hkey );
1495 return 0;
1496 }
1497
1499
1500 /* try again with a bigger buffer when we have to return the correct size */
1501 if (status == STATUS_BUFFER_OVERFLOW && !buffer && size > info_size)
1502 {
1504 if ((new_info = HeapReAlloc( GetProcessHeap(), 0, info, size )))
1505 {
1506 info = new_info;
1508 }
1509 }
1510
1511 NtClose( hkey );
1512
1513 if (!status)
1514 {
1515 INT length = (size - info_size) / sizeof(WCHAR);
1517
1518 if (!length || ((WCHAR *)&info->Data)[length-1])
1519 length++;
1520
1521 cached_value = HeapAlloc( GetProcessHeap(), 0, length * sizeof(WCHAR) );
1522
1523 if (!cached_value)
1524 {
1525 HeapFree( GetProcessHeap(), 0, info );
1528 return 0;
1529 }
1530
1531 memcpy( cached_value, info->Data, (length-1) * sizeof(WCHAR) );
1532 cached_value[length-1] = 0;
1533 HeapFree( GetProcessHeap(), 0, info );
1535 }
1536 else
1537 {
1539 {
1540 ret = (size - info_size) / sizeof(WCHAR);
1541 }
1543 {
1544 ret = -1;
1545 }
1546 else
1547 {
1549 ret = 0;
1550 }
1551 HeapFree( GetProcessHeap(), 0, info );
1553 return ret;
1554 }
1555 }
1556
1558
1559 if (buffer)
1560 {
1561 if (ret > len)
1562 {
1564 ret = 0;
1565 }
1566 else
1567 {
1569 }
1570 }
1571
1573
1574 return ret;
1575}
1576
1577
1578/******************************************************************************
1579 * GetLocaleInfoA (KERNEL32.@)
1580 *
1581 * Get information about an aspect of a locale.
1582 *
1583 * PARAMS
1584 * lcid [I] LCID of the locale
1585 * lctype [I] LCTYPE_ flags from "winnls.h"
1586 * buffer [O] Destination for the information
1587 * len [I] Length of buffer in characters
1588 *
1589 * RETURNS
1590 * Success: The size of the data requested. If buffer is non-NULL, it is filled
1591 * with the information.
1592 * Failure: 0. Use GetLastError() to determine the cause.
1593 *
1594 * NOTES
1595 * - LOCALE_NEUTRAL is equal to LOCALE_SYSTEM_DEFAULT
1596 * - The string returned is NUL terminated, except for LOCALE_FONTSIGNATURE,
1597 * which is a bit string.
1598 */
1600{
1601 WCHAR *bufferW;
1602 INT lenW, ret;
1603
1604 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1605
1606 if (len < 0 || (len && !buffer))
1607 {
1609 return 0;
1610 }
1613 {
1615 return 0;
1616 }
1617
1618 if (!len) buffer = NULL;
1619
1620 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
1621
1622 if (!(bufferW = HeapAlloc( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
1623 {
1625 return 0;
1626 }
1627 if ((ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW )))
1628 {
1629 if ((lctype & LOCALE_RETURN_NUMBER) ||
1631 {
1632 /* it's not an ASCII string, just bytes */
1633 ret *= sizeof(WCHAR);
1634 if (buffer)
1635 {
1636 if (ret <= len) memcpy( buffer, bufferW, ret );
1637 else
1638 {
1640 ret = 0;
1641 }
1642 }
1643 }
1644 else
1645 {
1648 ret = WideCharToMultiByte( codepage, 0, bufferW, ret, buffer, len, NULL, NULL );
1649 }
1650 }
1651 HeapFree( GetProcessHeap(), 0, bufferW );
1652 return ret;
1653}
1654
1656{
1657 return lctype == LOCALE_ILANGUAGE || lctype == LOCALE_IDEFAULTLANGUAGE ? 16 : 10;
1658}
1659
1660/******************************************************************************
1661 * GetLocaleInfoW (KERNEL32.@)
1662 *
1663 * See GetLocaleInfoA.
1664 */
1666{
1667 LANGID lang_id;
1668 HRSRC hrsrc;
1669 HGLOBAL hmem;
1670 INT ret;
1671 UINT lcflags;
1672 const WCHAR *p;
1673 unsigned int i;
1674
1675 if (len < 0 || (len && !buffer))
1676 {
1678 return 0;
1679 }
1682 {
1684 return 0;
1685 }
1686
1687 if (!len) buffer = NULL;
1688
1689 lcid = convert_default_lcid( lcid, lctype );
1690
1692 lctype &= 0xffff;
1693
1694 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
1695
1696 /* first check for overrides in the registry */
1697
1698 if (!(lcflags & LOCALE_NOUSEROVERRIDE) &&
1700 {
1702
1703 if (value)
1704 {
1705 if (lcflags & LOCALE_RETURN_NUMBER)
1706 {
1707 WCHAR tmp[16];
1708 ret = get_registry_locale_info( value, tmp, sizeof(tmp)/sizeof(WCHAR) );
1709 if (ret > 0)
1710 {
1711 WCHAR *end;
1713 if (*end) /* invalid number */
1714 {
1716 return 0;
1717 }
1718 ret = sizeof(UINT)/sizeof(WCHAR);
1719 if (!buffer) return ret;
1720 if (ret > len)
1721 {
1723 return 0;
1724 }
1725 memcpy( buffer, &number, sizeof(number) );
1726 }
1727 }
1729
1730 if (ret != -1) return ret;
1731 }
1732 }
1733
1734 /* now load it from kernel resources */
1735
1736 lang_id = LANGIDFROMLCID( lcid );
1737
1738 /* replace SUBLANG_NEUTRAL by SUBLANG_DEFAULT */
1739 if (SUBLANGID(lang_id) == SUBLANG_NEUTRAL) lang_id = get_default_sublang( lang_id );
1740
1742 ULongToPtr((lctype >> 4) + 1), lang_id )))
1743 {
1744 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
1745 return 0;
1746 }
1747 if (!(hmem = LoadResource( kernel32_handle, hrsrc )))
1748 return 0;
1749
1750 p = LockResource( hmem );
1751 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
1752
1753 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT)/sizeof(WCHAR);
1754 else if (is_genitive_name_supported( lctype ) && *p)
1755 {
1756 /* genitive form's stored after a null separator from a nominative */
1757 for (i = 1; i <= *p; i++) if (!p[i]) break;
1758
1759 if (i <= *p && (lcflags & LOCALE_RETURN_GENITIVE_NAMES))
1760 {
1761 ret = *p - i + 1;
1762 p += i;
1763 }
1764 else ret = i;
1765 }
1766 else
1767 ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
1768
1769 if (!buffer) return ret;
1770
1771 if (ret > len)
1772 {
1774 return 0;
1775 }
1776
1777 if (lcflags & LOCALE_RETURN_NUMBER)
1778 {
1779 UINT number;
1780 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
1781 if (!tmp) return 0;
1782 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
1783 tmp[*p] = 0;
1785 if (!*end)
1786 memcpy( buffer, &number, sizeof(number) );
1787 else /* invalid number */
1788 {
1790 ret = 0;
1791 }
1792 HeapFree( GetProcessHeap(), 0, tmp );
1793
1794 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
1795 lcid, lctype, buffer, len, number );
1796 }
1797 else
1798 {
1799 memcpy( buffer, p + 1, ret * sizeof(WCHAR) );
1800 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
1801
1802 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
1803 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
1804 }
1805 return ret;
1806}
1807
1808#if (WINVER >= 0x0600)
1809/******************************************************************************
1810 * GetLocaleInfoEx (KERNEL32.@)
1811 */
1812INT WINAPI GetLocaleInfoEx(LPCWSTR locale, LCTYPE info, LPWSTR buffer, INT len)
1813{
1814 LCID lcid = LocaleNameToLCID(locale, 0);
1815
1816 TRACE("%s, lcid=0x%x, 0x%x\n", debugstr_w(locale), lcid, info);
1817
1818 if (!lcid) return 0;
1819
1820 /* special handling for neutral locale names */
1821 if (locale && strlenW(locale) == 2)
1822 {
1823 switch (info)
1824 {
1825 case LOCALE_SNAME:
1826 if (len && len < 3)
1827 {
1829 return 0;
1830 }
1831 if (len) strcpyW(buffer, locale);
1832 return 3;
1833 case LOCALE_SPARENT:
1834 if (len) buffer[0] = 0;
1835 return 1;
1836 }
1837 }
1838
1839 return GetLocaleInfoW(lcid, info, buffer, len);
1840}
1841
1842BOOL
1843WINAPI
1845 LPCWSTR lpLocaleName
1846)
1847{
1848 TRACE( "IsValidLocaleName not implemented (lpLocaleName=%s)\n", debugstr_w(lpLocaleName));
1849 return TRUE;
1850}
1851
1852INT
1853WINAPI
1855 LPWSTR lpLocaleName,
1856 INT cchLocaleName
1857)
1858{
1859 TRACE( "GetUserDefaultLocaleName not implemented (lpLocaleName=%s, cchLocaleName=%d)\n", debugstr_w(lpLocaleName), cchLocaleName);
1860 return 0;
1861}
1862#endif
1863
1864/******************************************************************************
1865 * SetLocaleInfoA [KERNEL32.@]
1866 *
1867 * Set information about an aspect of a locale.
1868 *
1869 * PARAMS
1870 * lcid [I] LCID of the locale
1871 * lctype [I] LCTYPE_ flags from "winnls.h"
1872 * data [I] Information to set
1873 *
1874 * RETURNS
1875 * Success: TRUE. The information given will be returned by GetLocaleInfoA()
1876 * whenever it is called without LOCALE_NOUSEROVERRIDE.
1877 * Failure: FALSE. Use GetLastError() to determine the cause.
1878 *
1879 * NOTES
1880 * - Values are only be set for the current user locale; the system locale
1881 * settings cannot be changed.
1882 * - Any settings changed by this call are lost when the locale is changed by
1883 * the control panel (in Wine, this happens every time you change LANG).
1884 * - The native implementation of this function does not check that lcid matches
1885 * the current user locale, and simply sets the new values. Wine warns you in
1886 * this case, but behaves the same.
1887 */
1889{
1891 WCHAR *strW;
1892 DWORD len;
1893 BOOL ret;
1894
1896
1897 if (!data)
1898 {
1900 return FALSE;
1901 }
1902 len = MultiByteToWideChar( codepage, 0, data, -1, NULL, 0 );
1903 if (!(strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
1904 {
1906 return FALSE;
1907 }
1909 ret = SetLocaleInfoW( lcid, lctype, strW );
1910 HeapFree( GetProcessHeap(), 0, strW );
1911 return ret;
1912}
1913
1914
1915/******************************************************************************
1916 * SetLocaleInfoW (KERNEL32.@)
1917 *
1918 * See SetLocaleInfoA.
1919 */
1921{
1922 struct registry_value *value;
1923 static const WCHAR intlW[] = {'i','n','t','l',0 };
1926 HANDLE hkey;
1927
1928 lctype &= 0xffff;
1930
1931 if (!data || !value)
1932 {
1934 return FALSE;
1935 }
1936
1938 {
1940 return FALSE;
1941 }
1942
1943 TRACE("setting %x (%s) to %s\n", lctype, debugstr_w(value->name), debugstr_w(data) );
1944
1945 /* FIXME: should check that data to set is sane */
1946
1947 /* FIXME: profile functions should map to registry */
1948 WriteProfileStringW( intlW, value->name, data );
1949
1950 if (!(hkey = create_registry_key())) return FALSE;
1952 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, (PVOID)data, (strlenW(data)+1)*sizeof(WCHAR) );
1953
1955 HeapFree( GetProcessHeap(), 0, value->cached_value );
1956 value->cached_value = NULL;
1958
1960 {
1961 /* Set I-value from S value */
1962 WCHAR *lpD, *lpM, *lpY;
1963 WCHAR szBuff[2];
1964
1965 lpD = strrchrW(data, 'd');
1966 lpM = strrchrW(data, 'M');
1967 lpY = strrchrW(data, 'y');
1968
1969 if (lpD <= lpM)
1970 {
1971 szBuff[0] = '1'; /* D-M-Y */
1972 }
1973 else
1974 {
1975 if (lpY <= lpM)
1976 szBuff[0] = '2'; /* Y-M-D */
1977 else
1978 szBuff[0] = '0'; /* M-D-Y */
1979 }
1980
1981 szBuff[1] = '\0';
1982
1985 else
1987
1989
1990 WriteProfileStringW( intlW, value->name, szBuff );
1991
1993 status = NtSetValueKey( hkey, &valueW, 0, REG_SZ, szBuff, sizeof(szBuff) );
1994
1996 HeapFree( GetProcessHeap(), 0, value->cached_value );
1997 value->cached_value = NULL;
1999 }
2000
2001 NtClose( hkey );
2002
2004 return !status;
2005}
2006
2007
2008#ifndef __REACTOS__
2009/******************************************************************************
2010 * GetACP (KERNEL32.@)
2011 *
2012 * Get the current Ansi code page Id for the system.
2013 *
2014 * PARAMS
2015 * None.
2016 *
2017 * RETURNS
2018 * The current Ansi code page identifier for the system.
2019 */
2021{
2023 return ansi_cptable->info.codepage;
2024}
2025
2026
2027/******************************************************************************
2028 * SetCPGlobal (KERNEL32.@)
2029 *
2030 * Set the current Ansi code page Id for the system.
2031 *
2032 * PARAMS
2033 * acp [I] code page ID to be the new ACP.
2034 *
2035 * RETURNS
2036 * The previous ACP.
2037 */
2039{
2040 UINT ret = GetACP();
2041 const union cptable *new_cptable = wine_cp_get_table( acp );
2042
2043 if (new_cptable) ansi_cptable = new_cptable;
2044 return ret;
2045}
2046
2047
2048/***********************************************************************
2049 * GetOEMCP (KERNEL32.@)
2050 *
2051 * Get the current OEM code page Id for the system.
2052 *
2053 * PARAMS
2054 * None.
2055 *
2056 * RETURNS
2057 * The current OEM code page identifier for the system.
2058 */
2060{
2062 return oem_cptable->info.codepage;
2063}
2064
2065
2066/***********************************************************************
2067 * IsValidCodePage (KERNEL32.@)
2068 *
2069 * Determine if a given code page identifier is valid.
2070 *
2071 * PARAMS
2072 * codepage [I] Code page Id to verify.
2073 *
2074 * RETURNS
2075 * TRUE, If codepage is valid and available on the system,
2076 * FALSE otherwise.
2077 */
2079{
2080 switch(codepage) {
2081 case CP_UTF7:
2082 case CP_UTF8:
2083 return TRUE;
2084 default:
2085 return wine_cp_get_table( codepage ) != NULL;
2086 }
2087}
2088
2089
2090/***********************************************************************
2091 * IsDBCSLeadByteEx (KERNEL32.@)
2092 *
2093 * Determine if a character is a lead byte in a given code page.
2094 *
2095 * PARAMS
2096 * codepage [I] Code page for the test.
2097 * testchar [I] Character to test
2098 *
2099 * RETURNS
2100 * TRUE, if testchar is a lead byte in codepage,
2101 * FALSE otherwise.
2102 */
2104{
2105 const union cptable *table = get_codepage_table( codepage );
2106 return table && wine_is_dbcs_leadbyte( table, testchar );
2107}
2108
2109
2110/***********************************************************************
2111 * IsDBCSLeadByte (KERNEL32.@)
2112 * IsDBCSLeadByte (KERNEL.207)
2113 *
2114 * Determine if a character is a lead byte.
2115 *
2116 * PARAMS
2117 * testchar [I] Character to test
2118 *
2119 * RETURNS
2120 * TRUE, if testchar is a lead byte in the ANSI code page,
2121 * FALSE otherwise.
2122 */
2124{
2125 if (!ansi_cptable) return FALSE;
2126 return wine_is_dbcs_leadbyte( ansi_cptable, testchar );
2127}
2128
2129
2130/***********************************************************************
2131 * GetCPInfo (KERNEL32.@)
2132 *
2133 * Get information about a code page.
2134 *
2135 * PARAMS
2136 * codepage [I] Code page number
2137 * cpinfo [O] Destination for code page information
2138 *
2139 * RETURNS
2140 * Success: TRUE. cpinfo is updated with the information about codepage.
2141 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
2142 */
2144{
2145 const union cptable *table;
2146
2147 if (!cpinfo)
2148 {
2150 return FALSE;
2151 }
2152
2153 if (!(table = get_codepage_table( codepage )))
2154 {
2155 switch(codepage)
2156 {
2157 case CP_UTF7:
2158 case CP_UTF8:
2159 cpinfo->DefaultChar[0] = 0x3f;
2160 cpinfo->DefaultChar[1] = 0;
2161 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
2162 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
2163 return TRUE;
2164 }
2165
2167 return FALSE;
2168 }
2169 if (table->info.def_char & 0xff00)
2170 {
2171 cpinfo->DefaultChar[0] = (table->info.def_char & 0xff00) >> 8;
2172 cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
2173 }
2174 else
2175 {
2176 cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
2177 cpinfo->DefaultChar[1] = 0;
2178 }
2179 if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
2180 memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
2181 else
2182 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
2183
2184 return TRUE;
2185}
2186
2187/***********************************************************************
2188 * GetCPInfoExA (KERNEL32.@)
2189 *
2190 * Get extended information about a code page.
2191 *
2192 * PARAMS
2193 * codepage [I] Code page number
2194 * dwFlags [I] Reserved, must to 0.
2195 * cpinfo [O] Destination for code page information
2196 *
2197 * RETURNS
2198 * Success: TRUE. cpinfo is updated with the information about codepage.
2199 * Failure: FALSE, if codepage is invalid or cpinfo is NULL.
2200 */
2202{
2203 CPINFOEXW cpinfoW;
2204
2205 if (!GetCPInfoExW( codepage, dwFlags, &cpinfoW ))
2206 return FALSE;
2207
2208 /* the layout is the same except for CodePageName */
2209 memcpy(cpinfo, &cpinfoW, sizeof(CPINFOEXA));
2210 WideCharToMultiByte(CP_ACP, 0, cpinfoW.CodePageName, -1, cpinfo->CodePageName, sizeof(cpinfo->CodePageName), NULL, NULL);
2211 return TRUE;
2212}
2213
2214/***********************************************************************
2215 * GetCPInfoExW (KERNEL32.@)
2216 *
2217 * Unicode version of GetCPInfoExA.
2218 */
2220{
2221 if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
2222 return FALSE;
2223
2224 switch(codepage)
2225 {
2226 case CP_UTF7:
2227 {
2228 static const WCHAR utf7[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','7',')',0};
2229
2230 cpinfo->CodePage = CP_UTF7;
2231 cpinfo->UnicodeDefaultChar = 0x3f;
2232 strcpyW(cpinfo->CodePageName, utf7);
2233 break;
2234 }
2235
2236 case CP_UTF8:
2237 {
2238 static const WCHAR utf8[] = {'U','n','i','c','o','d','e',' ','(','U','T','F','-','8',')',0};
2239
2240 cpinfo->CodePage = CP_UTF8;
2241 cpinfo->UnicodeDefaultChar = 0x3f;
2242 strcpyW(cpinfo->CodePageName, utf8);
2243 break;
2244 }
2245
2246 default:
2247 {
2248 const union cptable *table = get_codepage_table( codepage );
2249
2250 cpinfo->CodePage = table->info.codepage;
2251 cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
2252 MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
2253 sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
2254 break;
2255 }
2256 }
2257 return TRUE;
2258}
2259
2260/***********************************************************************
2261 * EnumSystemCodePagesA (KERNEL32.@)
2262 *
2263 * Call a user defined function for every code page installed on the system.
2264 *
2265 * PARAMS
2266 * lpfnCodePageEnum [I] User CODEPAGE_ENUMPROC to call with each found code page
2267 * flags [I] Reserved, set to 0.
2268 *
2269 * RETURNS
2270 * TRUE, If all code pages have been enumerated, or
2271 * FALSE if lpfnCodePageEnum returned FALSE to stop the enumeration.
2272 */
2274{
2275 const union cptable *table;
2276 char buffer[10];
2277 int index = 0;
2278
2279 for (;;)
2280 {
2281 if (!(table = wine_cp_enum_table( index++ ))) break;
2282 sprintf( buffer, "%d", table->info.codepage );
2283 if (!lpfnCodePageEnum( buffer )) break;
2284 }
2285 return TRUE;
2286}
2287
2288
2289/***********************************************************************
2290 * EnumSystemCodePagesW (KERNEL32.@)
2291 *
2292 * See EnumSystemCodePagesA.
2293 */
2295{
2296 const union cptable *table;
2297 WCHAR buffer[10], *p;
2298 int page, index = 0;
2299
2300 for (;;)
2301 {
2302 if (!(table = wine_cp_enum_table( index++ ))) break;
2303 p = buffer + sizeof(buffer)/sizeof(WCHAR);
2304 *--p = 0;
2305 page = table->info.codepage;
2306 do
2307 {
2308 *--p = '0' + (page % 10);
2309 page /= 10;
2310 } while( page );
2311 if (!lpfnCodePageEnum( p )) break;
2312 }
2313 return TRUE;
2314}
2315
2316
2317/***********************************************************************
2318 * utf7_write_w
2319 *
2320 * Helper for utf7_mbstowcs
2321 *
2322 * RETURNS
2323 * TRUE on success, FALSE on error
2324 */
2325static inline BOOL utf7_write_w(WCHAR *dst, int dstlen, int *index, WCHAR character)
2326{
2327 if (dstlen > 0)
2328 {
2329 if (*index >= dstlen)
2330 return FALSE;
2331
2332 dst[*index] = character;
2333 }
2334
2335 (*index)++;
2336
2337 return TRUE;
2338}
2339
2340/***********************************************************************
2341 * utf7_mbstowcs
2342 *
2343 * UTF-7 to UTF-16 string conversion, helper for MultiByteToWideChar
2344 *
2345 * RETURNS
2346 * On success, the number of characters written
2347 * On dst buffer overflow, -1
2348 */
2349static int utf7_mbstowcs(const char *src, int srclen, WCHAR *dst, int dstlen)
2350{
2351 static const signed char base64_decoding_table[] =
2352 {
2353 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
2354 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
2355 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20-0x2F */
2356 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
2357 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4F */
2358 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50-0x5F */
2359 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6F */
2360 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70-0x7F */
2361 };
2362
2363 const char *source_end = src + srclen;
2364 int dest_index = 0;
2365
2366 DWORD byte_pair = 0;
2367 short offset = 0;
2368
2369 while (src < source_end)
2370 {
2371 if (*src == '+')
2372 {
2373 src++;
2374 if (src >= source_end)
2375 break;
2376
2377 if (*src == '-')
2378 {
2379 /* just a plus sign escaped as +- */
2380 if (!utf7_write_w(dst, dstlen, &dest_index, '+'))
2381 return -1;
2382 src++;
2383 continue;
2384 }
2385
2386 do
2387 {
2388 signed char sextet = *src;
2389 if (sextet == '-')
2390 {
2391 /* skip over the dash and end base64 decoding
2392 * the current, unfinished byte pair is discarded */
2393 src++;
2394 offset = 0;
2395 break;
2396 }
2397 if (sextet < 0)
2398 {
2399 /* the next character of src is < 0 and therefore not part of a base64 sequence
2400 * the current, unfinished byte pair is NOT discarded in this case
2401 * this is probably a bug in Windows */
2402 break;
2403 }
2404
2405 sextet = base64_decoding_table[sextet];
2406 if (sextet == -1)
2407 {
2408 /* -1 means that the next character of src is not part of a base64 sequence
2409 * in other words, all sextets in this base64 sequence have been processed
2410 * the current, unfinished byte pair is discarded */
2411 offset = 0;
2412 break;
2413 }
2414
2415 byte_pair = (byte_pair << 6) | sextet;
2416 offset += 6;
2417
2418 if (offset >= 16)
2419 {
2420 /* this byte pair is done */
2421 if (!utf7_write_w(dst, dstlen, &dest_index, (byte_pair >> (offset - 16)) & 0xFFFF))
2422 return -1;
2423 offset -= 16;
2424 }
2425
2426 src++;
2427 }
2428 while (src < source_end);
2429 }
2430 else
2431 {
2432 /* we have to convert to unsigned char in case *src < 0 */
2433 if (!utf7_write_w(dst, dstlen, &dest_index, (unsigned char)*src))
2434 return -1;
2435 src++;
2436 }
2437 }
2438
2439 return dest_index;
2440}
2441
2442/***********************************************************************
2443 * MultiByteToWideChar (KERNEL32.@)
2444 *
2445 * Convert a multibyte character string into a Unicode string.
2446 *
2447 * PARAMS
2448 * page [I] Codepage character set to convert from
2449 * flags [I] Character mapping flags
2450 * src [I] Source string buffer
2451 * srclen [I] Length of src (in bytes), or -1 if src is NUL terminated
2452 * dst [O] Destination buffer
2453 * dstlen [I] Length of dst (in WCHARs), or 0 to compute the required length
2454 *
2455 * RETURNS
2456 * Success: If dstlen > 0, the number of characters written to dst.
2457 * If dstlen == 0, the number of characters needed to perform the
2458 * conversion. In both cases the count includes the terminating NUL.
2459 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
2460 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
2461 * and dstlen != 0; ERROR_INVALID_PARAMETER, if an invalid parameter
2462 * is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
2463 * possible for src.
2464 */
2466 LPWSTR dst, INT dstlen )
2467{
2468 const union cptable *table;
2469 int ret;
2470
2471 if (!src || !srclen || (!dst && dstlen) || dstlen < 0)
2472 {
2474 return 0;
2475 }
2476
2477 if (srclen < 0) srclen = strlen(src) + 1;
2478
2479 switch(page)
2480 {
2481 case CP_SYMBOL:
2482 if (flags)
2483 {
2485 return 0;
2486 }
2488 break;
2489 case CP_UTF7:
2490 if (flags)
2491 {
2493 return 0;
2494 }
2496 break;
2497 case CP_UNIXCP:
2498 if (unix_cptable)
2499 {
2501 break;
2502 }
2503#ifdef __APPLE__
2504 flags |= MB_COMPOSITE; /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
2505#endif
2506 /* fall through */
2507 case CP_UTF8:
2508 if (flags & ~MB_FLAGSMASK)
2509 {
2511 return 0;
2512 }
2514 break;
2515 default:
2516 if (!(table = get_codepage_table( page )))
2517 {
2519 return 0;
2520 }
2521 if (flags & ~MB_FLAGSMASK)
2522 {
2524 return 0;
2525 }
2527 break;
2528 }
2529
2530 if (ret < 0)
2531 {
2532 switch(ret)
2533 {
2534 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
2535 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
2536 }
2537 ret = 0;
2538 }
2539 TRACE("cp %d %s -> %s, ret = %d\n",
2541 return ret;
2542}
2543
2544
2545/***********************************************************************
2546 * utf7_can_directly_encode
2547 *
2548 * Helper for utf7_wcstombs
2549 */
2550static inline BOOL utf7_can_directly_encode(WCHAR codepoint)
2551{
2552 static const BOOL directly_encodable_table[] =
2553 {
2554 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0x00 - 0x0F */
2555 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1F */
2556 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* 0x20 - 0x2F */
2557 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x30 - 0x3F */
2558 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4F */
2559 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50 - 0x5F */
2560 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6F */
2561 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* 0x70 - 0x7A */
2562 };
2563
2564 return codepoint <= 0x7A ? directly_encodable_table[codepoint] : FALSE;
2565}
2566
2567/***********************************************************************
2568 * utf7_write_c
2569 *
2570 * Helper for utf7_wcstombs
2571 *
2572 * RETURNS
2573 * TRUE on success, FALSE on error
2574 */
2575static inline BOOL utf7_write_c(char *dst, int dstlen, int *index, char character)
2576{
2577 if (dstlen > 0)
2578 {
2579 if (*index >= dstlen)
2580 return FALSE;
2581
2582 dst[*index] = character;
2583 }
2584
2585 (*index)++;
2586
2587 return TRUE;
2588}
2589
2590/***********************************************************************
2591 * utf7_wcstombs
2592 *
2593 * UTF-16 to UTF-7 string conversion, helper for WideCharToMultiByte
2594 *
2595 * RETURNS
2596 * On success, the number of characters written
2597 * On dst buffer overflow, -1
2598 */
2599static int utf7_wcstombs(const WCHAR *src, int srclen, char *dst, int dstlen)
2600{
2601 static const char base64_encoding_table[] =
2602 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2603
2604 const WCHAR *source_end = src + srclen;
2605 int dest_index = 0;
2606
2607 while (src < source_end)
2608 {
2609 if (*src == '+')
2610 {
2611 if (!utf7_write_c(dst, dstlen, &dest_index, '+'))
2612 return -1;
2613 if (!utf7_write_c(dst, dstlen, &dest_index, '-'))
2614 return -1;
2615 src++;
2616 }
2617 else if (utf7_can_directly_encode(*src))
2618 {
2619 if (!utf7_write_c(dst, dstlen, &dest_index, *src))
2620 return -1;
2621 src++;
2622 }
2623 else
2624 {
2625 unsigned int offset = 0;
2626 DWORD byte_pair = 0;
2627
2628 if (!utf7_write_c(dst, dstlen, &dest_index, '+'))
2629 return -1;
2630
2631 while (src < source_end && !utf7_can_directly_encode(*src))
2632 {
2633 byte_pair = (byte_pair << 16) | *src;
2634 offset += 16;
2635 while (offset >= 6)
2636 {
2637 if (!utf7_write_c(dst, dstlen, &dest_index, base64_encoding_table[(byte_pair >> (offset - 6)) & 0x3F]))
2638 return -1;
2639 offset -= 6;
2640 }
2641 src++;
2642 }
2643
2644 if (offset)
2645 {
2646 /* Windows won't create a padded base64 character if there's no room for the - sign
2647 * as well ; this is probably a bug in Windows */
2648 if (dstlen > 0 && dest_index + 1 >= dstlen)
2649 return -1;
2650
2651 byte_pair <<= (6 - offset);
2652 if (!utf7_write_c(dst, dstlen, &dest_index, base64_encoding_table[byte_pair & 0x3F]))
2653 return -1;
2654 }
2655
2656 /* Windows always explicitly terminates the base64 sequence
2657 even though RFC 2152 (page 3, rule 2) does not require this */
2658 if (!utf7_write_c(dst, dstlen, &dest_index, '-'))
2659 return -1;
2660 }
2661 }
2662
2663 return dest_index;
2664}
2665
2666/***********************************************************************
2667 * WideCharToMultiByte (KERNEL32.@)
2668 *
2669 * Convert a Unicode character string into a multibyte string.
2670 *
2671 * PARAMS
2672 * page [I] Code page character set to convert to
2673 * flags [I] Mapping Flags (MB_ constants from "winnls.h").
2674 * src [I] Source string buffer
2675 * srclen [I] Length of src (in WCHARs), or -1 if src is NUL terminated
2676 * dst [O] Destination buffer
2677 * dstlen [I] Length of dst (in bytes), or 0 to compute the required length
2678 * defchar [I] Default character to use for conversion if no exact
2679 * conversion can be made
2680 * used [O] Set if default character was used in the conversion
2681 *
2682 * RETURNS
2683 * Success: If dstlen > 0, the number of characters written to dst.
2684 * If dstlen == 0, number of characters needed to perform the
2685 * conversion. In both cases the count includes the terminating NUL.
2686 * Failure: 0. Use GetLastError() to determine the cause. Possible errors are
2687 * ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
2688 * and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
2689 * parameter was given.
2690 */
2692 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
2693{
2694 const union cptable *table;
2695 int ret, used_tmp;
2696
2697 if (!src || !srclen || (!dst && dstlen) || dstlen < 0)
2698 {
2700 return 0;
2701 }
2702
2703 if (srclen < 0) srclen = strlenW(src) + 1;
2704
2705 switch(page)
2706 {
2707 case CP_SYMBOL:
2708 /* when using CP_SYMBOL, ERROR_INVALID_FLAGS takes precedence */
2709 if (flags)
2710 {
2712 return 0;
2713 }
2714 if (defchar || used)
2715 {
2717 return 0;
2718 }
2720 break;
2721 case CP_UTF7:
2722 /* when using CP_UTF7, ERROR_INVALID_PARAMETER takes precedence */
2723 if (defchar || used)
2724 {
2726 return 0;
2727 }
2728 if (flags)
2729 {
2731 return 0;
2732 }
2734 break;
2735 case CP_UNIXCP:
2736 if (unix_cptable)
2737 {
2739 defchar, used ? &used_tmp : NULL );
2740 break;
2741 }
2742 /* fall through */
2743 case CP_UTF8:
2744 if (defchar || used)
2745 {
2747 return 0;
2748 }
2749 if (flags & ~WC_FLAGSMASK)
2750 {
2752 return 0;
2753 }
2755 break;
2756 default:
2757 if (!(table = get_codepage_table( page )))
2758 {
2760 return 0;
2761 }
2762 if (flags & ~WC_FLAGSMASK)
2763 {
2765 return 0;
2766 }
2768 defchar, used ? &used_tmp : NULL );
2769 if (used) *used = used_tmp;
2770 break;
2771 }
2772
2773 if (ret < 0)
2774 {
2775 switch(ret)
2776 {
2777 case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
2778 case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
2779 }
2780 ret = 0;
2781 }
2782 TRACE("cp %d %s -> %s, ret = %d\n",
2784 return ret;
2785}
2786#endif // !__REACTOS__
2787
2788
2789/***********************************************************************
2790 * GetThreadLocale (KERNEL32.@)
2791 *
2792 * Get the current threads locale.
2793 *
2794 * PARAMS
2795 * None.
2796 *
2797 * RETURNS
2798 * The LCID currently associated with the calling thread.
2799 */
2801{
2802 LCID ret = NtCurrentTeb()->CurrentLocale;
2803 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
2804 return ret;
2805}
2806
2807/**********************************************************************
2808 * SetThreadLocale (KERNEL32.@)
2809 *
2810 * Set the current threads locale.
2811 *
2812 * PARAMS
2813 * lcid [I] LCID of the locale to set
2814 *
2815 * RETURNS
2816 * Success: TRUE. The threads locale is set to lcid.
2817 * Failure: FALSE. Use GetLastError() to determine the cause.
2818 */
2820{
2821 TRACE("(0x%04X)\n", lcid);
2822
2823 lcid = ConvertDefaultLocale(lcid);
2824
2825 if (lcid != GetThreadLocale())
2826 {
2827 if (!IsValidLocale(lcid, LCID_SUPPORTED))
2828 {
2830 return FALSE;
2831 }
2832
2833 NtCurrentTeb()->CurrentLocale = lcid;
2834 }
2835 return TRUE;
2836}
2837
2838#ifndef __REACTOS__
2839/**********************************************************************
2840 * SetThreadUILanguage (KERNEL32.@)
2841 *
2842 * Set the current threads UI language.
2843 *
2844 * PARAMS
2845 * langid [I] LANGID of the language to set, or 0 to use
2846 * the available language which is best supported
2847 * for console applications
2848 *
2849 * RETURNS
2850 * Success: The return value is the same as the input value.
2851 * Failure: The return value differs from the input value.
2852 * Use GetLastError() to determine the cause.
2853 */
2855{
2856 TRACE("(0x%04x) stub - returning success\n", langid);
2857 return langid;
2858}
2859#endif // !__REACTOS__
2860
2861/******************************************************************************
2862 * ConvertDefaultLocale (KERNEL32.@)
2863 *
2864 * Convert a default locale identifier into a real identifier.
2865 *
2866 * PARAMS
2867 * lcid [I] LCID identifier of the locale to convert
2868 *
2869 * RETURNS
2870 * lcid unchanged, if not a default locale or its sublanguage is
2871 * not SUBLANG_NEUTRAL.
2872 * GetSystemDefaultLCID(), if lcid == LOCALE_SYSTEM_DEFAULT.
2873 * GetUserDefaultLCID(), if lcid == LOCALE_USER_DEFAULT or LOCALE_NEUTRAL.
2874 * Otherwise, lcid with sublanguage changed to SUBLANG_DEFAULT.
2875 */
2877{
2878 LANGID langid;
2879
2880 switch (lcid)
2881 {
2882 case LOCALE_INVARIANT:
2883 /* keep as-is */
2884 break;
2886 lcid = GetSystemDefaultLCID();
2887 break;
2889 case LOCALE_NEUTRAL:
2890 lcid = GetUserDefaultLCID();
2891 break;
2892 default:
2893 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
2894 langid = LANGIDFROMLCID(lcid);
2896 {
2898 lcid = MAKELCID(langid, SORTIDFROMLCID(lcid));
2899 }
2900 }
2901 return lcid;
2902}
2903
2904
2905/******************************************************************************
2906 * IsValidLocale (KERNEL32.@)
2907 *
2908 * Determine if a locale is valid.
2909 *
2910 * PARAMS
2911 * lcid [I] LCID of the locale to check
2912 * flags [I] LCID_SUPPORTED = Valid, LCID_INSTALLED = Valid and installed on the system
2913 *
2914 * RETURNS
2915 * TRUE, if lcid is valid,
2916 * FALSE, otherwise.
2917 *
2918 * NOTES
2919 * Wine does not currently make the distinction between supported and installed. All
2920 * languages supported are installed by default.
2921 */
2923{
2924 /* check if language is registered in the kernel32 resources */
2927}
2928
2929#ifndef __REACTOS__
2930/******************************************************************************
2931 * IsValidLocaleName (KERNEL32.@)
2932 */
2934{
2935 struct locale_name locale_name;
2936
2937 if (!locale)
2938 return FALSE;
2939
2940 /* string parsing */
2941 parse_locale_name( locale, &locale_name );
2942
2943 TRACE( "found lcid %x for %s, matches %d\n",
2945
2946 return locale_name.matches > 0;
2947}
2948#endif // !__REACTOS__
2949
2952{
2953 LOCALE_ENUMPROCA lpfnLocaleEnum = (LOCALE_ENUMPROCA)lParam;
2954 char buf[20];
2955
2956 sprintf(buf, "%08x", (UINT)LangID);
2957 return lpfnLocaleEnum( buf );
2958}
2959
2962{
2963 static const WCHAR formatW[] = {'%','0','8','x',0};
2964 LOCALE_ENUMPROCW lpfnLocaleEnum = (LOCALE_ENUMPROCW)lParam;
2965 WCHAR buf[20];
2966 sprintfW( buf, formatW, (UINT)LangID );
2967 return lpfnLocaleEnum( buf );
2968}
2969
2970/******************************************************************************
2971 * EnumSystemLocalesA (KERNEL32.@)
2972 *
2973 * Call a users function for each locale available on the system.
2974 *
2975 * PARAMS
2976 * lpfnLocaleEnum [I] Callback function to call for each locale
2977 * dwFlags [I] LOCALE_SUPPORTED=All supported, LOCALE_INSTALLED=Installed only
2978 *
2979 * RETURNS
2980 * Success: TRUE.
2981 * Failure: FALSE. Use GetLastError() to determine the cause.
2982 */
2984{
2985 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
2988 (LONG_PTR)lpfnLocaleEnum);
2989 return TRUE;
2990}
2991
2992
2993/******************************************************************************
2994 * EnumSystemLocalesW (KERNEL32.@)
2995 *
2996 * See EnumSystemLocalesA.
2997 */
2999{
3000 TRACE("(%p,%08x)\n", lpfnLocaleEnum, dwFlags);
3003 (LONG_PTR)lpfnLocaleEnum);
3004 return TRUE;
3005}
3006
3007
3009{
3013};
3014
3017{
3019 WCHAR buffer[256];
3020 DWORD neutral;
3021 unsigned int flags;
3022
3024 buffer, sizeof(buffer) / sizeof(WCHAR) );
3026 LOCALE_INEUTRAL | LOCALE_NOUSEROVERRIDE | LOCALE_RETURN_NUMBER,
3027 (LPWSTR)&neutral, sizeof(neutral) / sizeof(WCHAR) ))
3028 neutral = 0;
3031 if (data->flags && !(data->flags & flags)) return TRUE;
3032 return data->proc( buffer, flags, data->lparam );
3033}
3034
3035/******************************************************************************
3036 * EnumSystemLocalesEx (KERNEL32.@)
3037 */
3039{
3041
3042 if (reserved)
3043 {
3045 return FALSE;
3046 }
3047 data.proc = proc;
3048 data.flags = flags;
3049 data.lparam = lparam;
3053 return TRUE;
3054}
3055
3056
3057/***********************************************************************
3058 * VerLanguageNameA (KERNEL32.@)
3059 *
3060 * Get the name of a language.
3061 *
3062 * PARAMS
3063 * wLang [I] LANGID of the language
3064 * szLang [O] Destination for the language name
3065 *
3066 * RETURNS
3067 * Success: The size of the language name. If szLang is non-NULL, it is filled
3068 * with the name.
3069 * Failure: 0. Use GetLastError() to determine the cause.
3070 *
3071 */
3073{
3074 return GetLocaleInfoA( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
3075}
3076
3077
3078/***********************************************************************
3079 * VerLanguageNameW (KERNEL32.@)
3080 *
3081 * See VerLanguageNameA.
3082 */
3084{
3085 return GetLocaleInfoW( MAKELCID(wLang, SORT_DEFAULT), LOCALE_SENGLANGUAGE, szLang, nSize );
3086}
3087
3088
3089/******************************************************************************
3090 * GetStringTypeW (KERNEL32.@)
3091 *
3092 * See GetStringTypeA.
3093 */
3095{
3096 static const unsigned char type2_map[16] =
3097 {
3098 C2_NOTAPPLICABLE, /* unassigned */
3099 C2_LEFTTORIGHT, /* L */
3100 C2_RIGHTTOLEFT, /* R */
3101 C2_EUROPENUMBER, /* EN */
3102 C2_EUROPESEPARATOR, /* ES */
3103 C2_EUROPETERMINATOR, /* ET */
3104 C2_ARABICNUMBER, /* AN */
3105 C2_COMMONSEPARATOR, /* CS */
3106 C2_BLOCKSEPARATOR, /* B */
3107 C2_SEGMENTSEPARATOR, /* S */
3108 C2_WHITESPACE, /* WS */
3109 C2_OTHERNEUTRAL, /* ON */
3110 C2_RIGHTTOLEFT, /* AL */
3111 C2_NOTAPPLICABLE, /* NSM */
3112 C2_NOTAPPLICABLE, /* BN */
3113 C2_OTHERNEUTRAL /* LRE, LRO, RLE, RLO, PDF */
3114 };
3115
3116 if (!src)
3117 {
3119 return FALSE;
3120 }
3121
3122 if (count == -1) count = strlenW(src) + 1;
3123 switch(type)
3124 {
3125 case CT_CTYPE1:
3126 while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
3127 break;
3128 case CT_CTYPE2:
3129 while (count--) *chartype++ = type2_map[get_char_typeW( *src++ ) >> 12];
3130 break;
3131 case CT_CTYPE3:
3132 {
3133 WARN("CT_CTYPE3: semi-stub.\n");
3134 while (count--)
3135 {
3136 int c = *src;
3137 WORD type1, type3 = 0; /* C3_NOTAPPLICABLE */
3138
3139 type1 = get_char_typeW( *src++ ) & 0xfff;
3140 /* try to construct type3 from type1 */
3141 if(type1 & C1_SPACE) type3 |= C3_SYMBOL;
3142 if(type1 & C1_ALPHA) type3 |= C3_ALPHA;
3143 if ((c>=0x30A0)&&(c<=0x30FF)) type3 |= C3_KATAKANA;
3144 if ((c>=0x3040)&&(c<=0x309F)) type3 |= C3_HIRAGANA;
3145 if ((c>=0x4E00)&&(c<=0x9FAF)) type3 |= C3_IDEOGRAPH;
3146 if (c == 0x0640) type3 |= C3_KASHIDA;
3147 if ((c>=0x3000)&&(c<=0x303F)) type3 |= C3_SYMBOL;
3148
3149 if ((c>=0xD800)&&(c<=0xDBFF)) type3 |= C3_HIGHSURROGATE;
3150 if ((c>=0xDC00)&&(c<=0xDFFF)) type3 |= C3_LOWSURROGATE;
3151
3152 if ((c>=0xFF00)&&(c<=0xFF60)) type3 |= C3_FULLWIDTH;
3153 if ((c>=0xFF00)&&(c<=0xFF20)) type3 |= C3_SYMBOL;
3154 if ((c>=0xFF3B)&&(c<=0xFF40)) type3 |= C3_SYMBOL;
3155 if ((c>=0xFF5B)&&(c<=0xFF60)) type3 |= C3_SYMBOL;
3156 if ((c>=0xFF21)&&(c<=0xFF3A)) type3 |= C3_ALPHA;
3157 if ((c>=0xFF41)&&(c<=0xFF5A)) type3 |= C3_ALPHA;
3158 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_FULLWIDTH;
3159 if ((c>=0xFFE0)&&(c<=0xFFE6)) type3 |= C3_SYMBOL;
3160
3161 if ((c>=0xFF61)&&(c<=0xFFDC)) type3 |= C3_HALFWIDTH;
3162 if ((c>=0xFF61)&&(c<=0xFF64)) type3 |= C3_SYMBOL;
3163 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_KATAKANA;
3164 if ((c>=0xFF65)&&(c<=0xFF9F)) type3 |= C3_ALPHA;
3165 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_HALFWIDTH;
3166 if ((c>=0xFFE8)&&(c<=0xFFEE)) type3 |= C3_SYMBOL;
3167 *chartype++ = type3;
3168 }
3169 break;
3170 }
3171 default:
3173 return FALSE;
3174 }
3175 return TRUE;
3176}
3177
3178
3179/******************************************************************************
3180 * GetStringTypeExW (KERNEL32.@)
3181 *
3182 * See GetStringTypeExA.
3183 */
3185{
3186 /* locale is ignored for Unicode */
3187 return GetStringTypeW( type, src, count, chartype );
3188}
3189
3190
3191/******************************************************************************
3192 * GetStringTypeA (KERNEL32.@)
3193 *
3194 * Get characteristics of the characters making up a string.
3195 *
3196 * PARAMS
3197 * locale [I] Locale Id for the string
3198 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
3199 * src [I] String to analyse
3200 * count [I] Length of src in chars, or -1 if src is NUL terminated
3201 * chartype [O] Destination for the calculated characteristics
3202 *
3203 * RETURNS
3204 * Success: TRUE. chartype is filled with the requested characteristics of each char
3205 * in src.
3206 * Failure: FALSE. Use GetLastError() to determine the cause.
3207 */
3209{
3210 UINT cp;
3211 INT countW;
3212 LPWSTR srcW;
3213 BOOL ret = FALSE;
3214
3215 if(count == -1) count = strlen(src) + 1;
3216
3217 if (!(cp = get_lcid_codepage( locale )))
3218 {
3219 FIXME("For locale %04x using current ANSI code page\n", locale);
3220 cp = GetACP();
3221 }
3222
3223 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
3224 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
3225 {
3226 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
3227 /*
3228 * NOTE: the target buffer has 1 word for each CHARACTER in the source
3229 * string, with multibyte characters there maybe be more bytes in count
3230 * than character space in the buffer!
3231 */
3232 ret = GetStringTypeW(type, srcW, countW, chartype);
3233 HeapFree(GetProcessHeap(), 0, srcW);
3234 }
3235 return ret;
3236}
3237
3238/******************************************************************************
3239 * GetStringTypeExA (KERNEL32.@)
3240 *
3241 * Get characteristics of the characters making up a string.
3242 *
3243 * PARAMS
3244 * locale [I] Locale Id for the string
3245 * type [I] CT_CTYPE1 = classification, CT_CTYPE2 = directionality, CT_CTYPE3 = typographic info
3246 * src [I] String to analyse
3247 * count [I] Length of src in chars, or -1 if src is NUL terminated
3248 * chartype [O] Destination for the calculated characteristics
3249 *
3250 * RETURNS
3251 * Success: TRUE. chartype is filled with the requested characteristics of each char
3252 * in src.
3253 * Failure: FALSE. Use GetLastError() to determine the cause.
3254 */
3256{
3257 return GetStringTypeA(locale, type, src, count, chartype);
3258}
3259
3260#ifdef __REACTOS__
3261static inline void map_byterev(const WCHAR *src, int len, WCHAR *dst)
3262{
3263 while (len--)
3264 *dst++ = RtlUshortByteSwap(*src++);
3265}
3266
3267static int map_to_hiragana(const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3268{
3269 int pos;
3270 for (pos = 0; srclen; src++, srclen--, pos++)
3271 {
3272 /*
3273 * U+30A1 ... U+30F3: Katakana
3274 * U+30F4: Katakana Letter VU
3275 * U+30F5: Katakana Letter Small KA
3276 * U+30FD: Katakana Iteration Mark
3277 * U+30FE: Katakana Voiced Iteration Mark
3278 */
3279 WCHAR wch = *src;
3280 if ((0x30A1 <= wch && wch <= 0x30F3) ||
3281 wch == 0x30F4 || wch == 0x30F5 || wch == 0x30FD || wch == 0x30FE)
3282 {
3283 wch -= 0x60; /* Katakana to Hiragana */
3284 }
3285 if (pos < dstlen)
3286 dst[pos] = wch;
3287 }
3288 return pos;
3289}
3290
3291static int map_to_katakana(const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3292{
3293 int pos;
3294 for (pos = 0; srclen; src++, srclen--, pos++)
3295 {
3296 /*
3297 * U+3041 ... U+3093: Hiragana
3298 * U+3094: Hiragana Letter VU
3299 * U+3095: Hiragana Letter Small KA
3300 * U+309D: Hiragana Iteration Mark
3301 * U+309E: Hiragana Voiced Iteration Mark
3302 */
3303 WCHAR wch = *src;
3304 if ((0x3041 <= wch && wch <= 0x3093) ||
3305 wch == 3094 || wch == 0x3095 || wch == 0x309D || wch == 0x309E)
3306 {
3307 wch += 0x60; /* Hiragana to Katakana */
3308 }
3309 if (pos < dstlen)
3310 dst[pos] = wch;
3311 }
3312 return pos;
3313}
3314
3315/* The table that contains fullwidth characters and halfwidth characters */
3316typedef WCHAR FULL2HALF_ENTRY[3];
3317static const FULL2HALF_ENTRY full2half_table[] =
3318{
3319#define DEFINE_FULL2HALF(full, half1, half2) { full, half1, half2 },
3320#include "full2half.h"
3321#undef DEFINE_FULL2HALF
3322};
3323#define GET_FULL(table, index) ((table)[index][0])
3324#define GET_HALF1(table, index) ((table)[index][1])
3325#define GET_HALF2(table, index) ((table)[index][2])
3326
3327/* The table that contains dakuten entries */
3328typedef WCHAR DAKUTEN_ENTRY[3];
3329static const DAKUTEN_ENTRY dakuten_table[] =
3330{
3331#define DEFINE_DAKUTEN(voiced, single1, single2, half1, half2) { voiced, single1, single2 },
3332#include "dakuten.h"
3333#undef DEFINE_DAKUTEN
3334};
3335#define GET_VOICED(table, index) ((table)[index][0])
3336#define GET_SINGLE1(table, index) ((table)[index][1])
3337#define GET_SINGLE2(table, index) ((table)[index][2])
3338
3339static int map_to_halfwidth(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3340{
3341 int pos, i;
3342 const int count1 = (int)ARRAY_SIZE(full2half_table);
3343 const FULL2HALF_ENTRY *table1 = full2half_table;
3344
3345 for (pos = 0; srclen; src++, srclen--, pos++)
3346 {
3347 WCHAR ch = *src;
3348
3349 if (flags & LCMAP_KATAKANA)
3350 map_to_katakana(&ch, 1, &ch, 1);
3351 else if (flags & LCMAP_HIRAGANA)
3352 map_to_hiragana(&ch, 1, &ch, 1);
3353
3354 if (ch < 0x3000) /* Quick judgment */
3355 {
3356 if (pos < dstlen)
3357 dst[pos] = ch;
3358 continue;
3359 }
3360
3361 if (0xFF01 <= ch && ch <= 0xFF5E) /* U+FF01 ... U+FF5E */
3362 {
3363 if (pos < dstlen)
3364 dst[pos] = ch - 0xFEE0; /* Fullwidth ASCII to halfwidth ASCII */
3365 continue;
3366 }
3367
3368 /* Search in table1 (full/half) */
3369 for (i = count1 - 1; i >= 0; --i) /* In reverse order */
3370 {
3371 if (GET_FULL(table1, i) != ch)
3372 continue;
3373
3374 if (GET_HALF2(table1, i) == 0)
3375 {
3376 if (pos < dstlen)
3377 dst[pos] = GET_HALF1(table1, i);
3378 }
3379 else if (!dstlen)
3380 {
3381 pos++;
3382 }
3383 else if (pos + 1 < dstlen)
3384 {
3385 dst[pos++] = GET_HALF1(table1, i);
3386 dst[pos ] = GET_HALF2(table1, i);
3387 }
3388 else
3389 {
3390 dst[pos] = ch;
3391 }
3392 break;
3393 }
3394
3395 if (i >= 0)
3396 continue;
3397
3398 if (pos < dstlen)
3399 dst[pos] = ch;
3400 }
3401
3402 return pos;
3403}
3404
3405static int map_to_fullwidth(const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3406{
3407 int pos, i;
3408 const FULL2HALF_ENTRY *table1 = full2half_table;
3409 const DAKUTEN_ENTRY *table2 = dakuten_table;
3410 const int count1 = (int)ARRAY_SIZE(full2half_table);
3411 const int count2 = (int)ARRAY_SIZE(dakuten_table);
3412
3413 for (pos = 0; srclen; src++, srclen--, pos++)
3414 {
3415 WCHAR ch = *src;
3416
3417 if (ch == 0x20) /* U+0020: Space */
3418 {
3419 if (pos < dstlen)
3420 dst[pos] = 0x3000; /* U+3000: Ideographic Space */
3421 continue;
3422 }
3423
3424 if (0x21 <= ch && ch <= 0x7E) /* Mappable halfwidth ASCII */
3425 {
3426 if (pos < dstlen)
3427 dst[pos] = ch + 0xFEE0; /* U+FF01 ... U+FF5E */
3428 continue;
3429 }
3430
3431 if (ch < 0xFF00) /* Quick judgment */
3432 {
3433 if (pos < dstlen)
3434 dst[pos] = ch;
3435 continue;
3436 }
3437
3438 /* Search in table1 (full/half) */
3439 for (i = count1 - 1; i >= 0; --i) /* In reverse order */
3440 {
3441 if (GET_HALF1(table1, i) != ch)
3442 continue; /* Mismatched */
3443
3444 if (GET_HALF2(table1, i) == 0)
3445 {
3446 if (pos < dstlen)
3447 dst[pos] = GET_FULL(table1, i);
3448 break;
3449 }
3450
3451 if (srclen <= 1 || GET_HALF2(table1, i) != src[1])
3452 continue; /* Mismatched */
3453
3454 --srclen;
3455 ++src;
3456
3457 if (pos < dstlen)
3458 dst[pos] = GET_FULL(table1, i);
3459 break;
3460 }
3461
3462 if (i >= 0)
3463 continue;
3464
3465 /* Search in table2 (dakuten) */
3466 for (i = count2 - 1; i >= 0; --i) /* In reverse order */
3467 {
3468 if (GET_SINGLE1(table2, i) != ch)
3469 continue; /* Mismatched */
3470
3471 if (srclen <= 1 || GET_SINGLE2(table2, i) != src[1])
3472 continue; /* Mismatched */
3473
3474 --srclen;
3475 ++src;
3476
3477 if (pos < dstlen)
3478 dst[pos] = GET_VOICED(table2, i);
3479 break;
3480 }
3481
3482 if (i >= 0)
3483 continue;
3484
3485 if (pos < dstlen)
3486 dst[pos] = ch;
3487 }
3488
3489 return pos;
3490}
3491
3492static int map_to_lowercase(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3493{
3494 int pos;
3495 for (pos = 0; srclen; src++, srclen--)
3496 {
3497 WCHAR wch = *src;
3499 continue;
3500 if (pos < dstlen)
3501 dst[pos] = tolowerW(wch);
3502 pos++;
3503 }
3504 return pos;
3505}
3506
3507static int map_to_uppercase(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3508{
3509 int pos;
3510 for (pos = 0; srclen; src++, srclen--)
3511 {
3512 WCHAR wch = *src;
3514 continue;
3515 if (pos < dstlen)
3516 dst[pos] = toupperW(wch);
3517 pos++;
3518 }
3519 return pos;
3520}
3521
3522typedef struct tagWCHAR_PAIR
3523{
3524 WCHAR from, to;
3525} WCHAR_PAIR, *PWCHAR_PAIR;
3526
3527/* The table to convert Simplified Chinese to Traditional Chinese */
3528static const WCHAR_PAIR s_sim2tra[] =
3529{
3530#define DEFINE_SIM2TRA(from, to) { from, to },
3531#include "sim2tra.h"
3532#undef DEFINE_SIM2TRA
3533};
3534
3535/* The table to convert Traditional Chinese to Simplified Chinese */
3536static const WCHAR_PAIR s_tra2sim[] =
3537{
3538#define DEFINE_TRA2SIM(from, to) { from, to },
3539#include "tra2sim.h"
3540#undef DEFINE_TRA2SIM
3541};
3542
3543/* The comparison function to do bsearch */
3544static int compare_wchar_pair(const void *x, const void *y)
3545{
3546 const WCHAR_PAIR *a = x;
3547 const WCHAR_PAIR *b = y;
3548 if (a->from < b->from)
3549 return -1;
3550 if (a->from > b->from)
3551 return +1;
3552 return 0;
3553}
3554
3555static WCHAR find_wchar_pair(const WCHAR_PAIR *pairs, size_t count, WCHAR ch)
3556{
3557 PWCHAR_PAIR found = bsearch(&ch, pairs, count, sizeof(WCHAR_PAIR), compare_wchar_pair);
3558 if (found)
3559 return found->to;
3560 return ch;
3561}
3562
3563static int map_to_simplified_chinese(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3564{
3565 int pos;
3566 for (pos = 0; srclen; src++, srclen--)
3567 {
3568 WCHAR wch = *src;
3569 if (pos < dstlen)
3570 dst[pos] = find_wchar_pair(s_tra2sim, ARRAY_SIZE(s_tra2sim), wch);
3571 pos++;
3572 }
3573 return pos;
3574}
3575
3576static int map_to_traditional_chinese(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3577{
3578 int pos;
3579 for (pos = 0; srclen; src++, srclen--)
3580 {
3581 WCHAR wch = *src;
3582 if (pos < dstlen)
3583 dst[pos] = find_wchar_pair(s_sim2tra, ARRAY_SIZE(s_sim2tra), wch);
3584 pos++;
3585 }
3586 return pos;
3587}
3588
3589static int map_remove_ignored(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3590{
3591 int pos;
3592 WORD wC1, wC2, wC3;
3593 for (pos = 0; srclen; src++, srclen--)
3594 {
3595 WCHAR wch = *src;
3596 GetStringTypeW(CT_CTYPE1, &wch, 1, &wC1);
3597 GetStringTypeW(CT_CTYPE2, &wch, 1, &wC2);
3598 GetStringTypeW(CT_CTYPE3, &wch, 1, &wC3);
3600 {
3601 if ((wC1 & C1_PUNCT) || (wC3 & C3_SYMBOL))
3602 continue;
3603 }
3605 {
3606 if ((wC2 & C2_OTHERNEUTRAL) && (wC3 & (C3_NONSPACING | C3_DIACRITIC)))
3607 continue;
3608 }
3609 if (pos < dstlen)
3610 dst[pos] = wch;
3611 pos++;
3612 }
3613 return pos;
3614}
3615
3616static int lcmap_string(DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen)
3617{
3618 int ret = 0;
3619
3621 {
3623 return 0;
3624 }
3625
3627 {
3628 case LCMAP_HIRAGANA:
3629 ret = map_to_hiragana(src, srclen, dst, dstlen);
3630 break;
3631 case LCMAP_KATAKANA:
3632 ret = map_to_katakana(src, srclen, dst, dstlen);
3633 break;
3634 case LCMAP_HALFWIDTH:
3635 ret = map_to_halfwidth(flags, src, srclen, dst, dstlen);
3636 break;
3638 ret = map_to_halfwidth(flags, src, srclen, dst, dstlen);
3639 break;
3641 ret = map_to_halfwidth(flags, src, srclen, dst, dstlen);
3642 break;
3643 case LCMAP_FULLWIDTH:
3644 ret = map_to_fullwidth(src, srclen, dst, dstlen);
3645 break;
3647 ret = map_to_fullwidth(src, srclen, dst, dstlen);
3648 if (dstlen && ret)
3649 map_to_hiragana(dst, ret, dst, dstlen);
3650 break;
3652 ret = map_to_fullwidth(src, srclen, dst, dstlen);
3653 if (dstlen && ret)
3654 map_to_katakana(dst, ret, dst, dstlen);
3655 break;
3657 ret = map_to_simplified_chinese(flags, src, srclen, dst, dstlen);
3658 break;
3660 ret = map_to_traditional_chinese(flags, src, srclen, dst, dstlen);
3661 break;
3663 case NORM_IGNORESYMBOLS:
3666 {
3668 return 0;
3669 }
3670 ret = map_remove_ignored(flags, src, srclen, dst, dstlen);
3671 break;
3672 case 0:
3673 if (flags & LCMAP_LOWERCASE)
3674 {
3675 ret = map_to_lowercase(flags, src, srclen, dst, dstlen);
3676 flags &= ~LCMAP_LOWERCASE;
3677 break;
3678 }
3679 if (flags & LCMAP_UPPERCASE)
3680 {
3681 ret = map_to_uppercase(flags, src, srclen, dst, dstlen);
3682 flags &= ~LCMAP_UPPERCASE;
3683 break;
3684 }
3685 if (flags & LCMAP_BYTEREV)
3686 {
3687 if (dstlen == 0)
3688 {
3689 ret = srclen;
3690 break;
3691 }
3692 ret = min(srclen, dstlen);
3693 RtlCopyMemory(dst, src, ret * sizeof(WCHAR));
3694 break;
3695 }
3696 /* fall through */
3697 default:
3699 return 0;
3700 }
3701
3702 if (dstlen)
3703 {
3704 if (flags & LCMAP_LOWERCASE)
3705 map_to_lowercase(flags, dst, ret, dst, dstlen);
3706 if (flags & LCMAP_UPPERCASE)
3707 map_to_uppercase(flags, dst, ret, dst, dstlen);
3708 if (flags & LCMAP_BYTEREV)
3709 map_byterev(dst, min(ret, dstlen), dst);
3710
3711 if (dstlen < ret)
3712 {
3714 return 0;
3715 }
3716 }
3717
3718 return ret;
3719}
3720#endif // __REACTOS__
3721
3722/*************************************************************************
3723 * LCMapStringEx (KERNEL32.@)
3724 *
3725 * Map characters in a locale sensitive string.
3726 *
3727 * PARAMS
3728 * locale [I] Locale name for the conversion.
3729 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h")
3730 * src [I] String to map
3731 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
3732 * dst [O] Destination for mapped string
3733 * dstlen [I] Length of dst in characters
3734 * version [I] reserved, must be NULL
3735 * reserved [I] reserved, must be NULL
3736 * lparam [I] reserved, must be 0
3737 *
3738 * RETURNS
3739 * Success: The length of the mapped string in dst, including the NUL terminator.
3740 * Failure: 0. Use GetLastError() to determine the cause.
3741 */
3744{
3745 if (version) FIXME("unsupported version structure %p\n", version);
3746 if (reserved) FIXME("unsupported reserved pointer %p\n", reserved);
3747 if (handle)
3748 {
3749 static int once;
3750 if (!once++) FIXME("unsupported lparam %Ix\n", handle);
3751 }
3752
3753 if (!src || !srclen || dstlen < 0)
3754 {
3756 return 0;
3757 }
3758
3759 if (srclen < 0) srclen = lstrlenW(src) + 1;
3760
3761 TRACE( "(%s,0x%08lx,%s,%d,%p,%d)\n",
3763
3764 flags &= ~LOCALE_USE_CP_ACP;
3765
3766 if (src == dst && (flags & ~(LCMAP_LOWERCASE | LCMAP_UPPERCASE)))
3767 {
3769 return 0;
3770 }
3771
3772 if (!dstlen) dst = NULL;
3773
3774 if (flags & LCMAP_SORTKEY)
3775 {
3776 INT ret;
3777
3778 if (srclen < 0)
3779 srclen = strlenW(src);
3780
3781 ret = wine_get_sortkey(flags, src, srclen, (char *)dst, dstlen);
3782 if (ret == 0)
3784 else
3785 ret++;
3786 return ret;
3787 }
3788
3789 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
3790 if (flags & SORT_STRINGSORT)
3791 {
3793 return 0;
3794 }
3795
3796 return lcmap_string(flags, src, srclen, dst, dstlen);
3797}
3798
3799/*************************************************************************
3800 * LCMapStringW (KERNEL32.@)
3801 *
3802 * See LCMapStringA.
3803 */
3806{
3807 TRACE("(0x%04x,0x%08x,%s,%d,%p,%d)\n",
3809
3810 return LCMapStringEx(NULL, flags, src, srclen, dst, dstlen, NULL, NULL, 0);
3811}
3812
3813/*************************************************************************
3814 * LCMapStringA (KERNEL32.@)
3815 *
3816 * Map characters in a locale sensitive string.
3817 *
3818 * PARAMS
3819 * lcid [I] LCID for the conversion.
3820 * flags [I] Flags controlling the mapping (LCMAP_ constants from "winnls.h").
3821 * src [I] String to map
3822 * srclen [I] Length of src in chars, or -1 if src is NUL terminated
3823 * dst [O] Destination for mapped string
3824 * dstlen [I] Length of dst in characters
3825 *
3826 * RETURNS
3827 * Success: The length of the mapped string in dst, including the NUL terminator.
3828 * Failure: 0. Use GetLastError() to determine the cause.
3829 */
3832{
3833 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
3834 LPWSTR srcW, dstW;
3835 INT ret = 0, srclenW, dstlenW;
3836 UINT locale_cp = CP_ACP;
3837
3838 if (!src || !srclen || dstlen < 0)
3839 {
3841 return 0;
3842 }
3843
3844 if (!(flags & LOCALE_USE_CP_ACP)) locale_cp = get_lcid_codepage( lcid );
3845
3846 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, bufW, 260);
3847 if (srclenW)
3848 srcW = bufW;
3849 else
3850 {
3851 srclenW = MultiByteToWideChar(locale_cp, 0, src, srclen, NULL, 0);
3852 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
3853 if (!srcW)
3854 {
3856 return 0;
3857 }
3858 MultiByteToWideChar(locale_cp, 0, src, srclen, srcW, srclenW);
3859 }
3860
3861 if (flags & LCMAP_SORTKEY)
3862 {
3863 if (src == dst)
3864 {
3866 goto map_string_exit;
3867 }
3868 ret = wine_get_sortkey(flags, srcW, srclenW, dst, dstlen);
3869 if (ret == 0)
3871 else
3872 ret++;
3873 goto map_string_exit;
3874 }
3875
3876 if (flags & SORT_STRINGSORT)
3877 {
3879 goto map_string_exit;
3880 }
3881
3882 dstlenW = LCMapStringEx(NULL, flags, srcW, srclenW, NULL, 0, NULL, NULL, 0);
3883 if (!dstlenW)
3884 goto map_string_exit;
3885
3886 dstW = HeapAlloc(GetProcessHeap(), 0, dstlenW * sizeof(WCHAR));
3887 if (!dstW)
3888 {
3890 goto map_string_exit;
3891 }
3892
3893 LCMapStringEx(NULL, flags, srcW, srclenW, dstW, dstlenW, NULL, NULL, 0);
3894 ret = WideCharToMultiByte(locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL);
3895 HeapFree(GetProcessHeap(), 0, dstW);
3896
3897map_string_exit:
3898 if (srcW != bufW) HeapFree(GetProcessHeap(), 0, srcW);
3899 return ret;
3900}
3901
3902/*************************************************************************
3903 * FoldStringA (KERNEL32.@)
3904 *
3905 * Map characters in a string.
3906 *
3907 * PARAMS
3908 * dwFlags [I] Flags controlling chars to map (MAP_ constants from "winnls.h")
3909 * src [I] String to map
3910 * srclen [I] Length of src, or -1 if src is NUL terminated
3911 * dst [O] Destination for mapped string
3912 * dstlen [I] Length of dst, or 0 to find the required length for the mapped string
3913 *
3914 * RETURNS
3915 * Success: The length of the string written to dst, including the terminating NUL. If
3916 * dstlen is 0, the value returned is the same, but nothing is written to dst,
3917 * and dst may be NULL.
3918 * Failure: 0. Use GetLastError() to determine the cause.
3919 */
3922{
3923 INT ret = 0, srclenW = 0;
3924 WCHAR *srcW = NULL, *dstW = NULL;
3925
3926 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
3927 {
3929 return 0;
3930 }
3931
3933 src, srclen, NULL, 0);
3934 srcW = HeapAlloc(GetProcessHeap(), 0, srclenW * sizeof(WCHAR));
3935
3936 if (!srcW)
3937 {
3939 goto FoldStringA_exit;
3940 }
3941
3943 src, srclen, srcW, srclenW);
3944
3945 dwFlags = (dwFlags & ~MAP_PRECOMPOSED) | MAP_FOLDCZONE;
3946
3947 ret = FoldStringW(dwFlags, srcW, srclenW, NULL, 0);
3948 if (ret && dstlen)
3949 {
3950 dstW = HeapAlloc(GetProcessHeap(), 0, ret * sizeof(WCHAR));
3951
3952 if (!dstW)
3953 {
3955 goto FoldStringA_exit;
3956 }
3957
3958 ret = FoldStringW(dwFlags, srcW, srclenW, dstW, ret);
3959 if (!WideCharToMultiByte(CP_ACP, 0, dstW, ret, dst, dstlen, NULL, NULL))
3960 {
3961