string.c 32.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2019 Nikolay Sivov for CodeWeavers
 *
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "shlwapi.h"
25
#include "winternl.h"
26

27
#include "kernelbase.h"
28
#include "wine/debug.h"
29
#include "wine/exception.h"
30 31 32

WINE_DEFAULT_DEBUG_CHANNEL(string);

33 34 35 36
#define isxdigit(ch) (((ch) >= '0' && (ch) <= '9') || \
                      ((ch) >= 'A' && (ch) <= 'F') || \
                      ((ch) >= 'a' && (ch) <= 'f'))

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
static BOOL char_compare(WORD ch1, WORD ch2, DWORD flags)
{
    char str1[3], str2[3];

    str1[0] = LOBYTE(ch1);
    if (IsDBCSLeadByte(str1[0]))
    {
        str1[1] = HIBYTE(ch1);
        str1[2] = '\0';
    }
    else
        str1[1] = '\0';

    str2[0] = LOBYTE(ch2);
    if (IsDBCSLeadByte(str2[0]))
    {
        str2[1] = HIBYTE(ch2);
        str2[2] = '\0';
    }
    else
        str2[1] = '\0';

    return CompareStringA(GetThreadLocale(), flags, str1, -1, str2, -1) - CSTR_EQUAL;
}

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
int WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
{
    if (!str1 && !str2) return 0;
    if (!str1) return -1;
    if (!str2) return 1;
    return CompareStringA( GetThreadLocale(), LOCALE_USE_CP_ACP, str1, -1, str2, -1 ) - 2;
}

int WINAPI lstrcmpW(LPCWSTR str1, LPCWSTR str2)
{
    if (!str1 && !str2) return 0;
    if (!str1) return -1;
    if (!str2) return 1;
    return CompareStringW( GetThreadLocale(), 0, str1, -1, str2, -1 ) - 2;
}

int WINAPI lstrcmpiA(LPCSTR str1, LPCSTR str2)
{
    if (!str1 && !str2) return 0;
    if (!str1) return -1;
    if (!str2) return 1;
    return CompareStringA( GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, str1, -1, str2, -1 ) - 2;
}

int WINAPI lstrcmpiW(LPCWSTR str1, LPCWSTR str2)
{
    if (!str1 && !str2) return 0;
    if (!str1) return -1;
    if (!str2) return 1;
    return CompareStringW( GetThreadLocale(), NORM_IGNORECASE, str1, -1, str2, -1 ) - 2;
}

LPSTR WINAPI KERNELBASE_lstrcpynA( LPSTR dst, LPCSTR src, INT n )
{
    /* Note: this function differs from the UNIX strncpy, it _always_ writes
     * a terminating \0.
     *
     * Note: n is an INT but Windows treats it as unsigned, and will happily
     * copy a gazillion chars if n is negative.
     */
    __TRY
    {
        LPSTR d = dst;
        LPCSTR s = src;
        UINT count = n;

        while ((count > 1) && *s)
        {
            count--;
            *d++ = *s++;
        }
        if (count) *d = 0;
    }
    __EXCEPT_PAGE_FAULT
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }
    __ENDTRY
    return dst;
}

LPWSTR WINAPI KERNELBASE_lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
{
    /* Note: this function differs from the UNIX strncpy, it _always_ writes
     * a terminating \0
     *
     * Note: n is an INT but Windows treats it as unsigned, and will happily
     * copy a gazillion chars if n is negative.
     */
    __TRY
    {
        LPWSTR d = dst;
        LPCWSTR s = src;
        UINT count = n;

        while ((count > 1) && *s)
        {
            count--;
            *d++ = *s++;
        }
        if (count) *d = 0;
    }
    __EXCEPT_PAGE_FAULT
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }
    __ENDTRY
    return dst;
}

INT WINAPI KERNELBASE_lstrlenA( LPCSTR str )
{
    INT ret;
    __TRY
    {
        ret = strlen(str);
    }
    __EXCEPT_PAGE_FAULT
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }
    __ENDTRY
    return ret;
}

INT WINAPI KERNELBASE_lstrlenW( LPCWSTR str )
{
    INT ret;
    __TRY
    {
        ret = wcslen(str);
    }
    __EXCEPT_PAGE_FAULT
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return 0;
    }
    __ENDTRY
    return ret;
}

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 211 212 213 214 215
DWORD WINAPI StrCmpCA(const char *str, const char *cmp)
{
    return lstrcmpA(str, cmp);
}

DWORD WINAPI StrCmpCW(const WCHAR *str, const WCHAR *cmp)
{
    return lstrcmpW(str, cmp);
}

DWORD WINAPI StrCmpICA(const char *str, const char *cmp)
{
    return lstrcmpiA(str, cmp);
}

DWORD WINAPI StrCmpICW(const WCHAR *str, const WCHAR *cmp)
{
    return lstrcmpiW(str, cmp);
}

DWORD WINAPI StrCmpNICA(const char *str, const char *cmp, DWORD len)
{
    return StrCmpNIA(str, cmp, len);
}

