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
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
36 #include "ntdll_misc.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
40 UINT NlsAnsiCodePage
= 0;
41 BYTE NlsMbCodePageTag
= 0;
42 BYTE NlsMbOemCodePageTag
= 0;
44 static const union cptable
*ansi_table
;
45 static const union cptable
*oem_table
;
46 static const union cptable
* unix_table
; /* NULL if UTF8 */
49 /**************************************************************************
50 * __wine_init_codepages (NTDLL.@)
52 * Set the code page once kernel32 is loaded. Should be done differently.
54 void __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
55 const union cptable
*ucp
)
60 NlsAnsiCodePage
= ansi
->info
.codepage
;
63 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
66 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
67 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
70 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
71 const char* defchar
, int *used
)
74 wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
) :
75 wine_utf8_wcstombs( src
, srclen
, dst
, dstlen
);
78 /**************************************************************************
79 * RtlInitAnsiString (NTDLL.@)
81 * Initializes a buffered ansi string.
87 * Assigns source to target->Buffer. The length of source is assigned to
88 * target->Length and target->MaximumLength. If source is NULL the length
89 * of source is assumed to be 0.
91 void WINAPI
RtlInitAnsiString(
92 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
93 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
95 if ((target
->Buffer
= (PCHAR
) source
))
97 target
->Length
= strlen(source
);
98 target
->MaximumLength
= target
->Length
+ 1;
100 else target
->Length
= target
->MaximumLength
= 0;
104 /**************************************************************************
105 * RtlInitString (NTDLL.@)
107 * Initializes a buffered string.
113 * Assigns source to target->Buffer. The length of source is assigned to
114 * target->Length and target->MaximumLength. If source is NULL the length
115 * of source is assumed to be 0.
117 void WINAPI
RtlInitString(
118 PSTRING target
, /* [I/O] Buffered string to be initialized */
119 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
121 RtlInitAnsiString( target
, source
);
125 /**************************************************************************
126 * RtlFreeAnsiString (NTDLL.@)
128 void WINAPI
RtlFreeAnsiString( PSTRING str
)
130 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
134 /**************************************************************************
135 * RtlFreeOemString (NTDLL.@)
137 void WINAPI
RtlFreeOemString( PSTRING str
)
139 RtlFreeAnsiString( str
);
143 /**************************************************************************
144 * RtlCopyString (NTDLL.@)
146 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
150 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
151 memcpy( dst
->Buffer
, src
->Buffer
, len
);
154 else dst
->Length
= 0;
158 /**************************************************************************
159 * RtlInitUnicodeString (NTDLL.@)
161 * Initializes a buffered unicode string.
167 * Assigns source to target->Buffer. The length of source is assigned to
168 * target->Length and target->MaximumLength. If source is NULL the length
169 * of source is assumed to be 0.
171 void WINAPI
RtlInitUnicodeString(
172 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
173 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
175 if ((target
->Buffer
= (PWSTR
) source
))
177 target
->Length
= strlenW(source
) * sizeof(WCHAR
);
178 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
180 else target
->Length
= target
->MaximumLength
= 0;
184 /**************************************************************************
185 * RtlInitUnicodeStringEx (NTDLL.@)
187 * Initializes a buffered unicode string.
190 * Success: STATUS_SUCCESS. target is initialized.
191 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
194 * Assigns source to target->Buffer. The length of source is assigned to
195 * target->Length and target->MaximumLength. If source is NULL the length
196 * of source is assumed to be 0.
198 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
199 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
200 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
202 if (source
!= NULL
) {
203 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
206 return STATUS_NAME_TOO_LONG
;
208 target
->Length
= len
;
209 target
->MaximumLength
= len
+ sizeof(WCHAR
);
210 target
->Buffer
= (PWSTR
) source
;
214 target
->MaximumLength
= 0;
215 target
->Buffer
= NULL
;
217 return STATUS_SUCCESS
;
221 /**************************************************************************
222 * RtlCreateUnicodeString (NTDLL.@)
224 * Creates a UNICODE_STRING from a null-terminated Unicode string.
230 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
232 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
233 if (!(target
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) return FALSE
;
234 memcpy( target
->Buffer
, src
, len
);
235 target
->MaximumLength
= len
;
236 target
->Length
= len
- sizeof(WCHAR
);
241 /**************************************************************************
242 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
244 * Creates a UNICODE_STRING from a null-terminated Ascii string.
250 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
253 RtlInitAnsiString( &ansi
, src
);
254 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
258 /**************************************************************************
259 * RtlFreeUnicodeString (NTDLL.@)
261 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
262 * RtlCreateUnicodeStringFromAsciiz().
267 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
269 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
273 /**************************************************************************
274 * RtlCopyUnicodeString (NTDLL.@)
276 * Copies from one UNICODE_STRING to another.
281 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
285 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
286 memcpy( dst
->Buffer
, src
->Buffer
, len
);
288 /* append terminating '\0' if enough space */
289 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
291 else dst
->Length
= 0;
295 /**************************************************************************
296 * RtlDuplicateUnicodeString (NTDLL.@)
298 * Duplicates an unicode string.
301 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
302 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
303 * STATUS_NO_MEMORY, if the allocation fails.
306 * For add_nul there are several possible values:
307 * 0 = destination will not be '\0' terminated,
308 * 1 = destination will be '\0' terminated,
309 * 3 = like 1 but for an empty source string produce '\0' terminated empty
310 * Buffer instead of assigning NULL to the Buffer.
311 * Other add_nul values are invalid.
313 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
314 int add_nul
, /* [I] flag */
315 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
316 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
318 if (source
== NULL
|| destination
== NULL
||
319 source
->Length
> source
->MaximumLength
||
320 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
321 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
322 return STATUS_INVALID_PARAMETER
;
324 if (source
->Length
== 0 && add_nul
!= 3) {
325 destination
->Length
= 0;
326 destination
->MaximumLength
= 0;
327 destination
->Buffer
= NULL
;
329 unsigned int destination_max_len
= source
->Length
;
332 destination_max_len
+= sizeof(WCHAR
);
334 destination
->Buffer
= RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len
);
335 if (destination
->Buffer
== NULL
) {
336 return STATUS_NO_MEMORY
;
338 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
339 destination
->Length
= source
->Length
;
340 destination
->MaximumLength
= source
->Length
;
341 /* append terminating '\0' if enough space */
343 destination
->MaximumLength
= destination_max_len
;
344 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
349 return STATUS_SUCCESS
;
353 /**************************************************************************
354 * RtlEraseUnicodeString (NTDLL.@)
356 * Overwrites a UNICODE_STRING with zeros.
361 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
365 memset( str
->Buffer
, 0, str
->MaximumLength
);
376 /******************************************************************************
377 * RtlCompareString (NTDLL.@)
379 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
385 len
= min(s1
->Length
, s2
->Length
);
391 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
395 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
397 if (!ret
) ret
= s1
->Length
- s2
->Length
;
402 /******************************************************************************
403 * RtlCompareUnicodeString (NTDLL.@)
405 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
406 BOOLEAN CaseInsensitive
)
412 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
418 while (!ret
&& len
--) ret
= toupperW(*p1
++) - toupperW(*p2
++);
422 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
424 if (!ret
) ret
= s1
->Length
- s2
->Length
;
429 /**************************************************************************
430 * RtlEqualString (NTDLL.@)
432 * Determine if two strings are equal.
435 * s1 [I] Source string
436 * s2 [I] String to compare to s1
437 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
440 * Non-zero if s1 is equal to s2, 0 otherwise.
442 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
444 if (s1
->Length
!= s2
->Length
) return FALSE
;
445 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
449 /**************************************************************************
450 * RtlEqualUnicodeString (NTDLL.@)
452 * Unicode version of RtlEqualString.
454 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
455 BOOLEAN CaseInsensitive
)
457 if (s1
->Length
!= s2
->Length
) return FALSE
;
458 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
462 /**************************************************************************
463 * RtlPrefixString (NTDLL.@)
465 * Determine if one string is a prefix of another.
468 * s1 [I] Prefix to look for in s2
469 * s2 [I] String that may contain s1 as a prefix
470 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
473 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
475 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
479 if (s1
->Length
> s2
->Length
) return FALSE
;
482 for (i
= 0; i
< s1
->Length
; i
++)
483 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
487 for (i
= 0; i
< s1
->Length
; i
++)
488 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
494 /**************************************************************************
495 * RtlPrefixUnicodeString (NTDLL.@)
497 * Unicode version of RtlPrefixString.
499 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
500 const UNICODE_STRING
*s2
,
501 BOOLEAN ignore_case
)
505 if (s1
->Length
> s2
->Length
) return FALSE
;
508 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
509 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
513 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
514 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
520 /**************************************************************************
521 * RtlEqualComputerName (NTDLL.@)
523 * Determine if two computer names are the same.
526 * left [I] First computer name
527 * right [I] Second computer name
530 * 0 if the names are equal, non-zero otherwise.
533 * The comparason is case insensitive.
535 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
536 const UNICODE_STRING
*right
)
539 STRING upLeft
, upRight
;
541 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
543 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
545 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
546 RtlFreeOemString( &upRight
);
548 RtlFreeOemString( &upLeft
);
554 /**************************************************************************
555 * RtlEqualDomainName (NTDLL.@)
557 * Determine if two domain names are the same.
560 * left [I] First domain name
561 * right [I] Second domain name
564 * 0 if the names are equal, non-zero otherwise.
567 * The comparason is case insensitive.
569 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
570 const UNICODE_STRING
*right
)
572 return RtlEqualComputerName(left
, right
);
577 COPY BETWEEN ANSI_STRING or UNICODE_STRING
578 there is no parameter checking, it just crashes
582 /**************************************************************************
583 * RtlAnsiStringToUnicodeString (NTDLL.@)
585 * Converts an ansi string to an unicode string.
588 * Success: STATUS_SUCCESS. uni contains the converted string
589 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
590 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
591 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
594 * This function always writes a terminating '\0'.
596 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
597 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
598 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
599 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
601 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
603 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
604 uni
->Length
= total
- sizeof(WCHAR
);
607 uni
->MaximumLength
= total
;
608 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
609 return STATUS_NO_MEMORY
;
611 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
613 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
614 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
615 return STATUS_SUCCESS
;
619 /**************************************************************************
620 * RtlOemStringToUnicodeString (NTDLL.@)
622 * Converts an oem string to an unicode string.
625 * Success: STATUS_SUCCESS. uni contains the converted string
626 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
627 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
628 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
631 * This function always writes a terminating '\0'.
633 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
634 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
635 const STRING
*oem
, /* [I] Oem string to be converted */
636 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
638 DWORD total
= RtlOemStringToUnicodeSize( oem
);
640 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
641 uni
->Length
= total
- sizeof(WCHAR
);
644 uni
->MaximumLength
= total
;
645 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
646 return STATUS_NO_MEMORY
;
648 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
650 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
651 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
652 return STATUS_SUCCESS
;
656 /**************************************************************************
657 * RtlUnicodeStringToAnsiString (NTDLL.@)
659 * Converts an unicode string to an ansi string.
662 * Success: STATUS_SUCCESS. ansi contains the converted string
663 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
664 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
667 * This function always writes a terminating '\0'.
668 * It performs a partial copy if ansi is too small.
670 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
671 STRING
*ansi
, /* [I/O] Destination for the ansi string */
672 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
673 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
675 NTSTATUS ret
= STATUS_SUCCESS
;
676 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
678 ansi
->Length
= len
- 1;
681 ansi
->MaximumLength
= len
;
682 if (!(ansi
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
683 return STATUS_NO_MEMORY
;
685 else if (ansi
->MaximumLength
< len
)
687 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
688 ansi
->Length
= ansi
->MaximumLength
- 1;
689 ret
= STATUS_BUFFER_OVERFLOW
;
692 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
693 ansi
->Buffer
[ansi
->Length
] = 0;
698 /**************************************************************************
699 * RtlUnicodeStringToOemString (NTDLL.@)
701 * Converts a Rtl Unicode string to an OEM string.
704 * oem [O] Destination for OEM string
705 * uni [I] Source Unicode string
706 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
709 * Success: STATUS_SUCCESS. oem contains the converted string
710 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
711 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
714 * If doalloc is TRUE, the length allocated is uni->Length + 1.
715 * This function always '\0' terminates the string returned.
717 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
718 const UNICODE_STRING
*uni
,
721 NTSTATUS ret
= STATUS_SUCCESS
;
722 DWORD len
= RtlUnicodeStringToOemSize( uni
);
724 oem
->Length
= len
- 1;
727 oem
->MaximumLength
= len
;
728 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
729 return STATUS_NO_MEMORY
;
731 else if (oem
->MaximumLength
< len
)
733 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
734 oem
->Length
= oem
->MaximumLength
- 1;
735 ret
= STATUS_BUFFER_OVERFLOW
;
738 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
739 oem
->Buffer
[oem
->Length
] = 0;
744 /**************************************************************************
745 * RtlMultiByteToUnicodeN (NTDLL.@)
747 * Converts a multi-byte string to a Unicode string.
753 * Performs a partial copy if dst is too small.
755 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
756 LPCSTR src
, DWORD srclen
)
759 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
761 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
762 return STATUS_SUCCESS
;
766 /**************************************************************************
767 * RtlOemToUnicodeN (NTDLL.@)
769 * Converts a multi-byte string in the OEM code page to a Unicode string.
774 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
775 LPCSTR src
, DWORD srclen
)
777 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
779 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
780 return STATUS_SUCCESS
;
784 /**************************************************************************
785 * RtlUnicodeToMultiByteN (NTDLL.@)
787 * Converts a Unicode string to a multi-byte string in the ANSI code page.
792 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
793 LPCWSTR src
, DWORD srclen
)
795 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
796 dst
, dstlen
, NULL
, NULL
);
798 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
799 return STATUS_SUCCESS
;
803 /**************************************************************************
804 * RtlUnicodeToOemN (NTDLL.@)
806 * Converts a Unicode string to a multi-byte string in the OEM code page.
811 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
812 LPCWSTR src
, DWORD srclen
)
814 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
815 dst
, dstlen
, NULL
, NULL
);
817 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
818 return STATUS_SUCCESS
;
827 /**************************************************************************
828 * RtlUpperChar (NTDLL.@)
830 * Converts an Ascii character to uppercase.
833 * ch [I] Character to convert
836 * The uppercase character value.
839 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
840 * All other input characters are returned unchanged. The locale and
841 * multibyte characters are not taken into account (as native DLL).
843 CHAR WINAPI
RtlUpperChar( CHAR ch
)
845 if (ch
>= 'a' && ch
<= 'z') {
846 return ch
- 'a' + 'A';
853 /**************************************************************************
854 * RtlUpperString (NTDLL.@)
856 * Converts an Ascii string to uppercase.
859 * dst [O] Destination for converted string
860 * src [I] Source string to convert
866 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
867 * All other src characters are copied unchanged to dst. The locale and
868 * multibyte characters are not taken into account (as native DLL).
869 * The number of character copied is the minimum of src->Length and
870 * the dst->MaximumLength.
872 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
874 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
876 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
881 /**************************************************************************
882 * RtlUpcaseUnicodeChar (NTDLL.@)
884 * Converts an Unicode character to uppercase.
887 * wch [I] Character to convert
890 * The uppercase character value.
892 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
894 return toupperW(wch
);
898 /**************************************************************************
899 * RtlDowncaseUnicodeChar (NTDLL.@)
901 * Converts an Unicode character to lowercase.
904 * wch [I] Character to convert
907 * The lowercase character value.
909 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
911 return tolowerW(wch
);
915 /**************************************************************************
916 * RtlUpcaseUnicodeString (NTDLL.@)
918 * Converts an Unicode string to uppercase.
921 * dest [O] Destination for converted string
922 * src [I] Source string to convert
923 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
926 * Success: STATUS_SUCCESS. dest contains the converted string.
927 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
928 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
931 * dest is never '\0' terminated because it may be equal to src, and src
932 * might not be '\0' terminated. dest->Length is only set upon success.
934 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
935 const UNICODE_STRING
*src
,
938 DWORD i
, len
= src
->Length
;
942 dest
->MaximumLength
= len
;
943 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
944 return STATUS_NO_MEMORY
;
946 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
948 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
950 return STATUS_SUCCESS
;
954 /**************************************************************************
955 * RtlDowncaseUnicodeString (NTDLL.@)
957 * Converts an Unicode string to lowercase.
960 * dest [O] Destination for converted string
961 * src [I] Source string to convert
962 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
965 * Success: STATUS_SUCCESS. dest contains the converted string.
966 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
967 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
970 * dest is never '\0' terminated because it may be equal to src, and src
971 * might not be '\0' terminated. dest->Length is only set upon success.
973 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
974 UNICODE_STRING
*dest
,
975 const UNICODE_STRING
*src
,
979 DWORD len
= src
->Length
;
982 dest
->MaximumLength
= len
;
983 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) {
984 return STATUS_NO_MEMORY
;
986 } else if (len
> dest
->MaximumLength
) {
987 return STATUS_BUFFER_OVERFLOW
;
990 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
991 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
994 return STATUS_SUCCESS
;
998 /**************************************************************************
999 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1001 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1007 * writes terminating 0
1009 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1010 const UNICODE_STRING
*src
,
1014 UNICODE_STRING upcase
;
1016 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1018 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1019 RtlFreeUnicodeString( &upcase
);
1025 /**************************************************************************
1026 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1028 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1029 * stored in STRING format.
1035 * writes terminating 0
1037 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1038 const UNICODE_STRING
*src
,
1042 UNICODE_STRING upcase
;
1044 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1046 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1047 RtlFreeUnicodeString( &upcase
);
1053 /**************************************************************************
1054 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1056 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1057 * stored in STRING format.
1063 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1065 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1066 const UNICODE_STRING
*uni
,
1070 UNICODE_STRING upcase
;
1072 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
)))
1074 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1078 oem
->MaximumLength
= len
;
1079 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
1081 ret
= STATUS_NO_MEMORY
;
1085 else if (oem
->MaximumLength
< len
)
1087 ret
= STATUS_BUFFER_OVERFLOW
;
1088 oem
->Length
= oem
->MaximumLength
;
1089 if (!oem
->MaximumLength
) goto done
;
1091 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1093 RtlFreeUnicodeString( &upcase
);
1099 /**************************************************************************
1100 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1102 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1107 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1108 LPCWSTR src
, DWORD srclen
)
1114 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1115 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1116 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1117 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1122 /**************************************************************************
1123 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1125 * Converts a Unicode string to the equivalent OEM upper-case representation.
1130 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1131 LPCWSTR src
, DWORD srclen
)
1137 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1138 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1139 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1140 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1150 /**************************************************************************
1151 * RtlOemStringToUnicodeSize (NTDLL.@)
1152 * RtlxOemStringToUnicodeSize (NTDLL.@)
1154 * Calculate the size in bytes necessary for the Unicode conversion of str,
1155 * including the terminating '\0'.
1158 * str [I] String to calculate the size of
1161 * The calculated size.
1163 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1165 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1166 return (ret
+ 1) * sizeof(WCHAR
);
1170 /**************************************************************************
1171 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1172 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1174 * Calculate the size in bytes necessary for the Unicode conversion of str,
1175 * including the terminating '\0'.
1178 * str [I] String to calculate the size of
1181 * The calculated size.
1183 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1186 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1187 return ret
+ sizeof(WCHAR
);
1191 /**************************************************************************
1192 * RtlMultiByteToUnicodeSize (NTDLL.@)
1194 * Compute the size in bytes necessary for the Unicode conversion of str,
1195 * without the terminating '\0'.
1198 * size [O] Destination for size
1199 * str [I] String to calculate the size of
1200 * len [I] Length of str
1205 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1207 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1208 return STATUS_SUCCESS
;
1212 /**************************************************************************
1213 * RtlUnicodeToMultiByteSize (NTDLL.@)
1215 * Calculate the size in bytes necessary for the multibyte conversion of str,
1216 * without the terminating '\0'.
1219 * size [O] Destination for size
1220 * str [I] String to calculate the size of
1221 * len [I] Length of str
1226 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1228 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1229 return STATUS_SUCCESS
;
1233 /**************************************************************************
1234 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1235 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1237 * Calculate the size in bytes necessary for the Ansi conversion of str,
1238 * including the terminating '\0'.
1241 * str [I] String to calculate the size of
1244 * The calculated size.
1246 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1249 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1254 /**************************************************************************
1255 * RtlUnicodeStringToOemSize (NTDLL.@)
1256 * RtlxUnicodeStringToOemSize (NTDLL.@)
1258 * Calculate the size in bytes necessary for the OEM conversion of str,
1259 * including the terminating '\0'.
1262 * str [I] String to calculate the size of
1265 * The calculated size.
1267 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1269 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1270 NULL
, 0, NULL
, NULL
) + 1;
1274 /**************************************************************************
1275 * RtlAppendAsciizToString (NTDLL.@)
1277 * Concatenates a buffered character string and a '\0' terminated character
1281 * Success: STATUS_SUCCESS. src is appended to dest.
1282 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1283 * to hold the concatenated string.
1286 * if src is NULL dest is unchanged.
1287 * dest is never '\0' terminated.
1289 NTSTATUS WINAPI
RtlAppendAsciizToString(
1290 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1291 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1294 unsigned int src_len
= strlen(src
);
1295 unsigned int dest_len
= src_len
+ dest
->Length
;
1297 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1298 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1299 dest
->Length
= dest_len
;
1301 return STATUS_SUCCESS
;
1305 /**************************************************************************
1306 * RtlAppendStringToString (NTDLL.@)
1308 * Concatenates two buffered character strings
1311 * Success: STATUS_SUCCESS. src is appended to dest.
1312 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1313 * to hold the concatenated string.
1316 * if src->length is zero dest is unchanged.
1317 * dest is never '\0' terminated.
1319 NTSTATUS WINAPI
RtlAppendStringToString(
1320 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1321 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1323 if (src
->Length
!= 0) {
1324 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1326 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1327 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1328 dest
->Length
= dest_len
;
1330 return STATUS_SUCCESS
;
1334 /**************************************************************************
1335 * RtlAppendUnicodeToString (NTDLL.@)
1337 * Concatenates an buffered unicode string and a '\0' terminated unicode
1341 * Success: STATUS_SUCCESS. src is appended to dest.
1342 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1343 * to hold the concatenated string.
1346 * if src is NULL dest is unchanged.
1347 * dest is '\0' terminated when the MaximumLength allowes it.
1348 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1351 * Does not write in the src->Buffer beyond MaximumLength when
1352 * MaximumLength is odd as the native function does.
1354 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1355 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1356 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1359 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1360 unsigned int dest_len
= src_len
+ dest
->Length
;
1362 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1363 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1364 dest
->Length
= dest_len
;
1365 /* append terminating '\0' if enough space */
1366 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1367 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1370 return STATUS_SUCCESS
;
1374 /**************************************************************************
1375 * RtlAppendUnicodeStringToString (NTDLL.@)
1377 * Concatenates two buffered unicode strings
1380 * Success: STATUS_SUCCESS. src is appended to dest.
1381 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1382 * to hold the concatenated string.
1385 * if src->length is zero dest is unchanged.
1386 * dest is '\0' terminated when the MaximumLength allowes it.
1387 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1390 * Does not write in the src->Buffer beyond MaximumLength when
1391 * MaximumLength is odd as the native function does.
1393 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1394 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1395 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1397 if (src
->Length
!= 0) {
1398 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1400 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1401 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1402 dest
->Length
= dest_len
;
1403 /* append terminating '\0' if enough space */
1404 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1405 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1408 return STATUS_SUCCESS
;
1412 /**************************************************************************
1413 * RtlFindCharInUnicodeString (NTDLL.@)
1415 * Searches for one of several unicode characters in an unicode string.
1418 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1419 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1421 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1422 int flags
, /* [I] Flags */
1423 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1424 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1425 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1428 unsigned int search_idx
;
1432 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1433 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1434 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1435 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1436 return STATUS_SUCCESS
;
1441 return STATUS_NOT_FOUND
;
1443 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1444 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1445 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1446 *pos
= main_idx
* sizeof(WCHAR
);
1447 return STATUS_SUCCESS
;
1452 return STATUS_NOT_FOUND
;
1454 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1456 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1457 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1460 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1461 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1462 return STATUS_SUCCESS
;
1466 return STATUS_NOT_FOUND
;
1468 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1470 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1471 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1474 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1475 *pos
= main_idx
* sizeof(WCHAR
);
1476 return STATUS_SUCCESS
;
1480 return STATUS_NOT_FOUND
;
1482 return STATUS_NOT_FOUND
;
1489 /* Tests that we currently implement */
1490 #define ITU_IMPLEMENTED_TESTS \
1491 (IS_TEXT_UNICODE_SIGNATURE | \
1492 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1493 IS_TEXT_UNICODE_ODD_LENGTH | \
1494 IS_TEXT_UNICODE_STATISTICS | \
1495 IS_TEXT_UNICODE_NULL_BYTES)
1497 /**************************************************************************
1498 * RtlIsTextUnicode (NTDLL.@)
1500 * Attempt to guess whether a text buffer is Unicode.
1503 * buf [I] Text buffer to test
1504 * len [I] Length of buf
1505 * pf [O] Destination for test results
1508 * The length of the string if all tests were passed, 0 otherwise.
1511 * Should implement more tests.
1513 DWORD WINAPI
RtlIsTextUnicode(
1519 DWORD flags
= -1, out_flags
= 0;
1526 * Apply various tests to the text string. According to the
1527 * docs, each test "passed" sets the corresponding flag in
1528 * the output flags. But some of the tests are mutually
1529 * exclusive, so I don't see how you could pass all tests ...
1532 /* Check for an odd length ... pass if even. */
1533 if ((flags
& IS_TEXT_UNICODE_ODD_LENGTH
) && (len
& 1))
1534 out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1536 /* Check for the special byte order unicode marks. */
1537 if ((flags
& IS_TEXT_UNICODE_SIGNATURE
) && *s
== 0xFEFF)
1538 out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1540 if ((flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
) && *s
== 0xFFFE)
1541 out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1543 /* apply some statistical analysis */
1544 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1547 /* FIXME: checks only for ASCII characters in the unicode stream */
1548 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1550 if (s
[i
] <= 255) stats
++;
1552 if (stats
> len
/ sizeof(WCHAR
) / 2)
1553 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1556 /* Check for unicode NULL chars */
1557 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1560 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1564 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1571 * Check whether the string passed all of the tests.
1573 flags
&= ITU_IMPLEMENTED_TESTS
;
1574 if ((out_flags
& flags
) != flags
)
1583 /**************************************************************************
1584 * RtlCharToInteger (NTDLL.@)
1586 * Converts a character string into its integer equivalent.
1589 * Success: STATUS_SUCCESS. value contains the converted number
1590 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1591 * STATUS_ACCESS_VIOLATION, if value is NULL.
1594 * For base 0 it uses 10 as base and the string should be in the format
1595 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1596 * For other bases the string should be in the format
1597 * "{whitespace} [+|-] {digits}".
1598 * No check is made for value overflow, only the lower 32 bits are assigned.
1599 * If str is NULL it crashes, as the native function does.
1602 * This function does not read garbage behind '\0' as the native version does.
1604 NTSTATUS WINAPI
RtlCharToInteger(
1605 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1606 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1607 ULONG
*value
) /* [O] Destination for the converted value */
1611 ULONG RunningTotal
= 0;
1614 while (*str
!= '\0' && *str
<= ' ') {
1620 } else if (*str
== '-') {
1627 if (str
[0] == '0') {
1628 if (str
[1] == 'b') {
1631 } else if (str
[1] == 'o') {
1634 } else if (str
[1] == 'x') {
1639 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1640 return STATUS_INVALID_PARAMETER
;
1643 if (value
== NULL
) {
1644 return STATUS_ACCESS_VIOLATION
;
1647 while (*str
!= '\0') {
1649 if (chCurrent
>= '0' && chCurrent
<= '9') {
1650 digit
= chCurrent
- '0';
1651 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1652 digit
= chCurrent
- 'A' + 10;
1653 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1654 digit
= chCurrent
- 'a' + 10;
1658 if (digit
< 0 || digit
>= base
) {
1659 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1660 return STATUS_SUCCESS
;
1663 RunningTotal
= RunningTotal
* base
+ digit
;
1667 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1668 return STATUS_SUCCESS
;
1672 /**************************************************************************
1673 * RtlIntegerToChar (NTDLL.@)
1675 * Converts an unsigned integer to a character string.
1678 * Success: STATUS_SUCCESS. str contains the converted number
1679 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1680 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1681 * STATUS_ACCESS_VIOLATION, if str is NULL.
1684 * Instead of base 0 it uses 10 as base.
1685 * Writes at most length characters to the string str.
1686 * Str is '\0' terminated when length allowes it.
1687 * When str fits exactly in length characters the '\0' is ommitted.
1689 NTSTATUS WINAPI
RtlIntegerToChar(
1690 ULONG value
, /* [I] Value to be converted */
1691 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1692 ULONG length
, /* [I] Length of the str buffer in bytes */
1693 PCHAR str
) /* [O] Destination for the converted value */
1702 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1703 return STATUS_INVALID_PARAMETER
;
1711 digit
= value
% base
;
1712 value
= value
/ base
;
1716 *pos
= 'A' + digit
- 10;
1718 } while (value
!= 0L);
1720 len
= &buffer
[32] - pos
;
1722 return STATUS_BUFFER_OVERFLOW
;
1723 } else if (str
== NULL
) {
1724 return STATUS_ACCESS_VIOLATION
;
1725 } else if (len
== length
) {
1726 memcpy(str
, pos
, len
);
1728 memcpy(str
, pos
, len
+ 1);
1730 return STATUS_SUCCESS
;
1734 /**************************************************************************
1735 * RtlUnicodeStringToInteger (NTDLL.@)
1737 * Converts an unicode string into its integer equivalent.
1740 * Success: STATUS_SUCCESS. value contains the converted number
1741 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1742 * STATUS_ACCESS_VIOLATION, if value is NULL.
1745 * For base 0 it uses 10 as base and the string should be in the format
1746 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1747 * For other bases the string should be in the format
1748 * "{whitespace} [+|-] {digits}".
1749 * No check is made for value overflow, only the lower 32 bits are assigned.
1750 * If str is NULL it crashes, as the native function does.
1753 * This function does not read garbage on string length 0 as the native
1756 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1757 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1758 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1759 ULONG
*value
) /* [O] Destination for the converted value */
1761 LPWSTR lpwstr
= str
->Buffer
;
1762 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1765 ULONG RunningTotal
= 0;
1768 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1773 if (CharsRemaining
>= 1) {
1774 if (*lpwstr
== '+') {
1777 } else if (*lpwstr
== '-') {
1786 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1787 if (lpwstr
[1] == 'b') {
1789 CharsRemaining
-= 2;
1791 } else if (lpwstr
[1] == 'o') {
1793 CharsRemaining
-= 2;
1795 } else if (lpwstr
[1] == 'x') {
1797 CharsRemaining
-= 2;
1801 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1802 return STATUS_INVALID_PARAMETER
;
1805 if (value
== NULL
) {
1806 return STATUS_ACCESS_VIOLATION
;
1809 while (CharsRemaining
>= 1) {
1810 wchCurrent
= *lpwstr
;
1811 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1812 digit
= wchCurrent
- '0';
1813 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1814 digit
= wchCurrent
- 'A' + 10;
1815 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1816 digit
= wchCurrent
- 'a' + 10;
1820 if (digit
< 0 || digit
>= base
) {
1821 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1822 return STATUS_SUCCESS
;
1825 RunningTotal
= RunningTotal
* base
+ digit
;
1830 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1831 return STATUS_SUCCESS
;
1835 /**************************************************************************
1836 * RtlIntegerToUnicodeString (NTDLL.@)
1838 * Converts an unsigned integer to a '\0' terminated unicode string.
1841 * Success: STATUS_SUCCESS. str contains the converted number
1842 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1843 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1844 * (with the '\0' termination). In this case str->Length
1845 * is set to the length, the string would have (which can
1846 * be larger than the MaximumLength).
1849 * Instead of base 0 it uses 10 as base.
1850 * If str is NULL it crashes, as the native function does.
1853 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1854 * The native function does this when the string would be longer than 16
1855 * characters even when the string parameter is long enough.
1857 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1858 ULONG value
, /* [I] Value to be converted */
1859 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1860 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1868 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1869 return STATUS_INVALID_PARAMETER
;
1877 digit
= value
% base
;
1878 value
= value
/ base
;
1882 *pos
= 'A' + digit
- 10;
1884 } while (value
!= 0L);
1886 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1887 if (str
->Length
>= str
->MaximumLength
) {
1888 return STATUS_BUFFER_OVERFLOW
;
1890 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1892 return STATUS_SUCCESS
;
1896 /*************************************************************************
1897 * RtlGUIDFromString (NTDLL.@)
1899 * Convert a string representation of a GUID into a GUID.
1902 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1903 * guid [O] Destination for the converted GUID
1906 * Success: STATUS_SUCCESS. guid contains the converted value.
1907 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1910 * See RtlStringFromGUID.
1912 NTSTATUS WINAPI
RtlGUIDFromString(const UNICODE_STRING
*str
, GUID
* guid
)
1915 const WCHAR
*lpszCLSID
= str
->Buffer
;
1916 BYTE
* lpOut
= (BYTE
*)guid
;
1918 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
1920 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1921 * to memory: DWORD... WORD WORD BYTES............
1928 if (*lpszCLSID
!= '{')
1929 return STATUS_INVALID_PARAMETER
;
1932 case 9: case 14: case 19: case 24:
1933 if (*lpszCLSID
!= '-')
1934 return STATUS_INVALID_PARAMETER
;
1938 if (*lpszCLSID
!= '}')
1939 return STATUS_INVALID_PARAMETER
;
1944 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
1947 /* Read two hex digits as a byte value */
1948 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
1949 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
1950 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
1951 else return STATUS_INVALID_PARAMETER
;
1953 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
1954 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
1955 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
1956 else return STATUS_INVALID_PARAMETER
;
1958 byte
= ch
<< 4 | ch2
;
1962 #ifndef WORDS_BIGENDIAN
1963 /* For Big Endian machines, we store the data such that the
1964 * dword/word members can be read as DWORDS and WORDS correctly. */
1966 case 1: lpOut
[3] = byte
; break;
1967 case 3: lpOut
[2] = byte
; break;
1968 case 5: lpOut
[1] = byte
; break;
1969 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
1971 case 10: case 15: lpOut
[1] = byte
; break;
1972 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
1975 default: lpOut
[0] = byte
; lpOut
++; break;
1977 lpszCLSID
++; /* Skip 2nd character of byte */
1985 return STATUS_SUCCESS
;
1988 /*************************************************************************
1989 * RtlStringFromGUID (NTDLL.@)
1991 * Convert a GUID into a string representation of a GUID.
1994 * guid [I] GUID to convert
1995 * str [O] Destination for the converted string
1998 * Success: STATUS_SUCCESS. str contains the converted value.
1999 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2002 * See RtlGUIDFromString.
2004 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2006 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2007 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2008 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2009 '%','0','2','X','%','0','2','X','}','\0' };
2011 TRACE("(%p,%p)\n", guid
, str
);
2013 str
->Buffer
= (WCHAR
*)RtlAllocateHeap( ntdll_get_process_heap(), 0, 40 * sizeof(WCHAR
));
2016 str
->Length
= str
->MaximumLength
= 0;
2017 return STATUS_NO_MEMORY
;
2019 str
->Length
= str
->MaximumLength
= 40;
2020 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2021 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2022 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2024 return STATUS_SUCCESS
;