ASCII string comparisons should not depend on the locale.
[wine.git] / dlls / ntdll / rtlstr.c
blobacc3f7bb1d9456e0782a85e956e4b6e005de17fa
1 /*
2 * Rtl string functions
4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright (C) 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "ntdll_misc.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40 UINT NlsAnsiCodePage = 0;
41 BYTE NlsMbCodePageTag = 0;
42 BYTE NlsMbOemCodePageTag = 0;
44 static const union cptable *ansi_table;
45 static const union cptable *oem_table;
46 static const union cptable* unix_table; /* NULL if UTF8 */
49 /**************************************************************************
50 * __wine_init_codepages (NTDLL.@)
52 * Set the code page once kernel32 is loaded. Should be done differently.
54 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
55 const union cptable *ucp)
57 ansi_table = ansi;
58 oem_table = oem;
59 unix_table = ucp;
60 NlsAnsiCodePage = ansi->info.codepage;
63 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
65 return (unix_table) ?
66 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
67 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
70 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
71 const char* defchar, int *used )
73 return (unix_table) ?
74 wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used ) :
75 wine_utf8_wcstombs( src, srclen, dst, dstlen );
78 /**************************************************************************
79 * RtlInitAnsiString (NTDLL.@)
81 * Initializes a buffered ansi string.
83 * RETURNS
84 * Nothing.
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 void WINAPI RtlInitAnsiString(
92 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
93 PCSZ source) /* [I] '\0' terminated string used to initialize target */
95 if ((target->Buffer = (PCHAR) source))
97 target->Length = strlen(source);
98 target->MaximumLength = target->Length + 1;
100 else target->Length = target->MaximumLength = 0;
104 /**************************************************************************
105 * RtlInitString (NTDLL.@)
107 * Initializes a buffered string.
109 * RETURNS
110 * Nothing.
112 * NOTES
113 * Assigns source to target->Buffer. The length of source is assigned to
114 * target->Length and target->MaximumLength. If source is NULL the length
115 * of source is assumed to be 0.
117 void WINAPI RtlInitString(
118 PSTRING target, /* [I/O] Buffered string to be initialized */
119 PCSZ source) /* [I] '\0' terminated string used to initialize target */
121 RtlInitAnsiString( target, source );
125 /**************************************************************************
126 * RtlFreeAnsiString (NTDLL.@)
128 void WINAPI RtlFreeAnsiString( PSTRING str )
130 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
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 target->Length = strlenW(source) * sizeof(WCHAR);
178 target->MaximumLength = target->Length + sizeof(WCHAR);
180 else target->Length = target->MaximumLength = 0;
184 /**************************************************************************
185 * RtlInitUnicodeStringEx (NTDLL.@)
187 * Initializes a buffered unicode string.
189 * RETURNS
190 * Success: STATUS_SUCCESS. target is initialized.
191 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
193 * NOTES
194 * Assigns source to target->Buffer. The length of source is assigned to
195 * target->Length and target->MaximumLength. If source is NULL the length
196 * of source is assumed to be 0.
198 NTSTATUS WINAPI RtlInitUnicodeStringEx(
199 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
200 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
202 if (source != NULL) {
203 unsigned int len = strlenW(source) * sizeof(WCHAR);
205 if (len > 0xFFFC) {
206 return STATUS_NAME_TOO_LONG;
207 } else {
208 target->Length = len;
209 target->MaximumLength = len + sizeof(WCHAR);
210 target->Buffer = (PWSTR) source;
211 } /* if */
212 } else {
213 target->Length = 0;
214 target->MaximumLength = 0;
215 target->Buffer = NULL;
216 } /* if */
217 return STATUS_SUCCESS;
221 /**************************************************************************
222 * RtlCreateUnicodeString (NTDLL.@)
224 * Creates a UNICODE_STRING from a null-terminated Unicode string.
226 * RETURNS
227 * Success: TRUE
228 * Failure: FALSE
230 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
232 int len = (strlenW(src) + 1) * sizeof(WCHAR);
233 if (!(target->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) return FALSE;
234 memcpy( target->Buffer, src, len );
235 target->MaximumLength = len;
236 target->Length = len - sizeof(WCHAR);
237 return TRUE;
241 /**************************************************************************
242 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
244 * Creates a UNICODE_STRING from a null-terminated Ascii string.
246 * RETURNS
247 * Success: TRUE
248 * Failure: FALSE
250 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
252 STRING ansi;
253 RtlInitAnsiString( &ansi, src );
254 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
258 /**************************************************************************
259 * RtlFreeUnicodeString (NTDLL.@)
261 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
262 * RtlCreateUnicodeStringFromAsciiz().
264 * RETURNS
265 * nothing
267 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
269 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
273 /**************************************************************************
274 * RtlCopyUnicodeString (NTDLL.@)
276 * Copies from one UNICODE_STRING to another.
278 * RETURNS
279 * nothing
281 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
283 if (src)
285 unsigned int len = min( src->Length, dst->MaximumLength );
286 memcpy( dst->Buffer, src->Buffer, len );
287 dst->Length = len;
288 /* append terminating '\0' if enough space */
289 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
291 else dst->Length = 0;
295 /**************************************************************************
296 * RtlDuplicateUnicodeString (NTDLL.@)
298 * Duplicates an unicode string.
300 * RETURNS
301 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
302 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
303 * STATUS_NO_MEMORY, if the allocation fails.
305 * NOTES
306 * For add_nul there are several possible values:
307 * 0 = destination will not be '\0' terminated,
308 * 1 = destination will be '\0' terminated,
309 * 3 = like 1 but for an empty source string produce '\0' terminated empty
310 * Buffer instead of assigning NULL to the Buffer.
311 * Other add_nul values are invalid.
313 NTSTATUS WINAPI RtlDuplicateUnicodeString(
314 int add_nul, /* [I] flag */
315 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
316 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
318 if (source == NULL || destination == NULL ||
319 source->Length > source->MaximumLength ||
320 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
321 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
322 return STATUS_INVALID_PARAMETER;
323 } else {
324 if (source->Length == 0 && add_nul != 3) {
325 destination->Length = 0;
326 destination->MaximumLength = 0;
327 destination->Buffer = NULL;
328 } else {
329 unsigned int destination_max_len = source->Length;
331 if (add_nul) {
332 destination_max_len += sizeof(WCHAR);
333 } /* if */
334 destination->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len);
335 if (destination->Buffer == NULL) {
336 return STATUS_NO_MEMORY;
337 } else {
338 memcpy(destination->Buffer, source->Buffer, source->Length);
339 destination->Length = source->Length;
340 destination->MaximumLength = source->Length;
341 /* append terminating '\0' if enough space */
342 if (add_nul) {
343 destination->MaximumLength = destination_max_len;
344 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
345 } /* if */
346 } /* if */
347 } /* if */
348 } /* if */
349 return STATUS_SUCCESS;
353 /**************************************************************************
354 * RtlEraseUnicodeString (NTDLL.@)
356 * Overwrites a UNICODE_STRING with zeros.
358 * RETURNS
359 * nothing
361 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
363 if (str->Buffer)
365 memset( str->Buffer, 0, str->MaximumLength );
366 str->Length = 0;
372 COMPARISON FUNCTIONS
376 /******************************************************************************
377 * RtlCompareString (NTDLL.@)
379 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
381 unsigned int len;
382 LONG ret = 0;
383 LPCSTR p1, p2;
385 len = min(s1->Length, s2->Length);
386 p1 = s1->Buffer;
387 p2 = s2->Buffer;
389 if (CaseInsensitive)
391 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
393 else
395 while (!ret && len--) ret = *p1++ - *p2++;
397 if (!ret) ret = s1->Length - s2->Length;
398 return ret;
402 /******************************************************************************
403 * RtlCompareUnicodeString (NTDLL.@)
405 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
406 BOOLEAN CaseInsensitive )
408 unsigned int len;
409 LONG ret = 0;
410 LPCWSTR p1, p2;
412 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
413 p1 = s1->Buffer;
414 p2 = s2->Buffer;
416 if (CaseInsensitive)
418 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
420 else
422 while (!ret && len--) ret = *p1++ - *p2++;
424 if (!ret) ret = s1->Length - s2->Length;
425 return ret;
429 /**************************************************************************
430 * RtlEqualString (NTDLL.@)
432 * Determine if two strings are equal.
434 * PARAMS
435 * s1 [I] Source string
436 * s2 [I] String to compare to s1
437 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
439 * RETURNS
440 * Non-zero if s1 is equal to s2, 0 otherwise.
442 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
444 if (s1->Length != s2->Length) return FALSE;
445 return !RtlCompareString( s1, s2, CaseInsensitive );
449 /**************************************************************************
450 * RtlEqualUnicodeString (NTDLL.@)
452 * Unicode version of RtlEqualString.
454 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
455 BOOLEAN CaseInsensitive )
457 if (s1->Length != s2->Length) return FALSE;
458 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
462 /**************************************************************************
463 * RtlPrefixString (NTDLL.@)
465 * Determine if one string is a prefix of another.
467 * PARAMS
468 * s1 [I] Prefix to look for in s2
469 * s2 [I] String that may contain s1 as a prefix
470 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
472 * RETURNS
473 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
475 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
477 unsigned int i;
479 if (s1->Length > s2->Length) return FALSE;
480 if (ignore_case)
482 for (i = 0; i < s1->Length; i++)
483 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
485 else
487 for (i = 0; i < s1->Length; i++)
488 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
490 return TRUE;
494 /**************************************************************************
495 * RtlPrefixUnicodeString (NTDLL.@)
497 * Unicode version of RtlPrefixString.
499 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
500 const UNICODE_STRING *s2,
501 BOOLEAN ignore_case )
503 unsigned int i;
505 if (s1->Length > s2->Length) return FALSE;
506 if (ignore_case)
508 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
509 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
511 else
513 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
514 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
516 return TRUE;
520 /**************************************************************************
521 * RtlEqualComputerName (NTDLL.@)
523 * Determine if two computer names are the same.
525 * PARAMS
526 * left [I] First computer name
527 * right [I] Second computer name
529 * RETURNS
530 * 0 if the names are equal, non-zero otherwise.
532 * NOTES
533 * The comparason is case insensitive.
535 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
536 const UNICODE_STRING *right)
538 NTSTATUS ret;
539 STRING upLeft, upRight;
541 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
543 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
545 ret = RtlEqualString( &upLeft, &upRight, FALSE );
546 RtlFreeOemString( &upRight );
548 RtlFreeOemString( &upLeft );
550 return ret;
554 /**************************************************************************
555 * RtlEqualDomainName (NTDLL.@)
557 * Determine if two domain names are the same.
559 * PARAMS
560 * left [I] First domain name
561 * right [I] Second domain name
563 * RETURNS
564 * 0 if the names are equal, non-zero otherwise.
566 * NOTES
567 * The comparason is case insensitive.
569 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
570 const UNICODE_STRING *right)
572 return RtlEqualComputerName(left, right);
577 COPY BETWEEN ANSI_STRING or UNICODE_STRING
578 there is no parameter checking, it just crashes
582 /**************************************************************************
583 * RtlAnsiStringToUnicodeString (NTDLL.@)
585 * Converts an ansi string to an unicode string.
587 * RETURNS
588 * Success: STATUS_SUCCESS. uni contains the converted string
589 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
590 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
591 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
593 * NOTES
594 * This function always writes a terminating '\0'.
596 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
597 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
598 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
599 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
601 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
603 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
604 uni->Length = total - sizeof(WCHAR);
605 if (doalloc)
607 uni->MaximumLength = total;
608 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
609 return STATUS_NO_MEMORY;
611 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
613 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
614 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
615 return STATUS_SUCCESS;
619 /**************************************************************************
620 * RtlOemStringToUnicodeString (NTDLL.@)
622 * Converts an oem string to an unicode string.
624 * RETURNS
625 * Success: STATUS_SUCCESS. uni contains the converted string
626 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
627 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
628 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
630 * NOTES
631 * This function always writes a terminating '\0'.
633 NTSTATUS WINAPI RtlOemStringToUnicodeString(
634 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
635 const STRING *oem, /* [I] Oem string to be converted */
636 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
638 DWORD total = RtlOemStringToUnicodeSize( oem );
640 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
641 uni->Length = total - sizeof(WCHAR);
642 if (doalloc)
644 uni->MaximumLength = total;
645 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
646 return STATUS_NO_MEMORY;
648 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
650 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
651 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
652 return STATUS_SUCCESS;
656 /**************************************************************************
657 * RtlUnicodeStringToAnsiString (NTDLL.@)
659 * Converts an unicode string to an ansi string.
661 * RETURNS
662 * Success: STATUS_SUCCESS. ansi contains the converted string
663 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
664 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
666 * NOTES
667 * This function always writes a terminating '\0'.
668 * It performs a partial copy if ansi is too small.
670 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
671 STRING *ansi, /* [I/O] Destination for the ansi string */
672 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
673 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
675 NTSTATUS ret = STATUS_SUCCESS;
676 DWORD len = RtlUnicodeStringToAnsiSize( uni );
678 ansi->Length = len - 1;
679 if (doalloc)
681 ansi->MaximumLength = len;
682 if (!(ansi->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
683 return STATUS_NO_MEMORY;
685 else if (ansi->MaximumLength < len)
687 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
688 ansi->Length = ansi->MaximumLength - 1;
689 ret = STATUS_BUFFER_OVERFLOW;
692 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
693 ansi->Buffer[ansi->Length] = 0;
694 return ret;
698 /**************************************************************************
699 * RtlUnicodeStringToOemString (NTDLL.@)
701 * Converts a Rtl Unicode string to an OEM string.
703 * PARAMS
704 * oem [O] Destination for OEM string
705 * uni [I] Source Unicode string
706 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
708 * RETURNS
709 * Success: STATUS_SUCCESS. oem contains the converted string
710 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
711 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
713 * NOTES
714 * If doalloc is TRUE, the length allocated is uni->Length + 1.
715 * This function always '\0' terminates the string returned.
717 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
718 const UNICODE_STRING *uni,
719 BOOLEAN doalloc )
721 NTSTATUS ret = STATUS_SUCCESS;
722 DWORD len = RtlUnicodeStringToOemSize( uni );
724 oem->Length = len - 1;
725 if (doalloc)
727 oem->MaximumLength = len;
728 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
729 return STATUS_NO_MEMORY;
731 else if (oem->MaximumLength < len)
733 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
734 oem->Length = oem->MaximumLength - 1;
735 ret = STATUS_BUFFER_OVERFLOW;
738 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
739 oem->Buffer[oem->Length] = 0;
740 return ret;
744 /**************************************************************************
745 * RtlMultiByteToUnicodeN (NTDLL.@)
747 * Converts a multi-byte string to a Unicode string.
749 * RETURNS
750 * NTSTATUS code
752 * NOTES
753 * Performs a partial copy if dst is too small.
755 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
756 LPCSTR src, DWORD srclen )
759 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
760 if (reslen)
761 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
762 return STATUS_SUCCESS;
766 /**************************************************************************
767 * RtlOemToUnicodeN (NTDLL.@)
769 * Converts a multi-byte string in the OEM code page to a Unicode string.
771 * RETURNS
772 * NTSTATUS code
774 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
775 LPCSTR src, DWORD srclen )
777 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
778 if (reslen)
779 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
780 return STATUS_SUCCESS;
784 /**************************************************************************
785 * RtlUnicodeToMultiByteN (NTDLL.@)
787 * Converts a Unicode string to a multi-byte string in the ANSI code page.
789 * RETURNS
790 * NTSTATUS code
792 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
793 LPCWSTR src, DWORD srclen )
795 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
796 dst, dstlen, NULL, NULL );
797 if (reslen)
798 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
799 return STATUS_SUCCESS;
803 /**************************************************************************
804 * RtlUnicodeToOemN (NTDLL.@)
806 * Converts a Unicode string to a multi-byte string in the OEM code page.
808 * RETURNS
809 * NTSTATUS code
811 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
812 LPCWSTR src, DWORD srclen )
814 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
815 dst, dstlen, NULL, NULL );
816 if (reslen)
817 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
818 return STATUS_SUCCESS;
823 CASE CONVERSIONS
827 /**************************************************************************
828 * RtlUpperChar (NTDLL.@)
830 * Converts an Ascii character to uppercase.
832 * PARAMS
833 * ch [I] Character to convert
835 * RETURNS
836 * The uppercase character value.
838 * NOTES
839 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
840 * All other input characters are returned unchanged. The locale and
841 * multibyte characters are not taken into account (as native DLL).
843 CHAR WINAPI RtlUpperChar( CHAR ch )
845 if (ch >= 'a' && ch <= 'z') {
846 return ch - 'a' + 'A';
847 } else {
848 return ch;
849 } /* if */
853 /**************************************************************************
854 * RtlUpperString (NTDLL.@)
856 * Converts an Ascii string to uppercase.
858 * PARAMS
859 * dst [O] Destination for converted string
860 * src [I] Source string to convert
862 * RETURNS
863 * Nothing.
865 * NOTES
866 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
867 * All other src characters are copied unchanged to dst. The locale and
868 * multibyte characters are not taken into account (as native DLL).
869 * The number of character copied is the minimum of src->Length and
870 * the dst->MaximumLength.
872 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
874 unsigned int i, len = min(src->Length, dst->MaximumLength);
876 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
877 dst->Length = len;
881 /**************************************************************************
882 * RtlUpcaseUnicodeChar (NTDLL.@)
884 * Converts an Unicode character to uppercase.
886 * PARAMS
887 * wch [I] Character to convert
889 * RETURNS
890 * The uppercase character value.
892 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
894 return toupperW(wch);
898 /**************************************************************************
899 * RtlDowncaseUnicodeChar (NTDLL.@)
901 * Converts an Unicode character to lowercase.
903 * PARAMS
904 * wch [I] Character to convert
906 * RETURNS
907 * The lowercase character value.
909 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
911 return tolowerW(wch);
915 /**************************************************************************
916 * RtlUpcaseUnicodeString (NTDLL.@)
918 * Converts an Unicode string to uppercase.
920 * PARAMS
921 * dest [O] Destination for converted string
922 * src [I] Source string to convert
923 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
925 * RETURNS
926 * Success: STATUS_SUCCESS. dest contains the converted string.
927 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
928 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
930 * NOTES
931 * dest is never '\0' terminated because it may be equal to src, and src
932 * might not be '\0' terminated. dest->Length is only set upon success.
934 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
935 const UNICODE_STRING *src,
936 BOOLEAN doalloc)
938 DWORD i, len = src->Length;
940 if (doalloc)
942 dest->MaximumLength = len;
943 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
944 return STATUS_NO_MEMORY;
946 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
948 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
949 dest->Length = len;
950 return STATUS_SUCCESS;
954 /**************************************************************************
955 * RtlDowncaseUnicodeString (NTDLL.@)
957 * Converts an Unicode string to lowercase.
959 * PARAMS
960 * dest [O] Destination for converted string
961 * src [I] Source string to convert
962 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
964 * RETURNS
965 * Success: STATUS_SUCCESS. dest contains the converted string.
966 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
967 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
969 * NOTES
970 * dest is never '\0' terminated because it may be equal to src, and src
971 * might not be '\0' terminated. dest->Length is only set upon success.
973 NTSTATUS WINAPI RtlDowncaseUnicodeString(
974 UNICODE_STRING *dest,
975 const UNICODE_STRING *src,
976 BOOLEAN doalloc)
978 DWORD i;
979 DWORD len = src->Length;
981 if (doalloc) {
982 dest->MaximumLength = len;
983 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) {
984 return STATUS_NO_MEMORY;
985 } /* if */
986 } else if (len > dest->MaximumLength) {
987 return STATUS_BUFFER_OVERFLOW;
988 } /* if */
990 for (i = 0; i < len/sizeof(WCHAR); i++) {
991 dest->Buffer[i] = tolowerW(src->Buffer[i]);
992 } /* for */
993 dest->Length = len;
994 return STATUS_SUCCESS;
998 /**************************************************************************
999 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1001 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1003 * RETURNS
1004 * NTSTATUS code
1006 * NOTES
1007 * writes terminating 0
1009 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1010 const UNICODE_STRING *src,
1011 BOOLEAN doalloc )
1013 NTSTATUS ret;
1014 UNICODE_STRING upcase;
1016 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1018 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1019 RtlFreeUnicodeString( &upcase );
1021 return ret;
1025 /**************************************************************************
1026 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1028 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1029 * stored in STRING format.
1031 * RETURNS
1032 * NTSTATUS code
1034 * NOTES
1035 * writes terminating 0
1037 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1038 const UNICODE_STRING *src,
1039 BOOLEAN doalloc )
1041 NTSTATUS ret;
1042 UNICODE_STRING upcase;
1044 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1046 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1047 RtlFreeUnicodeString( &upcase );
1049 return ret;
1053 /**************************************************************************
1054 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1056 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1057 * stored in STRING format.
1059 * RETURNS
1060 * NTSTATUS code
1062 * NOTES
1063 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1065 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1066 const UNICODE_STRING *uni,
1067 BOOLEAN doalloc )
1069 NTSTATUS ret;
1070 UNICODE_STRING upcase;
1072 if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
1074 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1075 oem->Length = len;
1076 if (doalloc)
1078 oem->MaximumLength = len;
1079 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
1081 ret = STATUS_NO_MEMORY;
1082 goto done;
1085 else if (oem->MaximumLength < len)
1087 ret = STATUS_BUFFER_OVERFLOW;
1088 oem->Length = oem->MaximumLength;
1089 if (!oem->MaximumLength) goto done;
1091 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1092 done:
1093 RtlFreeUnicodeString( &upcase );
1095 return ret;
1099 /**************************************************************************
1100 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1102 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1104 * RETURNS
1105 * NTSTATUS code
1107 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1108 LPCWSTR src, DWORD srclen )
1110 NTSTATUS ret;
1111 LPWSTR upcase;
1112 DWORD i;
1114 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1115 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1116 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1117 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1118 return ret;
1122 /**************************************************************************
1123 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1125 * Converts a Unicode string to the equivalent OEM upper-case representation.
1127 * RETURNS
1128 * NTSTATUS code
1130 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1131 LPCWSTR src, DWORD srclen )
1133 NTSTATUS ret;
1134 LPWSTR upcase;
1135 DWORD i;
1137 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
1138 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1139 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1140 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
1141 return ret;
1146 STRING SIZE
1150 /**************************************************************************
1151 * RtlOemStringToUnicodeSize (NTDLL.@)
1152 * RtlxOemStringToUnicodeSize (NTDLL.@)
1154 * Calculate the size in bytes necessary for the Unicode conversion of str,
1155 * including the terminating '\0'.
1157 * PARAMS
1158 * str [I] String to calculate the size of
1160 * RETURNS
1161 * The calculated size.
1163 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1165 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1166 return (ret + 1) * sizeof(WCHAR);
1170 /**************************************************************************
1171 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1172 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1174 * Calculate the size in bytes necessary for the Unicode conversion of str,
1175 * including the terminating '\0'.
1177 * PARAMS
1178 * str [I] String to calculate the size of
1180 * RETURNS
1181 * The calculated size.
1183 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1185 DWORD ret;
1186 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1187 return ret + sizeof(WCHAR);
1191 /**************************************************************************
1192 * RtlMultiByteToUnicodeSize (NTDLL.@)
1194 * Compute the size in bytes necessary for the Unicode conversion of str,
1195 * without the terminating '\0'.
1197 * PARAMS
1198 * size [O] Destination for size
1199 * str [I] String to calculate the size of
1200 * len [I] Length of str
1202 * RETURNS
1203 * STATUS_SUCCESS.
1205 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1207 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1208 return STATUS_SUCCESS;
1212 /**************************************************************************
1213 * RtlUnicodeToMultiByteSize (NTDLL.@)
1215 * Calculate the size in bytes necessary for the multibyte conversion of str,
1216 * without the terminating '\0'.
1218 * PARAMS
1219 * size [O] Destination for size
1220 * str [I] String to calculate the size of
1221 * len [I] Length of str
1223 * RETURNS
1224 * STATUS_SUCCESS.
1226 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1228 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1229 return STATUS_SUCCESS;
1233 /**************************************************************************
1234 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1235 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1237 * Calculate the size in bytes necessary for the Ansi conversion of str,
1238 * including the terminating '\0'.
1240 * PARAMS
1241 * str [I] String to calculate the size of
1243 * RETURNS
1244 * The calculated size.
1246 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1248 DWORD ret;
1249 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1250 return ret + 1;
1254 /**************************************************************************
1255 * RtlUnicodeStringToOemSize (NTDLL.@)
1256 * RtlxUnicodeStringToOemSize (NTDLL.@)
1258 * Calculate the size in bytes necessary for the OEM conversion of str,
1259 * including the terminating '\0'.
1261 * PARAMS
1262 * str [I] String to calculate the size of
1264 * RETURNS
1265 * The calculated size.
1267 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1269 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1270 NULL, 0, NULL, NULL ) + 1;
1274 /**************************************************************************
1275 * RtlAppendAsciizToString (NTDLL.@)
1277 * Concatenates a buffered character string and a '\0' terminated character
1278 * string
1280 * RETURNS
1281 * Success: STATUS_SUCCESS. src is appended to dest.
1282 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1283 * to hold the concatenated string.
1285 * NOTES
1286 * if src is NULL dest is unchanged.
1287 * dest is never '\0' terminated.
1289 NTSTATUS WINAPI RtlAppendAsciizToString(
1290 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1291 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1293 if (src != NULL) {
1294 unsigned int src_len = strlen(src);
1295 unsigned int dest_len = src_len + dest->Length;
1297 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1298 memcpy(dest->Buffer + dest->Length, src, src_len);
1299 dest->Length = dest_len;
1300 } /* if */
1301 return STATUS_SUCCESS;
1305 /**************************************************************************
1306 * RtlAppendStringToString (NTDLL.@)
1308 * Concatenates two buffered character strings
1310 * RETURNS
1311 * Success: STATUS_SUCCESS. src is appended to dest.
1312 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1313 * to hold the concatenated string.
1315 * NOTES
1316 * if src->length is zero dest is unchanged.
1317 * dest is never '\0' terminated.
1319 NTSTATUS WINAPI RtlAppendStringToString(
1320 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1321 const STRING *src) /* [I] Buffered character string to be concatenated */
1323 if (src->Length != 0) {
1324 unsigned int dest_len = src->Length + dest->Length;
1326 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1327 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1328 dest->Length = dest_len;
1329 } /* if */
1330 return STATUS_SUCCESS;
1334 /**************************************************************************
1335 * RtlAppendUnicodeToString (NTDLL.@)
1337 * Concatenates an buffered unicode string and a '\0' terminated unicode
1338 * string
1340 * RETURNS
1341 * Success: STATUS_SUCCESS. src is appended to dest.
1342 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1343 * to hold the concatenated string.
1345 * NOTES
1346 * if src is NULL dest is unchanged.
1347 * dest is '\0' terminated when the MaximumLength allowes it.
1348 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1350 * DIFFERENCES
1351 * Does not write in the src->Buffer beyond MaximumLength when
1352 * MaximumLength is odd as the native function does.
1354 NTSTATUS WINAPI RtlAppendUnicodeToString(
1355 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1356 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1358 if (src != NULL) {
1359 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1360 unsigned int dest_len = src_len + dest->Length;
1362 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1363 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1364 dest->Length = dest_len;
1365 /* append terminating '\0' if enough space */
1366 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1367 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1368 } /* if */
1369 } /* if */
1370 return STATUS_SUCCESS;
1374 /**************************************************************************
1375 * RtlAppendUnicodeStringToString (NTDLL.@)
1377 * Concatenates two buffered unicode strings
1379 * RETURNS
1380 * Success: STATUS_SUCCESS. src is appended to dest.
1381 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1382 * to hold the concatenated string.
1384 * NOTES
1385 * if src->length is zero dest is unchanged.
1386 * dest is '\0' terminated when the MaximumLength allowes it.
1387 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1389 * DIFFERENCES
1390 * Does not write in the src->Buffer beyond MaximumLength when
1391 * MaximumLength is odd as the native function does.
1393 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1394 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1395 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1397 if (src->Length != 0) {
1398 unsigned int dest_len = src->Length + dest->Length;
1400 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1401 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1402 dest->Length = dest_len;
1403 /* append terminating '\0' if enough space */
1404 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1405 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1406 } /* if */
1407 } /* if */
1408 return STATUS_SUCCESS;
1412 /**************************************************************************
1413 * RtlFindCharInUnicodeString (NTDLL.@)
1415 * Searches for one of several unicode characters in an unicode string.
1417 * RETURNS
1418 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1419 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1421 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1422 int flags, /* [I] Flags */
1423 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1424 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1425 USHORT *pos) /* [O] Position of the first character found + 2 */
1427 int main_idx;
1428 unsigned int search_idx;
1430 switch (flags) {
1431 case 0:
1432 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1433 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1434 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1435 *pos = (main_idx + 1) * sizeof(WCHAR);
1436 return STATUS_SUCCESS;
1440 *pos = 0;
1441 return STATUS_NOT_FOUND;
1442 case 1:
1443 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1444 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1445 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1446 *pos = main_idx * sizeof(WCHAR);
1447 return STATUS_SUCCESS;
1451 *pos = 0;
1452 return STATUS_NOT_FOUND;
1453 case 2:
1454 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1455 search_idx = 0;
1456 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1457 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1458 search_idx++;
1460 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1461 *pos = (main_idx + 1) * sizeof(WCHAR);
1462 return STATUS_SUCCESS;
1465 *pos = 0;
1466 return STATUS_NOT_FOUND;
1467 case 3:
1468 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1469 search_idx = 0;
1470 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1471 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1472 search_idx++;
1474 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1475 *pos = main_idx * sizeof(WCHAR);
1476 return STATUS_SUCCESS;
1479 *pos = 0;
1480 return STATUS_NOT_FOUND;
1481 } /* switch */
1482 return STATUS_NOT_FOUND;
1487 MISC
1489 /* Tests that we currently implement */
1490 #define ITU_IMPLEMENTED_TESTS \
1491 (IS_TEXT_UNICODE_SIGNATURE | \
1492 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1493 IS_TEXT_UNICODE_ODD_LENGTH | \
1494 IS_TEXT_UNICODE_STATISTICS | \
1495 IS_TEXT_UNICODE_NULL_BYTES)
1497 /**************************************************************************
1498 * RtlIsTextUnicode (NTDLL.@)
1500 * Attempt to guess whether a text buffer is Unicode.
1502 * PARAMS
1503 * buf [I] Text buffer to test
1504 * len [I] Length of buf
1505 * pf [O] Destination for test results
1507 * RETURNS
1508 * The length of the string if all tests were passed, 0 otherwise.
1510 * FIXME
1511 * Should implement more tests.
1513 DWORD WINAPI RtlIsTextUnicode(
1514 LPVOID buf,
1515 DWORD len,
1516 DWORD *pf)
1518 LPWSTR s = buf;
1519 DWORD flags = -1, out_flags = 0;
1521 if (!len)
1522 goto out;
1523 if (pf)
1524 flags = *pf;
1526 * Apply various tests to the text string. According to the
1527 * docs, each test "passed" sets the corresponding flag in
1528 * the output flags. But some of the tests are mutually
1529 * exclusive, so I don't see how you could pass all tests ...
1532 /* Check for an odd length ... pass if even. */
1533 if ((flags & IS_TEXT_UNICODE_ODD_LENGTH) && (len & 1))
1534 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1536 /* Check for the special byte order unicode marks. */
1537 if ((flags & IS_TEXT_UNICODE_SIGNATURE) && *s == 0xFEFF)
1538 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1540 if ((flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) && *s == 0xFFFE)
1541 out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1543 /* apply some statistical analysis */
1544 if (flags & IS_TEXT_UNICODE_STATISTICS)
1546 DWORD i, stats = 0;
1547 /* FIXME: checks only for ASCII characters in the unicode stream */
1548 for (i = 0; i < len / sizeof(WCHAR); i++)
1550 if (s[i] <= 255) stats++;
1552 if (stats > len / sizeof(WCHAR) / 2)
1553 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1556 /* Check for unicode NULL chars */
1557 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1559 DWORD i;
1560 for (i = 0; i < len / sizeof(WCHAR); i++)
1562 if (!s[i])
1564 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1565 break;
1571 * Check whether the string passed all of the tests.
1573 flags &= ITU_IMPLEMENTED_TESTS;
1574 if ((out_flags & flags) != flags)
1575 len = 0;
1576 out:
1577 if (pf)
1578 *pf = out_flags;
1579 return len;
1583 /**************************************************************************
1584 * RtlCharToInteger (NTDLL.@)
1586 * Converts a character string into its integer equivalent.
1588 * RETURNS
1589 * Success: STATUS_SUCCESS. value contains the converted number
1590 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1591 * STATUS_ACCESS_VIOLATION, if value is NULL.
1593 * NOTES
1594 * For base 0 it uses 10 as base and the string should be in the format
1595 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1596 * For other bases the string should be in the format
1597 * "{whitespace} [+|-] {digits}".
1598 * No check is made for value overflow, only the lower 32 bits are assigned.
1599 * If str is NULL it crashes, as the native function does.
1601 * DIFFERENCES
1602 * This function does not read garbage behind '\0' as the native version does.
1604 NTSTATUS WINAPI RtlCharToInteger(
1605 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1606 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1607 ULONG *value) /* [O] Destination for the converted value */
1609 CHAR chCurrent;
1610 int digit;
1611 ULONG RunningTotal = 0;
1612 char bMinus = 0;
1614 while (*str != '\0' && *str <= ' ') {
1615 str++;
1616 } /* while */
1618 if (*str == '+') {
1619 str++;
1620 } else if (*str == '-') {
1621 bMinus = 1;
1622 str++;
1623 } /* if */
1625 if (base == 0) {
1626 base = 10;
1627 if (str[0] == '0') {
1628 if (str[1] == 'b') {
1629 str += 2;
1630 base = 2;
1631 } else if (str[1] == 'o') {
1632 str += 2;
1633 base = 8;
1634 } else if (str[1] == 'x') {
1635 str += 2;
1636 base = 16;
1637 } /* if */
1638 } /* if */
1639 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1640 return STATUS_INVALID_PARAMETER;
1641 } /* if */
1643 if (value == NULL) {
1644 return STATUS_ACCESS_VIOLATION;
1645 } /* if */
1647 while (*str != '\0') {
1648 chCurrent = *str;
1649 if (chCurrent >= '0' && chCurrent <= '9') {
1650 digit = chCurrent - '0';
1651 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1652 digit = chCurrent - 'A' + 10;
1653 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1654 digit = chCurrent - 'a' + 10;
1655 } else {
1656 digit = -1;
1657 } /* if */
1658 if (digit < 0 || digit >= base) {
1659 *value = bMinus ? -RunningTotal : RunningTotal;
1660 return STATUS_SUCCESS;
1661 } /* if */
1663 RunningTotal = RunningTotal * base + digit;
1664 str++;
1665 } /* while */
1667 *value = bMinus ? -RunningTotal : RunningTotal;
1668 return STATUS_SUCCESS;
1672 /**************************************************************************
1673 * RtlIntegerToChar (NTDLL.@)
1675 * Converts an unsigned integer to a character string.
1677 * RETURNS
1678 * Success: STATUS_SUCCESS. str contains the converted number
1679 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1680 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1681 * STATUS_ACCESS_VIOLATION, if str is NULL.
1683 * NOTES
1684 * Instead of base 0 it uses 10 as base.
1685 * Writes at most length characters to the string str.
1686 * Str is '\0' terminated when length allowes it.
1687 * When str fits exactly in length characters the '\0' is ommitted.
1689 NTSTATUS WINAPI RtlIntegerToChar(
1690 ULONG value, /* [I] Value to be converted */
1691 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1692 ULONG length, /* [I] Length of the str buffer in bytes */
1693 PCHAR str) /* [O] Destination for the converted value */
1695 CHAR buffer[33];
1696 PCHAR pos;
1697 CHAR digit;
1698 ULONG len;
1700 if (base == 0) {
1701 base = 10;
1702 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1703 return STATUS_INVALID_PARAMETER;
1704 } /* if */
1706 pos = &buffer[32];
1707 *pos = '\0';
1709 do {
1710 pos--;
1711 digit = value % base;
1712 value = value / base;
1713 if (digit < 10) {
1714 *pos = '0' + digit;
1715 } else {
1716 *pos = 'A' + digit - 10;
1717 } /* if */
1718 } while (value != 0L);
1720 len = &buffer[32] - pos;
1721 if (len > length) {
1722 return STATUS_BUFFER_OVERFLOW;
1723 } else if (str == NULL) {
1724 return STATUS_ACCESS_VIOLATION;
1725 } else if (len == length) {
1726 memcpy(str, pos, len);
1727 } else {
1728 memcpy(str, pos, len + 1);
1729 } /* if */
1730 return STATUS_SUCCESS;
1734 /**************************************************************************
1735 * RtlUnicodeStringToInteger (NTDLL.@)
1737 * Converts an unicode string into its integer equivalent.
1739 * RETURNS
1740 * Success: STATUS_SUCCESS. value contains the converted number
1741 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1742 * STATUS_ACCESS_VIOLATION, if value is NULL.
1744 * NOTES
1745 * For base 0 it uses 10 as base and the string should be in the format
1746 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1747 * For other bases the string should be in the format
1748 * "{whitespace} [+|-] {digits}".
1749 * No check is made for value overflow, only the lower 32 bits are assigned.
1750 * If str is NULL it crashes, as the native function does.
1752 * DIFFERENCES
1753 * This function does not read garbage on string length 0 as the native
1754 * version does.
1756 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1757 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1758 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1759 ULONG *value) /* [O] Destination for the converted value */
1761 LPWSTR lpwstr = str->Buffer;
1762 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1763 WCHAR wchCurrent;
1764 int digit;
1765 ULONG RunningTotal = 0;
1766 char bMinus = 0;
1768 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1769 lpwstr++;
1770 CharsRemaining--;
1771 } /* while */
1773 if (CharsRemaining >= 1) {
1774 if (*lpwstr == '+') {
1775 lpwstr++;
1776 CharsRemaining--;
1777 } else if (*lpwstr == '-') {
1778 bMinus = 1;
1779 lpwstr++;
1780 CharsRemaining--;
1781 } /* if */
1782 } /* if */
1784 if (base == 0) {
1785 base = 10;
1786 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1787 if (lpwstr[1] == 'b') {
1788 lpwstr += 2;
1789 CharsRemaining -= 2;
1790 base = 2;
1791 } else if (lpwstr[1] == 'o') {
1792 lpwstr += 2;
1793 CharsRemaining -= 2;
1794 base = 8;
1795 } else if (lpwstr[1] == 'x') {
1796 lpwstr += 2;
1797 CharsRemaining -= 2;
1798 base = 16;
1799 } /* if */
1800 } /* if */
1801 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1802 return STATUS_INVALID_PARAMETER;
1803 } /* if */
1805 if (value == NULL) {
1806 return STATUS_ACCESS_VIOLATION;
1807 } /* if */
1809 while (CharsRemaining >= 1) {
1810 wchCurrent = *lpwstr;
1811 if (wchCurrent >= '0' && wchCurrent <= '9') {
1812 digit = wchCurrent - '0';
1813 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1814 digit = wchCurrent - 'A' + 10;
1815 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1816 digit = wchCurrent - 'a' + 10;
1817 } else {
1818 digit = -1;
1819 } /* if */
1820 if (digit < 0 || digit >= base) {
1821 *value = bMinus ? -RunningTotal : RunningTotal;
1822 return STATUS_SUCCESS;
1823 } /* if */
1825 RunningTotal = RunningTotal * base + digit;
1826 lpwstr++;
1827 CharsRemaining--;
1828 } /* while */
1830 *value = bMinus ? -RunningTotal : RunningTotal;
1831 return STATUS_SUCCESS;
1835 /**************************************************************************
1836 * RtlIntegerToUnicodeString (NTDLL.@)
1838 * Converts an unsigned integer to a '\0' terminated unicode string.
1840 * RETURNS
1841 * Success: STATUS_SUCCESS. str contains the converted number
1842 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1843 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1844 * (with the '\0' termination). In this case str->Length
1845 * is set to the length, the string would have (which can
1846 * be larger than the MaximumLength).
1848 * NOTES
1849 * Instead of base 0 it uses 10 as base.
1850 * If str is NULL it crashes, as the native function does.
1852 * DIFFERENCES
1853 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1854 * The native function does this when the string would be longer than 16
1855 * characters even when the string parameter is long enough.
1857 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1858 ULONG value, /* [I] Value to be converted */
1859 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1860 UNICODE_STRING *str) /* [O] Destination for the converted value */
1862 WCHAR buffer[33];
1863 PWCHAR pos;
1864 WCHAR digit;
1866 if (base == 0) {
1867 base = 10;
1868 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1869 return STATUS_INVALID_PARAMETER;
1870 } /* if */
1872 pos = &buffer[32];
1873 *pos = '\0';
1875 do {
1876 pos--;
1877 digit = value % base;
1878 value = value / base;
1879 if (digit < 10) {
1880 *pos = '0' + digit;
1881 } else {
1882 *pos = 'A' + digit - 10;
1883 } /* if */
1884 } while (value != 0L);
1886 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1887 if (str->Length >= str->MaximumLength) {
1888 return STATUS_BUFFER_OVERFLOW;
1889 } else {
1890 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1891 } /* if */
1892 return STATUS_SUCCESS;
1896 /*************************************************************************
1897 * RtlGUIDFromString (NTDLL.@)
1899 * Convert a string representation of a GUID into a GUID.
1901 * PARAMS
1902 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1903 * guid [O] Destination for the converted GUID
1905 * RETURNS
1906 * Success: STATUS_SUCCESS. guid contains the converted value.
1907 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1909 * SEE ALSO
1910 * See RtlStringFromGUID.
1912 NTSTATUS WINAPI RtlGUIDFromString(const UNICODE_STRING *str, GUID* guid)
1914 int i = 0;
1915 const WCHAR *lpszCLSID = str->Buffer;
1916 BYTE* lpOut = (BYTE*)guid;
1918 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1920 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1921 * to memory: DWORD... WORD WORD BYTES............
1923 while (i < 37)
1925 switch (i)
1927 case 0:
1928 if (*lpszCLSID != '{')
1929 return STATUS_INVALID_PARAMETER;
1930 break;
1932 case 9: case 14: case 19: case 24:
1933 if (*lpszCLSID != '-')
1934 return STATUS_INVALID_PARAMETER;
1935 break;
1937 case 37:
1938 if (*lpszCLSID != '}')
1939 return STATUS_INVALID_PARAMETER;
1940 break;
1942 default:
1944 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1945 unsigned char byte;
1947 /* Read two hex digits as a byte value */
1948 if (ch >= '0' && ch <= '9') ch = ch - '0';
1949 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1950 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1951 else return STATUS_INVALID_PARAMETER;
1953 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1954 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1955 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1956 else return STATUS_INVALID_PARAMETER;
1958 byte = ch << 4 | ch2;
1960 switch (i)
1962 #ifndef WORDS_BIGENDIAN
1963 /* For Big Endian machines, we store the data such that the
1964 * dword/word members can be read as DWORDS and WORDS correctly. */
1965 /* Dword */
1966 case 1: lpOut[3] = byte; break;
1967 case 3: lpOut[2] = byte; break;
1968 case 5: lpOut[1] = byte; break;
1969 case 7: lpOut[0] = byte; lpOut += 4; break;
1970 /* Word */
1971 case 10: case 15: lpOut[1] = byte; break;
1972 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1973 #endif
1974 /* Byte */
1975 default: lpOut[0] = byte; lpOut++; break;
1977 lpszCLSID++; /* Skip 2nd character of byte */
1978 i++;
1981 lpszCLSID++;
1982 i++;
1985 return STATUS_SUCCESS;
1988 /*************************************************************************
1989 * RtlStringFromGUID (NTDLL.@)
1991 * Convert a GUID into a string representation of a GUID.
1993 * PARAMS
1994 * guid [I] GUID to convert
1995 * str [O] Destination for the converted string
1997 * RETURNS
1998 * Success: STATUS_SUCCESS. str contains the converted value.
1999 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2001 * SEE ALSO
2002 * See RtlGUIDFromString.
2004 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2006 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2007 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2008 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2009 '%','0','2','X','%','0','2','X','}','\0' };
2011 TRACE("(%p,%p)\n", guid, str);
2013 str->Buffer = (WCHAR*)RtlAllocateHeap( ntdll_get_process_heap(), 0, 40 * sizeof(WCHAR));
2014 if (!str->Buffer)
2016 str->Length = str->MaximumLength = 0;
2017 return STATUS_NO_MEMORY;
2019 str->Length = str->MaximumLength = 40;
2020 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2021 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2022 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2024 return STATUS_SUCCESS;