ntdll/tests: Adjust test_virtual_unwind() for Win11 results.
[wine.git] / dlls / ntdll / locale.c
bloba5777117cfafaa8e92c139e87bc5fcdedfe77bc1
1 /*
2 * Locale functions
4 * Copyright 2004, 2019 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <string.h>
23 #include <stdlib.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "ntdll_misc.h"
31 #include "locale_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(nls);
36 UINT NlsAnsiCodePage = 0;
37 BYTE NlsMbCodePageTag = 0;
38 BYTE NlsMbOemCodePageTag = 0;
40 static LCID user_resource_lcid;
41 static LCID user_resource_neutral_lcid;
42 static LCID system_lcid;
43 static NLSTABLEINFO nls_info = { { CP_UTF8 }, { CP_UTF8 } };
44 static struct norm_table *norm_tables[16];
45 static const NLS_LOCALE_HEADER *locale_table;
46 static const WCHAR *locale_strings;
49 static WCHAR casemap( USHORT *table, WCHAR ch )
51 return ch + table[table[table[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0x0f)];
55 static NTSTATUS load_norm_table( ULONG form, const struct norm_table **info )
57 unsigned int i;
58 USHORT *data, *tables;
59 SIZE_T size;
60 NTSTATUS status;
62 if (!form) return STATUS_INVALID_PARAMETER;
63 if (form >= ARRAY_SIZE(norm_tables)) return STATUS_OBJECT_NAME_NOT_FOUND;
65 if (!norm_tables[form])
67 if ((status = NtGetNlsSectionPtr( NLS_SECTION_NORMALIZE, form, NULL, (void **)&data, &size )))
68 return status;
70 /* sanity checks */
72 if (size <= 0x44) goto invalid;
73 if (data[0x14] != form) goto invalid;
74 tables = data + 0x1a;
75 for (i = 0; i < 8; i++)
77 if (tables[i] > size / sizeof(USHORT)) goto invalid;
78 if (i && tables[i] < tables[i-1]) goto invalid;
81 if (InterlockedCompareExchangePointer( (void **)&norm_tables[form], data, NULL ))
82 NtUnmapViewOfSection( GetCurrentProcess(), data );
84 *info = norm_tables[form];
85 return STATUS_SUCCESS;
87 invalid:
88 NtUnmapViewOfSection( GetCurrentProcess(), data );
89 return STATUS_INVALID_PARAMETER;
93 void locale_init(void)
95 const NLS_LOCALE_LCID_INDEX *entry;
96 USHORT utf8[2] = { 0, CP_UTF8 };
97 WCHAR locale[LOCALE_NAME_MAX_LENGTH];
98 LARGE_INTEGER unused;
99 SIZE_T size;
100 UINT ansi_cp = 1252, oem_cp = 437;
101 void *ansi_ptr = utf8, *oem_ptr = utf8, *case_ptr;
102 NTSTATUS status;
103 const struct locale_nls_header *header;
105 status = RtlGetLocaleFileMappingAddress( (void **)&header, &system_lcid, &unused );
106 if (status)
108 ERR( "locale init failed %lx\n", status );
109 return;
111 locale_table = (const NLS_LOCALE_HEADER *)((char *)header + header->locales);
112 locale_strings = (const WCHAR *)((char *)locale_table + locale_table->strings_offset);
114 entry = find_lcid_entry( locale_table, system_lcid );
115 ansi_cp = get_locale_data( locale_table, entry->idx )->idefaultansicodepage;
116 oem_cp = get_locale_data( locale_table, entry->idx )->idefaultcodepage;
118 NtQueryDefaultLocale( TRUE, &user_resource_lcid );
119 user_resource_neutral_lcid = PRIMARYLANGID( user_resource_lcid );
120 if (user_resource_lcid == LOCALE_CUSTOM_UNSPECIFIED)
122 const NLS_LOCALE_LCNAME_INDEX *entry;
123 const WCHAR *parent;
124 WCHAR bufferW[LOCALE_NAME_MAX_LENGTH];
125 SIZE_T len;
127 if (!RtlQueryEnvironmentVariable( NULL, L"WINEUSERLOCALE", 14, bufferW, ARRAY_SIZE(bufferW), &len )
128 && (entry = find_lcname_entry( locale_table, bufferW )))
130 user_resource_lcid = get_locale_data( locale_table, entry->idx )->unique_lcid;
131 parent = locale_strings + get_locale_data( locale_table, entry->idx )->sparent;
132 if (*parent && (entry = find_lcname_entry( locale_table, parent + 1 )))
133 user_resource_neutral_lcid = get_locale_data( locale_table, entry->idx )->unique_lcid;
136 TRACE( "resources: %04lx/%04lx/%04lx\n", user_resource_lcid, user_resource_neutral_lcid, system_lcid );
138 if (!RtlQueryActivationContextApplicationSettings( 0, NULL, L"http://schemas.microsoft.com/SMI/2019/WindowsSettings",
139 L"activeCodePage", locale, ARRAY_SIZE(locale), NULL ))
141 const NLS_LOCALE_LCNAME_INDEX *entry = find_lcname_entry( locale_table, locale );
143 if (!wcsicmp( locale, L"utf-8" ))
145 ansi_cp = oem_cp = CP_UTF8;
147 else if (!wcsicmp( locale, L"legacy" ))
149 if (ansi_cp == CP_UTF8) ansi_cp = 1252;
150 if (oem_cp == CP_UTF8) oem_cp = 437;
152 else if ((entry = find_lcname_entry( locale_table, locale )))
154 ansi_cp = get_locale_data( locale_table, entry->idx )->idefaultansicodepage;
155 oem_cp = get_locale_data( locale_table, entry->idx )->idefaultcodepage;
159 NtGetNlsSectionPtr( 10, 0, NULL, &case_ptr, &size );
160 NtCurrentTeb()->Peb->UnicodeCaseTableData = case_ptr;
161 if (ansi_cp != CP_UTF8)
163 NtGetNlsSectionPtr( 11, ansi_cp, NULL, &ansi_ptr, &size );
164 NtCurrentTeb()->Peb->AnsiCodePageData = ansi_ptr;
166 if (oem_cp != CP_UTF8)
168 NtGetNlsSectionPtr( 11, oem_cp, NULL, &oem_ptr, &size );
169 NtCurrentTeb()->Peb->OemCodePageData = oem_ptr;
171 RtlInitNlsTables( ansi_ptr, oem_ptr, case_ptr, &nls_info );
172 NlsAnsiCodePage = nls_info.AnsiTableInfo.CodePage;
173 NlsMbCodePageTag = nls_info.AnsiTableInfo.DBCSCodePage;
174 NlsMbOemCodePageTag = nls_info.OemTableInfo.DBCSCodePage;
178 /* return LCIDs to use for resource lookup */
179 void get_resource_lcids( LANGID *user, LANGID *user_neutral, LANGID *system )
181 *user = LANGIDFROMLCID( user_resource_lcid );
182 *user_neutral = LANGIDFROMLCID( user_resource_neutral_lcid );
183 *system = LANGIDFROMLCID( system_lcid );
187 static NTSTATUS get_dummy_preferred_ui_language( DWORD flags, LANGID lang, ULONG *count,
188 WCHAR *buffer, ULONG *size )
190 WCHAR name[LOCALE_NAME_MAX_LENGTH + 2];
191 NTSTATUS status;
192 ULONG len;
194 FIXME("(0x%lx %#x %p %p %p) returning a dummy value (current locale)\n", flags, lang, count, buffer, size);
196 if (flags & MUI_LANGUAGE_ID) swprintf( name, ARRAY_SIZE(name), L"%04lX", lang );
197 else
199 UNICODE_STRING str;
201 if (lang == LOCALE_CUSTOM_UNSPECIFIED)
202 NtQueryInstallUILanguage( &lang );
204 str.Buffer = name;
205 str.MaximumLength = sizeof(name);
206 status = RtlLcidToLocaleName( lang, &str, 0, FALSE );
207 if (status) return status;
210 len = wcslen( name ) + 2;
211 name[len - 1] = 0;
212 if (buffer)
214 if (len > *size)
216 *size = len;
217 return STATUS_BUFFER_TOO_SMALL;
219 memcpy( buffer, name, len * sizeof(WCHAR) );
221 *size = len;
222 *count = 1;
223 TRACE("returned variable content: %ld, \"%s\", %ld\n", *count, debugstr_w(buffer), *size);
224 return STATUS_SUCCESS;
228 /**************************************************************************
229 * RtlGetProcessPreferredUILanguages (NTDLL.@)
231 NTSTATUS WINAPI RtlGetProcessPreferredUILanguages( DWORD flags, ULONG *count, WCHAR *buffer, ULONG *size )
233 LANGID ui_language;
235 FIXME( "%08lx, %p, %p %p\n", flags, count, buffer, size );
237 NtQueryDefaultUILanguage( &ui_language );
238 return get_dummy_preferred_ui_language( flags, ui_language, count, buffer, size );
242 /**************************************************************************
243 * RtlGetSystemPreferredUILanguages (NTDLL.@)
245 NTSTATUS WINAPI RtlGetSystemPreferredUILanguages( DWORD flags, ULONG unknown, ULONG *count,
246 WCHAR *buffer, ULONG *size )
248 LANGID ui_language;
250 if (flags & ~(MUI_LANGUAGE_NAME | MUI_LANGUAGE_ID | MUI_MACHINE_LANGUAGE_SETTINGS)) return STATUS_INVALID_PARAMETER;
251 if ((flags & MUI_LANGUAGE_NAME) && (flags & MUI_LANGUAGE_ID)) return STATUS_INVALID_PARAMETER;
252 if (*size && !buffer) return STATUS_INVALID_PARAMETER;
254 NtQueryInstallUILanguage( &ui_language );
255 return get_dummy_preferred_ui_language( flags, ui_language, count, buffer, size );
259 /**************************************************************************
260 * RtlGetThreadPreferredUILanguages (NTDLL.@)
262 NTSTATUS WINAPI RtlGetThreadPreferredUILanguages( DWORD flags, ULONG *count, WCHAR *buffer, ULONG *size )
264 LANGID ui_language;
266 FIXME( "%08lx, %p, %p %p\n", flags, count, buffer, size );
268 NtQueryDefaultUILanguage( &ui_language );
269 return get_dummy_preferred_ui_language( flags, ui_language, count, buffer, size );
273 /**************************************************************************
274 * RtlGetUserPreferredUILanguages (NTDLL.@)
276 NTSTATUS WINAPI RtlGetUserPreferredUILanguages( DWORD flags, ULONG unknown, ULONG *count,
277 WCHAR *buffer, ULONG *size )
279 LANGID ui_language;
281 if (flags & ~(MUI_LANGUAGE_NAME | MUI_LANGUAGE_ID)) return STATUS_INVALID_PARAMETER;
282 if ((flags & MUI_LANGUAGE_NAME) && (flags & MUI_LANGUAGE_ID)) return STATUS_INVALID_PARAMETER;
283 if (*size && !buffer) return STATUS_INVALID_PARAMETER;
285 NtQueryDefaultUILanguage( &ui_language );
286 return get_dummy_preferred_ui_language( flags, ui_language, count, buffer, size );
290 /**************************************************************************
291 * RtlSetProcessPreferredUILanguages (NTDLL.@)
293 NTSTATUS WINAPI RtlSetProcessPreferredUILanguages( DWORD flags, PCZZWSTR buffer, ULONG *count )
295 FIXME( "%lu, %p, %p\n", flags, buffer, count );
296 return STATUS_SUCCESS;
300 /**************************************************************************
301 * RtlSetThreadPreferredUILanguages (NTDLL.@)
303 NTSTATUS WINAPI RtlSetThreadPreferredUILanguages( DWORD flags, PCZZWSTR buffer, ULONG *count )
305 FIXME( "%lu, %p, %p\n", flags, buffer, count );
306 return STATUS_SUCCESS;
310 /******************************************************************
311 * RtlInitCodePageTable (NTDLL.@)
313 void WINAPI RtlInitCodePageTable( USHORT *ptr, CPTABLEINFO *info )
315 static const CPTABLEINFO utf8_cpinfo = { CP_UTF8, 4, '?', 0xfffd, '?', '?' };
317 if (ptr[1] == CP_UTF8) *info = utf8_cpinfo;
318 else init_codepage_table( ptr, info );
322 /**************************************************************************
323 * RtlInitNlsTables (NTDLL.@)
325 void WINAPI RtlInitNlsTables( USHORT *ansi, USHORT *oem, USHORT *casetable, NLSTABLEINFO *info )
327 RtlInitCodePageTable( ansi, &info->AnsiTableInfo );
328 RtlInitCodePageTable( oem, &info->OemTableInfo );
329 info->UpperCaseTable = casetable + 2;
330 info->LowerCaseTable = casetable + casetable[1] + 2;
334 /**************************************************************************
335 * RtlResetRtlTranslations (NTDLL.@)
337 void WINAPI RtlResetRtlTranslations( const NLSTABLEINFO *info )
339 NlsAnsiCodePage = info->AnsiTableInfo.CodePage;
340 NlsMbCodePageTag = info->AnsiTableInfo.DBCSCodePage;
341 NlsMbOemCodePageTag = info->OemTableInfo.DBCSCodePage;
342 nls_info = *info;
346 /**************************************************************************
347 * RtlGetLocaleFileMappingAddress (NTDLL.@)
349 NTSTATUS WINAPI RtlGetLocaleFileMappingAddress( void **ptr, LCID *lcid, LARGE_INTEGER *size )
351 static void *cached_ptr;
352 static LCID cached_lcid;
354 if (!cached_ptr)
356 void *addr;
357 NTSTATUS status = NtInitializeNlsFiles( &addr, &cached_lcid, size );
359 if (status) return status;
360 if (InterlockedCompareExchangePointer( &cached_ptr, addr, NULL ))
361 NtUnmapViewOfSection( GetCurrentProcess(), addr );
363 *ptr = cached_ptr;
364 *lcid = cached_lcid;
365 return STATUS_SUCCESS;
369 /**************************************************************************
370 * RtlAnsiCharToUnicodeChar (NTDLL.@)
372 WCHAR WINAPI RtlAnsiCharToUnicodeChar( char **ansi )
374 unsigned char ch = *(*ansi)++;
376 if (nls_info.AnsiTableInfo.CodePage == CP_UTF8)
378 unsigned int res;
380 if (ch < 0x80) return ch;
381 if ((res = decode_utf8_char( ch, (const char **)ansi, *ansi + 3 )) > 0x10ffff) res = 0xfffd;
382 return res;
384 if (nls_info.AnsiTableInfo.DBCSOffsets)
386 USHORT off = nls_info.AnsiTableInfo.DBCSOffsets[ch];
387 if (off) return nls_info.AnsiTableInfo.DBCSOffsets[off + (unsigned char)*(*ansi)++];
389 return nls_info.AnsiTableInfo.MultiByteTable[ch];
393 /******************************************************************************
394 * RtlCompareUnicodeStrings (NTDLL.@)
396 LONG WINAPI RtlCompareUnicodeStrings( const WCHAR *s1, SIZE_T len1, const WCHAR *s2, SIZE_T len2,
397 BOOLEAN case_insensitive )
399 LONG ret = 0;
400 SIZE_T len = min( len1, len2 );
402 if (case_insensitive)
404 if (nls_info.UpperCaseTable)
406 while (!ret && len--) ret = casemap( nls_info.UpperCaseTable, *s1++ ) -
407 casemap( nls_info.UpperCaseTable, *s2++ );
409 else /* locale not setup yet */
411 while (!ret && len--) ret = casemap_ascii( *s1++ ) - casemap_ascii( *s2++ );
414 else
416 while (!ret && len--) ret = *s1++ - *s2++;
418 if (!ret) ret = len1 - len2;
419 return ret;
423 /**************************************************************************
424 * RtlPrefixUnicodeString (NTDLL.@)
426 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
427 BOOLEAN ignore_case )
429 unsigned int i;
431 if (s1->Length > s2->Length) return FALSE;
432 if (ignore_case)
434 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
435 if (casemap( nls_info.UpperCaseTable, s1->Buffer[i] ) !=
436 casemap( nls_info.UpperCaseTable, s2->Buffer[i] )) return FALSE;
438 else
440 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
441 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
443 return TRUE;
448 /******************************************************************************
449 * RtlHashUnicodeString (NTDLL.@)
451 NTSTATUS WINAPI RtlHashUnicodeString( const UNICODE_STRING *string, BOOLEAN case_insensitive,
452 ULONG alg, ULONG *hash )
454 unsigned int i;
456 if (!string || !hash) return STATUS_INVALID_PARAMETER;
458 switch (alg)
460 case HASH_STRING_ALGORITHM_DEFAULT:
461 case HASH_STRING_ALGORITHM_X65599:
462 break;
463 default:
464 return STATUS_INVALID_PARAMETER;
467 *hash = 0;
468 if (!case_insensitive)
469 for (i = 0; i < string->Length / sizeof(WCHAR); i++)
470 *hash = *hash * 65599 + string->Buffer[i];
471 else if (nls_info.UpperCaseTable)
472 for (i = 0; i < string->Length / sizeof(WCHAR); i++)
473 *hash = *hash * 65599 + casemap( nls_info.UpperCaseTable, string->Buffer[i] );
474 else /* locale not setup yet */
475 for (i = 0; i < string->Length / sizeof(WCHAR); i++)
476 *hash = *hash * 65599 + casemap_ascii( string->Buffer[i] );
477 return STATUS_SUCCESS;
481 /**************************************************************************
482 * RtlCustomCPToUnicodeN (NTDLL.@)
484 NTSTATUS WINAPI RtlCustomCPToUnicodeN( CPTABLEINFO *info, WCHAR *dst, DWORD dstlen, DWORD *reslen,
485 const char *src, DWORD srclen )
487 unsigned int ret = cp_mbstowcs( info, dst, dstlen / sizeof(WCHAR), src, srclen );
488 if (reslen) *reslen = ret * sizeof(WCHAR);
489 return STATUS_SUCCESS;
493 /**************************************************************************
494 * RtlUnicodeToCustomCPN (NTDLL.@)
496 NTSTATUS WINAPI RtlUnicodeToCustomCPN( CPTABLEINFO *info, char *dst, DWORD dstlen, DWORD *reslen,
497 const WCHAR *src, DWORD srclen )
499 unsigned int ret = cp_wcstombs( info, dst, dstlen, src, srclen / sizeof(WCHAR) );
500 if (reslen) *reslen = ret;
501 return STATUS_SUCCESS;
505 /**************************************************************************
506 * RtlMultiByteToUnicodeN (NTDLL.@)
508 NTSTATUS WINAPI RtlMultiByteToUnicodeN( WCHAR *dst, DWORD dstlen, DWORD *reslen,
509 const char *src, DWORD srclen )
511 unsigned int ret;
513 if (nls_info.AnsiTableInfo.CodePage != CP_UTF8)
514 ret = cp_mbstowcs( &nls_info.AnsiTableInfo, dst, dstlen / sizeof(WCHAR), src, srclen );
515 else
516 utf8_mbstowcs( dst, dstlen / sizeof(WCHAR), &ret, src, srclen );
518 if (reslen) *reslen = ret * sizeof(WCHAR);
519 return STATUS_SUCCESS;
523 /**************************************************************************
524 * RtlMultiByteToUnicodeSize (NTDLL.@)
526 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, const char *str, DWORD len )
528 unsigned int ret;
530 if (nls_info.AnsiTableInfo.CodePage != CP_UTF8)
531 ret = cp_mbstowcs_size( &nls_info.AnsiTableInfo, str, len );
532 else
533 utf8_mbstowcs_size( str, len, &ret );
535 *size = ret * sizeof(WCHAR);
536 return STATUS_SUCCESS;
540 /**************************************************************************
541 * RtlOemToUnicodeN (NTDLL.@)
543 NTSTATUS WINAPI RtlOemToUnicodeN( WCHAR *dst, DWORD dstlen, DWORD *reslen,
544 const char *src, DWORD srclen )
546 unsigned int ret;
548 if (nls_info.OemTableInfo.CodePage != CP_UTF8)
549 ret = cp_mbstowcs( &nls_info.OemTableInfo, dst, dstlen / sizeof(WCHAR), src, srclen );
550 else
551 utf8_mbstowcs( dst, dstlen / sizeof(WCHAR), &ret, src, srclen );
553 if (reslen) *reslen = ret * sizeof(WCHAR);
554 return STATUS_SUCCESS;
558 /**************************************************************************
559 * RtlOemStringToUnicodeSize (NTDLL.@)
560 * RtlxOemStringToUnicodeSize (NTDLL.@)
562 DWORD WINAPI RtlOemStringToUnicodeSize( const STRING *str )
564 unsigned int ret;
566 if (nls_info.OemTableInfo.CodePage != CP_UTF8)
567 ret = cp_mbstowcs_size( &nls_info.OemTableInfo, str->Buffer, str->Length );
568 else
569 utf8_mbstowcs_size( str->Buffer, str->Length, &ret );
571 return (ret + 1) * sizeof(WCHAR);
575 /**************************************************************************
576 * RtlUnicodeStringToOemSize (NTDLL.@)
577 * RtlxUnicodeStringToOemSize (NTDLL.@)
579 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
581 unsigned int ret;
583 if (nls_info.OemTableInfo.CodePage != CP_UTF8)
584 ret = cp_wcstombs_size( &nls_info.OemTableInfo, str->Buffer, str->Length / sizeof(WCHAR) );
585 else
586 utf8_wcstombs_size( str->Buffer, str->Length / sizeof(WCHAR), &ret );
588 return ret + 1;
592 /**************************************************************************
593 * RtlUnicodeToMultiByteN (NTDLL.@)
595 NTSTATUS WINAPI RtlUnicodeToMultiByteN( char *dst, DWORD dstlen, DWORD *reslen,
596 const WCHAR *src, DWORD srclen )
598 unsigned int ret;
600 if (nls_info.AnsiTableInfo.CodePage != CP_UTF8)
601 ret = cp_wcstombs( &nls_info.AnsiTableInfo, dst, dstlen, src, srclen / sizeof(WCHAR) );
602 else
603 utf8_wcstombs( dst, dstlen, &ret, src, srclen / sizeof(WCHAR) );
605 if (reslen) *reslen = ret;
606 return STATUS_SUCCESS;
610 /**************************************************************************
611 * RtlUnicodeToMultiByteSize (NTDLL.@)
613 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( DWORD *size, const WCHAR *str, DWORD len )
615 unsigned int ret;
617 if (nls_info.AnsiTableInfo.CodePage != CP_UTF8)
618 ret = cp_wcstombs_size( &nls_info.AnsiTableInfo, str, len / sizeof(WCHAR) );
619 else
620 utf8_wcstombs_size( str, len / sizeof(WCHAR), &ret );
622 *size = ret;
623 return STATUS_SUCCESS;
627 /**************************************************************************
628 * RtlUnicodeToOemN (NTDLL.@)
630 NTSTATUS WINAPI RtlUnicodeToOemN( char *dst, DWORD dstlen, DWORD *reslen,
631 const WCHAR *src, DWORD srclen )
633 unsigned int ret;
635 if (nls_info.OemTableInfo.CodePage != CP_UTF8)
636 ret = cp_wcstombs( &nls_info.OemTableInfo, dst, dstlen, src, srclen / sizeof(WCHAR) );
637 else
638 utf8_wcstombs( dst, dstlen, &ret, src, srclen / sizeof(WCHAR) );
640 if (reslen) *reslen = ret;
641 return STATUS_SUCCESS;
645 /**************************************************************************
646 * RtlDowncaseUnicodeChar (NTDLL.@)
648 WCHAR WINAPI RtlDowncaseUnicodeChar( WCHAR wch )
650 if (nls_info.LowerCaseTable) return casemap( nls_info.LowerCaseTable, wch );
651 if (wch >= 'A' && wch <= 'Z') wch += 'a' - 'A';
652 return wch;
656 /**************************************************************************
657 * RtlDowncaseUnicodeString (NTDLL.@)
659 NTSTATUS WINAPI RtlDowncaseUnicodeString( UNICODE_STRING *dest, const UNICODE_STRING *src,
660 BOOLEAN alloc )
662 DWORD i, len = src->Length;
664 if (alloc)
666 dest->MaximumLength = len;
667 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return STATUS_NO_MEMORY;
669 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
671 for (i = 0; i < len / sizeof(WCHAR); i++)
672 dest->Buffer[i] = casemap( nls_info.LowerCaseTable, src->Buffer[i] );
673 dest->Length = len;
674 return STATUS_SUCCESS;
678 /**************************************************************************
679 * RtlUpcaseUnicodeChar (NTDLL.@)
681 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
683 return casemap( nls_info.UpperCaseTable, wch );
687 /**************************************************************************
688 * RtlUpcaseUnicodeString (NTDLL.@)
690 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest, const UNICODE_STRING *src,
691 BOOLEAN alloc )
693 DWORD i, len = src->Length;
695 if (alloc)
697 dest->MaximumLength = len;
698 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return STATUS_NO_MEMORY;
700 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
702 for (i = 0; i < len / sizeof(WCHAR); i++)
703 dest->Buffer[i] = casemap( nls_info.UpperCaseTable, src->Buffer[i] );
704 dest->Length = len;
705 return STATUS_SUCCESS;
709 /**************************************************************************
710 * RtlUpcaseUnicodeToCustomCPN (NTDLL.@)
712 NTSTATUS WINAPI RtlUpcaseUnicodeToCustomCPN( CPTABLEINFO *info, char *dst, DWORD dstlen, DWORD *reslen,
713 const WCHAR *src, DWORD srclen )
715 DWORD i, ret;
717 srclen /= sizeof(WCHAR);
718 if (info->DBCSCodePage)
720 WCHAR *uni2cp = info->WideCharTable;
722 for (i = dstlen; srclen && i; i--, srclen--, src++)
724 WCHAR ch = casemap( nls_info.UpperCaseTable, *src );
725 if (uni2cp[ch] & 0xff00)
727 if (i == 1) break; /* do not output a partial char */
728 i--;
729 *dst++ = uni2cp[ch] >> 8;
731 *dst++ = (char)uni2cp[ch];
733 ret = dstlen - i;
735 else
737 char *uni2cp = info->WideCharTable;
738 ret = min( srclen, dstlen );
739 for (i = 0; i < ret; i++) dst[i] = uni2cp[casemap( nls_info.UpperCaseTable, src[i] )];
741 if (reslen) *reslen = ret;
742 return STATUS_SUCCESS;
746 static NTSTATUS upcase_unicode_to_utf8( char *dst, DWORD dstlen, DWORD *reslen,
747 const WCHAR *src, DWORD srclen )
749 char *end;
750 unsigned int val;
751 NTSTATUS status = STATUS_SUCCESS;
753 srclen /= sizeof(WCHAR);
755 for (end = dst + dstlen; srclen; srclen--, src++)
757 WCHAR ch = casemap( nls_info.UpperCaseTable, *src );
759 if (ch < 0x80) /* 0x00-0x7f: 1 byte */
761 if (dst > end - 1) break;
762 *dst++ = ch;
763 continue;
765 if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
767 if (dst > end - 2) break;
768 dst[1] = 0x80 | (ch & 0x3f);
769 ch >>= 6;
770 dst[0] = 0xc0 | ch;
771 dst += 2;
772 continue;
774 if (!get_utf16( src, srclen, &val ))
776 val = 0xfffd;
777 status = STATUS_SOME_NOT_MAPPED;
779 if (val < 0x10000) /* 0x800-0xffff: 3 bytes */
781 if (dst > end - 3) break;
782 dst[2] = 0x80 | (val & 0x3f);
783 val >>= 6;
784 dst[1] = 0x80 | (val & 0x3f);
785 val >>= 6;
786 dst[0] = 0xe0 | val;
787 dst += 3;
789 else /* 0x10000-0x10ffff: 4 bytes */
791 if (dst > end - 4) break;
792 dst[3] = 0x80 | (val & 0x3f);
793 val >>= 6;
794 dst[2] = 0x80 | (val & 0x3f);
795 val >>= 6;
796 dst[1] = 0x80 | (val & 0x3f);
797 val >>= 6;
798 dst[0] = 0xf0 | val;
799 dst += 4;
800 src++;
801 srclen--;
804 if (srclen) status = STATUS_BUFFER_TOO_SMALL;
805 if (reslen) *reslen = dstlen - (end - dst);
806 return status;
809 /**************************************************************************
810 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
812 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( char *dst, DWORD dstlen, DWORD *reslen,
813 const WCHAR *src, DWORD srclen )
815 if (nls_info.AnsiTableInfo.CodePage == CP_UTF8)
816 return upcase_unicode_to_utf8( dst, dstlen, reslen, src, srclen );
817 return RtlUpcaseUnicodeToCustomCPN( &nls_info.AnsiTableInfo, dst, dstlen, reslen, src, srclen );
821 /**************************************************************************
822 * RtlUpcaseUnicodeToOemN (NTDLL.@)
824 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( char *dst, DWORD dstlen, DWORD *reslen,
825 const WCHAR *src, DWORD srclen )
827 if (nls_info.OemTableInfo.CodePage == CP_UTF8)
828 return upcase_unicode_to_utf8( dst, dstlen, reslen, src, srclen );
829 return RtlUpcaseUnicodeToCustomCPN( &nls_info.OemTableInfo, dst, dstlen, reslen, src, srclen );
833 /*********************************************************************
834 * towlower (NTDLL.@)
836 WCHAR __cdecl towlower( WCHAR ch )
838 if (ch >= 0x100) return ch;
839 return casemap( nls_info.LowerCaseTable, ch );
843 /*********************************************************************
844 * towupper (NTDLL.@)
846 WCHAR __cdecl towupper( WCHAR ch )
848 if (nls_info.UpperCaseTable) return casemap( nls_info.UpperCaseTable, ch );
849 return casemap_ascii( ch );
853 /******************************************************************
854 * RtlIsValidLocaleName (NTDLL.@)
856 BOOLEAN WINAPI RtlIsValidLocaleName( const WCHAR *name, ULONG flags )
858 const NLS_LOCALE_LCNAME_INDEX *entry = find_lcname_entry( locale_table, name );
860 if (!entry) return FALSE;
861 /* reject neutral locale unless flag 2 is set */
862 if (!(flags & 2) && !get_locale_data( locale_table, entry->idx )->inotneutral) return FALSE;
863 return TRUE;
867 /******************************************************************
868 * RtlLcidToLocaleName (NTDLL.@)
870 NTSTATUS WINAPI RtlLcidToLocaleName( LCID lcid, UNICODE_STRING *str, ULONG flags, BOOLEAN alloc )
872 const NLS_LOCALE_LCID_INDEX *entry;
873 const WCHAR *name;
874 ULONG len;
876 if (!str) return STATUS_INVALID_PARAMETER_2;
878 switch (lcid)
880 case LOCALE_USER_DEFAULT:
881 NtQueryDefaultLocale( TRUE, &lcid );
882 break;
883 case LOCALE_SYSTEM_DEFAULT:
884 case LOCALE_CUSTOM_DEFAULT:
885 lcid = system_lcid;
886 break;
887 case LOCALE_CUSTOM_UI_DEFAULT:
888 return STATUS_UNSUCCESSFUL;
889 case LOCALE_CUSTOM_UNSPECIFIED:
890 return STATUS_INVALID_PARAMETER_1;
893 if (!(entry = find_lcid_entry( locale_table, lcid ))) return STATUS_INVALID_PARAMETER_1;
894 /* reject neutral locale unless flag 2 is set */
895 if (!(flags & 2) && !get_locale_data( locale_table, entry->idx )->inotneutral)
896 return STATUS_INVALID_PARAMETER_1;
898 name = locale_strings + entry->name;
899 len = *name++;
901 if (alloc)
903 if (!(str->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) )))
904 return STATUS_NO_MEMORY;
905 str->MaximumLength = (len + 1) * sizeof(WCHAR);
907 else if (str->MaximumLength < (len + 1) * sizeof(WCHAR)) return STATUS_BUFFER_TOO_SMALL;
909 wcscpy( str->Buffer, name );
910 str->Length = len * sizeof(WCHAR);
911 TRACE( "%04lx -> %s\n", lcid, debugstr_us(str) );
912 return STATUS_SUCCESS;
916 /******************************************************************
917 * RtlLocaleNameToLcid (NTDLL.@)
919 NTSTATUS WINAPI RtlLocaleNameToLcid( const WCHAR *name, LCID *lcid, ULONG flags )
921 const NLS_LOCALE_LCNAME_INDEX *entry = find_lcname_entry( locale_table, name );
923 if (!entry) return STATUS_INVALID_PARAMETER_1;
924 /* reject neutral locale unless flag 2 is set */
925 if (!(flags & 2) && !get_locale_data( locale_table, entry->idx )->inotneutral)
926 return STATUS_INVALID_PARAMETER_1;
927 *lcid = entry->id;
928 TRACE( "%s -> %04lx\n", debugstr_w(name), *lcid );
929 return STATUS_SUCCESS;
933 /**************************************************************************
934 * RtlUTF8ToUnicodeN (NTDLL.@)
936 NTSTATUS WINAPI RtlUTF8ToUnicodeN( WCHAR *dst, DWORD dstlen, DWORD *reslen, const char *src, DWORD srclen )
938 unsigned int ret;
939 NTSTATUS status;
941 if (!src) return STATUS_INVALID_PARAMETER_4;
942 if (!reslen) return STATUS_INVALID_PARAMETER;
944 if (!dst)
945 status = utf8_mbstowcs_size( src, srclen, &ret );
946 else
947 status = utf8_mbstowcs( dst, dstlen / sizeof(WCHAR), &ret, src, srclen );
949 *reslen = ret * sizeof(WCHAR);
950 return status;
954 /**************************************************************************
955 * RtlUnicodeToUTF8N (NTDLL.@)
957 NTSTATUS WINAPI RtlUnicodeToUTF8N( char *dst, DWORD dstlen, DWORD *reslen, const WCHAR *src, DWORD srclen )
959 unsigned int ret;
960 NTSTATUS status;
962 if (!src) return STATUS_INVALID_PARAMETER_4;
963 if (!reslen) return STATUS_INVALID_PARAMETER;
964 if (dst && (srclen & 1)) return STATUS_INVALID_PARAMETER_5;
966 if (!dst)
967 status = utf8_wcstombs_size( src, srclen / sizeof(WCHAR), &ret );
968 else
969 status = utf8_wcstombs( dst, dstlen, &ret, src, srclen / sizeof(WCHAR) );
971 *reslen = ret;
972 return status;
976 /******************************************************************************
977 * RtlIsNormalizedString (NTDLL.@)
979 NTSTATUS WINAPI RtlIsNormalizedString( ULONG form, const WCHAR *str, INT len, BOOLEAN *res )
981 const struct norm_table *info;
982 NTSTATUS status;
983 BYTE props, class, last_class = 0;
984 unsigned int ch;
985 int i, r, result = 1;
987 if ((status = load_norm_table( form, &info ))) return status;
989 if (len == -1) len = wcslen( str );
991 for (i = 0; i < len && result; i += r)
993 if (!(r = get_utf16( str + i, len - i, &ch ))) return STATUS_NO_UNICODE_TRANSLATION;
994 if (info->comp_size)
996 if ((ch >= HANGUL_VBASE && ch < HANGUL_VBASE + HANGUL_VCOUNT) ||
997 (ch >= HANGUL_TBASE && ch < HANGUL_TBASE + HANGUL_TCOUNT))
999 result = -1; /* QC=Maybe */
1000 continue;
1003 else if (ch >= HANGUL_SBASE && ch < HANGUL_SBASE + HANGUL_SCOUNT)
1005 result = 0; /* QC=No */
1006 break;
1008 props = get_char_props( info, ch );
1009 class = props & 0x3f;
1010 if (class == 0x3f)
1012 last_class = 0;
1013 if (props == 0xbf) result = 0; /* QC=No */
1014 else if (props == 0xff)
1016 /* ignore other chars in Hangul range */
1017 if (ch >= HANGUL_LBASE && ch < HANGUL_LBASE + 0x100) continue;
1018 if (ch >= HANGUL_SBASE && ch < HANGUL_SBASE + 0x2c00) continue;
1019 /* allow final null */
1020 if (!ch && i == len - 1) continue;
1021 return STATUS_NO_UNICODE_TRANSLATION;
1024 else if (props & 0x80)
1026 if ((props & 0xc0) == 0xc0) result = -1; /* QC=Maybe */
1027 if (class && class < last_class) result = 0; /* QC=No */
1028 last_class = class;
1030 else last_class = 0;
1033 if (result == -1)
1035 int dstlen = len * 4;
1036 NTSTATUS status;
1037 WCHAR *buffer = RtlAllocateHeap( GetProcessHeap(), 0, dstlen * sizeof(WCHAR) );
1038 if (!buffer) return STATUS_NO_MEMORY;
1039 status = RtlNormalizeString( form, str, len, buffer, &dstlen );
1040 result = !status && (dstlen == len) && !wcsncmp( buffer, str, len );
1041 RtlFreeHeap( GetProcessHeap(), 0, buffer );
1043 *res = result;
1044 return STATUS_SUCCESS;
1048 /******************************************************************************
1049 * RtlNormalizeString (NTDLL.@)
1051 NTSTATUS WINAPI RtlNormalizeString( ULONG form, const WCHAR *src, INT src_len, WCHAR *dst, INT *dst_len )
1053 int buf_len;
1054 WCHAR *buf = NULL;
1055 const struct norm_table *info;
1056 NTSTATUS status = STATUS_SUCCESS;
1058 TRACE( "%lx %s %d %p %d\n", form, debugstr_wn(src, src_len), src_len, dst, *dst_len );
1060 if ((status = load_norm_table( form, &info ))) return status;
1062 if (src_len == -1) src_len = wcslen(src) + 1;
1064 if (!*dst_len)
1066 *dst_len = src_len * info->len_factor;
1067 if (*dst_len > 64) *dst_len = max( 64, src_len + src_len / 8 );
1068 return STATUS_SUCCESS;
1070 if (!src_len)
1072 *dst_len = 0;
1073 return STATUS_SUCCESS;
1076 if (!info->comp_size) return decompose_string( info, src, src_len, dst, dst_len );
1078 buf_len = src_len * 4;
1079 for (;;)
1081 buf = RtlAllocateHeap( GetProcessHeap(), 0, buf_len * sizeof(WCHAR) );
1082 if (!buf) return STATUS_NO_MEMORY;
1083 status = decompose_string( info, src, src_len, buf, &buf_len );
1084 if (status != STATUS_BUFFER_TOO_SMALL) break;
1085 RtlFreeHeap( GetProcessHeap(), 0, buf );
1087 if (!status)
1089 buf_len = compose_string( info, buf, buf_len );
1090 if (*dst_len >= buf_len) memcpy( dst, buf, buf_len * sizeof(WCHAR) );
1091 else status = STATUS_BUFFER_TOO_SMALL;
1093 RtlFreeHeap( GetProcessHeap(), 0, buf );
1094 *dst_len = buf_len;
1095 return status;
1099 /* Punycode parameters */
1100 enum { BASE = 36, TMIN = 1, TMAX = 26, SKEW = 38, DAMP = 700 };
1102 static BOOL check_invalid_chars( const struct norm_table *info, DWORD flags,
1103 const unsigned int *buffer, int len )
1105 int i;
1107 for (i = 0; i < len; i++)
1109 switch (buffer[i])
1111 case 0x200c: /* zero-width non-joiner */
1112 case 0x200d: /* zero-width joiner */
1113 if (!i || get_combining_class( info, buffer[i - 1] ) != 9) return TRUE;
1114 break;
1115 case 0x2260: /* not equal to */
1116 case 0x226e: /* not less than */
1117 case 0x226f: /* not greater than */
1118 if (flags & IDN_USE_STD3_ASCII_RULES) return TRUE;
1119 break;
1121 switch (get_char_props( info, buffer[i] ))
1123 case 0xbf:
1124 return TRUE;
1125 case 0xff:
1126 if (buffer[i] >= HANGUL_SBASE && buffer[i] < HANGUL_SBASE + 0x2c00) break;
1127 return TRUE;
1128 case 0x7f:
1129 if (!(flags & IDN_ALLOW_UNASSIGNED)) return TRUE;
1130 break;
1134 if ((flags & IDN_USE_STD3_ASCII_RULES) && len && (buffer[0] == '-' || buffer[len - 1] == '-'))
1135 return TRUE;
1137 return FALSE;
1141 /******************************************************************************
1142 * RtlIdnToAscii (NTDLL.@)
1144 NTSTATUS WINAPI RtlIdnToAscii( DWORD flags, const WCHAR *src, INT srclen, WCHAR *dst, INT *dstlen )
1146 static const WCHAR prefixW[] = {'x','n','-','-'};
1147 const struct norm_table *info;
1148 NTSTATUS status;
1149 WCHAR normstr[256], res[256];
1150 unsigned int ch, buffer[64];
1151 int i, len, start, end, out_label, out = 0, normlen = ARRAY_SIZE(normstr);
1153 TRACE( "%lx %s %p %d\n", flags, debugstr_wn(src, srclen), dst, *dstlen );
1155 if ((status = load_norm_table( 13, &info ))) return status;
1157 if ((status = RtlIdnToNameprepUnicode( flags, src, srclen, normstr, &normlen ))) return status;
1159 /* implementation of Punycode based on RFC 3492 */
1161 for (start = 0; start < normlen; start = end + 1)
1163 int n = 0x80, bias = 72, delta = 0, b = 0, h, buflen = 0;
1165 out_label = out;
1166 for (i = start; i < normlen; i += len)
1168 if (!(len = get_utf16( normstr + i, normlen - i, &ch ))) break;
1169 if (!ch || ch == '.') break;
1170 if (ch < 0x80) b++;
1171 buffer[buflen++] = ch;
1173 end = i;
1175 if (b == end - start)
1177 if (end < normlen) b++;
1178 if (out + b > ARRAY_SIZE(res)) return STATUS_INVALID_IDN_NORMALIZATION;
1179 memcpy( res + out, normstr + start, b * sizeof(WCHAR) );
1180 out += b;
1181 continue;
1184 if (buflen >= 4 && buffer[2] == '-' && buffer[3] == '-') return STATUS_INVALID_IDN_NORMALIZATION;
1185 if (check_invalid_chars( info, flags, buffer, buflen )) return STATUS_INVALID_IDN_NORMALIZATION;
1187 if (out + 5 + b > ARRAY_SIZE(res)) return STATUS_INVALID_IDN_NORMALIZATION;
1188 memcpy( res + out, prefixW, sizeof(prefixW) );
1189 out += ARRAY_SIZE(prefixW);
1190 if (b)
1192 for (i = start; i < end; i++) if (normstr[i] < 0x80) res[out++] = normstr[i];
1193 res[out++] = '-';
1196 for (h = b; h < buflen; delta++, n++)
1198 int m = 0x10ffff, q, k;
1200 for (i = 0; i < buflen; i++) if (buffer[i] >= n && m > buffer[i]) m = buffer[i];
1201 delta += (m - n) * (h + 1);
1202 n = m;
1204 for (i = 0; i < buflen; i++)
1206 if (buffer[i] == n)
1208 for (q = delta, k = BASE; ; k += BASE)
1210 int t = k <= bias ? TMIN : k >= bias + TMAX ? TMAX : k - bias;
1211 int disp = q < t ? q : t + (q - t) % (BASE - t);
1212 if (out + 1 > ARRAY_SIZE(res)) return STATUS_INVALID_IDN_NORMALIZATION;
1213 res[out++] = disp <= 25 ? 'a' + disp : '0' + disp - 26;
1214 if (q < t) break;
1215 q = (q - t) / (BASE - t);
1217 delta /= (h == b ? DAMP : 2);
1218 delta += delta / (h + 1);
1219 for (k = 0; delta > ((BASE - TMIN) * TMAX) / 2; k += BASE) delta /= BASE - TMIN;
1220 bias = k + ((BASE - TMIN + 1) * delta) / (delta + SKEW);
1221 delta = 0;
1222 h++;
1224 else if (buffer[i] < n) delta++;
1228 if (out - out_label > 63) return STATUS_INVALID_IDN_NORMALIZATION;
1230 if (end < normlen)
1232 if (out + 1 > ARRAY_SIZE(res)) return STATUS_INVALID_IDN_NORMALIZATION;
1233 res[out++] = normstr[end];
1237 if (*dstlen)
1239 if (out <= *dstlen) memcpy( dst, res, out * sizeof(WCHAR) );
1240 else status = STATUS_BUFFER_TOO_SMALL;
1242 *dstlen = out;
1243 return status;
1247 /******************************************************************************
1248 * RtlIdnToNameprepUnicode (NTDLL.@)
1250 NTSTATUS WINAPI RtlIdnToNameprepUnicode( DWORD flags, const WCHAR *src, INT srclen,
1251 WCHAR *dst, INT *dstlen )
1253 const struct norm_table *info;
1254 unsigned int ch;
1255 NTSTATUS status;
1256 WCHAR buf[256];
1257 int i, start, len, buflen = ARRAY_SIZE(buf);
1259 if (flags & ~(IDN_ALLOW_UNASSIGNED | IDN_USE_STD3_ASCII_RULES)) return STATUS_INVALID_PARAMETER;
1260 if (!src || srclen < -1) return STATUS_INVALID_PARAMETER;
1262 TRACE( "%lx %s %p %d\n", flags, debugstr_wn(src, srclen), dst, *dstlen );
1264 if ((status = load_norm_table( 13, &info ))) return status;
1266 if (srclen == -1) srclen = wcslen(src) + 1;
1268 for (i = 0; i < srclen; i++) if (src[i] < 0x20 || src[i] >= 0x7f) break;
1270 if (i == srclen || (i == srclen - 1 && !src[i])) /* ascii only */
1272 if (srclen > buflen) return STATUS_INVALID_IDN_NORMALIZATION;
1273 memcpy( buf, src, srclen * sizeof(WCHAR) );
1274 buflen = srclen;
1276 else if ((status = RtlNormalizeString( 13, src, srclen, buf, &buflen )))
1278 if (status == STATUS_NO_UNICODE_TRANSLATION) status = STATUS_INVALID_IDN_NORMALIZATION;
1279 return status;
1282 for (i = start = 0; i < buflen; i += len)
1284 if (!(len = get_utf16( buf + i, buflen - i, &ch ))) break;
1285 if (!ch) break;
1286 if (ch == '.')
1288 if (start == i) return STATUS_INVALID_IDN_NORMALIZATION;
1289 /* maximal label length is 63 characters */
1290 if (i - start > 63) return STATUS_INVALID_IDN_NORMALIZATION;
1291 if ((flags & IDN_USE_STD3_ASCII_RULES) && (buf[start] == '-' || buf[i-1] == '-'))
1292 return STATUS_INVALID_IDN_NORMALIZATION;
1293 start = i + 1;
1294 continue;
1296 if (flags & IDN_USE_STD3_ASCII_RULES)
1298 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
1299 (ch >= '0' && ch <= '9') || ch == '-') continue;
1300 return STATUS_INVALID_IDN_NORMALIZATION;
1302 if (!(flags & IDN_ALLOW_UNASSIGNED))
1304 if (get_char_props( info, ch ) == 0x7f) return STATUS_INVALID_IDN_NORMALIZATION;
1307 if (!i || i - start > 63) return STATUS_INVALID_IDN_NORMALIZATION;
1308 if ((flags & IDN_USE_STD3_ASCII_RULES) && (buf[start] == '-' || buf[i-1] == '-'))
1309 return STATUS_INVALID_IDN_NORMALIZATION;
1311 if (*dstlen)
1313 if (buflen <= *dstlen) memcpy( dst, buf, buflen * sizeof(WCHAR) );
1314 else status = STATUS_BUFFER_TOO_SMALL;
1316 *dstlen = buflen;
1317 return status;
1321 /******************************************************************************
1322 * RtlIdnToUnicode (NTDLL.@)
1324 NTSTATUS WINAPI RtlIdnToUnicode( DWORD flags, const WCHAR *src, INT srclen, WCHAR *dst, INT *dstlen )
1326 const struct norm_table *info;
1327 int i, buflen, start, end, out_label, out = 0;
1328 NTSTATUS status;
1329 UINT buffer[64];
1330 WCHAR ch = 0;
1332 if (!src || srclen < -1) return STATUS_INVALID_PARAMETER;
1333 if (srclen == -1) srclen = wcslen( src ) + 1;
1335 TRACE( "%lx %s %p %d\n", flags, debugstr_wn(src, srclen), dst, *dstlen );
1337 if ((status = load_norm_table( 13, &info ))) return status;
1339 for (start = 0; start < srclen; )
1341 int n = 0x80, bias = 72, pos = 0, old_pos, w, k, t, delim = 0, digit, delta;
1343 out_label = out;
1344 for (i = start; i < srclen; i++)
1346 ch = src[i];
1347 if (ch > 0x7f || (i != srclen - 1 && !ch)) return STATUS_INVALID_IDN_NORMALIZATION;
1348 if (!ch || ch == '.') break;
1349 if (ch == '-') delim = i;
1351 if (!(flags & IDN_USE_STD3_ASCII_RULES)) continue;
1352 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
1353 (ch >= '0' && ch <= '9') || ch == '-')
1354 continue;
1355 return STATUS_INVALID_IDN_NORMALIZATION;
1357 end = i;
1359 /* last label may be empty */
1360 if (start == end && ch) return STATUS_INVALID_IDN_NORMALIZATION;
1362 if (end - start < 4 ||
1363 (src[start] != 'x' && src[start] != 'X') ||
1364 (src[start + 1] != 'n' && src[start + 1] != 'N') ||
1365 src[start + 2] != '-' || src[start + 3] != '-')
1367 if (end - start > 63) return STATUS_INVALID_IDN_NORMALIZATION;
1369 if ((flags & IDN_USE_STD3_ASCII_RULES) && (src[start] == '-' || src[end - 1] == '-'))
1370 return STATUS_INVALID_IDN_NORMALIZATION;
1372 if (end < srclen) end++;
1373 if (*dstlen)
1375 if (out + end - start <= *dstlen)
1376 memcpy( dst + out, src + start, (end - start) * sizeof(WCHAR));
1377 else return STATUS_BUFFER_TOO_SMALL;
1379 out += end - start;
1380 start = end;
1381 continue;
1384 if (delim == start + 3) delim++;
1385 buflen = 0;
1386 for (i = start + 4; i < delim && buflen < ARRAY_SIZE(buffer); i++) buffer[buflen++] = src[i];
1387 if (buflen) i++;
1388 while (i < end)
1390 old_pos = pos;
1391 w = 1;
1392 for (k = BASE; ; k += BASE)
1394 if (i >= end) return STATUS_INVALID_IDN_NORMALIZATION;
1395 ch = src[i++];
1396 if (ch >= 'a' && ch <= 'z') digit = ch - 'a';
1397 else if (ch >= 'A' && ch <= 'Z') digit = ch - 'A';
1398 else if (ch >= '0' && ch <= '9') digit = ch - '0' + 26;
1399 else return STATUS_INVALID_IDN_NORMALIZATION;
1400 pos += digit * w;
1401 t = k <= bias ? TMIN : k >= bias + TMAX ? TMAX : k - bias;
1402 if (digit < t) break;
1403 w *= BASE - t;
1406 delta = (pos - old_pos) / (!old_pos ? DAMP : 2);
1407 delta += delta / (buflen + 1);
1408 for (k = 0; delta > ((BASE - TMIN) * TMAX) / 2; k += BASE) delta /= BASE - TMIN;
1409 bias = k + ((BASE - TMIN + 1) * delta) / (delta + SKEW);
1410 n += pos / (buflen + 1);
1411 pos %= buflen + 1;
1413 if (buflen >= ARRAY_SIZE(buffer) - 1) return STATUS_INVALID_IDN_NORMALIZATION;
1414 memmove( buffer + pos + 1, buffer + pos, (buflen - pos) * sizeof(*buffer) );
1415 buffer[pos++] = n;
1416 buflen++;
1419 if (check_invalid_chars( info, flags, buffer, buflen )) return STATUS_INVALID_IDN_NORMALIZATION;
1421 for (i = 0; i < buflen; i++)
1423 int len = 1 + (buffer[i] >= 0x10000);
1424 if (*dstlen)
1426 if (out + len <= *dstlen) put_utf16( dst + out, buffer[i] );
1427 else return STATUS_BUFFER_TOO_SMALL;
1429 out += len;
1432 if (out - out_label > 63) return STATUS_INVALID_IDN_NORMALIZATION;
1434 if (end < srclen)
1436 if (*dstlen)
1438 if (out + 1 <= *dstlen) dst[out] = src[end];
1439 else return STATUS_BUFFER_TOO_SMALL;
1441 out++;
1443 start = end + 1;
1445 *dstlen = out;
1446 return STATUS_SUCCESS;