4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright (C) 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 #define GUID_STRING_LENGTH 38
43 UINT NlsAnsiCodePage = 0;
44 BYTE NlsMbCodePageTag = 0;
45 BYTE NlsMbOemCodePageTag = 0;
47 static const union cptable *ansi_table;
48 static const union cptable *oem_table;
49 static const union cptable* unix_table; /* NULL if UTF8 */
52 /**************************************************************************
53 * __wine_init_codepages (NTDLL.@)
55 * Set the code page once kernel32 is loaded. Should be done differently.
57 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
58 const union cptable *ucp)
63 NlsAnsiCodePage = ansi->info.codepage;
66 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
69 /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
70 if (!unix_table) flags |= MB_COMPOSITE;
73 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
74 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
77 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
78 const char* defchar, int *used )
81 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
82 if (used) *used = 0; /* all chars are valid for UTF-8 */
83 return wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
86 /**************************************************************************
87 * RtlInitAnsiString (NTDLL.@)
89 * Initializes a buffered ansi string.
95 * Assigns source to target->Buffer. The length of source is assigned to
96 * target->Length and target->MaximumLength. If source is NULL the length
97 * of source is assumed to be 0.
99 void WINAPI RtlInitAnsiString(
100 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
101 PCSZ source) /* [I] '\0' terminated string used to initialize target */
103 if ((target->Buffer = (PCHAR) source))
105 target->Length = strlen(source);
106 target->MaximumLength = target->Length + 1;
108 else target->Length = target->MaximumLength = 0;
111 /**************************************************************************
112 * RtlInitAnsiStringEx (NTDLL.@)
114 * Initializes a buffered ansi string.
117 * An appropriate NTSTATUS value.
120 * Assigns source to target->Buffer. The length of source is assigned to
121 * target->Length and target->MaximumLength. If source is NULL the length
122 * of source is assumed to be 0.
124 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
128 unsigned int len = strlen(source);
130 return STATUS_NAME_TOO_LONG;
132 target->Buffer = (PCHAR) source;
133 target->Length = len;
134 target->MaximumLength = len + 1;
138 target->Buffer = NULL;
140 target->MaximumLength = 0;
142 return STATUS_SUCCESS;
145 /**************************************************************************
146 * RtlInitString (NTDLL.@)
148 * Initializes a buffered string.
154 * Assigns source to target->Buffer. The length of source is assigned to
155 * target->Length and target->MaximumLength. If source is NULL the length
156 * of source is assumed to be 0.
158 void WINAPI RtlInitString(
159 PSTRING target, /* [I/O] Buffered string to be initialized */
160 PCSZ source) /* [I] '\0' terminated string used to initialize target */
162 RtlInitAnsiString( target, source );
166 /**************************************************************************
167 * RtlFreeAnsiString (NTDLL.@)
169 void WINAPI RtlFreeAnsiString( PSTRING str )
173 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
174 RtlZeroMemory( str, sizeof(*str) );
179 /**************************************************************************
180 * RtlFreeOemString (NTDLL.@)
182 void WINAPI RtlFreeOemString( PSTRING str )
184 RtlFreeAnsiString( str );
188 /**************************************************************************
189 * RtlCopyString (NTDLL.@)
191 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
195 unsigned int len = min( src->Length, dst->MaximumLength );
196 memcpy( dst->Buffer, src->Buffer, len );
199 else dst->Length = 0;
203 /**************************************************************************
204 * RtlInitUnicodeString (NTDLL.@)
206 * Initializes a buffered unicode string.
212 * Assigns source to target->Buffer. The length of source is assigned to
213 * target->Length and target->MaximumLength. If source is NULL the length
214 * of source is assumed to be 0.
216 void WINAPI RtlInitUnicodeString(
217 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
218 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
220 if ((target->Buffer = (PWSTR) source))
222 unsigned int length = strlenW(source) * sizeof(WCHAR);
225 target->Length = length;
226 target->MaximumLength = target->Length + sizeof(WCHAR);
228 else target->Length = target->MaximumLength = 0;
232 /**************************************************************************
233 * RtlInitUnicodeStringEx (NTDLL.@)
235 * Initializes a buffered unicode string.
238 * Success: STATUS_SUCCESS. target is initialized.
239 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
242 * Assigns source to target->Buffer. The length of source is assigned to
243 * target->Length and target->MaximumLength. If source is NULL the length
244 * of source is assumed to be 0.
246 NTSTATUS WINAPI RtlInitUnicodeStringEx(
247 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
248 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
250 if (source != NULL) {
251 unsigned int len = strlenW(source) * sizeof(WCHAR);
254 return STATUS_NAME_TOO_LONG;
256 target->Length = len;
257 target->MaximumLength = len + sizeof(WCHAR);
258 target->Buffer = (PWSTR) source;
262 target->MaximumLength = 0;
263 target->Buffer = NULL;
265 return STATUS_SUCCESS;
269 /**************************************************************************
270 * RtlCreateUnicodeString (NTDLL.@)
272 * Creates a UNICODE_STRING from a null-terminated Unicode string.
278 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
280 int len = (strlenW(src) + 1) * sizeof(WCHAR);
281 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
282 memcpy( target->Buffer, src, len );
283 target->MaximumLength = len;
284 target->Length = len - sizeof(WCHAR);
289 /**************************************************************************
290 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
292 * Creates a UNICODE_STRING from a null-terminated Ascii string.
298 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
301 RtlInitAnsiString( &ansi, src );
302 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
306 /**************************************************************************
307 * RtlFreeUnicodeString (NTDLL.@)
309 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
310 * RtlCreateUnicodeStringFromAsciiz().
315 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
319 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
320 RtlZeroMemory( str, sizeof(*str) );
325 /**************************************************************************
326 * RtlCopyUnicodeString (NTDLL.@)
328 * Copies from one UNICODE_STRING to another.
333 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
337 unsigned int len = min( src->Length, dst->MaximumLength );
338 memcpy( dst->Buffer, src->Buffer, len );
340 /* append terminating '\0' if enough space */
341 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
343 else dst->Length = 0;
347 /**************************************************************************
348 * RtlDuplicateUnicodeString (NTDLL.@)
350 * Duplicates an unicode string.
353 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
354 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
355 * STATUS_NO_MEMORY, if the allocation fails.
358 * For add_nul there are several possible values:
359 * 0 = destination will not be '\0' terminated,
360 * 1 = destination will be '\0' terminated,
361 * 3 = like 1 but for an empty source string produce '\0' terminated empty
362 * Buffer instead of assigning NULL to the Buffer.
363 * Other add_nul values are invalid.
365 NTSTATUS WINAPI RtlDuplicateUnicodeString(
366 int add_nul, /* [I] flag */
367 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
368 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
370 if (source == NULL || destination == NULL ||
371 source->Length > source->MaximumLength ||
372 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
373 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
374 return STATUS_INVALID_PARAMETER;
376 if (source->Length == 0 && add_nul != 3) {
377 destination->Length = 0;
378 destination->MaximumLength = 0;
379 destination->Buffer = NULL;
381 unsigned int destination_max_len = source->Length;
384 destination_max_len += sizeof(WCHAR);
386 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
387 if (destination->Buffer == NULL) {
388 return STATUS_NO_MEMORY;
390 memcpy(destination->Buffer, source->Buffer, source->Length);
391 destination->Length = source->Length;
392 destination->MaximumLength = source->Length;
393 /* append terminating '\0' if enough space */
395 destination->MaximumLength = destination_max_len;
396 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
401 return STATUS_SUCCESS;
405 /**************************************************************************
406 * RtlEraseUnicodeString (NTDLL.@)
408 * Overwrites a UNICODE_STRING with zeros.
413 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
417 memset( str->Buffer, 0, str->MaximumLength );
428 /******************************************************************************
429 * RtlCompareString (NTDLL.@)
431 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
437 len = min(s1->Length, s2->Length);
443 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
447 while (!ret && len--) ret = *p1++ - *p2++;
449 if (!ret) ret = s1->Length - s2->Length;
454 /******************************************************************************
455 * RtlCompareUnicodeString (NTDLL.@)
457 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
458 BOOLEAN CaseInsensitive )
464 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
470 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
474 while (!ret && len--) ret = *p1++ - *p2++;
476 if (!ret) ret = s1->Length - s2->Length;
481 /**************************************************************************
482 * RtlEqualString (NTDLL.@)
484 * Determine if two strings are equal.
487 * s1 [I] Source string
488 * s2 [I] String to compare to s1
489 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
492 * Non-zero if s1 is equal to s2, 0 otherwise.
494 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
496 if (s1->Length != s2->Length) return FALSE;
497 return !RtlCompareString( s1, s2, CaseInsensitive );
501 /**************************************************************************
502 * RtlEqualUnicodeString (NTDLL.@)
504 * Unicode version of RtlEqualString.
506 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
507 BOOLEAN CaseInsensitive )
509 if (s1->Length != s2->Length) return FALSE;
510 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
514 /**************************************************************************
515 * RtlPrefixString (NTDLL.@)
517 * Determine if one string is a prefix of another.
520 * s1 [I] Prefix to look for in s2
521 * s2 [I] String that may contain s1 as a prefix
522 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
525 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
527 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
531 if (s1->Length > s2->Length) return FALSE;
534 for (i = 0; i < s1->Length; i++)
535 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
539 for (i = 0; i < s1->Length; i++)
540 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
546 /**************************************************************************
547 * RtlPrefixUnicodeString (NTDLL.@)
549 * Unicode version of RtlPrefixString.
551 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
552 const UNICODE_STRING *s2,
553 BOOLEAN ignore_case )
557 if (s1->Length > s2->Length) return FALSE;
560 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
561 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
565 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
566 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
572 /**************************************************************************
573 * RtlEqualComputerName (NTDLL.@)
575 * Determine if two computer names are the same.
578 * left [I] First computer name
579 * right [I] Second computer name
582 * 0 if the names are equal, non-zero otherwise.
585 * The comparison is case insensitive.
587 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
588 const UNICODE_STRING *right)
591 STRING upLeft, upRight;
593 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
595 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
597 ret = RtlEqualString( &upLeft, &upRight, FALSE );
598 RtlFreeOemString( &upRight );
600 RtlFreeOemString( &upLeft );
606 /**************************************************************************
607 * RtlEqualDomainName (NTDLL.@)
609 * Determine if two domain names are the same.
612 * left [I] First domain name
613 * right [I] Second domain name
616 * 0 if the names are equal, non-zero otherwise.
619 * The comparison is case insensitive.
621 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
622 const UNICODE_STRING *right)
624 return RtlEqualComputerName(left, right);
628 /**************************************************************************
629 * RtlAnsiCharToUnicodeChar (NTDLL.@)
631 * Converts the first ansi character to a unicode character.
634 * ansi [I/O] Pointer to the ansi string.
637 * Unicode representation of the first character in the ansi string.
640 * Upon successful completion, the char pointer ansi points to is
641 * incremented by the size of the character.
643 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
646 DWORD charSize = sizeof(CHAR);
648 if (wine_is_dbcs_leadbyte(ansi_table, **ansi))
651 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
658 COPY BETWEEN ANSI_STRING or UNICODE_STRING
659 there is no parameter checking, it just crashes
663 /**************************************************************************
664 * RtlAnsiStringToUnicodeString (NTDLL.@)
666 * Converts an ansi string to an unicode string.
669 * Success: STATUS_SUCCESS. uni contains the converted string
670 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
671 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
672 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
675 * This function always writes a terminating '\0'.
677 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
678 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
679 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
680 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
682 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
684 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
685 uni->Length = total - sizeof(WCHAR);
688 uni->MaximumLength = total;
689 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
690 return STATUS_NO_MEMORY;
692 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
694 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
695 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
696 return STATUS_SUCCESS;
700 /**************************************************************************
701 * RtlOemStringToUnicodeString (NTDLL.@)
703 * Converts an oem string to an unicode string.
706 * Success: STATUS_SUCCESS. uni contains the converted string
707 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
708 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
709 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
712 * This function always writes a terminating '\0'.
714 NTSTATUS WINAPI RtlOemStringToUnicodeString(
715 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
716 const STRING *oem, /* [I] Oem string to be converted */
717 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
719 DWORD total = RtlOemStringToUnicodeSize( oem );
721 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
722 uni->Length = total - sizeof(WCHAR);
725 uni->MaximumLength = total;
726 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
727 return STATUS_NO_MEMORY;
729 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
731 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
732 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
733 return STATUS_SUCCESS;
737 /**************************************************************************
738 * RtlUnicodeStringToAnsiString (NTDLL.@)
740 * Converts an unicode string to an ansi string.
743 * Success: STATUS_SUCCESS. ansi contains the converted string
744 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
745 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
748 * This function always writes a terminating '\0'.
749 * It performs a partial copy if ansi is too small.
751 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
752 STRING *ansi, /* [I/O] Destination for the ansi string */
753 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
754 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
756 NTSTATUS ret = STATUS_SUCCESS;
757 DWORD len = RtlUnicodeStringToAnsiSize( uni );
759 ansi->Length = len - 1;
762 ansi->MaximumLength = len;
763 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
764 return STATUS_NO_MEMORY;
766 else if (ansi->MaximumLength < len)
768 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
769 ansi->Length = ansi->MaximumLength - 1;
770 ret = STATUS_BUFFER_OVERFLOW;
773 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
774 ansi->Buffer[ansi->Length] = 0;
779 /**************************************************************************
780 * RtlUnicodeStringToOemString (NTDLL.@)
782 * Converts a Rtl Unicode string to an OEM string.
785 * oem [O] Destination for OEM string
786 * uni [I] Source Unicode string
787 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
790 * Success: STATUS_SUCCESS. oem contains the converted string
791 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
792 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
795 * If doalloc is TRUE, the length allocated is uni->Length + 1.
796 * This function always '\0' terminates the string returned.
798 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
799 const UNICODE_STRING *uni,
802 NTSTATUS ret = STATUS_SUCCESS;
803 DWORD len = RtlUnicodeStringToOemSize( uni );
805 oem->Length = len - 1;
808 oem->MaximumLength = len;
809 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
810 return STATUS_NO_MEMORY;
812 else if (oem->MaximumLength < len)
814 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
815 oem->Length = oem->MaximumLength - 1;
816 ret = STATUS_BUFFER_OVERFLOW;
819 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
820 oem->Buffer[oem->Length] = 0;
825 /**************************************************************************
826 * RtlMultiByteToUnicodeN (NTDLL.@)
828 * Converts a multi-byte string to a Unicode string.
834 * Performs a partial copy if dst is too small.
836 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
837 LPCSTR src, DWORD srclen )
840 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
842 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
843 return STATUS_SUCCESS;
847 /**************************************************************************
848 * RtlOemToUnicodeN (NTDLL.@)
850 * Converts a multi-byte string in the OEM code page to a Unicode string.
855 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
856 LPCSTR src, DWORD srclen )
858 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
860 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
861 return STATUS_SUCCESS;
865 /**************************************************************************
866 * RtlUnicodeToMultiByteN (NTDLL.@)
868 * Converts a Unicode string to a multi-byte string in the ANSI code page.
873 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
874 LPCWSTR src, DWORD srclen )
876 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
877 dst, dstlen, NULL, NULL );
879 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
880 return STATUS_SUCCESS;
884 /**************************************************************************
885 * RtlUnicodeToOemN (NTDLL.@)
887 * Converts a Unicode string to a multi-byte string in the OEM code page.
892 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
893 LPCWSTR src, DWORD srclen )
895 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
896 dst, dstlen, NULL, NULL );
898 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
899 return STATUS_SUCCESS;
908 /**************************************************************************
909 * RtlUpperChar (NTDLL.@)
911 * Converts an Ascii character to uppercase.
914 * ch [I] Character to convert
917 * The uppercase character value.
920 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
921 * All other input characters are returned unchanged. The locale and
922 * multibyte characters are not taken into account (as native DLL).
924 CHAR WINAPI RtlUpperChar( CHAR ch )
926 if (ch >= 'a' && ch <= 'z') {
927 return ch - 'a' + 'A';
934 /**************************************************************************
935 * RtlUpperString (NTDLL.@)
937 * Converts an Ascii string to uppercase.
940 * dst [O] Destination for converted string
941 * src [I] Source string to convert
947 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
948 * All other src characters are copied unchanged to dst. The locale and
949 * multibyte characters are not taken into account (as native DLL).
950 * The number of character copied is the minimum of src->Length and
951 * the dst->MaximumLength.
953 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
955 unsigned int i, len = min(src->Length, dst->MaximumLength);
957 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
962 /**************************************************************************
963 * RtlUpcaseUnicodeChar (NTDLL.@)
965 * Converts an Unicode character to uppercase.
968 * wch [I] Character to convert
971 * The uppercase character value.
973 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
975 return toupperW(wch);
979 /**************************************************************************
980 * RtlDowncaseUnicodeChar (NTDLL.@)
982 * Converts an Unicode character to lowercase.
985 * wch [I] Character to convert
988 * The lowercase character value.
990 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
992 return tolowerW(wch);
996 /**************************************************************************
997 * RtlUpcaseUnicodeString (NTDLL.@)
999 * Converts an Unicode string to uppercase.
1002 * dest [O] Destination for converted string
1003 * src [I] Source string to convert
1004 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1007 * Success: STATUS_SUCCESS. dest contains the converted string.
1008 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1009 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1012 * dest is never '\0' terminated because it may be equal to src, and src
1013 * might not be '\0' terminated. dest->Length is only set upon success.
1015 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
1016 const UNICODE_STRING *src,
1019 DWORD i, len = src->Length;
1023 dest->MaximumLength = len;
1024 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1025 return STATUS_NO_MEMORY;
1027 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1029 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
1031 return STATUS_SUCCESS;
1035 /**************************************************************************
1036 * RtlDowncaseUnicodeString (NTDLL.@)
1038 * Converts an Unicode string to lowercase.
1041 * dest [O] Destination for converted string
1042 * src [I] Source string to convert
1043 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1046 * Success: STATUS_SUCCESS. dest contains the converted string.
1047 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1048 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1051 * dest is never '\0' terminated because it may be equal to src, and src
1052 * might not be '\0' terminated. dest->Length is only set upon success.
1054 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1055 UNICODE_STRING *dest,
1056 const UNICODE_STRING *src,
1060 DWORD len = src->Length;
1063 dest->MaximumLength = len;
1064 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1065 return STATUS_NO_MEMORY;
1067 } else if (len > dest->MaximumLength) {
1068 return STATUS_BUFFER_OVERFLOW;
1071 for (i = 0; i < len/sizeof(WCHAR); i++) {
1072 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1075 return STATUS_SUCCESS;
1079 /**************************************************************************
1080 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1082 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1088 * writes terminating 0
1090 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1091 const UNICODE_STRING *src,
1095 UNICODE_STRING upcase;
1097 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1099 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1100 RtlFreeUnicodeString( &upcase );
1106 /**************************************************************************
1107 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1109 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1110 * stored in STRING format.
1116 * writes terminating 0
1118 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1119 const UNICODE_STRING *src,
1123 UNICODE_STRING upcase;
1125 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1127 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1128 RtlFreeUnicodeString( &upcase );
1134 /**************************************************************************
1135 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1137 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1138 * stored in STRING format.
1144 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1146 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1147 const UNICODE_STRING *uni,
1151 UNICODE_STRING upcase;
1154 upcase.Buffer = tmp;
1155 upcase.MaximumLength = sizeof(tmp);
1156 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1157 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1161 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1165 oem->MaximumLength = len;
1166 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1168 ret = STATUS_NO_MEMORY;
1172 else if (oem->MaximumLength < len)
1174 ret = STATUS_BUFFER_OVERFLOW;
1175 oem->Length = oem->MaximumLength;
1176 if (!oem->MaximumLength) goto done;
1178 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1180 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1186 /**************************************************************************
1187 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1189 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1194 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1195 LPCWSTR src, DWORD srclen )
1201 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1202 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1203 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1204 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1209 /**************************************************************************
1210 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1212 * Converts a Unicode string to the equivalent OEM upper-case representation.
1217 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1218 LPCWSTR src, DWORD srclen )
1224 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1225 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1226 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1227 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1237 /**************************************************************************
1238 * RtlOemStringToUnicodeSize (NTDLL.@)
1239 * RtlxOemStringToUnicodeSize (NTDLL.@)
1241 * Calculate the size in bytes necessary for the Unicode conversion of str,
1242 * including the terminating '\0'.
1245 * str [I] String to calculate the size of
1248 * The calculated size.
1250 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1252 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1253 return (ret + 1) * sizeof(WCHAR);
1257 /**************************************************************************
1258 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1259 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1261 * Calculate the size in bytes necessary for the Unicode conversion of str,
1262 * including the terminating '\0'.
1265 * str [I] String to calculate the size of
1268 * The calculated size.
1270 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1273 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1274 return ret + sizeof(WCHAR);
1278 /**************************************************************************
1279 * RtlMultiByteToUnicodeSize (NTDLL.@)
1281 * Compute the size in bytes necessary for the Unicode conversion of str,
1282 * without the terminating '\0'.
1285 * size [O] Destination for size
1286 * str [I] String to calculate the size of
1287 * len [I] Length of str
1292 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1294 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1295 return STATUS_SUCCESS;
1299 /**************************************************************************
1300 * RtlUnicodeToMultiByteSize (NTDLL.@)
1302 * Calculate the size in bytes necessary for the multibyte conversion of str,
1303 * without the terminating '\0'.
1306 * size [O] Destination for size
1307 * str [I] String to calculate the size of
1308 * len [I] Length of str
1313 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1315 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1316 return STATUS_SUCCESS;
1320 /**************************************************************************
1321 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1322 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1324 * Calculate the size in bytes necessary for the Ansi conversion of str,
1325 * including the terminating '\0'.
1328 * str [I] String to calculate the size of
1331 * The calculated size.
1333 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1336 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1341 /**************************************************************************
1342 * RtlUnicodeStringToOemSize (NTDLL.@)
1343 * RtlxUnicodeStringToOemSize (NTDLL.@)
1345 * Calculate the size in bytes necessary for the OEM conversion of str,
1346 * including the terminating '\0'.
1349 * str [I] String to calculate the size of
1352 * The calculated size.
1354 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1356 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1357 NULL, 0, NULL, NULL ) + 1;
1361 /**************************************************************************
1362 * RtlAppendAsciizToString (NTDLL.@)
1364 * Concatenates a buffered character string and a '\0' terminated character
1368 * Success: STATUS_SUCCESS. src is appended to dest.
1369 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1370 * to hold the concatenated string.
1373 * if src is NULL dest is unchanged.
1374 * dest is never '\0' terminated.
1376 NTSTATUS WINAPI RtlAppendAsciizToString(
1377 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1378 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1381 unsigned int src_len = strlen(src);
1382 unsigned int dest_len = src_len + dest->Length;
1384 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1385 memcpy(dest->Buffer + dest->Length, src, src_len);
1386 dest->Length = dest_len;
1388 return STATUS_SUCCESS;
1392 /**************************************************************************
1393 * RtlAppendStringToString (NTDLL.@)
1395 * Concatenates two buffered character strings
1398 * Success: STATUS_SUCCESS. src is appended to dest.
1399 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1400 * to hold the concatenated string.
1403 * if src->length is zero dest is unchanged.
1404 * dest is never '\0' terminated.
1406 NTSTATUS WINAPI RtlAppendStringToString(
1407 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1408 const STRING *src) /* [I] Buffered character string to be concatenated */
1410 if (src->Length != 0) {
1411 unsigned int dest_len = src->Length + dest->Length;
1413 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1414 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1415 dest->Length = dest_len;
1417 return STATUS_SUCCESS;
1421 /**************************************************************************
1422 * RtlAppendUnicodeToString (NTDLL.@)
1424 * Concatenates a buffered unicode string and a '\0' terminated unicode
1428 * Success: STATUS_SUCCESS. src is appended to dest.
1429 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1430 * to hold the concatenated string.
1433 * if src is NULL dest is unchanged.
1434 * dest is '\0' terminated when the MaximumLength allows it.
1435 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1438 * Does not write in the src->Buffer beyond MaximumLength when
1439 * MaximumLength is odd as the native function does.
1441 NTSTATUS WINAPI RtlAppendUnicodeToString(
1442 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1443 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1446 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1447 unsigned int dest_len = src_len + dest->Length;
1449 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1450 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1451 dest->Length = dest_len;
1452 /* append terminating '\0' if enough space */
1453 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1454 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1457 return STATUS_SUCCESS;
1461 /**************************************************************************
1462 * RtlAppendUnicodeStringToString (NTDLL.@)
1464 * Concatenates two buffered unicode strings
1467 * Success: STATUS_SUCCESS. src is appended to dest.
1468 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1469 * to hold the concatenated string.
1472 * if src->length is zero dest is unchanged.
1473 * dest is '\0' terminated when the MaximumLength allows it.
1474 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1477 * Does not write in the src->Buffer beyond MaximumLength when
1478 * MaximumLength is odd as the native function does.
1480 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1481 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1482 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1484 if (src->Length != 0) {
1485 unsigned int dest_len = src->Length + dest->Length;
1487 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1488 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1489 dest->Length = dest_len;
1490 /* append terminating '\0' if enough space */
1491 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1492 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1495 return STATUS_SUCCESS;
1499 /**************************************************************************
1500 * RtlFindCharInUnicodeString (NTDLL.@)
1502 * Searches for one of several unicode characters in an unicode string.
1505 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1506 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1508 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1509 int flags, /* [I] Flags */
1510 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1511 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1512 USHORT *pos) /* [O] Position of the first character found + 2 */
1515 unsigned int search_idx;
1519 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1520 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1521 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1522 *pos = (main_idx + 1) * sizeof(WCHAR);
1523 return STATUS_SUCCESS;
1528 return STATUS_NOT_FOUND;
1530 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1531 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1532 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1533 *pos = main_idx * sizeof(WCHAR);
1534 return STATUS_SUCCESS;
1539 return STATUS_NOT_FOUND;
1541 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1543 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1544 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1547 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1548 *pos = (main_idx + 1) * sizeof(WCHAR);
1549 return STATUS_SUCCESS;
1553 return STATUS_NOT_FOUND;
1555 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1557 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1558 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1561 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1562 *pos = main_idx * sizeof(WCHAR);
1563 return STATUS_SUCCESS;
1567 return STATUS_NOT_FOUND;
1569 return STATUS_NOT_FOUND;
1577 /**************************************************************************
1578 * RtlIsTextUnicode (NTDLL.@)
1580 * Attempt to guess whether a text buffer is Unicode.
1583 * buf [I] Text buffer to test
1584 * len [I] Length of buf
1585 * pf [O] Destination for test results
1588 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1591 * Should implement more tests.
1593 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1595 static const WCHAR std_control_chars[] = {'\r','\n','\t',' ',0x3000,0};
1596 static const WCHAR byterev_control_chars[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1597 const WCHAR *s = buf;
1599 unsigned int flags = ~0U, out_flags = 0;
1601 if (len < sizeof(WCHAR))
1603 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1610 * Apply various tests to the text string. According to the
1611 * docs, each test "passed" sets the corresponding flag in
1612 * the output flags. But some of the tests are mutually
1613 * exclusive, so I don't see how you could pass all tests ...
1616 /* Check for an odd length ... pass if even. */
1617 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1619 if (((char *)buf)[len - 1] == 0)
1620 len--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1622 len /= sizeof(WCHAR);
1623 /* Windows only checks the first 256 characters */
1624 if (len > 256) len = 256;
1626 /* Check for the special byte order unicode marks. */
1627 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1628 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1630 /* apply some statistical analysis */
1631 if (flags & IS_TEXT_UNICODE_STATISTICS)
1634 /* FIXME: checks only for ASCII characters in the unicode stream */
1635 for (i = 0; i < len; i++)
1637 if (s[i] <= 255) stats++;
1639 if (stats > len / 2)
1640 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1643 /* Check for unicode NULL chars */
1644 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1646 for (i = 0; i < len; i++)
1648 if (!(s[i] & 0xff) || !(s[i] >> 8))
1650 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1656 if (flags & IS_TEXT_UNICODE_CONTROLS)
1658 for (i = 0; i < len; i++)
1660 if (strchrW(std_control_chars, s[i]))
1662 out_flags |= IS_TEXT_UNICODE_CONTROLS;
1668 if (flags & IS_TEXT_UNICODE_REVERSE_CONTROLS)
1670 for (i = 0; i < len; i++)
1672 if (strchrW(byterev_control_chars, s[i]))
1674 out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS;
1685 /* check for flags that indicate it's definitely not valid Unicode */
1686 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1687 /* now check for invalid ASCII, and assume Unicode if so */
1688 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1689 /* now check for Unicode flags */
1690 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1696 /**************************************************************************
1697 * RtlCharToInteger (NTDLL.@)
1699 * Converts a character string into its integer equivalent.
1702 * Success: STATUS_SUCCESS. value contains the converted number
1703 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1704 * STATUS_ACCESS_VIOLATION, if value is NULL.
1707 * For base 0 it uses 10 as base and the string should be in the format
1708 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1709 * For other bases the string should be in the format
1710 * "{whitespace} [+|-] {digits}".
1711 * No check is made for value overflow, only the lower 32 bits are assigned.
1712 * If str is NULL it crashes, as the native function does.
1715 * This function does not read garbage behind '\0' as the native version does.
1717 NTSTATUS WINAPI RtlCharToInteger(
1718 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1719 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1720 ULONG *value) /* [O] Destination for the converted value */
1724 ULONG RunningTotal = 0;
1727 while (*str != '\0' && *str <= ' ') {
1733 } else if (*str == '-') {
1740 if (str[0] == '0') {
1741 if (str[1] == 'b') {
1744 } else if (str[1] == 'o') {
1747 } else if (str[1] == 'x') {
1752 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1753 return STATUS_INVALID_PARAMETER;
1756 if (value == NULL) {
1757 return STATUS_ACCESS_VIOLATION;
1760 while (*str != '\0') {
1762 if (chCurrent >= '0' && chCurrent <= '9') {
1763 digit = chCurrent - '0';
1764 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1765 digit = chCurrent - 'A' + 10;
1766 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1767 digit = chCurrent - 'a' + 10;
1771 if (digit < 0 || digit >= base) {
1772 *value = bMinus ? -RunningTotal : RunningTotal;
1773 return STATUS_SUCCESS;
1776 RunningTotal = RunningTotal * base + digit;
1780 *value = bMinus ? -RunningTotal : RunningTotal;
1781 return STATUS_SUCCESS;
1785 /**************************************************************************
1786 * RtlIntegerToChar (NTDLL.@)
1788 * Converts an unsigned integer to a character string.
1791 * Success: STATUS_SUCCESS. str contains the converted number
1792 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1793 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1794 * STATUS_ACCESS_VIOLATION, if str is NULL.
1797 * Instead of base 0 it uses 10 as base.
1798 * Writes at most length characters to the string str.
1799 * Str is '\0' terminated when length allows it.
1800 * When str fits exactly in length characters the '\0' is omitted.
1802 NTSTATUS WINAPI RtlIntegerToChar(
1803 ULONG value, /* [I] Value to be converted */
1804 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1805 ULONG length, /* [I] Length of the str buffer in bytes */
1806 PCHAR str) /* [O] Destination for the converted value */
1815 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1816 return STATUS_INVALID_PARAMETER;
1824 digit = value % base;
1825 value = value / base;
1829 *pos = 'A' + digit - 10;
1831 } while (value != 0L);
1833 len = &buffer[32] - pos;
1835 return STATUS_BUFFER_OVERFLOW;
1836 } else if (str == NULL) {
1837 return STATUS_ACCESS_VIOLATION;
1838 } else if (len == length) {
1839 memcpy(str, pos, len);
1841 memcpy(str, pos, len + 1);
1843 return STATUS_SUCCESS;
1847 /**************************************************************************
1848 * RtlUnicodeStringToInteger (NTDLL.@)
1850 * Converts an unicode string into its integer equivalent.
1853 * Success: STATUS_SUCCESS. value contains the converted number
1854 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1855 * STATUS_ACCESS_VIOLATION, if value is NULL.
1858 * For base 0 it uses 10 as base and the string should be in the format
1859 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1860 * For other bases the string should be in the format
1861 * "{whitespace} [+|-] {digits}".
1862 * No check is made for value overflow, only the lower 32 bits are assigned.
1863 * If str is NULL it crashes, as the native function does.
1866 * This function does not read garbage on string length 0 as the native
1869 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1870 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1871 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1872 ULONG *value) /* [O] Destination for the converted value */
1874 LPWSTR lpwstr = str->Buffer;
1875 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1878 ULONG RunningTotal = 0;
1881 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1886 if (CharsRemaining >= 1) {
1887 if (*lpwstr == '+') {
1890 } else if (*lpwstr == '-') {
1899 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1900 if (lpwstr[1] == 'b') {
1902 CharsRemaining -= 2;
1904 } else if (lpwstr[1] == 'o') {
1906 CharsRemaining -= 2;
1908 } else if (lpwstr[1] == 'x') {
1910 CharsRemaining -= 2;
1914 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1915 return STATUS_INVALID_PARAMETER;
1918 if (value == NULL) {
1919 return STATUS_ACCESS_VIOLATION;
1922 while (CharsRemaining >= 1) {
1923 wchCurrent = *lpwstr;
1924 if (wchCurrent >= '0' && wchCurrent <= '9') {
1925 digit = wchCurrent - '0';
1926 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1927 digit = wchCurrent - 'A' + 10;
1928 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1929 digit = wchCurrent - 'a' + 10;
1933 if (digit < 0 || digit >= base) {
1934 *value = bMinus ? -RunningTotal : RunningTotal;
1935 return STATUS_SUCCESS;
1938 RunningTotal = RunningTotal * base + digit;
1943 *value = bMinus ? -RunningTotal : RunningTotal;
1944 return STATUS_SUCCESS;
1948 /**************************************************************************
1949 * RtlIntegerToUnicodeString (NTDLL.@)
1951 * Converts an unsigned integer to a '\0' terminated unicode string.
1954 * Success: STATUS_SUCCESS. str contains the converted number
1955 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1956 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1957 * (with the '\0' termination). In this case str->Length
1958 * is set to the length, the string would have (which can
1959 * be larger than the MaximumLength).
1962 * Instead of base 0 it uses 10 as base.
1963 * If str is NULL it crashes, as the native function does.
1966 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1967 * The native function does this when the string would be longer than 16
1968 * characters even when the string parameter is long enough.
1970 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1971 ULONG value, /* [I] Value to be converted */
1972 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1973 UNICODE_STRING *str) /* [O] Destination for the converted value */
1981 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1982 return STATUS_INVALID_PARAMETER;
1990 digit = value % base;
1991 value = value / base;
1995 *pos = 'A' + digit - 10;
1997 } while (value != 0L);
1999 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
2000 if (str->Length >= str->MaximumLength) {
2001 return STATUS_BUFFER_OVERFLOW;
2003 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
2005 return STATUS_SUCCESS;
2009 /*************************************************************************
2010 * RtlGUIDFromString (NTDLL.@)
2012 * Convert a string representation of a GUID into a GUID.
2015 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
2016 * guid [O] Destination for the converted GUID
2019 * Success: STATUS_SUCCESS. guid contains the converted value.
2020 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
2023 * See RtlStringFromGUID.
2025 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
2028 const WCHAR *lpszCLSID = str->Buffer;
2029 BYTE* lpOut = (BYTE*)guid;
2031 TRACE("(%s,%p)\n", debugstr_us(str), guid);
2033 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2034 * to memory: DWORD... WORD WORD BYTES............
2041 if (*lpszCLSID != '{')
2042 return STATUS_INVALID_PARAMETER;
2045 case 9: case 14: case 19: case 24:
2046 if (*lpszCLSID != '-')
2047 return STATUS_INVALID_PARAMETER;
2051 if (*lpszCLSID != '}')
2052 return STATUS_INVALID_PARAMETER;
2057 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
2060 /* Read two hex digits as a byte value */
2061 if (ch >= '0' && ch <= '9') ch = ch - '0';
2062 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
2063 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
2064 else return STATUS_INVALID_PARAMETER;
2066 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
2067 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
2068 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
2069 else return STATUS_INVALID_PARAMETER;
2071 byte = ch << 4 | ch2;
2075 #ifndef WORDS_BIGENDIAN
2076 /* For Big Endian machines, we store the data such that the
2077 * dword/word members can be read as DWORDS and WORDS correctly. */
2079 case 1: lpOut[3] = byte; break;
2080 case 3: lpOut[2] = byte; break;
2081 case 5: lpOut[1] = byte; break;
2082 case 7: lpOut[0] = byte; lpOut += 4; break;
2084 case 10: case 15: lpOut[1] = byte; break;
2085 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2088 default: lpOut[0] = byte; lpOut++; break;
2090 lpszCLSID++; /* Skip 2nd character of byte */
2098 return STATUS_SUCCESS;
2101 /*************************************************************************
2102 * RtlStringFromGUID (NTDLL.@)
2104 * Convert a GUID into a string representation of a GUID.
2107 * guid [I] GUID to convert
2108 * str [O] Destination for the converted string
2111 * Success: STATUS_SUCCESS. str contains the converted value.
2112 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2115 * See RtlGUIDFromString.
2117 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2119 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2120 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2121 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2122 '%','0','2','X','%','0','2','X','}','\0' };
2124 TRACE("(%p,%p)\n", guid, str);
2126 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2127 str->MaximumLength = str->Length + sizeof(WCHAR);
2128 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2131 str->Length = str->MaximumLength = 0;
2132 return STATUS_NO_MEMORY;
2134 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2135 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2136 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2138 return STATUS_SUCCESS;