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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
41 #define GUID_STRING_LENGTH 38
43 UINT NlsAnsiCodePage
= 0;
44 BYTE NlsMbCodePageTag
= 0;
45 BYTE NlsMbOemCodePageTag
= 0;
47 static const union cptable
*ansi_table
;
48 static const union cptable
*oem_table
;
49 static const union cptable
* unix_table
; /* NULL if UTF8 */
52 /**************************************************************************
53 * __wine_init_codepages (NTDLL.@)
55 * Set the code page once kernel32 is loaded. Should be done differently.
57 void CDECL
__wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
58 const union cptable
*ucp
)
63 NlsAnsiCodePage
= ansi
->info
.codepage
;
66 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
69 /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
70 if (!unix_table
) flags
|= MB_COMPOSITE
;
73 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
74 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
77 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
78 const char* defchar
, int *used
)
81 return wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
);
82 if (used
) *used
= 0; /* all chars are valid for UTF-8 */
83 return wine_utf8_wcstombs( flags
, src
, srclen
, dst
, dstlen
);
86 /**************************************************************************
87 * RtlInitAnsiString (NTDLL.@)
89 * Initializes a buffered ansi string.
95 * Assigns source to target->Buffer. The length of source is assigned to
96 * target->Length and target->MaximumLength. If source is NULL the length
97 * of source is assumed to be 0.
99 void WINAPI
RtlInitAnsiString(
100 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
101 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
103 if ((target
->Buffer
= (PCHAR
) source
))
105 target
->Length
= strlen(source
);
106 target
->MaximumLength
= target
->Length
+ 1;
108 else target
->Length
= target
->MaximumLength
= 0;
111 /**************************************************************************
112 * RtlInitAnsiStringEx (NTDLL.@)
114 * Initializes a buffered ansi string.
117 * An appropriate NTSTATUS value.
120 * Assigns source to target->Buffer. The length of source is assigned to
121 * target->Length and target->MaximumLength. If source is NULL the length
122 * of source is assumed to be 0.
124 NTSTATUS WINAPI
RtlInitAnsiStringEx(PANSI_STRING target
, PCSZ source
)
128 unsigned int len
= strlen(source
);
130 return STATUS_NAME_TOO_LONG
;
132 target
->Buffer
= (PCHAR
) source
;
133 target
->Length
= len
;
134 target
->MaximumLength
= len
+ 1;
138 target
->Buffer
= NULL
;
140 target
->MaximumLength
= 0;
142 return STATUS_SUCCESS
;
145 /**************************************************************************
146 * RtlInitString (NTDLL.@)
148 * Initializes a buffered string.
154 * Assigns source to target->Buffer. The length of source is assigned to
155 * target->Length and target->MaximumLength. If source is NULL the length
156 * of source is assumed to be 0.
158 void WINAPI
RtlInitString(
159 PSTRING target
, /* [I/O] Buffered string to be initialized */
160 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
162 RtlInitAnsiString( target
, source
);
166 /**************************************************************************
167 * RtlFreeAnsiString (NTDLL.@)
169 void WINAPI
RtlFreeAnsiString( PSTRING str
)
173 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
174 RtlZeroMemory( str
, sizeof(*str
) );
179 /**************************************************************************
180 * RtlFreeOemString (NTDLL.@)
182 void WINAPI
RtlFreeOemString( PSTRING str
)
184 RtlFreeAnsiString( str
);
188 /**************************************************************************
189 * RtlCopyString (NTDLL.@)
191 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
195 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
196 memcpy( dst
->Buffer
, src
->Buffer
, len
);
199 else dst
->Length
= 0;
203 /**************************************************************************
204 * RtlInitUnicodeString (NTDLL.@)
206 * Initializes a buffered unicode string.
212 * Assigns source to target->Buffer. The length of source is assigned to
213 * target->Length and target->MaximumLength. If source is NULL the length
214 * of source is assumed to be 0.
216 void WINAPI
RtlInitUnicodeString(
217 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
218 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
220 if ((target
->Buffer
= (PWSTR
) source
))
222 unsigned int length
= strlenW(source
) * sizeof(WCHAR
);
225 target
->Length
= length
;
226 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
228 else target
->Length
= target
->MaximumLength
= 0;
232 /**************************************************************************
233 * RtlInitUnicodeStringEx (NTDLL.@)
235 * Initializes a buffered unicode string.
238 * Success: STATUS_SUCCESS. target is initialized.
239 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
242 * Assigns source to target->Buffer. The length of source is assigned to
243 * target->Length and target->MaximumLength. If source is NULL the length
244 * of source is assumed to be 0.
246 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
247 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
248 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
250 if (source
!= NULL
) {
251 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
254 return STATUS_NAME_TOO_LONG
;
256 target
->Length
= len
;
257 target
->MaximumLength
= len
+ sizeof(WCHAR
);
258 target
->Buffer
= (PWSTR
) source
;
262 target
->MaximumLength
= 0;
263 target
->Buffer
= NULL
;
265 return STATUS_SUCCESS
;
269 /**************************************************************************
270 * RtlCreateUnicodeString (NTDLL.@)
272 * Creates a UNICODE_STRING from a null-terminated Unicode string.
278 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
280 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
281 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
282 memcpy( target
->Buffer
, src
, len
);
283 target
->MaximumLength
= len
;
284 target
->Length
= len
- sizeof(WCHAR
);
289 /**************************************************************************
290 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
292 * Creates a UNICODE_STRING from a null-terminated Ascii string.
298 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
301 RtlInitAnsiString( &ansi
, src
);
302 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
306 /**************************************************************************
307 * RtlFreeUnicodeString (NTDLL.@)
309 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
310 * RtlCreateUnicodeStringFromAsciiz().
315 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
319 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
320 RtlZeroMemory( str
, sizeof(*str
) );
325 /**************************************************************************
326 * RtlCopyUnicodeString (NTDLL.@)
328 * Copies from one UNICODE_STRING to another.
333 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
337 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
338 memcpy( dst
->Buffer
, src
->Buffer
, len
);
340 /* append terminating '\0' if enough space */
341 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
343 else dst
->Length
= 0;
347 /**************************************************************************
348 * RtlDuplicateUnicodeString (NTDLL.@)
350 * Duplicates a unicode string.
353 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
354 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
355 * STATUS_NO_MEMORY, if the allocation fails.
358 * For add_nul there are several possible values:
359 * 0 = destination will not be '\0' terminated,
360 * 1 = destination will be '\0' terminated,
361 * 3 = like 1 but for an empty source string produce '\0' terminated empty
362 * Buffer instead of assigning NULL to the Buffer.
363 * Other add_nul values are invalid.
365 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
366 int add_nul
, /* [I] flag */
367 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
368 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
370 if (source
== NULL
|| destination
== NULL
||
371 source
->Length
> source
->MaximumLength
||
372 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
373 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
374 return STATUS_INVALID_PARAMETER
;
376 if (source
->Length
== 0 && add_nul
!= 3) {
377 destination
->Length
= 0;
378 destination
->MaximumLength
= 0;
379 destination
->Buffer
= NULL
;
381 unsigned int destination_max_len
= source
->Length
;
384 destination_max_len
+= sizeof(WCHAR
);
386 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
387 if (destination
->Buffer
== NULL
) {
388 return STATUS_NO_MEMORY
;
390 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
391 destination
->Length
= source
->Length
;
392 destination
->MaximumLength
= source
->Length
;
393 /* append terminating '\0' if enough space */
395 destination
->MaximumLength
= destination_max_len
;
396 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
401 return STATUS_SUCCESS
;
405 /**************************************************************************
406 * RtlEraseUnicodeString (NTDLL.@)
408 * Overwrites a UNICODE_STRING with zeros.
413 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
417 memset( str
->Buffer
, 0, str
->MaximumLength
);
428 /******************************************************************************
429 * RtlCompareString (NTDLL.@)
431 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
437 len
= min(s1
->Length
, s2
->Length
);
443 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
447 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
449 if (!ret
) ret
= s1
->Length
- s2
->Length
;
454 /******************************************************************************
455 * RtlCompareUnicodeStrings (NTDLL.@)
457 LONG WINAPI
RtlCompareUnicodeStrings( const WCHAR
*s1
, SIZE_T len1
, const WCHAR
*s2
, SIZE_T len2
,
458 BOOLEAN case_insensitive
)
461 SIZE_T len
= min( len1
, len2
);
463 if (case_insensitive
)
465 while (!ret
&& len
--) ret
= toupperW(*s1
++) - toupperW(*s2
++);
469 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
471 if (!ret
) ret
= len1
- len2
;
476 /******************************************************************************
477 * RtlCompareUnicodeString (NTDLL.@)
479 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
480 BOOLEAN CaseInsensitive
)
482 return RtlCompareUnicodeStrings( s1
->Buffer
, s1
->Length
/ sizeof(WCHAR
),
483 s2
->Buffer
, s2
->Length
/ sizeof(WCHAR
), CaseInsensitive
);
487 /**************************************************************************
488 * RtlEqualString (NTDLL.@)
490 * Determine if two strings are equal.
493 * s1 [I] Source string
494 * s2 [I] String to compare to s1
495 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
498 * Non-zero if s1 is equal to s2, 0 otherwise.
500 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
502 if (s1
->Length
!= s2
->Length
) return FALSE
;
503 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
507 /**************************************************************************
508 * RtlEqualUnicodeString (NTDLL.@)
510 * Unicode version of RtlEqualString.
512 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
513 BOOLEAN CaseInsensitive
)
515 if (s1
->Length
!= s2
->Length
) return FALSE
;
516 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
520 /**************************************************************************
521 * RtlPrefixString (NTDLL.@)
523 * Determine if one string is a prefix of another.
526 * s1 [I] Prefix to look for in s2
527 * s2 [I] String that may contain s1 as a prefix
528 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
531 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
533 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
537 if (s1
->Length
> s2
->Length
) return FALSE
;
540 for (i
= 0; i
< s1
->Length
; i
++)
541 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
545 for (i
= 0; i
< s1
->Length
; i
++)
546 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
552 /**************************************************************************
553 * RtlPrefixUnicodeString (NTDLL.@)
555 * Unicode version of RtlPrefixString.
557 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
558 const UNICODE_STRING
*s2
,
559 BOOLEAN ignore_case
)
563 if (s1
->Length
> s2
->Length
) return FALSE
;
566 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
567 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
571 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
572 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
578 /**************************************************************************
579 * RtlEqualComputerName (NTDLL.@)
581 * Determine if two computer names are the same.
584 * left [I] First computer name
585 * right [I] Second computer name
588 * 0 if the names are equal, non-zero otherwise.
591 * The comparison is case insensitive.
593 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
594 const UNICODE_STRING
*right
)
597 STRING upLeft
, upRight
;
599 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
601 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
603 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
604 RtlFreeOemString( &upRight
);
606 RtlFreeOemString( &upLeft
);
612 /**************************************************************************
613 * RtlEqualDomainName (NTDLL.@)
615 * Determine if two domain names are the same.
618 * left [I] First domain name
619 * right [I] Second domain name
622 * 0 if the names are equal, non-zero otherwise.
625 * The comparison is case insensitive.
627 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
628 const UNICODE_STRING
*right
)
630 return RtlEqualComputerName(left
, right
);
634 /**************************************************************************
635 * RtlAnsiCharToUnicodeChar (NTDLL.@)
637 * Converts the first ansi character to a unicode character.
640 * ansi [I/O] Pointer to the ansi string.
643 * Unicode representation of the first character in the ansi string.
646 * Upon successful completion, the char pointer ansi points to is
647 * incremented by the size of the character.
649 WCHAR WINAPI
RtlAnsiCharToUnicodeChar(LPSTR
*ansi
)
652 DWORD charSize
= sizeof(CHAR
);
654 if (wine_is_dbcs_leadbyte(ansi_table
, **ansi
))
657 RtlMultiByteToUnicodeN(&str
, sizeof(WCHAR
), NULL
, *ansi
, charSize
);
664 COPY BETWEEN ANSI_STRING or UNICODE_STRING
665 there is no parameter checking, it just crashes
669 /**************************************************************************
670 * RtlAnsiStringToUnicodeString (NTDLL.@)
672 * Converts an ansi string to a unicode string.
675 * Success: STATUS_SUCCESS. uni contains the converted string
676 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
677 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
678 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
681 * This function always writes a terminating '\0'.
683 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
684 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
685 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
686 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
688 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
690 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
691 uni
->Length
= total
- sizeof(WCHAR
);
694 uni
->MaximumLength
= total
;
695 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
696 return STATUS_NO_MEMORY
;
698 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
700 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
701 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
702 return STATUS_SUCCESS
;
706 /**************************************************************************
707 * RtlOemStringToUnicodeString (NTDLL.@)
709 * Converts an oem string to a unicode string.
712 * Success: STATUS_SUCCESS. uni contains the converted string
713 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
714 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
715 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
718 * This function always writes a terminating '\0'.
720 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
721 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
722 const STRING
*oem
, /* [I] Oem string to be converted */
723 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
725 DWORD total
= RtlOemStringToUnicodeSize( oem
);
727 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
728 uni
->Length
= total
- sizeof(WCHAR
);
731 uni
->MaximumLength
= total
;
732 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
733 return STATUS_NO_MEMORY
;
735 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
737 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
738 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
739 return STATUS_SUCCESS
;
743 /**************************************************************************
744 * RtlUnicodeStringToAnsiString (NTDLL.@)
746 * Converts a unicode string to an ansi string.
749 * Success: STATUS_SUCCESS. ansi contains the converted string
750 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
751 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
754 * This function always writes a terminating '\0'.
755 * It performs a partial copy if ansi is too small.
757 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
758 STRING
*ansi
, /* [I/O] Destination for the ansi string */
759 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
760 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
762 NTSTATUS ret
= STATUS_SUCCESS
;
763 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
765 ansi
->Length
= len
- 1;
768 ansi
->MaximumLength
= len
;
769 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
770 return STATUS_NO_MEMORY
;
772 else if (ansi
->MaximumLength
< len
)
774 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
775 ansi
->Length
= ansi
->MaximumLength
- 1;
776 ret
= STATUS_BUFFER_OVERFLOW
;
779 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
780 ansi
->Buffer
[ansi
->Length
] = 0;
785 /**************************************************************************
786 * RtlUnicodeStringToOemString (NTDLL.@)
788 * Converts a Rtl Unicode string to an OEM string.
791 * oem [O] Destination for OEM string
792 * uni [I] Source Unicode string
793 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
796 * Success: STATUS_SUCCESS. oem contains the converted string
797 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
798 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
801 * If doalloc is TRUE, the length allocated is uni->Length + 1.
802 * This function always '\0' terminates the string returned.
804 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
805 const UNICODE_STRING
*uni
,
808 NTSTATUS ret
= STATUS_SUCCESS
;
809 DWORD len
= RtlUnicodeStringToOemSize( uni
);
811 oem
->Length
= len
- 1;
814 oem
->MaximumLength
= len
;
815 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
816 return STATUS_NO_MEMORY
;
818 else if (oem
->MaximumLength
< len
)
820 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
821 oem
->Length
= oem
->MaximumLength
- 1;
822 ret
= STATUS_BUFFER_OVERFLOW
;
825 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
826 oem
->Buffer
[oem
->Length
] = 0;
831 /**************************************************************************
832 * RtlMultiByteToUnicodeN (NTDLL.@)
834 * Converts a multi-byte string to a Unicode string.
840 * Performs a partial copy if dst is too small.
842 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
843 LPCSTR src
, DWORD srclen
)
846 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
848 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
849 return STATUS_SUCCESS
;
853 /**************************************************************************
854 * RtlOemToUnicodeN (NTDLL.@)
856 * Converts a multi-byte string in the OEM code page to a Unicode string.
861 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
862 LPCSTR src
, DWORD srclen
)
864 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
866 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
867 return STATUS_SUCCESS
;
871 /**************************************************************************
872 * RtlUnicodeToMultiByteN (NTDLL.@)
874 * Converts a Unicode string to a multi-byte string in the ANSI code page.
879 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
880 LPCWSTR src
, DWORD srclen
)
882 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
883 dst
, dstlen
, NULL
, NULL
);
885 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
886 return STATUS_SUCCESS
;
890 /**************************************************************************
891 * RtlUnicodeToOemN (NTDLL.@)
893 * Converts a Unicode string to a multi-byte string in the OEM code page.
898 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
899 LPCWSTR src
, DWORD srclen
)
901 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
902 dst
, dstlen
, NULL
, NULL
);
904 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
905 return STATUS_SUCCESS
;
914 /**************************************************************************
915 * RtlUpperChar (NTDLL.@)
917 * Converts an Ascii character to uppercase.
920 * ch [I] Character to convert
923 * The uppercase character value.
926 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
927 * All other input characters are returned unchanged. The locale and
928 * multibyte characters are not taken into account (as native DLL).
930 CHAR WINAPI
RtlUpperChar( CHAR ch
)
932 if (ch
>= 'a' && ch
<= 'z') {
933 return ch
- 'a' + 'A';
940 /**************************************************************************
941 * RtlUpperString (NTDLL.@)
943 * Converts an Ascii string to uppercase.
946 * dst [O] Destination for converted string
947 * src [I] Source string to convert
953 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
954 * All other src characters are copied unchanged to dst. The locale and
955 * multibyte characters are not taken into account (as native DLL).
956 * The number of character copied is the minimum of src->Length and
957 * the dst->MaximumLength.
959 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
961 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
963 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
968 /**************************************************************************
969 * RtlUpcaseUnicodeChar (NTDLL.@)
971 * Converts a Unicode character to uppercase.
974 * wch [I] Character to convert
977 * The uppercase character value.
979 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
981 return toupperW(wch
);
985 /**************************************************************************
986 * RtlDowncaseUnicodeChar (NTDLL.@)
988 * Converts a Unicode character to lowercase.
991 * wch [I] Character to convert
994 * The lowercase character value.
996 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
998 return tolowerW(wch
);
1002 /**************************************************************************
1003 * RtlUpcaseUnicodeString (NTDLL.@)
1005 * Converts a Unicode string to uppercase.
1008 * dest [O] Destination for converted string
1009 * src [I] Source string to convert
1010 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1013 * Success: STATUS_SUCCESS. dest contains the converted string.
1014 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1015 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1018 * dest is never '\0' terminated because it may be equal to src, and src
1019 * might not be '\0' terminated. dest->Length is only set upon success.
1021 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
1022 const UNICODE_STRING
*src
,
1025 DWORD i
, len
= src
->Length
;
1029 dest
->MaximumLength
= len
;
1030 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1031 return STATUS_NO_MEMORY
;
1033 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1035 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
1037 return STATUS_SUCCESS
;
1041 /**************************************************************************
1042 * RtlDowncaseUnicodeString (NTDLL.@)
1044 * Converts a Unicode string to lowercase.
1047 * dest [O] Destination for converted string
1048 * src [I] Source string to convert
1049 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1052 * Success: STATUS_SUCCESS. dest contains the converted string.
1053 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1054 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1057 * dest is never '\0' terminated because it may be equal to src, and src
1058 * might not be '\0' terminated. dest->Length is only set upon success.
1060 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
1061 UNICODE_STRING
*dest
,
1062 const UNICODE_STRING
*src
,
1066 DWORD len
= src
->Length
;
1069 dest
->MaximumLength
= len
;
1070 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
1071 return STATUS_NO_MEMORY
;
1073 } else if (len
> dest
->MaximumLength
) {
1074 return STATUS_BUFFER_OVERFLOW
;
1077 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
1078 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
1081 return STATUS_SUCCESS
;
1085 /**************************************************************************
1086 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1088 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1094 * writes terminating 0
1096 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1097 const UNICODE_STRING
*src
,
1101 UNICODE_STRING upcase
;
1103 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1105 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1106 RtlFreeUnicodeString( &upcase
);
1112 /**************************************************************************
1113 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1115 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1116 * stored in STRING format.
1122 * writes terminating 0
1124 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1125 const UNICODE_STRING
*src
,
1129 UNICODE_STRING upcase
;
1131 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1133 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1134 RtlFreeUnicodeString( &upcase
);
1140 /**************************************************************************
1141 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1143 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1144 * stored in STRING format.
1150 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1152 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1153 const UNICODE_STRING
*uni
,
1157 UNICODE_STRING upcase
;
1160 upcase
.Buffer
= tmp
;
1161 upcase
.MaximumLength
= sizeof(tmp
);
1162 ret
= RtlUpcaseUnicodeString( &upcase
, uni
, FALSE
);
1163 if (ret
== STATUS_BUFFER_OVERFLOW
) ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
);
1167 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1171 oem
->MaximumLength
= len
;
1172 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1174 ret
= STATUS_NO_MEMORY
;
1178 else if (oem
->MaximumLength
< len
)
1180 ret
= STATUS_BUFFER_OVERFLOW
;
1181 oem
->Length
= oem
->MaximumLength
;
1182 if (!oem
->MaximumLength
) goto done
;
1184 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1186 if (upcase
.Buffer
!= tmp
) RtlFreeUnicodeString( &upcase
);
1192 /**************************************************************************
1193 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1195 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1200 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1201 LPCWSTR src
, DWORD srclen
)
1207 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1208 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1209 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1210 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1215 /**************************************************************************
1216 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1218 * Converts a Unicode string to the equivalent OEM upper-case representation.
1223 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1224 LPCWSTR src
, DWORD srclen
)
1230 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1231 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1232 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1233 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1243 /**************************************************************************
1244 * RtlOemStringToUnicodeSize (NTDLL.@)
1245 * RtlxOemStringToUnicodeSize (NTDLL.@)
1247 * Calculate the size in bytes necessary for the Unicode conversion of str,
1248 * including the terminating '\0'.
1251 * str [I] String to calculate the size of
1254 * The calculated size.
1256 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1258 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1259 return (ret
+ 1) * sizeof(WCHAR
);
1263 /**************************************************************************
1264 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1265 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1267 * Calculate the size in bytes necessary for the Unicode conversion of str,
1268 * including the terminating '\0'.
1271 * str [I] String to calculate the size of
1274 * The calculated size.
1276 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1279 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1280 return ret
+ sizeof(WCHAR
);
1284 /**************************************************************************
1285 * RtlMultiByteToUnicodeSize (NTDLL.@)
1287 * Compute the size in bytes necessary for the Unicode conversion of str,
1288 * without the terminating '\0'.
1291 * size [O] Destination for size
1292 * str [I] String to calculate the size of
1293 * len [I] Length of str
1298 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1300 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1301 return STATUS_SUCCESS
;
1305 /**************************************************************************
1306 * RtlUnicodeToMultiByteSize (NTDLL.@)
1308 * Calculate the size in bytes necessary for the multibyte conversion of str,
1309 * without the terminating '\0'.
1312 * size [O] Destination for size
1313 * str [I] String to calculate the size of
1314 * len [I] Length of str
1319 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1321 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1322 return STATUS_SUCCESS
;
1326 /**************************************************************************
1327 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1328 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1330 * Calculate the size in bytes necessary for the Ansi conversion of str,
1331 * including the terminating '\0'.
1334 * str [I] String to calculate the size of
1337 * The calculated size.
1339 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1342 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1347 /**************************************************************************
1348 * RtlUnicodeStringToOemSize (NTDLL.@)
1349 * RtlxUnicodeStringToOemSize (NTDLL.@)
1351 * Calculate the size in bytes necessary for the OEM conversion of str,
1352 * including the terminating '\0'.
1355 * str [I] String to calculate the size of
1358 * The calculated size.
1360 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1362 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1363 NULL
, 0, NULL
, NULL
) + 1;
1367 /**************************************************************************
1368 * RtlAppendAsciizToString (NTDLL.@)
1370 * Concatenates a buffered character string and a '\0' terminated character
1374 * Success: STATUS_SUCCESS. src is appended to dest.
1375 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1376 * to hold the concatenated string.
1379 * if src is NULL dest is unchanged.
1380 * dest is never '\0' terminated.
1382 NTSTATUS WINAPI
RtlAppendAsciizToString(
1383 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1384 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1387 unsigned int src_len
= strlen(src
);
1388 unsigned int dest_len
= src_len
+ dest
->Length
;
1390 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1391 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1392 dest
->Length
= dest_len
;
1394 return STATUS_SUCCESS
;
1398 /**************************************************************************
1399 * RtlAppendStringToString (NTDLL.@)
1401 * Concatenates two buffered character strings
1404 * Success: STATUS_SUCCESS. src is appended to dest.
1405 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1406 * to hold the concatenated string.
1409 * if src->length is zero dest is unchanged.
1410 * dest is never '\0' terminated.
1412 NTSTATUS WINAPI
RtlAppendStringToString(
1413 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1414 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1416 if (src
->Length
!= 0) {
1417 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1419 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1420 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1421 dest
->Length
= dest_len
;
1423 return STATUS_SUCCESS
;
1427 /**************************************************************************
1428 * RtlAppendUnicodeToString (NTDLL.@)
1430 * Concatenates a buffered unicode string and a '\0' terminated unicode
1434 * Success: STATUS_SUCCESS. src is appended to dest.
1435 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1436 * to hold the concatenated string.
1439 * if src is NULL dest is unchanged.
1440 * dest is '\0' terminated when the MaximumLength allows it.
1441 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1444 * Does not write in the src->Buffer beyond MaximumLength when
1445 * MaximumLength is odd as the native function does.
1447 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1448 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1449 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1452 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1453 unsigned int dest_len
= src_len
+ dest
->Length
;
1455 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1456 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1457 dest
->Length
= dest_len
;
1458 /* append terminating '\0' if enough space */
1459 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1460 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1463 return STATUS_SUCCESS
;
1467 /**************************************************************************
1468 * RtlAppendUnicodeStringToString (NTDLL.@)
1470 * Concatenates two buffered unicode strings
1473 * Success: STATUS_SUCCESS. src is appended to dest.
1474 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1475 * to hold the concatenated string.
1478 * if src->length is zero dest is unchanged.
1479 * dest is '\0' terminated when the MaximumLength allows it.
1480 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1483 * Does not write in the src->Buffer beyond MaximumLength when
1484 * MaximumLength is odd as the native function does.
1486 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1487 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1488 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1490 if (src
->Length
!= 0) {
1491 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1493 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1494 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1495 dest
->Length
= dest_len
;
1496 /* append terminating '\0' if enough space */
1497 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1498 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1501 return STATUS_SUCCESS
;
1505 /**************************************************************************
1506 * RtlFindCharInUnicodeString (NTDLL.@)
1508 * Searches for one of several unicode characters in a unicode string.
1511 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1512 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1514 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1515 int flags
, /* [I] Flags */
1516 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1517 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1518 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1520 unsigned int main_idx
, search_idx
;
1524 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1525 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1526 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1527 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1528 return STATUS_SUCCESS
;
1533 return STATUS_NOT_FOUND
;
1535 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1536 while (main_idx
-- > 0) {
1537 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1538 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1539 *pos
= main_idx
* sizeof(WCHAR
);
1540 return STATUS_SUCCESS
;
1545 return STATUS_NOT_FOUND
;
1547 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1549 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1550 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1553 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1554 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1555 return STATUS_SUCCESS
;
1559 return STATUS_NOT_FOUND
;
1561 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1562 while (main_idx
-- > 0) {
1564 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1565 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1568 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1569 *pos
= main_idx
* sizeof(WCHAR
);
1570 return STATUS_SUCCESS
;
1574 return STATUS_NOT_FOUND
;
1576 return STATUS_NOT_FOUND
;
1584 /**************************************************************************
1585 * RtlIsTextUnicode (NTDLL.@)
1587 * Attempt to guess whether a text buffer is Unicode.
1590 * buf [I] Text buffer to test
1591 * len [I] Length of buf
1592 * pf [O] Destination for test results
1595 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1598 * Should implement more tests.
1600 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1602 static const WCHAR std_control_chars
[] = {'\r','\n','\t',' ',0x3000,0};
1603 static const WCHAR byterev_control_chars
[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1604 const WCHAR
*s
= buf
;
1606 unsigned int flags
= ~0U, out_flags
= 0;
1608 if (len
< sizeof(WCHAR
))
1610 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1617 * Apply various tests to the text string. According to the
1618 * docs, each test "passed" sets the corresponding flag in
1619 * the output flags. But some of the tests are mutually
1620 * exclusive, so I don't see how you could pass all tests ...
1623 /* Check for an odd length ... pass if even. */
1624 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1626 if (((const char *)buf
)[len
- 1] == 0)
1627 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1629 len
/= sizeof(WCHAR
);
1630 /* Windows only checks the first 256 characters */
1631 if (len
> 256) len
= 256;
1633 /* Check for the special byte order unicode marks. */
1634 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1635 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1637 /* apply some statistical analysis */
1638 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1641 /* FIXME: checks only for ASCII characters in the unicode stream */
1642 for (i
= 0; i
< len
; i
++)
1644 if (s
[i
] <= 255) stats
++;
1646 if (stats
> len
/ 2)
1647 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1650 /* Check for unicode NULL chars */
1651 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1653 for (i
= 0; i
< len
; i
++)
1655 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1657 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1663 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1665 for (i
= 0; i
< len
; i
++)
1667 if (strchrW(std_control_chars
, s
[i
]))
1669 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1675 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1677 for (i
= 0; i
< len
; i
++)
1679 if (strchrW(byterev_control_chars
, s
[i
]))
1681 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1692 /* check for flags that indicate it's definitely not valid Unicode */
1693 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1694 /* now check for invalid ASCII, and assume Unicode if so */
1695 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1696 /* now check for Unicode flags */
1697 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1703 /**************************************************************************
1704 * RtlCharToInteger (NTDLL.@)
1706 * Converts a character string into its integer equivalent.
1709 * Success: STATUS_SUCCESS. value contains the converted number
1710 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1711 * STATUS_ACCESS_VIOLATION, if value is NULL.
1714 * For base 0 it uses 10 as base and the string should be in the format
1715 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1716 * For other bases the string should be in the format
1717 * "{whitespace} [+|-] {digits}".
1718 * No check is made for value overflow, only the lower 32 bits are assigned.
1719 * If str is NULL it crashes, as the native function does.
1722 * This function does not read garbage behind '\0' as the native version does.
1724 NTSTATUS WINAPI
RtlCharToInteger(
1725 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1726 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1727 ULONG
*value
) /* [O] Destination for the converted value */
1731 ULONG RunningTotal
= 0;
1732 BOOL bMinus
= FALSE
;
1734 while (*str
!= '\0' && *str
<= ' ') {
1740 } else if (*str
== '-') {
1747 if (str
[0] == '0') {
1748 if (str
[1] == 'b') {
1751 } else if (str
[1] == 'o') {
1754 } else if (str
[1] == 'x') {
1759 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1760 return STATUS_INVALID_PARAMETER
;
1763 if (value
== NULL
) {
1764 return STATUS_ACCESS_VIOLATION
;
1767 while (*str
!= '\0') {
1769 if (chCurrent
>= '0' && chCurrent
<= '9') {
1770 digit
= chCurrent
- '0';
1771 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1772 digit
= chCurrent
- 'A' + 10;
1773 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1774 digit
= chCurrent
- 'a' + 10;
1778 if (digit
< 0 || digit
>= base
) {
1779 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1780 return STATUS_SUCCESS
;
1783 RunningTotal
= RunningTotal
* base
+ digit
;
1787 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1788 return STATUS_SUCCESS
;
1792 /**************************************************************************
1793 * RtlIntegerToChar (NTDLL.@)
1795 * Converts an unsigned integer to a character string.
1798 * Success: STATUS_SUCCESS. str contains the converted number
1799 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1800 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1801 * STATUS_ACCESS_VIOLATION, if str is NULL.
1804 * Instead of base 0 it uses 10 as base.
1805 * Writes at most length characters to the string str.
1806 * Str is '\0' terminated when length allows it.
1807 * When str fits exactly in length characters the '\0' is omitted.
1809 NTSTATUS WINAPI
RtlIntegerToChar(
1810 ULONG value
, /* [I] Value to be converted */
1811 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1812 ULONG length
, /* [I] Length of the str buffer in bytes */
1813 PCHAR str
) /* [O] Destination for the converted value */
1822 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1823 return STATUS_INVALID_PARAMETER
;
1831 digit
= value
% base
;
1832 value
= value
/ base
;
1836 *pos
= 'A' + digit
- 10;
1838 } while (value
!= 0L);
1840 len
= &buffer
[32] - pos
;
1842 return STATUS_BUFFER_OVERFLOW
;
1843 } else if (str
== NULL
) {
1844 return STATUS_ACCESS_VIOLATION
;
1845 } else if (len
== length
) {
1846 memcpy(str
, pos
, len
);
1848 memcpy(str
, pos
, len
+ 1);
1850 return STATUS_SUCCESS
;
1854 /**************************************************************************
1855 * RtlUnicodeStringToInteger (NTDLL.@)
1857 * Converts a unicode string into its integer equivalent.
1860 * Success: STATUS_SUCCESS. value contains the converted number
1861 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1862 * STATUS_ACCESS_VIOLATION, if value is NULL.
1865 * For base 0 it uses 10 as base and the string should be in the format
1866 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1867 * For other bases the string should be in the format
1868 * "{whitespace} [+|-] {digits}".
1869 * No check is made for value overflow, only the lower 32 bits are assigned.
1870 * If str is NULL it crashes, as the native function does.
1873 * This function does not read garbage on string length 0 as the native
1876 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1877 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1878 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1879 ULONG
*value
) /* [O] Destination for the converted value */
1881 LPWSTR lpwstr
= str
->Buffer
;
1882 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1885 ULONG RunningTotal
= 0;
1886 BOOL bMinus
= FALSE
;
1888 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1893 if (CharsRemaining
>= 1) {
1894 if (*lpwstr
== '+') {
1897 } else if (*lpwstr
== '-') {
1906 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1907 if (lpwstr
[1] == 'b') {
1909 CharsRemaining
-= 2;
1911 } else if (lpwstr
[1] == 'o') {
1913 CharsRemaining
-= 2;
1915 } else if (lpwstr
[1] == 'x') {
1917 CharsRemaining
-= 2;
1921 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1922 return STATUS_INVALID_PARAMETER
;
1925 if (value
== NULL
) {
1926 return STATUS_ACCESS_VIOLATION
;
1929 while (CharsRemaining
>= 1) {
1930 wchCurrent
= *lpwstr
;
1931 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1932 digit
= wchCurrent
- '0';
1933 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1934 digit
= wchCurrent
- 'A' + 10;
1935 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1936 digit
= wchCurrent
- 'a' + 10;
1940 if (digit
< 0 || digit
>= base
) {
1941 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1942 return STATUS_SUCCESS
;
1945 RunningTotal
= RunningTotal
* base
+ digit
;
1950 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1951 return STATUS_SUCCESS
;
1955 /**************************************************************************
1956 * RtlIntegerToUnicodeString (NTDLL.@)
1958 * Converts an unsigned integer to a '\0' terminated unicode string.
1961 * Success: STATUS_SUCCESS. str contains the converted number
1962 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1963 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1964 * (with the '\0' termination). In this case str->Length
1965 * is set to the length, the string would have (which can
1966 * be larger than the MaximumLength).
1969 * Instead of base 0 it uses 10 as base.
1970 * If str is NULL it crashes, as the native function does.
1973 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1974 * The native function does this when the string would be longer than 16
1975 * characters even when the string parameter is long enough.
1977 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1978 ULONG value
, /* [I] Value to be converted */
1979 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1980 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1988 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1989 return STATUS_INVALID_PARAMETER
;
1997 digit
= value
% base
;
1998 value
= value
/ base
;
2002 *pos
= 'A' + digit
- 10;
2004 } while (value
!= 0L);
2006 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
2007 if (str
->Length
>= str
->MaximumLength
) {
2008 return STATUS_BUFFER_OVERFLOW
;
2010 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
2012 return STATUS_SUCCESS
;
2016 /*************************************************************************
2017 * RtlGUIDFromString (NTDLL.@)
2019 * Convert a string representation of a GUID into a GUID.
2022 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
2023 * guid [O] Destination for the converted GUID
2026 * Success: STATUS_SUCCESS. guid contains the converted value.
2027 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
2030 * See RtlStringFromGUID.
2032 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
2035 const WCHAR
*lpszCLSID
= str
->Buffer
;
2036 BYTE
* lpOut
= (BYTE
*)guid
;
2038 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
2040 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2041 * to memory: DWORD... WORD WORD BYTES............
2048 if (*lpszCLSID
!= '{')
2049 return STATUS_INVALID_PARAMETER
;
2052 case 9: case 14: case 19: case 24:
2053 if (*lpszCLSID
!= '-')
2054 return STATUS_INVALID_PARAMETER
;
2058 if (*lpszCLSID
!= '}')
2059 return STATUS_INVALID_PARAMETER
;
2064 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
2067 /* Read two hex digits as a byte value */
2068 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
2069 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
2070 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
2071 else return STATUS_INVALID_PARAMETER
;
2073 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
2074 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
2075 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
2076 else return STATUS_INVALID_PARAMETER
;
2078 byte
= ch
<< 4 | ch2
;
2082 #ifndef WORDS_BIGENDIAN
2083 /* For Big Endian machines, we store the data such that the
2084 * dword/word members can be read as DWORDS and WORDS correctly. */
2086 case 1: lpOut
[3] = byte
; break;
2087 case 3: lpOut
[2] = byte
; break;
2088 case 5: lpOut
[1] = byte
; break;
2089 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
2091 case 10: case 15: lpOut
[1] = byte
; break;
2092 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
2095 default: lpOut
[0] = byte
; lpOut
++; break;
2097 lpszCLSID
++; /* Skip 2nd character of byte */
2105 return STATUS_SUCCESS
;
2108 /*************************************************************************
2109 * RtlStringFromGUID (NTDLL.@)
2111 * Convert a GUID into a string representation of a GUID.
2114 * guid [I] GUID to convert
2115 * str [O] Destination for the converted string
2118 * Success: STATUS_SUCCESS. str contains the converted value.
2119 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2122 * See RtlGUIDFromString.
2124 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2126 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2127 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2128 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2129 '%','0','2','X','%','0','2','X','}','\0' };
2131 TRACE("(%p,%p)\n", guid
, str
);
2133 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
2134 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
2135 str
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
2138 str
->Length
= str
->MaximumLength
= 0;
2139 return STATUS_NO_MEMORY
;
2141 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2142 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2143 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2145 return STATUS_SUCCESS
;
2148 /******************************************************************************
2149 * RtlHashUnicodeString [NTDLL.@]
2151 NTSTATUS WINAPI
RtlHashUnicodeString(PCUNICODE_STRING string
, BOOLEAN case_insensitive
, ULONG alg
, ULONG
*hash
)
2155 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
2159 case HASH_STRING_ALGORITHM_DEFAULT
:
2160 case HASH_STRING_ALGORITHM_X65599
:
2163 return STATUS_INVALID_PARAMETER
;
2167 for (i
= 0; i
< string
->Length
/sizeof(WCHAR
); i
++)
2168 *hash
= *hash
*65599 + (case_insensitive
? toupperW(string
->Buffer
[i
]) : string
->Buffer
[i
]);
2170 return STATUS_SUCCESS
;