ntdll: Reimplement Unicode to multibyte conversion functions using the Win32-format...
[wine.git] / dlls / ntdll / rtlstr.c
blob14fc1350dcc8541353e771f2cdeddd5fc7e66a73
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 "config.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winnt.h"
34 #include "winternl.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 #define GUID_STRING_LENGTH 38
44 /**************************************************************************
45 * __wine_init_codepages (NTDLL.@)
47 * Set the code page once kernel32 is loaded. Should be done differently.
49 void CDECL __wine_init_codepages( const union cptable *ansi, const union cptable *oem )
53 /**************************************************************************
54 * RtlInitAnsiString (NTDLL.@)
56 * Initializes a buffered ansi string.
58 * RETURNS
59 * Nothing.
61 * NOTES
62 * Assigns source to target->Buffer. The length of source is assigned to
63 * target->Length and target->MaximumLength. If source is NULL the length
64 * of source is assumed to be 0.
66 void WINAPI RtlInitAnsiString(
67 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
68 PCSZ source) /* [I] '\0' terminated string used to initialize target */
70 if ((target->Buffer = (PCHAR) source))
72 target->Length = strlen(source);
73 target->MaximumLength = target->Length + 1;
75 else target->Length = target->MaximumLength = 0;
78 /**************************************************************************
79 * RtlInitAnsiStringEx (NTDLL.@)
81 * Initializes a buffered ansi string.
83 * RETURNS
84 * An appropriate NTSTATUS value.
86 * NOTES
87 * Assigns source to target->Buffer. The length of source is assigned to
88 * target->Length and target->MaximumLength. If source is NULL the length
89 * of source is assumed to be 0.
91 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
93 if (source)
95 unsigned int len = strlen(source);
96 if (len+1 > 0xffff)
97 return STATUS_NAME_TOO_LONG;
99 target->Buffer = (PCHAR) source;
100 target->Length = len;
101 target->MaximumLength = len + 1;
103 else
105 target->Buffer = NULL;
106 target->Length = 0;
107 target->MaximumLength = 0;
109 return STATUS_SUCCESS;
112 /**************************************************************************
113 * RtlInitString (NTDLL.@)
115 * Initializes a buffered string.
117 * RETURNS
118 * Nothing.
120 * NOTES
121 * Assigns source to target->Buffer. The length of source is assigned to
122 * target->Length and target->MaximumLength. If source is NULL the length
123 * of source is assumed to be 0.
125 void WINAPI RtlInitString(
126 PSTRING target, /* [I/O] Buffered string to be initialized */
127 PCSZ source) /* [I] '\0' terminated string used to initialize target */
129 RtlInitAnsiString( target, source );
133 /**************************************************************************
134 * RtlFreeAnsiString (NTDLL.@)
136 void WINAPI RtlFreeAnsiString( PSTRING str )
138 if (str->Buffer)
140 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
141 RtlZeroMemory( str, sizeof(*str) );
146 /**************************************************************************
147 * RtlFreeOemString (NTDLL.@)
149 void WINAPI RtlFreeOemString( PSTRING str )
151 RtlFreeAnsiString( str );
155 /**************************************************************************
156 * RtlCopyString (NTDLL.@)
158 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
160 if (src)
162 unsigned int len = min( src->Length, dst->MaximumLength );
163 memcpy( dst->Buffer, src->Buffer, len );
164 dst->Length = len;
166 else dst->Length = 0;
170 /**************************************************************************
171 * RtlInitUnicodeString (NTDLL.@)
173 * Initializes a buffered unicode string.
175 * RETURNS
176 * Nothing.
178 * NOTES
179 * Assigns source to target->Buffer. The length of source is assigned to
180 * target->Length and target->MaximumLength. If source is NULL the length
181 * of source is assumed to be 0.
183 void WINAPI RtlInitUnicodeString(
184 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
185 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
187 if ((target->Buffer = (PWSTR) source))
189 unsigned int length = strlenW(source) * sizeof(WCHAR);
190 if (length > 0xfffc)
191 length = 0xfffc;
192 target->Length = length;
193 target->MaximumLength = target->Length + sizeof(WCHAR);
195 else target->Length = target->MaximumLength = 0;
199 /**************************************************************************
200 * RtlInitUnicodeStringEx (NTDLL.@)
202 * Initializes a buffered unicode string.
204 * RETURNS
205 * Success: STATUS_SUCCESS. target is initialized.
206 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
208 * NOTES
209 * Assigns source to target->Buffer. The length of source is assigned to
210 * target->Length and target->MaximumLength. If source is NULL the length
211 * of source is assumed to be 0.
213 NTSTATUS WINAPI RtlInitUnicodeStringEx(
214 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
215 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
217 if (source != NULL) {
218 unsigned int len = strlenW(source) * sizeof(WCHAR);
220 if (len > 0xFFFC) {
221 return STATUS_NAME_TOO_LONG;
222 } else {
223 target->Length = len;
224 target->MaximumLength = len + sizeof(WCHAR);
225 target->Buffer = (PWSTR) source;
226 } /* if */
227 } else {
228 target->Length = 0;
229 target->MaximumLength = 0;
230 target->Buffer = NULL;
231 } /* if */
232 return STATUS_SUCCESS;
236 /**************************************************************************
237 * RtlCreateUnicodeString (NTDLL.@)
239 * Creates a UNICODE_STRING from a null-terminated Unicode string.
241 * RETURNS
242 * Success: TRUE
243 * Failure: FALSE
245 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
247 int len = (strlenW(src) + 1) * sizeof(WCHAR);
248 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
249 memcpy( target->Buffer, src, len );
250 target->MaximumLength = len;
251 target->Length = len - sizeof(WCHAR);
252 return TRUE;
256 /**************************************************************************
257 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
259 * Creates a UNICODE_STRING from a null-terminated Ascii string.
261 * RETURNS
262 * Success: TRUE
263 * Failure: FALSE
265 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
267 STRING ansi;
268 RtlInitAnsiString( &ansi, src );
269 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
273 /**************************************************************************
274 * RtlFreeUnicodeString (NTDLL.@)
276 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
277 * RtlCreateUnicodeStringFromAsciiz().
279 * RETURNS
280 * nothing
282 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
284 if (str->Buffer)
286 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
287 RtlZeroMemory( str, sizeof(*str) );
292 /**************************************************************************
293 * RtlCopyUnicodeString (NTDLL.@)
295 * Copies from one UNICODE_STRING to another.
297 * RETURNS
298 * nothing
300 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
302 if (src)
304 unsigned int len = min( src->Length, dst->MaximumLength );
305 memcpy( dst->Buffer, src->Buffer, len );
306 dst->Length = len;
307 /* append terminating '\0' if enough space */
308 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
310 else dst->Length = 0;
314 /**************************************************************************
315 * RtlDuplicateUnicodeString (NTDLL.@)
317 * Duplicates a unicode string.
319 * RETURNS
320 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
321 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
322 * STATUS_NO_MEMORY, if the allocation fails.
324 * NOTES
325 * For add_nul there are several possible values:
326 * 0 = destination will not be '\0' terminated,
327 * 1 = destination will be '\0' terminated,
328 * 3 = like 1 but for an empty source string produce '\0' terminated empty
329 * Buffer instead of assigning NULL to the Buffer.
330 * Other add_nul values are invalid.
332 NTSTATUS WINAPI RtlDuplicateUnicodeString(
333 int add_nul, /* [I] flag */
334 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
335 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
337 if (source == NULL || destination == NULL ||
338 source->Length > source->MaximumLength ||
339 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
340 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
341 return STATUS_INVALID_PARAMETER;
342 } else {
343 if (source->Length == 0 && add_nul != 3) {
344 destination->Length = 0;
345 destination->MaximumLength = 0;
346 destination->Buffer = NULL;
347 } else {
348 unsigned int destination_max_len = source->Length;
350 if (add_nul) {
351 destination_max_len += sizeof(WCHAR);
352 } /* if */
353 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
354 if (destination->Buffer == NULL) {
355 return STATUS_NO_MEMORY;
356 } else {
357 memcpy(destination->Buffer, source->Buffer, source->Length);
358 destination->Length = source->Length;
359 destination->MaximumLength = source->Length;
360 /* append terminating '\0' if enough space */
361 if (add_nul) {
362 destination->MaximumLength = destination_max_len;
363 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
364 } /* if */
365 } /* if */
366 } /* if */
367 } /* if */
368 return STATUS_SUCCESS;
372 /**************************************************************************
373 * RtlEraseUnicodeString (NTDLL.@)
375 * Overwrites a UNICODE_STRING with zeros.
377 * RETURNS
378 * nothing
380 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
382 if (str->Buffer)
384 memset( str->Buffer, 0, str->MaximumLength );
385 str->Length = 0;
391 COMPARISON FUNCTIONS
395 /******************************************************************************
396 * RtlCompareString (NTDLL.@)
398 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
400 unsigned int len;
401 LONG ret = 0;
402 LPCSTR p1, p2;
404 len = min(s1->Length, s2->Length);
405 p1 = s1->Buffer;
406 p2 = s2->Buffer;
408 if (CaseInsensitive)
410 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
412 else
414 while (!ret && len--) ret = *p1++ - *p2++;
416 if (!ret) ret = s1->Length - s2->Length;
417 return ret;
421 /******************************************************************************
422 * RtlCompareUnicodeStrings (NTDLL.@)
424 LONG WINAPI RtlCompareUnicodeStrings( const WCHAR *s1, SIZE_T len1, const WCHAR *s2, SIZE_T len2,
425 BOOLEAN case_insensitive )
427 LONG ret = 0;
428 SIZE_T len = min( len1, len2 );
430 if (case_insensitive)
432 while (!ret && len--) ret = toupperW(*s1++) - toupperW(*s2++);
434 else
436 while (!ret && len--) ret = *s1++ - *s2++;
438 if (!ret) ret = len1 - len2;
439 return ret;
443 /******************************************************************************
444 * RtlCompareUnicodeString (NTDLL.@)
446 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
447 BOOLEAN CaseInsensitive )
449 return RtlCompareUnicodeStrings( s1->Buffer, s1->Length / sizeof(WCHAR),
450 s2->Buffer, s2->Length / sizeof(WCHAR), CaseInsensitive );
454 /**************************************************************************
455 * RtlEqualString (NTDLL.@)
457 * Determine if two strings are equal.
459 * PARAMS
460 * s1 [I] Source string
461 * s2 [I] String to compare to s1
462 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
464 * RETURNS
465 * Non-zero if s1 is equal to s2, 0 otherwise.
467 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
469 if (s1->Length != s2->Length) return FALSE;
470 return !RtlCompareString( s1, s2, CaseInsensitive );
474 /**************************************************************************
475 * RtlEqualUnicodeString (NTDLL.@)
477 * Unicode version of RtlEqualString.
479 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
480 BOOLEAN CaseInsensitive )
482 if (s1->Length != s2->Length) return FALSE;
483 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
487 /**************************************************************************
488 * RtlPrefixString (NTDLL.@)
490 * Determine if one string is a prefix of another.
492 * PARAMS
493 * s1 [I] Prefix to look for in s2
494 * s2 [I] String that may contain s1 as a prefix
495 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
497 * RETURNS
498 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
500 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
502 unsigned int i;
504 if (s1->Length > s2->Length) return FALSE;
505 if (ignore_case)
507 for (i = 0; i < s1->Length; i++)
508 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
510 else
512 for (i = 0; i < s1->Length; i++)
513 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
515 return TRUE;
519 /**************************************************************************
520 * RtlPrefixUnicodeString (NTDLL.@)
522 * Unicode version of RtlPrefixString.
524 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
525 const UNICODE_STRING *s2,
526 BOOLEAN ignore_case )
528 unsigned int i;
530 if (s1->Length > s2->Length) return FALSE;
531 if (ignore_case)
533 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
534 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
536 else
538 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
539 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
541 return TRUE;
545 /**************************************************************************
546 * RtlEqualComputerName (NTDLL.@)
548 * Determine if two computer names are the same.
550 * PARAMS
551 * left [I] First computer name
552 * right [I] Second computer name
554 * RETURNS
555 * 0 if the names are equal, non-zero otherwise.
557 * NOTES
558 * The comparison is case insensitive.
560 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
561 const UNICODE_STRING *right)
563 NTSTATUS ret;
564 STRING upLeft, upRight;
566 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
568 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
570 ret = RtlEqualString( &upLeft, &upRight, FALSE );
571 RtlFreeOemString( &upRight );
573 RtlFreeOemString( &upLeft );
575 return ret;
579 /**************************************************************************
580 * RtlEqualDomainName (NTDLL.@)
582 * Determine if two domain names are the same.
584 * PARAMS
585 * left [I] First domain name
586 * right [I] Second domain name
588 * RETURNS
589 * 0 if the names are equal, non-zero otherwise.
591 * NOTES
592 * The comparison is case insensitive.
594 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
595 const UNICODE_STRING *right)
597 return RtlEqualComputerName(left, right);
602 COPY BETWEEN ANSI_STRING or UNICODE_STRING
603 there is no parameter checking, it just crashes
607 /**************************************************************************
608 * RtlAnsiStringToUnicodeString (NTDLL.@)
610 * Converts an ansi string to a unicode string.
612 * RETURNS
613 * Success: STATUS_SUCCESS. uni contains the converted string
614 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
615 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
616 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
618 * NOTES
619 * This function always writes a terminating '\0'.
621 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
622 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
623 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
624 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
626 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
628 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
629 uni->Length = total - sizeof(WCHAR);
630 if (doalloc)
632 uni->MaximumLength = total;
633 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
634 return STATUS_NO_MEMORY;
636 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
638 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
639 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
640 return STATUS_SUCCESS;
644 /**************************************************************************
645 * RtlOemStringToUnicodeString (NTDLL.@)
647 * Converts an oem string to a unicode string.
649 * RETURNS
650 * Success: STATUS_SUCCESS. uni contains the converted string
651 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
652 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
653 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
655 * NOTES
656 * This function always writes a terminating '\0'.
658 NTSTATUS WINAPI RtlOemStringToUnicodeString(
659 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
660 const STRING *oem, /* [I] Oem string to be converted */
661 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
663 DWORD total = RtlOemStringToUnicodeSize( oem );
665 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
666 uni->Length = total - sizeof(WCHAR);
667 if (doalloc)
669 uni->MaximumLength = total;
670 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
671 return STATUS_NO_MEMORY;
673 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
675 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
676 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
677 return STATUS_SUCCESS;
681 /**************************************************************************
682 * RtlUnicodeStringToAnsiString (NTDLL.@)
684 * Converts a unicode string to an ansi string.
686 * RETURNS
687 * Success: STATUS_SUCCESS. ansi contains the converted string
688 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
689 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
691 * NOTES
692 * This function always writes a terminating '\0'.
693 * It performs a partial copy if ansi is too small.
695 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
696 STRING *ansi, /* [I/O] Destination for the ansi string */
697 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
698 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
700 NTSTATUS ret = STATUS_SUCCESS;
701 DWORD len = RtlUnicodeStringToAnsiSize( uni );
703 ansi->Length = len - 1;
704 if (doalloc)
706 ansi->MaximumLength = len;
707 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
708 return STATUS_NO_MEMORY;
710 else if (ansi->MaximumLength < len)
712 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
713 ansi->Length = ansi->MaximumLength - 1;
714 ret = STATUS_BUFFER_OVERFLOW;
717 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
718 ansi->Buffer[ansi->Length] = 0;
719 return ret;
723 /**************************************************************************
724 * RtlUnicodeStringToOemString (NTDLL.@)
726 * Converts a Rtl Unicode string to an OEM string.
728 * PARAMS
729 * oem [O] Destination for OEM string
730 * uni [I] Source Unicode string
731 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
733 * RETURNS
734 * Success: STATUS_SUCCESS. oem contains the converted string
735 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
736 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
738 * NOTES
739 * If doalloc is TRUE, the length allocated is uni->Length + 1.
740 * This function always '\0' terminates the string returned.
742 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
743 const UNICODE_STRING *uni,
744 BOOLEAN doalloc )
746 NTSTATUS ret = STATUS_SUCCESS;
747 DWORD len = RtlUnicodeStringToOemSize( uni );
749 oem->Length = len - 1;
750 if (doalloc)
752 oem->MaximumLength = len;
753 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
754 return STATUS_NO_MEMORY;
756 else if (oem->MaximumLength < len)
758 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
759 oem->Length = oem->MaximumLength - 1;
760 ret = STATUS_BUFFER_OVERFLOW;
763 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
764 oem->Buffer[oem->Length] = 0;
765 return ret;
769 /**************************************************************************
770 * RtlUnicodeToUTF8N (NTDLL.@)
772 * Converts a Unicode string to a UTF-8 string.
774 * RETURNS
775 * NTSTATUS code
777 NTSTATUS WINAPI RtlUnicodeToUTF8N( LPSTR dst, DWORD dstlen, LPDWORD reslen,
778 LPCWSTR src, DWORD srclen)
780 int ret;
782 if (!src) return STATUS_INVALID_PARAMETER_4;
783 if (!reslen) return STATUS_INVALID_PARAMETER;
784 if (dst && (srclen & 1)) return STATUS_INVALID_PARAMETER_5;
786 if (!dstlen && dst)
788 char c;
789 dst = &c;
790 ret = wine_utf8_wcstombs( 0, src, srclen / sizeof(WCHAR), dst, 1 );
791 if (ret > 0) ret--;
793 else
794 ret = wine_utf8_wcstombs( 0, src, srclen / sizeof(WCHAR), dst, dstlen );
795 if (reslen)
796 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
797 if (ret < 0) return STATUS_BUFFER_TOO_SMALL;
798 return STATUS_SUCCESS;
803 CASE CONVERSIONS
807 /**************************************************************************
808 * RtlUpperChar (NTDLL.@)
810 * Converts an Ascii character to uppercase.
812 * PARAMS
813 * ch [I] Character to convert
815 * RETURNS
816 * The uppercase character value.
818 * NOTES
819 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
820 * All other input characters are returned unchanged. The locale and
821 * multibyte characters are not taken into account (as native DLL).
823 CHAR WINAPI RtlUpperChar( CHAR ch )
825 if (ch >= 'a' && ch <= 'z') {
826 return ch - 'a' + 'A';
827 } else {
828 return ch;
829 } /* if */
833 /**************************************************************************
834 * RtlUpperString (NTDLL.@)
836 * Converts an Ascii string to uppercase.
838 * PARAMS
839 * dst [O] Destination for converted string
840 * src [I] Source string to convert
842 * RETURNS
843 * Nothing.
845 * NOTES
846 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
847 * All other src characters are copied unchanged to dst. The locale and
848 * multibyte characters are not taken into account (as native DLL).
849 * The number of character copied is the minimum of src->Length and
850 * the dst->MaximumLength.
852 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
854 unsigned int i, len = min(src->Length, dst->MaximumLength);
856 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
857 dst->Length = len;
861 /**************************************************************************
862 * RtlUpcaseUnicodeChar (NTDLL.@)
864 * Converts a Unicode character to uppercase.
866 * PARAMS
867 * wch [I] Character to convert
869 * RETURNS
870 * The uppercase character value.
872 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
874 return toupperW(wch);
878 /**************************************************************************
879 * RtlDowncaseUnicodeChar (NTDLL.@)
881 * Converts a Unicode character to lowercase.
883 * PARAMS
884 * wch [I] Character to convert
886 * RETURNS
887 * The lowercase character value.
889 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
891 return tolowerW(wch);
895 /**************************************************************************
896 * RtlUpcaseUnicodeString (NTDLL.@)
898 * Converts a Unicode string to uppercase.
900 * PARAMS
901 * dest [O] Destination for converted string
902 * src [I] Source string to convert
903 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
905 * RETURNS
906 * Success: STATUS_SUCCESS. dest contains the converted string.
907 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
908 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
910 * NOTES
911 * dest is never '\0' terminated because it may be equal to src, and src
912 * might not be '\0' terminated. dest->Length is only set upon success.
914 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
915 const UNICODE_STRING *src,
916 BOOLEAN doalloc)
918 DWORD i, len = src->Length;
920 if (doalloc)
922 dest->MaximumLength = len;
923 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
924 return STATUS_NO_MEMORY;
926 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
928 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
929 dest->Length = len;
930 return STATUS_SUCCESS;
934 /**************************************************************************
935 * RtlDowncaseUnicodeString (NTDLL.@)
937 * Converts a Unicode string to lowercase.
939 * PARAMS
940 * dest [O] Destination for converted string
941 * src [I] Source string to convert
942 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
944 * RETURNS
945 * Success: STATUS_SUCCESS. dest contains the converted string.
946 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
947 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
949 * NOTES
950 * dest is never '\0' terminated because it may be equal to src, and src
951 * might not be '\0' terminated. dest->Length is only set upon success.
953 NTSTATUS WINAPI RtlDowncaseUnicodeString(
954 UNICODE_STRING *dest,
955 const UNICODE_STRING *src,
956 BOOLEAN doalloc)
958 DWORD i;
959 DWORD len = src->Length;
961 if (doalloc) {
962 dest->MaximumLength = len;
963 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
964 return STATUS_NO_MEMORY;
965 } /* if */
966 } else if (len > dest->MaximumLength) {
967 return STATUS_BUFFER_OVERFLOW;
968 } /* if */
970 for (i = 0; i < len/sizeof(WCHAR); i++) {
971 dest->Buffer[i] = tolowerW(src->Buffer[i]);
972 } /* for */
973 dest->Length = len;
974 return STATUS_SUCCESS;
978 /**************************************************************************
979 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
981 * Converts a Unicode string to the equivalent ANSI upper-case representation.
983 * RETURNS
984 * NTSTATUS code
986 * NOTES
987 * writes terminating 0
989 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *ansi,
990 const UNICODE_STRING *uni,
991 BOOLEAN doalloc )
993 NTSTATUS ret = STATUS_SUCCESS;
994 DWORD len = RtlUnicodeStringToAnsiSize( uni );
996 ansi->Length = len - 1;
997 if (doalloc)
999 ansi->MaximumLength = len;
1000 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1001 return STATUS_NO_MEMORY;
1003 else if (ansi->MaximumLength < len)
1005 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1006 ansi->Length = ansi->MaximumLength - 1;
1007 ret = STATUS_BUFFER_OVERFLOW;
1010 RtlUpcaseUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
1011 ansi->Buffer[ansi->Length] = 0;
1012 return ret;
1016 /**************************************************************************
1017 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1019 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1020 * stored in STRING format.
1022 * RETURNS
1023 * NTSTATUS code
1025 * NOTES
1026 * writes terminating 0
1028 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *oem,
1029 const UNICODE_STRING *uni,
1030 BOOLEAN doalloc )
1032 NTSTATUS ret = STATUS_SUCCESS;
1033 DWORD len = RtlUnicodeStringToAnsiSize( uni );
1035 oem->Length = len - 1;
1036 if (doalloc)
1038 oem->MaximumLength = len;
1039 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1040 return STATUS_NO_MEMORY;
1042 else if (oem->MaximumLength < len)
1044 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1045 oem->Length = oem->MaximumLength - 1;
1046 ret = STATUS_BUFFER_OVERFLOW;
1049 RtlUpcaseUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
1050 oem->Buffer[oem->Length] = 0;
1051 return ret;
1055 /**************************************************************************
1056 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1058 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1059 * stored in STRING format.
1061 * RETURNS
1062 * NTSTATUS code
1064 * NOTES
1065 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1067 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1068 const UNICODE_STRING *uni,
1069 BOOLEAN doalloc )
1071 NTSTATUS ret = STATUS_SUCCESS;
1072 DWORD len = RtlUnicodeStringToOemSize( uni ) - 1;
1074 oem->Length = len;
1075 if (doalloc)
1077 oem->MaximumLength = len;
1078 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return STATUS_NO_MEMORY;
1080 else if (oem->MaximumLength < len)
1082 ret = STATUS_BUFFER_OVERFLOW;
1083 oem->Length = oem->MaximumLength;
1084 if (!oem->MaximumLength) return ret;
1086 RtlUpcaseUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
1087 return ret;
1091 /**************************************************************************
1092 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1094 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1096 * RETURNS
1097 * NTSTATUS code
1099 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1100 LPCWSTR src, DWORD srclen )
1102 NTSTATUS ret;
1103 LPWSTR upcase;
1104 DWORD i;
1106 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1107 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1108 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1109 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1110 return ret;
1114 /**************************************************************************
1115 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1117 * Converts a Unicode string to the equivalent OEM upper-case representation.
1119 * RETURNS
1120 * NTSTATUS code
1122 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1123 LPCWSTR src, DWORD srclen )
1125 NTSTATUS ret;
1126 LPWSTR upcase;
1127 DWORD i;
1129 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1130 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1131 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1132 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1133 return ret;
1138 STRING SIZE
1142 /**************************************************************************
1143 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1144 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1146 * Calculate the size in bytes necessary for the Unicode conversion of str,
1147 * including the terminating '\0'.
1149 * PARAMS
1150 * str [I] String to calculate the size of
1152 * RETURNS
1153 * The calculated size.
1155 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1157 DWORD ret;
1158 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1159 return ret + sizeof(WCHAR);
1163 /**************************************************************************
1164 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1165 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1167 * Calculate the size in bytes necessary for the Ansi conversion of str,
1168 * including the terminating '\0'.
1170 * PARAMS
1171 * str [I] String to calculate the size of
1173 * RETURNS
1174 * The calculated size.
1176 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1178 DWORD ret;
1179 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1180 return ret + 1;
1184 /**************************************************************************
1185 * RtlAppendAsciizToString (NTDLL.@)
1187 * Concatenates a buffered character string and a '\0' terminated character
1188 * string
1190 * RETURNS
1191 * Success: STATUS_SUCCESS. src is appended to dest.
1192 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1193 * to hold the concatenated string.
1195 * NOTES
1196 * if src is NULL dest is unchanged.
1197 * dest is never '\0' terminated.
1199 NTSTATUS WINAPI RtlAppendAsciizToString(
1200 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1201 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1203 if (src != NULL) {
1204 unsigned int src_len = strlen(src);
1205 unsigned int dest_len = src_len + dest->Length;
1207 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1208 memcpy(dest->Buffer + dest->Length, src, src_len);
1209 dest->Length = dest_len;
1210 } /* if */
1211 return STATUS_SUCCESS;
1215 /**************************************************************************
1216 * RtlAppendStringToString (NTDLL.@)
1218 * Concatenates two buffered character strings
1220 * RETURNS
1221 * Success: STATUS_SUCCESS. src is appended to dest.
1222 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1223 * to hold the concatenated string.
1225 * NOTES
1226 * if src->length is zero dest is unchanged.
1227 * dest is never '\0' terminated.
1229 NTSTATUS WINAPI RtlAppendStringToString(
1230 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1231 const STRING *src) /* [I] Buffered character string to be concatenated */
1233 if (src->Length != 0) {
1234 unsigned int dest_len = src->Length + dest->Length;
1236 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1237 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1238 dest->Length = dest_len;
1239 } /* if */
1240 return STATUS_SUCCESS;
1244 /**************************************************************************
1245 * RtlAppendUnicodeToString (NTDLL.@)
1247 * Concatenates a buffered unicode string and a '\0' terminated unicode
1248 * string
1250 * RETURNS
1251 * Success: STATUS_SUCCESS. src is appended to dest.
1252 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1253 * to hold the concatenated string.
1255 * NOTES
1256 * if src is NULL dest is unchanged.
1257 * dest is '\0' terminated when the MaximumLength allows it.
1258 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1260 * DIFFERENCES
1261 * Does not write in the src->Buffer beyond MaximumLength when
1262 * MaximumLength is odd as the native function does.
1264 NTSTATUS WINAPI RtlAppendUnicodeToString(
1265 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1266 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1268 if (src != NULL) {
1269 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1270 unsigned int dest_len = src_len + dest->Length;
1272 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1273 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1274 dest->Length = dest_len;
1275 /* append terminating '\0' if enough space */
1276 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1277 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1278 } /* if */
1279 } /* if */
1280 return STATUS_SUCCESS;
1284 /**************************************************************************
1285 * RtlAppendUnicodeStringToString (NTDLL.@)
1287 * Concatenates two buffered unicode strings
1289 * RETURNS
1290 * Success: STATUS_SUCCESS. src is appended to dest.
1291 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1292 * to hold the concatenated string.
1294 * NOTES
1295 * if src->length is zero dest is unchanged.
1296 * dest is '\0' terminated when the MaximumLength allows it.
1297 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1299 * DIFFERENCES
1300 * Does not write in the src->Buffer beyond MaximumLength when
1301 * MaximumLength is odd as the native function does.
1303 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1304 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1305 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1307 if (src->Length != 0) {
1308 unsigned int dest_len = src->Length + dest->Length;
1310 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1311 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1312 dest->Length = dest_len;
1313 /* append terminating '\0' if enough space */
1314 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1315 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1316 } /* if */
1317 } /* if */
1318 return STATUS_SUCCESS;
1322 /**************************************************************************
1323 * RtlFindCharInUnicodeString (NTDLL.@)
1325 * Searches for one of several unicode characters in a unicode string.
1327 * RETURNS
1328 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1329 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1331 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1332 int flags, /* [I] Flags */
1333 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1334 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1335 USHORT *pos) /* [O] Position of the first character found + 2 */
1337 unsigned int main_idx, search_idx;
1339 switch (flags) {
1340 case 0:
1341 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1342 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1343 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1344 *pos = (main_idx + 1) * sizeof(WCHAR);
1345 return STATUS_SUCCESS;
1349 *pos = 0;
1350 return STATUS_NOT_FOUND;
1351 case 1:
1352 main_idx = main_str->Length / sizeof(WCHAR);
1353 while (main_idx-- > 0) {
1354 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1355 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1356 *pos = main_idx * sizeof(WCHAR);
1357 return STATUS_SUCCESS;
1361 *pos = 0;
1362 return STATUS_NOT_FOUND;
1363 case 2:
1364 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1365 search_idx = 0;
1366 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1367 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1368 search_idx++;
1370 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1371 *pos = (main_idx + 1) * sizeof(WCHAR);
1372 return STATUS_SUCCESS;
1375 *pos = 0;
1376 return STATUS_NOT_FOUND;
1377 case 3:
1378 main_idx = main_str->Length / sizeof(WCHAR);
1379 while (main_idx-- > 0) {
1380 search_idx = 0;
1381 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1382 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1383 search_idx++;
1385 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1386 *pos = main_idx * sizeof(WCHAR);
1387 return STATUS_SUCCESS;
1390 *pos = 0;
1391 return STATUS_NOT_FOUND;
1392 } /* switch */
1393 return STATUS_NOT_FOUND;
1398 MISC
1401 /**************************************************************************
1402 * RtlIsTextUnicode (NTDLL.@)
1404 * Attempt to guess whether a text buffer is Unicode.
1406 * PARAMS
1407 * buf [I] Text buffer to test
1408 * len [I] Length of buf
1409 * pf [O] Destination for test results
1411 * RETURNS
1412 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1414 * FIXME
1415 * Should implement more tests.
1417 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1419 static const WCHAR std_control_chars[] = {'\r','\n','\t',' ',0x3000,0};
1420 static const WCHAR byterev_control_chars[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1421 const WCHAR *s = buf;
1422 int i;
1423 unsigned int flags = ~0U, out_flags = 0;
1425 if (len < sizeof(WCHAR))
1427 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1428 if (pf) *pf = 0;
1429 return FALSE;
1431 if (pf)
1432 flags = *pf;
1434 * Apply various tests to the text string. According to the
1435 * docs, each test "passed" sets the corresponding flag in
1436 * the output flags. But some of the tests are mutually
1437 * exclusive, so I don't see how you could pass all tests ...
1440 /* Check for an odd length ... pass if even. */
1441 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1443 if (((const char *)buf)[len - 1] == 0)
1444 len--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1446 len /= sizeof(WCHAR);
1447 /* Windows only checks the first 256 characters */
1448 if (len > 256) len = 256;
1450 /* Check for the special byte order unicode marks. */
1451 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1452 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1454 /* apply some statistical analysis */
1455 if (flags & IS_TEXT_UNICODE_STATISTICS)
1457 int stats = 0;
1458 /* FIXME: checks only for ASCII characters in the unicode stream */
1459 for (i = 0; i < len; i++)
1461 if (s[i] <= 255) stats++;
1463 if (stats > len / 2)
1464 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1467 /* Check for unicode NULL chars */
1468 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1470 for (i = 0; i < len; i++)
1472 if (!(s[i] & 0xff) || !(s[i] >> 8))
1474 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1475 break;
1480 if (flags & IS_TEXT_UNICODE_CONTROLS)
1482 for (i = 0; i < len; i++)
1484 if (strchrW(std_control_chars, s[i]))
1486 out_flags |= IS_TEXT_UNICODE_CONTROLS;
1487 break;
1492 if (flags & IS_TEXT_UNICODE_REVERSE_CONTROLS)
1494 for (i = 0; i < len; i++)
1496 if (strchrW(byterev_control_chars, s[i]))
1498 out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS;
1499 break;
1504 if (pf)
1506 out_flags &= *pf;
1507 *pf = out_flags;
1509 /* check for flags that indicate it's definitely not valid Unicode */
1510 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1511 /* now check for invalid ASCII, and assume Unicode if so */
1512 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1513 /* now check for Unicode flags */
1514 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1515 /* no flags set */
1516 return FALSE;
1520 /**************************************************************************
1521 * RtlCharToInteger (NTDLL.@)
1523 * Converts a character string into its integer equivalent.
1525 * RETURNS
1526 * Success: STATUS_SUCCESS. value contains the converted number
1527 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1528 * STATUS_ACCESS_VIOLATION, if value is NULL.
1530 * NOTES
1531 * For base 0 it uses 10 as base and the string should be in the format
1532 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1533 * For other bases the string should be in the format
1534 * "{whitespace} [+|-] {digits}".
1535 * No check is made for value overflow, only the lower 32 bits are assigned.
1536 * If str is NULL it crashes, as the native function does.
1538 * DIFFERENCES
1539 * This function does not read garbage behind '\0' as the native version does.
1541 NTSTATUS WINAPI RtlCharToInteger(
1542 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1543 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1544 ULONG *value) /* [O] Destination for the converted value */
1546 CHAR chCurrent;
1547 int digit;
1548 ULONG RunningTotal = 0;
1549 BOOL bMinus = FALSE;
1551 while (*str != '\0' && *str <= ' ') {
1552 str++;
1553 } /* while */
1555 if (*str == '+') {
1556 str++;
1557 } else if (*str == '-') {
1558 bMinus = TRUE;
1559 str++;
1560 } /* if */
1562 if (base == 0) {
1563 base = 10;
1564 if (str[0] == '0') {
1565 if (str[1] == 'b') {
1566 str += 2;
1567 base = 2;
1568 } else if (str[1] == 'o') {
1569 str += 2;
1570 base = 8;
1571 } else if (str[1] == 'x') {
1572 str += 2;
1573 base = 16;
1574 } /* if */
1575 } /* if */
1576 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1577 return STATUS_INVALID_PARAMETER;
1578 } /* if */
1580 if (value == NULL) {
1581 return STATUS_ACCESS_VIOLATION;
1582 } /* if */
1584 while (*str != '\0') {
1585 chCurrent = *str;
1586 if (chCurrent >= '0' && chCurrent <= '9') {
1587 digit = chCurrent - '0';
1588 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1589 digit = chCurrent - 'A' + 10;
1590 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1591 digit = chCurrent - 'a' + 10;
1592 } else {
1593 digit = -1;
1594 } /* if */
1595 if (digit < 0 || digit >= base) {
1596 *value = bMinus ? -RunningTotal : RunningTotal;
1597 return STATUS_SUCCESS;
1598 } /* if */
1600 RunningTotal = RunningTotal * base + digit;
1601 str++;
1602 } /* while */
1604 *value = bMinus ? -RunningTotal : RunningTotal;
1605 return STATUS_SUCCESS;
1609 /**************************************************************************
1610 * RtlIntegerToChar (NTDLL.@)
1612 * Converts an unsigned integer to a character string.
1614 * RETURNS
1615 * Success: STATUS_SUCCESS. str contains the converted number
1616 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1617 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1618 * STATUS_ACCESS_VIOLATION, if str is NULL.
1620 * NOTES
1621 * Instead of base 0 it uses 10 as base.
1622 * Writes at most length characters to the string str.
1623 * Str is '\0' terminated when length allows it.
1624 * When str fits exactly in length characters the '\0' is omitted.
1626 NTSTATUS WINAPI RtlIntegerToChar(
1627 ULONG value, /* [I] Value to be converted */
1628 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1629 ULONG length, /* [I] Length of the str buffer in bytes */
1630 PCHAR str) /* [O] Destination for the converted value */
1632 CHAR buffer[33];
1633 PCHAR pos;
1634 CHAR digit;
1635 ULONG len;
1637 if (base == 0) {
1638 base = 10;
1639 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1640 return STATUS_INVALID_PARAMETER;
1641 } /* if */
1643 pos = &buffer[32];
1644 *pos = '\0';
1646 do {
1647 pos--;
1648 digit = value % base;
1649 value = value / base;
1650 if (digit < 10) {
1651 *pos = '0' + digit;
1652 } else {
1653 *pos = 'A' + digit - 10;
1654 } /* if */
1655 } while (value != 0L);
1657 len = &buffer[32] - pos;
1658 if (len > length) {
1659 return STATUS_BUFFER_OVERFLOW;
1660 } else if (str == NULL) {
1661 return STATUS_ACCESS_VIOLATION;
1662 } else if (len == length) {
1663 memcpy(str, pos, len);
1664 } else {
1665 memcpy(str, pos, len + 1);
1666 } /* if */
1667 return STATUS_SUCCESS;
1671 /**************************************************************************
1672 * RtlUnicodeStringToInteger (NTDLL.@)
1674 * Converts a unicode string into its integer equivalent.
1676 * RETURNS
1677 * Success: STATUS_SUCCESS. value contains the converted number
1678 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1679 * STATUS_ACCESS_VIOLATION, if value is NULL.
1681 * NOTES
1682 * For base 0 it uses 10 as base and the string should be in the format
1683 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1684 * For other bases the string should be in the format
1685 * "{whitespace} [+|-] {digits}".
1686 * No check is made for value overflow, only the lower 32 bits are assigned.
1687 * If str is NULL it crashes, as the native function does.
1689 * DIFFERENCES
1690 * This function does not read garbage on string length 0 as the native
1691 * version does.
1693 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1694 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1695 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1696 ULONG *value) /* [O] Destination for the converted value */
1698 LPWSTR lpwstr = str->Buffer;
1699 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1700 WCHAR wchCurrent;
1701 int digit;
1702 ULONG RunningTotal = 0;
1703 BOOL bMinus = FALSE;
1705 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1706 lpwstr++;
1707 CharsRemaining--;
1708 } /* while */
1710 if (CharsRemaining >= 1) {
1711 if (*lpwstr == '+') {
1712 lpwstr++;
1713 CharsRemaining--;
1714 } else if (*lpwstr == '-') {
1715 bMinus = TRUE;
1716 lpwstr++;
1717 CharsRemaining--;
1718 } /* if */
1719 } /* if */
1721 if (base == 0) {
1722 base = 10;
1723 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1724 if (lpwstr[1] == 'b') {
1725 lpwstr += 2;
1726 CharsRemaining -= 2;
1727 base = 2;
1728 } else if (lpwstr[1] == 'o') {
1729 lpwstr += 2;
1730 CharsRemaining -= 2;
1731 base = 8;
1732 } else if (lpwstr[1] == 'x') {
1733 lpwstr += 2;
1734 CharsRemaining -= 2;
1735 base = 16;
1736 } /* if */
1737 } /* if */
1738 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1739 return STATUS_INVALID_PARAMETER;
1740 } /* if */
1742 if (value == NULL) {
1743 return STATUS_ACCESS_VIOLATION;
1744 } /* if */
1746 while (CharsRemaining >= 1) {
1747 wchCurrent = *lpwstr;
1748 if (wchCurrent >= '0' && wchCurrent <= '9') {
1749 digit = wchCurrent - '0';
1750 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1751 digit = wchCurrent - 'A' + 10;
1752 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1753 digit = wchCurrent - 'a' + 10;
1754 } else {
1755 digit = -1;
1756 } /* if */
1757 if (digit < 0 || digit >= base) {
1758 *value = bMinus ? -RunningTotal : RunningTotal;
1759 return STATUS_SUCCESS;
1760 } /* if */
1762 RunningTotal = RunningTotal * base + digit;
1763 lpwstr++;
1764 CharsRemaining--;
1765 } /* while */
1767 *value = bMinus ? -RunningTotal : RunningTotal;
1768 return STATUS_SUCCESS;
1772 /**************************************************************************
1773 * RtlIntegerToUnicodeString (NTDLL.@)
1775 * Converts an unsigned integer to a '\0' terminated unicode string.
1777 * RETURNS
1778 * Success: STATUS_SUCCESS. str contains the converted number
1779 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1780 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1781 * (with the '\0' termination). In this case str->Length
1782 * is set to the length, the string would have (which can
1783 * be larger than the MaximumLength).
1785 * NOTES
1786 * Instead of base 0 it uses 10 as base.
1787 * If str is NULL it crashes, as the native function does.
1789 * DIFFERENCES
1790 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1791 * The native function does this when the string would be longer than 16
1792 * characters even when the string parameter is long enough.
1794 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1795 ULONG value, /* [I] Value to be converted */
1796 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1797 UNICODE_STRING *str) /* [O] Destination for the converted value */
1799 WCHAR buffer[33];
1800 PWCHAR pos;
1801 WCHAR digit;
1803 if (base == 0) {
1804 base = 10;
1805 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1806 return STATUS_INVALID_PARAMETER;
1807 } /* if */
1809 pos = &buffer[32];
1810 *pos = '\0';
1812 do {
1813 pos--;
1814 digit = value % base;
1815 value = value / base;
1816 if (digit < 10) {
1817 *pos = '0' + digit;
1818 } else {
1819 *pos = 'A' + digit - 10;
1820 } /* if */
1821 } while (value != 0L);
1823 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1824 if (str->Length >= str->MaximumLength) {
1825 return STATUS_BUFFER_OVERFLOW;
1826 } else {
1827 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1828 } /* if */
1829 return STATUS_SUCCESS;
1833 /*************************************************************************
1834 * RtlGUIDFromString (NTDLL.@)
1836 * Convert a string representation of a GUID into a GUID.
1838 * PARAMS
1839 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1840 * guid [O] Destination for the converted GUID
1842 * RETURNS
1843 * Success: STATUS_SUCCESS. guid contains the converted value.
1844 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1846 * SEE ALSO
1847 * See RtlStringFromGUID.
1849 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
1851 int i = 0;
1852 const WCHAR *lpszCLSID = str->Buffer;
1853 BYTE* lpOut = (BYTE*)guid;
1855 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1857 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1858 * to memory: DWORD... WORD WORD BYTES............
1860 while (i <= 37)
1862 switch (i)
1864 case 0:
1865 if (*lpszCLSID != '{')
1866 return STATUS_INVALID_PARAMETER;
1867 break;
1869 case 9: case 14: case 19: case 24:
1870 if (*lpszCLSID != '-')
1871 return STATUS_INVALID_PARAMETER;
1872 break;
1874 case 37:
1875 if (*lpszCLSID != '}')
1876 return STATUS_INVALID_PARAMETER;
1877 break;
1879 default:
1881 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1882 unsigned char byte;
1884 /* Read two hex digits as a byte value */
1885 if (ch >= '0' && ch <= '9') ch = ch - '0';
1886 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1887 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1888 else return STATUS_INVALID_PARAMETER;
1890 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1891 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1892 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1893 else return STATUS_INVALID_PARAMETER;
1895 byte = ch << 4 | ch2;
1897 switch (i)
1899 #ifndef WORDS_BIGENDIAN
1900 /* For Big Endian machines, we store the data such that the
1901 * dword/word members can be read as DWORDS and WORDS correctly. */
1902 /* Dword */
1903 case 1: lpOut[3] = byte; break;
1904 case 3: lpOut[2] = byte; break;
1905 case 5: lpOut[1] = byte; break;
1906 case 7: lpOut[0] = byte; lpOut += 4; break;
1907 /* Word */
1908 case 10: case 15: lpOut[1] = byte; break;
1909 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1910 #endif
1911 /* Byte */
1912 default: lpOut[0] = byte; lpOut++; break;
1914 lpszCLSID++; /* Skip 2nd character of byte */
1915 i++;
1918 lpszCLSID++;
1919 i++;
1922 return STATUS_SUCCESS;
1925 /*************************************************************************
1926 * RtlStringFromGUID (NTDLL.@)
1928 * Convert a GUID into a string representation of a GUID.
1930 * PARAMS
1931 * guid [I] GUID to convert
1932 * str [O] Destination for the converted string
1934 * RETURNS
1935 * Success: STATUS_SUCCESS. str contains the converted value.
1936 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
1938 * SEE ALSO
1939 * See RtlGUIDFromString.
1941 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
1943 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
1944 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
1945 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
1946 '%','0','2','X','%','0','2','X','}','\0' };
1948 TRACE("(%p,%p)\n", guid, str);
1950 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
1951 str->MaximumLength = str->Length + sizeof(WCHAR);
1952 str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
1953 if (!str->Buffer)
1955 str->Length = str->MaximumLength = 0;
1956 return STATUS_NO_MEMORY;
1958 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
1959 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
1960 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
1962 return STATUS_SUCCESS;
1965 /******************************************************************************
1966 * RtlHashUnicodeString [NTDLL.@]
1968 NTSTATUS WINAPI RtlHashUnicodeString(PCUNICODE_STRING string, BOOLEAN case_insensitive, ULONG alg, ULONG *hash)
1970 unsigned int i;
1972 if (!string || !hash) return STATUS_INVALID_PARAMETER;
1974 switch (alg)
1976 case HASH_STRING_ALGORITHM_DEFAULT:
1977 case HASH_STRING_ALGORITHM_X65599:
1978 break;
1979 default:
1980 return STATUS_INVALID_PARAMETER;
1983 *hash = 0;
1984 for (i = 0; i < string->Length/sizeof(WCHAR); i++)
1985 *hash = *hash*65599 + (case_insensitive ? toupperW(string->Buffer[i]) : string->Buffer[i]);
1987 return STATUS_SUCCESS;