4 * Copyright (C) 1996-1998 Marcus Meissner
5 * Copyright (C) 2000 Alexandre Julliard
6 * Copyright (C) 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
41 #define GUID_STRING_LENGTH 38
43 UINT NlsAnsiCodePage
= 0;
44 BYTE NlsMbCodePageTag
= 0;
45 BYTE NlsMbOemCodePageTag
= 0;
47 static const union cptable
*ansi_table
;
48 static const union cptable
*oem_table
;
49 static const union cptable
* unix_table
; /* NULL if UTF8 */
52 /**************************************************************************
53 * __wine_init_codepages (NTDLL.@)
55 * Set the code page once kernel32 is loaded. Should be done differently.
57 void CDECL
__wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
58 const union cptable
*ucp
)
63 NlsAnsiCodePage
= ansi
->info
.codepage
;
67 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
70 /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
71 if (!unix_table
) flags
|= MB_COMPOSITE
;
74 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
75 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
78 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
79 const char* defchar
, int *used
)
82 return wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
);
83 if (used
) *used
= 0; /* all chars are valid for UTF-8 */
84 return wine_utf8_wcstombs( flags
, src
, srclen
, dst
, dstlen
);
87 /**************************************************************************
88 * RtlInitAnsiString (NTDLL.@)
90 * Initializes a buffered ansi string.
96 * Assigns source to target->Buffer. The length of source is assigned to
97 * target->Length and target->MaximumLength. If source is NULL the length
98 * of source is assumed to be 0.
100 void WINAPI
RtlInitAnsiString(
101 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
102 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
104 if ((target
->Buffer
= (PCHAR
) source
))
106 target
->Length
= strlen(source
);
107 target
->MaximumLength
= target
->Length
+ 1;
109 else target
->Length
= target
->MaximumLength
= 0;
112 /**************************************************************************
113 * RtlInitAnsiStringEx (NTDLL.@)
115 * Initializes a buffered ansi string.
118 * An appropriate NTSTATUS value.
121 * Assigns source to target->Buffer. The length of source is assigned to
122 * target->Length and target->MaximumLength. If source is NULL the length
123 * of source is assumed to be 0.
125 NTSTATUS WINAPI
RtlInitAnsiStringEx(PANSI_STRING target
, PCSZ source
)
129 unsigned int len
= strlen(source
);
131 return STATUS_NAME_TOO_LONG
;
133 target
->Buffer
= (PCHAR
) source
;
134 target
->Length
= len
;
135 target
->MaximumLength
= len
+ 1;
139 target
->Buffer
= NULL
;
141 target
->MaximumLength
= 0;
143 return STATUS_SUCCESS
;
146 /**************************************************************************
147 * RtlInitString (NTDLL.@)
149 * Initializes a buffered string.
155 * Assigns source to target->Buffer. The length of source is assigned to
156 * target->Length and target->MaximumLength. If source is NULL the length
157 * of source is assumed to be 0.
159 void WINAPI
RtlInitString(
160 PSTRING target
, /* [I/O] Buffered string to be initialized */
161 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
163 RtlInitAnsiString( target
, source
);
167 /**************************************************************************
168 * RtlFreeAnsiString (NTDLL.@)
170 void WINAPI
RtlFreeAnsiString( PSTRING str
)
174 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
175 RtlZeroMemory( str
, sizeof(*str
) );
180 /**************************************************************************
181 * RtlFreeOemString (NTDLL.@)
183 void WINAPI
RtlFreeOemString( PSTRING str
)
185 RtlFreeAnsiString( str
);
189 /**************************************************************************
190 * RtlCopyString (NTDLL.@)
192 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
196 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
197 memcpy( dst
->Buffer
, src
->Buffer
, len
);
200 else dst
->Length
= 0;
204 /**************************************************************************
205 * RtlInitUnicodeString (NTDLL.@)
207 * Initializes a buffered unicode string.
213 * Assigns source to target->Buffer. The length of source is assigned to
214 * target->Length and target->MaximumLength. If source is NULL the length
215 * of source is assumed to be 0.
217 void WINAPI
RtlInitUnicodeString(
218 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
219 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
221 if ((target
->Buffer
= (PWSTR
) source
))
223 unsigned int length
= strlenW(source
) * sizeof(WCHAR
);
226 target
->Length
= length
;
227 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
229 else target
->Length
= target
->MaximumLength
= 0;
233 /**************************************************************************
234 * RtlInitUnicodeStringEx (NTDLL.@)
236 * Initializes a buffered unicode string.
239 * Success: STATUS_SUCCESS. target is initialized.
240 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
243 * Assigns source to target->Buffer. The length of source is assigned to
244 * target->Length and target->MaximumLength. If source is NULL the length
245 * of source is assumed to be 0.
247 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
248 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
249 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
251 if (source
!= NULL
) {
252 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
255 return STATUS_NAME_TOO_LONG
;
257 target
->Length
= len
;
258 target
->MaximumLength
= len
+ sizeof(WCHAR
);
259 target
->Buffer
= (PWSTR
) source
;
263 target
->MaximumLength
= 0;
264 target
->Buffer
= NULL
;
266 return STATUS_SUCCESS
;
270 /**************************************************************************
271 * RtlCreateUnicodeString (NTDLL.@)
273 * Creates a UNICODE_STRING from a null-terminated Unicode string.
279 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
281 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
282 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
283 memcpy( target
->Buffer
, src
, len
);
284 target
->MaximumLength
= len
;
285 target
->Length
= len
- sizeof(WCHAR
);
290 /**************************************************************************
291 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
293 * Creates a UNICODE_STRING from a null-terminated Ascii string.
299 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
302 RtlInitAnsiString( &ansi
, src
);
303 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
307 /**************************************************************************
308 * RtlFreeUnicodeString (NTDLL.@)
310 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
311 * RtlCreateUnicodeStringFromAsciiz().
316 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
320 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
321 RtlZeroMemory( str
, sizeof(*str
) );
326 /**************************************************************************
327 * RtlCopyUnicodeString (NTDLL.@)
329 * Copies from one UNICODE_STRING to another.
334 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
338 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
339 memcpy( dst
->Buffer
, src
->Buffer
, len
);
341 /* append terminating '\0' if enough space */
342 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
344 else dst
->Length
= 0;
348 /**************************************************************************
349 * RtlDuplicateUnicodeString (NTDLL.@)
351 * Duplicates a unicode string.
354 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
355 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
356 * STATUS_NO_MEMORY, if the allocation fails.
359 * For add_nul there are several possible values:
360 * 0 = destination will not be '\0' terminated,
361 * 1 = destination will be '\0' terminated,
362 * 3 = like 1 but for an empty source string produce '\0' terminated empty
363 * Buffer instead of assigning NULL to the Buffer.
364 * Other add_nul values are invalid.
366 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
367 int add_nul
, /* [I] flag */
368 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
369 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
371 if (source
== NULL
|| destination
== NULL
||
372 source
->Length
> source
->MaximumLength
||
373 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
374 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
375 return STATUS_INVALID_PARAMETER
;
377 if (source
->Length
== 0 && add_nul
!= 3) {
378 destination
->Length
= 0;
379 destination
->MaximumLength
= 0;
380 destination
->Buffer
= NULL
;
382 unsigned int destination_max_len
= source
->Length
;
385 destination_max_len
+= sizeof(WCHAR
);
387 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
388 if (destination
->Buffer
== NULL
) {
389 return STATUS_NO_MEMORY
;
391 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
392 destination
->Length
= source
->Length
;
393 destination
->MaximumLength
= source
->Length
;
394 /* append terminating '\0' if enough space */
396 destination
->MaximumLength
= destination_max_len
;
397 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
402 return STATUS_SUCCESS
;
406 /**************************************************************************
407 * RtlEraseUnicodeString (NTDLL.@)
409 * Overwrites a UNICODE_STRING with zeros.
414 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
418 memset( str
->Buffer
, 0, str
->MaximumLength
);
429 /******************************************************************************
430 * RtlCompareString (NTDLL.@)
432 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
438 len
= min(s1
->Length
, s2
->Length
);
444 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
448 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
450 if (!ret
) ret
= s1
->Length
- s2
->Length
;
455 /******************************************************************************
456 * RtlCompareUnicodeStrings (NTDLL.@)
458 LONG WINAPI
RtlCompareUnicodeStrings( const WCHAR
*s1
, SIZE_T len1
, const WCHAR
*s2
, SIZE_T len2
,
459 BOOLEAN case_insensitive
)
462 SIZE_T len
= min( len1
, len2
);
464 if (case_insensitive
)
466 while (!ret
&& len
--) ret
= toupperW(*s1
++) - toupperW(*s2
++);
470 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
472 if (!ret
) ret
= len1
- len2
;
477 /******************************************************************************
478 * RtlCompareUnicodeString (NTDLL.@)
480 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
481 BOOLEAN CaseInsensitive
)
483 return RtlCompareUnicodeStrings( s1
->Buffer
, s1
->Length
/ sizeof(WCHAR
),
484 s2
->Buffer
, s2
->Length
/ sizeof(WCHAR
), CaseInsensitive
);
488 /**************************************************************************
489 * RtlEqualString (NTDLL.@)
491 * Determine if two strings are equal.
494 * s1 [I] Source string
495 * s2 [I] String to compare to s1
496 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
499 * Non-zero if s1 is equal to s2, 0 otherwise.
501 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
503 if (s1
->Length
!= s2
->Length
) return FALSE
;
504 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
508 /**************************************************************************
509 * RtlEqualUnicodeString (NTDLL.@)
511 * Unicode version of RtlEqualString.
513 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
514 BOOLEAN CaseInsensitive
)
516 if (s1
->Length
!= s2
->Length
) return FALSE
;
517 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
521 /**************************************************************************
522 * RtlPrefixString (NTDLL.@)
524 * Determine if one string is a prefix of another.
527 * s1 [I] Prefix to look for in s2
528 * s2 [I] String that may contain s1 as a prefix
529 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
532 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
534 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
538 if (s1
->Length
> s2
->Length
) return FALSE
;
541 for (i
= 0; i
< s1
->Length
; i
++)
542 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
546 for (i
= 0; i
< s1
->Length
; i
++)
547 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
553 /**************************************************************************
554 * RtlPrefixUnicodeString (NTDLL.@)
556 * Unicode version of RtlPrefixString.
558 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
559 const UNICODE_STRING
*s2
,
560 BOOLEAN ignore_case
)
564 if (s1
->Length
> s2
->Length
) return FALSE
;
567 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
568 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
572 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
573 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
579 /**************************************************************************
580 * RtlEqualComputerName (NTDLL.@)
582 * Determine if two computer names are the same.
585 * left [I] First computer name
586 * right [I] Second computer name
589 * 0 if the names are equal, non-zero otherwise.
592 * The comparison is case insensitive.
594 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
595 const UNICODE_STRING
*right
)
598 STRING upLeft
, upRight
;
600 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
602 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
604 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
605 RtlFreeOemString( &upRight
);
607 RtlFreeOemString( &upLeft
);
613 /**************************************************************************
614 * RtlEqualDomainName (NTDLL.@)
616 * Determine if two domain names are the same.
619 * left [I] First domain name
620 * right [I] Second domain name
623 * 0 if the names are equal, non-zero otherwise.
626 * The comparison is case insensitive.
628 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
629 const UNICODE_STRING
*right
)
631 return RtlEqualComputerName(left
, right
);
635 /**************************************************************************
636 * RtlAnsiCharToUnicodeChar (NTDLL.@)
638 * Converts the first ansi character to a unicode character.
641 * ansi [I/O] Pointer to the ansi string.
644 * Unicode representation of the first character in the ansi string.
647 * Upon successful completion, the char pointer ansi points to is
648 * incremented by the size of the character.
650 WCHAR WINAPI
RtlAnsiCharToUnicodeChar(LPSTR
*ansi
)
653 DWORD charSize
= sizeof(CHAR
);
655 if (wine_is_dbcs_leadbyte(ansi_table
, **ansi
))
658 RtlMultiByteToUnicodeN(&str
, sizeof(WCHAR
), NULL
, *ansi
, charSize
);
665 COPY BETWEEN ANSI_STRING or UNICODE_STRING
666 there is no parameter checking, it just crashes
670 /**************************************************************************
671 * RtlAnsiStringToUnicodeString (NTDLL.@)
673 * Converts an ansi string to a unicode string.
676 * Success: STATUS_SUCCESS. uni contains the converted string
677 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
678 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
679 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
682 * This function always writes a terminating '\0'.
684 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
685 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
686 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
687 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
689 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
691 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
692 uni
->Length
= total
- sizeof(WCHAR
);
695 uni
->MaximumLength
= total
;
696 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
697 return STATUS_NO_MEMORY
;
699 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
701 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
702 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
703 return STATUS_SUCCESS
;
707 /**************************************************************************
708 * RtlOemStringToUnicodeString (NTDLL.@)
710 * Converts an oem string to a unicode string.
713 * Success: STATUS_SUCCESS. uni contains the converted string
714 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
715 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
716 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
719 * This function always writes a terminating '\0'.
721 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
722 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
723 const STRING
*oem
, /* [I] Oem string to be converted */
724 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
726 DWORD total
= RtlOemStringToUnicodeSize( oem
);
728 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
729 uni
->Length
= total
- sizeof(WCHAR
);
732 uni
->MaximumLength
= total
;
733 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
734 return STATUS_NO_MEMORY
;
736 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
738 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
739 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
740 return STATUS_SUCCESS
;
744 /**************************************************************************
745 * RtlUnicodeStringToAnsiString (NTDLL.@)
747 * Converts a unicode string to an ansi string.
750 * Success: STATUS_SUCCESS. ansi contains the converted string
751 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
752 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
755 * This function always writes a terminating '\0'.
756 * It performs a partial copy if ansi is too small.
758 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
759 STRING
*ansi
, /* [I/O] Destination for the ansi string */
760 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
761 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
763 NTSTATUS ret
= STATUS_SUCCESS
;
764 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
766 ansi
->Length
= len
- 1;
769 ansi
->MaximumLength
= len
;
770 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
771 return STATUS_NO_MEMORY
;
773 else if (ansi
->MaximumLength
< len
)
775 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
776 ansi
->Length
= ansi
->MaximumLength
- 1;
777 ret
= STATUS_BUFFER_OVERFLOW
;
780 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
781 ansi
->Buffer
[ansi
->Length
] = 0;
786 /**************************************************************************
787 * RtlUnicodeStringToOemString (NTDLL.@)
789 * Converts a Rtl Unicode string to an OEM string.
792 * oem [O] Destination for OEM string
793 * uni [I] Source Unicode string
794 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
797 * Success: STATUS_SUCCESS. oem contains the converted string
798 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
799 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
802 * If doalloc is TRUE, the length allocated is uni->Length + 1.
803 * This function always '\0' terminates the string returned.
805 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
806 const UNICODE_STRING
*uni
,
809 NTSTATUS ret
= STATUS_SUCCESS
;
810 DWORD len
= RtlUnicodeStringToOemSize( uni
);
812 oem
->Length
= len
- 1;
815 oem
->MaximumLength
= len
;
816 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
817 return STATUS_NO_MEMORY
;
819 else if (oem
->MaximumLength
< len
)
821 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
822 oem
->Length
= oem
->MaximumLength
- 1;
823 ret
= STATUS_BUFFER_OVERFLOW
;
826 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
827 oem
->Buffer
[oem
->Length
] = 0;
832 /**************************************************************************
833 * RtlMultiByteToUnicodeN (NTDLL.@)
835 * Converts a multi-byte string to a Unicode string.
841 * Performs a partial copy if dst is too small.
843 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
844 LPCSTR src
, DWORD srclen
)
847 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
849 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
850 return STATUS_SUCCESS
;
854 /**************************************************************************
855 * RtlOemToUnicodeN (NTDLL.@)
857 * Converts a multi-byte string in the OEM code page to a Unicode string.
862 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
863 LPCSTR src
, DWORD srclen
)
865 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
867 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
868 return STATUS_SUCCESS
;
872 /**************************************************************************
873 * RtlUnicodeToMultiByteN (NTDLL.@)
875 * Converts a Unicode string to a multi-byte string in the ANSI code page.
880 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
881 LPCWSTR src
, DWORD srclen
)
883 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
884 dst
, dstlen
, NULL
, NULL
);
886 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
887 return STATUS_SUCCESS
;
891 /**************************************************************************
892 * RtlUnicodeToOemN (NTDLL.@)
894 * Converts a Unicode string to a multi-byte string in the OEM code page.
899 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
900 LPCWSTR src
, DWORD srclen
)
902 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
903 dst
, dstlen
, NULL
, NULL
);
905 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
906 return STATUS_SUCCESS
;
915 /**************************************************************************
916 * RtlUpperChar (NTDLL.@)
918 * Converts an Ascii character to uppercase.
921 * ch [I] Character to convert
924 * The uppercase character value.
927 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
928 * All other input characters are returned unchanged. The locale and
929 * multibyte characters are not taken into account (as native DLL).
931 CHAR WINAPI
RtlUpperChar( CHAR ch
)
933 if (ch
>= 'a' && ch
<= 'z') {
934 return ch
- 'a' + 'A';
941 /**************************************************************************
942 * RtlUpperString (NTDLL.@)
944 * Converts an Ascii string to uppercase.
947 * dst [O] Destination for converted string
948 * src [I] Source string to convert
954 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
955 * All other src characters are copied unchanged to dst. The locale and
956 * multibyte characters are not taken into account (as native DLL).
957 * The number of character copied is the minimum of src->Length and
958 * the dst->MaximumLength.
960 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
962 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
964 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
969 /**************************************************************************
970 * RtlUpcaseUnicodeChar (NTDLL.@)
972 * Converts a Unicode character to uppercase.
975 * wch [I] Character to convert
978 * The uppercase character value.
980 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
982 return toupperW(wch
);
986 /**************************************************************************
987 * RtlDowncaseUnicodeChar (NTDLL.@)
989 * Converts a Unicode character to lowercase.
992 * wch [I] Character to convert
995 * The lowercase character value.
997 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
999 return tolowerW(wch
);
1003 /**************************************************************************
1004 * RtlUpcaseUnicodeString (NTDLL.@)
1006 * Converts a Unicode string to uppercase.
1009 * dest [O] Destination for converted string
1010 * src [I] Source string to convert
1011 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1014 * Success: STATUS_SUCCESS. dest contains the converted string.
1015 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1016 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1019 * dest is never '\0' terminated because it may be equal to src, and src
1020 * might not be '\0' terminated. dest->Length is only set upon success.
1022 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
1023 const UNICODE_STRING
*src
,
1026 DWORD i
, len
= src
->Length
;
1030 dest
->MaximumLength
= len
;
1031 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1032 return STATUS_NO_MEMORY
;
1034 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1036 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
1038 return STATUS_SUCCESS
;
1042 /**************************************************************************
1043 * RtlDowncaseUnicodeString (NTDLL.@)
1045 * Converts a Unicode string to lowercase.
1048 * dest [O] Destination for converted string
1049 * src [I] Source string to convert
1050 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1053 * Success: STATUS_SUCCESS. dest contains the converted string.
1054 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1055 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1058 * dest is never '\0' terminated because it may be equal to src, and src
1059 * might not be '\0' terminated. dest->Length is only set upon success.
1061 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
1062 UNICODE_STRING
*dest
,
1063 const UNICODE_STRING
*src
,
1067 DWORD len
= src
->Length
;
1070 dest
->MaximumLength
= len
;
1071 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
1072 return STATUS_NO_MEMORY
;
1074 } else if (len
> dest
->MaximumLength
) {
1075 return STATUS_BUFFER_OVERFLOW
;
1078 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
1079 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
1082 return STATUS_SUCCESS
;
1086 /**************************************************************************
1087 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1089 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1095 * writes terminating 0
1097 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1098 const UNICODE_STRING
*src
,
1102 UNICODE_STRING upcase
;
1104 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1106 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1107 RtlFreeUnicodeString( &upcase
);
1113 /**************************************************************************
1114 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1116 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1117 * stored in STRING format.
1123 * writes terminating 0
1125 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1126 const UNICODE_STRING
*src
,
1130 UNICODE_STRING upcase
;
1132 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1134 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1135 RtlFreeUnicodeString( &upcase
);
1141 /**************************************************************************
1142 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1144 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1145 * stored in STRING format.
1151 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1153 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1154 const UNICODE_STRING
*uni
,
1158 UNICODE_STRING upcase
;
1161 upcase
.Buffer
= tmp
;
1162 upcase
.MaximumLength
= sizeof(tmp
);
1163 ret
= RtlUpcaseUnicodeString( &upcase
, uni
, FALSE
);
1164 if (ret
== STATUS_BUFFER_OVERFLOW
) ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
);
1168 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1172 oem
->MaximumLength
= len
;
1173 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1175 ret
= STATUS_NO_MEMORY
;
1179 else if (oem
->MaximumLength
< len
)
1181 ret
= STATUS_BUFFER_OVERFLOW
;
1182 oem
->Length
= oem
->MaximumLength
;
1183 if (!oem
->MaximumLength
) goto done
;
1185 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1187 if (upcase
.Buffer
!= tmp
) RtlFreeUnicodeString( &upcase
);
1193 /**************************************************************************
1194 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1196 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1201 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1202 LPCWSTR src
, DWORD srclen
)
1208 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1209 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1210 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1211 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1216 /**************************************************************************
1217 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1219 * Converts a Unicode string to the equivalent OEM upper-case representation.
1224 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1225 LPCWSTR src
, DWORD srclen
)
1231 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1232 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1233 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1234 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1244 /**************************************************************************
1245 * RtlOemStringToUnicodeSize (NTDLL.@)
1246 * RtlxOemStringToUnicodeSize (NTDLL.@)
1248 * Calculate the size in bytes necessary for the Unicode conversion of str,
1249 * including the terminating '\0'.
1252 * str [I] String to calculate the size of
1255 * The calculated size.
1257 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1259 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1260 return (ret
+ 1) * sizeof(WCHAR
);
1264 /**************************************************************************
1265 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1266 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1268 * Calculate the size in bytes necessary for the Unicode conversion of str,
1269 * including the terminating '\0'.
1272 * str [I] String to calculate the size of
1275 * The calculated size.
1277 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1280 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1281 return ret
+ sizeof(WCHAR
);
1285 /**************************************************************************
1286 * RtlMultiByteToUnicodeSize (NTDLL.@)
1288 * Compute the size in bytes necessary for the Unicode conversion of str,
1289 * without the terminating '\0'.
1292 * size [O] Destination for size
1293 * str [I] String to calculate the size of
1294 * len [I] Length of str
1299 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1301 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1302 return STATUS_SUCCESS
;
1306 /**************************************************************************
1307 * RtlUnicodeToMultiByteSize (NTDLL.@)
1309 * Calculate the size in bytes necessary for the multibyte conversion of str,
1310 * without the terminating '\0'.
1313 * size [O] Destination for size
1314 * str [I] String to calculate the size of
1315 * len [I] Length of str
1320 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1322 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1323 return STATUS_SUCCESS
;
1327 /**************************************************************************
1328 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1329 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1331 * Calculate the size in bytes necessary for the Ansi conversion of str,
1332 * including the terminating '\0'.
1335 * str [I] String to calculate the size of
1338 * The calculated size.
1340 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1343 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1348 /**************************************************************************
1349 * RtlUnicodeStringToOemSize (NTDLL.@)
1350 * RtlxUnicodeStringToOemSize (NTDLL.@)
1352 * Calculate the size in bytes necessary for the OEM conversion of str,
1353 * including the terminating '\0'.
1356 * str [I] String to calculate the size of
1359 * The calculated size.
1361 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1363 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1364 NULL
, 0, NULL
, NULL
) + 1;
1368 /**************************************************************************
1369 * RtlAppendAsciizToString (NTDLL.@)
1371 * Concatenates a buffered character string and a '\0' terminated character
1375 * Success: STATUS_SUCCESS. src is appended to dest.
1376 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1377 * to hold the concatenated string.
1380 * if src is NULL dest is unchanged.
1381 * dest is never '\0' terminated.
1383 NTSTATUS WINAPI
RtlAppendAsciizToString(
1384 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1385 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1388 unsigned int src_len
= strlen(src
);
1389 unsigned int dest_len
= src_len
+ dest
->Length
;
1391 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1392 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1393 dest
->Length
= dest_len
;
1395 return STATUS_SUCCESS
;
1399 /**************************************************************************
1400 * RtlAppendStringToString (NTDLL.@)
1402 * Concatenates two buffered character strings
1405 * Success: STATUS_SUCCESS. src is appended to dest.
1406 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1407 * to hold the concatenated string.
1410 * if src->length is zero dest is unchanged.
1411 * dest is never '\0' terminated.
1413 NTSTATUS WINAPI
RtlAppendStringToString(
1414 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1415 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1417 if (src
->Length
!= 0) {
1418 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1420 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1421 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1422 dest
->Length
= dest_len
;
1424 return STATUS_SUCCESS
;
1428 /**************************************************************************
1429 * RtlAppendUnicodeToString (NTDLL.@)
1431 * Concatenates a buffered unicode string and a '\0' terminated unicode
1435 * Success: STATUS_SUCCESS. src is appended to dest.
1436 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1437 * to hold the concatenated string.
1440 * if src is NULL dest is unchanged.
1441 * dest is '\0' terminated when the MaximumLength allows it.
1442 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1445 * Does not write in the src->Buffer beyond MaximumLength when
1446 * MaximumLength is odd as the native function does.
1448 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1449 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1450 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1453 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1454 unsigned int dest_len
= src_len
+ dest
->Length
;
1456 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1457 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1458 dest
->Length
= dest_len
;
1459 /* append terminating '\0' if enough space */
1460 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1461 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1464 return STATUS_SUCCESS
;
1468 /**************************************************************************
1469 * RtlAppendUnicodeStringToString (NTDLL.@)
1471 * Concatenates two buffered unicode strings
1474 * Success: STATUS_SUCCESS. src is appended to dest.
1475 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1476 * to hold the concatenated string.
1479 * if src->length is zero dest is unchanged.
1480 * dest is '\0' terminated when the MaximumLength allows it.
1481 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1484 * Does not write in the src->Buffer beyond MaximumLength when
1485 * MaximumLength is odd as the native function does.
1487 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1488 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1489 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1491 if (src
->Length
!= 0) {
1492 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1494 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1495 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1496 dest
->Length
= dest_len
;
1497 /* append terminating '\0' if enough space */
1498 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1499 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1502 return STATUS_SUCCESS
;
1506 /**************************************************************************
1507 * RtlFindCharInUnicodeString (NTDLL.@)
1509 * Searches for one of several unicode characters in a unicode string.
1512 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1513 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1515 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1516 int flags
, /* [I] Flags */
1517 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1518 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1519 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1521 unsigned int main_idx
, search_idx
;
1525 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1526 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1527 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1528 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1529 return STATUS_SUCCESS
;
1534 return STATUS_NOT_FOUND
;
1536 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1537 while (main_idx
-- > 0) {
1538 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1539 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1540 *pos
= main_idx
* sizeof(WCHAR
);
1541 return STATUS_SUCCESS
;
1546 return STATUS_NOT_FOUND
;
1548 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1550 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1551 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1554 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1555 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1556 return STATUS_SUCCESS
;
1560 return STATUS_NOT_FOUND
;
1562 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1563 while (main_idx
-- > 0) {
1565 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1566 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1569 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1570 *pos
= main_idx
* sizeof(WCHAR
);
1571 return STATUS_SUCCESS
;
1575 return STATUS_NOT_FOUND
;
1577 return STATUS_NOT_FOUND
;
1585 /**************************************************************************
1586 * RtlIsTextUnicode (NTDLL.@)
1588 * Attempt to guess whether a text buffer is Unicode.
1591 * buf [I] Text buffer to test
1592 * len [I] Length of buf
1593 * pf [O] Destination for test results
1596 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1599 * Should implement more tests.
1601 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1603 static const WCHAR std_control_chars
[] = {'\r','\n','\t',' ',0x3000,0};
1604 static const WCHAR byterev_control_chars
[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1605 const WCHAR
*s
= buf
;
1607 unsigned int flags
= ~0U, out_flags
= 0;
1609 if (len
< sizeof(WCHAR
))
1611 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1618 * Apply various tests to the text string. According to the
1619 * docs, each test "passed" sets the corresponding flag in
1620 * the output flags. But some of the tests are mutually
1621 * exclusive, so I don't see how you could pass all tests ...
1624 /* Check for an odd length ... pass if even. */
1625 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1627 if (((const char *)buf
)[len
- 1] == 0)
1628 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1630 len
/= sizeof(WCHAR
);
1631 /* Windows only checks the first 256 characters */
1632 if (len
> 256) len
= 256;
1634 /* Check for the special byte order unicode marks. */
1635 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1636 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1638 /* apply some statistical analysis */
1639 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1642 /* FIXME: checks only for ASCII characters in the unicode stream */
1643 for (i
= 0; i
< len
; i
++)
1645 if (s
[i
] <= 255) stats
++;
1647 if (stats
> len
/ 2)
1648 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1651 /* Check for unicode NULL chars */
1652 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1654 for (i
= 0; i
< len
; i
++)
1656 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1658 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1664 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1666 for (i
= 0; i
< len
; i
++)
1668 if (strchrW(std_control_chars
, s
[i
]))
1670 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1676 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1678 for (i
= 0; i
< len
; i
++)
1680 if (strchrW(byterev_control_chars
, s
[i
]))
1682 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1693 /* check for flags that indicate it's definitely not valid Unicode */
1694 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1695 /* now check for invalid ASCII, and assume Unicode if so */
1696 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1697 /* now check for Unicode flags */
1698 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1704 /**************************************************************************
1705 * RtlCharToInteger (NTDLL.@)
1707 * Converts a character string into its integer equivalent.
1710 * Success: STATUS_SUCCESS. value contains the converted number
1711 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1712 * STATUS_ACCESS_VIOLATION, if value is NULL.
1715 * For base 0 it uses 10 as base and the string should be in the format
1716 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1717 * For other bases the string should be in the format
1718 * "{whitespace} [+|-] {digits}".
1719 * No check is made for value overflow, only the lower 32 bits are assigned.
1720 * If str is NULL it crashes, as the native function does.
1723 * This function does not read garbage behind '\0' as the native version does.
1725 NTSTATUS WINAPI
RtlCharToInteger(
1726 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1727 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1728 ULONG
*value
) /* [O] Destination for the converted value */
1732 ULONG RunningTotal
= 0;
1733 BOOL bMinus
= FALSE
;
1735 while (*str
!= '\0' && *str
<= ' ') {
1741 } else if (*str
== '-') {
1748 if (str
[0] == '0') {
1749 if (str
[1] == 'b') {
1752 } else if (str
[1] == 'o') {
1755 } else if (str
[1] == 'x') {
1760 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1761 return STATUS_INVALID_PARAMETER
;
1764 if (value
== NULL
) {
1765 return STATUS_ACCESS_VIOLATION
;
1768 while (*str
!= '\0') {
1770 if (chCurrent
>= '0' && chCurrent
<= '9') {
1771 digit
= chCurrent
- '0';
1772 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1773 digit
= chCurrent
- 'A' + 10;
1774 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1775 digit
= chCurrent
- 'a' + 10;
1779 if (digit
< 0 || digit
>= base
) {
1780 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1781 return STATUS_SUCCESS
;
1784 RunningTotal
= RunningTotal
* base
+ digit
;
1788 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1789 return STATUS_SUCCESS
;
1793 /**************************************************************************
1794 * RtlIntegerToChar (NTDLL.@)
1796 * Converts an unsigned integer to a character string.
1799 * Success: STATUS_SUCCESS. str contains the converted number
1800 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1801 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1802 * STATUS_ACCESS_VIOLATION, if str is NULL.
1805 * Instead of base 0 it uses 10 as base.
1806 * Writes at most length characters to the string str.
1807 * Str is '\0' terminated when length allows it.
1808 * When str fits exactly in length characters the '\0' is omitted.
1810 NTSTATUS WINAPI
RtlIntegerToChar(
1811 ULONG value
, /* [I] Value to be converted */
1812 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1813 ULONG length
, /* [I] Length of the str buffer in bytes */
1814 PCHAR str
) /* [O] Destination for the converted value */
1823 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1824 return STATUS_INVALID_PARAMETER
;
1832 digit
= value
% base
;
1833 value
= value
/ base
;
1837 *pos
= 'A' + digit
- 10;
1839 } while (value
!= 0L);
1841 len
= &buffer
[32] - pos
;
1843 return STATUS_BUFFER_OVERFLOW
;
1844 } else if (str
== NULL
) {
1845 return STATUS_ACCESS_VIOLATION
;
1846 } else if (len
== length
) {
1847 memcpy(str
, pos
, len
);
1849 memcpy(str
, pos
, len
+ 1);
1851 return STATUS_SUCCESS
;
1855 /**************************************************************************
1856 * RtlUnicodeStringToInteger (NTDLL.@)
1858 * Converts a unicode string into its integer equivalent.
1861 * Success: STATUS_SUCCESS. value contains the converted number
1862 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1863 * STATUS_ACCESS_VIOLATION, if value is NULL.
1866 * For base 0 it uses 10 as base and the string should be in the format
1867 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1868 * For other bases the string should be in the format
1869 * "{whitespace} [+|-] {digits}".
1870 * No check is made for value overflow, only the lower 32 bits are assigned.
1871 * If str is NULL it crashes, as the native function does.
1874 * This function does not read garbage on string length 0 as the native
1877 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1878 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1879 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1880 ULONG
*value
) /* [O] Destination for the converted value */
1882 LPWSTR lpwstr
= str
->Buffer
;
1883 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1886 ULONG RunningTotal
= 0;
1887 BOOL bMinus
= FALSE
;
1889 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1894 if (CharsRemaining
>= 1) {
1895 if (*lpwstr
== '+') {
1898 } else if (*lpwstr
== '-') {
1907 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1908 if (lpwstr
[1] == 'b') {
1910 CharsRemaining
-= 2;
1912 } else if (lpwstr
[1] == 'o') {
1914 CharsRemaining
-= 2;
1916 } else if (lpwstr
[1] == 'x') {
1918 CharsRemaining
-= 2;
1922 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1923 return STATUS_INVALID_PARAMETER
;
1926 if (value
== NULL
) {
1927 return STATUS_ACCESS_VIOLATION
;
1930 while (CharsRemaining
>= 1) {
1931 wchCurrent
= *lpwstr
;
1932 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1933 digit
= wchCurrent
- '0';
1934 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1935 digit
= wchCurrent
- 'A' + 10;
1936 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1937 digit
= wchCurrent
- 'a' + 10;
1941 if (digit
< 0 || digit
>= base
) {
1942 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1943 return STATUS_SUCCESS
;
1946 RunningTotal
= RunningTotal
* base
+ digit
;
1951 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1952 return STATUS_SUCCESS
;
1956 /**************************************************************************
1957 * RtlIntegerToUnicodeString (NTDLL.@)
1959 * Converts an unsigned integer to a '\0' terminated unicode string.
1962 * Success: STATUS_SUCCESS. str contains the converted number
1963 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1964 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1965 * (with the '\0' termination). In this case str->Length
1966 * is set to the length, the string would have (which can
1967 * be larger than the MaximumLength).
1970 * Instead of base 0 it uses 10 as base.
1971 * If str is NULL it crashes, as the native function does.
1974 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1975 * The native function does this when the string would be longer than 16
1976 * characters even when the string parameter is long enough.
1978 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1979 ULONG value
, /* [I] Value to be converted */
1980 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1981 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1989 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1990 return STATUS_INVALID_PARAMETER
;
1998 digit
= value
% base
;
1999 value
= value
/ base
;
2003 *pos
= 'A' + digit
- 10;
2005 } while (value
!= 0L);
2007 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
2008 if (str
->Length
>= str
->MaximumLength
) {
2009 return STATUS_BUFFER_OVERFLOW
;
2011 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
2013 return STATUS_SUCCESS
;
2017 /*************************************************************************
2018 * RtlGUIDFromString (NTDLL.@)
2020 * Convert a string representation of a GUID into a GUID.
2023 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
2024 * guid [O] Destination for the converted GUID
2027 * Success: STATUS_SUCCESS. guid contains the converted value.
2028 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
2031 * See RtlStringFromGUID.
2033 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
2036 const WCHAR
*lpszCLSID
= str
->Buffer
;
2037 BYTE
* lpOut
= (BYTE
*)guid
;
2039 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
2041 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2042 * to memory: DWORD... WORD WORD BYTES............
2049 if (*lpszCLSID
!= '{')
2050 return STATUS_INVALID_PARAMETER
;
2053 case 9: case 14: case 19: case 24:
2054 if (*lpszCLSID
!= '-')
2055 return STATUS_INVALID_PARAMETER
;
2059 if (*lpszCLSID
!= '}')
2060 return STATUS_INVALID_PARAMETER
;
2065 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
2068 /* Read two hex digits as a byte value */
2069 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
2070 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
2071 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
2072 else return STATUS_INVALID_PARAMETER
;
2074 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
2075 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
2076 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
2077 else return STATUS_INVALID_PARAMETER
;
2079 byte
= ch
<< 4 | ch2
;
2083 #ifndef WORDS_BIGENDIAN
2084 /* For Big Endian machines, we store the data such that the
2085 * dword/word members can be read as DWORDS and WORDS correctly. */
2087 case 1: lpOut
[3] = byte
; break;
2088 case 3: lpOut
[2] = byte
; break;
2089 case 5: lpOut
[1] = byte
; break;
2090 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
2092 case 10: case 15: lpOut
[1] = byte
; break;
2093 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
2096 default: lpOut
[0] = byte
; lpOut
++; break;
2098 lpszCLSID
++; /* Skip 2nd character of byte */
2106 return STATUS_SUCCESS
;
2109 /*************************************************************************
2110 * RtlStringFromGUID (NTDLL.@)
2112 * Convert a GUID into a string representation of a GUID.
2115 * guid [I] GUID to convert
2116 * str [O] Destination for the converted string
2119 * Success: STATUS_SUCCESS. str contains the converted value.
2120 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2123 * See RtlGUIDFromString.
2125 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2127 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2128 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2129 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2130 '%','0','2','X','%','0','2','X','}','\0' };
2132 TRACE("(%p,%p)\n", guid
, str
);
2134 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
2135 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
2136 str
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
2139 str
->Length
= str
->MaximumLength
= 0;
2140 return STATUS_NO_MEMORY
;
2142 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2143 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2144 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2146 return STATUS_SUCCESS
;
2149 /******************************************************************************
2150 * RtlHashUnicodeString [NTDLL.@]
2152 NTSTATUS WINAPI
RtlHashUnicodeString(PCUNICODE_STRING string
, BOOLEAN case_insensitive
, ULONG alg
, ULONG
*hash
)
2156 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
2160 case HASH_STRING_ALGORITHM_DEFAULT
:
2161 case HASH_STRING_ALGORITHM_X65599
:
2164 return STATUS_INVALID_PARAMETER
;
2168 for (i
= 0; i
< string
->Length
/sizeof(WCHAR
); i
++)
2169 *hash
= *hash
*65599 + (case_insensitive
? toupperW(string
->Buffer
[i
]) : string
->Buffer
[i
]);
2171 return STATUS_SUCCESS
;