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
44 /**************************************************************************
45 * __wine_init_codepages (NTDLL.@)
47 * Set the code page once kernel32 is loaded. Should be done differently.
49 void CDECL
__wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
)
53 /**************************************************************************
54 * RtlInitAnsiString (NTDLL.@)
56 * Initializes a buffered ansi string.
62 * Assigns source to target->Buffer. The length of source is assigned to
63 * target->Length and target->MaximumLength. If source is NULL the length
64 * of source is assumed to be 0.
66 void WINAPI
RtlInitAnsiString(
67 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
68 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
70 if ((target
->Buffer
= (PCHAR
) source
))
72 target
->Length
= strlen(source
);
73 target
->MaximumLength
= target
->Length
+ 1;
75 else target
->Length
= target
->MaximumLength
= 0;
78 /**************************************************************************
79 * RtlInitAnsiStringEx (NTDLL.@)
81 * Initializes a buffered ansi string.
84 * An appropriate NTSTATUS value.
87 * Assigns source to target->Buffer. The length of source is assigned to
88 * target->Length and target->MaximumLength. If source is NULL the length
89 * of source is assumed to be 0.
91 NTSTATUS WINAPI
RtlInitAnsiStringEx(PANSI_STRING target
, PCSZ source
)
95 unsigned int len
= strlen(source
);
97 return STATUS_NAME_TOO_LONG
;
99 target
->Buffer
= (PCHAR
) source
;
100 target
->Length
= len
;
101 target
->MaximumLength
= len
+ 1;
105 target
->Buffer
= NULL
;
107 target
->MaximumLength
= 0;
109 return STATUS_SUCCESS
;
112 /**************************************************************************
113 * RtlInitString (NTDLL.@)
115 * Initializes a buffered string.
121 * Assigns source to target->Buffer. The length of source is assigned to
122 * target->Length and target->MaximumLength. If source is NULL the length
123 * of source is assumed to be 0.
125 void WINAPI
RtlInitString(
126 PSTRING target
, /* [I/O] Buffered string to be initialized */
127 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
129 RtlInitAnsiString( target
, source
);
133 /**************************************************************************
134 * RtlFreeAnsiString (NTDLL.@)
136 void WINAPI
RtlFreeAnsiString( PSTRING str
)
140 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
141 RtlZeroMemory( str
, sizeof(*str
) );
146 /**************************************************************************
147 * RtlFreeOemString (NTDLL.@)
149 void WINAPI
RtlFreeOemString( PSTRING str
)
151 RtlFreeAnsiString( str
);
155 /**************************************************************************
156 * RtlCopyString (NTDLL.@)
158 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
162 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
163 memcpy( dst
->Buffer
, src
->Buffer
, len
);
166 else dst
->Length
= 0;
170 /**************************************************************************
171 * RtlInitUnicodeString (NTDLL.@)
173 * Initializes a buffered unicode string.
179 * Assigns source to target->Buffer. The length of source is assigned to
180 * target->Length and target->MaximumLength. If source is NULL the length
181 * of source is assumed to be 0.
183 void WINAPI
RtlInitUnicodeString(
184 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
185 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
187 if ((target
->Buffer
= (PWSTR
) source
))
189 unsigned int length
= strlenW(source
) * sizeof(WCHAR
);
192 target
->Length
= length
;
193 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
195 else target
->Length
= target
->MaximumLength
= 0;
199 /**************************************************************************
200 * RtlInitUnicodeStringEx (NTDLL.@)
202 * Initializes a buffered unicode string.
205 * Success: STATUS_SUCCESS. target is initialized.
206 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
209 * Assigns source to target->Buffer. The length of source is assigned to
210 * target->Length and target->MaximumLength. If source is NULL the length
211 * of source is assumed to be 0.
213 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
214 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
215 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
217 if (source
!= NULL
) {
218 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
221 return STATUS_NAME_TOO_LONG
;
223 target
->Length
= len
;
224 target
->MaximumLength
= len
+ sizeof(WCHAR
);
225 target
->Buffer
= (PWSTR
) source
;
229 target
->MaximumLength
= 0;
230 target
->Buffer
= NULL
;
232 return STATUS_SUCCESS
;
236 /**************************************************************************
237 * RtlCreateUnicodeString (NTDLL.@)
239 * Creates a UNICODE_STRING from a null-terminated Unicode string.
245 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
247 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
248 if (!(target
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return FALSE
;
249 memcpy( target
->Buffer
, src
, len
);
250 target
->MaximumLength
= len
;
251 target
->Length
= len
- sizeof(WCHAR
);
256 /**************************************************************************
257 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
259 * Creates a UNICODE_STRING from a null-terminated Ascii string.
265 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
268 RtlInitAnsiString( &ansi
, src
);
269 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
273 /**************************************************************************
274 * RtlFreeUnicodeString (NTDLL.@)
276 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or
277 * RtlCreateUnicodeStringFromAsciiz().
282 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
286 RtlFreeHeap( GetProcessHeap(), 0, str
->Buffer
);
287 RtlZeroMemory( str
, sizeof(*str
) );
292 /**************************************************************************
293 * RtlCopyUnicodeString (NTDLL.@)
295 * Copies from one UNICODE_STRING to another.
300 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
304 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
305 memcpy( dst
->Buffer
, src
->Buffer
, len
);
307 /* append terminating '\0' if enough space */
308 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
310 else dst
->Length
= 0;
314 /**************************************************************************
315 * RtlDuplicateUnicodeString (NTDLL.@)
317 * Duplicates a unicode string.
320 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
321 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
322 * STATUS_NO_MEMORY, if the allocation fails.
325 * For add_nul there are several possible values:
326 * 0 = destination will not be '\0' terminated,
327 * 1 = destination will be '\0' terminated,
328 * 3 = like 1 but for an empty source string produce '\0' terminated empty
329 * Buffer instead of assigning NULL to the Buffer.
330 * Other add_nul values are invalid.
332 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
333 int add_nul
, /* [I] flag */
334 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
335 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
337 if (source
== NULL
|| destination
== NULL
||
338 source
->Length
> source
->MaximumLength
||
339 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
340 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
341 return STATUS_INVALID_PARAMETER
;
343 if (source
->Length
== 0 && add_nul
!= 3) {
344 destination
->Length
= 0;
345 destination
->MaximumLength
= 0;
346 destination
->Buffer
= NULL
;
348 unsigned int destination_max_len
= source
->Length
;
351 destination_max_len
+= sizeof(WCHAR
);
353 destination
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len
);
354 if (destination
->Buffer
== NULL
) {
355 return STATUS_NO_MEMORY
;
357 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
358 destination
->Length
= source
->Length
;
359 destination
->MaximumLength
= source
->Length
;
360 /* append terminating '\0' if enough space */
362 destination
->MaximumLength
= destination_max_len
;
363 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
368 return STATUS_SUCCESS
;
372 /**************************************************************************
373 * RtlEraseUnicodeString (NTDLL.@)
375 * Overwrites a UNICODE_STRING with zeros.
380 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
384 memset( str
->Buffer
, 0, str
->MaximumLength
);
395 /******************************************************************************
396 * RtlCompareString (NTDLL.@)
398 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
404 len
= min(s1
->Length
, s2
->Length
);
410 while (!ret
&& len
--) ret
= RtlUpperChar(*p1
++) - RtlUpperChar(*p2
++);
414 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
416 if (!ret
) ret
= s1
->Length
- s2
->Length
;
421 /******************************************************************************
422 * RtlCompareUnicodeStrings (NTDLL.@)
424 LONG WINAPI
RtlCompareUnicodeStrings( const WCHAR
*s1
, SIZE_T len1
, const WCHAR
*s2
, SIZE_T len2
,
425 BOOLEAN case_insensitive
)
428 SIZE_T len
= min( len1
, len2
);
430 if (case_insensitive
)
432 while (!ret
&& len
--) ret
= toupperW(*s1
++) - toupperW(*s2
++);
436 while (!ret
&& len
--) ret
= *s1
++ - *s2
++;
438 if (!ret
) ret
= len1
- len2
;
443 /******************************************************************************
444 * RtlCompareUnicodeString (NTDLL.@)
446 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
447 BOOLEAN CaseInsensitive
)
449 return RtlCompareUnicodeStrings( s1
->Buffer
, s1
->Length
/ sizeof(WCHAR
),
450 s2
->Buffer
, s2
->Length
/ sizeof(WCHAR
), CaseInsensitive
);
454 /**************************************************************************
455 * RtlEqualString (NTDLL.@)
457 * Determine if two strings are equal.
460 * s1 [I] Source string
461 * s2 [I] String to compare to s1
462 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
465 * Non-zero if s1 is equal to s2, 0 otherwise.
467 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
469 if (s1
->Length
!= s2
->Length
) return FALSE
;
470 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
474 /**************************************************************************
475 * RtlEqualUnicodeString (NTDLL.@)
477 * Unicode version of RtlEqualString.
479 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
480 BOOLEAN CaseInsensitive
)
482 if (s1
->Length
!= s2
->Length
) return FALSE
;
483 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
487 /**************************************************************************
488 * RtlPrefixString (NTDLL.@)
490 * Determine if one string is a prefix of another.
493 * s1 [I] Prefix to look for in s2
494 * s2 [I] String that may contain s1 as a prefix
495 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
498 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
500 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
504 if (s1
->Length
> s2
->Length
) return FALSE
;
507 for (i
= 0; i
< s1
->Length
; i
++)
508 if (RtlUpperChar(s1
->Buffer
[i
]) != RtlUpperChar(s2
->Buffer
[i
])) return FALSE
;
512 for (i
= 0; i
< s1
->Length
; i
++)
513 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
519 /**************************************************************************
520 * RtlPrefixUnicodeString (NTDLL.@)
522 * Unicode version of RtlPrefixString.
524 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
525 const UNICODE_STRING
*s2
,
526 BOOLEAN ignore_case
)
530 if (s1
->Length
> s2
->Length
) return FALSE
;
533 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
534 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
538 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
539 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
545 /**************************************************************************
546 * RtlEqualComputerName (NTDLL.@)
548 * Determine if two computer names are the same.
551 * left [I] First computer name
552 * right [I] Second computer name
555 * 0 if the names are equal, non-zero otherwise.
558 * The comparison is case insensitive.
560 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
561 const UNICODE_STRING
*right
)
564 STRING upLeft
, upRight
;
566 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
568 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
570 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
571 RtlFreeOemString( &upRight
);
573 RtlFreeOemString( &upLeft
);
579 /**************************************************************************
580 * RtlEqualDomainName (NTDLL.@)
582 * Determine if two domain names are the same.
585 * left [I] First domain name
586 * right [I] Second domain name
589 * 0 if the names are equal, non-zero otherwise.
592 * The comparison is case insensitive.
594 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
595 const UNICODE_STRING
*right
)
597 return RtlEqualComputerName(left
, right
);
602 COPY BETWEEN ANSI_STRING or UNICODE_STRING
603 there is no parameter checking, it just crashes
607 /**************************************************************************
608 * RtlAnsiStringToUnicodeString (NTDLL.@)
610 * Converts an ansi string to a unicode string.
613 * Success: STATUS_SUCCESS. uni contains the converted string
614 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
615 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
616 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
619 * This function always writes a terminating '\0'.
621 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
622 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
623 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
624 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
626 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
628 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
629 uni
->Length
= total
- sizeof(WCHAR
);
632 uni
->MaximumLength
= total
;
633 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
634 return STATUS_NO_MEMORY
;
636 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
638 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
639 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
640 return STATUS_SUCCESS
;
644 /**************************************************************************
645 * RtlOemStringToUnicodeString (NTDLL.@)
647 * Converts an oem string to a unicode string.
650 * Success: STATUS_SUCCESS. uni contains the converted string
651 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
652 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
653 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
656 * This function always writes a terminating '\0'.
658 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
659 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
660 const STRING
*oem
, /* [I] Oem string to be converted */
661 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
663 DWORD total
= RtlOemStringToUnicodeSize( oem
);
665 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
666 uni
->Length
= total
- sizeof(WCHAR
);
669 uni
->MaximumLength
= total
;
670 if (!(uni
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, total
)))
671 return STATUS_NO_MEMORY
;
673 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
675 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
676 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
677 return STATUS_SUCCESS
;
681 /**************************************************************************
682 * RtlUnicodeStringToAnsiString (NTDLL.@)
684 * Converts a unicode string to an ansi string.
687 * Success: STATUS_SUCCESS. ansi contains the converted string
688 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
689 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
692 * This function always writes a terminating '\0'.
693 * It performs a partial copy if ansi is too small.
695 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
696 STRING
*ansi
, /* [I/O] Destination for the ansi string */
697 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
698 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
700 NTSTATUS ret
= STATUS_SUCCESS
;
701 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
703 ansi
->Length
= len
- 1;
706 ansi
->MaximumLength
= len
;
707 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
708 return STATUS_NO_MEMORY
;
710 else if (ansi
->MaximumLength
< len
)
712 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
713 ansi
->Length
= ansi
->MaximumLength
- 1;
714 ret
= STATUS_BUFFER_OVERFLOW
;
717 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
718 ansi
->Buffer
[ansi
->Length
] = 0;
723 /**************************************************************************
724 * RtlUnicodeStringToOemString (NTDLL.@)
726 * Converts a Rtl Unicode string to an OEM string.
729 * oem [O] Destination for OEM string
730 * uni [I] Source Unicode string
731 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
734 * Success: STATUS_SUCCESS. oem contains the converted string
735 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
736 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
739 * If doalloc is TRUE, the length allocated is uni->Length + 1.
740 * This function always '\0' terminates the string returned.
742 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
743 const UNICODE_STRING
*uni
,
746 NTSTATUS ret
= STATUS_SUCCESS
;
747 DWORD len
= RtlUnicodeStringToOemSize( uni
);
749 oem
->Length
= len
- 1;
752 oem
->MaximumLength
= len
;
753 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
754 return STATUS_NO_MEMORY
;
756 else if (oem
->MaximumLength
< len
)
758 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
759 oem
->Length
= oem
->MaximumLength
- 1;
760 ret
= STATUS_BUFFER_OVERFLOW
;
763 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
764 oem
->Buffer
[oem
->Length
] = 0;
769 /**************************************************************************
770 * RtlUnicodeToUTF8N (NTDLL.@)
772 * Converts a Unicode string to a UTF-8 string.
777 NTSTATUS WINAPI
RtlUnicodeToUTF8N( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
778 LPCWSTR src
, DWORD srclen
)
782 if (!src
) return STATUS_INVALID_PARAMETER_4
;
783 if (!reslen
) return STATUS_INVALID_PARAMETER
;
784 if (dst
&& (srclen
& 1)) return STATUS_INVALID_PARAMETER_5
;
790 ret
= wine_utf8_wcstombs( 0, src
, srclen
/ sizeof(WCHAR
), dst
, 1 );
794 ret
= wine_utf8_wcstombs( 0, src
, srclen
/ sizeof(WCHAR
), dst
, dstlen
);
796 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
797 if (ret
< 0) return STATUS_BUFFER_TOO_SMALL
;
798 return STATUS_SUCCESS
;
807 /**************************************************************************
808 * RtlUpperChar (NTDLL.@)
810 * Converts an Ascii character to uppercase.
813 * ch [I] Character to convert
816 * The uppercase character value.
819 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
820 * All other input characters are returned unchanged. The locale and
821 * multibyte characters are not taken into account (as native DLL).
823 CHAR WINAPI
RtlUpperChar( CHAR ch
)
825 if (ch
>= 'a' && ch
<= 'z') {
826 return ch
- 'a' + 'A';
833 /**************************************************************************
834 * RtlUpperString (NTDLL.@)
836 * Converts an Ascii string to uppercase.
839 * dst [O] Destination for converted string
840 * src [I] Source string to convert
846 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
847 * All other src characters are copied unchanged to dst. The locale and
848 * multibyte characters are not taken into account (as native DLL).
849 * The number of character copied is the minimum of src->Length and
850 * the dst->MaximumLength.
852 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
854 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
856 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
861 /**************************************************************************
862 * RtlUpcaseUnicodeChar (NTDLL.@)
864 * Converts a Unicode character to uppercase.
867 * wch [I] Character to convert
870 * The uppercase character value.
872 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
874 return toupperW(wch
);
878 /**************************************************************************
879 * RtlDowncaseUnicodeChar (NTDLL.@)
881 * Converts a Unicode character to lowercase.
884 * wch [I] Character to convert
887 * The lowercase character value.
889 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
891 return tolowerW(wch
);
895 /**************************************************************************
896 * RtlUpcaseUnicodeString (NTDLL.@)
898 * Converts a Unicode string to uppercase.
901 * dest [O] Destination for converted string
902 * src [I] Source string to convert
903 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
906 * Success: STATUS_SUCCESS. dest contains the converted string.
907 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
908 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
911 * dest is never '\0' terminated because it may be equal to src, and src
912 * might not be '\0' terminated. dest->Length is only set upon success.
914 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
915 const UNICODE_STRING
*src
,
918 DWORD i
, len
= src
->Length
;
922 dest
->MaximumLength
= len
;
923 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
924 return STATUS_NO_MEMORY
;
926 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
928 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
930 return STATUS_SUCCESS
;
934 /**************************************************************************
935 * RtlDowncaseUnicodeString (NTDLL.@)
937 * Converts a Unicode string to lowercase.
940 * dest [O] Destination for converted string
941 * src [I] Source string to convert
942 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
945 * Success: STATUS_SUCCESS. dest contains the converted string.
946 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
947 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
950 * dest is never '\0' terminated because it may be equal to src, and src
951 * might not be '\0' terminated. dest->Length is only set upon success.
953 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
954 UNICODE_STRING
*dest
,
955 const UNICODE_STRING
*src
,
959 DWORD len
= src
->Length
;
962 dest
->MaximumLength
= len
;
963 if (!(dest
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) {
964 return STATUS_NO_MEMORY
;
966 } else if (len
> dest
->MaximumLength
) {
967 return STATUS_BUFFER_OVERFLOW
;
970 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
971 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
974 return STATUS_SUCCESS
;
978 /**************************************************************************
979 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
981 * Converts a Unicode string to the equivalent ANSI upper-case representation.
987 * writes terminating 0
989 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*ansi
,
990 const UNICODE_STRING
*uni
,
993 NTSTATUS ret
= STATUS_SUCCESS
;
994 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
996 ansi
->Length
= len
- 1;
999 ansi
->MaximumLength
= len
;
1000 if (!(ansi
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1001 return STATUS_NO_MEMORY
;
1003 else if (ansi
->MaximumLength
< len
)
1005 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1006 ansi
->Length
= ansi
->MaximumLength
- 1;
1007 ret
= STATUS_BUFFER_OVERFLOW
;
1010 RtlUpcaseUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
1011 ansi
->Buffer
[ansi
->Length
] = 0;
1016 /**************************************************************************
1017 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
1019 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1020 * stored in STRING format.
1026 * writes terminating 0
1028 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*oem
,
1029 const UNICODE_STRING
*uni
,
1032 NTSTATUS ret
= STATUS_SUCCESS
;
1033 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
1035 oem
->Length
= len
- 1;
1038 oem
->MaximumLength
= len
;
1039 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
)))
1040 return STATUS_NO_MEMORY
;
1042 else if (oem
->MaximumLength
< len
)
1044 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
1045 oem
->Length
= oem
->MaximumLength
- 1;
1046 ret
= STATUS_BUFFER_OVERFLOW
;
1049 RtlUpcaseUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
1050 oem
->Buffer
[oem
->Length
] = 0;
1055 /**************************************************************************
1056 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
1058 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
1059 * stored in STRING format.
1065 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1067 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1068 const UNICODE_STRING
*uni
,
1071 NTSTATUS ret
= STATUS_SUCCESS
;
1072 DWORD len
= RtlUnicodeStringToOemSize( uni
) - 1;
1077 oem
->MaximumLength
= len
;
1078 if (!(oem
->Buffer
= RtlAllocateHeap( GetProcessHeap(), 0, len
))) return STATUS_NO_MEMORY
;
1080 else if (oem
->MaximumLength
< len
)
1082 ret
= STATUS_BUFFER_OVERFLOW
;
1083 oem
->Length
= oem
->MaximumLength
;
1084 if (!oem
->MaximumLength
) return ret
;
1086 RtlUpcaseUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
1091 /**************************************************************************
1092 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1094 * Converts a Unicode string to the equivalent ANSI upper-case representation.
1099 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1100 LPCWSTR src
, DWORD srclen
)
1106 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1107 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1108 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1109 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1114 /**************************************************************************
1115 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1117 * Converts a Unicode string to the equivalent OEM upper-case representation.
1122 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1123 LPCWSTR src
, DWORD srclen
)
1129 if (!(upcase
= RtlAllocateHeap( GetProcessHeap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1130 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1131 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1132 RtlFreeHeap( GetProcessHeap(), 0, upcase
);
1142 /**************************************************************************
1143 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1144 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1146 * Calculate the size in bytes necessary for the Unicode conversion of str,
1147 * including the terminating '\0'.
1150 * str [I] String to calculate the size of
1153 * The calculated size.
1155 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1158 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1159 return ret
+ sizeof(WCHAR
);
1163 /**************************************************************************
1164 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1165 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1167 * Calculate the size in bytes necessary for the Ansi conversion of str,
1168 * including the terminating '\0'.
1171 * str [I] String to calculate the size of
1174 * The calculated size.
1176 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1179 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1184 /**************************************************************************
1185 * RtlAppendAsciizToString (NTDLL.@)
1187 * Concatenates a buffered character string and a '\0' terminated character
1191 * Success: STATUS_SUCCESS. src is appended to dest.
1192 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1193 * to hold the concatenated string.
1196 * if src is NULL dest is unchanged.
1197 * dest is never '\0' terminated.
1199 NTSTATUS WINAPI
RtlAppendAsciizToString(
1200 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1201 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1204 unsigned int src_len
= strlen(src
);
1205 unsigned int dest_len
= src_len
+ dest
->Length
;
1207 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1208 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1209 dest
->Length
= dest_len
;
1211 return STATUS_SUCCESS
;
1215 /**************************************************************************
1216 * RtlAppendStringToString (NTDLL.@)
1218 * Concatenates two buffered character strings
1221 * Success: STATUS_SUCCESS. src is appended to dest.
1222 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1223 * to hold the concatenated string.
1226 * if src->length is zero dest is unchanged.
1227 * dest is never '\0' terminated.
1229 NTSTATUS WINAPI
RtlAppendStringToString(
1230 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1231 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1233 if (src
->Length
!= 0) {
1234 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1236 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1237 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1238 dest
->Length
= dest_len
;
1240 return STATUS_SUCCESS
;
1244 /**************************************************************************
1245 * RtlAppendUnicodeToString (NTDLL.@)
1247 * Concatenates a buffered unicode string and a '\0' terminated unicode
1251 * Success: STATUS_SUCCESS. src is appended to dest.
1252 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1253 * to hold the concatenated string.
1256 * if src is NULL dest is unchanged.
1257 * dest is '\0' terminated when the MaximumLength allows it.
1258 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1261 * Does not write in the src->Buffer beyond MaximumLength when
1262 * MaximumLength is odd as the native function does.
1264 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1265 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1266 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1269 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1270 unsigned int dest_len
= src_len
+ dest
->Length
;
1272 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1273 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1274 dest
->Length
= dest_len
;
1275 /* append terminating '\0' if enough space */
1276 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1277 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1280 return STATUS_SUCCESS
;
1284 /**************************************************************************
1285 * RtlAppendUnicodeStringToString (NTDLL.@)
1287 * Concatenates two buffered unicode strings
1290 * Success: STATUS_SUCCESS. src is appended to dest.
1291 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1292 * to hold the concatenated string.
1295 * if src->length is zero dest is unchanged.
1296 * dest is '\0' terminated when the MaximumLength allows it.
1297 * When dest fits exactly in MaximumLength characters the '\0' is omitted.
1300 * Does not write in the src->Buffer beyond MaximumLength when
1301 * MaximumLength is odd as the native function does.
1303 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1304 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1305 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1307 if (src
->Length
!= 0) {
1308 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1310 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1311 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1312 dest
->Length
= dest_len
;
1313 /* append terminating '\0' if enough space */
1314 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1315 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1318 return STATUS_SUCCESS
;
1322 /**************************************************************************
1323 * RtlFindCharInUnicodeString (NTDLL.@)
1325 * Searches for one of several unicode characters in a unicode string.
1328 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1329 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1331 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1332 int flags
, /* [I] Flags */
1333 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1334 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1335 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1337 unsigned int main_idx
, search_idx
;
1341 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1342 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1343 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1344 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1345 return STATUS_SUCCESS
;
1350 return STATUS_NOT_FOUND
;
1352 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1353 while (main_idx
-- > 0) {
1354 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1355 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1356 *pos
= main_idx
* sizeof(WCHAR
);
1357 return STATUS_SUCCESS
;
1362 return STATUS_NOT_FOUND
;
1364 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1366 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1367 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1370 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1371 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1372 return STATUS_SUCCESS
;
1376 return STATUS_NOT_FOUND
;
1378 main_idx
= main_str
->Length
/ sizeof(WCHAR
);
1379 while (main_idx
-- > 0) {
1381 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1382 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1385 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1386 *pos
= main_idx
* sizeof(WCHAR
);
1387 return STATUS_SUCCESS
;
1391 return STATUS_NOT_FOUND
;
1393 return STATUS_NOT_FOUND
;
1401 /**************************************************************************
1402 * RtlIsTextUnicode (NTDLL.@)
1404 * Attempt to guess whether a text buffer is Unicode.
1407 * buf [I] Text buffer to test
1408 * len [I] Length of buf
1409 * pf [O] Destination for test results
1412 * TRUE if the buffer is likely Unicode, FALSE otherwise.
1415 * Should implement more tests.
1417 BOOLEAN WINAPI
RtlIsTextUnicode( LPCVOID buf
, INT len
, INT
*pf
)
1419 static const WCHAR std_control_chars
[] = {'\r','\n','\t',' ',0x3000,0};
1420 static const WCHAR byterev_control_chars
[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1421 const WCHAR
*s
= buf
;
1423 unsigned int flags
= ~0U, out_flags
= 0;
1425 if (len
< sizeof(WCHAR
))
1427 /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
1434 * Apply various tests to the text string. According to the
1435 * docs, each test "passed" sets the corresponding flag in
1436 * the output flags. But some of the tests are mutually
1437 * exclusive, so I don't see how you could pass all tests ...
1440 /* Check for an odd length ... pass if even. */
1441 if (len
& 1) out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1443 if (((const char *)buf
)[len
- 1] == 0)
1444 len
--; /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES */
1446 len
/= sizeof(WCHAR
);
1447 /* Windows only checks the first 256 characters */
1448 if (len
> 256) len
= 256;
1450 /* Check for the special byte order unicode marks. */
1451 if (*s
== 0xFEFF) out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1452 if (*s
== 0xFFFE) out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1454 /* apply some statistical analysis */
1455 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1458 /* FIXME: checks only for ASCII characters in the unicode stream */
1459 for (i
= 0; i
< len
; i
++)
1461 if (s
[i
] <= 255) stats
++;
1463 if (stats
> len
/ 2)
1464 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1467 /* Check for unicode NULL chars */
1468 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1470 for (i
= 0; i
< len
; i
++)
1472 if (!(s
[i
] & 0xff) || !(s
[i
] >> 8))
1474 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1480 if (flags
& IS_TEXT_UNICODE_CONTROLS
)
1482 for (i
= 0; i
< len
; i
++)
1484 if (strchrW(std_control_chars
, s
[i
]))
1486 out_flags
|= IS_TEXT_UNICODE_CONTROLS
;
1492 if (flags
& IS_TEXT_UNICODE_REVERSE_CONTROLS
)
1494 for (i
= 0; i
< len
; i
++)
1496 if (strchrW(byterev_control_chars
, s
[i
]))
1498 out_flags
|= IS_TEXT_UNICODE_REVERSE_CONTROLS
;
1509 /* check for flags that indicate it's definitely not valid Unicode */
1510 if (out_flags
& (IS_TEXT_UNICODE_REVERSE_MASK
| IS_TEXT_UNICODE_NOT_UNICODE_MASK
)) return FALSE
;
1511 /* now check for invalid ASCII, and assume Unicode if so */
1512 if (out_flags
& IS_TEXT_UNICODE_NOT_ASCII_MASK
) return TRUE
;
1513 /* now check for Unicode flags */
1514 if (out_flags
& IS_TEXT_UNICODE_UNICODE_MASK
) return TRUE
;
1520 /**************************************************************************
1521 * RtlCharToInteger (NTDLL.@)
1523 * Converts a character string into its integer equivalent.
1526 * Success: STATUS_SUCCESS. value contains the converted number
1527 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1528 * STATUS_ACCESS_VIOLATION, if value is NULL.
1531 * For base 0 it uses 10 as base and the string should be in the format
1532 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1533 * For other bases the string should be in the format
1534 * "{whitespace} [+|-] {digits}".
1535 * No check is made for value overflow, only the lower 32 bits are assigned.
1536 * If str is NULL it crashes, as the native function does.
1539 * This function does not read garbage behind '\0' as the native version does.
1541 NTSTATUS WINAPI
RtlCharToInteger(
1542 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1543 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1544 ULONG
*value
) /* [O] Destination for the converted value */
1548 ULONG RunningTotal
= 0;
1549 BOOL bMinus
= FALSE
;
1551 while (*str
!= '\0' && *str
<= ' ') {
1557 } else if (*str
== '-') {
1564 if (str
[0] == '0') {
1565 if (str
[1] == 'b') {
1568 } else if (str
[1] == 'o') {
1571 } else if (str
[1] == 'x') {
1576 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1577 return STATUS_INVALID_PARAMETER
;
1580 if (value
== NULL
) {
1581 return STATUS_ACCESS_VIOLATION
;
1584 while (*str
!= '\0') {
1586 if (chCurrent
>= '0' && chCurrent
<= '9') {
1587 digit
= chCurrent
- '0';
1588 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1589 digit
= chCurrent
- 'A' + 10;
1590 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1591 digit
= chCurrent
- 'a' + 10;
1595 if (digit
< 0 || digit
>= base
) {
1596 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1597 return STATUS_SUCCESS
;
1600 RunningTotal
= RunningTotal
* base
+ digit
;
1604 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1605 return STATUS_SUCCESS
;
1609 /**************************************************************************
1610 * RtlIntegerToChar (NTDLL.@)
1612 * Converts an unsigned integer to a character string.
1615 * Success: STATUS_SUCCESS. str contains the converted number
1616 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1617 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1618 * STATUS_ACCESS_VIOLATION, if str is NULL.
1621 * Instead of base 0 it uses 10 as base.
1622 * Writes at most length characters to the string str.
1623 * Str is '\0' terminated when length allows it.
1624 * When str fits exactly in length characters the '\0' is omitted.
1626 NTSTATUS WINAPI
RtlIntegerToChar(
1627 ULONG value
, /* [I] Value to be converted */
1628 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1629 ULONG length
, /* [I] Length of the str buffer in bytes */
1630 PCHAR str
) /* [O] Destination for the converted value */
1639 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1640 return STATUS_INVALID_PARAMETER
;
1648 digit
= value
% base
;
1649 value
= value
/ base
;
1653 *pos
= 'A' + digit
- 10;
1655 } while (value
!= 0L);
1657 len
= &buffer
[32] - pos
;
1659 return STATUS_BUFFER_OVERFLOW
;
1660 } else if (str
== NULL
) {
1661 return STATUS_ACCESS_VIOLATION
;
1662 } else if (len
== length
) {
1663 memcpy(str
, pos
, len
);
1665 memcpy(str
, pos
, len
+ 1);
1667 return STATUS_SUCCESS
;
1671 /**************************************************************************
1672 * RtlUnicodeStringToInteger (NTDLL.@)
1674 * Converts a unicode string into its integer equivalent.
1677 * Success: STATUS_SUCCESS. value contains the converted number
1678 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1679 * STATUS_ACCESS_VIOLATION, if value is NULL.
1682 * For base 0 it uses 10 as base and the string should be in the format
1683 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1684 * For other bases the string should be in the format
1685 * "{whitespace} [+|-] {digits}".
1686 * No check is made for value overflow, only the lower 32 bits are assigned.
1687 * If str is NULL it crashes, as the native function does.
1690 * This function does not read garbage on string length 0 as the native
1693 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1694 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1695 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1696 ULONG
*value
) /* [O] Destination for the converted value */
1698 LPWSTR lpwstr
= str
->Buffer
;
1699 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1702 ULONG RunningTotal
= 0;
1703 BOOL bMinus
= FALSE
;
1705 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1710 if (CharsRemaining
>= 1) {
1711 if (*lpwstr
== '+') {
1714 } else if (*lpwstr
== '-') {
1723 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1724 if (lpwstr
[1] == 'b') {
1726 CharsRemaining
-= 2;
1728 } else if (lpwstr
[1] == 'o') {
1730 CharsRemaining
-= 2;
1732 } else if (lpwstr
[1] == 'x') {
1734 CharsRemaining
-= 2;
1738 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1739 return STATUS_INVALID_PARAMETER
;
1742 if (value
== NULL
) {
1743 return STATUS_ACCESS_VIOLATION
;
1746 while (CharsRemaining
>= 1) {
1747 wchCurrent
= *lpwstr
;
1748 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1749 digit
= wchCurrent
- '0';
1750 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1751 digit
= wchCurrent
- 'A' + 10;
1752 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1753 digit
= wchCurrent
- 'a' + 10;
1757 if (digit
< 0 || digit
>= base
) {
1758 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1759 return STATUS_SUCCESS
;
1762 RunningTotal
= RunningTotal
* base
+ digit
;
1767 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1768 return STATUS_SUCCESS
;
1772 /**************************************************************************
1773 * RtlIntegerToUnicodeString (NTDLL.@)
1775 * Converts an unsigned integer to a '\0' terminated unicode string.
1778 * Success: STATUS_SUCCESS. str contains the converted number
1779 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1780 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1781 * (with the '\0' termination). In this case str->Length
1782 * is set to the length, the string would have (which can
1783 * be larger than the MaximumLength).
1786 * Instead of base 0 it uses 10 as base.
1787 * If str is NULL it crashes, as the native function does.
1790 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1791 * The native function does this when the string would be longer than 16
1792 * characters even when the string parameter is long enough.
1794 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1795 ULONG value
, /* [I] Value to be converted */
1796 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1797 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1805 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1806 return STATUS_INVALID_PARAMETER
;
1814 digit
= value
% base
;
1815 value
= value
/ base
;
1819 *pos
= 'A' + digit
- 10;
1821 } while (value
!= 0L);
1823 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1824 if (str
->Length
>= str
->MaximumLength
) {
1825 return STATUS_BUFFER_OVERFLOW
;
1827 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1829 return STATUS_SUCCESS
;
1833 /*************************************************************************
1834 * RtlGUIDFromString (NTDLL.@)
1836 * Convert a string representation of a GUID into a GUID.
1839 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1840 * guid [O] Destination for the converted GUID
1843 * Success: STATUS_SUCCESS. guid contains the converted value.
1844 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1847 * See RtlStringFromGUID.
1849 NTSTATUS WINAPI
RtlGUIDFromString(PUNICODE_STRING str
, GUID
* guid
)
1852 const WCHAR
*lpszCLSID
= str
->Buffer
;
1853 BYTE
* lpOut
= (BYTE
*)guid
;
1855 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
1857 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1858 * to memory: DWORD... WORD WORD BYTES............
1865 if (*lpszCLSID
!= '{')
1866 return STATUS_INVALID_PARAMETER
;
1869 case 9: case 14: case 19: case 24:
1870 if (*lpszCLSID
!= '-')
1871 return STATUS_INVALID_PARAMETER
;
1875 if (*lpszCLSID
!= '}')
1876 return STATUS_INVALID_PARAMETER
;
1881 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
1884 /* Read two hex digits as a byte value */
1885 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
1886 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
1887 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
1888 else return STATUS_INVALID_PARAMETER
;
1890 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
1891 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
1892 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
1893 else return STATUS_INVALID_PARAMETER
;
1895 byte
= ch
<< 4 | ch2
;
1899 #ifndef WORDS_BIGENDIAN
1900 /* For Big Endian machines, we store the data such that the
1901 * dword/word members can be read as DWORDS and WORDS correctly. */
1903 case 1: lpOut
[3] = byte
; break;
1904 case 3: lpOut
[2] = byte
; break;
1905 case 5: lpOut
[1] = byte
; break;
1906 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
1908 case 10: case 15: lpOut
[1] = byte
; break;
1909 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
1912 default: lpOut
[0] = byte
; lpOut
++; break;
1914 lpszCLSID
++; /* Skip 2nd character of byte */
1922 return STATUS_SUCCESS
;
1925 /*************************************************************************
1926 * RtlStringFromGUID (NTDLL.@)
1928 * Convert a GUID into a string representation of a GUID.
1931 * guid [I] GUID to convert
1932 * str [O] Destination for the converted string
1935 * Success: STATUS_SUCCESS. str contains the converted value.
1936 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
1939 * See RtlGUIDFromString.
1941 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
1943 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
1944 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
1945 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
1946 '%','0','2','X','%','0','2','X','}','\0' };
1948 TRACE("(%p,%p)\n", guid
, str
);
1950 str
->Length
= GUID_STRING_LENGTH
* sizeof(WCHAR
);
1951 str
->MaximumLength
= str
->Length
+ sizeof(WCHAR
);
1952 str
->Buffer
= RtlAllocateHeap(GetProcessHeap(), 0, str
->MaximumLength
);
1955 str
->Length
= str
->MaximumLength
= 0;
1956 return STATUS_NO_MEMORY
;
1958 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
1959 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
1960 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
1962 return STATUS_SUCCESS
;
1965 /******************************************************************************
1966 * RtlHashUnicodeString [NTDLL.@]
1968 NTSTATUS WINAPI
RtlHashUnicodeString(PCUNICODE_STRING string
, BOOLEAN case_insensitive
, ULONG alg
, ULONG
*hash
)
1972 if (!string
|| !hash
) return STATUS_INVALID_PARAMETER
;
1976 case HASH_STRING_ALGORITHM_DEFAULT
:
1977 case HASH_STRING_ALGORITHM_X65599
:
1980 return STATUS_INVALID_PARAMETER
;
1984 for (i
= 0; i
< string
->Length
/sizeof(WCHAR
); i
++)
1985 *hash
= *hash
*65599 + (case_insensitive
? toupperW(string
->Buffer
[i
]) : string
->Buffer
[i
]);
1987 return STATUS_SUCCESS
;