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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
70 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
73 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
74 const char* defchar, int *used )
77 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
78 if (used) *used = 0; /* all chars are valid for UTF-8 */
79 return wine_utf8_wcstombs( src, srclen, dst, dstlen );
82 /**************************************************************************
83 * RtlInitAnsiString (NTDLL.@)
85 * Initializes a buffered ansi string.
91 * Assigns source to target->Buffer. The length of source is assigned to
92 * target->Length and target->MaximumLength. If source is NULL the length
93 * of source is assumed to be 0.
95 void WINAPI RtlInitAnsiString(
96 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
97 PCSZ source) /* [I] '\0' terminated string used to initialize target */
99 if ((target->Buffer = (PCHAR) source))
101 target->Length = strlen(source);
102 target->MaximumLength = target->Length + 1;
104 else target->Length = target->MaximumLength = 0;
107 /**************************************************************************
108 * RtlInitAnsiStringEx (NTDLL.@)
110 * Initializes a buffered ansi string.
113 * An appropriate NTSTATUS value.
116 * Assigns source to target->Buffer. The length of source is assigned to
117 * target->Length and target->MaximumLength. If source is NULL the length
118 * of source is assumed to be 0.
120 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
124 unsigned int len = strlen(source);
126 return STATUS_NAME_TOO_LONG;
128 target->Buffer = (PCHAR) source;
129 target->Length = len;
130 target->MaximumLength = len + 1;
134 target->Buffer = NULL;
136 target->MaximumLength = 0;
138 return STATUS_SUCCESS;
141 /**************************************************************************
142 * RtlInitString (NTDLL.@)
144 * Initializes a buffered string.
150 * Assigns source to target->Buffer. The length of source is assigned to
151 * target->Length and target->MaximumLength. If source is NULL the length
152 * of source is assumed to be 0.
154 void WINAPI RtlInitString(
155 PSTRING target, /* [I/O] Buffered string to be initialized */
156 PCSZ source) /* [I] '\0' terminated string used to initialize target */
158 RtlInitAnsiString( target, source );
162 /**************************************************************************
163 * RtlFreeAnsiString (NTDLL.@)
165 void WINAPI RtlFreeAnsiString( PSTRING str )
169 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
170 RtlZeroMemory( str, sizeof(*str) );
175 /**************************************************************************
176 * RtlFreeOemString (NTDLL.@)
178 void WINAPI RtlFreeOemString( PSTRING str )
180 RtlFreeAnsiString( str );
184 /**************************************************************************
185 * RtlCopyString (NTDLL.@)
187 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
191 unsigned int len = min( src->Length, dst->MaximumLength );
192 memcpy( dst->Buffer, src->Buffer, len );
195 else dst->Length = 0;
199 /**************************************************************************
200 * RtlInitUnicodeString (NTDLL.@)
202 * Initializes a buffered unicode string.
208 * Assigns source to target->Buffer. The length of source is assigned to
209 * target->Length and target->MaximumLength. If source is NULL the length
210 * of source is assumed to be 0.
212 void WINAPI RtlInitUnicodeString(
213 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
214 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
216 if ((target->Buffer = (PWSTR) source))
218 target->Length = strlenW(source) * sizeof(WCHAR);
219 target->MaximumLength = target->Length + sizeof(WCHAR);
221 else target->Length = target->MaximumLength = 0;
225 /**************************************************************************
226 * RtlInitUnicodeStringEx (NTDLL.@)
228 * Initializes a buffered unicode string.
231 * Success: STATUS_SUCCESS. target is initialized.
232 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
235 * Assigns source to target->Buffer. The length of source is assigned to
236 * target->Length and target->MaximumLength. If source is NULL the length
237 * of source is assumed to be 0.
239 NTSTATUS WINAPI RtlInitUnicodeStringEx(
240 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
241 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
243 if (source != NULL) {
244 unsigned int len = strlenW(source) * sizeof(WCHAR);
247 return STATUS_NAME_TOO_LONG;
249 target->Length = len;
250 target->MaximumLength = len + sizeof(WCHAR);
251 target->Buffer = (PWSTR) source;
255 target->MaximumLength = 0;
256 target->Buffer = NULL;
258 return STATUS_SUCCESS;
262 /**************************************************************************
263 * RtlCreateUnicodeString (NTDLL.@)
265 * Creates a UNICODE_STRING from a null-terminated Unicode string.
271 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
273 int len = (strlenW(src) + 1) * sizeof(WCHAR);
274 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
275 memcpy( target->Buffer, src, len );
276 target->MaximumLength = len;
277 target->Length = len - sizeof(WCHAR);
282 /**************************************************************************
283 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
285 * Creates a UNICODE_STRING from a null-terminated Ascii string.
291 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
294 RtlInitAnsiString( &ansi, src );
295 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
299 /**************************************************************************
300 * RtlFreeUnicodeString (NTDLL.@)
302 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
303 * RtlCreateUnicodeStringFromAsciiz().
308 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
312 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
313 RtlZeroMemory( str, sizeof(*str) );
318 /**************************************************************************
319 * RtlCopyUnicodeString (NTDLL.@)
321 * Copies from one UNICODE_STRING to another.
326 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
330 unsigned int len = min( src->Length, dst->MaximumLength );
331 memcpy( dst->Buffer, src->Buffer, len );
333 /* append terminating '\0' if enough space */
334 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
336 else dst->Length = 0;
340 /**************************************************************************
341 * RtlDuplicateUnicodeString (NTDLL.@)
343 * Duplicates an unicode string.
346 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
347 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
348 * STATUS_NO_MEMORY, if the allocation fails.
351 * For add_nul there are several possible values:
352 * 0 = destination will not be '\0' terminated,
353 * 1 = destination will be '\0' terminated,
354 * 3 = like 1 but for an empty source string produce '\0' terminated empty
355 * Buffer instead of assigning NULL to the Buffer.
356 * Other add_nul values are invalid.
358 NTSTATUS WINAPI RtlDuplicateUnicodeString(
359 int add_nul, /* [I] flag */
360 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
361 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
363 if (source == NULL || destination == NULL ||
364 source->Length > source->MaximumLength ||
365 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
366 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
367 return STATUS_INVALID_PARAMETER;
369 if (source->Length == 0 && add_nul != 3) {
370 destination->Length = 0;
371 destination->MaximumLength = 0;
372 destination->Buffer = NULL;
374 unsigned int destination_max_len = source->Length;
377 destination_max_len += sizeof(WCHAR);
379 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
380 if (destination->Buffer == NULL) {
381 return STATUS_NO_MEMORY;
383 memcpy(destination->Buffer, source->Buffer, source->Length);
384 destination->Length = source->Length;
385 destination->MaximumLength = source->Length;
386 /* append terminating '\0' if enough space */
388 destination->MaximumLength = destination_max_len;
389 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
394 return STATUS_SUCCESS;
398 /**************************************************************************
399 * RtlEraseUnicodeString (NTDLL.@)
401 * Overwrites a UNICODE_STRING with zeros.
406 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
410 memset( str->Buffer, 0, str->MaximumLength );
421 /******************************************************************************
422 * RtlCompareString (NTDLL.@)
424 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
430 len = min(s1->Length, s2->Length);
436 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
440 while (!ret && len--) ret = *p1++ - *p2++;
442 if (!ret) ret = s1->Length - s2->Length;
447 /******************************************************************************
448 * RtlCompareUnicodeString (NTDLL.@)
450 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
451 BOOLEAN CaseInsensitive )
457 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
463 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
467 while (!ret && len--) ret = *p1++ - *p2++;
469 if (!ret) ret = s1->Length - s2->Length;
474 /**************************************************************************
475 * RtlEqualString (NTDLL.@)
477 * Determine if two strings are equal.
480 * s1 [I] Source string
481 * s2 [I] String to compare to s1
482 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
485 * Non-zero if s1 is equal to s2, 0 otherwise.
487 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
489 if (s1->Length != s2->Length) return FALSE;
490 return !RtlCompareString( s1, s2, CaseInsensitive );
494 /**************************************************************************
495 * RtlEqualUnicodeString (NTDLL.@)
497 * Unicode version of RtlEqualString.
499 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
500 BOOLEAN CaseInsensitive )
502 if (s1->Length != s2->Length) return FALSE;
503 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
507 /**************************************************************************
508 * RtlPrefixString (NTDLL.@)
510 * Determine if one string is a prefix of another.
513 * s1 [I] Prefix to look for in s2
514 * s2 [I] String that may contain s1 as a prefix
515 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
518 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
520 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
524 if (s1->Length > s2->Length) return FALSE;
527 for (i = 0; i < s1->Length; i++)
528 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
532 for (i = 0; i < s1->Length; i++)
533 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
539 /**************************************************************************
540 * RtlPrefixUnicodeString (NTDLL.@)
542 * Unicode version of RtlPrefixString.
544 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
545 const UNICODE_STRING *s2,
546 BOOLEAN ignore_case )
550 if (s1->Length > s2->Length) return FALSE;
553 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
554 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
558 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
559 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
565 /**************************************************************************
566 * RtlEqualComputerName (NTDLL.@)
568 * Determine if two computer names are the same.
571 * left [I] First computer name
572 * right [I] Second computer name
575 * 0 if the names are equal, non-zero otherwise.
578 * The comparison is case insensitive.
580 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
581 const UNICODE_STRING *right)
584 STRING upLeft, upRight;
586 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
588 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
590 ret = RtlEqualString( &upLeft, &upRight, FALSE );
591 RtlFreeOemString( &upRight );
593 RtlFreeOemString( &upLeft );
599 /**************************************************************************
600 * RtlEqualDomainName (NTDLL.@)
602 * Determine if two domain names are the same.
605 * left [I] First domain name
606 * right [I] Second domain name
609 * 0 if the names are equal, non-zero otherwise.
612 * The comparison is case insensitive.
614 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
615 const UNICODE_STRING *right)
617 return RtlEqualComputerName(left, right);
621 /**************************************************************************
622 * RtlAnsiCharToUnicodeChar (NTDLL.@)
624 * Converts the first ansi character to a unicode character.
627 * ansi [I/O] Pointer to the ansi string.
630 * Unicode representation of the first character in the ansi string.
633 * Upon successful completion, the char pointer ansi points to is
634 * incremented by the size of the character.
636 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
639 DWORD charSize = sizeof(CHAR);
641 if (is_dbcs_leadbyte(ansi_table, **ansi))
644 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
651 COPY BETWEEN ANSI_STRING or UNICODE_STRING
652 there is no parameter checking, it just crashes
656 /**************************************************************************
657 * RtlAnsiStringToUnicodeString (NTDLL.@)
659 * Converts an ansi string to an unicode string.
662 * Success: STATUS_SUCCESS. uni contains the converted string
663 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
664 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
665 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
668 * This function always writes a terminating '\0'.
670 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
671 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
672 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
673 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
675 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
677 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
678 uni->Length = total - sizeof(WCHAR);
681 uni->MaximumLength = total;
682 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
683 return STATUS_NO_MEMORY;
685 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
687 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
688 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
689 return STATUS_SUCCESS;
693 /**************************************************************************
694 * RtlOemStringToUnicodeString (NTDLL.@)
696 * Converts an oem string to an unicode string.
699 * Success: STATUS_SUCCESS. uni contains the converted string
700 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
701 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
702 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
705 * This function always writes a terminating '\0'.
707 NTSTATUS WINAPI RtlOemStringToUnicodeString(
708 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
709 const STRING *oem, /* [I] Oem string to be converted */
710 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
712 DWORD total = RtlOemStringToUnicodeSize( oem );
714 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
715 uni->Length = total - sizeof(WCHAR);
718 uni->MaximumLength = total;
719 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
720 return STATUS_NO_MEMORY;
722 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
724 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
725 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
726 return STATUS_SUCCESS;
730 /**************************************************************************
731 * RtlUnicodeStringToAnsiString (NTDLL.@)
733 * Converts an unicode string to an ansi string.
736 * Success: STATUS_SUCCESS. ansi contains the converted string
737 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
738 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
741 * This function always writes a terminating '\0'.
742 * It performs a partial copy if ansi is too small.
744 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
745 STRING *ansi, /* [I/O] Destination for the ansi string */
746 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
747 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
749 NTSTATUS ret = STATUS_SUCCESS;
750 DWORD len = RtlUnicodeStringToAnsiSize( uni );
752 ansi->Length = len - 1;
755 ansi->MaximumLength = len;
756 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
757 return STATUS_NO_MEMORY;
759 else if (ansi->MaximumLength < len)
761 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
762 ansi->Length = ansi->MaximumLength - 1;
763 ret = STATUS_BUFFER_OVERFLOW;
766 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
767 ansi->Buffer[ansi->Length] = 0;
772 /**************************************************************************
773 * RtlUnicodeStringToOemString (NTDLL.@)
775 * Converts a Rtl Unicode string to an OEM string.
778 * oem [O] Destination for OEM string
779 * uni [I] Source Unicode string
780 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
783 * Success: STATUS_SUCCESS. oem contains the converted string
784 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
785 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
788 * If doalloc is TRUE, the length allocated is uni->Length + 1.
789 * This function always '\0' terminates the string returned.
791 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
792 const UNICODE_STRING *uni,
795 NTSTATUS ret = STATUS_SUCCESS;
796 DWORD len = RtlUnicodeStringToOemSize( uni );
798 oem->Length = len - 1;
801 oem->MaximumLength = len;
802 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
803 return STATUS_NO_MEMORY;
805 else if (oem->MaximumLength < len)
807 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
808 oem->Length = oem->MaximumLength - 1;
809 ret = STATUS_BUFFER_OVERFLOW;
812 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
813 oem->Buffer[oem->Length] = 0;
818 /**************************************************************************
819 * RtlMultiByteToUnicodeN (NTDLL.@)
821 * Converts a multi-byte string to a Unicode string.
827 * Performs a partial copy if dst is too small.
829 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
830 LPCSTR src, DWORD srclen )
833 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
835 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
836 return STATUS_SUCCESS;
840 /**************************************************************************
841 * RtlOemToUnicodeN (NTDLL.@)
843 * Converts a multi-byte string in the OEM code page to a Unicode string.
848 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
849 LPCSTR src, DWORD srclen )
851 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
853 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
854 return STATUS_SUCCESS;
858 /**************************************************************************
859 * RtlUnicodeToMultiByteN (NTDLL.@)
861 * Converts a Unicode string to a multi-byte string in the ANSI code page.
866 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
867 LPCWSTR src, DWORD srclen )
869 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
870 dst, dstlen, NULL, NULL );
872 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
873 return STATUS_SUCCESS;
877 /**************************************************************************
878 * RtlUnicodeToOemN (NTDLL.@)
880 * Converts a Unicode string to a multi-byte string in the OEM code page.
885 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
886 LPCWSTR src, DWORD srclen )
888 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
889 dst, dstlen, NULL, NULL );
891 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
892 return STATUS_SUCCESS;
901 /**************************************************************************
902 * RtlUpperChar (NTDLL.@)
904 * Converts an Ascii character to uppercase.
907 * ch [I] Character to convert
910 * The uppercase character value.
913 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
914 * All other input characters are returned unchanged. The locale and
915 * multibyte characters are not taken into account (as native DLL).
917 CHAR WINAPI RtlUpperChar( CHAR ch )
919 if (ch >= 'a' && ch <= 'z') {
920 return ch - 'a' + 'A';
927 /**************************************************************************
928 * RtlUpperString (NTDLL.@)
930 * Converts an Ascii string to uppercase.
933 * dst [O] Destination for converted string
934 * src [I] Source string to convert
940 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
941 * All other src characters are copied unchanged to dst. The locale and
942 * multibyte characters are not taken into account (as native DLL).
943 * The number of character copied is the minimum of src->Length and
944 * the dst->MaximumLength.
946 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
948 unsigned int i, len = min(src->Length, dst->MaximumLength);
950 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
955 /**************************************************************************
956 * RtlUpcaseUnicodeChar (NTDLL.@)
958 * Converts an Unicode character to uppercase.
961 * wch [I] Character to convert
964 * The uppercase character value.
966 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
968 return toupperW(wch);
972 /**************************************************************************
973 * RtlDowncaseUnicodeChar (NTDLL.@)
975 * Converts an Unicode character to lowercase.
978 * wch [I] Character to convert
981 * The lowercase character value.
983 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
985 return tolowerW(wch);
989 /**************************************************************************
990 * RtlUpcaseUnicodeString (NTDLL.@)
992 * Converts an Unicode string to uppercase.
995 * dest [O] Destination for converted string
996 * src [I] Source string to convert
997 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1000 * Success: STATUS_SUCCESS. dest contains the converted string.
1001 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1002 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1005 * dest is never '\0' terminated because it may be equal to src, and src
1006 * might not be '\0' terminated. dest->Length is only set upon success.
1008 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
1009 const UNICODE_STRING *src,
1012 DWORD i, len = src->Length;
1016 dest->MaximumLength = len;
1017 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1018 return STATUS_NO_MEMORY;
1020 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1022 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
1024 return STATUS_SUCCESS;
1028 /**************************************************************************
1029 * RtlDowncaseUnicodeString (NTDLL.@)
1031 * Converts an Unicode string to lowercase.
1034 * dest [O] Destination for converted string
1035 * src [I] Source string to convert
1036 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1039 * Success: STATUS_SUCCESS. dest contains the converted string.
1040 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1041 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1044 * dest is never '\0' terminated because it may be equal to src, and src
1045 * might not be '\0' terminated. dest->Length is only set upon success.
1047 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1048 UNICODE_STRING *dest,
1049 const UNICODE_STRING *src,
1053 DWORD len = src->Length;
1056 dest->MaximumLength = len;
1057 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1058 return STATUS_NO_MEMORY;
1060 } else if (len > dest->MaximumLength) {
1061 return STATUS_BUFFER_OVERFLOW;
1064 for (i = 0; i < len/sizeof(WCHAR); i++) {
1065 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1068 return STATUS_SUCCESS;
1072 /**************************************************************************
1073 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1075 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1081 * writes terminating 0
1083 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1084 const UNICODE_STRING *src,
1088 UNICODE_STRING upcase;
1090 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1092 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1093 RtlFreeUnicodeString( &upcase );
1099 /**************************************************************************
1100 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1102 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1103 * stored in STRING format.
1109 * writes terminating 0
1111 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1112 const UNICODE_STRING *src,
1116 UNICODE_STRING upcase;
1118 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1120 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1121 RtlFreeUnicodeString( &upcase );
1127 /**************************************************************************
1128 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1130 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1131 * stored in STRING format.
1137 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1139 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1140 const UNICODE_STRING *uni,
1144 UNICODE_STRING upcase;
1147 upcase.Buffer = tmp;
1148 upcase.MaximumLength = sizeof(tmp);
1149 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1150 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1154 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1158 oem->MaximumLength = len;
1159 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1161 ret = STATUS_NO_MEMORY;
1165 else if (oem->MaximumLength < len)
1167 ret = STATUS_BUFFER_OVERFLOW;
1168 oem->Length = oem->MaximumLength;
1169 if (!oem->MaximumLength) goto done;
1171 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1173 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1179 /**************************************************************************
1180 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1182 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1187 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1188 LPCWSTR src, DWORD srclen )
1194 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1195 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1196 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1197 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1202 /**************************************************************************
1203 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1205 * Converts a Unicode string to the equivalent OEM upper-case representation.
1210 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1211 LPCWSTR src, DWORD srclen )
1217 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1218 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1219 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1220 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1230 /**************************************************************************
1231 * RtlOemStringToUnicodeSize (NTDLL.@)
1232 * RtlxOemStringToUnicodeSize (NTDLL.@)
1234 * Calculate the size in bytes necessary for the Unicode conversion of str,
1235 * including the terminating '\0'.
1238 * str [I] String to calculate the size of
1241 * The calculated size.
1243 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1245 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1246 return (ret + 1) * sizeof(WCHAR);
1250 /**************************************************************************
1251 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1252 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1254 * Calculate the size in bytes necessary for the Unicode conversion of str,
1255 * including the terminating '\0'.
1258 * str [I] String to calculate the size of
1261 * The calculated size.
1263 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1266 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1267 return ret + sizeof(WCHAR);
1271 /**************************************************************************
1272 * RtlMultiByteToUnicodeSize (NTDLL.@)
1274 * Compute the size in bytes necessary for the Unicode conversion of str,
1275 * without the terminating '\0'.
1278 * size [O] Destination for size
1279 * str [I] String to calculate the size of
1280 * len [I] Length of str
1285 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1287 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1288 return STATUS_SUCCESS;
1292 /**************************************************************************
1293 * RtlUnicodeToMultiByteSize (NTDLL.@)
1295 * Calculate the size in bytes necessary for the multibyte conversion of str,
1296 * without the terminating '\0'.
1299 * size [O] Destination for size
1300 * str [I] String to calculate the size of
1301 * len [I] Length of str
1306 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1308 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1309 return STATUS_SUCCESS;
1313 /**************************************************************************
1314 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1315 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1317 * Calculate the size in bytes necessary for the Ansi conversion of str,
1318 * including the terminating '\0'.
1321 * str [I] String to calculate the size of
1324 * The calculated size.
1326 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1329 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1334 /**************************************************************************
1335 * RtlUnicodeStringToOemSize (NTDLL.@)
1336 * RtlxUnicodeStringToOemSize (NTDLL.@)
1338 * Calculate the size in bytes necessary for the OEM conversion of str,
1339 * including the terminating '\0'.
1342 * str [I] String to calculate the size of
1345 * The calculated size.
1347 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1349 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1350 NULL, 0, NULL, NULL ) + 1;
1354 /**************************************************************************
1355 * RtlAppendAsciizToString (NTDLL.@)
1357 * Concatenates a buffered character string and a '\0' terminated character
1361 * Success: STATUS_SUCCESS. src is appended to dest.
1362 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1363 * to hold the concatenated string.
1366 * if src is NULL dest is unchanged.
1367 * dest is never '\0' terminated.
1369 NTSTATUS WINAPI RtlAppendAsciizToString(
1370 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1371 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1374 unsigned int src_len = strlen(src);
1375 unsigned int dest_len = src_len + dest->Length;
1377 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1378 memcpy(dest->Buffer + dest->Length, src, src_len);
1379 dest->Length = dest_len;
1381 return STATUS_SUCCESS;
1385 /**************************************************************************
1386 * RtlAppendStringToString (NTDLL.@)
1388 * Concatenates two buffered character strings
1391 * Success: STATUS_SUCCESS. src is appended to dest.
1392 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1393 * to hold the concatenated string.
1396 * if src->length is zero dest is unchanged.
1397 * dest is never '\0' terminated.
1399 NTSTATUS WINAPI RtlAppendStringToString(
1400 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1401 const STRING *src) /* [I] Buffered character string to be concatenated */
1403 if (src->Length != 0) {
1404 unsigned int dest_len = src->Length + dest->Length;
1406 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1407 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1408 dest->Length = dest_len;
1410 return STATUS_SUCCESS;
1414 /**************************************************************************
1415 * RtlAppendUnicodeToString (NTDLL.@)
1417 * Concatenates a buffered unicode string and a '\0' terminated unicode
1421 * Success: STATUS_SUCCESS. src is appended to dest.
1422 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1423 * to hold the concatenated string.
1426 * if src is NULL dest is unchanged.
1427 * dest is '\0' terminated when the MaximumLength allows it.
1428 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1431 * Does not write in the src->Buffer beyond MaximumLength when
1432 * MaximumLength is odd as the native function does.
1434 NTSTATUS WINAPI RtlAppendUnicodeToString(
1435 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1436 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1439 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1440 unsigned int dest_len = src_len + dest->Length;
1442 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1443 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1444 dest->Length = dest_len;
1445 /* append terminating '\0' if enough space */
1446 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1447 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1450 return STATUS_SUCCESS;
1454 /**************************************************************************
1455 * RtlAppendUnicodeStringToString (NTDLL.@)
1457 * Concatenates two buffered unicode strings
1460 * Success: STATUS_SUCCESS. src is appended to dest.
1461 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1462 * to hold the concatenated string.
1465 * if src->length is zero dest is unchanged.
1466 * dest is '\0' terminated when the MaximumLength allows it.
1467 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1470 * Does not write in the src->Buffer beyond MaximumLength when
1471 * MaximumLength is odd as the native function does.
1473 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1474 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1475 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1477 if (src->Length != 0) {
1478 unsigned int dest_len = src->Length + dest->Length;
1480 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1481 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1482 dest->Length = dest_len;
1483 /* append terminating '\0' if enough space */
1484 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1485 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1488 return STATUS_SUCCESS;
1492 /**************************************************************************
1493 * RtlFindCharInUnicodeString (NTDLL.@)
1495 * Searches for one of several unicode characters in an unicode string.
1498 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1499 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1501 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1502 int flags, /* [I] Flags */
1503 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1504 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1505 USHORT *pos) /* [O] Position of the first character found + 2 */
1508 unsigned int search_idx;
1512 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1513 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1514 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1515 *pos = (main_idx + 1) * sizeof(WCHAR);
1516 return STATUS_SUCCESS;
1521 return STATUS_NOT_FOUND;
1523 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1524 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1525 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1526 *pos = main_idx * sizeof(WCHAR);
1527 return STATUS_SUCCESS;
1532 return STATUS_NOT_FOUND;
1534 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1536 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1537 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1540 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1541 *pos = (main_idx + 1) * sizeof(WCHAR);
1542 return STATUS_SUCCESS;
1546 return STATUS_NOT_FOUND;
1548 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1550 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1551 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1554 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1555 *pos = main_idx * sizeof(WCHAR);
1556 return STATUS_SUCCESS;
1560 return STATUS_NOT_FOUND;
1562 return STATUS_NOT_FOUND;
1570 /**************************************************************************
1571 * RtlIsTextUnicode (NTDLL.@)
1573 * Attempt to guess whether a text buffer is Unicode.
1576 * buf [I] Text buffer to test
1577 * len [I] Length of buf
1578 * pf [O] Destination for test results
1581 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1584 * Should implement more tests.
1586 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1588 const WCHAR *s = buf;
1590 unsigned int flags = ~0U, out_flags = 0;
1592 if (len < sizeof(WCHAR))
1594 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1601 * Apply various tests to the text string. According to the
1602 * docs, each test "passed" sets the corresponding flag in
1603 * the output flags. But some of the tests are mutually
1604 * exclusive, so I don't see how you could pass all tests ...
1607 /* Check for an odd length ... pass if even. */
1608 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1610 len /= sizeof(WCHAR);
1611 /* Windows only checks the first 256 characters */
1612 if (len > 256) len = 256;
1614 /* Check for the special byte order unicode marks. */
1615 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1616 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1618 /* apply some statistical analysis */
1619 if (flags & IS_TEXT_UNICODE_STATISTICS)
1622 /* FIXME: checks only for ASCII characters in the unicode stream */
1623 for (i = 0; i < len; i++)
1625 if (s[i] <= 255) stats++;
1627 if (stats > len / 2)
1628 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1631 /* Check for unicode NULL chars */
1632 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1634 for (i = 0; i < len; i++)
1638 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1649 /* check for flags that indicate it's definitely not valid Unicode */
1650 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1651 /* now check for invalid ASCII, and assume Unicode if so */
1652 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1653 /* now check for Unicode flags */
1654 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1660 /**************************************************************************
1661 * RtlCharToInteger (NTDLL.@)
1663 * Converts a character string into its integer equivalent.
1666 * Success: STATUS_SUCCESS. value contains the converted number
1667 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1668 * STATUS_ACCESS_VIOLATION, if value is NULL.
1671 * For base 0 it uses 10 as base and the string should be in the format
1672 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1673 * For other bases the string should be in the format
1674 * "{whitespace} [+|-] {digits}".
1675 * No check is made for value overflow, only the lower 32 bits are assigned.
1676 * If str is NULL it crashes, as the native function does.
1679 * This function does not read garbage behind '\0' as the native version does.
1681 NTSTATUS WINAPI RtlCharToInteger(
1682 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1683 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1684 ULONG *value) /* [O] Destination for the converted value */
1688 ULONG RunningTotal = 0;
1691 while (*str != '\0' && *str <= ' ') {
1697 } else if (*str == '-') {
1704 if (str[0] == '0') {
1705 if (str[1] == 'b') {
1708 } else if (str[1] == 'o') {
1711 } else if (str[1] == 'x') {
1716 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1717 return STATUS_INVALID_PARAMETER;
1720 if (value == NULL) {
1721 return STATUS_ACCESS_VIOLATION;
1724 while (*str != '\0') {
1726 if (chCurrent >= '0' && chCurrent <= '9') {
1727 digit = chCurrent - '0';
1728 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1729 digit = chCurrent - 'A' + 10;
1730 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1731 digit = chCurrent - 'a' + 10;
1735 if (digit < 0 || digit >= base) {
1736 *value = bMinus ? -RunningTotal : RunningTotal;
1737 return STATUS_SUCCESS;
1740 RunningTotal = RunningTotal * base + digit;
1744 *value = bMinus ? -RunningTotal : RunningTotal;
1745 return STATUS_SUCCESS;
1749 /**************************************************************************
1750 * RtlIntegerToChar (NTDLL.@)
1752 * Converts an unsigned integer to a character string.
1755 * Success: STATUS_SUCCESS. str contains the converted number
1756 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1757 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1758 * STATUS_ACCESS_VIOLATION, if str is NULL.
1761 * Instead of base 0 it uses 10 as base.
1762 * Writes at most length characters to the string str.
1763 * Str is '\0' terminated when length allows it.
1764 * When str fits exactly in length characters the '\0' is omitted.
1766 NTSTATUS WINAPI RtlIntegerToChar(
1767 ULONG value, /* [I] Value to be converted */
1768 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1769 ULONG length, /* [I] Length of the str buffer in bytes */
1770 PCHAR str) /* [O] Destination for the converted value */
1779 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1780 return STATUS_INVALID_PARAMETER;
1788 digit = value % base;
1789 value = value / base;
1793 *pos = 'A' + digit - 10;
1795 } while (value != 0L);
1797 len = &buffer[32] - pos;
1799 return STATUS_BUFFER_OVERFLOW;
1800 } else if (str == NULL) {
1801 return STATUS_ACCESS_VIOLATION;
1802 } else if (len == length) {
1803 memcpy(str, pos, len);
1805 memcpy(str, pos, len + 1);
1807 return STATUS_SUCCESS;
1811 /**************************************************************************
1812 * RtlUnicodeStringToInteger (NTDLL.@)
1814 * Converts an unicode string into its integer equivalent.
1817 * Success: STATUS_SUCCESS. value contains the converted number
1818 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1819 * STATUS_ACCESS_VIOLATION, if value is NULL.
1822 * For base 0 it uses 10 as base and the string should be in the format
1823 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1824 * For other bases the string should be in the format
1825 * "{whitespace} [+|-] {digits}".
1826 * No check is made for value overflow, only the lower 32 bits are assigned.
1827 * If str is NULL it crashes, as the native function does.
1830 * This function does not read garbage on string length 0 as the native
1833 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1834 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1835 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1836 ULONG *value) /* [O] Destination for the converted value */
1838 LPWSTR lpwstr = str->Buffer;
1839 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1842 ULONG RunningTotal = 0;
1845 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1850 if (CharsRemaining >= 1) {
1851 if (*lpwstr == '+') {
1854 } else if (*lpwstr == '-') {
1863 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1864 if (lpwstr[1] == 'b') {
1866 CharsRemaining -= 2;
1868 } else if (lpwstr[1] == 'o') {
1870 CharsRemaining -= 2;
1872 } else if (lpwstr[1] == 'x') {
1874 CharsRemaining -= 2;
1878 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1879 return STATUS_INVALID_PARAMETER;
1882 if (value == NULL) {
1883 return STATUS_ACCESS_VIOLATION;
1886 while (CharsRemaining >= 1) {
1887 wchCurrent = *lpwstr;
1888 if (wchCurrent >= '0' && wchCurrent <= '9') {
1889 digit = wchCurrent - '0';
1890 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1891 digit = wchCurrent - 'A' + 10;
1892 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1893 digit = wchCurrent - 'a' + 10;
1897 if (digit < 0 || digit >= base) {
1898 *value = bMinus ? -RunningTotal : RunningTotal;
1899 return STATUS_SUCCESS;
1902 RunningTotal = RunningTotal * base + digit;
1907 *value = bMinus ? -RunningTotal : RunningTotal;
1908 return STATUS_SUCCESS;
1912 /**************************************************************************
1913 * RtlIntegerToUnicodeString (NTDLL.@)
1915 * Converts an unsigned integer to a '\0' terminated unicode string.
1918 * Success: STATUS_SUCCESS. str contains the converted number
1919 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1920 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1921 * (with the '\0' termination). In this case str->Length
1922 * is set to the length, the string would have (which can
1923 * be larger than the MaximumLength).
1926 * Instead of base 0 it uses 10 as base.
1927 * If str is NULL it crashes, as the native function does.
1930 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1931 * The native function does this when the string would be longer than 16
1932 * characters even when the string parameter is long enough.
1934 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1935 ULONG value, /* [I] Value to be converted */
1936 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1937 UNICODE_STRING *str) /* [O] Destination for the converted value */
1945 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1946 return STATUS_INVALID_PARAMETER;
1954 digit = value % base;
1955 value = value / base;
1959 *pos = 'A' + digit - 10;
1961 } while (value != 0L);
1963 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1964 if (str->Length >= str->MaximumLength) {
1965 return STATUS_BUFFER_OVERFLOW;
1967 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1969 return STATUS_SUCCESS;
1973 /*************************************************************************
1974 * RtlGUIDFromString (NTDLL.@)
1976 * Convert a string representation of a GUID into a GUID.
1979 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1980 * guid [O] Destination for the converted GUID
1983 * Success: STATUS_SUCCESS. guid contains the converted value.
1984 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1987 * See RtlStringFromGUID.
1989 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
1992 const WCHAR *lpszCLSID = str->Buffer;
1993 BYTE* lpOut = (BYTE*)guid;
1995 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1997 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1998 * to memory: DWORD... WORD WORD BYTES............
2005 if (*lpszCLSID != '{')
2006 return STATUS_INVALID_PARAMETER;
2009 case 9: case 14: case 19: case 24:
2010 if (*lpszCLSID != '-')
2011 return STATUS_INVALID_PARAMETER;
2015 if (*lpszCLSID != '}')
2016 return STATUS_INVALID_PARAMETER;
2021 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
2024 /* Read two hex digits as a byte value */
2025 if (ch >= '0' && ch <= '9') ch = ch - '0';
2026 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
2027 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
2028 else return STATUS_INVALID_PARAMETER;
2030 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
2031 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
2032 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
2033 else return STATUS_INVALID_PARAMETER;
2035 byte = ch << 4 | ch2;
2039 #ifndef WORDS_BIGENDIAN
2040 /* For Big Endian machines, we store the data such that the
2041 * dword/word members can be read as DWORDS and WORDS correctly. */
2043 case 1: lpOut[3] = byte; break;
2044 case 3: lpOut[2] = byte; break;
2045 case 5: lpOut[1] = byte; break;
2046 case 7: lpOut[0] = byte; lpOut += 4; break;
2048 case 10: case 15: lpOut[1] = byte; break;
2049 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2052 default: lpOut[0] = byte; lpOut++; break;
2054 lpszCLSID++; /* Skip 2nd character of byte */
2062 return STATUS_SUCCESS;
2065 /*************************************************************************
2066 * RtlStringFromGUID (NTDLL.@)
2068 * Convert a GUID into a string representation of a GUID.
2071 * guid [I] GUID to convert
2072 * str [O] Destination for the converted string
2075 * Success: STATUS_SUCCESS. str contains the converted value.
2076 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2079 * See RtlGUIDFromString.
2081 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2083 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2084 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2085 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2086 '%','0','2','X','%','0','2','X','}','\0' };
2088 TRACE("(%p,%p)\n", guid, str);
2090 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2091 str->MaximumLength = str->Length + sizeof(WCHAR);
2092 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2095 str->Length = str->MaximumLength = 0;
2096 return STATUS_NO_MEMORY;
2098 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2099 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2100 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2102 return STATUS_SUCCESS;