ncrypt: Add NCryptIsKeyHandle stub.
[wine.git] / dlls / ntdll / rtlstr.c
blobd8ff81b805055a188c7cdf8978b2b33e0c4268c2
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "ntstatus.h"
29 #define WIN32_NO_STATUS
30 #include "windef.h"
31 #include "winnt.h"
32 #include "winternl.h"
33 #include "wine/debug.h"
34 #include "ntdll_misc.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
38 #define GUID_STRING_LENGTH 38
41 /**************************************************************************
42 * RtlInitAnsiString (NTDLL.@)
44 * Initializes a buffered ansi string.
46 * RETURNS
47 * Nothing.
49 * NOTES
50 * Assigns source to target->Buffer. The length of source is assigned to
51 * target->Length and target->MaximumLength. If source is NULL the length
52 * of source is assumed to be 0.
54 void WINAPI RtlInitAnsiString(
55 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
56 PCSZ source) /* [I] '\0' terminated string used to initialize target */
58 if ((target->Buffer = (PCHAR) source))
60 target->Length = strlen(source);
61 target->MaximumLength = target->Length + 1;
63 else target->Length = target->MaximumLength = 0;
66 /**************************************************************************
67 * RtlInitAnsiStringEx (NTDLL.@)
69 * Initializes a buffered ansi string.
71 * RETURNS
72 * An appropriate NTSTATUS value.
74 * NOTES
75 * Assigns source to target->Buffer. The length of source is assigned to
76 * target->Length and target->MaximumLength. If source is NULL the length
77 * of source is assumed to be 0.
79 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
81 if (source)
83 unsigned int len = strlen(source);
84 if (len+1 > 0xffff)
85 return STATUS_NAME_TOO_LONG;
87 target->Buffer = (PCHAR) source;
88 target->Length = len;
89 target->MaximumLength = len + 1;
91 else
93 target->Buffer = NULL;
94 target->Length = 0;
95 target->MaximumLength = 0;
97 return STATUS_SUCCESS;
100 /**************************************************************************
101 * RtlInitString (NTDLL.@)
103 * Initializes a buffered string.
105 * RETURNS
106 * Nothing.
108 * NOTES
109 * Assigns source to target->Buffer. The length of source is assigned to
110 * target->Length and target->MaximumLength. If source is NULL the length
111 * of source is assumed to be 0.
113 void WINAPI RtlInitString(
114 PSTRING target, /* [I/O] Buffered string to be initialized */
115 PCSZ source) /* [I] '\0' terminated string used to initialize target */
117 RtlInitAnsiString( target, source );
121 /**************************************************************************
122 * RtlFreeAnsiString (NTDLL.@)
124 void WINAPI RtlFreeAnsiString( PSTRING str )
126 if (str->Buffer)
128 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
129 RtlZeroMemory( str, sizeof(*str) );
134 /**************************************************************************
135 * RtlFreeOemString (NTDLL.@)
137 void WINAPI RtlFreeOemString( PSTRING str )
139 RtlFreeAnsiString( str );
143 /**************************************************************************
144 * RtlCopyString (NTDLL.@)
146 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
148 if (src)
150 unsigned int len = min( src->Length, dst->MaximumLength );
151 memcpy( dst->Buffer, src->Buffer, len );
152 dst->Length = len;
154 else dst->Length = 0;
158 /**************************************************************************
159 * RtlInitUnicodeString (NTDLL.@)
161 * Initializes a buffered unicode string.
163 * RETURNS
164 * Nothing.
166 * NOTES
167 * Assigns source to target->Buffer. The length of source is assigned to
168 * target->Length and target->MaximumLength. If source is NULL the length
169 * of source is assumed to be 0.
171 void WINAPI RtlInitUnicodeString(
172 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
173 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
175 if ((target->Buffer = (PWSTR) source))
177 unsigned int length = wcslen(source) * sizeof(WCHAR);
178 if (length > 0xfffc)
179 length = 0xfffc;
180 target->Length = length;
181 target->MaximumLength = target->Length + sizeof(WCHAR);
183 else target->Length = target->MaximumLength = 0;
187 /**************************************************************************
188 * RtlInitUnicodeStringEx (NTDLL.@)
190 * Initializes a buffered unicode string.
192 * RETURNS
193 * Success: STATUS_SUCCESS. target is initialized.
194 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
196 * NOTES
197 * Assigns source to target->Buffer. The length of source is assigned to
198 * target->Length and target->MaximumLength. If source is NULL the length
199 * of source is assumed to be 0.
201 NTSTATUS WINAPI RtlInitUnicodeStringEx(
202 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
203 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
205 if (source != NULL) {
206 unsigned int len = wcslen(source) * sizeof(WCHAR);
208 if (len > 0xFFFC) {
209 return STATUS_NAME_TOO_LONG;
210 } else {
211 target->Length = len;
212 target->MaximumLength = len + sizeof(WCHAR);
213 target->Buffer = (PWSTR) source;
214 } /* if */
215 } else {
216 target->Length = 0;
217 target->MaximumLength = 0;
218 target->Buffer = NULL;
219 } /* if */
220 return STATUS_SUCCESS;
224 /**************************************************************************
225 * RtlCreateUnicodeString (NTDLL.@)
227 * Creates a UNICODE_STRING from a null-terminated Unicode string.
229 * RETURNS
230 * Success: TRUE
231 * Failure: FALSE
233 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
235 int len = (wcslen(src) + 1) * sizeof(WCHAR);
236 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
237 memcpy( target->Buffer, src, len );
238 target->MaximumLength = len;
239 target->Length = len - sizeof(WCHAR);
240 return TRUE;
244 /**************************************************************************
245 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
247 * Creates a UNICODE_STRING from a null-terminated Ascii string.
249 * RETURNS
250 * Success: TRUE
251 * Failure: FALSE
253 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
255 STRING ansi;
256 RtlInitAnsiString( &ansi, src );
257 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
261 /**************************************************************************
262 * RtlFreeUnicodeString (NTDLL.@)
264 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
265 * RtlCreateUnicodeStringFromAsciiz().
267 * RETURNS
268 * nothing
270 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
272 if (str->Buffer)
274 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
275 RtlZeroMemory( str, sizeof(*str) );
280 /**************************************************************************
281 * RtlCopyUnicodeString (NTDLL.@)
283 * Copies from one UNICODE_STRING to another.
285 * RETURNS
286 * nothing
288 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
290 if (src)
292 unsigned int len = min( src->Length, dst->MaximumLength );
293 memcpy( dst->Buffer, src->Buffer, len );
294 dst->Length = len;
295 /* append terminating '\0' if enough space */
296 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
298 else dst->Length = 0;
302 /**************************************************************************
303 * RtlDuplicateUnicodeString (NTDLL.@)
305 * Duplicates a unicode string.
307 * RETURNS
308 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
309 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
310 * STATUS_NO_MEMORY, if the allocation fails.
312 * NOTES
313 * For add_nul there are several possible values:
314 * 0 = destination will not be '\0' terminated,
315 * 1 = destination will be '\0' terminated,
316 * 3 = like 1 but for an empty source string produce '\0' terminated empty
317 * Buffer instead of assigning NULL to the Buffer.
318 * Other add_nul values are invalid.
320 NTSTATUS WINAPI RtlDuplicateUnicodeString(
321 int add_nul, /* [I] flag */
322 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
323 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
325 if (source == NULL || destination == NULL ||
326 source->Length > source->MaximumLength ||
327 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
328 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
329 return STATUS_INVALID_PARAMETER;
330 } else {
331 if (source->Length == 0 && add_nul != 3) {
332 destination->Length = 0;
333 destination->MaximumLength = 0;
334 destination->Buffer = NULL;
335 } else {
336 unsigned int destination_max_len = source->Length;
338 if (add_nul) {
339 destination_max_len += sizeof(WCHAR);
340 } /* if */
341 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
342 if (destination->Buffer == NULL) {
343 return STATUS_NO_MEMORY;
344 } else {
345 memcpy(destination->Buffer, source->Buffer, source->Length);
346 destination->Length = source->Length;
347 destination->MaximumLength = source->Length;
348 /* append terminating '\0' if enough space */
349 if (add_nul) {
350 destination->MaximumLength = destination_max_len;
351 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
352 } /* if */
353 } /* if */
354 } /* if */
355 } /* if */
356 return STATUS_SUCCESS;
360 /**************************************************************************
361 * RtlEraseUnicodeString (NTDLL.@)
363 * Overwrites a UNICODE_STRING with zeros.
365 * RETURNS
366 * nothing
368 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
370 if (str->Buffer)
372 memset( str->Buffer, 0, str->MaximumLength );
373 str->Length = 0;
379 COMPARISON FUNCTIONS
383 /******************************************************************************
384 * RtlCompareString (NTDLL.@)
386 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
388 unsigned int len;
389 LONG ret = 0;
390 LPCSTR p1, p2;
392 len = min(s1->Length, s2->Length);
393 p1 = s1->Buffer;
394 p2 = s2->Buffer;
396 if (CaseInsensitive)
398 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
400 else
402 while (!ret && len--) ret = *p1++ - *p2++;
404 if (!ret) ret = s1->Length - s2->Length;
405 return ret;
409 /******************************************************************************
410 * RtlCompareUnicodeString (NTDLL.@)
412 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
413 BOOLEAN CaseInsensitive )
415 return RtlCompareUnicodeStrings( s1->Buffer, s1->Length / sizeof(WCHAR),
416 s2->Buffer, s2->Length / sizeof(WCHAR), CaseInsensitive );
420 /**************************************************************************
421 * RtlEqualString (NTDLL.@)
423 * Determine if two strings are equal.
425 * PARAMS
426 * s1 [I] Source string
427 * s2 [I] String to compare to s1
428 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
430 * RETURNS
431 * Non-zero if s1 is equal to s2, 0 otherwise.
433 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
435 if (s1->Length != s2->Length) return FALSE;
436 return !RtlCompareString( s1, s2, CaseInsensitive );
440 /**************************************************************************
441 * RtlEqualUnicodeString (NTDLL.@)
443 * Unicode version of RtlEqualString.
445 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
446 BOOLEAN CaseInsensitive )
448 if (s1->Length != s2->Length) return FALSE;
449 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
453 /**************************************************************************
454 * RtlPrefixString (NTDLL.@)
456 * Determine if one string is a prefix of another.
458 * PARAMS
459 * s1 [I] Prefix to look for in s2
460 * s2 [I] String that may contain s1 as a prefix
461 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
463 * RETURNS
464 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
466 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
468 unsigned int i;
470 if (s1->Length > s2->Length) return FALSE;
471 if (ignore_case)
473 for (i = 0; i < s1->Length; i++)
474 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
476 else
478 for (i = 0; i < s1->Length; i++)
479 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
481 return TRUE;
485 /**************************************************************************
486 * RtlEqualComputerName (NTDLL.@)
488 * Determine if two computer names are the same.
490 * PARAMS
491 * left [I] First computer name
492 * right [I] Second computer name
494 * RETURNS
495 * 0 if the names are equal, non-zero otherwise.
497 * NOTES
498 * The comparison is case insensitive.
500 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
501 const UNICODE_STRING *right)
503 NTSTATUS ret;
504 STRING upLeft, upRight;
506 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
508 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
510 ret = RtlEqualString( &upLeft, &upRight, FALSE );
511 RtlFreeOemString( &upRight );
513 RtlFreeOemString( &upLeft );
515 return ret;
519 /**************************************************************************
520 * RtlEqualDomainName (NTDLL.@)
522 * Determine if two domain names are the same.
524 * PARAMS
525 * left [I] First domain name
526 * right [I] Second domain name
528 * RETURNS
529 * 0 if the names are equal, non-zero otherwise.
531 * NOTES
532 * The comparison is case insensitive.
534 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
535 const UNICODE_STRING *right)
537 return RtlEqualComputerName(left, right);
542 COPY BETWEEN ANSI_STRING or UNICODE_STRING
543 there is no parameter checking, it just crashes
547 /**************************************************************************
548 * RtlAnsiStringToUnicodeString (NTDLL.@)
550 * Converts an ansi string to a unicode string.
552 * RETURNS
553 * Success: STATUS_SUCCESS. uni contains the converted string
554 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
555 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
556 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
558 * NOTES
559 * This function always writes a terminating '\0'.
561 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
562 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
563 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
564 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
566 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
568 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
569 uni->Length = total - sizeof(WCHAR);
570 if (doalloc)
572 uni->MaximumLength = total;
573 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
574 return STATUS_NO_MEMORY;
576 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
578 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
579 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
580 return STATUS_SUCCESS;
584 /**************************************************************************
585 * RtlOemStringToUnicodeString (NTDLL.@)
587 * Converts an oem string to a unicode string.
589 * RETURNS
590 * Success: STATUS_SUCCESS. uni contains the converted string
591 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
592 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
593 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
595 * NOTES
596 * This function always writes a terminating '\0'.
598 NTSTATUS WINAPI RtlOemStringToUnicodeString(
599 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
600 const STRING *oem, /* [I] Oem string to be converted */
601 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
603 DWORD total = RtlOemStringToUnicodeSize( oem );
605 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
606 uni->Length = total - sizeof(WCHAR);
607 if (doalloc)
609 uni->MaximumLength = total;
610 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
611 return STATUS_NO_MEMORY;
613 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
615 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
616 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
617 return STATUS_SUCCESS;
621 /**************************************************************************
622 * RtlUnicodeStringToAnsiString (NTDLL.@)
624 * Converts a unicode string to an ansi string.
626 * RETURNS
627 * Success: STATUS_SUCCESS. ansi contains the converted string
628 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
629 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
631 * NOTES
632 * This function always writes a terminating '\0'.
633 * It performs a partial copy if ansi is too small.
635 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
636 STRING *ansi, /* [I/O] Destination for the ansi string */
637 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
638 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
640 NTSTATUS ret = STATUS_SUCCESS;
641 DWORD len = RtlUnicodeStringToAnsiSize( uni );
643 ansi->Length = len - 1;
644 if (doalloc)
646 ansi->MaximumLength = len;
647 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
648 return STATUS_NO_MEMORY;
650 else if (ansi->MaximumLength < len)
652 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
653 ansi->Length = ansi->MaximumLength - 1;
654 ret = STATUS_BUFFER_OVERFLOW;
657 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
658 ansi->Buffer[ansi->Length] = 0;
659 return ret;
663 /**************************************************************************
664 * RtlUnicodeStringToOemString (NTDLL.@)
666 * Converts a Rtl Unicode string to an OEM string.
668 * PARAMS
669 * oem [O] Destination for OEM string
670 * uni [I] Source Unicode string
671 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
673 * RETURNS
674 * Success: STATUS_SUCCESS. oem contains the converted string
675 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
676 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
678 * NOTES
679 * If doalloc is TRUE, the length allocated is uni->Length + 1.
680 * This function always '\0' terminates the string returned.
682 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
683 const UNICODE_STRING *uni,
684 BOOLEAN doalloc )
686 NTSTATUS ret = STATUS_SUCCESS;
687 DWORD len = RtlUnicodeStringToOemSize( uni );
689 oem->Length = len - 1;
690 if (doalloc)
692 oem->MaximumLength = len;
693 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
694 return STATUS_NO_MEMORY;
696 else if (oem->MaximumLength < len)
698 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
699 oem->Length = oem->MaximumLength - 1;
700 ret = STATUS_BUFFER_OVERFLOW;
703 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
704 oem->Buffer[oem->Length] = 0;
705 return ret;
710 CASE CONVERSIONS
714 /**************************************************************************
715 * RtlUpperChar (NTDLL.@)
717 * Converts an Ascii character to uppercase.
719 * PARAMS
720 * ch [I] Character to convert
722 * RETURNS
723 * The uppercase character value.
725 * NOTES
726 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
727 * All other input characters are returned unchanged. The locale and
728 * multibyte characters are not taken into account (as native DLL).
730 CHAR WINAPI RtlUpperChar( CHAR ch )
732 if (ch >= 'a' && ch <= 'z') {
733 return ch - 'a' + 'A';
734 } else {
735 return ch;
736 } /* if */
740 /**************************************************************************
741 * RtlUpperString (NTDLL.@)
743 * Converts an Ascii string to uppercase.
745 * PARAMS
746 * dst [O] Destination for converted string
747 * src [I] Source string to convert
749 * RETURNS
750 * Nothing.
752 * NOTES
753 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
754 * All other src characters are copied unchanged to dst. The locale and
755 * multibyte characters are not taken into account (as native DLL).
756 * The number of character copied is the minimum of src->Length and
757 * the dst->MaximumLength.
759 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
761 unsigned int i, len = min(src->Length, dst->MaximumLength);
763 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
764 dst->Length = len;
768 /**************************************************************************
769 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
771 * Converts a Unicode string to the equivalent ANSI upper-case representation.
773 * RETURNS
774 * NTSTATUS code
776 * NOTES
777 * writes terminating 0
779 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *ansi,
780 const UNICODE_STRING *uni,
781 BOOLEAN doalloc )
783 NTSTATUS ret = STATUS_SUCCESS;
784 DWORD len = RtlUnicodeStringToAnsiSize( uni );
786 ansi->Length = len - 1;
787 if (doalloc)
789 ansi->MaximumLength = len;
790 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
791 return STATUS_NO_MEMORY;
793 else if (ansi->MaximumLength < len)
795 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
796 ansi->Length = ansi->MaximumLength - 1;
797 ret = STATUS_BUFFER_OVERFLOW;
800 RtlUpcaseUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
801 ansi->Buffer[ansi->Length] = 0;
802 return ret;
806 /**************************************************************************
807 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
809 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
810 * stored in STRING format.
812 * RETURNS
813 * NTSTATUS code
815 * NOTES
816 * writes terminating 0
818 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *oem,
819 const UNICODE_STRING *uni,
820 BOOLEAN doalloc )
822 NTSTATUS ret = STATUS_SUCCESS;
823 DWORD len = RtlUnicodeStringToAnsiSize( uni );
825 oem->Length = len - 1;
826 if (doalloc)
828 oem->MaximumLength = len;
829 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
830 return STATUS_NO_MEMORY;
832 else if (oem->MaximumLength < len)
834 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
835 oem->Length = oem->MaximumLength - 1;
836 ret = STATUS_BUFFER_OVERFLOW;
839 RtlUpcaseUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
840 oem->Buffer[oem->Length] = 0;
841 return ret;
845 /**************************************************************************
846 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
848 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
849 * stored in STRING format.
851 * RETURNS
852 * NTSTATUS code
854 * NOTES
855 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
857 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
858 const UNICODE_STRING *uni,
859 BOOLEAN doalloc )
861 NTSTATUS ret = STATUS_SUCCESS;
862 DWORD len = RtlUnicodeStringToOemSize( uni ) - 1;
864 oem->Length = len;
865 if (doalloc)
867 oem->MaximumLength = len;
868 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return STATUS_NO_MEMORY;
870 else if (oem->MaximumLength < len)
872 ret = STATUS_BUFFER_OVERFLOW;
873 oem->Length = oem->MaximumLength;
874 if (!oem->MaximumLength) return ret;
876 RtlUpcaseUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
877 return ret;
882 STRING SIZE
886 /**************************************************************************
887 * RtlAnsiStringToUnicodeSize (NTDLL.@)
888 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
890 * Calculate the size in bytes necessary for the Unicode conversion of str,
891 * including the terminating '\0'.
893 * PARAMS
894 * str [I] String to calculate the size of
896 * RETURNS
897 * The calculated size.
899 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
901 DWORD ret;
902 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
903 return ret + sizeof(WCHAR);
907 /**************************************************************************
908 * RtlUnicodeStringToAnsiSize (NTDLL.@)
909 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
911 * Calculate the size in bytes necessary for the Ansi conversion of str,
912 * including the terminating '\0'.
914 * PARAMS
915 * str [I] String to calculate the size of
917 * RETURNS
918 * The calculated size.
920 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
922 DWORD ret;
923 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
924 return ret + 1;
928 /**************************************************************************
929 * RtlAppendAsciizToString (NTDLL.@)
931 * Concatenates a buffered character string and a '\0' terminated character
932 * string
934 * RETURNS
935 * Success: STATUS_SUCCESS. src is appended to dest.
936 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
937 * to hold the concatenated string.
939 * NOTES
940 * if src is NULL dest is unchanged.
941 * dest is never '\0' terminated.
943 NTSTATUS WINAPI RtlAppendAsciizToString(
944 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
945 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
947 if (src != NULL) {
948 unsigned int src_len = strlen(src);
949 unsigned int dest_len = src_len + dest->Length;
951 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
952 memcpy(dest->Buffer + dest->Length, src, src_len);
953 dest->Length = dest_len;
954 } /* if */
955 return STATUS_SUCCESS;
959 /**************************************************************************
960 * RtlAppendStringToString (NTDLL.@)
962 * Concatenates two buffered character strings
964 * RETURNS
965 * Success: STATUS_SUCCESS. src is appended to dest.
966 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
967 * to hold the concatenated string.
969 * NOTES
970 * if src->length is zero dest is unchanged.
971 * dest is never '\0' terminated.
973 NTSTATUS WINAPI RtlAppendStringToString(
974 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
975 const STRING *src) /* [I] Buffered character string to be concatenated */
977 if (src->Length != 0) {
978 unsigned int dest_len = src->Length + dest->Length;
980 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
981 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
982 dest->Length = dest_len;
983 } /* if */
984 return STATUS_SUCCESS;
988 /**************************************************************************
989 * RtlAppendUnicodeToString (NTDLL.@)
991 * Concatenates a buffered unicode string and a '\0' terminated unicode
992 * string
994 * RETURNS
995 * Success: STATUS_SUCCESS. src is appended to dest.
996 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
997 * to hold the concatenated string.
999 * NOTES
1000 * if src is NULL dest is unchanged.
1001 * dest is '\0' terminated when the MaximumLength allows it.
1002 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1004 * DIFFERENCES
1005 * Does not write in the src->Buffer beyond MaximumLength when
1006 * MaximumLength is odd as the native function does.
1008 NTSTATUS WINAPI RtlAppendUnicodeToString(
1009 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1010 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1012 if (src != NULL) {
1013 unsigned int src_len = wcslen(src) * sizeof(WCHAR);
1014 unsigned int dest_len = src_len + dest->Length;
1016 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1017 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1018 dest->Length = dest_len;
1019 /* append terminating '\0' if enough space */
1020 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1021 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1022 } /* if */
1023 } /* if */
1024 return STATUS_SUCCESS;
1028 /**************************************************************************
1029 * RtlAppendUnicodeStringToString (NTDLL.@)
1031 * Concatenates two buffered unicode strings
1033 * RETURNS
1034 * Success: STATUS_SUCCESS. src is appended to dest.
1035 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1036 * to hold the concatenated string.
1038 * NOTES
1039 * if src->length is zero dest is unchanged.
1040 * dest is '\0' terminated when the MaximumLength allows it.
1041 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1043 * DIFFERENCES
1044 * Does not write in the src->Buffer beyond MaximumLength when
1045 * MaximumLength is odd as the native function does.
1047 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1048 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1049 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1051 if (src->Length != 0) {
1052 unsigned int dest_len = src->Length + dest->Length;
1054 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1055 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1056 dest->Length = dest_len;
1057 /* append terminating '\0' if enough space */
1058 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1059 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1060 } /* if */
1061 } /* if */
1062 return STATUS_SUCCESS;
1066 /**************************************************************************
1067 * RtlFindCharInUnicodeString (NTDLL.@)
1069 * Searches for one of several unicode characters in a unicode string.
1071 * RETURNS
1072 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1073 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1075 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1076 int flags, /* [I] Flags */
1077 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1078 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1079 USHORT *pos) /* [O] Position of the first character found + 2 */
1081 unsigned int main_idx, search_idx;
1083 switch (flags) {
1084 case 0:
1085 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1086 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1087 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1088 *pos = (main_idx + 1) * sizeof(WCHAR);
1089 return STATUS_SUCCESS;
1093 *pos = 0;
1094 return STATUS_NOT_FOUND;
1095 case 1:
1096 main_idx = main_str->Length / sizeof(WCHAR);
1097 while (main_idx-- > 0) {
1098 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1099 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1100 *pos = main_idx * sizeof(WCHAR);
1101 return STATUS_SUCCESS;
1105 *pos = 0;
1106 return STATUS_NOT_FOUND;
1107 case 2:
1108 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1109 search_idx = 0;
1110 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1111 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1112 search_idx++;
1114 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1115 *pos = (main_idx + 1) * sizeof(WCHAR);
1116 return STATUS_SUCCESS;
1119 *pos = 0;
1120 return STATUS_NOT_FOUND;
1121 case 3:
1122 main_idx = main_str->Length / sizeof(WCHAR);
1123 while (main_idx-- > 0) {
1124 search_idx = 0;
1125 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1126 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1127 search_idx++;
1129 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1130 *pos = main_idx * sizeof(WCHAR);
1131 return STATUS_SUCCESS;
1134 *pos = 0;
1135 return STATUS_NOT_FOUND;
1136 } /* switch */
1137 return STATUS_NOT_FOUND;
1142 MISC
1145 /**************************************************************************
1146 * RtlIsTextUnicode (NTDLL.@)
1148 * Attempt to guess whether a text buffer is Unicode.
1150 * PARAMS
1151 * buf [I] Text buffer to test
1152 * len [I] Length of buf
1153 * pf [O] Destination for test results
1155 * RETURNS
1156 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1158 * FIXME
1159 * Should implement more tests.
1161 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1163 static const WCHAR std_control_chars[] = {'\r','\n','\t',' ',0x3000,0};
1164 static const WCHAR byterev_control_chars[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1165 const WCHAR *s = buf;
1166 int i;
1167 unsigned int flags = ~0U, out_flags = 0;
1169 if (len < sizeof(WCHAR))
1171 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1172 if (pf) *pf = 0;
1173 return FALSE;
1175 if (pf)
1176 flags = *pf;
1178 * Apply various tests to the text string. According to the
1179 * docs, each test "passed" sets the corresponding flag in
1180 * the output flags. But some of the tests are mutually
1181 * exclusive, so I don't see how you could pass all tests ...
1184 /* Check for an odd length ... pass if even. */
1185 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1187 if (((const char *)buf)[len - 1] == 0)
1188 len--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1190 len /= sizeof(WCHAR);
1191 /* Windows only checks the first 256 characters */
1192 if (len > 256) len = 256;
1194 /* Check for the special byte order unicode marks. */
1195 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1196 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1198 /* apply some statistical analysis */
1199 if (flags & IS_TEXT_UNICODE_STATISTICS)
1201 int stats = 0;
1202 /* FIXME: checks only for ASCII characters in the unicode stream */
1203 for (i = 0; i < len; i++)
1205 if (s[i] <= 255) stats++;
1207 if (stats > len / 2)
1208 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1211 /* Check for unicode NULL chars */
1212 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1214 for (i = 0; i < len; i++)
1216 if (!(s[i] & 0xff) || !(s[i] >> 8))
1218 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1219 break;
1224 if (flags & IS_TEXT_UNICODE_CONTROLS)
1226 for (i = 0; i < len; i++)
1228 if (wcschr(std_control_chars, s[i]))
1230 out_flags |= IS_TEXT_UNICODE_CONTROLS;
1231 break;
1236 if (flags & IS_TEXT_UNICODE_REVERSE_CONTROLS)
1238 for (i = 0; i < len; i++)
1240 if (wcschr(byterev_control_chars, s[i]))
1242 out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS;
1243 break;
1248 if (pf)
1250 out_flags &= *pf;
1251 *pf = out_flags;
1253 /* check for flags that indicate it's definitely not valid Unicode */
1254 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1255 /* now check for invalid ASCII, and assume Unicode if so */
1256 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1257 /* now check for Unicode flags */
1258 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1259 /* no flags set */
1260 return FALSE;
1264 /**************************************************************************
1265 * RtlCharToInteger (NTDLL.@)
1267 * Converts a character string into its integer equivalent.
1269 * RETURNS
1270 * Success: STATUS_SUCCESS. value contains the converted number
1271 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1272 * STATUS_ACCESS_VIOLATION, if value is NULL.
1274 * NOTES
1275 * For base 0 it uses 10 as base and the string should be in the format
1276 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1277 * For other bases the string should be in the format
1278 * "{whitespace} [+|-] {digits}".
1279 * No check is made for value overflow, only the lower 32 bits are assigned.
1280 * If str is NULL it crashes, as the native function does.
1282 * DIFFERENCES
1283 * This function does not read garbage behind '\0' as the native version does.
1285 NTSTATUS WINAPI RtlCharToInteger(
1286 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1287 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1288 ULONG *value) /* [O] Destination for the converted value */
1290 CHAR chCurrent;
1291 int digit;
1292 ULONG RunningTotal = 0;
1293 BOOL bMinus = FALSE;
1295 while (*str != '\0' && *str <= ' ') {
1296 str++;
1297 } /* while */
1299 if (*str == '+') {
1300 str++;
1301 } else if (*str == '-') {
1302 bMinus = TRUE;
1303 str++;
1304 } /* if */
1306 if (base == 0) {
1307 base = 10;
1308 if (str[0] == '0') {
1309 if (str[1] == 'b') {
1310 str += 2;
1311 base = 2;
1312 } else if (str[1] == 'o') {
1313 str += 2;
1314 base = 8;
1315 } else if (str[1] == 'x') {
1316 str += 2;
1317 base = 16;
1318 } /* if */
1319 } /* if */
1320 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1321 return STATUS_INVALID_PARAMETER;
1322 } /* if */
1324 if (value == NULL) {
1325 return STATUS_ACCESS_VIOLATION;
1326 } /* if */
1328 while (*str != '\0') {
1329 chCurrent = *str;
1330 if (chCurrent >= '0' && chCurrent <= '9') {
1331 digit = chCurrent - '0';
1332 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1333 digit = chCurrent - 'A' + 10;
1334 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1335 digit = chCurrent - 'a' + 10;
1336 } else {
1337 digit = -1;
1338 } /* if */
1339 if (digit < 0 || digit >= base) {
1340 *value = bMinus ? -RunningTotal : RunningTotal;
1341 return STATUS_SUCCESS;
1342 } /* if */
1344 RunningTotal = RunningTotal * base + digit;
1345 str++;
1346 } /* while */
1348 *value = bMinus ? -RunningTotal : RunningTotal;
1349 return STATUS_SUCCESS;
1353 /**************************************************************************
1354 * RtlIntegerToChar (NTDLL.@)
1356 * Converts an unsigned integer to a character string.
1358 * RETURNS
1359 * Success: STATUS_SUCCESS. str contains the converted number
1360 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1361 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1362 * STATUS_ACCESS_VIOLATION, if str is NULL.
1364 * NOTES
1365 * Instead of base 0 it uses 10 as base.
1366 * Writes at most length characters to the string str.
1367 * Str is '\0' terminated when length allows it.
1368 * When str fits exactly in length characters the '\0' is omitted.
1370 NTSTATUS WINAPI RtlIntegerToChar(
1371 ULONG value, /* [I] Value to be converted */
1372 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1373 ULONG length, /* [I] Length of the str buffer in bytes */
1374 PCHAR str) /* [O] Destination for the converted value */
1376 CHAR buffer[33];
1377 PCHAR pos;
1378 CHAR digit;
1379 ULONG len;
1381 if (base == 0) {
1382 base = 10;
1383 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1384 return STATUS_INVALID_PARAMETER;
1385 } /* if */
1387 pos = &buffer[32];
1388 *pos = '\0';
1390 do {
1391 pos--;
1392 digit = value % base;
1393 value = value / base;
1394 if (digit < 10) {
1395 *pos = '0' + digit;
1396 } else {
1397 *pos = 'A' + digit - 10;
1398 } /* if */
1399 } while (value != 0L);
1401 len = &buffer[32] - pos;
1402 if (len > length) {
1403 return STATUS_BUFFER_OVERFLOW;
1404 } else if (str == NULL) {
1405 return STATUS_ACCESS_VIOLATION;
1406 } else if (len == length) {
1407 memcpy(str, pos, len);
1408 } else {
1409 memcpy(str, pos, len + 1);
1410 } /* if */
1411 return STATUS_SUCCESS;
1415 /**************************************************************************
1416 * RtlUnicodeStringToInteger (NTDLL.@)
1418 * Converts a unicode string into its integer equivalent.
1420 * RETURNS
1421 * Success: STATUS_SUCCESS. value contains the converted number
1422 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1423 * STATUS_ACCESS_VIOLATION, if value is NULL.
1425 * NOTES
1426 * For base 0 it uses 10 as base and the string should be in the format
1427 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1428 * For other bases the string should be in the format
1429 * "{whitespace} [+|-] {digits}".
1430 * No check is made for value overflow, only the lower 32 bits are assigned.
1431 * If str is NULL it crashes, as the native function does.
1433 * DIFFERENCES
1434 * This function does not read garbage on string length 0 as the native
1435 * version does.
1437 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1438 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1439 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1440 ULONG *value) /* [O] Destination for the converted value */
1442 LPWSTR lpwstr = str->Buffer;
1443 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1444 WCHAR wchCurrent;
1445 int digit;
1446 ULONG RunningTotal = 0;
1447 BOOL bMinus = FALSE;
1449 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1450 lpwstr++;
1451 CharsRemaining--;
1452 } /* while */
1454 if (CharsRemaining >= 1) {
1455 if (*lpwstr == '+') {
1456 lpwstr++;
1457 CharsRemaining--;
1458 } else if (*lpwstr == '-') {
1459 bMinus = TRUE;
1460 lpwstr++;
1461 CharsRemaining--;
1462 } /* if */
1463 } /* if */
1465 if (base == 0) {
1466 base = 10;
1467 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1468 if (lpwstr[1] == 'b') {
1469 lpwstr += 2;
1470 CharsRemaining -= 2;
1471 base = 2;
1472 } else if (lpwstr[1] == 'o') {
1473 lpwstr += 2;
1474 CharsRemaining -= 2;
1475 base = 8;
1476 } else if (lpwstr[1] == 'x') {
1477 lpwstr += 2;
1478 CharsRemaining -= 2;
1479 base = 16;
1480 } /* if */
1481 } /* if */
1482 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1483 return STATUS_INVALID_PARAMETER;
1484 } /* if */
1486 if (value == NULL) {
1487 return STATUS_ACCESS_VIOLATION;
1488 } /* if */
1490 while (CharsRemaining >= 1) {
1491 wchCurrent = *lpwstr;
1492 if (wchCurrent >= '0' && wchCurrent <= '9') {
1493 digit = wchCurrent - '0';
1494 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1495 digit = wchCurrent - 'A' + 10;
1496 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1497 digit = wchCurrent - 'a' + 10;
1498 } else {
1499 digit = -1;
1500 } /* if */
1501 if (digit < 0 || digit >= base) {
1502 *value = bMinus ? -RunningTotal : RunningTotal;
1503 return STATUS_SUCCESS;
1504 } /* if */
1506 RunningTotal = RunningTotal * base + digit;
1507 lpwstr++;
1508 CharsRemaining--;
1509 } /* while */
1511 *value = bMinus ? -RunningTotal : RunningTotal;
1512 return STATUS_SUCCESS;
1516 /**************************************************************************
1517 * RtlIntegerToUnicodeString (NTDLL.@)
1519 * Converts an unsigned integer to a '\0' terminated unicode string.
1521 * RETURNS
1522 * Success: STATUS_SUCCESS. str contains the converted number
1523 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1524 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1525 * (with the '\0' termination). In this case str->Length
1526 * is set to the length, the string would have (which can
1527 * be larger than the MaximumLength).
1529 * NOTES
1530 * Instead of base 0 it uses 10 as base.
1531 * If str is NULL it crashes, as the native function does.
1533 * DIFFERENCES
1534 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1535 * The native function does this when the string would be longer than 16
1536 * characters even when the string parameter is long enough.
1538 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1539 ULONG value, /* [I] Value to be converted */
1540 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1541 UNICODE_STRING *str) /* [O] Destination for the converted value */
1543 WCHAR buffer[33];
1544 PWCHAR pos;
1545 WCHAR digit;
1547 if (base == 0) {
1548 base = 10;
1549 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1550 return STATUS_INVALID_PARAMETER;
1551 } /* if */
1553 pos = &buffer[32];
1554 *pos = '\0';
1556 do {
1557 pos--;
1558 digit = value % base;
1559 value = value / base;
1560 if (digit < 10) {
1561 *pos = '0' + digit;
1562 } else {
1563 *pos = 'A' + digit - 10;
1564 } /* if */
1565 } while (value != 0L);
1567 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1568 if (str->Length >= str->MaximumLength) {
1569 return STATUS_BUFFER_OVERFLOW;
1570 } else {
1571 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1572 } /* if */
1573 return STATUS_SUCCESS;
1577 /*************************************************************************
1578 * RtlGUIDFromString (NTDLL.@)
1580 * Convert a string representation of a GUID into a GUID.
1582 * PARAMS
1583 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1584 * guid [O] Destination for the converted GUID
1586 * RETURNS
1587 * Success: STATUS_SUCCESS. guid contains the converted value.
1588 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1590 * SEE ALSO
1591 * See RtlStringFromGUID.
1593 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
1595 int i = 0;
1596 const WCHAR *lpszCLSID = str->Buffer;
1597 BYTE* lpOut = (BYTE*)guid;
1599 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1601 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1602 * to memory: DWORD... WORD WORD BYTES............
1604 while (i <= 37)
1606 switch (i)
1608 case 0:
1609 if (*lpszCLSID != '{')
1610 return STATUS_INVALID_PARAMETER;
1611 break;
1613 case 9: case 14: case 19: case 24:
1614 if (*lpszCLSID != '-')
1615 return STATUS_INVALID_PARAMETER;
1616 break;
1618 case 37:
1619 if (*lpszCLSID != '}')
1620 return STATUS_INVALID_PARAMETER;
1621 break;
1623 default:
1625 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1626 unsigned char byte;
1628 /* Read two hex digits as a byte value */
1629 if (ch >= '0' && ch <= '9') ch = ch - '0';
1630 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1631 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1632 else return STATUS_INVALID_PARAMETER;
1634 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1635 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1636 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1637 else return STATUS_INVALID_PARAMETER;
1639 byte = ch << 4 | ch2;
1641 switch (i)
1643 #ifndef WORDS_BIGENDIAN
1644 /* For Big Endian machines, we store the data such that the
1645 * dword/word members can be read as DWORDS and WORDS correctly. */
1646 /* Dword */
1647 case 1: lpOut[3] = byte; break;
1648 case 3: lpOut[2] = byte; break;
1649 case 5: lpOut[1] = byte; break;
1650 case 7: lpOut[0] = byte; lpOut += 4; break;
1651 /* Word */
1652 case 10: case 15: lpOut[1] = byte; break;
1653 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1654 #endif
1655 /* Byte */
1656 default: lpOut[0] = byte; lpOut++; break;
1658 lpszCLSID++; /* Skip 2nd character of byte */
1659 i++;
1662 lpszCLSID++;
1663 i++;
1666 return STATUS_SUCCESS;
1669 /*************************************************************************
1670 * RtlStringFromGUID (NTDLL.@)
1672 * Convert a GUID into a string representation of a GUID.
1674 * PARAMS
1675 * guid [I] GUID to convert
1676 * str [O] Destination for the converted string
1678 * RETURNS
1679 * Success: STATUS_SUCCESS. str contains the converted value.
1680 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
1682 * SEE ALSO
1683 * See RtlGUIDFromString.
1685 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
1687 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
1688 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
1689 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
1690 '%','0','2','X','%','0','2','X','}','\0' };
1692 TRACE("(%p,%p)\n", guid, str);
1694 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
1695 str->MaximumLength = str->Length + sizeof(WCHAR);
1696 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
1697 if (!str->Buffer)
1699 str->Length = str->MaximumLength = 0;
1700 return STATUS_NO_MEMORY;
1702 swprintf(str->Buffer, str->MaximumLength/sizeof(WCHAR), szFormat, guid->Data1, guid->Data2, guid->Data3,
1703 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
1704 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
1706 return STATUS_SUCCESS;
1710 /***********************************************************************
1711 * Message formatting
1712 ***********************************************************************/
1714 struct format_message_args
1716 int last; /* last used arg */
1717 ULONG_PTR *array; /* args array */
1718 __ms_va_list *list; /* args va_list */
1719 UINT64 arglist[102]; /* arguments fetched from va_list */
1722 static NTSTATUS add_chars( WCHAR **buffer, WCHAR *end, const WCHAR *str, ULONG len )
1724 if (len > end - *buffer) return STATUS_BUFFER_OVERFLOW;
1725 memcpy( *buffer, str, len * sizeof(WCHAR) );
1726 *buffer += len;
1727 return STATUS_SUCCESS;
1730 static UINT64 get_arg( int nr, struct format_message_args *args_data, BOOL is64 )
1732 if (nr == -1) nr = args_data->last + 1;
1733 while (nr > args_data->last)
1734 args_data->arglist[args_data->last++] = is64 ? va_arg( *args_data->list, UINT64 )
1735 : va_arg( *args_data->list, ULONG_PTR );
1736 return args_data->arglist[nr - 1];
1739 static NTSTATUS add_format( WCHAR **buffer, WCHAR *end, const WCHAR **src, int insert, BOOLEAN ansi,
1740 struct format_message_args *args_data )
1742 static const WCHAR modifiers[] = {'0','1','2','3','4','5','6','7','8','9',' ','+','-','*','#','.',0};
1743 const WCHAR *format = *src;
1744 WCHAR *p, fmt[32];
1745 ULONG_PTR args[5] = { 0 };
1746 BOOL is_64 = FALSE;
1747 UINT64 val;
1748 int len, stars = 0, nb_args = 0;
1750 p = fmt;
1751 *p++ = '%';
1753 if (*format++ == '!')
1755 const WCHAR *end = wcschr( format, '!' );
1757 if (!end || end - format > ARRAY_SIZE(fmt) - 2) return STATUS_INVALID_PARAMETER;
1758 *src = end + 1;
1760 while (wcschr( modifiers, *format ))
1762 if (*format == '*') stars++;
1763 *p++ = *format++;
1765 if (stars > 2) return STATUS_INVALID_PARAMETER;
1767 switch (*format)
1769 case 'c': case 'C':
1770 case 's': case 'S':
1771 if (ansi) *p++ = *format++ ^ ('s' - 'S');
1772 break;
1773 case 'I':
1774 if (sizeof(void *) == sizeof(int) && format[1] == '6' && format[2] == '4') is_64 = TRUE;
1775 break;
1777 while (format != end) *p++ = *format++;
1779 else *p++ = ansi ? 'S' : 's'; /* simple string */
1781 *p = 0;
1782 if (args_data->list)
1784 get_arg( insert - 1, args_data, is_64 ); /* make sure previous args have been fetched */
1785 while (stars--)
1787 args[nb_args++] = get_arg( insert, args_data, FALSE );
1788 insert = -1;
1790 /* replicate MS bug: drop an argument when using va_list with width/precision */
1791 if (insert == -1) args_data->last--;
1792 val = get_arg( insert, args_data, is_64 );
1793 args[nb_args++] = val;
1794 args[nb_args] = val >> 32;
1796 else if (args_data->array)
1798 args[nb_args++] = args_data->array[insert - 1];
1799 if (args_data->last < insert) args_data->last = insert;
1800 /* replicate MS bug: first arg is considered 64-bit, even if it's actually width or precision */
1801 if (is_64) nb_args++;
1802 while (stars--) args[nb_args++] = args_data->array[args_data->last++];
1804 else return STATUS_INVALID_PARAMETER;
1806 len = _snwprintf_s( *buffer, end - *buffer, end - *buffer - 1, fmt,
1807 args[0], args[1], args[2], args[3], args[4] );
1808 if (len == -1) return STATUS_BUFFER_OVERFLOW;
1809 *buffer += len;
1810 return STATUS_SUCCESS;
1814 /**********************************************************************
1815 * RtlFormatMessage (NTDLL.@)
1817 NTSTATUS WINAPI RtlFormatMessage( const WCHAR *src, ULONG width, BOOLEAN ignore_inserts,
1818 BOOLEAN ansi, BOOLEAN is_array, __ms_va_list *args,
1819 WCHAR *buffer, ULONG size, ULONG *retsize )
1821 return RtlFormatMessageEx( src, width, ignore_inserts, ansi, is_array, args, buffer, size, retsize, 0 );
1825 /**********************************************************************
1826 * RtlFormatMessageEx (NTDLL.@)
1828 NTSTATUS WINAPI RtlFormatMessageEx( const WCHAR *src, ULONG width, BOOLEAN ignore_inserts,
1829 BOOLEAN ansi, BOOLEAN is_array, __ms_va_list *args,
1830 WCHAR *buffer, ULONG size, ULONG *retsize, ULONG flags )
1832 static const WCHAR emptyW = 0;
1833 static const WCHAR spaceW = ' ';
1834 static const WCHAR crW = '\r';
1835 static const WCHAR tabW = '\t';
1836 static const WCHAR crlfW[] = {'\r','\n'};
1838 struct format_message_args args_data;
1839 NTSTATUS status = STATUS_SUCCESS;
1841 WCHAR *start = buffer; /* start of buffer */
1842 WCHAR *end = buffer + size / sizeof(WCHAR); /* end of buffer */
1843 WCHAR *line = buffer; /* start of last line */
1844 WCHAR *space = NULL; /* last space */
1846 if (flags) FIXME( "%s unknown flags %x\n", debugstr_w(src), flags );
1848 args_data.last = 0;
1849 args_data.array = is_array ? (ULONG_PTR *)args : NULL;
1850 args_data.list = is_array ? NULL : args;
1852 for ( ; *src; src++)
1854 switch (*src)
1856 case '\r':
1857 if (src[1] == '\n') src++;
1858 /* fall through */
1859 case '\n':
1860 if (!width)
1862 status = add_chars( &buffer, end, crlfW, 2 );
1863 line = buffer;
1864 space = NULL;
1865 break;
1867 /* fall through */
1868 case ' ':
1869 space = buffer;
1870 status = add_chars( &buffer, end, &spaceW, 1 );
1871 break;
1872 case '\t':
1873 if (space == buffer - 1) space = buffer;
1874 status = add_chars( &buffer, end, &tabW, 1 );
1875 break;
1876 case '%':
1877 src++;
1878 switch (*src)
1880 case 0:
1881 return STATUS_INVALID_PARAMETER;
1882 case 't':
1883 if (!width)
1885 status = add_chars( &buffer, end, &tabW, 1 );
1886 break;
1888 /* fall through */
1889 case 'n':
1890 status = add_chars( &buffer, end, crlfW, 2 );
1891 line = buffer;
1892 space = NULL;
1893 break;
1894 case 'r':
1895 status = add_chars( &buffer, end, &crW, 1 );
1896 line = buffer;
1897 space = NULL;
1898 break;
1899 case '0':
1900 while (src[1]) src++;
1901 break;
1902 case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1903 if (!ignore_inserts)
1905 int nr = *src++ - '0';
1907 if (*src >= '0' && *src <= '9') nr = nr * 10 + *src++ - '0';
1908 status = add_format( &buffer, end, &src, nr, ansi, &args_data );
1909 src--;
1910 break;
1912 /* fall through */
1913 default:
1914 if (ignore_inserts) status = add_chars( &buffer, end, src - 1, 2 );
1915 else status = add_chars( &buffer, end, src, 1 );
1916 break;
1918 break;
1919 default:
1920 status = add_chars( &buffer, end, src, 1 );
1921 break;
1924 if (status) return status;
1926 if (width && buffer - line >= width)
1928 LONG_PTR diff = 2;
1929 WCHAR *next;
1931 if (space) /* split line at the last space */
1933 next = space + 1;
1934 while (space > line && (space[-1] == ' ' || space[-1] == '\t')) space--;
1935 diff -= next - space;
1937 else space = next = buffer; /* split at the end of the buffer */
1939 if (diff > 0 && end - buffer < diff) return STATUS_BUFFER_OVERFLOW;
1940 memmove( space + 2, next, (buffer - next) * sizeof(WCHAR) );
1941 buffer += diff;
1942 memcpy( space, crlfW, sizeof(crlfW) );
1943 line = space + 2;
1944 space = NULL;
1948 if ((status = add_chars( &buffer, end, &emptyW, 1 ))) return status;
1950 *retsize = (buffer - start) * sizeof(WCHAR);
1951 return STATUS_SUCCESS;