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
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "ntdll_misc.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
41 UINT NlsAnsiCodePage
= 0;
42 BYTE NlsMbCodePageTag
= 0;
43 BYTE NlsMbOemCodePageTag
= 0;
45 static const union cptable
*ansi_table
;
46 static const union cptable
*oem_table
;
47 static const union cptable
* unix_table
; /* NULL if UTF8 */
50 /**************************************************************************
51 * __wine_init_codepages (NTDLL.@)
53 * Set the code page once kernel32 is loaded. Should be done differently.
55 void __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
,
56 const union cptable
*ucp
)
61 NlsAnsiCodePage
= ansi
->info
.codepage
;
64 int ntdll_umbstowcs(DWORD flags
, const char* src
, int srclen
, WCHAR
* dst
, int dstlen
)
67 wine_cp_mbstowcs( unix_table
, flags
, src
, srclen
, dst
, dstlen
) :
68 wine_utf8_mbstowcs( flags
, src
, srclen
, dst
, dstlen
);
71 int ntdll_wcstoumbs(DWORD flags
, const WCHAR
* src
, int srclen
, char* dst
, int dstlen
,
72 const char* defchar
, int *used
)
75 wine_cp_wcstombs( unix_table
, flags
, src
, srclen
, dst
, dstlen
, defchar
, used
) :
76 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( ntdll_get_process_heap(), 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 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
227 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
228 if (!(target
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) return FALSE
;
229 memcpy( target
->Buffer
, src
, len
);
230 target
->MaximumLength
= len
;
231 target
->Length
= len
- sizeof(WCHAR
);
236 /**************************************************************************
237 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
239 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
242 RtlInitAnsiString( &ansi
, src
);
243 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
247 /**************************************************************************
248 * RtlFreeUnicodeString (NTDLL.@)
250 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
252 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
256 /**************************************************************************
257 * RtlCopyUnicodeString (NTDLL.@)
259 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
263 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
264 memcpy( dst
->Buffer
, src
->Buffer
, len
);
266 /* append terminating '\0' if enough space */
267 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
269 else dst
->Length
= 0;
273 /**************************************************************************
274 * RtlDuplicateUnicodeString (NTDLL.@)
276 * Duplicates an unicode string.
279 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
280 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
281 * STATUS_NO_MEMORY, if the allocation fails.
284 * For add_nul there are several possible values:
285 * 0 = destination will not be '\0' terminated,
286 * 1 = destination will be '\0' terminated,
287 * 3 = like 1 but for an empty source string produce '\0' terminated empty
288 * Buffer instead of assigning NULL to the Buffer.
289 * Other add_nul values are invalid.
291 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
292 int add_nul
, /* [I] flag */
293 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
294 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
296 if (source
== NULL
|| destination
== NULL
||
297 source
->Length
> source
->MaximumLength
||
298 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
299 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
300 return STATUS_INVALID_PARAMETER
;
302 if (source
->Length
== 0 && add_nul
!= 3) {
303 destination
->Length
= 0;
304 destination
->MaximumLength
= 0;
305 destination
->Buffer
= NULL
;
307 unsigned int destination_max_len
= source
->Length
;
310 destination_max_len
+= sizeof(WCHAR
);
312 destination
->Buffer
= RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len
);
313 if (destination
->Buffer
== NULL
) {
314 return STATUS_NO_MEMORY
;
316 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
317 destination
->Length
= source
->Length
;
318 destination
->MaximumLength
= source
->Length
;
319 /* append terminating '\0' if enough space */
321 destination
->MaximumLength
= destination_max_len
;
322 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
327 return STATUS_SUCCESS
;
331 /**************************************************************************
332 * RtlEraseUnicodeString (NTDLL.@)
334 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
338 memset( str
->Buffer
, 0, str
->MaximumLength
);
349 /******************************************************************************
350 * RtlCompareString (NTDLL.@)
352 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
358 len
= min(s1
->Length
, s2
->Length
);
364 while (!ret
&& len
--) ret
= toupper(*p1
++) - toupper(*p2
++);
368 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
370 if (!ret
) ret
= s1
->Length
- s2
->Length
;
375 /******************************************************************************
376 * RtlCompareUnicodeString (NTDLL.@)
378 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
379 BOOLEAN CaseInsensitive
)
385 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
391 while (!ret
&& len
--) ret
= toupperW(*p1
++) - toupperW(*p2
++);
395 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
397 if (!ret
) ret
= s1
->Length
- s2
->Length
;
402 /**************************************************************************
403 * RtlEqualString (NTDLL.@)
405 * Determine if two strings are equal.
408 * s1 [I] Source string
409 * s2 [I] String to compare to s1
410 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
413 * Non-zero if s1 is equal to s2, 0 otherwise.
415 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
417 if (s1
->Length
!= s2
->Length
) return FALSE
;
418 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
422 /**************************************************************************
423 * RtlEqualUnicodeString (NTDLL.@)
425 * Unicode version of RtlEqualString.
427 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
428 BOOLEAN CaseInsensitive
)
430 if (s1
->Length
!= s2
->Length
) return FALSE
;
431 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
435 /**************************************************************************
436 * RtlPrefixString (NTDLL.@)
438 * Determine if one string is a prefix of another.
441 * s1 [I] Prefix to look for in s2
442 * s2 [I] String that may contain s1 as a prefix
443 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
446 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
448 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
452 if (s1
->Length
> s2
->Length
) return FALSE
;
455 for (i
= 0; i
< s1
->Length
; i
++)
456 if (toupper(s1
->Buffer
[i
]) != toupper(s2
->Buffer
[i
])) return FALSE
;
460 for (i
= 0; i
< s1
->Length
; i
++)
461 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
467 /**************************************************************************
468 * RtlPrefixUnicodeString (NTDLL.@)
470 * Unicode version of RtlPrefixString.
472 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
473 const UNICODE_STRING
*s2
,
474 BOOLEAN ignore_case
)
478 if (s1
->Length
> s2
->Length
) return FALSE
;
481 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
482 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
486 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
487 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
493 /**************************************************************************
494 * RtlEqualComputerName (NTDLL.@)
496 * Determine if two computer names are the same.
499 * left [I] First computer name
500 * right [I] Second computer name
503 * 0 if the names are equal, non-zero otherwise.
506 * The comparason is case insensitive.
508 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
509 const UNICODE_STRING
*right
)
512 STRING upLeft
, upRight
;
514 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
516 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
518 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
519 RtlFreeOemString( &upRight
);
521 RtlFreeOemString( &upLeft
);
527 /**************************************************************************
528 * RtlEqualDomainName (NTDLL.@)
530 * Determine if two domain names are the same.
533 * left [I] First domain name
534 * right [I] Second domain name
537 * 0 if the names are equal, non-zero otherwise.
540 * The comparason is case insensitive.
542 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
543 const UNICODE_STRING
*right
)
545 return RtlEqualComputerName(left
, right
);
550 COPY BETWEEN ANSI_STRING or UNICODE_STRING
551 there is no parameter checking, it just crashes
555 /**************************************************************************
556 * RtlAnsiStringToUnicodeString (NTDLL.@)
558 * Converts an ansi string to an unicode string.
561 * Success: STATUS_SUCCESS. uni contains the converted string
562 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
563 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
564 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
567 * This function always writes a terminating '\0'.
569 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
570 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
571 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
572 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
574 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
576 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
577 uni
->Length
= total
- sizeof(WCHAR
);
580 uni
->MaximumLength
= total
;
581 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
582 return STATUS_NO_MEMORY
;
584 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
586 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
587 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
588 return STATUS_SUCCESS
;
592 /**************************************************************************
593 * RtlOemStringToUnicodeString (NTDLL.@)
595 * Converts an oem string to an unicode string.
598 * Success: STATUS_SUCCESS. uni contains the converted string
599 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
600 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
601 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
604 * This function always writes a terminating '\0'.
606 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
607 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
608 const STRING
*oem
, /* [I] Oem string to be converted */
609 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
611 DWORD total
= RtlOemStringToUnicodeSize( oem
);
613 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
614 uni
->Length
= total
- sizeof(WCHAR
);
617 uni
->MaximumLength
= total
;
618 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
619 return STATUS_NO_MEMORY
;
621 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
623 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
624 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
625 return STATUS_SUCCESS
;
629 /**************************************************************************
630 * RtlUnicodeStringToAnsiString (NTDLL.@)
632 * Converts an unicode string to an ansi string.
635 * Success: STATUS_SUCCESS. ansi contains the converted string
636 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
637 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
640 * This function always writes a terminating '\0'.
641 * It performs a partial copy if ansi is too small.
643 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
644 STRING
*ansi
, /* [I/O] Destination for the ansi string */
645 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
646 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
648 NTSTATUS ret
= STATUS_SUCCESS
;
649 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
651 ansi
->Length
= len
- 1;
654 ansi
->MaximumLength
= len
;
655 if (!(ansi
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
656 return STATUS_NO_MEMORY
;
658 else if (ansi
->MaximumLength
< len
)
660 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
661 ansi
->Length
= ansi
->MaximumLength
- 1;
662 ret
= STATUS_BUFFER_OVERFLOW
;
665 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
666 ansi
->Buffer
[ansi
->Length
] = 0;
671 /**************************************************************************
672 * RtlUnicodeStringToOemString (NTDLL.@)
674 * Converts a Rtl Unicode string to an OEM string.
677 * oem [O] Destination for OEM string
678 * uni [I] Source Unicode string
679 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
682 * Success: STATUS_SUCCESS. oem contains the converted string
683 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
684 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
687 * If doalloc is TRUE, the length allocated is uni->Length + 1.
688 * This function always '\0' terminates the string returned.
690 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
691 const UNICODE_STRING
*uni
,
694 NTSTATUS ret
= STATUS_SUCCESS
;
695 DWORD len
= RtlUnicodeStringToOemSize( uni
);
697 oem
->Length
= len
- 1;
700 oem
->MaximumLength
= len
;
701 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
702 return STATUS_NO_MEMORY
;
704 else if (oem
->MaximumLength
< len
)
706 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
707 oem
->Length
= oem
->MaximumLength
- 1;
708 ret
= STATUS_BUFFER_OVERFLOW
;
711 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
712 oem
->Buffer
[oem
->Length
] = 0;
717 /**************************************************************************
718 * RtlMultiByteToUnicodeN (NTDLL.@)
721 * Performs a partial copy if dst is too small.
723 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
724 LPCSTR src
, DWORD srclen
)
727 int ret
= wine_cp_mbstowcs( ansi_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
729 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
730 return STATUS_SUCCESS
;
734 /**************************************************************************
735 * RtlOemToUnicodeN (NTDLL.@)
737 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
738 LPCSTR src
, DWORD srclen
)
740 int ret
= wine_cp_mbstowcs( oem_table
, 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
742 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
743 return STATUS_SUCCESS
;
747 /**************************************************************************
748 * RtlUnicodeToMultiByteN (NTDLL.@)
750 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
751 LPCWSTR src
, DWORD srclen
)
753 int ret
= wine_cp_wcstombs( ansi_table
, 0, src
, srclen
/ sizeof(WCHAR
),
754 dst
, dstlen
, NULL
, NULL
);
756 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
757 return STATUS_SUCCESS
;
761 /**************************************************************************
762 * RtlUnicodeToOemN (NTDLL.@)
764 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
765 LPCWSTR src
, DWORD srclen
)
767 int ret
= wine_cp_wcstombs( oem_table
, 0, src
, srclen
/ sizeof(WCHAR
),
768 dst
, dstlen
, NULL
, NULL
);
770 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
771 return STATUS_SUCCESS
;
780 /**************************************************************************
781 * RtlUpperChar (NTDLL.@)
783 * Converts an Ascii character to uppercase.
786 * ch [I] Character to convert
789 * The uppercase character value.
792 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
793 * All other input characters are returned unchanged. The locale and
794 * multibyte characters are not taken into account (as native DLL).
796 CHAR WINAPI
RtlUpperChar( CHAR ch
)
798 if (ch
>= 'a' && ch
<= 'z') {
799 return ch
- 'a' + 'A';
806 /**************************************************************************
807 * RtlUpperString (NTDLL.@)
809 * Converts an Ascii string to uppercase.
812 * dst [O] Destination for converted string
813 * src [I] Source string to convert
819 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
820 * All other src characters are copied unchanged to dst. The locale and
821 * multibyte characters are not taken into account (as native DLL).
822 * The number of character copied is the minimum of src->Length and
823 * the dst->MaximumLength.
825 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
827 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
829 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
834 /**************************************************************************
835 * RtlUpcaseUnicodeChar (NTDLL.@)
837 * Converts an Unicode character to uppercase.
840 * wch [I] Character to convert
843 * The uppercase character value.
845 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
847 return toupperW(wch
);
851 /**************************************************************************
852 * RtlDowncaseUnicodeChar (NTDLL.@)
854 * Converts an Unicode character to lowercase.
857 * wch [I] Character to convert
860 * The lowercase character value.
862 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
864 return tolowerW(wch
);
868 /**************************************************************************
869 * RtlUpcaseUnicodeString (NTDLL.@)
871 * Converts an Unicode string to uppercase.
874 * dest [O] Destination for converted string
875 * src [I] Source string to convert
876 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
879 * Success: STATUS_SUCCESS. dest contains the converted string.
880 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
881 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
884 * dest is never '\0' terminated because it may be equal to src, and src
885 * might not be '\0' terminated. dest->Length is only set upon success.
887 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
888 const UNICODE_STRING
*src
,
891 DWORD i
, len
= src
->Length
;
895 dest
->MaximumLength
= len
;
896 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
897 return STATUS_NO_MEMORY
;
899 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
901 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
903 return STATUS_SUCCESS
;
907 /**************************************************************************
908 * RtlDowncaseUnicodeString (NTDLL.@)
910 * Converts an Unicode string to lowercase.
913 * dest [O] Destination for converted string
914 * src [I] Source string to convert
915 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
918 * Success: STATUS_SUCCESS. dest contains the converted string.
919 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
920 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
923 * dest is never '\0' terminated because it may be equal to src, and src
924 * might not be '\0' terminated. dest->Length is only set upon success.
926 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
927 UNICODE_STRING
*dest
,
928 const UNICODE_STRING
*src
,
932 DWORD len
= src
->Length
;
935 dest
->MaximumLength
= len
;
936 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) {
937 return STATUS_NO_MEMORY
;
939 } else if (len
> dest
->MaximumLength
) {
940 return STATUS_BUFFER_OVERFLOW
;
943 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
944 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
947 return STATUS_SUCCESS
;
951 /**************************************************************************
952 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
955 * writes terminating 0
957 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
958 const UNICODE_STRING
*src
,
962 UNICODE_STRING upcase
;
964 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
966 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
967 RtlFreeUnicodeString( &upcase
);
973 /**************************************************************************
974 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
977 * writes terminating 0
979 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
980 const UNICODE_STRING
*src
,
984 UNICODE_STRING upcase
;
986 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
988 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
989 RtlFreeUnicodeString( &upcase
);
995 /**************************************************************************
996 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
999 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
1001 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
1002 const UNICODE_STRING
*uni
,
1006 UNICODE_STRING upcase
;
1008 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
)))
1010 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1014 oem
->MaximumLength
= len
;
1015 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
1017 ret
= STATUS_NO_MEMORY
;
1021 else if (oem
->MaximumLength
< len
)
1023 ret
= STATUS_BUFFER_OVERFLOW
;
1024 oem
->Length
= oem
->MaximumLength
;
1025 if (!oem
->MaximumLength
) goto done
;
1027 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1029 RtlFreeUnicodeString( &upcase
);
1035 /**************************************************************************
1036 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1038 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1039 LPCWSTR src
, DWORD srclen
)
1045 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1046 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1047 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1048 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1053 /**************************************************************************
1054 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1056 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1057 LPCWSTR src
, DWORD srclen
)
1063 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1064 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1065 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1066 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1076 /**************************************************************************
1077 * RtlOemStringToUnicodeSize (NTDLL.@)
1078 * RtlxOemStringToUnicodeSize (NTDLL.@)
1080 * Calculate the size in bytes necessary for the Unicode conversion of str,
1081 * including the terminating '\0'.
1084 * str [I] String to calculate the size of
1087 * The calculated size.
1089 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1091 int ret
= wine_cp_mbstowcs( oem_table
, 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1092 return (ret
+ 1) * sizeof(WCHAR
);
1096 /**************************************************************************
1097 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1098 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1100 * Calculate the size in bytes necessary for the Unicode conversion of str,
1101 * including the terminating '\0'.
1104 * str [I] String to calculate the size of
1107 * The calculated size.
1109 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1112 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1113 return ret
+ sizeof(WCHAR
);
1117 /**************************************************************************
1118 * RtlMultiByteToUnicodeSize (NTDLL.@)
1120 * Compute the size in bytes necessary for the Unicode conversion of str,
1121 * without the terminating '\0'.
1124 * size [O] Destination for size
1125 * str [I] String to calculate the size of
1126 * len [I] Length of str
1131 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1133 *size
= wine_cp_mbstowcs( ansi_table
, 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1134 return STATUS_SUCCESS
;
1138 /**************************************************************************
1139 * RtlUnicodeToMultiByteSize (NTDLL.@)
1141 * Calculate the size in bytes necessary for the multibyte conversion of str,
1142 * without the terminating '\0'.
1145 * size [O] Destination for size
1146 * str [I] String to calculate the size of
1147 * len [I] Length of str
1152 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1154 *size
= wine_cp_wcstombs( ansi_table
, 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1155 return STATUS_SUCCESS
;
1159 /**************************************************************************
1160 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1161 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1163 * Calculate the size in bytes necessary for the Ansi conversion of str,
1164 * including the terminating '\0'.
1167 * str [I] String to calculate the size of
1170 * The calculated size.
1172 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1175 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1180 /**************************************************************************
1181 * RtlUnicodeStringToOemSize (NTDLL.@)
1182 * RtlxUnicodeStringToOemSize (NTDLL.@)
1184 * Calculate the size in bytes necessary for the OEM conversion of str,
1185 * including the terminating '\0'.
1188 * str [I] String to calculate the size of
1191 * The calculated size.
1193 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1195 return wine_cp_wcstombs( oem_table
, 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1196 NULL
, 0, NULL
, NULL
) + 1;
1200 /**************************************************************************
1201 * RtlAppendAsciizToString (NTDLL.@)
1203 * Concatenates a buffered character string and a '\0' terminated character
1207 * Success: STATUS_SUCCESS. src is appended to dest.
1208 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1209 * to hold the concatenated string.
1212 * if src is NULL dest is unchanged.
1213 * dest is never '\0' terminated.
1215 NTSTATUS WINAPI
RtlAppendAsciizToString(
1216 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1217 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1220 unsigned int src_len
= strlen(src
);
1221 unsigned int dest_len
= src_len
+ dest
->Length
;
1223 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1224 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1225 dest
->Length
= dest_len
;
1227 return STATUS_SUCCESS
;
1231 /**************************************************************************
1232 * RtlAppendStringToString (NTDLL.@)
1234 * Concatenates two buffered character strings
1237 * Success: STATUS_SUCCESS. src is appended to dest.
1238 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1239 * to hold the concatenated string.
1242 * if src->length is zero dest is unchanged.
1243 * dest is never '\0' terminated.
1245 NTSTATUS WINAPI
RtlAppendStringToString(
1246 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1247 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1249 if (src
->Length
!= 0) {
1250 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1252 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1253 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1254 dest
->Length
= dest_len
;
1256 return STATUS_SUCCESS
;
1260 /**************************************************************************
1261 * RtlAppendUnicodeToString (NTDLL.@)
1263 * Concatenates an buffered unicode string and a '\0' terminated unicode
1267 * Success: STATUS_SUCCESS. src is appended to dest.
1268 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1269 * to hold the concatenated string.
1272 * if src is NULL dest is unchanged.
1273 * dest is '\0' terminated when the MaximumLength allowes it.
1274 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1277 * Does not write in the src->Buffer beyond MaximumLength when
1278 * MaximumLength is odd as the native function does.
1280 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1281 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1282 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1285 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1286 unsigned int dest_len
= src_len
+ dest
->Length
;
1288 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1289 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1290 dest
->Length
= dest_len
;
1291 /* append terminating '\0' if enough space */
1292 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1293 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1296 return STATUS_SUCCESS
;
1300 /**************************************************************************
1301 * RtlAppendUnicodeStringToString (NTDLL.@)
1303 * Concatenates two buffered unicode strings
1306 * Success: STATUS_SUCCESS. src is appended to dest.
1307 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1308 * to hold the concatenated string.
1311 * if src->length is zero dest is unchanged.
1312 * dest is '\0' terminated when the MaximumLength allowes it.
1313 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1316 * Does not write in the src->Buffer beyond MaximumLength when
1317 * MaximumLength is odd as the native function does.
1319 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1320 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1321 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1323 if (src
->Length
!= 0) {
1324 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1326 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1327 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1328 dest
->Length
= dest_len
;
1329 /* append terminating '\0' if enough space */
1330 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1331 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1334 return STATUS_SUCCESS
;
1338 /**************************************************************************
1339 * RtlFindCharInUnicodeString (NTDLL.@)
1341 * Searches for one of several unicode characters in an unicode string.
1344 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1345 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1347 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1348 int flags
, /* [I] Flags */
1349 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1350 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1351 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1354 unsigned int search_idx
;
1358 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1359 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1360 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1361 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1362 return STATUS_SUCCESS
;
1367 return STATUS_NOT_FOUND
;
1369 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1370 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1371 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1372 *pos
= main_idx
* sizeof(WCHAR
);
1373 return STATUS_SUCCESS
;
1378 return STATUS_NOT_FOUND
;
1380 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1382 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1383 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1386 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1387 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1388 return STATUS_SUCCESS
;
1392 return STATUS_NOT_FOUND
;
1394 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1396 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1397 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1400 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1401 *pos
= main_idx
* sizeof(WCHAR
);
1402 return STATUS_SUCCESS
;
1406 return STATUS_NOT_FOUND
;
1408 return STATUS_NOT_FOUND
;
1415 /* Tests that we currently implement */
1416 #define ITU_IMPLEMENTED_TESTS \
1417 (IS_TEXT_UNICODE_SIGNATURE | \
1418 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1419 IS_TEXT_UNICODE_ODD_LENGTH | \
1420 IS_TEXT_UNICODE_STATISTICS | \
1421 IS_TEXT_UNICODE_NULL_BYTES)
1423 /**************************************************************************
1424 * RtlIsTextUnicode (NTDLL.@)
1426 * Attempt to guess whether a text buffer is Unicode.
1429 * buf [I] Text buffer to test
1430 * len [I] Length of buf
1431 * pf [O] Destination for test results
1434 * The length of the string if all tests were passed, 0 otherwise.
1437 * Should implement more tests.
1439 DWORD WINAPI
RtlIsTextUnicode(
1445 DWORD flags
= -1, out_flags
= 0;
1452 * Apply various tests to the text string. According to the
1453 * docs, each test "passed" sets the corresponding flag in
1454 * the output flags. But some of the tests are mutually
1455 * exclusive, so I don't see how you could pass all tests ...
1458 /* Check for an odd length ... pass if even. */
1459 if ((flags
& IS_TEXT_UNICODE_ODD_LENGTH
) && (len
& 1))
1460 out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1462 /* Check for the special byte order unicode marks. */
1463 if ((flags
& IS_TEXT_UNICODE_SIGNATURE
) && *s
== 0xFEFF)
1464 out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1466 if ((flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
) && *s
== 0xFFFE)
1467 out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1469 /* apply some statistical analysis */
1470 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1473 /* FIXME: checks only for ASCII characters in the unicode stream */
1474 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1476 if (s
[i
] <= 255) stats
++;
1478 if (stats
> len
/ sizeof(WCHAR
) / 2)
1479 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1482 /* Check for unicode NULL chars */
1483 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1486 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1490 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1497 * Check whether the string passed all of the tests.
1499 flags
&= ITU_IMPLEMENTED_TESTS
;
1500 if ((out_flags
& flags
) != flags
)
1509 /**************************************************************************
1510 * RtlCharToInteger (NTDLL.@)
1512 * Converts a character string into its integer equivalent.
1515 * Success: STATUS_SUCCESS. value contains the converted number
1516 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1517 * STATUS_ACCESS_VIOLATION, if value is NULL.
1520 * For base 0 it uses 10 as base and the string should be in the format
1521 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1522 * For other bases the string should be in the format
1523 * "{whitespace} [+|-] {digits}".
1524 * No check is made for value overflow, only the lower 32 bits are assigned.
1525 * If str is NULL it crashes, as the native function does.
1528 * This function does not read garbage behind '\0' as the native version does.
1530 NTSTATUS WINAPI
RtlCharToInteger(
1531 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1532 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1533 ULONG
*value
) /* [O] Destination for the converted value */
1537 ULONG RunningTotal
= 0;
1540 while (*str
!= '\0' && *str
<= ' ') {
1546 } else if (*str
== '-') {
1553 if (str
[0] == '0') {
1554 if (str
[1] == 'b') {
1557 } else if (str
[1] == 'o') {
1560 } else if (str
[1] == 'x') {
1565 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1566 return STATUS_INVALID_PARAMETER
;
1569 if (value
== NULL
) {
1570 return STATUS_ACCESS_VIOLATION
;
1573 while (*str
!= '\0') {
1575 if (chCurrent
>= '0' && chCurrent
<= '9') {
1576 digit
= chCurrent
- '0';
1577 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1578 digit
= chCurrent
- 'A' + 10;
1579 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1580 digit
= chCurrent
- 'a' + 10;
1584 if (digit
< 0 || digit
>= base
) {
1585 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1586 return STATUS_SUCCESS
;
1589 RunningTotal
= RunningTotal
* base
+ digit
;
1593 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1594 return STATUS_SUCCESS
;
1598 /**************************************************************************
1599 * RtlIntegerToChar (NTDLL.@)
1601 * Converts an unsigned integer to a character string.
1604 * Success: STATUS_SUCCESS. str contains the converted number
1605 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1606 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1607 * STATUS_ACCESS_VIOLATION, if str is NULL.
1610 * Instead of base 0 it uses 10 as base.
1611 * Writes at most length characters to the string str.
1612 * Str is '\0' terminated when length allowes it.
1613 * When str fits exactly in length characters the '\0' is ommitted.
1615 NTSTATUS WINAPI
RtlIntegerToChar(
1616 ULONG value
, /* [I] Value to be converted */
1617 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1618 ULONG length
, /* [I] Length of the str buffer in bytes */
1619 PCHAR str
) /* [O] Destination for the converted value */
1628 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1629 return STATUS_INVALID_PARAMETER
;
1637 digit
= value
% base
;
1638 value
= value
/ base
;
1642 *pos
= 'A' + digit
- 10;
1644 } while (value
!= 0L);
1646 len
= &buffer
[32] - pos
;
1648 return STATUS_BUFFER_OVERFLOW
;
1649 } else if (str
== NULL
) {
1650 return STATUS_ACCESS_VIOLATION
;
1651 } else if (len
== length
) {
1652 memcpy(str
, pos
, len
);
1654 memcpy(str
, pos
, len
+ 1);
1656 return STATUS_SUCCESS
;
1660 /**************************************************************************
1661 * RtlUnicodeStringToInteger (NTDLL.@)
1663 * Converts an unicode string into its integer equivalent.
1666 * Success: STATUS_SUCCESS. value contains the converted number
1667 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1668 * STATUS_ACCESS_VIOLATION, if value is NULL.
1671 * For base 0 it uses 10 as base and the string should be in the format
1672 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1673 * For other bases the string should be in the format
1674 * "{whitespace} [+|-] {digits}".
1675 * No check is made for value overflow, only the lower 32 bits are assigned.
1676 * If str is NULL it crashes, as the native function does.
1679 * This function does not read garbage on string length 0 as the native
1682 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1683 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1684 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1685 ULONG
*value
) /* [O] Destination for the converted value */
1687 LPWSTR lpwstr
= str
->Buffer
;
1688 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1691 ULONG RunningTotal
= 0;
1694 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1699 if (CharsRemaining
>= 1) {
1700 if (*lpwstr
== '+') {
1703 } else if (*lpwstr
== '-') {
1712 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1713 if (lpwstr
[1] == 'b') {
1715 CharsRemaining
-= 2;
1717 } else if (lpwstr
[1] == 'o') {
1719 CharsRemaining
-= 2;
1721 } else if (lpwstr
[1] == 'x') {
1723 CharsRemaining
-= 2;
1727 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1728 return STATUS_INVALID_PARAMETER
;
1731 if (value
== NULL
) {
1732 return STATUS_ACCESS_VIOLATION
;
1735 while (CharsRemaining
>= 1) {
1736 wchCurrent
= *lpwstr
;
1737 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1738 digit
= wchCurrent
- '0';
1739 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1740 digit
= wchCurrent
- 'A' + 10;
1741 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1742 digit
= wchCurrent
- 'a' + 10;
1746 if (digit
< 0 || digit
>= base
) {
1747 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1748 return STATUS_SUCCESS
;
1751 RunningTotal
= RunningTotal
* base
+ digit
;
1756 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1757 return STATUS_SUCCESS
;
1761 /**************************************************************************
1762 * RtlIntegerToUnicodeString (NTDLL.@)
1764 * Converts an unsigned integer to a '\0' terminated unicode string.
1767 * Success: STATUS_SUCCESS. str contains the converted number
1768 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1769 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1770 * (with the '\0' termination). In this case str->Length
1771 * is set to the length, the string would have (which can
1772 * be larger than the MaximumLength).
1775 * Instead of base 0 it uses 10 as base.
1776 * If str is NULL it crashes, as the native function does.
1779 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1780 * The native function does this when the string would be longer than 16
1781 * characters even when the string parameter is long enough.
1783 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1784 ULONG value
, /* [I] Value to be converted */
1785 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1786 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1794 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1795 return STATUS_INVALID_PARAMETER
;
1803 digit
= value
% base
;
1804 value
= value
/ base
;
1808 *pos
= 'A' + digit
- 10;
1810 } while (value
!= 0L);
1812 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1813 if (str
->Length
>= str
->MaximumLength
) {
1814 return STATUS_BUFFER_OVERFLOW
;
1816 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1818 return STATUS_SUCCESS
;
1822 /*************************************************************************
1823 * RtlGUIDFromString (NTDLL.@)
1825 * Convert a string representation of a GUID into a GUID.
1828 * str [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
1829 * guid [O] Destination for the converted GUID
1832 * Success: STATUS_SUCCESS. guid contains the converted value.
1833 * Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
1836 * See RtlStringFromGUID.
1838 NTSTATUS WINAPI
RtlGUIDFromString(const UNICODE_STRING
*str
, GUID
* guid
)
1841 const WCHAR
*lpszCLSID
= str
->Buffer
;
1842 BYTE
* lpOut
= (BYTE
*)guid
;
1844 TRACE("(%s,%p)\n", debugstr_us(str
), guid
);
1846 /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
1847 * to memory: DWORD... WORD WORD BYTES............
1854 if (*lpszCLSID
!= '{')
1855 return STATUS_INVALID_PARAMETER
;
1858 case 9: case 14: case 19: case 24:
1859 if (*lpszCLSID
!= '-')
1860 return STATUS_INVALID_PARAMETER
;
1864 if (*lpszCLSID
!= '}')
1865 return STATUS_INVALID_PARAMETER
;
1870 WCHAR ch
= *lpszCLSID
, ch2
= lpszCLSID
[1];
1873 /* Read two hex digits as a byte value */
1874 if (ch
>= '0' && ch
<= '9') ch
= ch
- '0';
1875 else if (ch
>= 'a' && ch
<= 'f') ch
= ch
- 'a' + 10;
1876 else if (ch
>= 'A' && ch
<= 'F') ch
= ch
- 'A' + 10;
1877 else return STATUS_INVALID_PARAMETER
;
1879 if (ch2
>= '0' && ch2
<= '9') ch2
= ch2
- '0';
1880 else if (ch2
>= 'a' && ch2
<= 'f') ch2
= ch2
- 'a' + 10;
1881 else if (ch2
>= 'A' && ch2
<= 'F') ch2
= ch2
- 'A' + 10;
1882 else return STATUS_INVALID_PARAMETER
;
1884 byte
= ch
<< 4 | ch2
;
1888 #ifndef WORDS_BIGENDIAN
1889 /* For Big Endian machines, we store the data such that the
1890 * dword/word members can be read as DWORDS and WORDS correctly. */
1892 case 1: lpOut
[3] = byte
; break;
1893 case 3: lpOut
[2] = byte
; break;
1894 case 5: lpOut
[1] = byte
; break;
1895 case 7: lpOut
[0] = byte
; lpOut
+= 4; break;
1897 case 10: case 15: lpOut
[1] = byte
; break;
1898 case 12: case 17: lpOut
[0] = byte
; lpOut
+= 2; break;
1901 default: lpOut
[0] = byte
; lpOut
++; break;
1903 lpszCLSID
++; /* Skip 2nd character of byte */
1911 return STATUS_SUCCESS
;
1914 /*************************************************************************
1915 * RtlStringFromGUID (NTDLL.@)
1917 * Convert a GUID into a string representation of a GUID.
1920 * guid [I] GUID to convert
1921 * str [O] Destination for the converted string
1924 * Success: STATUS_SUCCESS. str contains the converted value.
1925 * Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
1928 * See RtlGUIDFromString.
1930 NTSTATUS WINAPI
RtlStringFromGUID(const GUID
* guid
, UNICODE_STRING
*str
)
1932 static const WCHAR szFormat
[] = { '{','%','0','8','l','X','-',
1933 '%','0','4','X','-', '%','0','4','X','-','%','0','2','X','%','0','2','X',
1934 '-', '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
1935 '%','0','2','X','%','0','2','X','}','\0' };
1937 TRACE("(%p,%p)\n", guid
, str
);
1939 str
->Buffer
= (WCHAR
*)RtlAllocateHeap( ntdll_get_process_heap(), 0, 40 * sizeof(WCHAR
));
1942 str
->Length
= str
->MaximumLength
= 0;
1943 return STATUS_NO_MEMORY
;
1945 str
->Length
= str
->MaximumLength
= 40;
1946 sprintfW(str
->Buffer
, szFormat
, guid
->Data1
, guid
->Data2
, guid
->Data3
,
1947 guid
->Data4
[0], guid
->Data4
[1], guid
->Data4
[2], guid
->Data4
[3],
1948 guid
->Data4
[4], guid
->Data4
[5], guid
->Data4
[6], guid
->Data4
[7]);
1950 return STATUS_SUCCESS
;