winhelp: Window style has not been initialized.
[wine/multimedia.git] / dlls / ntdll / rtlstr.c
blob8e997e72ba863a1a56d047cce5b2a63596a92c6b
1 /*
2 * Rtl string functions
4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright (C) 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winnt.h"
34 #include "winternl.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 #define GUID_STRING_LENGTH 38
43 UINT NlsAnsiCodePage = 0;
44 BYTE NlsMbCodePageTag = 0;
45 BYTE NlsMbOemCodePageTag = 0;
47 static const union cptable *ansi_table;
48 static const union cptable *oem_table;
49 static const union cptable* unix_table; /* NULL if UTF8 */
52 /**************************************************************************
53 * __wine_init_codepages (NTDLL.@)
55 * Set the code page once kernel32 is loaded. Should be done differently.
57 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
58 const union cptable *ucp)
60 ansi_table = ansi;
61 oem_table = oem;
62 unix_table = ucp;
63 NlsAnsiCodePage = ansi->info.codepage;
66 int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
68 return (unix_table) ?
69 wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
70 wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
73 int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
74 const char* defchar, int *used )
76 if (unix_table)
77 return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
78 if (used) *used = 0; /* all chars are valid for UTF-8 */
79 return wine_utf8_wcstombs( src, srclen, dst, dstlen );
82 /**************************************************************************
83 * RtlInitAnsiString (NTDLL.@)
85 * Initializes a buffered ansi string.
87 * RETURNS
88 * Nothing.
90 * NOTES
91 * Assigns source to target->Buffer. The length of source is assigned to
92 * target->Length and target->MaximumLength. If source is NULL the length
93 * of source is assumed to be 0.
95 void WINAPI RtlInitAnsiString(
96 PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
97 PCSZ source) /* [I] '\0' terminated string used to initialize target */
99 if ((target->Buffer = (PCHAR) source))
101 target->Length = strlen(source);
102 target->MaximumLength = target->Length + 1;
104 else target->Length = target->MaximumLength = 0;
107 /**************************************************************************
108 * RtlInitAnsiStringEx (NTDLL.@)
110 * Initializes a buffered ansi string.
112 * RETURNS
113 * An appropriate NTSTATUS value.
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 NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
122 if (source)
124 unsigned int len = strlen(source);
125 if (len+1 > 0xffff)
126 return STATUS_NAME_TOO_LONG;
128 target->Buffer = (PCHAR) source;
129 target->Length = len;
130 target->MaximumLength = len + 1;
132 else
134 target->Buffer = NULL;
135 target->Length = 0;
136 target->MaximumLength = 0;
138 return STATUS_SUCCESS;
141 /**************************************************************************
142 * RtlInitString (NTDLL.@)
144 * Initializes a buffered string.
146 * RETURNS
147 * Nothing.
149 * NOTES
150 * Assigns source to target->Buffer. The length of source is assigned to
151 * target->Length and target->MaximumLength. If source is NULL the length
152 * of source is assumed to be 0.
154 void WINAPI RtlInitString(
155 PSTRING target, /* [I/O] Buffered string to be initialized */
156 PCSZ source) /* [I] '\0' terminated string used to initialize target */
158 RtlInitAnsiString( target, source );
162 /**************************************************************************
163 * RtlFreeAnsiString (NTDLL.@)
165 void WINAPI RtlFreeAnsiString( PSTRING str )
167 if (str->Buffer)
169 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
170 RtlZeroMemory( str, sizeof(*str) );
175 /**************************************************************************
176 * RtlFreeOemString (NTDLL.@)
178 void WINAPI RtlFreeOemString( PSTRING str )
180 RtlFreeAnsiString( str );
184 /**************************************************************************
185 * RtlCopyString (NTDLL.@)
187 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
189 if (src)
191 unsigned int len = min( src->Length, dst->MaximumLength );
192 memcpy( dst->Buffer, src->Buffer, len );
193 dst->Length = len;
195 else dst->Length = 0;
199 /**************************************************************************
200 * RtlInitUnicodeString (NTDLL.@)
202 * Initializes a buffered unicode string.
204 * RETURNS
205 * Nothing.
207 * NOTES
208 * Assigns source to target->Buffer. The length of source is assigned to
209 * target->Length and target->MaximumLength. If source is NULL the length
210 * of source is assumed to be 0.
212 void WINAPI RtlInitUnicodeString(
213 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
214 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
216 if ((target->Buffer = (PWSTR) source))
218 target->Length = strlenW(source) * sizeof(WCHAR);
219 target->MaximumLength = target->Length + sizeof(WCHAR);
221 else target->Length = target->MaximumLength = 0;
225 /**************************************************************************
226 * RtlInitUnicodeStringEx (NTDLL.@)
228 * Initializes a buffered unicode string.
230 * RETURNS
231 * Success: STATUS_SUCCESS. target is initialized.
232 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
234 * NOTES
235 * Assigns source to target->Buffer. The length of source is assigned to
236 * target->Length and target->MaximumLength. If source is NULL the length
237 * of source is assumed to be 0.
239 NTSTATUS WINAPI RtlInitUnicodeStringEx(
240 PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
241 PCWSTR source) /* [I] '\0' terminated unicode string used to initialize target */
243 if (source != NULL) {
244 unsigned int len = strlenW(source) * sizeof(WCHAR);
246 if (len > 0xFFFC) {
247 return STATUS_NAME_TOO_LONG;
248 } else {
249 target->Length = len;
250 target->MaximumLength = len + sizeof(WCHAR);
251 target->Buffer = (PWSTR) source;
252 } /* if */
253 } else {
254 target->Length = 0;
255 target->MaximumLength = 0;
256 target->Buffer = NULL;
257 } /* if */
258 return STATUS_SUCCESS;
262 /**************************************************************************
263 * RtlCreateUnicodeString (NTDLL.@)
265 * Creates a UNICODE_STRING from a null-terminated Unicode string.
267 * RETURNS
268 * Success: TRUE
269 * Failure: FALSE
271 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
273 int len = (strlenW(src) + 1) * sizeof(WCHAR);
274 if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
275 memcpy( target->Buffer, src, len );
276 target->MaximumLength = len;
277 target->Length = len - sizeof(WCHAR);
278 return TRUE;
282 /**************************************************************************
283 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
285 * Creates a UNICODE_STRING from a null-terminated Ascii string.
287 * RETURNS
288 * Success: TRUE
289 * Failure: FALSE
291 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
293 STRING ansi;
294 RtlInitAnsiString( &ansi, src );
295 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
299 /**************************************************************************
300 * RtlFreeUnicodeString (NTDLL.@)
302 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
303 * RtlCreateUnicodeStringFromAsciiz().
305 * RETURNS
306 * nothing
308 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
310 if (str->Buffer)
312 RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
313 RtlZeroMemory( str, sizeof(*str) );
318 /**************************************************************************
319 * RtlCopyUnicodeString (NTDLL.@)
321 * Copies from one UNICODE_STRING to another.
323 * RETURNS
324 * nothing
326 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
328 if (src)
330 unsigned int len = min( src->Length, dst->MaximumLength );
331 memcpy( dst->Buffer, src->Buffer, len );
332 dst->Length = len;
333 /* append terminating '\0' if enough space */
334 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
336 else dst->Length = 0;
340 /**************************************************************************
341 * RtlDuplicateUnicodeString (NTDLL.@)
343 * Duplicates an unicode string.
345 * RETURNS
346 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
347 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
348 * STATUS_NO_MEMORY, if the allocation fails.
350 * NOTES
351 * For add_nul there are several possible values:
352 * 0 = destination will not be '\0' terminated,
353 * 1 = destination will be '\0' terminated,
354 * 3 = like 1 but for an empty source string produce '\0' terminated empty
355 * Buffer instead of assigning NULL to the Buffer.
356 * Other add_nul values are invalid.
358 NTSTATUS WINAPI RtlDuplicateUnicodeString(
359 int add_nul, /* [I] flag */
360 const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
361 UNICODE_STRING *destination) /* [O] destination for the duplicated unicode string */
363 if (source == NULL || destination == NULL ||
364 source->Length > source->MaximumLength ||
365 (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
366 add_nul == 2 || add_nul >= 4 || add_nul < 0) {
367 return STATUS_INVALID_PARAMETER;
368 } else {
369 if (source->Length == 0 && add_nul != 3) {
370 destination->Length = 0;
371 destination->MaximumLength = 0;
372 destination->Buffer = NULL;
373 } else {
374 unsigned int destination_max_len = source->Length;
376 if (add_nul) {
377 destination_max_len += sizeof(WCHAR);
378 } /* if */
379 destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
380 if (destination->Buffer == NULL) {
381 return STATUS_NO_MEMORY;
382 } else {
383 memcpy(destination->Buffer, source->Buffer, source->Length);
384 destination->Length = source->Length;
385 destination->MaximumLength = source->Length;
386 /* append terminating '\0' if enough space */
387 if (add_nul) {
388 destination->MaximumLength = destination_max_len;
389 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
390 } /* if */
391 } /* if */
392 } /* if */
393 } /* if */
394 return STATUS_SUCCESS;
398 /**************************************************************************
399 * RtlEraseUnicodeString (NTDLL.@)
401 * Overwrites a UNICODE_STRING with zeros.
403 * RETURNS
404 * nothing
406 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
408 if (str->Buffer)
410 memset( str->Buffer, 0, str->MaximumLength );
411 str->Length = 0;
417 COMPARISON FUNCTIONS
421 /******************************************************************************
422 * RtlCompareString (NTDLL.@)
424 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
426 unsigned int len;
427 LONG ret = 0;
428 LPCSTR p1, p2;
430 len = min(s1->Length, s2->Length);
431 p1 = s1->Buffer;
432 p2 = s2->Buffer;
434 if (CaseInsensitive)
436 while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
438 else
440 while (!ret && len--) ret = *p1++ - *p2++;
442 if (!ret) ret = s1->Length - s2->Length;
443 return ret;
447 /******************************************************************************
448 * RtlCompareUnicodeString (NTDLL.@)
450 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
451 BOOLEAN CaseInsensitive )
453 unsigned int len;
454 LONG ret = 0;
455 LPCWSTR p1, p2;
457 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
458 p1 = s1->Buffer;
459 p2 = s2->Buffer;
461 if (CaseInsensitive)
463 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
465 else
467 while (!ret && len--) ret = *p1++ - *p2++;
469 if (!ret) ret = s1->Length - s2->Length;
470 return ret;
474 /**************************************************************************
475 * RtlEqualString (NTDLL.@)
477 * Determine if two strings are equal.
479 * PARAMS
480 * s1 [I] Source string
481 * s2 [I] String to compare to s1
482 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
484 * RETURNS
485 * Non-zero if s1 is equal to s2, 0 otherwise.
487 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
489 if (s1->Length != s2->Length) return FALSE;
490 return !RtlCompareString( s1, s2, CaseInsensitive );
494 /**************************************************************************
495 * RtlEqualUnicodeString (NTDLL.@)
497 * Unicode version of RtlEqualString.
499 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
500 BOOLEAN CaseInsensitive )
502 if (s1->Length != s2->Length) return FALSE;
503 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
507 /**************************************************************************
508 * RtlPrefixString (NTDLL.@)
510 * Determine if one string is a prefix of another.
512 * PARAMS
513 * s1 [I] Prefix to look for in s2
514 * s2 [I] String that may contain s1 as a prefix
515 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
517 * RETURNS
518 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
520 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
522 unsigned int i;
524 if (s1->Length > s2->Length) return FALSE;
525 if (ignore_case)
527 for (i = 0; i < s1->Length; i++)
528 if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
530 else
532 for (i = 0; i < s1->Length; i++)
533 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
535 return TRUE;
539 /**************************************************************************
540 * RtlPrefixUnicodeString (NTDLL.@)
542 * Unicode version of RtlPrefixString.
544 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
545 const UNICODE_STRING *s2,
546 BOOLEAN ignore_case )
548 unsigned int i;
550 if (s1->Length > s2->Length) return FALSE;
551 if (ignore_case)
553 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
554 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
556 else
558 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
559 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
561 return TRUE;
565 /**************************************************************************
566 * RtlEqualComputerName (NTDLL.@)
568 * Determine if two computer names are the same.
570 * PARAMS
571 * left [I] First computer name
572 * right [I] Second computer name
574 * RETURNS
575 * 0 if the names are equal, non-zero otherwise.
577 * NOTES
578 * The comparison is case insensitive.
580 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
581 const UNICODE_STRING *right)
583 NTSTATUS ret;
584 STRING upLeft, upRight;
586 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
588 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
590 ret = RtlEqualString( &upLeft, &upRight, FALSE );
591 RtlFreeOemString( &upRight );
593 RtlFreeOemString( &upLeft );
595 return ret;
599 /**************************************************************************
600 * RtlEqualDomainName (NTDLL.@)
602 * Determine if two domain names are the same.
604 * PARAMS
605 * left [I] First domain name
606 * right [I] Second domain name
608 * RETURNS
609 * 0 if the names are equal, non-zero otherwise.
611 * NOTES
612 * The comparison is case insensitive.
614 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
615 const UNICODE_STRING *right)
617 return RtlEqualComputerName(left, right);
621 /**************************************************************************
622 * RtlAnsiCharToUnicodeChar (NTDLL.@)
624 * Converts the first ansi character to a unicode character.
626 * PARAMS
627 * ansi [I/O] Pointer to the ansi string.
629 * RETURNS
630 * Unicode representation of the first character in the ansi string.
632 * NOTES
633 * Upon successful completion, the char pointer ansi points to is
634 * incremented by the size of the character.
636 WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
638 WCHAR str;
639 DWORD charSize = sizeof(CHAR);
641 if (wine_is_dbcs_leadbyte(ansi_table, **ansi))
642 charSize++;
644 RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
645 *ansi += charSize;
647 return str;
651 COPY BETWEEN ANSI_STRING or UNICODE_STRING
652 there is no parameter checking, it just crashes
656 /**************************************************************************
657 * RtlAnsiStringToUnicodeString (NTDLL.@)
659 * Converts an ansi string to an unicode string.
661 * RETURNS
662 * Success: STATUS_SUCCESS. uni 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.
665 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
667 * NOTES
668 * This function always writes a terminating '\0'.
670 NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
671 PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
672 PCANSI_STRING ansi, /* [I] Ansi string to be converted */
673 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
675 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
677 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
678 uni->Length = total - sizeof(WCHAR);
679 if (doalloc)
681 uni->MaximumLength = total;
682 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
683 return STATUS_NO_MEMORY;
685 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
687 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
688 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
689 return STATUS_SUCCESS;
693 /**************************************************************************
694 * RtlOemStringToUnicodeString (NTDLL.@)
696 * Converts an oem string to an unicode string.
698 * RETURNS
699 * Success: STATUS_SUCCESS. uni contains the converted string
700 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
701 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
702 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
704 * NOTES
705 * This function always writes a terminating '\0'.
707 NTSTATUS WINAPI RtlOemStringToUnicodeString(
708 UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
709 const STRING *oem, /* [I] Oem string to be converted */
710 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
712 DWORD total = RtlOemStringToUnicodeSize( oem );
714 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
715 uni->Length = total - sizeof(WCHAR);
716 if (doalloc)
718 uni->MaximumLength = total;
719 if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
720 return STATUS_NO_MEMORY;
722 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
724 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
725 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
726 return STATUS_SUCCESS;
730 /**************************************************************************
731 * RtlUnicodeStringToAnsiString (NTDLL.@)
733 * Converts an unicode string to an ansi string.
735 * RETURNS
736 * Success: STATUS_SUCCESS. ansi contains the converted string
737 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
738 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
740 * NOTES
741 * This function always writes a terminating '\0'.
742 * It performs a partial copy if ansi is too small.
744 NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
745 STRING *ansi, /* [I/O] Destination for the ansi string */
746 const UNICODE_STRING *uni, /* [I] Unicode string to be converted */
747 BOOLEAN doalloc) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
749 NTSTATUS ret = STATUS_SUCCESS;
750 DWORD len = RtlUnicodeStringToAnsiSize( uni );
752 ansi->Length = len - 1;
753 if (doalloc)
755 ansi->MaximumLength = len;
756 if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
757 return STATUS_NO_MEMORY;
759 else if (ansi->MaximumLength < len)
761 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
762 ansi->Length = ansi->MaximumLength - 1;
763 ret = STATUS_BUFFER_OVERFLOW;
766 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
767 ansi->Buffer[ansi->Length] = 0;
768 return ret;
772 /**************************************************************************
773 * RtlUnicodeStringToOemString (NTDLL.@)
775 * Converts a Rtl Unicode string to an OEM string.
777 * PARAMS
778 * oem [O] Destination for OEM string
779 * uni [I] Source Unicode string
780 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
782 * RETURNS
783 * Success: STATUS_SUCCESS. oem contains the converted string
784 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
785 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
787 * NOTES
788 * If doalloc is TRUE, the length allocated is uni->Length + 1.
789 * This function always '\0' terminates the string returned.
791 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
792 const UNICODE_STRING *uni,
793 BOOLEAN doalloc )
795 NTSTATUS ret = STATUS_SUCCESS;
796 DWORD len = RtlUnicodeStringToOemSize( uni );
798 oem->Length = len - 1;
799 if (doalloc)
801 oem->MaximumLength = len;
802 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
803 return STATUS_NO_MEMORY;
805 else if (oem->MaximumLength < len)
807 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
808 oem->Length = oem->MaximumLength - 1;
809 ret = STATUS_BUFFER_OVERFLOW;
812 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
813 oem->Buffer[oem->Length] = 0;
814 return ret;
818 /**************************************************************************
819 * RtlMultiByteToUnicodeN (NTDLL.@)
821 * Converts a multi-byte string to a Unicode string.
823 * RETURNS
824 * NTSTATUS code
826 * NOTES
827 * Performs a partial copy if dst is too small.
829 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
830 LPCSTR src, DWORD srclen )
833 int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
834 if (reslen)
835 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
836 return STATUS_SUCCESS;
840 /**************************************************************************
841 * RtlOemToUnicodeN (NTDLL.@)
843 * Converts a multi-byte string in the OEM code page to a Unicode string.
845 * RETURNS
846 * NTSTATUS code
848 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
849 LPCSTR src, DWORD srclen )
851 int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
852 if (reslen)
853 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
854 return STATUS_SUCCESS;
858 /**************************************************************************
859 * RtlUnicodeToMultiByteN (NTDLL.@)
861 * Converts a Unicode string to a multi-byte string in the ANSI code page.
863 * RETURNS
864 * NTSTATUS code
866 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
867 LPCWSTR src, DWORD srclen )
869 int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
870 dst, dstlen, NULL, NULL );
871 if (reslen)
872 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
873 return STATUS_SUCCESS;
877 /**************************************************************************
878 * RtlUnicodeToOemN (NTDLL.@)
880 * Converts a Unicode string to a multi-byte string in the OEM code page.
882 * RETURNS
883 * NTSTATUS code
885 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
886 LPCWSTR src, DWORD srclen )
888 int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
889 dst, dstlen, NULL, NULL );
890 if (reslen)
891 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
892 return STATUS_SUCCESS;
897 CASE CONVERSIONS
901 /**************************************************************************
902 * RtlUpperChar (NTDLL.@)
904 * Converts an Ascii character to uppercase.
906 * PARAMS
907 * ch [I] Character to convert
909 * RETURNS
910 * The uppercase character value.
912 * NOTES
913 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
914 * All other input characters are returned unchanged. The locale and
915 * multibyte characters are not taken into account (as native DLL).
917 CHAR WINAPI RtlUpperChar( CHAR ch )
919 if (ch >= 'a' && ch <= 'z') {
920 return ch - 'a' + 'A';
921 } else {
922 return ch;
923 } /* if */
927 /**************************************************************************
928 * RtlUpperString (NTDLL.@)
930 * Converts an Ascii string to uppercase.
932 * PARAMS
933 * dst [O] Destination for converted string
934 * src [I] Source string to convert
936 * RETURNS
937 * Nothing.
939 * NOTES
940 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
941 * All other src characters are copied unchanged to dst. The locale and
942 * multibyte characters are not taken into account (as native DLL).
943 * The number of character copied is the minimum of src->Length and
944 * the dst->MaximumLength.
946 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
948 unsigned int i, len = min(src->Length, dst->MaximumLength);
950 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
951 dst->Length = len;
955 /**************************************************************************
956 * RtlUpcaseUnicodeChar (NTDLL.@)
958 * Converts an Unicode character to uppercase.
960 * PARAMS
961 * wch [I] Character to convert
963 * RETURNS
964 * The uppercase character value.
966 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
968 return toupperW(wch);
972 /**************************************************************************
973 * RtlDowncaseUnicodeChar (NTDLL.@)
975 * Converts an Unicode character to lowercase.
977 * PARAMS
978 * wch [I] Character to convert
980 * RETURNS
981 * The lowercase character value.
983 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
985 return tolowerW(wch);
989 /**************************************************************************
990 * RtlUpcaseUnicodeString (NTDLL.@)
992 * Converts an Unicode string to uppercase.
994 * PARAMS
995 * dest [O] Destination for converted string
996 * src [I] Source string to convert
997 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
999 * RETURNS
1000 * Success: STATUS_SUCCESS. dest contains the converted string.
1001 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1002 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1004 * NOTES
1005 * dest is never '\0' terminated because it may be equal to src, and src
1006 * might not be '\0' terminated. dest->Length is only set upon success.
1008 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
1009 const UNICODE_STRING *src,
1010 BOOLEAN doalloc)
1012 DWORD i, len = src->Length;
1014 if (doalloc)
1016 dest->MaximumLength = len;
1017 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1018 return STATUS_NO_MEMORY;
1020 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
1022 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
1023 dest->Length = len;
1024 return STATUS_SUCCESS;
1028 /**************************************************************************
1029 * RtlDowncaseUnicodeString (NTDLL.@)
1031 * Converts an Unicode string to lowercase.
1033 * PARAMS
1034 * dest [O] Destination for converted string
1035 * src [I] Source string to convert
1036 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1038 * RETURNS
1039 * Success: STATUS_SUCCESS. dest contains the converted string.
1040 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1041 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1043 * NOTES
1044 * dest is never '\0' terminated because it may be equal to src, and src
1045 * might not be '\0' terminated. dest->Length is only set upon success.
1047 NTSTATUS WINAPI RtlDowncaseUnicodeString(
1048 UNICODE_STRING *dest,
1049 const UNICODE_STRING *src,
1050 BOOLEAN doalloc)
1052 DWORD i;
1053 DWORD len = src->Length;
1055 if (doalloc) {
1056 dest->MaximumLength = len;
1057 if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1058 return STATUS_NO_MEMORY;
1059 } /* if */
1060 } else if (len > dest->MaximumLength) {
1061 return STATUS_BUFFER_OVERFLOW;
1062 } /* if */
1064 for (i = 0; i < len/sizeof(WCHAR); i++) {
1065 dest->Buffer[i] = tolowerW(src->Buffer[i]);
1066 } /* for */
1067 dest->Length = len;
1068 return STATUS_SUCCESS;
1072 /**************************************************************************
1073 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1075 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1077 * RETURNS
1078 * NTSTATUS code
1080 * NOTES
1081 * writes terminating 0
1083 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
1084 const UNICODE_STRING *src,
1085 BOOLEAN doalloc )
1087 NTSTATUS ret;
1088 UNICODE_STRING upcase;
1090 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1092 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
1093 RtlFreeUnicodeString( &upcase );
1095 return ret;
1099 /**************************************************************************
1100 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1102 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1103 * stored in STRING format.
1105 * RETURNS
1106 * NTSTATUS code
1108 * NOTES
1109 * writes terminating 0
1111 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
1112 const UNICODE_STRING *src,
1113 BOOLEAN doalloc )
1115 NTSTATUS ret;
1116 UNICODE_STRING upcase;
1118 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
1120 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
1121 RtlFreeUnicodeString( &upcase );
1123 return ret;
1127 /**************************************************************************
1128 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1130 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1131 * stored in STRING format.
1133 * RETURNS
1134 * NTSTATUS code
1136 * NOTES
1137 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1139 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
1140 const UNICODE_STRING *uni,
1141 BOOLEAN doalloc )
1143 NTSTATUS ret;
1144 UNICODE_STRING upcase;
1145 WCHAR tmp[32];
1147 upcase.Buffer = tmp;
1148 upcase.MaximumLength = sizeof(tmp);
1149 ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
1150 if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );
1152 if (!ret)
1154 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1155 oem->Length = len;
1156 if (doalloc)
1158 oem->MaximumLength = len;
1159 if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1161 ret = STATUS_NO_MEMORY;
1162 goto done;
1165 else if (oem->MaximumLength < len)
1167 ret = STATUS_BUFFER_OVERFLOW;
1168 oem->Length = oem->MaximumLength;
1169 if (!oem->MaximumLength) goto done;
1171 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
1172 done:
1173 if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1175 return ret;
1179 /**************************************************************************
1180 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1182 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1184 * RETURNS
1185 * NTSTATUS code
1187 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1188 LPCWSTR src, DWORD srclen )
1190 NTSTATUS ret;
1191 LPWSTR upcase;
1192 DWORD i;
1194 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1195 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1196 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1197 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1198 return ret;
1202 /**************************************************************************
1203 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1205 * Converts a Unicode string to the equivalent OEM upper-case representation.
1207 * RETURNS
1208 * NTSTATUS code
1210 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1211 LPCWSTR src, DWORD srclen )
1213 NTSTATUS ret;
1214 LPWSTR upcase;
1215 DWORD i;
1217 if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1218 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
1219 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1220 RtlFreeHeap( GetProcessHeap(), 0, upcase );
1221 return ret;
1226 STRING SIZE
1230 /**************************************************************************
1231 * RtlOemStringToUnicodeSize (NTDLL.@)
1232 * RtlxOemStringToUnicodeSize (NTDLL.@)
1234 * Calculate the size in bytes necessary for the Unicode conversion of str,
1235 * including the terminating '\0'.
1237 * PARAMS
1238 * str [I] String to calculate the size of
1240 * RETURNS
1241 * The calculated size.
1243 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1245 int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1246 return (ret + 1) * sizeof(WCHAR);
1250 /**************************************************************************
1251 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1252 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1254 * Calculate the size in bytes necessary for the Unicode conversion of str,
1255 * including the terminating '\0'.
1257 * PARAMS
1258 * str [I] String to calculate the size of
1260 * RETURNS
1261 * The calculated size.
1263 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1265 DWORD ret;
1266 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
1267 return ret + sizeof(WCHAR);
1271 /**************************************************************************
1272 * RtlMultiByteToUnicodeSize (NTDLL.@)
1274 * Compute the size in bytes necessary for the Unicode conversion of str,
1275 * without the terminating '\0'.
1277 * PARAMS
1278 * size [O] Destination for size
1279 * str [I] String to calculate the size of
1280 * len [I] Length of str
1282 * RETURNS
1283 * STATUS_SUCCESS.
1285 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
1287 *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1288 return STATUS_SUCCESS;
1292 /**************************************************************************
1293 * RtlUnicodeToMultiByteSize (NTDLL.@)
1295 * Calculate the size in bytes necessary for the multibyte conversion of str,
1296 * without the terminating '\0'.
1298 * PARAMS
1299 * size [O] Destination for size
1300 * str [I] String to calculate the size of
1301 * len [I] Length of str
1303 * RETURNS
1304 * STATUS_SUCCESS.
1306 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1308 *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1309 return STATUS_SUCCESS;
1313 /**************************************************************************
1314 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1315 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1317 * Calculate the size in bytes necessary for the Ansi conversion of str,
1318 * including the terminating '\0'.
1320 * PARAMS
1321 * str [I] String to calculate the size of
1323 * RETURNS
1324 * The calculated size.
1326 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1328 DWORD ret;
1329 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1330 return ret + 1;
1334 /**************************************************************************
1335 * RtlUnicodeStringToOemSize (NTDLL.@)
1336 * RtlxUnicodeStringToOemSize (NTDLL.@)
1338 * Calculate the size in bytes necessary for the OEM conversion of str,
1339 * including the terminating '\0'.
1341 * PARAMS
1342 * str [I] String to calculate the size of
1344 * RETURNS
1345 * The calculated size.
1347 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
1349 return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1350 NULL, 0, NULL, NULL ) + 1;
1354 /**************************************************************************
1355 * RtlAppendAsciizToString (NTDLL.@)
1357 * Concatenates a buffered character string and a '\0' terminated character
1358 * string
1360 * RETURNS
1361 * Success: STATUS_SUCCESS. src is appended to dest.
1362 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1363 * to hold the concatenated string.
1365 * NOTES
1366 * if src is NULL dest is unchanged.
1367 * dest is never '\0' terminated.
1369 NTSTATUS WINAPI RtlAppendAsciizToString(
1370 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1371 LPCSTR src) /* [I] '\0' terminated character string to be concatenated */
1373 if (src != NULL) {
1374 unsigned int src_len = strlen(src);
1375 unsigned int dest_len = src_len + dest->Length;
1377 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1378 memcpy(dest->Buffer + dest->Length, src, src_len);
1379 dest->Length = dest_len;
1380 } /* if */
1381 return STATUS_SUCCESS;
1385 /**************************************************************************
1386 * RtlAppendStringToString (NTDLL.@)
1388 * Concatenates two buffered character strings
1390 * RETURNS
1391 * Success: STATUS_SUCCESS. src is appended to dest.
1392 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1393 * to hold the concatenated string.
1395 * NOTES
1396 * if src->length is zero dest is unchanged.
1397 * dest is never '\0' terminated.
1399 NTSTATUS WINAPI RtlAppendStringToString(
1400 STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
1401 const STRING *src) /* [I] Buffered character string to be concatenated */
1403 if (src->Length != 0) {
1404 unsigned int dest_len = src->Length + dest->Length;
1406 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1407 memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
1408 dest->Length = dest_len;
1409 } /* if */
1410 return STATUS_SUCCESS;
1414 /**************************************************************************
1415 * RtlAppendUnicodeToString (NTDLL.@)
1417 * Concatenates a buffered unicode string and a '\0' terminated unicode
1418 * string
1420 * RETURNS
1421 * Success: STATUS_SUCCESS. src is appended to dest.
1422 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1423 * to hold the concatenated string.
1425 * NOTES
1426 * if src is NULL dest is unchanged.
1427 * dest is '\0' terminated when the MaximumLength allows it.
1428 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1430 * DIFFERENCES
1431 * Does not write in the src->Buffer beyond MaximumLength when
1432 * MaximumLength is odd as the native function does.
1434 NTSTATUS WINAPI RtlAppendUnicodeToString(
1435 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1436 LPCWSTR src) /* [I] '\0' terminated unicode string to be concatenated */
1438 if (src != NULL) {
1439 unsigned int src_len = strlenW(src) * sizeof(WCHAR);
1440 unsigned int dest_len = src_len + dest->Length;
1442 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1443 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
1444 dest->Length = dest_len;
1445 /* append terminating '\0' if enough space */
1446 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1447 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1448 } /* if */
1449 } /* if */
1450 return STATUS_SUCCESS;
1454 /**************************************************************************
1455 * RtlAppendUnicodeStringToString (NTDLL.@)
1457 * Concatenates two buffered unicode strings
1459 * RETURNS
1460 * Success: STATUS_SUCCESS. src is appended to dest.
1461 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1462 * to hold the concatenated string.
1464 * NOTES
1465 * if src->length is zero dest is unchanged.
1466 * dest is '\0' terminated when the MaximumLength allows it.
1467 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1469 * DIFFERENCES
1470 * Does not write in the src->Buffer beyond MaximumLength when
1471 * MaximumLength is odd as the native function does.
1473 NTSTATUS WINAPI RtlAppendUnicodeStringToString(
1474 UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
1475 const UNICODE_STRING *src) /* [I] Buffered unicode string to be concatenated */
1477 if (src->Length != 0) {
1478 unsigned int dest_len = src->Length + dest->Length;
1480 if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
1481 memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
1482 dest->Length = dest_len;
1483 /* append terminating '\0' if enough space */
1484 if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1485 dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
1486 } /* if */
1487 } /* if */
1488 return STATUS_SUCCESS;
1492 /**************************************************************************
1493 * RtlFindCharInUnicodeString (NTDLL.@)
1495 * Searches for one of several unicode characters in an unicode string.
1497 * RETURNS
1498 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1499 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1501 NTSTATUS WINAPI RtlFindCharInUnicodeString(
1502 int flags, /* [I] Flags */
1503 const UNICODE_STRING *main_str, /* [I] Unicode string in which one or more characters are searched */
1504 const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
1505 USHORT *pos) /* [O] Position of the first character found + 2 */
1507 int main_idx;
1508 unsigned int search_idx;
1510 switch (flags) {
1511 case 0:
1512 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1513 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1514 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1515 *pos = (main_idx + 1) * sizeof(WCHAR);
1516 return STATUS_SUCCESS;
1520 *pos = 0;
1521 return STATUS_NOT_FOUND;
1522 case 1:
1523 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1524 for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
1525 if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
1526 *pos = main_idx * sizeof(WCHAR);
1527 return STATUS_SUCCESS;
1531 *pos = 0;
1532 return STATUS_NOT_FOUND;
1533 case 2:
1534 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1535 search_idx = 0;
1536 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1537 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1538 search_idx++;
1540 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1541 *pos = (main_idx + 1) * sizeof(WCHAR);
1542 return STATUS_SUCCESS;
1545 *pos = 0;
1546 return STATUS_NOT_FOUND;
1547 case 3:
1548 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1549 search_idx = 0;
1550 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1551 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1552 search_idx++;
1554 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1555 *pos = main_idx * sizeof(WCHAR);
1556 return STATUS_SUCCESS;
1559 *pos = 0;
1560 return STATUS_NOT_FOUND;
1561 } /* switch */
1562 return STATUS_NOT_FOUND;
1567 MISC
1570 /**************************************************************************
1571 * RtlIsTextUnicode (NTDLL.@)
1573 * Attempt to guess whether a text buffer is Unicode.
1575 * PARAMS
1576 * buf [I] Text buffer to test
1577 * len [I] Length of buf
1578 * pf [O] Destination for test results
1580 * RETURNS
1581 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1583 * FIXME
1584 * Should implement more tests.
1586 BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
1588 const WCHAR *s = buf;
1589 int i;
1590 unsigned int flags = ~0U, out_flags = 0;
1592 if (len < sizeof(WCHAR))
1594 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1595 if (pf) *pf = 0;
1596 return FALSE;
1598 if (pf)
1599 flags = *pf;
1601 * Apply various tests to the text string. According to the
1602 * docs, each test "passed" sets the corresponding flag in
1603 * the output flags. But some of the tests are mutually
1604 * exclusive, so I don't see how you could pass all tests ...
1607 /* Check for an odd length ... pass if even. */
1608 if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1610 len /= sizeof(WCHAR);
1611 /* Windows only checks the first 256 characters */
1612 if (len > 256) len = 256;
1614 /* Check for the special byte order unicode marks. */
1615 if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1616 if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1618 /* apply some statistical analysis */
1619 if (flags & IS_TEXT_UNICODE_STATISTICS)
1621 int stats = 0;
1622 /* FIXME: checks only for ASCII characters in the unicode stream */
1623 for (i = 0; i < len; i++)
1625 if (s[i] <= 255) stats++;
1627 if (stats > len / 2)
1628 out_flags |= IS_TEXT_UNICODE_STATISTICS;
1631 /* Check for unicode NULL chars */
1632 if (flags & IS_TEXT_UNICODE_NULL_BYTES)
1634 for (i = 0; i < len; i++)
1636 if (!s[i])
1638 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1639 break;
1644 if (pf)
1646 out_flags &= *pf;
1647 *pf = out_flags;
1649 /* check for flags that indicate it's definitely not valid Unicode */
1650 if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
1651 /* now check for invalid ASCII, and assume Unicode if so */
1652 if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
1653 /* now check for Unicode flags */
1654 if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
1655 /* no flags set */
1656 return FALSE;
1660 /**************************************************************************
1661 * RtlCharToInteger (NTDLL.@)
1663 * Converts a character string into its integer equivalent.
1665 * RETURNS
1666 * Success: STATUS_SUCCESS. value contains the converted number
1667 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1668 * STATUS_ACCESS_VIOLATION, if value is NULL.
1670 * NOTES
1671 * For base 0 it uses 10 as base and the string should be in the format
1672 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1673 * For other bases the string should be in the format
1674 * "{whitespace} [+|-] {digits}".
1675 * No check is made for value overflow, only the lower 32 bits are assigned.
1676 * If str is NULL it crashes, as the native function does.
1678 * DIFFERENCES
1679 * This function does not read garbage behind '\0' as the native version does.
1681 NTSTATUS WINAPI RtlCharToInteger(
1682 PCSZ str, /* [I] '\0' terminated single-byte string containing a number */
1683 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1684 ULONG *value) /* [O] Destination for the converted value */
1686 CHAR chCurrent;
1687 int digit;
1688 ULONG RunningTotal = 0;
1689 char bMinus = 0;
1691 while (*str != '\0' && *str <= ' ') {
1692 str++;
1693 } /* while */
1695 if (*str == '+') {
1696 str++;
1697 } else if (*str == '-') {
1698 bMinus = 1;
1699 str++;
1700 } /* if */
1702 if (base == 0) {
1703 base = 10;
1704 if (str[0] == '0') {
1705 if (str[1] == 'b') {
1706 str += 2;
1707 base = 2;
1708 } else if (str[1] == 'o') {
1709 str += 2;
1710 base = 8;
1711 } else if (str[1] == 'x') {
1712 str += 2;
1713 base = 16;
1714 } /* if */
1715 } /* if */
1716 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1717 return STATUS_INVALID_PARAMETER;
1718 } /* if */
1720 if (value == NULL) {
1721 return STATUS_ACCESS_VIOLATION;
1722 } /* if */
1724 while (*str != '\0') {
1725 chCurrent = *str;
1726 if (chCurrent >= '0' && chCurrent <= '9') {
1727 digit = chCurrent - '0';
1728 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1729 digit = chCurrent - 'A' + 10;
1730 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1731 digit = chCurrent - 'a' + 10;
1732 } else {
1733 digit = -1;
1734 } /* if */
1735 if (digit < 0 || digit >= base) {
1736 *value = bMinus ? -RunningTotal : RunningTotal;
1737 return STATUS_SUCCESS;
1738 } /* if */
1740 RunningTotal = RunningTotal * base + digit;
1741 str++;
1742 } /* while */
1744 *value = bMinus ? -RunningTotal : RunningTotal;
1745 return STATUS_SUCCESS;
1749 /**************************************************************************
1750 * RtlIntegerToChar (NTDLL.@)
1752 * Converts an unsigned integer to a character string.
1754 * RETURNS
1755 * Success: STATUS_SUCCESS. str contains the converted number
1756 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1757 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1758 * STATUS_ACCESS_VIOLATION, if str is NULL.
1760 * NOTES
1761 * Instead of base 0 it uses 10 as base.
1762 * Writes at most length characters to the string str.
1763 * Str is '\0' terminated when length allows it.
1764 * When str fits exactly in length characters the '\0' is omitted.
1766 NTSTATUS WINAPI RtlIntegerToChar(
1767 ULONG value, /* [I] Value to be converted */
1768 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1769 ULONG length, /* [I] Length of the str buffer in bytes */
1770 PCHAR str) /* [O] Destination for the converted value */
1772 CHAR buffer[33];
1773 PCHAR pos;
1774 CHAR digit;
1775 ULONG len;
1777 if (base == 0) {
1778 base = 10;
1779 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1780 return STATUS_INVALID_PARAMETER;
1781 } /* if */
1783 pos = &buffer[32];
1784 *pos = '\0';
1786 do {
1787 pos--;
1788 digit = value % base;
1789 value = value / base;
1790 if (digit < 10) {
1791 *pos = '0' + digit;
1792 } else {
1793 *pos = 'A' + digit - 10;
1794 } /* if */
1795 } while (value != 0L);
1797 len = &buffer[32] - pos;
1798 if (len > length) {
1799 return STATUS_BUFFER_OVERFLOW;
1800 } else if (str == NULL) {
1801 return STATUS_ACCESS_VIOLATION;
1802 } else if (len == length) {
1803 memcpy(str, pos, len);
1804 } else {
1805 memcpy(str, pos, len + 1);
1806 } /* if */
1807 return STATUS_SUCCESS;
1811 /**************************************************************************
1812 * RtlUnicodeStringToInteger (NTDLL.@)
1814 * Converts an unicode string into its integer equivalent.
1816 * RETURNS
1817 * Success: STATUS_SUCCESS. value contains the converted number
1818 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1819 * STATUS_ACCESS_VIOLATION, if value is NULL.
1821 * NOTES
1822 * For base 0 it uses 10 as base and the string should be in the format
1823 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1824 * For other bases the string should be in the format
1825 * "{whitespace} [+|-] {digits}".
1826 * No check is made for value overflow, only the lower 32 bits are assigned.
1827 * If str is NULL it crashes, as the native function does.
1829 * DIFFERENCES
1830 * This function does not read garbage on string length 0 as the native
1831 * version does.
1833 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1834 const UNICODE_STRING *str, /* [I] Unicode string to be converted */
1835 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1836 ULONG *value) /* [O] Destination for the converted value */
1838 LPWSTR lpwstr = str->Buffer;
1839 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1840 WCHAR wchCurrent;
1841 int digit;
1842 ULONG RunningTotal = 0;
1843 char bMinus = 0;
1845 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1846 lpwstr++;
1847 CharsRemaining--;
1848 } /* while */
1850 if (CharsRemaining >= 1) {
1851 if (*lpwstr == '+') {
1852 lpwstr++;
1853 CharsRemaining--;
1854 } else if (*lpwstr == '-') {
1855 bMinus = 1;
1856 lpwstr++;
1857 CharsRemaining--;
1858 } /* if */
1859 } /* if */
1861 if (base == 0) {
1862 base = 10;
1863 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1864 if (lpwstr[1] == 'b') {
1865 lpwstr += 2;
1866 CharsRemaining -= 2;
1867 base = 2;
1868 } else if (lpwstr[1] == 'o') {
1869 lpwstr += 2;
1870 CharsRemaining -= 2;
1871 base = 8;
1872 } else if (lpwstr[1] == 'x') {
1873 lpwstr += 2;
1874 CharsRemaining -= 2;
1875 base = 16;
1876 } /* if */
1877 } /* if */
1878 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1879 return STATUS_INVALID_PARAMETER;
1880 } /* if */
1882 if (value == NULL) {
1883 return STATUS_ACCESS_VIOLATION;
1884 } /* if */
1886 while (CharsRemaining >= 1) {
1887 wchCurrent = *lpwstr;
1888 if (wchCurrent >= '0' && wchCurrent <= '9') {
1889 digit = wchCurrent - '0';
1890 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1891 digit = wchCurrent - 'A' + 10;
1892 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1893 digit = wchCurrent - 'a' + 10;
1894 } else {
1895 digit = -1;
1896 } /* if */
1897 if (digit < 0 || digit >= base) {
1898 *value = bMinus ? -RunningTotal : RunningTotal;
1899 return STATUS_SUCCESS;
1900 } /* if */
1902 RunningTotal = RunningTotal * base + digit;
1903 lpwstr++;
1904 CharsRemaining--;
1905 } /* while */
1907 *value = bMinus ? -RunningTotal : RunningTotal;
1908 return STATUS_SUCCESS;
1912 /**************************************************************************
1913 * RtlIntegerToUnicodeString (NTDLL.@)
1915 * Converts an unsigned integer to a '\0' terminated unicode string.
1917 * RETURNS
1918 * Success: STATUS_SUCCESS. str contains the converted number
1919 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1920 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1921 * (with the '\0' termination). In this case str->Length
1922 * is set to the length, the string would have (which can
1923 * be larger than the MaximumLength).
1925 * NOTES
1926 * Instead of base 0 it uses 10 as base.
1927 * If str is NULL it crashes, as the native function does.
1929 * DIFFERENCES
1930 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1931 * The native function does this when the string would be longer than 16
1932 * characters even when the string parameter is long enough.
1934 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1935 ULONG value, /* [I] Value to be converted */
1936 ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1937 UNICODE_STRING *str) /* [O] Destination for the converted value */
1939 WCHAR buffer[33];
1940 PWCHAR pos;
1941 WCHAR digit;
1943 if (base == 0) {
1944 base = 10;
1945 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1946 return STATUS_INVALID_PARAMETER;
1947 } /* if */
1949 pos = &buffer[32];
1950 *pos = '\0';
1952 do {
1953 pos--;
1954 digit = value % base;
1955 value = value / base;
1956 if (digit < 10) {
1957 *pos = '0' + digit;
1958 } else {
1959 *pos = 'A' + digit - 10;
1960 } /* if */
1961 } while (value != 0L);
1963 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1964 if (str->Length >= str->MaximumLength) {
1965 return STATUS_BUFFER_OVERFLOW;
1966 } else {
1967 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1968 } /* if */
1969 return STATUS_SUCCESS;
1973 /*************************************************************************
1974 * RtlGUIDFromString (NTDLL.@)
1976 * Convert a string representation of a GUID into a GUID.
1978 * PARAMS
1979 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1980 * guid [O] Destination for the converted GUID
1982 * RETURNS
1983 * Success: STATUS_SUCCESS. guid contains the converted value.
1984 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1986 * SEE ALSO
1987 * See RtlStringFromGUID.
1989 NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
1991 int i = 0;
1992 const WCHAR *lpszCLSID = str->Buffer;
1993 BYTE* lpOut = (BYTE*)guid;
1995 TRACE("(%s,%p)\n", debugstr_us(str), guid);
1997 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1998 * to memory: DWORD... WORD WORD BYTES............
2000 while (i <= 37)
2002 switch (i)
2004 case 0:
2005 if (*lpszCLSID != '{')
2006 return STATUS_INVALID_PARAMETER;
2007 break;
2009 case 9: case 14: case 19: case 24:
2010 if (*lpszCLSID != '-')
2011 return STATUS_INVALID_PARAMETER;
2012 break;
2014 case 37:
2015 if (*lpszCLSID != '}')
2016 return STATUS_INVALID_PARAMETER;
2017 break;
2019 default:
2021 WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
2022 unsigned char byte;
2024 /* Read two hex digits as a byte value */
2025 if (ch >= '0' && ch <= '9') ch = ch - '0';
2026 else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
2027 else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
2028 else return STATUS_INVALID_PARAMETER;
2030 if (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
2031 else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
2032 else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
2033 else return STATUS_INVALID_PARAMETER;
2035 byte = ch << 4 | ch2;
2037 switch (i)
2039 #ifndef WORDS_BIGENDIAN
2040 /* For Big Endian machines, we store the data such that the
2041 * dword/word members can be read as DWORDS and WORDS correctly. */
2042 /* Dword */
2043 case 1: lpOut[3] = byte; break;
2044 case 3: lpOut[2] = byte; break;
2045 case 5: lpOut[1] = byte; break;
2046 case 7: lpOut[0] = byte; lpOut += 4; break;
2047 /* Word */
2048 case 10: case 15: lpOut[1] = byte; break;
2049 case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
2050 #endif
2051 /* Byte */
2052 default: lpOut[0] = byte; lpOut++; break;
2054 lpszCLSID++; /* Skip 2nd character of byte */
2055 i++;
2058 lpszCLSID++;
2059 i++;
2062 return STATUS_SUCCESS;
2065 /*************************************************************************
2066 * RtlStringFromGUID (NTDLL.@)
2068 * Convert a GUID into a string representation of a GUID.
2070 * PARAMS
2071 * guid [I] GUID to convert
2072 * str [O] Destination for the converted string
2074 * RETURNS
2075 * Success: STATUS_SUCCESS. str contains the converted value.
2076 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2078 * SEE ALSO
2079 * See RtlGUIDFromString.
2081 NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
2083 static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
2084 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2085 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2086 '%','0','2','X','%','0','2','X','}','\0' };
2088 TRACE("(%p,%p)\n", guid, str);
2090 str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
2091 str->MaximumLength = str->Length + sizeof(WCHAR);
2092 str->Buffer = (WCHAR*)RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2093 if (!str->Buffer)
2095 str->Length = str->MaximumLength = 0;
2096 return STATUS_NO_MEMORY;
2098 sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
2099 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
2100 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
2102 return STATUS_SUCCESS;