string.c 42.2 KB
Newer Older
1 2 3 4
/*
 * NTDLL string functions
 *
 * Copyright 2000 Alexandre Julliard
5
 * Copyright 2000 Jon Griffiths
6
 * Copyright 2003 Thomas Mertes
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
21 22 23
 */

#include "config.h"
24
#include "wine/port.h"
25 26

#include <ctype.h>
27
#include <limits.h>
28
#include <stdarg.h>
29 30
#include <stdlib.h>
#include <stdio.h>
31 32 33
#include <string.h>

#include "windef.h"
34 35
#include "winternl.h"

36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/*********************************************************************
 *                  memchr   (NTDLL.@)
 */
void * __cdecl NTDLL_memchr( const void *ptr, int c, size_t n )
{
    return memchr( ptr, c, n );
}


/*********************************************************************
 *                  memcmp   (NTDLL.@)
 */
int __cdecl NTDLL_memcmp( const void *ptr1, const void *ptr2, size_t n )
{
    return memcmp( ptr1, ptr2, n );
}


/*********************************************************************
 *                  memcpy   (NTDLL.@)
 *
 * NOTES
 *  Behaves like memmove.
 */
void * __cdecl NTDLL_memcpy( void *dst, const void *src, size_t n )
{
    return memmove( dst, src, n );
}


/*********************************************************************
 *                  memmove   (NTDLL.@)
 */
void * __cdecl NTDLL_memmove( void *dst, const void *src, size_t n )
{
    return memmove( dst, src, n );
}


/*********************************************************************
 *                  memset   (NTDLL.@)
 */
void * __cdecl NTDLL_memset( void *dst, int c, size_t n )
{
    return memset( dst, c, n );
}


/*********************************************************************
 *                  strcat   (NTDLL.@)
 */
char * __cdecl NTDLL_strcat( char *dst, const char *src )
{
    return strcat( dst, src );
}


/*********************************************************************
 *                  strchr   (NTDLL.@)
 */
char * __cdecl NTDLL_strchr( const char *str, int c )
{
    return strchr( str, c );
}


/*********************************************************************
 *                  strcmp   (NTDLL.@)
 */
int __cdecl NTDLL_strcmp( const char *str1, const char *str2 )
{
    return strcmp( str1, str2 );
}


/*********************************************************************
 *                  strcpy   (NTDLL.@)
 */
char * __cdecl NTDLL_strcpy( char *dst, const char *src )
{
    return strcpy( dst, src );
}


/*********************************************************************
 *                  strcspn   (NTDLL.@)
 */
size_t __cdecl NTDLL_strcspn( const char *str, const char *reject )
{
    return strcspn( str, reject );
}


/*********************************************************************
 *                  strlen   (NTDLL.@)
 */
size_t __cdecl NTDLL_strlen( const char *str )
{
    return strlen( str );
}


/*********************************************************************
 *                  strncat   (NTDLL.@)
 */
char * __cdecl NTDLL_strncat( char *dst, const char *src, size_t len )
{
    return strncat( dst, src, len );
}


/*********************************************************************
 *                  strncmp   (NTDLL.@)
 */
int __cdecl NTDLL_strncmp( const char *str1, const char *str2, size_t len )
{
    return strncmp( str1, str2, len );
}


/*********************************************************************
 *                  strncpy   (NTDLL.@)
 */
char * __cdecl NTDLL_strncpy( char *dst, const char *src, size_t len )
{
    return strncpy( dst, src, len );
}


/*********************************************************************
 *                  strpbrk   (NTDLL.@)
 */
char * __cdecl NTDLL_strpbrk( const char *str, const char *accept )
{
    return strpbrk( str, accept );
}


/*********************************************************************
 *                  strrchr   (NTDLL.@)
 */
char * __cdecl NTDLL_strrchr( const char *str, int c )
{
    return strrchr( str, c );
}


/*********************************************************************
 *                  strspn   (NTDLL.@)
 */
size_t __cdecl NTDLL_strspn( const char *str, const char *accept )
{
    return strspn( str, accept );
}


/*********************************************************************
 *                  strstr   (NTDLL.@)
 */
char * __cdecl NTDLL_strstr( const char *haystack, const char *needle )
{
    return strstr( haystack, needle );
}


/*********************************************************************
 *                  _memccpy   (NTDLL.@)
 */
void * __cdecl _memccpy( void *dst, const void *src, int c, size_t n )
{
    return memccpy( dst, src, c, n );
}


