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
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "ntdll_misc.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
37 UINT NlsAnsiCodePage = 1252;
38 BYTE NlsMbCodePageTag = 0;
39 BYTE NlsMbOemCodePageTag = 0;
41 static const union cptable *ansi_table;
42 static const union cptable *oem_table;
44 inline static const union cptable *get_ansi_table(void)
46 if (!ansi_table) ansi_table = cp_get_table( 1252 );
50 inline static const union cptable *get_oem_table(void)
52 if (!oem_table) oem_table = cp_get_table( 437 );
57 /**************************************************************************
58 * __wine_init_codepages (NTDLL.@)
60 * Set the code page once kernel32 is loaded. Should be done differently.
62 void __wine_init_codepages( const union cptable *ansi, const union cptable *oem )
66 NlsAnsiCodePage = ansi->info.codepage;
70 /**************************************************************************
71 * RtlInitAnsiString (NTDLL.@)
73 void WINAPI RtlInitAnsiString( PSTRING target, LPCSTR source)
75 if ((target->Buffer = (LPSTR)source))
77 target->Length = strlen(source);
78 target->MaximumLength = target->Length + 1;
80 else target->Length = target->MaximumLength = 0;
84 /**************************************************************************
85 * RtlInitString (NTDLL.@)
87 void WINAPI RtlInitString( PSTRING target, LPCSTR source )
89 RtlInitAnsiString( target, source );
93 /**************************************************************************
94 * RtlFreeAnsiString (NTDLL.@)
96 void WINAPI RtlFreeAnsiString( PSTRING str )
98 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
102 /**************************************************************************
103 * RtlFreeOemString (NTDLL.@)
105 void WINAPI RtlFreeOemString( PSTRING str )
107 RtlFreeAnsiString( str );
111 /**************************************************************************
112 * RtlCopyString (NTDLL.@)
114 void WINAPI RtlCopyString( STRING *dst, const STRING *src )
118 unsigned int len = min( src->Length, dst->MaximumLength );
119 memcpy( dst->Buffer, src->Buffer, len );
122 else dst->Length = 0;
126 /**************************************************************************
127 * RtlInitUnicodeString (NTDLL.@)
129 void WINAPI RtlInitUnicodeString( PUNICODE_STRING target, LPCWSTR source )
131 if ((target->Buffer = (LPWSTR)source))
133 target->Length = strlenW(source) * sizeof(WCHAR);
134 target->MaximumLength = target->Length + sizeof(WCHAR);
136 else target->Length = target->MaximumLength = 0;
140 /**************************************************************************
141 * RtlCreateUnicodeString (NTDLL.@)
143 BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
145 int len = (strlenW(src) + 1) * sizeof(WCHAR);
146 if (!(target->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) return FALSE;
147 memcpy( target->Buffer, src, len );
148 target->MaximumLength = len;
149 target->Length = len - sizeof(WCHAR);
154 /**************************************************************************
155 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
157 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
160 RtlInitAnsiString( &ansi, src );
161 return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
165 /**************************************************************************
166 * RtlFreeUnicodeString (NTDLL.@)
168 void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
170 if (str->Buffer) RtlFreeHeap( ntdll_get_process_heap(), 0, str->Buffer );
174 /**************************************************************************
175 * RtlCopyUnicodeString (NTDLL.@)
177 void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
181 unsigned int len = min( src->Length, dst->MaximumLength );
182 memcpy( dst->Buffer, src->Buffer, len );
184 /* append terminating NULL if enough space */
185 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
187 else dst->Length = 0;
191 /**************************************************************************
192 * RtlEraseUnicodeString (NTDLL.@)
194 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
198 memset( str->Buffer, 0, str->MaximumLength );
209 /******************************************************************************
210 * RtlCompareString (NTDLL.@)
212 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
218 len = min(s1->Length, s2->Length);
224 while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++);
228 while (!ret && len--) ret = *p1++ - *p2++;
230 if (!ret) ret = s1->Length - s2->Length;
235 /******************************************************************************
236 * RtlCompareUnicodeString (NTDLL.@)
238 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
239 BOOLEAN CaseInsensitive )
245 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
251 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
255 while (!ret && len--) ret = *p1++ - *p2++;
257 if (!ret) ret = s1->Length - s2->Length;
262 /**************************************************************************
263 * RtlEqualString (NTDLL.@)
265 * Determine if two strings are equal.
268 * s1 [I] Source string
269 * s2 [I] String to compare to s1
270 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
273 * Non-zero if s1 is equal to s2, 0 otherwise.
275 BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
277 if (s1->Length != s2->Length) return FALSE;
278 return !RtlCompareString( s1, s2, CaseInsensitive );
282 /**************************************************************************
283 * RtlEqualUnicodeString (NTDLL.@)
285 * Unicode version of RtlEqualString.
287 BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
288 BOOLEAN CaseInsensitive )
290 if (s1->Length != s2->Length) return FALSE;
291 return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
295 /**************************************************************************
296 * RtlPrefixString (NTDLL.@)
298 * Determine if one string is a prefix of another.
301 * s1 [I] Prefix to look for in s2
302 * s2 [I] String that may contain s1 as a prefix
303 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
306 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
308 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
312 if (s1->Length > s2->Length) return FALSE;
315 for (i = 0; i < s1->Length; i++)
316 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
320 for (i = 0; i < s1->Length; i++)
321 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
327 /**************************************************************************
328 * RtlPrefixUnicodeString (NTDLL.@)
330 * Unicode version of RtlPrefixString.
332 BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
333 const UNICODE_STRING *s2,
334 BOOLEAN ignore_case )
338 if (s1->Length > s2->Length) return FALSE;
341 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
342 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
346 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
347 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
353 /**************************************************************************
354 * RtlEqualComputerName (NTDLL.@)
356 * Determine if two computer names are the same.
359 * left [I] First computer name
360 * right [I] Second computer name
363 * 0 if the names are equal, non-zero otherwise.
366 * The comparason is case insensitive.
368 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
369 const UNICODE_STRING *right)
372 STRING upLeft, upRight;
374 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
376 if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
378 ret = RtlEqualString( &upLeft, &upRight, FALSE );
379 RtlFreeOemString( &upRight );
381 RtlFreeOemString( &upLeft );
386 /**************************************************************************
387 * RtlEqualDomainName (NTDLL.@)
389 * Determine if two domain names are the same.
392 * left [I] First domain name
393 * right [I] Second domain name
396 * 0 if the names are equal, non-zero otherwise.
399 * The comparason is case insensitive.
401 NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
402 const UNICODE_STRING *right)
404 return RtlEqualComputerName(left, right);
408 COPY BETWEEN ANSI_STRING or UNICODE_STRING
409 there is no parameter checking, it just crashes
413 /**************************************************************************
414 * RtlAnsiStringToUnicodeString (NTDLL.@)
417 * This function always writes a terminating NUL.
419 NTSTATUS WINAPI RtlAnsiStringToUnicodeString( PUNICODE_STRING uni,
423 DWORD total = RtlAnsiStringToUnicodeSize( ansi );
425 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
426 uni->Length = total - sizeof(WCHAR);
429 uni->MaximumLength = total;
430 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
431 return STATUS_NO_MEMORY;
433 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
435 RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
436 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
437 return STATUS_SUCCESS;
441 /**************************************************************************
442 * RtlOemStringToUnicodeString (NTDLL.@)
445 * This function always writes a terminating NUL.
446 * If the resulting length > 0xffff it returns STATUS_INVALID_PARAMETER_2
448 NTSTATUS WINAPI RtlOemStringToUnicodeString( UNICODE_STRING *uni,
452 DWORD total = RtlOemStringToUnicodeSize( oem );
454 if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
455 uni->Length = total - sizeof(WCHAR);
458 uni->MaximumLength = total;
459 if (!(uni->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, total )))
460 return STATUS_NO_MEMORY;
462 else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;
464 RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
465 uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
466 return STATUS_SUCCESS;
470 /**************************************************************************
471 * RtlUnicodeStringToAnsiString (NTDLL.@)
474 * This function always writes a terminating NUL.
475 * Performs a partial copy if ansi is too small.
477 NTSTATUS WINAPI RtlUnicodeStringToAnsiString( STRING *ansi,
478 const UNICODE_STRING *uni,
481 NTSTATUS ret = STATUS_SUCCESS;
482 DWORD len = RtlUnicodeStringToAnsiSize( uni );
484 ansi->Length = len - 1;
487 ansi->MaximumLength = len;
488 if (!(ansi->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
489 return STATUS_NO_MEMORY;
491 else if (ansi->MaximumLength < len)
493 if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
494 ansi->Length = ansi->MaximumLength - 1;
495 ret = STATUS_BUFFER_OVERFLOW;
498 RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
499 ansi->Buffer[ansi->Length] = 0;
504 /**************************************************************************
505 * RtlUnicodeStringToOemString (NTDLL.@)
507 * Convert a Rtl Unicode string to an OEM string.
510 * oem [O] Destination for OEM string
511 * uni [I] Source Unicode string
512 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
515 * Success: STATUS_SUCCESS. oem contains the converted string
516 * Failure: STATUS_BUFFER_OVERFLOW if doalloc is FALSE and oem is too small.
517 * STATUS_NO_MEMORY if doalloc is TRUE and allocation fails.
520 * If doalloc is TRUE, the length allocated is uni->Length + 1.
521 * This function always NUL terminates the string returned.
523 NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
524 const UNICODE_STRING *uni,
527 NTSTATUS ret = STATUS_SUCCESS;
528 DWORD len = RtlUnicodeStringToOemSize( uni );
530 oem->Length = len - 1;
533 oem->MaximumLength = len;
534 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
535 return STATUS_NO_MEMORY;
537 else if (oem->MaximumLength < len)
539 if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
540 oem->Length = oem->MaximumLength - 1;
541 ret = STATUS_BUFFER_OVERFLOW;
544 RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
545 oem->Buffer[oem->Length] = 0;
550 /**************************************************************************
551 * RtlMultiByteToUnicodeN (NTDLL.@)
554 * Performs a partial copy if dst is too small.
556 NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
557 LPCSTR src, DWORD srclen )
560 int ret = cp_mbstowcs( get_ansi_table(), 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
562 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
563 return STATUS_SUCCESS;
567 /**************************************************************************
568 * RtlOemToUnicodeN (NTDLL.@)
570 NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
571 LPCSTR src, DWORD srclen )
573 int ret = cp_mbstowcs( get_oem_table(), 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
575 *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
576 return STATUS_SUCCESS;
580 /**************************************************************************
581 * RtlUnicodeToMultiByteN (NTDLL.@)
583 NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
584 LPCWSTR src, DWORD srclen )
586 int ret = cp_wcstombs( get_ansi_table(), 0, src, srclen / sizeof(WCHAR),
587 dst, dstlen, NULL, NULL );
589 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
590 return STATUS_SUCCESS;
594 /**************************************************************************
595 * RtlUnicodeToOemN (NTDLL.@)
597 NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
598 LPCWSTR src, DWORD srclen )
600 int ret = cp_wcstombs( get_oem_table(), 0, src, srclen / sizeof(WCHAR),
601 dst, dstlen, NULL, NULL );
603 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
604 return STATUS_SUCCESS;
613 /**************************************************************************
614 * RtlUpperChar (NTDLL.@)
616 * Convert an Ascii character to uppercase.
619 * ch [I] Character to convert
622 * The uppercase character value.
624 CHAR WINAPI RtlUpperChar( CHAR ch )
626 if (ch >= 'a' && ch <= 'z') {
627 return ch - 'a' + 'A';
634 /**************************************************************************
635 * RtlUpperString (NTDLL.@)
637 * Convert an Ascii string to uppercase.
640 * dst [O] Destination for converted string
641 * src [I] Source string to convert
646 void WINAPI RtlUpperString( STRING *dst, const STRING *src )
648 unsigned int i, len = min(src->Length, dst->MaximumLength);
650 for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
655 /**************************************************************************
656 * RtlUpcaseUnicodeChar (NTDLL.@)
658 * Unicode version of of RtlUpperChar.
660 WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
662 return toupperW(wch);
665 /**************************************************************************
666 * RtlDowncaseUnicodeChar (NTDLL.@)
668 * Convert a Unicode character to lowercase.
671 * wch [I] Character to convert
674 * The lowercase character value.
676 WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
678 return tolowerW(wch);
681 /**************************************************************************
682 * RtlUpcaseUnicodeString (NTDLL.@)
684 * Convert a Unicode string to uppercase.
687 * dest [O] Destination for converted string
688 * src [I] Source string to convert
689 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
692 * Success: STATUS_SUCCESS. dest contains the converted string.
693 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
694 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
697 * dest is never NUL terminated because it may be equal to src, and src
698 * might not be NUL terminated. dest->Length is only set upon success.
700 NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
701 const UNICODE_STRING *src,
704 DWORD i, len = src->Length;
708 dest->MaximumLength = len;
709 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
710 return STATUS_NO_MEMORY;
712 else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;
714 for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
716 return STATUS_SUCCESS;
720 /**************************************************************************
721 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
724 * writes terminating 0
726 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
727 const UNICODE_STRING *src,
731 UNICODE_STRING upcase;
733 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
735 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
736 RtlFreeUnicodeString( &upcase );
742 /**************************************************************************
743 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
746 * writes terminating 0
748 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
749 const UNICODE_STRING *src,
753 UNICODE_STRING upcase;
755 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
757 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
758 RtlFreeUnicodeString( &upcase );
764 /**************************************************************************
765 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
767 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
768 LPCWSTR src, DWORD srclen )
774 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
775 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
776 ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
777 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
782 /**************************************************************************
783 * RtlUpcaseUnicodeToOemN (NTDLL.@)
785 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
786 LPCWSTR src, DWORD srclen )
792 if (!(upcase = RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen ))) return STATUS_NO_MEMORY;
793 for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
794 ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
795 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase );
805 /**************************************************************************
806 * RtlOemStringToUnicodeSize (NTDLL.@)
807 * RtlxOemStringToUnicodeSize (NTDLL.@)
809 * Calculate the size in bytes necessary for the Unicode conversion of str,
810 * including the terminating NUL.
813 * str [I] String to calculate the size of
816 * The calculated size.
818 UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
820 int ret = cp_mbstowcs( get_oem_table(), 0, str->Buffer, str->Length, NULL, 0 );
821 return (ret + 1) * sizeof(WCHAR);
825 /**************************************************************************
826 * RtlAnsiStringToUnicodeSize (NTDLL.@)
827 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
829 * Calculate the size in bytes necessary for the Unicode conversion of str,
830 * including the terminating NUL.
833 * str [I] String to calculate the size of
836 * The calculated size.
838 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
841 RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
842 return ret + sizeof(WCHAR);
846 /**************************************************************************
847 * RtlMultiByteToUnicodeSize (NTDLL.@)
849 * Compute the size in bytes necessary for the Unicode conversion of str,
850 * without the terminating NUL.
853 * size [O] Destination for size
854 * str [I] String to calculate the size of
855 * len [I] Length of str
860 NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
862 *size = cp_mbstowcs( get_ansi_table(), 0, str, len, NULL, 0 ) * sizeof(WCHAR);
863 return STATUS_SUCCESS;
867 /**************************************************************************
868 * RtlUnicodeToMultiByteSize (NTDLL.@)
870 * Calculate the size necessary for the multibyte conversion of str,
871 * without the terminating NULL.
874 * size [O] Destination for size
875 * str [I] String to calculate the size of
876 * len [I] Length of str
881 NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
883 *size = cp_wcstombs( get_ansi_table(), 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
884 return STATUS_SUCCESS;
888 /**************************************************************************
889 * RtlUnicodeStringToAnsiSize (NTDLL.@)
890 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
892 * Calculate the size in bytes necessary for the Ansi conversion of str,
893 * including the terminating NUL.
896 * str [I] String to calculate the size of
899 * The calculated size.
901 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
904 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
909 /**************************************************************************
910 * RtlUnicodeStringToOemSize (NTDLL.@)
911 * RtlxUnicodeStringToOemSize (NTDLL.@)
913 * Calculate the size in bytes necessary for the OEM conversion of str,
914 * including the terminating NUL.
917 * str [I] String to calculate the size of
920 * The calculated size.
922 DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
924 return cp_wcstombs( get_oem_table(), 0, str->Buffer, str->Length / sizeof(WCHAR),
925 NULL, 0, NULL, NULL ) + 1;
929 /**************************************************************************
930 * RtlAppendStringToString (NTDLL.@)
932 NTSTATUS WINAPI RtlAppendStringToString( STRING *dst, const STRING *src )
934 unsigned int len = src->Length + dst->Length;
935 if (len > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
936 memcpy( dst->Buffer + dst->Length, src->Buffer, src->Length );
938 return STATUS_SUCCESS;
942 /**************************************************************************
943 * RtlAppendAsciizToString (NTDLL.@)
945 NTSTATUS WINAPI RtlAppendAsciizToString( STRING *dst, LPCSTR src )
949 unsigned int srclen = strlen(src);
950 unsigned int total = srclen + dst->Length;
951 if (total > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
952 memcpy( dst->Buffer + dst->Length, src, srclen );
955 return STATUS_SUCCESS;
959 /**************************************************************************
960 * RtlAppendUnicodeToString (NTDLL.@)
962 NTSTATUS WINAPI RtlAppendUnicodeToString( UNICODE_STRING *dst, LPCWSTR src )
966 unsigned int srclen = strlenW(src) * sizeof(WCHAR);
967 unsigned int total = srclen + dst->Length;
968 if (total > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
969 memcpy( dst->Buffer + dst->Length/sizeof(WCHAR), src, srclen );
971 /* append terminating NULL if enough space */
972 if (total < dst->MaximumLength) dst->Buffer[total / sizeof(WCHAR)] = 0;
974 return STATUS_SUCCESS;
978 /**************************************************************************
979 * RtlAppendUnicodeStringToString (NTDLL.@)
981 NTSTATUS WINAPI RtlAppendUnicodeStringToString( UNICODE_STRING *dst, const UNICODE_STRING *src )
983 unsigned int len = src->Length + dst->Length;
984 if (len > dst->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
985 memcpy( dst->Buffer + dst->Length/sizeof(WCHAR), src->Buffer, src->Length );
987 /* append terminating NULL if enough space */
988 if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
989 return STATUS_SUCCESS;
997 /**************************************************************************
998 * RtlIsTextUnicode (NTDLL.@)
1000 * Attempt to guess whether a text buffer is Unicode.
1003 * buf [I] Text buffer to test
1004 * len [I] Length of buf
1005 * pf [O] Destination for test results
1008 * The length of the string if all tests were passed, 0 otherwise.
1011 * Should implement more tests.
1013 DWORD WINAPI RtlIsTextUnicode(
1019 DWORD flags = -1, out_flags = 0;
1026 * Apply various tests to the text string. According to the
1027 * docs, each test "passed" sets the corresponding flag in
1028 * the output flags. But some of the tests are mutually
1029 * exclusive, so I don't see how you could pass all tests ...
1032 /* Check for an odd length ... pass if even. */
1034 out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;
1036 /* Check for the special unicode marker byte. */
1038 out_flags |= IS_TEXT_UNICODE_SIGNATURE;
1041 * Check whether the string passed all of the tests.
1043 flags &= ITU_IMPLEMENTED_TESTS;
1044 if ((out_flags & flags) != flags)
1053 /**************************************************************************
1054 * RtlCharToInteger (NTDLL.@)
1056 * Convert a character string into its integer equivalent.
1058 * On success assign an integer value and return STATUS_SUCCESS.
1059 * For base 0 accept: {whitespace} [+|-] [0[x|o|b]] {digits}
1060 * For bases 2, 8, 10 and 16 accept: {whitespace} [+|-] {digits}
1061 * For other bases return STATUS_INVALID_PARAMETER.
1062 * For value == NULL return STATUS_ACCESS_VIOLATION.
1063 * No check of value overflow: Just assign lower 32 bits (as native DLL).
1064 * Do not check for str != NULL (as native DLL).
1067 * - Do not read garbage behind '\0' as native DLL does.
1069 NTSTATUS WINAPI RtlCharToInteger(
1076 ULONG RunningTotal = 0;
1079 while (*str != '\0' && *str <= ' ') {
1085 } else if (*str == '-') {
1092 if (str[0] == '0') {
1093 if (str[1] == 'b') {
1096 } else if (str[1] == 'o') {
1099 } else if (str[1] == 'x') {
1104 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1105 return STATUS_INVALID_PARAMETER;
1108 if (value == NULL) {
1109 return STATUS_ACCESS_VIOLATION;
1112 while (*str != '\0') {
1114 if (chCurrent >= '0' && chCurrent <= '9') {
1115 digit = chCurrent - '0';
1116 } else if (chCurrent >= 'A' && chCurrent <= 'Z') {
1117 digit = chCurrent - 'A' + 10;
1118 } else if (chCurrent >= 'a' && chCurrent <= 'z') {
1119 digit = chCurrent - 'a' + 10;
1123 if (digit < 0 || digit >= base) {
1124 *value = bMinus ? -RunningTotal : RunningTotal;
1125 return STATUS_SUCCESS;
1128 RunningTotal = RunningTotal * base + digit;
1132 *value = bMinus ? -RunningTotal : RunningTotal;
1133 return STATUS_SUCCESS;
1137 /**************************************************************************
1138 * RtlIntegerToChar (NTDLL.@)
1140 * Convert an unsigned integer to a character string.
1143 * On success assign a string and return STATUS_SUCCESS.
1144 * If base is not 0 (=10), 2, 8, 10 or 16 return STATUS_INVALID_PARAMETER
1145 * Writes at most length characters to the string str.
1146 * Str is '\0' terminated when length allowes it.
1147 * When str fits exactly in length characters the '\0' is ommitted.
1148 * When str would be larger than length: return STATUS_BUFFER_OVERFLOW
1149 * For str == NULL return STATUS_ACCESS_VIOLATION.
1151 NTSTATUS WINAPI RtlIntegerToChar(
1164 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1165 return STATUS_INVALID_PARAMETER;
1173 digit = value % base;
1174 value = value / base;
1178 *pos = 'A' + digit - 10;
1180 } while (value != 0L);
1182 len = &buffer[32] - pos;
1184 return STATUS_BUFFER_OVERFLOW;
1185 } else if (str == NULL) {
1186 return STATUS_ACCESS_VIOLATION;
1187 } else if (len == length) {
1188 memcpy(str, pos, len);
1190 memcpy(str, pos, len + 1);
1192 return STATUS_SUCCESS;
1196 /**************************************************************************
1197 * RtlUnicodeStringToInteger (NTDLL.@)
1199 * Convert an unicode string into its integer equivalent.
1202 * On success assign an integer value and return STATUS_SUCCESS.
1203 * For base 0 accept: {whitespace} [+|-] [0[x|o|b]] {digits}
1204 * For bases 2, 8, 10 and 16 accept: {whitespace} [+|-] {digits}
1205 * For other bases return STATUS_INVALID_PARAMETER.
1206 * For value == NULL return STATUS_ACCESS_VIOLATION.
1207 * No check of value overflow: Just assign lower 32 bits (as native DLL).
1208 * Do not check for str != NULL (as native DLL).
1211 * - Do not read garbage on string length 0 as native DLL does.
1213 NTSTATUS WINAPI RtlUnicodeStringToInteger(
1214 const UNICODE_STRING *str,
1218 LPWSTR lpwstr = str->Buffer;
1219 USHORT CharsRemaining = str->Length / sizeof(WCHAR);
1222 ULONG RunningTotal = 0;
1225 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1230 if (CharsRemaining >= 1) {
1231 if (*lpwstr == '+') {
1234 } else if (*lpwstr == '-') {
1243 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1244 if (lpwstr[1] == 'b') {
1246 CharsRemaining -= 2;
1248 } else if (lpwstr[1] == 'o') {
1250 CharsRemaining -= 2;
1252 } else if (lpwstr[1] == 'x') {
1254 CharsRemaining -= 2;
1258 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1259 return STATUS_INVALID_PARAMETER;
1262 if (value == NULL) {
1263 return STATUS_ACCESS_VIOLATION;
1266 while (CharsRemaining >= 1) {
1267 wchCurrent = *lpwstr;
1268 if (wchCurrent >= '0' && wchCurrent <= '9') {
1269 digit = wchCurrent - '0';
1270 } else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
1271 digit = wchCurrent - 'A' + 10;
1272 } else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
1273 digit = wchCurrent - 'a' + 10;
1277 if (digit < 0 || digit >= base) {
1278 *value = bMinus ? -RunningTotal : RunningTotal;
1279 return STATUS_SUCCESS;
1282 RunningTotal = RunningTotal * base + digit;
1287 *value = bMinus ? -RunningTotal : RunningTotal;
1288 return STATUS_SUCCESS;
1292 /**************************************************************************
1293 * RtlIntegerToUnicodeString (NTDLL.@)
1295 * Convert an unsigned integer to a NULL terminated unicode string.
1298 * On success assign a NULL terminated string and return STATUS_SUCCESS.
1299 * If base is not 0 (=10), 2, 8, 10 or 16 return STATUS_INVALID_PARAMETER.
1300 * If str is too small to hold the string (with the NULL termination):
1301 * Set str->Length to the length the string would have (which can be
1302 * larger than the MaximumLength) and return STATUS_BUFFER_OVERFLOW.
1303 * Do not check for str != NULL (as native DLL).
1306 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1307 * The native DLL does this when the string would be longer than 16
1308 * characters even when the string parameter is long enough.
1310 NTSTATUS WINAPI RtlIntegerToUnicodeString(
1313 UNICODE_STRING *str)
1321 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1322 return STATUS_INVALID_PARAMETER;
1330 digit = value % base;
1331 value = value / base;
1335 *pos = 'A' + digit - 10;
1337 } while (value != 0L);
1339 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1340 if (str->Length >= str->MaximumLength) {
1341 return STATUS_BUFFER_OVERFLOW;
1343 memcpy(str->Buffer, pos, str->Length + 1);
1345 return STATUS_SUCCESS;