protonscr

Duplicate code in dlls/ntdll/loader.c

wineclosed
ValveSoftware/wine#96 · opened 2020-07-25 by GloriousEggroll · updated 2021-01-25 · 3 comments · github
GGloriousEggroll 2020-07-25 github

This is the normal version used throughout loader.c which was added for the steamclient swap:

https://github.com/ValveSoftware/wine/blob/2409bd1f74be116172688a25df725290637c255a/dlls/ntdll/loader.c#L1618

static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
{
    while (*str)
    {
        const WCHAR *p1 = str, *p2 = sub;
        while (*p1 && *p2 && tolowerW(*p1) == tolowerW(*p2)) { p1++; p2++; }
        if (!*p2) return (WCHAR *)str;
        str++;
    }
    return NULL;
}

And this second one was added specifically for the AoE3 game check:

https://github.com/ValveSoftware/wine/blob/2409bd1f74be116172688a25df725290637c255a/dlls/ntdll/loader.c#L206

static WCHAR *strcasestrW( const WCHAR *str, const WCHAR *sub )
{
    while (*str)
    {
        const WCHAR *p1 = str, *p2 = sub;
        while (*p1 && *p2 && tolowerW(*p1) == tolowerW(*p2)) { p1++; p2++; }
        if (!*p2) return (WCHAR *)str;
        str++;
    }
    return NULL;
}

It's the exact same function with a different name.

Used specifically for:

https://github.com/ValveSoftware/wine/blob/2409bd1f74be116172688a25df725290637c255a/dlls/ntdll/loader.c#L3205

            strcasestrW( libname, mfc42W ))
            
            

So you can remove the duplicate function and change strcasestrW on that line to strstriW

Also for future proofing, you can use RtlDowncaseUnicodeChar in the function instead of tolowerW:

static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
{
    while (*str)
    {
        const WCHAR *p1 = str, *p2 = sub;
        while (*p1 && *p2 && RtlDowncaseUnicodeChar(*p1) == RtlDowncaseUnicodeChar(*p2)) { p1++; p2++; }
        if (!*p2) return (WCHAR *)str;
        str++;
    }
    return NULL;
}
Aaeikum 2020-07-27 github

Yep. I'm working on a rebase right now and I've already cleaned this up. I'm using tolower (i.e. ASCII-only, but I think that's OK) because this code runs very early, before we init the NLS data, so I think it will explode with towlower and RtlDowncaseUnicodeChar.

Edit: Actually reading the code, RtlDowncaseUnicodeChar may actually function before the NLS data is initted. Not sure.

Zzfigura 2020-07-27 github

RtlDowncaseUnicodeChar() does work before the NLS data is initialized, but I suggested tolower() instead since I don't think we want to be locale-sensitive here.

Aaeikum 2020-10-15 github

I think this is fixed in 5.13.

Nothing extracted yet.