211
/*********************************************************************
212
 *                  _memicmp   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
 *
 * Compare two blocks of memory as strings, ignoring case.
 *
 * PARAMS
 *  s1  [I] First string to compare to s2
 *  s2  [I] Second string to compare to s1
 *  len [I] Number of bytes to compare
 *
 * RETURNS
 *  An integer less than, equal to, or greater than zero indicating that
 *  s1 is less than, equal to or greater than s2 respectively.
 *
 * NOTES
 *  Any Nul characters in s1 or s2 are ignored. This function always
 *  compares up to len bytes or the first place where s1 and s2 differ.
228
 */
229
INT __cdecl _memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
230 231 232 233 234 235 236 237 238 239 240
{
    int ret = 0;
    while (len--)
    {
        if ((ret = tolower(*s1) - tolower(*s2))) break;
        s1++;
        s2++;
    }
    return ret;
}

241

242
/*********************************************************************
243 244
 *                  _stricmp   (NTDLL.@)
 *                  _strcmpi   (NTDLL.@)
245
 */
246
int __cdecl _stricmp( LPCSTR str1, LPCSTR str2 )
247
{
248 249 250 251 252 253 254 255 256 257
    return strcasecmp( str1, str2 );
}


/*********************************************************************
 *                  _strnicmp   (NTDLL.@)
 */
int __cdecl _strnicmp( LPCSTR str1, LPCSTR str2, size_t n )
{
    return strncasecmp( str1, str2, n );
258 259 260
}


261
/*********************************************************************
262
 *                  _strupr   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
263 264 265 266 267 268 269 270 271
 *
 * Convert a string to upper case.
 *
 * PARAMS
 *  str [I/O] String to convert
 *
 * RETURNS
 *  str. There is no error return, if str is NULL or invalid, this
 *  function will crash.
272 273 274 275 276 277 278 279
 */
LPSTR __cdecl _strupr( LPSTR str )
{
    LPSTR ret = str;
    for ( ; *str; str++) *str = toupper(*str);
    return ret;
}

280

281
/*********************************************************************
282
 *                  _strlwr   (NTDLL.@)
283
 *
Jon Griffiths's avatar
Jon Griffiths committed
284 285 286 287 288 289 290 291
 * Convert a string to lowercase
 *
 * PARAMS
 *  str [I/O] String to convert
 *
 * RETURNS
 *  str. There is no error return, if str is NULL or invalid, this
 *  function will crash.
292 293 294 295 296 297 298 299 300
 */
LPSTR __cdecl _strlwr( LPSTR str )
{
    LPSTR ret = str;
    for ( ; *str; str++) *str = tolower(*str);
    return ret;
}


301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/*********************************************************************
 *                  tolower   (NTDLL.@)
 */
int __cdecl NTDLL_tolower( int c )
{
    return tolower( c );
}


/*********************************************************************
 *                  toupper   (NTDLL.@)
 */
int __cdecl NTDLL_toupper( int c )
{
    return toupper( c );
}


/*********************************************************************
 *                  isalnum   (NTDLL.@)
 */
int __cdecl NTDLL_isalnum( int c )
{
    return isalnum( c );
}


/*********************************************************************
 *                  isalpha   (NTDLL.@)
 */
int __cdecl NTDLL_isalpha( int c )
{
    return isalpha( c );
}


/*********************************************************************
 *                  iscntrl   (NTDLL.@)
 */
int __cdecl NTDLL_iscntrl( int c )
{
    return iscntrl( c );
}


/*********************************************************************
 *                  isdigit   (NTDLL.@)
 */
int __cdecl NTDLL_isdigit( int c )
{
    return isdigit( c );
}


/*********************************************************************
 *                  isgraph   (NTDLL.@)
 */
int __cdecl NTDLL_isgraph( int c )
{
    return isgraph( c );
}


/*********************************************************************
 *                  islower   (NTDLL.@)
 */
int __cdecl NTDLL_islower( int c )
{
    return islower( c );
}


/*********************************************************************
 *                  isprint   (NTDLL.@)
 */
int __cdecl NTDLL_isprint( int c )
{
    return isprint( c );
}


/*********************************************************************
 *                  ispunct   (NTDLL.@)
 */
int __cdecl NTDLL_ispunct( int c )
{
    return ispunct( c );
}


/*********************************************************************
 *                  isspace   (NTDLL.@)
 */
int __cdecl NTDLL_isspace( int c )
{
    return isspace( c );
}


/*********************************************************************
 *                  isupper   (NTDLL.@)
 */
int __cdecl NTDLL_isupper( int c )
{
    return isupper( c );
}


/*********************************************************************
 *                  isxdigit   (NTDLL.@)
 */
int __cdecl NTDLL_isxdigit( int c )
{
    return isxdigit( c );
}


418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
/*********************************************************************
 *		__isascii (NTDLL.@)
 */
int CDECL NTDLL___isascii(int c)
{
    return (unsigned)c < 0x80;
}


