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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "ntdll_misc.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
38 #define GUID_STRING_LENGTH 38
40 UINT NlsAnsiCodePage
= 0;
41 BYTE NlsMbCodePageTag
= 0;
42 BYTE NlsMbOemCodePageTag
= 0;
44 static const union cptable
*ansi_table
;
45 static const union cptable
*oem_table
;
46 static const union cptable
* unix_table
; /* NULL if UTF8 */
49 /**************************************************************************
50 * __wine_init_codepages (NTDLL.@)
52 * Set the code page once kernel32 is loaded. Should be done differently.
54 void __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
55 const union cptable
*ucp
)
60 NlsAnsiCodePage
= ansi
->info
.codepage
;
63 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
66 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
67 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
70 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
71 const char* defchar
, int *used
)
74 return wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
);
75 if (used
) *used
= 0; /* all chars are valid for UTF-8 */
76 return wine_utf8_wcstombs( src
, srclen
, dst
, dstlen
);
79 /**************************************************************************
80 * RtlInitAnsiString (NTDLL.@)
82 * Initializes a buffered ansi string.
88 * Assigns source to target->Buffer. The length of source is assigned to
89 * target->Length and target->MaximumLength. If source is NULL the length
90 * of source is assumed to be 0.
92 void WINAPI
RtlInitAnsiString(
93 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
94 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
96 if ((target
->Buffer
= (PCHAR
) source
))
98 target
->Length
= strlen(source
);
99 target
->MaximumLength
= target
->Length
+ 1;
101 else target
->Length
= target
->MaximumLength
= 0;
105 /**************************************************************************
106 * RtlInitString (NTDLL.@)
108 * Initializes a buffered string.
114 * Assigns source to target->Buffer. The length of source is assigned to
115 * target->Length and target->MaximumLength. If source is NULL the length
116 * of source is assumed to be 0.
118 void WINAPI
RtlInitString(
119 PSTRING target
, /* [I/O] Buffered string to be initialized */
120 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
122 RtlInitAnsiString( target
, source
);
126 /**************************************************************************
127 * RtlFreeAnsiString (NTDLL.@)
129 void WINAPI
RtlFreeAnsiString( PSTRING str
)
131 if (str
->Buffer
) RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
135 /**************************************************************************
136 * RtlFreeOemString (NTDLL.@)
138 void WINAPI
RtlFreeOemString( PSTRING str
)
140 RtlFreeAnsiString( str
);
144 /**************************************************************************
145 * RtlCopyString (NTDLL.@)
147 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
151 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
152 memcpy( dst
->Buffer
, src
->Buffer
, len
);
155 else dst
->Length
= 0;
159 /**************************************************************************
160 * RtlInitUnicodeString (NTDLL.@)
162 * Initializes a buffered unicode string.
168 * Assigns source to target->Buffer. The length of source is assigned to
169 * target->Length and target->MaximumLength. If source is NULL the length
170 * of source is assumed to be 0.
172 void WINAPI
RtlInitUnicodeString(
173 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
174 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
176 if ((target
->Buffer
= (PWSTR
) source
))
178 target
->Length
= strlenW(source
) * sizeof(WCHAR
);
179 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
181 else target
->Length
= target
->MaximumLength
= 0;
185 /**************************************************************************
186 * RtlInitUnicodeStringEx (NTDLL.@)
188 * Initializes a buffered unicode string.
191 * Success: STATUS_SUCCESS. target is initialized.
192 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
195 * Assigns source to target->Buffer. The length of source is assigned to
196 * target->Length and target->MaximumLength. If source is NULL the length
197 * of source is assumed to be 0.
199 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
200 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
201 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
203 if (source
!= NULL
) {
204 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
207 return STATUS_NAME_TOO_LONG
;
209 target
->Length
= len
;
210 target
->MaximumLength
= len
+ sizeof(WCHAR
);
211 target
->Buffer
= (PWSTR
) source
;
215 target
->MaximumLength
= 0;
216 target
->Buffer
= NULL
;
218 return STATUS_SUCCESS
;
222 /**************************************************************************
223 * RtlCreateUnicodeString (NTDLL.@)
225 * Creates a UNICODE_STRING from a null-terminated Unicode string.
231 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
233 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
234 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
235 memcpy( target
->Buffer
, src
, len
);
236 target
->MaximumLength
= len
;
237 target
->Length
= len
- sizeof(WCHAR
);
242 /**************************************************************************
243 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
245 * Creates a UNICODE_STRING from a null-terminated Ascii string.
251 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
254 RtlInitAnsiString( &ansi
, src
);
255 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
259 /**************************************************************************
260 * RtlFreeUnicodeString (NTDLL.@)
262 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
263 * RtlCreateUnicodeStringFromAsciiz().
268 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
270 if (str
->Buffer
) RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
274 /**************************************************************************
275 * RtlCopyUnicodeString (NTDLL.@)
277 * Copies from one UNICODE_STRING to another.
282 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
286 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
287 memcpy( dst
->Buffer
, src
->Buffer
, len
);
289 /* append terminating '\0' if enough space */
290 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
292 else dst
->Length
= 0;
296 /**************************************************************************
297 * RtlDuplicateUnicodeString (NTDLL.@)
299 * Duplicates an unicode string.
302 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
303 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
304 * STATUS_NO_MEMORY, if the allocation fails.
307 * For add_nul there are several possible values:
308 * 0 = destination will not be '\0' terminated,
309 * 1 = destination will be '\0' terminated,
310 * 3 = like 1 but for an empty source string produce '\0' terminated empty
311 * Buffer instead of assigning NULL to the Buffer.
312 * Other add_nul values are invalid.
314 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
315 int add_nul
, /* [I] flag */
316 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
317 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
319 if (source
== NULL
|| destination
== NULL
||
320 source
->Length
> source
->MaximumLength
||
321 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
322 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
323 return STATUS_INVALID_PARAMETER
;
325 if (source
->Length
== 0 && add_nul
!= 3) {
326 destination
->Length
= 0;
327 destination
->MaximumLength
= 0;
328 destination
->Buffer
= NULL
;
330 unsigned int destination_max_len
= source
->Length
;
333 destination_max_len
+= sizeof(WCHAR
);
335 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
336 if (destination
->Buffer
== NULL
) {
337 return STATUS_NO_MEMORY
;
339 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
340 destination
->Length
= source
->Length
;
341 destination
->MaximumLength
= source
->Length
;
342 /* append terminating '\0' if enough space */
344 destination
->MaximumLength
= destination_max_len
;
345 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
350 return STATUS_SUCCESS
;
354 /**************************************************************************
355 * RtlEraseUnicodeString (NTDLL.@)
357 * Overwrites a UNICODE_STRING with zeros.
362 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
366 memset( str
->Buffer
, 0, str
->MaximumLength
);
377 /******************************************************************************
378 * RtlCompareString (NTDLL.@)
380 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
386 len
= min(s1
->Length
, s2
->Length
);
392 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
396 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
398 if (!ret
) ret
= s1
->Length
- s2
->Length
;
403 /******************************************************************************
404 * RtlCompareUnicodeString (NTDLL.@)
406 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
407 BOOLEAN CaseInsensitive
)
413 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
419 while (!ret
&& len
--) ret
= toupperW(*p1
++) - toupperW(*p2
++);
423 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
425 if (!ret
) ret
= s1
->Length
- s2
->Length
;
430 /**************************************************************************
431 * RtlEqualString (NTDLL.@)
433 * Determine if two strings are equal.
436 * s1 [I] Source string
437 * s2 [I] String to compare to s1
438 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
441 * Non-zero if s1 is equal to s2, 0 otherwise.
443 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
445 if (s1
->Length
!= s2
->Length
) return FALSE
;
446 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
450 /**************************************************************************
451 * RtlEqualUnicodeString (NTDLL.@)
453 * Unicode version of RtlEqualString.
455 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
456 BOOLEAN CaseInsensitive
)
458 if (s1
->Length
!= s2
->Length
) return FALSE
;
459 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
463 /**************************************************************************
464 * RtlPrefixString (NTDLL.@)
466 * Determine if one string is a prefix of another.
469 * s1 [I] Prefix to look for in s2
470 * s2 [I] String that may contain s1 as a prefix
471 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
474 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
476 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
480 if (s1
->Length
> s2
->Length
) return FALSE
;
483 for (i
= 0; i
< s1
->Length
; i
++)
484 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
488 for (i
= 0; i
< s1
->Length
; i
++)
489 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
495 /**************************************************************************
496 * RtlPrefixUnicodeString (NTDLL.@)
498 * Unicode version of RtlPrefixString.
500 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
501 const UNICODE_STRING
*s2
,
502 BOOLEAN ignore_case
)
506 if (s1
->Length
> s2
->Length
) return FALSE
;
509 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
510 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
514 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
515 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
521 /**************************************************************************
522 * RtlEqualComputerName (NTDLL.@)
524 * Determine if two computer names are the same.
527 * left [I] First computer name
528 * right [I] Second computer name
531 * 0 if the names are equal, non-zero otherwise.
534 * The comparison is case insensitive.
536 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
537 const UNICODE_STRING
*right
)
540 STRING upLeft
, upRight
;
542 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
544 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
546 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
547 RtlFreeOemString( &upRight
);
549 RtlFreeOemString( &upLeft
);
555 /**************************************************************************
556 * RtlEqualDomainName (NTDLL.@)
558 * Determine if two domain names are the same.
561 * left [I] First domain name
562 * right [I] Second domain name
565 * 0 if the names are equal, non-zero otherwise.
568 * The comparison is case insensitive.
570 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
571 const UNICODE_STRING
*right
)
573 return RtlEqualComputerName(left
, right
);
577 /**************************************************************************
578 * RtlAnsiCharToUnicodeChar (NTDLL.@)
580 * Converts the first ansi character to a unicode character.
583 * ansi [I/O] Pointer to the ansi string.
586 * Unicode representation of the first character in the ansi string.
589 * Upon successful completion, the char pointer ansi points to is
590 * incremented by the size of the character.
592 WCHAR WINAPI
RtlAnsiCharToUnicodeChar(LPSTR
*ansi
)
595 DWORD charSize
= sizeof(CHAR
);
597 if (is_dbcs_leadbyte(ansi_table
, **ansi
))
600 RtlMultiByteToUnicodeN(&str
, sizeof(WCHAR
), NULL
, *ansi
, charSize
);
607 COPY BETWEEN ANSI_STRING or UNICODE_STRING
608 there is no parameter checking, it just crashes
612 /**************************************************************************
613 * RtlAnsiStringToUnicodeString (NTDLL.@)
615 * Converts an ansi string to an unicode string.
618 * Success: STATUS_SUCCESS. uni contains the converted string
619 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
620 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
621 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
624 * This function always writes a terminating '\0'.
626 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
627 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
628 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
629 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
631 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
633 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
634 uni
->Length
= total
- sizeof(WCHAR
);
637 uni
->MaximumLength
= total
;
638 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
639 return STATUS_NO_MEMORY
;
641 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
643 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
644 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
645 return STATUS_SUCCESS
;
649 /**************************************************************************
650 * RtlOemStringToUnicodeString (NTDLL.@)
652 * Converts an oem string to an unicode string.
655 * Success: STATUS_SUCCESS. uni contains the converted string
656 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
657 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
658 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
661 * This function always writes a terminating '\0'.
663 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
664 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
665 const STRING
*oem
, /* [I] Oem string to be converted */
666 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
668 DWORD total
= RtlOemStringToUnicodeSize( oem
);
670 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
671 uni
->Length
= total
- sizeof(WCHAR
);
674 uni
->MaximumLength
= total
;
675 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
676 return STATUS_NO_MEMORY
;
678 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
680 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
681 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
682 return STATUS_SUCCESS
;
686 /**************************************************************************
687 * RtlUnicodeStringToAnsiString (NTDLL.@)
689 * Converts an unicode string to an ansi string.
692 * Success: STATUS_SUCCESS. ansi contains the converted string
693 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
694 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
697 * This function always writes a terminating '\0'.
698 * It performs a partial copy if ansi is too small.
700 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
701 STRING
*ansi
, /* [I/O] Destination for the ansi string */
702 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
703 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
705 NTSTATUS ret
= STATUS_SUCCESS
;
706 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
708 ansi
->Length
= len
- 1;
711 ansi
->MaximumLength
= len
;
712 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
713 return STATUS_NO_MEMORY
;
715 else if (ansi
->MaximumLength
< len
)
717 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
718 ansi
->Length
= ansi
->MaximumLength
- 1;
719 ret
= STATUS_BUFFER_OVERFLOW
;
722 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
723 ansi
->Buffer
[ansi
->Length
] = 0;
728 /**************************************************************************
729 * RtlUnicodeStringToOemString (NTDLL.@)
731 * Converts a Rtl Unicode string to an OEM string.
734 * oem [O] Destination for OEM string
735 * uni [I] Source Unicode string
736 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
739 * Success: STATUS_SUCCESS. oem contains the converted string
740 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
741 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
744 * If doalloc is TRUE, the length allocated is uni->Length + 1.
745 * This function always '\0' terminates the string returned.
747 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
748 const UNICODE_STRING
*uni
,
751 NTSTATUS ret
= STATUS_SUCCESS
;
752 DWORD len
= RtlUnicodeStringToOemSize( uni
);
754 oem
->Length
= len
- 1;
757 oem
->MaximumLength
= len
;
758 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
759 return STATUS_NO_MEMORY
;
761 else if (oem
->MaximumLength
< len
)
763 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
764 oem
->Length
= oem
->MaximumLength
- 1;
765 ret
= STATUS_BUFFER_OVERFLOW
;
768 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
769 oem
->Buffer
[oem
->Length
] = 0;
774 /**************************************************************************
775 * RtlMultiByteToUnicodeN (NTDLL.@)
777 * Converts a multi-byte string to a Unicode string.
783 * Performs a partial copy if dst is too small.
785 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
786 LPCSTR src
, DWORD srclen
)
789 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
791 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
792 return STATUS_SUCCESS
;
796 /**************************************************************************
797 * RtlOemToUnicodeN (NTDLL.@)
799 * Converts a multi-byte string in the OEM code page to a Unicode string.
804 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
805 LPCSTR src
, DWORD srclen
)
807 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
809 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
810 return STATUS_SUCCESS
;
814 /**************************************************************************
815 * RtlUnicodeToMultiByteN (NTDLL.@)
817 * Converts a Unicode string to a multi-byte string in the ANSI code page.
822 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
823 LPCWSTR src
, DWORD srclen
)
825 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
826 dst
, dstlen
, NULL
, NULL
);
828 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
829 return STATUS_SUCCESS
;
833 /**************************************************************************
834 * RtlUnicodeToOemN (NTDLL.@)
836 * Converts a Unicode string to a multi-byte string in the OEM code page.
841 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
842 LPCWSTR src
, DWORD srclen
)
844 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
845 dst
, dstlen
, NULL
, NULL
);
847 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
848 return STATUS_SUCCESS
;
857 /**************************************************************************
858 * RtlUpperChar (NTDLL.@)
860 * Converts an Ascii character to uppercase.
863 * ch [I] Character to convert
866 * The uppercase character value.
869 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
870 * All other input characters are returned unchanged. The locale and
871 * multibyte characters are not taken into account (as native DLL).
873 CHAR WINAPI
RtlUpperChar( CHAR ch
)
875 if (ch
>= 'a' && ch
<= 'z') {
876 return ch
- 'a' + 'A';
883 /**************************************************************************
884 * RtlUpperString (NTDLL.@)
886 * Converts an Ascii string to uppercase.
889 * dst [O] Destination for converted string
890 * src [I] Source string to convert
896 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
897 * All other src characters are copied unchanged to dst. The locale and
898 * multibyte characters are not taken into account (as native DLL).
899 * The number of character copied is the minimum of src->Length and
900 * the dst->MaximumLength.
902 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
904 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
906 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
911 /**************************************************************************
912 * RtlUpcaseUnicodeChar (NTDLL.@)
914 * Converts an Unicode character to uppercase.
917 * wch [I] Character to convert
920 * The uppercase character value.
922 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
924 return toupperW(wch
);
928 /**************************************************************************
929 * RtlDowncaseUnicodeChar (NTDLL.@)
931 * Converts an Unicode character to lowercase.
934 * wch [I] Character to convert
937 * The lowercase character value.
939 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
941 return tolowerW(wch
);
945 /**************************************************************************
946 * RtlUpcaseUnicodeString (NTDLL.@)
948 * Converts an Unicode string to uppercase.
951 * dest [O] Destination for converted string
952 * src [I] Source string to convert
953 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
956 * Success: STATUS_SUCCESS. dest contains the converted string.
957 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
958 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
961 * dest is never '\0' terminated because it may be equal to src, and src
962 * might not be '\0' terminated. dest->Length is only set upon success.
964 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
965 const UNICODE_STRING
*src
,
968 DWORD i
, len
= src
->Length
;
972 dest
->MaximumLength
= len
;
973 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
974 return STATUS_NO_MEMORY
;
976 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
978 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
980 return STATUS_SUCCESS
;
984 /**************************************************************************
985 * RtlDowncaseUnicodeString (NTDLL.@)
987 * Converts an Unicode string to lowercase.
990 * dest [O] Destination for converted string
991 * src [I] Source string to convert
992 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
995 * Success: STATUS_SUCCESS. dest contains the converted string.
996 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
997 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
1000 * dest is never '\0' terminated because it may be equal to src, and src
1001 * might not be '\0' terminated. dest->Length is only set upon success.
1003 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
1004 UNICODE_STRING
*dest
,
1005 const UNICODE_STRING
*src
,
1009 DWORD len
= src
->Length
;
1012 dest
->MaximumLength
= len
;
1013 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
1014 return STATUS_NO_MEMORY
;
1016 } else if (len
> dest
->MaximumLength
) {
1017 return STATUS_BUFFER_OVERFLOW
;
1020 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
1021 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
1024 return STATUS_SUCCESS
;
1028 /**************************************************************************
1029 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
1031 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1037 * writes terminating 0
1039 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
1040 const UNICODE_STRING
*src
,
1044 UNICODE_STRING upcase
;
1046 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1048 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
1049 RtlFreeUnicodeString( &upcase
);
1055 /**************************************************************************
1056 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1058 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1059 * stored in STRING format.
1065 * writes terminating 0
1067 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
1068 const UNICODE_STRING
*src
,
1072 UNICODE_STRING upcase
;
1074 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
1076 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
1077 RtlFreeUnicodeString( &upcase
);
1083 /**************************************************************************
1084 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1086 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1087 * stored in STRING format.
1093 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1095 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1096 const UNICODE_STRING
*uni
,
1100 UNICODE_STRING upcase
;
1103 upcase
.Buffer
= tmp
;
1104 upcase
.MaximumLength
= sizeof(tmp
);
1105 ret
= RtlUpcaseUnicodeString( &upcase
, uni
, FALSE
);
1106 if (ret
== STATUS_BUFFER_OVERFLOW
) ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
);
1110 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1114 oem
->MaximumLength
= len
;
1115 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1117 ret
= STATUS_NO_MEMORY
;
1121 else if (oem
->MaximumLength
< len
)
1123 ret
= STATUS_BUFFER_OVERFLOW
;
1124 oem
->Length
= oem
->MaximumLength
;
1125 if (!oem
->MaximumLength
) goto done
;
1127 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1129 if (upcase
.Buffer
!= tmp
) RtlFreeUnicodeString( &upcase
);
1135 /**************************************************************************
1136 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1138 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1143 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1144 LPCWSTR src
, DWORD srclen
)
1150 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1151 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1152 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1153 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1158 /**************************************************************************
1159 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1161 * Converts a Unicode string to the equivalent OEM upper-case representation.
1166 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1167 LPCWSTR src
, DWORD srclen
)
1173 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1174 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1175 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1176 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1186 /**************************************************************************
1187 * RtlOemStringToUnicodeSize (NTDLL.@)
1188 * RtlxOemStringToUnicodeSize (NTDLL.@)
1190 * Calculate the size in bytes necessary for the Unicode conversion of str,
1191 * including the terminating '\0'.
1194 * str [I] String to calculate the size of
1197 * The calculated size.
1199 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1201 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1202 return (ret
+ 1) * sizeof(WCHAR
);
1206 /**************************************************************************
1207 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1208 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1210 * Calculate the size in bytes necessary for the Unicode conversion of str,
1211 * including the terminating '\0'.
1214 * str [I] String to calculate the size of
1217 * The calculated size.
1219 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1222 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1223 return ret
+ sizeof(WCHAR
);
1227 /**************************************************************************
1228 * RtlMultiByteToUnicodeSize (NTDLL.@)
1230 * Compute the size in bytes necessary for the Unicode conversion of str,
1231 * without the terminating '\0'.
1234 * size [O] Destination for size
1235 * str [I] String to calculate the size of
1236 * len [I] Length of str
1241 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1243 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1244 return STATUS_SUCCESS
;
1248 /**************************************************************************
1249 * RtlUnicodeToMultiByteSize (NTDLL.@)
1251 * Calculate the size in bytes necessary for the multibyte conversion of str,
1252 * without the terminating '\0'.
1255 * size [O] Destination for size
1256 * str [I] String to calculate the size of
1257 * len [I] Length of str
1262 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1264 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1265 return STATUS_SUCCESS
;
1269 /**************************************************************************
1270 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1271 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1273 * Calculate the size in bytes necessary for the Ansi conversion of str,
1274 * including the terminating '\0'.
1277 * str [I] String to calculate the size of
1280 * The calculated size.
1282 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1285 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1290 /**************************************************************************
1291 * RtlUnicodeStringToOemSize (NTDLL.@)
1292 * RtlxUnicodeStringToOemSize (NTDLL.@)
1294 * Calculate the size in bytes necessary for the OEM conversion of str,
1295 * including the terminating '\0'.
1298 * str [I] String to calculate the size of
1301 * The calculated size.
1303 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1305 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1306 NULL
, 0, NULL
, NULL
) + 1;
1310 /**************************************************************************
1311 * RtlAppendAsciizToString (NTDLL.@)
1313 * Concatenates a buffered character string and a '\0' terminated character
1317 * Success: STATUS_SUCCESS. src is appended to dest.
1318 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1319 * to hold the concatenated string.
1322 * if src is NULL dest is unchanged.
1323 * dest is never '\0' terminated.
1325 NTSTATUS WINAPI
RtlAppendAsciizToString(
1326 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1327 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1330 unsigned int src_len
= strlen(src
);
1331 unsigned int dest_len
= src_len
+ dest
->Length
;
1333 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1334 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1335 dest
->Length
= dest_len
;
1337 return STATUS_SUCCESS
;
1341 /**************************************************************************
1342 * RtlAppendStringToString (NTDLL.@)
1344 * Concatenates two buffered character strings
1347 * Success: STATUS_SUCCESS. src is appended to dest.
1348 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1349 * to hold the concatenated string.
1352 * if src->length is zero dest is unchanged.
1353 * dest is never '\0' terminated.
1355 NTSTATUS WINAPI
RtlAppendStringToString(
1356 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1357 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1359 if (src
->Length
!= 0) {
1360 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1362 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1363 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1364 dest
->Length
= dest_len
;
1366 return STATUS_SUCCESS
;
1370 /**************************************************************************
1371 * RtlAppendUnicodeToString (NTDLL.@)
1373 * Concatenates a buffered unicode string and a '\0' terminated unicode
1377 * Success: STATUS_SUCCESS. src is appended to dest.
1378 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1379 * to hold the concatenated string.
1382 * if src is NULL dest is unchanged.
1383 * dest is '\0' terminated when the MaximumLength allows it.
1384 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1387 * Does not write in the src->Buffer beyond MaximumLength when
1388 * MaximumLength is odd as the native function does.
1390 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1391 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1392 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1395 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1396 unsigned int dest_len
= src_len
+ dest
->Length
;
1398 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1399 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1400 dest
->Length
= dest_len
;
1401 /* append terminating '\0' if enough space */
1402 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1403 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1406 return STATUS_SUCCESS
;
1410 /**************************************************************************
1411 * RtlAppendUnicodeStringToString (NTDLL.@)
1413 * Concatenates two buffered unicode strings
1416 * Success: STATUS_SUCCESS. src is appended to dest.
1417 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1418 * to hold the concatenated string.
1421 * if src->length is zero dest is unchanged.
1422 * dest is '\0' terminated when the MaximumLength allows it.
1423 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1426 * Does not write in the src->Buffer beyond MaximumLength when
1427 * MaximumLength is odd as the native function does.
1429 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1430 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1431 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1433 if (src
->Length
!= 0) {
1434 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1436 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1437 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1438 dest
->Length
= dest_len
;
1439 /* append terminating '\0' if enough space */
1440 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1441 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1444 return STATUS_SUCCESS
;
1448 /**************************************************************************
1449 * RtlFindCharInUnicodeString (NTDLL.@)
1451 * Searches for one of several unicode characters in an unicode string.
1454 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1455 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1457 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1458 int flags
, /* [I] Flags */
1459 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1460 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1461 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1464 unsigned int search_idx
;
1468 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1469 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1470 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1471 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1472 return STATUS_SUCCESS
;
1477 return STATUS_NOT_FOUND
;
1479 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1480 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1481 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1482 *pos
= main_idx
* sizeof(WCHAR
);
1483 return STATUS_SUCCESS
;
1488 return STATUS_NOT_FOUND
;
1490 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1492 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1493 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1496 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1497 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1498 return STATUS_SUCCESS
;
1502 return STATUS_NOT_FOUND
;
1504 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1506 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1507 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1510 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1511 *pos
= main_idx
* sizeof(WCHAR
);
1512 return STATUS_SUCCESS
;
1516 return STATUS_NOT_FOUND
;
1518 return STATUS_NOT_FOUND
;
1526 /**************************************************************************
1527 * RtlIsTextUnicode (NTDLL.@)
1529 * Attempt to guess whether a text buffer is Unicode.
1532 * buf [I] Text buffer to test
1533 * len [I] Length of buf
1534 * pf [O] Destination for test results
1537 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1540 * Should implement more tests.
1542 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1544 const WCHAR
*s
= buf
;
1546 unsigned int flags
= ~0U, out_flags
= 0;
1548 if (len
< sizeof(WCHAR
))
1550 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1557 * Apply various tests to the text string. According to the
1558 * docs, each test "passed" sets the corresponding flag in
1559 * the output flags. But some of the tests are mutually
1560 * exclusive, so I don't see how you could pass all tests ...
1563 /* Check for an odd length ... pass if even. */
1564 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1566 len
/= sizeof(WCHAR
);
1567 /* Windows only checks the first 256 characters */
1568 if (len
> 256) len
= 256;
1570 /* Check for the special byte order unicode marks. */
1571 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1572 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1574 /* apply some statistical analysis */
1575 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1578 /* FIXME: checks only for ASCII characters in the unicode stream */
1579 for (i
= 0; i
< len
; i
++)
1581 if (s
[i
] <= 255) stats
++;
1583 if (stats
> len
/ 2)
1584 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1587 /* Check for unicode NULL chars */
1588 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1590 for (i
= 0; i
< len
; i
++)
1594 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1605 /* check for flags that indicate it's definitely not valid Unicode */
1606 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1607 /* now check for invalid ASCII, and assume Unicode if so */
1608 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1609 /* now check for Unicode flags */
1610 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1616 /**************************************************************************
1617 * RtlCharToInteger (NTDLL.@)
1619 * Converts a character string into its integer equivalent.
1622 * Success: STATUS_SUCCESS. value contains the converted number
1623 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1624 * STATUS_ACCESS_VIOLATION, if value is NULL.
1627 * For base 0 it uses 10 as base and the string should be in the format
1628 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1629 * For other bases the string should be in the format
1630 * "{whitespace} [+|-] {digits}".
1631 * No check is made for value overflow, only the lower 32 bits are assigned.
1632 * If str is NULL it crashes, as the native function does.
1635 * This function does not read garbage behind '\0' as the native version does.
1637 NTSTATUS WINAPI
RtlCharToInteger(
1638 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1639 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1640 ULONG
*value
) /* [O] Destination for the converted value */
1644 ULONG RunningTotal
= 0;
1647 while (*str
!= '\0' && *str
<= ' ') {
1653 } else if (*str
== '-') {
1660 if (str
[0] == '0') {
1661 if (str
[1] == 'b') {
1664 } else if (str
[1] == 'o') {
1667 } else if (str
[1] == 'x') {
1672 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1673 return STATUS_INVALID_PARAMETER
;
1676 if (value
== NULL
) {
1677 return STATUS_ACCESS_VIOLATION
;
1680 while (*str
!= '\0') {
1682 if (chCurrent
>= '0' && chCurrent
<= '9') {
1683 digit
= chCurrent
- '0';
1684 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1685 digit
= chCurrent
- 'A' + 10;
1686 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1687 digit
= chCurrent
- 'a' + 10;
1691 if (digit
< 0 || digit
>= base
) {
1692 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1693 return STATUS_SUCCESS
;
1696 RunningTotal
= RunningTotal
* base
+ digit
;
1700 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1701 return STATUS_SUCCESS
;
1705 /**************************************************************************
1706 * RtlIntegerToChar (NTDLL.@)
1708 * Converts an unsigned integer to a character string.
1711 * Success: STATUS_SUCCESS. str contains the converted number
1712 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1713 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1714 * STATUS_ACCESS_VIOLATION, if str is NULL.
1717 * Instead of base 0 it uses 10 as base.
1718 * Writes at most length characters to the string str.
1719 * Str is '\0' terminated when length allows it.
1720 * When str fits exactly in length characters the '\0' is omitted.
1722 NTSTATUS WINAPI
RtlIntegerToChar(
1723 ULONG value
, /* [I] Value to be converted */
1724 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1725 ULONG length
, /* [I] Length of the str buffer in bytes */
1726 PCHAR str
) /* [O] Destination for the converted value */
1735 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1736 return STATUS_INVALID_PARAMETER
;
1744 digit
= value
% base
;
1745 value
= value
/ base
;
1749 *pos
= 'A' + digit
- 10;
1751 } while (value
!= 0L);
1753 len
= &buffer
[32] - pos
;
1755 return STATUS_BUFFER_OVERFLOW
;
1756 } else if (str
== NULL
) {
1757 return STATUS_ACCESS_VIOLATION
;
1758 } else if (len
== length
) {
1759 memcpy(str
, pos
, len
);
1761 memcpy(str
, pos
, len
+ 1);
1763 return STATUS_SUCCESS
;
1767 /**************************************************************************
1768 * RtlUnicodeStringToInteger (NTDLL.@)
1770 * Converts an unicode string into its integer equivalent.
1773 * Success: STATUS_SUCCESS. value contains the converted number
1774 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1775 * STATUS_ACCESS_VIOLATION, if value is NULL.
1778 * For base 0 it uses 10 as base and the string should be in the format
1779 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1780 * For other bases the string should be in the format
1781 * "{whitespace} [+|-] {digits}".
1782 * No check is made for value overflow, only the lower 32 bits are assigned.
1783 * If str is NULL it crashes, as the native function does.
1786 * This function does not read garbage on string length 0 as the native
1789 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1790 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1791 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1792 ULONG
*value
) /* [O] Destination for the converted value */
1794 LPWSTR lpwstr
= str
->Buffer
;
1795 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1798 ULONG RunningTotal
= 0;
1801 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1806 if (CharsRemaining
>= 1) {
1807 if (*lpwstr
== '+') {
1810 } else if (*lpwstr
== '-') {
1819 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1820 if (lpwstr
[1] == 'b') {
1822 CharsRemaining
-= 2;
1824 } else if (lpwstr
[1] == 'o') {
1826 CharsRemaining
-= 2;
1828 } else if (lpwstr
[1] == 'x') {
1830 CharsRemaining
-= 2;
1834 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1835 return STATUS_INVALID_PARAMETER
;
1838 if (value
== NULL
) {
1839 return STATUS_ACCESS_VIOLATION
;
1842 while (CharsRemaining
>= 1) {
1843 wchCurrent
= *lpwstr
;
1844 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1845 digit
= wchCurrent
- '0';
1846 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1847 digit
= wchCurrent
- 'A' + 10;
1848 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1849 digit
= wchCurrent
- 'a' + 10;
1853 if (digit
< 0 || digit
>= base
) {
1854 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1855 return STATUS_SUCCESS
;
1858 RunningTotal
= RunningTotal
* base
+ digit
;
1863 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1864 return STATUS_SUCCESS
;
1868 /**************************************************************************
1869 * RtlIntegerToUnicodeString (NTDLL.@)
1871 * Converts an unsigned integer to a '\0' terminated unicode string.
1874 * Success: STATUS_SUCCESS. str contains the converted number
1875 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1876 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1877 * (with the '\0' termination). In this case str->Length
1878 * is set to the length, the string would have (which can
1879 * be larger than the MaximumLength).
1882 * Instead of base 0 it uses 10 as base.
1883 * If str is NULL it crashes, as the native function does.
1886 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1887 * The native function does this when the string would be longer than 16
1888 * characters even when the string parameter is long enough.
1890 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1891 ULONG value
, /* [I] Value to be converted */
1892 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1893 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1901 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1902 return STATUS_INVALID_PARAMETER
;
1910 digit
= value
% base
;
1911 value
= value
/ base
;
1915 *pos
= 'A' + digit
- 10;
1917 } while (value
!= 0L);
1919 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1920 if (str
->Length
>= str
->MaximumLength
) {
1921 return STATUS_BUFFER_OVERFLOW
;
1923 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1925 return STATUS_SUCCESS
;
1929 /*************************************************************************
1930 * RtlGUIDFromString (NTDLL.@)
1932 * Convert a string representation of a GUID into a GUID.
1935 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1936 * guid [O] Destination for the converted GUID
1939 * Success: STATUS_SUCCESS. guid contains the converted value.
1940 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1943 * See RtlStringFromGUID.
1945 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
1948 const WCHAR
*lpszCLSID
= str
->Buffer
;
1949 BYTE
* lpOut
= (BYTE
*)guid
;
1951 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
1953 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1954 * to memory: DWORD... WORD WORD BYTES............
1961 if (*lpszCLSID
!= '{')
1962 return STATUS_INVALID_PARAMETER
;
1965 case 9: case 14: case 19: case 24:
1966 if (*lpszCLSID
!= '-')
1967 return STATUS_INVALID_PARAMETER
;
1971 if (*lpszCLSID
!= '}')
1972 return STATUS_INVALID_PARAMETER
;
1977 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
1980 /* Read two hex digits as a byte value */
1981 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
1982 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
1983 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
1984 else return STATUS_INVALID_PARAMETER
;
1986 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
1987 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
1988 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
1989 else return STATUS_INVALID_PARAMETER
;
1991 byte
= ch
<< 4 | ch2
;
1995 #ifndef WORDS_BIGENDIAN
1996 /* For Big Endian machines, we store the data such that the
1997 * dword/word members can be read as DWORDS and WORDS correctly. */
1999 case 1: lpOut
[3] = byte
; break;
2000 case 3: lpOut
[2] = byte
; break;
2001 case 5: lpOut
[1] = byte
; break;
2002 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
2004 case 10: case 15: lpOut
[1] = byte
; break;
2005 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
2008 default: lpOut
[0] = byte
; lpOut
++; break;
2010 lpszCLSID
++; /* Skip 2nd character of byte */
2018 return STATUS_SUCCESS
;
2021 /*************************************************************************
2022 * RtlStringFromGUID (NTDLL.@)
2024 * Convert a GUID into a string representation of a GUID.
2027 * guid [I] GUID to convert
2028 * str [O] Destination for the converted string
2031 * Success: STATUS_SUCCESS. str contains the converted value.
2032 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
2035 * See RtlGUIDFromString.
2037 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
2039 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
2040 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
2041 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
2042 '%','0','2','X','%','0','2','X','}','\0' };
2044 TRACE("(%p,%p)\n", guid
, str
);
2046 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
2047 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
2048 str
->Buffer
= (WCHAR
*)RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
2051 str
->Length
= str
->MaximumLength
= 0;
2052 return STATUS_NO_MEMORY
;
2054 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
2055 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
2056 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
2058 return STATUS_SUCCESS
;