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
= 1252;
42 BYTE NlsMbCodePageTag
= 0;
43 BYTE NlsMbOemCodePageTag
= 0;
45 static const union cptable
*ansi_table
;
46 static const union cptable
*oem_table
;
48 inline static const union cptable
*get_ansi_table(void)
50 if (!ansi_table
) ansi_table
= wine_cp_get_table( 1252 );
54 inline static const union cptable
*get_oem_table(void)
56 if (!oem_table
) oem_table
= wine_cp_get_table( 437 );
61 /**************************************************************************
62 * __wine_init_codepages (NTDLL.@)
64 * Set the code page once kernel32 is loaded. Should be done differently.
66 void __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
)
70 NlsAnsiCodePage
= ansi
->info
.codepage
;
74 /**************************************************************************
75 * RtlInitAnsiString (NTDLL.@)
77 * Initializes a buffered ansi string.
83 * Assigns source to target->Buffer. The length of source is assigned to
84 * target->Length and target->MaximumLength. If source is NULL the length
85 * of source is assumed to be 0.
87 void WINAPI
RtlInitAnsiString(
88 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
89 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
91 if ((target
->Buffer
= (PCHAR
) source
))
93 target
->Length
= strlen(source
);
94 target
->MaximumLength
= target
->Length
+ 1;
96 else target
->Length
= target
->MaximumLength
= 0;
100 /**************************************************************************
101 * RtlInitString (NTDLL.@)
103 * Initializes a buffered string.
109 * Assigns source to target->Buffer. The length of source is assigned to
110 * target->Length and target->MaximumLength. If source is NULL the length
111 * of source is assumed to be 0.
113 void WINAPI
RtlInitString(
114 PSTRING target
, /* [I/O] Buffered string to be initialized */
115 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
117 RtlInitAnsiString( target
, source
);
121 /**************************************************************************
122 * RtlFreeAnsiString (NTDLL.@)
124 void WINAPI
RtlFreeAnsiString( PSTRING str
)
126 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
130 /**************************************************************************
131 * RtlFreeOemString (NTDLL.@)
133 void WINAPI
RtlFreeOemString( PSTRING str
)
135 RtlFreeAnsiString( str
);
139 /**************************************************************************
140 * RtlCopyString (NTDLL.@)
142 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
146 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
147 memcpy( dst
->Buffer
, src
->Buffer
, len
);
150 else dst
->Length
= 0;
154 /**************************************************************************
155 * RtlInitUnicodeString (NTDLL.@)
157 * Initializes a buffered unicode string.
163 * Assigns source to target->Buffer. The length of source is assigned to
164 * target->Length and target->MaximumLength. If source is NULL the length
165 * of source is assumed to be 0.
167 void WINAPI
RtlInitUnicodeString(
168 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
169 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
171 if ((target
->Buffer
= (PWSTR
) source
))
173 target
->Length
= strlenW(source
) * sizeof(WCHAR
);
174 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
176 else target
->Length
= target
->MaximumLength
= 0;
180 /**************************************************************************
181 * RtlInitUnicodeStringEx (NTDLL.@)
183 * Initializes a buffered unicode string.
186 * Success: STATUS_SUCCESS. target is initialized.
187 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
190 * Assigns source to target->Buffer. The length of source is assigned to
191 * target->Length and target->MaximumLength. If source is NULL the length
192 * of source is assumed to be 0.
194 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
195 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
196 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
198 if (source
!= NULL
) {
199 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
202 return STATUS_NAME_TOO_LONG
;
204 target
->Length
= len
;
205 target
->MaximumLength
= len
+ sizeof(WCHAR
);
206 target
->Buffer
= (PWSTR
) source
;
210 target
->MaximumLength
= 0;
211 target
->Buffer
= NULL
;
213 return STATUS_SUCCESS
;
217 /**************************************************************************
218 * RtlCreateUnicodeString (NTDLL.@)
220 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
222 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
223 if (!(target
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) return FALSE
;
224 memcpy( target
->Buffer
, src
, len
);
225 target
->MaximumLength
= len
;
226 target
->Length
= len
- sizeof(WCHAR
);
231 /**************************************************************************
232 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
234 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
237 RtlInitAnsiString( &ansi
, src
);
238 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
242 /**************************************************************************
243 * RtlFreeUnicodeString (NTDLL.@)
245 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
247 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
251 /**************************************************************************
252 * RtlCopyUnicodeString (NTDLL.@)
254 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
258 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
259 memcpy( dst
->Buffer
, src
->Buffer
, len
);
261 /* append terminating '\0' if enough space */
262 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
264 else dst
->Length
= 0;
268 /**************************************************************************
269 * RtlDuplicateUnicodeString (NTDLL.@)
271 * Duplicates an unicode string.
274 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
275 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
276 * STATUS_NO_MEMORY, if the allocation fails.
279 * For add_nul there are several possible values:
280 * 0 = destination will not be '\0' terminated,
281 * 1 = destination will be '\0' terminated,
282 * 3 = like 1 but for an empty source string produce '\0' terminated empty
283 * Buffer instead of assigning NULL to the Buffer.
284 * Other add_nul values are invalid.
286 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
287 int add_nul
, /* [I] flag */
288 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
289 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
291 if (source
== NULL
|| destination
== NULL
||
292 source
->Length
> source
->MaximumLength
||
293 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
294 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
295 return STATUS_INVALID_PARAMETER
;
297 if (source
->Length
== 0 && add_nul
!= 3) {
298 destination
->Length
= 0;
299 destination
->MaximumLength
= 0;
300 destination
->Buffer
= NULL
;
302 unsigned int destination_max_len
= source
->Length
;
305 destination_max_len
+= sizeof(WCHAR
);
307 destination
->Buffer
= RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len
);
308 if (destination
->Buffer
== NULL
) {
309 return STATUS_NO_MEMORY
;
311 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
312 destination
->Length
= source
->Length
;
313 destination
->MaximumLength
= source
->Length
;
314 /* append terminating '\0' if enough space */
316 destination
->MaximumLength
= destination_max_len
;
317 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
322 return STATUS_SUCCESS
;
326 /**************************************************************************
327 * RtlEraseUnicodeString (NTDLL.@)
329 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
333 memset( str
->Buffer
, 0, str
->MaximumLength
);
344 /******************************************************************************
345 * RtlCompareString (NTDLL.@)
347 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
353 len
= min(s1
->Length
, s2
->Length
);
359 while (!ret
&& len
--) ret
= toupper(*p1
++) - toupper(*p2
++);
363 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
365 if (!ret
) ret
= s1
->Length
- s2
->Length
;
370 /******************************************************************************
371 * RtlCompareUnicodeString (NTDLL.@)
373 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
374 BOOLEAN CaseInsensitive
)
380 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
386 while (!ret
&& len
--) ret
= toupperW(*p1
++) - toupperW(*p2
++);
390 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
392 if (!ret
) ret
= s1
->Length
- s2
->Length
;
397 /**************************************************************************
398 * RtlEqualString (NTDLL.@)
400 * Determine if two strings are equal.
403 * s1 [I] Source string
404 * s2 [I] String to compare to s1
405 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
408 * Non-zero if s1 is equal to s2, 0 otherwise.
410 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
412 if (s1
->Length
!= s2
->Length
) return FALSE
;
413 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
417 /**************************************************************************
418 * RtlEqualUnicodeString (NTDLL.@)
420 * Unicode version of RtlEqualString.
422 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
423 BOOLEAN CaseInsensitive
)
425 if (s1
->Length
!= s2
->Length
) return FALSE
;
426 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
430 /**************************************************************************
431 * RtlPrefixString (NTDLL.@)
433 * Determine if one string is a prefix of another.
436 * s1 [I] Prefix to look for in s2
437 * s2 [I] String that may contain s1 as a prefix
438 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
441 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
443 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
447 if (s1
->Length
> s2
->Length
) return FALSE
;
450 for (i
= 0; i
< s1
->Length
; i
++)
451 if (toupper(s1
->Buffer
[i
]) != toupper(s2
->Buffer
[i
])) return FALSE
;
455 for (i
= 0; i
< s1
->Length
; i
++)
456 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
462 /**************************************************************************
463 * RtlPrefixUnicodeString (NTDLL.@)
465 * Unicode version of RtlPrefixString.
467 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
468 const UNICODE_STRING
*s2
,
469 BOOLEAN ignore_case
)
473 if (s1
->Length
> s2
->Length
) return FALSE
;
476 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
477 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
481 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
482 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
488 /**************************************************************************
489 * RtlEqualComputerName (NTDLL.@)
491 * Determine if two computer names are the same.
494 * left [I] First computer name
495 * right [I] Second computer name
498 * 0 if the names are equal, non-zero otherwise.
501 * The comparason is case insensitive.
503 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
504 const UNICODE_STRING
*right
)
507 STRING upLeft
, upRight
;
509 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
511 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
513 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
514 RtlFreeOemString( &upRight
);
516 RtlFreeOemString( &upLeft
);
522 /**************************************************************************
523 * RtlEqualDomainName (NTDLL.@)
525 * Determine if two domain names are the same.
528 * left [I] First domain name
529 * right [I] Second domain name
532 * 0 if the names are equal, non-zero otherwise.
535 * The comparason is case insensitive.
537 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
538 const UNICODE_STRING
*right
)
540 return RtlEqualComputerName(left
, right
);
545 COPY BETWEEN ANSI_STRING or UNICODE_STRING
546 there is no parameter checking, it just crashes
550 /**************************************************************************
551 * RtlAnsiStringToUnicodeString (NTDLL.@)
553 * Converts an ansi string to an unicode string.
556 * Success: STATUS_SUCCESS. uni contains the converted string
557 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
558 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
559 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
562 * This function always writes a terminating '\0'.
564 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
565 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
566 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
567 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
569 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
571 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
572 uni
->Length
= total
- sizeof(WCHAR
);
575 uni
->MaximumLength
= total
;
576 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
577 return STATUS_NO_MEMORY
;
579 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
581 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
582 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
583 return STATUS_SUCCESS
;
587 /**************************************************************************
588 * RtlOemStringToUnicodeString (NTDLL.@)
590 * Converts an oem string to an unicode string.
593 * Success: STATUS_SUCCESS. uni contains the converted string
594 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
595 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
596 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
599 * This function always writes a terminating '\0'.
601 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
602 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
603 const STRING
*oem
, /* [I] Oem string to be converted */
604 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
606 DWORD total
= RtlOemStringToUnicodeSize( oem
);
608 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
609 uni
->Length
= total
- sizeof(WCHAR
);
612 uni
->MaximumLength
= total
;
613 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
614 return STATUS_NO_MEMORY
;
616 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
618 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
619 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
620 return STATUS_SUCCESS
;
624 /**************************************************************************
625 * RtlUnicodeStringToAnsiString (NTDLL.@)
627 * Converts an unicode string to an ansi string.
630 * Success: STATUS_SUCCESS. ansi contains the converted string
631 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
632 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
635 * This function always writes a terminating '\0'.
636 * It performs a partial copy if ansi is too small.
638 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
639 STRING
*ansi
, /* [I/O] Destination for the ansi string */
640 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
641 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
643 NTSTATUS ret
= STATUS_SUCCESS
;
644 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
646 ansi
->Length
= len
- 1;
649 ansi
->MaximumLength
= len
;
650 if (!(ansi
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
651 return STATUS_NO_MEMORY
;
653 else if (ansi
->MaximumLength
< len
)
655 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
656 ansi
->Length
= ansi
->MaximumLength
- 1;
657 ret
= STATUS_BUFFER_OVERFLOW
;
660 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
661 ansi
->Buffer
[ansi
->Length
] = 0;
666 /**************************************************************************
667 * RtlUnicodeStringToOemString (NTDLL.@)
669 * Converts a Rtl Unicode string to an OEM string.
672 * oem [O] Destination for OEM string
673 * uni [I] Source Unicode string
674 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
677 * Success: STATUS_SUCCESS. oem contains the converted string
678 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
679 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
682 * If doalloc is TRUE, the length allocated is uni->Length + 1.
683 * This function always '\0' terminates the string returned.
685 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
686 const UNICODE_STRING
*uni
,
689 NTSTATUS ret
= STATUS_SUCCESS
;
690 DWORD len
= RtlUnicodeStringToOemSize( uni
);
692 oem
->Length
= len
- 1;
695 oem
->MaximumLength
= len
;
696 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
697 return STATUS_NO_MEMORY
;
699 else if (oem
->MaximumLength
< len
)
701 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
702 oem
->Length
= oem
->MaximumLength
- 1;
703 ret
= STATUS_BUFFER_OVERFLOW
;
706 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
707 oem
->Buffer
[oem
->Length
] = 0;
712 /**************************************************************************
713 * RtlMultiByteToUnicodeN (NTDLL.@)
716 * Performs a partial copy if dst is too small.
718 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
719 LPCSTR src
, DWORD srclen
)
722 int ret
= wine_cp_mbstowcs( get_ansi_table(), 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
724 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
725 return STATUS_SUCCESS
;
729 /**************************************************************************
730 * RtlOemToUnicodeN (NTDLL.@)
732 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
733 LPCSTR src
, DWORD srclen
)
735 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
737 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
738 return STATUS_SUCCESS
;
742 /**************************************************************************
743 * RtlUnicodeToMultiByteN (NTDLL.@)
745 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
746 LPCWSTR src
, DWORD srclen
)
748 int ret
= wine_cp_wcstombs( get_ansi_table(), 0, src
, srclen
/ sizeof(WCHAR
),
749 dst
, dstlen
, NULL
, NULL
);
751 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
752 return STATUS_SUCCESS
;
756 /**************************************************************************
757 * RtlUnicodeToOemN (NTDLL.@)
759 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
760 LPCWSTR src
, DWORD srclen
)
762 int ret
= wine_cp_wcstombs( get_oem_table(), 0, src
, srclen
/ sizeof(WCHAR
),
763 dst
, dstlen
, NULL
, NULL
);
765 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
766 return STATUS_SUCCESS
;
775 /**************************************************************************
776 * RtlUpperChar (NTDLL.@)
778 * Converts an Ascii character to uppercase.
781 * ch [I] Character to convert
784 * The uppercase character value.
787 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
788 * All other input characters are returned unchanged. The locale and
789 * multibyte characters are not taken into account (as native DLL).
791 CHAR WINAPI
RtlUpperChar( CHAR ch
)
793 if (ch
>= 'a' && ch
<= 'z') {
794 return ch
- 'a' + 'A';
801 /**************************************************************************
802 * RtlUpperString (NTDLL.@)
804 * Converts an Ascii string to uppercase.
807 * dst [O] Destination for converted string
808 * src [I] Source string to convert
814 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
815 * All other src characters are copied unchanged to dst. The locale and
816 * multibyte characters are not taken into account (as native DLL).
817 * The number of character copied is the minimum of src->Length and
818 * the dst->MaximumLength.
820 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
822 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
824 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
829 /**************************************************************************
830 * RtlUpcaseUnicodeChar (NTDLL.@)
832 * Converts an Unicode character to uppercase.
835 * wch [I] Character to convert
838 * The uppercase character value.
840 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
842 return toupperW(wch
);
846 /**************************************************************************
847 * RtlDowncaseUnicodeChar (NTDLL.@)
849 * Converts an Unicode character to lowercase.
852 * wch [I] Character to convert
855 * The lowercase character value.
857 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
859 return tolowerW(wch
);
863 /**************************************************************************
864 * RtlUpcaseUnicodeString (NTDLL.@)
866 * Converts an Unicode string to uppercase.
869 * dest [O] Destination for converted string
870 * src [I] Source string to convert
871 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
874 * Success: STATUS_SUCCESS. dest contains the converted string.
875 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
876 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
879 * dest is never '\0' terminated because it may be equal to src, and src
880 * might not be '\0' terminated. dest->Length is only set upon success.
882 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
883 const UNICODE_STRING
*src
,
886 DWORD i
, len
= src
->Length
;
890 dest
->MaximumLength
= len
;
891 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
892 return STATUS_NO_MEMORY
;
894 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
896 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
898 return STATUS_SUCCESS
;
902 /**************************************************************************
903 * RtlDowncaseUnicodeString (NTDLL.@)
905 * Converts an Unicode string to lowercase.
908 * dest [O] Destination for converted string
909 * src [I] Source string to convert
910 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
913 * Success: STATUS_SUCCESS. dest contains the converted string.
914 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
915 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
918 * dest is never '\0' terminated because it may be equal to src, and src
919 * might not be '\0' terminated. dest->Length is only set upon success.
921 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
922 UNICODE_STRING
*dest
,
923 const UNICODE_STRING
*src
,
927 DWORD len
= src
->Length
;
930 dest
->MaximumLength
= len
;
931 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) {
932 return STATUS_NO_MEMORY
;
934 } else if (len
> dest
->MaximumLength
) {
935 return STATUS_BUFFER_OVERFLOW
;
938 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
939 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
942 return STATUS_SUCCESS
;
946 /**************************************************************************
947 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
950 * writes terminating 0
952 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
953 const UNICODE_STRING
*src
,
957 UNICODE_STRING upcase
;
959 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
961 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
962 RtlFreeUnicodeString( &upcase
);
968 /**************************************************************************
969 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
972 * writes terminating 0
974 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
975 const UNICODE_STRING
*src
,
979 UNICODE_STRING upcase
;
981 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
983 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
984 RtlFreeUnicodeString( &upcase
);
990 /**************************************************************************
991 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
994 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
996 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
997 const UNICODE_STRING
*uni
,
1001 UNICODE_STRING upcase
;
1003 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
)))
1005 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1009 oem
->MaximumLength
= len
;
1010 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
1012 ret
= STATUS_NO_MEMORY
;
1016 else if (oem
->MaximumLength
< len
)
1018 ret
= STATUS_BUFFER_OVERFLOW
;
1019 oem
->Length
= oem
->MaximumLength
;
1020 if (!oem
->MaximumLength
) goto done
;
1022 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1024 RtlFreeUnicodeString( &upcase
);
1030 /**************************************************************************
1031 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1033 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1034 LPCWSTR src
, DWORD srclen
)
1040 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1041 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1042 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1043 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1048 /**************************************************************************
1049 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1051 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1052 LPCWSTR src
, DWORD srclen
)
1058 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1059 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1060 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1061 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1071 /**************************************************************************
1072 * RtlOemStringToUnicodeSize (NTDLL.@)
1073 * RtlxOemStringToUnicodeSize (NTDLL.@)
1075 * Calculate the size in bytes necessary for the Unicode conversion of str,
1076 * including the terminating '\0'.
1079 * str [I] String to calculate the size of
1082 * The calculated size.
1084 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1086 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1087 return (ret
+ 1) * sizeof(WCHAR
);
1091 /**************************************************************************
1092 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1093 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1095 * Calculate the size in bytes necessary for the Unicode conversion of str,
1096 * including the terminating '\0'.
1099 * str [I] String to calculate the size of
1102 * The calculated size.
1104 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1107 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1108 return ret
+ sizeof(WCHAR
);
1112 /**************************************************************************
1113 * RtlMultiByteToUnicodeSize (NTDLL.@)
1115 * Compute the size in bytes necessary for the Unicode conversion of str,
1116 * without the terminating '\0'.
1119 * size [O] Destination for size
1120 * str [I] String to calculate the size of
1121 * len [I] Length of str
1126 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1128 *size
= wine_cp_mbstowcs( get_ansi_table(), 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1129 return STATUS_SUCCESS
;
1133 /**************************************************************************
1134 * RtlUnicodeToMultiByteSize (NTDLL.@)
1136 * Calculate the size in bytes necessary for the multibyte conversion of str,
1137 * without the terminating '\0'.
1140 * size [O] Destination for size
1141 * str [I] String to calculate the size of
1142 * len [I] Length of str
1147 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1149 *size
= wine_cp_wcstombs( get_ansi_table(), 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1150 return STATUS_SUCCESS
;
1154 /**************************************************************************
1155 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1156 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1158 * Calculate the size in bytes necessary for the Ansi conversion of str,
1159 * including the terminating '\0'.
1162 * str [I] String to calculate the size of
1165 * The calculated size.
1167 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1170 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1175 /**************************************************************************
1176 * RtlUnicodeStringToOemSize (NTDLL.@)
1177 * RtlxUnicodeStringToOemSize (NTDLL.@)
1179 * Calculate the size in bytes necessary for the OEM conversion of str,
1180 * including the terminating '\0'.
1183 * str [I] String to calculate the size of
1186 * The calculated size.
1188 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1190 return wine_cp_wcstombs( get_oem_table(), 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1191 NULL
, 0, NULL
, NULL
) + 1;
1195 /**************************************************************************
1196 * RtlAppendAsciizToString (NTDLL.@)
1198 * Concatenates a buffered character string and a '\0' terminated character
1202 * Success: STATUS_SUCCESS. src is appended to dest.
1203 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1204 * to hold the concatenated string.
1207 * if src is NULL dest is unchanged.
1208 * dest is never '\0' terminated.
1210 NTSTATUS WINAPI
RtlAppendAsciizToString(
1211 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1212 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1215 unsigned int src_len
= strlen(src
);
1216 unsigned int dest_len
= src_len
+ dest
->Length
;
1218 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1219 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1220 dest
->Length
= dest_len
;
1222 return STATUS_SUCCESS
;
1226 /**************************************************************************
1227 * RtlAppendStringToString (NTDLL.@)
1229 * Concatenates two buffered character strings
1232 * Success: STATUS_SUCCESS. src is appended to dest.
1233 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1234 * to hold the concatenated string.
1237 * if src->length is zero dest is unchanged.
1238 * dest is never '\0' terminated.
1240 NTSTATUS WINAPI
RtlAppendStringToString(
1241 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1242 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1244 if (src
->Length
!= 0) {
1245 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1247 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1248 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1249 dest
->Length
= dest_len
;
1251 return STATUS_SUCCESS
;
1255 /**************************************************************************
1256 * RtlAppendUnicodeToString (NTDLL.@)
1258 * Concatenates an buffered unicode string and a '\0' terminated unicode
1262 * Success: STATUS_SUCCESS. src is appended to dest.
1263 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1264 * to hold the concatenated string.
1267 * if src is NULL dest is unchanged.
1268 * dest is '\0' terminated when the MaximumLength allowes it.
1269 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1272 * Does not write in the src->Buffer beyond MaximumLength when
1273 * MaximumLength is odd as the native function does.
1275 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1276 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1277 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1280 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1281 unsigned int dest_len
= src_len
+ dest
->Length
;
1283 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1284 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1285 dest
->Length
= dest_len
;
1286 /* append terminating '\0' if enough space */
1287 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1288 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1291 return STATUS_SUCCESS
;
1295 /**************************************************************************
1296 * RtlAppendUnicodeStringToString (NTDLL.@)
1298 * Concatenates two buffered unicode strings
1301 * Success: STATUS_SUCCESS. src is appended to dest.
1302 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1303 * to hold the concatenated string.
1306 * if src->length is zero dest is unchanged.
1307 * dest is '\0' terminated when the MaximumLength allowes it.
1308 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1311 * Does not write in the src->Buffer beyond MaximumLength when
1312 * MaximumLength is odd as the native function does.
1314 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1315 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1316 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1318 if (src
->Length
!= 0) {
1319 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1321 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1322 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1323 dest
->Length
= dest_len
;
1324 /* append terminating '\0' if enough space */
1325 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1326 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1329 return STATUS_SUCCESS
;
1333 /**************************************************************************
1334 * RtlFindCharInUnicodeString (NTDLL.@)
1336 * Searches for one of several unicode characters in an unicode string.
1339 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1340 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1342 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1343 int flags
, /* [I] Flags */
1344 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1345 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1346 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1349 unsigned int search_idx
;
1353 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
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
+ 1) * sizeof(WCHAR
);
1357 return STATUS_SUCCESS
;
1362 return STATUS_NOT_FOUND
;
1364 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1365 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1366 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1367 *pos
= main_idx
* sizeof(WCHAR
);
1368 return STATUS_SUCCESS
;
1373 return STATUS_NOT_FOUND
;
1375 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1377 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1378 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1381 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1382 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1383 return STATUS_SUCCESS
;
1387 return STATUS_NOT_FOUND
;
1389 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1391 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1392 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1395 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1396 *pos
= main_idx
* sizeof(WCHAR
);
1397 return STATUS_SUCCESS
;
1401 return STATUS_NOT_FOUND
;
1403 return STATUS_NOT_FOUND
;
1410 /* Tests that we currently implement */
1411 #define ITU_IMPLEMENTED_TESTS \
1412 (IS_TEXT_UNICODE_SIGNATURE | \
1413 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1414 IS_TEXT_UNICODE_ODD_LENGTH | \
1415 IS_TEXT_UNICODE_STATISTICS | \
1416 IS_TEXT_UNICODE_NULL_BYTES)
1418 /**************************************************************************
1419 * RtlIsTextUnicode (NTDLL.@)
1421 * Attempt to guess whether a text buffer is Unicode.
1424 * buf [I] Text buffer to test
1425 * len [I] Length of buf
1426 * pf [O] Destination for test results
1429 * The length of the string if all tests were passed, 0 otherwise.
1432 * Should implement more tests.
1434 DWORD WINAPI
RtlIsTextUnicode(
1440 DWORD flags
= -1, out_flags
= 0;
1447 * Apply various tests to the text string. According to the
1448 * docs, each test "passed" sets the corresponding flag in
1449 * the output flags. But some of the tests are mutually
1450 * exclusive, so I don't see how you could pass all tests ...
1453 /* Check for an odd length ... pass if even. */
1454 if ((flags
& IS_TEXT_UNICODE_ODD_LENGTH
) && (len
& 1))
1455 out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1457 /* Check for the special byte order unicode marks. */
1458 if ((flags
& IS_TEXT_UNICODE_SIGNATURE
) && *s
== 0xFEFF)
1459 out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1461 if ((flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
) && *s
== 0xFFFE)
1462 out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1464 /* apply some statistical analysis */
1465 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1468 /* FIXME: checks only for ASCII characters in the unicode stream */
1469 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1471 if (s
[i
] <= 255) stats
++;
1473 if (stats
> len
/ sizeof(WCHAR
) / 2)
1474 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1477 /* Check for unicode NULL chars */
1478 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1481 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1485 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1492 * Check whether the string passed all of the tests.
1494 flags
&= ITU_IMPLEMENTED_TESTS
;
1495 if ((out_flags
& flags
) != flags
)
1504 /**************************************************************************
1505 * RtlCharToInteger (NTDLL.@)
1507 * Converts a character string into its integer equivalent.
1510 * Success: STATUS_SUCCESS. value contains the converted number
1511 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1512 * STATUS_ACCESS_VIOLATION, if value is NULL.
1515 * For base 0 it uses 10 as base and the string should be in the format
1516 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1517 * For other bases the string should be in the format
1518 * "{whitespace} [+|-] {digits}".
1519 * No check is made for value overflow, only the lower 32 bits are assigned.
1520 * If str is NULL it crashes, as the native function does.
1523 * This function does not read garbage behind '\0' as the native version does.
1525 NTSTATUS WINAPI
RtlCharToInteger(
1526 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1527 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1528 ULONG
*value
) /* [O] Destination for the converted value */
1532 ULONG RunningTotal
= 0;
1535 while (*str
!= '\0' && *str
<= ' ') {
1541 } else if (*str
== '-') {
1548 if (str
[0] == '0') {
1549 if (str
[1] == 'b') {
1552 } else if (str
[1] == 'o') {
1555 } else if (str
[1] == 'x') {
1560 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1561 return STATUS_INVALID_PARAMETER
;
1564 if (value
== NULL
) {
1565 return STATUS_ACCESS_VIOLATION
;
1568 while (*str
!= '\0') {
1570 if (chCurrent
>= '0' && chCurrent
<= '9') {
1571 digit
= chCurrent
- '0';
1572 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1573 digit
= chCurrent
- 'A' + 10;
1574 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1575 digit
= chCurrent
- 'a' + 10;
1579 if (digit
< 0 || digit
>= base
) {
1580 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1581 return STATUS_SUCCESS
;
1584 RunningTotal
= RunningTotal
* base
+ digit
;
1588 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1589 return STATUS_SUCCESS
;
1593 /**************************************************************************
1594 * RtlIntegerToChar (NTDLL.@)
1596 * Converts an unsigned integer to a character string.
1599 * Success: STATUS_SUCCESS. str contains the converted number
1600 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1601 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1602 * STATUS_ACCESS_VIOLATION, if str is NULL.
1605 * Instead of base 0 it uses 10 as base.
1606 * Writes at most length characters to the string str.
1607 * Str is '\0' terminated when length allowes it.
1608 * When str fits exactly in length characters the '\0' is ommitted.
1610 NTSTATUS WINAPI
RtlIntegerToChar(
1611 ULONG value
, /* [I] Value to be converted */
1612 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1613 ULONG length
, /* [I] Length of the str buffer in bytes */
1614 PCHAR str
) /* [O] Destination for the converted value */
1623 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1624 return STATUS_INVALID_PARAMETER
;
1632 digit
= value
% base
;
1633 value
= value
/ base
;
1637 *pos
= 'A' + digit
- 10;
1639 } while (value
!= 0L);
1641 len
= &buffer
[32] - pos
;
1643 return STATUS_BUFFER_OVERFLOW
;
1644 } else if (str
== NULL
) {
1645 return STATUS_ACCESS_VIOLATION
;
1646 } else if (len
== length
) {
1647 memcpy(str
, pos
, len
);
1649 memcpy(str
, pos
, len
+ 1);
1651 return STATUS_SUCCESS
;
1655 /**************************************************************************
1656 * RtlUnicodeStringToInteger (NTDLL.@)
1658 * Converts an unicode string into its integer equivalent.
1661 * Success: STATUS_SUCCESS. value contains the converted number
1662 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1663 * STATUS_ACCESS_VIOLATION, if value is NULL.
1666 * For base 0 it uses 10 as base and the string should be in the format
1667 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1668 * For other bases the string should be in the format
1669 * "{whitespace} [+|-] {digits}".
1670 * No check is made for value overflow, only the lower 32 bits are assigned.
1671 * If str is NULL it crashes, as the native function does.
1674 * This function does not read garbage on string length 0 as the native
1677 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1678 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1679 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1680 ULONG
*value
) /* [O] Destination for the converted value */
1682 LPWSTR lpwstr
= str
->Buffer
;
1683 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1686 ULONG RunningTotal
= 0;
1689 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1694 if (CharsRemaining
>= 1) {
1695 if (*lpwstr
== '+') {
1698 } else if (*lpwstr
== '-') {
1707 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1708 if (lpwstr
[1] == 'b') {
1710 CharsRemaining
-= 2;
1712 } else if (lpwstr
[1] == 'o') {
1714 CharsRemaining
-= 2;
1716 } else if (lpwstr
[1] == 'x') {
1718 CharsRemaining
-= 2;
1722 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1723 return STATUS_INVALID_PARAMETER
;
1726 if (value
== NULL
) {
1727 return STATUS_ACCESS_VIOLATION
;
1730 while (CharsRemaining
>= 1) {
1731 wchCurrent
= *lpwstr
;
1732 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1733 digit
= wchCurrent
- '0';
1734 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1735 digit
= wchCurrent
- 'A' + 10;
1736 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1737 digit
= wchCurrent
- 'a' + 10;
1741 if (digit
< 0 || digit
>= base
) {
1742 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1743 return STATUS_SUCCESS
;
1746 RunningTotal
= RunningTotal
* base
+ digit
;
1751 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1752 return STATUS_SUCCESS
;
1756 /**************************************************************************
1757 * RtlIntegerToUnicodeString (NTDLL.@)
1759 * Converts an unsigned integer to a '\0' terminated unicode string.
1762 * Success: STATUS_SUCCESS. str contains the converted number
1763 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1764 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1765 * (with the '\0' termination). In this case str->Length
1766 * is set to the length, the string would have (which can
1767 * be larger than the MaximumLength).
1770 * Instead of base 0 it uses 10 as base.
1771 * If str is NULL it crashes, as the native function does.
1774 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1775 * The native function does this when the string would be longer than 16
1776 * characters even when the string parameter is long enough.
1778 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1779 ULONG value
, /* [I] Value to be converted */
1780 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1781 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1789 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1790 return STATUS_INVALID_PARAMETER
;
1798 digit
= value
% base
;
1799 value
= value
/ base
;
1803 *pos
= 'A' + digit
- 10;
1805 } while (value
!= 0L);
1807 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1808 if (str
->Length
>= str
->MaximumLength
) {
1809 return STATUS_BUFFER_OVERFLOW
;
1811 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1813 return STATUS_SUCCESS
;