/*********************************************************************
 *		__toascii (NTDLL.@)
 */
int CDECL NTDLL___toascii(int c)
{
    return (unsigned)c & 0x7f;
}


/*********************************************************************
 *		__iscsym (NTDLL.@)
 */
int CDECL NTDLL___iscsym(int c)
{
    return (c < 127 && (isalnum(c) || c == '_'));
}


/*********************************************************************
 *		__iscsymf (NTDLL.@)
 */
int CDECL NTDLL___iscsymf(int c)
{
    return (c < 127 && (isalpha(c) || c == '_'));
}


/*********************************************************************
 *		_toupper (NTDLL.@)
 */
int CDECL NTDLL__toupper(int c)
{
    return c - 0x20;  /* sic */
}


/*********************************************************************
 *		_tolower (NTDLL.@)
 */
int CDECL NTDLL__tolower(int c)
{
    return c + 0x20;  /* sic */
}


472 473 474
/*********************************************************************
 *                  strtol   (NTDLL.@)
 */
475
LONG __cdecl NTDLL_strtol( const char *nptr, char **endptr, int base )
476 477 478 479 480 481 482 483
{
    return strtol( nptr, endptr, base );
}


/*********************************************************************
 *                  strtoul   (NTDLL.@)
 */
484
ULONG __cdecl NTDLL_strtoul( const char *nptr, char **endptr, int base )
485 486 487 488 489
{
    return strtoul( nptr, endptr, base );
}


490
/*********************************************************************
491 492
 *      _ultoa   (NTDLL.@)
 *
Jon Griffiths's avatar
Jon Griffiths committed
493
 * Convert an unsigned long integer to a string.
494
 *
495
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
496
 *  str.
497 498
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
499 500 501 502
 *  - Converts value to a Nul terminated string which is copied to str.
 *  - The maximum length of the copied str is 33 bytes.
 *  - Does not check if radix is in the range of 2 to 36.
 *  - If str is NULL it crashes, as the native function does.
503
 */
504
char * __cdecl _ultoa(
505
    ULONG value,         /* [I] Value to be converted */
506 507
    char *str,           /* [O] Destination for the converted value */
    int radix)           /* [I] Number base for conversion */
