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
33 #include "wine/unicode.h"
34 #include "wine/debug.h"
35 #include "ntdll_misc.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39 #define GUID_STRING_LENGTH 38
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 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
76 if (used) *used = 0; /* all chars are valid for UTF-8 */
77 return wine_utf8_wcstombs( src, srclen, dst, dstlen );
80 /**************************************************************************
81 * RtlInitAnsiString (NTDLL.@)
83 * Initializes a buffered ansi string.
89 * Assigns source to target->Buffer. The length of source is assigned to
90 * target->Length and target->MaximumLength. If source is NULL the length
91 * of source is assumed to be 0.
93 void WINAPI RtlInitAnsiString(
94 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
95 PCSZ source) /* [I] '\0' terminated string used to initialize target */
97 if ((target->Buffer = (PCHAR) source))
99 target->Length = strlen(source);
100 target->MaximumLength = target->Length + 1;
102 else target->Length = target->MaximumLength = 0;
106 /**************************************************************************
107 * RtlInitString (NTDLL.@)
109 * Initializes a buffered string.
115 * Assigns source to target->Buffer. The length of source is assigned to
116 * target->Length and target->MaximumLength. If source is NULL the length
117 * of source is assumed to be 0.
119 void WINAPI RtlInitString(
120 PSTRING target, /* [I/O] Buffered string to be initialized */
121 PCSZ source) /* [I] '\0' terminated string used to initialize target */
123 RtlInitAnsiString( target, source );
127 /**************************************************************************
128 * RtlFreeAnsiString (NTDLL.@)
130 void WINAPI RtlFreeAnsiString( PSTRING str )
134 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
135 RtlZeroMemory( str, sizeof(*str) );
140 /**************************************************************************
141 * RtlFreeOemString (NTDLL.@)
143 void WINAPI RtlFreeOemString( PSTRING str )
145 RtlFreeAnsiString( str );
149 /**************************************************************************
150 * RtlCopyString (NTDLL.@)
152 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
156 unsigned int len = min( src->Length, dst->MaximumLength );
157 memcpy( dst->Buffer, src->Buffer, len );
160 else dst->Length = 0;
164 /**************************************************************************
165 * RtlInitUnicodeString (NTDLL.@)
167 * Initializes a buffered unicode string.
173 * Assigns source to target->Buffer. The length of source is assigned to
174 * target->Length and target->MaximumLength. If source is NULL the length
175 * of source is assumed to be 0.
177 void WINAPI RtlInitUnicodeString(
178 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
179 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
181 if ((target->Buffer = (PWSTR) source))
183 target->Length = strlenW(source) * sizeof(WCHAR);
184 target->MaximumLength = target->Length + sizeof(WCHAR);
186 else target->Length = target->MaximumLength = 0;
190 /**************************************************************************
191 * RtlInitUnicodeStringEx (NTDLL.@)
193 * Initializes a buffered unicode string.
196 * Success: STATUS_SUCCESS. target is initialized.
197 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
200 * Assigns source to target->Buffer. The length of source is assigned to
201 * target->Length and target->MaximumLength. If source is NULL the length
202 * of source is assumed to be 0.
204 NTSTATUS WINAPI RtlInitUnicodeStringEx(
205 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
206 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
208 if (source != NULL) {
209 unsigned int len = strlenW(source) * sizeof(WCHAR);
212 return STATUS_NAME_TOO_LONG;
214 target->Length = len;
215 target->MaximumLength = len + sizeof(WCHAR);
216 target->Buffer = (PWSTR) source;
220 target->MaximumLength = 0;
221 target->Buffer = NULL;
223 return STATUS_SUCCESS;
227 /**************************************************************************
228 * RtlCreateUnicodeString (NTDLL.@)
230 * Creates a UNICODE_STRING from a null-terminated Unicode string.
236 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
238 int len = (strlenW(src) + 1) * sizeof(WCHAR);
239 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
240 memcpy( target->Buffer, src, len );
241 target->MaximumLength = len;
242 target->Length = len - sizeof(WCHAR);
247 /**************************************************************************
248 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
250 * Creates a UNICODE_STRING from a null-terminated Ascii string.
256 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
259 RtlInitAnsiString( &ansi, src );
260 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
264 /**************************************************************************
265 * RtlFreeUnicodeString (NTDLL.@)
267 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
268 * RtlCreateUnicodeStringFromAsciiz().
273 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
277 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
278 RtlZeroMemory( str, sizeof(*str) );
283 /**************************************************************************
284 * RtlCopyUnicodeString (NTDLL.@)
286 * Copies from one UNICODE_STRING to another.
291 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
295 unsigned int len = min( src->Length, dst->MaximumLength );
296 memcpy( dst->Buffer, src->Buffer, len );
298 /* append terminating '\0' if enough space */
299 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
301 else dst->Length = 0;
305 /**************************************************************************
306 * RtlDuplicateUnicodeString (NTDLL.@)
308 * Duplicates an unicode string.
311 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
312 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
313 * STATUS_NO_MEMORY, if the allocation fails.
316 * For add_nul there are several possible values:
317 * 0 = destination will not be '\0' terminated,
318 * 1 = destination will be '\0' terminated,
319 * 3 = like 1 but for an empty source string produce '\0' terminated empty
320 * Buffer instead of assigning NULL to the Buffer.
321 * Other add_nul values are invalid.
323 NTSTATUS WINAPI RtlDuplicateUnicodeString(
324 int add_nul, /* [I] flag */
325 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
326 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
328 if (source == NULL || destination == NULL ||
329 source->Length > source->MaximumLength ||
330 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
331 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
332 return STATUS_INVALID_PARAMETER;
334 if (source->Length == 0 && add_nul != 3) {
335 destination->Length = 0;
336 destination->MaximumLength = 0;
337 destination->Buffer = NULL;
339 unsigned int destination_max_len = source->Length;
342 destination_max_len += sizeof(WCHAR);
344 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
345 if (destination->Buffer == NULL) {
346 return STATUS_NO_MEMORY;
348 memcpy(destination->Buffer, source->Buffer, source->Length);
349 destination->Length = source->Length;
350 destination->MaximumLength = source->Length;
351 /* append terminating '\0' if enough space */
353 destination->MaximumLength = destination_max_len;
354 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
359 return STATUS_SUCCESS;
363 /**************************************************************************
364 * RtlEraseUnicodeString (NTDLL.@)
366 * Overwrites a UNICODE_STRING with zeros.
371 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
375 memset( str->Buffer, 0, str->MaximumLength );
386 /******************************************************************************
387 * RtlCompareString (NTDLL.@)
389 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
395 len = min(s1->Length, s2->Length);
401 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
405 while (!ret && len--) ret = *p1++ - *p2++;
407 if (!ret) ret = s1->Length - s2->Length;
412 /******************************************************************************
413 * RtlCompareUnicodeString (NTDLL.@)
415 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
416 BOOLEAN CaseInsensitive )
422 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
428 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
432 while (!ret && len--) ret = *p1++ - *p2++;
434 if (!ret) ret = s1->Length - s2->Length;
439 /**************************************************************************
440 * RtlEqualString (NTDLL.@)
442 * Determine if two strings are equal.
445 * s1 [I] Source string
446 * s2 [I] String to compare to s1
447 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
450 * Non-zero if s1 is equal to s2, 0 otherwise.
452 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
454 if (s1->Length != s2->Length) return FALSE;
455 return !RtlCompareString( s1, s2, CaseInsensitive );
459 /**************************************************************************
460 * RtlEqualUnicodeString (NTDLL.@)
462 * Unicode version of RtlEqualString.
464 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
465 BOOLEAN CaseInsensitive )
467 if (s1->Length != s2->Length) return FALSE;
468 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
472 /**************************************************************************
473 * RtlPrefixString (NTDLL.@)
475 * Determine if one string is a prefix of another.
478 * s1 [I] Prefix to look for in s2
479 * s2 [I] String that may contain s1 as a prefix
480 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
483 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
485 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
489 if (s1->Length > s2->Length) return FALSE;
492 for (i = 0; i < s1->Length; i++)
493 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
497 for (i = 0; i < s1->Length; i++)
498 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
504 /**************************************************************************
505 * RtlPrefixUnicodeString (NTDLL.@)
507 * Unicode version of RtlPrefixString.
509 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
510 const UNICODE_STRING *s2,
511 BOOLEAN ignore_case )
515 if (s1->Length > s2->Length) return FALSE;
518 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
519 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
523 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
524 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
530 /**************************************************************************
531 * RtlEqualComputerName (NTDLL.@)
533 * Determine if two computer names are the same.
536 * left [I] First computer name
537 * right [I] Second computer name
540 * 0 if the names are equal, non-zero otherwise.
543 * The comparison is case insensitive.
545 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
546 const UNICODE_STRING *right)
549 STRING upLeft, upRight;
551 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
553 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
555 ret = RtlEqualString( &upLeft, &upRight, FALSE );
556 RtlFreeOemString( &upRight );
558 RtlFreeOemString( &upLeft );
564 /**************************************************************************
565 * RtlEqualDomainName (NTDLL.@)
567 * Determine if two domain names are the same.
570 * left [I] First domain name
571 * right [I] Second domain name
574 * 0 if the names are equal, non-zero otherwise.
577 * The comparison is case insensitive.
579 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
580 const UNICODE_STRING *right)
582 return RtlEqualComputerName(left, right);
586 /**************************************************************************
587 * RtlAnsiCharToUnicodeChar (NTDLL.@)
589 * Converts the first ansi character to a unicode character.
592 * ansi [I/O] Pointer to the ansi string.
595 * Unicode representation of the first character in the ansi string.
598 * Upon successful completion, the char pointer ansi points to is
599 * incremented by the size of the character.
601 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
604 DWORD charSize = sizeof(CHAR);
606 if (is_dbcs_leadbyte(ansi_table, **ansi))
609 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
616 COPY BETWEEN ANSI_STRING or UNICODE_STRING
617 there is no parameter checking, it just crashes
621 /**************************************************************************
622 * RtlAnsiStringToUnicodeString (NTDLL.@)
624 * Converts an ansi string to an unicode string.
627 * Success: STATUS_SUCCESS. uni contains the converted string
628 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
629 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
630 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
633 * This function always writes a terminating '\0'.
635 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
636 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
637 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
638 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
640 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
642 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
643 uni->Length = total - sizeof(WCHAR);
646 uni->MaximumLength = total;
647 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
648 return STATUS_NO_MEMORY;
650 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
652 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
653 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
654 return STATUS_SUCCESS;
658 /**************************************************************************
659 * RtlOemStringToUnicodeString (NTDLL.@)
661 * Converts an oem string to an unicode string.
664 * Success: STATUS_SUCCESS. uni contains the converted string
665 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
666 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
667 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
670 * This function always writes a terminating '\0'.
672 NTSTATUS WINAPI RtlOemStringToUnicodeString(
673 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
674 const STRING *oem, /* [I] Oem string to be converted */
675 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
677 DWORD total = RtlOemStringToUnicodeSize( oem );
679 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
680 uni->Length = total - sizeof(WCHAR);
683 uni->MaximumLength = total;
684 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
685 return STATUS_NO_MEMORY;
687 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
689 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
690 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
691 return STATUS_SUCCESS;
695 /**************************************************************************
696 * RtlUnicodeStringToAnsiString (NTDLL.@)
698 * Converts an unicode string to an ansi string.
701 * Success: STATUS_SUCCESS. ansi contains the converted string
702 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
703 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
706 * This function always writes a terminating '\0'.
707 * It performs a partial copy if ansi is too small.
709 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
710 STRING *ansi, /* [I/O] Destination for the ansi string */
711 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
712 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
714 NTSTATUS ret = STATUS_SUCCESS;
715 DWORD len = RtlUnicodeStringToAnsiSize( uni );
717 ansi->Length = len - 1;
720 ansi->MaximumLength = len;
721 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
722 return STATUS_NO_MEMORY;
724 else if (ansi->MaximumLength < len)
726 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
727 ansi->Length = ansi->MaximumLength - 1;
728 ret = STATUS_BUFFER_OVERFLOW;
731 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
732 ansi->Buffer[ansi->Length] = 0;
737 /**************************************************************************
738 * RtlUnicodeStringToOemString (NTDLL.@)
740 * Converts a Rtl Unicode string to an OEM string.
743 * oem [O] Destination for OEM string
744 * uni [I] Source Unicode string
745 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
748 * Success: STATUS_SUCCESS. oem contains the converted string
749 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
750 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
753 * If doalloc is TRUE, the length allocated is uni->Length + 1.
754 * This function always '\0' terminates the string returned.
756 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
757 const UNICODE_STRING *uni,
760 NTSTATUS ret = STATUS_SUCCESS;
761 DWORD len = RtlUnicodeStringToOemSize( uni );
763 oem->Length = len - 1;
766 oem->MaximumLength = len;
767 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
768 return STATUS_NO_MEMORY;
770 else if (oem->MaximumLength < len)
772 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
773 oem->Length = oem->MaximumLength - 1;
774 ret = STATUS_BUFFER_OVERFLOW;
777 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
778 oem->Buffer[oem->Length] = 0;
783 /**************************************************************************
784 * RtlMultiByteToUnicodeN (NTDLL.@)
786 * Converts a multi-byte string to a Unicode string.
792 * Performs a partial copy if dst is too small.
794 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
795 LPCSTR src, DWORD srclen )
798 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
800 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
801 return STATUS_SUCCESS;
805 /**************************************************************************
806 * RtlOemToUnicodeN (NTDLL.@)
808 * Converts a multi-byte string in the OEM code page to a Unicode string.
813 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
814 LPCSTR src, DWORD srclen )
816 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
818 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
819 return STATUS_SUCCESS;
823 /**************************************************************************
824 * RtlUnicodeToMultiByteN (NTDLL.@)
826 * Converts a Unicode string to a multi-byte string in the ANSI code page.
831 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
832 LPCWSTR src, DWORD srclen )
834 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
835 dst, dstlen, NULL, NULL );
837 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
838 return STATUS_SUCCESS;
842 /**************************************************************************
843 * RtlUnicodeToOemN (NTDLL.@)
845 * Converts a Unicode string to a multi-byte string in the OEM code page.
850 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
851 LPCWSTR src, DWORD srclen )
853 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
854 dst, dstlen, NULL, NULL );
856 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
857 return STATUS_SUCCESS;
866 /**************************************************************************
867 * RtlUpperChar (NTDLL.@)
869 * Converts an Ascii character to uppercase.
872 * ch [I] Character to convert
875 * The uppercase character value.
878 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
879 * All other input characters are returned unchanged. The locale and
880 * multibyte characters are not taken into account (as native DLL).
882 CHAR WINAPI RtlUpperChar( CHAR ch )
884 if (ch >= 'a' && ch <= 'z') {
885 return ch - 'a' + 'A';
892 /**************************************************************************
893 * RtlUpperString (NTDLL.@)
895 * Converts an Ascii string to uppercase.
898 * dst [O] Destination for converted string
899 * src [I] Source string to convert
905 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
906 * All other src characters are copied unchanged to dst. The locale and
907 * multibyte characters are not taken into account (as native DLL).
908 * The number of character copied is the minimum of src->Length and
909 * the dst->MaximumLength.
911 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
913 unsigned int i, len = min(src->Length, dst->MaximumLength);
915 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
920 /**************************************************************************
921 * RtlUpcaseUnicodeChar (NTDLL.@)
923 * Converts an Unicode character to uppercase.
926 * wch [I] Character to convert
929 * The uppercase character value.
931 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
933 return toupperW(wch);
937 /**************************************************************************
938 * RtlDowncaseUnicodeChar (NTDLL.@)
940 * Converts an Unicode character to lowercase.
943 * wch [I] Character to convert
946 * The lowercase character value.
948 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
950 return tolowerW(wch);
954 /**************************************************************************
955 * RtlUpcaseUnicodeString (NTDLL.@)
957 * Converts an Unicode string to uppercase.
960 * dest [O] Destination for converted string
961 * src [I] Source string to convert
962 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
965 * Success: STATUS_SUCCESS. dest contains the converted string.
966 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
967 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
970 * dest is never '\0' terminated because it may be equal to src, and src
971 * might not be '\0' terminated. dest->Length is only set upon success.
973 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
974 const UNICODE_STRING *src,
977 DWORD i, len = src->Length;
981 dest->MaximumLength = len;
982 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
983 return STATUS_NO_MEMORY;
985 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
987 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
989 return STATUS_SUCCESS;
993 /**************************************************************************
994 * RtlDowncaseUnicodeString (NTDLL.@)
996 * Converts an Unicode string to lowercase.
999 * dest [O] Destination for converted string
1000 * src [I] Source string to convert
1001 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1004 * Success: STATUS_SUCCESS. dest contains the converted string.
1005 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1006 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1009 * dest is never '\0' terminated because it may be equal to src, and src
1010 * might not be '\0' terminated. dest->Length is only set upon success.
1012 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1013 UNICODE_STRING *dest,
1014 const UNICODE_STRING *src,
1018 DWORD len = src->Length;
1021 dest->MaximumLength = len;
1022 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1023 return STATUS_NO_MEMORY;
1025 } else if (len > dest->MaximumLength) {
1026 return STATUS_BUFFER_OVERFLOW;
1029 for (i = 0; i < len/sizeof(WCHAR); i++) {
1030 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1033 return STATUS_SUCCESS;
1037 /**************************************************************************
1038 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1040 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1046 * writes terminating 0
1048 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1049 const UNICODE_STRING *src,
1053 UNICODE_STRING upcase;
1055 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1057 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1058 RtlFreeUnicodeString( &upcase );
1064 /**************************************************************************
1065 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1067 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1068 * stored in STRING format.
1074 * writes terminating 0
1076 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1077 const UNICODE_STRING *src,
1081 UNICODE_STRING upcase;
1083 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1085 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1086 RtlFreeUnicodeString( &upcase );
1092 /**************************************************************************
1093 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1095 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1096 * stored in STRING format.
1102 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1104 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1105 const UNICODE_STRING *uni,
1109 UNICODE_STRING upcase;
1112 upcase.Buffer = tmp;
1113 upcase.MaximumLength = sizeof(tmp);
1114 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1115 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1119 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1123 oem->MaximumLength = len;
1124 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1126 ret = STATUS_NO_MEMORY;
1130 else if (oem->MaximumLength < len)
1132 ret = STATUS_BUFFER_OVERFLOW;
1133 oem->Length = oem->MaximumLength;
1134 if (!oem->MaximumLength) goto done;
1136 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1138 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1144 /**************************************************************************
1145 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1147 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1152 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1153 LPCWSTR src, DWORD srclen )
1159 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1160 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1161 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1162 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1167 /**************************************************************************
1168 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1170 * Converts a Unicode string to the equivalent OEM upper-case representation.
1175 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1176 LPCWSTR src, DWORD srclen )
1182 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1183 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1184 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1185 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1195 /**************************************************************************
1196 * RtlOemStringToUnicodeSize (NTDLL.@)
1197 * RtlxOemStringToUnicodeSize (NTDLL.@)
1199 * Calculate the size in bytes necessary for the Unicode conversion of str,
1200 * including the terminating '\0'.
1203 * str [I] String to calculate the size of
1206 * The calculated size.
1208 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1210 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1211 return (ret + 1) * sizeof(WCHAR);
1215 /**************************************************************************
1216 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1217 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1219 * Calculate the size in bytes necessary for the Unicode conversion of str,
1220 * including the terminating '\0'.
1223 * str [I] String to calculate the size of
1226 * The calculated size.
1228 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1231 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1232 return ret + sizeof(WCHAR);
1236 /**************************************************************************
1237 * RtlMultiByteToUnicodeSize (NTDLL.@)
1239 * Compute the size in bytes necessary for the Unicode conversion of str,
1240 * without the terminating '\0'.
1243 * size [O] Destination for size
1244 * str [I] String to calculate the size of
1245 * len [I] Length of str
1250 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1252 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1253 return STATUS_SUCCESS;
1257 /**************************************************************************
1258 * RtlUnicodeToMultiByteSize (NTDLL.@)
1260 * Calculate the size in bytes necessary for the multibyte conversion of str,
1261 * without the terminating '\0'.
1264 * size [O] Destination for size
1265 * str [I] String to calculate the size of
1266 * len [I] Length of str
1271 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1273 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1274 return STATUS_SUCCESS;
1278 /**************************************************************************
1279 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1280 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1282 * Calculate the size in bytes necessary for the Ansi conversion of str,
1283 * including the terminating '\0'.
1286 * str [I] String to calculate the size of
1289 * The calculated size.
1291 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1294 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1299 /**************************************************************************
1300 * RtlUnicodeStringToOemSize (NTDLL.@)
1301 * RtlxUnicodeStringToOemSize (NTDLL.@)
1303 * Calculate the size in bytes necessary for the OEM conversion of str,
1304 * including the terminating '\0'.
1307 * str [I] String to calculate the size of
1310 * The calculated size.
1312 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1314 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1315 NULL, 0, NULL, NULL ) + 1;
1319 /**************************************************************************
1320 * RtlAppendAsciizToString (NTDLL.@)
1322 * Concatenates a buffered character string and a '\0' terminated character
1326 * Success: STATUS_SUCCESS. src is appended to dest.
1327 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1328 * to hold the concatenated string.
1331 * if src is NULL dest is unchanged.
1332 * dest is never '\0' terminated.
1334 NTSTATUS WINAPI RtlAppendAsciizToString(
1335 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1336 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1339 unsigned int src_len = strlen(src);
1340 unsigned int dest_len = src_len + dest->Length;
1342 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1343 memcpy(dest->Buffer + dest->Length, src, src_len);
1344 dest->Length = dest_len;
1346 return STATUS_SUCCESS;
1350 /**************************************************************************
1351 * RtlAppendStringToString (NTDLL.@)
1353 * Concatenates two buffered character strings
1356 * Success: STATUS_SUCCESS. src is appended to dest.
1357 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1358 * to hold the concatenated string.
1361 * if src->length is zero dest is unchanged.
1362 * dest is never '\0' terminated.
1364 NTSTATUS WINAPI RtlAppendStringToString(
1365 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1366 const STRING *src) /* [I] Buffered character string to be concatenated */
1368 if (src->Length != 0) {
1369 unsigned int dest_len = src->Length + dest->Length;
1371 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1372 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1373 dest->Length = dest_len;
1375 return STATUS_SUCCESS;
1379 /**************************************************************************
1380 * RtlAppendUnicodeToString (NTDLL.@)
1382 * Concatenates a buffered unicode string and a '\0' terminated unicode
1386 * Success: STATUS_SUCCESS. src is appended to dest.
1387 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1388 * to hold the concatenated string.
1391 * if src is NULL dest is unchanged.
1392 * dest is '\0' terminated when the MaximumLength allows it.
1393 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1396 * Does not write in the src->Buffer beyond MaximumLength when
1397 * MaximumLength is odd as the native function does.
1399 NTSTATUS WINAPI RtlAppendUnicodeToString(
1400 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1401 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1404 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1405 unsigned int dest_len = src_len + dest->Length;
1407 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1408 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1409 dest->Length = dest_len;
1410 /* append terminating '\0' if enough space */
1411 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1412 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1415 return STATUS_SUCCESS;
1419 /**************************************************************************
1420 * RtlAppendUnicodeStringToString (NTDLL.@)
1422 * Concatenates two buffered unicode strings
1425 * Success: STATUS_SUCCESS. src is appended to dest.
1426 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1427 * to hold the concatenated string.
1430 * if src->length is zero dest is unchanged.
1431 * dest is '\0' terminated when the MaximumLength allows it.
1432 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1435 * Does not write in the src->Buffer beyond MaximumLength when
1436 * MaximumLength is odd as the native function does.
1438 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1439 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1440 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1442 if (src->Length != 0) {
1443 unsigned int dest_len = src->Length + dest->Length;
1445 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1446 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1447 dest->Length = dest_len;
1448 /* append terminating '\0' if enough space */
1449 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1450 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1453 return STATUS_SUCCESS;
1457 /**************************************************************************
1458 * RtlFindCharInUnicodeString (NTDLL.@)
1460 * Searches for one of several unicode characters in an unicode string.
1463 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1464 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1466 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1467 int flags, /* [I] Flags */
1468 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1469 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1470 USHORT *pos) /* [O] Position of the first character found + 2 */
1473 unsigned int search_idx;
1477 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1478 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1479 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1480 *pos = (main_idx + 1) * sizeof(WCHAR);
1481 return STATUS_SUCCESS;
1486 return STATUS_NOT_FOUND;
1488 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1489 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1490 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1491 *pos = main_idx * sizeof(WCHAR);
1492 return STATUS_SUCCESS;
1497 return STATUS_NOT_FOUND;
1499 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1501 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1502 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1505 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1506 *pos = (main_idx + 1) * sizeof(WCHAR);
1507 return STATUS_SUCCESS;
1511 return STATUS_NOT_FOUND;
1513 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1515 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1516 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1519 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1520 *pos = main_idx * sizeof(WCHAR);
1521 return STATUS_SUCCESS;
1525 return STATUS_NOT_FOUND;
1527 return STATUS_NOT_FOUND;
1535 /**************************************************************************
1536 * RtlIsTextUnicode (NTDLL.@)
1538 * Attempt to guess whether a text buffer is Unicode.
1541 * buf [I] Text buffer to test
1542 * len [I] Length of buf
1543 * pf [O] Destination for test results
1546 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1549 * Should implement more tests.
1551 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1553 const WCHAR *s = buf;
1555 unsigned int flags = ~0U, out_flags = 0;
1557 if (len < sizeof(WCHAR))
1559 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1566 * Apply various tests to the text string. According to the
1567 * docs, each test "passed" sets the corresponding flag in
1568 * the output flags. But some of the tests are mutually
1569 * exclusive, so I don't see how you could pass all tests ...
1572 /* Check for an odd length ... pass if even. */
1573 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1575 len /= sizeof(WCHAR);
1576 /* Windows only checks the first 256 characters */
1577 if (len > 256) len = 256;
1579 /* Check for the special byte order unicode marks. */
1580 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1581 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1583 /* apply some statistical analysis */
1584 if (flags & IS_TEXT_UNICODE_STATISTICS)
1587 /* FIXME: checks only for ASCII characters in the unicode stream */
1588 for (i = 0; i < len; i++)
1590 if (s[i] <= 255) stats++;
1592 if (stats > len / 2)
1593 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1596 /* Check for unicode NULL chars */
1597 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1599 for (i = 0; i < len; i++)
1603 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1614 /* check for flags that indicate it's definitely not valid Unicode */
1615 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1616 /* now check for invalid ASCII, and assume Unicode if so */
1617 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1618 /* now check for Unicode flags */
1619 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1625 /**************************************************************************
1626 * RtlCharToInteger (NTDLL.@)
1628 * Converts a character string into its integer equivalent.
1631 * Success: STATUS_SUCCESS. value contains the converted number
1632 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1633 * STATUS_ACCESS_VIOLATION, if value is NULL.
1636 * For base 0 it uses 10 as base and the string should be in the format
1637 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1638 * For other bases the string should be in the format
1639 * "{whitespace} [+|-] {digits}".
1640 * No check is made for value overflow, only the lower 32 bits are assigned.
1641 * If str is NULL it crashes, as the native function does.
1644 * This function does not read garbage behind '\0' as the native version does.
1646 NTSTATUS WINAPI RtlCharToInteger(
1647 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1648 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1649 ULONG *value) /* [O] Destination for the converted value */
1653 ULONG RunningTotal = 0;
1656 while (*str != '\0' && *str <= ' ') {
1662 } else if (*str == '-') {
1669 if (str[0] == '0') {
1670 if (str[1] == 'b') {
1673 } else if (str[1] == 'o') {
1676 } else if (str[1] == 'x') {
1681 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1682 return STATUS_INVALID_PARAMETER;
1685 if (value == NULL) {
1686 return STATUS_ACCESS_VIOLATION;
1689 while (*str != '\0') {
1691 if (chCurrent >= '0' && chCurrent <= '9') {
1692 digit = chCurrent - '0';
1693 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1694 digit = chCurrent - 'A' + 10;
1695 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1696 digit = chCurrent - 'a' + 10;
1700 if (digit < 0 || digit >= base) {
1701 *value = bMinus ? -RunningTotal : RunningTotal;
1702 return STATUS_SUCCESS;
1705 RunningTotal = RunningTotal * base + digit;
1709 *value = bMinus ? -RunningTotal : RunningTotal;
1710 return STATUS_SUCCESS;
1714 /**************************************************************************
1715 * RtlIntegerToChar (NTDLL.@)
1717 * Converts an unsigned integer to a character string.
1720 * Success: STATUS_SUCCESS. str contains the converted number
1721 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1722 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1723 * STATUS_ACCESS_VIOLATION, if str is NULL.
1726 * Instead of base 0 it uses 10 as base.
1727 * Writes at most length characters to the string str.
1728 * Str is '\0' terminated when length allows it.
1729 * When str fits exactly in length characters the '\0' is omitted.
1731 NTSTATUS WINAPI RtlIntegerToChar(
1732 ULONG value, /* [I] Value to be converted */
1733 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1734 ULONG length, /* [I] Length of the str buffer in bytes */
1735 PCHAR str) /* [O] Destination for the converted value */
1744 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1745 return STATUS_INVALID_PARAMETER;
1753 digit = value % base;
1754 value = value / base;
1758 *pos = 'A' + digit - 10;
1760 } while (value != 0L);
1762 len = &buffer[32] - pos;
1764 return STATUS_BUFFER_OVERFLOW;
1765 } else if (str == NULL) {
1766 return STATUS_ACCESS_VIOLATION;
1767 } else if (len == length) {
1768 memcpy(str, pos, len);
1770 memcpy(str, pos, len + 1);
1772 return STATUS_SUCCESS;
1776 /**************************************************************************
1777 * RtlUnicodeStringToInteger (NTDLL.@)
1779 * Converts an unicode string into its integer equivalent.
1782 * Success: STATUS_SUCCESS. value contains the converted number
1783 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1784 * STATUS_ACCESS_VIOLATION, if value is NULL.
1787 * For base 0 it uses 10 as base and the string should be in the format
1788 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1789 * For other bases the string should be in the format
1790 * "{whitespace} [+|-] {digits}".
1791 * No check is made for value overflow, only the lower 32 bits are assigned.
1792 * If str is NULL it crashes, as the native function does.
1795 * This function does not read garbage on string length 0 as the native
1798 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1799 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1800 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1801 ULONG *value) /* [O] Destination for the converted value */
1803 LPWSTR lpwstr = str->Buffer;
1804 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1807 ULONG RunningTotal = 0;
1810 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1815 if (CharsRemaining >= 1) {
1816 if (*lpwstr == '+') {
1819 } else if (*lpwstr == '-') {
1828 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1829 if (lpwstr[1] == 'b') {
1831 CharsRemaining -= 2;
1833 } else if (lpwstr[1] == 'o') {
1835 CharsRemaining -= 2;
1837 } else if (lpwstr[1] == 'x') {
1839 CharsRemaining -= 2;
1843 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1844 return STATUS_INVALID_PARAMETER;
1847 if (value == NULL) {
1848 return STATUS_ACCESS_VIOLATION;
1851 while (CharsRemaining >= 1) {
1852 wchCurrent = *lpwstr;
1853 if (wchCurrent >= '0' && wchCurrent <= '9') {
1854 digit = wchCurrent - '0';
1855 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1856 digit = wchCurrent - 'A' + 10;
1857 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1858 digit = wchCurrent - 'a' + 10;
1862 if (digit < 0 || digit >= base) {
1863 *value = bMinus ? -RunningTotal : RunningTotal;
1864 return STATUS_SUCCESS;
1867 RunningTotal = RunningTotal * base + digit;
1872 *value = bMinus ? -RunningTotal : RunningTotal;
1873 return STATUS_SUCCESS;
1877 /**************************************************************************
1878 * RtlIntegerToUnicodeString (NTDLL.@)
1880 * Converts an unsigned integer to a '\0' terminated unicode string.
1883 * Success: STATUS_SUCCESS. str contains the converted number
1884 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1885 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1886 * (with the '\0' termination). In this case str->Length
1887 * is set to the length, the string would have (which can
1888 * be larger than the MaximumLength).
1891 * Instead of base 0 it uses 10 as base.
1892 * If str is NULL it crashes, as the native function does.
1895 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1896 * The native function does this when the string would be longer than 16
1897 * characters even when the string parameter is long enough.
1899 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1900 ULONG value, /* [I] Value to be converted */
1901 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1902 UNICODE_STRING *str) /* [O] Destination for the converted value */
1910 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1911 return STATUS_INVALID_PARAMETER;
1919 digit = value % base;
1920 value = value / base;
1924 *pos = 'A' + digit - 10;
1926 } while (value != 0L);
1928 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1929 if (str->Length >= str->MaximumLength) {
1930 return STATUS_BUFFER_OVERFLOW;
1932 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1934 return STATUS_SUCCESS;
1938 /*************************************************************************
1939 * RtlGUIDFromString (NTDLL.@)
1941 * Convert a string representation of a GUID into a GUID.
1944 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1945 * guid [O] Destination for the converted GUID
1948 * Success: STATUS_SUCCESS. guid contains the converted value.
1949 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1952 * See RtlStringFromGUID.
1954 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
1957 const WCHAR *lpszCLSID = str->Buffer;
1958 BYTE* lpOut = (BYTE*)guid;
1960 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1962 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1963 * to memory: DWORD... WORD WORD BYTES............
1970 if (*lpszCLSID != '{')
1971 return STATUS_INVALID_PARAMETER;
1974 case 9: case 14: case 19: case 24:
1975 if (*lpszCLSID != '-')
1976 return STATUS_INVALID_PARAMETER;
1980 if (*lpszCLSID != '}')
1981 return STATUS_INVALID_PARAMETER;
1986 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1989 /* Read two hex digits as a byte value */
1990 if (ch >= '0' && ch <= '9') ch = ch - '0';
1991 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1992 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1993 else return STATUS_INVALID_PARAMETER;
1995 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1996 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1997 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1998 else return STATUS_INVALID_PARAMETER;
2000 byte = ch << 4 | ch2;
2004 #ifndef WORDS_BIGENDIAN
2005 /* For Big Endian machines, we store the data such that the
2006 * dword/word members can be read as DWORDS and WORDS correctly. */
2008 case 1: lpOut[3] = byte; break;
2009 case 3: lpOut[2] = byte; break;
2010 case 5: lpOut[1] = byte; break;
2011 case 7: lpOut[0] = byte; lpOut += 4; break;
2013 case 10: case 15: lpOut[1] = byte; break;
2014 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2017 default: lpOut[0] = byte; lpOut++; break;
2019 lpszCLSID++; /* Skip 2nd character of byte */
2027 return STATUS_SUCCESS;
2030 /*************************************************************************
2031 * RtlStringFromGUID (NTDLL.@)
2033 * Convert a GUID into a string representation of a GUID.
2036 * guid [I] GUID to convert
2037 * str [O] Destination for the converted string
2040 * Success: STATUS_SUCCESS. str contains the converted value.
2041 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2044 * See RtlGUIDFromString.
2046 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2048 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2049 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2050 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2051 '%','0','2','X','%','0','2','X','}','\0' };
2053 TRACE("(%p,%p)\n", guid, str);
2055 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2056 str->MaximumLength = str->Length + sizeof(WCHAR);
2057 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2060 str->Length = str->MaximumLength = 0;
2061 return STATUS_NO_MEMORY;
2063 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2064 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2065 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2067 return STATUS_SUCCESS;