Implement A->W call for GetNamedSecurityInfo.
[wine/multimedia.git] / dlls / ntdll / wcstring.c
blob1d997cbbb541273c2cb16ead89b30c13dfcebf2e
1 /*
2 * NTDLL wide-char functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 * Copyright 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 <ctype.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #include "ntstatus.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winternl.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
43 /*********************************************************************
44 * _wcsicmp (NTDLL.@)
46 INT __cdecl NTDLL__wcsicmp( LPCWSTR str1, LPCWSTR str2 )
48 return strcmpiW( str1, str2 );
52 /*********************************************************************
53 * _wcslwr (NTDLL.@)
55 LPWSTR __cdecl NTDLL__wcslwr( LPWSTR str )
57 return strlwrW( str );
61 /*********************************************************************
62 * _wcsnicmp (NTDLL.@)
64 INT __cdecl NTDLL__wcsnicmp( LPCWSTR str1, LPCWSTR str2, INT n )
66 return strncmpiW( str1, str2, n );
70 /*********************************************************************
71 * _wcsupr (NTDLL.@)
73 LPWSTR __cdecl NTDLL__wcsupr( LPWSTR str )
75 return struprW( str );
79 /*********************************************************************
80 * towlower (NTDLL.@)
82 WCHAR __cdecl NTDLL_towlower( WCHAR ch )
84 return tolowerW(ch);
88 /*********************************************************************
89 * towupper (NTDLL.@)
91 WCHAR __cdecl NTDLL_towupper( WCHAR ch )
93 return toupperW(ch);
97 /***********************************************************************
98 * wcscat (NTDLL.@)
100 LPWSTR __cdecl NTDLL_wcscat( LPWSTR dst, LPCWSTR src )
102 return strcatW( dst, src );
106 /*********************************************************************
107 * wcschr (NTDLL.@)
109 LPWSTR __cdecl NTDLL_wcschr( LPCWSTR str, WCHAR ch )
111 return strchrW( str, ch );
115 /*********************************************************************
116 * wcscmp (NTDLL.@)
118 INT __cdecl NTDLL_wcscmp( LPCWSTR str1, LPCWSTR str2 )
120 return strcmpW( str1, str2 );
124 /***********************************************************************
125 * wcscpy (NTDLL.@)
127 LPWSTR __cdecl NTDLL_wcscpy( LPWSTR dst, LPCWSTR src )
129 return strcpyW( dst, src );
133 /*********************************************************************
134 * wcscspn (NTDLL.@)
136 INT __cdecl NTDLL_wcscspn( LPCWSTR str, LPCWSTR reject )
138 LPCWSTR start = str;
139 while (*str)
141 LPCWSTR p = reject;
142 while (*p && (*p != *str)) p++;
143 if (*p) break;
144 str++;
146 return str - start;
150 /***********************************************************************
151 * wcslen (NTDLL.@)
153 INT __cdecl NTDLL_wcslen( LPCWSTR str )
155 return strlenW( str );
159 /*********************************************************************
160 * wcsncat (NTDLL.@)
162 LPWSTR __cdecl NTDLL_wcsncat( LPWSTR s1, LPCWSTR s2, INT n )
164 LPWSTR ret = s1;
165 while (*s1) s1++;
166 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
167 *s1 = 0;
168 return ret;
172 /*********************************************************************
173 * wcsncmp (NTDLL.@)
175 INT __cdecl NTDLL_wcsncmp( LPCWSTR str1, LPCWSTR str2, INT n )
177 return strncmpW( str1, str2, n );
181 /*********************************************************************
182 * wcsncpy (NTDLL.@)
184 LPWSTR __cdecl NTDLL_wcsncpy( LPWSTR s1, LPCWSTR s2, INT n )
186 return strncpyW( s1, s2, n );
190 /*********************************************************************
191 * wcspbrk (NTDLL.@)
193 LPWSTR __cdecl NTDLL_wcspbrk( LPCWSTR str, LPCWSTR accept )
195 LPCWSTR p;
196 while (*str)
198 for (p = accept; *p; p++) if (*p == *str) return (LPWSTR)str;
199 str++;
201 return NULL;
205 /*********************************************************************
206 * wcsrchr (NTDLL.@)
208 LPWSTR __cdecl NTDLL_wcsrchr( LPWSTR str, WCHAR ch )
210 LPWSTR last = NULL;
211 while (*str)
213 if (*str == ch) last = str;
214 str++;
216 return last;
220 /*********************************************************************
221 * wcsspn (NTDLL.@)
223 INT __cdecl NTDLL_wcsspn( LPCWSTR str, LPCWSTR accept )
225 LPCWSTR start = str;
226 while (*str)
228 LPCWSTR p = accept;
229 while (*p && (*p != *str)) p++;
230 if (!*p) break;
231 str++;
233 return str - start;
237 /*********************************************************************
238 * wcsstr (NTDLL.@)
240 LPWSTR __cdecl NTDLL_wcsstr( LPCWSTR str, LPCWSTR sub )
242 return strstrW( str, sub );
246 /*********************************************************************
247 * wcstok (NTDLL.@)
249 LPWSTR __cdecl NTDLL_wcstok( LPWSTR str, LPCWSTR delim )
251 static LPWSTR next = NULL;
252 LPWSTR ret;
254 if (!str)
255 if (!(str = next)) return NULL;
257 while (*str && NTDLL_wcschr( delim, *str )) str++;
258 if (!*str) return NULL;
259 ret = str++;
260 while (*str && !NTDLL_wcschr( delim, *str )) str++;
261 if (*str) *str++ = 0;
262 next = str;
263 return ret;
267 /*********************************************************************
268 * wcstombs (NTDLL.@)
270 INT __cdecl NTDLL_wcstombs( LPSTR dst, LPCWSTR src, INT n )
272 DWORD len;
274 if (!dst)
276 RtlUnicodeToMultiByteSize( &len, src, strlenW(src)*sizeof(WCHAR) );
277 return len;
279 else
281 if (n <= 0) return 0;
282 RtlUnicodeToMultiByteN( dst, n, &len, src, strlenW(src)*sizeof(WCHAR) );
283 if (len < n) dst[len] = 0;
285 return len;
289 /*********************************************************************
290 * mbstowcs (NTDLL.@)
292 INT __cdecl NTDLL_mbstowcs( LPWSTR dst, LPCSTR src, INT n )
294 DWORD len;
296 if (!dst)
298 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
300 else
302 if (n <= 0) return 0;
303 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
304 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
306 return len / sizeof(WCHAR);
310 /*********************************************************************
311 * wcstol (NTDLL.@)
313 long __cdecl NTDLL_wcstol(LPCWSTR s,LPWSTR *end,INT base)
315 return strtolW( s, end, base );
319 /*********************************************************************
320 * wcstoul (NTDLL.@)
322 unsigned long __cdecl NTDLL_wcstoul(LPCWSTR s,LPWSTR *end,INT base)
324 return strtoulW( s, end, base );
328 /*********************************************************************
329 * iswctype (NTDLL.@)
331 INT __cdecl NTDLL_iswctype( WCHAR wc, WCHAR wct )
333 return (get_char_typeW(wc) & 0xfff) & wct;
337 /*********************************************************************
338 * iswalpha (NTDLL.@)
340 * Checks if an unicode char wc is a letter
342 * RETURNS
343 * TRUE: The unicode char wc is a letter.
344 * FALSE: Otherwise
346 INT __cdecl NTDLL_iswalpha( WCHAR wc )
348 return isalphaW(wc);
352 /*********************************************************************
353 * iswdigit (NTDLL.@)
355 * Checks if an unicode char wc is a digit
357 * RETURNS
358 * TRUE: The unicode char wc is a digit.
359 * FALSE: Otherwise
361 INT __cdecl NTDLL_iswdigit( WCHAR wc )
363 return isdigitW(wc);
367 /*********************************************************************
368 * iswlower (NTDLL.@)
370 * Checks if an unicode char wc is a lower case letter
372 * RETURNS
373 * TRUE: The unicode char wc is a lower case letter.
374 * FALSE: Otherwise
376 INT __cdecl NTDLL_iswlower( WCHAR wc )
378 return islowerW(wc);
382 /*********************************************************************
383 * iswspace (NTDLL.@)
385 * Checks if an unicode char wc is a white space character
387 * RETURNS
388 * TRUE: The unicode char wc is a white space character.
389 * FALSE: Otherwise
391 INT __cdecl NTDLL_iswspace( WCHAR wc )
393 return isspaceW(wc);
397 /*********************************************************************
398 * iswxdigit (NTDLL.@)
400 * Checks if an unicode char wc is an extended digit
402 * RETURNS
403 * TRUE: The unicode char wc is an extended digit.
404 * FALSE: Otherwise
406 INT __cdecl NTDLL_iswxdigit( WCHAR wc )
408 return isxdigitW(wc);
412 /*********************************************************************
413 * _ultow (NTDLL.@)
415 * Converts an unsigned long integer to an unicode string.
417 * RETURNS
418 * Always returns str.
420 * NOTES
421 * Converts value to a '\0' terminated wstring which is copied to str.
422 * The maximum length of the copied str is 33 bytes.
423 * Does not check if radix is in the range of 2 to 36.
424 * If str is NULL it just returns NULL.
426 LPWSTR __cdecl _ultow(
427 unsigned long value, /* [I] Value to be converted */
428 LPWSTR str, /* [O] Destination for the converted value */
429 INT radix) /* [I] Number base for conversion */
431 WCHAR buffer[33];
432 PWCHAR pos;
433 WCHAR digit;
435 pos = &buffer[32];
436 *pos = '\0';
438 do {
439 digit = value % radix;
440 value = value / radix;
441 if (digit < 10) {
442 *--pos = '0' + digit;
443 } else {
444 *--pos = 'a' + digit - 10;
445 } /* if */
446 } while (value != 0L);
448 if (str != NULL) {
449 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
450 } /* if */
451 return str;
455 /*********************************************************************
456 * _ltow (NTDLL.@)
458 * Converts a long integer to an unicode string.
460 * RETURNS
461 * Always returns str.
463 * NOTES
464 * Converts value to a '\0' terminated wstring which is copied to str.
465 * The maximum length of the copied str is 33 bytes. If radix
466 * is 10 and value is negative, the value is converted with sign.
467 * Does not check if radix is in the range of 2 to 36.
468 * If str is NULL it just returns NULL.
470 LPWSTR __cdecl _ltow(
471 long value, /* [I] Value to be converted */
472 LPWSTR str, /* [O] Destination for the converted value */
473 INT radix) /* [I] Number base for conversion */
475 unsigned long val;
476 int negative;
477 WCHAR buffer[33];
478 PWCHAR pos;
479 WCHAR digit;
481 if (value < 0 && radix == 10) {
482 negative = 1;
483 val = -value;
484 } else {
485 negative = 0;
486 val = value;
487 } /* if */
489 pos = &buffer[32];
490 *pos = '\0';
492 do {
493 digit = val % radix;
494 val = val / radix;
495 if (digit < 10) {
496 *--pos = '0' + digit;
497 } else {
498 *--pos = 'a' + digit - 10;
499 } /* if */
500 } while (val != 0L);
502 if (negative) {
503 *--pos = '-';
504 } /* if */
506 if (str != NULL) {
507 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
508 } /* if */
509 return str;
513 /*********************************************************************
514 * _itow (NTDLL.@)
516 * Converts an integer to an unicode string.
518 * RETURNS
519 * Always returns str.
521 * NOTES
522 * Converts value to a '\0' terminated wstring which is copied to str.
523 * The maximum length of the copied str is 33 bytes. If radix
524 * is 10 and value is negative, the value is converted with sign.
525 * Does not check if radix is in the range of 2 to 36.
526 * If str is NULL it just returns NULL.
528 * DIFFERENCES
529 * - The native function crashes when the string is longer than 19 chars.
530 * This function does not have this bug.
532 LPWSTR __cdecl _itow(
533 int value, /* [I] Value to be converted */
534 LPWSTR str, /* [O] Destination for the converted value */
535 INT radix) /* [I] Number base for conversion */
537 return _ltow(value, str, radix);
541 /*********************************************************************
542 * _ui64tow (NTDLL.@)
544 * Converts a large unsigned integer to an unicode string.
546 * RETURNS
547 * Always returns str.
549 * NOTES
550 * Converts value to a '\0' terminated wstring which is copied to str.
551 * The maximum length of the copied str is 33 bytes.
552 * Does not check if radix is in the range of 2 to 36.
553 * If str is NULL it just returns NULL.
555 * DIFFERENCES
556 * - This function does not exist in the native DLL (but in msvcrt).
557 * But since the maintenance of all these functions is better done
558 * in one place we implement it here.
560 LPWSTR __cdecl _ui64tow(
561 ULONGLONG value, /* [I] Value to be converted */
562 LPWSTR str, /* [O] Destination for the converted value */
563 INT radix) /* [I] Number base for conversion */
565 WCHAR buffer[65];
566 PWCHAR pos;
567 WCHAR digit;
569 pos = &buffer[64];
570 *pos = '\0';
572 do {
573 digit = value % radix;
574 value = value / radix;
575 if (digit < 10) {
576 *--pos = '0' + digit;
577 } else {
578 *--pos = 'a' + digit - 10;
579 } /* if */
580 } while (value != 0L);
582 if (str != NULL) {
583 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
584 } /* if */
585 return str;
589 /*********************************************************************
590 * _i64tow (NTDLL.@)
592 * Converts a large integer to an unicode string.
594 * RETURNS
595 * Always returns str.
597 * NOTES
598 * Converts value to a '\0' terminated wstring which is copied to str.
599 * The maximum length of the copied str is 33 bytes. If radix
600 * is 10 and value is negative, the value is converted with sign.
601 * Does not check if radix is in the range of 2 to 36.
602 * If str is NULL it just returns NULL.
604 * DIFFERENCES
605 * - The native DLL converts negative values (for base 10) wrong:
606 * -1 is converted to -18446744073709551615
607 * -2 is converted to -18446744073709551614
608 * -9223372036854775807 is converted to -9223372036854775809
609 * -9223372036854775808 is converted to -9223372036854775808
610 * The native msvcrt _i64tow function and our ntdll function do
611 * not have this bug.
613 LPWSTR __cdecl _i64tow(
614 LONGLONG value, /* [I] Value to be converted */
615 LPWSTR str, /* [O] Destination for the converted value */
616 INT radix) /* [I] Number base for conversion */
618 ULONGLONG val;
619 int negative;
620 WCHAR buffer[65];
621 PWCHAR pos;
622 WCHAR digit;
624 if (value < 0 && radix == 10) {
625 negative = 1;
626 val = -value;
627 } else {
628 negative = 0;
629 val = value;
630 } /* if */
632 pos = &buffer[64];
633 *pos = '\0';
635 do {
636 digit = val % radix;
637 val = val / radix;
638 if (digit < 10) {
639 *--pos = '0' + digit;
640 } else {
641 *--pos = 'a' + digit - 10;
642 } /* if */
643 } while (val != 0L);
645 if (negative) {
646 *--pos = '-';
647 } /* if */
649 if (str != NULL) {
650 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
651 } /* if */
652 return str;
656 /*********************************************************************
657 * _wtol (NTDLL.@)
659 * Converts an unicode string to a long integer.
661 * PARAMS
662 * str [I] Wstring to be converted
664 * RETURNS
665 * On success it returns the integer value otherwise it returns 0.
667 * NOTES
668 * Accepts: {whitespace} [+|-] {digits}
669 * No check is made for value overflow, only the lower 32 bits are assigned.
670 * If str is NULL it crashes, as the native function does.
672 LONG __cdecl _wtol( LPCWSTR str )
674 ULONG RunningTotal = 0;
675 char bMinus = 0;
677 while (isspaceW(*str)) {
678 str++;
679 } /* while */
681 if (*str == '+') {
682 str++;
683 } else if (*str == '-') {
684 bMinus = 1;
685 str++;
686 } /* if */
688 while (*str >= '0' && *str <= '9') {
689 RunningTotal = RunningTotal * 10 + *str - '0';
690 str++;
691 } /* while */
693 return bMinus ? -RunningTotal : RunningTotal;
697 /*********************************************************************
698 * _wtoi (NTDLL.@)
700 * Converts an unicode string to an integer.
702 * PARAMS
703 * str [I] Wstring to be converted
705 * RETURNS
706 * On success it returns the integer value otherwise it returns 0.
708 * NOTES
709 * Accepts: {whitespace} [+|-] {digits}
710 * No check is made for value overflow, only the lower 32 bits are assigned.
711 * If str is NULL it crashes, as the native function does.
713 int __cdecl _wtoi( LPCWSTR str )
715 return _wtol(str);
719 /*********************************************************************
720 * _wtoi64 (NTDLL.@)
722 * Converts an unicode string to a large integer.
724 * PARAMS
725 * str [I] Wstring to be converted
727 * RETURNS
728 * On success it returns the integer value otherwise it returns 0.
730 * NOTES
731 * Accepts: {whitespace} [+|-] {digits}
732 * No check is made for value overflow, only the lower 64 bits are assigned.
733 * If str is NULL it crashes, as the native function does.
735 LONGLONG __cdecl _wtoi64( LPCWSTR str )
737 ULONGLONG RunningTotal = 0;
738 char bMinus = 0;
740 while (isspaceW(*str)) {
741 str++;
742 } /* while */
744 if (*str == '+') {
745 str++;
746 } else if (*str == '-') {
747 bMinus = 1;
748 str++;
749 } /* if */
751 while (*str >= '0' && *str <= '9') {
752 RunningTotal = RunningTotal * 10 + *str - '0';
753 str++;
754 } /* while */
756 return bMinus ? -RunningTotal : RunningTotal;
760 /***********************************************************************
761 * _snwprintf (NTDLL.@)
763 int __cdecl _snwprintf(WCHAR *str, unsigned int len, const WCHAR *format, ...)
765 int retval;
766 va_list valist;
767 va_start(valist, format);
768 retval = vsnprintfW(str, len, format, valist);
769 va_end(valist);
770 return retval;
774 /***********************************************************************
775 * swprintf (NTDLL.@)
777 int __cdecl NTDLL_swprintf(WCHAR *str, const WCHAR *format, ...)
779 int retval;
780 va_list valist;
781 va_start(valist, format);
782 retval = vsnprintfW(str, INT_MAX, format, valist);
783 va_end(valist);
784 return retval;