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 #define NONAMELESSUNION
28 #define WIN32_NO_STATUS
32 #include "ntdll_misc.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(nls
);
37 /* NLS codepage file format:
40 * WORD offset to cp2uni table in words
42 * WORD MaximumCharacterSize
45 * WORD TransDefaultChar
46 * WORD TransUniDefaultChar
49 * WORD offset to uni2cp table in words
50 * WORD[256] cp2uni table
51 * WORD glyph table size
52 * WORD[glyph_table_size] glyph table
53 * WORD number of lead byte ranges
54 * WORD[256] lead byte offsets in words
55 * WORD[leadbytes][256] cp2uni table for lead bytes
58 * BYTE[65536] / WORD[65536] uni2cp table
63 NLS_SECTION_SORTKEYS
= 9,
64 NLS_SECTION_CASEMAP
= 10,
65 NLS_SECTION_CODEPAGE
= 11,
66 NLS_SECTION_NORMALIZE
= 12
69 UINT NlsAnsiCodePage
= 0;
70 BYTE NlsMbCodePageTag
= 0;
71 BYTE NlsMbOemCodePageTag
= 0;
73 /* NLS normalization file */
76 WCHAR name
[13]; /* 00 file name */
77 USHORT checksum
[3]; /* 1a checksum? */
78 USHORT version
[4]; /* 20 Unicode version */
79 USHORT form
; /* 28 normalization form */
80 USHORT len_factor
; /* 2a factor for length estimates */
81 USHORT unknown1
; /* 2c */
82 USHORT decomp_size
; /* 2e decomposition hash size */
83 USHORT comp_size
; /* 30 composition hash size */
84 USHORT unknown2
; /* 32 */
85 USHORT classes
; /* 34 combining classes table offset */
86 USHORT props_level1
; /* 36 char properties table level 1 offset */
87 USHORT props_level2
; /* 38 char properties table level 2 offset */
88 USHORT decomp_hash
; /* 3a decomposition hash table offset */
89 USHORT decomp_map
; /* 3c decomposition character map table offset */
90 USHORT decomp_seq
; /* 3e decomposition character sequences offset */
91 USHORT comp_hash
; /* 40 composition hash table offset */
92 USHORT comp_seq
; /* 42 composition character sequences offset */
93 /* BYTE[] combining class values */
94 /* BYTE[0x2200] char properties index level 1 */
95 /* BYTE[] char properties index level 2 */
96 /* WORD[] decomposition hash table */
97 /* WORD[] decomposition character map */
98 /* WORD[] decomposition character sequences */
99 /* WORD[] composition hash table */
100 /* WORD[] composition character sequences */
103 LCID user_lcid
= 0, system_lcid
= 0;
105 static NLSTABLEINFO nls_info
;
106 static HMODULE kernel32_handle
;
107 static CPTABLEINFO unix_table
;
108 static struct norm_table
*norm_tables
[16];
111 static NTSTATUS
load_string( ULONG id
, LANGID lang
, WCHAR
*buffer
, ULONG len
)
113 const IMAGE_RESOURCE_DATA_ENTRY
*data
;
114 LDR_RESOURCE_INFO info
;
119 info
.Type
= 6; /* RT_STRING */
120 info
.Name
= (id
>> 4) + 1;
121 info
.Language
= lang
;
122 if ((status
= LdrFindResource_U( kernel32_handle
, &info
, 3, &data
))) return status
;
123 p
= (WCHAR
*)((char *)kernel32_handle
+ data
->OffsetToData
);
124 for (i
= 0; i
< (id
& 0x0f); i
++) p
+= *p
+ 1;
125 if (*p
>= len
) return STATUS_BUFFER_TOO_SMALL
;
126 memcpy( buffer
, p
+ 1, *p
* sizeof(WCHAR
) );
128 return STATUS_SUCCESS
;
132 static DWORD
mbtowc_size( const CPTABLEINFO
*info
, LPCSTR str
, UINT len
)
136 if (!info
->DBCSCodePage
) return len
;
138 for (res
= 0; len
; len
--, str
++, res
++)
140 if (info
->DBCSOffsets
[(unsigned char)*str
] && len
> 1)
150 static DWORD
wctomb_size( const CPTABLEINFO
*info
, LPCWSTR str
, UINT len
)
152 if (info
->DBCSCodePage
)
154 WCHAR
*uni2cp
= info
->WideCharTable
;
157 for (res
= 0; len
; len
--, str
++, res
++)
158 if (uni2cp
[*str
] & 0xff00) res
++;
165 static WCHAR
casemap( USHORT
*table
, WCHAR ch
)
167 return ch
+ table
[table
[table
[ch
>> 8] + ((ch
>> 4) & 0x0f)] + (ch
& 0x0f)];
171 static WCHAR
casemap_ascii( WCHAR ch
)
173 if (ch
>= 'a' && ch
<= 'z') ch
-= 'a' - 'A';
178 static int get_utf16( const WCHAR
*src
, unsigned int srclen
, unsigned int *ch
)
180 if (IS_HIGH_SURROGATE( src
[0] ))
182 if (srclen
<= 1) return 0;
183 if (!IS_LOW_SURROGATE( src
[1] )) return 0;
184 *ch
= 0x10000 + ((src
[0] & 0x3ff) << 10) + (src
[1] & 0x3ff);
187 if (IS_LOW_SURROGATE( src
[0] )) return 0;
192 static void put_utf16( WCHAR
*dst
, unsigned int ch
)
197 dst
[0] = 0xd800 | (ch
>> 10);
198 dst
[1] = 0xdc00 | (ch
& 0x3ff);
204 static NTSTATUS
load_norm_table( ULONG form
, const struct norm_table
**info
)
207 USHORT
*data
, *tables
;
211 if (!form
) return STATUS_INVALID_PARAMETER
;
212 if (form
>= ARRAY_SIZE(norm_tables
)) return STATUS_OBJECT_NAME_NOT_FOUND
;
214 if (!norm_tables
[form
])
216 if ((status
= NtGetNlsSectionPtr( NLS_SECTION_NORMALIZE
, form
, NULL
, (void **)&data
, &size
)))
221 if (size
<= 0x44) goto invalid
;
222 if (data
[0x14] != form
) goto invalid
;
223 tables
= data
+ 0x1a;
224 for (i
= 0; i
< 8; i
++)
226 if (tables
[i
] > size
/ sizeof(USHORT
)) goto invalid
;
227 if (i
&& tables
[i
] < tables
[i
-1]) goto invalid
;
230 if (InterlockedCompareExchangePointer( (void **)&norm_tables
[form
], data
, NULL
))
231 NtUnmapViewOfSection( GetCurrentProcess(), data
);
233 *info
= norm_tables
[form
];
234 return STATUS_SUCCESS
;
237 NtUnmapViewOfSection( GetCurrentProcess(), data
);
238 return STATUS_INVALID_PARAMETER
;
242 static BYTE
rol( BYTE val
, BYTE count
)
244 return (val
<< count
) | (val
>> (8 - count
));
248 static BYTE
get_char_props( const struct norm_table
*info
, unsigned int ch
)
250 const BYTE
*level1
= (const BYTE
*)((const USHORT
*)info
+ info
->props_level1
);
251 const BYTE
*level2
= (const BYTE
*)((const USHORT
*)info
+ info
->props_level2
);
252 BYTE off
= level1
[ch
/ 128];
254 if (!off
|| off
>= 0xfb) return rol( off
, 5 );
255 return level2
[(off
- 1) * 128 + ch
% 128];
259 #define HANGUL_SBASE 0xac00
260 #define HANGUL_LBASE 0x1100
261 #define HANGUL_VBASE 0x1161
262 #define HANGUL_TBASE 0x11a7
263 #define HANGUL_LCOUNT 19
264 #define HANGUL_VCOUNT 21
265 #define HANGUL_TCOUNT 28
266 #define HANGUL_NCOUNT (HANGUL_VCOUNT * HANGUL_TCOUNT)
267 #define HANGUL_SCOUNT (HANGUL_LCOUNT * HANGUL_NCOUNT)
269 static const WCHAR
*get_decomposition( const struct norm_table
*info
, unsigned int ch
,
270 BYTE props
, WCHAR
*buffer
, unsigned int *ret_len
)
272 const struct pair
{ WCHAR src
; USHORT dst
; } *pairs
;
273 const USHORT
*hash_table
= (const USHORT
*)info
+ info
->decomp_hash
;
275 unsigned int i
, pos
, end
, len
, hash
;
277 /* default to no decomposition */
278 put_utf16( buffer
, ch
);
279 *ret_len
= 1 + (ch
>= 0x10000);
280 if (!props
|| props
== 0x7f) return buffer
;
282 if (props
== 0xff) /* Hangul or invalid char */
284 if (ch
>= HANGUL_SBASE
&& ch
< HANGUL_SBASE
+ HANGUL_SCOUNT
)
286 unsigned short sindex
= ch
- HANGUL_SBASE
;
287 unsigned short tindex
= sindex
% HANGUL_TCOUNT
;
288 buffer
[0] = HANGUL_LBASE
+ sindex
/ HANGUL_NCOUNT
;
289 buffer
[1] = HANGUL_VBASE
+ (sindex
% HANGUL_NCOUNT
) / HANGUL_TCOUNT
;
290 if (tindex
) buffer
[2] = HANGUL_TBASE
+ tindex
;
291 *ret_len
= 2 + !!tindex
;
294 /* ignore other chars in Hangul range */
295 if (ch
>= HANGUL_LBASE
&& ch
< HANGUL_LBASE
+ 0x100) return buffer
;
296 if (ch
>= HANGUL_SBASE
&& ch
< HANGUL_SBASE
+ 0x2c00) return buffer
;
300 hash
= ch
% info
->decomp_size
;
301 pos
= hash_table
[hash
];
304 if (props
!= 0xbf) return buffer
;
305 ret
= (const USHORT
*)info
+ info
->decomp_seq
+ (pos
& 0x1fff);
310 pairs
= (const struct pair
*)((const USHORT
*)info
+ info
->decomp_map
);
312 /* find the end of the hash bucket */
313 for (i
= hash
+ 1; i
< info
->decomp_size
; i
++) if (!(hash_table
[i
] >> 13)) break;
314 if (i
< info
->decomp_size
) end
= hash_table
[i
];
315 else for (end
= pos
; pairs
[end
].src
; end
++) ;
317 for ( ; pos
< end
; pos
++)
319 if (pairs
[pos
].src
!= (WCHAR
)ch
) continue;
320 ret
= (const USHORT
*)info
+ info
->decomp_seq
+ (pairs
[pos
].dst
& 0x1fff);
321 len
= pairs
[pos
].dst
>> 13;
324 if (pos
>= end
) return buffer
;
327 if (len
== 7) while (ret
[len
]) len
++;
328 if (!ret
[0]) len
= 0; /* ignored char */
334 static BYTE
get_combining_class( const struct norm_table
*info
, unsigned int c
)
336 const BYTE
*classes
= (const BYTE
*)((const USHORT
*)info
+ info
->classes
);
337 BYTE
class = get_char_props( info
, c
) & 0x3f;
339 if (class == 0x3f) return 0;
340 return classes
[class];
344 static BOOL
is_starter( const struct norm_table
*info
, unsigned int c
)
346 return !get_combining_class( info
, c
);
350 static BOOL
reorderable_pair( const struct norm_table
*info
, unsigned int c1
, unsigned int c2
)
354 /* reorderable if ccc1 > ccc2 > 0 */
355 ccc1
= get_combining_class( info
, c1
);
356 if (ccc1
< 2) return FALSE
;
357 ccc2
= get_combining_class( info
, c2
);
358 return ccc2
&& (ccc1
> ccc2
);
361 static void canonical_order_substring( const struct norm_table
*info
, WCHAR
*str
, unsigned int len
)
363 unsigned int i
, ch1
, ch2
, len1
, len2
;
369 for (i
= 0; i
< len
- 1; i
+= len1
)
371 if (!(len1
= get_utf16( str
+ i
, len
- i
, &ch1
))) break;
372 if (i
+ len1
>= len
) break;
373 if (!(len2
= get_utf16( str
+ i
+ len1
, len
- i
- len1
, &ch2
))) break;
374 if (reorderable_pair( info
, ch1
, ch2
))
377 memcpy( tmp
, str
+ i
, len1
* sizeof(WCHAR
) );
378 memcpy( str
+ i
, str
+ i
+ len1
, len2
* sizeof(WCHAR
) );
379 memcpy( str
+ i
+ len2
, tmp
, len1
* sizeof(WCHAR
) );
388 /****************************************************************************
389 * canonical_order_string
391 * Reorder the string into canonical order - D108/D109.
393 * Starters (chars with combining class == 0) don't move, so look for continuous
394 * substrings of non-starters and only reorder those.
396 static void canonical_order_string( const struct norm_table
*info
, WCHAR
*str
, unsigned int len
)
398 unsigned int ch
, i
, r
, next
= 0;
400 for (i
= 0; i
< len
; i
+= r
)
402 if (!(r
= get_utf16( str
+ i
, len
- i
, &ch
))) return;
403 if (i
&& is_starter( info
, ch
))
405 if (i
> next
+ 1) /* at least two successive non-starters */
406 canonical_order_substring( info
, str
+ next
, i
- next
);
410 if (i
> next
+ 1) canonical_order_substring( info
, str
+ next
, i
- next
);
414 static NTSTATUS
decompose_string( const struct norm_table
*info
, const WCHAR
*src
, int src_len
,
415 WCHAR
*dst
, int *dst_len
)
418 int src_pos
, dst_pos
;
419 unsigned int ch
, len
, decomp_len
;
423 for (src_pos
= dst_pos
= 0; src_pos
< src_len
; src_pos
+= len
)
425 if (!(len
= get_utf16( src
+ src_pos
, src_len
- src_pos
, &ch
)))
427 *dst_len
= src_pos
+ IS_HIGH_SURROGATE( src
[src_pos
] );
428 return STATUS_NO_UNICODE_TRANSLATION
;
430 props
= get_char_props( info
, ch
);
431 if (!(decomp
= get_decomposition( info
, ch
, props
, buffer
, &decomp_len
)))
433 /* allow final null */
434 if (!ch
&& src_pos
== src_len
- 1 && dst_pos
< *dst_len
)
440 return STATUS_NO_UNICODE_TRANSLATION
;
442 if (dst_pos
+ decomp_len
> *dst_len
)
444 *dst_len
+= (src_len
- src_pos
) * info
->len_factor
;
445 return STATUS_BUFFER_TOO_SMALL
;
447 memcpy( dst
+ dst_pos
, decomp
, decomp_len
* sizeof(WCHAR
) );
448 dst_pos
+= decomp_len
;
451 canonical_order_string( info
, dst
, dst_pos
);
453 return STATUS_SUCCESS
;
457 static unsigned int compose_hangul( unsigned int ch1
, unsigned int ch2
)
459 if (ch1
>= HANGUL_LBASE
&& ch1
< HANGUL_LBASE
+ HANGUL_LCOUNT
)
461 int lindex
= ch1
- HANGUL_LBASE
;
462 int vindex
= ch2
- HANGUL_VBASE
;
463 if (vindex
>= 0 && vindex
< HANGUL_VCOUNT
)
464 return HANGUL_SBASE
+ (lindex
* HANGUL_VCOUNT
+ vindex
) * HANGUL_TCOUNT
;
466 if (ch1
>= HANGUL_SBASE
&& ch1
< HANGUL_SBASE
+ HANGUL_SCOUNT
)
468 int sindex
= ch1
- HANGUL_SBASE
;
469 if (!(sindex
% HANGUL_TCOUNT
))
471 int tindex
= ch2
- HANGUL_TBASE
;
472 if (tindex
> 0 && tindex
< HANGUL_TCOUNT
) return ch1
+ tindex
;
479 static unsigned int compose_chars( const struct norm_table
*info
, unsigned int ch1
, unsigned int ch2
)
481 const USHORT
*table
= (const USHORT
*)info
+ info
->comp_hash
;
482 const WCHAR
*chars
= (const USHORT
*)info
+ info
->comp_seq
;
483 unsigned int hash
, start
, end
, i
, len
, ch
[3];
485 hash
= (ch1
+ 95 * ch2
) % info
->comp_size
;
487 end
= table
[hash
+ 1];
490 for (i
= 0; i
< 3; i
++, start
+= len
) len
= get_utf16( chars
+ start
, end
- start
, ch
+ i
);
491 if (ch
[0] == ch1
&& ch
[1] == ch2
) return ch
[2];
496 static unsigned int compose_string( const struct norm_table
*info
, WCHAR
*str
, unsigned int srclen
)
498 unsigned int i
, ch
, comp
, len
, start_ch
= 0, last_starter
= srclen
;
499 BYTE
class, prev_class
= 0;
501 for (i
= 0; i
< srclen
; i
+= len
)
503 if (!(len
= get_utf16( str
+ i
, srclen
- i
, &ch
))) return 0;
504 class = get_combining_class( info
, ch
);
505 if (last_starter
== srclen
|| (prev_class
&& prev_class
>= class) ||
506 (!(comp
= compose_hangul( start_ch
, ch
)) &&
507 !(comp
= compose_chars( info
, start_ch
, ch
))))
518 int comp_len
= 1 + (comp
>= 0x10000);
519 int start_len
= 1 + (start_ch
>= 0x10000);
521 if (comp_len
!= start_len
)
522 memmove( str
+ last_starter
+ comp_len
, str
+ last_starter
+ start_len
,
523 (i
- (last_starter
+ start_len
)) * sizeof(WCHAR
) );
524 memmove( str
+ i
+ comp_len
- start_len
, str
+ i
+ len
, (srclen
- i
- len
) * sizeof(WCHAR
) );
525 srclen
+= comp_len
- start_len
- len
;
530 put_utf16( str
+ i
, comp
);
537 void init_unix_codepage(void)
539 USHORT
*data
= unix_funcs
->get_unix_codepage_data();
540 if (data
) RtlInitCodePageTable( data
, &unix_table
);
544 static LCID
locale_to_lcid( WCHAR
*win_name
)
549 if (!RtlLocaleNameToLcid( win_name
, &lcid
, 0 )) return lcid
;
551 /* try neutral name */
552 if ((p
= wcsrchr( win_name
, '-' )))
555 if (!RtlLocaleNameToLcid( win_name
, &lcid
, 2 ))
557 if (SUBLANGID(lcid
) == SUBLANG_NEUTRAL
)
558 lcid
= MAKELANGID( PRIMARYLANGID(lcid
), SUBLANG_DEFAULT
);
566 /******************************************************************
569 void init_locale( HMODULE module
)
571 WCHAR system_locale
[LOCALE_NAME_MAX_LENGTH
];
572 WCHAR user_locale
[LOCALE_NAME_MAX_LENGTH
];
574 kernel32_handle
= module
;
576 unix_funcs
->get_locales( system_locale
, user_locale
);
577 system_lcid
= locale_to_lcid( system_locale
);
578 user_lcid
= locale_to_lcid( user_locale
);
579 if (!system_lcid
) system_lcid
= MAKELCID( MAKELANGID(LANG_ENGLISH
,SUBLANG_DEFAULT
), SORT_DEFAULT
);
580 if (!user_lcid
) user_lcid
= system_lcid
;
582 NtSetDefaultUILanguage( LANGIDFROMLCID(user_lcid
) );
583 NtSetDefaultLocale( TRUE
, user_lcid
);
584 NtSetDefaultLocale( FALSE
, system_lcid
);
585 TRACE( "system=%04x user=%04x\n", system_lcid
, user_lcid
);
589 /******************************************************************
592 DWORD
ntdll_umbstowcs( const char *src
, DWORD srclen
, WCHAR
*dst
, DWORD dstlen
)
596 if (unix_table
.CodePage
)
597 RtlCustomCPToUnicodeN( &unix_table
, dst
, dstlen
* sizeof(WCHAR
), &reslen
, src
, srclen
);
599 RtlUTF8ToUnicodeN( dst
, dstlen
* sizeof(WCHAR
), &reslen
, src
, srclen
);
600 return reslen
/ sizeof(WCHAR
);
604 /******************************************************************
607 int ntdll_wcstoumbs( const WCHAR
*src
, DWORD srclen
, char *dst
, DWORD dstlen
, BOOL strict
)
611 if (!unix_table
.CodePage
)
612 RtlUnicodeToUTF8N( dst
, dstlen
, &reslen
, src
, srclen
* sizeof(WCHAR
) );
614 RtlUnicodeToCustomCPN( &unix_table
, dst
, dstlen
, &reslen
, src
, srclen
* sizeof(WCHAR
) );
615 else /* do it by hand to make sure every character roundtrips correctly */
617 if (unix_table
.DBCSOffsets
)
619 const unsigned short *uni2cp
= unix_table
.WideCharTable
;
620 for (i
= dstlen
; srclen
&& i
; i
--, srclen
--, src
++)
622 unsigned short ch
= uni2cp
[*src
];
625 if (unix_table
.DBCSOffsets
[unix_table
.DBCSOffsets
[ch
>> 8] + (ch
& 0xff)] != *src
)
627 if (i
== 1) break; /* do not output a partial char */
633 if (unix_table
.MultiByteTable
[ch
] != *src
) return -1;
641 const unsigned char *uni2cp
= unix_table
.WideCharTable
;
642 reslen
= min( srclen
, dstlen
);
643 for (i
= 0; i
< reslen
; i
++)
645 unsigned char ch
= uni2cp
[src
[i
]];
646 if (unix_table
.MultiByteTable
[ch
] != src
[i
]) return -1;
655 /******************************************************************
656 * __wine_get_unix_codepage (NTDLL.@)
658 UINT CDECL
__wine_get_unix_codepage(void)
660 if (!unix_table
.CodePage
) return CP_UTF8
;
661 return unix_table
.CodePage
;
665 static NTSTATUS
get_dummy_preferred_ui_language( DWORD flags
, LANGID lang
, ULONG
*count
,
666 WCHAR
*buffer
, ULONG
*size
)
668 WCHAR name
[LOCALE_NAME_MAX_LENGTH
+ 2];
672 FIXME("(0x%x %p %p %p) returning a dummy value (current locale)\n", flags
, count
, buffer
, size
);
674 status
= load_string( (flags
& MUI_LANGUAGE_ID
) ? LOCALE_ILANGUAGE
: LOCALE_SNAME
,
675 lang
, name
, ARRAY_SIZE(name
) );
676 if (status
) return status
;
678 len
= wcslen( name
) + 2;
685 return STATUS_BUFFER_TOO_SMALL
;
687 memcpy( buffer
, name
, len
* sizeof(WCHAR
) );
691 TRACE("returned variable content: %d, \"%s\", %d\n", *count
, debugstr_w(buffer
), *size
);
692 return STATUS_SUCCESS
;
696 /**************************************************************************
697 * RtlGetProcessPreferredUILanguages (NTDLL.@)
699 NTSTATUS WINAPI
RtlGetProcessPreferredUILanguages( DWORD flags
, ULONG
*count
, WCHAR
*buffer
, ULONG
*size
)
703 FIXME( "%08x, %p, %p %p\n", flags
, count
, buffer
, size
);
705 NtQueryDefaultUILanguage( &ui_language
);
706 return get_dummy_preferred_ui_language( flags
, ui_language
, count
, buffer
, size
);
710 /**************************************************************************
711 * RtlGetSystemPreferredUILanguages (NTDLL.@)
713 NTSTATUS WINAPI
RtlGetSystemPreferredUILanguages( DWORD flags
, ULONG unknown
, ULONG
*count
,
714 WCHAR
*buffer
, ULONG
*size
)
718 if (flags
& ~(MUI_LANGUAGE_NAME
| MUI_LANGUAGE_ID
| MUI_MACHINE_LANGUAGE_SETTINGS
)) return STATUS_INVALID_PARAMETER
;
719 if ((flags
& MUI_LANGUAGE_NAME
) && (flags
& MUI_LANGUAGE_ID
)) return STATUS_INVALID_PARAMETER
;
720 if (*size
&& !buffer
) return STATUS_INVALID_PARAMETER
;
722 NtQueryInstallUILanguage( &ui_language
);
723 return get_dummy_preferred_ui_language( flags
, ui_language
, count
, buffer
, size
);
727 /**************************************************************************
728 * RtlGetThreadPreferredUILanguages (NTDLL.@)
730 NTSTATUS WINAPI
RtlGetThreadPreferredUILanguages( DWORD flags
, ULONG
*count
, WCHAR
*buffer
, ULONG
*size
)
734 FIXME( "%08x, %p, %p %p\n", flags
, count
, buffer
, size
);
736 NtQueryDefaultUILanguage( &ui_language
);
737 return get_dummy_preferred_ui_language( flags
, ui_language
, count
, buffer
, size
);
741 /**************************************************************************
742 * RtlGetUserPreferredUILanguages (NTDLL.@)
744 NTSTATUS WINAPI
RtlGetUserPreferredUILanguages( DWORD flags
, ULONG unknown
, ULONG
*count
,
745 WCHAR
*buffer
, ULONG
*size
)
749 if (flags
& ~(MUI_LANGUAGE_NAME
| MUI_LANGUAGE_ID
)) return STATUS_INVALID_PARAMETER
;
750 if ((flags
& MUI_LANGUAGE_NAME
) && (flags
& MUI_LANGUAGE_ID
)) return STATUS_INVALID_PARAMETER
;
751 if (*size
&& !buffer
) return STATUS_INVALID_PARAMETER
;
753 NtQueryDefaultUILanguage( &ui_language
);
754 return get_dummy_preferred_ui_language( flags
, ui_language
, count
, buffer
, size
);
758 /**************************************************************************
759 * RtlSetProcessPreferredUILanguages (NTDLL.@)
761 NTSTATUS WINAPI
RtlSetProcessPreferredUILanguages( DWORD flags
, PCZZWSTR buffer
, ULONG
*count
)
763 FIXME( "%u, %p, %p\n", flags
, buffer
, count
);
764 return STATUS_SUCCESS
;
768 /**************************************************************************
769 * RtlSetThreadPreferredUILanguages (NTDLL.@)
771 NTSTATUS WINAPI
RtlSetThreadPreferredUILanguages( DWORD flags
, PCZZWSTR buffer
, ULONG
*count
)
773 FIXME( "%u, %p, %p\n", flags
, buffer
, count
);
774 return STATUS_SUCCESS
;
778 /******************************************************************
779 * RtlInitCodePageTable (NTDLL.@)
781 void WINAPI
RtlInitCodePageTable( USHORT
*ptr
, CPTABLEINFO
*info
)
783 USHORT hdr_size
= ptr
[0];
785 info
->CodePage
= ptr
[1];
786 info
->MaximumCharacterSize
= ptr
[2];
787 info
->DefaultChar
= ptr
[3];
788 info
->UniDefaultChar
= ptr
[4];
789 info
->TransDefaultChar
= ptr
[5];
790 info
->TransUniDefaultChar
= ptr
[6];
791 memcpy( info
->LeadByte
, ptr
+ 7, sizeof(info
->LeadByte
) );
794 info
->WideCharTable
= ptr
+ ptr
[0] + 1;
795 info
->MultiByteTable
= ++ptr
;
797 if (*ptr
++) ptr
+= 256; /* glyph table */
798 info
->DBCSRanges
= ptr
;
799 if (*ptr
) /* dbcs ranges */
801 info
->DBCSCodePage
= 1;
802 info
->DBCSOffsets
= ptr
+ 1;
806 info
->DBCSCodePage
= 0;
807 info
->DBCSOffsets
= NULL
;
812 /**************************************************************************
813 * RtlInitNlsTables (NTDLL.@)
815 void WINAPI
RtlInitNlsTables( USHORT
*ansi
, USHORT
*oem
, USHORT
*casetable
, NLSTABLEINFO
*info
)
817 RtlInitCodePageTable( ansi
, &info
->AnsiTableInfo
);
818 RtlInitCodePageTable( oem
, &info
->OemTableInfo
);
819 info
->UpperCaseTable
= casetable
+ 2;
820 info
->LowerCaseTable
= casetable
+ casetable
[1] + 2;
824 /**************************************************************************
825 * RtlResetRtlTranslations (NTDLL.@)
827 void WINAPI
RtlResetRtlTranslations( const NLSTABLEINFO
*info
)
829 NlsAnsiCodePage
= info
->AnsiTableInfo
.CodePage
;
830 NlsMbCodePageTag
= info
->AnsiTableInfo
.DBCSCodePage
;
831 NlsMbOemCodePageTag
= info
->OemTableInfo
.DBCSCodePage
;
836 /**************************************************************************
837 * RtlAnsiCharToUnicodeChar (NTDLL.@)
839 WCHAR WINAPI
RtlAnsiCharToUnicodeChar( char **ansi
)
841 if (nls_info
.AnsiTableInfo
.DBCSOffsets
)
843 USHORT off
= nls_info
.AnsiTableInfo
.DBCSOffsets
[(unsigned char)**ansi
];
847 return nls_info
.AnsiTableInfo
.DBCSOffsets
[off
+ (unsigned char)*(*ansi
)++];
850 return nls_info
.AnsiTableInfo
.MultiByteTable
[(unsigned char)*(*ansi
)++];
854 /******************************************************************************
855 * RtlCompareUnicodeStrings (NTDLL.@)
857 LONG WINAPI
RtlCompareUnicodeStrings( const WCHAR
*s1
, SIZE_T len1
, const WCHAR
*s2
, SIZE_T len2
,
858 BOOLEAN case_insensitive
)
861 SIZE_T len
= min( len1
, len2
);
863 if (case_insensitive
)
865 if (nls_info
.UpperCaseTable
)
867 while (!ret
&& len
--) ret
= casemap( nls_info
.UpperCaseTable
, *s1
++ ) -
868 casemap( nls_info
.UpperCaseTable
, *s2
++ );
870 else /* locale not setup yet */
872 while (!ret
&& len
--) ret
= casemap_ascii( *s1
++ ) - casemap_ascii( *s2
++ );
877 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
879 if (!ret
) ret
= len1
- len2
;
884 /**************************************************************************
885 * RtlPrefixUnicodeString (NTDLL.@)
887 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
888 BOOLEAN ignore_case
)
892 if (s1
->Length
> s2
->Length
) return FALSE
;
895 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
896 if (casemap( nls_info
.UpperCaseTable
, s1
->Buffer
[i
] ) !=
897 casemap( nls_info
.UpperCaseTable
, s2
->Buffer
[i
] )) return FALSE
;
901 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
902 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
909 /******************************************************************************
910 * RtlHashUnicodeString (NTDLL.@)
912 NTSTATUS WINAPI
RtlHashUnicodeString( const UNICODE_STRING
*string
, BOOLEAN case_insensitive
,
913 ULONG alg
, ULONG
*hash
)
917 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
921 case HASH_STRING_ALGORITHM_DEFAULT
:
922 case HASH_STRING_ALGORITHM_X65599
:
925 return STATUS_INVALID_PARAMETER
;
929 if (!case_insensitive
)
930 for (i
= 0; i
< string
->Length
/ sizeof(WCHAR
); i
++)
931 *hash
= *hash
* 65599 + string
->Buffer
[i
];
932 else if (nls_info
.UpperCaseTable
)
933 for (i
= 0; i
< string
->Length
/ sizeof(WCHAR
); i
++)
934 *hash
= *hash
* 65599 + casemap( nls_info
.UpperCaseTable
, string
->Buffer
[i
] );
935 else /* locale not setup yet */
936 for (i
= 0; i
< string
->Length
/ sizeof(WCHAR
); i
++)
937 *hash
= *hash
* 65599 + casemap_ascii( string
->Buffer
[i
] );
938 return STATUS_SUCCESS
;
942 /**************************************************************************
943 * RtlCustomCPToUnicodeN (NTDLL.@)
945 NTSTATUS WINAPI
RtlCustomCPToUnicodeN( CPTABLEINFO
*info
, WCHAR
*dst
, DWORD dstlen
, DWORD
*reslen
,
946 const char *src
, DWORD srclen
)
950 dstlen
/= sizeof(WCHAR
);
951 if (info
->DBCSOffsets
)
953 for (i
= dstlen
; srclen
&& i
; i
--, srclen
--, src
++, dst
++)
955 USHORT off
= info
->DBCSOffsets
[(unsigned char)*src
];
956 if (off
&& srclen
> 1)
960 *dst
= info
->DBCSOffsets
[off
+ (unsigned char)*src
];
962 else *dst
= info
->MultiByteTable
[(unsigned char)*src
];
968 ret
= min( srclen
, dstlen
);
969 for (i
= 0; i
< ret
; i
++) dst
[i
] = info
->MultiByteTable
[(unsigned char)src
[i
]];
971 if (reslen
) *reslen
= ret
* sizeof(WCHAR
);
972 return STATUS_SUCCESS
;
976 /**************************************************************************
977 * RtlUnicodeToCustomCPN (NTDLL.@)
979 NTSTATUS WINAPI
RtlUnicodeToCustomCPN( CPTABLEINFO
*info
, char *dst
, DWORD dstlen
, DWORD
*reslen
,
980 const WCHAR
*src
, DWORD srclen
)
984 srclen
/= sizeof(WCHAR
);
985 if (info
->DBCSCodePage
)
987 WCHAR
*uni2cp
= info
->WideCharTable
;
989 for (i
= dstlen
; srclen
&& i
; i
--, srclen
--, src
++)
991 if (uni2cp
[*src
] & 0xff00)
993 if (i
== 1) break; /* do not output a partial char */
995 *dst
++ = uni2cp
[*src
] >> 8;
997 *dst
++ = (char)uni2cp
[*src
];
1003 char *uni2cp
= info
->WideCharTable
;
1004 ret
= min( srclen
, dstlen
);
1005 for (i
= 0; i
< ret
; i
++) dst
[i
] = uni2cp
[src
[i
]];
1007 if (reslen
) *reslen
= ret
;
1008 return STATUS_SUCCESS
;
1012 /**************************************************************************
1013 * RtlMultiByteToUnicodeN (NTDLL.@)
1015 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( WCHAR
*dst
, DWORD dstlen
, DWORD
*reslen
,
1016 const char *src
, DWORD srclen
)
1018 if (nls_info
.AnsiTableInfo
.WideCharTable
)
1019 return RtlCustomCPToUnicodeN( &nls_info
.AnsiTableInfo
, dst
, dstlen
, reslen
, src
, srclen
);
1021 /* locale not setup yet */
1022 dstlen
= min( srclen
, dstlen
/ sizeof(WCHAR
) );
1023 if (reslen
) *reslen
= dstlen
* sizeof(WCHAR
);
1024 while (dstlen
--) *dst
++ = *src
++ & 0x7f;
1025 return STATUS_SUCCESS
;
1029 /**************************************************************************
1030 * RtlMultiByteToUnicodeSize (NTDLL.@)
1032 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, const char *str
, DWORD len
)
1034 *size
= mbtowc_size( &nls_info
.AnsiTableInfo
, str
, len
) * sizeof(WCHAR
);
1035 return STATUS_SUCCESS
;
1039 /**************************************************************************
1040 * RtlOemToUnicodeN (NTDLL.@)
1042 NTSTATUS WINAPI
RtlOemToUnicodeN( WCHAR
*dst
, DWORD dstlen
, DWORD
*reslen
,
1043 const char *src
, DWORD srclen
)
1045 return RtlCustomCPToUnicodeN( &nls_info
.OemTableInfo
, dst
, dstlen
, reslen
, src
, srclen
);
1049 /**************************************************************************
1050 * RtlOemStringToUnicodeSize (NTDLL.@)
1051 * RtlxOemStringToUnicodeSize (NTDLL.@)
1053 DWORD WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1055 return (mbtowc_size( &nls_info
.OemTableInfo
, str
->Buffer
, str
->Length
) + 1) * sizeof(WCHAR
);
1059 /**************************************************************************
1060 * RtlUnicodeStringToOemSize (NTDLL.@)
1061 * RtlxUnicodeStringToOemSize (NTDLL.@)
1063 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1065 return wctomb_size( &nls_info
.OemTableInfo
, str
->Buffer
, str
->Length
/ sizeof(WCHAR
) ) + 1;
1069 /**************************************************************************
1070 * RtlUnicodeToMultiByteN (NTDLL.@)
1072 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( char *dst
, DWORD dstlen
, DWORD
*reslen
,
1073 const WCHAR
*src
, DWORD srclen
)
1075 if (nls_info
.AnsiTableInfo
.WideCharTable
)
1076 return RtlUnicodeToCustomCPN( &nls_info
.AnsiTableInfo
, dst
, dstlen
, reslen
, src
, srclen
);
1078 /* locale not setup yet */
1079 dstlen
= min( srclen
/ sizeof(WCHAR
), dstlen
);
1080 if (reslen
) *reslen
= dstlen
;
1084 if (ch
> 0x7f) ch
= '?';
1087 return STATUS_SUCCESS
;
1091 /**************************************************************************
1092 * RtlUnicodeToMultiByteSize (NTDLL.@)
1094 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( DWORD
*size
, const WCHAR
*str
, DWORD len
)
1096 *size
= wctomb_size( &nls_info
.AnsiTableInfo
, str
, len
/ sizeof(WCHAR
) );
1097 return STATUS_SUCCESS
;
1101 /**************************************************************************
1102 * RtlUnicodeToOemN (NTDLL.@)
1104 NTSTATUS WINAPI
RtlUnicodeToOemN( char *dst
, DWORD dstlen
, DWORD
*reslen
,
1105 const WCHAR
*src
, DWORD srclen
)
1107 return RtlUnicodeToCustomCPN( &nls_info
.OemTableInfo
, dst
, dstlen
, reslen
, src
, srclen
);
1111 /**************************************************************************
1112 * RtlDowncaseUnicodeChar (NTDLL.@)
1114 WCHAR WINAPI
RtlDowncaseUnicodeChar( WCHAR wch
)
1116 if (nls_info
.LowerCaseTable
) return casemap( nls_info
.LowerCaseTable
, wch
);
1117 if (wch
>= 'A' && wch
<= 'Z') wch
+= 'a' - 'A';
1122 /**************************************************************************
1123 * RtlDowncaseUnicodeString (NTDLL.@)
1125 NTSTATUS WINAPI
RtlDowncaseUnicodeString( UNICODE_STRING
*dest
, const UNICODE_STRING
*src
,
1128 DWORD i
, len
= src
->Length
;
1132 dest
->MaximumLength
= len
;
1133 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return STATUS_NO_MEMORY
;
1135 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1137 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1138 dest
->Buffer
[i
] = casemap( nls_info
.LowerCaseTable
, src
->Buffer
[i
] );
1140 return STATUS_SUCCESS
;
1144 /**************************************************************************
1145 * RtlUpcaseUnicodeChar (NTDLL.@)
1147 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
1149 return casemap( nls_info
.UpperCaseTable
, wch
);
1153 /**************************************************************************
1154 * RtlUpcaseUnicodeString (NTDLL.@)
1156 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
, const UNICODE_STRING
*src
,
1159 DWORD i
, len
= src
->Length
;
1163 dest
->MaximumLength
= len
;
1164 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return STATUS_NO_MEMORY
;
1166 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1168 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1169 dest
->Buffer
[i
] = casemap( nls_info
.UpperCaseTable
, src
->Buffer
[i
] );
1171 return STATUS_SUCCESS
;
1175 /**************************************************************************
1176 * RtlUpcaseUnicodeToCustomCPN (NTDLL.@)
1178 NTSTATUS WINAPI
RtlUpcaseUnicodeToCustomCPN( CPTABLEINFO
*info
, char *dst
, DWORD dstlen
, DWORD
*reslen
,
1179 const WCHAR
*src
, DWORD srclen
)
1183 srclen
/= sizeof(WCHAR
);
1184 if (info
->DBCSCodePage
)
1186 WCHAR
*uni2cp
= info
->WideCharTable
;
1188 for (i
= dstlen
; srclen
&& i
; i
--, srclen
--, src
++)
1190 WCHAR ch
= casemap( nls_info
.UpperCaseTable
, *src
);
1191 if (uni2cp
[ch
] & 0xff00)
1193 if (i
== 1) break; /* do not output a partial char */
1195 *dst
++ = uni2cp
[ch
] >> 8;
1197 *dst
++ = (char)uni2cp
[ch
];
1203 char *uni2cp
= info
->WideCharTable
;
1204 ret
= min( srclen
, dstlen
);
1205 for (i
= 0; i
< ret
; i
++) dst
[i
] = uni2cp
[casemap( nls_info
.UpperCaseTable
, src
[i
] )];
1207 if (reslen
) *reslen
= ret
;
1208 return STATUS_SUCCESS
;
1212 /**************************************************************************
1213 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1215 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( char *dst
, DWORD dstlen
, DWORD
*reslen
,
1216 const WCHAR
*src
, DWORD srclen
)
1218 return RtlUpcaseUnicodeToCustomCPN( &nls_info
.AnsiTableInfo
, dst
, dstlen
, reslen
, src
, srclen
);
1222 /**************************************************************************
1223 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1225 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( char *dst
, DWORD dstlen
, DWORD
*reslen
,
1226 const WCHAR
*src
, DWORD srclen
)
1228 if (nls_info
.OemTableInfo
.WideCharTable
)
1229 return RtlUpcaseUnicodeToCustomCPN( &nls_info
.OemTableInfo
, dst
, dstlen
, reslen
, src
, srclen
);
1231 /* locale not setup yet */
1232 dstlen
= min( srclen
/ sizeof(WCHAR
), dstlen
);
1233 if (reslen
) *reslen
= dstlen
;
1237 if (ch
> 0x7f) ch
= '?';
1238 else ch
= casemap_ascii( ch
);
1241 return STATUS_SUCCESS
;
1245 /*********************************************************************
1246 * towlower (NTDLL.@)
1248 WCHAR __cdecl
towlower( WCHAR ch
)
1250 if (ch
>= 0x100) return ch
;
1251 return casemap( nls_info
.LowerCaseTable
, ch
);
1255 /*********************************************************************
1256 * towupper (NTDLL.@)
1258 WCHAR __cdecl
towupper( WCHAR ch
)
1260 if (nls_info
.UpperCaseTable
) return casemap( nls_info
.UpperCaseTable
, ch
);
1261 return casemap_ascii( ch
);
1265 /******************************************************************
1266 * RtlLocaleNameToLcid (NTDLL.@)
1268 NTSTATUS WINAPI
RtlLocaleNameToLcid( const WCHAR
*name
, LCID
*lcid
, ULONG flags
)
1270 /* locale name format is: lang[-script][-country][_modifier] */
1272 static const WCHAR sepW
[] = {'-','_',0};
1274 const IMAGE_RESOURCE_DIRECTORY
*resdir
;
1275 const IMAGE_RESOURCE_DIRECTORY_ENTRY
*et
;
1276 LDR_RESOURCE_INFO info
;
1277 WCHAR buf
[LOCALE_NAME_MAX_LENGTH
];
1278 WCHAR lang
[LOCALE_NAME_MAX_LENGTH
]; /* language ("en") (note: buffer contains the other strings too) */
1279 WCHAR
*country
= NULL
; /* country ("US") */
1280 WCHAR
*script
= NULL
; /* script ("Latn") */
1284 if (!name
) return STATUS_INVALID_PARAMETER_1
;
1288 *lcid
= LANG_INVARIANT
;
1291 if (wcslen( name
) >= LOCALE_NAME_MAX_LENGTH
) return STATUS_INVALID_PARAMETER_1
;
1292 wcscpy( lang
, name
);
1294 if ((p
= wcspbrk( lang
, sepW
)) && *p
== '-')
1298 if ((p
= wcspbrk( p
, sepW
)) && *p
== '-')
1303 p
= wcspbrk( p
, sepW
);
1305 if (p
) *p
= 0; /* FIXME: modifier is ignored */
1306 /* second value can be script or country, check length to resolve the ambiguity */
1307 if (!script
&& wcslen( country
) == 4)
1314 info
.Type
= 6; /* RT_STRING */
1315 info
.Name
= (LOCALE_SNAME
>> 4) + 1;
1316 if (LdrFindResourceDirectory_U( kernel32_handle
, &info
, 2, &resdir
))
1317 return STATUS_INVALID_PARAMETER_1
;
1319 et
= (const IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(resdir
+ 1);
1320 for (i
= 0; i
< resdir
->NumberOfNamedEntries
+ resdir
->NumberOfIdEntries
; i
++)
1322 LANGID id
= et
[i
].u
.Id
;
1324 if (PRIMARYLANGID(id
) == LANG_NEUTRAL
) continue;
1326 if (!load_string( LOCALE_SNAME
, id
, buf
, ARRAY_SIZE(buf
) ) && !wcsicmp( name
, buf
))
1328 *lcid
= MAKELCID( id
, SORT_DEFAULT
); /* FIXME: handle sort order */
1332 if (load_string( LOCALE_SISO639LANGNAME
, id
, buf
, ARRAY_SIZE(buf
) ) || wcsicmp( lang
, buf
))
1337 unsigned int len
= wcslen( script
);
1338 if (load_string( LOCALE_SSCRIPTS
, id
, buf
, ARRAY_SIZE(buf
) )) continue;
1342 if (!wcsnicmp( p
, script
, len
) && (!p
[len
] || p
[len
] == ';')) break;
1343 if (!(p
= wcschr( p
, ';'))) break;
1346 if (!p
|| !*p
) continue;
1349 if (!country
&& (flags
& 2))
1351 if (!script
) id
= MAKELANGID( PRIMARYLANGID(id
), LANG_NEUTRAL
);
1354 case MAKELANGID( LANG_CHINESE
, SUBLANG_NEUTRAL
):
1355 case MAKELANGID( LANG_CHINESE
, SUBLANG_CHINESE_SINGAPORE
):
1356 *lcid
= MAKELCID( 0x7804, SORT_DEFAULT
);
1358 case MAKELANGID( LANG_CHINESE
, SUBLANG_CHINESE_TRADITIONAL
):
1359 case MAKELANGID( LANG_CHINESE
, SUBLANG_CHINESE_MACAU
):
1360 case MAKELANGID( LANG_CHINESE
, SUBLANG_CHINESE_HONGKONG
):
1361 *lcid
= MAKELCID( 0x7c04, SORT_DEFAULT
);
1364 *lcid
= MAKELANGID( PRIMARYLANGID(id
), SUBLANG_NEUTRAL
);
1370 return STATUS_INVALID_PARAMETER_1
;
1373 TRACE( "%s -> %04x\n", debugstr_w(name
), *lcid
);
1374 return STATUS_SUCCESS
;
1378 /* helper for the various utf8 mbstowcs functions */
1379 static unsigned int decode_utf8_char( unsigned char ch
, const char **str
, const char *strend
)
1381 /* number of following bytes in sequence based on first byte value (for bytes above 0x7f) */
1382 static const char utf8_length
[128] =
1384 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
1385 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
1386 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
1387 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
1388 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
1389 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
1390 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
1391 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
1394 /* first byte mask depending on UTF-8 sequence length */
1395 static const unsigned char utf8_mask
[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
1397 unsigned int len
= utf8_length
[ch
- 0x80];
1398 unsigned int res
= ch
& utf8_mask
[len
];
1399 const char *end
= *str
+ len
;
1409 if ((ch
= end
[-3] ^ 0x80) >= 0x40) break;
1410 res
= (res
<< 6) | ch
;
1412 if (res
< 0x10) break;
1414 if ((ch
= end
[-2] ^ 0x80) >= 0x40) break;
1415 res
= (res
<< 6) | ch
;
1416 if (res
>= 0x110000 >> 6) break;
1418 if (res
< 0x20) break;
1419 if (res
>= 0xd800 >> 6 && res
<= 0xdfff >> 6) break;
1421 if ((ch
= end
[-1] ^ 0x80) >= 0x40) break;
1422 res
= (res
<< 6) | ch
;
1424 if (res
< 0x80) break;
1431 /**************************************************************************
1432 * RtlUTF8ToUnicodeN (NTDLL.@)
1434 NTSTATUS WINAPI
RtlUTF8ToUnicodeN( WCHAR
*dst
, DWORD dstlen
, DWORD
*reslen
, const char *src
, DWORD srclen
)
1436 unsigned int res
, len
;
1437 NTSTATUS status
= STATUS_SUCCESS
;
1438 const char *srcend
= src
+ srclen
;
1441 if (!src
) return STATUS_INVALID_PARAMETER_4
;
1442 if (!reslen
) return STATUS_INVALID_PARAMETER
;
1444 dstlen
/= sizeof(WCHAR
);
1445 dstend
= dst
+ dstlen
;
1448 for (len
= 0; src
< srcend
; len
++)
1450 unsigned char ch
= *src
++;
1451 if (ch
< 0x80) continue;
1452 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) > 0x10ffff)
1453 status
= STATUS_SOME_NOT_MAPPED
;
1455 if (res
> 0xffff) len
++;
1457 *reslen
= len
* sizeof(WCHAR
);
1461 while ((dst
< dstend
) && (src
< srcend
))
1463 unsigned char ch
= *src
++;
1464 if (ch
< 0x80) /* special fast case for 7-bit ASCII */
1469 if ((res
= decode_utf8_char( ch
, &src
, srcend
)) <= 0xffff)
1473 else if (res
<= 0x10ffff) /* we need surrogates */
1476 *dst
++ = 0xd800 | (res
>> 10);
1477 if (dst
== dstend
) break;
1478 *dst
++ = 0xdc00 | (res
& 0x3ff);
1483 status
= STATUS_SOME_NOT_MAPPED
;
1486 if (src
< srcend
) status
= STATUS_BUFFER_TOO_SMALL
; /* overflow */
1487 *reslen
= (dstlen
- (dstend
- dst
)) * sizeof(WCHAR
);
1492 /**************************************************************************
1493 * RtlUnicodeToUTF8N (NTDLL.@)
1495 NTSTATUS WINAPI
RtlUnicodeToUTF8N( char *dst
, DWORD dstlen
, DWORD
*reslen
, const WCHAR
*src
, DWORD srclen
)
1498 unsigned int val
, len
;
1499 NTSTATUS status
= STATUS_SUCCESS
;
1501 if (!src
) return STATUS_INVALID_PARAMETER_4
;
1502 if (!reslen
) return STATUS_INVALID_PARAMETER
;
1503 if (dst
&& (srclen
& 1)) return STATUS_INVALID_PARAMETER_5
;
1505 srclen
/= sizeof(WCHAR
);
1509 for (len
= 0; srclen
; srclen
--, src
++)
1511 if (*src
< 0x80) len
++; /* 0x00-0x7f: 1 byte */
1512 else if (*src
< 0x800) len
+= 2; /* 0x80-0x7ff: 2 bytes */
1515 if (!get_utf16( src
, srclen
, &val
))
1518 status
= STATUS_SOME_NOT_MAPPED
;
1520 if (val
< 0x10000) len
+= 3; /* 0x800-0xffff: 3 bytes */
1521 else /* 0x10000-0x10ffff: 4 bytes */
1533 for (end
= dst
+ dstlen
; srclen
; srclen
--, src
++)
1537 if (ch
< 0x80) /* 0x00-0x7f: 1 byte */
1539 if (dst
> end
- 1) break;
1543 if (ch
< 0x800) /* 0x80-0x7ff: 2 bytes */
1545 if (dst
> end
- 2) break;
1546 dst
[1] = 0x80 | (ch
& 0x3f);
1552 if (!get_utf16( src
, srclen
, &val
))
1555 status
= STATUS_SOME_NOT_MAPPED
;
1557 if (val
< 0x10000) /* 0x800-0xffff: 3 bytes */
1559 if (dst
> end
- 3) break;
1560 dst
[2] = 0x80 | (val
& 0x3f);
1562 dst
[1] = 0x80 | (val
& 0x3f);
1564 dst
[0] = 0xe0 | val
;
1567 else /* 0x10000-0x10ffff: 4 bytes */
1569 if (dst
> end
- 4) break;
1570 dst
[3] = 0x80 | (val
& 0x3f);
1572 dst
[2] = 0x80 | (val
& 0x3f);
1574 dst
[1] = 0x80 | (val
& 0x3f);
1576 dst
[0] = 0xf0 | val
;
1582 if (srclen
) status
= STATUS_BUFFER_TOO_SMALL
;
1583 *reslen
= dstlen
- (end
- dst
);
1588 /******************************************************************************
1589 * RtlIsNormalizedString (NTDLL.@)
1591 NTSTATUS WINAPI
RtlIsNormalizedString( ULONG form
, const WCHAR
*str
, INT len
, BOOLEAN
*res
)
1593 const struct norm_table
*info
;
1595 BYTE props
, class, last_class
= 0;
1597 int i
, r
, result
= 1;
1599 if ((status
= load_norm_table( form
, &info
))) return status
;
1601 if (len
== -1) len
= wcslen( str
);
1603 for (i
= 0; i
< len
&& result
; i
+= r
)
1605 if (!(r
= get_utf16( str
+ i
, len
- i
, &ch
))) return STATUS_NO_UNICODE_TRANSLATION
;
1606 if (info
->comp_size
)
1608 if ((ch
>= HANGUL_VBASE
&& ch
< HANGUL_VBASE
+ HANGUL_VCOUNT
) ||
1609 (ch
>= HANGUL_TBASE
&& ch
< HANGUL_TBASE
+ HANGUL_TCOUNT
))
1611 result
= -1; /* QC=Maybe */
1615 else if (ch
>= HANGUL_SBASE
&& ch
< HANGUL_SBASE
+ HANGUL_SCOUNT
)
1617 result
= 0; /* QC=No */
1620 props
= get_char_props( info
, ch
);
1621 class = props
& 0x3f;
1625 if (props
== 0xbf) result
= 0; /* QC=No */
1626 else if (props
== 0xff)
1628 /* ignore other chars in Hangul range */
1629 if (ch
>= HANGUL_LBASE
&& ch
< HANGUL_LBASE
+ 0x100) continue;
1630 if (ch
>= HANGUL_SBASE
&& ch
< HANGUL_SBASE
+ 0x2c00) continue;
1631 /* allow final null */
1632 if (!ch
&& i
== len
- 1) continue;
1633 return STATUS_NO_UNICODE_TRANSLATION
;
1636 else if (props
& 0x80)
1638 if ((props
& 0xc0) == 0xc0) result
= -1; /* QC=Maybe */
1639 if (class && class < last_class
) result
= 0; /* QC=No */
1642 else last_class
= 0;
1647 int dstlen
= len
* 4;
1649 WCHAR
*buffer
= RtlAllocateHeap( GetProcessHeap(), 0, dstlen
* sizeof(WCHAR
) );
1650 if (!buffer
) return STATUS_NO_MEMORY
;
1651 status
= RtlNormalizeString( form
, str
, len
, buffer
, &dstlen
);
1652 result
= !status
&& (dstlen
== len
) && !wcsncmp( buffer
, str
, len
);
1653 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
1656 return STATUS_SUCCESS
;
1660 /******************************************************************************
1661 * RtlNormalizeString (NTDLL.@)
1663 NTSTATUS WINAPI
RtlNormalizeString( ULONG form
, const WCHAR
*src
, INT src_len
, WCHAR
*dst
, INT
*dst_len
)
1667 const struct norm_table
*info
;
1668 NTSTATUS status
= STATUS_SUCCESS
;
1670 TRACE( "%x %s %d %p %d\n", form
, debugstr_wn(src
, src_len
), src_len
, dst
, *dst_len
);
1672 if ((status
= load_norm_table( form
, &info
))) return status
;
1674 if (src_len
== -1) src_len
= wcslen(src
) + 1;
1678 *dst_len
= src_len
* info
->len_factor
;
1679 if (*dst_len
> 64) *dst_len
= max( 64, src_len
+ src_len
/ 8 );
1680 return STATUS_SUCCESS
;
1685 return STATUS_SUCCESS
;
1688 if (!info
->comp_size
) return decompose_string( info
, src
, src_len
, dst
, dst_len
);
1690 buf_len
= src_len
* 4;
1693 buf
= RtlAllocateHeap( GetProcessHeap(), 0, buf_len
* sizeof(WCHAR
) );
1694 if (!buf
) return STATUS_NO_MEMORY
;
1695 status
= decompose_string( info
, src
, src_len
, buf
, &buf_len
);
1696 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
1697 RtlFreeHeap( GetProcessHeap(), 0, buf
);
1701 buf_len
= compose_string( info
, buf
, buf_len
);
1702 if (*dst_len
>= buf_len
) memcpy( dst
, buf
, buf_len
* sizeof(WCHAR
) );
1703 else status
= STATUS_BUFFER_TOO_SMALL
;
1705 RtlFreeHeap( GetProcessHeap(), 0, buf
);
1711 /* Punycode parameters */
1712 enum { BASE
= 36, TMIN
= 1, TMAX
= 26, SKEW
= 38, DAMP
= 700 };
1714 static BOOL
check_invalid_chars( const struct norm_table
*info
, DWORD flags
,
1715 const unsigned int *buffer
, int len
)
1719 for (i
= 0; i
< len
; i
++)
1723 case 0x200c: /* zero-width non-joiner */
1724 case 0x200d: /* zero-width joiner */
1725 if (!i
|| get_combining_class( info
, buffer
[i
- 1] ) != 9) return TRUE
;
1727 case 0x2260: /* not equal to */
1728 case 0x226e: /* not less than */
1729 case 0x226f: /* not greater than */
1730 if (flags
& IDN_USE_STD3_ASCII_RULES
) return TRUE
;
1733 switch (get_char_props( info
, buffer
[i
] ))
1738 if (buffer
[i
] >= HANGUL_SBASE
&& buffer
[i
] < HANGUL_SBASE
+ 0x2c00) break;
1741 if (!(flags
& IDN_ALLOW_UNASSIGNED
)) return TRUE
;
1746 if ((flags
& IDN_USE_STD3_ASCII_RULES
) && len
&& (buffer
[0] == '-' || buffer
[len
- 1] == '-'))
1753 /******************************************************************************
1754 * RtlIdnToAscii (NTDLL.@)
1756 NTSTATUS WINAPI
RtlIdnToAscii( DWORD flags
, const WCHAR
*src
, INT srclen
, WCHAR
*dst
, INT
*dstlen
)
1758 static const WCHAR prefixW
[] = {'x','n','-','-'};
1759 const struct norm_table
*info
;
1761 WCHAR normstr
[256], res
[256];
1762 unsigned int ch
, buffer
[64];
1763 int i
, len
, start
, end
, out_label
, out
= 0, normlen
= ARRAY_SIZE(normstr
);
1765 TRACE( "%x %s %p %d\n", flags
, debugstr_wn(src
, srclen
), dst
, *dstlen
);
1767 if ((status
= load_norm_table( 13, &info
))) return status
;
1769 if ((status
= RtlIdnToNameprepUnicode( flags
, src
, srclen
, normstr
, &normlen
))) return status
;
1771 /* implementation of Punycode based on RFC 3492 */
1773 for (start
= 0; start
< normlen
; start
= end
+ 1)
1775 int n
= 0x80, bias
= 72, delta
= 0, b
= 0, h
, buflen
= 0;
1778 for (i
= start
; i
< normlen
; i
+= len
)
1780 if (!(len
= get_utf16( normstr
+ i
, normlen
- i
, &ch
))) break;
1781 if (!ch
|| ch
== '.') break;
1783 buffer
[buflen
++] = ch
;
1787 if (b
== end
- start
)
1789 if (end
< normlen
) b
++;
1790 if (out
+ b
> ARRAY_SIZE(res
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1791 memcpy( res
+ out
, normstr
+ start
, b
* sizeof(WCHAR
) );
1796 if (buflen
>= 4 && buffer
[2] == '-' && buffer
[3] == '-') return STATUS_INVALID_IDN_NORMALIZATION
;
1797 if (check_invalid_chars( info
, flags
, buffer
, buflen
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1799 if (out
+ 5 + b
> ARRAY_SIZE(res
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1800 memcpy( res
+ out
, prefixW
, sizeof(prefixW
) );
1801 out
+= ARRAY_SIZE(prefixW
);
1804 for (i
= start
; i
< end
; i
++) if (normstr
[i
] < 0x80) res
[out
++] = normstr
[i
];
1808 for (h
= b
; h
< buflen
; delta
++, n
++)
1810 int m
= 0x10ffff, q
, k
;
1812 for (i
= 0; i
< buflen
; i
++) if (buffer
[i
] >= n
&& m
> buffer
[i
]) m
= buffer
[i
];
1813 delta
+= (m
- n
) * (h
+ 1);
1816 for (i
= 0; i
< buflen
; i
++)
1820 for (q
= delta
, k
= BASE
; ; k
+= BASE
)
1822 int t
= k
<= bias
? TMIN
: k
>= bias
+ TMAX
? TMAX
: k
- bias
;
1823 int disp
= q
< t
? q
: t
+ (q
- t
) % (BASE
- t
);
1824 if (out
+ 1 > ARRAY_SIZE(res
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1825 res
[out
++] = disp
<= 25 ? 'a' + disp
: '0' + disp
- 26;
1827 q
= (q
- t
) / (BASE
- t
);
1829 delta
/= (h
== b
? DAMP
: 2);
1830 delta
+= delta
/ (h
+ 1);
1831 for (k
= 0; delta
> ((BASE
- TMIN
) * TMAX
) / 2; k
+= BASE
) delta
/= BASE
- TMIN
;
1832 bias
= k
+ ((BASE
- TMIN
+ 1) * delta
) / (delta
+ SKEW
);
1836 else if (buffer
[i
] < n
) delta
++;
1840 if (out
- out_label
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
1844 if (out
+ 1 > ARRAY_SIZE(res
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1845 res
[out
++] = normstr
[end
];
1851 if (out
<= *dstlen
) memcpy( dst
, res
, out
* sizeof(WCHAR
) );
1852 else status
= STATUS_BUFFER_TOO_SMALL
;
1859 /******************************************************************************
1860 * RtlIdnToNameprepUnicode (NTDLL.@)
1862 NTSTATUS WINAPI
RtlIdnToNameprepUnicode( DWORD flags
, const WCHAR
*src
, INT srclen
,
1863 WCHAR
*dst
, INT
*dstlen
)
1865 const struct norm_table
*info
;
1869 int i
, start
, len
, buflen
= ARRAY_SIZE(buf
);
1871 if (flags
& ~(IDN_ALLOW_UNASSIGNED
| IDN_USE_STD3_ASCII_RULES
)) return STATUS_INVALID_PARAMETER
;
1872 if (!src
|| srclen
< -1) return STATUS_INVALID_PARAMETER
;
1874 TRACE( "%x %s %p %d\n", flags
, debugstr_wn(src
, srclen
), dst
, *dstlen
);
1876 if ((status
= load_norm_table( 13, &info
))) return status
;
1878 if (srclen
== -1) srclen
= wcslen(src
) + 1;
1880 for (i
= 0; i
< srclen
; i
++) if (src
[i
] < 0x20 || src
[i
] >= 0x7f) break;
1882 if (i
== srclen
|| (i
== srclen
- 1 && !src
[i
])) /* ascii only */
1884 if (srclen
> buflen
) return STATUS_INVALID_IDN_NORMALIZATION
;
1885 memcpy( buf
, src
, srclen
* sizeof(WCHAR
) );
1888 else if ((status
= RtlNormalizeString( 13, src
, srclen
, buf
, &buflen
)))
1890 if (status
== STATUS_NO_UNICODE_TRANSLATION
) status
= STATUS_INVALID_IDN_NORMALIZATION
;
1894 for (i
= start
= 0; i
< buflen
; i
+= len
)
1896 if (!(len
= get_utf16( buf
+ i
, buflen
- i
, &ch
))) break;
1900 if (start
== i
) return STATUS_INVALID_IDN_NORMALIZATION
;
1901 /* maximal label length is 63 characters */
1902 if (i
- start
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
1903 if ((flags
& IDN_USE_STD3_ASCII_RULES
) && (buf
[start
] == '-' || buf
[i
-1] == '-'))
1904 return STATUS_INVALID_IDN_NORMALIZATION
;
1908 if (flags
& IDN_USE_STD3_ASCII_RULES
)
1910 if ((ch
>= 'a' && ch
<= 'z') || (ch
>= 'A' && ch
<= 'Z') ||
1911 (ch
>= '0' && ch
<= '9') || ch
== '-') continue;
1912 return STATUS_INVALID_IDN_NORMALIZATION
;
1914 if (!(flags
& IDN_ALLOW_UNASSIGNED
))
1916 if (get_char_props( info
, ch
) == 0x7f) return STATUS_INVALID_IDN_NORMALIZATION
;
1919 if (!i
|| i
- start
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
1920 if ((flags
& IDN_USE_STD3_ASCII_RULES
) && (buf
[start
] == '-' || buf
[i
-1] == '-'))
1921 return STATUS_INVALID_IDN_NORMALIZATION
;
1925 if (buflen
<= *dstlen
) memcpy( dst
, buf
, buflen
* sizeof(WCHAR
) );
1926 else status
= STATUS_BUFFER_TOO_SMALL
;
1933 /******************************************************************************
1934 * RtlIdnToUnicode (NTDLL.@)
1936 NTSTATUS WINAPI
RtlIdnToUnicode( DWORD flags
, const WCHAR
*src
, INT srclen
, WCHAR
*dst
, INT
*dstlen
)
1938 const struct norm_table
*info
;
1939 int i
, buflen
, start
, end
, out_label
, out
= 0;
1944 if (!src
|| srclen
< -1) return STATUS_INVALID_PARAMETER
;
1945 if (srclen
== -1) srclen
= wcslen( src
) + 1;
1947 TRACE( "%x %s %p %d\n", flags
, debugstr_wn(src
, srclen
), dst
, *dstlen
);
1949 if ((status
= load_norm_table( 13, &info
))) return status
;
1951 for (start
= 0; start
< srclen
; )
1953 int n
= 0x80, bias
= 72, pos
= 0, old_pos
, w
, k
, t
, delim
= 0, digit
, delta
;
1956 for (i
= start
; i
< srclen
; i
++)
1959 if (ch
> 0x7f || (i
!= srclen
- 1 && !ch
)) return STATUS_INVALID_IDN_NORMALIZATION
;
1960 if (!ch
|| ch
== '.') break;
1961 if (ch
== '-') delim
= i
;
1963 if (!(flags
& IDN_USE_STD3_ASCII_RULES
)) continue;
1964 if ((ch
>= 'a' && ch
<= 'z') || (ch
>= 'A' && ch
<= 'Z') ||
1965 (ch
>= '0' && ch
<= '9') || ch
== '-')
1967 return STATUS_INVALID_IDN_NORMALIZATION
;
1971 /* last label may be empty */
1972 if (start
== end
&& ch
) return STATUS_INVALID_IDN_NORMALIZATION
;
1974 if (end
- start
< 4 ||
1975 (src
[start
] != 'x' && src
[start
] != 'X') ||
1976 (src
[start
+ 1] != 'n' && src
[start
+ 1] != 'N') ||
1977 src
[start
+ 2] != '-' || src
[start
+ 3] != '-')
1979 if (end
- start
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
1981 if ((flags
& IDN_USE_STD3_ASCII_RULES
) && (src
[start
] == '-' || src
[end
- 1] == '-'))
1982 return STATUS_INVALID_IDN_NORMALIZATION
;
1984 if (end
< srclen
) end
++;
1987 if (out
+ end
- start
<= *dstlen
)
1988 memcpy( dst
+ out
, src
+ start
, (end
- start
) * sizeof(WCHAR
));
1989 else return STATUS_BUFFER_TOO_SMALL
;
1996 if (delim
== start
+ 3) delim
++;
1998 for (i
= start
+ 4; i
< delim
&& buflen
< ARRAY_SIZE(buffer
); i
++) buffer
[buflen
++] = src
[i
];
2004 for (k
= BASE
; ; k
+= BASE
)
2006 if (i
>= end
) return STATUS_INVALID_IDN_NORMALIZATION
;
2008 if (ch
>= 'a' && ch
<= 'z') digit
= ch
- 'a';
2009 else if (ch
>= 'A' && ch
<= 'Z') digit
= ch
- 'A';
2010 else if (ch
>= '0' && ch
<= '9') digit
= ch
- '0' + 26;
2011 else return STATUS_INVALID_IDN_NORMALIZATION
;
2013 t
= k
<= bias
? TMIN
: k
>= bias
+ TMAX
? TMAX
: k
- bias
;
2014 if (digit
< t
) break;
2018 delta
= (pos
- old_pos
) / (!old_pos
? DAMP
: 2);
2019 delta
+= delta
/ (buflen
+ 1);
2020 for (k
= 0; delta
> ((BASE
- TMIN
) * TMAX
) / 2; k
+= BASE
) delta
/= BASE
- TMIN
;
2021 bias
= k
+ ((BASE
- TMIN
+ 1) * delta
) / (delta
+ SKEW
);
2022 n
+= pos
/ (buflen
+ 1);
2025 if (buflen
>= ARRAY_SIZE(buffer
) - 1) return STATUS_INVALID_IDN_NORMALIZATION
;
2026 memmove( buffer
+ pos
+ 1, buffer
+ pos
, (buflen
- pos
) * sizeof(*buffer
) );
2031 if (check_invalid_chars( info
, flags
, buffer
, buflen
)) return STATUS_INVALID_IDN_NORMALIZATION
;
2033 for (i
= 0; i
< buflen
; i
++)
2035 int len
= 1 + (buffer
[i
] >= 0x10000);
2038 if (out
+ len
<= *dstlen
) put_utf16( dst
+ out
, buffer
[i
] );
2039 else return STATUS_BUFFER_TOO_SMALL
;
2044 if (out
- out_label
> 63) return STATUS_INVALID_IDN_NORMALIZATION
;
2050 if (out
+ 1 <= *dstlen
) dst
[out
] = src
[end
];
2051 else return STATUS_BUFFER_TOO_SMALL
;
2058 return STATUS_SUCCESS
;