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
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "ntdll_misc.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40 #define GUID_STRING_LENGTH 38
42 UINT NlsAnsiCodePage = 0;
43 BYTE NlsMbCodePageTag = 0;
44 BYTE NlsMbOemCodePageTag = 0;
46 static const union cptable *ansi_table;
47 static const union cptable *oem_table;
48 static const union cptable* unix_table; /* NULL if UTF8 */
51 /**************************************************************************
52 * __wine_init_codepages (NTDLL.@)
54 * Set the code page once kernel32 is loaded. Should be done differently.
56 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
57 const union cptable *ucp)
62 NlsAnsiCodePage = ansi->info.codepage;
65 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
68 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
69 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
72 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
73 const char* defchar, int *used )
76 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
77 if (used) *used = 0; /* all chars are valid for UTF-8 */
78 return wine_utf8_wcstombs( src, srclen, dst, dstlen );
81 /**************************************************************************
82 * RtlInitAnsiString (NTDLL.@)
84 * Initializes a buffered ansi string.
90 * Assigns source to target->Buffer. The length of source is assigned to
91 * target->Length and target->MaximumLength. If source is NULL the length
92 * of source is assumed to be 0.
94 void WINAPI RtlInitAnsiString(
95 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
96 PCSZ source) /* [I] '\0' terminated string used to initialize target */
98 if ((target->Buffer = (PCHAR) source))
100 target->Length = strlen(source);
101 target->MaximumLength = target->Length + 1;
103 else target->Length = target->MaximumLength = 0;
107 /**************************************************************************
108 * RtlInitString (NTDLL.@)
110 * Initializes a buffered string.
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 void WINAPI RtlInitString(
121 PSTRING target, /* [I/O] Buffered string to be initialized */
122 PCSZ source) /* [I] '\0' terminated string used to initialize target */
124 RtlInitAnsiString( target, source );
128 /**************************************************************************
129 * RtlFreeAnsiString (NTDLL.@)
131 void WINAPI RtlFreeAnsiString( PSTRING str )
133 if (str->Buffer) RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
137 /**************************************************************************
138 * RtlFreeOemString (NTDLL.@)
140 void WINAPI RtlFreeOemString( PSTRING str )
142 RtlFreeAnsiString( str );
146 /**************************************************************************
147 * RtlCopyString (NTDLL.@)
149 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
153 unsigned int len = min( src->Length, dst->MaximumLength );
154 memcpy( dst->Buffer, src->Buffer, len );
157 else dst->Length = 0;
161 /**************************************************************************
162 * RtlInitUnicodeString (NTDLL.@)
164 * Initializes a buffered unicode string.
170 * Assigns source to target->Buffer. The length of source is assigned to
171 * target->Length and target->MaximumLength. If source is NULL the length
172 * of source is assumed to be 0.
174 void WINAPI RtlInitUnicodeString(
175 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
176 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
178 if ((target->Buffer = (PWSTR) source))
180 target->Length = strlenW(source) * sizeof(WCHAR);
181 target->MaximumLength = target->Length + sizeof(WCHAR);
183 else target->Length = target->MaximumLength = 0;
187 /**************************************************************************
188 * RtlInitUnicodeStringEx (NTDLL.@)
190 * Initializes a buffered unicode string.
193 * Success: STATUS_SUCCESS. target is initialized.
194 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
197 * Assigns source to target->Buffer. The length of source is assigned to
198 * target->Length and target->MaximumLength. If source is NULL the length
199 * of source is assumed to be 0.
201 NTSTATUS WINAPI RtlInitUnicodeStringEx(
202 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
203 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
205 if (source != NULL) {
206 unsigned int len = strlenW(source) * sizeof(WCHAR);
209 return STATUS_NAME_TOO_LONG;
211 target->Length = len;
212 target->MaximumLength = len + sizeof(WCHAR);
213 target->Buffer = (PWSTR) source;
217 target->MaximumLength = 0;
218 target->Buffer = NULL;
220 return STATUS_SUCCESS;
224 /**************************************************************************
225 * RtlCreateUnicodeString (NTDLL.@)
227 * Creates a UNICODE_STRING from a null-terminated Unicode string.
233 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
235 int len = (strlenW(src) + 1) * sizeof(WCHAR);
236 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
237 memcpy( target->Buffer, src, len );
238 target->MaximumLength = len;
239 target->Length = len - sizeof(WCHAR);
244 /**************************************************************************
245 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
247 * Creates a UNICODE_STRING from a null-terminated Ascii string.
253 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
256 RtlInitAnsiString( &ansi, src );
257 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
261 /**************************************************************************
262 * RtlFreeUnicodeString (NTDLL.@)
264 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
265 * RtlCreateUnicodeStringFromAsciiz().
270 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
272 if (str->Buffer) RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
276 /**************************************************************************
277 * RtlCopyUnicodeString (NTDLL.@)
279 * Copies from one UNICODE_STRING to another.
284 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
288 unsigned int len = min( src->Length, dst->MaximumLength );
289 memcpy( dst->Buffer, src->Buffer, len );
291 /* append terminating '\0' if enough space */
292 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
294 else dst->Length = 0;
298 /**************************************************************************
299 * RtlDuplicateUnicodeString (NTDLL.@)
301 * Duplicates an unicode string.
304 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
305 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
306 * STATUS_NO_MEMORY, if the allocation fails.
309 * For add_nul there are several possible values:
310 * 0 = destination will not be '\0' terminated,
311 * 1 = destination will be '\0' terminated,
312 * 3 = like 1 but for an empty source string produce '\0' terminated empty
313 * Buffer instead of assigning NULL to the Buffer.
314 * Other add_nul values are invalid.
316 NTSTATUS WINAPI RtlDuplicateUnicodeString(
317 int add_nul, /* [I] flag */
318 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
319 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
321 if (source == NULL || destination == NULL ||
322 source->Length > source->MaximumLength ||
323 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
324 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
325 return STATUS_INVALID_PARAMETER;
327 if (source->Length == 0 && add_nul != 3) {
328 destination->Length = 0;
329 destination->MaximumLength = 0;
330 destination->Buffer = NULL;
332 unsigned int destination_max_len = source->Length;
335 destination_max_len += sizeof(WCHAR);
337 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
338 if (destination->Buffer == NULL) {
339 return STATUS_NO_MEMORY;
341 memcpy(destination->Buffer, source->Buffer, source->Length);
342 destination->Length = source->Length;
343 destination->MaximumLength = source->Length;
344 /* append terminating '\0' if enough space */
346 destination->MaximumLength = destination_max_len;
347 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
352 return STATUS_SUCCESS;
356 /**************************************************************************
357 * RtlEraseUnicodeString (NTDLL.@)
359 * Overwrites a UNICODE_STRING with zeros.
364 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
368 memset( str->Buffer, 0, str->MaximumLength );
379 /******************************************************************************
380 * RtlCompareString (NTDLL.@)
382 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
388 len = min(s1->Length, s2->Length);
394 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
398 while (!ret && len--) ret = *p1++ - *p2++;
400 if (!ret) ret = s1->Length - s2->Length;
405 /******************************************************************************
406 * RtlCompareUnicodeString (NTDLL.@)
408 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
409 BOOLEAN CaseInsensitive )
415 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
421 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
425 while (!ret && len--) ret = *p1++ - *p2++;
427 if (!ret) ret = s1->Length - s2->Length;
432 /**************************************************************************
433 * RtlEqualString (NTDLL.@)
435 * Determine if two strings are equal.
438 * s1 [I] Source string
439 * s2 [I] String to compare to s1
440 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
443 * Non-zero if s1 is equal to s2, 0 otherwise.
445 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
447 if (s1->Length != s2->Length) return FALSE;
448 return !RtlCompareString( s1, s2, CaseInsensitive );
452 /**************************************************************************
453 * RtlEqualUnicodeString (NTDLL.@)
455 * Unicode version of RtlEqualString.
457 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
458 BOOLEAN CaseInsensitive )
460 if (s1->Length != s2->Length) return FALSE;
461 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
465 /**************************************************************************
466 * RtlPrefixString (NTDLL.@)
468 * Determine if one string is a prefix of another.
471 * s1 [I] Prefix to look for in s2
472 * s2 [I] String that may contain s1 as a prefix
473 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
476 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
478 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
482 if (s1->Length > s2->Length) return FALSE;
485 for (i = 0; i < s1->Length; i++)
486 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
490 for (i = 0; i < s1->Length; i++)
491 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
497 /**************************************************************************
498 * RtlPrefixUnicodeString (NTDLL.@)
500 * Unicode version of RtlPrefixString.
502 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
503 const UNICODE_STRING *s2,
504 BOOLEAN ignore_case )
508 if (s1->Length > s2->Length) return FALSE;
511 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
512 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
516 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
517 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
523 /**************************************************************************
524 * RtlEqualComputerName (NTDLL.@)
526 * Determine if two computer names are the same.
529 * left [I] First computer name
530 * right [I] Second computer name
533 * 0 if the names are equal, non-zero otherwise.
536 * The comparison is case insensitive.
538 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
539 const UNICODE_STRING *right)
542 STRING upLeft, upRight;
544 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
546 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
548 ret = RtlEqualString( &upLeft, &upRight, FALSE );
549 RtlFreeOemString( &upRight );
551 RtlFreeOemString( &upLeft );
557 /**************************************************************************
558 * RtlEqualDomainName (NTDLL.@)
560 * Determine if two domain names are the same.
563 * left [I] First domain name
564 * right [I] Second domain name
567 * 0 if the names are equal, non-zero otherwise.
570 * The comparison is case insensitive.
572 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
573 const UNICODE_STRING *right)
575 return RtlEqualComputerName(left, right);
579 /**************************************************************************
580 * RtlAnsiCharToUnicodeChar (NTDLL.@)
582 * Converts the first ansi character to a unicode character.
585 * ansi [I/O] Pointer to the ansi string.
588 * Unicode representation of the first character in the ansi string.
591 * Upon successful completion, the char pointer ansi points to is
592 * incremented by the size of the character.
594 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
597 DWORD charSize = sizeof(CHAR);
599 if (is_dbcs_leadbyte(ansi_table, **ansi))
602 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
609 COPY BETWEEN ANSI_STRING or UNICODE_STRING
610 there is no parameter checking, it just crashes
614 /**************************************************************************
615 * RtlAnsiStringToUnicodeString (NTDLL.@)
617 * Converts an ansi string to an unicode string.
620 * Success: STATUS_SUCCESS. uni contains the converted string
621 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
622 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
623 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
626 * This function always writes a terminating '\0'.
628 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
629 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
630 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
631 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
633 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
635 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
636 uni->Length = total - sizeof(WCHAR);
639 uni->MaximumLength = total;
640 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
641 return STATUS_NO_MEMORY;
643 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
645 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
646 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
647 return STATUS_SUCCESS;
651 /**************************************************************************
652 * RtlOemStringToUnicodeString (NTDLL.@)
654 * Converts an oem string to an unicode string.
657 * Success: STATUS_SUCCESS. uni contains the converted string
658 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
659 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
660 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
663 * This function always writes a terminating '\0'.
665 NTSTATUS WINAPI RtlOemStringToUnicodeString(
666 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
667 const STRING *oem, /* [I] Oem string to be converted */
668 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
670 DWORD total = RtlOemStringToUnicodeSize( oem );
672 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
673 uni->Length = total - sizeof(WCHAR);
676 uni->MaximumLength = total;
677 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
678 return STATUS_NO_MEMORY;
680 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
682 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
683 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
684 return STATUS_SUCCESS;
688 /**************************************************************************
689 * RtlUnicodeStringToAnsiString (NTDLL.@)
691 * Converts an unicode string to an ansi string.
694 * Success: STATUS_SUCCESS. ansi contains the converted string
695 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
696 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
699 * This function always writes a terminating '\0'.
700 * It performs a partial copy if ansi is too small.
702 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
703 STRING *ansi, /* [I/O] Destination for the ansi string */
704 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
705 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
707 NTSTATUS ret = STATUS_SUCCESS;
708 DWORD len = RtlUnicodeStringToAnsiSize( uni );
710 ansi->Length = len - 1;
713 ansi->MaximumLength = len;
714 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
715 return STATUS_NO_MEMORY;
717 else if (ansi->MaximumLength < len)
719 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
720 ansi->Length = ansi->MaximumLength - 1;
721 ret = STATUS_BUFFER_OVERFLOW;
724 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
725 ansi->Buffer[ansi->Length] = 0;
730 /**************************************************************************
731 * RtlUnicodeStringToOemString (NTDLL.@)
733 * Converts a Rtl Unicode string to an OEM string.
736 * oem [O] Destination for OEM string
737 * uni [I] Source Unicode string
738 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
741 * Success: STATUS_SUCCESS. oem contains the converted string
742 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
743 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
746 * If doalloc is TRUE, the length allocated is uni->Length + 1.
747 * This function always '\0' terminates the string returned.
749 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
750 const UNICODE_STRING *uni,
753 NTSTATUS ret = STATUS_SUCCESS;
754 DWORD len = RtlUnicodeStringToOemSize( uni );
756 oem->Length = len - 1;
759 oem->MaximumLength = len;
760 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
761 return STATUS_NO_MEMORY;
763 else if (oem->MaximumLength < len)
765 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
766 oem->Length = oem->MaximumLength - 1;
767 ret = STATUS_BUFFER_OVERFLOW;
770 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
771 oem->Buffer[oem->Length] = 0;
776 /**************************************************************************
777 * RtlMultiByteToUnicodeN (NTDLL.@)
779 * Converts a multi-byte string to a Unicode string.
785 * Performs a partial copy if dst is too small.
787 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
788 LPCSTR src, DWORD srclen )
791 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
793 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
794 return STATUS_SUCCESS;
798 /**************************************************************************
799 * RtlOemToUnicodeN (NTDLL.@)
801 * Converts a multi-byte string in the OEM code page to a Unicode string.
806 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
807 LPCSTR src, DWORD srclen )
809 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
811 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
812 return STATUS_SUCCESS;
816 /**************************************************************************
817 * RtlUnicodeToMultiByteN (NTDLL.@)
819 * Converts a Unicode string to a multi-byte string in the ANSI code page.
824 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
825 LPCWSTR src, DWORD srclen )
827 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
828 dst, dstlen, NULL, NULL );
830 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
831 return STATUS_SUCCESS;
835 /**************************************************************************
836 * RtlUnicodeToOemN (NTDLL.@)
838 * Converts a Unicode string to a multi-byte string in the OEM code page.
843 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
844 LPCWSTR src, DWORD srclen )
846 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
847 dst, dstlen, NULL, NULL );
849 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
850 return STATUS_SUCCESS;
859 /**************************************************************************
860 * RtlUpperChar (NTDLL.@)
862 * Converts an Ascii character to uppercase.
865 * ch [I] Character to convert
868 * The uppercase character value.
871 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
872 * All other input characters are returned unchanged. The locale and
873 * multibyte characters are not taken into account (as native DLL).
875 CHAR WINAPI RtlUpperChar( CHAR ch )
877 if (ch >= 'a' && ch <= 'z') {
878 return ch - 'a' + 'A';
885 /**************************************************************************
886 * RtlUpperString (NTDLL.@)
888 * Converts an Ascii string to uppercase.
891 * dst [O] Destination for converted string
892 * src [I] Source string to convert
898 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
899 * All other src characters are copied unchanged to dst. The locale and
900 * multibyte characters are not taken into account (as native DLL).
901 * The number of character copied is the minimum of src->Length and
902 * the dst->MaximumLength.
904 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
906 unsigned int i, len = min(src->Length, dst->MaximumLength);
908 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
913 /**************************************************************************
914 * RtlUpcaseUnicodeChar (NTDLL.@)
916 * Converts an Unicode character to uppercase.
919 * wch [I] Character to convert
922 * The uppercase character value.
924 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
926 return toupperW(wch);
930 /**************************************************************************
931 * RtlDowncaseUnicodeChar (NTDLL.@)
933 * Converts an Unicode character to lowercase.
936 * wch [I] Character to convert
939 * The lowercase character value.
941 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
943 return tolowerW(wch);
947 /**************************************************************************
948 * RtlUpcaseUnicodeString (NTDLL.@)
950 * Converts an Unicode string to uppercase.
953 * dest [O] Destination for converted string
954 * src [I] Source string to convert
955 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
958 * Success: STATUS_SUCCESS. dest contains the converted string.
959 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
960 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
963 * dest is never '\0' terminated because it may be equal to src, and src
964 * might not be '\0' terminated. dest->Length is only set upon success.
966 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
967 const UNICODE_STRING *src,
970 DWORD i, len = src->Length;
974 dest->MaximumLength = len;
975 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
976 return STATUS_NO_MEMORY;
978 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
980 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
982 return STATUS_SUCCESS;
986 /**************************************************************************
987 * RtlDowncaseUnicodeString (NTDLL.@)
989 * Converts an Unicode string to lowercase.
992 * dest [O] Destination for converted string
993 * src [I] Source string to convert
994 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
997 * Success: STATUS_SUCCESS. dest contains the converted string.
998 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
999 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1002 * dest is never '\0' terminated because it may be equal to src, and src
1003 * might not be '\0' terminated. dest->Length is only set upon success.
1005 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1006 UNICODE_STRING *dest,
1007 const UNICODE_STRING *src,
1011 DWORD len = src->Length;
1014 dest->MaximumLength = len;
1015 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1016 return STATUS_NO_MEMORY;
1018 } else if (len > dest->MaximumLength) {
1019 return STATUS_BUFFER_OVERFLOW;
1022 for (i = 0; i < len/sizeof(WCHAR); i++) {
1023 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1026 return STATUS_SUCCESS;
1030 /**************************************************************************
1031 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1033 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1039 * writes terminating 0
1041 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1042 const UNICODE_STRING *src,
1046 UNICODE_STRING upcase;
1048 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1050 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1051 RtlFreeUnicodeString( &upcase );
1057 /**************************************************************************
1058 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1060 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1061 * stored in STRING format.
1067 * writes terminating 0
1069 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1070 const UNICODE_STRING *src,
1074 UNICODE_STRING upcase;
1076 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1078 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1079 RtlFreeUnicodeString( &upcase );
1085 /**************************************************************************
1086 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1088 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1089 * stored in STRING format.
1095 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1097 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1098 const UNICODE_STRING *uni,
1102 UNICODE_STRING upcase;
1105 upcase.Buffer = tmp;
1106 upcase.MaximumLength = sizeof(tmp);
1107 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1108 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1112 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1116 oem->MaximumLength = len;
1117 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1119 ret = STATUS_NO_MEMORY;
1123 else if (oem->MaximumLength < len)
1125 ret = STATUS_BUFFER_OVERFLOW;
1126 oem->Length = oem->MaximumLength;
1127 if (!oem->MaximumLength) goto done;
1129 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1131 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1137 /**************************************************************************
1138 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1140 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1145 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1146 LPCWSTR src, DWORD srclen )
1152 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1153 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1154 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1155 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1160 /**************************************************************************
1161 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1163 * Converts a Unicode string to the equivalent OEM upper-case representation.
1168 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1169 LPCWSTR src, DWORD srclen )
1175 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1176 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1177 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1178 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1188 /**************************************************************************
1189 * RtlOemStringToUnicodeSize (NTDLL.@)
1190 * RtlxOemStringToUnicodeSize (NTDLL.@)
1192 * Calculate the size in bytes necessary for the Unicode conversion of str,
1193 * including the terminating '\0'.
1196 * str [I] String to calculate the size of
1199 * The calculated size.
1201 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1203 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1204 return (ret + 1) * sizeof(WCHAR);
1208 /**************************************************************************
1209 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1210 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1212 * Calculate the size in bytes necessary for the Unicode conversion of str,
1213 * including the terminating '\0'.
1216 * str [I] String to calculate the size of
1219 * The calculated size.
1221 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1224 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1225 return ret + sizeof(WCHAR);
1229 /**************************************************************************
1230 * RtlMultiByteToUnicodeSize (NTDLL.@)
1232 * Compute the size in bytes necessary for the Unicode conversion of str,
1233 * without the terminating '\0'.
1236 * size [O] Destination for size
1237 * str [I] String to calculate the size of
1238 * len [I] Length of str
1243 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1245 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1246 return STATUS_SUCCESS;
1250 /**************************************************************************
1251 * RtlUnicodeToMultiByteSize (NTDLL.@)
1253 * Calculate the size in bytes necessary for the multibyte conversion of str,
1254 * without the terminating '\0'.
1257 * size [O] Destination for size
1258 * str [I] String to calculate the size of
1259 * len [I] Length of str
1264 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1266 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1267 return STATUS_SUCCESS;
1271 /**************************************************************************
1272 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1273 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1275 * Calculate the size in bytes necessary for the Ansi conversion of str,
1276 * including the terminating '\0'.
1279 * str [I] String to calculate the size of
1282 * The calculated size.
1284 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1287 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1292 /**************************************************************************
1293 * RtlUnicodeStringToOemSize (NTDLL.@)
1294 * RtlxUnicodeStringToOemSize (NTDLL.@)
1296 * Calculate the size in bytes necessary for the OEM conversion of str,
1297 * including the terminating '\0'.
1300 * str [I] String to calculate the size of
1303 * The calculated size.
1305 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1307 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1308 NULL, 0, NULL, NULL ) + 1;
1312 /**************************************************************************
1313 * RtlAppendAsciizToString (NTDLL.@)
1315 * Concatenates a buffered character string and a '\0' terminated character
1319 * Success: STATUS_SUCCESS. src is appended to dest.
1320 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1321 * to hold the concatenated string.
1324 * if src is NULL dest is unchanged.
1325 * dest is never '\0' terminated.
1327 NTSTATUS WINAPI RtlAppendAsciizToString(
1328 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1329 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1332 unsigned int src_len = strlen(src);
1333 unsigned int dest_len = src_len + dest->Length;
1335 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1336 memcpy(dest->Buffer + dest->Length, src, src_len);
1337 dest->Length = dest_len;
1339 return STATUS_SUCCESS;
1343 /**************************************************************************
1344 * RtlAppendStringToString (NTDLL.@)
1346 * Concatenates two buffered character strings
1349 * Success: STATUS_SUCCESS. src is appended to dest.
1350 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1351 * to hold the concatenated string.
1354 * if src->length is zero dest is unchanged.
1355 * dest is never '\0' terminated.
1357 NTSTATUS WINAPI RtlAppendStringToString(
1358 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1359 const STRING *src) /* [I] Buffered character string to be concatenated */
1361 if (src->Length != 0) {
1362 unsigned int dest_len = src->Length + dest->Length;
1364 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1365 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1366 dest->Length = dest_len;
1368 return STATUS_SUCCESS;
1372 /**************************************************************************
1373 * RtlAppendUnicodeToString (NTDLL.@)
1375 * Concatenates a buffered unicode string and a '\0' terminated unicode
1379 * Success: STATUS_SUCCESS. src is appended to dest.
1380 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1381 * to hold the concatenated string.
1384 * if src is NULL dest is unchanged.
1385 * dest is '\0' terminated when the MaximumLength allows it.
1386 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1389 * Does not write in the src->Buffer beyond MaximumLength when
1390 * MaximumLength is odd as the native function does.
1392 NTSTATUS WINAPI RtlAppendUnicodeToString(
1393 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1394 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1397 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1398 unsigned int dest_len = src_len + dest->Length;
1400 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1401 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1402 dest->Length = dest_len;
1403 /* append terminating '\0' if enough space */
1404 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1405 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1408 return STATUS_SUCCESS;
1412 /**************************************************************************
1413 * RtlAppendUnicodeStringToString (NTDLL.@)
1415 * Concatenates two buffered unicode strings
1418 * Success: STATUS_SUCCESS. src is appended to dest.
1419 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1420 * to hold the concatenated string.
1423 * if src->length is zero dest is unchanged.
1424 * dest is '\0' terminated when the MaximumLength allows it.
1425 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1428 * Does not write in the src->Buffer beyond MaximumLength when
1429 * MaximumLength is odd as the native function does.
1431 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1432 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1433 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1435 if (src->Length != 0) {
1436 unsigned int dest_len = src->Length + dest->Length;
1438 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1439 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1440 dest->Length = dest_len;
1441 /* append terminating '\0' if enough space */
1442 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1443 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1446 return STATUS_SUCCESS;
1450 /**************************************************************************
1451 * RtlFindCharInUnicodeString (NTDLL.@)
1453 * Searches for one of several unicode characters in an unicode string.
1456 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1457 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1459 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1460 int flags, /* [I] Flags */
1461 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1462 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1463 USHORT *pos) /* [O] Position of the first character found + 2 */
1466 unsigned int search_idx;
1470 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1471 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1472 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1473 *pos = (main_idx + 1) * sizeof(WCHAR);
1474 return STATUS_SUCCESS;
1479 return STATUS_NOT_FOUND;
1481 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1482 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1483 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1484 *pos = main_idx * sizeof(WCHAR);
1485 return STATUS_SUCCESS;
1490 return STATUS_NOT_FOUND;
1492 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1494 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1495 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1498 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1499 *pos = (main_idx + 1) * sizeof(WCHAR);
1500 return STATUS_SUCCESS;
1504 return STATUS_NOT_FOUND;
1506 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1508 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1509 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1512 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1513 *pos = main_idx * sizeof(WCHAR);
1514 return STATUS_SUCCESS;
1518 return STATUS_NOT_FOUND;
1520 return STATUS_NOT_FOUND;
1528 /**************************************************************************
1529 * RtlIsTextUnicode (NTDLL.@)
1531 * Attempt to guess whether a text buffer is Unicode.
1534 * buf [I] Text buffer to test
1535 * len [I] Length of buf
1536 * pf [O] Destination for test results
1539 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1542 * Should implement more tests.
1544 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1546 const WCHAR *s = buf;
1548 unsigned int flags = ~0U, out_flags = 0;
1550 if (len < sizeof(WCHAR))
1552 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1559 * Apply various tests to the text string. According to the
1560 * docs, each test "passed" sets the corresponding flag in
1561 * the output flags. But some of the tests are mutually
1562 * exclusive, so I don't see how you could pass all tests ...
1565 /* Check for an odd length ... pass if even. */
1566 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1568 len /= sizeof(WCHAR);
1569 /* Windows only checks the first 256 characters */
1570 if (len > 256) len = 256;
1572 /* Check for the special byte order unicode marks. */
1573 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1574 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1576 /* apply some statistical analysis */
1577 if (flags & IS_TEXT_UNICODE_STATISTICS)
1580 /* FIXME: checks only for ASCII characters in the unicode stream */
1581 for (i = 0; i < len; i++)
1583 if (s[i] <= 255) stats++;
1585 if (stats > len / 2)
1586 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1589 /* Check for unicode NULL chars */
1590 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1592 for (i = 0; i < len; i++)
1596 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1607 /* check for flags that indicate it's definitely not valid Unicode */
1608 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1609 /* now check for invalid ASCII, and assume Unicode if so */
1610 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1611 /* now check for Unicode flags */
1612 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1618 /**************************************************************************
1619 * RtlCharToInteger (NTDLL.@)
1621 * Converts a character string into its integer equivalent.
1624 * Success: STATUS_SUCCESS. value contains the converted number
1625 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1626 * STATUS_ACCESS_VIOLATION, if value is NULL.
1629 * For base 0 it uses 10 as base and the string should be in the format
1630 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1631 * For other bases the string should be in the format
1632 * "{whitespace} [+|-] {digits}".
1633 * No check is made for value overflow, only the lower 32 bits are assigned.
1634 * If str is NULL it crashes, as the native function does.
1637 * This function does not read garbage behind '\0' as the native version does.
1639 NTSTATUS WINAPI RtlCharToInteger(
1640 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1641 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1642 ULONG *value) /* [O] Destination for the converted value */
1646 ULONG RunningTotal = 0;
1649 while (*str != '\0' && *str <= ' ') {
1655 } else if (*str == '-') {
1662 if (str[0] == '0') {
1663 if (str[1] == 'b') {
1666 } else if (str[1] == 'o') {
1669 } else if (str[1] == 'x') {
1674 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1675 return STATUS_INVALID_PARAMETER;
1678 if (value == NULL) {
1679 return STATUS_ACCESS_VIOLATION;
1682 while (*str != '\0') {
1684 if (chCurrent >= '0' && chCurrent <= '9') {
1685 digit = chCurrent - '0';
1686 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1687 digit = chCurrent - 'A' + 10;
1688 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1689 digit = chCurrent - 'a' + 10;
1693 if (digit < 0 || digit >= base) {
1694 *value = bMinus ? -RunningTotal : RunningTotal;
1695 return STATUS_SUCCESS;
1698 RunningTotal = RunningTotal * base + digit;
1702 *value = bMinus ? -RunningTotal : RunningTotal;
1703 return STATUS_SUCCESS;
1707 /**************************************************************************
1708 * RtlIntegerToChar (NTDLL.@)
1710 * Converts an unsigned integer to a character string.
1713 * Success: STATUS_SUCCESS. str contains the converted number
1714 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1715 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1716 * STATUS_ACCESS_VIOLATION, if str is NULL.
1719 * Instead of base 0 it uses 10 as base.
1720 * Writes at most length characters to the string str.
1721 * Str is '\0' terminated when length allows it.
1722 * When str fits exactly in length characters the '\0' is omitted.
1724 NTSTATUS WINAPI RtlIntegerToChar(
1725 ULONG value, /* [I] Value to be converted */
1726 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1727 ULONG length, /* [I] Length of the str buffer in bytes */
1728 PCHAR str) /* [O] Destination for the converted value */
1737 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1738 return STATUS_INVALID_PARAMETER;
1746 digit = value % base;
1747 value = value / base;
1751 *pos = 'A' + digit - 10;
1753 } while (value != 0L);
1755 len = &buffer[32] - pos;
1757 return STATUS_BUFFER_OVERFLOW;
1758 } else if (str == NULL) {
1759 return STATUS_ACCESS_VIOLATION;
1760 } else if (len == length) {
1761 memcpy(str, pos, len);
1763 memcpy(str, pos, len + 1);
1765 return STATUS_SUCCESS;
1769 /**************************************************************************
1770 * RtlUnicodeStringToInteger (NTDLL.@)
1772 * Converts an unicode string into its integer equivalent.
1775 * Success: STATUS_SUCCESS. value contains the converted number
1776 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1777 * STATUS_ACCESS_VIOLATION, if value is NULL.
1780 * For base 0 it uses 10 as base and the string should be in the format
1781 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1782 * For other bases the string should be in the format
1783 * "{whitespace} [+|-] {digits}".
1784 * No check is made for value overflow, only the lower 32 bits are assigned.
1785 * If str is NULL it crashes, as the native function does.
1788 * This function does not read garbage on string length 0 as the native
1791 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1792 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1793 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1794 ULONG *value) /* [O] Destination for the converted value */
1796 LPWSTR lpwstr = str->Buffer;
1797 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1800 ULONG RunningTotal = 0;
1803 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1808 if (CharsRemaining >= 1) {
1809 if (*lpwstr == '+') {
1812 } else if (*lpwstr == '-') {
1821 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1822 if (lpwstr[1] == 'b') {
1824 CharsRemaining -= 2;
1826 } else if (lpwstr[1] == 'o') {
1828 CharsRemaining -= 2;
1830 } else if (lpwstr[1] == 'x') {
1832 CharsRemaining -= 2;
1836 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1837 return STATUS_INVALID_PARAMETER;
1840 if (value == NULL) {
1841 return STATUS_ACCESS_VIOLATION;
1844 while (CharsRemaining >= 1) {
1845 wchCurrent = *lpwstr;
1846 if (wchCurrent >= '0' && wchCurrent <= '9') {
1847 digit = wchCurrent - '0';
1848 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1849 digit = wchCurrent - 'A' + 10;
1850 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1851 digit = wchCurrent - 'a' + 10;
1855 if (digit < 0 || digit >= base) {
1856 *value = bMinus ? -RunningTotal : RunningTotal;
1857 return STATUS_SUCCESS;
1860 RunningTotal = RunningTotal * base + digit;
1865 *value = bMinus ? -RunningTotal : RunningTotal;
1866 return STATUS_SUCCESS;
1870 /**************************************************************************
1871 * RtlIntegerToUnicodeString (NTDLL.@)
1873 * Converts an unsigned integer to a '\0' terminated unicode string.
1876 * Success: STATUS_SUCCESS. str contains the converted number
1877 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1878 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1879 * (with the '\0' termination). In this case str->Length
1880 * is set to the length, the string would have (which can
1881 * be larger than the MaximumLength).
1884 * Instead of base 0 it uses 10 as base.
1885 * If str is NULL it crashes, as the native function does.
1888 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1889 * The native function does this when the string would be longer than 16
1890 * characters even when the string parameter is long enough.
1892 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1893 ULONG value, /* [I] Value to be converted */
1894 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1895 UNICODE_STRING *str) /* [O] Destination for the converted value */
1903 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1904 return STATUS_INVALID_PARAMETER;
1912 digit = value % base;
1913 value = value / base;
1917 *pos = 'A' + digit - 10;
1919 } while (value != 0L);
1921 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1922 if (str->Length >= str->MaximumLength) {
1923 return STATUS_BUFFER_OVERFLOW;
1925 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1927 return STATUS_SUCCESS;
1931 /*************************************************************************
1932 * RtlGUIDFromString (NTDLL.@)
1934 * Convert a string representation of a GUID into a GUID.
1937 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1938 * guid [O] Destination for the converted GUID
1941 * Success: STATUS_SUCCESS. guid contains the converted value.
1942 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1945 * See RtlStringFromGUID.
1947 NTSTATUS WINAPI RtlGUIDFromString(const UNICODE_STRING *str, GUID* guid)
1950 const WCHAR *lpszCLSID = str->Buffer;
1951 BYTE* lpOut = (BYTE*)guid;
1953 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1955 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1956 * to memory: DWORD... WORD WORD BYTES............
1963 if (*lpszCLSID != '{')
1964 return STATUS_INVALID_PARAMETER;
1967 case 9: case 14: case 19: case 24:
1968 if (*lpszCLSID != '-')
1969 return STATUS_INVALID_PARAMETER;
1973 if (*lpszCLSID != '}')
1974 return STATUS_INVALID_PARAMETER;
1979 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1982 /* Read two hex digits as a byte value */
1983 if (ch >= '0' && ch <= '9') ch = ch - '0';
1984 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1985 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1986 else return STATUS_INVALID_PARAMETER;
1988 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1989 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1990 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1991 else return STATUS_INVALID_PARAMETER;
1993 byte = ch << 4 | ch2;
1997 #ifndef WORDS_BIGENDIAN
1998 /* For Big Endian machines, we store the data such that the
1999 * dword/word members can be read as DWORDS and WORDS correctly. */
2001 case 1: lpOut[3] = byte; break;
2002 case 3: lpOut[2] = byte; break;
2003 case 5: lpOut[1] = byte; break;
2004 case 7: lpOut[0] = byte; lpOut += 4; break;
2006 case 10: case 15: lpOut[1] = byte; break;
2007 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2010 default: lpOut[0] = byte; lpOut++; break;
2012 lpszCLSID++; /* Skip 2nd character of byte */
2020 return STATUS_SUCCESS;
2023 /*************************************************************************
2024 * RtlStringFromGUID (NTDLL.@)
2026 * Convert a GUID into a string representation of a GUID.
2029 * guid [I] GUID to convert
2030 * str [O] Destination for the converted string
2033 * Success: STATUS_SUCCESS. str contains the converted value.
2034 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2037 * See RtlGUIDFromString.
2039 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2041 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2042 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2043 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2044 '%','0','2','X','%','0','2','X','}','\0' };
2046 TRACE("(%p,%p)\n", guid, str);
2048 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2049 str->MaximumLength = str->Length + sizeof(WCHAR);
2050 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2053 str->Length = str->MaximumLength = 0;
2054 return STATUS_NO_MEMORY;
2056 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2057 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2058 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2060 return STATUS_SUCCESS;