4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
13 #include "wine/winestring.h"
14 #include "wine/unicode.h"
17 #include "debugtools.h"
18 #include "ntdll_misc.h"
21 DEFAULT_DEBUG_CHANNEL(ntdll);
23 /* STRING CREATION FUNCTIONS */
25 /**************************************************************************
26 * RtlInitAnsiString (NTDLL.@)
28 void WINAPI RtlInitAnsiString( PSTRING target, LPCSTR source)
30 if ((target->Buffer = (LPSTR)source))
32 target->Length = strlen(source);
33 target->MaximumLength = target->Length + 1;
35 else target->Length = target->MaximumLength = 0;
39 /**************************************************************************
40 * RtlInitString (NTDLL.@)
42 void WINAPI RtlInitString( PSTRING target, LPCSTR source )
44 return RtlInitAnsiString( target, source );
48 /**************************************************************************
49 * RtlFreeAnsiString (NTDLL.@)
51 void WINAPI RtlFreeAnsiString( PSTRING str )
53 if (str->Buffer) HeapFree( GetProcessHeap(), 0, str->Buffer );
57 /**************************************************************************
58 * RtlFreeOemString (NTDLL.@)
60 void WINAPI RtlFreeOemString( PSTRING str )
62 RtlFreeAnsiString( str );
66 /**************************************************************************
67 * RtlCopyString (NTDLL.@)
69 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
73 unsigned int len = min( src->Length, dst->MaximumLength );
74 memcpy( dst->Buffer, src->Buffer, len );
81 /**************************************************************************
82 * RtlInitUnicodeString (NTDLL.@)
84 void WINAPI RtlInitUnicodeString( PUNICODE_STRING target, LPCWSTR source )
86 if ((target->Buffer = (LPWSTR)source))
88 target->Length = strlenW(source) * sizeof(WCHAR);
89 target->MaximumLength = target->Length + sizeof(WCHAR);
91 else target->Length = target->MaximumLength = 0;
95 /**************************************************************************
96 * RtlCreateUnicodeString (NTDLL.@)
98 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
100 int len = (strlenW(src) + 1) * sizeof(WCHAR);
101 if (!(target->Buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
102 memcpy( target->Buffer, src, len );
103 target->MaximumLength = len;
104 target->Length = len - 2;
109 /**************************************************************************
110 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
112 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
115 RtlInitAnsiString( &ansi, src );
116 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
120 /**************************************************************************
121 * RtlFreeUnicodeString (NTDLL.@)
123 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
125 if (str->Buffer) HeapFree( GetProcessHeap(), 0, str->Buffer );
129 /**************************************************************************
130 * RtlCopyUnicodeString (NTDLL.@)
132 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
136 unsigned int len = min( src->Length, dst->MaximumLength );
137 memcpy( dst->Buffer, src->Buffer, len );
139 /* append terminating NULL if enough space */
140 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
142 else dst->Length = 0;
146 /**************************************************************************
147 * RtlEraseUnicodeString (NTDLL.@)
149 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
153 memset( str->Buffer, 0, str->MaximumLength );
162 /******************************************************************************
163 * RtlCompareString (NTDLL.@)
165 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
171 len = min(s1->Length, s2->Length);
177 while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++);
181 while (!ret && len--) ret = *p1++ - *p2++;
183 if (!ret) ret = s1->Length - s2->Length;
188 /******************************************************************************
189 * RtlCompareUnicodeString (NTDLL.@)
191 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
192 BOOLEAN CaseInsensitive )
198 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
204 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
208 while (!ret && len--) ret = *p1++ - *p2++;
210 if (!ret) ret = s1->Length - s2->Length;
215 /**************************************************************************
216 * RtlEqualString (NTDLL.@)
218 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
220 if (s1->Length != s2->Length) return FALSE;
221 return !RtlCompareString( s1, s2, CaseInsensitive );
225 /**************************************************************************
226 * RtlEqualUnicodeString (NTDLL.@)
228 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
229 BOOLEAN CaseInsensitive )
231 if (s1->Length != s2->Length) return FALSE;
232 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
236 /**************************************************************************
237 * RtlPrefixString (NTDLL.@)
239 * Test if s1 is a prefix in s2
241 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
245 if (s1->Length > s2->Length) return FALSE;
248 for (i = 0; i < s1->Length; i++)
249 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
253 for (i = 0; i < s1->Length; i++)
254 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
260 /**************************************************************************
261 * RtlPrefixUnicodeString (NTDLL.@)
263 * Test if s1 is a prefix in s2
265 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
266 const UNICODE_STRING *s2,
267 BOOLEAN ignore_case )
271 if (s1->Length > s2->Length) return FALSE;
274 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
275 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
279 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
280 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
287 COPY BETWEEN ANSI_STRING or UNICODE_STRING
288 there is no parameter checking, it just crashes
292 /**************************************************************************
293 * RtlAnsiStringToUnicodeString (NTDLL.@)
296 * writes terminating 0
298 NTSTATUS WINAPI RtlAnsiStringToUnicodeString( UNICODE_STRING *uni,
302 DWORD len = MultiByteToWideChar( CP_ACP, 0, ansi->Buffer, ansi->Length, NULL, 0 );
303 DWORD total = (len + 1) * sizeof(WCHAR);
305 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
306 uni->Length = len * sizeof(WCHAR);
309 uni->MaximumLength = total;
310 if (!(uni->Buffer = HeapAlloc( GetProcessHeap(), 0, total ))) return STATUS_NO_MEMORY;
312 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
314 MultiByteToWideChar( CP_ACP, 0, ansi->Buffer, ansi->Length, uni->Buffer, len );
315 uni->Buffer[len] = 0;
316 return STATUS_SUCCESS;
320 /**************************************************************************
321 * RtlOemStringToUnicodeString (NTDLL.@)
324 * writes terminating 0
325 * if resulting length > 0xffff it returns STATUS_INVALID_PARAMETER_2
327 NTSTATUS WINAPI RtlOemStringToUnicodeString( UNICODE_STRING *uni,
331 DWORD len = MultiByteToWideChar( CP_OEMCP, 0, oem->Buffer, oem->Length, NULL, 0 );
332 DWORD total = (len + 1) * sizeof(WCHAR);
334 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
335 uni->Length = len * sizeof(WCHAR);
338 uni->MaximumLength = total;
339 if (!(uni->Buffer = HeapAlloc( GetProcessHeap(), 0, total ))) return STATUS_NO_MEMORY;
341 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
343 MultiByteToWideChar( CP_OEMCP, 0, oem->Buffer, oem->Length, uni->Buffer, len );
344 uni->Buffer[len] = 0;
345 return STATUS_SUCCESS;
349 /**************************************************************************
350 * RtlUnicodeStringToAnsiString (NTDLL.@)
353 * writes terminating 0
354 * copies a part if the buffer is too small
356 NTSTATUS WINAPI RtlUnicodeStringToAnsiString( STRING *ansi,
357 const UNICODE_STRING *uni,
360 NTSTATUS ret = STATUS_SUCCESS;
361 DWORD len = RtlUnicodeStringToAnsiSize( uni );
366 ansi->MaximumLength = len + 1;
367 if (!(ansi->Buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 ))) return STATUS_NO_MEMORY;
369 else if (ansi->MaximumLength <= len)
371 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
372 ansi->Length = ansi->MaximumLength - 1;
373 ret = STATUS_BUFFER_OVERFLOW;
376 WideCharToMultiByte( CP_ACP, 0, uni->Buffer, uni->Length / sizeof(WCHAR),
377 ansi->Buffer, ansi->Length, NULL, NULL );
378 ansi->Buffer[ansi->Length] = 0;
383 /**************************************************************************
384 * RtlUnicodeStringToOemString (NTDLL.@)
387 * allocates uni->Length+1
388 * writes terminating 0
390 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
391 const UNICODE_STRING *uni,
394 NTSTATUS ret = STATUS_SUCCESS;
395 DWORD len = RtlUnicodeStringToOemSize( uni );
400 oem->MaximumLength = len + 1;
401 if (!(oem->Buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 ))) return STATUS_NO_MEMORY;
403 else if (oem->MaximumLength <= len)
405 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
406 oem->Length = oem->MaximumLength - 1;
407 ret = STATUS_BUFFER_OVERFLOW;
410 WideCharToMultiByte( CP_OEMCP, 0, uni->Buffer, uni->Length / sizeof(WCHAR),
411 oem->Buffer, oem->Length, NULL, NULL );
412 oem->Buffer[oem->Length] = 0;
417 /**************************************************************************
418 * RtlMultiByteToUnicodeN (NTDLL.@)
421 * if unistr is too small a part is copied
423 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
424 LPCSTR src, DWORD srclen )
426 DWORD res = MultiByteToWideChar( CP_ACP, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
428 *reslen = res ? res * sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
429 return STATUS_SUCCESS;
433 /**************************************************************************
434 * RtlOemToUnicodeN (NTDLL.@)
436 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
437 LPCSTR src, DWORD srclen )
439 DWORD res = MultiByteToWideChar( CP_OEMCP, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
441 *reslen = res ? res * sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
442 return STATUS_SUCCESS;
446 /**************************************************************************
447 * RtlUnicodeToMultiByteN (NTDLL.@)
449 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
450 LPCWSTR src, DWORD srclen )
452 DWORD res = WideCharToMultiByte( CP_ACP, 0, src, srclen/sizeof(WCHAR),
453 dst, dstlen, NULL, NULL );
455 *reslen = res ? res * sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
456 return STATUS_SUCCESS;
460 /**************************************************************************
461 * RtlUnicodeToOemN (NTDLL.@)
463 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
464 LPCWSTR src, DWORD srclen )
466 DWORD res = WideCharToMultiByte( CP_OEMCP, 0, src, srclen/sizeof(WCHAR),
467 dst, dstlen, NULL, NULL );
469 *reslen = res ? res * sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
470 return STATUS_SUCCESS;
478 /**************************************************************************
479 * RtlUpperString (NTDLL.@)
481 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
483 unsigned int i, len = min(src->Length, dst->MaximumLength);
485 for (i = 0; i < len; i++) dst->Buffer[i] = toupper(src->Buffer[i]);
490 /**************************************************************************
491 * RtlUpcaseUnicodeString (NTDLL.@)
494 * destination string is never 0-terminated because dest can be equal to src
495 * and src might be not 0-terminated
496 * dest.Length only set when success
498 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
499 const UNICODE_STRING *src,
502 DWORD i, len = src->Length;
506 dest->MaximumLength = len;
507 if (!(dest->Buffer = HeapAlloc( GetProcessHeap(), 0, len ))) return STATUS_NO_MEMORY;
509 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
511 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
513 return STATUS_SUCCESS;
517 /**************************************************************************
518 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
521 * writes terminating 0
523 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
524 const UNICODE_STRING *src,
528 UNICODE_STRING upcase;
530 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
532 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
533 RtlFreeUnicodeString( &upcase );
539 /**************************************************************************
540 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
543 * writes terminating 0
545 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
546 const UNICODE_STRING *src,
550 UNICODE_STRING upcase;
552 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
554 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
555 RtlFreeUnicodeString( &upcase );
561 /**************************************************************************
562 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
564 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
565 LPCWSTR src, DWORD srclen )
571 if (!(upcase = HeapAlloc( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
572 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
573 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
574 HeapFree( GetProcessHeap(), 0, upcase );
579 /**************************************************************************
580 * RtlUpcaseUnicodeToOemN (NTDLL.@)
582 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
583 LPCWSTR src, DWORD srclen )
589 if (!(upcase = HeapAlloc( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
590 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
591 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
592 HeapFree( GetProcessHeap(), 0, upcase );
601 /**************************************************************************
602 * RtlOemStringToUnicodeSize (NTDLL.@)
604 * Return the size in bytes necessary for the Unicode conversion of 'str',
605 * including the terminating NULL.
607 UINT WINAPI RtlOemStringToUnicodeSize(PSTRING str)
609 DWORD ret = MultiByteToWideChar( CP_OEMCP, 0, str->Buffer, str->Length, NULL, 0 );
610 return (ret + 1) * sizeof(WCHAR);
614 /**************************************************************************
615 * RtlAnsiStringToUnicodeSize (NTDLL.@)
617 * Return the size in bytes necessary for the Unicode conversion of 'str',
618 * including the terminating NULL.
620 DWORD WINAPI RtlAnsiStringToUnicodeSize(PSTRING str)
622 DWORD ret = MultiByteToWideChar( CP_ACP, 0, str->Buffer, str->Length, NULL, 0 );
623 return (ret + 1) * sizeof(WCHAR);
627 /**************************************************************************
628 * RtlMultiByteToUnicodeSize (NTDLL.@)
630 * Compute the size in bytes necessary for the Unicode conversion of 'str',
631 * without the terminating NULL.
633 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
635 *size = MultiByteToWideChar( CP_ACP, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
640 /**************************************************************************
641 * RtlUnicodeToMultiByteSize (NTDLL.@)
643 * Compute the size necessary for the multibyte conversion of 'str',
644 * without the terminating NULL.
646 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( DWORD *size, LPCWSTR str, UINT len )
648 *size = WideCharToMultiByte( CP_ACP, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
653 /**************************************************************************
654 * RtlUnicodeStringToAnsiSize (NTDLL.@)
656 * Return the size in bytes necessary for the Ansi conversion of 'str',
657 * including the terminating NULL.
659 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
661 return WideCharToMultiByte( CP_ACP, 0, str->Buffer, str->Length / sizeof(WCHAR),
662 NULL, 0, NULL, NULL ) + 1;
666 /**************************************************************************
667 * RtlUnicodeStringToOemSize (NTDLL.@)
669 * Return the size in bytes necessary for the OEM conversion of 'str',
670 * including the terminating NULL.
672 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
674 return WideCharToMultiByte( CP_OEMCP, 0, str->Buffer, str->Length / sizeof(WCHAR),
675 NULL, 0, NULL, NULL ) + 1;
679 /**************************************************************************
680 * RtlAppendStringToString (NTDLL.@)
682 NTSTATUS WINAPI RtlAppendStringToString( STRING *dst, const STRING *src )
684 unsigned int len = src->Length + dst->Length;
685 if (len > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
686 memcpy( dst->Buffer + dst->Length, src->Buffer, src->Length );
688 return STATUS_SUCCESS;
692 /**************************************************************************
693 * RtlAppendAsciizToString (NTDLL.@)
695 NTSTATUS WINAPI RtlAppendAsciizToString( STRING *dst, LPCSTR src )
699 unsigned int srclen = strlen(src);
700 unsigned int total = srclen + dst->Length;
701 if (total > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
702 memcpy( dst->Buffer + dst->Length, src, srclen );
705 return STATUS_SUCCESS;
709 /**************************************************************************
710 * RtlAppendUnicodeToString (NTDLL.@)
712 NTSTATUS WINAPI RtlAppendUnicodeToString( UNICODE_STRING *dst, LPCWSTR src )
716 unsigned int srclen = strlenW(src) * sizeof(WCHAR);
717 unsigned int total = srclen + dst->Length;
718 if (total > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
719 memcpy( dst->Buffer + dst->Length/sizeof(WCHAR), src, srclen );
721 /* append terminating NULL if enough space */
722 if (total < dst->MaximumLength) dst->Buffer[total / sizeof(WCHAR)] = 0;
724 return STATUS_SUCCESS;
728 /**************************************************************************
729 * RtlAppendUnicodeStringToString (NTDLL.@)
731 NTSTATUS WINAPI RtlAppendUnicodeStringToString( UNICODE_STRING *dst, const UNICODE_STRING *src )
733 unsigned int len = src->Length + dst->Length;
734 if (len > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
735 memcpy( dst->Buffer + dst->Length/sizeof(WCHAR), src->Buffer, src->Length );
737 /* append terminating NULL if enough space */
738 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
739 return STATUS_SUCCESS;
747 /**************************************************************************
750 * Apply various feeble heuristics to guess whether
751 * the text buffer contains Unicode.
752 * FIXME: should implement more tests.
754 DWORD WINAPI RtlIsTextUnicode(
760 DWORD flags = -1, out_flags = 0;
767 * Apply various tests to the text string. According to the
768 * docs, each test "passed" sets the corresponding flag in
769 * the output flags. But some of the tests are mutually
770 * exclusive, so I don't see how you could pass all tests ...
773 /* Check for an odd length ... pass if even. */
775 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
777 /* Check for the special unicode marker byte. */
779 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
782 * Check whether the string passed all of the tests.
784 flags &= ITU_IMPLEMENTED_TESTS;
785 if ((out_flags & flags) != flags)