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
= wine_cp_get_table( 1252 );
50 inline static const union cptable
*get_oem_table(void)
52 if (!oem_table
) oem_table
= wine_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
);
387 /**************************************************************************
388 * RtlEqualDomainName (NTDLL.@)
390 * Determine if two domain names are the same.
393 * left [I] First domain name
394 * right [I] Second domain name
397 * 0 if the names are equal, non-zero otherwise.
400 * The comparason is case insensitive.
402 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
403 const UNICODE_STRING
*right
)
405 return RtlEqualComputerName(left
, right
);
410 COPY BETWEEN ANSI_STRING or UNICODE_STRING
411 there is no parameter checking, it just crashes
415 /**************************************************************************
416 * RtlAnsiStringToUnicodeString (NTDLL.@)
419 * This function always writes a terminating NUL.
421 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString( PUNICODE_STRING uni
,
425 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
427 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
428 uni
->Length
= total
- sizeof(WCHAR
);
431 uni
->MaximumLength
= total
;
432 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
433 return STATUS_NO_MEMORY
;
435 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
437 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
438 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
439 return STATUS_SUCCESS
;
443 /**************************************************************************
444 * RtlOemStringToUnicodeString (NTDLL.@)
447 * This function always writes a terminating NUL.
448 * If the resulting length > 0xffff it returns STATUS_INVALID_PARAMETER_2
450 NTSTATUS WINAPI
RtlOemStringToUnicodeString( UNICODE_STRING
*uni
,
454 DWORD total
= RtlOemStringToUnicodeSize( oem
);
456 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
457 uni
->Length
= total
- sizeof(WCHAR
);
460 uni
->MaximumLength
= total
;
461 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
462 return STATUS_NO_MEMORY
;
464 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
466 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
467 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
468 return STATUS_SUCCESS
;
472 /**************************************************************************
473 * RtlUnicodeStringToAnsiString (NTDLL.@)
475 * Converts an Unicode string to an Ansi string.
478 * Success: STATUS_SUCCESS. ansi contains the converted string
479 * Failure: STATUS_BUFFER_OVERFLOW if doalloc is FALSE and ansi is too small.
480 * STATUS_NO_MEMORY if doalloc is TRUE and the allocation fails.
483 * This function always writes a terminating '\0'.
484 * It performs a partial copy if ansi is too small.
486 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
487 STRING
*ansi
, /* [I/O] Destination for the Ansi string */
488 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
489 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
491 NTSTATUS ret
= STATUS_SUCCESS
;
492 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
494 ansi
->Length
= len
- 1;
497 ansi
->MaximumLength
= len
;
498 if (!(ansi
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
499 return STATUS_NO_MEMORY
;
501 else if (ansi
->MaximumLength
< len
)
503 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
504 ansi
->Length
= ansi
->MaximumLength
- 1;
505 ret
= STATUS_BUFFER_OVERFLOW
;
508 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
509 ansi
->Buffer
[ansi
->Length
] = 0;
514 /**************************************************************************
515 * RtlUnicodeStringToOemString (NTDLL.@)
517 * Converts a Rtl Unicode string to an OEM string.
520 * oem [O] Destination for OEM string
521 * uni [I] Source Unicode string
522 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
525 * Success: STATUS_SUCCESS. oem contains the converted string
526 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
527 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
530 * If doalloc is TRUE, the length allocated is uni->Length + 1.
531 * This function always NUL terminates the string returned.
533 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
534 const UNICODE_STRING
*uni
,
537 NTSTATUS ret
= STATUS_SUCCESS
;
538 DWORD len
= RtlUnicodeStringToOemSize( uni
);
540 oem
->Length
= len
- 1;
543 oem
->MaximumLength
= len
;
544 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
545 return STATUS_NO_MEMORY
;
547 else if (oem
->MaximumLength
< len
)
549 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
550 oem
->Length
= oem
->MaximumLength
- 1;
551 ret
= STATUS_BUFFER_OVERFLOW
;
554 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
555 oem
->Buffer
[oem
->Length
] = 0;
560 /**************************************************************************
561 * RtlMultiByteToUnicodeN (NTDLL.@)
564 * Performs a partial copy if dst is too small.
566 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
567 LPCSTR src
, DWORD srclen
)
570 int ret
= wine_cp_mbstowcs( get_ansi_table(), 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
572 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
573 return STATUS_SUCCESS
;
577 /**************************************************************************
578 * RtlOemToUnicodeN (NTDLL.@)
580 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
581 LPCSTR src
, DWORD srclen
)
583 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
585 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
586 return STATUS_SUCCESS
;
590 /**************************************************************************
591 * RtlUnicodeToMultiByteN (NTDLL.@)
593 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
594 LPCWSTR src
, DWORD srclen
)
596 int ret
= wine_cp_wcstombs( get_ansi_table(), 0, src
, srclen
/ sizeof(WCHAR
),
597 dst
, dstlen
, NULL
, NULL
);
599 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
600 return STATUS_SUCCESS
;
604 /**************************************************************************
605 * RtlUnicodeToOemN (NTDLL.@)
607 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
608 LPCWSTR src
, DWORD srclen
)
610 int ret
= wine_cp_wcstombs( get_oem_table(), 0, src
, srclen
/ sizeof(WCHAR
),
611 dst
, dstlen
, NULL
, NULL
);
613 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
614 return STATUS_SUCCESS
;
623 /**************************************************************************
624 * RtlUpperChar (NTDLL.@)
626 * Converts an Ascii character to uppercase.
629 * ch [I] Character to convert
632 * The uppercase character value.
635 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
636 * All other input characters are returned unchanged. The locale and
637 * multibyte characters are not taken into account (as native DLL).
639 CHAR WINAPI
RtlUpperChar( CHAR ch
)
641 if (ch
>= 'a' && ch
<= 'z') {
642 return ch
- 'a' + 'A';
649 /**************************************************************************
650 * RtlUpperString (NTDLL.@)
652 * Converts an Ascii string to uppercase.
655 * dst [O] Destination for converted string
656 * src [I] Source string to convert
662 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
663 * All other src characters are copied unchanged to dst. The locale and
664 * multibyte characters are not taken into account (as native DLL).
665 * The number of character copied is the minimum of src->Length and
666 * the dst->MaximumLength.
668 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
670 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
672 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
677 /**************************************************************************
678 * RtlUpcaseUnicodeChar (NTDLL.@)
680 * Converts an Unicode character to uppercase.
683 * wch [I] Character to convert
686 * The uppercase character value.
688 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
690 return toupperW(wch
);
694 /**************************************************************************
695 * RtlDowncaseUnicodeChar (NTDLL.@)
697 * Converts an Unicode character to lowercase.
700 * wch [I] Character to convert
703 * The lowercase character value.
705 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
707 return tolowerW(wch
);
711 /**************************************************************************
712 * RtlUpcaseUnicodeString (NTDLL.@)
714 * Converts an Unicode string to uppercase.
717 * dest [O] Destination for converted string
718 * src [I] Source string to convert
719 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
722 * Success: STATUS_SUCCESS. dest contains the converted string.
723 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
724 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
727 * dest is never NUL terminated because it may be equal to src, and src
728 * might not be NUL terminated. dest->Length is only set upon success.
730 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
731 const UNICODE_STRING
*src
,
734 DWORD i
, len
= src
->Length
;
738 dest
->MaximumLength
= len
;
739 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
740 return STATUS_NO_MEMORY
;
742 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
744 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
746 return STATUS_SUCCESS
;
750 /**************************************************************************
751 * RtlDowncaseUnicodeString (NTDLL.@)
753 * Converts an Unicode string to lowercase.
756 * dest [O] Destination for converted string
757 * src [I] Source string to convert
758 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
761 * Success: STATUS_SUCCESS. dest contains the converted string.
762 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
763 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
766 * dest is never NUL terminated because it may be equal to src, and src
767 * might not be NUL terminated. dest->Length is only set upon success.
769 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
770 UNICODE_STRING
*dest
,
771 const UNICODE_STRING
*src
,
775 DWORD len
= src
->Length
;
778 dest
->MaximumLength
= len
;
779 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) {
780 return STATUS_NO_MEMORY
;
782 } else if (len
> dest
->MaximumLength
) {
783 return STATUS_BUFFER_OVERFLOW
;
786 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
787 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
790 return STATUS_SUCCESS
;
794 /**************************************************************************
795 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
798 * writes terminating 0
800 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
801 const UNICODE_STRING
*src
,
805 UNICODE_STRING upcase
;
807 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
809 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
810 RtlFreeUnicodeString( &upcase
);
816 /**************************************************************************
817 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
820 * writes terminating 0
822 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
823 const UNICODE_STRING
*src
,
827 UNICODE_STRING upcase
;
829 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
831 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
832 RtlFreeUnicodeString( &upcase
);
838 /**************************************************************************
839 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
842 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
844 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
845 const UNICODE_STRING
*uni
,
849 UNICODE_STRING upcase
;
851 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
)))
853 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
857 oem
->MaximumLength
= len
;
858 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
860 ret
= STATUS_NO_MEMORY
;
864 else if (oem
->MaximumLength
< len
)
866 ret
= STATUS_BUFFER_OVERFLOW
;
867 oem
->Length
= oem
->MaximumLength
;
868 if (!oem
->MaximumLength
) goto done
;
870 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
872 RtlFreeUnicodeString( &upcase
);
878 /**************************************************************************
879 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
881 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
882 LPCWSTR src
, DWORD srclen
)
888 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
889 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
890 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
891 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
896 /**************************************************************************
897 * RtlUpcaseUnicodeToOemN (NTDLL.@)
899 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
900 LPCWSTR src
, DWORD srclen
)
906 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
907 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
908 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
909 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
919 /**************************************************************************
920 * RtlOemStringToUnicodeSize (NTDLL.@)
921 * RtlxOemStringToUnicodeSize (NTDLL.@)
923 * Calculate the size in bytes necessary for the Unicode conversion of str,
924 * including the terminating NUL.
927 * str [I] String to calculate the size of
930 * The calculated size.
932 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
934 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, str
->Buffer
, str
->Length
, NULL
, 0 );
935 return (ret
+ 1) * sizeof(WCHAR
);
939 /**************************************************************************
940 * RtlAnsiStringToUnicodeSize (NTDLL.@)
941 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
943 * Calculate the size in bytes necessary for the Unicode conversion of str,
944 * including the terminating NUL.
947 * str [I] String to calculate the size of
950 * The calculated size.
952 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
955 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
956 return ret
+ sizeof(WCHAR
);
960 /**************************************************************************
961 * RtlMultiByteToUnicodeSize (NTDLL.@)
963 * Compute the size in bytes necessary for the Unicode conversion of str,
964 * without the terminating NUL.
967 * size [O] Destination for size
968 * str [I] String to calculate the size of
969 * len [I] Length of str
974 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
976 *size
= wine_cp_mbstowcs( get_ansi_table(), 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
977 return STATUS_SUCCESS
;
981 /**************************************************************************
982 * RtlUnicodeToMultiByteSize (NTDLL.@)
984 * Calculate the size in bytes necessary for the multibyte conversion of str,
985 * without the terminating NULL.
988 * size [O] Destination for size
989 * str [I] String to calculate the size of
990 * len [I] Length of str
995 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
997 *size
= wine_cp_wcstombs( get_ansi_table(), 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
998 return STATUS_SUCCESS
;
1002 /**************************************************************************
1003 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1004 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1006 * Calculate the size in bytes necessary for the Ansi conversion of str,
1007 * including the terminating NUL.
1010 * str [I] String to calculate the size of
1013 * The calculated size.
1015 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1018 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1023 /**************************************************************************
1024 * RtlUnicodeStringToOemSize (NTDLL.@)
1025 * RtlxUnicodeStringToOemSize (NTDLL.@)
1027 * Calculate the size in bytes necessary for the OEM conversion of str,
1028 * including the terminating NUL.
1031 * str [I] String to calculate the size of
1034 * The calculated size.
1036 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1038 return wine_cp_wcstombs( get_oem_table(), 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1039 NULL
, 0, NULL
, NULL
) + 1;
1043 /**************************************************************************
1044 * RtlAppendAsciizToString (NTDLL.@)
1046 * Concatenates a buffered character string and a '\0' terminated character
1050 * Success: STATUS_SUCCESS. src is appended to dest.
1051 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1052 * to hold the concatenated string.
1055 * if src is NULL dest is unchanged.
1056 * dest is never '\0' terminated.
1058 NTSTATUS WINAPI
RtlAppendAsciizToString(
1059 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1060 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1063 unsigned int src_len
= strlen(src
);
1064 unsigned int dest_len
= src_len
+ dest
->Length
;
1066 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1067 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1068 dest
->Length
= dest_len
;
1070 return STATUS_SUCCESS
;
1074 /**************************************************************************
1075 * RtlAppendStringToString (NTDLL.@)
1077 * Concatenates two buffered character strings
1080 * Success: STATUS_SUCCESS. src is appended to dest.
1081 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1082 * to hold the concatenated string.
1085 * if src->length is zero dest is unchanged.
1086 * dest is never '\0' terminated.
1088 NTSTATUS WINAPI
RtlAppendStringToString(
1089 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1090 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1092 if (src
->Length
!= 0) {
1093 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1095 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1096 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1097 dest
->Length
= dest_len
;
1099 return STATUS_SUCCESS
;
1103 /**************************************************************************
1104 * RtlAppendUnicodeToString (NTDLL.@)
1106 * Concatenates an buffered unicode string and a '\0' terminated unicode
1110 * Success: STATUS_SUCCESS. src is appended to dest.
1111 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1112 * to hold the concatenated string.
1115 * if src is NULL dest is unchanged.
1116 * dest is '\0' terminated when the MaximumLength allowes it.
1117 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1120 * Does not write in the src->Buffer beyond MaximumLength when
1121 * MaximumLength is odd as the native function does.
1123 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1124 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1125 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1128 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1129 unsigned int dest_len
= src_len
+ dest
->Length
;
1131 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1132 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1133 dest
->Length
= dest_len
;
1134 /* append terminating NULL if enough space */
1135 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1136 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1139 return STATUS_SUCCESS
;
1143 /**************************************************************************
1144 * RtlAppendUnicodeStringToString (NTDLL.@)
1146 * Concatenates two buffered unicode strings
1149 * Success: STATUS_SUCCESS. src is appended to dest.
1150 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1151 * to hold the concatenated string.
1154 * if src->length is zero dest is unchanged.
1155 * dest is '\0' terminated when the MaximumLength allowes it.
1156 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1159 * Does not write in the src->Buffer beyond MaximumLength when
1160 * MaximumLength is odd as the native function does.
1162 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1163 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1164 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1166 if (src
->Length
!= 0) {
1167 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1169 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1170 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1171 dest
->Length
= dest_len
;
1172 /* append terminating NULL if enough space */
1173 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1174 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1177 return STATUS_SUCCESS
;
1186 /**************************************************************************
1187 * RtlIsTextUnicode (NTDLL.@)
1189 * Attempt to guess whether a text buffer is Unicode.
1192 * buf [I] Text buffer to test
1193 * len [I] Length of buf
1194 * pf [O] Destination for test results
1197 * The length of the string if all tests were passed, 0 otherwise.
1200 * Should implement more tests.
1202 DWORD WINAPI
RtlIsTextUnicode(
1208 DWORD flags
= -1, out_flags
= 0;
1215 * Apply various tests to the text string. According to the
1216 * docs, each test "passed" sets the corresponding flag in
1217 * the output flags. But some of the tests are mutually
1218 * exclusive, so I don't see how you could pass all tests ...
1221 /* Check for an odd length ... pass if even. */
1223 out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1225 /* Check for the special unicode marker byte. */
1227 out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1230 * Check whether the string passed all of the tests.
1232 flags
&= ITU_IMPLEMENTED_TESTS
;
1233 if ((out_flags
& flags
) != flags
)
1242 /**************************************************************************
1243 * RtlCharToInteger (NTDLL.@)
1245 * Converts a character string into its integer equivalent.
1248 * Success: STATUS_SUCCESS. value contains the converted number
1249 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1250 * STATUS_ACCESS_VIOLATION, if value is NULL.
1253 * For base 0 it uses 10 as base and the string should be in the format
1254 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1255 * For other bases the string should be in the format
1256 * "{whitespace} [+|-] {digits}".
1257 * No check is made for value overflow, only the lower 32 bits are assigned.
1258 * If str is NULL it crashes, as the native function does.
1261 * This function does not read garbage behind '\0' as the native version does.
1263 NTSTATUS WINAPI
RtlCharToInteger(
1264 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1265 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1266 ULONG
*value
) /* [O] Destination for the converted value */
1270 ULONG RunningTotal
= 0;
1273 while (*str
!= '\0' && *str
<= ' ') {
1279 } else if (*str
== '-') {
1286 if (str
[0] == '0') {
1287 if (str
[1] == 'b') {
1290 } else if (str
[1] == 'o') {
1293 } else if (str
[1] == 'x') {
1298 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1299 return STATUS_INVALID_PARAMETER
;
1302 if (value
== NULL
) {
1303 return STATUS_ACCESS_VIOLATION
;
1306 while (*str
!= '\0') {
1308 if (chCurrent
>= '0' && chCurrent
<= '9') {
1309 digit
= chCurrent
- '0';
1310 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1311 digit
= chCurrent
- 'A' + 10;
1312 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1313 digit
= chCurrent
- 'a' + 10;
1317 if (digit
< 0 || digit
>= base
) {
1318 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1319 return STATUS_SUCCESS
;
1322 RunningTotal
= RunningTotal
* base
+ digit
;
1326 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1327 return STATUS_SUCCESS
;
1331 /**************************************************************************
1332 * RtlIntegerToChar (NTDLL.@)
1334 * Converts an unsigned integer to a character string.
1337 * Success: STATUS_SUCCESS. str contains the converted number
1338 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1339 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1340 * STATUS_ACCESS_VIOLATION, if str is NULL.
1343 * Instead of base 0 it uses 10 as base.
1344 * Writes at most length characters to the string str.
1345 * Str is '\0' terminated when length allowes it.
1346 * When str fits exactly in length characters the '\0' is ommitted.
1348 NTSTATUS WINAPI
RtlIntegerToChar(
1349 ULONG value
, /* [I] Value to be converted */
1350 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1351 ULONG length
, /* [I] Length of the str buffer in bytes */
1352 PCHAR str
) /* [O] Destination for the converted value */
1361 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1362 return STATUS_INVALID_PARAMETER
;
1370 digit
= value
% base
;
1371 value
= value
/ base
;
1375 *pos
= 'A' + digit
- 10;
1377 } while (value
!= 0L);
1379 len
= &buffer
[32] - pos
;
1381 return STATUS_BUFFER_OVERFLOW
;
1382 } else if (str
== NULL
) {
1383 return STATUS_ACCESS_VIOLATION
;
1384 } else if (len
== length
) {
1385 memcpy(str
, pos
, len
);
1387 memcpy(str
, pos
, len
+ 1);
1389 return STATUS_SUCCESS
;
1393 /**************************************************************************
1394 * RtlUnicodeStringToInteger (NTDLL.@)
1396 * Converts an unicode string into its integer equivalent.
1399 * Success: STATUS_SUCCESS. value contains the converted number
1400 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1401 * STATUS_ACCESS_VIOLATION, if value is NULL.
1404 * For base 0 it uses 10 as base and the string should be in the format
1405 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1406 * For other bases the string should be in the format
1407 * "{whitespace} [+|-] {digits}".
1408 * No check is made for value overflow, only the lower 32 bits are assigned.
1409 * If str is NULL it crashes, as the native function does.
1412 * This function does not read garbage on string length 0 as the native
1415 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1416 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1417 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1418 ULONG
*value
) /* [O] Destination for the converted value */
1420 LPWSTR lpwstr
= str
->Buffer
;
1421 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1424 ULONG RunningTotal
= 0;
1427 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1432 if (CharsRemaining
>= 1) {
1433 if (*lpwstr
== '+') {
1436 } else if (*lpwstr
== '-') {
1445 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1446 if (lpwstr
[1] == 'b') {
1448 CharsRemaining
-= 2;
1450 } else if (lpwstr
[1] == 'o') {
1452 CharsRemaining
-= 2;
1454 } else if (lpwstr
[1] == 'x') {
1456 CharsRemaining
-= 2;
1460 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1461 return STATUS_INVALID_PARAMETER
;
1464 if (value
== NULL
) {
1465 return STATUS_ACCESS_VIOLATION
;
1468 while (CharsRemaining
>= 1) {
1469 wchCurrent
= *lpwstr
;
1470 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1471 digit
= wchCurrent
- '0';
1472 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1473 digit
= wchCurrent
- 'A' + 10;
1474 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1475 digit
= wchCurrent
- 'a' + 10;
1479 if (digit
< 0 || digit
>= base
) {
1480 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1481 return STATUS_SUCCESS
;
1484 RunningTotal
= RunningTotal
* base
+ digit
;
1489 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1490 return STATUS_SUCCESS
;
1494 /**************************************************************************
1495 * RtlIntegerToUnicodeString (NTDLL.@)
1497 * Converts an unsigned integer to a '\0' terminated unicode string.
1500 * Success: STATUS_SUCCESS. str contains the converted number
1501 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1502 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1503 * (with the '\0' termination). In this case str->Length
1504 * is set to the length, the string would have (which can
1505 * be larger than the MaximumLength).
1508 * Instead of base 0 it uses 10 as base.
1509 * If str is NULL it crashes, as the native function does.
1512 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1513 * The native function does this when the string would be longer than 16
1514 * characters even when the string parameter is long enough.
1516 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1517 ULONG value
, /* [I] Value to be converted */
1518 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1519 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1527 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1528 return STATUS_INVALID_PARAMETER
;
1536 digit
= value
% base
;
1537 value
= value
/ base
;
1541 *pos
= 'A' + digit
- 10;
1543 } while (value
!= 0L);
1545 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1546 if (str
->Length
>= str
->MaximumLength
) {
1547 return STATUS_BUFFER_OVERFLOW
;
1549 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1551 return STATUS_SUCCESS
;