Implement the repair mechanism in msiexec, and stub it out in
[wine.git] / dlls / ntdll / rtlstr.c
blobccedf876b9bf4c7a694796929272bbe775a8eca6
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 #define GUID_STRING_LENGTH 38
42 UINT NlsAnsiCodePage = 0;
43 BYTE NlsMbCodePageTag = 0;
44 BYTE NlsMbOemCodePageTag = 0;
46 static const union cptable *ansi_table;
47 static const union cptable *oem_table;
48 static const union cptable* unix_table; /* NULL if UTF8 */
51 /**************************************************************************
52 * __wine_init_codepages (NTDLL.@)
54 * Set the code page once kernel32 is loaded. Should be done differently.
56 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
57 const union cptable *ucp)
59 ansi_table = ansi;
60 oem_table = oem;
61 unix_table = ucp;
62 NlsAnsiCodePage = ansi->info.codepage;
65 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
67 return (unix_table) ?
68 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
69 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
72 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
73 const char* defchar, int *used )
75 if (unix_table)
76 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
77 if (used) *used = 0; /* all chars are valid for UTF-8 */
78 return wine_utf8_wcstombs( src, srclen, dst, dstlen );
81 /**************************************************************************
82 * RtlInitAnsiString (NTDLL.@)
84 * Initializes a buffered ansi string.
86 * RETURNS
87 * Nothing.
89 * NOTES
90 * Assigns source to target->Buffer. The length of source is assigned to
91 * target->Length and target->MaximumLength. If source is NULL the length
92 * of source is assumed to be 0.
94 void WINAPI RtlInitAnsiString(
95 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
96 PCSZ source) /* [I] '\0' terminated string used to initialize target */
98 if ((target->Buffer = (PCHAR) source))
100 target->Length = strlen(source);
101 target->MaximumLength = target->Length + 1;
103 else target->Length = target->MaximumLength = 0;
107 /**************************************************************************
108 * RtlInitString (NTDLL.@)
110 * Initializes a buffered string.
112 * RETURNS
113 * Nothing.
115 * NOTES
116 * Assigns source to target->Buffer. The length of source is assigned to
117 * target->Length and target->MaximumLength. If source is NULL the length
118 * of source is assumed to be 0.
120 void WINAPI RtlInitString(
121 PSTRING target, /* [I/O] Buffered string to be initialized */
122 PCSZ source) /* [I] '\0' terminated string used to initialize target */
124 RtlInitAnsiString( target, source );
128 /**************************************************************************
129 * RtlFreeAnsiString (NTDLL.@)
131 void WINAPI RtlFreeAnsiString( PSTRING str )
133 if (str->Buffer) RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
137 /**************************************************************************
138 * RtlFreeOemString (NTDLL.@)
140 void WINAPI RtlFreeOemString( PSTRING str )
142 RtlFreeAnsiString( str );
146 /**************************************************************************
147 * RtlCopyString (NTDLL.@)
149 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
151 if (src)
153 unsigned int len = min( src->Length, dst->MaximumLength );
154 memcpy( dst->Buffer, src->Buffer, len );
155 dst->Length = len;
157 else dst->Length = 0;
161 /**************************************************************************
162 * RtlInitUnicodeString (NTDLL.@)
164 * Initializes a buffered unicode string.
166 * RETURNS
167 * Nothing.
169 * NOTES
170 * Assigns source to target->Buffer. The length of source is assigned to
171 * target->Length and target->MaximumLength. If source is NULL the length
172 * of source is assumed to be 0.
174 void WINAPI RtlInitUnicodeString(
175 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
176 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
178 if ((target->Buffer = (PWSTR) source))
180 target->Length = strlenW(source) * sizeof(WCHAR);
181 target->MaximumLength = target->Length + sizeof(WCHAR);
183 else target->Length = target->MaximumLength = 0;
187 /**************************************************************************
188 * RtlInitUnicodeStringEx (NTDLL.@)
190 * Initializes a buffered unicode string.
192 * RETURNS
193 * Success: STATUS_SUCCESS. target is initialized.
194 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
196 * NOTES
197 * Assigns source to target->Buffer. The length of source is assigned to
198 * target->Length and target->MaximumLength. If source is NULL the length
199 * of source is assumed to be 0.
201 NTSTATUS WINAPI RtlInitUnicodeStringEx(
202 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
203 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
205 if (source != NULL) {
206 unsigned int len = strlenW(source) * sizeof(WCHAR);
208 if (len > 0xFFFC) {
209 return STATUS_NAME_TOO_LONG;
210 } else {
211 target->Length = len;
212 target->MaximumLength = len + sizeof(WCHAR);
213 target->Buffer = (PWSTR) source;
214 } /* if */
215 } else {
216 target->Length = 0;
217 target->MaximumLength = 0;
218 target->Buffer = NULL;
219 } /* if */
220 return STATUS_SUCCESS;
224 /**************************************************************************
225 * RtlCreateUnicodeString (NTDLL.@)
227 * Creates a UNICODE_STRING from a null-terminated Unicode string.
229 * RETURNS
230 * Success: TRUE
231 * Failure: FALSE
233 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
235 int len = (strlenW(src) + 1) * sizeof(WCHAR);
236 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
237 memcpy( target->Buffer, src, len );
238 target->MaximumLength = len;
239 target->Length = len - sizeof(WCHAR);
240 return TRUE;
244 /**************************************************************************
245 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
247 * Creates a UNICODE_STRING from a null-terminated Ascii string.
249 * RETURNS
250 * Success: TRUE
251 * Failure: FALSE
253 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
255 STRING ansi;
256 RtlInitAnsiString( &ansi, src );
257 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
261 /**************************************************************************
262 * RtlFreeUnicodeString (NTDLL.@)
264 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
265 * RtlCreateUnicodeStringFromAsciiz().
267 * RETURNS
268 * nothing
270 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
272 if (str->Buffer) RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
276 /**************************************************************************
277 * RtlCopyUnicodeString (NTDLL.@)
279 * Copies from one UNICODE_STRING to another.
281 * RETURNS
282 * nothing
284 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
286 if (src)
288 unsigned int len = min( src->Length, dst->MaximumLength );
289 memcpy( dst->Buffer, src->Buffer, len );
290 dst->Length = len;
291 /* append terminating '\0' if enough space */
292 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
294 else dst->Length = 0;
298 /**************************************************************************
299 * RtlDuplicateUnicodeString (NTDLL.@)
301 * Duplicates an unicode string.
303 * RETURNS
304 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
305 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
306 * STATUS_NO_MEMORY, if the allocation fails.
308 * NOTES
309 * For add_nul there are several possible values:
310 * 0 = destination will not be '\0' terminated,
311 * 1 = destination will be '\0' terminated,
312 * 3 = like 1 but for an empty source string produce '\0' terminated empty
313 * Buffer instead of assigning NULL to the Buffer.
314 * Other add_nul values are invalid.
316 NTSTATUS WINAPI RtlDuplicateUnicodeString(
317 int add_nul, /* [I] flag */
318 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
319 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
321 if (source == NULL || destination == NULL ||
322 source->Length > source->MaximumLength ||
323 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
324 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
325 return STATUS_INVALID_PARAMETER;
326 } else {
327 if (source->Length == 0 && add_nul != 3) {
328 destination->Length = 0;
329 destination->MaximumLength = 0;
330 destination->Buffer = NULL;
331 } else {
332 unsigned int destination_max_len = source->Length;
334 if (add_nul) {
335 destination_max_len += sizeof(WCHAR);
336 } /* if */
337 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
338 if (destination->Buffer == NULL) {
339 return STATUS_NO_MEMORY;
340 } else {
341 memcpy(destination->Buffer, source->Buffer, source->Length);
342 destination->Length = source->Length;
343 destination->MaximumLength = source->Length;
344 /* append terminating '\0' if enough space */
345 if (add_nul) {
346 destination->MaximumLength = destination_max_len;
347 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
348 } /* if */
349 } /* if */
350 } /* if */
351 } /* if */
352 return STATUS_SUCCESS;
356 /**************************************************************************
357 * RtlEraseUnicodeString (NTDLL.@)
359 * Overwrites a UNICODE_STRING with zeros.
361 * RETURNS
362 * nothing
364 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
366 if (str->Buffer)
368 memset( str->Buffer, 0, str->MaximumLength );
369 str->Length = 0;
375 COMPARISON FUNCTIONS
379 /******************************************************************************
380 * RtlCompareString (NTDLL.@)
382 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
384 unsigned int len;
385 LONG ret = 0;
386 LPCSTR p1, p2;
388 len = min(s1->Length, s2->Length);
389 p1 = s1->Buffer;
390 p2 = s2->Buffer;
392 if (CaseInsensitive)
394 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
396 else
398 while (!ret && len--) ret = *p1++ - *p2++;
400 if (!ret) ret = s1->Length - s2->Length;
401 return ret;
405 /******************************************************************************
406 * RtlCompareUnicodeString (NTDLL.@)
408 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
409 BOOLEAN CaseInsensitive )
411 unsigned int len;
412 LONG ret = 0;
413 LPCWSTR p1, p2;
415 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
416 p1 = s1->Buffer;
417 p2 = s2->Buffer;
419 if (CaseInsensitive)
421 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
423 else
425 while (!ret && len--) ret = *p1++ - *p2++;
427 if (!ret) ret = s1->Length - s2->Length;
428 return ret;
432 /**************************************************************************
433 * RtlEqualString (NTDLL.@)
435 * Determine if two strings are equal.
437 * PARAMS
438 * s1 [I] Source string
439 * s2 [I] String to compare to s1
440 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
442 * RETURNS
443 * Non-zero if s1 is equal to s2, 0 otherwise.
445 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
447 if (s1->Length != s2->Length) return FALSE;
448 return !RtlCompareString( s1, s2, CaseInsensitive );
452 /**************************************************************************
453 * RtlEqualUnicodeString (NTDLL.@)
455 * Unicode version of RtlEqualString.
457 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
458 BOOLEAN CaseInsensitive )
460 if (s1->Length != s2->Length) return FALSE;
461 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
465 /**************************************************************************
466 * RtlPrefixString (NTDLL.@)
468 * Determine if one string is a prefix of another.
470 * PARAMS
471 * s1 [I] Prefix to look for in s2
472 * s2 [I] String that may contain s1 as a prefix
473 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
475 * RETURNS
476 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
478 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
480 unsigned int i;
482 if (s1->Length > s2->Length) return FALSE;
483 if (ignore_case)
485 for (i = 0; i < s1->Length; i++)
486 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
488 else
490 for (i = 0; i < s1->Length; i++)
491 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
493 return TRUE;
497 /**************************************************************************
498 * RtlPrefixUnicodeString (NTDLL.@)
500 * Unicode version of RtlPrefixString.
502 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
503 const UNICODE_STRING *s2,
504 BOOLEAN ignore_case )
506 unsigned int i;
508 if (s1->Length > s2->Length) return FALSE;
509 if (ignore_case)
511 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
512 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
514 else
516 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
517 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
519 return TRUE;
523 /**************************************************************************
524 * RtlEqualComputerName (NTDLL.@)
526 * Determine if two computer names are the same.
528 * PARAMS
529 * left [I] First computer name
530 * right [I] Second computer name
532 * RETURNS
533 * 0 if the names are equal, non-zero otherwise.
535 * NOTES
536 * The comparison is case insensitive.
538 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
539 const UNICODE_STRING *right)
541 NTSTATUS ret;
542 STRING upLeft, upRight;
544 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
546 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
548 ret = RtlEqualString( &upLeft, &upRight, FALSE );
549 RtlFreeOemString( &upRight );
551 RtlFreeOemString( &upLeft );
553 return ret;
557 /**************************************************************************
558 * RtlEqualDomainName (NTDLL.@)
560 * Determine if two domain names are the same.
562 * PARAMS
563 * left [I] First domain name
564 * right [I] Second domain name
566 * RETURNS
567 * 0 if the names are equal, non-zero otherwise.
569 * NOTES
570 * The comparison is case insensitive.
572 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
573 const UNICODE_STRING *right)
575 return RtlEqualComputerName(left, right);
580 COPY BETWEEN ANSI_STRING or UNICODE_STRING
581 there is no parameter checking, it just crashes
585 /**************************************************************************
586 * RtlAnsiStringToUnicodeString (NTDLL.@)
588 * Converts an ansi string to an unicode string.
590 * RETURNS
591 * Success: STATUS_SUCCESS. uni contains the converted string
592 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
593 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
594 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
596 * NOTES
597 * This function always writes a terminating '\0'.
599 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
600 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
601 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
602 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
604 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
606 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
607 uni->Length = total - sizeof(WCHAR);
608 if (doalloc)
610 uni->MaximumLength = total;
611 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
612 return STATUS_NO_MEMORY;
614 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
616 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
617 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
618 return STATUS_SUCCESS;
622 /**************************************************************************
623 * RtlOemStringToUnicodeString (NTDLL.@)
625 * Converts an oem string to an unicode string.
627 * RETURNS
628 * Success: STATUS_SUCCESS. uni contains the converted string
629 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
630 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
631 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
633 * NOTES
634 * This function always writes a terminating '\0'.
636 NTSTATUS WINAPI RtlOemStringToUnicodeString(
637 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
638 const STRING *oem, /* [I] Oem string to be converted */
639 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
641 DWORD total = RtlOemStringToUnicodeSize( oem );
643 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
644 uni->Length = total - sizeof(WCHAR);
645 if (doalloc)
647 uni->MaximumLength = total;
648 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
649 return STATUS_NO_MEMORY;
651 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
653 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
654 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
655 return STATUS_SUCCESS;
659 /**************************************************************************
660 * RtlUnicodeStringToAnsiString (NTDLL.@)
662 * Converts an unicode string to an ansi string.
664 * RETURNS
665 * Success: STATUS_SUCCESS. ansi contains the converted string
666 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
667 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
669 * NOTES
670 * This function always writes a terminating '\0'.
671 * It performs a partial copy if ansi is too small.
673 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
674 STRING *ansi, /* [I/O] Destination for the ansi string */
675 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
676 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
678 NTSTATUS ret = STATUS_SUCCESS;
679 DWORD len = RtlUnicodeStringToAnsiSize( uni );
681 ansi->Length = len - 1;
682 if (doalloc)
684 ansi->MaximumLength = len;
685 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
686 return STATUS_NO_MEMORY;
688 else if (ansi->MaximumLength < len)
690 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
691 ansi->Length = ansi->MaximumLength - 1;
692 ret = STATUS_BUFFER_OVERFLOW;
695 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
696 ansi->Buffer[ansi->Length] = 0;
697 return ret;
701 /**************************************************************************
702 * RtlUnicodeStringToOemString (NTDLL.@)
704 * Converts a Rtl Unicode string to an OEM string.
706 * PARAMS
707 * oem [O] Destination for OEM string
708 * uni [I] Source Unicode string
709 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
711 * RETURNS
712 * Success: STATUS_SUCCESS. oem contains the converted string
713 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
714 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
716 * NOTES
717 * If doalloc is TRUE, the length allocated is uni->Length + 1.
718 * This function always '\0' terminates the string returned.
720 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
721 const UNICODE_STRING *uni,
722 BOOLEAN doalloc )
724 NTSTATUS ret = STATUS_SUCCESS;
725 DWORD len = RtlUnicodeStringToOemSize( uni );
727 oem->Length = len - 1;
728 if (doalloc)
730 oem->MaximumLength = len;
731 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
732 return STATUS_NO_MEMORY;
734 else if (oem->MaximumLength < len)
736 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
737 oem->Length = oem->MaximumLength - 1;
738 ret = STATUS_BUFFER_OVERFLOW;
741 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
742 oem->Buffer[oem->Length] = 0;
743 return ret;
747 /**************************************************************************
748 * RtlMultiByteToUnicodeN (NTDLL.@)
750 * Converts a multi-byte string to a Unicode string.
752 * RETURNS
753 * NTSTATUS code
755 * NOTES
756 * Performs a partial copy if dst is too small.
758 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
759 LPCSTR src, DWORD srclen )
762 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
763 if (reslen)
764 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
765 return STATUS_SUCCESS;
769 /**************************************************************************
770 * RtlOemToUnicodeN (NTDLL.@)
772 * Converts a multi-byte string in the OEM code page to a Unicode string.
774 * RETURNS
775 * NTSTATUS code
777 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
778 LPCSTR src, DWORD srclen )
780 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
781 if (reslen)
782 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
783 return STATUS_SUCCESS;
787 /**************************************************************************
788 * RtlUnicodeToMultiByteN (NTDLL.@)
790 * Converts a Unicode string to a multi-byte string in the ANSI code page.
792 * RETURNS
793 * NTSTATUS code
795 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
796 LPCWSTR src, DWORD srclen )
798 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
799 dst, dstlen, NULL, NULL );
800 if (reslen)
801 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
802 return STATUS_SUCCESS;
806 /**************************************************************************
807 * RtlUnicodeToOemN (NTDLL.@)
809 * Converts a Unicode string to a multi-byte string in the OEM code page.
811 * RETURNS
812 * NTSTATUS code
814 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
815 LPCWSTR src, DWORD srclen )
817 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
818 dst, dstlen, NULL, NULL );
819 if (reslen)
820 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
821 return STATUS_SUCCESS;
826 CASE CONVERSIONS
830 /**************************************************************************
831 * RtlUpperChar (NTDLL.@)
833 * Converts an Ascii character to uppercase.
835 * PARAMS
836 * ch [I] Character to convert
838 * RETURNS
839 * The uppercase character value.
841 * NOTES
842 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
843 * All other input characters are returned unchanged. The locale and
844 * multibyte characters are not taken into account (as native DLL).
846 CHAR WINAPI RtlUpperChar( CHAR ch )
848 if (ch >= 'a' && ch <= 'z') {
849 return ch - 'a' + 'A';
850 } else {
851 return ch;
852 } /* if */
856 /**************************************************************************
857 * RtlUpperString (NTDLL.@)
859 * Converts an Ascii string to uppercase.
861 * PARAMS
862 * dst [O] Destination for converted string
863 * src [I] Source string to convert
865 * RETURNS
866 * Nothing.
868 * NOTES
869 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
870 * All other src characters are copied unchanged to dst. The locale and
871 * multibyte characters are not taken into account (as native DLL).
872 * The number of character copied is the minimum of src->Length and
873 * the dst->MaximumLength.
875 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
877 unsigned int i, len = min(src->Length, dst->MaximumLength);
879 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
880 dst->Length = len;
884 /**************************************************************************
885 * RtlUpcaseUnicodeChar (NTDLL.@)
887 * Converts an Unicode character to uppercase.
889 * PARAMS
890 * wch [I] Character to convert
892 * RETURNS
893 * The uppercase character value.
895 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
897 return toupperW(wch);
901 /**************************************************************************
902 * RtlDowncaseUnicodeChar (NTDLL.@)
904 * Converts an Unicode character to lowercase.
906 * PARAMS
907 * wch [I] Character to convert
909 * RETURNS
910 * The lowercase character value.
912 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
914 return tolowerW(wch);
918 /**************************************************************************
919 * RtlUpcaseUnicodeString (NTDLL.@)
921 * Converts an Unicode string to uppercase.
923 * PARAMS
924 * dest [O] Destination for converted string
925 * src [I] Source string to convert
926 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
928 * RETURNS
929 * Success: STATUS_SUCCESS. dest contains the converted string.
930 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
931 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
933 * NOTES
934 * dest is never '\0' terminated because it may be equal to src, and src
935 * might not be '\0' terminated. dest->Length is only set upon success.
937 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
938 const UNICODE_STRING *src,
939 BOOLEAN doalloc)
941 DWORD i, len = src->Length;
943 if (doalloc)
945 dest->MaximumLength = len;
946 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
947 return STATUS_NO_MEMORY;
949 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
951 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
952 dest->Length = len;
953 return STATUS_SUCCESS;
957 /**************************************************************************
958 * RtlDowncaseUnicodeString (NTDLL.@)
960 * Converts an Unicode string to lowercase.
962 * PARAMS
963 * dest [O] Destination for converted string
964 * src [I] Source string to convert
965 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
967 * RETURNS
968 * Success: STATUS_SUCCESS. dest contains the converted string.
969 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
970 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
972 * NOTES
973 * dest is never '\0' terminated because it may be equal to src, and src
974 * might not be '\0' terminated. dest->Length is only set upon success.
976 NTSTATUS WINAPI RtlDowncaseUnicodeString(
977 UNICODE_STRING *dest,
978 const UNICODE_STRING *src,
979 BOOLEAN doalloc)
981 DWORD i;
982 DWORD len = src->Length;
984 if (doalloc) {
985 dest->MaximumLength = len;
986 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
987 return STATUS_NO_MEMORY;
988 } /* if */
989 } else if (len > dest->MaximumLength) {
990 return STATUS_BUFFER_OVERFLOW;
991 } /* if */
993 for (i = 0; i < len/sizeof(WCHAR); i++) {
994 dest->Buffer[i] = tolowerW(src->Buffer[i]);
995 } /* for */
996 dest->Length = len;
997 return STATUS_SUCCESS;
1001 /**************************************************************************
1002 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1004 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1006 * RETURNS
1007 * NTSTATUS code
1009 * NOTES
1010 * writes terminating 0
1012 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1013 const UNICODE_STRING *src,
1014 BOOLEAN doalloc )
1016 NTSTATUS ret;
1017 UNICODE_STRING upcase;
1019 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1021 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1022 RtlFreeUnicodeString( &upcase );
1024 return ret;
1028 /**************************************************************************
1029 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1031 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1032 * stored in STRING format.
1034 * RETURNS
1035 * NTSTATUS code
1037 * NOTES
1038 * writes terminating 0
1040 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1041 const UNICODE_STRING *src,
1042 BOOLEAN doalloc )
1044 NTSTATUS ret;
1045 UNICODE_STRING upcase;
1047 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1049 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1050 RtlFreeUnicodeString( &upcase );
1052 return ret;
1056 /**************************************************************************
1057 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1059 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1060 * stored in STRING format.
1062 * RETURNS
1063 * NTSTATUS code
1065 * NOTES
1066 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1068 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1069 const UNICODE_STRING *uni,
1070 BOOLEAN doalloc )
1072 NTSTATUS ret;
1073 UNICODE_STRING upcase;
1075 if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
1077 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1078 oem->Length = len;
1079 if (doalloc)
1081 oem->MaximumLength = len;
1082 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1084 ret = STATUS_NO_MEMORY;
1085 goto done;
1088 else if (oem->MaximumLength < len)
1090 ret = STATUS_BUFFER_OVERFLOW;
1091 oem->Length = oem->MaximumLength;
1092 if (!oem->MaximumLength) goto done;
1094 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1095 done:
1096 RtlFreeUnicodeString( &upcase );
1098 return ret;
1102 /**************************************************************************
1103 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1105 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1107 * RETURNS
1108 * NTSTATUS code
1110 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1111 LPCWSTR src, DWORD srclen )
1113 NTSTATUS ret;
1114 LPWSTR upcase;
1115 DWORD i;
1117 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1118 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1119 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1120 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1121 return ret;
1125 /**************************************************************************
1126 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1128 * Converts a Unicode string to the equivalent OEM upper-case representation.
1130 * RETURNS
1131 * NTSTATUS code
1133 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1134 LPCWSTR src, DWORD srclen )
1136 NTSTATUS ret;
1137 LPWSTR upcase;
1138 DWORD i;
1140 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1141 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1142 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1143 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1144 return ret;
1149 STRING SIZE
1153 /**************************************************************************
1154 * RtlOemStringToUnicodeSize (NTDLL.@)
1155 * RtlxOemStringToUnicodeSize (NTDLL.@)
1157 * Calculate the size in bytes necessary for the Unicode conversion of str,
1158 * including the terminating '\0'.
1160 * PARAMS
1161 * str [I] String to calculate the size of
1163 * RETURNS
1164 * The calculated size.
1166 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1168 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1169 return (ret + 1) * sizeof(WCHAR);
1173 /**************************************************************************
1174 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1175 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1177 * Calculate the size in bytes necessary for the Unicode conversion of str,
1178 * including the terminating '\0'.
1180 * PARAMS
1181 * str [I] String to calculate the size of
1183 * RETURNS
1184 * The calculated size.
1186 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1188 DWORD ret;
1189 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1190 return ret + sizeof(WCHAR);
1194 /**************************************************************************
1195 * RtlMultiByteToUnicodeSize (NTDLL.@)
1197 * Compute the size in bytes necessary for the Unicode conversion of str,
1198 * without the terminating '\0'.
1200 * PARAMS
1201 * size [O] Destination for size
1202 * str [I] String to calculate the size of
1203 * len [I] Length of str
1205 * RETURNS
1206 * STATUS_SUCCESS.
1208 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1210 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1211 return STATUS_SUCCESS;
1215 /**************************************************************************
1216 * RtlUnicodeToMultiByteSize (NTDLL.@)
1218 * Calculate the size in bytes necessary for the multibyte conversion of str,
1219 * without the terminating '\0'.
1221 * PARAMS
1222 * size [O] Destination for size
1223 * str [I] String to calculate the size of
1224 * len [I] Length of str
1226 * RETURNS
1227 * STATUS_SUCCESS.
1229 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1231 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1232 return STATUS_SUCCESS;
1236 /**************************************************************************
1237 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1238 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1240 * Calculate the size in bytes necessary for the Ansi conversion of str,
1241 * including the terminating '\0'.
1243 * PARAMS
1244 * str [I] String to calculate the size of
1246 * RETURNS
1247 * The calculated size.
1249 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1251 DWORD ret;
1252 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1253 return ret + 1;
1257 /**************************************************************************
1258 * RtlUnicodeStringToOemSize (NTDLL.@)
1259 * RtlxUnicodeStringToOemSize (NTDLL.@)
1261 * Calculate the size in bytes necessary for the OEM conversion of str,
1262 * including the terminating '\0'.
1264 * PARAMS
1265 * str [I] String to calculate the size of
1267 * RETURNS
1268 * The calculated size.
1270 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1272 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1273 NULL, 0, NULL, NULL ) + 1;
1277 /**************************************************************************
1278 * RtlAppendAsciizToString (NTDLL.@)
1280 * Concatenates a buffered character string and a '\0' terminated character
1281 * string
1283 * RETURNS
1284 * Success: STATUS_SUCCESS. src is appended to dest.
1285 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1286 * to hold the concatenated string.
1288 * NOTES
1289 * if src is NULL dest is unchanged.
1290 * dest is never '\0' terminated.
1292 NTSTATUS WINAPI RtlAppendAsciizToString(
1293 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1294 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1296 if (src != NULL) {
1297 unsigned int src_len = strlen(src);
1298 unsigned int dest_len = src_len + dest->Length;
1300 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1301 memcpy(dest->Buffer + dest->Length, src, src_len);
1302 dest->Length = dest_len;
1303 } /* if */
1304 return STATUS_SUCCESS;
1308 /**************************************************************************
1309 * RtlAppendStringToString (NTDLL.@)
1311 * Concatenates two buffered character strings
1313 * RETURNS
1314 * Success: STATUS_SUCCESS. src is appended to dest.
1315 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1316 * to hold the concatenated string.
1318 * NOTES
1319 * if src->length is zero dest is unchanged.
1320 * dest is never '\0' terminated.
1322 NTSTATUS WINAPI RtlAppendStringToString(
1323 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1324 const STRING *src) /* [I] Buffered character string to be concatenated */
1326 if (src->Length != 0) {
1327 unsigned int dest_len = src->Length + dest->Length;
1329 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1330 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1331 dest->Length = dest_len;
1332 } /* if */
1333 return STATUS_SUCCESS;
1337 /**************************************************************************
1338 * RtlAppendUnicodeToString (NTDLL.@)
1340 * Concatenates an buffered unicode string and a '\0' terminated unicode
1341 * string
1343 * RETURNS
1344 * Success: STATUS_SUCCESS. src is appended to dest.
1345 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1346 * to hold the concatenated string.
1348 * NOTES
1349 * if src is NULL dest is unchanged.
1350 * dest is '\0' terminated when the MaximumLength allowes it.
1351 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1353 * DIFFERENCES
1354 * Does not write in the src->Buffer beyond MaximumLength when
1355 * MaximumLength is odd as the native function does.
1357 NTSTATUS WINAPI RtlAppendUnicodeToString(
1358 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1359 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1361 if (src != NULL) {
1362 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1363 unsigned int dest_len = src_len + dest->Length;
1365 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1366 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1367 dest->Length = dest_len;
1368 /* append terminating '\0' if enough space */
1369 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1370 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1371 } /* if */
1372 } /* if */
1373 return STATUS_SUCCESS;
1377 /**************************************************************************
1378 * RtlAppendUnicodeStringToString (NTDLL.@)
1380 * Concatenates two buffered unicode strings
1382 * RETURNS
1383 * Success: STATUS_SUCCESS. src is appended to dest.
1384 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1385 * to hold the concatenated string.
1387 * NOTES
1388 * if src->length is zero dest is unchanged.
1389 * dest is '\0' terminated when the MaximumLength allowes it.
1390 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1392 * DIFFERENCES
1393 * Does not write in the src->Buffer beyond MaximumLength when
1394 * MaximumLength is odd as the native function does.
1396 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1397 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1398 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1400 if (src->Length != 0) {
1401 unsigned int dest_len = src->Length + dest->Length;
1403 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1404 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1405 dest->Length = dest_len;
1406 /* append terminating '\0' if enough space */
1407 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1408 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1409 } /* if */
1410 } /* if */
1411 return STATUS_SUCCESS;
1415 /**************************************************************************
1416 * RtlFindCharInUnicodeString (NTDLL.@)
1418 * Searches for one of several unicode characters in an unicode string.
1420 * RETURNS
1421 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1422 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1424 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1425 int flags, /* [I] Flags */
1426 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1427 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1428 USHORT *pos) /* [O] Position of the first character found + 2 */
1430 int main_idx;
1431 unsigned int search_idx;
1433 switch (flags) {
1434 case 0:
1435 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1436 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1437 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1438 *pos = (main_idx + 1) * sizeof(WCHAR);
1439 return STATUS_SUCCESS;
1443 *pos = 0;
1444 return STATUS_NOT_FOUND;
1445 case 1:
1446 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1447 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1448 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1449 *pos = main_idx * sizeof(WCHAR);
1450 return STATUS_SUCCESS;
1454 *pos = 0;
1455 return STATUS_NOT_FOUND;
1456 case 2:
1457 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1458 search_idx = 0;
1459 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1460 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1461 search_idx++;
1463 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1464 *pos = (main_idx + 1) * sizeof(WCHAR);
1465 return STATUS_SUCCESS;
1468 *pos = 0;
1469 return STATUS_NOT_FOUND;
1470 case 3:
1471 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1472 search_idx = 0;
1473 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1474 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1475 search_idx++;
1477 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1478 *pos = main_idx * sizeof(WCHAR);
1479 return STATUS_SUCCESS;
1482 *pos = 0;
1483 return STATUS_NOT_FOUND;
1484 } /* switch */
1485 return STATUS_NOT_FOUND;
1490 MISC
1492 /* Tests that we currently implement */
1493 #define ITU_IMPLEMENTED_TESTS \
1494 (IS_TEXT_UNICODE_SIGNATURE | \
1495 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1496 IS_TEXT_UNICODE_ODD_LENGTH | \
1497 IS_TEXT_UNICODE_STATISTICS | \
1498 IS_TEXT_UNICODE_NULL_BYTES)
1500 /**************************************************************************
1501 * RtlIsTextUnicode (NTDLL.@)
1503 * Attempt to guess whether a text buffer is Unicode.
1505 * PARAMS
1506 * buf [I] Text buffer to test
1507 * len [I] Length of buf
1508 * pf [O] Destination for test results
1510 * RETURNS
1511 * The length of the string if all tests were passed, 0 otherwise.
1513 * FIXME
1514 * Should implement more tests.
1516 DWORD WINAPI RtlIsTextUnicode(
1517 LPVOID buf,
1518 DWORD len,
1519 DWORD *pf)
1521 LPWSTR s = buf;
1522 DWORD flags = -1, out_flags = 0;
1524 if (!len)
1525 goto out;
1526 if (pf)
1527 flags = *pf;
1529 * Apply various tests to the text string. According to the
1530 * docs, each test "passed" sets the corresponding flag in
1531 * the output flags. But some of the tests are mutually
1532 * exclusive, so I don't see how you could pass all tests ...
1535 /* Check for an odd length ... pass if even. */
1536 if ((flags & IS_TEXT_UNICODE_ODD_LENGTH) && (len & 1))
1537 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1539 /* Check for the special byte order unicode marks. */
1540 if ((flags & IS_TEXT_UNICODE_SIGNATURE) && *s == 0xFEFF)
1541 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1543 if ((flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE) && *s == 0xFFFE)
1544 out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1546 /* apply some statistical analysis */
1547 if (flags & IS_TEXT_UNICODE_STATISTICS)
1549 DWORD i, stats = 0;
1550 /* FIXME: checks only for ASCII characters in the unicode stream */
1551 for (i = 0; i < len / sizeof(WCHAR); i++)
1553 if (s[i] <= 255) stats++;
1555 if (stats > len / sizeof(WCHAR) / 2)
1556 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1559 /* Check for unicode NULL chars */
1560 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1562 DWORD i;
1563 for (i = 0; i < len / sizeof(WCHAR); i++)
1565 if (!s[i])
1567 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1568 break;
1574 * Check whether the string passed all of the tests.
1576 flags &= ITU_IMPLEMENTED_TESTS;
1577 if ((out_flags & flags) != flags)
1578 len = 0;
1579 out:
1580 if (pf)
1581 *pf = out_flags;
1582 return len;
1586 /**************************************************************************
1587 * RtlCharToInteger (NTDLL.@)
1589 * Converts a character string into its integer equivalent.
1591 * RETURNS
1592 * Success: STATUS_SUCCESS. value contains the converted number
1593 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1594 * STATUS_ACCESS_VIOLATION, if value is NULL.
1596 * NOTES
1597 * For base 0 it uses 10 as base and the string should be in the format
1598 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1599 * For other bases the string should be in the format
1600 * "{whitespace} [+|-] {digits}".
1601 * No check is made for value overflow, only the lower 32 bits are assigned.
1602 * If str is NULL it crashes, as the native function does.
1604 * DIFFERENCES
1605 * This function does not read garbage behind '\0' as the native version does.
1607 NTSTATUS WINAPI RtlCharToInteger(
1608 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1609 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1610 ULONG *value) /* [O] Destination for the converted value */
1612 CHAR chCurrent;
1613 int digit;
1614 ULONG RunningTotal = 0;
1615 char bMinus = 0;
1617 while (*str != '\0' && *str <= ' ') {
1618 str++;
1619 } /* while */
1621 if (*str == '+') {
1622 str++;
1623 } else if (*str == '-') {
1624 bMinus = 1;
1625 str++;
1626 } /* if */
1628 if (base == 0) {
1629 base = 10;
1630 if (str[0] == '0') {
1631 if (str[1] == 'b') {
1632 str += 2;
1633 base = 2;
1634 } else if (str[1] == 'o') {
1635 str += 2;
1636 base = 8;
1637 } else if (str[1] == 'x') {
1638 str += 2;
1639 base = 16;
1640 } /* if */
1641 } /* if */
1642 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1643 return STATUS_INVALID_PARAMETER;
1644 } /* if */
1646 if (value == NULL) {
1647 return STATUS_ACCESS_VIOLATION;
1648 } /* if */
1650 while (*str != '\0') {
1651 chCurrent = *str;
1652 if (chCurrent >= '0' && chCurrent <= '9') {
1653 digit = chCurrent - '0';
1654 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1655 digit = chCurrent - 'A' + 10;
1656 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1657 digit = chCurrent - 'a' + 10;
1658 } else {
1659 digit = -1;
1660 } /* if */
1661 if (digit < 0 || digit >= base) {
1662 *value = bMinus ? -RunningTotal : RunningTotal;
1663 return STATUS_SUCCESS;
1664 } /* if */
1666 RunningTotal = RunningTotal * base + digit;
1667 str++;
1668 } /* while */
1670 *value = bMinus ? -RunningTotal : RunningTotal;
1671 return STATUS_SUCCESS;
1675 /**************************************************************************
1676 * RtlIntegerToChar (NTDLL.@)
1678 * Converts an unsigned integer to a character string.
1680 * RETURNS
1681 * Success: STATUS_SUCCESS. str contains the converted number
1682 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1683 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1684 * STATUS_ACCESS_VIOLATION, if str is NULL.
1686 * NOTES
1687 * Instead of base 0 it uses 10 as base.
1688 * Writes at most length characters to the string str.
1689 * Str is '\0' terminated when length allowes it.
1690 * When str fits exactly in length characters the '\0' is ommitted.
1692 NTSTATUS WINAPI RtlIntegerToChar(
1693 ULONG value, /* [I] Value to be converted */
1694 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1695 ULONG length, /* [I] Length of the str buffer in bytes */
1696 PCHAR str) /* [O] Destination for the converted value */
1698 CHAR buffer[33];
1699 PCHAR pos;
1700 CHAR digit;
1701 ULONG len;
1703 if (base == 0) {
1704 base = 10;
1705 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1706 return STATUS_INVALID_PARAMETER;
1707 } /* if */
1709 pos = &buffer[32];
1710 *pos = '\0';
1712 do {
1713 pos--;
1714 digit = value % base;
1715 value = value / base;
1716 if (digit < 10) {
1717 *pos = '0' + digit;
1718 } else {
1719 *pos = 'A' + digit - 10;
1720 } /* if */
1721 } while (value != 0L);
1723 len = &buffer[32] - pos;
1724 if (len > length) {
1725 return STATUS_BUFFER_OVERFLOW;
1726 } else if (str == NULL) {
1727 return STATUS_ACCESS_VIOLATION;
1728 } else if (len == length) {
1729 memcpy(str, pos, len);
1730 } else {
1731 memcpy(str, pos, len + 1);
1732 } /* if */
1733 return STATUS_SUCCESS;
1737 /**************************************************************************
1738 * RtlUnicodeStringToInteger (NTDLL.@)
1740 * Converts an unicode string into its integer equivalent.
1742 * RETURNS
1743 * Success: STATUS_SUCCESS. value contains the converted number
1744 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1745 * STATUS_ACCESS_VIOLATION, if value is NULL.
1747 * NOTES
1748 * For base 0 it uses 10 as base and the string should be in the format
1749 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1750 * For other bases the string should be in the format
1751 * "{whitespace} [+|-] {digits}".
1752 * No check is made for value overflow, only the lower 32 bits are assigned.
1753 * If str is NULL it crashes, as the native function does.
1755 * DIFFERENCES
1756 * This function does not read garbage on string length 0 as the native
1757 * version does.
1759 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1760 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1761 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1762 ULONG *value) /* [O] Destination for the converted value */
1764 LPWSTR lpwstr = str->Buffer;
1765 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1766 WCHAR wchCurrent;
1767 int digit;
1768 ULONG RunningTotal = 0;
1769 char bMinus = 0;
1771 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1772 lpwstr++;
1773 CharsRemaining--;
1774 } /* while */
1776 if (CharsRemaining >= 1) {
1777 if (*lpwstr == '+') {
1778 lpwstr++;
1779 CharsRemaining--;
1780 } else if (*lpwstr == '-') {
1781 bMinus = 1;
1782 lpwstr++;
1783 CharsRemaining--;
1784 } /* if */
1785 } /* if */
1787 if (base == 0) {
1788 base = 10;
1789 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1790 if (lpwstr[1] == 'b') {
1791 lpwstr += 2;
1792 CharsRemaining -= 2;
1793 base = 2;
1794 } else if (lpwstr[1] == 'o') {
1795 lpwstr += 2;
1796 CharsRemaining -= 2;
1797 base = 8;
1798 } else if (lpwstr[1] == 'x') {
1799 lpwstr += 2;
1800 CharsRemaining -= 2;
1801 base = 16;
1802 } /* if */
1803 } /* if */
1804 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1805 return STATUS_INVALID_PARAMETER;
1806 } /* if */
1808 if (value == NULL) {
1809 return STATUS_ACCESS_VIOLATION;
1810 } /* if */
1812 while (CharsRemaining >= 1) {
1813 wchCurrent = *lpwstr;
1814 if (wchCurrent >= '0' && wchCurrent <= '9') {
1815 digit = wchCurrent - '0';
1816 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1817 digit = wchCurrent - 'A' + 10;
1818 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1819 digit = wchCurrent - 'a' + 10;
1820 } else {
1821 digit = -1;
1822 } /* if */
1823 if (digit < 0 || digit >= base) {
1824 *value = bMinus ? -RunningTotal : RunningTotal;
1825 return STATUS_SUCCESS;
1826 } /* if */
1828 RunningTotal = RunningTotal * base + digit;
1829 lpwstr++;
1830 CharsRemaining--;
1831 } /* while */
1833 *value = bMinus ? -RunningTotal : RunningTotal;
1834 return STATUS_SUCCESS;
1838 /**************************************************************************
1839 * RtlIntegerToUnicodeString (NTDLL.@)
1841 * Converts an unsigned integer to a '\0' terminated unicode string.
1843 * RETURNS
1844 * Success: STATUS_SUCCESS. str contains the converted number
1845 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1846 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1847 * (with the '\0' termination). In this case str->Length
1848 * is set to the length, the string would have (which can
1849 * be larger than the MaximumLength).
1851 * NOTES
1852 * Instead of base 0 it uses 10 as base.
1853 * If str is NULL it crashes, as the native function does.
1855 * DIFFERENCES
1856 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1857 * The native function does this when the string would be longer than 16
1858 * characters even when the string parameter is long enough.
1860 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1861 ULONG value, /* [I] Value to be converted */
1862 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1863 UNICODE_STRING *str) /* [O] Destination for the converted value */
1865 WCHAR buffer[33];
1866 PWCHAR pos;
1867 WCHAR digit;
1869 if (base == 0) {
1870 base = 10;
1871 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1872 return STATUS_INVALID_PARAMETER;
1873 } /* if */
1875 pos = &buffer[32];
1876 *pos = '\0';
1878 do {
1879 pos--;
1880 digit = value % base;
1881 value = value / base;
1882 if (digit < 10) {
1883 *pos = '0' + digit;
1884 } else {
1885 *pos = 'A' + digit - 10;
1886 } /* if */
1887 } while (value != 0L);
1889 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1890 if (str->Length >= str->MaximumLength) {
1891 return STATUS_BUFFER_OVERFLOW;
1892 } else {
1893 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1894 } /* if */
1895 return STATUS_SUCCESS;
1899 /*************************************************************************
1900 * RtlGUIDFromString (NTDLL.@)
1902 * Convert a string representation of a GUID into a GUID.
1904 * PARAMS
1905 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1906 * guid [O] Destination for the converted GUID
1908 * RETURNS
1909 * Success: STATUS_SUCCESS. guid contains the converted value.
1910 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1912 * SEE ALSO
1913 * See RtlStringFromGUID.
1915 NTSTATUS WINAPI RtlGUIDFromString(const UNICODE_STRING *str, GUID* guid)
1917 int i = 0;
1918 const WCHAR *lpszCLSID = str->Buffer;
1919 BYTE* lpOut = (BYTE*)guid;
1921 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1923 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1924 * to memory: DWORD... WORD WORD BYTES............
1926 while (i < 37)
1928 switch (i)
1930 case 0:
1931 if (*lpszCLSID != '{')
1932 return STATUS_INVALID_PARAMETER;
1933 break;
1935 case 9: case 14: case 19: case 24:
1936 if (*lpszCLSID != '-')
1937 return STATUS_INVALID_PARAMETER;
1938 break;
1940 case 37:
1941 if (*lpszCLSID != '}')
1942 return STATUS_INVALID_PARAMETER;
1943 break;
1945 default:
1947 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
1948 unsigned char byte;
1950 /* Read two hex digits as a byte value */
1951 if (ch >= '0' && ch <= '9') ch = ch - '0';
1952 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
1953 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
1954 else return STATUS_INVALID_PARAMETER;
1956 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
1957 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
1958 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
1959 else return STATUS_INVALID_PARAMETER;
1961 byte = ch << 4 | ch2;
1963 switch (i)
1965 #ifndef WORDS_BIGENDIAN
1966 /* For Big Endian machines, we store the data such that the
1967 * dword/word members can be read as DWORDS and WORDS correctly. */
1968 /* Dword */
1969 case 1: lpOut[3] = byte; break;
1970 case 3: lpOut[2] = byte; break;
1971 case 5: lpOut[1] = byte; break;
1972 case 7: lpOut[0] = byte; lpOut += 4; break;
1973 /* Word */
1974 case 10: case 15: lpOut[1] = byte; break;
1975 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
1976 #endif
1977 /* Byte */
1978 default: lpOut[0] = byte; lpOut++; break;
1980 lpszCLSID++; /* Skip 2nd character of byte */
1981 i++;
1984 lpszCLSID++;
1985 i++;
1988 return STATUS_SUCCESS;
1991 /*************************************************************************
1992 * RtlStringFromGUID (NTDLL.@)
1994 * Convert a GUID into a string representation of a GUID.
1996 * PARAMS
1997 * guid [I] GUID to convert
1998 * str [O] Destination for the converted string
2000 * RETURNS
2001 * Success: STATUS_SUCCESS. str contains the converted value.
2002 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2004 * SEE ALSO
2005 * See RtlGUIDFromString.
2007 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2009 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2010 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2011 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2012 '%','0','2','X','%','0','2','X','}','\0' };
2014 TRACE("(%p,%p)\n", guid, str);
2016 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2017 str->MaximumLength = str->Length + sizeof(WCHAR);
2018 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2019 if (!str->Buffer)
2021 str->Length = str->MaximumLength = 0;
2022 return STATUS_NO_MEMORY;
2024 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2025 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2026 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2028 return STATUS_SUCCESS;