DWORD WINAPI StrCmpNICW(const WCHAR *str, const WCHAR *cmp, DWORD len)
{
    return StrCmpNIW(str, cmp, len);
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
char * WINAPI StrChrA(const char *str, WORD ch)
{
    TRACE("%s, %#x\n", wine_dbgstr_a(str), ch);

    if (!str)
        return NULL;

    while (*str)
    {
        if (!char_compare(*str, ch, 0))
            return (char *)str;
        str = CharNextA(str);
    }

    return NULL;
}

233 234 235 236 237 238 239
WCHAR * WINAPI StrChrW(const WCHAR *str, WCHAR ch)
{
    TRACE("%s, %#x\n", wine_dbgstr_w(str), ch);

    if (!str)
        return NULL;

240
    return wcschr(str, ch);
241 242
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
char * WINAPI StrChrIA(const char *str, WORD ch)
{
    TRACE("%s, %i\n", wine_dbgstr_a(str), ch);

    if (!str)
        return NULL;

    while (*str)
    {
        if (!ChrCmpIA(*str, ch))
            return (char *)str;
        str = CharNextA(str);
    }

    return NULL;
}

260 261 262 263 264 265 266
WCHAR * WINAPI StrChrIW(const WCHAR *str, WCHAR ch)
{
    TRACE("%s, %#x\n", wine_dbgstr_w(str), ch);

    if (!str)
        return NULL;

267
    ch = towupper(ch);
268 269
    while (*str)
    {
270
        if (towupper(*str) == ch)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
            return (WCHAR *)str;
        str++;
    }
    str = NULL;

    return (WCHAR *)str;
}

WCHAR * WINAPI StrChrNW(const WCHAR *str, WCHAR ch, UINT max_len)
{
    TRACE("%s, %#x, %u\n", wine_dbgstr_wn(str, max_len), ch, max_len);

    if (!str)
        return NULL;

    while (*str && max_len-- > 0)
    {
        if (*str == ch)
            return (WCHAR *)str;
        str++;
    }

    return NULL;
}

char * WINAPI StrDupA(const char *str)
{
    unsigned int len;
    char *ret;

    TRACE("%s\n", wine_dbgstr_a(str));

    len = str ? strlen(str) + 1 : 1;
    ret = LocalAlloc(LMEM_FIXED, len);

    if (ret)
    {
        if (str)
            memcpy(ret, str, len);
        else
            *ret = '\0';
    }

    return ret;
}

WCHAR * WINAPI StrDupW(const WCHAR *str)
{
    unsigned int len;
    WCHAR *ret;

    TRACE("%s\n", wine_dbgstr_w(str));

324
    len = (str ? lstrlenW(str) + 1 : 1) * sizeof(WCHAR);
325 326 327 328 329 330 331 332 333 334 335 336 337
    ret = LocalAlloc(LMEM_FIXED, len);

    if (ret)
    {
        if (str)
            memcpy(ret, str, len);
        else
            *ret = '\0';
    }

    return ret;
}

338 339 340 341 342 343 344
BOOL WINAPI ChrCmpIA(WORD ch1, WORD ch2)
{
    TRACE("%#x, %#x\n", ch1, ch2);

    return char_compare(ch1, ch2, NORM_IGNORECASE);
}

345 346 347 348 349
BOOL WINAPI ChrCmpIW(WCHAR ch1, WCHAR ch2)
{
    return CompareStringW(GetThreadLocale(), NORM_IGNORECASE, &ch1, 1, &ch2, 1) - CSTR_EQUAL;
}

350
char * WINAPI StrStrA(const char *str, const char *search)
351 352 353 354
{
    const char *end;
    size_t len;

355 356 357
    TRACE("%s, %s\n", wine_dbgstr_a(str), wine_dbgstr_a(search));

    if (!str || !search || !*search) return NULL;
358 359 360 361 362 363

    len = strlen(search);
    end = str + strlen(str);

    while (str + len <= end)
    {
364
        if (!StrCmpNA(str, search, len)) return (char *)str;
365 366 367 368 369
        str = CharNextA(str);
    }
    return NULL;
}

370 371 372 373 374 375 376
WCHAR * WINAPI StrStrW(const WCHAR *str, const WCHAR *search)
{
    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(search));

    if (!str || !search || !*search)
        return NULL;

377
    return wcsstr(str, search);
378 379 380 381 382 383 384 385 386 387 388
}

WCHAR * WINAPI StrStrNW(const WCHAR *str, const WCHAR *search, UINT max_len)
{
    unsigned int i, len;

    TRACE("%s, %s, %u\n", wine_dbgstr_w(str), wine_dbgstr_w(search), max_len);

    if (!str || !search || !*search || !max_len)
        return NULL;

389
    len = lstrlenW(search);
390 391 392

    for (i = max_len; *str && (i > 0); i--, str++)
    {
393
        if (!wcsncmp(str, search, len))
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
            return (WCHAR *)str;
    }

    return NULL;
}

int WINAPI StrCmpNIA(const char *str, const char *cmp, int len)
{
    TRACE("%s, %s, %i\n", wine_dbgstr_a(str), wine_dbgstr_a(cmp), len);
    return CompareStringA(GetThreadLocale(), NORM_IGNORECASE, str, len, cmp, len) - CSTR_EQUAL;
}

WCHAR * WINAPI StrStrNIW(const WCHAR *str, const WCHAR *search, UINT max_len)
{
    unsigned int i, len;

    TRACE("%s, %s, %u\n", wine_dbgstr_w(str), wine_dbgstr_w(search), max_len);

    if (!str || !search || !*search || !max_len)
        return NULL;

415
    len = lstrlenW(search);
416 417 418

    for (i = max_len; *str && (i > 0); i--, str++)
    {
419
        if (!StrCmpNIW(str, search, len))
420 421 422 423 424 425
            return (WCHAR *)str;
    }

    return NULL;
}

426 427 428 429 430 431
int WINAPI StrCmpNA(const char *str, const char *comp, int len)
{
    TRACE("%s, %s, %i\n", wine_dbgstr_a(str), wine_dbgstr_a(comp), len);
    return CompareStringA(GetThreadLocale(), 0, str, len, comp, len) - CSTR_EQUAL;
}

432 433 434 435 436 437
int WINAPI StrCmpNW(const WCHAR *str, const WCHAR *comp, int len)
{
    TRACE("%s, %s, %i\n", wine_dbgstr_w(str), wine_dbgstr_w(comp), len);
    return CompareStringW(GetThreadLocale(), 0, str, len, comp, len) - CSTR_EQUAL;
}

438 439 440 441 442 443 444 445 446 447
DWORD WINAPI StrCmpNCA(const char *str, const char *comp, int len)
{
    return StrCmpNA(str, comp, len);
}

DWORD WINAPI StrCmpNCW(const WCHAR *str, const WCHAR *comp, int len)
{
    return StrCmpNW(str, comp, len);
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
int WINAPI StrCmpNIW(const WCHAR *str, const WCHAR *comp, int len)
{
    TRACE("%s, %s, %i\n", wine_dbgstr_w(str), wine_dbgstr_w(comp), len);
    return CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str, len, comp, len) - CSTR_EQUAL;
}

int WINAPI StrCmpW(const WCHAR *str, const WCHAR *comp)
{
    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(comp));
    return CompareStringW(GetThreadLocale(), 0, str, -1, comp, -1) - CSTR_EQUAL;
}

