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 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
841 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
842 LPCWSTR src
, DWORD srclen
)
848 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
849 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
850 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
851 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
856 /**************************************************************************
857 * RtlUpcaseUnicodeToOemN (NTDLL.@)
859 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
860 LPCWSTR src
, DWORD srclen
)
866 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
867 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
868 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
869 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
879 /**************************************************************************
880 * RtlOemStringToUnicodeSize (NTDLL.@)
881 * RtlxOemStringToUnicodeSize (NTDLL.@)
883 * Calculate the size in bytes necessary for the Unicode conversion of str,
884 * including the terminating NUL.
887 * str [I] String to calculate the size of
890 * The calculated size.
892 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
894 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, str
->Buffer
, str
->Length
, NULL
, 0 );
895 return (ret
+ 1) * sizeof(WCHAR
);
899 /**************************************************************************
900 * RtlAnsiStringToUnicodeSize (NTDLL.@)
901 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
903 * Calculate the size in bytes necessary for the Unicode conversion of str,
904 * including the terminating NUL.
907 * str [I] String to calculate the size of
910 * The calculated size.
912 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
915 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
916 return ret
+ sizeof(WCHAR
);
920 /**************************************************************************
921 * RtlMultiByteToUnicodeSize (NTDLL.@)
923 * Compute the size in bytes necessary for the Unicode conversion of str,
924 * without the terminating NUL.
927 * size [O] Destination for size
928 * str [I] String to calculate the size of
929 * len [I] Length of str
934 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
936 *size
= wine_cp_mbstowcs( get_ansi_table(), 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
937 return STATUS_SUCCESS
;
941 /**************************************************************************
942 * RtlUnicodeToMultiByteSize (NTDLL.@)
944 * Calculate the size in bytes necessary for the multibyte conversion of str,
945 * without the terminating NULL.
948 * size [O] Destination for size
949 * str [I] String to calculate the size of
950 * len [I] Length of str
955 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
957 *size
= wine_cp_wcstombs( get_ansi_table(), 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
958 return STATUS_SUCCESS
;
962 /**************************************************************************
963 * RtlUnicodeStringToAnsiSize (NTDLL.@)
964 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
966 * Calculate the size in bytes necessary for the Ansi conversion of str,
967 * including the terminating NUL.
970 * str [I] String to calculate the size of
973 * The calculated size.
975 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
978 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
983 /**************************************************************************
984 * RtlUnicodeStringToOemSize (NTDLL.@)
985 * RtlxUnicodeStringToOemSize (NTDLL.@)
987 * Calculate the size in bytes necessary for the OEM conversion of str,
988 * including the terminating NUL.
991 * str [I] String to calculate the size of
994 * The calculated size.
996 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
998 return wine_cp_wcstombs( get_oem_table(), 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
999 NULL
, 0, NULL
, NULL
) + 1;
1003 /**************************************************************************
1004 * RtlAppendAsciizToString (NTDLL.@)
1006 * Concatenates a buffered character string and a '\0' terminated character
1010 * Success: STATUS_SUCCESS. src is appended to dest.
1011 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1012 * to hold the concatenated string.
1015 * if src is NULL dest is unchanged.
1016 * dest is never '\0' terminated.
1018 NTSTATUS WINAPI
RtlAppendAsciizToString(
1019 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1020 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1023 unsigned int src_len
= strlen(src
);
1024 unsigned int dest_len
= src_len
+ dest
->Length
;
1026 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1027 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1028 dest
->Length
= dest_len
;
1030 return STATUS_SUCCESS
;
1034 /**************************************************************************
1035 * RtlAppendStringToString (NTDLL.@)
1037 * Concatenates two buffered character strings
1040 * Success: STATUS_SUCCESS. src is appended to dest.
1041 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1042 * to hold the concatenated string.
1045 * if src->length is zero dest is unchanged.
1046 * dest is never '\0' terminated.
1048 NTSTATUS WINAPI
RtlAppendStringToString(
1049 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1050 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1052 if (src
->Length
!= 0) {
1053 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1055 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1056 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1057 dest
->Length
= dest_len
;
1059 return STATUS_SUCCESS
;
1063 /**************************************************************************
1064 * RtlAppendUnicodeToString (NTDLL.@)
1066 * Concatenates an buffered unicode string and a '\0' terminated unicode
1070 * Success: STATUS_SUCCESS. src is appended to dest.
1071 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1072 * to hold the concatenated string.
1075 * if src is NULL dest is unchanged.
1076 * dest is '\0' terminated when the MaximumLength allowes it.
1077 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1080 * Does not write in the src->Buffer beyond MaximumLength when
1081 * MaximumLength is odd as the native function does.
1083 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1084 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1085 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1088 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1089 unsigned int dest_len
= src_len
+ dest
->Length
;
1091 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1092 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1093 dest
->Length
= dest_len
;
1094 /* append terminating NULL if enough space */
1095 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1096 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1099 return STATUS_SUCCESS
;
1103 /**************************************************************************
1104 * RtlAppendUnicodeStringToString (NTDLL.@)
1106 * Concatenates two buffered unicode strings
1109 * Success: STATUS_SUCCESS. src is appended to dest.
1110 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1111 * to hold the concatenated string.
1114 * if src->length is zero dest is unchanged.
1115 * dest is '\0' terminated when the MaximumLength allowes it.
1116 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1119 * Does not write in the src->Buffer beyond MaximumLength when
1120 * MaximumLength is odd as the native function does.
1122 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1123 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1124 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1126 if (src
->Length
!= 0) {
1127 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1129 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1130 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1131 dest
->Length
= dest_len
;
1132 /* append terminating NULL if enough space */
1133 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1134 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1137 return STATUS_SUCCESS
;
1146 /**************************************************************************
1147 * RtlIsTextUnicode (NTDLL.@)
1149 * Attempt to guess whether a text buffer is Unicode.
1152 * buf [I] Text buffer to test
1153 * len [I] Length of buf
1154 * pf [O] Destination for test results
1157 * The length of the string if all tests were passed, 0 otherwise.
1160 * Should implement more tests.
1162 DWORD WINAPI
RtlIsTextUnicode(
1168 DWORD flags
= -1, out_flags
= 0;
1175 * Apply various tests to the text string. According to the
1176 * docs, each test "passed" sets the corresponding flag in
1177 * the output flags. But some of the tests are mutually
1178 * exclusive, so I don't see how you could pass all tests ...
1181 /* Check for an odd length ... pass if even. */
1183 out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1185 /* Check for the special unicode marker byte. */
1187 out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1190 * Check whether the string passed all of the tests.
1192 flags
&= ITU_IMPLEMENTED_TESTS
;
1193 if ((out_flags
& flags
) != flags
)
1202 /**************************************************************************
1203 * RtlCharToInteger (NTDLL.@)
1205 * Converts a character string into its integer equivalent.
1208 * Success: STATUS_SUCCESS. value contains the converted number
1209 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1210 * STATUS_ACCESS_VIOLATION, if value is NULL.
1213 * For base 0 it uses 10 as base and the string should be in the format
1214 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1215 * For other bases the string should be in the format
1216 * "{whitespace} [+|-] {digits}".
1217 * No check is made for value overflow, only the lower 32 bits are assigned.
1218 * If str is NULL it crashes, as the native function does.
1221 * This function does not read garbage behind '\0' as the native version does.
1223 NTSTATUS WINAPI
RtlCharToInteger(
1224 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1225 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1226 ULONG
*value
) /* [O] Destination for the converted value */
1230 ULONG RunningTotal
= 0;
1233 while (*str
!= '\0' && *str
<= ' ') {
1239 } else if (*str
== '-') {
1246 if (str
[0] == '0') {
1247 if (str
[1] == 'b') {
1250 } else if (str
[1] == 'o') {
1253 } else if (str
[1] == 'x') {
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 (*str
!= '\0') {
1268 if (chCurrent
>= '0' && chCurrent
<= '9') {
1269 digit
= chCurrent
- '0';
1270 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1271 digit
= chCurrent
- 'A' + 10;
1272 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1273 digit
= chCurrent
- 'a' + 10;
1277 if (digit
< 0 || digit
>= base
) {
1278 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1279 return STATUS_SUCCESS
;
1282 RunningTotal
= RunningTotal
* base
+ digit
;
1286 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1287 return STATUS_SUCCESS
;
1291 /**************************************************************************
1292 * RtlIntegerToChar (NTDLL.@)
1294 * Converts an unsigned integer to a character string.
1297 * Success: STATUS_SUCCESS. str contains the converted number
1298 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1299 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1300 * STATUS_ACCESS_VIOLATION, if str is NULL.
1303 * Instead of base 0 it uses 10 as base.
1304 * Writes at most length characters to the string str.
1305 * Str is '\0' terminated when length allowes it.
1306 * When str fits exactly in length characters the '\0' is ommitted.
1308 NTSTATUS WINAPI
RtlIntegerToChar(
1309 ULONG value
, /* [I] Value to be converted */
1310 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1311 ULONG length
, /* [I] Length of the str buffer in bytes */
1312 PCHAR str
) /* [O] Destination for the converted value */
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 len
= &buffer
[32] - pos
;
1341 return STATUS_BUFFER_OVERFLOW
;
1342 } else if (str
== NULL
) {
1343 return STATUS_ACCESS_VIOLATION
;
1344 } else if (len
== length
) {
1345 memcpy(str
, pos
, len
);
1347 memcpy(str
, pos
, len
+ 1);
1349 return STATUS_SUCCESS
;
1353 /**************************************************************************
1354 * RtlUnicodeStringToInteger (NTDLL.@)
1356 * Converts an unicode string into its integer equivalent.
1359 * Success: STATUS_SUCCESS. value contains the converted number
1360 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1361 * STATUS_ACCESS_VIOLATION, if value is NULL.
1364 * For base 0 it uses 10 as base and the string should be in the format
1365 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1366 * For other bases the string should be in the format
1367 * "{whitespace} [+|-] {digits}".
1368 * No check is made for value overflow, only the lower 32 bits are assigned.
1369 * If str is NULL it crashes, as the native function does.
1372 * This function does not read garbage on string length 0 as the native
1375 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1376 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1377 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1378 ULONG
*value
) /* [O] Destination for the converted value */
1380 LPWSTR lpwstr
= str
->Buffer
;
1381 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1384 ULONG RunningTotal
= 0;
1387 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1392 if (CharsRemaining
>= 1) {
1393 if (*lpwstr
== '+') {
1396 } else if (*lpwstr
== '-') {
1405 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1406 if (lpwstr
[1] == 'b') {
1408 CharsRemaining
-= 2;
1410 } else if (lpwstr
[1] == 'o') {
1412 CharsRemaining
-= 2;
1414 } else if (lpwstr
[1] == 'x') {
1416 CharsRemaining
-= 2;
1420 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1421 return STATUS_INVALID_PARAMETER
;
1424 if (value
== NULL
) {
1425 return STATUS_ACCESS_VIOLATION
;
1428 while (CharsRemaining
>= 1) {
1429 wchCurrent
= *lpwstr
;
1430 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1431 digit
= wchCurrent
- '0';
1432 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1433 digit
= wchCurrent
- 'A' + 10;
1434 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1435 digit
= wchCurrent
- 'a' + 10;
1439 if (digit
< 0 || digit
>= base
) {
1440 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1441 return STATUS_SUCCESS
;
1444 RunningTotal
= RunningTotal
* base
+ digit
;
1449 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1450 return STATUS_SUCCESS
;
1454 /**************************************************************************
1455 * RtlIntegerToUnicodeString (NTDLL.@)
1457 * Converts an unsigned integer to a '\0' terminated unicode string.
1460 * Success: STATUS_SUCCESS. str contains the converted number
1461 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1462 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1463 * (with the '\0' termination). In this case str->Length
1464 * is set to the length, the string would have (which can
1465 * be larger than the MaximumLength).
1468 * Instead of base 0 it uses 10 as base.
1469 * If str is NULL it crashes, as the native function does.
1472 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1473 * The native function does this when the string would be longer than 16
1474 * characters even when the string parameter is long enough.
1476 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1477 ULONG value
, /* [I] Value to be converted */
1478 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1479 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1487 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1488 return STATUS_INVALID_PARAMETER
;
1496 digit
= value
% base
;
1497 value
= value
/ base
;
1501 *pos
= 'A' + digit
- 10;
1503 } while (value
!= 0L);
1505 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1506 if (str
->Length
>= str
->MaximumLength
) {
1507 return STATUS_BUFFER_OVERFLOW
;
1509 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1511 return STATUS_SUCCESS
;