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 extern const union cptable cptable_20127
; /* 7-bit ASCII */
49 static const union cptable
*ansi_table
= &cptable_20127
;
50 static const union cptable
*oem_table
= &cptable_20127
;
51 static const union cptable
* unix_table
; /* NULL if UTF8 */
54 /**************************************************************************
55 * __wine_init_codepages (NTDLL.@)
57 * Set the code page once kernel32 is loaded. Should be done differently.
59 void CDECL
__wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
60 const union cptable
*ucp
)
65 NlsAnsiCodePage
= ansi
->info
.codepage
;
68 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
71 /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
72 if (!unix_table
) flags
|= MB_COMPOSITE
;
75 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
76 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
79 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
80 const char* defchar
, int *used
)
83 return wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
);
84 if (used
) *used
= 0; /* all chars are valid for UTF-8 */
85 return wine_utf8_wcstombs( flags
, src
, srclen
, dst
, dstlen
);
88 /**************************************************************************
89 * RtlInitAnsiString (NTDLL.@)
91 * Initializes a buffered ansi string.
97 * Assigns source to target->Buffer. The length of source is assigned to
98 * target->Length and target->MaximumLength. If source is NULL the length
99 * of source is assumed to be 0.
101 void WINAPI
RtlInitAnsiString(
102 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
103 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
105 if ((target
->Buffer
= (PCHAR
) source
))
107 target
->Length
= strlen(source
);
108 target
->MaximumLength
= target
->Length
+ 1;
110 else target
->Length
= target
->MaximumLength
= 0;
113 /**************************************************************************
114 * RtlInitAnsiStringEx (NTDLL.@)
116 * Initializes a buffered ansi string.
119 * An appropriate NTSTATUS value.
122 * Assigns source to target->Buffer. The length of source is assigned to
123 * target->Length and target->MaximumLength. If source is NULL the length
124 * of source is assumed to be 0.
126 NTSTATUS WINAPI
RtlInitAnsiStringEx(PANSI_STRING target
, PCSZ source
)
130 unsigned int len
= strlen(source
);
132 return STATUS_NAME_TOO_LONG
;
134 target
->Buffer
= (PCHAR
) source
;
135 target
->Length
= len
;
136 target
->MaximumLength
= len
+ 1;
140 target
->Buffer
= NULL
;
142 target
->MaximumLength
= 0;
144 return STATUS_SUCCESS
;
147 /**************************************************************************
148 * RtlInitString (NTDLL.@)
150 * Initializes a buffered string.
156 * Assigns source to target->Buffer. The length of source is assigned to
157 * target->Length and target->MaximumLength. If source is NULL the length
158 * of source is assumed to be 0.
160 void WINAPI
RtlInitString(
161 PSTRING target
, /* [I/O] Buffered string to be initialized */
162 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
164 RtlInitAnsiString( target
, source
);
168 /**************************************************************************
169 * RtlFreeAnsiString (NTDLL.@)
171 void WINAPI
RtlFreeAnsiString( PSTRING str
)
175 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
176 RtlZeroMemory( str
, sizeof(*str
) );
181 /**************************************************************************
182 * RtlFreeOemString (NTDLL.@)
184 void WINAPI
RtlFreeOemString( PSTRING str
)
186 RtlFreeAnsiString( str
);
190 /**************************************************************************
191 * RtlCopyString (NTDLL.@)
193 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
197 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
198 memcpy( dst
->Buffer
, src
->Buffer
, len
);
201 else dst
->Length
= 0;
205 /**************************************************************************
206 * RtlInitUnicodeString (NTDLL.@)
208 * Initializes a buffered unicode string.
214 * Assigns source to target->Buffer. The length of source is assigned to
215 * target->Length and target->MaximumLength. If source is NULL the length
216 * of source is assumed to be 0.
218 void WINAPI
RtlInitUnicodeString(
219 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
220 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
222 if ((target
->Buffer
= (PWSTR
) source
))
224 unsigned int length
= strlenW(source
) * sizeof(WCHAR
);
227 target
->Length
= length
;
228 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
230 else target
->Length
= target
->MaximumLength
= 0;
234 /**************************************************************************
235 * RtlInitUnicodeStringEx (NTDLL.@)
237 * Initializes a buffered unicode string.
240 * Success: STATUS_SUCCESS. target is initialized.
241 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
244 * Assigns source to target->Buffer. The length of source is assigned to
245 * target->Length and target->MaximumLength. If source is NULL the length
246 * of source is assumed to be 0.
248 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
249 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
250 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
252 if (source
!= NULL
) {
253 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
256 return STATUS_NAME_TOO_LONG
;
258 target
->Length
= len
;
259 target
->MaximumLength
= len
+ sizeof(WCHAR
);
260 target
->Buffer
= (PWSTR
) source
;
264 target
->MaximumLength
= 0;
265 target
->Buffer
= NULL
;
267 return STATUS_SUCCESS
;
271 /**************************************************************************
272 * RtlCreateUnicodeString (NTDLL.@)
274 * Creates a UNICODE_STRING from a null-terminated Unicode string.
280 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
282 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
283 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
284 memcpy( target
->Buffer
, src
, len
);
285 target
->MaximumLength
= len
;
286 target
->Length
= len
- sizeof(WCHAR
);
291 /**************************************************************************
292 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
294 * Creates a UNICODE_STRING from a null-terminated Ascii string.
300 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
303 RtlInitAnsiString( &ansi
, src
);
304 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
308 /**************************************************************************
309 * RtlFreeUnicodeString (NTDLL.@)
311 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
312 * RtlCreateUnicodeStringFromAsciiz().
317 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
321 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
322 RtlZeroMemory( str
, sizeof(*str
) );
327 /**************************************************************************
328 * RtlCopyUnicodeString (NTDLL.@)
330 * Copies from one UNICODE_STRING to another.
335 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
339 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
340 memcpy( dst
->Buffer
, src
->Buffer
, len
);
342 /* append terminating '\0' if enough space */
343 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
345 else dst
->Length
= 0;
349 /**************************************************************************
350 * RtlDuplicateUnicodeString (NTDLL.@)
352 * Duplicates a unicode string.
355 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
356 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
357 * STATUS_NO_MEMORY, if the allocation fails.
360 * For add_nul there are several possible values:
361 * 0 = destination will not be '\0' terminated,
362 * 1 = destination will be '\0' terminated,
363 * 3 = like 1 but for an empty source string produce '\0' terminated empty
364 * Buffer instead of assigning NULL to the Buffer.
365 * Other add_nul values are invalid.
367 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
368 int add_nul
, /* [I] flag */
369 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
370 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
372 if (source
== NULL
|| destination
== NULL
||
373 source
->Length
> source
->MaximumLength
||
374 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
375 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
376 return STATUS_INVALID_PARAMETER
;
378 if (source
->Length
== 0 && add_nul
!= 3) {
379 destination
->Length
= 0;
380 destination
->MaximumLength
= 0;
381 destination
->Buffer
= NULL
;
383 unsigned int destination_max_len
= source
->Length
;
386 destination_max_len
+= sizeof(WCHAR
);
388 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
389 if (destination
->Buffer
== NULL
) {
390 return STATUS_NO_MEMORY
;
392 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
393 destination
->Length
= source
->Length
;
394 destination
->MaximumLength
= source
->Length
;
395 /* append terminating '\0' if enough space */
397 destination
->MaximumLength
= destination_max_len
;
398 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
403 return STATUS_SUCCESS
;
407 /**************************************************************************
408 * RtlEraseUnicodeString (NTDLL.@)
410 * Overwrites a UNICODE_STRING with zeros.
415 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
419 memset( str
->Buffer
, 0, str
->MaximumLength
);
430 /******************************************************************************
431 * RtlCompareString (NTDLL.@)
433 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
439 len
= min(s1
->Length
, s2
->Length
);
445 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
449 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
451 if (!ret
) ret
= s1
->Length
- s2
->Length
;
456 /******************************************************************************
457 * RtlCompareUnicodeStrings (NTDLL.@)
459 LONG WINAPI
RtlCompareUnicodeStrings( const WCHAR
*s1
, SIZE_T len1
, const WCHAR
*s2
, SIZE_T len2
,
460 BOOLEAN case_insensitive
)
463 SIZE_T len
= min( len1
, len2
);
465 if (case_insensitive
)
467 while (!ret
&& len
--) ret
= toupperW(*s1
++) - toupperW(*s2
++);
471 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
473 if (!ret
) ret
= len1
- len2
;
478 /******************************************************************************
479 * RtlCompareUnicodeString (NTDLL.@)
481 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
482 BOOLEAN CaseInsensitive
)
484 return RtlCompareUnicodeStrings( s1
->Buffer
, s1
->Length
/ sizeof(WCHAR
),
485 s2
->Buffer
, s2
->Length
/ sizeof(WCHAR
), CaseInsensitive
);
489 /**************************************************************************
490 * RtlEqualString (NTDLL.@)
492 * Determine if two strings are equal.
495 * s1 [I] Source string
496 * s2 [I] String to compare to s1
497 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
500 * Non-zero if s1 is equal to s2, 0 otherwise.
502 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
504 if (s1
->Length
!= s2
->Length
) return FALSE
;
505 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
509 /**************************************************************************
510 * RtlEqualUnicodeString (NTDLL.@)
512 * Unicode version of RtlEqualString.
514 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
515 BOOLEAN CaseInsensitive
)
517 if (s1
->Length
!= s2
->Length
) return FALSE
;
518 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
522 /**************************************************************************
523 * RtlPrefixString (NTDLL.@)
525 * Determine if one string is a prefix of another.
528 * s1 [I] Prefix to look for in s2
529 * s2 [I] String that may contain s1 as a prefix
530 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
533 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
535 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
539 if (s1
->Length
> s2
->Length
) return FALSE
;
542 for (i
= 0; i
< s1
->Length
; i
++)
543 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
547 for (i
= 0; i
< s1
->Length
; i
++)
548 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
554 /**************************************************************************
555 * RtlPrefixUnicodeString (NTDLL.@)
557 * Unicode version of RtlPrefixString.
559 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
560 const UNICODE_STRING
*s2
,
561 BOOLEAN ignore_case
)
565 if (s1
->Length
> s2
->Length
) return FALSE
;
568 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
569 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
573 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
574 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
580 /**************************************************************************
581 * RtlEqualComputerName (NTDLL.@)
583 * Determine if two computer names are the same.
586 * left [I] First computer name
587 * right [I] Second computer name
590 * 0 if the names are equal, non-zero otherwise.
593 * The comparison is case insensitive.
595 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
596 const UNICODE_STRING
*right
)
599 STRING upLeft
, upRight
;
601 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
603 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
605 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
606 RtlFreeOemString( &upRight
);
608 RtlFreeOemString( &upLeft
);
614 /**************************************************************************
615 * RtlEqualDomainName (NTDLL.@)
617 * Determine if two domain names are the same.
620 * left [I] First domain name
621 * right [I] Second domain name
624 * 0 if the names are equal, non-zero otherwise.
627 * The comparison is case insensitive.
629 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
630 const UNICODE_STRING
*right
)
632 return RtlEqualComputerName(left
, right
);
636 /**************************************************************************
637 * RtlAnsiCharToUnicodeChar (NTDLL.@)
639 * Converts the first ansi character to a unicode character.
642 * ansi [I/O] Pointer to the ansi string.
645 * Unicode representation of the first character in the ansi string.
648 * Upon successful completion, the char pointer ansi points to is
649 * incremented by the size of the character.
651 WCHAR WINAPI
RtlAnsiCharToUnicodeChar(LPSTR
*ansi
)
654 DWORD charSize
= sizeof(CHAR
);
656 if (wine_is_dbcs_leadbyte(ansi_table
, **ansi
))
659 RtlMultiByteToUnicodeN(&str
, sizeof(WCHAR
), NULL
, *ansi
, charSize
);
666 COPY BETWEEN ANSI_STRING or UNICODE_STRING
667 there is no parameter checking, it just crashes
671 /**************************************************************************
672 * RtlAnsiStringToUnicodeString (NTDLL.@)
674 * Converts an ansi string to a unicode string.
677 * Success: STATUS_SUCCESS. uni contains the converted string
678 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
679 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
680 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
683 * This function always writes a terminating '\0'.
685 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
686 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
687 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
688 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
690 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
692 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
693 uni
->Length
= total
- sizeof(WCHAR
);
696 uni
->MaximumLength
= total
;
697 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
698 return STATUS_NO_MEMORY
;
700 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
702 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
703 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
704 return STATUS_SUCCESS
;
708 /**************************************************************************
709 * RtlOemStringToUnicodeString (NTDLL.@)
711 * Converts an oem string to a unicode string.
714 * Success: STATUS_SUCCESS. uni contains the converted string
715 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
716 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
717 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
720 * This function always writes a terminating '\0'.
722 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
723 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
724 const STRING
*oem
, /* [I] Oem string to be converted */
725 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
727 DWORD total
= RtlOemStringToUnicodeSize( oem
);
729 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
730 uni
->Length
= total
- sizeof(WCHAR
);
733 uni
->MaximumLength
= total
;
734 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
735 return STATUS_NO_MEMORY
;
737 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
739 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
740 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
741 return STATUS_SUCCESS
;
745 /**************************************************************************
746 * RtlUnicodeStringToAnsiString (NTDLL.@)
748 * Converts a unicode string to an ansi string.
751 * Success: STATUS_SUCCESS. ansi contains the converted string
752 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
753 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
756 * This function always writes a terminating '\0'.
757 * It performs a partial copy if ansi is too small.
759 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
760 STRING
*ansi
, /* [I/O] Destination for the ansi string */
761 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
762 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
764 NTSTATUS ret
= STATUS_SUCCESS
;
765 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
767 ansi
->Length
= len
- 1;
770 ansi
->MaximumLength
= len
;
771 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
772 return STATUS_NO_MEMORY
;
774 else if (ansi
->MaximumLength
< len
)
776 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
777 ansi
->Length
= ansi
->MaximumLength
- 1;
778 ret
= STATUS_BUFFER_OVERFLOW
;
781 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
782 ansi
->Buffer
[ansi
->Length
] = 0;
787 /**************************************************************************
788 * RtlUnicodeStringToOemString (NTDLL.@)
790 * Converts a Rtl Unicode string to an OEM string.
793 * oem [O] Destination for OEM string
794 * uni [I] Source Unicode string
795 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
798 * Success: STATUS_SUCCESS. oem contains the converted string
799 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
800 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
803 * If doalloc is TRUE, the length allocated is uni->Length + 1.
804 * This function always '\0' terminates the string returned.
806 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
807 const UNICODE_STRING
*uni
,
810 NTSTATUS ret
= STATUS_SUCCESS
;
811 DWORD len
= RtlUnicodeStringToOemSize( uni
);
813 oem
->Length
= len
- 1;
816 oem
->MaximumLength
= len
;
817 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
818 return STATUS_NO_MEMORY
;
820 else if (oem
->MaximumLength
< len
)
822 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
823 oem
->Length
= oem
->MaximumLength
- 1;
824 ret
= STATUS_BUFFER_OVERFLOW
;
827 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
828 oem
->Buffer
[oem
->Length
] = 0;
833 /**************************************************************************
834 * RtlMultiByteToUnicodeN (NTDLL.@)
836 * Converts a multi-byte string to a Unicode string.
842 * Performs a partial copy if dst is too small.
844 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
845 LPCSTR src
, DWORD srclen
)
848 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
850 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
851 return STATUS_SUCCESS
;
855 /**************************************************************************
856 * RtlOemToUnicodeN (NTDLL.@)
858 * Converts a multi-byte string in the OEM code page to a Unicode string.
863 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
864 LPCSTR src
, DWORD srclen
)
866 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
868 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
869 return STATUS_SUCCESS
;
873 /**************************************************************************
874 * RtlUnicodeToMultiByteN (NTDLL.@)
876 * Converts a Unicode string to a multi-byte string in the ANSI code page.
881 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
882 LPCWSTR src
, DWORD srclen
)
884 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
885 dst
, dstlen
, NULL
, NULL
);
887 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
888 return STATUS_SUCCESS
;
892 /**************************************************************************
893 * RtlUnicodeToOemN (NTDLL.@)
895 * Converts a Unicode string to a multi-byte string in the OEM code page.
900 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
901 LPCWSTR src
, DWORD srclen
)
903 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
904 dst
, dstlen
, NULL
, NULL
);
906 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
907 return STATUS_SUCCESS
;
916 /**************************************************************************
917 * RtlUpperChar (NTDLL.@)
919 * Converts an Ascii character to uppercase.
922 * ch [I] Character to convert
925 * The uppercase character value.
928 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
929 * All other input characters are returned unchanged. The locale and
930 * multibyte characters are not taken into account (as native DLL).
932 CHAR WINAPI
RtlUpperChar( CHAR ch
)
934 if (ch
>= 'a' && ch
<= 'z') {
935 return ch
- 'a' + 'A';
942 /**************************************************************************
943 * RtlUpperString (NTDLL.@)
945 * Converts an Ascii string to uppercase.
948 * dst [O] Destination for converted string
949 * src [I] Source string to convert
955 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
956 * All other src characters are copied unchanged to dst. The locale and
957 * multibyte characters are not taken into account (as native DLL).
958 * The number of character copied is the minimum of src->Length and
959 * the dst->MaximumLength.
961 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
963 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
965 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
970 /**************************************************************************
971 * RtlUpcaseUnicodeChar (NTDLL.@)
973 * Converts a Unicode character to uppercase.
976 * wch [I] Character to convert
979 * The uppercase character value.
981 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
983 return toupperW(wch
);
987 /**************************************************************************
988 * RtlDowncaseUnicodeChar (NTDLL.@)
990 * Converts a Unicode character to lowercase.
993 * wch [I] Character to convert
996 * The lowercase character value.
998 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
1000 return tolowerW(wch
);
1004 /**************************************************************************
1005 * RtlUpcaseUnicodeString (NTDLL.@)
1007 * Converts a Unicode string to uppercase.
1010 * dest [O] Destination for converted string
1011 * src [I] Source string to convert
1012 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1015 * Success: STATUS_SUCCESS. dest contains the converted string.
1016 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1017 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1020 * dest is never '\0' terminated because it may be equal to src, and src
1021 * might not be '\0' terminated. dest->Length is only set upon success.
1023 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
1024 const UNICODE_STRING
*src
,
1027 DWORD i
, len
= src
->Length
;
1031 dest
->MaximumLength
= len
;
1032 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1033 return STATUS_NO_MEMORY
;
1035 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1037 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
1039 return STATUS_SUCCESS
;
1043 /**************************************************************************
1044 * RtlDowncaseUnicodeString (NTDLL.@)
1046 * Converts a Unicode string to lowercase.
1049 * dest [O] Destination for converted string
1050 * src [I] Source string to convert
1051 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1054 * Success: STATUS_SUCCESS. dest contains the converted string.
1055 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1056 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1059 * dest is never '\0' terminated because it may be equal to src, and src
1060 * might not be '\0' terminated. dest->Length is only set upon success.
1062 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
1063 UNICODE_STRING
*dest
,
1064 const UNICODE_STRING
*src
,
1068 DWORD len
= src
->Length
;
1071 dest
->MaximumLength
= len
;
1072 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
1073 return STATUS_NO_MEMORY
;
1075 } else if (len
> dest
->MaximumLength
) {
1076 return STATUS_BUFFER_OVERFLOW
;
1079 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
1080 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
1083 return STATUS_SUCCESS
;
1087 /**************************************************************************
1088 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1090 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1096 * writes terminating 0
1098 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1099 const UNICODE_STRING
*src
,
1103 UNICODE_STRING upcase
;
1105 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1107 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1108 RtlFreeUnicodeString( &upcase
);
1114 /**************************************************************************
1115 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1117 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1118 * stored in STRING format.
1124 * writes terminating 0
1126 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1127 const UNICODE_STRING
*src
,
1131 UNICODE_STRING upcase
;
1133 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1135 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1136 RtlFreeUnicodeString( &upcase
);
1142 /**************************************************************************
1143 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1145 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1146 * stored in STRING format.
1152 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1154 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1155 const UNICODE_STRING
*uni
,
1159 UNICODE_STRING upcase
;
1162 upcase
.Buffer
= tmp
;
1163 upcase
.MaximumLength
= sizeof(tmp
);
1164 ret
= RtlUpcaseUnicodeString( &upcase
, uni
, FALSE
);
1165 if (ret
== STATUS_BUFFER_OVERFLOW
) ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
);
1169 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1173 oem
->MaximumLength
= len
;
1174 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1176 ret
= STATUS_NO_MEMORY
;
1180 else if (oem
->MaximumLength
< len
)
1182 ret
= STATUS_BUFFER_OVERFLOW
;
1183 oem
->Length
= oem
->MaximumLength
;
1184 if (!oem
->MaximumLength
) goto done
;
1186 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1188 if (upcase
.Buffer
!= tmp
) RtlFreeUnicodeString( &upcase
);
1194 /**************************************************************************
1195 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1197 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1202 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1203 LPCWSTR src
, DWORD srclen
)
1209 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1210 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1211 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1212 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1217 /**************************************************************************
1218 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1220 * Converts a Unicode string to the equivalent OEM upper-case representation.
1225 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1226 LPCWSTR src
, DWORD srclen
)
1232 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1233 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1234 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1235 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1245 /**************************************************************************
1246 * RtlOemStringToUnicodeSize (NTDLL.@)
1247 * RtlxOemStringToUnicodeSize (NTDLL.@)
1249 * Calculate the size in bytes necessary for the Unicode conversion of str,
1250 * including the terminating '\0'.
1253 * str [I] String to calculate the size of
1256 * The calculated size.
1258 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1260 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1261 return (ret
+ 1) * sizeof(WCHAR
);
1265 /**************************************************************************
1266 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1267 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1269 * Calculate the size in bytes necessary for the Unicode conversion of str,
1270 * including the terminating '\0'.
1273 * str [I] String to calculate the size of
1276 * The calculated size.
1278 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1281 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1282 return ret
+ sizeof(WCHAR
);
1286 /**************************************************************************
1287 * RtlMultiByteToUnicodeSize (NTDLL.@)
1289 * Compute the size in bytes necessary for the Unicode conversion of str,
1290 * without the terminating '\0'.
1293 * size [O] Destination for size
1294 * str [I] String to calculate the size of
1295 * len [I] Length of str
1300 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1302 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1303 return STATUS_SUCCESS
;
1307 /**************************************************************************
1308 * RtlUnicodeToMultiByteSize (NTDLL.@)
1310 * Calculate the size in bytes necessary for the multibyte conversion of str,
1311 * without the terminating '\0'.
1314 * size [O] Destination for size
1315 * str [I] String to calculate the size of
1316 * len [I] Length of str
1321 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1323 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1324 return STATUS_SUCCESS
;
1328 /**************************************************************************
1329 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1330 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1332 * Calculate the size in bytes necessary for the Ansi conversion of str,
1333 * including the terminating '\0'.
1336 * str [I] String to calculate the size of
1339 * The calculated size.
1341 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1344 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1349 /**************************************************************************
1350 * RtlUnicodeStringToOemSize (NTDLL.@)
1351 * RtlxUnicodeStringToOemSize (NTDLL.@)
1353 * Calculate the size in bytes necessary for the OEM conversion of str,
1354 * including the terminating '\0'.
1357 * str [I] String to calculate the size of
1360 * The calculated size.
1362 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1364 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1365 NULL
, 0, NULL
, NULL
) + 1;
1369 /**************************************************************************
1370 * RtlAppendAsciizToString (NTDLL.@)
1372 * Concatenates a buffered character string and a '\0' terminated character
1376 * Success: STATUS_SUCCESS. src is appended to dest.
1377 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1378 * to hold the concatenated string.
1381 * if src is NULL dest is unchanged.
1382 * dest is never '\0' terminated.
1384 NTSTATUS WINAPI
RtlAppendAsciizToString(
1385 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1386 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1389 unsigned int src_len
= strlen(src
);
1390 unsigned int dest_len
= src_len
+ dest
->Length
;
1392 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1393 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1394 dest
->Length
= dest_len
;
1396 return STATUS_SUCCESS
;
1400 /**************************************************************************
1401 * RtlAppendStringToString (NTDLL.@)
1403 * Concatenates two buffered character strings
1406 * Success: STATUS_SUCCESS. src is appended to dest.
1407 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1408 * to hold the concatenated string.
1411 * if src->length is zero dest is unchanged.
1412 * dest is never '\0' terminated.
1414 NTSTATUS WINAPI
RtlAppendStringToString(
1415 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1416 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1418 if (src
->Length
!= 0) {
1419 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1421 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1422 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1423 dest
->Length
= dest_len
;
1425 return STATUS_SUCCESS
;
1429 /**************************************************************************
1430 * RtlAppendUnicodeToString (NTDLL.@)
1432 * Concatenates a buffered unicode string and a '\0' terminated unicode
1436 * Success: STATUS_SUCCESS. src is appended to dest.
1437 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1438 * to hold the concatenated string.
1441 * if src is NULL dest is unchanged.
1442 * dest is '\0' terminated when the MaximumLength allows it.
1443 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1446 * Does not write in the src->Buffer beyond MaximumLength when
1447 * MaximumLength is odd as the native function does.
1449 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1450 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1451 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1454 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1455 unsigned int dest_len
= src_len
+ dest
->Length
;
1457 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1458 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1459 dest
->Length
= dest_len
;
1460 /* append terminating '\0' if enough space */
1461 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1462 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1465 return STATUS_SUCCESS
;
1469 /**************************************************************************
1470 * RtlAppendUnicodeStringToString (NTDLL.@)
1472 * Concatenates two buffered unicode strings
1475 * Success: STATUS_SUCCESS. src is appended to dest.
1476 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1477 * to hold the concatenated string.
1480 * if src->length is zero dest is unchanged.
1481 * dest is '\0' terminated when the MaximumLength allows it.
1482 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1485 * Does not write in the src->Buffer beyond MaximumLength when
1486 * MaximumLength is odd as the native function does.
1488 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1489 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1490 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1492 if (src
->Length
!= 0) {
1493 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1495 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1496 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1497 dest
->Length
= dest_len
;
1498 /* append terminating '\0' if enough space */
1499 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1500 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1503 return STATUS_SUCCESS
;
1507 /**************************************************************************
1508 * RtlFindCharInUnicodeString (NTDLL.@)
1510 * Searches for one of several unicode characters in a unicode string.
1513 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1514 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1516 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1517 int flags
, /* [I] Flags */
1518 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1519 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1520 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1522 unsigned int main_idx
, search_idx
;
1526 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1527 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1528 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1529 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1530 return STATUS_SUCCESS
;
1535 return STATUS_NOT_FOUND
;
1537 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1538 while (main_idx
-- > 0) {
1539 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1540 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1541 *pos
= main_idx
* sizeof(WCHAR
);
1542 return STATUS_SUCCESS
;
1547 return STATUS_NOT_FOUND
;
1549 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1551 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1552 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1555 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1556 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1557 return STATUS_SUCCESS
;
1561 return STATUS_NOT_FOUND
;
1563 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1564 while (main_idx
-- > 0) {
1566 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1567 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1570 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1571 *pos
= main_idx
* sizeof(WCHAR
);
1572 return STATUS_SUCCESS
;
1576 return STATUS_NOT_FOUND
;
1578 return STATUS_NOT_FOUND
;
1586 /**************************************************************************
1587 * RtlIsTextUnicode (NTDLL.@)
1589 * Attempt to guess whether a text buffer is Unicode.
1592 * buf [I] Text buffer to test
1593 * len [I] Length of buf
1594 * pf [O] Destination for test results
1597 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1600 * Should implement more tests.
1602 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1604 static const WCHAR std_control_chars
[] = {'\r','\n','\t',' ',0x3000,0};
1605 static const WCHAR byterev_control_chars
[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1606 const WCHAR
*s
= buf
;
1608 unsigned int flags
= ~0U, out_flags
= 0;
1610 if (len
< sizeof(WCHAR
))
1612 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1619 * Apply various tests to the text string. According to the
1620 * docs, each test "passed" sets the corresponding flag in
1621 * the output flags. But some of the tests are mutually
1622 * exclusive, so I don't see how you could pass all tests ...
1625 /* Check for an odd length ... pass if even. */
1626 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1628 if (((const char *)buf
)[len
- 1] == 0)
1629 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1631 len
/= sizeof(WCHAR
);
1632 /* Windows only checks the first 256 characters */
1633 if (len
> 256) len
= 256;
1635 /* Check for the special byte order unicode marks. */
1636 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1637 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1639 /* apply some statistical analysis */
1640 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1643 /* FIXME: checks only for ASCII characters in the unicode stream */
1644 for (i
= 0; i
< len
; i
++)
1646 if (s
[i
] <= 255) stats
++;
1648 if (stats
> len
/ 2)
1649 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1652 /* Check for unicode NULL chars */
1653 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1655 for (i
= 0; i
< len
; i
++)
1657 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1659 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1665 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1667 for (i
= 0; i
< len
; i
++)
1669 if (strchrW(std_control_chars
, s
[i
]))
1671 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1677 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1679 for (i
= 0; i
< len
; i
++)
1681 if (strchrW(byterev_control_chars
, s
[i
]))
1683 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1694 /* check for flags that indicate it's definitely not valid Unicode */
1695 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1696 /* now check for invalid ASCII, and assume Unicode if so */
1697 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1698 /* now check for Unicode flags */
1699 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1705 /**************************************************************************
1706 * RtlCharToInteger (NTDLL.@)
1708 * Converts a character string into its integer equivalent.
1711 * Success: STATUS_SUCCESS. value contains the converted number
1712 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1713 * STATUS_ACCESS_VIOLATION, if value is NULL.
1716 * For base 0 it uses 10 as base and the string should be in the format
1717 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1718 * For other bases the string should be in the format
1719 * "{whitespace} [+|-] {digits}".
1720 * No check is made for value overflow, only the lower 32 bits are assigned.
1721 * If str is NULL it crashes, as the native function does.
1724 * This function does not read garbage behind '\0' as the native version does.
1726 NTSTATUS WINAPI
RtlCharToInteger(
1727 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1728 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1729 ULONG
*value
) /* [O] Destination for the converted value */
1733 ULONG RunningTotal
= 0;
1734 BOOL bMinus
= FALSE
;
1736 while (*str
!= '\0' && *str
<= ' ') {
1742 } else if (*str
== '-') {
1749 if (str
[0] == '0') {
1750 if (str
[1] == 'b') {
1753 } else if (str
[1] == 'o') {
1756 } else if (str
[1] == 'x') {
1761 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1762 return STATUS_INVALID_PARAMETER
;
1765 if (value
== NULL
) {
1766 return STATUS_ACCESS_VIOLATION
;
1769 while (*str
!= '\0') {
1771 if (chCurrent
>= '0' && chCurrent
<= '9') {
1772 digit
= chCurrent
- '0';
1773 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1774 digit
= chCurrent
- 'A' + 10;
1775 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1776 digit
= chCurrent
- 'a' + 10;
1780 if (digit
< 0 || digit
>= base
) {
1781 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1782 return STATUS_SUCCESS
;
1785 RunningTotal
= RunningTotal
* base
+ digit
;
1789 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1790 return STATUS_SUCCESS
;
1794 /**************************************************************************
1795 * RtlIntegerToChar (NTDLL.@)
1797 * Converts an unsigned integer to a character string.
1800 * Success: STATUS_SUCCESS. str contains the converted number
1801 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1802 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1803 * STATUS_ACCESS_VIOLATION, if str is NULL.
1806 * Instead of base 0 it uses 10 as base.
1807 * Writes at most length characters to the string str.
1808 * Str is '\0' terminated when length allows it.
1809 * When str fits exactly in length characters the '\0' is omitted.
1811 NTSTATUS WINAPI
RtlIntegerToChar(
1812 ULONG value
, /* [I] Value to be converted */
1813 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1814 ULONG length
, /* [I] Length of the str buffer in bytes */
1815 PCHAR str
) /* [O] Destination for the converted value */
1824 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1825 return STATUS_INVALID_PARAMETER
;
1833 digit
= value
% base
;
1834 value
= value
/ base
;
1838 *pos
= 'A' + digit
- 10;
1840 } while (value
!= 0L);
1842 len
= &buffer
[32] - pos
;
1844 return STATUS_BUFFER_OVERFLOW
;
1845 } else if (str
== NULL
) {
1846 return STATUS_ACCESS_VIOLATION
;
1847 } else if (len
== length
) {
1848 memcpy(str
, pos
, len
);
1850 memcpy(str
, pos
, len
+ 1);
1852 return STATUS_SUCCESS
;
1856 /**************************************************************************
1857 * RtlUnicodeStringToInteger (NTDLL.@)
1859 * Converts a unicode string into its integer equivalent.
1862 * Success: STATUS_SUCCESS. value contains the converted number
1863 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1864 * STATUS_ACCESS_VIOLATION, if value is NULL.
1867 * For base 0 it uses 10 as base and the string should be in the format
1868 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1869 * For other bases the string should be in the format
1870 * "{whitespace} [+|-] {digits}".
1871 * No check is made for value overflow, only the lower 32 bits are assigned.
1872 * If str is NULL it crashes, as the native function does.
1875 * This function does not read garbage on string length 0 as the native
1878 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1879 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1880 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1881 ULONG
*value
) /* [O] Destination for the converted value */
1883 LPWSTR lpwstr
= str
->Buffer
;
1884 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1887 ULONG RunningTotal
= 0;
1888 BOOL bMinus
= FALSE
;
1890 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1895 if (CharsRemaining
>= 1) {
1896 if (*lpwstr
== '+') {
1899 } else if (*lpwstr
== '-') {
1908 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1909 if (lpwstr
[1] == 'b') {
1911 CharsRemaining
-= 2;
1913 } else if (lpwstr
[1] == 'o') {
1915 CharsRemaining
-= 2;
1917 } else if (lpwstr
[1] == 'x') {
1919 CharsRemaining
-= 2;
1923 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1924 return STATUS_INVALID_PARAMETER
;
1927 if (value
== NULL
) {
1928 return STATUS_ACCESS_VIOLATION
;
1931 while (CharsRemaining
>= 1) {
1932 wchCurrent
= *lpwstr
;
1933 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1934 digit
= wchCurrent
- '0';
1935 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1936 digit
= wchCurrent
- 'A' + 10;
1937 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1938 digit
= wchCurrent
- 'a' + 10;
1942 if (digit
< 0 || digit
>= base
) {
1943 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1944 return STATUS_SUCCESS
;
1947 RunningTotal
= RunningTotal
* base
+ digit
;
1952 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1953 return STATUS_SUCCESS
;
1957 /**************************************************************************
1958 * RtlIntegerToUnicodeString (NTDLL.@)
1960 * Converts an unsigned integer to a '\0' terminated unicode string.
1963 * Success: STATUS_SUCCESS. str contains the converted number
1964 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1965 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1966 * (with the '\0' termination). In this case str->Length
1967 * is set to the length, the string would have (which can
1968 * be larger than the MaximumLength).
1971 * Instead of base 0 it uses 10 as base.
1972 * If str is NULL it crashes, as the native function does.
1975 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1976 * The native function does this when the string would be longer than 16
1977 * characters even when the string parameter is long enough.
1979 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1980 ULONG value
, /* [I] Value to be converted */
1981 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1982 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1990 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1991 return STATUS_INVALID_PARAMETER
;
1999 digit
= value
% base
;
2000 value
= value
/ base
;
2004 *pos
= 'A' + digit
- 10;
2006 } while (value
!= 0L);
2008 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
2009 if (str
->Length
>= str
->MaximumLength
) {
2010 return STATUS_BUFFER_OVERFLOW
;
2012 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
2014 return STATUS_SUCCESS
;
2018 /*************************************************************************
2019 * RtlGUIDFromString (NTDLL.@)
2021 * Convert a string representation of a GUID into a GUID.
2024 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
2025 * guid [O] Destination for the converted GUID
2028 * Success: STATUS_SUCCESS. guid contains the converted value.
2029 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
2032 * See RtlStringFromGUID.
2034 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
2037 const WCHAR
*lpszCLSID
= str
->Buffer
;
2038 BYTE
* lpOut
= (BYTE
*)guid
;
2040 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
2042 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2043 * to memory: DWORD... WORD WORD BYTES............
2050 if (*lpszCLSID
!= '{')
2051 return STATUS_INVALID_PARAMETER
;
2054 case 9: case 14: case 19: case 24:
2055 if (*lpszCLSID
!= '-')
2056 return STATUS_INVALID_PARAMETER
;
2060 if (*lpszCLSID
!= '}')
2061 return STATUS_INVALID_PARAMETER
;
2066 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
2069 /* Read two hex digits as a byte value */
2070 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
2071 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
2072 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
2073 else return STATUS_INVALID_PARAMETER
;
2075 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
2076 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
2077 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
2078 else return STATUS_INVALID_PARAMETER
;
2080 byte
= ch
<< 4 | ch2
;
2084 #ifndef WORDS_BIGENDIAN
2085 /* For Big Endian machines, we store the data such that the
2086 * dword/word members can be read as DWORDS and WORDS correctly. */
2088 case 1: lpOut
[3] = byte
; break;
2089 case 3: lpOut
[2] = byte
; break;
2090 case 5: lpOut
[1] = byte
; break;
2091 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
2093 case 10: case 15: lpOut
[1] = byte
; break;
2094 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
2097 default: lpOut
[0] = byte
; lpOut
++; break;
2099 lpszCLSID
++; /* Skip 2nd character of byte */
2107 return STATUS_SUCCESS
;
2110 /*************************************************************************
2111 * RtlStringFromGUID (NTDLL.@)
2113 * Convert a GUID into a string representation of a GUID.
2116 * guid [I] GUID to convert
2117 * str [O] Destination for the converted string
2120 * Success: STATUS_SUCCESS. str contains the converted value.
2121 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2124 * See RtlGUIDFromString.
2126 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2128 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2129 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2130 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2131 '%','0','2','X','%','0','2','X','}','\0' };
2133 TRACE("(%p,%p)\n", guid
, str
);
2135 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
2136 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
2137 str
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
2140 str
->Length
= str
->MaximumLength
= 0;
2141 return STATUS_NO_MEMORY
;
2143 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2144 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2145 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2147 return STATUS_SUCCESS
;
2150 /******************************************************************************
2151 * RtlHashUnicodeString [NTDLL.@]
2153 NTSTATUS WINAPI
RtlHashUnicodeString(PCUNICODE_STRING string
, BOOLEAN case_insensitive
, ULONG alg
, ULONG
*hash
)
2157 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
2161 case HASH_STRING_ALGORITHM_DEFAULT
:
2162 case HASH_STRING_ALGORITHM_X65599
:
2165 return STATUS_INVALID_PARAMETER
;
2169 for (i
= 0; i
< string
->Length
/sizeof(WCHAR
); i
++)
2170 *hash
= *hash
*65599 + (case_insensitive
? toupperW(string
->Buffer
[i
]) : string
->Buffer
[i
]);
2172 return STATUS_SUCCESS
;