int WINAPI StrCmpIW(const WCHAR *str, const WCHAR *comp)
{
    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(comp));
    return CompareStringW(GetThreadLocale(), NORM_IGNORECASE, str, -1, comp, -1) - CSTR_EQUAL;
}

WCHAR * WINAPI StrCpyNW(WCHAR *dst, const WCHAR *src, int count)
{
    const WCHAR *s = src;
    WCHAR *d = dst;

    TRACE("%p, %s, %i\n", dst, wine_dbgstr_w(src), count);

    if (s)
    {
        while ((count > 1) && *s)
        {
            count--;
            *d++ = *s++;
        }
    }
    if (count) *d = 0;

    return dst;
}

486 487
char * WINAPI StrStrIA(const char *str, const char *search)
{
488 489 490
    const char *end;
    size_t len;

491 492
    TRACE("%s, %s\n", wine_dbgstr_a(str), debugstr_a(search));

493 494 495 496 497 498 499 500 501 502 503
    if (!str || !search || !*search) return NULL;

    len = strlen(search);
    end = str + strlen(str);

    while (str + len <= end)
    {
        if (!StrCmpNIA(str, search, len)) return (char *)str;
        str = CharNextA(str);
    }
    return NULL;
504 505
}

506 507 508 509 510 511 512 513 514 515
WCHAR * WINAPI StrStrIW(const WCHAR *str, const WCHAR *search)
{
    unsigned int len;
    const WCHAR *end;

    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(search));

    if (!str || !search || !*search)
        return NULL;

516 517
    len = lstrlenW(search);
    end = str + lstrlenW(str);
518 519 520 521 522 523 524 525 526 527 528

    while (str + len <= end)
    {
        if (!StrCmpNIW(str, search, len))
            return (WCHAR *)str;
        str++;
    }

    return NULL;
}

529
int WINAPI StrSpnA(const char *str, const char *match)
530 531 532
{
    const char *ptr = str;

533 534 535
    TRACE("%s, %s\n", wine_dbgstr_a(str), wine_dbgstr_a(match));

    if (!str || !match) return 0;
536 537 538

    while (*ptr)
    {
539
        if (!StrChrA(match, *ptr)) break;
540
        ptr = CharNextA(ptr);
541
    }
542 543 544
    return ptr - str;
}

545 546 547
int WINAPI StrSpnW(const WCHAR *str, const WCHAR *match)
{
    if (!str || !match) return 0;
548
    return wcsspn(str, match);
549 550
}

551 552
int WINAPI StrCSpnA(const char *str, const char *match)
{
553 554
    const char *ptr = str;

555 556
    TRACE("%s, %s\n", wine_dbgstr_a(str), wine_dbgstr_a(match));

557 558 559 560 561 562 563 564
    if (!str || !match) return 0;

    while (*ptr)
    {
        if (StrChrA(match, *ptr)) break;
        ptr = CharNextA(ptr);
    }
    return ptr - str;
565 566 567 568 569 570 571
}

int WINAPI StrCSpnW(const WCHAR *str, const WCHAR *match)
{
    if (!str || !match)
        return 0;

572
    return wcscspn(str, match);
573 574 575 576
}

