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
;
69 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
72 /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
73 if (!unix_table
) flags
|= MB_COMPOSITE
;
76 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
77 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
80 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
81 const char* defchar
, int *used
)
84 return wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
);
85 if (used
) *used
= 0; /* all chars are valid for UTF-8 */
86 return wine_utf8_wcstombs( flags
, src
, srclen
, dst
, dstlen
);
89 /**************************************************************************
90 * RtlInitAnsiString (NTDLL.@)
92 * Initializes a buffered ansi string.
98 * Assigns source to target->Buffer. The length of source is assigned to
99 * target->Length and target->MaximumLength. If source is NULL the length
100 * of source is assumed to be 0.
102 void WINAPI
RtlInitAnsiString(
103 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
104 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
106 if ((target
->Buffer
= (PCHAR
) source
))
108 target
->Length
= strlen(source
);
109 target
->MaximumLength
= target
->Length
+ 1;
111 else target
->Length
= target
->MaximumLength
= 0;
114 /**************************************************************************
115 * RtlInitAnsiStringEx (NTDLL.@)
117 * Initializes a buffered ansi string.
120 * An appropriate NTSTATUS value.
123 * Assigns source to target->Buffer. The length of source is assigned to
124 * target->Length and target->MaximumLength. If source is NULL the length
125 * of source is assumed to be 0.
127 NTSTATUS WINAPI
RtlInitAnsiStringEx(PANSI_STRING target
, PCSZ source
)
131 unsigned int len
= strlen(source
);
133 return STATUS_NAME_TOO_LONG
;
135 target
->Buffer
= (PCHAR
) source
;
136 target
->Length
= len
;
137 target
->MaximumLength
= len
+ 1;
141 target
->Buffer
= NULL
;
143 target
->MaximumLength
= 0;
145 return STATUS_SUCCESS
;
148 /**************************************************************************
149 * RtlInitString (NTDLL.@)
151 * Initializes a buffered string.
157 * Assigns source to target->Buffer. The length of source is assigned to
158 * target->Length and target->MaximumLength. If source is NULL the length
159 * of source is assumed to be 0.
161 void WINAPI
RtlInitString(
162 PSTRING target
, /* [I/O] Buffered string to be initialized */
163 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
165 RtlInitAnsiString( target
, source
);
169 /**************************************************************************
170 * RtlFreeAnsiString (NTDLL.@)
172 void WINAPI
RtlFreeAnsiString( PSTRING str
)
176 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
177 RtlZeroMemory( str
, sizeof(*str
) );
182 /**************************************************************************
183 * RtlFreeOemString (NTDLL.@)
185 void WINAPI
RtlFreeOemString( PSTRING str
)
187 RtlFreeAnsiString( str
);
191 /**************************************************************************
192 * RtlCopyString (NTDLL.@)
194 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
198 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
199 memcpy( dst
->Buffer
, src
->Buffer
, len
);
202 else dst
->Length
= 0;
206 /**************************************************************************
207 * RtlInitUnicodeString (NTDLL.@)
209 * Initializes a buffered unicode string.
215 * Assigns source to target->Buffer. The length of source is assigned to
216 * target->Length and target->MaximumLength. If source is NULL the length
217 * of source is assumed to be 0.
219 void WINAPI
RtlInitUnicodeString(
220 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
221 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
223 if ((target
->Buffer
= (PWSTR
) source
))
225 unsigned int length
= strlenW(source
) * sizeof(WCHAR
);
228 target
->Length
= length
;
229 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
231 else target
->Length
= target
->MaximumLength
= 0;
235 /**************************************************************************
236 * RtlInitUnicodeStringEx (NTDLL.@)
238 * Initializes a buffered unicode string.
241 * Success: STATUS_SUCCESS. target is initialized.
242 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
245 * Assigns source to target->Buffer. The length of source is assigned to
246 * target->Length and target->MaximumLength. If source is NULL the length
247 * of source is assumed to be 0.
249 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
250 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
251 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
253 if (source
!= NULL
) {
254 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
257 return STATUS_NAME_TOO_LONG
;
259 target
->Length
= len
;
260 target
->MaximumLength
= len
+ sizeof(WCHAR
);
261 target
->Buffer
= (PWSTR
) source
;
265 target
->MaximumLength
= 0;
266 target
->Buffer
= NULL
;
268 return STATUS_SUCCESS
;
272 /**************************************************************************
273 * RtlCreateUnicodeString (NTDLL.@)
275 * Creates a UNICODE_STRING from a null-terminated Unicode string.
281 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
283 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
284 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
285 memcpy( target
->Buffer
, src
, len
);
286 target
->MaximumLength
= len
;
287 target
->Length
= len
- sizeof(WCHAR
);
292 /**************************************************************************
293 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
295 * Creates a UNICODE_STRING from a null-terminated Ascii string.
301 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
304 RtlInitAnsiString( &ansi
, src
);
305 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
309 /**************************************************************************
310 * RtlFreeUnicodeString (NTDLL.@)
312 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
313 * RtlCreateUnicodeStringFromAsciiz().
318 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
322 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
323 RtlZeroMemory( str
, sizeof(*str
) );
328 /**************************************************************************
329 * RtlCopyUnicodeString (NTDLL.@)
331 * Copies from one UNICODE_STRING to another.
336 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
340 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
341 memcpy( dst
->Buffer
, src
->Buffer
, len
);
343 /* append terminating '\0' if enough space */
344 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
346 else dst
->Length
= 0;
350 /**************************************************************************
351 * RtlDuplicateUnicodeString (NTDLL.@)
353 * Duplicates a unicode string.
356 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
357 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
358 * STATUS_NO_MEMORY, if the allocation fails.
361 * For add_nul there are several possible values:
362 * 0 = destination will not be '\0' terminated,
363 * 1 = destination will be '\0' terminated,
364 * 3 = like 1 but for an empty source string produce '\0' terminated empty
365 * Buffer instead of assigning NULL to the Buffer.
366 * Other add_nul values are invalid.
368 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
369 int add_nul
, /* [I] flag */
370 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
371 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
373 if (source
== NULL
|| destination
== NULL
||
374 source
->Length
> source
->MaximumLength
||
375 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
376 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
377 return STATUS_INVALID_PARAMETER
;
379 if (source
->Length
== 0 && add_nul
!= 3) {
380 destination
->Length
= 0;
381 destination
->MaximumLength
= 0;
382 destination
->Buffer
= NULL
;
384 unsigned int destination_max_len
= source
->Length
;
387 destination_max_len
+= sizeof(WCHAR
);
389 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
390 if (destination
->Buffer
== NULL
) {
391 return STATUS_NO_MEMORY
;
393 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
394 destination
->Length
= source
->Length
;
395 destination
->MaximumLength
= source
->Length
;
396 /* append terminating '\0' if enough space */
398 destination
->MaximumLength
= destination_max_len
;
399 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
404 return STATUS_SUCCESS
;
408 /**************************************************************************
409 * RtlEraseUnicodeString (NTDLL.@)
411 * Overwrites a UNICODE_STRING with zeros.
416 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
420 memset( str
->Buffer
, 0, str
->MaximumLength
);
431 /******************************************************************************
432 * RtlCompareString (NTDLL.@)
434 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
440 len
= min(s1
->Length
, s2
->Length
);
446 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
450 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
452 if (!ret
) ret
= s1
->Length
- s2
->Length
;
457 /******************************************************************************
458 * RtlCompareUnicodeStrings (NTDLL.@)
460 LONG WINAPI
RtlCompareUnicodeStrings( const WCHAR
*s1
, SIZE_T len1
, const WCHAR
*s2
, SIZE_T len2
,
461 BOOLEAN case_insensitive
)
464 SIZE_T len
= min( len1
, len2
);
466 if (case_insensitive
)
468 while (!ret
&& len
--) ret
= toupperW(*s1
++) - toupperW(*s2
++);
472 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
474 if (!ret
) ret
= len1
- len2
;
479 /******************************************************************************
480 * RtlCompareUnicodeString (NTDLL.@)
482 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
483 BOOLEAN CaseInsensitive
)
485 return RtlCompareUnicodeStrings( s1
->Buffer
, s1
->Length
/ sizeof(WCHAR
),
486 s2
->Buffer
, s2
->Length
/ sizeof(WCHAR
), CaseInsensitive
);
490 /**************************************************************************
491 * RtlEqualString (NTDLL.@)
493 * Determine if two strings are equal.
496 * s1 [I] Source string
497 * s2 [I] String to compare to s1
498 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
501 * Non-zero if s1 is equal to s2, 0 otherwise.
503 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
505 if (s1
->Length
!= s2
->Length
) return FALSE
;
506 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
510 /**************************************************************************
511 * RtlEqualUnicodeString (NTDLL.@)
513 * Unicode version of RtlEqualString.
515 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
516 BOOLEAN CaseInsensitive
)
518 if (s1
->Length
!= s2
->Length
) return FALSE
;
519 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
523 /**************************************************************************
524 * RtlPrefixString (NTDLL.@)
526 * Determine if one string is a prefix of another.
529 * s1 [I] Prefix to look for in s2
530 * s2 [I] String that may contain s1 as a prefix
531 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
534 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
536 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
540 if (s1
->Length
> s2
->Length
) return FALSE
;
543 for (i
= 0; i
< s1
->Length
; i
++)
544 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
548 for (i
= 0; i
< s1
->Length
; i
++)
549 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
555 /**************************************************************************
556 * RtlPrefixUnicodeString (NTDLL.@)
558 * Unicode version of RtlPrefixString.
560 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
561 const UNICODE_STRING
*s2
,
562 BOOLEAN ignore_case
)
566 if (s1
->Length
> s2
->Length
) return FALSE
;
569 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
570 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
574 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
575 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
581 /**************************************************************************
582 * RtlEqualComputerName (NTDLL.@)
584 * Determine if two computer names are the same.
587 * left [I] First computer name
588 * right [I] Second computer name
591 * 0 if the names are equal, non-zero otherwise.
594 * The comparison is case insensitive.
596 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
597 const UNICODE_STRING
*right
)
600 STRING upLeft
, upRight
;
602 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
604 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
606 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
607 RtlFreeOemString( &upRight
);
609 RtlFreeOemString( &upLeft
);
615 /**************************************************************************
616 * RtlEqualDomainName (NTDLL.@)
618 * Determine if two domain names are the same.
621 * left [I] First domain name
622 * right [I] Second domain name
625 * 0 if the names are equal, non-zero otherwise.
628 * The comparison is case insensitive.
630 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
631 const UNICODE_STRING
*right
)
633 return RtlEqualComputerName(left
, right
);
637 /**************************************************************************
638 * RtlAnsiCharToUnicodeChar (NTDLL.@)
640 * Converts the first ansi character to a unicode character.
643 * ansi [I/O] Pointer to the ansi string.
646 * Unicode representation of the first character in the ansi string.
649 * Upon successful completion, the char pointer ansi points to is
650 * incremented by the size of the character.
652 WCHAR WINAPI
RtlAnsiCharToUnicodeChar(LPSTR
*ansi
)
655 DWORD charSize
= sizeof(CHAR
);
657 if (wine_is_dbcs_leadbyte(ansi_table
, **ansi
))
660 RtlMultiByteToUnicodeN(&str
, sizeof(WCHAR
), NULL
, *ansi
, charSize
);
667 COPY BETWEEN ANSI_STRING or UNICODE_STRING
668 there is no parameter checking, it just crashes
672 /**************************************************************************
673 * RtlAnsiStringToUnicodeString (NTDLL.@)
675 * Converts an ansi string to a unicode string.
678 * Success: STATUS_SUCCESS. uni contains the converted string
679 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
680 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
681 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
684 * This function always writes a terminating '\0'.
686 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
687 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
688 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
689 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
691 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
693 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
694 uni
->Length
= total
- sizeof(WCHAR
);
697 uni
->MaximumLength
= total
;
698 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
699 return STATUS_NO_MEMORY
;
701 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
703 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
704 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
705 return STATUS_SUCCESS
;
709 /**************************************************************************
710 * RtlOemStringToUnicodeString (NTDLL.@)
712 * Converts an oem string to a unicode string.
715 * Success: STATUS_SUCCESS. uni contains the converted string
716 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
717 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
718 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
721 * This function always writes a terminating '\0'.
723 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
724 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
725 const STRING
*oem
, /* [I] Oem string to be converted */
726 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
728 DWORD total
= RtlOemStringToUnicodeSize( oem
);
730 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
731 uni
->Length
= total
- sizeof(WCHAR
);
734 uni
->MaximumLength
= total
;
735 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
736 return STATUS_NO_MEMORY
;
738 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
740 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
741 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
742 return STATUS_SUCCESS
;
746 /**************************************************************************
747 * RtlUnicodeStringToAnsiString (NTDLL.@)
749 * Converts a unicode string to an ansi string.
752 * Success: STATUS_SUCCESS. ansi contains the converted string
753 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
754 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
757 * This function always writes a terminating '\0'.
758 * It performs a partial copy if ansi is too small.
760 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
761 STRING
*ansi
, /* [I/O] Destination for the ansi string */
762 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
763 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
765 NTSTATUS ret
= STATUS_SUCCESS
;
766 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
768 ansi
->Length
= len
- 1;
771 ansi
->MaximumLength
= len
;
772 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
773 return STATUS_NO_MEMORY
;
775 else if (ansi
->MaximumLength
< len
)
777 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
778 ansi
->Length
= ansi
->MaximumLength
- 1;
779 ret
= STATUS_BUFFER_OVERFLOW
;
782 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
783 ansi
->Buffer
[ansi
->Length
] = 0;
788 /**************************************************************************
789 * RtlUnicodeStringToOemString (NTDLL.@)
791 * Converts a Rtl Unicode string to an OEM string.
794 * oem [O] Destination for OEM string
795 * uni [I] Source Unicode string
796 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
799 * Success: STATUS_SUCCESS. oem contains the converted string
800 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
801 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
804 * If doalloc is TRUE, the length allocated is uni->Length + 1.
805 * This function always '\0' terminates the string returned.
807 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
808 const UNICODE_STRING
*uni
,
811 NTSTATUS ret
= STATUS_SUCCESS
;
812 DWORD len
= RtlUnicodeStringToOemSize( uni
);
814 oem
->Length
= len
- 1;
817 oem
->MaximumLength
= len
;
818 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
819 return STATUS_NO_MEMORY
;
821 else if (oem
->MaximumLength
< len
)
823 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
824 oem
->Length
= oem
->MaximumLength
- 1;
825 ret
= STATUS_BUFFER_OVERFLOW
;
828 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
829 oem
->Buffer
[oem
->Length
] = 0;
834 /**************************************************************************
835 * RtlMultiByteToUnicodeN (NTDLL.@)
837 * Converts a multi-byte string to a Unicode string.
843 * Performs a partial copy if dst is too small.
845 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
846 LPCSTR src
, DWORD srclen
)
849 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
851 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
852 return STATUS_SUCCESS
;
856 /**************************************************************************
857 * RtlOemToUnicodeN (NTDLL.@)
859 * Converts a multi-byte string in the OEM code page to a Unicode string.
864 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
865 LPCSTR src
, DWORD srclen
)
867 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
869 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
870 return STATUS_SUCCESS
;
874 /**************************************************************************
875 * RtlUnicodeToMultiByteN (NTDLL.@)
877 * Converts a Unicode string to a multi-byte string in the ANSI code page.
882 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
883 LPCWSTR src
, DWORD srclen
)
885 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
886 dst
, dstlen
, NULL
, NULL
);
888 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
889 return STATUS_SUCCESS
;
893 /**************************************************************************
894 * RtlUnicodeToOemN (NTDLL.@)
896 * Converts a Unicode string to a multi-byte string in the OEM code page.
901 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
902 LPCWSTR src
, DWORD srclen
)
904 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
905 dst
, dstlen
, NULL
, NULL
);
907 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
908 return STATUS_SUCCESS
;
917 /**************************************************************************
918 * RtlUpperChar (NTDLL.@)
920 * Converts an Ascii character to uppercase.
923 * ch [I] Character to convert
926 * The uppercase character value.
929 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
930 * All other input characters are returned unchanged. The locale and
931 * multibyte characters are not taken into account (as native DLL).
933 CHAR WINAPI
RtlUpperChar( CHAR ch
)
935 if (ch
>= 'a' && ch
<= 'z') {
936 return ch
- 'a' + 'A';
943 /**************************************************************************
944 * RtlUpperString (NTDLL.@)
946 * Converts an Ascii string to uppercase.
949 * dst [O] Destination for converted string
950 * src [I] Source string to convert
956 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
957 * All other src characters are copied unchanged to dst. The locale and
958 * multibyte characters are not taken into account (as native DLL).
959 * The number of character copied is the minimum of src->Length and
960 * the dst->MaximumLength.
962 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
964 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
966 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
971 /**************************************************************************
972 * RtlUpcaseUnicodeChar (NTDLL.@)
974 * Converts a Unicode character to uppercase.
977 * wch [I] Character to convert
980 * The uppercase character value.
982 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
984 return toupperW(wch
);
988 /**************************************************************************
989 * RtlDowncaseUnicodeChar (NTDLL.@)
991 * Converts a Unicode character to lowercase.
994 * wch [I] Character to convert
997 * The lowercase character value.
999 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
1001 return tolowerW(wch
);
1005 /**************************************************************************
1006 * RtlUpcaseUnicodeString (NTDLL.@)
1008 * Converts a Unicode string to uppercase.
1011 * dest [O] Destination for converted string
1012 * src [I] Source string to convert
1013 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1016 * Success: STATUS_SUCCESS. dest contains the converted string.
1017 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1018 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1021 * dest is never '\0' terminated because it may be equal to src, and src
1022 * might not be '\0' terminated. dest->Length is only set upon success.
1024 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
1025 const UNICODE_STRING
*src
,
1028 DWORD i
, len
= src
->Length
;
1032 dest
->MaximumLength
= len
;
1033 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1034 return STATUS_NO_MEMORY
;
1036 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1038 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
1040 return STATUS_SUCCESS
;
1044 /**************************************************************************
1045 * RtlDowncaseUnicodeString (NTDLL.@)
1047 * Converts a Unicode string to lowercase.
1050 * dest [O] Destination for converted string
1051 * src [I] Source string to convert
1052 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1055 * Success: STATUS_SUCCESS. dest contains the converted string.
1056 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1057 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1060 * dest is never '\0' terminated because it may be equal to src, and src
1061 * might not be '\0' terminated. dest->Length is only set upon success.
1063 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
1064 UNICODE_STRING
*dest
,
1065 const UNICODE_STRING
*src
,
1069 DWORD len
= src
->Length
;
1072 dest
->MaximumLength
= len
;
1073 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
1074 return STATUS_NO_MEMORY
;
1076 } else if (len
> dest
->MaximumLength
) {
1077 return STATUS_BUFFER_OVERFLOW
;
1080 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
1081 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
1084 return STATUS_SUCCESS
;
1088 /**************************************************************************
1089 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1091 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1097 * writes terminating 0
1099 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1100 const UNICODE_STRING
*src
,
1104 UNICODE_STRING upcase
;
1106 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1108 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1109 RtlFreeUnicodeString( &upcase
);
1115 /**************************************************************************
1116 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1118 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1119 * stored in STRING format.
1125 * writes terminating 0
1127 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1128 const UNICODE_STRING
*src
,
1132 UNICODE_STRING upcase
;
1134 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1136 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1137 RtlFreeUnicodeString( &upcase
);
1143 /**************************************************************************
1144 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1146 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1147 * stored in STRING format.
1153 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1155 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1156 const UNICODE_STRING
*uni
,
1160 UNICODE_STRING upcase
;
1163 upcase
.Buffer
= tmp
;
1164 upcase
.MaximumLength
= sizeof(tmp
);
1165 ret
= RtlUpcaseUnicodeString( &upcase
, uni
, FALSE
);
1166 if (ret
== STATUS_BUFFER_OVERFLOW
) ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
);
1170 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1174 oem
->MaximumLength
= len
;
1175 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1177 ret
= STATUS_NO_MEMORY
;
1181 else if (oem
->MaximumLength
< len
)
1183 ret
= STATUS_BUFFER_OVERFLOW
;
1184 oem
->Length
= oem
->MaximumLength
;
1185 if (!oem
->MaximumLength
) goto done
;
1187 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1189 if (upcase
.Buffer
!= tmp
) RtlFreeUnicodeString( &upcase
);
1195 /**************************************************************************
1196 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1198 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1203 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1204 LPCWSTR src
, DWORD srclen
)
1210 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1211 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1212 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1213 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1218 /**************************************************************************
1219 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1221 * Converts a Unicode string to the equivalent OEM upper-case representation.
1226 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1227 LPCWSTR src
, DWORD srclen
)
1233 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1234 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1235 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1236 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1246 /**************************************************************************
1247 * RtlOemStringToUnicodeSize (NTDLL.@)
1248 * RtlxOemStringToUnicodeSize (NTDLL.@)
1250 * Calculate the size in bytes necessary for the Unicode conversion of str,
1251 * including the terminating '\0'.
1254 * str [I] String to calculate the size of
1257 * The calculated size.
1259 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1261 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1262 return (ret
+ 1) * sizeof(WCHAR
);
1266 /**************************************************************************
1267 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1268 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1270 * Calculate the size in bytes necessary for the Unicode conversion of str,
1271 * including the terminating '\0'.
1274 * str [I] String to calculate the size of
1277 * The calculated size.
1279 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1282 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1283 return ret
+ sizeof(WCHAR
);
1287 /**************************************************************************
1288 * RtlMultiByteToUnicodeSize (NTDLL.@)
1290 * Compute the size in bytes necessary for the Unicode conversion of str,
1291 * without the terminating '\0'.
1294 * size [O] Destination for size
1295 * str [I] String to calculate the size of
1296 * len [I] Length of str
1301 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1303 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1304 return STATUS_SUCCESS
;
1308 /**************************************************************************
1309 * RtlUnicodeToMultiByteSize (NTDLL.@)
1311 * Calculate the size in bytes necessary for the multibyte conversion of str,
1312 * without the terminating '\0'.
1315 * size [O] Destination for size
1316 * str [I] String to calculate the size of
1317 * len [I] Length of str
1322 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1324 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1325 return STATUS_SUCCESS
;
1329 /**************************************************************************
1330 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1331 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1333 * Calculate the size in bytes necessary for the Ansi conversion of str,
1334 * including the terminating '\0'.
1337 * str [I] String to calculate the size of
1340 * The calculated size.
1342 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1345 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1350 /**************************************************************************
1351 * RtlUnicodeStringToOemSize (NTDLL.@)
1352 * RtlxUnicodeStringToOemSize (NTDLL.@)
1354 * Calculate the size in bytes necessary for the OEM conversion of str,
1355 * including the terminating '\0'.
1358 * str [I] String to calculate the size of
1361 * The calculated size.
1363 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1365 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1366 NULL
, 0, NULL
, NULL
) + 1;
1370 /**************************************************************************
1371 * RtlAppendAsciizToString (NTDLL.@)
1373 * Concatenates a buffered character string and a '\0' terminated character
1377 * Success: STATUS_SUCCESS. src is appended to dest.
1378 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1379 * to hold the concatenated string.
1382 * if src is NULL dest is unchanged.
1383 * dest is never '\0' terminated.
1385 NTSTATUS WINAPI
RtlAppendAsciizToString(
1386 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1387 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1390 unsigned int src_len
= strlen(src
);
1391 unsigned int dest_len
= src_len
+ dest
->Length
;
1393 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1394 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1395 dest
->Length
= dest_len
;
1397 return STATUS_SUCCESS
;
1401 /**************************************************************************
1402 * RtlAppendStringToString (NTDLL.@)
1404 * Concatenates two buffered character strings
1407 * Success: STATUS_SUCCESS. src is appended to dest.
1408 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1409 * to hold the concatenated string.
1412 * if src->length is zero dest is unchanged.
1413 * dest is never '\0' terminated.
1415 NTSTATUS WINAPI
RtlAppendStringToString(
1416 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1417 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1419 if (src
->Length
!= 0) {
1420 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1422 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1423 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1424 dest
->Length
= dest_len
;
1426 return STATUS_SUCCESS
;
1430 /**************************************************************************
1431 * RtlAppendUnicodeToString (NTDLL.@)
1433 * Concatenates a buffered unicode string and a '\0' terminated unicode
1437 * Success: STATUS_SUCCESS. src is appended to dest.
1438 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1439 * to hold the concatenated string.
1442 * if src is NULL dest is unchanged.
1443 * dest is '\0' terminated when the MaximumLength allows it.
1444 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1447 * Does not write in the src->Buffer beyond MaximumLength when
1448 * MaximumLength is odd as the native function does.
1450 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1451 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1452 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1455 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1456 unsigned int dest_len
= src_len
+ dest
->Length
;
1458 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1459 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1460 dest
->Length
= dest_len
;
1461 /* append terminating '\0' if enough space */
1462 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1463 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1466 return STATUS_SUCCESS
;
1470 /**************************************************************************
1471 * RtlAppendUnicodeStringToString (NTDLL.@)
1473 * Concatenates two buffered unicode strings
1476 * Success: STATUS_SUCCESS. src is appended to dest.
1477 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1478 * to hold the concatenated string.
1481 * if src->length is zero dest is unchanged.
1482 * dest is '\0' terminated when the MaximumLength allows it.
1483 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1486 * Does not write in the src->Buffer beyond MaximumLength when
1487 * MaximumLength is odd as the native function does.
1489 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1490 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1491 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1493 if (src
->Length
!= 0) {
1494 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1496 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1497 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1498 dest
->Length
= dest_len
;
1499 /* append terminating '\0' if enough space */
1500 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1501 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1504 return STATUS_SUCCESS
;
1508 /**************************************************************************
1509 * RtlFindCharInUnicodeString (NTDLL.@)
1511 * Searches for one of several unicode characters in a unicode string.
1514 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1515 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1517 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1518 int flags
, /* [I] Flags */
1519 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1520 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1521 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1523 unsigned int main_idx
, search_idx
;
1527 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1528 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1529 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1530 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1531 return STATUS_SUCCESS
;
1536 return STATUS_NOT_FOUND
;
1538 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1539 while (main_idx
-- > 0) {
1540 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1541 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1542 *pos
= main_idx
* sizeof(WCHAR
);
1543 return STATUS_SUCCESS
;
1548 return STATUS_NOT_FOUND
;
1550 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1552 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1553 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1556 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1557 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1558 return STATUS_SUCCESS
;
1562 return STATUS_NOT_FOUND
;
1564 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1565 while (main_idx
-- > 0) {
1567 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1568 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1571 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1572 *pos
= main_idx
* sizeof(WCHAR
);
1573 return STATUS_SUCCESS
;
1577 return STATUS_NOT_FOUND
;
1579 return STATUS_NOT_FOUND
;
1587 /**************************************************************************
1588 * RtlIsTextUnicode (NTDLL.@)
1590 * Attempt to guess whether a text buffer is Unicode.
1593 * buf [I] Text buffer to test
1594 * len [I] Length of buf
1595 * pf [O] Destination for test results
1598 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1601 * Should implement more tests.
1603 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1605 static const WCHAR std_control_chars
[] = {'\r','\n','\t',' ',0x3000,0};
1606 static const WCHAR byterev_control_chars
[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1607 const WCHAR
*s
= buf
;
1609 unsigned int flags
= ~0U, out_flags
= 0;
1611 if (len
< sizeof(WCHAR
))
1613 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1620 * Apply various tests to the text string. According to the
1621 * docs, each test "passed" sets the corresponding flag in
1622 * the output flags. But some of the tests are mutually
1623 * exclusive, so I don't see how you could pass all tests ...
1626 /* Check for an odd length ... pass if even. */
1627 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1629 if (((const char *)buf
)[len
- 1] == 0)
1630 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1632 len
/= sizeof(WCHAR
);
1633 /* Windows only checks the first 256 characters */
1634 if (len
> 256) len
= 256;
1636 /* Check for the special byte order unicode marks. */
1637 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1638 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1640 /* apply some statistical analysis */
1641 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1644 /* FIXME: checks only for ASCII characters in the unicode stream */
1645 for (i
= 0; i
< len
; i
++)
1647 if (s
[i
] <= 255) stats
++;
1649 if (stats
> len
/ 2)
1650 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1653 /* Check for unicode NULL chars */
1654 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1656 for (i
= 0; i
< len
; i
++)
1658 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1660 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1666 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1668 for (i
= 0; i
< len
; i
++)
1670 if (strchrW(std_control_chars
, s
[i
]))
1672 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1678 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1680 for (i
= 0; i
< len
; i
++)
1682 if (strchrW(byterev_control_chars
, s
[i
]))
1684 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1695 /* check for flags that indicate it's definitely not valid Unicode */
1696 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1697 /* now check for invalid ASCII, and assume Unicode if so */
1698 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1699 /* now check for Unicode flags */
1700 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1706 /**************************************************************************
1707 * RtlCharToInteger (NTDLL.@)
1709 * Converts a character string into its integer equivalent.
1712 * Success: STATUS_SUCCESS. value contains the converted number
1713 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1714 * STATUS_ACCESS_VIOLATION, if value is NULL.
1717 * For base 0 it uses 10 as base and the string should be in the format
1718 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1719 * For other bases the string should be in the format
1720 * "{whitespace} [+|-] {digits}".
1721 * No check is made for value overflow, only the lower 32 bits are assigned.
1722 * If str is NULL it crashes, as the native function does.
1725 * This function does not read garbage behind '\0' as the native version does.
1727 NTSTATUS WINAPI
RtlCharToInteger(
1728 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1729 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1730 ULONG
*value
) /* [O] Destination for the converted value */
1734 ULONG RunningTotal
= 0;
1735 BOOL bMinus
= FALSE
;
1737 while (*str
!= '\0' && *str
<= ' ') {
1743 } else if (*str
== '-') {
1750 if (str
[0] == '0') {
1751 if (str
[1] == 'b') {
1754 } else if (str
[1] == 'o') {
1757 } else if (str
[1] == 'x') {
1762 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1763 return STATUS_INVALID_PARAMETER
;
1766 if (value
== NULL
) {
1767 return STATUS_ACCESS_VIOLATION
;
1770 while (*str
!= '\0') {
1772 if (chCurrent
>= '0' && chCurrent
<= '9') {
1773 digit
= chCurrent
- '0';
1774 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1775 digit
= chCurrent
- 'A' + 10;
1776 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1777 digit
= chCurrent
- 'a' + 10;
1781 if (digit
< 0 || digit
>= base
) {
1782 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1783 return STATUS_SUCCESS
;
1786 RunningTotal
= RunningTotal
* base
+ digit
;
1790 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1791 return STATUS_SUCCESS
;
1795 /**************************************************************************
1796 * RtlIntegerToChar (NTDLL.@)
1798 * Converts an unsigned integer to a character string.
1801 * Success: STATUS_SUCCESS. str contains the converted number
1802 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1803 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1804 * STATUS_ACCESS_VIOLATION, if str is NULL.
1807 * Instead of base 0 it uses 10 as base.
1808 * Writes at most length characters to the string str.
1809 * Str is '\0' terminated when length allows it.
1810 * When str fits exactly in length characters the '\0' is omitted.
1812 NTSTATUS WINAPI
RtlIntegerToChar(
1813 ULONG value
, /* [I] Value to be converted */
1814 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1815 ULONG length
, /* [I] Length of the str buffer in bytes */
1816 PCHAR str
) /* [O] Destination for the converted value */
1825 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1826 return STATUS_INVALID_PARAMETER
;
1834 digit
= value
% base
;
1835 value
= value
/ base
;
1839 *pos
= 'A' + digit
- 10;
1841 } while (value
!= 0L);
1843 len
= &buffer
[32] - pos
;
1845 return STATUS_BUFFER_OVERFLOW
;
1846 } else if (str
== NULL
) {
1847 return STATUS_ACCESS_VIOLATION
;
1848 } else if (len
== length
) {
1849 memcpy(str
, pos
, len
);
1851 memcpy(str
, pos
, len
+ 1);
1853 return STATUS_SUCCESS
;
1857 /**************************************************************************
1858 * RtlUnicodeStringToInteger (NTDLL.@)
1860 * Converts a unicode string into its integer equivalent.
1863 * Success: STATUS_SUCCESS. value contains the converted number
1864 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1865 * STATUS_ACCESS_VIOLATION, if value is NULL.
1868 * For base 0 it uses 10 as base and the string should be in the format
1869 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1870 * For other bases the string should be in the format
1871 * "{whitespace} [+|-] {digits}".
1872 * No check is made for value overflow, only the lower 32 bits are assigned.
1873 * If str is NULL it crashes, as the native function does.
1876 * This function does not read garbage on string length 0 as the native
1879 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1880 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1881 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1882 ULONG
*value
) /* [O] Destination for the converted value */
1884 LPWSTR lpwstr
= str
->Buffer
;
1885 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1888 ULONG RunningTotal
= 0;
1889 BOOL bMinus
= FALSE
;
1891 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1896 if (CharsRemaining
>= 1) {
1897 if (*lpwstr
== '+') {
1900 } else if (*lpwstr
== '-') {
1909 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1910 if (lpwstr
[1] == 'b') {
1912 CharsRemaining
-= 2;
1914 } else if (lpwstr
[1] == 'o') {
1916 CharsRemaining
-= 2;
1918 } else if (lpwstr
[1] == 'x') {
1920 CharsRemaining
-= 2;
1924 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1925 return STATUS_INVALID_PARAMETER
;
1928 if (value
== NULL
) {
1929 return STATUS_ACCESS_VIOLATION
;
1932 while (CharsRemaining
>= 1) {
1933 wchCurrent
= *lpwstr
;
1934 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1935 digit
= wchCurrent
- '0';
1936 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1937 digit
= wchCurrent
- 'A' + 10;
1938 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1939 digit
= wchCurrent
- 'a' + 10;
1943 if (digit
< 0 || digit
>= base
) {
1944 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1945 return STATUS_SUCCESS
;
1948 RunningTotal
= RunningTotal
* base
+ digit
;
1953 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1954 return STATUS_SUCCESS
;
1958 /**************************************************************************
1959 * RtlIntegerToUnicodeString (NTDLL.@)
1961 * Converts an unsigned integer to a '\0' terminated unicode string.
1964 * Success: STATUS_SUCCESS. str contains the converted number
1965 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1966 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1967 * (with the '\0' termination). In this case str->Length
1968 * is set to the length, the string would have (which can
1969 * be larger than the MaximumLength).
1972 * Instead of base 0 it uses 10 as base.
1973 * If str is NULL it crashes, as the native function does.
1976 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1977 * The native function does this when the string would be longer than 16
1978 * characters even when the string parameter is long enough.
1980 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1981 ULONG value
, /* [I] Value to be converted */
1982 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1983 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1991 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1992 return STATUS_INVALID_PARAMETER
;
2000 digit
= value
% base
;
2001 value
= value
/ base
;
2005 *pos
= 'A' + digit
- 10;
2007 } while (value
!= 0L);
2009 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
2010 if (str
->Length
>= str
->MaximumLength
) {
2011 return STATUS_BUFFER_OVERFLOW
;
2013 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
2015 return STATUS_SUCCESS
;
2019 /*************************************************************************
2020 * RtlGUIDFromString (NTDLL.@)
2022 * Convert a string representation of a GUID into a GUID.
2025 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
2026 * guid [O] Destination for the converted GUID
2029 * Success: STATUS_SUCCESS. guid contains the converted value.
2030 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
2033 * See RtlStringFromGUID.
2035 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
2038 const WCHAR
*lpszCLSID
= str
->Buffer
;
2039 BYTE
* lpOut
= (BYTE
*)guid
;
2041 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
2043 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2044 * to memory: DWORD... WORD WORD BYTES............
2051 if (*lpszCLSID
!= '{')
2052 return STATUS_INVALID_PARAMETER
;
2055 case 9: case 14: case 19: case 24:
2056 if (*lpszCLSID
!= '-')
2057 return STATUS_INVALID_PARAMETER
;
2061 if (*lpszCLSID
!= '}')
2062 return STATUS_INVALID_PARAMETER
;
2067 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
2070 /* Read two hex digits as a byte value */
2071 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
2072 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
2073 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
2074 else return STATUS_INVALID_PARAMETER
;
2076 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
2077 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
2078 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
2079 else return STATUS_INVALID_PARAMETER
;
2081 byte
= ch
<< 4 | ch2
;
2085 #ifndef WORDS_BIGENDIAN
2086 /* For Big Endian machines, we store the data such that the
2087 * dword/word members can be read as DWORDS and WORDS correctly. */
2089 case 1: lpOut
[3] = byte
; break;
2090 case 3: lpOut
[2] = byte
; break;
2091 case 5: lpOut
[1] = byte
; break;
2092 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
2094 case 10: case 15: lpOut
[1] = byte
; break;
2095 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
2098 default: lpOut
[0] = byte
; lpOut
++; break;
2100 lpszCLSID
++; /* Skip 2nd character of byte */
2108 return STATUS_SUCCESS
;
2111 /*************************************************************************
2112 * RtlStringFromGUID (NTDLL.@)
2114 * Convert a GUID into a string representation of a GUID.
2117 * guid [I] GUID to convert
2118 * str [O] Destination for the converted string
2121 * Success: STATUS_SUCCESS. str contains the converted value.
2122 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2125 * See RtlGUIDFromString.
2127 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2129 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2130 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2131 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2132 '%','0','2','X','%','0','2','X','}','\0' };
2134 TRACE("(%p,%p)\n", guid
, str
);
2136 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
2137 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
2138 str
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
2141 str
->Length
= str
->MaximumLength
= 0;
2142 return STATUS_NO_MEMORY
;
2144 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2145 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2146 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2148 return STATUS_SUCCESS
;
2151 /******************************************************************************
2152 * RtlHashUnicodeString [NTDLL.@]
2154 NTSTATUS WINAPI
RtlHashUnicodeString(PCUNICODE_STRING string
, BOOLEAN case_insensitive
, ULONG alg
, ULONG
*hash
)
2158 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
2162 case HASH_STRING_ALGORITHM_DEFAULT
:
2163 case HASH_STRING_ALGORITHM_X65599
:
2166 return STATUS_INVALID_PARAMETER
;
2170 for (i
= 0; i
< string
->Length
/sizeof(WCHAR
); i
++)
2171 *hash
= *hash
*65599 + (case_insensitive
? toupperW(string
->Buffer
[i
]) : string
->Buffer
[i
]);
2173 return STATUS_SUCCESS
;