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 flags |= MB_COMPOSITE; /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
72 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
73 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
76 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
77 const char* defchar, int *used )
80 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
81 if (used) *used = 0; /* all chars are valid for UTF-8 */
82 return wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
85 /**************************************************************************
86 * RtlInitAnsiString (NTDLL.@)
88 * Initializes a buffered ansi string.
94 * Assigns source to target->Buffer. The length of source is assigned to
95 * target->Length and target->MaximumLength. If source is NULL the length
96 * of source is assumed to be 0.
98 void WINAPI RtlInitAnsiString(
99 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
100 PCSZ source) /* [I] '\0' terminated string used to initialize target */
102 if ((target->Buffer = (PCHAR) source))
104 target->Length = strlen(source);
105 target->MaximumLength = target->Length + 1;
107 else target->Length = target->MaximumLength = 0;
110 /**************************************************************************
111 * RtlInitAnsiStringEx (NTDLL.@)
113 * Initializes a buffered ansi string.
116 * An appropriate NTSTATUS value.
119 * Assigns source to target->Buffer. The length of source is assigned to
120 * target->Length and target->MaximumLength. If source is NULL the length
121 * of source is assumed to be 0.
123 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
127 unsigned int len = strlen(source);
129 return STATUS_NAME_TOO_LONG;
131 target->Buffer = (PCHAR) source;
132 target->Length = len;
133 target->MaximumLength = len + 1;
137 target->Buffer = NULL;
139 target->MaximumLength = 0;
141 return STATUS_SUCCESS;
144 /**************************************************************************
145 * RtlInitString (NTDLL.@)
147 * Initializes a buffered string.
153 * Assigns source to target->Buffer. The length of source is assigned to
154 * target->Length and target->MaximumLength. If source is NULL the length
155 * of source is assumed to be 0.
157 void WINAPI RtlInitString(
158 PSTRING target, /* [I/O] Buffered string to be initialized */
159 PCSZ source) /* [I] '\0' terminated string used to initialize target */
161 RtlInitAnsiString( target, source );
165 /**************************************************************************
166 * RtlFreeAnsiString (NTDLL.@)
168 void WINAPI RtlFreeAnsiString( PSTRING str )
172 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
173 RtlZeroMemory( str, sizeof(*str) );
178 /**************************************************************************
179 * RtlFreeOemString (NTDLL.@)
181 void WINAPI RtlFreeOemString( PSTRING str )
183 RtlFreeAnsiString( str );
187 /**************************************************************************
188 * RtlCopyString (NTDLL.@)
190 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
194 unsigned int len = min( src->Length, dst->MaximumLength );
195 memcpy( dst->Buffer, src->Buffer, len );
198 else dst->Length = 0;
202 /**************************************************************************
203 * RtlInitUnicodeString (NTDLL.@)
205 * Initializes a buffered unicode string.
211 * Assigns source to target->Buffer. The length of source is assigned to
212 * target->Length and target->MaximumLength. If source is NULL the length
213 * of source is assumed to be 0.
215 void WINAPI RtlInitUnicodeString(
216 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
217 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
219 if ((target->Buffer = (PWSTR) source))
221 unsigned int length = strlenW(source) * sizeof(WCHAR);
224 target->Length = length;
225 target->MaximumLength = target->Length + sizeof(WCHAR);
227 else target->Length = target->MaximumLength = 0;
231 /**************************************************************************
232 * RtlInitUnicodeStringEx (NTDLL.@)
234 * Initializes a buffered unicode string.
237 * Success: STATUS_SUCCESS. target is initialized.
238 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
241 * Assigns source to target->Buffer. The length of source is assigned to
242 * target->Length and target->MaximumLength. If source is NULL the length
243 * of source is assumed to be 0.
245 NTSTATUS WINAPI RtlInitUnicodeStringEx(
246 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
247 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
249 if (source != NULL) {
250 unsigned int len = strlenW(source) * sizeof(WCHAR);
253 return STATUS_NAME_TOO_LONG;
255 target->Length = len;
256 target->MaximumLength = len + sizeof(WCHAR);
257 target->Buffer = (PWSTR) source;
261 target->MaximumLength = 0;
262 target->Buffer = NULL;
264 return STATUS_SUCCESS;
268 /**************************************************************************
269 * RtlCreateUnicodeString (NTDLL.@)
271 * Creates a UNICODE_STRING from a null-terminated Unicode string.
277 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
279 int len = (strlenW(src) + 1) * sizeof(WCHAR);
280 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
281 memcpy( target->Buffer, src, len );
282 target->MaximumLength = len;
283 target->Length = len - sizeof(WCHAR);
288 /**************************************************************************
289 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
291 * Creates a UNICODE_STRING from a null-terminated Ascii string.
297 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
300 RtlInitAnsiString( &ansi, src );
301 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
305 /**************************************************************************
306 * RtlFreeUnicodeString (NTDLL.@)
308 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
309 * RtlCreateUnicodeStringFromAsciiz().
314 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
318 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
319 RtlZeroMemory( str, sizeof(*str) );
324 /**************************************************************************
325 * RtlCopyUnicodeString (NTDLL.@)
327 * Copies from one UNICODE_STRING to another.
332 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
336 unsigned int len = min( src->Length, dst->MaximumLength );
337 memcpy( dst->Buffer, src->Buffer, len );
339 /* append terminating '\0' if enough space */
340 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
342 else dst->Length = 0;
346 /**************************************************************************
347 * RtlDuplicateUnicodeString (NTDLL.@)
349 * Duplicates an unicode string.
352 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
353 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
354 * STATUS_NO_MEMORY, if the allocation fails.
357 * For add_nul there are several possible values:
358 * 0 = destination will not be '\0' terminated,
359 * 1 = destination will be '\0' terminated,
360 * 3 = like 1 but for an empty source string produce '\0' terminated empty
361 * Buffer instead of assigning NULL to the Buffer.
362 * Other add_nul values are invalid.
364 NTSTATUS WINAPI RtlDuplicateUnicodeString(
365 int add_nul, /* [I] flag */
366 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
367 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
369 if (source == NULL || destination == NULL ||
370 source->Length > source->MaximumLength ||
371 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
372 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
373 return STATUS_INVALID_PARAMETER;
375 if (source->Length == 0 && add_nul != 3) {
376 destination->Length = 0;
377 destination->MaximumLength = 0;
378 destination->Buffer = NULL;
380 unsigned int destination_max_len = source->Length;
383 destination_max_len += sizeof(WCHAR);
385 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
386 if (destination->Buffer == NULL) {
387 return STATUS_NO_MEMORY;
389 memcpy(destination->Buffer, source->Buffer, source->Length);
390 destination->Length = source->Length;
391 destination->MaximumLength = source->Length;
392 /* append terminating '\0' if enough space */
394 destination->MaximumLength = destination_max_len;
395 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
400 return STATUS_SUCCESS;
404 /**************************************************************************
405 * RtlEraseUnicodeString (NTDLL.@)
407 * Overwrites a UNICODE_STRING with zeros.
412 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
416 memset( str->Buffer, 0, str->MaximumLength );
427 /******************************************************************************
428 * RtlCompareString (NTDLL.@)
430 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
436 len = min(s1->Length, s2->Length);
442 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
446 while (!ret && len--) ret = *p1++ - *p2++;
448 if (!ret) ret = s1->Length - s2->Length;
453 /******************************************************************************
454 * RtlCompareUnicodeString (NTDLL.@)
456 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
457 BOOLEAN CaseInsensitive )
463 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
469 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
473 while (!ret && len--) ret = *p1++ - *p2++;
475 if (!ret) ret = s1->Length - s2->Length;
480 /**************************************************************************
481 * RtlEqualString (NTDLL.@)
483 * Determine if two strings are equal.
486 * s1 [I] Source string
487 * s2 [I] String to compare to s1
488 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
491 * Non-zero if s1 is equal to s2, 0 otherwise.
493 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
495 if (s1->Length != s2->Length) return FALSE;
496 return !RtlCompareString( s1, s2, CaseInsensitive );
500 /**************************************************************************
501 * RtlEqualUnicodeString (NTDLL.@)
503 * Unicode version of RtlEqualString.
505 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
506 BOOLEAN CaseInsensitive )
508 if (s1->Length != s2->Length) return FALSE;
509 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
513 /**************************************************************************
514 * RtlPrefixString (NTDLL.@)
516 * Determine if one string is a prefix of another.
519 * s1 [I] Prefix to look for in s2
520 * s2 [I] String that may contain s1 as a prefix
521 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
524 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
526 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
530 if (s1->Length > s2->Length) return FALSE;
533 for (i = 0; i < s1->Length; i++)
534 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
538 for (i = 0; i < s1->Length; i++)
539 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
545 /**************************************************************************
546 * RtlPrefixUnicodeString (NTDLL.@)
548 * Unicode version of RtlPrefixString.
550 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
551 const UNICODE_STRING *s2,
552 BOOLEAN ignore_case )
556 if (s1->Length > s2->Length) return FALSE;
559 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
560 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
564 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
565 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
571 /**************************************************************************
572 * RtlEqualComputerName (NTDLL.@)
574 * Determine if two computer names are the same.
577 * left [I] First computer name
578 * right [I] Second computer name
581 * 0 if the names are equal, non-zero otherwise.
584 * The comparison is case insensitive.
586 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
587 const UNICODE_STRING *right)
590 STRING upLeft, upRight;
592 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
594 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
596 ret = RtlEqualString( &upLeft, &upRight, FALSE );
597 RtlFreeOemString( &upRight );
599 RtlFreeOemString( &upLeft );
605 /**************************************************************************
606 * RtlEqualDomainName (NTDLL.@)
608 * Determine if two domain names are the same.
611 * left [I] First domain name
612 * right [I] Second domain name
615 * 0 if the names are equal, non-zero otherwise.
618 * The comparison is case insensitive.
620 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
621 const UNICODE_STRING *right)
623 return RtlEqualComputerName(left, right);
627 /**************************************************************************
628 * RtlAnsiCharToUnicodeChar (NTDLL.@)
630 * Converts the first ansi character to a unicode character.
633 * ansi [I/O] Pointer to the ansi string.
636 * Unicode representation of the first character in the ansi string.
639 * Upon successful completion, the char pointer ansi points to is
640 * incremented by the size of the character.
642 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
645 DWORD charSize = sizeof(CHAR);
647 if (wine_is_dbcs_leadbyte(ansi_table, **ansi))
650 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
657 COPY BETWEEN ANSI_STRING or UNICODE_STRING
658 there is no parameter checking, it just crashes
662 /**************************************************************************
663 * RtlAnsiStringToUnicodeString (NTDLL.@)
665 * Converts an ansi string to an unicode string.
668 * Success: STATUS_SUCCESS. uni contains the converted string
669 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
670 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
671 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
674 * This function always writes a terminating '\0'.
676 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
677 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
678 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
679 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
681 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
683 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
684 uni->Length = total - sizeof(WCHAR);
687 uni->MaximumLength = total;
688 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
689 return STATUS_NO_MEMORY;
691 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
693 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
694 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
695 return STATUS_SUCCESS;
699 /**************************************************************************
700 * RtlOemStringToUnicodeString (NTDLL.@)
702 * Converts an oem string to an unicode string.
705 * Success: STATUS_SUCCESS. uni contains the converted string
706 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
707 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
708 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
711 * This function always writes a terminating '\0'.
713 NTSTATUS WINAPI RtlOemStringToUnicodeString(
714 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
715 const STRING *oem, /* [I] Oem string to be converted */
716 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
718 DWORD total = RtlOemStringToUnicodeSize( oem );
720 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
721 uni->Length = total - sizeof(WCHAR);
724 uni->MaximumLength = total;
725 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
726 return STATUS_NO_MEMORY;
728 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
730 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
731 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
732 return STATUS_SUCCESS;
736 /**************************************************************************
737 * RtlUnicodeStringToAnsiString (NTDLL.@)
739 * Converts an unicode string to an ansi string.
742 * Success: STATUS_SUCCESS. ansi contains the converted string
743 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
744 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
747 * This function always writes a terminating '\0'.
748 * It performs a partial copy if ansi is too small.
750 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
751 STRING *ansi, /* [I/O] Destination for the ansi string */
752 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
753 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
755 NTSTATUS ret = STATUS_SUCCESS;
756 DWORD len = RtlUnicodeStringToAnsiSize( uni );
758 ansi->Length = len - 1;
761 ansi->MaximumLength = len;
762 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
763 return STATUS_NO_MEMORY;
765 else if (ansi->MaximumLength < len)
767 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
768 ansi->Length = ansi->MaximumLength - 1;
769 ret = STATUS_BUFFER_OVERFLOW;
772 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
773 ansi->Buffer[ansi->Length] = 0;
778 /**************************************************************************
779 * RtlUnicodeStringToOemString (NTDLL.@)
781 * Converts a Rtl Unicode string to an OEM string.
784 * oem [O] Destination for OEM string
785 * uni [I] Source Unicode string
786 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
789 * Success: STATUS_SUCCESS. oem contains the converted string
790 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
791 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
794 * If doalloc is TRUE, the length allocated is uni->Length + 1.
795 * This function always '\0' terminates the string returned.
797 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
798 const UNICODE_STRING *uni,
801 NTSTATUS ret = STATUS_SUCCESS;
802 DWORD len = RtlUnicodeStringToOemSize( uni );
804 oem->Length = len - 1;
807 oem->MaximumLength = len;
808 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
809 return STATUS_NO_MEMORY;
811 else if (oem->MaximumLength < len)
813 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
814 oem->Length = oem->MaximumLength - 1;
815 ret = STATUS_BUFFER_OVERFLOW;
818 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
819 oem->Buffer[oem->Length] = 0;
824 /**************************************************************************
825 * RtlMultiByteToUnicodeN (NTDLL.@)
827 * Converts a multi-byte string to a Unicode string.
833 * Performs a partial copy if dst is too small.
835 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
836 LPCSTR src, DWORD srclen )
839 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
841 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
842 return STATUS_SUCCESS;
846 /**************************************************************************
847 * RtlOemToUnicodeN (NTDLL.@)
849 * Converts a multi-byte string in the OEM code page to a Unicode string.
854 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
855 LPCSTR src, DWORD srclen )
857 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
859 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
860 return STATUS_SUCCESS;
864 /**************************************************************************
865 * RtlUnicodeToMultiByteN (NTDLL.@)
867 * Converts a Unicode string to a multi-byte string in the ANSI code page.
872 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
873 LPCWSTR src, DWORD srclen )
875 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
876 dst, dstlen, NULL, NULL );
878 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
879 return STATUS_SUCCESS;
883 /**************************************************************************
884 * RtlUnicodeToOemN (NTDLL.@)
886 * Converts a Unicode string to a multi-byte string in the OEM code page.
891 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
892 LPCWSTR src, DWORD srclen )
894 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
895 dst, dstlen, NULL, NULL );
897 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
898 return STATUS_SUCCESS;
907 /**************************************************************************
908 * RtlUpperChar (NTDLL.@)
910 * Converts an Ascii character to uppercase.
913 * ch [I] Character to convert
916 * The uppercase character value.
919 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
920 * All other input characters are returned unchanged. The locale and
921 * multibyte characters are not taken into account (as native DLL).
923 CHAR WINAPI RtlUpperChar( CHAR ch )
925 if (ch >= 'a' && ch <= 'z') {
926 return ch - 'a' + 'A';
933 /**************************************************************************
934 * RtlUpperString (NTDLL.@)
936 * Converts an Ascii string to uppercase.
939 * dst [O] Destination for converted string
940 * src [I] Source string to convert
946 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
947 * All other src characters are copied unchanged to dst. The locale and
948 * multibyte characters are not taken into account (as native DLL).
949 * The number of character copied is the minimum of src->Length and
950 * the dst->MaximumLength.
952 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
954 unsigned int i, len = min(src->Length, dst->MaximumLength);
956 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
961 /**************************************************************************
962 * RtlUpcaseUnicodeChar (NTDLL.@)
964 * Converts an Unicode character to uppercase.
967 * wch [I] Character to convert
970 * The uppercase character value.
972 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
974 return toupperW(wch);
978 /**************************************************************************
979 * RtlDowncaseUnicodeChar (NTDLL.@)
981 * Converts an Unicode character to lowercase.
984 * wch [I] Character to convert
987 * The lowercase character value.
989 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
991 return tolowerW(wch);
995 /**************************************************************************
996 * RtlUpcaseUnicodeString (NTDLL.@)
998 * Converts an Unicode string to uppercase.
1001 * dest [O] Destination for converted string
1002 * src [I] Source string to convert
1003 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1006 * Success: STATUS_SUCCESS. dest contains the converted string.
1007 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1008 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1011 * dest is never '\0' terminated because it may be equal to src, and src
1012 * might not be '\0' terminated. dest->Length is only set upon success.
1014 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
1015 const UNICODE_STRING *src,
1018 DWORD i, len = src->Length;
1022 dest->MaximumLength = len;
1023 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1024 return STATUS_NO_MEMORY;
1026 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1028 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
1030 return STATUS_SUCCESS;
1034 /**************************************************************************
1035 * RtlDowncaseUnicodeString (NTDLL.@)
1037 * Converts an Unicode string to lowercase.
1040 * dest [O] Destination for converted string
1041 * src [I] Source string to convert
1042 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1045 * Success: STATUS_SUCCESS. dest contains the converted string.
1046 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1047 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1050 * dest is never '\0' terminated because it may be equal to src, and src
1051 * might not be '\0' terminated. dest->Length is only set upon success.
1053 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1054 UNICODE_STRING *dest,
1055 const UNICODE_STRING *src,
1059 DWORD len = src->Length;
1062 dest->MaximumLength = len;
1063 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1064 return STATUS_NO_MEMORY;
1066 } else if (len > dest->MaximumLength) {
1067 return STATUS_BUFFER_OVERFLOW;
1070 for (i = 0; i < len/sizeof(WCHAR); i++) {
1071 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1074 return STATUS_SUCCESS;
1078 /**************************************************************************
1079 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1081 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1087 * writes terminating 0
1089 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1090 const UNICODE_STRING *src,
1094 UNICODE_STRING upcase;
1096 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1098 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1099 RtlFreeUnicodeString( &upcase );
1105 /**************************************************************************
1106 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1108 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1109 * stored in STRING format.
1115 * writes terminating 0
1117 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1118 const UNICODE_STRING *src,
1122 UNICODE_STRING upcase;
1124 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1126 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1127 RtlFreeUnicodeString( &upcase );
1133 /**************************************************************************
1134 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1136 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1137 * stored in STRING format.
1143 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1145 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1146 const UNICODE_STRING *uni,
1150 UNICODE_STRING upcase;
1153 upcase.Buffer = tmp;
1154 upcase.MaximumLength = sizeof(tmp);
1155 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1156 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1160 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1164 oem->MaximumLength = len;
1165 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1167 ret = STATUS_NO_MEMORY;
1171 else if (oem->MaximumLength < len)
1173 ret = STATUS_BUFFER_OVERFLOW;
1174 oem->Length = oem->MaximumLength;
1175 if (!oem->MaximumLength) goto done;
1177 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1179 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1185 /**************************************************************************
1186 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1188 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1193 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1194 LPCWSTR src, DWORD srclen )
1200 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1201 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1202 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1203 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1208 /**************************************************************************
1209 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1211 * Converts a Unicode string to the equivalent OEM upper-case representation.
1216 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1217 LPCWSTR src, DWORD srclen )
1223 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1224 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1225 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1226 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1236 /**************************************************************************
1237 * RtlOemStringToUnicodeSize (NTDLL.@)
1238 * RtlxOemStringToUnicodeSize (NTDLL.@)
1240 * Calculate the size in bytes necessary for the Unicode conversion of str,
1241 * including the terminating '\0'.
1244 * str [I] String to calculate the size of
1247 * The calculated size.
1249 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1251 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1252 return (ret + 1) * sizeof(WCHAR);
1256 /**************************************************************************
1257 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1258 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1260 * Calculate the size in bytes necessary for the Unicode conversion of str,
1261 * including the terminating '\0'.
1264 * str [I] String to calculate the size of
1267 * The calculated size.
1269 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1272 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1273 return ret + sizeof(WCHAR);
1277 /**************************************************************************
1278 * RtlMultiByteToUnicodeSize (NTDLL.@)
1280 * Compute the size in bytes necessary for the Unicode conversion of str,
1281 * without the terminating '\0'.
1284 * size [O] Destination for size
1285 * str [I] String to calculate the size of
1286 * len [I] Length of str
1291 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1293 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1294 return STATUS_SUCCESS;
1298 /**************************************************************************
1299 * RtlUnicodeToMultiByteSize (NTDLL.@)
1301 * Calculate the size in bytes necessary for the multibyte conversion of str,
1302 * without the terminating '\0'.
1305 * size [O] Destination for size
1306 * str [I] String to calculate the size of
1307 * len [I] Length of str
1312 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1314 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1315 return STATUS_SUCCESS;
1319 /**************************************************************************
1320 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1321 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1323 * Calculate the size in bytes necessary for the Ansi conversion of str,
1324 * including the terminating '\0'.
1327 * str [I] String to calculate the size of
1330 * The calculated size.
1332 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1335 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1340 /**************************************************************************
1341 * RtlUnicodeStringToOemSize (NTDLL.@)
1342 * RtlxUnicodeStringToOemSize (NTDLL.@)
1344 * Calculate the size in bytes necessary for the OEM conversion of str,
1345 * including the terminating '\0'.
1348 * str [I] String to calculate the size of
1351 * The calculated size.
1353 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1355 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1356 NULL, 0, NULL, NULL ) + 1;
1360 /**************************************************************************
1361 * RtlAppendAsciizToString (NTDLL.@)
1363 * Concatenates a buffered character string and a '\0' terminated character
1367 * Success: STATUS_SUCCESS. src is appended to dest.
1368 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1369 * to hold the concatenated string.
1372 * if src is NULL dest is unchanged.
1373 * dest is never '\0' terminated.
1375 NTSTATUS WINAPI RtlAppendAsciizToString(
1376 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1377 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1380 unsigned int src_len = strlen(src);
1381 unsigned int dest_len = src_len + dest->Length;
1383 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1384 memcpy(dest->Buffer + dest->Length, src, src_len);
1385 dest->Length = dest_len;
1387 return STATUS_SUCCESS;
1391 /**************************************************************************
1392 * RtlAppendStringToString (NTDLL.@)
1394 * Concatenates two buffered character strings
1397 * Success: STATUS_SUCCESS. src is appended to dest.
1398 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1399 * to hold the concatenated string.
1402 * if src->length is zero dest is unchanged.
1403 * dest is never '\0' terminated.
1405 NTSTATUS WINAPI RtlAppendStringToString(
1406 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1407 const STRING *src) /* [I] Buffered character string to be concatenated */
1409 if (src->Length != 0) {
1410 unsigned int dest_len = src->Length + dest->Length;
1412 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1413 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1414 dest->Length = dest_len;
1416 return STATUS_SUCCESS;
1420 /**************************************************************************
1421 * RtlAppendUnicodeToString (NTDLL.@)
1423 * Concatenates a buffered unicode string and a '\0' terminated unicode
1427 * Success: STATUS_SUCCESS. src is appended to dest.
1428 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1429 * to hold the concatenated string.
1432 * if src is NULL dest is unchanged.
1433 * dest is '\0' terminated when the MaximumLength allows it.
1434 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1437 * Does not write in the src->Buffer beyond MaximumLength when
1438 * MaximumLength is odd as the native function does.
1440 NTSTATUS WINAPI RtlAppendUnicodeToString(
1441 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1442 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1445 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1446 unsigned int dest_len = src_len + dest->Length;
1448 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1449 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1450 dest->Length = dest_len;
1451 /* append terminating '\0' if enough space */
1452 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1453 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1456 return STATUS_SUCCESS;
1460 /**************************************************************************
1461 * RtlAppendUnicodeStringToString (NTDLL.@)
1463 * Concatenates two buffered unicode strings
1466 * Success: STATUS_SUCCESS. src is appended to dest.
1467 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1468 * to hold the concatenated string.
1471 * if src->length is zero dest is unchanged.
1472 * dest is '\0' terminated when the MaximumLength allows it.
1473 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1476 * Does not write in the src->Buffer beyond MaximumLength when
1477 * MaximumLength is odd as the native function does.
1479 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1480 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1481 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1483 if (src->Length != 0) {
1484 unsigned int dest_len = src->Length + dest->Length;
1486 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1487 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1488 dest->Length = dest_len;
1489 /* append terminating '\0' if enough space */
1490 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1491 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1494 return STATUS_SUCCESS;
1498 /**************************************************************************
1499 * RtlFindCharInUnicodeString (NTDLL.@)
1501 * Searches for one of several unicode characters in an unicode string.
1504 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1505 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1507 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1508 int flags, /* [I] Flags */
1509 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1510 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1511 USHORT *pos) /* [O] Position of the first character found + 2 */
1514 unsigned int search_idx;
1518 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1519 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1520 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1521 *pos = (main_idx + 1) * sizeof(WCHAR);
1522 return STATUS_SUCCESS;
1527 return STATUS_NOT_FOUND;
1529 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1530 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1531 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1532 *pos = main_idx * sizeof(WCHAR);
1533 return STATUS_SUCCESS;
1538 return STATUS_NOT_FOUND;
1540 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1542 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1543 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1546 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1547 *pos = (main_idx + 1) * sizeof(WCHAR);
1548 return STATUS_SUCCESS;
1552 return STATUS_NOT_FOUND;
1554 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1556 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1557 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1560 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1561 *pos = main_idx * sizeof(WCHAR);
1562 return STATUS_SUCCESS;
1566 return STATUS_NOT_FOUND;
1568 return STATUS_NOT_FOUND;
1576 /**************************************************************************
1577 * RtlIsTextUnicode (NTDLL.@)
1579 * Attempt to guess whether a text buffer is Unicode.
1582 * buf [I] Text buffer to test
1583 * len [I] Length of buf
1584 * pf [O] Destination for test results
1587 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1590 * Should implement more tests.
1592 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1594 const WCHAR *s = buf;
1596 unsigned int flags = ~0U, out_flags = 0;
1598 if (len < sizeof(WCHAR))
1600 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1607 * Apply various tests to the text string. According to the
1608 * docs, each test "passed" sets the corresponding flag in
1609 * the output flags. But some of the tests are mutually
1610 * exclusive, so I don't see how you could pass all tests ...
1613 /* Check for an odd length ... pass if even. */
1614 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1616 if (((char *)buf)[len - 1] == 0)
1617 len--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1619 len /= sizeof(WCHAR);
1620 /* Windows only checks the first 256 characters */
1621 if (len > 256) len = 256;
1623 /* Check for the special byte order unicode marks. */
1624 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1625 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1627 /* apply some statistical analysis */
1628 if (flags & IS_TEXT_UNICODE_STATISTICS)
1631 /* FIXME: checks only for ASCII characters in the unicode stream */
1632 for (i = 0; i < len; i++)
1634 if (s[i] <= 255) stats++;
1636 if (stats > len / 2)
1637 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1640 /* Check for unicode NULL chars */
1641 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1643 for (i = 0; i < len; i++)
1645 if (!(s[i] & 0xff) || !(s[i] >> 8))
1647 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1658 /* check for flags that indicate it's definitely not valid Unicode */
1659 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1660 /* now check for invalid ASCII, and assume Unicode if so */
1661 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1662 /* now check for Unicode flags */
1663 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1669 /**************************************************************************
1670 * RtlCharToInteger (NTDLL.@)
1672 * Converts a character string into its integer equivalent.
1675 * Success: STATUS_SUCCESS. value contains the converted number
1676 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1677 * STATUS_ACCESS_VIOLATION, if value is NULL.
1680 * For base 0 it uses 10 as base and the string should be in the format
1681 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1682 * For other bases the string should be in the format
1683 * "{whitespace} [+|-] {digits}".
1684 * No check is made for value overflow, only the lower 32 bits are assigned.
1685 * If str is NULL it crashes, as the native function does.
1688 * This function does not read garbage behind '\0' as the native version does.
1690 NTSTATUS WINAPI RtlCharToInteger(
1691 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1692 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1693 ULONG *value) /* [O] Destination for the converted value */
1697 ULONG RunningTotal = 0;
1700 while (*str != '\0' && *str <= ' ') {
1706 } else if (*str == '-') {
1713 if (str[0] == '0') {
1714 if (str[1] == 'b') {
1717 } else if (str[1] == 'o') {
1720 } else if (str[1] == 'x') {
1725 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1726 return STATUS_INVALID_PARAMETER;
1729 if (value == NULL) {
1730 return STATUS_ACCESS_VIOLATION;
1733 while (*str != '\0') {
1735 if (chCurrent >= '0' && chCurrent <= '9') {
1736 digit = chCurrent - '0';
1737 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1738 digit = chCurrent - 'A' + 10;
1739 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1740 digit = chCurrent - 'a' + 10;
1744 if (digit < 0 || digit >= base) {
1745 *value = bMinus ? -RunningTotal : RunningTotal;
1746 return STATUS_SUCCESS;
1749 RunningTotal = RunningTotal * base + digit;
1753 *value = bMinus ? -RunningTotal : RunningTotal;
1754 return STATUS_SUCCESS;
1758 /**************************************************************************
1759 * RtlIntegerToChar (NTDLL.@)
1761 * Converts an unsigned integer to a character string.
1764 * Success: STATUS_SUCCESS. str contains the converted number
1765 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1766 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1767 * STATUS_ACCESS_VIOLATION, if str is NULL.
1770 * Instead of base 0 it uses 10 as base.
1771 * Writes at most length characters to the string str.
1772 * Str is '\0' terminated when length allows it.
1773 * When str fits exactly in length characters the '\0' is omitted.
1775 NTSTATUS WINAPI RtlIntegerToChar(
1776 ULONG value, /* [I] Value to be converted */
1777 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1778 ULONG length, /* [I] Length of the str buffer in bytes */
1779 PCHAR str) /* [O] Destination for the converted value */
1788 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1789 return STATUS_INVALID_PARAMETER;
1797 digit = value % base;
1798 value = value / base;
1802 *pos = 'A' + digit - 10;
1804 } while (value != 0L);
1806 len = &buffer[32] - pos;
1808 return STATUS_BUFFER_OVERFLOW;
1809 } else if (str == NULL) {
1810 return STATUS_ACCESS_VIOLATION;
1811 } else if (len == length) {
1812 memcpy(str, pos, len);
1814 memcpy(str, pos, len + 1);
1816 return STATUS_SUCCESS;
1820 /**************************************************************************
1821 * RtlUnicodeStringToInteger (NTDLL.@)
1823 * Converts an unicode string into its integer equivalent.
1826 * Success: STATUS_SUCCESS. value contains the converted number
1827 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1828 * STATUS_ACCESS_VIOLATION, if value is NULL.
1831 * For base 0 it uses 10 as base and the string should be in the format
1832 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1833 * For other bases the string should be in the format
1834 * "{whitespace} [+|-] {digits}".
1835 * No check is made for value overflow, only the lower 32 bits are assigned.
1836 * If str is NULL it crashes, as the native function does.
1839 * This function does not read garbage on string length 0 as the native
1842 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1843 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1844 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1845 ULONG *value) /* [O] Destination for the converted value */
1847 LPWSTR lpwstr = str->Buffer;
1848 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1851 ULONG RunningTotal = 0;
1854 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1859 if (CharsRemaining >= 1) {
1860 if (*lpwstr == '+') {
1863 } else if (*lpwstr == '-') {
1872 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1873 if (lpwstr[1] == 'b') {
1875 CharsRemaining -= 2;
1877 } else if (lpwstr[1] == 'o') {
1879 CharsRemaining -= 2;
1881 } else if (lpwstr[1] == 'x') {
1883 CharsRemaining -= 2;
1887 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1888 return STATUS_INVALID_PARAMETER;
1891 if (value == NULL) {
1892 return STATUS_ACCESS_VIOLATION;
1895 while (CharsRemaining >= 1) {
1896 wchCurrent = *lpwstr;
1897 if (wchCurrent >= '0' && wchCurrent <= '9') {
1898 digit = wchCurrent - '0';
1899 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1900 digit = wchCurrent - 'A' + 10;
1901 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1902 digit = wchCurrent - 'a' + 10;
1906 if (digit < 0 || digit >= base) {
1907 *value = bMinus ? -RunningTotal : RunningTotal;
1908 return STATUS_SUCCESS;
1911 RunningTotal = RunningTotal * base + digit;
1916 *value = bMinus ? -RunningTotal : RunningTotal;
1917 return STATUS_SUCCESS;
1921 /**************************************************************************
1922 * RtlIntegerToUnicodeString (NTDLL.@)
1924 * Converts an unsigned integer to a '\0' terminated unicode string.
1927 * Success: STATUS_SUCCESS. str contains the converted number
1928 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1929 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1930 * (with the '\0' termination). In this case str->Length
1931 * is set to the length, the string would have (which can
1932 * be larger than the MaximumLength).
1935 * Instead of base 0 it uses 10 as base.
1936 * If str is NULL it crashes, as the native function does.
1939 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1940 * The native function does this when the string would be longer than 16
1941 * characters even when the string parameter is long enough.
1943 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1944 ULONG value, /* [I] Value to be converted */
1945 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1946 UNICODE_STRING *str) /* [O] Destination for the converted value */
1954 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1955 return STATUS_INVALID_PARAMETER;
1963 digit = value % base;
1964 value = value / base;
1968 *pos = 'A' + digit - 10;
1970 } while (value != 0L);
1972 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1973 if (str->Length >= str->MaximumLength) {
1974 return STATUS_BUFFER_OVERFLOW;
1976 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1978 return STATUS_SUCCESS;
1982 /*************************************************************************
1983 * RtlGUIDFromString (NTDLL.@)
1985 * Convert a string representation of a GUID into a GUID.
1988 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1989 * guid [O] Destination for the converted GUID
1992 * Success: STATUS_SUCCESS. guid contains the converted value.
1993 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1996 * See RtlStringFromGUID.
1998 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
2001 const WCHAR *lpszCLSID = str->Buffer;
2002 BYTE* lpOut = (BYTE*)guid;
2004 TRACE("(%s,%p)\n", debugstr_us(str), guid);
2006 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2007 * to memory: DWORD... WORD WORD BYTES............
2014 if (*lpszCLSID != '{')
2015 return STATUS_INVALID_PARAMETER;
2018 case 9: case 14: case 19: case 24:
2019 if (*lpszCLSID != '-')
2020 return STATUS_INVALID_PARAMETER;
2024 if (*lpszCLSID != '}')
2025 return STATUS_INVALID_PARAMETER;
2030 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
2033 /* Read two hex digits as a byte value */
2034 if (ch >= '0' && ch <= '9') ch = ch - '0';
2035 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
2036 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
2037 else return STATUS_INVALID_PARAMETER;
2039 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
2040 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
2041 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
2042 else return STATUS_INVALID_PARAMETER;
2044 byte = ch << 4 | ch2;
2048 #ifndef WORDS_BIGENDIAN
2049 /* For Big Endian machines, we store the data such that the
2050 * dword/word members can be read as DWORDS and WORDS correctly. */
2052 case 1: lpOut[3] = byte; break;
2053 case 3: lpOut[2] = byte; break;
2054 case 5: lpOut[1] = byte; break;
2055 case 7: lpOut[0] = byte; lpOut += 4; break;
2057 case 10: case 15: lpOut[1] = byte; break;
2058 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2061 default: lpOut[0] = byte; lpOut++; break;
2063 lpszCLSID++; /* Skip 2nd character of byte */
2071 return STATUS_SUCCESS;
2074 /*************************************************************************
2075 * RtlStringFromGUID (NTDLL.@)
2077 * Convert a GUID into a string representation of a GUID.
2080 * guid [I] GUID to convert
2081 * str [O] Destination for the converted string
2084 * Success: STATUS_SUCCESS. str contains the converted value.
2085 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2088 * See RtlGUIDFromString.
2090 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2092 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2093 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2094 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2095 '%','0','2','X','%','0','2','X','}','\0' };
2097 TRACE("(%p,%p)\n", guid, str);
2099 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2100 str->MaximumLength = str->Length + sizeof(WCHAR);
2101 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2104 str->Length = str->MaximumLength = 0;
2105 return STATUS_NO_MEMORY;
2107 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2108 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2109 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2111 return STATUS_SUCCESS;