508
{
509 510 511
    char buffer[33];
    char *pos;
    int digit;
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
    pos = &buffer[32];
    *pos = '\0';

    do {
	digit = value % radix;
	value = value / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (value != 0L);

    memcpy(str, pos, &buffer[32] - pos + 1);
    return str;
528 529 530 531
}


/*********************************************************************
532 533
 *      _ltoa   (NTDLL.@)
 *
Jon Griffiths's avatar
Jon Griffiths committed
534
 * Convert a long integer to a string.
535
 *
536
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
537
 *  str.
538 539
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
540 541
 *  - Converts value to a Nul terminated string which is copied to str.
 *  - The maximum length of the copied str is 33 bytes. If radix
542
 *  is 10 and value is negative, the value is converted with sign.
Jon Griffiths's avatar
Jon Griffiths committed
543 544
 *  - Does not check if radix is in the range of 2 to 36.
 *  - If str is NULL it crashes, as the native function does.
545
 */
546
char * __cdecl _ltoa(
547
    LONG value, /* [I] Value to be converted */
548 549
    char *str,  /* [O] Destination for the converted value */
    int radix)  /* [I] Number base for conversion */
550
{
551
    ULONG val;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    int negative;
    char buffer[33];
    char *pos;
    int digit;

    if (value < 0 && radix == 10) {
	negative = 1;
        val = -value;
    } else {
	negative = 0;
        val = value;
    } /* if */

    pos = &buffer[32];
    *pos = '\0';

    do {
	digit = val % radix;
	val = val / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (val != 0L);

    if (negative) {
	*--pos = '-';
    } /* if */

    memcpy(str, pos, &buffer[32] - pos + 1);
    return str;
}


/*********************************************************************
 *      _itoa    (NTDLL.@)
 *
 * Converts an integer to a string.
 *
592
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
593
 *  str.
594 595
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
596 597
 *  - Converts value to a '\0' terminated string which is copied to str.
 *  - The maximum length of the copied str is 33 bytes. If radix
598
 *  is 10 and value is negative, the value is converted with sign.
Jon Griffiths's avatar
Jon Griffiths committed
599 600
 *  - Does not check if radix is in the range of 2 to 36.
 *  - If str is NULL it crashes, as the native function does.
601
 */
602 603 604 605
char * __cdecl _itoa(
    int value, /* [I] Value to be converted */
    char *str, /* [O] Destination for the converted value */
    int radix) /* [I] Number base for conversion */
606 607 608 609 610 611 612 613 614 615
{
    return _ltoa(value, str, radix);
}


/*********************************************************************
 *      _ui64toa   (NTDLL.@)
 *
 * Converts a large unsigned integer to a string.
 *
616
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
617
 *  str.
618 619
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
620 621 622 623
 *  - Converts value to a '\0' terminated string which is copied to str.
 *  - The maximum length of the copied str is 65 bytes.
 *  - Does not check if radix is in the range of 2 to 36.
 *  - If str is NULL it crashes, as the native function does.
624
 */
625 626 627 628
char * __cdecl _ui64toa(
    ULONGLONG value, /* [I] Value to be converted */
    char *str,       /* [O] Destination for the converted value */
    int radix)       /* [I] Number base for conversion */
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
{
    char buffer[65];
    char *pos;
    int digit;

    pos = &buffer[64];
    *pos = '\0';

    do {
	digit = value % radix;
	value = value / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (value != 0L);

    memcpy(str, pos, &buffer[64] - pos + 1);
    return str;
}


/*********************************************************************
 *      _i64toa   (NTDLL.@)
 *
 * Converts a large integer to a string.
 *
657
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
658
 *  str.
659
 *
660
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
661 662
 *  - Converts value to a Nul terminated string which is copied to str.
 *  - The maximum length of the copied str is 65 bytes. If radix
663
 *  is 10 and value is negative, the value is converted with sign.
Jon Griffiths's avatar
Jon Griffiths committed
664 665
 *  - Does not check if radix is in the range of 2 to 36.
 *  - If str is NULL it crashes, as the native function does.
666 667
 *
 * DIFFERENCES
668
 * - The native DLL converts negative values (for base 10) wrong:
Jon Griffiths's avatar
Jon Griffiths committed
669 670 671 672
 *|                     -1 is converted to -18446744073709551615
 *|                     -2 is converted to -18446744073709551614
 *|   -9223372036854775807 is converted to  -9223372036854775809
 *|   -9223372036854775808 is converted to  -9223372036854775808
673 674
 *   The native msvcrt _i64toa function and our ntdll _i64toa function
 *   do not have this bug.
675
 */
676 677 678 679
char * __cdecl _i64toa(
    LONGLONG value, /* [I] Value to be converted */
    char *str,      /* [O] Destination for the converted value */
    int radix)      /* [I] Number base for conversion */
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
{
    ULONGLONG val;
    int negative;
    char buffer[65];
    char *pos;
    int digit;

    if (value < 0 && radix == 10) {
	negative = 1;
        val = -value;
    } else {
	negative = 0;
        val = value;
    } /* if */

    pos = &buffer[64];
    *pos = '\0';

    do {
	digit = val % radix;
	val = val / radix;
	if (digit < 10) {
	    *--pos = '0' + digit;
	} else {
	    *--pos = 'a' + digit - 10;
	} /* if */
    } while (val != 0L);

    if (negative) {
	*--pos = '-';
    } /* if */

    memcpy(str, pos, &buffer[64] - pos + 1);
    return str;
714 715 716 717
}


/*********************************************************************
718 719
 *      _atoi64   (NTDLL.@)
 *
Jon Griffiths's avatar
Jon Griffiths committed
720
 * Convert a string to a large integer.
721
 *
722
 * PARAMS
Jon Griffiths's avatar
Jon Griffiths committed
723
 *  str [I] String to be converted
724 725
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
726 727 728
 *  Success: The integer value represented by str.
 *  Failure: 0. Note that this cannot be distinguished from a successful
 *           return, if the string contains "0".
729 730
 *
 * NOTES
Jon Griffiths's avatar
Jon Griffiths committed
731 732 733
 *  - Accepts: {whitespace} [+|-] {digits}
 *  - No check is made for value overflow, only the lower 64 bits are assigned.
 *  - If str is NULL it crashes, as the native function does.
734
 */
735
LONGLONG __cdecl _atoi64( const char *str )
736
{
737
    ULONGLONG RunningTotal = 0;
738
    BOOL bMinus = FALSE;
739 740 741 742 743 744 745 746

    while (*str == ' ' || (*str >= '\011' && *str <= '\015')) {
	str++;
    } /* while */

    if (*str == '+') {
	str++;
    } else if (*str == '-') {
747
        bMinus = TRUE;
748 749 750 751 752 753 754 755 756
	str++;
    } /* if */

    while (*str >= '0' && *str <= '9') {
	RunningTotal = RunningTotal * 10 + *str - '0';
	str++;
    } /* while */

    return bMinus ? -RunningTotal : RunningTotal;
757
}
758 759


760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/*********************************************************************
 *                  atoi   (NTDLL.@)
 */
int __cdecl NTDLL_atoi( const char *nptr )
{
    return _atoi64( nptr );
}


/*********************************************************************
 *                  atol   (NTDLL.@)
 */
LONG __cdecl NTDLL_atol( const char *nptr )
{
    return _atoi64( nptr );
}


778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
/* helper function for *scanf.  Returns the value of character c in the
 * given base, or -1 if the given character is not a digit of the base.
 */
static int char2digit( char c, int base )
{
    if ((c >= '0' && c <= '9') && (c <= '0'+base-1)) return (c-'0');
    if (base <= 10) return -1;
    if ((c >= 'A') && (c <= 'Z') && (c <= 'A'+base-11)) return (c-'A'+10);
    if ((c >= 'a') && (c <= 'z') && (c <= 'a'+base-11)) return (c-'a'+10);
    return -1;
}


static int NTDLL_vsscanf( const char *str, const char *format, __ms_va_list ap)
{
    int rd = 0, consumed = 0;
    int nch;
    if (!*format) return 0;

    nch = (consumed++, *str++);
    if (nch == '\0')
        return EOF;

    while (*format)
    {
        if (isspace( *format ))
        {
            /* skip whitespace */
            while ((nch != '\0') && isspace( nch ))
                nch = (consumed++, *str++);
        }
        else if (*format == '%')
        {
            int st = 0;
            BOOLEAN suppress = 0;
            int width = 0;
            int base;
            int h_prefix = 0;
            BOOLEAN l_prefix = FALSE;
            BOOLEAN L_prefix = FALSE;
            BOOLEAN w_prefix = FALSE;
            BOOLEAN I64_prefix = FALSE;
            BOOLEAN prefix_finished = FALSE;
            format++;
            /* a leading asterisk means 'suppress assignment of this field' */
            if (*format == '*')
            {
                format++;
                suppress = TRUE;
            }
            /* look for width specification */
            while (isdigit( *format ))
            {
                width *= 10;
                width += *format++ - '0';
            }
            if (width == 0) width = -1; /* no width spec seen */
            /* read prefix (if any) */
            while (!prefix_finished)
            {
                switch (*format)
                {
                case 'h': h_prefix++; break;
                case 'l':
                    if (*(format+1) == 'l')
                    {
                        I64_prefix = TRUE;
                        format++;
                    }
                    l_prefix = TRUE;
                    break;
                case 'w': w_prefix = TRUE; break;
                case 'L': L_prefix = TRUE; break;
                case 'I':
                    if (*(format + 1) == '6' &&
                        *(format + 2) == '4')
                    {
                        I64_prefix = TRUE;
                        format += 2;
                    }
                    break;
                default:
                    prefix_finished = TRUE;
                }
                if (!prefix_finished) format++;
            }
            /* read type */
            switch (*format)
            {
            case 'p':
            case 'P': /* pointer. */
                if (sizeof(void *) == sizeof(LONGLONG)) I64_prefix = TRUE;
                /* fall through */
            case 'x':
            case 'X': /* hexadecimal integer. */
                base = 16;
                goto number;
            case 'o': /* octal integer */
                base = 8;
                goto number;
            case 'u': /* unsigned decimal integer */
                base = 10;
                goto number;
            case 'd': /* signed decimal integer */
                base = 10;
                goto number;
            case 'i': /* generic integer */
                base = 0;
            number:
                {
                    /* read an integer */
                    ULONGLONG cur = 0;
                    BOOLEAN negative = FALSE;
                    BOOLEAN seendigit = FALSE;
                    /* skip initial whitespace */
                    while ((nch != '\0') && isspace( nch ))
                        nch = (consumed++, *str++);
                    /* get sign */
                    if (nch == '-' || nch == '+')
                    {
                        negative = (nch == '-');
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                    }
                    /* look for leading indication of base */
                    if (width != 0 && nch == '0' && *format != 'p' && *format != 'P')
                    {
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        seendigit = TRUE;
                        if (width != 0 && (nch == 'x' || nch == 'X'))
                        {
                            if (base == 0)
                                base = 16;
                            if (base == 16)
                            {
                                nch = (consumed++, *str++);
                                if (width > 0) width--;
                                seendigit = FALSE;
                            }
                        } else if (base == 0)
                            base = 8;
                    }
                    /* format %i without indication of base */
                    if (base == 0)
                        base = 10;
                    /* throw away leading zeros */
                    while (width != 0 && nch == '0')
                    {
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        seendigit = TRUE;
                    }
                    if (width != 0 && char2digit( nch, base ) != -1)
                    {
                        cur = char2digit( nch, base );
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        seendigit = TRUE;
                    }
                    /* read until no more digits */
                    while (width != 0 && nch != '\0' && char2digit( nch, base ) != -1)
                    {
                        cur = cur*base + char2digit( nch, base );
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        seendigit = TRUE;
                    }
                    /* okay, done! */
                    if (!seendigit) break; /* not a valid number */
                    st = 1;
                    if (!suppress)
                    {
#define _SET_NUMBER_( type ) *va_arg( ap, type* ) = negative ? -cur : cur
                        if (I64_prefix) _SET_NUMBER_( LONGLONG );
                        else if (l_prefix) _SET_NUMBER_( LONG );
                        else if (h_prefix == 1) _SET_NUMBER_( short int );
                        else _SET_NUMBER_( int );
                    }
                }
                break;
            case 'e':
            case 'E':
            case 'f':
            case 'g':
            case 'G':
                { /* read a float */
                    long double cur = 1, expcnt = 10;
                    ULONGLONG d, hlp;
                    int exp = 0;
                    BOOLEAN negative = FALSE;
                    /*unsigned fpcontrol;*/
                    BOOLEAN negexp;

                    /* skip initial whitespace */
                    while (nch != '\0' && isspace( nch ))
                        nch = (consumed++, *str++);

                    /* get sign */
                    if (nch == '-' || nch == '+')
                    {
                        negative = (nch == '-');
                        if (width > 0) width--;
                        if (width == 0) break;
                        nch = (consumed++, *str++);
                    }

                    /* get first digit */
                    if ('.' != nch)
                    {
                        if (!isdigit( nch )) break;
                        d = nch - '0';
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        /* read until no more digits */
                        while (width != 0 && nch != '\0' && isdigit( nch ))
                        {
                            hlp = d * 10 + nch - '0';
                            nch = (consumed++, *str++);
                            if (width > 0) width--;
                            if(d > (ULONGLONG)-1/10 || hlp < d)
                            {
                                exp++;
                                break;
                            }
                            else
                                d = hlp;
                        }
                        while (width != 0 && nch != '\0' && isdigit( nch ))
                        {
                            exp++;
                            nch = (consumed++, *str++);
                            if (width > 0) width--;
                        }
                    }
                    else
                        d = 0; /* Fix: .8 -> 0.8 */

                    /* handle decimals */
                    if (width != 0 && nch == '.')
                    {
                        nch = (consumed++, *str++);
                        if (width > 0) width--;

                        while (width != 0 && nch != '\0' && isdigit( nch ))
                        {
                            hlp = d * 10 + nch - '0';
                            nch = (consumed++, *str++);
                            if (width > 0) width--;
                            if(d > (ULONGLONG)-1/10 || hlp < d)
                                break;

                            d = hlp;
                            exp--;
                        }
                        while (width != 0 && nch != '\0' && isdigit( nch ))
                        {
                            nch = (consumed++, *str++);
                            if (width > 0) width--;
                        }
                    }

                    /* handle exponent */
                    if (width != 0 && (nch == 'e' || nch == 'E'))
                    {
                        int sign = 1, e = 0;

                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        if (width != 0 && (nch == '+' || nch == '-'))
                        {
                            if(nch == '-')
                                sign = -1;
                            nch = (consumed++, *str++);
                            if (width > 0) width--;
                        }

                        /* exponent digits */
                        while (width != 0 && nch != '\0' && isdigit( nch ))
                        {
                            if (e > INT_MAX/10 || (e = e * 10 + nch - '0') < 0)
                                e = INT_MAX;
                            nch = (consumed++, *str++);
                            if (width > 0) width--;
                        }
                        e *= sign;

                        if(exp < 0 && e < 0 && e+exp > 0) exp = INT_MIN;
                        else if(exp > 0 && e > 0 && e+exp < 0) exp = INT_MAX;
                        else exp += e;
                    }

                    /*fpcontrol = _control87(0, 0);
                    _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
                            |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);*/

                    negexp = (exp < 0);
                    if (negexp)
                        exp = -exp;
                    /* update 'cur' with this exponent. */
                    while (exp)
                    {
                        if(exp & 1)
                            cur *= expcnt;
                        exp /= 2;
                        expcnt = expcnt*expcnt;
                    }
                    cur = (negexp ? d/cur : d*cur);

                    /*_control87(fpcontrol, 0xffffffff);*/

                    st = 1;
                    if (!suppress)
                    {
                        if (L_prefix || l_prefix) _SET_NUMBER_( double );
                        else _SET_NUMBER_( float );
                    }
                }
                break;
                /* According to msdn,
                 * 's' reads a character string in a call to fscanf
                 * and 'S' a wide character string and vice versa in a
                 * call to fwscanf. The 'h', 'w' and 'l' prefixes override
                 * this behaviour. 'h' forces reading char * but 'l' and 'w'
                 * force reading WCHAR. */
            case 's':
                if (w_prefix || l_prefix) goto widecharstring;
                else if (h_prefix) goto charstring;
                else goto charstring;
            case 'S':
                if (w_prefix || l_prefix) goto widecharstring;
                else if (h_prefix) goto charstring;
                else goto widecharstring;
            charstring:
                { /* read a word into a char */
                    char *sptr = suppress ? NULL : va_arg( ap, char * );
                    char *sptr_beg = sptr;
                    unsigned size = UINT_MAX;
                    /* skip initial whitespace */
                    while (nch != '\0' && isspace( nch ))
                        nch = (consumed++, *str++);
                    /* read until whitespace */
                    while (width != 0 && nch != '\0' && !isspace( nch ))
                    {
                        if (!suppress)
                        {
                            *sptr++ = nch;
                            if(size > 1) size--;
                            else
                            {
                                *sptr_beg = 0;
                                return rd;
                            }
                        }
                        st++;
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                    }
                    /* terminate */
                    if (st && !suppress) *sptr = 0;
                }
                break;
            widecharstring:
                { /* read a word into a WCHAR * */
                    WCHAR *sptr = suppress ? NULL : va_arg( ap, WCHAR * );
                    WCHAR *sptr_beg = sptr;
                    unsigned size = UINT_MAX;
                    /* skip initial whitespace */
                    while (nch != '\0' && isspace( nch ))
                        nch = (consumed++, *str++);
                    /* read until whitespace */
                    while (width != 0 && nch != '\0' && !isspace( nch ))
                    {
                        if (!suppress)
                        {
                            *sptr++ = nch;
                            if (size > 1) size--;
                            else
                            {
                                *sptr_beg = 0;
                                return rd;
                            }
                        }
                        st++;
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                    }
                    /* terminate */
                    if (st && !suppress) *sptr = 0;
                }
                break;
            /* 'c' and 'C work analogously to 's' and 'S' as described
             * above */
            case 'c':
                if (w_prefix || l_prefix) goto widecharacter;
                else if (h_prefix) goto character;
                else goto character;
            case 'C':
                if (w_prefix || l_prefix) goto widecharacter;
                else if (h_prefix) goto character;
                else goto widecharacter;
            character:
                { /* read single character into char */
                    char *str = suppress ? NULL : va_arg( ap, char * );
                    char *pstr = str;
                    unsigned size = UINT_MAX;
                    if (width == -1) width = 1;
                    while (width && nch != '\0')
                    {
                        if (!suppress)
                        {
                            *str++ = nch;
                            if(size) size--;
                            else
                            {
                                *pstr = 0;
                                return rd;
                            }
                        }
                        st++;
                        width--;
                        nch = (consumed++, *str++);
                    }
                }
                break;
            widecharacter:
                { /* read single character into a WCHAR */
                    WCHAR *str = suppress ? NULL : va_arg( ap, WCHAR * );
                    WCHAR *pstr = str;
                    unsigned size = UINT_MAX;
                    if (width == -1) width = 1;
                    while (width && nch != '\0')
                    {
                        if (!suppress)
                        {
                            *str++ = nch;
                            if (size) size--;
                            else
                            {
                                *pstr = 0;
                                return rd;
                            }
                        }
                        st++;
                        width--;
                        nch = (consumed++, *str++);
                    }
                }
                break;
            case 'n':
                {
                    if (!suppress)
                    {
                        int *n = va_arg( ap, int * );
                        *n = consumed - 1;
                    }
                    /* This is an odd one: according to the standard,
                     * "Execution of a %n directive does not increment the
                     * assignment count returned at the completion of
                     * execution" even if it wasn't suppressed with the
                     * '*' flag.  The Corrigendum to the standard seems
                     * to contradict this (comment out the assignment to
                     * suppress below if you want to implement these
                     * alternate semantics) but the windows program I'm
                     * looking at expects the behavior I've coded here
                     * (which happens to be what glibc does as well).
                     */
                    suppress = TRUE;
                    st = 1;
                }
                break;
            case '[':
                {
                    char *str = suppress ? NULL : va_arg( ap, char * );
                    char *sptr = str;
                    RTL_BITMAP bitMask;
                    ULONG Mask[8];
                    BOOLEAN invert = FALSE; /* Set if we are NOT to find the chars */
                    unsigned size = UINT_MAX;

                    RtlInitializeBitMap( &bitMask, Mask, sizeof(Mask) * 8 );

                    /* Read the format */
                    format++;
                    if (*format == '^')
                    {
                        invert = TRUE;
                        format++;
                    }
                    if (*format == ']')
                    {
                        RtlSetBits( &bitMask, ']', 1 );
                        format++;
                    }
                    while (*format && (*format != ']'))
                    {
                        /* According to msdn:
                         * "Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]." */
                        if ((*format == '-') && (*(format + 1) != ']'))
                        {
                            if ((*(format - 1)) < *(format + 1))
                                RtlSetBits( &bitMask, *(format - 1) +1 , *(format + 1) - *(format - 1) );
                            else
                                RtlSetBits( &bitMask, *(format + 1)    , *(format - 1) - *(format + 1) );
                            format++;
                        }
                        else
                            RtlSetBits( &bitMask, *format, 1 );
                        format++;
                    }
                    /* read until char is not suitable */
                    while (width != 0 && nch != '\0')
                    {
                        if (!invert)
                        {
                            if(RtlAreBitsSet( &bitMask, nch, 1 ))
                            {
                                if (!suppress) *sptr++ = nch;
                            }
                            else
                                break;
                        }
                        else
                        {
                            if (RtlAreBitsClear( &bitMask, nch, 1 ))
                            {
                                if (!suppress) *sptr++ = nch;
                            }
                            else
                                break;
                        }
                        st++;
                        nch = (consumed++, *str++);
                        if (width > 0) width--;
                        if(size > 1) size--;
                        else
                        {
                            *str = 0;
                            return rd;
                        }
                    }
                    /* terminate */
                    if (!suppress) *sptr = 0;
                }
                break;
            default:
                /* From spec: "if a percent sign is followed by a character
                 * that has no meaning as a format-control character, that
                 * character and the following characters are treated as
                 * an ordinary sequence of characters, that is, a sequence
                 * of characters that must match the input.  For example,
                 * to specify that a percent-sign character is to be input,
                 * use %%." */
                while (nch != '\0' && isspace( nch ))
                    nch = (consumed++, *str++);
                if (nch == *format)
                {
                    suppress = TRUE; /* whoops no field to be read */
                    st = 1; /* but we got what we expected */
                    nch = (consumed++, *str++);
                }
                break;
            }
            if (st && !suppress) rd++;
            else if (!st) break;
        }
        /* A non-white-space character causes scanf to read, but not store,
         * a matching non-white-space character. */
        else
        {
            if (nch == *format)
                nch = (consumed++, *str++);
            else break;
        }
        format++;
    }
    if (nch != '\0')
    {
        consumed--, str--;
    }

    return rd;
}


1363 1364 1365 1366 1367 1368
/*********************************************************************
 *                  sscanf   (NTDLL.@)
 */
int __cdecl NTDLL_sscanf( const char *str, const char *format, ... )
{
    int ret;
1369 1370 1371 1372
    __ms_va_list valist;
    __ms_va_start( valist, format );
    ret = NTDLL_vsscanf( str, format, valist );
    __ms_va_end( valist );
1373 1374 1375 1376
    return ret;
}


1377
/*********************************************************************
1378
 *		_splitpath (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
1379 1380 1381 1382 1383 1384 1385 1386 1387
 *
 * Split a path into its component pieces.
 *
 * PARAMS
 *  inpath [I] Path to split
 *  drv    [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
 *  dir    [O] Destination for directory component. Should be at least MAX_PATH characters.
 *  fname  [O] Destination for File name component. Should be at least MAX_PATH characters.
 *  ext    [O] Destination for file extension component. Should be at least MAX_PATH characters.
1388 1389 1390
 *
 * RETURNS
 *  Nothing.
1391 1392 1393 1394
 */
void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
                        char* fname, char * ext )
{
1395 1396 1397
    const char *p, *end;

    if (inpath[0] && inpath[1] == ':')
1398
    {
1399 1400 1401 1402 1403 1404 1405
        if (drv)
        {
            drv[0] = inpath[0];
            drv[1] = inpath[1];
            drv[2] = 0;
        }
        inpath += 2;
1406
    }
1407 1408 1409 1410 1411 1412 1413
    else if (drv) drv[0] = 0;

    /* look for end of directory part */
    end = NULL;
    for (p = inpath; *p; p++) if (*p == '/' || *p == '\\') end = p + 1;

    if (end)  /* got a directory */
1414
    {
1415 1416 1417 1418 1419 1420
        if (dir)
        {
            memcpy( dir, inpath, end - inpath );
            dir[end - inpath] = 0;
        }
        inpath = end;
1421
    }
1422 1423
    else if (dir) dir[0] = 0;

1424 1425 1426 1427 1428
    /* look for extension: what's after the last dot */
    end = NULL;
    for (p = inpath; *p; p++) if (*p == '.') end = p;

    if (!end) end = p; /* there's no extension */
1429 1430 1431

    if (fname)
    {
1432 1433
        memcpy( fname, inpath, end - inpath );
        fname[end - inpath] = 0;
1434
    }
1435
    if (ext) strcpy( ext, end );
1436
}