4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright (C) 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 UINT NlsAnsiCodePage = 0;
42 BYTE NlsMbCodePageTag = 0;
43 BYTE NlsMbOemCodePageTag = 0;
45 static const union cptable *ansi_table;
46 static const union cptable *oem_table;
48 /**************************************************************************
49 * __wine_init_codepages (NTDLL.@)
51 * Set the code page once kernel32 is loaded. Should be done differently.
53 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem )
57 NlsAnsiCodePage = ansi->info.codepage;
61 /**************************************************************************
62 * RtlInitAnsiString (NTDLL.@)
64 * Initializes a buffered ansi string.
70 * Assigns source to target->Buffer. The length of source is assigned to
71 * target->Length and target->MaximumLength. If source is NULL the length
72 * of source is assumed to be 0.
74 void WINAPI RtlInitAnsiString(
75 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
76 PCSZ source) /* [I] '\0' terminated string used to initialize target */
78 if ((target->Buffer = (PCHAR) source))
80 target->Length = strlen(source);
81 target->MaximumLength = target->Length + 1;
83 else target->Length = target->MaximumLength = 0;
87 /**************************************************************************
88 * RtlInitString (NTDLL.@)
90 * Initializes a buffered string.
96 * Assigns source to target->Buffer. The length of source is assigned to
97 * target->Length and target->MaximumLength. If source is NULL the length
98 * of source is assumed to be 0.
100 void WINAPI RtlInitString(
101 PSTRING target, /* [I/O] Buffered string to be initialized */
102 PCSZ source) /* [I] '\0' terminated string used to initialize target */
104 RtlInitAnsiString( target, source );
108 /**************************************************************************
109 * RtlFreeAnsiString (NTDLL.@)
111 void WINAPI RtlFreeAnsiString( PSTRING str )
113 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
117 /**************************************************************************
118 * RtlFreeOemString (NTDLL.@)
120 void WINAPI RtlFreeOemString( PSTRING str )
122 RtlFreeAnsiString( str );
126 /**************************************************************************
127 * RtlCopyString (NTDLL.@)
129 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
133 unsigned int len = min( src->Length, dst->MaximumLength );
134 memcpy( dst->Buffer, src->Buffer, len );
137 else dst->Length = 0;
141 /**************************************************************************
142 * RtlInitUnicodeString (NTDLL.@)
144 * Initializes a buffered unicode 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 RtlInitUnicodeString(
155 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
156 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
158 if ((target->Buffer = (PWSTR) source))
160 target->Length = strlenW(source) * sizeof(WCHAR);
161 target->MaximumLength = target->Length + sizeof(WCHAR);
163 else target->Length = target->MaximumLength = 0;
167 /**************************************************************************
168 * RtlInitUnicodeStringEx (NTDLL.@)
170 * Initializes a buffered unicode string.
173 * Success: STATUS_SUCCESS. target is initialized.
174 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
177 * Assigns source to target->Buffer. The length of source is assigned to
178 * target->Length and target->MaximumLength. If source is NULL the length
179 * of source is assumed to be 0.
181 NTSTATUS WINAPI RtlInitUnicodeStringEx(
182 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
183 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
185 if (source != NULL) {
186 unsigned int len = strlenW(source) * sizeof(WCHAR);
189 return STATUS_NAME_TOO_LONG;
191 target->Length = len;
192 target->MaximumLength = len + sizeof(WCHAR);
193 target->Buffer = (PWSTR) source;
197 target->MaximumLength = 0;
198 target->Buffer = NULL;
200 return STATUS_SUCCESS;
204 /**************************************************************************
205 * RtlCreateUnicodeString (NTDLL.@)
207 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
209 int len = (strlenW(src) + 1) * sizeof(WCHAR);
210 if (!(target->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) return FALSE;
211 memcpy( target->Buffer, src, len );
212 target->MaximumLength = len;
213 target->Length = len - sizeof(WCHAR);
218 /**************************************************************************
219 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
221 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
224 RtlInitAnsiString( &ansi, src );
225 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
229 /**************************************************************************
230 * RtlFreeUnicodeString (NTDLL.@)
232 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
234 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
238 /**************************************************************************
239 * RtlCopyUnicodeString (NTDLL.@)
241 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
245 unsigned int len = min( src->Length, dst->MaximumLength );
246 memcpy( dst->Buffer, src->Buffer, len );
248 /* append terminating '\0' if enough space */
249 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
251 else dst->Length = 0;
255 /**************************************************************************
256 * RtlDuplicateUnicodeString (NTDLL.@)
258 * Duplicates an unicode string.
261 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
262 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
263 * STATUS_NO_MEMORY, if the allocation fails.
266 * For add_nul there are several possible values:
267 * 0 = destination will not be '\0' terminated,
268 * 1 = destination will be '\0' terminated,
269 * 3 = like 1 but for an empty source string produce '\0' terminated empty
270 * Buffer instead of assigning NULL to the Buffer.
271 * Other add_nul values are invalid.
273 NTSTATUS WINAPI RtlDuplicateUnicodeString(
274 int add_nul, /* [I] flag */
275 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
276 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
278 if (source == NULL || destination == NULL ||
279 source->Length > source->MaximumLength ||
280 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
281 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
282 return STATUS_INVALID_PARAMETER;
284 if (source->Length == 0 && add_nul != 3) {
285 destination->Length = 0;
286 destination->MaximumLength = 0;
287 destination->Buffer = NULL;
289 unsigned int destination_max_len = source->Length;
292 destination_max_len += sizeof(WCHAR);
294 destination->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len);
295 if (destination->Buffer == NULL) {
296 return STATUS_NO_MEMORY;
298 memcpy(destination->Buffer, source->Buffer, source->Length);
299 destination->Length = source->Length;
300 destination->MaximumLength = source->Length;
301 /* append terminating '\0' if enough space */
303 destination->MaximumLength = destination_max_len;
304 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
309 return STATUS_SUCCESS;
313 /**************************************************************************
314 * RtlEraseUnicodeString (NTDLL.@)
316 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
320 memset( str->Buffer, 0, str->MaximumLength );
331 /******************************************************************************
332 * RtlCompareString (NTDLL.@)
334 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
340 len = min(s1->Length, s2->Length);
346 while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++);
350 while (!ret && len--) ret = *p1++ - *p2++;
352 if (!ret) ret = s1->Length - s2->Length;
357 /******************************************************************************
358 * RtlCompareUnicodeString (NTDLL.@)
360 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
361 BOOLEAN CaseInsensitive )
367 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
373 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
377 while (!ret && len--) ret = *p1++ - *p2++;
379 if (!ret) ret = s1->Length - s2->Length;
384 /**************************************************************************
385 * RtlEqualString (NTDLL.@)
387 * Determine if two strings are equal.
390 * s1 [I] Source string
391 * s2 [I] String to compare to s1
392 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
395 * Non-zero if s1 is equal to s2, 0 otherwise.
397 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
399 if (s1->Length != s2->Length) return FALSE;
400 return !RtlCompareString( s1, s2, CaseInsensitive );
404 /**************************************************************************
405 * RtlEqualUnicodeString (NTDLL.@)
407 * Unicode version of RtlEqualString.
409 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
410 BOOLEAN CaseInsensitive )
412 if (s1->Length != s2->Length) return FALSE;
413 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
417 /**************************************************************************
418 * RtlPrefixString (NTDLL.@)
420 * Determine if one string is a prefix of another.
423 * s1 [I] Prefix to look for in s2
424 * s2 [I] String that may contain s1 as a prefix
425 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
428 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
430 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
434 if (s1->Length > s2->Length) return FALSE;
437 for (i = 0; i < s1->Length; i++)
438 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
442 for (i = 0; i < s1->Length; i++)
443 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
449 /**************************************************************************
450 * RtlPrefixUnicodeString (NTDLL.@)
452 * Unicode version of RtlPrefixString.
454 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
455 const UNICODE_STRING *s2,
456 BOOLEAN ignore_case )
460 if (s1->Length > s2->Length) return FALSE;
463 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
464 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
468 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
469 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
475 /**************************************************************************
476 * RtlEqualComputerName (NTDLL.@)
478 * Determine if two computer names are the same.
481 * left [I] First computer name
482 * right [I] Second computer name
485 * 0 if the names are equal, non-zero otherwise.
488 * The comparason is case insensitive.
490 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
491 const UNICODE_STRING *right)
494 STRING upLeft, upRight;
496 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
498 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
500 ret = RtlEqualString( &upLeft, &upRight, FALSE );
501 RtlFreeOemString( &upRight );
503 RtlFreeOemString( &upLeft );
509 /**************************************************************************
510 * RtlEqualDomainName (NTDLL.@)
512 * Determine if two domain names are the same.
515 * left [I] First domain name
516 * right [I] Second domain name
519 * 0 if the names are equal, non-zero otherwise.
522 * The comparason is case insensitive.
524 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
525 const UNICODE_STRING *right)
527 return RtlEqualComputerName(left, right);
532 COPY BETWEEN ANSI_STRING or UNICODE_STRING
533 there is no parameter checking, it just crashes
537 /**************************************************************************
538 * RtlAnsiStringToUnicodeString (NTDLL.@)
540 * Converts an ansi string to an unicode string.
543 * Success: STATUS_SUCCESS. uni contains the converted string
544 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
545 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
546 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
549 * This function always writes a terminating '\0'.
551 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
552 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
553 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
554 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
556 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
558 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
559 uni->Length = total - sizeof(WCHAR);
562 uni->MaximumLength = total;
563 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
564 return STATUS_NO_MEMORY;
566 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
568 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
569 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
570 return STATUS_SUCCESS;
574 /**************************************************************************
575 * RtlOemStringToUnicodeString (NTDLL.@)
577 * Converts an oem string to an unicode string.
580 * Success: STATUS_SUCCESS. uni contains the converted string
581 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
582 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
583 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
586 * This function always writes a terminating '\0'.
588 NTSTATUS WINAPI RtlOemStringToUnicodeString(
589 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
590 const STRING *oem, /* [I] Oem string to be converted */
591 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
593 DWORD total = RtlOemStringToUnicodeSize( oem );
595 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
596 uni->Length = total - sizeof(WCHAR);
599 uni->MaximumLength = total;
600 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
601 return STATUS_NO_MEMORY;
603 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
605 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
606 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
607 return STATUS_SUCCESS;
611 /**************************************************************************
612 * RtlUnicodeStringToAnsiString (NTDLL.@)
614 * Converts an unicode string to an ansi string.
617 * Success: STATUS_SUCCESS. ansi contains the converted string
618 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
619 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
622 * This function always writes a terminating '\0'.
623 * It performs a partial copy if ansi is too small.
625 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
626 STRING *ansi, /* [I/O] Destination for the ansi string */
627 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
628 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
630 NTSTATUS ret = STATUS_SUCCESS;
631 DWORD len = RtlUnicodeStringToAnsiSize( uni );
633 ansi->Length = len - 1;
636 ansi->MaximumLength = len;
637 if (!(ansi->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
638 return STATUS_NO_MEMORY;
640 else if (ansi->MaximumLength < len)
642 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
643 ansi->Length = ansi->MaximumLength - 1;
644 ret = STATUS_BUFFER_OVERFLOW;
647 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
648 ansi->Buffer[ansi->Length] = 0;
653 /**************************************************************************
654 * RtlUnicodeStringToOemString (NTDLL.@)
656 * Converts a Rtl Unicode string to an OEM string.
659 * oem [O] Destination for OEM string
660 * uni [I] Source Unicode string
661 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
664 * Success: STATUS_SUCCESS. oem contains the converted string
665 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
666 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
669 * If doalloc is TRUE, the length allocated is uni->Length + 1.
670 * This function always '\0' terminates the string returned.
672 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
673 const UNICODE_STRING *uni,
676 NTSTATUS ret = STATUS_SUCCESS;
677 DWORD len = RtlUnicodeStringToOemSize( uni );
679 oem->Length = len - 1;
682 oem->MaximumLength = len;
683 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
684 return STATUS_NO_MEMORY;
686 else if (oem->MaximumLength < len)
688 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
689 oem->Length = oem->MaximumLength - 1;
690 ret = STATUS_BUFFER_OVERFLOW;
693 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
694 oem->Buffer[oem->Length] = 0;
699 /**************************************************************************
700 * RtlMultiByteToUnicodeN (NTDLL.@)
703 * Performs a partial copy if dst is too small.
705 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
706 LPCSTR src, DWORD srclen )
709 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
711 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
712 return STATUS_SUCCESS;
716 /**************************************************************************
717 * RtlOemToUnicodeN (NTDLL.@)
719 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
720 LPCSTR src, DWORD srclen )
722 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
724 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
725 return STATUS_SUCCESS;
729 /**************************************************************************
730 * RtlUnicodeToMultiByteN (NTDLL.@)
732 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
733 LPCWSTR src, DWORD srclen )
735 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
736 dst, dstlen, NULL, NULL );
738 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
739 return STATUS_SUCCESS;
743 /**************************************************************************
744 * RtlUnicodeToOemN (NTDLL.@)
746 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
747 LPCWSTR src, DWORD srclen )
749 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
750 dst, dstlen, NULL, NULL );
752 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
753 return STATUS_SUCCESS;
762 /**************************************************************************
763 * RtlUpperChar (NTDLL.@)
765 * Converts an Ascii character to uppercase.
768 * ch [I] Character to convert
771 * The uppercase character value.
774 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
775 * All other input characters are returned unchanged. The locale and
776 * multibyte characters are not taken into account (as native DLL).
778 CHAR WINAPI RtlUpperChar( CHAR ch )
780 if (ch >= 'a' && ch <= 'z') {
781 return ch - 'a' + 'A';
788 /**************************************************************************
789 * RtlUpperString (NTDLL.@)
791 * Converts an Ascii string to uppercase.
794 * dst [O] Destination for converted string
795 * src [I] Source string to convert
801 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
802 * All other src characters are copied unchanged to dst. The locale and
803 * multibyte characters are not taken into account (as native DLL).
804 * The number of character copied is the minimum of src->Length and
805 * the dst->MaximumLength.
807 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
809 unsigned int i, len = min(src->Length, dst->MaximumLength);
811 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
816 /**************************************************************************
817 * RtlUpcaseUnicodeChar (NTDLL.@)
819 * Converts an Unicode character to uppercase.
822 * wch [I] Character to convert
825 * The uppercase character value.
827 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
829 return toupperW(wch);
833 /**************************************************************************
834 * RtlDowncaseUnicodeChar (NTDLL.@)
836 * Converts an Unicode character to lowercase.
839 * wch [I] Character to convert
842 * The lowercase character value.
844 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
846 return tolowerW(wch);
850 /**************************************************************************
851 * RtlUpcaseUnicodeString (NTDLL.@)
853 * Converts an Unicode string to uppercase.
856 * dest [O] Destination for converted string
857 * src [I] Source string to convert
858 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
861 * Success: STATUS_SUCCESS. dest contains the converted string.
862 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
863 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
866 * dest is never '\0' terminated because it may be equal to src, and src
867 * might not be '\0' terminated. dest->Length is only set upon success.
869 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
870 const UNICODE_STRING *src,
873 DWORD i, len = src->Length;
877 dest->MaximumLength = len;
878 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
879 return STATUS_NO_MEMORY;
881 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
883 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
885 return STATUS_SUCCESS;
889 /**************************************************************************
890 * RtlDowncaseUnicodeString (NTDLL.@)
892 * Converts an Unicode string to lowercase.
895 * dest [O] Destination for converted string
896 * src [I] Source string to convert
897 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
900 * Success: STATUS_SUCCESS. dest contains the converted string.
901 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
902 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
905 * dest is never '\0' terminated because it may be equal to src, and src
906 * might not be '\0' terminated. dest->Length is only set upon success.
908 NTSTATUS WINAPI RtlDowncaseUnicodeString(
909 UNICODE_STRING *dest,
910 const UNICODE_STRING *src,
914 DWORD len = src->Length;
917 dest->MaximumLength = len;
918 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) {
919 return STATUS_NO_MEMORY;
921 } else if (len > dest->MaximumLength) {
922 return STATUS_BUFFER_OVERFLOW;
925 for (i = 0; i < len/sizeof(WCHAR); i++) {
926 dest->Buffer[i] = tolowerW(src->Buffer[i]);
929 return STATUS_SUCCESS;
933 /**************************************************************************
934 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
937 * writes terminating 0
939 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
940 const UNICODE_STRING *src,
944 UNICODE_STRING upcase;
946 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
948 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
949 RtlFreeUnicodeString( &upcase );
955 /**************************************************************************
956 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
959 * writes terminating 0
961 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
962 const UNICODE_STRING *src,
966 UNICODE_STRING upcase;
968 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
970 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
971 RtlFreeUnicodeString( &upcase );
977 /**************************************************************************
978 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
981 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
983 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
984 const UNICODE_STRING *uni,
988 UNICODE_STRING upcase;
990 if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
992 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
996 oem->MaximumLength = len;
997 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
999 ret = STATUS_NO_MEMORY;
1003 else if (oem->MaximumLength < len)
1005 ret = STATUS_BUFFER_OVERFLOW;
1006 oem->Length = oem->MaximumLength;
1007 if (!oem->MaximumLength) goto done;
1009 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1011 RtlFreeUnicodeString( &upcase );
1017 /**************************************************************************
1018 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1020 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1021 LPCWSTR src, DWORD srclen )
1027 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1028 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1029 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1030 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1035 /**************************************************************************
1036 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1038 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1039 LPCWSTR src, DWORD srclen )
1045 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1046 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1047 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1048 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1058 /**************************************************************************
1059 * RtlOemStringToUnicodeSize (NTDLL.@)
1060 * RtlxOemStringToUnicodeSize (NTDLL.@)
1062 * Calculate the size in bytes necessary for the Unicode conversion of str,
1063 * including the terminating '\0'.
1066 * str [I] String to calculate the size of
1069 * The calculated size.
1071 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1073 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1074 return (ret + 1) * sizeof(WCHAR);
1078 /**************************************************************************
1079 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1080 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1082 * Calculate the size in bytes necessary for the Unicode conversion of str,
1083 * including the terminating '\0'.
1086 * str [I] String to calculate the size of
1089 * The calculated size.
1091 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1094 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1095 return ret + sizeof(WCHAR);
1099 /**************************************************************************
1100 * RtlMultiByteToUnicodeSize (NTDLL.@)
1102 * Compute the size in bytes necessary for the Unicode conversion of str,
1103 * without the terminating '\0'.
1106 * size [O] Destination for size
1107 * str [I] String to calculate the size of
1108 * len [I] Length of str
1113 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1115 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1116 return STATUS_SUCCESS;
1120 /**************************************************************************
1121 * RtlUnicodeToMultiByteSize (NTDLL.@)
1123 * Calculate the size in bytes necessary for the multibyte conversion of str,
1124 * without the terminating '\0'.
1127 * size [O] Destination for size
1128 * str [I] String to calculate the size of
1129 * len [I] Length of str
1134 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1136 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1137 return STATUS_SUCCESS;
1141 /**************************************************************************
1142 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1143 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1145 * Calculate the size in bytes necessary for the Ansi conversion of str,
1146 * including the terminating '\0'.
1149 * str [I] String to calculate the size of
1152 * The calculated size.
1154 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1157 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1162 /**************************************************************************
1163 * RtlUnicodeStringToOemSize (NTDLL.@)
1164 * RtlxUnicodeStringToOemSize (NTDLL.@)
1166 * Calculate the size in bytes necessary for the OEM conversion of str,
1167 * including the terminating '\0'.
1170 * str [I] String to calculate the size of
1173 * The calculated size.
1175 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1177 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1178 NULL, 0, NULL, NULL ) + 1;
1182 /**************************************************************************
1183 * RtlAppendAsciizToString (NTDLL.@)
1185 * Concatenates a buffered character string and a '\0' terminated character
1189 * Success: STATUS_SUCCESS. src is appended to dest.
1190 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1191 * to hold the concatenated string.
1194 * if src is NULL dest is unchanged.
1195 * dest is never '\0' terminated.
1197 NTSTATUS WINAPI RtlAppendAsciizToString(
1198 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1199 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1202 unsigned int src_len = strlen(src);
1203 unsigned int dest_len = src_len + dest->Length;
1205 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1206 memcpy(dest->Buffer + dest->Length, src, src_len);
1207 dest->Length = dest_len;
1209 return STATUS_SUCCESS;
1213 /**************************************************************************
1214 * RtlAppendStringToString (NTDLL.@)
1216 * Concatenates two buffered character strings
1219 * Success: STATUS_SUCCESS. src is appended to dest.
1220 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1221 * to hold the concatenated string.
1224 * if src->length is zero dest is unchanged.
1225 * dest is never '\0' terminated.
1227 NTSTATUS WINAPI RtlAppendStringToString(
1228 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1229 const STRING *src) /* [I] Buffered character string to be concatenated */
1231 if (src->Length != 0) {
1232 unsigned int dest_len = src->Length + dest->Length;
1234 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1235 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1236 dest->Length = dest_len;
1238 return STATUS_SUCCESS;
1242 /**************************************************************************
1243 * RtlAppendUnicodeToString (NTDLL.@)
1245 * Concatenates an buffered unicode string and a '\0' terminated unicode
1249 * Success: STATUS_SUCCESS. src is appended to dest.
1250 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1251 * to hold the concatenated string.
1254 * if src is NULL dest is unchanged.
1255 * dest is '\0' terminated when the MaximumLength allowes it.
1256 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1259 * Does not write in the src->Buffer beyond MaximumLength when
1260 * MaximumLength is odd as the native function does.
1262 NTSTATUS WINAPI RtlAppendUnicodeToString(
1263 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1264 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1267 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1268 unsigned int dest_len = src_len + dest->Length;
1270 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1271 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1272 dest->Length = dest_len;
1273 /* append terminating '\0' if enough space */
1274 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1275 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1278 return STATUS_SUCCESS;
1282 /**************************************************************************
1283 * RtlAppendUnicodeStringToString (NTDLL.@)
1285 * Concatenates two buffered unicode strings
1288 * Success: STATUS_SUCCESS. src is appended to dest.
1289 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1290 * to hold the concatenated string.
1293 * if src->length is zero dest is unchanged.
1294 * dest is '\0' terminated when the MaximumLength allowes it.
1295 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1298 * Does not write in the src->Buffer beyond MaximumLength when
1299 * MaximumLength is odd as the native function does.
1301 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1302 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1303 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1305 if (src->Length != 0) {
1306 unsigned int dest_len = src->Length + dest->Length;
1308 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1309 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1310 dest->Length = dest_len;
1311 /* append terminating '\0' if enough space */
1312 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1313 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1316 return STATUS_SUCCESS;
1320 /**************************************************************************
1321 * RtlFindCharInUnicodeString (NTDLL.@)
1323 * Searches for one of several unicode characters in an unicode string.
1326 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1327 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1329 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1330 int flags, /* [I] Flags */
1331 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1332 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1333 USHORT *pos) /* [O] Position of the first character found + 2 */
1336 unsigned int search_idx;
1340 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1341 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1342 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1343 *pos = (main_idx + 1) * sizeof(WCHAR);
1344 return STATUS_SUCCESS;
1349 return STATUS_NOT_FOUND;
1351 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1352 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1353 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1354 *pos = main_idx * sizeof(WCHAR);
1355 return STATUS_SUCCESS;
1360 return STATUS_NOT_FOUND;
1362 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1364 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1365 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1368 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1369 *pos = (main_idx + 1) * sizeof(WCHAR);
1370 return STATUS_SUCCESS;
1374 return STATUS_NOT_FOUND;
1376 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1378 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1379 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1382 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1383 *pos = main_idx * sizeof(WCHAR);
1384 return STATUS_SUCCESS;
1388 return STATUS_NOT_FOUND;
1390 return STATUS_NOT_FOUND;
1397 /* Tests that we currently implement */
1398 #define ITU_IMPLEMENTED_TESTS \
1399 (IS_TEXT_UNICODE_SIGNATURE | \
1400 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1401 IS_TEXT_UNICODE_ODD_LENGTH | \
1402 IS_TEXT_UNICODE_STATISTICS | \
1403 IS_TEXT_UNICODE_NULL_BYTES)
1405 /**************************************************************************
1406 * RtlIsTextUnicode (NTDLL.@)
1408 * Attempt to guess whether a text buffer is Unicode.
1411 * buf [I] Text buffer to test
1412 * len [I] Length of buf
1413 * pf [O] Destination for test results
1416 * The length of the string if all tests were passed, 0 otherwise.
1419 * Should implement more tests.
1421 DWORD WINAPI RtlIsTextUnicode(
1427 DWORD flags = -1, out_flags = 0;
1434 * Apply various tests to the text string. According to the
1435 * docs, each test "passed" sets the corresponding flag in
1436 * the output flags. But some of the tests are mutually
1437 * exclusive, so I don't see how you could pass all tests ...
1440 /* Check for an odd length ... pass if even. */
1441 if ((flags & IS_TEXT_UNICODE_ODD_LENGTH) && (len & 1))
1442 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1444 /* Check for the special byte order unicode marks. */
1445 if ((flags & IS_TEXT_UNICODE_SIGNATURE) && *s == 0xFEFF)
1446 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1448 if ((flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) && *s == 0xFFFE)
1449 out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1451 /* apply some statistical analysis */
1452 if (flags & IS_TEXT_UNICODE_STATISTICS)
1455 /* FIXME: checks only for ASCII characters in the unicode stream */
1456 for (i = 0; i < len / sizeof(WCHAR); i++)
1458 if (s[i] <= 255) stats++;
1460 if (stats > len / sizeof(WCHAR) / 2)
1461 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1464 /* Check for unicode NULL chars */
1465 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1468 for (i = 0; i < len / sizeof(WCHAR); i++)
1472 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1479 * Check whether the string passed all of the tests.
1481 flags &= ITU_IMPLEMENTED_TESTS;
1482 if ((out_flags & flags) != flags)
1491 /**************************************************************************
1492 * RtlCharToInteger (NTDLL.@)
1494 * Converts a character string into its integer equivalent.
1497 * Success: STATUS_SUCCESS. value contains the converted number
1498 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1499 * STATUS_ACCESS_VIOLATION, if value is NULL.
1502 * For base 0 it uses 10 as base and the string should be in the format
1503 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1504 * For other bases the string should be in the format
1505 * "{whitespace} [+|-] {digits}".
1506 * No check is made for value overflow, only the lower 32 bits are assigned.
1507 * If str is NULL it crashes, as the native function does.
1510 * This function does not read garbage behind '\0' as the native version does.
1512 NTSTATUS WINAPI RtlCharToInteger(
1513 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1514 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1515 ULONG *value) /* [O] Destination for the converted value */
1519 ULONG RunningTotal = 0;
1522 while (*str != '\0' && *str <= ' ') {
1528 } else if (*str == '-') {
1535 if (str[0] == '0') {
1536 if (str[1] == 'b') {
1539 } else if (str[1] == 'o') {
1542 } else if (str[1] == 'x') {
1547 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1548 return STATUS_INVALID_PARAMETER;
1551 if (value == NULL) {
1552 return STATUS_ACCESS_VIOLATION;
1555 while (*str != '\0') {
1557 if (chCurrent >= '0' && chCurrent <= '9') {
1558 digit = chCurrent - '0';
1559 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1560 digit = chCurrent - 'A' + 10;
1561 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1562 digit = chCurrent - 'a' + 10;
1566 if (digit < 0 || digit >= base) {
1567 *value = bMinus ? -RunningTotal : RunningTotal;
1568 return STATUS_SUCCESS;
1571 RunningTotal = RunningTotal * base + digit;
1575 *value = bMinus ? -RunningTotal : RunningTotal;
1576 return STATUS_SUCCESS;
1580 /**************************************************************************
1581 * RtlIntegerToChar (NTDLL.@)
1583 * Converts an unsigned integer to a character string.
1586 * Success: STATUS_SUCCESS. str contains the converted number
1587 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1588 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1589 * STATUS_ACCESS_VIOLATION, if str is NULL.
1592 * Instead of base 0 it uses 10 as base.
1593 * Writes at most length characters to the string str.
1594 * Str is '\0' terminated when length allowes it.
1595 * When str fits exactly in length characters the '\0' is ommitted.
1597 NTSTATUS WINAPI RtlIntegerToChar(
1598 ULONG value, /* [I] Value to be converted */
1599 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1600 ULONG length, /* [I] Length of the str buffer in bytes */
1601 PCHAR str) /* [O] Destination for the converted value */
1610 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1611 return STATUS_INVALID_PARAMETER;
1619 digit = value % base;
1620 value = value / base;
1624 *pos = 'A' + digit - 10;
1626 } while (value != 0L);
1628 len = &buffer[32] - pos;
1630 return STATUS_BUFFER_OVERFLOW;
1631 } else if (str == NULL) {
1632 return STATUS_ACCESS_VIOLATION;
1633 } else if (len == length) {
1634 memcpy(str, pos, len);
1636 memcpy(str, pos, len + 1);
1638 return STATUS_SUCCESS;
1642 /**************************************************************************
1643 * RtlUnicodeStringToInteger (NTDLL.@)
1645 * Converts an unicode string into its integer equivalent.
1648 * Success: STATUS_SUCCESS. value contains the converted number
1649 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1650 * STATUS_ACCESS_VIOLATION, if value is NULL.
1653 * For base 0 it uses 10 as base and the string should be in the format
1654 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1655 * For other bases the string should be in the format
1656 * "{whitespace} [+|-] {digits}".
1657 * No check is made for value overflow, only the lower 32 bits are assigned.
1658 * If str is NULL it crashes, as the native function does.
1661 * This function does not read garbage on string length 0 as the native
1664 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1665 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1666 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1667 ULONG *value) /* [O] Destination for the converted value */
1669 LPWSTR lpwstr = str->Buffer;
1670 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1673 ULONG RunningTotal = 0;
1676 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1681 if (CharsRemaining >= 1) {
1682 if (*lpwstr == '+') {
1685 } else if (*lpwstr == '-') {
1694 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1695 if (lpwstr[1] == 'b') {
1697 CharsRemaining -= 2;
1699 } else if (lpwstr[1] == 'o') {
1701 CharsRemaining -= 2;
1703 } else if (lpwstr[1] == 'x') {
1705 CharsRemaining -= 2;
1709 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1710 return STATUS_INVALID_PARAMETER;
1713 if (value == NULL) {
1714 return STATUS_ACCESS_VIOLATION;
1717 while (CharsRemaining >= 1) {
1718 wchCurrent = *lpwstr;
1719 if (wchCurrent >= '0' && wchCurrent <= '9') {
1720 digit = wchCurrent - '0';
1721 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1722 digit = wchCurrent - 'A' + 10;
1723 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1724 digit = wchCurrent - 'a' + 10;
1728 if (digit < 0 || digit >= base) {
1729 *value = bMinus ? -RunningTotal : RunningTotal;
1730 return STATUS_SUCCESS;
1733 RunningTotal = RunningTotal * base + digit;
1738 *value = bMinus ? -RunningTotal : RunningTotal;
1739 return STATUS_SUCCESS;
1743 /**************************************************************************
1744 * RtlIntegerToUnicodeString (NTDLL.@)
1746 * Converts an unsigned integer to a '\0' terminated unicode string.
1749 * Success: STATUS_SUCCESS. str contains the converted number
1750 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1751 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1752 * (with the '\0' termination). In this case str->Length
1753 * is set to the length, the string would have (which can
1754 * be larger than the MaximumLength).
1757 * Instead of base 0 it uses 10 as base.
1758 * If str is NULL it crashes, as the native function does.
1761 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1762 * The native function does this when the string would be longer than 16
1763 * characters even when the string parameter is long enough.
1765 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1766 ULONG value, /* [I] Value to be converted */
1767 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1768 UNICODE_STRING *str) /* [O] Destination for the converted value */
1776 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1777 return STATUS_INVALID_PARAMETER;
1785 digit = value % base;
1786 value = value / base;
1790 *pos = 'A' + digit - 10;
1792 } while (value != 0L);
1794 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1795 if (str->Length >= str->MaximumLength) {
1796 return STATUS_BUFFER_OVERFLOW;
1798 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1800 return STATUS_SUCCESS;