Apply only specified tests.
[wine/wine-kai.git] / dlls / ntdll / rtlstr.c
blob71a6be7b811a5ac23a13a569c47859445f7069fd
1 /*
2 * Rtl string functions
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
23 #include "config.h"
25 #include <assert.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "winternl.h"
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 );
47 return ansi_table;
50 inline static const union cptable *get_oem_table(void)
52 if (!oem_table) oem_table = wine_cp_get_table( 437 );
53 return oem_table;
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 )
64 ansi_table = ansi;
65 oem_table = oem;
66 NlsAnsiCodePage = ansi->info.codepage;
70 /**************************************************************************
71 * RtlInitAnsiString (NTDLL.@)
73 * Initializes a buffered ansi string.
75 * RETURNS
76 * Nothing.
78 * NOTES
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.
101 * RETURNS
102 * Nothing.
104 * NOTES
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 )
140 if (src)
142 unsigned int len = min( src->Length, dst->MaximumLength );
143 memcpy( dst->Buffer, src->Buffer, len );
144 dst->Length = len;
146 else dst->Length = 0;
150 /**************************************************************************
151 * RtlInitUnicodeString (NTDLL.@)
153 * Initializes a buffered unicode string.
155 * RETURNS
156 * Nothing.
158 * NOTES
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.
181 * RETURNS
182 * Success: STATUS_SUCCESS. target is initialized.
183 * Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
185 * NOTES
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);
197 if (len > 0xFFFC) {
198 return STATUS_NAME_TOO_LONG;
199 } else {
200 target->Length = len;
201 target->MaximumLength = len + sizeof(WCHAR);
202 target->Buffer = (PWSTR) source;
203 } /* if */
204 } else {
205 target->Length = 0;
206 target->MaximumLength = 0;
207 target->Buffer = NULL;
208 } /* if */
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);
223 return TRUE;
227 /**************************************************************************
228 * RtlCreateUnicodeStringFromAsciiz (NTDLL.@)
230 BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
232 STRING ansi;
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 )
252 if (src)
254 unsigned int len = min( src->Length, dst->MaximumLength );
255 memcpy( dst->Buffer, src->Buffer, len );
256 dst->Length = 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.
269 * RETURNS
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.
274 * NOTES
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;
292 } else {
293 if (source->Length == 0 && add_nul != 3) {
294 destination->Length = 0;
295 destination->MaximumLength = 0;
296 destination->Buffer = NULL;
297 } else {
298 unsigned int destination_max_len = source->Length;
300 if (add_nul) {
301 destination_max_len += sizeof(WCHAR);
302 } /* if */
303 destination->Buffer = RtlAllocateHeap(ntdll_get_process_heap(), 0, destination_max_len);
304 if (destination->Buffer == NULL) {
305 return STATUS_NO_MEMORY;
306 } else {
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 */
311 if (add_nul) {
312 destination->MaximumLength = destination_max_len;
313 destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
314 } /* if */
315 } /* if */
316 } /* if */
317 } /* if */
318 return STATUS_SUCCESS;
322 /**************************************************************************
323 * RtlEraseUnicodeString (NTDLL.@)
325 void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
327 if (str->Buffer)
329 memset( str->Buffer, 0, str->MaximumLength );
330 str->Length = 0;
336 COMPARISON FUNCTIONS
340 /******************************************************************************
341 * RtlCompareString (NTDLL.@)
343 LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
345 unsigned int len;
346 LONG ret = 0;
347 LPCSTR p1, p2;
349 len = min(s1->Length, s2->Length);
350 p1 = s1->Buffer;
351 p2 = s2->Buffer;
353 if (CaseInsensitive)
355 while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++);
357 else
359 while (!ret && len--) ret = *p1++ - *p2++;
361 if (!ret) ret = s1->Length - s2->Length;
362 return ret;
366 /******************************************************************************
367 * RtlCompareUnicodeString (NTDLL.@)
369 LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
370 BOOLEAN CaseInsensitive )
372 unsigned int len;
373 LONG ret = 0;
374 LPCWSTR p1, p2;
376 len = min(s1->Length, s2->Length) / sizeof(WCHAR);
377 p1 = s1->Buffer;
378 p2 = s2->Buffer;
380 if (CaseInsensitive)
382 while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
384 else
386 while (!ret && len--) ret = *p1++ - *p2++;
388 if (!ret) ret = s1->Length - s2->Length;
389 return ret;
393 /**************************************************************************
394 * RtlEqualString (NTDLL.@)
396 * Determine if two strings are equal.
398 * PARAMS
399 * s1 [I] Source string
400 * s2 [I] String to compare to s1
401 * CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
403 * RETURNS
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.
431 * PARAMS
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
436 * RETURNS
437 * TRUE if s2 contains s1 as a prefix, FALSE otherwise.
439 BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
441 unsigned int i;
443 if (s1->Length > s2->Length) return FALSE;
444 if (ignore_case)
446 for (i = 0; i < s1->Length; i++)
447 if (toupper(s1->Buffer[i]) != toupper(s2->Buffer[i])) return FALSE;
449 else
451 for (i = 0; i < s1->Length; i++)
452 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
454 return TRUE;
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 )
467 unsigned int i;
469 if (s1->Length > s2->Length) return FALSE;
470 if (ignore_case)
472 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
473 if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
475 else
477 for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
478 if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
480 return TRUE;
484 /**************************************************************************
485 * RtlEqualComputerName (NTDLL.@)
487 * Determine if two computer names are the same.
489 * PARAMS
490 * left [I] First computer name
491 * right [I] Second computer name
493 * RETURNS
494 * 0 if the names are equal, non-zero otherwise.
496 * NOTES
497 * The comparason is case insensitive.
499 NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
500 const UNICODE_STRING *right)
502 NTSTATUS ret;
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 );
514 return ret;
518 /**************************************************************************
519 * RtlEqualDomainName (NTDLL.@)
521 * Determine if two domain names are the same.
523 * PARAMS
524 * left [I] First domain name
525 * right [I] Second domain name
527 * RETURNS
528 * 0 if the names are equal, non-zero otherwise.
530 * NOTES
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.
551 * RETURNS
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.
557 * NOTES
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);
569 if (doalloc)
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.
588 * RETURNS
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.
594 * NOTES
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);
606 if (doalloc)
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.
625 * RETURNS
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.
630 * NOTES
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;
643 if (doalloc)
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;
658 return ret;
662 /**************************************************************************
663 * RtlUnicodeStringToOemString (NTDLL.@)
665 * Converts a Rtl Unicode string to an OEM string.
667 * PARAMS
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
672 * RETURNS
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.
677 * NOTES
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,
683 BOOLEAN doalloc )
685 NTSTATUS ret = STATUS_SUCCESS;
686 DWORD len = RtlUnicodeStringToOemSize( uni );
688 oem->Length = len - 1;
689 if (doalloc)
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;
704 return ret;
708 /**************************************************************************
709 * RtlMultiByteToUnicodeN (NTDLL.@)
711 * NOTES
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) );
719 if (reslen)
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) );
732 if (reslen)
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 );
746 if (reslen)
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 );
760 if (reslen)
761 *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
762 return STATUS_SUCCESS;
767 CASE CONVERSIONS
771 /**************************************************************************
772 * RtlUpperChar (NTDLL.@)
774 * Converts an Ascii character to uppercase.
776 * PARAMS
777 * ch [I] Character to convert
779 * RETURNS
780 * The uppercase character value.
782 * NOTES
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';
791 } else {
792 return ch;
793 } /* if */
797 /**************************************************************************
798 * RtlUpperString (NTDLL.@)
800 * Converts an Ascii string to uppercase.
802 * PARAMS
803 * dst [O] Destination for converted string
804 * src [I] Source string to convert
806 * RETURNS
807 * Nothing.
809 * NOTES
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]);
821 dst->Length = len;
825 /**************************************************************************
826 * RtlUpcaseUnicodeChar (NTDLL.@)
828 * Converts an Unicode character to uppercase.
830 * PARAMS
831 * wch [I] Character to convert
833 * RETURNS
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.
847 * PARAMS
848 * wch [I] Character to convert
850 * RETURNS
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.
864 * PARAMS
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
869 * RETURNS
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.
874 * NOTES
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,
880 BOOLEAN doalloc)
882 DWORD i, len = src->Length;
884 if (doalloc)
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]);
893 dest->Length = len;
894 return STATUS_SUCCESS;
898 /**************************************************************************
899 * RtlDowncaseUnicodeString (NTDLL.@)
901 * Converts an Unicode string to lowercase.
903 * PARAMS
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
908 * RETURNS
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.
913 * NOTES
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,
920 BOOLEAN doalloc)
922 DWORD i;
923 DWORD len = src->Length;
925 if (doalloc) {
926 dest->MaximumLength = len;
927 if (!(dest->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len ))) {
928 return STATUS_NO_MEMORY;
929 } /* if */
930 } else if (len > dest->MaximumLength) {
931 return STATUS_BUFFER_OVERFLOW;
932 } /* if */
934 for (i = 0; i < len/sizeof(WCHAR); i++) {
935 dest->Buffer[i] = tolowerW(src->Buffer[i]);
936 } /* for */
937 dest->Length = len;
938 return STATUS_SUCCESS;
942 /**************************************************************************
943 * RtlUpcaseUnicodeStringToAnsiString (NTDLL.@)
945 * NOTES
946 * writes terminating 0
948 NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
949 const UNICODE_STRING *src,
950 BOOLEAN doalloc )
952 NTSTATUS ret;
953 UNICODE_STRING upcase;
955 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
957 ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
958 RtlFreeUnicodeString( &upcase );
960 return ret;
964 /**************************************************************************
965 * RtlUpcaseUnicodeStringToOemString (NTDLL.@)
967 * NOTES
968 * writes terminating 0
970 NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
971 const UNICODE_STRING *src,
972 BOOLEAN doalloc )
974 NTSTATUS ret;
975 UNICODE_STRING upcase;
977 if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
979 ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
980 RtlFreeUnicodeString( &upcase );
982 return ret;
986 /**************************************************************************
987 * RtlUpcaseUnicodeStringToCountedOemString (NTDLL.@)
989 * NOTES
990 * Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
992 NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
993 const UNICODE_STRING *uni,
994 BOOLEAN doalloc )
996 NTSTATUS ret;
997 UNICODE_STRING upcase;
999 if (!(ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE )))
1001 DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
1002 oem->Length = len;
1003 if (doalloc)
1005 oem->MaximumLength = len;
1006 if (!(oem->Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len )))
1008 ret = STATUS_NO_MEMORY;
1009 goto done;
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 );
1019 done:
1020 RtlFreeUnicodeString( &upcase );
1022 return ret;
1026 /**************************************************************************
1027 * RtlUpcaseUnicodeToMultiByteN (NTDLL.@)
1029 NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1030 LPCWSTR src, DWORD srclen )
1032 NTSTATUS ret;
1033 LPWSTR upcase;
1034 DWORD i;
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 );
1040 return ret;
1044 /**************************************************************************
1045 * RtlUpcaseUnicodeToOemN (NTDLL.@)
1047 NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
1048 LPCWSTR src, DWORD srclen )
1050 NTSTATUS ret;
1051 LPWSTR upcase;
1052 DWORD i;
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 );
1058 return ret;
1063 STRING SIZE
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'.
1074 * PARAMS
1075 * str [I] String to calculate the size of
1077 * RETURNS
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'.
1094 * PARAMS
1095 * str [I] String to calculate the size of
1097 * RETURNS
1098 * The calculated size.
1100 DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1102 DWORD ret;
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'.
1114 * PARAMS
1115 * size [O] Destination for size
1116 * str [I] String to calculate the size of
1117 * len [I] Length of str
1119 * RETURNS
1120 * STATUS_SUCCESS.
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'.
1135 * PARAMS
1136 * size [O] Destination for size
1137 * str [I] String to calculate the size of
1138 * len [I] Length of str
1140 * RETURNS
1141 * STATUS_SUCCESS.
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'.
1157 * PARAMS
1158 * str [I] String to calculate the size of
1160 * RETURNS
1161 * The calculated size.
1163 DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
1165 DWORD ret;
1166 RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
1167 return ret + 1;
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'.
1178 * PARAMS
1179 * str [I] String to calculate the size of
1181 * RETURNS
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
1195 * string
1197 * RETURNS
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.
1202 * NOTES
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 */
1210 if (src != NULL) {
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;
1217 } /* if */
1218 return STATUS_SUCCESS;
1222 /**************************************************************************
1223 * RtlAppendStringToString (NTDLL.@)
1225 * Concatenates two buffered character strings
1227 * RETURNS
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.
1232 * NOTES
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;
1246 } /* if */
1247 return STATUS_SUCCESS;
1251 /**************************************************************************
1252 * RtlAppendUnicodeToString (NTDLL.@)
1254 * Concatenates an buffered unicode string and a '\0' terminated unicode
1255 * string
1257 * RETURNS
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.
1262 * NOTES
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.
1267 * DIFFERENCES
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 */
1275 if (src != NULL) {
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;
1285 } /* if */
1286 } /* if */
1287 return STATUS_SUCCESS;
1291 /**************************************************************************
1292 * RtlAppendUnicodeStringToString (NTDLL.@)
1294 * Concatenates two buffered unicode strings
1296 * RETURNS
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.
1301 * NOTES
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.
1306 * DIFFERENCES
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;
1323 } /* if */
1324 } /* if */
1325 return STATUS_SUCCESS;
1329 /**************************************************************************
1330 * RtlFindCharInUnicodeString (NTDLL.@)
1332 * Searches for one of several unicode characters in an unicode string.
1334 * RETURNS
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 */
1344 int main_idx;
1345 unsigned int search_idx;
1347 switch (flags) {
1348 case 0:
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;
1357 *pos = 0;
1358 return STATUS_NOT_FOUND;
1359 case 1:
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;
1368 *pos = 0;
1369 return STATUS_NOT_FOUND;
1370 case 2:
1371 for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
1372 search_idx = 0;
1373 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1374 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1375 search_idx++;
1377 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1378 *pos = (main_idx + 1) * sizeof(WCHAR);
1379 return STATUS_SUCCESS;
1382 *pos = 0;
1383 return STATUS_NOT_FOUND;
1384 case 3:
1385 for (main_idx = main_str->Length / sizeof(WCHAR) - 1; main_idx >= 0; main_idx--) {
1386 search_idx = 0;
1387 while (search_idx < search_chars->Length / sizeof(WCHAR) &&
1388 main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
1389 search_idx++;
1391 if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
1392 *pos = main_idx * sizeof(WCHAR);
1393 return STATUS_SUCCESS;
1396 *pos = 0;
1397 return STATUS_NOT_FOUND;
1398 } /* switch */
1399 return STATUS_NOT_FOUND;
1404 MISC
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.
1419 * PARAMS
1420 * buf [I] Text buffer to test
1421 * len [I] Length of buf
1422 * pf [O] Destination for test results
1424 * RETURNS
1425 * The length of the string if all tests were passed, 0 otherwise.
1427 * FIXME
1428 * Should implement more tests.
1430 DWORD WINAPI RtlIsTextUnicode(
1431 LPVOID buf,
1432 DWORD len,
1433 DWORD *pf)
1435 LPWSTR s = buf;
1436 DWORD flags = -1, out_flags = 0;
1438 if (!len)
1439 goto out;
1440 if (pf)
1441 flags = *pf;
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)
1463 DWORD i, stats = 0;
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)
1476 DWORD i;
1477 for (i = 0; i < len / sizeof(WCHAR); i++)
1479 if (!s[i])
1481 out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
1482 break;
1488 * Check whether the string passed all of the tests.
1490 flags &= ITU_IMPLEMENTED_TESTS;
1491 if ((out_flags & flags) != flags)
1492 len = 0;
1493 out:
1494 if (pf)
1495 *pf = out_flags;
1496 return len;
1500 /**************************************************************************
1501 * RtlCharToInteger (NTDLL.@)
1503 * Converts a character string into its integer equivalent.
1505 * RETURNS
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.
1510 * NOTES
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.
1518 * DIFFERENCES
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 */
1526 CHAR chCurrent;
1527 int digit;
1528 ULONG RunningTotal = 0;
1529 char bMinus = 0;
1531 while (*str != '\0' && *str <= ' ') {
1532 str++;
1533 } /* while */
1535 if (*str == '+') {
1536 str++;
1537 } else if (*str == '-') {
1538 bMinus = 1;
1539 str++;
1540 } /* if */
1542 if (base == 0) {
1543 base = 10;
1544 if (str[0] == '0') {
1545 if (str[1] == 'b') {
1546 str += 2;
1547 base = 2;
1548 } else if (str[1] == 'o') {
1549 str += 2;
1550 base = 8;
1551 } else if (str[1] == 'x') {
1552 str += 2;
1553 base = 16;
1554 } /* if */
1555 } /* if */
1556 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1557 return STATUS_INVALID_PARAMETER;
1558 } /* if */
1560 if (value == NULL) {
1561 return STATUS_ACCESS_VIOLATION;
1562 } /* if */
1564 while (*str != '\0') {
1565 chCurrent = *str;
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;
1572 } else {
1573 digit = -1;
1574 } /* if */
1575 if (digit < 0 || digit >= base) {
1576 *value = bMinus ? -RunningTotal : RunningTotal;
1577 return STATUS_SUCCESS;
1578 } /* if */
1580 RunningTotal = RunningTotal * base + digit;
1581 str++;
1582 } /* while */
1584 *value = bMinus ? -RunningTotal : RunningTotal;
1585 return STATUS_SUCCESS;
1589 /**************************************************************************
1590 * RtlIntegerToChar (NTDLL.@)
1592 * Converts an unsigned integer to a character string.
1594 * RETURNS
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.
1600 * NOTES
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 */
1612 CHAR buffer[33];
1613 PCHAR pos;
1614 CHAR digit;
1615 ULONG len;
1617 if (base == 0) {
1618 base = 10;
1619 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1620 return STATUS_INVALID_PARAMETER;
1621 } /* if */
1623 pos = &buffer[32];
1624 *pos = '\0';
1626 do {
1627 pos--;
1628 digit = value % base;
1629 value = value / base;
1630 if (digit < 10) {
1631 *pos = '0' + digit;
1632 } else {
1633 *pos = 'A' + digit - 10;
1634 } /* if */
1635 } while (value != 0L);
1637 len = &buffer[32] - pos;
1638 if (len > length) {
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);
1644 } else {
1645 memcpy(str, pos, len + 1);
1646 } /* if */
1647 return STATUS_SUCCESS;
1651 /**************************************************************************
1652 * RtlUnicodeStringToInteger (NTDLL.@)
1654 * Converts an unicode string into its integer equivalent.
1656 * RETURNS
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.
1661 * NOTES
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.
1669 * DIFFERENCES
1670 * This function does not read garbage on string length 0 as the native
1671 * version does.
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);
1680 WCHAR wchCurrent;
1681 int digit;
1682 ULONG RunningTotal = 0;
1683 char bMinus = 0;
1685 while (CharsRemaining >= 1 && *lpwstr <= ' ') {
1686 lpwstr++;
1687 CharsRemaining--;
1688 } /* while */
1690 if (CharsRemaining >= 1) {
1691 if (*lpwstr == '+') {
1692 lpwstr++;
1693 CharsRemaining--;
1694 } else if (*lpwstr == '-') {
1695 bMinus = 1;
1696 lpwstr++;
1697 CharsRemaining--;
1698 } /* if */
1699 } /* if */
1701 if (base == 0) {
1702 base = 10;
1703 if (CharsRemaining >= 2 && lpwstr[0] == '0') {
1704 if (lpwstr[1] == 'b') {
1705 lpwstr += 2;
1706 CharsRemaining -= 2;
1707 base = 2;
1708 } else if (lpwstr[1] == 'o') {
1709 lpwstr += 2;
1710 CharsRemaining -= 2;
1711 base = 8;
1712 } else if (lpwstr[1] == 'x') {
1713 lpwstr += 2;
1714 CharsRemaining -= 2;
1715 base = 16;
1716 } /* if */
1717 } /* if */
1718 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1719 return STATUS_INVALID_PARAMETER;
1720 } /* if */
1722 if (value == NULL) {
1723 return STATUS_ACCESS_VIOLATION;
1724 } /* if */
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;
1734 } else {
1735 digit = -1;
1736 } /* if */
1737 if (digit < 0 || digit >= base) {
1738 *value = bMinus ? -RunningTotal : RunningTotal;
1739 return STATUS_SUCCESS;
1740 } /* if */
1742 RunningTotal = RunningTotal * base + digit;
1743 lpwstr++;
1744 CharsRemaining--;
1745 } /* while */
1747 *value = bMinus ? -RunningTotal : RunningTotal;
1748 return STATUS_SUCCESS;
1752 /**************************************************************************
1753 * RtlIntegerToUnicodeString (NTDLL.@)
1755 * Converts an unsigned integer to a '\0' terminated unicode string.
1757 * RETURNS
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).
1765 * NOTES
1766 * Instead of base 0 it uses 10 as base.
1767 * If str is NULL it crashes, as the native function does.
1769 * DIFFERENCES
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 */
1779 WCHAR buffer[33];
1780 PWCHAR pos;
1781 WCHAR digit;
1783 if (base == 0) {
1784 base = 10;
1785 } else if (base != 2 && base != 8 && base != 10 && base != 16) {
1786 return STATUS_INVALID_PARAMETER;
1787 } /* if */
1789 pos = &buffer[32];
1790 *pos = '\0';
1792 do {
1793 pos--;
1794 digit = value % base;
1795 value = value / base;
1796 if (digit < 10) {
1797 *pos = '0' + digit;
1798 } else {
1799 *pos = 'A' + digit - 10;
1800 } /* if */
1801 } while (value != 0L);
1803 str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
1804 if (str->Length >= str->MaximumLength) {
1805 return STATUS_BUFFER_OVERFLOW;
1806 } else {
1807 memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
1808 } /* if */
1809 return STATUS_SUCCESS;