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 __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
58 const union cptable
*ucp
)
63 NlsAnsiCodePage
= ansi
->info
.codepage
;
66 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
69 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
70 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
73 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
74 const char* defchar
, int *used
)
77 return wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
);
78 if (used
) *used
= 0; /* all chars are valid for UTF-8 */
79 return wine_utf8_wcstombs( flags
, src
, srclen
, dst
, dstlen
);
82 /**************************************************************************
83 * RtlInitAnsiString (NTDLL.@)
85 * Initializes a buffered ansi string.
91 * Assigns source to target->Buffer. The length of source is assigned to
92 * target->Length and target->MaximumLength. If source is NULL the length
93 * of source is assumed to be 0.
95 void WINAPI
RtlInitAnsiString(
96 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
97 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
99 if ((target
->Buffer
= (PCHAR
) source
))
101 target
->Length
= strlen(source
);
102 target
->MaximumLength
= target
->Length
+ 1;
104 else target
->Length
= target
->MaximumLength
= 0;
107 /**************************************************************************
108 * RtlInitAnsiStringEx (NTDLL.@)
110 * Initializes a buffered ansi string.
113 * An appropriate NTSTATUS value.
116 * Assigns source to target->Buffer. The length of source is assigned to
117 * target->Length and target->MaximumLength. If source is NULL the length
118 * of source is assumed to be 0.
120 NTSTATUS WINAPI
RtlInitAnsiStringEx(PANSI_STRING target
, PCSZ source
)
124 unsigned int len
= strlen(source
);
126 return STATUS_NAME_TOO_LONG
;
128 target
->Buffer
= (PCHAR
) source
;
129 target
->Length
= len
;
130 target
->MaximumLength
= len
+ 1;
134 target
->Buffer
= NULL
;
136 target
->MaximumLength
= 0;
138 return STATUS_SUCCESS
;
141 /**************************************************************************
142 * RtlInitString (NTDLL.@)
144 * Initializes a buffered string.
150 * Assigns source to target->Buffer. The length of source is assigned to
151 * target->Length and target->MaximumLength. If source is NULL the length
152 * of source is assumed to be 0.
154 void WINAPI
RtlInitString(
155 PSTRING target
, /* [I/O] Buffered string to be initialized */
156 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
158 RtlInitAnsiString( target
, source
);
162 /**************************************************************************
163 * RtlFreeAnsiString (NTDLL.@)
165 void WINAPI
RtlFreeAnsiString( PSTRING str
)
169 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
170 RtlZeroMemory( str
, sizeof(*str
) );
175 /**************************************************************************
176 * RtlFreeOemString (NTDLL.@)
178 void WINAPI
RtlFreeOemString( PSTRING str
)
180 RtlFreeAnsiString( str
);
184 /**************************************************************************
185 * RtlCopyString (NTDLL.@)
187 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
191 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
192 memcpy( dst
->Buffer
, src
->Buffer
, len
);
195 else dst
->Length
= 0;
199 /**************************************************************************
200 * RtlInitUnicodeString (NTDLL.@)
202 * Initializes a buffered unicode string.
208 * Assigns source to target->Buffer. The length of source is assigned to
209 * target->Length and target->MaximumLength. If source is NULL the length
210 * of source is assumed to be 0.
212 void WINAPI
RtlInitUnicodeString(
213 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
214 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
216 if ((target
->Buffer
= (PWSTR
) source
))
218 unsigned int length
= strlenW(source
) * sizeof(WCHAR
);
221 target
->Length
= length
;
222 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
224 else target
->Length
= target
->MaximumLength
= 0;
228 /**************************************************************************
229 * RtlInitUnicodeStringEx (NTDLL.@)
231 * Initializes a buffered unicode string.
234 * Success: STATUS_SUCCESS. target is initialized.
235 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
238 * Assigns source to target->Buffer. The length of source is assigned to
239 * target->Length and target->MaximumLength. If source is NULL the length
240 * of source is assumed to be 0.
242 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
243 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
244 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
246 if (source
!= NULL
) {
247 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
250 return STATUS_NAME_TOO_LONG
;
252 target
->Length
= len
;
253 target
->MaximumLength
= len
+ sizeof(WCHAR
);
254 target
->Buffer
= (PWSTR
) source
;
258 target
->MaximumLength
= 0;
259 target
->Buffer
= NULL
;
261 return STATUS_SUCCESS
;
265 /**************************************************************************
266 * RtlCreateUnicodeString (NTDLL.@)
268 * Creates a UNICODE_STRING from a null-terminated Unicode string.
274 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
276 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
277 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
278 memcpy( target
->Buffer
, src
, len
);
279 target
->MaximumLength
= len
;
280 target
->Length
= len
- sizeof(WCHAR
);
285 /**************************************************************************
286 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
288 * Creates a UNICODE_STRING from a null-terminated Ascii string.
294 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
297 RtlInitAnsiString( &ansi
, src
);
298 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
302 /**************************************************************************
303 * RtlFreeUnicodeString (NTDLL.@)
305 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
306 * RtlCreateUnicodeStringFromAsciiz().
311 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
315 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
316 RtlZeroMemory( str
, sizeof(*str
) );
321 /**************************************************************************
322 * RtlCopyUnicodeString (NTDLL.@)
324 * Copies from one UNICODE_STRING to another.
329 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
333 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
334 memcpy( dst
->Buffer
, src
->Buffer
, len
);
336 /* append terminating '\0' if enough space */
337 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
339 else dst
->Length
= 0;
343 /**************************************************************************
344 * RtlDuplicateUnicodeString (NTDLL.@)
346 * Duplicates an unicode string.
349 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
350 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
351 * STATUS_NO_MEMORY, if the allocation fails.
354 * For add_nul there are several possible values:
355 * 0 = destination will not be '\0' terminated,
356 * 1 = destination will be '\0' terminated,
357 * 3 = like 1 but for an empty source string produce '\0' terminated empty
358 * Buffer instead of assigning NULL to the Buffer.
359 * Other add_nul values are invalid.
361 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
362 int add_nul
, /* [I] flag */
363 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
364 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
366 if (source
== NULL
|| destination
== NULL
||
367 source
->Length
> source
->MaximumLength
||
368 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
369 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
370 return STATUS_INVALID_PARAMETER
;
372 if (source
->Length
== 0 && add_nul
!= 3) {
373 destination
->Length
= 0;
374 destination
->MaximumLength
= 0;
375 destination
->Buffer
= NULL
;
377 unsigned int destination_max_len
= source
->Length
;
380 destination_max_len
+= sizeof(WCHAR
);
382 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
383 if (destination
->Buffer
== NULL
) {
384 return STATUS_NO_MEMORY
;
386 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
387 destination
->Length
= source
->Length
;
388 destination
->MaximumLength
= source
->Length
;
389 /* append terminating '\0' if enough space */
391 destination
->MaximumLength
= destination_max_len
;
392 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
397 return STATUS_SUCCESS
;
401 /**************************************************************************
402 * RtlEraseUnicodeString (NTDLL.@)
404 * Overwrites a UNICODE_STRING with zeros.
409 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
413 memset( str
->Buffer
, 0, str
->MaximumLength
);
424 /******************************************************************************
425 * RtlCompareString (NTDLL.@)
427 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
433 len
= min(s1
->Length
, s2
->Length
);
439 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
443 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
445 if (!ret
) ret
= s1
->Length
- s2
->Length
;
450 /******************************************************************************
451 * RtlCompareUnicodeString (NTDLL.@)
453 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
454 BOOLEAN CaseInsensitive
)
460 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
466 while (!ret
&& len
--) ret
= toupperW(*p1
++) - toupperW(*p2
++);
470 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
472 if (!ret
) ret
= s1
->Length
- s2
->Length
;
477 /**************************************************************************
478 * RtlEqualString (NTDLL.@)
480 * Determine if two strings are equal.
483 * s1 [I] Source string
484 * s2 [I] String to compare to s1
485 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
488 * Non-zero if s1 is equal to s2, 0 otherwise.
490 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
492 if (s1
->Length
!= s2
->Length
) return FALSE
;
493 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
497 /**************************************************************************
498 * RtlEqualUnicodeString (NTDLL.@)
500 * Unicode version of RtlEqualString.
502 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
503 BOOLEAN CaseInsensitive
)
505 if (s1
->Length
!= s2
->Length
) return FALSE
;
506 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
510 /**************************************************************************
511 * RtlPrefixString (NTDLL.@)
513 * Determine if one string is a prefix of another.
516 * s1 [I] Prefix to look for in s2
517 * s2 [I] String that may contain s1 as a prefix
518 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
521 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
523 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
527 if (s1
->Length
> s2
->Length
) return FALSE
;
530 for (i
= 0; i
< s1
->Length
; i
++)
531 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
535 for (i
= 0; i
< s1
->Length
; i
++)
536 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
542 /**************************************************************************
543 * RtlPrefixUnicodeString (NTDLL.@)
545 * Unicode version of RtlPrefixString.
547 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
548 const UNICODE_STRING
*s2
,
549 BOOLEAN ignore_case
)
553 if (s1
->Length
> s2
->Length
) return FALSE
;
556 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
557 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
561 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
562 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
568 /**************************************************************************
569 * RtlEqualComputerName (NTDLL.@)
571 * Determine if two computer names are the same.
574 * left [I] First computer name
575 * right [I] Second computer name
578 * 0 if the names are equal, non-zero otherwise.
581 * The comparison is case insensitive.
583 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
584 const UNICODE_STRING
*right
)
587 STRING upLeft
, upRight
;
589 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
591 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
593 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
594 RtlFreeOemString( &upRight
);
596 RtlFreeOemString( &upLeft
);
602 /**************************************************************************
603 * RtlEqualDomainName (NTDLL.@)
605 * Determine if two domain names are the same.
608 * left [I] First domain name
609 * right [I] Second domain name
612 * 0 if the names are equal, non-zero otherwise.
615 * The comparison is case insensitive.
617 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
618 const UNICODE_STRING
*right
)
620 return RtlEqualComputerName(left
, right
);
624 /**************************************************************************
625 * RtlAnsiCharToUnicodeChar (NTDLL.@)
627 * Converts the first ansi character to a unicode character.
630 * ansi [I/O] Pointer to the ansi string.
633 * Unicode representation of the first character in the ansi string.
636 * Upon successful completion, the char pointer ansi points to is
637 * incremented by the size of the character.
639 WCHAR WINAPI
RtlAnsiCharToUnicodeChar(LPSTR
*ansi
)
642 DWORD charSize
= sizeof(CHAR
);
644 if (wine_is_dbcs_leadbyte(ansi_table
, **ansi
))
647 RtlMultiByteToUnicodeN(&str
, sizeof(WCHAR
), NULL
, *ansi
, charSize
);
654 COPY BETWEEN ANSI_STRING or UNICODE_STRING
655 there is no parameter checking, it just crashes
659 /**************************************************************************
660 * RtlAnsiStringToUnicodeString (NTDLL.@)
662 * Converts an ansi string to an unicode string.
665 * Success: STATUS_SUCCESS. uni contains the converted string
666 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
667 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
668 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
671 * This function always writes a terminating '\0'.
673 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
674 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
675 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
676 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
678 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
680 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
681 uni
->Length
= total
- sizeof(WCHAR
);
684 uni
->MaximumLength
= total
;
685 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
686 return STATUS_NO_MEMORY
;
688 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
690 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
691 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
692 return STATUS_SUCCESS
;
696 /**************************************************************************
697 * RtlOemStringToUnicodeString (NTDLL.@)
699 * Converts an oem string to an unicode string.
702 * Success: STATUS_SUCCESS. uni contains the converted string
703 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
704 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
705 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
708 * This function always writes a terminating '\0'.
710 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
711 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
712 const STRING
*oem
, /* [I] Oem string to be converted */
713 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
715 DWORD total
= RtlOemStringToUnicodeSize( oem
);
717 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
718 uni
->Length
= total
- sizeof(WCHAR
);
721 uni
->MaximumLength
= total
;
722 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
723 return STATUS_NO_MEMORY
;
725 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
727 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
728 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
729 return STATUS_SUCCESS
;
733 /**************************************************************************
734 * RtlUnicodeStringToAnsiString (NTDLL.@)
736 * Converts an unicode string to an ansi string.
739 * Success: STATUS_SUCCESS. ansi contains the converted string
740 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
741 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
744 * This function always writes a terminating '\0'.
745 * It performs a partial copy if ansi is too small.
747 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
748 STRING
*ansi
, /* [I/O] Destination for the ansi string */
749 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
750 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
752 NTSTATUS ret
= STATUS_SUCCESS
;
753 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
755 ansi
->Length
= len
- 1;
758 ansi
->MaximumLength
= len
;
759 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
760 return STATUS_NO_MEMORY
;
762 else if (ansi
->MaximumLength
< len
)
764 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
765 ansi
->Length
= ansi
->MaximumLength
- 1;
766 ret
= STATUS_BUFFER_OVERFLOW
;
769 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
770 ansi
->Buffer
[ansi
->Length
] = 0;
775 /**************************************************************************
776 * RtlUnicodeStringToOemString (NTDLL.@)
778 * Converts a Rtl Unicode string to an OEM string.
781 * oem [O] Destination for OEM string
782 * uni [I] Source Unicode string
783 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
786 * Success: STATUS_SUCCESS. oem contains the converted string
787 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
788 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
791 * If doalloc is TRUE, the length allocated is uni->Length + 1.
792 * This function always '\0' terminates the string returned.
794 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
795 const UNICODE_STRING
*uni
,
798 NTSTATUS ret
= STATUS_SUCCESS
;
799 DWORD len
= RtlUnicodeStringToOemSize( uni
);
801 oem
->Length
= len
- 1;
804 oem
->MaximumLength
= len
;
805 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
806 return STATUS_NO_MEMORY
;
808 else if (oem
->MaximumLength
< len
)
810 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
811 oem
->Length
= oem
->MaximumLength
- 1;
812 ret
= STATUS_BUFFER_OVERFLOW
;
815 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
816 oem
->Buffer
[oem
->Length
] = 0;
821 /**************************************************************************
822 * RtlMultiByteToUnicodeN (NTDLL.@)
824 * Converts a multi-byte string to a Unicode string.
830 * Performs a partial copy if dst is too small.
832 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
833 LPCSTR src
, DWORD srclen
)
836 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
838 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
839 return STATUS_SUCCESS
;
843 /**************************************************************************
844 * RtlOemToUnicodeN (NTDLL.@)
846 * Converts a multi-byte string in the OEM code page to a Unicode string.
851 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
852 LPCSTR src
, DWORD srclen
)
854 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
856 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
857 return STATUS_SUCCESS
;
861 /**************************************************************************
862 * RtlUnicodeToMultiByteN (NTDLL.@)
864 * Converts a Unicode string to a multi-byte string in the ANSI code page.
869 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
870 LPCWSTR src
, DWORD srclen
)
872 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
873 dst
, dstlen
, NULL
, NULL
);
875 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
876 return STATUS_SUCCESS
;
880 /**************************************************************************
881 * RtlUnicodeToOemN (NTDLL.@)
883 * Converts a Unicode string to a multi-byte string in the OEM code page.
888 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
889 LPCWSTR src
, DWORD srclen
)
891 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
892 dst
, dstlen
, NULL
, NULL
);
894 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
895 return STATUS_SUCCESS
;
904 /**************************************************************************
905 * RtlUpperChar (NTDLL.@)
907 * Converts an Ascii character to uppercase.
910 * ch [I] Character to convert
913 * The uppercase character value.
916 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
917 * All other input characters are returned unchanged. The locale and
918 * multibyte characters are not taken into account (as native DLL).
920 CHAR WINAPI
RtlUpperChar( CHAR ch
)
922 if (ch
>= 'a' && ch
<= 'z') {
923 return ch
- 'a' + 'A';
930 /**************************************************************************
931 * RtlUpperString (NTDLL.@)
933 * Converts an Ascii string to uppercase.
936 * dst [O] Destination for converted string
937 * src [I] Source string to convert
943 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
944 * All other src characters are copied unchanged to dst. The locale and
945 * multibyte characters are not taken into account (as native DLL).
946 * The number of character copied is the minimum of src->Length and
947 * the dst->MaximumLength.
949 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
951 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
953 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
958 /**************************************************************************
959 * RtlUpcaseUnicodeChar (NTDLL.@)
961 * Converts an Unicode character to uppercase.
964 * wch [I] Character to convert
967 * The uppercase character value.
969 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
971 return toupperW(wch
);
975 /**************************************************************************
976 * RtlDowncaseUnicodeChar (NTDLL.@)
978 * Converts an Unicode character to lowercase.
981 * wch [I] Character to convert
984 * The lowercase character value.
986 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
988 return tolowerW(wch
);
992 /**************************************************************************
993 * RtlUpcaseUnicodeString (NTDLL.@)
995 * Converts an Unicode string to uppercase.
998 * dest [O] Destination for converted string
999 * src [I] Source string to convert
1000 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1003 * Success: STATUS_SUCCESS. dest contains the converted string.
1004 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1005 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1008 * dest is never '\0' terminated because it may be equal to src, and src
1009 * might not be '\0' terminated. dest->Length is only set upon success.
1011 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
1012 const UNICODE_STRING
*src
,
1015 DWORD i
, len
= src
->Length
;
1019 dest
->MaximumLength
= len
;
1020 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1021 return STATUS_NO_MEMORY
;
1023 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1025 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
1027 return STATUS_SUCCESS
;
1031 /**************************************************************************
1032 * RtlDowncaseUnicodeString (NTDLL.@)
1034 * Converts an Unicode string to lowercase.
1037 * dest [O] Destination for converted string
1038 * src [I] Source string to convert
1039 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
1042 * Success: STATUS_SUCCESS. dest contains the converted string.
1043 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
1044 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1047 * dest is never '\0' terminated because it may be equal to src, and src
1048 * might not be '\0' terminated. dest->Length is only set upon success.
1050 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
1051 UNICODE_STRING
*dest
,
1052 const UNICODE_STRING
*src
,
1056 DWORD len
= src
->Length
;
1059 dest
->MaximumLength
= len
;
1060 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
1061 return STATUS_NO_MEMORY
;
1063 } else if (len
> dest
->MaximumLength
) {
1064 return STATUS_BUFFER_OVERFLOW
;
1067 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
1068 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
1071 return STATUS_SUCCESS
;
1075 /**************************************************************************
1076 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1078 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1084 * writes terminating 0
1086 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1087 const UNICODE_STRING
*src
,
1091 UNICODE_STRING upcase
;
1093 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1095 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1096 RtlFreeUnicodeString( &upcase
);
1102 /**************************************************************************
1103 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1105 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1106 * stored in STRING format.
1112 * writes terminating 0
1114 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1115 const UNICODE_STRING
*src
,
1119 UNICODE_STRING upcase
;
1121 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1123 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1124 RtlFreeUnicodeString( &upcase
);
1130 /**************************************************************************
1131 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1133 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1134 * stored in STRING format.
1140 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1142 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1143 const UNICODE_STRING
*uni
,
1147 UNICODE_STRING upcase
;
1150 upcase
.Buffer
= tmp
;
1151 upcase
.MaximumLength
= sizeof(tmp
);
1152 ret
= RtlUpcaseUnicodeString( &upcase
, uni
, FALSE
);
1153 if (ret
== STATUS_BUFFER_OVERFLOW
) ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
);
1157 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1161 oem
->MaximumLength
= len
;
1162 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1164 ret
= STATUS_NO_MEMORY
;
1168 else if (oem
->MaximumLength
< len
)
1170 ret
= STATUS_BUFFER_OVERFLOW
;
1171 oem
->Length
= oem
->MaximumLength
;
1172 if (!oem
->MaximumLength
) goto done
;
1174 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1176 if (upcase
.Buffer
!= tmp
) RtlFreeUnicodeString( &upcase
);
1182 /**************************************************************************
1183 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1185 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1190 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1191 LPCWSTR src
, DWORD srclen
)
1197 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1198 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1199 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1200 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1205 /**************************************************************************
1206 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1208 * Converts a Unicode string to the equivalent OEM upper-case representation.
1213 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1214 LPCWSTR src
, DWORD srclen
)
1220 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1221 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1222 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1223 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1233 /**************************************************************************
1234 * RtlOemStringToUnicodeSize (NTDLL.@)
1235 * RtlxOemStringToUnicodeSize (NTDLL.@)
1237 * Calculate the size in bytes necessary for the Unicode conversion of str,
1238 * including the terminating '\0'.
1241 * str [I] String to calculate the size of
1244 * The calculated size.
1246 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1248 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1249 return (ret
+ 1) * sizeof(WCHAR
);
1253 /**************************************************************************
1254 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1255 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1257 * Calculate the size in bytes necessary for the Unicode conversion of str,
1258 * including the terminating '\0'.
1261 * str [I] String to calculate the size of
1264 * The calculated size.
1266 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1269 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1270 return ret
+ sizeof(WCHAR
);
1274 /**************************************************************************
1275 * RtlMultiByteToUnicodeSize (NTDLL.@)
1277 * Compute the size in bytes necessary for the Unicode conversion of str,
1278 * without the terminating '\0'.
1281 * size [O] Destination for size
1282 * str [I] String to calculate the size of
1283 * len [I] Length of str
1288 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1290 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1291 return STATUS_SUCCESS
;
1295 /**************************************************************************
1296 * RtlUnicodeToMultiByteSize (NTDLL.@)
1298 * Calculate the size in bytes necessary for the multibyte conversion of str,
1299 * without the terminating '\0'.
1302 * size [O] Destination for size
1303 * str [I] String to calculate the size of
1304 * len [I] Length of str
1309 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1311 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1312 return STATUS_SUCCESS
;
1316 /**************************************************************************
1317 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1318 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1320 * Calculate the size in bytes necessary for the Ansi conversion of str,
1321 * including the terminating '\0'.
1324 * str [I] String to calculate the size of
1327 * The calculated size.
1329 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1332 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1337 /**************************************************************************
1338 * RtlUnicodeStringToOemSize (NTDLL.@)
1339 * RtlxUnicodeStringToOemSize (NTDLL.@)
1341 * Calculate the size in bytes necessary for the OEM conversion of str,
1342 * including the terminating '\0'.
1345 * str [I] String to calculate the size of
1348 * The calculated size.
1350 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1352 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1353 NULL
, 0, NULL
, NULL
) + 1;
1357 /**************************************************************************
1358 * RtlAppendAsciizToString (NTDLL.@)
1360 * Concatenates a buffered character string and a '\0' terminated character
1364 * Success: STATUS_SUCCESS. src is appended to dest.
1365 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1366 * to hold the concatenated string.
1369 * if src is NULL dest is unchanged.
1370 * dest is never '\0' terminated.
1372 NTSTATUS WINAPI
RtlAppendAsciizToString(
1373 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1374 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1377 unsigned int src_len
= strlen(src
);
1378 unsigned int dest_len
= src_len
+ dest
->Length
;
1380 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1381 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1382 dest
->Length
= dest_len
;
1384 return STATUS_SUCCESS
;
1388 /**************************************************************************
1389 * RtlAppendStringToString (NTDLL.@)
1391 * Concatenates two buffered character strings
1394 * Success: STATUS_SUCCESS. src is appended to dest.
1395 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1396 * to hold the concatenated string.
1399 * if src->length is zero dest is unchanged.
1400 * dest is never '\0' terminated.
1402 NTSTATUS WINAPI
RtlAppendStringToString(
1403 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1404 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1406 if (src
->Length
!= 0) {
1407 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1409 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1410 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1411 dest
->Length
= dest_len
;
1413 return STATUS_SUCCESS
;
1417 /**************************************************************************
1418 * RtlAppendUnicodeToString (NTDLL.@)
1420 * Concatenates a buffered unicode string and a '\0' terminated unicode
1424 * Success: STATUS_SUCCESS. src is appended to dest.
1425 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1426 * to hold the concatenated string.
1429 * if src is NULL dest is unchanged.
1430 * dest is '\0' terminated when the MaximumLength allows it.
1431 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1434 * Does not write in the src->Buffer beyond MaximumLength when
1435 * MaximumLength is odd as the native function does.
1437 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1438 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1439 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1442 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1443 unsigned int dest_len
= src_len
+ dest
->Length
;
1445 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1446 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1447 dest
->Length
= dest_len
;
1448 /* append terminating '\0' if enough space */
1449 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1450 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1453 return STATUS_SUCCESS
;
1457 /**************************************************************************
1458 * RtlAppendUnicodeStringToString (NTDLL.@)
1460 * Concatenates two buffered unicode strings
1463 * Success: STATUS_SUCCESS. src is appended to dest.
1464 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1465 * to hold the concatenated string.
1468 * if src->length is zero dest is unchanged.
1469 * dest is '\0' terminated when the MaximumLength allows it.
1470 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1473 * Does not write in the src->Buffer beyond MaximumLength when
1474 * MaximumLength is odd as the native function does.
1476 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1477 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1478 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1480 if (src
->Length
!= 0) {
1481 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1483 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1484 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1485 dest
->Length
= dest_len
;
1486 /* append terminating '\0' if enough space */
1487 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1488 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1491 return STATUS_SUCCESS
;
1495 /**************************************************************************
1496 * RtlFindCharInUnicodeString (NTDLL.@)
1498 * Searches for one of several unicode characters in an unicode string.
1501 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1502 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1504 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1505 int flags
, /* [I] Flags */
1506 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1507 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1508 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1511 unsigned int search_idx
;
1515 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1516 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1517 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1518 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1519 return STATUS_SUCCESS
;
1524 return STATUS_NOT_FOUND
;
1526 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1527 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1528 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1529 *pos
= main_idx
* sizeof(WCHAR
);
1530 return STATUS_SUCCESS
;
1535 return STATUS_NOT_FOUND
;
1537 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1539 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1540 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1543 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1544 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1545 return STATUS_SUCCESS
;
1549 return STATUS_NOT_FOUND
;
1551 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1553 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1554 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1557 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1558 *pos
= main_idx
* sizeof(WCHAR
);
1559 return STATUS_SUCCESS
;
1563 return STATUS_NOT_FOUND
;
1565 return STATUS_NOT_FOUND
;
1573 /**************************************************************************
1574 * RtlIsTextUnicode (NTDLL.@)
1576 * Attempt to guess whether a text buffer is Unicode.
1579 * buf [I] Text buffer to test
1580 * len [I] Length of buf
1581 * pf [O] Destination for test results
1584 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1587 * Should implement more tests.
1589 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1591 const WCHAR
*s
= buf
;
1593 unsigned int flags
= ~0U, out_flags
= 0;
1595 if (len
< sizeof(WCHAR
))
1597 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1604 * Apply various tests to the text string. According to the
1605 * docs, each test "passed" sets the corresponding flag in
1606 * the output flags. But some of the tests are mutually
1607 * exclusive, so I don't see how you could pass all tests ...
1610 /* Check for an odd length ... pass if even. */
1611 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1613 if (((char *)buf
)[len
- 1] == 0)
1614 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1616 len
/= sizeof(WCHAR
);
1617 /* Windows only checks the first 256 characters */
1618 if (len
> 256) len
= 256;
1620 /* Check for the special byte order unicode marks. */
1621 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1622 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1624 /* apply some statistical analysis */
1625 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1628 /* FIXME: checks only for ASCII characters in the unicode stream */
1629 for (i
= 0; i
< len
; i
++)
1631 if (s
[i
] <= 255) stats
++;
1633 if (stats
> len
/ 2)
1634 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1637 /* Check for unicode NULL chars */
1638 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1640 for (i
= 0; i
< len
; i
++)
1642 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1644 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1655 /* check for flags that indicate it's definitely not valid Unicode */
1656 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1657 /* now check for invalid ASCII, and assume Unicode if so */
1658 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1659 /* now check for Unicode flags */
1660 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1666 /**************************************************************************
1667 * RtlCharToInteger (NTDLL.@)
1669 * Converts a character string into its integer equivalent.
1672 * Success: STATUS_SUCCESS. value contains the converted number
1673 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1674 * STATUS_ACCESS_VIOLATION, if value is NULL.
1677 * For base 0 it uses 10 as base and the string should be in the format
1678 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1679 * For other bases the string should be in the format
1680 * "{whitespace} [+|-] {digits}".
1681 * No check is made for value overflow, only the lower 32 bits are assigned.
1682 * If str is NULL it crashes, as the native function does.
1685 * This function does not read garbage behind '\0' as the native version does.
1687 NTSTATUS WINAPI
RtlCharToInteger(
1688 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1689 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1690 ULONG
*value
) /* [O] Destination for the converted value */
1694 ULONG RunningTotal
= 0;
1697 while (*str
!= '\0' && *str
<= ' ') {
1703 } else if (*str
== '-') {
1710 if (str
[0] == '0') {
1711 if (str
[1] == 'b') {
1714 } else if (str
[1] == 'o') {
1717 } else if (str
[1] == 'x') {
1722 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1723 return STATUS_INVALID_PARAMETER
;
1726 if (value
== NULL
) {
1727 return STATUS_ACCESS_VIOLATION
;
1730 while (*str
!= '\0') {
1732 if (chCurrent
>= '0' && chCurrent
<= '9') {
1733 digit
= chCurrent
- '0';
1734 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1735 digit
= chCurrent
- 'A' + 10;
1736 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1737 digit
= chCurrent
- 'a' + 10;
1741 if (digit
< 0 || digit
>= base
) {
1742 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1743 return STATUS_SUCCESS
;
1746 RunningTotal
= RunningTotal
* base
+ digit
;
1750 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1751 return STATUS_SUCCESS
;
1755 /**************************************************************************
1756 * RtlIntegerToChar (NTDLL.@)
1758 * Converts an unsigned integer to a character string.
1761 * Success: STATUS_SUCCESS. str contains the converted number
1762 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1763 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1764 * STATUS_ACCESS_VIOLATION, if str is NULL.
1767 * Instead of base 0 it uses 10 as base.
1768 * Writes at most length characters to the string str.
1769 * Str is '\0' terminated when length allows it.
1770 * When str fits exactly in length characters the '\0' is omitted.
1772 NTSTATUS WINAPI
RtlIntegerToChar(
1773 ULONG value
, /* [I] Value to be converted */
1774 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1775 ULONG length
, /* [I] Length of the str buffer in bytes */
1776 PCHAR str
) /* [O] Destination for the converted value */
1785 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1786 return STATUS_INVALID_PARAMETER
;
1794 digit
= value
% base
;
1795 value
= value
/ base
;
1799 *pos
= 'A' + digit
- 10;
1801 } while (value
!= 0L);
1803 len
= &buffer
[32] - pos
;
1805 return STATUS_BUFFER_OVERFLOW
;
1806 } else if (str
== NULL
) {
1807 return STATUS_ACCESS_VIOLATION
;
1808 } else if (len
== length
) {
1809 memcpy(str
, pos
, len
);
1811 memcpy(str
, pos
, len
+ 1);
1813 return STATUS_SUCCESS
;
1817 /**************************************************************************
1818 * RtlUnicodeStringToInteger (NTDLL.@)
1820 * Converts an unicode string into its integer equivalent.
1823 * Success: STATUS_SUCCESS. value contains the converted number
1824 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1825 * STATUS_ACCESS_VIOLATION, if value is NULL.
1828 * For base 0 it uses 10 as base and the string should be in the format
1829 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1830 * For other bases the string should be in the format
1831 * "{whitespace} [+|-] {digits}".
1832 * No check is made for value overflow, only the lower 32 bits are assigned.
1833 * If str is NULL it crashes, as the native function does.
1836 * This function does not read garbage on string length 0 as the native
1839 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1840 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1841 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1842 ULONG
*value
) /* [O] Destination for the converted value */
1844 LPWSTR lpwstr
= str
->Buffer
;
1845 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1848 ULONG RunningTotal
= 0;
1851 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1856 if (CharsRemaining
>= 1) {
1857 if (*lpwstr
== '+') {
1860 } else if (*lpwstr
== '-') {
1869 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1870 if (lpwstr
[1] == 'b') {
1872 CharsRemaining
-= 2;
1874 } else if (lpwstr
[1] == 'o') {
1876 CharsRemaining
-= 2;
1878 } else if (lpwstr
[1] == 'x') {
1880 CharsRemaining
-= 2;
1884 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1885 return STATUS_INVALID_PARAMETER
;
1888 if (value
== NULL
) {
1889 return STATUS_ACCESS_VIOLATION
;
1892 while (CharsRemaining
>= 1) {
1893 wchCurrent
= *lpwstr
;
1894 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1895 digit
= wchCurrent
- '0';
1896 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1897 digit
= wchCurrent
- 'A' + 10;
1898 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1899 digit
= wchCurrent
- 'a' + 10;
1903 if (digit
< 0 || digit
>= base
) {
1904 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1905 return STATUS_SUCCESS
;
1908 RunningTotal
= RunningTotal
* base
+ digit
;
1913 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1914 return STATUS_SUCCESS
;
1918 /**************************************************************************
1919 * RtlIntegerToUnicodeString (NTDLL.@)
1921 * Converts an unsigned integer to a '\0' terminated unicode string.
1924 * Success: STATUS_SUCCESS. str contains the converted number
1925 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1926 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1927 * (with the '\0' termination). In this case str->Length
1928 * is set to the length, the string would have (which can
1929 * be larger than the MaximumLength).
1932 * Instead of base 0 it uses 10 as base.
1933 * If str is NULL it crashes, as the native function does.
1936 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1937 * The native function does this when the string would be longer than 16
1938 * characters even when the string parameter is long enough.
1940 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1941 ULONG value
, /* [I] Value to be converted */
1942 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1943 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1951 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1952 return STATUS_INVALID_PARAMETER
;
1960 digit
= value
% base
;
1961 value
= value
/ base
;
1965 *pos
= 'A' + digit
- 10;
1967 } while (value
!= 0L);
1969 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1970 if (str
->Length
>= str
->MaximumLength
) {
1971 return STATUS_BUFFER_OVERFLOW
;
1973 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1975 return STATUS_SUCCESS
;
1979 /*************************************************************************
1980 * RtlGUIDFromString (NTDLL.@)
1982 * Convert a string representation of a GUID into a GUID.
1985 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1986 * guid [O] Destination for the converted GUID
1989 * Success: STATUS_SUCCESS. guid contains the converted value.
1990 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1993 * See RtlStringFromGUID.
1995 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
1998 const WCHAR
*lpszCLSID
= str
->Buffer
;
1999 BYTE
* lpOut
= (BYTE
*)guid
;
2001 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
2003 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
2004 * to memory: DWORD... WORD WORD BYTES............
2011 if (*lpszCLSID
!= '{')
2012 return STATUS_INVALID_PARAMETER
;
2015 case 9: case 14: case 19: case 24:
2016 if (*lpszCLSID
!= '-')
2017 return STATUS_INVALID_PARAMETER
;
2021 if (*lpszCLSID
!= '}')
2022 return STATUS_INVALID_PARAMETER
;
2027 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
2030 /* Read two hex digits as a byte value */
2031 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
2032 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
2033 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
2034 else return STATUS_INVALID_PARAMETER
;
2036 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
2037 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
2038 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
2039 else return STATUS_INVALID_PARAMETER
;
2041 byte
= ch
<< 4 | ch2
;
2045 #ifndef WORDS_BIGENDIAN
2046 /* For Big Endian machines, we store the data such that the
2047 * dword/word members can be read as DWORDS and WORDS correctly. */
2049 case 1: lpOut
[3] = byte
; break;
2050 case 3: lpOut
[2] = byte
; break;
2051 case 5: lpOut
[1] = byte
; break;
2052 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
2054 case 10: case 15: lpOut
[1] = byte
; break;
2055 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
2058 default: lpOut
[0] = byte
; lpOut
++; break;
2060 lpszCLSID
++; /* Skip 2nd character of byte */
2068 return STATUS_SUCCESS
;
2071 /*************************************************************************
2072 * RtlStringFromGUID (NTDLL.@)
2074 * Convert a GUID into a string representation of a GUID.
2077 * guid [I] GUID to convert
2078 * str [O] Destination for the converted string
2081 * Success: STATUS_SUCCESS. str contains the converted value.
2082 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2085 * See RtlGUIDFromString.
2087 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2089 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2090 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2091 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2092 '%','0','2','X','%','0','2','X','}','\0' };
2094 TRACE("(%p,%p)\n", guid
, str
);
2096 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
2097 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
2098 str
->Buffer
= (WCHAR
*)RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
2101 str
->Length
= str
->MaximumLength
= 0;
2102 return STATUS_NO_MEMORY
;
2104 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2105 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2106 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2108 return STATUS_SUCCESS
;