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
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 UINT NlsAnsiCodePage = 0;
42 BYTE NlsMbCodePageTag = 0;
43 BYTE NlsMbOemCodePageTag = 0;
45 static const union cptable *ansi_table;
46 static const union cptable *oem_table;
47 static const union cptable* unix_table; /* NULL if UTF8 */
50 /**************************************************************************
51 * __wine_init_codepages (NTDLL.@)
53 * Set the code page once kernel32 is loaded. Should be done differently.
55 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
56 const union cptable *ucp)
61 NlsAnsiCodePage = ansi->info.codepage;
64 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
67 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
68 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
71 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
72 const char* defchar, int *used )
75 wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used ) :
76 wine_utf8_wcstombs( src, srclen, dst, dstlen );
79 /**************************************************************************
80 * RtlInitAnsiString (NTDLL.@)
82 * Initializes a buffered ansi string.
88 * Assigns source to target->Buffer. The length of source is assigned to
89 * target->Length and target->MaximumLength. If source is NULL the length
90 * of source is assumed to be 0.
92 void WINAPI RtlInitAnsiString(
93 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
94 PCSZ source) /* [I] '\0' terminated string used to initialize target */
96 if ((target->Buffer = (PCHAR) source))
98 target->Length = strlen(source);
99 target->MaximumLength = target->Length + 1;
101 else target->Length = target->MaximumLength = 0;
105 /**************************************************************************
106 * RtlInitString (NTDLL.@)
108 * Initializes a buffered string.
114 * Assigns source to target->Buffer. The length of source is assigned to
115 * target->Length and target->MaximumLength. If source is NULL the length
116 * of source is assumed to be 0.
118 void WINAPI RtlInitString(
119 PSTRING target, /* [I/O] Buffered string to be initialized */
120 PCSZ source) /* [I] '\0' terminated string used to initialize target */
122 RtlInitAnsiString( target, source );
126 /**************************************************************************
127 * RtlFreeAnsiString (NTDLL.@)
129 void WINAPI RtlFreeAnsiString( PSTRING str )
131 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
135 /**************************************************************************
136 * RtlFreeOemString (NTDLL.@)
138 void WINAPI RtlFreeOemString( PSTRING str )
140 RtlFreeAnsiString( str );
144 /**************************************************************************
145 * RtlCopyString (NTDLL.@)
147 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
151 unsigned int len = min( src->Length, dst->MaximumLength );
152 memcpy( dst->Buffer, src->Buffer, len );
155 else dst->Length = 0;
159 /**************************************************************************
160 * RtlInitUnicodeString (NTDLL.@)
162 * Initializes a buffered unicode string.
168 * Assigns source to target->Buffer. The length of source is assigned to
169 * target->Length and target->MaximumLength. If source is NULL the length
170 * of source is assumed to be 0.
172 void WINAPI RtlInitUnicodeString(
173 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
174 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
176 if ((target->Buffer = (PWSTR) source))
178 target->Length = strlenW(source) * sizeof(WCHAR);
179 target->MaximumLength = target->Length + sizeof(WCHAR);
181 else target->Length = target->MaximumLength = 0;
185 /**************************************************************************
186 * RtlInitUnicodeStringEx (NTDLL.@)
188 * Initializes a buffered unicode string.
191 * Success: STATUS_SUCCESS. target is initialized.
192 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
195 * Assigns source to target->Buffer. The length of source is assigned to
196 * target->Length and target->MaximumLength. If source is NULL the length
197 * of source is assumed to be 0.
199 NTSTATUS WINAPI RtlInitUnicodeStringEx(
200 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
201 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
203 if (source != NULL) {
204 unsigned int len = strlenW(source) * sizeof(WCHAR);
207 return STATUS_NAME_TOO_LONG;
209 target->Length = len;
210 target->MaximumLength = len + sizeof(WCHAR);
211 target->Buffer = (PWSTR) source;
215 target->MaximumLength = 0;
216 target->Buffer = NULL;
218 return STATUS_SUCCESS;
222 /**************************************************************************
223 * RtlCreateUnicodeString (NTDLL.@)
225 * Creates a UNICODE_STRING from a null-terminated Unicode string.
231 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
233 int len = (strlenW(src) + 1) * sizeof(WCHAR);
234 if (!(target->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) return FALSE;
235 memcpy( target->Buffer, src, len );
236 target->MaximumLength = len;
237 target->Length = len - sizeof(WCHAR);
242 /**************************************************************************
243 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
245 * Creates a UNICODE_STRING from a null-terminated Ascii string.
251 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
254 RtlInitAnsiString( &ansi, src );
255 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
259 /**************************************************************************
260 * RtlFreeUnicodeString (NTDLL.@)
262 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
263 * RtlCreateUnicodeStringFromAsciiz().
268 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
270 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
274 /**************************************************************************
275 * RtlCopyUnicodeString (NTDLL.@)
277 * Copies from one UNICODE_STRING to another.
282 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
286 unsigned int len = min( src->Length, dst->MaximumLength );
287 memcpy( dst->Buffer, src->Buffer, len );
289 /* append terminating '\0' if enough space */
290 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
292 else dst->Length = 0;
296 /**************************************************************************
297 * RtlDuplicateUnicodeString (NTDLL.@)
299 * Duplicates an unicode string.
302 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
303 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
304 * STATUS_NO_MEMORY, if the allocation fails.
307 * For add_nul there are several possible values:
308 * 0 = destination will not be '\0' terminated,
309 * 1 = destination will be '\0' terminated,
310 * 3 = like 1 but for an empty source string produce '\0' terminated empty
311 * Buffer instead of assigning NULL to the Buffer.
312 * Other add_nul values are invalid.
314 NTSTATUS WINAPI RtlDuplicateUnicodeString(
315 int add_nul, /* [I] flag */
316 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
317 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
319 if (source == NULL || destination == NULL ||
320 source->Length > source->MaximumLength ||
321 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
322 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
323 return STATUS_INVALID_PARAMETER;
325 if (source->Length == 0 && add_nul != 3) {
326 destination->Length = 0;
327 destination->MaximumLength = 0;
328 destination->Buffer = NULL;
330 unsigned int destination_max_len = source->Length;
333 destination_max_len += sizeof(WCHAR);
335 destination->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len);
336 if (destination->Buffer == NULL) {
337 return STATUS_NO_MEMORY;
339 memcpy(destination->Buffer, source->Buffer, source->Length);
340 destination->Length = source->Length;
341 destination->MaximumLength = source->Length;
342 /* append terminating '\0' if enough space */
344 destination->MaximumLength = destination_max_len;
345 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
350 return STATUS_SUCCESS;
354 /**************************************************************************
355 * RtlEraseUnicodeString (NTDLL.@)
357 * Overwrites a UNICODE_STRING with zeros.
362 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
366 memset( str->Buffer, 0, str->MaximumLength );
377 /******************************************************************************
378 * RtlCompareString (NTDLL.@)
380 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
386 len = min(s1->Length, s2->Length);
392 while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++);
396 while (!ret && len--) ret = *p1++ - *p2++;
398 if (!ret) ret = s1->Length - s2->Length;
403 /******************************************************************************
404 * RtlCompareUnicodeString (NTDLL.@)
406 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
407 BOOLEAN CaseInsensitive )
413 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
419 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
423 while (!ret && len--) ret = *p1++ - *p2++;
425 if (!ret) ret = s1->Length - s2->Length;
430 /**************************************************************************
431 * RtlEqualString (NTDLL.@)
433 * Determine if two strings are equal.
436 * s1 [I] Source string
437 * s2 [I] String to compare to s1
438 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
441 * Non-zero if s1 is equal to s2, 0 otherwise.
443 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
445 if (s1->Length != s2->Length) return FALSE;
446 return !RtlCompareString( s1, s2, CaseInsensitive );
450 /**************************************************************************
451 * RtlEqualUnicodeString (NTDLL.@)
453 * Unicode version of RtlEqualString.
455 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
456 BOOLEAN CaseInsensitive )
458 if (s1->Length != s2->Length) return FALSE;
459 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
463 /**************************************************************************
464 * RtlPrefixString (NTDLL.@)
466 * Determine if one string is a prefix of another.
469 * s1 [I] Prefix to look for in s2
470 * s2 [I] String that may contain s1 as a prefix
471 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
474 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
476 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
480 if (s1->Length > s2->Length) return FALSE;
483 for (i = 0; i < s1->Length; i++)
484 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
488 for (i = 0; i < s1->Length; i++)
489 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
495 /**************************************************************************
496 * RtlPrefixUnicodeString (NTDLL.@)
498 * Unicode version of RtlPrefixString.
500 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
501 const UNICODE_STRING *s2,
502 BOOLEAN ignore_case )
506 if (s1->Length > s2->Length) return FALSE;
509 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
510 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
514 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
515 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
521 /**************************************************************************
522 * RtlEqualComputerName (NTDLL.@)
524 * Determine if two computer names are the same.
527 * left [I] First computer name
528 * right [I] Second computer name
531 * 0 if the names are equal, non-zero otherwise.
534 * The comparason is case insensitive.
536 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
537 const UNICODE_STRING *right)
540 STRING upLeft, upRight;
542 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
544 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
546 ret = RtlEqualString( &upLeft, &upRight, FALSE );
547 RtlFreeOemString( &upRight );
549 RtlFreeOemString( &upLeft );
555 /**************************************************************************
556 * RtlEqualDomainName (NTDLL.@)
558 * Determine if two domain names are the same.
561 * left [I] First domain name
562 * right [I] Second domain name
565 * 0 if the names are equal, non-zero otherwise.
568 * The comparason is case insensitive.
570 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
571 const UNICODE_STRING *right)
573 return RtlEqualComputerName(left, right);
578 COPY BETWEEN ANSI_STRING or UNICODE_STRING
579 there is no parameter checking, it just crashes
583 /**************************************************************************
584 * RtlAnsiStringToUnicodeString (NTDLL.@)
586 * Converts an ansi string to an unicode string.
589 * Success: STATUS_SUCCESS. uni contains the converted string
590 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
591 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
592 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
595 * This function always writes a terminating '\0'.
597 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
598 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
599 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
600 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
602 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
604 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
605 uni->Length = total - sizeof(WCHAR);
608 uni->MaximumLength = total;
609 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
610 return STATUS_NO_MEMORY;
612 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
614 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
615 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
616 return STATUS_SUCCESS;
620 /**************************************************************************
621 * RtlOemStringToUnicodeString (NTDLL.@)
623 * Converts an oem string to an unicode string.
626 * Success: STATUS_SUCCESS. uni contains the converted string
627 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
628 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
629 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
632 * This function always writes a terminating '\0'.
634 NTSTATUS WINAPI RtlOemStringToUnicodeString(
635 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
636 const STRING *oem, /* [I] Oem string to be converted */
637 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
639 DWORD total = RtlOemStringToUnicodeSize( oem );
641 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
642 uni->Length = total - sizeof(WCHAR);
645 uni->MaximumLength = total;
646 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
647 return STATUS_NO_MEMORY;
649 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
651 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
652 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
653 return STATUS_SUCCESS;
657 /**************************************************************************
658 * RtlUnicodeStringToAnsiString (NTDLL.@)
660 * Converts an unicode string to an ansi string.
663 * Success: STATUS_SUCCESS. ansi contains the converted string
664 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
665 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
668 * This function always writes a terminating '\0'.
669 * It performs a partial copy if ansi is too small.
671 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
672 STRING *ansi, /* [I/O] Destination for the ansi string */
673 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
674 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
676 NTSTATUS ret = STATUS_SUCCESS;
677 DWORD len = RtlUnicodeStringToAnsiSize( uni );
679 ansi->Length = len - 1;
682 ansi->MaximumLength = len;
683 if (!(ansi->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
684 return STATUS_NO_MEMORY;
686 else if (ansi->MaximumLength < len)
688 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
689 ansi->Length = ansi->MaximumLength - 1;
690 ret = STATUS_BUFFER_OVERFLOW;
693 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
694 ansi->Buffer[ansi->Length] = 0;
699 /**************************************************************************
700 * RtlUnicodeStringToOemString (NTDLL.@)
702 * Converts a Rtl Unicode string to an OEM string.
705 * oem [O] Destination for OEM string
706 * uni [I] Source Unicode string
707 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
710 * Success: STATUS_SUCCESS. oem contains the converted string
711 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
712 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
715 * If doalloc is TRUE, the length allocated is uni->Length + 1.
716 * This function always '\0' terminates the string returned.
718 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
719 const UNICODE_STRING *uni,
722 NTSTATUS ret = STATUS_SUCCESS;
723 DWORD len = RtlUnicodeStringToOemSize( uni );
725 oem->Length = len - 1;
728 oem->MaximumLength = len;
729 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
730 return STATUS_NO_MEMORY;
732 else if (oem->MaximumLength < len)
734 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
735 oem->Length = oem->MaximumLength - 1;
736 ret = STATUS_BUFFER_OVERFLOW;
739 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
740 oem->Buffer[oem->Length] = 0;
745 /**************************************************************************
746 * RtlMultiByteToUnicodeN (NTDLL.@)
748 * Converts a multi-byte string to a Unicode string.
754 * Performs a partial copy if dst is too small.
756 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
757 LPCSTR src, DWORD srclen )
760 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
762 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
763 return STATUS_SUCCESS;
767 /**************************************************************************
768 * RtlOemToUnicodeN (NTDLL.@)
770 * Converts a multi-byte string in the OEM code page to a Unicode string.
775 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
776 LPCSTR src, DWORD srclen )
778 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
780 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
781 return STATUS_SUCCESS;
785 /**************************************************************************
786 * RtlUnicodeToMultiByteN (NTDLL.@)
788 * Converts a Unicode string to a multi-byte string in the ANSI code page.
793 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
794 LPCWSTR src, DWORD srclen )
796 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
797 dst, dstlen, NULL, NULL );
799 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
800 return STATUS_SUCCESS;
804 /**************************************************************************
805 * RtlUnicodeToOemN (NTDLL.@)
807 * Converts a Unicode string to a multi-byte string in the OEM code page.
812 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
813 LPCWSTR src, DWORD srclen )
815 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
816 dst, dstlen, NULL, NULL );
818 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
819 return STATUS_SUCCESS;
828 /**************************************************************************
829 * RtlUpperChar (NTDLL.@)
831 * Converts an Ascii character to uppercase.
834 * ch [I] Character to convert
837 * The uppercase character value.
840 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
841 * All other input characters are returned unchanged. The locale and
842 * multibyte characters are not taken into account (as native DLL).
844 CHAR WINAPI RtlUpperChar( CHAR ch )
846 if (ch >= 'a' && ch <= 'z') {
847 return ch - 'a' + 'A';
854 /**************************************************************************
855 * RtlUpperString (NTDLL.@)
857 * Converts an Ascii string to uppercase.
860 * dst [O] Destination for converted string
861 * src [I] Source string to convert
867 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
868 * All other src characters are copied unchanged to dst. The locale and
869 * multibyte characters are not taken into account (as native DLL).
870 * The number of character copied is the minimum of src->Length and
871 * the dst->MaximumLength.
873 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
875 unsigned int i, len = min(src->Length, dst->MaximumLength);
877 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
882 /**************************************************************************
883 * RtlUpcaseUnicodeChar (NTDLL.@)
885 * Converts an Unicode character to uppercase.
888 * wch [I] Character to convert
891 * The uppercase character value.
893 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
895 return toupperW(wch);
899 /**************************************************************************
900 * RtlDowncaseUnicodeChar (NTDLL.@)
902 * Converts an Unicode character to lowercase.
905 * wch [I] Character to convert
908 * The lowercase character value.
910 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
912 return tolowerW(wch);
916 /**************************************************************************
917 * RtlUpcaseUnicodeString (NTDLL.@)
919 * Converts an Unicode string to uppercase.
922 * dest [O] Destination for converted string
923 * src [I] Source string to convert
924 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
927 * Success: STATUS_SUCCESS. dest contains the converted string.
928 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
929 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
932 * dest is never '\0' terminated because it may be equal to src, and src
933 * might not be '\0' terminated. dest->Length is only set upon success.
935 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
936 const UNICODE_STRING *src,
939 DWORD i, len = src->Length;
943 dest->MaximumLength = len;
944 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
945 return STATUS_NO_MEMORY;
947 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
949 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
951 return STATUS_SUCCESS;
955 /**************************************************************************
956 * RtlDowncaseUnicodeString (NTDLL.@)
958 * Converts an Unicode string to lowercase.
961 * dest [O] Destination for converted string
962 * src [I] Source string to convert
963 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
966 * Success: STATUS_SUCCESS. dest contains the converted string.
967 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
968 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
971 * dest is never '\0' terminated because it may be equal to src, and src
972 * might not be '\0' terminated. dest->Length is only set upon success.
974 NTSTATUS WINAPI RtlDowncaseUnicodeString(
975 UNICODE_STRING *dest,
976 const UNICODE_STRING *src,
980 DWORD len = src->Length;
983 dest->MaximumLength = len;
984 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) {
985 return STATUS_NO_MEMORY;
987 } else if (len > dest->MaximumLength) {
988 return STATUS_BUFFER_OVERFLOW;
991 for (i = 0; i < len/sizeof(WCHAR); i++) {
992 dest->Buffer[i] = tolowerW(src->Buffer[i]);
995 return STATUS_SUCCESS;
999 /**************************************************************************
1000 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1002 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1008 * writes terminating 0
1010 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1011 const UNICODE_STRING *src,
1015 UNICODE_STRING upcase;
1017 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1019 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1020 RtlFreeUnicodeString( &upcase );
1026 /**************************************************************************
1027 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1029 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1030 * stored in STRING format.
1036 * writes terminating 0
1038 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1039 const UNICODE_STRING *src,
1043 UNICODE_STRING upcase;
1045 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1047 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1048 RtlFreeUnicodeString( &upcase );
1054 /**************************************************************************
1055 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1057 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1058 * stored in STRING format.
1064 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1066 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1067 const UNICODE_STRING *uni,
1071 UNICODE_STRING upcase;
1073 if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
1075 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1079 oem->MaximumLength = len;
1080 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
1082 ret = STATUS_NO_MEMORY;
1086 else if (oem->MaximumLength < len)
1088 ret = STATUS_BUFFER_OVERFLOW;
1089 oem->Length = oem->MaximumLength;
1090 if (!oem->MaximumLength) goto done;
1092 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1094 RtlFreeUnicodeString( &upcase );
1100 /**************************************************************************
1101 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1103 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1108 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1109 LPCWSTR src, DWORD srclen )
1115 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1116 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1117 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1118 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1123 /**************************************************************************
1124 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1126 * Converts a Unicode string to the equivalent OEM upper-case representation.
1131 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1132 LPCWSTR src, DWORD srclen )
1138 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1139 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1140 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1141 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1151 /**************************************************************************
1152 * RtlOemStringToUnicodeSize (NTDLL.@)
1153 * RtlxOemStringToUnicodeSize (NTDLL.@)
1155 * Calculate the size in bytes necessary for the Unicode conversion of str,
1156 * including the terminating '\0'.
1159 * str [I] String to calculate the size of
1162 * The calculated size.
1164 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1166 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1167 return (ret + 1) * sizeof(WCHAR);
1171 /**************************************************************************
1172 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1173 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1175 * Calculate the size in bytes necessary for the Unicode conversion of str,
1176 * including the terminating '\0'.
1179 * str [I] String to calculate the size of
1182 * The calculated size.
1184 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1187 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1188 return ret + sizeof(WCHAR);
1192 /**************************************************************************
1193 * RtlMultiByteToUnicodeSize (NTDLL.@)
1195 * Compute the size in bytes necessary for the Unicode conversion of str,
1196 * without the terminating '\0'.
1199 * size [O] Destination for size
1200 * str [I] String to calculate the size of
1201 * len [I] Length of str
1206 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1208 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1209 return STATUS_SUCCESS;
1213 /**************************************************************************
1214 * RtlUnicodeToMultiByteSize (NTDLL.@)
1216 * Calculate the size in bytes necessary for the multibyte conversion of str,
1217 * without the terminating '\0'.
1220 * size [O] Destination for size
1221 * str [I] String to calculate the size of
1222 * len [I] Length of str
1227 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1229 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1230 return STATUS_SUCCESS;
1234 /**************************************************************************
1235 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1236 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1238 * Calculate the size in bytes necessary for the Ansi conversion of str,
1239 * including the terminating '\0'.
1242 * str [I] String to calculate the size of
1245 * The calculated size.
1247 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1250 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1255 /**************************************************************************
1256 * RtlUnicodeStringToOemSize (NTDLL.@)
1257 * RtlxUnicodeStringToOemSize (NTDLL.@)
1259 * Calculate the size in bytes necessary for the OEM conversion of str,
1260 * including the terminating '\0'.
1263 * str [I] String to calculate the size of
1266 * The calculated size.
1268 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1270 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1271 NULL, 0, NULL, NULL ) + 1;
1275 /**************************************************************************
1276 * RtlAppendAsciizToString (NTDLL.@)
1278 * Concatenates a buffered character string and a '\0' terminated character
1282 * Success: STATUS_SUCCESS. src is appended to dest.
1283 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1284 * to hold the concatenated string.
1287 * if src is NULL dest is unchanged.
1288 * dest is never '\0' terminated.
1290 NTSTATUS WINAPI RtlAppendAsciizToString(
1291 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1292 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1295 unsigned int src_len = strlen(src);
1296 unsigned int dest_len = src_len + dest->Length;
1298 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1299 memcpy(dest->Buffer + dest->Length, src, src_len);
1300 dest->Length = dest_len;
1302 return STATUS_SUCCESS;
1306 /**************************************************************************
1307 * RtlAppendStringToString (NTDLL.@)
1309 * Concatenates two buffered character strings
1312 * Success: STATUS_SUCCESS. src is appended to dest.
1313 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1314 * to hold the concatenated string.
1317 * if src->length is zero dest is unchanged.
1318 * dest is never '\0' terminated.
1320 NTSTATUS WINAPI RtlAppendStringToString(
1321 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1322 const STRING *src) /* [I] Buffered character string to be concatenated */
1324 if (src->Length != 0) {
1325 unsigned int dest_len = src->Length + dest->Length;
1327 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1328 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1329 dest->Length = dest_len;
1331 return STATUS_SUCCESS;
1335 /**************************************************************************
1336 * RtlAppendUnicodeToString (NTDLL.@)
1338 * Concatenates an buffered unicode string and a '\0' terminated unicode
1342 * Success: STATUS_SUCCESS. src is appended to dest.
1343 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1344 * to hold the concatenated string.
1347 * if src is NULL dest is unchanged.
1348 * dest is '\0' terminated when the MaximumLength allowes it.
1349 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1352 * Does not write in the src->Buffer beyond MaximumLength when
1353 * MaximumLength is odd as the native function does.
1355 NTSTATUS WINAPI RtlAppendUnicodeToString(
1356 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1357 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1360 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1361 unsigned int dest_len = src_len + dest->Length;
1363 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1364 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1365 dest->Length = dest_len;
1366 /* append terminating '\0' if enough space */
1367 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1368 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1371 return STATUS_SUCCESS;
1375 /**************************************************************************
1376 * RtlAppendUnicodeStringToString (NTDLL.@)
1378 * Concatenates two buffered unicode strings
1381 * Success: STATUS_SUCCESS. src is appended to dest.
1382 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1383 * to hold the concatenated string.
1386 * if src->length is zero dest is unchanged.
1387 * dest is '\0' terminated when the MaximumLength allowes it.
1388 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1391 * Does not write in the src->Buffer beyond MaximumLength when
1392 * MaximumLength is odd as the native function does.
1394 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1395 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1396 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1398 if (src->Length != 0) {
1399 unsigned int dest_len = src->Length + dest->Length;
1401 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1402 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1403 dest->Length = dest_len;
1404 /* append terminating '\0' if enough space */
1405 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1406 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1409 return STATUS_SUCCESS;
1413 /**************************************************************************
1414 * RtlFindCharInUnicodeString (NTDLL.@)
1416 * Searches for one of several unicode characters in an unicode string.
1419 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1420 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1422 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1423 int flags, /* [I] Flags */
1424 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1425 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1426 USHORT *pos) /* [O] Position of the first character found + 2 */
1429 unsigned int search_idx;
1433 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1434 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1435 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1436 *pos = (main_idx + 1) * sizeof(WCHAR);
1437 return STATUS_SUCCESS;
1442 return STATUS_NOT_FOUND;
1444 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1445 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1446 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1447 *pos = main_idx * sizeof(WCHAR);
1448 return STATUS_SUCCESS;
1453 return STATUS_NOT_FOUND;
1455 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1457 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1458 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1461 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1462 *pos = (main_idx + 1) * sizeof(WCHAR);
1463 return STATUS_SUCCESS;
1467 return STATUS_NOT_FOUND;
1469 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1471 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1472 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1475 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1476 *pos = main_idx * sizeof(WCHAR);
1477 return STATUS_SUCCESS;
1481 return STATUS_NOT_FOUND;
1483 return STATUS_NOT_FOUND;
1490 /* Tests that we currently implement */
1491 #define ITU_IMPLEMENTED_TESTS \
1492 (IS_TEXT_UNICODE_SIGNATURE | \
1493 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1494 IS_TEXT_UNICODE_ODD_LENGTH | \
1495 IS_TEXT_UNICODE_STATISTICS | \
1496 IS_TEXT_UNICODE_NULL_BYTES)
1498 /**************************************************************************
1499 * RtlIsTextUnicode (NTDLL.@)
1501 * Attempt to guess whether a text buffer is Unicode.
1504 * buf [I] Text buffer to test
1505 * len [I] Length of buf
1506 * pf [O] Destination for test results
1509 * The length of the string if all tests were passed, 0 otherwise.
1512 * Should implement more tests.
1514 DWORD WINAPI RtlIsTextUnicode(
1520 DWORD flags = -1, out_flags = 0;
1527 * Apply various tests to the text string. According to the
1528 * docs, each test "passed" sets the corresponding flag in
1529 * the output flags. But some of the tests are mutually
1530 * exclusive, so I don't see how you could pass all tests ...
1533 /* Check for an odd length ... pass if even. */
1534 if ((flags & IS_TEXT_UNICODE_ODD_LENGTH) && (len & 1))
1535 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1537 /* Check for the special byte order unicode marks. */
1538 if ((flags & IS_TEXT_UNICODE_SIGNATURE) && *s == 0xFEFF)
1539 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1541 if ((flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) && *s == 0xFFFE)
1542 out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1544 /* apply some statistical analysis */
1545 if (flags & IS_TEXT_UNICODE_STATISTICS)
1548 /* FIXME: checks only for ASCII characters in the unicode stream */
1549 for (i = 0; i < len / sizeof(WCHAR); i++)
1551 if (s[i] <= 255) stats++;
1553 if (stats > len / sizeof(WCHAR) / 2)
1554 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1557 /* Check for unicode NULL chars */
1558 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1561 for (i = 0; i < len / sizeof(WCHAR); i++)
1565 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1572 * Check whether the string passed all of the tests.
1574 flags &= ITU_IMPLEMENTED_TESTS;
1575 if ((out_flags & flags) != flags)
1584 /**************************************************************************
1585 * RtlCharToInteger (NTDLL.@)
1587 * Converts a character string into its integer equivalent.
1590 * Success: STATUS_SUCCESS. value contains the converted number
1591 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1592 * STATUS_ACCESS_VIOLATION, if value is NULL.
1595 * For base 0 it uses 10 as base and the string should be in the format
1596 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1597 * For other bases the string should be in the format
1598 * "{whitespace} [+|-] {digits}".
1599 * No check is made for value overflow, only the lower 32 bits are assigned.
1600 * If str is NULL it crashes, as the native function does.
1603 * This function does not read garbage behind '\0' as the native version does.
1605 NTSTATUS WINAPI RtlCharToInteger(
1606 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1607 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1608 ULONG *value) /* [O] Destination for the converted value */
1612 ULONG RunningTotal = 0;
1615 while (*str != '\0' && *str <= ' ') {
1621 } else if (*str == '-') {
1628 if (str[0] == '0') {
1629 if (str[1] == 'b') {
1632 } else if (str[1] == 'o') {
1635 } else if (str[1] == 'x') {
1640 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1641 return STATUS_INVALID_PARAMETER;
1644 if (value == NULL) {
1645 return STATUS_ACCESS_VIOLATION;
1648 while (*str != '\0') {
1650 if (chCurrent >= '0' && chCurrent <= '9') {
1651 digit = chCurrent - '0';
1652 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1653 digit = chCurrent - 'A' + 10;
1654 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1655 digit = chCurrent - 'a' + 10;
1659 if (digit < 0 || digit >= base) {
1660 *value = bMinus ? -RunningTotal : RunningTotal;
1661 return STATUS_SUCCESS;
1664 RunningTotal = RunningTotal * base + digit;
1668 *value = bMinus ? -RunningTotal : RunningTotal;
1669 return STATUS_SUCCESS;
1673 /**************************************************************************
1674 * RtlIntegerToChar (NTDLL.@)
1676 * Converts an unsigned integer to a character string.
1679 * Success: STATUS_SUCCESS. str contains the converted number
1680 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1681 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1682 * STATUS_ACCESS_VIOLATION, if str is NULL.
1685 * Instead of base 0 it uses 10 as base.
1686 * Writes at most length characters to the string str.
1687 * Str is '\0' terminated when length allowes it.
1688 * When str fits exactly in length characters the '\0' is ommitted.
1690 NTSTATUS WINAPI RtlIntegerToChar(
1691 ULONG value, /* [I] Value to be converted */
1692 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1693 ULONG length, /* [I] Length of the str buffer in bytes */
1694 PCHAR str) /* [O] Destination for the converted value */
1703 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1704 return STATUS_INVALID_PARAMETER;
1712 digit = value % base;
1713 value = value / base;
1717 *pos = 'A' + digit - 10;
1719 } while (value != 0L);
1721 len = &buffer[32] - pos;
1723 return STATUS_BUFFER_OVERFLOW;
1724 } else if (str == NULL) {
1725 return STATUS_ACCESS_VIOLATION;
1726 } else if (len == length) {
1727 memcpy(str, pos, len);
1729 memcpy(str, pos, len + 1);
1731 return STATUS_SUCCESS;
1735 /**************************************************************************
1736 * RtlUnicodeStringToInteger (NTDLL.@)
1738 * Converts an unicode string into its integer equivalent.
1741 * Success: STATUS_SUCCESS. value contains the converted number
1742 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1743 * STATUS_ACCESS_VIOLATION, if value is NULL.
1746 * For base 0 it uses 10 as base and the string should be in the format
1747 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1748 * For other bases the string should be in the format
1749 * "{whitespace} [+|-] {digits}".
1750 * No check is made for value overflow, only the lower 32 bits are assigned.
1751 * If str is NULL it crashes, as the native function does.
1754 * This function does not read garbage on string length 0 as the native
1757 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1758 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1759 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1760 ULONG *value) /* [O] Destination for the converted value */
1762 LPWSTR lpwstr = str->Buffer;
1763 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1766 ULONG RunningTotal = 0;
1769 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1774 if (CharsRemaining >= 1) {
1775 if (*lpwstr == '+') {
1778 } else if (*lpwstr == '-') {
1787 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1788 if (lpwstr[1] == 'b') {
1790 CharsRemaining -= 2;
1792 } else if (lpwstr[1] == 'o') {
1794 CharsRemaining -= 2;
1796 } else if (lpwstr[1] == 'x') {
1798 CharsRemaining -= 2;
1802 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1803 return STATUS_INVALID_PARAMETER;
1806 if (value == NULL) {
1807 return STATUS_ACCESS_VIOLATION;
1810 while (CharsRemaining >= 1) {
1811 wchCurrent = *lpwstr;
1812 if (wchCurrent >= '0' && wchCurrent <= '9') {
1813 digit = wchCurrent - '0';
1814 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1815 digit = wchCurrent - 'A' + 10;
1816 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1817 digit = wchCurrent - 'a' + 10;
1821 if (digit < 0 || digit >= base) {
1822 *value = bMinus ? -RunningTotal : RunningTotal;
1823 return STATUS_SUCCESS;
1826 RunningTotal = RunningTotal * base + digit;
1831 *value = bMinus ? -RunningTotal : RunningTotal;
1832 return STATUS_SUCCESS;
1836 /**************************************************************************
1837 * RtlIntegerToUnicodeString (NTDLL.@)
1839 * Converts an unsigned integer to a '\0' terminated unicode string.
1842 * Success: STATUS_SUCCESS. str contains the converted number
1843 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1844 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1845 * (with the '\0' termination). In this case str->Length
1846 * is set to the length, the string would have (which can
1847 * be larger than the MaximumLength).
1850 * Instead of base 0 it uses 10 as base.
1851 * If str is NULL it crashes, as the native function does.
1854 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1855 * The native function does this when the string would be longer than 16
1856 * characters even when the string parameter is long enough.
1858 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1859 ULONG value, /* [I] Value to be converted */
1860 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1861 UNICODE_STRING *str) /* [O] Destination for the converted value */
1869 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1870 return STATUS_INVALID_PARAMETER;
1878 digit = value % base;
1879 value = value / base;
1883 *pos = 'A' + digit - 10;
1885 } while (value != 0L);
1887 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1888 if (str->Length >= str->MaximumLength) {
1889 return STATUS_BUFFER_OVERFLOW;
1891 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1893 return STATUS_SUCCESS;
1897 /*************************************************************************
1898 * RtlGUIDFromString (NTDLL.@)
1900 * Convert a string representation of a GUID into a GUID.
1903 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1904 * guid [O] Destination for the converted GUID
1907 * Success: STATUS_SUCCESS. guid contains the converted value.
1908 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1911 * See RtlStringFromGUID.
1913 NTSTATUS WINAPI RtlGUIDFromString(const UNICODE_STRING *str, GUID* guid)
1916 const WCHAR *lpszCLSID = str->Buffer;
1917 BYTE* lpOut = (BYTE*)guid;
1919 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1921 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1922 * to memory: DWORD... WORD WORD BYTES............
1929 if (*lpszCLSID != '{')
1930 return STATUS_INVALID_PARAMETER;
1933 case 9: case 14: case 19: case 24:
1934 if (*lpszCLSID != '-')
1935 return STATUS_INVALID_PARAMETER;
1939 if (*lpszCLSID != '}')
1940 return STATUS_INVALID_PARAMETER;
1945 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1948 /* Read two hex digits as a byte value */
1949 if (ch >= '0' && ch <= '9') ch = ch - '0';
1950 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1951 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1952 else return STATUS_INVALID_PARAMETER;
1954 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1955 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1956 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1957 else return STATUS_INVALID_PARAMETER;
1959 byte = ch << 4 | ch2;
1963 #ifndef WORDS_BIGENDIAN
1964 /* For Big Endian machines, we store the data such that the
1965 * dword/word members can be read as DWORDS and WORDS correctly. */
1967 case 1: lpOut[3] = byte; break;
1968 case 3: lpOut[2] = byte; break;
1969 case 5: lpOut[1] = byte; break;
1970 case 7: lpOut[0] = byte; lpOut += 4; break;
1972 case 10: case 15: lpOut[1] = byte; break;
1973 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1976 default: lpOut[0] = byte; lpOut++; break;
1978 lpszCLSID++; /* Skip 2nd character of byte */
1986 return STATUS_SUCCESS;
1989 /*************************************************************************
1990 * RtlStringFromGUID (NTDLL.@)
1992 * Convert a GUID into a string representation of a GUID.
1995 * guid [I] GUID to convert
1996 * str [O] Destination for the converted string
1999 * Success: STATUS_SUCCESS. str contains the converted value.
2000 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2003 * See RtlGUIDFromString.
2005 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2007 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2008 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2009 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2010 '%','0','2','X','%','0','2','X','}','\0' };
2012 TRACE("(%p,%p)\n", guid, str);
2014 str->Buffer = (WCHAR*)RtlAllocateHeap( ntdll_get_process_heap(), 0, 40 * sizeof(WCHAR));
2017 str->Length = str->MaximumLength = 0;
2018 return STATUS_NO_MEMORY;
2020 str->Length = str->MaximumLength = 40;
2021 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2022 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2023 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2025 return STATUS_SUCCESS;