int WINAPI StrCSpnIA(const char *str, const char *match)
{
577 578
    const char *ptr = str;

579 580
    TRACE("%s, %s\n", wine_dbgstr_a(str), wine_dbgstr_a(match));

581 582 583 584 585 586 587 588
    if (!str || !match) return 0;

    while (*ptr)
    {
        if (StrChrIA(match, *ptr)) break;
        ptr = CharNextA(ptr);
    }
    return ptr - str;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
}

int WINAPI StrCSpnIW(const WCHAR *str, const WCHAR *match)
{
    const WCHAR *ptr = str;

    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(match));

    if (!str || !*str || !match)
        return 0;

    while (*ptr)
    {
        if (StrChrIW(match, *ptr)) break;
        ptr++;
    }

    return ptr - str;
}

609
char * WINAPI StrRChrA(const char *str, const char *end, WORD ch)
610 611 612
{
    const char *ret = NULL;

613
    TRACE("%s, %s, %#x\n", wine_dbgstr_a(str), wine_dbgstr_a(end), ch);
614

615 616
    if (!str) return NULL;
    if (!end) end = str + lstrlenA(str);
617 618
    while (*str && str <= end)
    {
619 620
        WORD ch2 = IsDBCSLeadByte(*str) ? *str << 8 | str[1] : *str;
        if (!char_compare(ch, ch2, 0)) ret = str;
621 622 623 624 625
        str = CharNextA(str);
    }
    return (char *)ret;
}

626 627 628 629 630
WCHAR * WINAPI StrRChrW(const WCHAR *str, const WCHAR *end, WORD ch)
{
    WCHAR *ret = NULL;

    if (!str) return NULL;
631
    if (!end) end = str + lstrlenW(str);
632 633 634 635 636 637 638 639
    while (str < end)
    {
        if (*str == ch) ret = (WCHAR *)str;
        str++;
    }
    return ret;
}

640 641
char * WINAPI StrRChrIA(const char *str, const char *end, WORD ch)
{
642 643
    const char *ret = NULL;

644 645
    TRACE("%s, %s, %#x\n", wine_dbgstr_a(str), wine_dbgstr_a(end), ch);

646 647 648 649 650 651 652 653 654 655
    if (!str) return NULL;
    if (!end) end = str + lstrlenA(str);

    while (*str && str <= end)
    {
        WORD ch2 = IsDBCSLeadByte(*str) ? *str << 8 | str[1] : *str;
        if (!ChrCmpIA(ch, ch2)) ret = str;
        str = CharNextA(str);
    }
    return (char *)ret;
656 657
}

658 659 660 661 662
WCHAR * WINAPI StrRChrIW(const WCHAR *str, const WCHAR *end, WORD ch)
{
    WCHAR *ret = NULL;

    if (!str) return NULL;
663
    if (!end) end = str + lstrlenW(str);
664 665 666 667 668 669 670 671
    while (str < end)
    {
        if (!ChrCmpIW(*str, ch)) ret = (WCHAR *)str;
        str++;
    }
    return ret;
}

672 673 674 675 676 677 678 679 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 714 715 716 717 718
char * WINAPI StrRStrIA(const char *str, const char *end, const char *search)
{
    char *ret = NULL;
    WORD ch1, ch2;
    int len;

    TRACE("%s, %s\n", wine_dbgstr_a(str), wine_dbgstr_a(search));

    if (!str || !search || !*search)
        return NULL;

    if (IsDBCSLeadByte(*search))
        ch1 = *search << 8 | (UCHAR)search[1];
    else
        ch1 = *search;
    len = lstrlenA(search);

    if (!end)
        end = str + lstrlenA(str);
    else /* reproduce the broken behaviour on Windows */
        end += min(len - 1, lstrlenA(end));

    while (str + len <= end && *str)
    {
        ch2 = IsDBCSLeadByte(*str) ? *str << 8 | (UCHAR)str[1] : *str;
        if (!ChrCmpIA(ch1, ch2))
        {
            if (!StrCmpNIA(str, search, len))
                ret = (char *)str;
        }

        str = CharNextA(str);
    }

    return ret;
}

WCHAR * WINAPI StrRStrIW(const WCHAR *str, const WCHAR *end, const WCHAR *search)
{
    WCHAR *ret = NULL;
    int len;

    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(search));

    if (!str || !search || !*search)
        return NULL;

719
    len = lstrlenW(search);
720 721

    if (!end)
722
        end = str + lstrlenW(str);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
    else
        end += min(len - 1, lstrlenW(end));

    while (str + len <= end && *str)
    {
        if (!ChrCmpIW(*search, *str))
        {
            if (!StrCmpNIW(str, search, len))
                ret = (WCHAR *)str;
        }
        str++;
    }

    return ret;
}

739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
char * WINAPI StrPBrkA(const char *str, const char *match)
{
    TRACE("%s, %s\n", wine_dbgstr_a(str), wine_dbgstr_a(match));

    if (!str || !match || !*match)
        return NULL;

    while (*str)
    {
        if (StrChrA(match, *str))
            return (char *)str;
        str = CharNextA(str);
    }

    return NULL;
}

