win32u: Use the dummy surface for empty layered window surfaces.
[wine.git] / dlls / ntdll / wcstring.c
blobaa43c3de16b72d22053c20598d0b76d0301b101c
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <ctype.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "winternl.h"
35 #include "ntdll_misc.h"
37 static const unsigned short wctypes[256] =
39 /* 00 */
40 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
41 0x0020, 0x0068, 0x0028, 0x0028, 0x0028, 0x0028, 0x0020, 0x0020,
42 /* 10 */
43 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
44 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
45 /* 20 */
46 0x0048, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
47 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
48 /* 30 */
49 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084, 0x0084,
50 0x0084, 0x0084, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
51 /* 40 */
52 0x0010, 0x0181, 0x0181, 0x0181, 0x0181, 0x0181, 0x0181, 0x0101,
53 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
54 /* 50 */
55 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
56 0x0101, 0x0101, 0x0101, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
57 /* 60 */
58 0x0010, 0x0182, 0x0182, 0x0182, 0x0182, 0x0182, 0x0182, 0x0102,
59 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102,
60 /* 70 */
61 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102,
62 0x0102, 0x0102, 0x0102, 0x0010, 0x0010, 0x0010, 0x0010, 0x0020,
63 /* 80 */
64 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
65 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
66 /* 90 */
67 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
68 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
69 /* a0 */
70 0x0048, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
71 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
72 /* b0 */
73 0x0010, 0x0010, 0x0014, 0x0014, 0x0010, 0x0010, 0x0010, 0x0010,
74 0x0010, 0x0014, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010, 0x0010,
75 /* c0 */
76 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
77 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101,
78 /* d0 */
79 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0010,
80 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0101, 0x0102,
81 /* e0 */
82 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102,
83 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102,
84 /* f0 */
85 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0010,
86 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102, 0x0102
90 /*********************************************************************
91 * _wcsicmp (NTDLL.@)
93 int __cdecl _wcsicmp( LPCWSTR str1, LPCWSTR str2 )
95 for (;;)
97 WCHAR ch1 = (*str1 >= 'A' && *str1 <= 'Z') ? *str1 + 32 : *str1;
98 WCHAR ch2 = (*str2 >= 'A' && *str2 <= 'Z') ? *str2 + 32 : *str2;
99 if (ch1 != ch2 || !*str1) return ch1 - ch2;
100 str1++;
101 str2++;
106 /*********************************************************************
107 * _wcslwr (NTDLL.@)
109 LPWSTR __cdecl _wcslwr( LPWSTR str )
111 WCHAR *ret = str;
112 for ( ; *str; str++) if (*str >= 'A' && *str <= 'Z') *str += 'a' - 'A';
113 return ret;
117 /*********************************************************************
118 * _wcslwr_s (NTDLL.@)
120 errno_t __cdecl _wcslwr_s( wchar_t *str, size_t len )
122 if (!str) return EINVAL;
124 if (wcsnlen( str, len ) == len)
126 *str = 0;
127 return EINVAL;
130 _wcslwr( str );
131 return 0;
135 /*********************************************************************
136 * _wcsnicmp (NTDLL.@)
138 int __cdecl _wcsnicmp( LPCWSTR str1, LPCWSTR str2, size_t n )
140 int ret = 0;
141 for ( ; n > 0; n--, str1++, str2++)
143 WCHAR ch1 = (*str1 >= 'A' && *str1 <= 'Z') ? *str1 + 32 : *str1;
144 WCHAR ch2 = (*str2 >= 'A' && *str2 <= 'Z') ? *str2 + 32 : *str2;
145 if ((ret = ch1 - ch2) || !*str1) break;
147 return ret;
151 /*********************************************************************
152 * _wcsupr (NTDLL.@)
154 LPWSTR __cdecl _wcsupr( LPWSTR str )
156 WCHAR *ret = str;
158 for ( ; *str; str++) if (*str >= 'a' && *str <= 'z') *str += 'A' - 'a';
159 return ret;
163 /*********************************************************************
164 * _wcsupr_s (NTDLL.@)
166 errno_t __cdecl _wcsupr_s( wchar_t *str, size_t len )
168 if (!str) return EINVAL;
170 if (wcsnlen( str, len ) == len)
172 *str = 0;
173 return EINVAL;
176 _wcsupr( str );
177 return 0;
181 /***********************************************************************
182 * wcscpy (NTDLL.@)
184 LPWSTR __cdecl wcscpy( LPWSTR dst, LPCWSTR src )
186 WCHAR *p = dst;
187 while ((*p++ = *src++));
188 return dst;
192 /*********************************************************************
193 * wcscpy_s (NTDLL.@)
195 errno_t __cdecl wcscpy_s( wchar_t *dst, size_t len, const wchar_t *src )
197 size_t i;
199 if (!dst || !len) return EINVAL;
200 if (!src)
202 *dst = 0;
203 return EINVAL;
206 for (i = 0; i < len; i++) if (!(dst[i] = src[i])) return 0;
207 *dst = 0;
208 return ERANGE;
212 /***********************************************************************
213 * wcslen (NTDLL.@)
215 size_t __cdecl wcslen( LPCWSTR str )
217 const WCHAR *s = str;
218 while (*s) s++;
219 return s - str;
223 /***********************************************************************
224 * wcscat (NTDLL.@)
226 LPWSTR __cdecl wcscat( LPWSTR dst, LPCWSTR src )
228 wcscpy( dst + wcslen(dst), src );
229 return dst;
233 /*********************************************************************
234 * wcscat_s (NTDLL.@)
236 errno_t __cdecl wcscat_s( wchar_t *dst, size_t len, const wchar_t *src )
238 size_t i, j;
240 if (!dst || !len) return EINVAL;
241 if (!src)
243 *dst = 0;
244 return EINVAL;
246 for (i = 0; i < len; i++) if (!dst[i]) break;
247 for (j = 0; (j + i) < len; j++) if (!(dst[j + i] = src[j])) return 0;
248 *dst = 0;
249 return ERANGE;
253 /*********************************************************************
254 * wcschr (NTDLL.@)
256 LPWSTR __cdecl wcschr( LPCWSTR str, WCHAR ch )
258 do { if (*str == ch) return (WCHAR *)(ULONG_PTR)str; } while (*str++);
259 return NULL;
263 /*********************************************************************
264 * wcscmp (NTDLL.@)
266 int __cdecl wcscmp( LPCWSTR str1, LPCWSTR str2 )
268 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
269 return *str1 - *str2;
273 /*********************************************************************
274 * wcscspn (NTDLL.@)
276 size_t __cdecl wcscspn( LPCWSTR str, LPCWSTR reject )
278 const WCHAR *ptr;
279 for (ptr = str; *ptr; ptr++) if (wcschr( reject, *ptr )) break;
280 return ptr - str;
284 /*********************************************************************
285 * wcsncat (NTDLL.@)
287 LPWSTR __cdecl wcsncat( LPWSTR s1, LPCWSTR s2, size_t n )
289 LPWSTR ret = s1;
290 while (*s1) s1++;
291 while (n-- > 0) if (!(*s1++ = *s2++)) return ret;
292 *s1 = 0;
293 return ret;
297 /*********************************************************************
298 * wcsncat_s (NTDLL.@)
300 errno_t __cdecl wcsncat_s( wchar_t *dst, size_t len, const wchar_t *src, size_t count )
302 size_t i, j;
304 if (!dst || !len) return EINVAL;
305 if (!count) return 0;
306 if (!src)
308 *dst = 0;
309 return EINVAL;
312 for (i = 0; i < len; i++) if (!dst[i]) break;
314 if (i == len)
316 *dst = 0;
317 return EINVAL;
320 for (j = 0; (j + i) < len; j++)
322 if (count == _TRUNCATE && j + i == len - 1)
324 dst[j + i] = 0;
325 return STRUNCATE;
327 if (j == count || !(dst[j + i] = src[j]))
329 dst[j + i] = 0;
330 return 0;
333 *dst = 0;
334 return ERANGE;
338 /*********************************************************************
339 * wcsncmp (NTDLL.@)
341 int __cdecl wcsncmp( LPCWSTR str1, LPCWSTR str2, size_t n )
343 if (n <= 0) return 0;
344 while ((--n > 0) && *str1 && (*str1 == *str2)) { str1++; str2++; }
345 return *str1 - *str2;
349 /*********************************************************************
350 * wcsncpy (NTDLL.@)
352 #undef wcsncpy
353 LPWSTR __cdecl wcsncpy( LPWSTR s1, LPCWSTR s2, size_t n )
355 WCHAR *ret = s1;
356 for ( ; n; n--) if (!(*s1++ = *s2++)) break;
357 for ( ; n; n--) *s1++ = 0;
358 return ret;
362 /*********************************************************************
363 * wcsncpy_s (NTDLL.@)
365 errno_t __cdecl wcsncpy_s( wchar_t *dst, size_t len, const wchar_t *src, size_t count )
367 size_t i, end;
369 if (!count)
371 if (dst && len) *dst = 0;
372 return 0;
374 if (!dst || !len) return EINVAL;
375 if (!src)
377 *dst = 0;
378 return EINVAL;
381 if (count != _TRUNCATE && count < len)
382 end = count;
383 else
384 end = len - 1;
386 for (i = 0; i < end; i++)
387 if (!(dst[i] = src[i])) return 0;
389 if (count == _TRUNCATE)
391 dst[i] = 0;
392 return STRUNCATE;
394 if (end == count)
396 dst[i] = 0;
397 return 0;
399 dst[0] = 0;
400 return ERANGE;
404 /*********************************************************************
405 * wcsnlen (NTDLL.@)
407 size_t __cdecl wcsnlen( const WCHAR *str, size_t len )
409 const WCHAR *s;
410 for (s = str; len && *s; s++, len--) ;
411 return s - str;
415 /*********************************************************************
416 * wcspbrk (NTDLL.@)
418 LPWSTR __cdecl wcspbrk( LPCWSTR str, LPCWSTR accept )
420 for ( ; *str; str++) if (wcschr( accept, *str )) return (WCHAR *)(ULONG_PTR)str;
421 return NULL;
425 /*********************************************************************
426 * wcsrchr (NTDLL.@)
428 LPWSTR __cdecl wcsrchr( LPCWSTR str, WCHAR ch )
430 WCHAR *ret = NULL;
431 do { if (*str == ch) ret = (WCHAR *)(ULONG_PTR)str; } while (*str++);
432 return ret;
436 /*********************************************************************
437 * wcsspn (NTDLL.@)
439 size_t __cdecl wcsspn( LPCWSTR str, LPCWSTR accept )
441 const WCHAR *ptr;
442 for (ptr = str; *ptr; ptr++) if (!wcschr( accept, *ptr )) break;
443 return ptr - str;
447 /*********************************************************************
448 * wcsstr (NTDLL.@)
450 LPWSTR __cdecl wcsstr( LPCWSTR str, LPCWSTR sub )
452 while (*str)
454 const WCHAR *p1 = str, *p2 = sub;
455 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
456 if (!*p2) return (WCHAR *)str;
457 str++;
459 return NULL;
463 /*********************************************************************
464 * wcstok (NTDLL.@)
466 LPWSTR __cdecl wcstok( LPWSTR str, LPCWSTR delim )
468 static LPWSTR next = NULL;
469 LPWSTR ret;
471 if (!str)
472 if (!(str = next)) return NULL;
474 while (*str && wcschr( delim, *str )) str++;
475 if (!*str) return NULL;
476 ret = str++;
477 while (*str && !wcschr( delim, *str )) str++;
478 if (*str) *str++ = 0;
479 next = str;
480 return ret;
484 /*********************************************************************
485 * wcstok_s (NTDLL.@)
487 wchar_t * __cdecl wcstok_s( wchar_t *str, const wchar_t *delim, wchar_t **ctx )
489 wchar_t *next;
491 if (!delim || !ctx) return NULL;
492 if (!str)
494 str = *ctx;
495 if (!str) return NULL;
497 while (*str && wcschr( delim, *str )) str++;
498 if (!*str)
500 *ctx = str;
501 return NULL;
503 next = str + 1;
504 while (*next && !wcschr( delim, *next )) next++;
505 if (*next) *next++ = 0;
506 *ctx = next;
507 return str;
511 /*********************************************************************
512 * wcstombs (NTDLL.@)
514 size_t __cdecl wcstombs( char *dst, const WCHAR *src, size_t n )
516 DWORD len;
518 if (!dst)
520 RtlUnicodeToMultiByteSize( &len, src, wcslen(src) * sizeof(WCHAR) );
521 return len;
523 else
525 if (n <= 0) return 0;
526 RtlUnicodeToMultiByteN( dst, n, &len, src, wcslen(src) * sizeof(WCHAR) );
527 if (len < n) dst[len] = 0;
529 return len;
533 /*********************************************************************
534 * mbstowcs (NTDLL.@)
536 size_t __cdecl mbstowcs( WCHAR *dst, const char *src, size_t n )
538 DWORD len;
540 if (!dst)
542 RtlMultiByteToUnicodeSize( &len, src, strlen(src) );
544 else
546 if (n <= 0) return 0;
547 RtlMultiByteToUnicodeN( dst, n*sizeof(WCHAR), &len, src, strlen(src) );
548 if (len / sizeof(WCHAR) < n) dst[len / sizeof(WCHAR)] = 0;
550 return len / sizeof(WCHAR);
554 /*********************************************************************
555 * iswctype (NTDLL.@)
557 INT __cdecl iswctype( WCHAR wc, unsigned short type )
559 if (wc >= 256) return 0;
560 return wctypes[wc] & type;
564 /*********************************************************************
565 * iswalnum (NTDLL.@)
567 INT __cdecl iswalnum( WCHAR wc )
569 return iswctype( wc, C1_ALPHA | C1_UPPER | C1_LOWER | C1_DIGIT );
573 /*********************************************************************
574 * iswalpha (NTDLL.@)
576 INT __cdecl iswalpha( WCHAR wc )
578 return iswctype( wc, C1_ALPHA | C1_UPPER | C1_LOWER );
582 /*********************************************************************
583 * iswascii (NTDLL.@)
585 INT __cdecl iswascii( WCHAR wc )
587 return wc < 0x80;
591 /*********************************************************************
592 * iswdigit (NTDLL.@)
594 INT __cdecl iswdigit( WCHAR wc )
596 return iswctype( wc, C1_DIGIT );
600 /*********************************************************************
601 * iswgraph (NTDLL.@)
603 INT __cdecl iswgraph( WCHAR wc )
605 return iswctype( wc, C1_ALPHA | C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT );
609 /*********************************************************************
610 * iswlower (NTDLL.@)
612 INT __cdecl iswlower( WCHAR wc )
614 return iswctype( wc, C1_LOWER );
618 /*********************************************************************
619 * iswprint (NTDLL.@)
621 INT __cdecl iswprint( WCHAR wc )
623 return iswctype( wc, C1_ALPHA | C1_UPPER | C1_LOWER | C1_DIGIT | C1_PUNCT | C1_BLANK );
627 /*********************************************************************
628 * iswspace (NTDLL.@)
630 INT __cdecl iswspace( WCHAR wc )
632 return iswctype( wc, C1_SPACE );
636 /*********************************************************************
637 * iswxdigit (NTDLL.@)
639 INT __cdecl iswxdigit( WCHAR wc )
641 return iswctype( wc, C1_XDIGIT );
645 static int wctoint( WCHAR c )
647 /* NOTE: MAP_FOLDDIGITS supports too many things. */
648 /* Unicode points that contain digits 0-9; keep this sorted! */
649 static const WCHAR zeros[] =
651 0x0660, 0x06f0, 0x0966, 0x09e6, 0x0a66, 0x0ae6, 0x0b66, 0x0c66, 0x0ce6,
652 0x0d66, 0x0e50, 0x0ed0, 0x0f20, 0x1040, 0x17e0, 0x1810, 0xff10
654 int i;
656 if ('0' <= c && c <= '9') return c - '0';
657 if ('A' <= c && c <= 'Z') return c - 'A' + 10;
658 if ('a' <= c && c <= 'z') return c - 'a' + 10;
659 for (i = 0; i < ARRAY_SIZE(zeros) && c >= zeros[i]; i++)
660 if (zeros[i] <= c && c <= zeros[i] + 9) return c - zeros[i];
662 return -1;
665 /*********************************************************************
666 * wcstol (NTDLL.@)
668 __msvcrt_long __cdecl wcstol(LPCWSTR s, LPWSTR *end, INT base)
670 BOOL negative = FALSE, empty = TRUE;
671 LONG ret = 0;
673 if (base < 0 || base == 1 || base > 36) return 0;
674 if (end) *end = (WCHAR *)s;
675 while (iswspace(*s)) s++;
677 if (*s == '-')
679 negative = TRUE;
680 s++;
682 else if (*s == '+') s++;
684 if ((base == 0 || base == 16) && !wctoint( *s ) && (s[1] == 'x' || s[1] == 'X'))
686 base = 16;
687 s += 2;
689 if (base == 0) base = wctoint( *s ) ? 10 : 8;
691 while (*s)
693 int v = wctoint( *s );
694 if (v < 0 || v >= base) break;
695 if (negative) v = -v;
696 s++;
697 empty = FALSE;
699 if (!negative && (ret > MAXLONG / base || ret * base > MAXLONG - v))
700 ret = MAXLONG;
701 else if (negative && (ret < (LONG)MINLONG / base || ret * base < (LONG)(MINLONG - v)))
702 ret = MINLONG;
703 else
704 ret = ret * base + v;
707 if (end && !empty) *end = (WCHAR *)s;
708 return ret;
712 /*********************************************************************
713 * wcstoul (NTDLL.@)
715 __msvcrt_ulong __cdecl wcstoul(LPCWSTR s, LPWSTR *end, INT base)
717 BOOL negative = FALSE, empty = TRUE;
718 ULONG ret = 0;
720 if (base < 0 || base == 1 || base > 36) return 0;
721 if (end) *end = (WCHAR *)s;
722 while (iswspace(*s)) s++;
724 if (*s == '-')
726 negative = TRUE;
727 s++;
729 else if (*s == '+') s++;
731 if ((base == 0 || base == 16) && !wctoint( *s ) && (s[1] == 'x' || s[1] == 'X'))
733 base = 16;
734 s += 2;
736 if (base == 0) base = wctoint( *s ) ? 10 : 8;
738 while (*s)
740 int v = wctoint( *s );
741 if (v < 0 || v >= base) break;
742 s++;
743 empty = FALSE;
745 if (ret > MAXDWORD / base || ret * base > MAXDWORD - v)
746 ret = MAXDWORD;
747 else
748 ret = ret * base + v;
751 if (end && !empty) *end = (WCHAR *)s;
752 return negative ? -ret : ret;
756 /*********************************************************************
757 * _wcstoi64 (NTDLL.@)
759 __int64 __cdecl _wcstoi64( const wchar_t *s, wchar_t **end, int base )
761 BOOL negative = FALSE, empty = TRUE;
762 __int64 ret = 0;
764 if (base < 0 || base == 1 || base > 36) return 0;
765 if (end) *end = (wchar_t *)s;
766 while (iswspace(*s)) s++;
768 if (*s == '-')
770 negative = TRUE;
771 s++;
773 else if (*s == '+') s++;
775 if ((base == 0 || base == 16) && !wctoint( *s ) && (s[1] == 'x' || s[1] == 'X'))
777 base = 16;
778 s += 2;
780 if (base == 0) base = wctoint( *s ) ? 10 : 8;
782 while (*s)
784 int v = wctoint( *s );
785 if (v < 0 || v >= base) break;
786 if (negative) v = -v;
787 s++;
788 empty = FALSE;
790 if (!negative && (ret > I64_MAX / base || ret * base > I64_MAX - v))
791 ret = I64_MAX;
792 else if (negative && (ret < I64_MIN / base || ret * base < I64_MIN - v))
793 ret = I64_MIN;
794 else
795 ret = ret * base + v;
798 if (end && !empty) *end = (wchar_t *)s;
799 return ret;
803 /*********************************************************************
804 * _wcstoui64 (NTDLL.@)
806 unsigned __int64 __cdecl _wcstoui64( const wchar_t *s, wchar_t **end, int base )
808 BOOL negative = FALSE, empty = TRUE;
809 unsigned __int64 ret = 0;
811 if (base < 0 || base == 1 || base > 36) return 0;
812 if (end) *end = (wchar_t *)s;
813 while (iswspace(*s)) s++;
815 if (*s == '-')
817 negative = TRUE;
818 s++;
820 else if (*s == '+') s++;
822 if ((base == 0 || base == 16) && !wctoint( *s ) && (s[1] == 'x' || s[1] == 'X'))
824 base = 16;
825 s += 2;
827 if (base == 0) base = wctoint( *s ) ? 10 : 8;
829 while (*s)
831 int v = wctoint( *s );
832 if (v < 0 || v >= base) break;
833 s++;
834 empty = FALSE;
836 if (ret > UI64_MAX / base || ret * base > UI64_MAX - v)
837 ret = UI64_MAX;
838 else
839 ret = ret * base + v;
842 if (end && !empty) *end = (wchar_t *)s;
843 return negative ? -ret : ret;
847 /*********************************************************************
848 * _ultow (NTDLL.@)
850 * Converts an unsigned long integer to a unicode string.
852 * RETURNS
853 * Always returns str.
855 * NOTES
856 * Converts value to a '\0' terminated wstring which is copied to str.
857 * The maximum length of the copied str is 33 bytes.
858 * Does not check if radix is in the range of 2 to 36.
859 * If str is NULL it just returns NULL.
861 LPWSTR __cdecl _ultow( __msvcrt_ulong value, LPWSTR str, INT radix )
863 WCHAR buffer[33];
864 PWCHAR pos;
865 WCHAR digit;
867 pos = &buffer[32];
868 *pos = '\0';
870 do {
871 digit = value % radix;
872 value = value / radix;
873 if (digit < 10) {
874 *--pos = '0' + digit;
875 } else {
876 *--pos = 'a' + digit - 10;
877 } /* if */
878 } while (value != 0L);
880 if (str != NULL) {
881 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
882 } /* if */
883 return str;
887 /*********************************************************************
888 * _ltow (NTDLL.@)
890 * Converts a long integer to a unicode string.
892 * RETURNS
893 * Always returns str.
895 * NOTES
896 * Converts value to a '\0' terminated wstring which is copied to str.
897 * The maximum length of the copied str is 33 bytes. If radix
898 * is 10 and value is negative, the value is converted with sign.
899 * Does not check if radix is in the range of 2 to 36.
900 * If str is NULL it just returns NULL.
902 LPWSTR __cdecl _ltow( __msvcrt_long value, LPWSTR str, INT radix )
904 ULONG val;
905 int negative;
906 WCHAR buffer[33];
907 PWCHAR pos;
908 WCHAR digit;
910 if (value < 0 && radix == 10) {
911 negative = 1;
912 val = -value;
913 } else {
914 negative = 0;
915 val = value;
916 } /* if */
918 pos = &buffer[32];
919 *pos = '\0';
921 do {
922 digit = val % radix;
923 val = val / radix;
924 if (digit < 10) {
925 *--pos = '0' + digit;
926 } else {
927 *--pos = 'a' + digit - 10;
928 } /* if */
929 } while (val != 0L);
931 if (negative) {
932 *--pos = '-';
933 } /* if */
935 if (str != NULL) {
936 memcpy(str, pos, (&buffer[32] - pos + 1) * sizeof(WCHAR));
937 } /* if */
938 return str;
942 /*********************************************************************
943 * _itow (NTDLL.@)
945 * Converts an integer to a unicode string.
947 * RETURNS
948 * Always returns str.
950 * NOTES
951 * Converts value to a '\0' terminated wstring which is copied to str.
952 * The maximum length of the copied str is 33 bytes. If radix
953 * is 10 and value is negative, the value is converted with sign.
954 * Does not check if radix is in the range of 2 to 36.
955 * If str is NULL it just returns NULL.
957 * DIFFERENCES
958 * - The native function crashes when the string is longer than 19 chars.
959 * This function does not have this bug.
961 LPWSTR __cdecl _itow(
962 int value, /* [I] Value to be converted */
963 LPWSTR str, /* [O] Destination for the converted value */
964 INT radix) /* [I] Number base for conversion */
966 return _ltow(value, str, radix);
970 /*********************************************************************
971 * _ui64tow (NTDLL.@)
973 * Converts a large unsigned integer to a unicode string.
975 * RETURNS
976 * Always returns str.
978 * NOTES
979 * Converts value to a '\0' terminated wstring which is copied to str.
980 * The maximum length of the copied str is 33 bytes.
981 * Does not check if radix is in the range of 2 to 36.
982 * If str is NULL it just returns NULL.
984 * DIFFERENCES
985 * - This function does not exist in the native DLL (but in msvcrt).
986 * But since the maintenance of all these functions is better done
987 * in one place we implement it here.
989 LPWSTR __cdecl _ui64tow(
990 ULONGLONG value, /* [I] Value to be converted */
991 LPWSTR str, /* [O] Destination for the converted value */
992 INT radix) /* [I] Number base for conversion */
994 WCHAR buffer[65];
995 PWCHAR pos;
996 WCHAR digit;
998 pos = &buffer[64];
999 *pos = '\0';
1001 do {
1002 digit = value % radix;
1003 value = value / radix;
1004 if (digit < 10) {
1005 *--pos = '0' + digit;
1006 } else {
1007 *--pos = 'a' + digit - 10;
1008 } /* if */
1009 } while (value != 0L);
1011 if (str != NULL) {
1012 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
1013 } /* if */
1014 return str;
1018 /*********************************************************************
1019 * _i64tow (NTDLL.@)
1021 * Converts a large integer to a unicode string.
1023 * RETURNS
1024 * Always returns str.
1026 * NOTES
1027 * Converts value to a '\0' terminated wstring which is copied to str.
1028 * The maximum length of the copied str is 33 bytes. If radix
1029 * is 10 and value is negative, the value is converted with sign.
1030 * Does not check if radix is in the range of 2 to 36.
1031 * If str is NULL it just returns NULL.
1033 * DIFFERENCES
1034 * - The native DLL converts negative values (for base 10) wrong:
1035 * -1 is converted to -18446744073709551615
1036 * -2 is converted to -18446744073709551614
1037 * -9223372036854775807 is converted to -9223372036854775809
1038 * -9223372036854775808 is converted to -9223372036854775808
1039 * The native msvcrt _i64tow function and our ntdll function do
1040 * not have this bug.
1042 LPWSTR __cdecl _i64tow(
1043 LONGLONG value, /* [I] Value to be converted */
1044 LPWSTR str, /* [O] Destination for the converted value */
1045 INT radix) /* [I] Number base for conversion */
1047 ULONGLONG val;
1048 int negative;
1049 WCHAR buffer[65];
1050 PWCHAR pos;
1051 WCHAR digit;
1053 if (value < 0 && radix == 10) {
1054 negative = 1;
1055 val = -value;
1056 } else {
1057 negative = 0;
1058 val = value;
1059 } /* if */
1061 pos = &buffer[64];
1062 *pos = '\0';
1064 do {
1065 digit = val % radix;
1066 val = val / radix;
1067 if (digit < 10) {
1068 *--pos = '0' + digit;
1069 } else {
1070 *--pos = 'a' + digit - 10;
1071 } /* if */
1072 } while (val != 0L);
1074 if (negative) {
1075 *--pos = '-';
1076 } /* if */
1078 if (str != NULL) {
1079 memcpy(str, pos, (&buffer[64] - pos + 1) * sizeof(WCHAR));
1080 } /* if */
1081 return str;
1085 /*********************************************************************
1086 * _ui64tow_s (NTDLL.@)
1088 errno_t __cdecl _ui64tow_s( unsigned __int64 value, wchar_t *str, size_t size, int radix )
1090 wchar_t buffer[65], *pos;
1092 if (!str || !size) return EINVAL;
1093 if (radix < 2 || radix > 36)
1095 str[0] = 0;
1096 return EINVAL;
1099 pos = buffer + 64;
1100 *pos = 0;
1102 do {
1103 int digit = value % radix;
1104 value = value / radix;
1105 if (digit < 10)
1106 *--pos = '0' + digit;
1107 else
1108 *--pos = 'a' + digit - 10;
1109 } while (value != 0);
1111 if (buffer - pos + 65 > size)
1113 str[0] = 0;
1114 return ERANGE;
1116 memcpy( str, pos, (buffer - pos + 65) * sizeof(wchar_t) );
1117 return 0;
1121 /*********************************************************************
1122 * _ultow_s (NTDLL.@)
1124 errno_t __cdecl _ultow_s( __msvcrt_ulong value, wchar_t *str, size_t size, int radix )
1126 return _ui64tow_s( value, str, size, radix );
1130 /*********************************************************************
1131 * _i64tow_s (NTDLL.@)
1133 errno_t __cdecl _i64tow_s( __int64 value, wchar_t *str, size_t size, int radix )
1135 unsigned __int64 val;
1136 BOOL is_negative;
1137 wchar_t buffer[65], *pos;
1139 if (!str || !size) return EINVAL;
1140 if (radix < 2 || radix > 36)
1142 str[0] = 0;
1143 return EINVAL;
1146 if (value < 0 && radix == 10)
1148 is_negative = TRUE;
1149 val = -value;
1151 else
1153 is_negative = FALSE;
1154 val = value;
1157 pos = buffer + 64;
1158 *pos = 0;
1162 unsigned int digit = val % radix;
1163 val /= radix;
1165 if (digit < 10)
1166 *--pos = '0' + digit;
1167 else
1168 *--pos = 'a' + digit - 10;
1170 while (val != 0);
1172 if (is_negative) *--pos = '-';
1174 if (buffer - pos + 65 > size)
1176 str[0] = 0;
1177 return ERANGE;
1179 memcpy( str, pos, (buffer - pos + 65) * sizeof(wchar_t) );
1180 return 0;
1184 /*********************************************************************
1185 * _ltow_s (NTDLL.@)
1187 errno_t __cdecl _ltow_s( __msvcrt_long value, wchar_t *str, size_t size, int radix )
1189 if (value < 0 && radix == 10) return _i64tow_s( value, str, size, radix );
1190 return _ui64tow_s( (__msvcrt_ulong)value, str, size, radix );
1194 /*********************************************************************
1195 * _itow_s (NTDLL.@)
1197 errno_t __cdecl _itow_s( int value, wchar_t *str, size_t size, int radix )
1199 if (value < 0 && radix == 10) return _i64tow_s( value, str, size, radix );
1200 return _ui64tow_s( (unsigned int)value, str, size, radix );
1204 /*********************************************************************
1205 * _wtol (NTDLL.@)
1207 * Converts a unicode string to a long integer.
1209 * PARAMS
1210 * str [I] Wstring to be converted
1212 * RETURNS
1213 * On success it returns the integer value otherwise it returns 0.
1215 * NOTES
1216 * Accepts: {whitespace} [+|-] {digits}
1217 * No check is made for value overflow, only the lower 32 bits are assigned.
1218 * If str is NULL it crashes, as the native function does.
1220 __msvcrt_long __cdecl _wtol( LPCWSTR str )
1222 ULONG RunningTotal = 0;
1223 BOOL bMinus = FALSE;
1225 while (iswspace(*str)) str++;
1227 if (*str == '+') {
1228 str++;
1229 } else if (*str == '-') {
1230 bMinus = TRUE;
1231 str++;
1232 } /* if */
1234 while (*str >= '0' && *str <= '9') {
1235 RunningTotal = RunningTotal * 10 + *str - '0';
1236 str++;
1237 } /* while */
1239 return bMinus ? -RunningTotal : RunningTotal;
1243 /*********************************************************************
1244 * _wtoi (NTDLL.@)
1246 * Converts a unicode string to an integer.
1248 * PARAMS
1249 * str [I] Wstring to be converted
1251 * RETURNS
1252 * On success it returns the integer value otherwise it returns 0.
1254 * NOTES
1255 * Accepts: {whitespace} [+|-] {digits}
1256 * No check is made for value overflow, only the lower 32 bits are assigned.
1257 * If str is NULL it crashes, as the native function does.
1259 int __cdecl _wtoi( LPCWSTR str )
1261 return _wtol(str);
1265 /*********************************************************************
1266 * _wtoi64 (NTDLL.@)
1268 * Converts a unicode string to a large integer.
1270 * PARAMS
1271 * str [I] Wstring to be converted
1273 * RETURNS
1274 * On success it returns the integer value otherwise it returns 0.
1276 * NOTES
1277 * Accepts: {whitespace} [+|-] {digits}
1278 * No check is made for value overflow, only the lower 64 bits are assigned.
1279 * If str is NULL it crashes, as the native function does.
1281 LONGLONG __cdecl _wtoi64( LPCWSTR str )
1283 ULONGLONG RunningTotal = 0;
1284 BOOL bMinus = FALSE;
1286 while (iswspace(*str)) str++;
1288 if (*str == '+') {
1289 str++;
1290 } else if (*str == '-') {
1291 bMinus = TRUE;
1292 str++;
1293 } /* if */
1295 while (*str >= '0' && *str <= '9') {
1296 RunningTotal = RunningTotal * 10 + *str - '0';
1297 str++;
1298 } /* while */
1300 return bMinus ? -RunningTotal : RunningTotal;
1304 /******************************************************************
1305 * _wsplitpath_s (NTDLL.@)
1307 errno_t __cdecl _wsplitpath_s( const wchar_t *inpath, wchar_t *drive, size_t sz_drive,
1308 wchar_t *dir, size_t sz_dir, wchar_t *fname, size_t sz_fname,
1309 wchar_t *ext, size_t sz_ext )
1311 const wchar_t *p, *end;
1313 if (!inpath || (!drive && sz_drive) ||
1314 (drive && !sz_drive) ||
1315 (!dir && sz_dir) ||
1316 (dir && !sz_dir) ||
1317 (!fname && sz_fname) ||
1318 (fname && !sz_fname) ||
1319 (!ext && sz_ext) ||
1320 (ext && !sz_ext))
1321 return EINVAL;
1323 if (inpath[0] && inpath[1] == ':')
1325 if (drive)
1327 if (sz_drive <= 2) goto error;
1328 drive[0] = inpath[0];
1329 drive[1] = inpath[1];
1330 drive[2] = 0;
1332 inpath += 2;
1334 else if (drive) drive[0] = '\0';
1336 /* look for end of directory part */
1337 end = NULL;
1338 for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;
1340 if (end) /* got a directory */
1342 if (dir)
1344 if (sz_dir <= end - inpath) goto error;
1345 memcpy( dir, inpath, (end - inpath) * sizeof(wchar_t) );
1346 dir[end - inpath] = 0;
1348 inpath = end;
1350 else if (dir) dir[0] = 0;
1352 /* look for extension: what's after the last dot */
1353 end = NULL;
1354 for (p = inpath; *p; p++) if (*p == '.') end = p;
1356 if (!end) end = p; /* there's no extension */
1358 if (fname)
1360 if (sz_fname <= end - inpath) goto error;
1361 memcpy( fname, inpath, (end - inpath) * sizeof(wchar_t) );
1362 fname[end - inpath] = 0;
1364 if (ext)
1366 if (sz_ext <= wcslen(end)) goto error;
1367 wcscpy( ext, end );
1369 return 0;
1371 error:
1372 if (drive) drive[0] = 0;
1373 if (dir) dir[0] = 0;
1374 if (fname) fname[0]= 0;
1375 if (ext) ext[0]= 0;
1376 return ERANGE;
1380 /*********************************************************************
1381 * _wmakepath_s (NTDLL.@)
1383 errno_t __cdecl _wmakepath_s( wchar_t *path, size_t size, const wchar_t *drive,
1384 const wchar_t *directory, const wchar_t *filename,
1385 const wchar_t *extension )
1387 wchar_t *p = path;
1389 if (!path || !size) return EINVAL;
1391 if (drive && drive[0])
1393 if (size <= 2) goto range;
1394 *p++ = drive[0];
1395 *p++ = ':';
1396 size -= 2;
1399 if (directory && directory[0])
1401 unsigned int len = wcslen(directory);
1402 unsigned int needs_separator = directory[len - 1] != '/' && directory[len - 1] != '\\';
1403 unsigned int copylen = min(size - 1, len);
1405 if (size < 2) goto range;
1406 memmove(p, directory, copylen * sizeof(wchar_t));
1407 if (size <= len) goto range;
1408 p += copylen;
1409 size -= copylen;
1410 if (needs_separator)
1412 if (size < 2) goto range;
1413 *p++ = '\\';
1414 size -= 1;
1418 if (filename && filename[0])
1420 unsigned int len = wcslen(filename);
1421 unsigned int copylen = min(size - 1, len);
1423 if (size < 2) goto range;
1424 memmove(p, filename, copylen * sizeof(wchar_t));
1425 if (size <= len) goto range;
1426 p += len;
1427 size -= len;
1430 if (extension && extension[0])
1432 unsigned int len = wcslen(extension);
1433 unsigned int needs_period = extension[0] != '.';
1434 unsigned int copylen;
1436 if (size < 2) goto range;
1437 if (needs_period)
1439 *p++ = '.';
1440 size -= 1;
1442 copylen = min(size - 1, len);
1443 memcpy(p, extension, copylen * sizeof(wchar_t));
1444 if (size <= len) goto range;
1445 p += copylen;
1448 *p = 0;
1449 return 0;
1451 range:
1452 path[0] = 0;
1453 return ERANGE;