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
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "ntdll_misc.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
37 UINT NlsAnsiCodePage
= 1252;
38 BYTE NlsMbCodePageTag
= 0;
39 BYTE NlsMbOemCodePageTag
= 0;
41 static const union cptable
*ansi_table
;
42 static const union cptable
*oem_table
;
44 inline static const union cptable
*get_ansi_table(void)
46 if (!ansi_table
) ansi_table
= wine_cp_get_table( 1252 );
50 inline static const union cptable
*get_oem_table(void)
52 if (!oem_table
) oem_table
= wine_cp_get_table( 437 );
57 /**************************************************************************
58 * __wine_init_codepages (NTDLL.@)
60 * Set the code page once kernel32 is loaded. Should be done differently.
62 void __wine_init_codepages( const union cptable
*ansi
, const union cptable
*oem
)
66 NlsAnsiCodePage
= ansi
->info
.codepage
;
70 /**************************************************************************
71 * RtlInitAnsiString (NTDLL.@)
73 * Initializes a buffered ansi string.
79 * Assigns source to target->Buffer. The length of source is assigned to
80 * target->Length and target->MaximumLength. If source is NULL the length
81 * of source is assumed to be 0.
83 void WINAPI
RtlInitAnsiString(
84 PANSI_STRING target
, /* [I/O] Buffered ansi string to be initialized */
85 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
87 if ((target
->Buffer
= (PCHAR
) source
))
89 target
->Length
= strlen(source
);
90 target
->MaximumLength
= target
->Length
+ 1;
92 else target
->Length
= target
->MaximumLength
= 0;
96 /**************************************************************************
97 * RtlInitString (NTDLL.@)
99 * Initializes a buffered string.
105 * Assigns source to target->Buffer. The length of source is assigned to
106 * target->Length and target->MaximumLength. If source is NULL the length
107 * of source is assumed to be 0.
109 void WINAPI
RtlInitString(
110 PSTRING target
, /* [I/O] Buffered string to be initialized */
111 PCSZ source
) /* [I] '\0' terminated string used to initialize target */
113 RtlInitAnsiString( target
, source
);
117 /**************************************************************************
118 * RtlFreeAnsiString (NTDLL.@)
120 void WINAPI
RtlFreeAnsiString( PSTRING str
)
122 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
126 /**************************************************************************
127 * RtlFreeOemString (NTDLL.@)
129 void WINAPI
RtlFreeOemString( PSTRING str
)
131 RtlFreeAnsiString( str
);
135 /**************************************************************************
136 * RtlCopyString (NTDLL.@)
138 void WINAPI
RtlCopyString( STRING
*dst
, const STRING
*src
)
142 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
143 memcpy( dst
->Buffer
, src
->Buffer
, len
);
146 else dst
->Length
= 0;
150 /**************************************************************************
151 * RtlInitUnicodeString (NTDLL.@)
153 * Initializes a buffered unicode string.
159 * Assigns source to target->Buffer. The length of source is assigned to
160 * target->Length and target->MaximumLength. If source is NULL the length
161 * of source is assumed to be 0.
163 void WINAPI
RtlInitUnicodeString(
164 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
165 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
167 if ((target
->Buffer
= (PWSTR
) source
))
169 target
->Length
= strlenW(source
) * sizeof(WCHAR
);
170 target
->MaximumLength
= target
->Length
+ sizeof(WCHAR
);
172 else target
->Length
= target
->MaximumLength
= 0;
176 /**************************************************************************
177 * RtlInitUnicodeStringEx (NTDLL.@)
179 * Initializes a buffered unicode string.
182 * Success: STATUS_SUCCESS. target is initialized.
183 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
186 * Assigns source to target->Buffer. The length of source is assigned to
187 * target->Length and target->MaximumLength. If source is NULL the length
188 * of source is assumed to be 0.
190 NTSTATUS WINAPI
RtlInitUnicodeStringEx(
191 PUNICODE_STRING target
, /* [I/O] Buffered unicode string to be initialized */
192 PCWSTR source
) /* [I] '\0' terminated unicode string used to initialize target */
194 if (source
!= NULL
) {
195 unsigned int len
= strlenW(source
) * sizeof(WCHAR
);
198 return STATUS_NAME_TOO_LONG
;
200 target
->Length
= len
;
201 target
->MaximumLength
= len
+ sizeof(WCHAR
);
202 target
->Buffer
= (PWSTR
) source
;
206 target
->MaximumLength
= 0;
207 target
->Buffer
= NULL
;
209 return STATUS_SUCCESS
;
213 /**************************************************************************
214 * RtlCreateUnicodeString (NTDLL.@)
216 BOOLEAN WINAPI
RtlCreateUnicodeString( PUNICODE_STRING target
, LPCWSTR src
)
218 int len
= (strlenW(src
) + 1) * sizeof(WCHAR
);
219 if (!(target
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) return FALSE
;
220 memcpy( target
->Buffer
, src
, len
);
221 target
->MaximumLength
= len
;
222 target
->Length
= len
- sizeof(WCHAR
);
227 /**************************************************************************
228 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
230 BOOLEAN WINAPI
RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target
, LPCSTR src
)
233 RtlInitAnsiString( &ansi
, src
);
234 return !RtlAnsiStringToUnicodeString( target
, &ansi
, TRUE
);
238 /**************************************************************************
239 * RtlFreeUnicodeString (NTDLL.@)
241 void WINAPI
RtlFreeUnicodeString( PUNICODE_STRING str
)
243 if (str
->Buffer
) RtlFreeHeap( ntdll_get_process_heap(), 0, str
->Buffer
);
247 /**************************************************************************
248 * RtlCopyUnicodeString (NTDLL.@)
250 void WINAPI
RtlCopyUnicodeString( UNICODE_STRING
*dst
, const UNICODE_STRING
*src
)
254 unsigned int len
= min( src
->Length
, dst
->MaximumLength
);
255 memcpy( dst
->Buffer
, src
->Buffer
, len
);
257 /* append terminating '\0' if enough space */
258 if (len
< dst
->MaximumLength
) dst
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
260 else dst
->Length
= 0;
264 /**************************************************************************
265 * RtlDuplicateUnicodeString (NTDLL.@)
267 * Duplicates an unicode string.
270 * Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
271 * Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
272 * STATUS_NO_MEMORY, if the allocation fails.
275 * For add_nul there are several possible values:
276 * 0 = destination will not be '\0' terminated,
277 * 1 = destination will be '\0' terminated,
278 * 3 = like 1 but for an empty source string produce '\0' terminated empty
279 * Buffer instead of assigning NULL to the Buffer.
280 * Other add_nul values are invalid.
282 NTSTATUS WINAPI
RtlDuplicateUnicodeString(
283 int add_nul
, /* [I] flag */
284 const UNICODE_STRING
*source
, /* [I] Unicode string to be duplicated */
285 UNICODE_STRING
*destination
) /* [O] destination for the duplicated unicode string */
287 if (source
== NULL
|| destination
== NULL
||
288 source
->Length
> source
->MaximumLength
||
289 (source
->Length
== 0 && source
->MaximumLength
> 0 && source
->Buffer
== NULL
) ||
290 add_nul
== 2 || add_nul
>= 4 || add_nul
< 0) {
291 return STATUS_INVALID_PARAMETER
;
293 if (source
->Length
== 0 && add_nul
!= 3) {
294 destination
->Length
= 0;
295 destination
->MaximumLength
= 0;
296 destination
->Buffer
= NULL
;
298 unsigned int destination_max_len
= source
->Length
;
301 destination_max_len
+= sizeof(WCHAR
);
303 destination
->Buffer
= RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len
);
304 if (destination
->Buffer
== NULL
) {
305 return STATUS_NO_MEMORY
;
307 memcpy(destination
->Buffer
, source
->Buffer
, source
->Length
);
308 destination
->Length
= source
->Length
;
309 destination
->MaximumLength
= source
->Length
;
310 /* append terminating '\0' if enough space */
312 destination
->MaximumLength
= destination_max_len
;
313 destination
->Buffer
[destination
->Length
/ sizeof(WCHAR
)] = 0;
318 return STATUS_SUCCESS
;
322 /**************************************************************************
323 * RtlEraseUnicodeString (NTDLL.@)
325 void WINAPI
RtlEraseUnicodeString( UNICODE_STRING
*str
)
329 memset( str
->Buffer
, 0, str
->MaximumLength
);
340 /******************************************************************************
341 * RtlCompareString (NTDLL.@)
343 LONG WINAPI
RtlCompareString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
349 len
= min(s1
->Length
, s2
->Length
);
355 while (!ret
&& len
--) ret
= toupper(*p1
++) - toupper(*p2
++);
359 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
361 if (!ret
) ret
= s1
->Length
- s2
->Length
;
366 /******************************************************************************
367 * RtlCompareUnicodeString (NTDLL.@)
369 LONG WINAPI
RtlCompareUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
370 BOOLEAN CaseInsensitive
)
376 len
= min(s1
->Length
, s2
->Length
) / sizeof(WCHAR
);
382 while (!ret
&& len
--) ret
= toupperW(*p1
++) - toupperW(*p2
++);
386 while (!ret
&& len
--) ret
= *p1
++ - *p2
++;
388 if (!ret
) ret
= s1
->Length
- s2
->Length
;
393 /**************************************************************************
394 * RtlEqualString (NTDLL.@)
396 * Determine if two strings are equal.
399 * s1 [I] Source string
400 * s2 [I] String to compare to s1
401 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
404 * Non-zero if s1 is equal to s2, 0 otherwise.
406 BOOLEAN WINAPI
RtlEqualString( const STRING
*s1
, const STRING
*s2
, BOOLEAN CaseInsensitive
)
408 if (s1
->Length
!= s2
->Length
) return FALSE
;
409 return !RtlCompareString( s1
, s2
, CaseInsensitive
);
413 /**************************************************************************
414 * RtlEqualUnicodeString (NTDLL.@)
416 * Unicode version of RtlEqualString.
418 BOOLEAN WINAPI
RtlEqualUnicodeString( const UNICODE_STRING
*s1
, const UNICODE_STRING
*s2
,
419 BOOLEAN CaseInsensitive
)
421 if (s1
->Length
!= s2
->Length
) return FALSE
;
422 return !RtlCompareUnicodeString( s1
, s2
, CaseInsensitive
);
426 /**************************************************************************
427 * RtlPrefixString (NTDLL.@)
429 * Determine if one string is a prefix of another.
432 * s1 [I] Prefix to look for in s2
433 * s2 [I] String that may contain s1 as a prefix
434 * ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
437 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
439 BOOLEAN WINAPI
RtlPrefixString( const STRING
*s1
, const STRING
*s2
, BOOLEAN ignore_case
)
443 if (s1
->Length
> s2
->Length
) return FALSE
;
446 for (i
= 0; i
< s1
->Length
; i
++)
447 if (toupper(s1
->Buffer
[i
]) != toupper(s2
->Buffer
[i
])) return FALSE
;
451 for (i
= 0; i
< s1
->Length
; i
++)
452 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
458 /**************************************************************************
459 * RtlPrefixUnicodeString (NTDLL.@)
461 * Unicode version of RtlPrefixString.
463 BOOLEAN WINAPI
RtlPrefixUnicodeString( const UNICODE_STRING
*s1
,
464 const UNICODE_STRING
*s2
,
465 BOOLEAN ignore_case
)
469 if (s1
->Length
> s2
->Length
) return FALSE
;
472 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
473 if (toupperW(s1
->Buffer
[i
]) != toupperW(s2
->Buffer
[i
])) return FALSE
;
477 for (i
= 0; i
< s1
->Length
/ sizeof(WCHAR
); i
++)
478 if (s1
->Buffer
[i
] != s2
->Buffer
[i
]) return FALSE
;
484 /**************************************************************************
485 * RtlEqualComputerName (NTDLL.@)
487 * Determine if two computer names are the same.
490 * left [I] First computer name
491 * right [I] Second computer name
494 * 0 if the names are equal, non-zero otherwise.
497 * The comparason is case insensitive.
499 NTSTATUS WINAPI
RtlEqualComputerName(const UNICODE_STRING
*left
,
500 const UNICODE_STRING
*right
)
503 STRING upLeft
, upRight
;
505 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upLeft
, left
, TRUE
)))
507 if (!(ret
= RtlUpcaseUnicodeStringToOemString( &upRight
, right
, TRUE
)))
509 ret
= RtlEqualString( &upLeft
, &upRight
, FALSE
);
510 RtlFreeOemString( &upRight
);
512 RtlFreeOemString( &upLeft
);
518 /**************************************************************************
519 * RtlEqualDomainName (NTDLL.@)
521 * Determine if two domain names are the same.
524 * left [I] First domain name
525 * right [I] Second domain name
528 * 0 if the names are equal, non-zero otherwise.
531 * The comparason is case insensitive.
533 NTSTATUS WINAPI
RtlEqualDomainName(const UNICODE_STRING
*left
,
534 const UNICODE_STRING
*right
)
536 return RtlEqualComputerName(left
, right
);
541 COPY BETWEEN ANSI_STRING or UNICODE_STRING
542 there is no parameter checking, it just crashes
546 /**************************************************************************
547 * RtlAnsiStringToUnicodeString (NTDLL.@)
549 * Converts an ansi string to an unicode string.
552 * Success: STATUS_SUCCESS. uni contains the converted string
553 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
554 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
555 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
558 * This function always writes a terminating '\0'.
560 NTSTATUS WINAPI
RtlAnsiStringToUnicodeString(
561 PUNICODE_STRING uni
, /* [I/O] Destination for the unicode string */
562 PCANSI_STRING ansi
, /* [I] Ansi string to be converted */
563 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
565 DWORD total
= RtlAnsiStringToUnicodeSize( ansi
);
567 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
568 uni
->Length
= total
- sizeof(WCHAR
);
571 uni
->MaximumLength
= total
;
572 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
573 return STATUS_NO_MEMORY
;
575 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
577 RtlMultiByteToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, ansi
->Buffer
, ansi
->Length
);
578 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
579 return STATUS_SUCCESS
;
583 /**************************************************************************
584 * RtlOemStringToUnicodeString (NTDLL.@)
586 * Converts an oem string to an unicode string.
589 * Success: STATUS_SUCCESS. uni contains the converted string
590 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
591 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
592 * STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
595 * This function always writes a terminating '\0'.
597 NTSTATUS WINAPI
RtlOemStringToUnicodeString(
598 UNICODE_STRING
*uni
, /* [I/O] Destination for the unicode string */
599 const STRING
*oem
, /* [I] Oem string to be converted */
600 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
602 DWORD total
= RtlOemStringToUnicodeSize( oem
);
604 if (total
> 0xffff) return STATUS_INVALID_PARAMETER_2
;
605 uni
->Length
= total
- sizeof(WCHAR
);
608 uni
->MaximumLength
= total
;
609 if (!(uni
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, total
)))
610 return STATUS_NO_MEMORY
;
612 else if (total
> uni
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
614 RtlOemToUnicodeN( uni
->Buffer
, uni
->Length
, NULL
, oem
->Buffer
, oem
->Length
);
615 uni
->Buffer
[uni
->Length
/ sizeof(WCHAR
)] = 0;
616 return STATUS_SUCCESS
;
620 /**************************************************************************
621 * RtlUnicodeStringToAnsiString (NTDLL.@)
623 * Converts an unicode string to an ansi string.
626 * Success: STATUS_SUCCESS. ansi contains the converted string
627 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
628 * STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
631 * This function always writes a terminating '\0'.
632 * It performs a partial copy if ansi is too small.
634 NTSTATUS WINAPI
RtlUnicodeStringToAnsiString(
635 STRING
*ansi
, /* [I/O] Destination for the ansi string */
636 const UNICODE_STRING
*uni
, /* [I] Unicode string to be converted */
637 BOOLEAN doalloc
) /* [I] TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
639 NTSTATUS ret
= STATUS_SUCCESS
;
640 DWORD len
= RtlUnicodeStringToAnsiSize( uni
);
642 ansi
->Length
= len
- 1;
645 ansi
->MaximumLength
= len
;
646 if (!(ansi
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
647 return STATUS_NO_MEMORY
;
649 else if (ansi
->MaximumLength
< len
)
651 if (!ansi
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
652 ansi
->Length
= ansi
->MaximumLength
- 1;
653 ret
= STATUS_BUFFER_OVERFLOW
;
656 RtlUnicodeToMultiByteN( ansi
->Buffer
, ansi
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
657 ansi
->Buffer
[ansi
->Length
] = 0;
662 /**************************************************************************
663 * RtlUnicodeStringToOemString (NTDLL.@)
665 * Converts a Rtl Unicode string to an OEM string.
668 * oem [O] Destination for OEM string
669 * uni [I] Source Unicode string
670 * doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
673 * Success: STATUS_SUCCESS. oem contains the converted string
674 * Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
675 * STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
678 * If doalloc is TRUE, the length allocated is uni->Length + 1.
679 * This function always '\0' terminates the string returned.
681 NTSTATUS WINAPI
RtlUnicodeStringToOemString( STRING
*oem
,
682 const UNICODE_STRING
*uni
,
685 NTSTATUS ret
= STATUS_SUCCESS
;
686 DWORD len
= RtlUnicodeStringToOemSize( uni
);
688 oem
->Length
= len
- 1;
691 oem
->MaximumLength
= len
;
692 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
693 return STATUS_NO_MEMORY
;
695 else if (oem
->MaximumLength
< len
)
697 if (!oem
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
698 oem
->Length
= oem
->MaximumLength
- 1;
699 ret
= STATUS_BUFFER_OVERFLOW
;
702 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, uni
->Buffer
, uni
->Length
);
703 oem
->Buffer
[oem
->Length
] = 0;
708 /**************************************************************************
709 * RtlMultiByteToUnicodeN (NTDLL.@)
712 * Performs a partial copy if dst is too small.
714 NTSTATUS WINAPI
RtlMultiByteToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
715 LPCSTR src
, DWORD srclen
)
718 int ret
= wine_cp_mbstowcs( get_ansi_table(), 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
720 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
721 return STATUS_SUCCESS
;
725 /**************************************************************************
726 * RtlOemToUnicodeN (NTDLL.@)
728 NTSTATUS WINAPI
RtlOemToUnicodeN( LPWSTR dst
, DWORD dstlen
, LPDWORD reslen
,
729 LPCSTR src
, DWORD srclen
)
731 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, src
, srclen
, dst
, dstlen
/sizeof(WCHAR
) );
733 *reslen
= (ret
>= 0) ? ret
*sizeof(WCHAR
) : dstlen
; /* overflow -> we filled up to dstlen */
734 return STATUS_SUCCESS
;
738 /**************************************************************************
739 * RtlUnicodeToMultiByteN (NTDLL.@)
741 NTSTATUS WINAPI
RtlUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
742 LPCWSTR src
, DWORD srclen
)
744 int ret
= wine_cp_wcstombs( get_ansi_table(), 0, src
, srclen
/ sizeof(WCHAR
),
745 dst
, dstlen
, NULL
, NULL
);
747 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
748 return STATUS_SUCCESS
;
752 /**************************************************************************
753 * RtlUnicodeToOemN (NTDLL.@)
755 NTSTATUS WINAPI
RtlUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
756 LPCWSTR src
, DWORD srclen
)
758 int ret
= wine_cp_wcstombs( get_oem_table(), 0, src
, srclen
/ sizeof(WCHAR
),
759 dst
, dstlen
, NULL
, NULL
);
761 *reslen
= (ret
>= 0) ? ret
: dstlen
; /* overflow -> we filled up to dstlen */
762 return STATUS_SUCCESS
;
771 /**************************************************************************
772 * RtlUpperChar (NTDLL.@)
774 * Converts an Ascii character to uppercase.
777 * ch [I] Character to convert
780 * The uppercase character value.
783 * For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
784 * All other input characters are returned unchanged. The locale and
785 * multibyte characters are not taken into account (as native DLL).
787 CHAR WINAPI
RtlUpperChar( CHAR ch
)
789 if (ch
>= 'a' && ch
<= 'z') {
790 return ch
- 'a' + 'A';
797 /**************************************************************************
798 * RtlUpperString (NTDLL.@)
800 * Converts an Ascii string to uppercase.
803 * dst [O] Destination for converted string
804 * src [I] Source string to convert
810 * For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
811 * All other src characters are copied unchanged to dst. The locale and
812 * multibyte characters are not taken into account (as native DLL).
813 * The number of character copied is the minimum of src->Length and
814 * the dst->MaximumLength.
816 void WINAPI
RtlUpperString( STRING
*dst
, const STRING
*src
)
818 unsigned int i
, len
= min(src
->Length
, dst
->MaximumLength
);
820 for (i
= 0; i
< len
; i
++) dst
->Buffer
[i
] = RtlUpperChar(src
->Buffer
[i
]);
825 /**************************************************************************
826 * RtlUpcaseUnicodeChar (NTDLL.@)
828 * Converts an Unicode character to uppercase.
831 * wch [I] Character to convert
834 * The uppercase character value.
836 WCHAR WINAPI
RtlUpcaseUnicodeChar( WCHAR wch
)
838 return toupperW(wch
);
842 /**************************************************************************
843 * RtlDowncaseUnicodeChar (NTDLL.@)
845 * Converts an Unicode character to lowercase.
848 * wch [I] Character to convert
851 * The lowercase character value.
853 WCHAR WINAPI
RtlDowncaseUnicodeChar(WCHAR wch
)
855 return tolowerW(wch
);
859 /**************************************************************************
860 * RtlUpcaseUnicodeString (NTDLL.@)
862 * Converts an Unicode string to uppercase.
865 * dest [O] Destination for converted string
866 * src [I] Source string to convert
867 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
870 * Success: STATUS_SUCCESS. dest contains the converted string.
871 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
872 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
875 * dest is never '\0' terminated because it may be equal to src, and src
876 * might not be '\0' terminated. dest->Length is only set upon success.
878 NTSTATUS WINAPI
RtlUpcaseUnicodeString( UNICODE_STRING
*dest
,
879 const UNICODE_STRING
*src
,
882 DWORD i
, len
= src
->Length
;
886 dest
->MaximumLength
= len
;
887 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
888 return STATUS_NO_MEMORY
;
890 else if (len
> dest
->MaximumLength
) return STATUS_BUFFER_OVERFLOW
;
892 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) dest
->Buffer
[i
] = toupperW(src
->Buffer
[i
]);
894 return STATUS_SUCCESS
;
898 /**************************************************************************
899 * RtlDowncaseUnicodeString (NTDLL.@)
901 * Converts an Unicode string to lowercase.
904 * dest [O] Destination for converted string
905 * src [I] Source string to convert
906 * doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
909 * Success: STATUS_SUCCESS. dest contains the converted string.
910 * Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
911 * STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
914 * dest is never '\0' terminated because it may be equal to src, and src
915 * might not be '\0' terminated. dest->Length is only set upon success.
917 NTSTATUS WINAPI
RtlDowncaseUnicodeString(
918 UNICODE_STRING
*dest
,
919 const UNICODE_STRING
*src
,
923 DWORD len
= src
->Length
;
926 dest
->MaximumLength
= len
;
927 if (!(dest
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
))) {
928 return STATUS_NO_MEMORY
;
930 } else if (len
> dest
->MaximumLength
) {
931 return STATUS_BUFFER_OVERFLOW
;
934 for (i
= 0; i
< len
/sizeof(WCHAR
); i
++) {
935 dest
->Buffer
[i
] = tolowerW(src
->Buffer
[i
]);
938 return STATUS_SUCCESS
;
942 /**************************************************************************
943 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
946 * writes terminating 0
948 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToAnsiString( STRING
*dst
,
949 const UNICODE_STRING
*src
,
953 UNICODE_STRING upcase
;
955 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
957 ret
= RtlUnicodeStringToAnsiString( dst
, &upcase
, doalloc
);
958 RtlFreeUnicodeString( &upcase
);
964 /**************************************************************************
965 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
968 * writes terminating 0
970 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToOemString( STRING
*dst
,
971 const UNICODE_STRING
*src
,
975 UNICODE_STRING upcase
;
977 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, src
, TRUE
)))
979 ret
= RtlUnicodeStringToOemString( dst
, &upcase
, doalloc
);
980 RtlFreeUnicodeString( &upcase
);
986 /**************************************************************************
987 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
990 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
992 NTSTATUS WINAPI
RtlUpcaseUnicodeStringToCountedOemString( STRING
*oem
,
993 const UNICODE_STRING
*uni
,
997 UNICODE_STRING upcase
;
999 if (!(ret
= RtlUpcaseUnicodeString( &upcase
, uni
, TRUE
)))
1001 DWORD len
= RtlUnicodeStringToOemSize( &upcase
) - 1;
1005 oem
->MaximumLength
= len
;
1006 if (!(oem
->Buffer
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
)))
1008 ret
= STATUS_NO_MEMORY
;
1012 else if (oem
->MaximumLength
< len
)
1014 ret
= STATUS_BUFFER_OVERFLOW
;
1015 oem
->Length
= oem
->MaximumLength
;
1016 if (!oem
->MaximumLength
) goto done
;
1018 RtlUnicodeToOemN( oem
->Buffer
, oem
->Length
, NULL
, upcase
.Buffer
, upcase
.Length
);
1020 RtlFreeUnicodeString( &upcase
);
1026 /**************************************************************************
1027 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1029 NTSTATUS WINAPI
RtlUpcaseUnicodeToMultiByteN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1030 LPCWSTR src
, DWORD srclen
)
1036 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1037 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1038 ret
= RtlUnicodeToMultiByteN( dst
, dstlen
, reslen
, upcase
, srclen
);
1039 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1044 /**************************************************************************
1045 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1047 NTSTATUS WINAPI
RtlUpcaseUnicodeToOemN( LPSTR dst
, DWORD dstlen
, LPDWORD reslen
,
1048 LPCWSTR src
, DWORD srclen
)
1054 if (!(upcase
= RtlAllocateHeap( ntdll_get_process_heap(), 0, srclen
))) return STATUS_NO_MEMORY
;
1055 for (i
= 0; i
< srclen
/sizeof(WCHAR
); i
++) upcase
[i
] = toupperW(src
[i
]);
1056 ret
= RtlUnicodeToOemN( dst
, dstlen
, reslen
, upcase
, srclen
);
1057 RtlFreeHeap( ntdll_get_process_heap(), 0, upcase
);
1067 /**************************************************************************
1068 * RtlOemStringToUnicodeSize (NTDLL.@)
1069 * RtlxOemStringToUnicodeSize (NTDLL.@)
1071 * Calculate the size in bytes necessary for the Unicode conversion of str,
1072 * including the terminating '\0'.
1075 * str [I] String to calculate the size of
1078 * The calculated size.
1080 UINT WINAPI
RtlOemStringToUnicodeSize( const STRING
*str
)
1082 int ret
= wine_cp_mbstowcs( get_oem_table(), 0, str
->Buffer
, str
->Length
, NULL
, 0 );
1083 return (ret
+ 1) * sizeof(WCHAR
);
1087 /**************************************************************************
1088 * RtlAnsiStringToUnicodeSize (NTDLL.@)
1089 * RtlxAnsiStringToUnicodeSize (NTDLL.@)
1091 * Calculate the size in bytes necessary for the Unicode conversion of str,
1092 * including the terminating '\0'.
1095 * str [I] String to calculate the size of
1098 * The calculated size.
1100 DWORD WINAPI
RtlAnsiStringToUnicodeSize( const STRING
*str
)
1103 RtlMultiByteToUnicodeSize( &ret
, str
->Buffer
, str
->Length
);
1104 return ret
+ sizeof(WCHAR
);
1108 /**************************************************************************
1109 * RtlMultiByteToUnicodeSize (NTDLL.@)
1111 * Compute the size in bytes necessary for the Unicode conversion of str,
1112 * without the terminating '\0'.
1115 * size [O] Destination for size
1116 * str [I] String to calculate the size of
1117 * len [I] Length of str
1122 NTSTATUS WINAPI
RtlMultiByteToUnicodeSize( DWORD
*size
, LPCSTR str
, UINT len
)
1124 *size
= wine_cp_mbstowcs( get_ansi_table(), 0, str
, len
, NULL
, 0 ) * sizeof(WCHAR
);
1125 return STATUS_SUCCESS
;
1129 /**************************************************************************
1130 * RtlUnicodeToMultiByteSize (NTDLL.@)
1132 * Calculate the size in bytes necessary for the multibyte conversion of str,
1133 * without the terminating '\0'.
1136 * size [O] Destination for size
1137 * str [I] String to calculate the size of
1138 * len [I] Length of str
1143 NTSTATUS WINAPI
RtlUnicodeToMultiByteSize( PULONG size
, LPCWSTR str
, ULONG len
)
1145 *size
= wine_cp_wcstombs( get_ansi_table(), 0, str
, len
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
1146 return STATUS_SUCCESS
;
1150 /**************************************************************************
1151 * RtlUnicodeStringToAnsiSize (NTDLL.@)
1152 * RtlxUnicodeStringToAnsiSize (NTDLL.@)
1154 * Calculate the size in bytes necessary for the Ansi conversion of str,
1155 * including the terminating '\0'.
1158 * str [I] String to calculate the size of
1161 * The calculated size.
1163 DWORD WINAPI
RtlUnicodeStringToAnsiSize( const UNICODE_STRING
*str
)
1166 RtlUnicodeToMultiByteSize( &ret
, str
->Buffer
, str
->Length
);
1171 /**************************************************************************
1172 * RtlUnicodeStringToOemSize (NTDLL.@)
1173 * RtlxUnicodeStringToOemSize (NTDLL.@)
1175 * Calculate the size in bytes necessary for the OEM conversion of str,
1176 * including the terminating '\0'.
1179 * str [I] String to calculate the size of
1182 * The calculated size.
1184 DWORD WINAPI
RtlUnicodeStringToOemSize( const UNICODE_STRING
*str
)
1186 return wine_cp_wcstombs( get_oem_table(), 0, str
->Buffer
, str
->Length
/ sizeof(WCHAR
),
1187 NULL
, 0, NULL
, NULL
) + 1;
1191 /**************************************************************************
1192 * RtlAppendAsciizToString (NTDLL.@)
1194 * Concatenates a buffered character string and a '\0' terminated character
1198 * Success: STATUS_SUCCESS. src is appended to dest.
1199 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1200 * to hold the concatenated string.
1203 * if src is NULL dest is unchanged.
1204 * dest is never '\0' terminated.
1206 NTSTATUS WINAPI
RtlAppendAsciizToString(
1207 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1208 LPCSTR src
) /* [I] '\0' terminated character string to be concatenated */
1211 unsigned int src_len
= strlen(src
);
1212 unsigned int dest_len
= src_len
+ dest
->Length
;
1214 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1215 memcpy(dest
->Buffer
+ dest
->Length
, src
, src_len
);
1216 dest
->Length
= dest_len
;
1218 return STATUS_SUCCESS
;
1222 /**************************************************************************
1223 * RtlAppendStringToString (NTDLL.@)
1225 * Concatenates two buffered character strings
1228 * Success: STATUS_SUCCESS. src is appended to dest.
1229 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1230 * to hold the concatenated string.
1233 * if src->length is zero dest is unchanged.
1234 * dest is never '\0' terminated.
1236 NTSTATUS WINAPI
RtlAppendStringToString(
1237 STRING
*dest
, /* [I/O] Buffered character string to which src is concatenated */
1238 const STRING
*src
) /* [I] Buffered character string to be concatenated */
1240 if (src
->Length
!= 0) {
1241 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1243 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1244 memcpy(dest
->Buffer
+ dest
->Length
, src
->Buffer
, src
->Length
);
1245 dest
->Length
= dest_len
;
1247 return STATUS_SUCCESS
;
1251 /**************************************************************************
1252 * RtlAppendUnicodeToString (NTDLL.@)
1254 * Concatenates an buffered unicode string and a '\0' terminated unicode
1258 * Success: STATUS_SUCCESS. src is appended to dest.
1259 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1260 * to hold the concatenated string.
1263 * if src is NULL dest is unchanged.
1264 * dest is '\0' terminated when the MaximumLength allowes it.
1265 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1268 * Does not write in the src->Buffer beyond MaximumLength when
1269 * MaximumLength is odd as the native function does.
1271 NTSTATUS WINAPI
RtlAppendUnicodeToString(
1272 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1273 LPCWSTR src
) /* [I] '\0' terminated unicode string to be concatenated */
1276 unsigned int src_len
= strlenW(src
) * sizeof(WCHAR
);
1277 unsigned int dest_len
= src_len
+ dest
->Length
;
1279 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1280 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
, src_len
);
1281 dest
->Length
= dest_len
;
1282 /* append terminating '\0' if enough space */
1283 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1284 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1287 return STATUS_SUCCESS
;
1291 /**************************************************************************
1292 * RtlAppendUnicodeStringToString (NTDLL.@)
1294 * Concatenates two buffered unicode strings
1297 * Success: STATUS_SUCCESS. src is appended to dest.
1298 * Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is to small
1299 * to hold the concatenated string.
1302 * if src->length is zero dest is unchanged.
1303 * dest is '\0' terminated when the MaximumLength allowes it.
1304 * When dest fits exactly in MaximumLength characters the '\0' is ommitted.
1307 * Does not write in the src->Buffer beyond MaximumLength when
1308 * MaximumLength is odd as the native function does.
1310 NTSTATUS WINAPI
RtlAppendUnicodeStringToString(
1311 UNICODE_STRING
*dest
, /* [I/O] Buffered unicode string to which src is concatenated */
1312 const UNICODE_STRING
*src
) /* [I] Buffered unicode string to be concatenated */
1314 if (src
->Length
!= 0) {
1315 unsigned int dest_len
= src
->Length
+ dest
->Length
;
1317 if (dest_len
> dest
->MaximumLength
) return STATUS_BUFFER_TOO_SMALL
;
1318 memcpy(dest
->Buffer
+ dest
->Length
/sizeof(WCHAR
), src
->Buffer
, src
->Length
);
1319 dest
->Length
= dest_len
;
1320 /* append terminating '\0' if enough space */
1321 if (dest_len
+ sizeof(WCHAR
) <= dest
->MaximumLength
) {
1322 dest
->Buffer
[dest_len
/ sizeof(WCHAR
)] = 0;
1325 return STATUS_SUCCESS
;
1329 /**************************************************************************
1330 * RtlFindCharInUnicodeString (NTDLL.@)
1332 * Searches for one of several unicode characters in an unicode string.
1335 * Success: STATUS_SUCCESS. pos contains the position after the character found.
1336 * Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
1338 NTSTATUS WINAPI
RtlFindCharInUnicodeString(
1339 int flags
, /* [I] Flags */
1340 const UNICODE_STRING
*main_str
, /* [I] Unicode string in which one or more characters are searched */
1341 const UNICODE_STRING
*search_chars
, /* [I] Unicode string which contains the characters to search for */
1342 USHORT
*pos
) /* [O] Position of the first character found + 2 */
1345 unsigned int search_idx
;
1349 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1350 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1351 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1352 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1353 return STATUS_SUCCESS
;
1358 return STATUS_NOT_FOUND
;
1360 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1361 for (search_idx
= 0; search_idx
< search_chars
->Length
/ sizeof(WCHAR
); search_idx
++) {
1362 if (main_str
->Buffer
[main_idx
] == search_chars
->Buffer
[search_idx
]) {
1363 *pos
= main_idx
* sizeof(WCHAR
);
1364 return STATUS_SUCCESS
;
1369 return STATUS_NOT_FOUND
;
1371 for (main_idx
= 0; main_idx
< main_str
->Length
/ sizeof(WCHAR
); main_idx
++) {
1373 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1374 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1377 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1378 *pos
= (main_idx
+ 1) * sizeof(WCHAR
);
1379 return STATUS_SUCCESS
;
1383 return STATUS_NOT_FOUND
;
1385 for (main_idx
= main_str
->Length
/ sizeof(WCHAR
) - 1; main_idx
>= 0; main_idx
--) {
1387 while (search_idx
< search_chars
->Length
/ sizeof(WCHAR
) &&
1388 main_str
->Buffer
[main_idx
] != search_chars
->Buffer
[search_idx
]) {
1391 if (search_idx
>= search_chars
->Length
/ sizeof(WCHAR
)) {
1392 *pos
= main_idx
* sizeof(WCHAR
);
1393 return STATUS_SUCCESS
;
1397 return STATUS_NOT_FOUND
;
1399 return STATUS_NOT_FOUND
;
1406 /* Tests that we currently implement */
1407 #define ITU_IMPLEMENTED_TESTS \
1408 (IS_TEXT_UNICODE_SIGNATURE | \
1409 IS_TEXT_UNICODE_REVERSE_SIGNATURE | \
1410 IS_TEXT_UNICODE_ODD_LENGTH | \
1411 IS_TEXT_UNICODE_STATISTICS | \
1412 IS_TEXT_UNICODE_NULL_BYTES)
1414 /**************************************************************************
1415 * RtlIsTextUnicode (NTDLL.@)
1417 * Attempt to guess whether a text buffer is Unicode.
1420 * buf [I] Text buffer to test
1421 * len [I] Length of buf
1422 * pf [O] Destination for test results
1425 * The length of the string if all tests were passed, 0 otherwise.
1428 * Should implement more tests.
1430 DWORD WINAPI
RtlIsTextUnicode(
1436 DWORD flags
= -1, out_flags
= 0;
1443 * Apply various tests to the text string. According to the
1444 * docs, each test "passed" sets the corresponding flag in
1445 * the output flags. But some of the tests are mutually
1446 * exclusive, so I don't see how you could pass all tests ...
1449 /* Check for an odd length ... pass if even. */
1450 if ((flags
& IS_TEXT_UNICODE_ODD_LENGTH
) && (len
& 1))
1451 out_flags
|= IS_TEXT_UNICODE_ODD_LENGTH
;
1453 /* Check for the special byte order unicode marks. */
1454 if ((flags
& IS_TEXT_UNICODE_SIGNATURE
) && *s
== 0xFEFF)
1455 out_flags
|= IS_TEXT_UNICODE_SIGNATURE
;
1457 if ((flags
& IS_TEXT_UNICODE_REVERSE_SIGNATURE
) && *s
== 0xFFFE)
1458 out_flags
|= IS_TEXT_UNICODE_REVERSE_SIGNATURE
;
1460 /* apply some statistical analysis */
1461 if (flags
& IS_TEXT_UNICODE_STATISTICS
)
1464 /* FIXME: checks only for ASCII characters in the unicode stream */
1465 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1467 if (s
[i
] <= 255) stats
++;
1469 if (stats
> len
/ sizeof(WCHAR
) / 2)
1470 out_flags
|= IS_TEXT_UNICODE_STATISTICS
;
1473 /* Check for unicode NULL chars */
1474 if (flags
& IS_TEXT_UNICODE_NULL_BYTES
)
1477 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
1481 out_flags
|= IS_TEXT_UNICODE_NULL_BYTES
;
1488 * Check whether the string passed all of the tests.
1490 flags
&= ITU_IMPLEMENTED_TESTS
;
1491 if ((out_flags
& flags
) != flags
)
1500 /**************************************************************************
1501 * RtlCharToInteger (NTDLL.@)
1503 * Converts a character string into its integer equivalent.
1506 * Success: STATUS_SUCCESS. value contains the converted number
1507 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1508 * STATUS_ACCESS_VIOLATION, if value is NULL.
1511 * For base 0 it uses 10 as base and the string should be in the format
1512 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1513 * For other bases the string should be in the format
1514 * "{whitespace} [+|-] {digits}".
1515 * No check is made for value overflow, only the lower 32 bits are assigned.
1516 * If str is NULL it crashes, as the native function does.
1519 * This function does not read garbage behind '\0' as the native version does.
1521 NTSTATUS WINAPI
RtlCharToInteger(
1522 PCSZ str
, /* [I] '\0' terminated single-byte string containing a number */
1523 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1524 ULONG
*value
) /* [O] Destination for the converted value */
1528 ULONG RunningTotal
= 0;
1531 while (*str
!= '\0' && *str
<= ' ') {
1537 } else if (*str
== '-') {
1544 if (str
[0] == '0') {
1545 if (str
[1] == 'b') {
1548 } else if (str
[1] == 'o') {
1551 } else if (str
[1] == 'x') {
1556 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1557 return STATUS_INVALID_PARAMETER
;
1560 if (value
== NULL
) {
1561 return STATUS_ACCESS_VIOLATION
;
1564 while (*str
!= '\0') {
1566 if (chCurrent
>= '0' && chCurrent
<= '9') {
1567 digit
= chCurrent
- '0';
1568 } else if (chCurrent
>= 'A' && chCurrent
<= 'Z') {
1569 digit
= chCurrent
- 'A' + 10;
1570 } else if (chCurrent
>= 'a' && chCurrent
<= 'z') {
1571 digit
= chCurrent
- 'a' + 10;
1575 if (digit
< 0 || digit
>= base
) {
1576 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1577 return STATUS_SUCCESS
;
1580 RunningTotal
= RunningTotal
* base
+ digit
;
1584 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1585 return STATUS_SUCCESS
;
1589 /**************************************************************************
1590 * RtlIntegerToChar (NTDLL.@)
1592 * Converts an unsigned integer to a character string.
1595 * Success: STATUS_SUCCESS. str contains the converted number
1596 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1597 * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
1598 * STATUS_ACCESS_VIOLATION, if str is NULL.
1601 * Instead of base 0 it uses 10 as base.
1602 * Writes at most length characters to the string str.
1603 * Str is '\0' terminated when length allowes it.
1604 * When str fits exactly in length characters the '\0' is ommitted.
1606 NTSTATUS WINAPI
RtlIntegerToChar(
1607 ULONG value
, /* [I] Value to be converted */
1608 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1609 ULONG length
, /* [I] Length of the str buffer in bytes */
1610 PCHAR str
) /* [O] Destination for the converted value */
1619 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1620 return STATUS_INVALID_PARAMETER
;
1628 digit
= value
% base
;
1629 value
= value
/ base
;
1633 *pos
= 'A' + digit
- 10;
1635 } while (value
!= 0L);
1637 len
= &buffer
[32] - pos
;
1639 return STATUS_BUFFER_OVERFLOW
;
1640 } else if (str
== NULL
) {
1641 return STATUS_ACCESS_VIOLATION
;
1642 } else if (len
== length
) {
1643 memcpy(str
, pos
, len
);
1645 memcpy(str
, pos
, len
+ 1);
1647 return STATUS_SUCCESS
;
1651 /**************************************************************************
1652 * RtlUnicodeStringToInteger (NTDLL.@)
1654 * Converts an unicode string into its integer equivalent.
1657 * Success: STATUS_SUCCESS. value contains the converted number
1658 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1659 * STATUS_ACCESS_VIOLATION, if value is NULL.
1662 * For base 0 it uses 10 as base and the string should be in the format
1663 * "{whitespace} [+|-] [0[x|o|b]] {digits}".
1664 * For other bases the string should be in the format
1665 * "{whitespace} [+|-] {digits}".
1666 * No check is made for value overflow, only the lower 32 bits are assigned.
1667 * If str is NULL it crashes, as the native function does.
1670 * This function does not read garbage on string length 0 as the native
1673 NTSTATUS WINAPI
RtlUnicodeStringToInteger(
1674 const UNICODE_STRING
*str
, /* [I] Unicode string to be converted */
1675 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1676 ULONG
*value
) /* [O] Destination for the converted value */
1678 LPWSTR lpwstr
= str
->Buffer
;
1679 USHORT CharsRemaining
= str
->Length
/ sizeof(WCHAR
);
1682 ULONG RunningTotal
= 0;
1685 while (CharsRemaining
>= 1 && *lpwstr
<= ' ') {
1690 if (CharsRemaining
>= 1) {
1691 if (*lpwstr
== '+') {
1694 } else if (*lpwstr
== '-') {
1703 if (CharsRemaining
>= 2 && lpwstr
[0] == '0') {
1704 if (lpwstr
[1] == 'b') {
1706 CharsRemaining
-= 2;
1708 } else if (lpwstr
[1] == 'o') {
1710 CharsRemaining
-= 2;
1712 } else if (lpwstr
[1] == 'x') {
1714 CharsRemaining
-= 2;
1718 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1719 return STATUS_INVALID_PARAMETER
;
1722 if (value
== NULL
) {
1723 return STATUS_ACCESS_VIOLATION
;
1726 while (CharsRemaining
>= 1) {
1727 wchCurrent
= *lpwstr
;
1728 if (wchCurrent
>= '0' && wchCurrent
<= '9') {
1729 digit
= wchCurrent
- '0';
1730 } else if (wchCurrent
>= 'A' && wchCurrent
<= 'Z') {
1731 digit
= wchCurrent
- 'A' + 10;
1732 } else if (wchCurrent
>= 'a' && wchCurrent
<= 'z') {
1733 digit
= wchCurrent
- 'a' + 10;
1737 if (digit
< 0 || digit
>= base
) {
1738 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1739 return STATUS_SUCCESS
;
1742 RunningTotal
= RunningTotal
* base
+ digit
;
1747 *value
= bMinus
? -RunningTotal
: RunningTotal
;
1748 return STATUS_SUCCESS
;
1752 /**************************************************************************
1753 * RtlIntegerToUnicodeString (NTDLL.@)
1755 * Converts an unsigned integer to a '\0' terminated unicode string.
1758 * Success: STATUS_SUCCESS. str contains the converted number
1759 * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
1760 * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
1761 * (with the '\0' termination). In this case str->Length
1762 * is set to the length, the string would have (which can
1763 * be larger than the MaximumLength).
1766 * Instead of base 0 it uses 10 as base.
1767 * If str is NULL it crashes, as the native function does.
1770 * Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
1771 * The native function does this when the string would be longer than 16
1772 * characters even when the string parameter is long enough.
1774 NTSTATUS WINAPI
RtlIntegerToUnicodeString(
1775 ULONG value
, /* [I] Value to be converted */
1776 ULONG base
, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
1777 UNICODE_STRING
*str
) /* [O] Destination for the converted value */
1785 } else if (base
!= 2 && base
!= 8 && base
!= 10 && base
!= 16) {
1786 return STATUS_INVALID_PARAMETER
;
1794 digit
= value
% base
;
1795 value
= value
/ base
;
1799 *pos
= 'A' + digit
- 10;
1801 } while (value
!= 0L);
1803 str
->Length
= (&buffer
[32] - pos
) * sizeof(WCHAR
);
1804 if (str
->Length
>= str
->MaximumLength
) {
1805 return STATUS_BUFFER_OVERFLOW
;
1807 memcpy(str
->Buffer
, pos
, str
->Length
+ sizeof(WCHAR
));
1809 return STATUS_SUCCESS
;