756 757 758
WCHAR * WINAPI StrPBrkW(const WCHAR *str, const WCHAR *match)
{
    if (!str || !match) return NULL;
759
    return wcspbrk(str, match);
760 761
}

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
BOOL WINAPI StrTrimA(char *str, const char *trim)
{
    unsigned int len;
    BOOL ret = FALSE;
    char *ptr = str;

    TRACE("%s, %s\n", debugstr_a(str), debugstr_a(trim));

    if (!str || !*str)
        return FALSE;

    while (*ptr && StrChrA(trim, *ptr))
        ptr = CharNextA(ptr); /* Skip leading matches */

    len = strlen(ptr);

    if (ptr != str)
    {
        memmove(str, ptr, len + 1);
        ret = TRUE;
    }

    if (len > 0)
    {
        ptr = str + len;
        while (StrChrA(trim, ptr[-1]))
            ptr = CharPrevA(str, ptr); /* Skip trailing matches */

        if (ptr != str + len)
        {
            *ptr = '\0';
            ret = TRUE;
        }
    }

    return ret;
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813
BOOL WINAPI StrTrimW(WCHAR *str, const WCHAR *trim)
{
    unsigned int len;
    WCHAR *ptr = str;
    BOOL ret = FALSE;

    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(trim));

    if (!str || !*str)
        return FALSE;

    while (*ptr && StrChrW(trim, *ptr))
        ptr++;

814
    len = lstrlenW(ptr);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

    if (ptr != str)
    {
        memmove(str, ptr, (len + 1) * sizeof(WCHAR));
        ret = TRUE;
    }

    if (len > 0)
    {
        ptr = str + len;
        while (StrChrW(trim, ptr[-1]))
            ptr--; /* Skip trailing matches */

        if (ptr != str + len)
        {
            *ptr = '\0';
            ret = TRUE;
        }
    }

    return ret;
}

838 839 840 841 842 843 844 845 846 847 848 849 850 851
BOOL WINAPI StrToInt64ExA(const char *str, DWORD flags, LONGLONG *ret)
{
    BOOL negative = FALSE;
    LONGLONG value = 0;

    TRACE("%s, %#x, %p\n", wine_dbgstr_a(str), flags, ret);

    if (!str || !ret)
        return FALSE;

    if (flags > STIF_SUPPORT_HEX)
        WARN("Unknown flags %#x\n", flags);

    /* Skip leading space, '+', '-' */
852
    while (*str == ' ' || *str == '\t' || *str == '\n') str++;
853 854 855 856 857 858 859 860 861

    if (*str == '-')
    {
        negative = TRUE;
        str++;
    }
    else if (*str == '+')
        str++;

862
    if (flags & STIF_SUPPORT_HEX && *str == '0' && (str[1] == 'x' || str[1] == 'X'))
863 864 865 866 867 868 869 870 871 872
    {
        /* Read hex number */
        str += 2;

        if (!isxdigit(*str))
            return FALSE;

        while (isxdigit(*str))
        {
            value *= 16;
873
            if (*str >= '0' && *str <= '9')
874
                value += (*str - '0');
875 876
            else if (*str >= 'A' && *str <= 'F')
                value += 10 + *str - 'A';
877
            else
878
                value += 10 + *str - 'a';
879 880 881 882 883 884 885 886
            str++;
        }

        *ret = value;
        return TRUE;
    }

    /* Read decimal number */
887
    if (*str < '0' || *str > '9')
888 889
        return FALSE;

890
    while (*str >= '0' && *str <= '9')
891 892 893 894 895 896 897 898 899 900
    {
        value *= 10;
        value += (*str - '0');
        str++;
    }

    *ret = negative ? -value : value;
    return TRUE;
}

901 902 903 904 905 906 907 908 909 910 911 912 913 914
BOOL WINAPI StrToInt64ExW(const WCHAR *str, DWORD flags, LONGLONG *ret)
{
    BOOL negative = FALSE;
    LONGLONG value = 0;

    TRACE("%s, %#x, %p\n", wine_dbgstr_w(str), flags, ret);

    if (!str || !ret)
        return FALSE;

    if (flags > STIF_SUPPORT_HEX)
        WARN("Unknown flags %#x.\n", flags);

    /* Skip leading space, '+', '-' */
915
    while (*str == ' ' || *str == '\t' || *str == '\n') str++;
916 917 918 919 920 921 922 923 924

    if (*str == '-')
    {
        negative = TRUE;
        str++;
    }
    else if (*str == '+')
        str++;

925
    if (flags & STIF_SUPPORT_HEX && *str == '0' && (str[1] == 'x' || str[1] == 'X'))
926 927 928 929
    {
        /* Read hex number */
        str += 2;

930
        if (!isxdigit(*str))
931 932
            return FALSE;

933
        while (isxdigit(*str))
934 935
        {
            value *= 16;
936
            if (*str >= '0' && *str <= '9')
937
                value += (*str - '0');
938 939
            else if (*str >= 'A' && *str <= 'Z')
                value += 10 + (*str - 'A');
940
            else
941
                value += 10 + (*str - 'a');
942 943 944 945 946 947 948 949
            str++;
        }

        *ret = value;
        return TRUE;
    }

    /* Read decimal number */
950
    if (*str < '0' || *str > '9')
951 952
        return FALSE;

953
    while (*str >= '0' && *str <= '9')
954 955 956 957 958 959 960 961 962 963
    {
        value *= 10;
        value += (*str - '0');
        str++;
    }

    *ret = negative ? -value : value;
    return TRUE;
}

