4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright (C) 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 #define GUID_STRING_LENGTH 38
43 UINT NlsAnsiCodePage = 0;
44 BYTE NlsMbCodePageTag = 0;
45 BYTE NlsMbOemCodePageTag = 0;
47 static const union cptable *ansi_table;
48 static const union cptable *oem_table;
49 static const union cptable* unix_table; /* NULL if UTF8 */
52 /**************************************************************************
53 * __wine_init_codepages (NTDLL.@)
55 * Set the code page once kernel32 is loaded. Should be done differently.
57 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
58 const union cptable *ucp)
63 NlsAnsiCodePage = ansi->info.codepage;
66 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
69 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
70 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
73 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
74 const char* defchar, int *used )
77 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
78 if (used) *used = 0; /* all chars are valid for UTF-8 */
79 return wine_utf8_wcstombs( src, srclen, dst, dstlen );
82 /**************************************************************************
83 * RtlInitAnsiString (NTDLL.@)
85 * Initializes a buffered ansi string.
91 * Assigns source to target->Buffer. The length of source is assigned to
92 * target->Length and target->MaximumLength. If source is NULL the length
93 * of source is assumed to be 0.
95 void WINAPI RtlInitAnsiString(
96 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
97 PCSZ source) /* [I] '\0' terminated string used to initialize target */
99 if ((target->Buffer = (PCHAR) source))
101 target->Length = strlen(source);
102 target->MaximumLength = target->Length + 1;
104 else target->Length = target->MaximumLength = 0;
107 /**************************************************************************
108 * RtlInitAnsiStringEx (NTDLL.@)
110 * Initializes a buffered ansi string.
113 * An appropriate NTSTATUS value.
116 * Assigns source to target->Buffer. The length of source is assigned to
117 * target->Length and target->MaximumLength. If source is NULL the length
118 * of source is assumed to be 0.
120 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
124 unsigned int len = strlen(source);
126 return STATUS_NAME_TOO_LONG;
128 target->Buffer = (PCHAR) source;
129 target->Length = len;
130 target->MaximumLength = len + 1;
134 target->Buffer = NULL;
136 target->MaximumLength = 0;
138 return STATUS_SUCCESS;
141 /**************************************************************************
142 * RtlInitString (NTDLL.@)
144 * Initializes a buffered string.
150 * Assigns source to target->Buffer. The length of source is assigned to
151 * target->Length and target->MaximumLength. If source is NULL the length
152 * of source is assumed to be 0.
154 void WINAPI RtlInitString(
155 PSTRING target, /* [I/O] Buffered string to be initialized */
156 PCSZ source) /* [I] '\0' terminated string used to initialize target */
158 RtlInitAnsiString( target, source );
162 /**************************************************************************
163 * RtlFreeAnsiString (NTDLL.@)
165 void WINAPI RtlFreeAnsiString( PSTRING str )
169 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
170 RtlZeroMemory( str, sizeof(*str) );
175 /**************************************************************************
176 * RtlFreeOemString (NTDLL.@)
178 void WINAPI RtlFreeOemString( PSTRING str )
180 RtlFreeAnsiString( str );
184 /**************************************************************************
185 * RtlCopyString (NTDLL.@)
187 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
191 unsigned int len = min( src->Length, dst->MaximumLength );
192 memcpy( dst->Buffer, src->Buffer, len );
195 else dst->Length = 0;
199 /**************************************************************************
200 * RtlInitUnicodeString (NTDLL.@)
202 * Initializes a buffered unicode string.
208 * Assigns source to target->Buffer. The length of source is assigned to
209 * target->Length and target->MaximumLength. If source is NULL the length
210 * of source is assumed to be 0.
212 void WINAPI RtlInitUnicodeString(
213 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
214 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
216 if ((target->Buffer = (PWSTR) source))
218 unsigned int length = strlenW(source) * sizeof(WCHAR);
221 target->Length = length;
222 target->MaximumLength = target->Length + sizeof(WCHAR);
224 else target->Length = target->MaximumLength = 0;
228 /**************************************************************************
229 * RtlInitUnicodeStringEx (NTDLL.@)
231 * Initializes a buffered unicode string.
234 * Success: STATUS_SUCCESS. target is initialized.
235 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
238 * Assigns source to target->Buffer. The length of source is assigned to
239 * target->Length and target->MaximumLength. If source is NULL the length
240 * of source is assumed to be 0.
242 NTSTATUS WINAPI RtlInitUnicodeStringEx(
243 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
244 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
246 if (source != NULL) {
247 unsigned int len = strlenW(source) * sizeof(WCHAR);
250 return STATUS_NAME_TOO_LONG;
252 target->Length = len;
253 target->MaximumLength = len + sizeof(WCHAR);
254 target->Buffer = (PWSTR) source;
258 target->MaximumLength = 0;
259 target->Buffer = NULL;
261 return STATUS_SUCCESS;
265 /**************************************************************************
266 * RtlCreateUnicodeString (NTDLL.@)
268 * Creates a UNICODE_STRING from a null-terminated Unicode string.
274 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
276 int len = (strlenW(src) + 1) * sizeof(WCHAR);
277 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
278 memcpy( target->Buffer, src, len );
279 target->MaximumLength = len;
280 target->Length = len - sizeof(WCHAR);
285 /**************************************************************************
286 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
288 * Creates a UNICODE_STRING from a null-terminated Ascii string.
294 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
297 RtlInitAnsiString( &ansi, src );
298 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
302 /**************************************************************************
303 * RtlFreeUnicodeString (NTDLL.@)
305 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
306 * RtlCreateUnicodeStringFromAsciiz().
311 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
315 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
316 RtlZeroMemory( str, sizeof(*str) );
321 /**************************************************************************
322 * RtlCopyUnicodeString (NTDLL.@)
324 * Copies from one UNICODE_STRING to another.
329 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
333 unsigned int len = min( src->Length, dst->MaximumLength );
334 memcpy( dst->Buffer, src->Buffer, len );
336 /* append terminating '\0' if enough space */
337 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
339 else dst->Length = 0;
343 /**************************************************************************
344 * RtlDuplicateUnicodeString (NTDLL.@)
346 * Duplicates an unicode string.
349 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
350 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
351 * STATUS_NO_MEMORY, if the allocation fails.
354 * For add_nul there are several possible values:
355 * 0 = destination will not be '\0' terminated,
356 * 1 = destination will be '\0' terminated,
357 * 3 = like 1 but for an empty source string produce '\0' terminated empty
358 * Buffer instead of assigning NULL to the Buffer.
359 * Other add_nul values are invalid.
361 NTSTATUS WINAPI RtlDuplicateUnicodeString(
362 int add_nul, /* [I] flag */
363 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
364 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
366 if (source == NULL || destination == NULL ||
367 source->Length > source->MaximumLength ||
368 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
369 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
370 return STATUS_INVALID_PARAMETER;
372 if (source->Length == 0 && add_nul != 3) {
373 destination->Length = 0;
374 destination->MaximumLength = 0;
375 destination->Buffer = NULL;
377 unsigned int destination_max_len = source->Length;
380 destination_max_len += sizeof(WCHAR);
382 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
383 if (destination->Buffer == NULL) {
384 return STATUS_NO_MEMORY;
386 memcpy(destination->Buffer, source->Buffer, source->Length);
387 destination->Length = source->Length;
388 destination->MaximumLength = source->Length;
389 /* append terminating '\0' if enough space */
391 destination->MaximumLength = destination_max_len;
392 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
397 return STATUS_SUCCESS;
401 /**************************************************************************
402 * RtlEraseUnicodeString (NTDLL.@)
404 * Overwrites a UNICODE_STRING with zeros.
409 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
413 memset( str->Buffer, 0, str->MaximumLength );
424 /******************************************************************************
425 * RtlCompareString (NTDLL.@)
427 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
433 len = min(s1->Length, s2->Length);
439 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
443 while (!ret && len--) ret = *p1++ - *p2++;
445 if (!ret) ret = s1->Length - s2->Length;
450 /******************************************************************************
451 * RtlCompareUnicodeString (NTDLL.@)
453 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
454 BOOLEAN CaseInsensitive )
460 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
466 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
470 while (!ret && len--) ret = *p1++ - *p2++;
472 if (!ret) ret = s1->Length - s2->Length;
477 /**************************************************************************
478 * RtlEqualString (NTDLL.@)
480 * Determine if two strings are equal.
483 * s1 [I] Source string
484 * s2 [I] String to compare to s1
485 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
488 * Non-zero if s1 is equal to s2, 0 otherwise.
490 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
492 if (s1->Length != s2->Length) return FALSE;
493 return !RtlCompareString( s1, s2, CaseInsensitive );
497 /**************************************************************************
498 * RtlEqualUnicodeString (NTDLL.@)
500 * Unicode version of RtlEqualString.
502 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
503 BOOLEAN CaseInsensitive )
505 if (s1->Length != s2->Length) return FALSE;
506 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
510 /**************************************************************************
511 * RtlPrefixString (NTDLL.@)
513 * Determine if one string is a prefix of another.
516 * s1 [I] Prefix to look for in s2
517 * s2 [I] String that may contain s1 as a prefix
518 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
521 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
523 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
527 if (s1->Length > s2->Length) return FALSE;
530 for (i = 0; i < s1->Length; i++)
531 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
535 for (i = 0; i < s1->Length; i++)
536 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
542 /**************************************************************************
543 * RtlPrefixUnicodeString (NTDLL.@)
545 * Unicode version of RtlPrefixString.
547 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
548 const UNICODE_STRING *s2,
549 BOOLEAN ignore_case )
553 if (s1->Length > s2->Length) return FALSE;
556 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
557 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
561 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
562 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
568 /**************************************************************************
569 * RtlEqualComputerName (NTDLL.@)
571 * Determine if two computer names are the same.
574 * left [I] First computer name
575 * right [I] Second computer name
578 * 0 if the names are equal, non-zero otherwise.
581 * The comparison is case insensitive.
583 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
584 const UNICODE_STRING *right)
587 STRING upLeft, upRight;
589 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
591 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
593 ret = RtlEqualString( &upLeft, &upRight, FALSE );
594 RtlFreeOemString( &upRight );
596 RtlFreeOemString( &upLeft );
602 /**************************************************************************
603 * RtlEqualDomainName (NTDLL.@)
605 * Determine if two domain names are the same.
608 * left [I] First domain name
609 * right [I] Second domain name
612 * 0 if the names are equal, non-zero otherwise.
615 * The comparison is case insensitive.
617 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
618 const UNICODE_STRING *right)
620 return RtlEqualComputerName(left, right);
624 /**************************************************************************
625 * RtlAnsiCharToUnicodeChar (NTDLL.@)
627 * Converts the first ansi character to a unicode character.
630 * ansi [I/O] Pointer to the ansi string.
633 * Unicode representation of the first character in the ansi string.
636 * Upon successful completion, the char pointer ansi points to is
637 * incremented by the size of the character.
639 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
642 DWORD charSize = sizeof(CHAR);
644 if (wine_is_dbcs_leadbyte(ansi_table, **ansi))
647 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
654 COPY BETWEEN ANSI_STRING or UNICODE_STRING
655 there is no parameter checking, it just crashes
659 /**************************************************************************
660 * RtlAnsiStringToUnicodeString (NTDLL.@)
662 * Converts an ansi string to an unicode string.
665 * Success: STATUS_SUCCESS. uni contains the converted string
666 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
667 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
668 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
671 * This function always writes a terminating '\0'.
673 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
674 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
675 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
676 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
678 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
680 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
681 uni->Length = total - sizeof(WCHAR);
684 uni->MaximumLength = total;
685 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
686 return STATUS_NO_MEMORY;
688 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
690 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
691 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
692 return STATUS_SUCCESS;
696 /**************************************************************************
697 * RtlOemStringToUnicodeString (NTDLL.@)
699 * Converts an oem string to an unicode string.
702 * Success: STATUS_SUCCESS. uni contains the converted string
703 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
704 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
705 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
708 * This function always writes a terminating '\0'.
710 NTSTATUS WINAPI RtlOemStringToUnicodeString(
711 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
712 const STRING *oem, /* [I] Oem string to be converted */
713 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
715 DWORD total = RtlOemStringToUnicodeSize( oem );
717 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
718 uni->Length = total - sizeof(WCHAR);
721 uni->MaximumLength = total;
722 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
723 return STATUS_NO_MEMORY;
725 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
727 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
728 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
729 return STATUS_SUCCESS;
733 /**************************************************************************
734 * RtlUnicodeStringToAnsiString (NTDLL.@)
736 * Converts an unicode string to an ansi string.
739 * Success: STATUS_SUCCESS. ansi contains the converted string
740 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
741 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
744 * This function always writes a terminating '\0'.
745 * It performs a partial copy if ansi is too small.
747 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
748 STRING *ansi, /* [I/O] Destination for the ansi string */
749 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
750 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
752 NTSTATUS ret = STATUS_SUCCESS;
753 DWORD len = RtlUnicodeStringToAnsiSize( uni );
755 ansi->Length = len - 1;
758 ansi->MaximumLength = len;
759 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
760 return STATUS_NO_MEMORY;
762 else if (ansi->MaximumLength < len)
764 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
765 ansi->Length = ansi->MaximumLength - 1;
766 ret = STATUS_BUFFER_OVERFLOW;
769 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
770 ansi->Buffer[ansi->Length] = 0;
775 /**************************************************************************
776 * RtlUnicodeStringToOemString (NTDLL.@)
778 * Converts a Rtl Unicode string to an OEM string.
781 * oem [O] Destination for OEM string
782 * uni [I] Source Unicode string
783 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
786 * Success: STATUS_SUCCESS. oem contains the converted string
787 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
788 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
791 * If doalloc is TRUE, the length allocated is uni->Length + 1.
792 * This function always '\0' terminates the string returned.
794 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
795 const UNICODE_STRING *uni,
798 NTSTATUS ret = STATUS_SUCCESS;
799 DWORD len = RtlUnicodeStringToOemSize( uni );
801 oem->Length = len - 1;
804 oem->MaximumLength = len;
805 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
806 return STATUS_NO_MEMORY;
808 else if (oem->MaximumLength < len)
810 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
811 oem->Length = oem->MaximumLength - 1;
812 ret = STATUS_BUFFER_OVERFLOW;
815 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
816 oem->Buffer[oem->Length] = 0;
821 /**************************************************************************
822 * RtlMultiByteToUnicodeN (NTDLL.@)
824 * Converts a multi-byte string to a Unicode string.
830 * Performs a partial copy if dst is too small.
832 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
833 LPCSTR src, DWORD srclen )
836 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
838 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
839 return STATUS_SUCCESS;
843 /**************************************************************************
844 * RtlOemToUnicodeN (NTDLL.@)
846 * Converts a multi-byte string in the OEM code page to a Unicode string.
851 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
852 LPCSTR src, DWORD srclen )
854 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
856 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
857 return STATUS_SUCCESS;
861 /**************************************************************************
862 * RtlUnicodeToMultiByteN (NTDLL.@)
864 * Converts a Unicode string to a multi-byte string in the ANSI code page.
869 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
870 LPCWSTR src, DWORD srclen )
872 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
873 dst, dstlen, NULL, NULL );
875 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
876 return STATUS_SUCCESS;
880 /**************************************************************************
881 * RtlUnicodeToOemN (NTDLL.@)
883 * Converts a Unicode string to a multi-byte string in the OEM code page.
888 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
889 LPCWSTR src, DWORD srclen )
891 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
892 dst, dstlen, NULL, NULL );
894 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
895 return STATUS_SUCCESS;
904 /**************************************************************************
905 * RtlUpperChar (NTDLL.@)
907 * Converts an Ascii character to uppercase.
910 * ch [I] Character to convert
913 * The uppercase character value.
916 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
917 * All other input characters are returned unchanged. The locale and
918 * multibyte characters are not taken into account (as native DLL).
920 CHAR WINAPI RtlUpperChar( CHAR ch )
922 if (ch >= 'a' && ch <= 'z') {
923 return ch - 'a' + 'A';
930 /**************************************************************************
931 * RtlUpperString (NTDLL.@)
933 * Converts an Ascii string to uppercase.
936 * dst [O] Destination for converted string
937 * src [I] Source string to convert
943 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
944 * All other src characters are copied unchanged to dst. The locale and
945 * multibyte characters are not taken into account (as native DLL).
946 * The number of character copied is the minimum of src->Length and
947 * the dst->MaximumLength.
949 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
951 unsigned int i, len = min(src->Length, dst->MaximumLength);
953 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
958 /**************************************************************************
959 * RtlUpcaseUnicodeChar (NTDLL.@)
961 * Converts an Unicode character to uppercase.
964 * wch [I] Character to convert
967 * The uppercase character value.
969 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
971 return toupperW(wch);
975 /**************************************************************************
976 * RtlDowncaseUnicodeChar (NTDLL.@)
978 * Converts an Unicode character to lowercase.
981 * wch [I] Character to convert
984 * The lowercase character value.
986 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
988 return tolowerW(wch);
992 /**************************************************************************
993 * RtlUpcaseUnicodeString (NTDLL.@)
995 * Converts an Unicode string to uppercase.
998 * dest [O] Destination for converted string
999 * src [I] Source string to convert
1000 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1003 * Success: STATUS_SUCCESS. dest contains the converted string.
1004 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1005 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1008 * dest is never '\0' terminated because it may be equal to src, and src
1009 * might not be '\0' terminated. dest->Length is only set upon success.
1011 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
1012 const UNICODE_STRING *src,
1015 DWORD i, len = src->Length;
1019 dest->MaximumLength = len;
1020 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1021 return STATUS_NO_MEMORY;
1023 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1025 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
1027 return STATUS_SUCCESS;
1031 /**************************************************************************
1032 * RtlDowncaseUnicodeString (NTDLL.@)
1034 * Converts an Unicode string to lowercase.
1037 * dest [O] Destination for converted string
1038 * src [I] Source string to convert
1039 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1042 * Success: STATUS_SUCCESS. dest contains the converted string.
1043 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1044 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1047 * dest is never '\0' terminated because it may be equal to src, and src
1048 * might not be '\0' terminated. dest->Length is only set upon success.
1050 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1051 UNICODE_STRING *dest,
1052 const UNICODE_STRING *src,
1056 DWORD len = src->Length;
1059 dest->MaximumLength = len;
1060 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1061 return STATUS_NO_MEMORY;
1063 } else if (len > dest->MaximumLength) {
1064 return STATUS_BUFFER_OVERFLOW;
1067 for (i = 0; i < len/sizeof(WCHAR); i++) {
1068 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1071 return STATUS_SUCCESS;
1075 /**************************************************************************
1076 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1078 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1084 * writes terminating 0
1086 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1087 const UNICODE_STRING *src,
1091 UNICODE_STRING upcase;
1093 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1095 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1096 RtlFreeUnicodeString( &upcase );
1102 /**************************************************************************
1103 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1105 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1106 * stored in STRING format.
1112 * writes terminating 0
1114 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1115 const UNICODE_STRING *src,
1119 UNICODE_STRING upcase;
1121 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1123 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1124 RtlFreeUnicodeString( &upcase );
1130 /**************************************************************************
1131 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1133 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1134 * stored in STRING format.
1140 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1142 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1143 const UNICODE_STRING *uni,
1147 UNICODE_STRING upcase;
1150 upcase.Buffer = tmp;
1151 upcase.MaximumLength = sizeof(tmp);
1152 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1153 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1157 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1161 oem->MaximumLength = len;
1162 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1164 ret = STATUS_NO_MEMORY;
1168 else if (oem->MaximumLength < len)
1170 ret = STATUS_BUFFER_OVERFLOW;
1171 oem->Length = oem->MaximumLength;
1172 if (!oem->MaximumLength) goto done;
1174 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1176 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1182 /**************************************************************************
1183 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1185 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1190 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1191 LPCWSTR src, DWORD srclen )
1197 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1198 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1199 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1200 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1205 /**************************************************************************
1206 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1208 * Converts a Unicode string to the equivalent OEM upper-case representation.
1213 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1214 LPCWSTR src, DWORD srclen )
1220 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1221 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1222 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1223 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1233 /**************************************************************************
1234 * RtlOemStringToUnicodeSize (NTDLL.@)
1235 * RtlxOemStringToUnicodeSize (NTDLL.@)
1237 * Calculate the size in bytes necessary for the Unicode conversion of str,
1238 * including the terminating '\0'.
1241 * str [I] String to calculate the size of
1244 * The calculated size.
1246 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1248 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1249 return (ret + 1) * sizeof(WCHAR);
1253 /**************************************************************************
1254 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1255 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1257 * Calculate the size in bytes necessary for the Unicode conversion of str,
1258 * including the terminating '\0'.
1261 * str [I] String to calculate the size of
1264 * The calculated size.
1266 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1269 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1270 return ret + sizeof(WCHAR);
1274 /**************************************************************************
1275 * RtlMultiByteToUnicodeSize (NTDLL.@)
1277 * Compute the size in bytes necessary for the Unicode conversion of str,
1278 * without the terminating '\0'.
1281 * size [O] Destination for size
1282 * str [I] String to calculate the size of
1283 * len [I] Length of str
1288 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1290 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1291 return STATUS_SUCCESS;
1295 /**************************************************************************
1296 * RtlUnicodeToMultiByteSize (NTDLL.@)
1298 * Calculate the size in bytes necessary for the multibyte conversion of str,
1299 * without the terminating '\0'.
1302 * size [O] Destination for size
1303 * str [I] String to calculate the size of
1304 * len [I] Length of str
1309 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1311 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1312 return STATUS_SUCCESS;
1316 /**************************************************************************
1317 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1318 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1320 * Calculate the size in bytes necessary for the Ansi conversion of str,
1321 * including the terminating '\0'.
1324 * str [I] String to calculate the size of
1327 * The calculated size.
1329 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1332 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1337 /**************************************************************************
1338 * RtlUnicodeStringToOemSize (NTDLL.@)
1339 * RtlxUnicodeStringToOemSize (NTDLL.@)
1341 * Calculate the size in bytes necessary for the OEM conversion of str,
1342 * including the terminating '\0'.
1345 * str [I] String to calculate the size of
1348 * The calculated size.
1350 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1352 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1353 NULL, 0, NULL, NULL ) + 1;
1357 /**************************************************************************
1358 * RtlAppendAsciizToString (NTDLL.@)
1360 * Concatenates a buffered character string and a '\0' terminated character
1364 * Success: STATUS_SUCCESS. src is appended to dest.
1365 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1366 * to hold the concatenated string.
1369 * if src is NULL dest is unchanged.
1370 * dest is never '\0' terminated.
1372 NTSTATUS WINAPI RtlAppendAsciizToString(
1373 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1374 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1377 unsigned int src_len = strlen(src);
1378 unsigned int dest_len = src_len + dest->Length;
1380 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1381 memcpy(dest->Buffer + dest->Length, src, src_len);
1382 dest->Length = dest_len;
1384 return STATUS_SUCCESS;
1388 /**************************************************************************
1389 * RtlAppendStringToString (NTDLL.@)
1391 * Concatenates two buffered character strings
1394 * Success: STATUS_SUCCESS. src is appended to dest.
1395 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1396 * to hold the concatenated string.
1399 * if src->length is zero dest is unchanged.
1400 * dest is never '\0' terminated.
1402 NTSTATUS WINAPI RtlAppendStringToString(
1403 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1404 const STRING *src) /* [I] Buffered character string to be concatenated */
1406 if (src->Length != 0) {
1407 unsigned int dest_len = src->Length + dest->Length;
1409 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1410 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1411 dest->Length = dest_len;
1413 return STATUS_SUCCESS;
1417 /**************************************************************************
1418 * RtlAppendUnicodeToString (NTDLL.@)
1420 * Concatenates a buffered unicode string and a '\0' terminated unicode
1424 * Success: STATUS_SUCCESS. src is appended to dest.
1425 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1426 * to hold the concatenated string.
1429 * if src is NULL dest is unchanged.
1430 * dest is '\0' terminated when the MaximumLength allows it.
1431 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1434 * Does not write in the src->Buffer beyond MaximumLength when
1435 * MaximumLength is odd as the native function does.
1437 NTSTATUS WINAPI RtlAppendUnicodeToString(
1438 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1439 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1442 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1443 unsigned int dest_len = src_len + dest->Length;
1445 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1446 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
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 * RtlAppendUnicodeStringToString (NTDLL.@)
1460 * Concatenates two buffered unicode strings
1463 * Success: STATUS_SUCCESS. src is appended to dest.
1464 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1465 * to hold the concatenated string.
1468 * if src->length is zero dest is unchanged.
1469 * dest is '\0' terminated when the MaximumLength allows it.
1470 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1473 * Does not write in the src->Buffer beyond MaximumLength when
1474 * MaximumLength is odd as the native function does.
1476 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1477 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1478 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1480 if (src->Length != 0) {
1481 unsigned int dest_len = src->Length + dest->Length;
1483 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1484 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1485 dest->Length = dest_len;
1486 /* append terminating '\0' if enough space */
1487 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1488 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1491 return STATUS_SUCCESS;
1495 /**************************************************************************
1496 * RtlFindCharInUnicodeString (NTDLL.@)
1498 * Searches for one of several unicode characters in an unicode string.
1501 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1502 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1504 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1505 int flags, /* [I] Flags */
1506 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1507 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1508 USHORT *pos) /* [O] Position of the first character found + 2 */
1511 unsigned int search_idx;
1515 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1516 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1517 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1518 *pos = (main_idx + 1) * sizeof(WCHAR);
1519 return STATUS_SUCCESS;
1524 return STATUS_NOT_FOUND;
1526 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1527 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1528 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1529 *pos = main_idx * sizeof(WCHAR);
1530 return STATUS_SUCCESS;
1535 return STATUS_NOT_FOUND;
1537 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1539 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1540 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1543 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1544 *pos = (main_idx + 1) * sizeof(WCHAR);
1545 return STATUS_SUCCESS;
1549 return STATUS_NOT_FOUND;
1551 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1553 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1554 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1557 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1558 *pos = main_idx * sizeof(WCHAR);
1559 return STATUS_SUCCESS;
1563 return STATUS_NOT_FOUND;
1565 return STATUS_NOT_FOUND;
1573 /**************************************************************************
1574 * RtlIsTextUnicode (NTDLL.@)
1576 * Attempt to guess whether a text buffer is Unicode.
1579 * buf [I] Text buffer to test
1580 * len [I] Length of buf
1581 * pf [O] Destination for test results
1584 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1587 * Should implement more tests.
1589 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1591 const WCHAR *s = buf;
1593 unsigned int flags = ~0U, out_flags = 0;
1595 if (len < sizeof(WCHAR))
1597 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1604 * Apply various tests to the text string. According to the
1605 * docs, each test "passed" sets the corresponding flag in
1606 * the output flags. But some of the tests are mutually
1607 * exclusive, so I don't see how you could pass all tests ...
1610 /* Check for an odd length ... pass if even. */
1611 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1613 len /= sizeof(WCHAR);
1614 /* Windows only checks the first 256 characters */
1615 if (len > 256) len = 256;
1617 /* Check for the special byte order unicode marks. */
1618 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1619 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1621 /* apply some statistical analysis */
1622 if (flags & IS_TEXT_UNICODE_STATISTICS)
1625 /* FIXME: checks only for ASCII characters in the unicode stream */
1626 for (i = 0; i < len; i++)
1628 if (s[i] <= 255) stats++;
1630 if (stats > len / 2)
1631 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1634 /* Check for unicode NULL chars */
1635 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1637 for (i = 0; i < len; i++)
1641 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1652 /* check for flags that indicate it's definitely not valid Unicode */
1653 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1654 /* now check for invalid ASCII, and assume Unicode if so */
1655 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1656 /* now check for Unicode flags */
1657 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1663 /**************************************************************************
1664 * RtlCharToInteger (NTDLL.@)
1666 * Converts a character string into its integer equivalent.
1669 * Success: STATUS_SUCCESS. value contains the converted number
1670 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1671 * STATUS_ACCESS_VIOLATION, if value is NULL.
1674 * For base 0 it uses 10 as base and the string should be in the format
1675 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1676 * For other bases the string should be in the format
1677 * "{whitespace} [+|-] {digits}".
1678 * No check is made for value overflow, only the lower 32 bits are assigned.
1679 * If str is NULL it crashes, as the native function does.
1682 * This function does not read garbage behind '\0' as the native version does.
1684 NTSTATUS WINAPI RtlCharToInteger(
1685 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1686 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1687 ULONG *value) /* [O] Destination for the converted value */
1691 ULONG RunningTotal = 0;
1694 while (*str != '\0' && *str <= ' ') {
1700 } else if (*str == '-') {
1707 if (str[0] == '0') {
1708 if (str[1] == 'b') {
1711 } else if (str[1] == 'o') {
1714 } else if (str[1] == 'x') {
1719 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1720 return STATUS_INVALID_PARAMETER;
1723 if (value == NULL) {
1724 return STATUS_ACCESS_VIOLATION;
1727 while (*str != '\0') {
1729 if (chCurrent >= '0' && chCurrent <= '9') {
1730 digit = chCurrent - '0';
1731 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1732 digit = chCurrent - 'A' + 10;
1733 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1734 digit = chCurrent - 'a' + 10;
1738 if (digit < 0 || digit >= base) {
1739 *value = bMinus ? -RunningTotal : RunningTotal;
1740 return STATUS_SUCCESS;
1743 RunningTotal = RunningTotal * base + digit;
1747 *value = bMinus ? -RunningTotal : RunningTotal;
1748 return STATUS_SUCCESS;
1752 /**************************************************************************
1753 * RtlIntegerToChar (NTDLL.@)
1755 * Converts an unsigned integer to a character string.
1758 * Success: STATUS_SUCCESS. str contains the converted number
1759 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1760 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1761 * STATUS_ACCESS_VIOLATION, if str is NULL.
1764 * Instead of base 0 it uses 10 as base.
1765 * Writes at most length characters to the string str.
1766 * Str is '\0' terminated when length allows it.
1767 * When str fits exactly in length characters the '\0' is omitted.
1769 NTSTATUS WINAPI RtlIntegerToChar(
1770 ULONG value, /* [I] Value to be converted */
1771 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1772 ULONG length, /* [I] Length of the str buffer in bytes */
1773 PCHAR str) /* [O] Destination for the converted value */
1782 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1783 return STATUS_INVALID_PARAMETER;
1791 digit = value % base;
1792 value = value / base;
1796 *pos = 'A' + digit - 10;
1798 } while (value != 0L);
1800 len = &buffer[32] - pos;
1802 return STATUS_BUFFER_OVERFLOW;
1803 } else if (str == NULL) {
1804 return STATUS_ACCESS_VIOLATION;
1805 } else if (len == length) {
1806 memcpy(str, pos, len);
1808 memcpy(str, pos, len + 1);
1810 return STATUS_SUCCESS;
1814 /**************************************************************************
1815 * RtlUnicodeStringToInteger (NTDLL.@)
1817 * Converts an unicode string into its integer equivalent.
1820 * Success: STATUS_SUCCESS. value contains the converted number
1821 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1822 * STATUS_ACCESS_VIOLATION, if value is NULL.
1825 * For base 0 it uses 10 as base and the string should be in the format
1826 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1827 * For other bases the string should be in the format
1828 * "{whitespace} [+|-] {digits}".
1829 * No check is made for value overflow, only the lower 32 bits are assigned.
1830 * If str is NULL it crashes, as the native function does.
1833 * This function does not read garbage on string length 0 as the native
1836 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1837 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1838 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1839 ULONG *value) /* [O] Destination for the converted value */
1841 LPWSTR lpwstr = str->Buffer;
1842 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1845 ULONG RunningTotal = 0;
1848 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1853 if (CharsRemaining >= 1) {
1854 if (*lpwstr == '+') {
1857 } else if (*lpwstr == '-') {
1866 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1867 if (lpwstr[1] == 'b') {
1869 CharsRemaining -= 2;
1871 } else if (lpwstr[1] == 'o') {
1873 CharsRemaining -= 2;
1875 } else if (lpwstr[1] == 'x') {
1877 CharsRemaining -= 2;
1881 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1882 return STATUS_INVALID_PARAMETER;
1885 if (value == NULL) {
1886 return STATUS_ACCESS_VIOLATION;
1889 while (CharsRemaining >= 1) {
1890 wchCurrent = *lpwstr;
1891 if (wchCurrent >= '0' && wchCurrent <= '9') {
1892 digit = wchCurrent - '0';
1893 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1894 digit = wchCurrent - 'A' + 10;
1895 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1896 digit = wchCurrent - 'a' + 10;
1900 if (digit < 0 || digit >= base) {
1901 *value = bMinus ? -RunningTotal : RunningTotal;
1902 return STATUS_SUCCESS;
1905 RunningTotal = RunningTotal * base + digit;
1910 *value = bMinus ? -RunningTotal : RunningTotal;
1911 return STATUS_SUCCESS;
1915 /**************************************************************************
1916 * RtlIntegerToUnicodeString (NTDLL.@)
1918 * Converts an unsigned integer to a '\0' terminated unicode string.
1921 * Success: STATUS_SUCCESS. str contains the converted number
1922 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1923 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1924 * (with the '\0' termination). In this case str->Length
1925 * is set to the length, the string would have (which can
1926 * be larger than the MaximumLength).
1929 * Instead of base 0 it uses 10 as base.
1930 * If str is NULL it crashes, as the native function does.
1933 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1934 * The native function does this when the string would be longer than 16
1935 * characters even when the string parameter is long enough.
1937 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1938 ULONG value, /* [I] Value to be converted */
1939 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1940 UNICODE_STRING *str) /* [O] Destination for the converted value */
1948 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1949 return STATUS_INVALID_PARAMETER;
1957 digit = value % base;
1958 value = value / base;
1962 *pos = 'A' + digit - 10;
1964 } while (value != 0L);
1966 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1967 if (str->Length >= str->MaximumLength) {
1968 return STATUS_BUFFER_OVERFLOW;
1970 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1972 return STATUS_SUCCESS;
1976 /*************************************************************************
1977 * RtlGUIDFromString (NTDLL.@)
1979 * Convert a string representation of a GUID into a GUID.
1982 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1983 * guid [O] Destination for the converted GUID
1986 * Success: STATUS_SUCCESS. guid contains the converted value.
1987 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1990 * See RtlStringFromGUID.
1992 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
1995 const WCHAR *lpszCLSID = str->Buffer;
1996 BYTE* lpOut = (BYTE*)guid;
1998 TRACE("(%s,%p)\n", debugstr_us(str), guid);
2000 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2001 * to memory: DWORD... WORD WORD BYTES............
2008 if (*lpszCLSID != '{')
2009 return STATUS_INVALID_PARAMETER;
2012 case 9: case 14: case 19: case 24:
2013 if (*lpszCLSID != '-')
2014 return STATUS_INVALID_PARAMETER;
2018 if (*lpszCLSID != '}')
2019 return STATUS_INVALID_PARAMETER;
2024 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
2027 /* Read two hex digits as a byte value */
2028 if (ch >= '0' && ch <= '9') ch = ch - '0';
2029 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
2030 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
2031 else return STATUS_INVALID_PARAMETER;
2033 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
2034 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
2035 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
2036 else return STATUS_INVALID_PARAMETER;
2038 byte = ch << 4 | ch2;
2042 #ifndef WORDS_BIGENDIAN
2043 /* For Big Endian machines, we store the data such that the
2044 * dword/word members can be read as DWORDS and WORDS correctly. */
2046 case 1: lpOut[3] = byte; break;
2047 case 3: lpOut[2] = byte; break;
2048 case 5: lpOut[1] = byte; break;
2049 case 7: lpOut[0] = byte; lpOut += 4; break;
2051 case 10: case 15: lpOut[1] = byte; break;
2052 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2055 default: lpOut[0] = byte; lpOut++; break;
2057 lpszCLSID++; /* Skip 2nd character of byte */
2065 return STATUS_SUCCESS;
2068 /*************************************************************************
2069 * RtlStringFromGUID (NTDLL.@)
2071 * Convert a GUID into a string representation of a GUID.
2074 * guid [I] GUID to convert
2075 * str [O] Destination for the converted string
2078 * Success: STATUS_SUCCESS. str contains the converted value.
2079 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2082 * See RtlGUIDFromString.
2084 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2086 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2087 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2088 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2089 '%','0','2','X','%','0','2','X','}','\0' };
2091 TRACE("(%p,%p)\n", guid, str);
2093 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2094 str->MaximumLength = str->Length + sizeof(WCHAR);
2095 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2098 str->Length = str->MaximumLength = 0;
2099 return STATUS_NO_MEMORY;
2101 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2102 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2103 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2105 return STATUS_SUCCESS;