Fixed a couple of races with exiting threads in suspend_for_ptrace().
[wine/gsoc_dplay.git] / dlls / ntdll / rtlstr.c
blobf8e1b1cad5b7bbb3814219b53e3a955969803ff0
1 /*
2 * Rtl string functions
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
23 #include "config.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "winternl.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "ntdll_misc.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
37 UINT NlsAnsiCodePage = 1252;
38 BYTE NlsMbCodePageTag = 0;
39 BYTE NlsMbOemCodePageTag = 0;
41 static const union cptable *ansi_table;
42 static const union cptable *oem_table;
44 inline static const union cptable *get_ansi_table(void)
46 if (!ansi_table) ansi_table = wine_cp_get_table( 1252 );
47 return ansi_table;
50 inline static const union cptable *get_oem_table(void)
52 if (!oem_table) oem_table = wine_cp_get_table( 437 );
53 return oem_table;
57 /**************************************************************************
58 * __wine_init_codepages (NTDLL.@)
60 * Set the code page once kernel32 is loaded. Should be done differently.
62 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem )
64 ansi_table = ansi;
65 oem_table = oem;
66 NlsAnsiCodePage = ansi->info.codepage;
70 /**************************************************************************
71 * RtlInitAnsiString (NTDLL.@)
73 void WINAPI RtlInitAnsiString( PSTRING target, LPCSTR source)
75 if ((target->Buffer = (LPSTR)source))
77 target->Length = strlen(source);
78 target->MaximumLength = target->Length + 1;
80 else target->Length = target->MaximumLength = 0;
84 /**************************************************************************
85 * RtlInitString (NTDLL.@)
87 void WINAPI RtlInitString( PSTRING target, LPCSTR source )
89 RtlInitAnsiString( target, source );
93 /**************************************************************************
94 * RtlFreeAnsiString (NTDLL.@)
96 void WINAPI RtlFreeAnsiString( PSTRING str )
98 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
102 /**************************************************************************
103 * RtlFreeOemString (NTDLL.@)
105 void WINAPI RtlFreeOemString( PSTRING str )
107 RtlFreeAnsiString( str );
111 /**************************************************************************
112 * RtlCopyString (NTDLL.@)
114 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
116 if (src)
118 unsigned int len = min( src->Length, dst->MaximumLength );
119 memcpy( dst->Buffer, src->Buffer, len );
120 dst->Length = len;
122 else dst->Length = 0;
126 /**************************************************************************
127 * RtlInitUnicodeString (NTDLL.@)
129 void WINAPI RtlInitUnicodeString( PUNICODE_STRING target, LPCWSTR source )
131 if ((target->Buffer = (LPWSTR)source))
133 target->Length = strlenW(source) * sizeof(WCHAR);
134 target->MaximumLength = target->Length + sizeof(WCHAR);
136 else target->Length = target->MaximumLength = 0;
140 /**************************************************************************
141 * RtlCreateUnicodeString (NTDLL.@)
143 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
145 int len = (strlenW(src) + 1) * sizeof(WCHAR);
146 if (!(target->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) return FALSE;
147 memcpy( target->Buffer, src, len );
148 target->MaximumLength = len;
149 target->Length = len - sizeof(WCHAR);
150 return TRUE;
154 /**************************************************************************
155 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
157 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
159 STRING ansi;
160 RtlInitAnsiString( &ansi, src );
161 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
165 /**************************************************************************
166 * RtlFreeUnicodeString (NTDLL.@)
168 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
170 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
174 /**************************************************************************
175 * RtlCopyUnicodeString (NTDLL.@)
177 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
179 if (src)
181 unsigned int len = min( src->Length, dst->MaximumLength );
182 memcpy( dst->Buffer, src->Buffer, len );
183 dst->Length = len;
184 /* append terminating NULL if enough space */
185 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
187 else dst->Length = 0;
191 /**************************************************************************
192 * RtlEraseUnicodeString (NTDLL.@)
194 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
196 if (str->Buffer)
198 memset( str->Buffer, 0, str->MaximumLength );
199 str->Length = 0;
205 COMPARISON FUNCTIONS
209 /******************************************************************************
210 * RtlCompareString (NTDLL.@)
212 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
214 unsigned int len;
215 LONG ret = 0;
216 LPCSTR p1, p2;
218 len = min(s1->Length, s2->Length);
219 p1 = s1->Buffer;
220 p2 = s2->Buffer;
222 if (CaseInsensitive)
224 while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++);
226 else
228 while (!ret && len--) ret = *p1++ - *p2++;
230 if (!ret) ret = s1->Length - s2->Length;
231 return ret;
235 /******************************************************************************
236 * RtlCompareUnicodeString (NTDLL.@)
238 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
239 BOOLEAN CaseInsensitive )
241 unsigned int len;
242 LONG ret = 0;
243 LPCWSTR p1, p2;
245 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
246 p1 = s1->Buffer;
247 p2 = s2->Buffer;
249 if (CaseInsensitive)
251 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
253 else
255 while (!ret && len--) ret = *p1++ - *p2++;
257 if (!ret) ret = s1->Length - s2->Length;
258 return ret;
262 /**************************************************************************
263 * RtlEqualString (NTDLL.@)
265 * Determine if two strings are equal.
267 * PARAMS
268 * s1 [I] Source string
269 * s2 [I] String to compare to s1
270 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
272 * RETURNS
273 * Non-zero if s1 is equal to s2, 0 otherwise.
275 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
277 if (s1->Length != s2->Length) return FALSE;
278 return !RtlCompareString( s1, s2, CaseInsensitive );
282 /**************************************************************************
283 * RtlEqualUnicodeString (NTDLL.@)
285 * Unicode version of RtlEqualString.
287 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
288 BOOLEAN CaseInsensitive )
290 if (s1->Length != s2->Length) return FALSE;
291 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
295 /**************************************************************************
296 * RtlPrefixString (NTDLL.@)
298 * Determine if one string is a prefix of another.
300 * PARAMS
301 * s1 [I] Prefix to look for in s2
302 * s2 [I] String that may contain s1 as a prefix
303 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
305 * RETURNS
306 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
308 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
310 unsigned int i;
312 if (s1->Length > s2->Length) return FALSE;
313 if (ignore_case)
315 for (i = 0; i < s1->Length; i++)
316 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
318 else
320 for (i = 0; i < s1->Length; i++)
321 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
323 return TRUE;
327 /**************************************************************************
328 * RtlPrefixUnicodeString (NTDLL.@)
330 * Unicode version of RtlPrefixString.
332 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
333 const UNICODE_STRING *s2,
334 BOOLEAN ignore_case )
336 unsigned int i;
338 if (s1->Length > s2->Length) return FALSE;
339 if (ignore_case)
341 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
342 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
344 else
346 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
347 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
349 return TRUE;
353 /**************************************************************************
354 * RtlEqualComputerName (NTDLL.@)
356 * Determine if two computer names are the same.
358 * PARAMS
359 * left [I] First computer name
360 * right [I] Second computer name
362 * RETURNS
363 * 0 if the names are equal, non-zero otherwise.
365 * NOTES
366 * The comparason is case insensitive.
368 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
369 const UNICODE_STRING *right)
371 NTSTATUS ret;
372 STRING upLeft, upRight;
374 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
376 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
378 ret = RtlEqualString( &upLeft, &upRight, FALSE );
379 RtlFreeOemString( &upRight );
381 RtlFreeOemString( &upLeft );
383 return ret;
387 /**************************************************************************
388 * RtlEqualDomainName (NTDLL.@)
390 * Determine if two domain names are the same.
392 * PARAMS
393 * left [I] First domain name
394 * right [I] Second domain name
396 * RETURNS
397 * 0 if the names are equal, non-zero otherwise.
399 * NOTES
400 * The comparason is case insensitive.
402 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
403 const UNICODE_STRING *right)
405 return RtlEqualComputerName(left, right);
410 COPY BETWEEN ANSI_STRING or UNICODE_STRING
411 there is no parameter checking, it just crashes
415 /**************************************************************************
416 * RtlAnsiStringToUnicodeString (NTDLL.@)
418 * NOTES
419 * This function always writes a terminating NUL.
421 NTSTATUS WINAPI RtlAnsiStringToUnicodeString( PUNICODE_STRING uni,
422 PCANSI_STRING ansi,
423 BOOLEAN doalloc )
425 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
427 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
428 uni->Length = total - sizeof(WCHAR);
429 if (doalloc)
431 uni->MaximumLength = total;
432 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
433 return STATUS_NO_MEMORY;
435 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
437 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
438 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
439 return STATUS_SUCCESS;
443 /**************************************************************************
444 * RtlOemStringToUnicodeString (NTDLL.@)
446 * NOTES
447 * This function always writes a terminating NUL.
448 * If the resulting length > 0xffff it returns STATUS_INVALID_PARAMETER_2
450 NTSTATUS WINAPI RtlOemStringToUnicodeString( UNICODE_STRING *uni,
451 const STRING *oem,
452 BOOLEAN doalloc )
454 DWORD total = RtlOemStringToUnicodeSize( oem );
456 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
457 uni->Length = total - sizeof(WCHAR);
458 if (doalloc)
460 uni->MaximumLength = total;
461 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
462 return STATUS_NO_MEMORY;
464 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
466 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
467 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
468 return STATUS_SUCCESS;
472 /**************************************************************************
473 * RtlUnicodeStringToAnsiString (NTDLL.@)
475 * Converts an Unicode string to an Ansi string.
477 * RETURNS
478 * Success: STATUS_SUCCESS. ansi contains the converted string
479 * Failure: STATUS_BUFFER_OVERFLOW if doalloc is FALSE and ansi is too small.
480 * STATUS_NO_MEMORY if doalloc is TRUE and the allocation fails.
482 * NOTES
483 * This function always writes a terminating '\0'.
484 * It performs a partial copy if ansi is too small.
486 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
487 STRING *ansi, /* [I/O] Destination for the Ansi string */
488 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
489 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
491 NTSTATUS ret = STATUS_SUCCESS;
492 DWORD len = RtlUnicodeStringToAnsiSize( uni );
494 ansi->Length = len - 1;
495 if (doalloc)
497 ansi->MaximumLength = len;
498 if (!(ansi->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
499 return STATUS_NO_MEMORY;
501 else if (ansi->MaximumLength < len)
503 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
504 ansi->Length = ansi->MaximumLength - 1;
505 ret = STATUS_BUFFER_OVERFLOW;
508 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
509 ansi->Buffer[ansi->Length] = 0;
510 return ret;
514 /**************************************************************************
515 * RtlUnicodeStringToOemString (NTDLL.@)
517 * Converts a Rtl Unicode string to an OEM string.
519 * PARAMS
520 * oem [O] Destination for OEM string
521 * uni [I] Source Unicode string
522 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
524 * RETURNS
525 * Success: STATUS_SUCCESS. oem contains the converted string
526 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
527 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
529 * NOTES
530 * If doalloc is TRUE, the length allocated is uni->Length + 1.
531 * This function always NUL terminates the string returned.
533 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
534 const UNICODE_STRING *uni,
535 BOOLEAN doalloc )
537 NTSTATUS ret = STATUS_SUCCESS;
538 DWORD len = RtlUnicodeStringToOemSize( uni );
540 oem->Length = len - 1;
541 if (doalloc)
543 oem->MaximumLength = len;
544 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
545 return STATUS_NO_MEMORY;
547 else if (oem->MaximumLength < len)
549 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
550 oem->Length = oem->MaximumLength - 1;
551 ret = STATUS_BUFFER_OVERFLOW;
554 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
555 oem->Buffer[oem->Length] = 0;
556 return ret;
560 /**************************************************************************
561 * RtlMultiByteToUnicodeN (NTDLL.@)
563 * NOTES
564 * Performs a partial copy if dst is too small.
566 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
567 LPCSTR src, DWORD srclen )
570 int ret = wine_cp_mbstowcs( get_ansi_table(), 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
571 if (reslen)
572 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
573 return STATUS_SUCCESS;
577 /**************************************************************************
578 * RtlOemToUnicodeN (NTDLL.@)
580 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
581 LPCSTR src, DWORD srclen )
583 int ret = wine_cp_mbstowcs( get_oem_table(), 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
584 if (reslen)
585 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
586 return STATUS_SUCCESS;
590 /**************************************************************************
591 * RtlUnicodeToMultiByteN (NTDLL.@)
593 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
594 LPCWSTR src, DWORD srclen )
596 int ret = wine_cp_wcstombs( get_ansi_table(), 0, src, srclen / sizeof(WCHAR),
597 dst, dstlen, NULL, NULL );
598 if (reslen)
599 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
600 return STATUS_SUCCESS;
604 /**************************************************************************
605 * RtlUnicodeToOemN (NTDLL.@)
607 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
608 LPCWSTR src, DWORD srclen )
610 int ret = wine_cp_wcstombs( get_oem_table(), 0, src, srclen / sizeof(WCHAR),
611 dst, dstlen, NULL, NULL );
612 if (reslen)
613 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
614 return STATUS_SUCCESS;
619 CASE CONVERSIONS
623 /**************************************************************************
624 * RtlUpperChar (NTDLL.@)
626 * Converts an Ascii character to uppercase.
628 * PARAMS
629 * ch [I] Character to convert
631 * RETURNS
632 * The uppercase character value.
634 * NOTES
635 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
636 * All other input characters are returned unchanged. The locale and
637 * multibyte characters are not taken into account (as native DLL).
639 CHAR WINAPI RtlUpperChar( CHAR ch )
641 if (ch >= 'a' && ch <= 'z') {
642 return ch - 'a' + 'A';
643 } else {
644 return ch;
645 } /* if */
649 /**************************************************************************
650 * RtlUpperString (NTDLL.@)
652 * Converts an Ascii string to uppercase.
654 * PARAMS
655 * dst [O] Destination for converted string
656 * src [I] Source string to convert
658 * RETURNS
659 * Nothing.
661 * NOTES
662 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
663 * All other src characters are copied unchanged to dst. The locale and
664 * multibyte characters are not taken into account (as native DLL).
665 * The number of character copied is the minimum of src->Length and
666 * the dst->MaximumLength.
668 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
670 unsigned int i, len = min(src->Length, dst->MaximumLength);
672 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
673 dst->Length = len;
677 /**************************************************************************
678 * RtlUpcaseUnicodeChar (NTDLL.@)
680 * Converts an Unicode character to uppercase.
682 * PARAMS
683 * wch [I] Character to convert
685 * RETURNS
686 * The uppercase character value.
688 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
690 return toupperW(wch);
694 /**************************************************************************
695 * RtlDowncaseUnicodeChar (NTDLL.@)
697 * Converts an Unicode character to lowercase.
699 * PARAMS
700 * wch [I] Character to convert
702 * RETURNS
703 * The lowercase character value.
705 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
707 return tolowerW(wch);
711 /**************************************************************************
712 * RtlUpcaseUnicodeString (NTDLL.@)
714 * Converts an Unicode string to uppercase.
716 * PARAMS
717 * dest [O] Destination for converted string
718 * src [I] Source string to convert
719 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
721 * RETURNS
722 * Success: STATUS_SUCCESS. dest contains the converted string.
723 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
724 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
726 * NOTES
727 * dest is never NUL terminated because it may be equal to src, and src
728 * might not be NUL terminated. dest->Length is only set upon success.
730 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
731 const UNICODE_STRING *src,
732 BOOLEAN doalloc )
734 DWORD i, len = src->Length;
736 if (doalloc)
738 dest->MaximumLength = len;
739 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
740 return STATUS_NO_MEMORY;
742 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
744 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
745 dest->Length = len;
746 return STATUS_SUCCESS;
750 /**************************************************************************
751 * RtlDowncaseUnicodeString (NTDLL.@)
753 * Converts an Unicode string to lowercase.
755 * PARAMS
756 * dest [O] Destination for converted string
757 * src [I] Source string to convert
758 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
760 * RETURNS
761 * Success: STATUS_SUCCESS. dest contains the converted string.
762 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
763 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
765 * NOTES
766 * dest is never NUL terminated because it may be equal to src, and src
767 * might not be NUL terminated. dest->Length is only set upon success.
769 NTSTATUS WINAPI RtlDowncaseUnicodeString(
770 UNICODE_STRING *dest,
771 const UNICODE_STRING *src,
772 BOOLEAN doalloc)
774 DWORD i;
775 DWORD len = src->Length;
777 if (doalloc) {
778 dest->MaximumLength = len;
779 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) {
780 return STATUS_NO_MEMORY;
781 } /* if */
782 } else if (len > dest->MaximumLength) {
783 return STATUS_BUFFER_OVERFLOW;
784 } /* if */
786 for (i = 0; i < len/sizeof(WCHAR); i++) {
787 dest->Buffer[i] = tolowerW(src->Buffer[i]);
788 } /* for */
789 dest->Length = len;
790 return STATUS_SUCCESS;
794 /**************************************************************************
795 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
797 * NOTES
798 * writes terminating 0
800 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
801 const UNICODE_STRING *src,
802 BOOLEAN doalloc )
804 NTSTATUS ret;
805 UNICODE_STRING upcase;
807 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
809 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
810 RtlFreeUnicodeString( &upcase );
812 return ret;
816 /**************************************************************************
817 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
819 * NOTES
820 * writes terminating 0
822 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
823 const UNICODE_STRING *src,
824 BOOLEAN doalloc )
826 NTSTATUS ret;
827 UNICODE_STRING upcase;
829 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
831 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
832 RtlFreeUnicodeString( &upcase );
834 return ret;
838 /**************************************************************************
839 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
841 * NOTES
842 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
844 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
845 const UNICODE_STRING *uni,
846 BOOLEAN doalloc )
848 NTSTATUS ret;
849 UNICODE_STRING upcase;
851 if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
853 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
854 oem->Length = len;
855 if (doalloc)
857 oem->MaximumLength = len;
858 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
860 ret = STATUS_NO_MEMORY;
861 goto done;
864 else if (oem->MaximumLength < len)
866 ret = STATUS_BUFFER_OVERFLOW;
867 oem->Length = oem->MaximumLength;
868 if (!oem->MaximumLength) goto done;
870 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
871 done:
872 RtlFreeUnicodeString( &upcase );
874 return ret;
878 /**************************************************************************
879 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
881 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
882 LPCWSTR src, DWORD srclen )
884 NTSTATUS ret;
885 LPWSTR upcase;
886 DWORD i;
888 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
889 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
890 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
891 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
892 return ret;
896 /**************************************************************************
897 * RtlUpcaseUnicodeToOemN (NTDLL.@)
899 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
900 LPCWSTR src, DWORD srclen )
902 NTSTATUS ret;
903 LPWSTR upcase;
904 DWORD i;
906 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
907 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
908 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
909 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
910 return ret;
915 STRING SIZE
919 /**************************************************************************
920 * RtlOemStringToUnicodeSize (NTDLL.@)
921 * RtlxOemStringToUnicodeSize (NTDLL.@)
923 * Calculate the size in bytes necessary for the Unicode conversion of str,
924 * including the terminating NUL.
926 * PARAMS
927 * str [I] String to calculate the size of
929 * RETURNS
930 * The calculated size.
932 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
934 int ret = wine_cp_mbstowcs( get_oem_table(), 0, str->Buffer, str->Length, NULL, 0 );
935 return (ret + 1) * sizeof(WCHAR);
939 /**************************************************************************
940 * RtlAnsiStringToUnicodeSize (NTDLL.@)
941 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
943 * Calculate the size in bytes necessary for the Unicode conversion of str,
944 * including the terminating NUL.
946 * PARAMS
947 * str [I] String to calculate the size of
949 * RETURNS
950 * The calculated size.
952 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
954 DWORD ret;
955 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
956 return ret + sizeof(WCHAR);
960 /**************************************************************************
961 * RtlMultiByteToUnicodeSize (NTDLL.@)
963 * Compute the size in bytes necessary for the Unicode conversion of str,
964 * without the terminating NUL.
966 * PARAMS
967 * size [O] Destination for size
968 * str [I] String to calculate the size of
969 * len [I] Length of str
971 * RETURNS
972 * STATUS_SUCCESS.
974 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
976 *size = wine_cp_mbstowcs( get_ansi_table(), 0, str, len, NULL, 0 ) * sizeof(WCHAR);
977 return STATUS_SUCCESS;
981 /**************************************************************************
982 * RtlUnicodeToMultiByteSize (NTDLL.@)
984 * Calculate the size in bytes necessary for the multibyte conversion of str,
985 * without the terminating NULL.
987 * PARAMS
988 * size [O] Destination for size
989 * str [I] String to calculate the size of
990 * len [I] Length of str
992 * RETURNS
993 * STATUS_SUCCESS.
995 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
997 *size = wine_cp_wcstombs( get_ansi_table(), 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
998 return STATUS_SUCCESS;
1002 /**************************************************************************
1003 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1004 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1006 * Calculate the size in bytes necessary for the Ansi conversion of str,
1007 * including the terminating NUL.
1009 * PARAMS
1010 * str [I] String to calculate the size of
1012 * RETURNS
1013 * The calculated size.
1015 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1017 DWORD ret;
1018 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1019 return ret + 1;
1023 /**************************************************************************
1024 * RtlUnicodeStringToOemSize (NTDLL.@)
1025 * RtlxUnicodeStringToOemSize (NTDLL.@)
1027 * Calculate the size in bytes necessary for the OEM conversion of str,
1028 * including the terminating NUL.
1030 * PARAMS
1031 * str [I] String to calculate the size of
1033 * RETURNS
1034 * The calculated size.
1036 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1038 return wine_cp_wcstombs( get_oem_table(), 0, str->Buffer, str->Length / sizeof(WCHAR),
1039 NULL, 0, NULL, NULL ) + 1;
1043 /**************************************************************************
1044 * RtlAppendAsciizToString (NTDLL.@)
1046 * Concatenates a buffered character string and a '\0' terminated character
1047 * string
1049 * RETURNS
1050 * Success: STATUS_SUCCESS. src is appended to dest.
1051 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1052 * to hold the concatenated string.
1054 * NOTES
1055 * if src is NULL dest is unchanged.
1056 * dest is never '\0' terminated.
1058 NTSTATUS WINAPI RtlAppendAsciizToString(
1059 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1060 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1062 if (src != NULL) {
1063 unsigned int src_len = strlen(src);
1064 unsigned int dest_len = src_len + dest->Length;
1066 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1067 memcpy(dest->Buffer + dest->Length, src, src_len);
1068 dest->Length = dest_len;
1069 } /* if */
1070 return STATUS_SUCCESS;
1074 /**************************************************************************
1075 * RtlAppendStringToString (NTDLL.@)
1077 * Concatenates two buffered character strings
1079 * RETURNS
1080 * Success: STATUS_SUCCESS. src is appended to dest.
1081 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1082 * to hold the concatenated string.
1084 * NOTES
1085 * if src->length is zero dest is unchanged.
1086 * dest is never '\0' terminated.
1088 NTSTATUS WINAPI RtlAppendStringToString(
1089 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1090 const STRING *src) /* [I] Buffered character string to be concatenated */
1092 if (src->Length != 0) {
1093 unsigned int dest_len = src->Length + dest->Length;
1095 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1096 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1097 dest->Length = dest_len;
1098 } /* if */
1099 return STATUS_SUCCESS;
1103 /**************************************************************************
1104 * RtlAppendUnicodeToString (NTDLL.@)
1106 * Concatenates an buffered unicode string and a '\0' terminated unicode
1107 * string
1109 * RETURNS
1110 * Success: STATUS_SUCCESS. src is appended to dest.
1111 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1112 * to hold the concatenated string.
1114 * NOTES
1115 * if src is NULL dest is unchanged.
1116 * dest is '\0' terminated when the MaximumLength allowes it.
1117 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1119 * DIFFERENCES
1120 * Does not write in the src->Buffer beyond MaximumLength when
1121 * MaximumLength is odd as the native function does.
1123 NTSTATUS WINAPI RtlAppendUnicodeToString(
1124 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1125 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1127 if (src != NULL) {
1128 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1129 unsigned int dest_len = src_len + dest->Length;
1131 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1132 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1133 dest->Length = dest_len;
1134 /* append terminating NULL if enough space */
1135 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1136 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1137 } /* if */
1138 } /* if */
1139 return STATUS_SUCCESS;
1143 /**************************************************************************
1144 * RtlAppendUnicodeStringToString (NTDLL.@)
1146 * Concatenates two buffered unicode strings
1148 * RETURNS
1149 * Success: STATUS_SUCCESS. src is appended to dest.
1150 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1151 * to hold the concatenated string.
1153 * NOTES
1154 * if src->length is zero dest is unchanged.
1155 * dest is '\0' terminated when the MaximumLength allowes it.
1156 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1158 * DIFFERENCES
1159 * Does not write in the src->Buffer beyond MaximumLength when
1160 * MaximumLength is odd as the native function does.
1162 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1163 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1164 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1166 if (src->Length != 0) {
1167 unsigned int dest_len = src->Length + dest->Length;
1169 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1170 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1171 dest->Length = dest_len;
1172 /* append terminating NULL if enough space */
1173 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1174 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1175 } /* if */
1176 } /* if */
1177 return STATUS_SUCCESS;
1182 MISC
1186 /**************************************************************************
1187 * RtlIsTextUnicode (NTDLL.@)
1189 * Attempt to guess whether a text buffer is Unicode.
1191 * PARAMS
1192 * buf [I] Text buffer to test
1193 * len [I] Length of buf
1194 * pf [O] Destination for test results
1196 * RETURNS
1197 * The length of the string if all tests were passed, 0 otherwise.
1199 * FIXME
1200 * Should implement more tests.
1202 DWORD WINAPI RtlIsTextUnicode(
1203 LPVOID buf,
1204 DWORD len,
1205 DWORD *pf)
1207 LPWSTR s = buf;
1208 DWORD flags = -1, out_flags = 0;
1210 if (!len)
1211 goto out;
1212 if (pf)
1213 flags = *pf;
1215 * Apply various tests to the text string. According to the
1216 * docs, each test "passed" sets the corresponding flag in
1217 * the output flags. But some of the tests are mutually
1218 * exclusive, so I don't see how you could pass all tests ...
1221 /* Check for an odd length ... pass if even. */
1222 if (!(len & 1))
1223 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1225 /* Check for the special unicode marker byte. */
1226 if (*s == 0xFEFF)
1227 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1230 * Check whether the string passed all of the tests.
1232 flags &= ITU_IMPLEMENTED_TESTS;
1233 if ((out_flags & flags) != flags)
1234 len = 0;
1235 out:
1236 if (pf)
1237 *pf = out_flags;
1238 return len;
1242 /**************************************************************************
1243 * RtlCharToInteger (NTDLL.@)
1245 * Converts a character string into its integer equivalent.
1247 * RETURNS
1248 * Success: STATUS_SUCCESS. value contains the converted number
1249 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1250 * STATUS_ACCESS_VIOLATION, if value is NULL.
1252 * NOTES
1253 * For base 0 it uses 10 as base and the string should be in the format
1254 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1255 * For other bases the string should be in the format
1256 * "{whitespace} [+|-] {digits}".
1257 * No check is made for value overflow, only the lower 32 bits are assigned.
1258 * If str is NULL it crashes, as the native function does.
1260 * DIFFERENCES
1261 * This function does not read garbage behind '\0' as the native version does.
1263 NTSTATUS WINAPI RtlCharToInteger(
1264 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1265 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1266 ULONG *value) /* [O] Destination for the converted value */
1268 CHAR chCurrent;
1269 int digit;
1270 ULONG RunningTotal = 0;
1271 char bMinus = 0;
1273 while (*str != '\0' && *str <= ' ') {
1274 str++;
1275 } /* while */
1277 if (*str == '+') {
1278 str++;
1279 } else if (*str == '-') {
1280 bMinus = 1;
1281 str++;
1282 } /* if */
1284 if (base == 0) {
1285 base = 10;
1286 if (str[0] == '0') {
1287 if (str[1] == 'b') {
1288 str += 2;
1289 base = 2;
1290 } else if (str[1] == 'o') {
1291 str += 2;
1292 base = 8;
1293 } else if (str[1] == 'x') {
1294 str += 2;
1295 base = 16;
1296 } /* if */
1297 } /* if */
1298 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1299 return STATUS_INVALID_PARAMETER;
1300 } /* if */
1302 if (value == NULL) {
1303 return STATUS_ACCESS_VIOLATION;
1304 } /* if */
1306 while (*str != '\0') {
1307 chCurrent = *str;
1308 if (chCurrent >= '0' && chCurrent <= '9') {
1309 digit = chCurrent - '0';
1310 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1311 digit = chCurrent - 'A' + 10;
1312 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1313 digit = chCurrent - 'a' + 10;
1314 } else {
1315 digit = -1;
1316 } /* if */
1317 if (digit < 0 || digit >= base) {
1318 *value = bMinus ? -RunningTotal : RunningTotal;
1319 return STATUS_SUCCESS;
1320 } /* if */
1322 RunningTotal = RunningTotal * base + digit;
1323 str++;
1324 } /* while */
1326 *value = bMinus ? -RunningTotal : RunningTotal;
1327 return STATUS_SUCCESS;
1331 /**************************************************************************
1332 * RtlIntegerToChar (NTDLL.@)
1334 * Converts an unsigned integer to a character string.
1336 * RETURNS
1337 * Success: STATUS_SUCCESS. str contains the converted number
1338 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1339 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1340 * STATUS_ACCESS_VIOLATION, if str is NULL.
1342 * NOTES
1343 * Instead of base 0 it uses 10 as base.
1344 * Writes at most length characters to the string str.
1345 * Str is '\0' terminated when length allowes it.
1346 * When str fits exactly in length characters the '\0' is ommitted.
1348 NTSTATUS WINAPI RtlIntegerToChar(
1349 ULONG value, /* [I] Value to be converted */
1350 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1351 ULONG length, /* [I] Length of the str buffer in bytes */
1352 PCHAR str) /* [O] Destination for the converted value */
1354 CHAR buffer[33];
1355 PCHAR pos;
1356 CHAR digit;
1357 ULONG len;
1359 if (base == 0) {
1360 base = 10;
1361 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1362 return STATUS_INVALID_PARAMETER;
1363 } /* if */
1365 pos = &buffer[32];
1366 *pos = '\0';
1368 do {
1369 pos--;
1370 digit = value % base;
1371 value = value / base;
1372 if (digit < 10) {
1373 *pos = '0' + digit;
1374 } else {
1375 *pos = 'A' + digit - 10;
1376 } /* if */
1377 } while (value != 0L);
1379 len = &buffer[32] - pos;
1380 if (len > length) {
1381 return STATUS_BUFFER_OVERFLOW;
1382 } else if (str == NULL) {
1383 return STATUS_ACCESS_VIOLATION;
1384 } else if (len == length) {
1385 memcpy(str, pos, len);
1386 } else {
1387 memcpy(str, pos, len + 1);
1388 } /* if */
1389 return STATUS_SUCCESS;
1393 /**************************************************************************
1394 * RtlUnicodeStringToInteger (NTDLL.@)
1396 * Converts an unicode string into its integer equivalent.
1398 * RETURNS
1399 * Success: STATUS_SUCCESS. value contains the converted number
1400 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1401 * STATUS_ACCESS_VIOLATION, if value is NULL.
1403 * NOTES
1404 * For base 0 it uses 10 as base and the string should be in the format
1405 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1406 * For other bases the string should be in the format
1407 * "{whitespace} [+|-] {digits}".
1408 * No check is made for value overflow, only the lower 32 bits are assigned.
1409 * If str is NULL it crashes, as the native function does.
1411 * DIFFERENCES
1412 * This function does not read garbage on string length 0 as the native
1413 * version does.
1415 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1416 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1417 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1418 ULONG *value) /* [O] Destination for the converted value */
1420 LPWSTR lpwstr = str->Buffer;
1421 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1422 WCHAR wchCurrent;
1423 int digit;
1424 ULONG RunningTotal = 0;
1425 char bMinus = 0;
1427 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1428 lpwstr++;
1429 CharsRemaining--;
1430 } /* while */
1432 if (CharsRemaining >= 1) {
1433 if (*lpwstr == '+') {
1434 lpwstr++;
1435 CharsRemaining--;
1436 } else if (*lpwstr == '-') {
1437 bMinus = 1;
1438 lpwstr++;
1439 CharsRemaining--;
1440 } /* if */
1441 } /* if */
1443 if (base == 0) {
1444 base = 10;
1445 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1446 if (lpwstr[1] == 'b') {
1447 lpwstr += 2;
1448 CharsRemaining -= 2;
1449 base = 2;
1450 } else if (lpwstr[1] == 'o') {
1451 lpwstr += 2;
1452 CharsRemaining -= 2;
1453 base = 8;
1454 } else if (lpwstr[1] == 'x') {
1455 lpwstr += 2;
1456 CharsRemaining -= 2;
1457 base = 16;
1458 } /* if */
1459 } /* if */
1460 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1461 return STATUS_INVALID_PARAMETER;
1462 } /* if */
1464 if (value == NULL) {
1465 return STATUS_ACCESS_VIOLATION;
1466 } /* if */
1468 while (CharsRemaining >= 1) {
1469 wchCurrent = *lpwstr;
1470 if (wchCurrent >= '0' && wchCurrent <= '9') {
1471 digit = wchCurrent - '0';
1472 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1473 digit = wchCurrent - 'A' + 10;
1474 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1475 digit = wchCurrent - 'a' + 10;
1476 } else {
1477 digit = -1;
1478 } /* if */
1479 if (digit < 0 || digit >= base) {
1480 *value = bMinus ? -RunningTotal : RunningTotal;
1481 return STATUS_SUCCESS;
1482 } /* if */
1484 RunningTotal = RunningTotal * base + digit;
1485 lpwstr++;
1486 CharsRemaining--;
1487 } /* while */
1489 *value = bMinus ? -RunningTotal : RunningTotal;
1490 return STATUS_SUCCESS;
1494 /**************************************************************************
1495 * RtlIntegerToUnicodeString (NTDLL.@)
1497 * Converts an unsigned integer to a '\0' terminated unicode string.
1499 * RETURNS
1500 * Success: STATUS_SUCCESS. str contains the converted number
1501 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1502 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1503 * (with the '\0' termination). In this case str->Length
1504 * is set to the length, the string would have (which can
1505 * be larger than the MaximumLength).
1507 * NOTES
1508 * Instead of base 0 it uses 10 as base.
1509 * If str is NULL it crashes, as the native function does.
1511 * DIFFERENCES
1512 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1513 * The native function does this when the string would be longer than 16
1514 * characters even when the string parameter is long enough.
1516 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1517 ULONG value, /* [I] Value to be converted */
1518 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1519 UNICODE_STRING *str) /* [O] Destination for the converted value */
1521 WCHAR buffer[33];
1522 PWCHAR pos;
1523 WCHAR digit;
1525 if (base == 0) {
1526 base = 10;
1527 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1528 return STATUS_INVALID_PARAMETER;
1529 } /* if */
1531 pos = &buffer[32];
1532 *pos = '\0';
1534 do {
1535 pos--;
1536 digit = value % base;
1537 value = value / base;
1538 if (digit < 10) {
1539 *pos = '0' + digit;
1540 } else {
1541 *pos = 'A' + digit - 10;
1542 } /* if */
1543 } while (value != 0L);
1545 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1546 if (str->Length >= str->MaximumLength) {
1547 return STATUS_BUFFER_OVERFLOW;
1548 } else {
1549 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1550 } /* if */
1551 return STATUS_SUCCESS;