964 965 966 967 968 969 970 971 972 973 974 975
BOOL WINAPI StrToIntExA(const char *str, DWORD flags, INT *ret)
{
    LONGLONG value;
    BOOL res;

    TRACE("%s, %#x, %p\n", wine_dbgstr_a(str), flags, ret);

    res = StrToInt64ExA(str, flags, &value);
    if (res) *ret = value;
    return res;
}

976 977 978 979 980 981 982 983 984 985 986 987
BOOL WINAPI StrToIntExW(const WCHAR *str, DWORD flags, INT *ret)
{
    LONGLONG value;
    BOOL res;

    TRACE("%s, %#x, %p\n", wine_dbgstr_w(str), flags, ret);

    res = StrToInt64ExW(str, flags, &value);
    if (res) *ret = value;
    return res;
}

988 989 990 991 992 993 994 995 996
int WINAPI StrToIntA(const char *str)
{
    int value = 0;

    TRACE("%s\n", wine_dbgstr_a(str));

    if (!str)
        return 0;

997
    if (*str == '-' || (*str >= '0' && *str <= '9'))
998 999 1000 1001 1002
        StrToIntExA(str, 0, &value);

    return value;
}

1003 1004 1005 1006 1007 1008 1009 1010 1011
int WINAPI StrToIntW(const WCHAR *str)
{
    int value = 0;

    TRACE("%s\n", wine_dbgstr_w(str));

    if (!str)
        return 0;

1012
    if (*str == '-' || (*str >= '0' && *str <= '9'))
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
        StrToIntExW(str, 0, &value);
    return value;
}

char * WINAPI StrCpyNXA(char *dst, const char *src, int len)
{
    TRACE("%p, %s, %i\n", dst, wine_dbgstr_a(src), len);

    if (dst && src && len > 0)
    {
        while ((len-- > 1) && *src)
            *dst++ = *src++;
        if (len >= 0)
            *dst = '\0';
    }

    return dst;
}

WCHAR * WINAPI StrCpyNXW(WCHAR *dst, const WCHAR *src, int len)
{
    TRACE("%p, %s, %i\n", dst, wine_dbgstr_w(src), len);

    if (dst && src && len > 0)
    {
        while ((len-- > 1) && *src)
            *dst++ = *src++;
        if (len >= 0)
            *dst = '\0';
    }

    return dst;
}
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

LPSTR WINAPI CharLowerA(char *str)
{
    if (IS_INTRESOURCE(str))
    {
        char ch = LOWORD(str);
        CharLowerBuffA( &ch, 1 );
        return (LPSTR)(UINT_PTR)(BYTE)ch;
    }

    __TRY
    {
        CharLowerBuffA( str, strlen(str) );
    }
    __EXCEPT_PAGE_FAULT
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return NULL;
    }
    __ENDTRY
    return str;
}

DWORD WINAPI CharLowerBuffA(char *str, DWORD len)
{
    DWORD lenW;
    WCHAR buffer[32];
    WCHAR *strW = buffer;

    if (!str) return 0; /* YES */

    lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
    if (lenW > ARRAY_SIZE(buffer))
    {
        strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
        if (!strW) return 0;
    }
    MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
    CharLowerBuffW(strW, lenW);
    len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
    if (strW != buffer) HeapFree(GetProcessHeap(), 0, strW);
    return len;
}

DWORD WINAPI CharLowerBuffW(WCHAR *str, DWORD len)
{
    if (!str) return 0; /* YES */
    return LCMapStringW(LOCALE_USER_DEFAULT, LCMAP_LOWERCASE, str, len, str, len);
}

LPWSTR WINAPI CharLowerW(WCHAR *str)
{
    if (!IS_INTRESOURCE(str))
    {
        CharLowerBuffW(str, lstrlenW(str));
        return str;
    }
    else
    {
        WCHAR ch = LOWORD(str);
        CharLowerBuffW(&ch, 1);
        return (LPWSTR)(UINT_PTR)ch;
    }
}

