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
26 #define WIN32_NO_STATUS
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
)
58 USHORT
*data
, *tables
;
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
)))
72 if (size
<= 0x44) goto invalid
;
73 if (data
[0x14] != form
) goto invalid
;
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
;
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
];
100 UINT ansi_cp
= 1252, oem_cp
= 437;
101 void *ansi_ptr
= utf8
, *oem_ptr
= utf8
, *case_ptr
;
103 const struct locale_nls_header
*header
;
105 status
= RtlGetLocaleFileMappingAddress( (void **)&header
, &system_lcid
, &unused
);
108 ERR( "locale init failed %lx\n", status
);
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
;
124 WCHAR bufferW
[LOCALE_NAME_MAX_LENGTH
];
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];
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
);
201 if (lang
== LOCALE_CUSTOM_UNSPECIFIED
)
202 NtQueryInstallUILanguage( &lang
);
205 str
.MaximumLength
= sizeof(name
);
206 status
= RtlLcidToLocaleName( lang
, &str
, 0, FALSE
);
207 if (status
) return status
;
210 len
= wcslen( name
) + 2;
217 return STATUS_BUFFER_TOO_SMALL
;
219 memcpy( buffer
, name
, len
* sizeof(WCHAR
) );
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
)
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
)
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
)
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
)
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
;
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
;
357 NTSTATUS status
= NtInitializeNlsFiles( &addr
, &cached_lcid
, size
);
359 if (status
) return status
;
360 if (InterlockedCompareExchangePointer( &cached_ptr
, addr
, NULL
))
361 NtUnmapViewOfSection( GetCurrentProcess(), addr
);
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
)
380 if (ch
< 0x80) return ch
;
381 if ((res
= decode_utf8_char( ch
, (const char **)ansi
, *ansi
+ 3 )) > 0x10ffff) res
= 0xfffd;
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
)
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
++ );
416 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
418 if (!ret
) ret
= len1
- len2
;
423 /**************************************************************************
424 * RtlPrefixUnicodeString (NTDLL.@)
426 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
427 BOOLEAN ignore_case
)
431 if (s1
->Length
> s2
->Length
) return FALSE
;
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
;
440 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
441 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
448 /******************************************************************************
449 * RtlHashUnicodeString (NTDLL.@)
451 NTSTATUS WINAPI
RtlHashUnicodeString( const UNICODE_STRING
*string
, BOOLEAN case_insensitive
,
452 ULONG alg
, ULONG
*hash
)
456 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
460 case HASH_STRING_ALGORITHM_DEFAULT
:
461 case HASH_STRING_ALGORITHM_X65599
:
464 return STATUS_INVALID_PARAMETER
;
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
)
513 if (nls_info
.AnsiTableInfo
.CodePage
!= CP_UTF8
)
514 ret
= cp_mbstowcs( &nls_info
.AnsiTableInfo
, dst
, dstlen
/ sizeof(WCHAR
), src
, srclen
);
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
)
530 if (nls_info
.AnsiTableInfo
.CodePage
!= CP_UTF8
)
531 ret
= cp_mbstowcs_size( &nls_info
.AnsiTableInfo
, str
, len
);
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
)
548 if (nls_info
.OemTableInfo
.CodePage
!= CP_UTF8
)
549 ret
= cp_mbstowcs( &nls_info
.OemTableInfo
, dst
, dstlen
/ sizeof(WCHAR
), src
, srclen
);
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
)
566 if (nls_info
.OemTableInfo
.CodePage
!= CP_UTF8
)
567 ret
= cp_mbstowcs_size( &nls_info
.OemTableInfo
, str
->Buffer
, str
->Length
);
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
)
583 if (nls_info
.OemTableInfo
.CodePage
!= CP_UTF8
)
584 ret
= cp_wcstombs_size( &nls_info
.OemTableInfo
, str
->Buffer
, str
->Length
/ sizeof(WCHAR
) );
586 utf8_wcstombs_size( str
->Buffer
, str
->Length
/ sizeof(WCHAR
), &ret
);
592 /**************************************************************************
593 * RtlUnicodeToMultiByteN (NTDLL.@)
595 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( char *dst
, DWORD dstlen
, DWORD
*reslen
,
596 const WCHAR
*src
, DWORD srclen
)
600 if (nls_info
.AnsiTableInfo
.CodePage
!= CP_UTF8
)
601 ret
= cp_wcstombs( &nls_info
.AnsiTableInfo
, dst
, dstlen
, src
, srclen
/ sizeof(WCHAR
) );
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
)
617 if (nls_info
.AnsiTableInfo
.CodePage
!= CP_UTF8
)
618 ret
= cp_wcstombs_size( &nls_info
.AnsiTableInfo
, str
, len
/ sizeof(WCHAR
) );
620 utf8_wcstombs_size( str
, len
/ sizeof(WCHAR
), &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
)
635 if (nls_info
.OemTableInfo
.CodePage
!= CP_UTF8
)
636 ret
= cp_wcstombs( &nls_info
.OemTableInfo
, dst
, dstlen
, src
, srclen
/ sizeof(WCHAR
) );
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';
656 /**************************************************************************
657 * RtlDowncaseUnicodeString (NTDLL.@)
659 NTSTATUS WINAPI
RtlDowncaseUnicodeString( UNICODE_STRING
*dest
, const UNICODE_STRING
*src
,
662 DWORD i
, len
= src
->Length
;
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
] );
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
,
693 DWORD i
, len
= src
->Length
;
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
] );
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
)
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 */
729 *dst
++ = uni2cp
[ch
] >> 8;
731 *dst
++ = (char)uni2cp
[ch
];
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
)
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;
765 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
767 if (dst
> end
- 2) break;
768 dst
[1] = 0x80 | (ch
& 0x3f);
774 if (!get_utf16( src
, srclen
, &val
))
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);
784 dst
[1] = 0x80 | (val
& 0x3f);
789 else /* 0x10000-0x10ffff: 4 bytes */
791 if (dst
> end
- 4) break;
792 dst
[3] = 0x80 | (val
& 0x3f);
794 dst
[2] = 0x80 | (val
& 0x3f);
796 dst
[1] = 0x80 | (val
& 0x3f);
804 if (srclen
) status
= STATUS_BUFFER_TOO_SMALL
;
805 if (reslen
) *reslen
= dstlen
- (end
- dst
);
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 /*********************************************************************
836 WCHAR __cdecl
towlower( WCHAR ch
)
838 if (ch
>= 0x100) return ch
;
839 return casemap( nls_info
.LowerCaseTable
, ch
);
843 /*********************************************************************
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
;
867 /******************************************************************
868 * RtlLcidToLocaleName (NTDLL.@)
870 NTSTATUS WINAPI
RtlLcidToLocaleName( LCID lcid
, UNICODE_STRING
*str
, ULONG flags
, BOOLEAN alloc
)
872 const NLS_LOCALE_LCID_INDEX
*entry
;
876 if (!str
) return STATUS_INVALID_PARAMETER_2
;
880 case LOCALE_USER_DEFAULT
:
881 NtQueryDefaultLocale( TRUE
, &lcid
);
883 case LOCALE_SYSTEM_DEFAULT
:
884 case LOCALE_CUSTOM_DEFAULT
:
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
;
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
;
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
)
941 if (!src
) return STATUS_INVALID_PARAMETER_4
;
942 if (!reslen
) return STATUS_INVALID_PARAMETER
;
945 status
= utf8_mbstowcs_size( src
, srclen
, &ret
);
947 status
= utf8_mbstowcs( dst
, dstlen
/ sizeof(WCHAR
), &ret
, src
, srclen
);
949 *reslen
= ret
* sizeof(WCHAR
);
954 /**************************************************************************
955 * RtlUnicodeToUTF8N (NTDLL.@)
957 NTSTATUS WINAPI
RtlUnicodeToUTF8N( char *dst
, DWORD dstlen
, DWORD
*reslen
, const WCHAR
*src
, DWORD srclen
)
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
;
967 status
= utf8_wcstombs_size( src
, srclen
/ sizeof(WCHAR
), &ret
);
969 status
= utf8_wcstombs( dst
, dstlen
, &ret
, src
, srclen
/ sizeof(WCHAR
) );
976 /******************************************************************************
977 * RtlIsNormalizedString (NTDLL.@)
979 NTSTATUS WINAPI
RtlIsNormalizedString( ULONG form
, const WCHAR
*str
, INT len
, BOOLEAN
*res
)
981 const struct norm_table
*info
;
983 BYTE props
, class, last_class
= 0;
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
;
996 if ((ch
>= HANGUL_VBASE
&& ch
< HANGUL_VBASE
+ HANGUL_VCOUNT
) ||
997 (ch
>= HANGUL_TBASE
&& ch
< HANGUL_TBASE
+ HANGUL_TCOUNT
))
999 result
= -1; /* QC=Maybe */
1003 else if (ch
>= HANGUL_SBASE
&& ch
< HANGUL_SBASE
+ HANGUL_SCOUNT
)
1005 result
= 0; /* QC=No */
1008 props
= get_char_props( info
, ch
);
1009 class = props
& 0x3f;
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 */
1030 else last_class
= 0;
1035 int dstlen
= len
* 4;
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
);
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
)
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;
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
;
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;
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
);
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
);
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
)
1107 for (i
= 0; i
< len
; 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
;
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
;
1121 switch (get_char_props( info
, buffer
[i
] ))
1126 if (buffer
[i
] >= HANGUL_SBASE
&& buffer
[i
] < HANGUL_SBASE
+ 0x2c00) break;
1129 if (!(flags
& IDN_ALLOW_UNASSIGNED
)) return TRUE
;
1134 if ((flags
& IDN_USE_STD3_ASCII_RULES
) && len
&& (buffer
[0] == '-' || buffer
[len
- 1] == '-'))
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
;
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;
1166 for (i
= start
; i
< normlen
; i
+= len
)
1168 if (!(len
= get_utf16( normstr
+ i
, normlen
- i
, &ch
))) break;
1169 if (!ch
|| ch
== '.') break;
1171 buffer
[buflen
++] = ch
;
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
) );
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
);
1192 for (i
= start
; i
< end
; i
++) if (normstr
[i
] < 0x80) res
[out
++] = normstr
[i
];
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);
1204 for (i
= 0; i
< buflen
; i
++)
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;
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
);
1224 else if (buffer
[i
] < n
) delta
++;
1228 if (out
- out_label
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
1232 if (out
+ 1 > ARRAY_SIZE(res
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1233 res
[out
++] = normstr
[end
];
1239 if (out
<= *dstlen
) memcpy( dst
, res
, out
* sizeof(WCHAR
) );
1240 else status
= STATUS_BUFFER_TOO_SMALL
;
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
;
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
) );
1276 else if ((status
= RtlNormalizeString( 13, src
, srclen
, buf
, &buflen
)))
1278 if (status
== STATUS_NO_UNICODE_TRANSLATION
) status
= STATUS_INVALID_IDN_NORMALIZATION
;
1282 for (i
= start
= 0; i
< buflen
; i
+= len
)
1284 if (!(len
= get_utf16( buf
+ i
, buflen
- i
, &ch
))) break;
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
;
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
;
1313 if (buflen
<= *dstlen
) memcpy( dst
, buf
, buflen
* sizeof(WCHAR
) );
1314 else status
= STATUS_BUFFER_TOO_SMALL
;
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;
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
;
1344 for (i
= start
; i
< srclen
; 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
== '-')
1355 return STATUS_INVALID_IDN_NORMALIZATION
;
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
++;
1375 if (out
+ end
- start
<= *dstlen
)
1376 memcpy( dst
+ out
, src
+ start
, (end
- start
) * sizeof(WCHAR
));
1377 else return STATUS_BUFFER_TOO_SMALL
;
1384 if (delim
== start
+ 3) delim
++;
1386 for (i
= start
+ 4; i
< delim
&& buflen
< ARRAY_SIZE(buffer
); i
++) buffer
[buflen
++] = src
[i
];
1392 for (k
= BASE
; ; k
+= BASE
)
1394 if (i
>= end
) return STATUS_INVALID_IDN_NORMALIZATION
;
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
;
1401 t
= k
<= bias
? TMIN
: k
>= bias
+ TMAX
? TMAX
: k
- bias
;
1402 if (digit
< t
) break;
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);
1413 if (buflen
>= ARRAY_SIZE(buffer
) - 1) return STATUS_INVALID_IDN_NORMALIZATION
;
1414 memmove( buffer
+ pos
+ 1, buffer
+ pos
, (buflen
- pos
) * sizeof(*buffer
) );
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);
1426 if (out
+ len
<= *dstlen
) put_utf16( dst
+ out
, buffer
[i
] );
1427 else return STATUS_BUFFER_TOO_SMALL
;
1432 if (out
- out_label
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
1438 if (out
+ 1 <= *dstlen
) dst
[out
] = src
[end
];
1439 else return STATUS_BUFFER_TOO_SMALL
;
1446 return STATUS_SUCCESS
;