Release 20030408.
[wine/gsoc-2012-control.git] / dlls / ntdll / rtlstr.c
blob88d7e042625181b183c276cb8e5f70dd8f16d504
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 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
841 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
842 LPCWSTR src, DWORD srclen )
844 NTSTATUS ret;
845 LPWSTR upcase;
846 DWORD i;
848 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
849 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
850 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
851 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
852 return ret;
856 /**************************************************************************
857 * RtlUpcaseUnicodeToOemN (NTDLL.@)
859 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
860 LPCWSTR src, DWORD srclen )
862 NTSTATUS ret;
863 LPWSTR upcase;
864 DWORD i;
866 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
867 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
868 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
869 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
870 return ret;
875 STRING SIZE
879 /**************************************************************************
880 * RtlOemStringToUnicodeSize (NTDLL.@)
881 * RtlxOemStringToUnicodeSize (NTDLL.@)
883 * Calculate the size in bytes necessary for the Unicode conversion of str,
884 * including the terminating NUL.
886 * PARAMS
887 * str [I] String to calculate the size of
889 * RETURNS
890 * The calculated size.
892 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
894 int ret = wine_cp_mbstowcs( get_oem_table(), 0, str->Buffer, str->Length, NULL, 0 );
895 return (ret + 1) * sizeof(WCHAR);
899 /**************************************************************************
900 * RtlAnsiStringToUnicodeSize (NTDLL.@)
901 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
903 * Calculate the size in bytes necessary for the Unicode conversion of str,
904 * including the terminating NUL.
906 * PARAMS
907 * str [I] String to calculate the size of
909 * RETURNS
910 * The calculated size.
912 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
914 DWORD ret;
915 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
916 return ret + sizeof(WCHAR);
920 /**************************************************************************
921 * RtlMultiByteToUnicodeSize (NTDLL.@)
923 * Compute the size in bytes necessary for the Unicode conversion of str,
924 * without the terminating NUL.
926 * PARAMS
927 * size [O] Destination for size
928 * str [I] String to calculate the size of
929 * len [I] Length of str
931 * RETURNS
932 * STATUS_SUCCESS.
934 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
936 *size = wine_cp_mbstowcs( get_ansi_table(), 0, str, len, NULL, 0 ) * sizeof(WCHAR);
937 return STATUS_SUCCESS;
941 /**************************************************************************
942 * RtlUnicodeToMultiByteSize (NTDLL.@)
944 * Calculate the size in bytes necessary for the multibyte conversion of str,
945 * without the terminating NULL.
947 * PARAMS
948 * size [O] Destination for size
949 * str [I] String to calculate the size of
950 * len [I] Length of str
952 * RETURNS
953 * STATUS_SUCCESS.
955 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
957 *size = wine_cp_wcstombs( get_ansi_table(), 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
958 return STATUS_SUCCESS;
962 /**************************************************************************
963 * RtlUnicodeStringToAnsiSize (NTDLL.@)
964 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
966 * Calculate the size in bytes necessary for the Ansi conversion of str,
967 * including the terminating NUL.
969 * PARAMS
970 * str [I] String to calculate the size of
972 * RETURNS
973 * The calculated size.
975 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
977 DWORD ret;
978 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
979 return ret + 1;
983 /**************************************************************************
984 * RtlUnicodeStringToOemSize (NTDLL.@)
985 * RtlxUnicodeStringToOemSize (NTDLL.@)
987 * Calculate the size in bytes necessary for the OEM conversion of str,
988 * including the terminating NUL.
990 * PARAMS
991 * str [I] String to calculate the size of
993 * RETURNS
994 * The calculated size.
996 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
998 return wine_cp_wcstombs( get_oem_table(), 0, str->Buffer, str->Length / sizeof(WCHAR),
999 NULL, 0, NULL, NULL ) + 1;
1003 /**************************************************************************
1004 * RtlAppendAsciizToString (NTDLL.@)
1006 * Concatenates a buffered character string and a '\0' terminated character
1007 * string
1009 * RETURNS
1010 * Success: STATUS_SUCCESS. src is appended to dest.
1011 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1012 * to hold the concatenated string.
1014 * NOTES
1015 * if src is NULL dest is unchanged.
1016 * dest is never '\0' terminated.
1018 NTSTATUS WINAPI RtlAppendAsciizToString(
1019 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1020 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1022 if (src != NULL) {
1023 unsigned int src_len = strlen(src);
1024 unsigned int dest_len = src_len + dest->Length;
1026 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1027 memcpy(dest->Buffer + dest->Length, src, src_len);
1028 dest->Length = dest_len;
1029 } /* if */
1030 return STATUS_SUCCESS;
1034 /**************************************************************************
1035 * RtlAppendStringToString (NTDLL.@)
1037 * Concatenates two buffered character strings
1039 * RETURNS
1040 * Success: STATUS_SUCCESS. src is appended to dest.
1041 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1042 * to hold the concatenated string.
1044 * NOTES
1045 * if src->length is zero dest is unchanged.
1046 * dest is never '\0' terminated.
1048 NTSTATUS WINAPI RtlAppendStringToString(
1049 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1050 const STRING *src) /* [I] Buffered character string to be concatenated */
1052 if (src->Length != 0) {
1053 unsigned int dest_len = src->Length + dest->Length;
1055 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1056 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1057 dest->Length = dest_len;
1058 } /* if */
1059 return STATUS_SUCCESS;
1063 /**************************************************************************
1064 * RtlAppendUnicodeToString (NTDLL.@)
1066 * Concatenates an buffered unicode string and a '\0' terminated unicode
1067 * string
1069 * RETURNS
1070 * Success: STATUS_SUCCESS. src is appended to dest.
1071 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1072 * to hold the concatenated string.
1074 * NOTES
1075 * if src is NULL dest is unchanged.
1076 * dest is '\0' terminated when the MaximumLength allowes it.
1077 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1079 * DIFFERENCES
1080 * Does not write in the src->Buffer beyond MaximumLength when
1081 * MaximumLength is odd as the native function does.
1083 NTSTATUS WINAPI RtlAppendUnicodeToString(
1084 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1085 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1087 if (src != NULL) {
1088 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1089 unsigned int dest_len = src_len + dest->Length;
1091 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1092 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1093 dest->Length = dest_len;
1094 /* append terminating NULL if enough space */
1095 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1096 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1097 } /* if */
1098 } /* if */
1099 return STATUS_SUCCESS;
1103 /**************************************************************************
1104 * RtlAppendUnicodeStringToString (NTDLL.@)
1106 * Concatenates two buffered unicode strings
1108 * RETURNS
1109 * Success: STATUS_SUCCESS. src is appended to dest.
1110 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1111 * to hold the concatenated string.
1113 * NOTES
1114 * if src->length is zero dest is unchanged.
1115 * dest is '\0' terminated when the MaximumLength allowes it.
1116 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1118 * DIFFERENCES
1119 * Does not write in the src->Buffer beyond MaximumLength when
1120 * MaximumLength is odd as the native function does.
1122 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1123 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1124 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1126 if (src->Length != 0) {
1127 unsigned int dest_len = src->Length + dest->Length;
1129 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1130 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1131 dest->Length = dest_len;
1132 /* append terminating NULL if enough space */
1133 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1134 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1135 } /* if */
1136 } /* if */
1137 return STATUS_SUCCESS;
1142 MISC
1146 /**************************************************************************
1147 * RtlIsTextUnicode (NTDLL.@)
1149 * Attempt to guess whether a text buffer is Unicode.
1151 * PARAMS
1152 * buf [I] Text buffer to test
1153 * len [I] Length of buf
1154 * pf [O] Destination for test results
1156 * RETURNS
1157 * The length of the string if all tests were passed, 0 otherwise.
1159 * FIXME
1160 * Should implement more tests.
1162 DWORD WINAPI RtlIsTextUnicode(
1163 LPVOID buf,
1164 DWORD len,
1165 DWORD *pf)
1167 LPWSTR s = buf;
1168 DWORD flags = -1, out_flags = 0;
1170 if (!len)
1171 goto out;
1172 if (pf)
1173 flags = *pf;
1175 * Apply various tests to the text string. According to the
1176 * docs, each test "passed" sets the corresponding flag in
1177 * the output flags. But some of the tests are mutually
1178 * exclusive, so I don't see how you could pass all tests ...
1181 /* Check for an odd length ... pass if even. */
1182 if (!(len & 1))
1183 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1185 /* Check for the special unicode marker byte. */
1186 if (*s == 0xFEFF)
1187 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1190 * Check whether the string passed all of the tests.
1192 flags &= ITU_IMPLEMENTED_TESTS;
1193 if ((out_flags & flags) != flags)
1194 len = 0;
1195 out:
1196 if (pf)
1197 *pf = out_flags;
1198 return len;
1202 /**************************************************************************
1203 * RtlCharToInteger (NTDLL.@)
1205 * Converts a character string into its integer equivalent.
1207 * RETURNS
1208 * Success: STATUS_SUCCESS. value contains the converted number
1209 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1210 * STATUS_ACCESS_VIOLATION, if value is NULL.
1212 * NOTES
1213 * For base 0 it uses 10 as base and the string should be in the format
1214 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1215 * For other bases the string should be in the format
1216 * "{whitespace} [+|-] {digits}".
1217 * No check is made for value overflow, only the lower 32 bits are assigned.
1218 * If str is NULL it crashes, as the native function does.
1220 * DIFFERENCES
1221 * This function does not read garbage behind '\0' as the native version does.
1223 NTSTATUS WINAPI RtlCharToInteger(
1224 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1225 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1226 ULONG *value) /* [O] Destination for the converted value */
1228 CHAR chCurrent;
1229 int digit;
1230 ULONG RunningTotal = 0;
1231 char bMinus = 0;
1233 while (*str != '\0' && *str <= ' ') {
1234 str++;
1235 } /* while */
1237 if (*str == '+') {
1238 str++;
1239 } else if (*str == '-') {
1240 bMinus = 1;
1241 str++;
1242 } /* if */
1244 if (base == 0) {
1245 base = 10;
1246 if (str[0] == '0') {
1247 if (str[1] == 'b') {
1248 str += 2;
1249 base = 2;
1250 } else if (str[1] == 'o') {
1251 str += 2;
1252 base = 8;
1253 } else if (str[1] == 'x') {
1254 str += 2;
1255 base = 16;
1256 } /* if */
1257 } /* if */
1258 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1259 return STATUS_INVALID_PARAMETER;
1260 } /* if */
1262 if (value == NULL) {
1263 return STATUS_ACCESS_VIOLATION;
1264 } /* if */
1266 while (*str != '\0') {
1267 chCurrent = *str;
1268 if (chCurrent >= '0' && chCurrent <= '9') {
1269 digit = chCurrent - '0';
1270 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1271 digit = chCurrent - 'A' + 10;
1272 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1273 digit = chCurrent - 'a' + 10;
1274 } else {
1275 digit = -1;
1276 } /* if */
1277 if (digit < 0 || digit >= base) {
1278 *value = bMinus ? -RunningTotal : RunningTotal;
1279 return STATUS_SUCCESS;
1280 } /* if */
1282 RunningTotal = RunningTotal * base + digit;
1283 str++;
1284 } /* while */
1286 *value = bMinus ? -RunningTotal : RunningTotal;
1287 return STATUS_SUCCESS;
1291 /**************************************************************************
1292 * RtlIntegerToChar (NTDLL.@)
1294 * Converts an unsigned integer to a character string.
1296 * RETURNS
1297 * Success: STATUS_SUCCESS. str contains the converted number
1298 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1299 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1300 * STATUS_ACCESS_VIOLATION, if str is NULL.
1302 * NOTES
1303 * Instead of base 0 it uses 10 as base.
1304 * Writes at most length characters to the string str.
1305 * Str is '\0' terminated when length allowes it.
1306 * When str fits exactly in length characters the '\0' is ommitted.
1308 NTSTATUS WINAPI RtlIntegerToChar(
1309 ULONG value, /* [I] Value to be converted */
1310 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1311 ULONG length, /* [I] Length of the str buffer in bytes */
1312 PCHAR str) /* [O] Destination for the converted value */
1314 CHAR buffer[33];
1315 PCHAR pos;
1316 CHAR digit;
1317 ULONG len;
1319 if (base == 0) {
1320 base = 10;
1321 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1322 return STATUS_INVALID_PARAMETER;
1323 } /* if */
1325 pos = &buffer[32];
1326 *pos = '\0';
1328 do {
1329 pos--;
1330 digit = value % base;
1331 value = value / base;
1332 if (digit < 10) {
1333 *pos = '0' + digit;
1334 } else {
1335 *pos = 'A' + digit - 10;
1336 } /* if */
1337 } while (value != 0L);
1339 len = &buffer[32] - pos;
1340 if (len > length) {
1341 return STATUS_BUFFER_OVERFLOW;
1342 } else if (str == NULL) {
1343 return STATUS_ACCESS_VIOLATION;
1344 } else if (len == length) {
1345 memcpy(str, pos, len);
1346 } else {
1347 memcpy(str, pos, len + 1);
1348 } /* if */
1349 return STATUS_SUCCESS;
1353 /**************************************************************************
1354 * RtlUnicodeStringToInteger (NTDLL.@)
1356 * Converts an unicode string into its integer equivalent.
1358 * RETURNS
1359 * Success: STATUS_SUCCESS. value contains the converted number
1360 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1361 * STATUS_ACCESS_VIOLATION, if value is NULL.
1363 * NOTES
1364 * For base 0 it uses 10 as base and the string should be in the format
1365 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1366 * For other bases the string should be in the format
1367 * "{whitespace} [+|-] {digits}".
1368 * No check is made for value overflow, only the lower 32 bits are assigned.
1369 * If str is NULL it crashes, as the native function does.
1371 * DIFFERENCES
1372 * This function does not read garbage on string length 0 as the native
1373 * version does.
1375 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1376 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1377 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1378 ULONG *value) /* [O] Destination for the converted value */
1380 LPWSTR lpwstr = str->Buffer;
1381 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1382 WCHAR wchCurrent;
1383 int digit;
1384 ULONG RunningTotal = 0;
1385 char bMinus = 0;
1387 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1388 lpwstr++;
1389 CharsRemaining--;
1390 } /* while */
1392 if (CharsRemaining >= 1) {
1393 if (*lpwstr == '+') {
1394 lpwstr++;
1395 CharsRemaining--;
1396 } else if (*lpwstr == '-') {
1397 bMinus = 1;
1398 lpwstr++;
1399 CharsRemaining--;
1400 } /* if */
1401 } /* if */
1403 if (base == 0) {
1404 base = 10;
1405 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1406 if (lpwstr[1] == 'b') {
1407 lpwstr += 2;
1408 CharsRemaining -= 2;
1409 base = 2;
1410 } else if (lpwstr[1] == 'o') {
1411 lpwstr += 2;
1412 CharsRemaining -= 2;
1413 base = 8;
1414 } else if (lpwstr[1] == 'x') {
1415 lpwstr += 2;
1416 CharsRemaining -= 2;
1417 base = 16;
1418 } /* if */
1419 } /* if */
1420 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1421 return STATUS_INVALID_PARAMETER;
1422 } /* if */
1424 if (value == NULL) {
1425 return STATUS_ACCESS_VIOLATION;
1426 } /* if */
1428 while (CharsRemaining >= 1) {
1429 wchCurrent = *lpwstr;
1430 if (wchCurrent >= '0' && wchCurrent <= '9') {
1431 digit = wchCurrent - '0';
1432 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1433 digit = wchCurrent - 'A' + 10;
1434 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1435 digit = wchCurrent - 'a' + 10;
1436 } else {
1437 digit = -1;
1438 } /* if */
1439 if (digit < 0 || digit >= base) {
1440 *value = bMinus ? -RunningTotal : RunningTotal;
1441 return STATUS_SUCCESS;
1442 } /* if */
1444 RunningTotal = RunningTotal * base + digit;
1445 lpwstr++;
1446 CharsRemaining--;
1447 } /* while */
1449 *value = bMinus ? -RunningTotal : RunningTotal;
1450 return STATUS_SUCCESS;
1454 /**************************************************************************
1455 * RtlIntegerToUnicodeString (NTDLL.@)
1457 * Converts an unsigned integer to a '\0' terminated unicode string.
1459 * RETURNS
1460 * Success: STATUS_SUCCESS. str contains the converted number
1461 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1462 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1463 * (with the '\0' termination). In this case str->Length
1464 * is set to the length, the string would have (which can
1465 * be larger than the MaximumLength).
1467 * NOTES
1468 * Instead of base 0 it uses 10 as base.
1469 * If str is NULL it crashes, as the native function does.
1471 * DIFFERENCES
1472 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1473 * The native function does this when the string would be longer than 16
1474 * characters even when the string parameter is long enough.
1476 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1477 ULONG value, /* [I] Value to be converted */
1478 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1479 UNICODE_STRING *str) /* [O] Destination for the converted value */
1481 WCHAR buffer[33];
1482 PWCHAR pos;
1483 WCHAR digit;
1485 if (base == 0) {
1486 base = 10;
1487 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1488 return STATUS_INVALID_PARAMETER;
1489 } /* if */
1491 pos = &buffer[32];
1492 *pos = '\0';
1494 do {
1495 pos--;
1496 digit = value % base;
1497 value = value / base;
1498 if (digit < 10) {
1499 *pos = '0' + digit;
1500 } else {
1501 *pos = 'A' + digit - 10;
1502 } /* if */
1503 } while (value != 0L);
1505 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1506 if (str->Length >= str->MaximumLength) {
1507 return STATUS_BUFFER_OVERFLOW;
1508 } else {
1509 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1510 } /* if */
1511 return STATUS_SUCCESS;