LPSTR WINAPI CharNextA(const char *ptr)
{
    if (!*ptr) return (LPSTR)ptr;
    if (IsDBCSLeadByte( ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
    return (LPSTR)(ptr + 1);
}

LPSTR WINAPI CharNextExA(WORD codepage, const char *ptr, DWORD flags)
{
    if (!*ptr) return (LPSTR)ptr;
    if (IsDBCSLeadByteEx( codepage, ptr[0] ) && ptr[1]) return (LPSTR)(ptr + 2);
    return (LPSTR)(ptr + 1);
}

LPWSTR WINAPI CharNextW(const WCHAR *x)
{
    if (*x) x++;

    return (WCHAR *)x;
}

LPSTR WINAPI CharPrevA(const char *start, const char *ptr)
{
    while (*start && (start < ptr))
    {
        LPCSTR next = CharNextA(start);
        if (next >= ptr) break;
        start = next;
    }
    return (LPSTR)start;
}

LPSTR WINAPI CharPrevExA(WORD codepage, const char *start, const char *ptr, DWORD flags)
{
    while (*start && (start < ptr))
    {
        LPCSTR next = CharNextExA(codepage, start, flags);
        if (next >= ptr) break;
        start = next;
    }
    return (LPSTR)start;
}

LPWSTR WINAPI CharPrevW(const WCHAR *start, const WCHAR *x)
{
    if (x > start) return (LPWSTR)(x - 1);
    else return (LPWSTR)x;
}

LPSTR WINAPI CharUpperA(LPSTR str)
{
    if (IS_INTRESOURCE(str))
    {
        char ch = LOWORD(str);
        CharUpperBuffA(&ch, 1);
        return (LPSTR)(UINT_PTR)(BYTE)ch;
    }

    __TRY
    {
        CharUpperBuffA(str, strlen(str));
    }
    __EXCEPT_PAGE_FAULT
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return NULL;
    }
    __ENDTRY
    return str;
}

DWORD WINAPI CharUpperBuffA(LPSTR str, DWORD len)
{
    DWORD lenW;
    WCHAR buffer[32];
    WCHAR *strW = buffer;

    if (!str) return 0; /* YES */

    lenW = MultiByteToWideChar(CP_ACP, 0, str, len, NULL, 0);
    if (lenW > ARRAY_SIZE(buffer))
    {
        strW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR));
        if (!strW) return 0;
    }
    MultiByteToWideChar(CP_ACP, 0, str, len, strW, lenW);
    CharUpperBuffW(strW, lenW);
    len = WideCharToMultiByte(CP_ACP, 0, strW, lenW, str, len, NULL, NULL);
    if (strW != buffer) HeapFree(GetProcessHeap(), 0, strW);
    return len;
}

DWORD WINAPI CharUpperBuffW(WCHAR *str, DWORD len)
{
    if (!str) return 0; /* YES */
    return LCMapStringW(LOCALE_USER_DEFAULT, LCMAP_UPPERCASE, str, len, str, len);
}

LPWSTR WINAPI CharUpperW(WCHAR *str)
{
    if (!IS_INTRESOURCE(str))
    {
        CharUpperBuffW(str, lstrlenW(str));
        return str;
    }
    else
    {
        WCHAR ch = LOWORD(str);
        CharUpperBuffW(&ch, 1);
        return (LPWSTR)(UINT_PTR)ch;
    }
}
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

INT WINAPI DECLSPEC_HOTPATCH LoadStringW(HINSTANCE instance, UINT resource_id, LPWSTR buffer, INT buflen)
{
    int string_num, i;
    HGLOBAL hmem;
    HRSRC hrsrc;
    WCHAR *p;

    TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n", instance, resource_id, buffer, buflen);

    if (!buffer)
        return 0;

    /* Use loword (incremented by 1) as resourceid */
    hrsrc = FindResourceW(instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1), (LPWSTR)RT_STRING);
    if (!hrsrc) return 0;
    hmem = LoadResource(instance, hrsrc);
    if (!hmem) return 0;

    p = LockResource(hmem);
    string_num = resource_id & 0x000f;
    for (i = 0; i < string_num; i++)
        p += *p + 1;

    TRACE("strlen = %d\n", (int)*p );

    /*if buflen == 0, then return a read-only pointer to the resource itself in buffer
    it is assumed that buffer is actually a (LPWSTR *) */
    if (buflen == 0)
    {
        *((LPWSTR *)buffer) = p + 1;
        return *p;
    }

    i = min(buflen - 1, *p);
    if (i > 0)
    {
        memcpy(buffer, p + 1, i * sizeof (WCHAR));
        buffer[i] = 0;
    }
    else
    {
        if (buflen > 1)
        {
            buffer[0] = 0;
            return 0;
        }
    }

    TRACE("returning %s\n", debugstr_w(buffer));
    return i;
}

INT WINAPI DECLSPEC_HOTPATCH LoadStringA(HINSTANCE instance, UINT resource_id, LPSTR buffer, INT buflen)
{
    DWORD retval = 0;
    HGLOBAL hmem;
    HRSRC hrsrc;

    TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n", instance, resource_id, buffer, buflen);

    if (!buflen) return -1;

    /* Use loword (incremented by 1) as resourceid */
    if ((hrsrc = FindResourceW(instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1), (LPWSTR)RT_STRING )) &&
            (hmem = LoadResource(instance, hrsrc)))
    {
        const WCHAR *p = LockResource(hmem);
        unsigned int id = resource_id & 0x000f;

        while (id--) p += *p + 1;

1295
        RtlUnicodeToMultiByteN(buffer, buflen - 1, &retval, p + 1, *p * sizeof(WCHAR));
1296 1297 1298 1299 1300
    }
    buffer[retval] = 0;
    TRACE("returning %s\n", debugstr_a(buffer));
    return retval;
}
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312

int WINAPI StrCmpLogicalW(const WCHAR *str, const WCHAR *comp)
{
    TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(comp));

    if (!str || !comp)
        return 0;

    while (*str)
    {
        if (!*comp)
            return 1;
1313
        else if (*str >= '0' && *str <= '9')
1314 1315 1316
        {
            int str_value, comp_value;

1317
            if (*comp < '0' || *comp > '9')
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
                return -1;

            /* Compare the numbers */
            StrToIntExW(str, 0, &str_value);
            StrToIntExW(comp, 0, &comp_value);

            if (str_value < comp_value)
                return -1;
            else if (str_value > comp_value)
                return 1;

            /* Skip */
1330 1331
            while (*str >= '0' && *str <= '9') str++;
            while (*comp >= '0' && *comp <= '9') comp++;
1332
        }
1333
        else if (*comp >= '0' && *comp <= '9')
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 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
            return 1;
        else
        {
            int diff = ChrCmpIW(*str, *comp);
            if (diff > 0)
                return 1;
            else if (diff < 0)
                return -1;

            str++;
            comp++;
        }
    }

    if (*comp)
      return -1;

    return 0;
}

BOOL WINAPI StrIsIntlEqualA(BOOL case_sensitive, const char *str, const char *cmp, int len)
{
    DWORD flags;

    TRACE("%d, %s, %s, %d\n", case_sensitive, wine_dbgstr_a(str), wine_dbgstr_a(cmp), len);

    /* FIXME: This flag is undocumented and unknown by our CompareString.
     *        We need a define for it.
     */
    flags = 0x10000000;
    if (!case_sensitive)
        flags |= NORM_IGNORECASE;

    return (CompareStringA(GetThreadLocale(), flags, str, len, cmp, len) == CSTR_EQUAL);
}

BOOL WINAPI StrIsIntlEqualW(BOOL case_sensitive, const WCHAR *str, const WCHAR *cmp, int len)
{
    DWORD flags;

    TRACE("%d, %s, %s, %d\n", case_sensitive, debugstr_w(str), debugstr_w(cmp), len);

    /* FIXME: This flag is undocumented and unknown by our CompareString.
     *        We need a define for it.
     */
    flags = 0x10000000;
    if (!case_sensitive)
        flags |= NORM_IGNORECASE;

    return (CompareStringW(GetThreadLocale(), flags, str, len, cmp, len) == CSTR_EQUAL);
}
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411

char * WINAPI StrCatBuffA(char *str, const char *cat, INT max_len)
{
    INT len;

    TRACE("%p, %s, %d\n", str, wine_dbgstr_a(cat), max_len);

    if (!str)
        return NULL;

    len = strlen(str);
    max_len -= len;
    if (max_len > 0)
        StrCpyNA(str + len, cat, max_len);

    return str;
}

WCHAR * WINAPI StrCatBuffW(WCHAR *str, const WCHAR *cat, INT max_len)
{
    INT len;

    TRACE("%p, %s, %d\n", str, wine_dbgstr_w(cat), max_len);

    if (!str)
        return NULL;

1412
    len = lstrlenW(str);
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    max_len -= len;
    if (max_len > 0)
        StrCpyNW(str + len, cat, max_len);

    return str;
}

DWORD WINAPI StrCatChainW(WCHAR *str, DWORD max_len, DWORD at, const WCHAR *cat)
{
    TRACE("%s, %u, %d, %s\n", wine_dbgstr_w(str), max_len, at, wine_dbgstr_w(cat));

    if (at == -1)
1425
        at = lstrlenW(str);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

    if (!max_len)
        return at;

    if (at == max_len)
        at--;

    if (cat && at < max_len)
    {
        str += at;
        while (at < max_len - 1 && *cat)
        {
            *str++ = *cat++;
            at++;
        }
        *str = 0;
    }

    return at;
}

DWORD WINAPI SHTruncateString(char *str, DWORD size)
{
    char *last_byte;

    if (!str || !size)
        return 0;

    last_byte = str + size - 1;

    while (str < last_byte)
        str += IsDBCSLeadByte(*str) ? 2 : 1;

    if (str == last_byte && IsDBCSLeadByte(*str))
    {
        *str = '\0';
        size--;
    }

    return size;
}
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482

HRESULT WINAPI SHLoadIndirectString(const WCHAR *src, WCHAR *dst, UINT dst_len, void **reserved)
{
    WCHAR *dllname = NULL;
    HMODULE hmod = NULL;
    HRESULT hr = E_FAIL;

    TRACE("%s, %p, %#x, %p\n", debugstr_w(src), dst, dst_len, reserved);

    if (src[0] == '@')
    {
        WCHAR *index_str;
        int index;

        dst[0] = 0;
        dllname = StrDupW(src + 1);
1483
        index_str = wcschr(dllname, ',');
1484 1485 1486 1487 1488

        if(!index_str) goto end;

        *index_str = 0;
        index_str++;
1489
        index = wcstol(index_str, NULL, 10);
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514

        hmod = LoadLibraryW(dllname);
        if (!hmod) goto end;

        if (index < 0)
        {
            if (LoadStringW(hmod, -index, dst, dst_len))
                hr = S_OK;
        }
        else
            FIXME("can't handle non-negative indices (%d)\n", index);
    }
    else
    {
        if (dst != src)
            lstrcpynW(dst, src, dst_len);
        hr = S_OK;
    }

    TRACE("returning %s\n", debugstr_w(dst));
end:
    if (hmod) FreeLibrary(hmod);
    LocalFree(dllname);
    return hr;
}