Labels in C#

Posts 14 of 4 · Page 1 of 1
Labels in C#
So I'm reversing in IDA and came across a function what is a pain in the ass to follow logic on, so I'm going to put it in C# and test the code directly. I got a basic understanding of what it does from reading the references to the function in ida, but I want to confirm what I think it does. The function requires a label which doesn't work in C# for some odd reason.

Picture of the function:
https://i.gyazo.com/e11d376bc296dbea...383903fbc8.png

Error:
Error 2 No such label 'Label' within the scope of the goto statement


Code:
        unsafe static int sub_47A540(int a1, int a2, uint a3)
        {
            int v3; // ecx@1
            int v4; // edx@1
            uint v5; // esi@1
            int v6; // eax@5

            v3 = a2;
            v4 = a1;
            v5 = a3;
            if (a3 < 4)
            {
            LABEL_4:
                if (v5 == 0)
                    return 0;
            }
            else
            {
                while (*(uint*)v4 == *(uint*)v3)
                {
                    v5 -= 4;
                    v3 += 4;
                    v4 += 4;
                    if (v5 < 4)
                        goto LABEL_4;
                }
            }
            v6 = *(byte*)v4 - *(byte*)v3;
            if (*(byte*)v4 != *(byte*)v3)
                return (v6 >> 31) | 1;
            if (v5 <= 1)
                return 0;
            v6 = *(byte*)(v4 + 1) - *(byte*)(v3 + 1);
            if (*(byte*)(v4 + 1) != *(byte*)(v3 + 1))
                return (v6 >> 31) | 1;
            if (v5 <= 2)
                return 0;
            v6 = *(byte*)(v4 + 2) - *(byte*)(v3 + 2);
            if (*(byte*)(v4 + 2) != *(byte*)(v3 + 2))
                return (v6 >> 31) | 1;
            if (v5 > 3)
            {
                v6 = *(byte*)(v4 + 3) - *(byte*)(v3 + 3);
                return (v6 >> 31) | 1;
            }
            return 0;
        }
Any help will be appreciated.
Quote Originally Posted by Silent View Post

Error:
Error 2 No such label 'Label' within the scope of the goto statement
This is because LABEL_4 is defined within the scope of the (a3 < 4) if statement. the 'else' part is a different scope, so it doesn't know about LABEL_4.
Code:
if (a3 < 4)
{
    // Declaring LABEL_4 within this scope
    LABEL_4:
    if (v5 == 0)
        return 0;
}
else
{
    // This is a different scope.
    while (*(uint*)v4 == *(uint*)v3)
    {
         v5 -= 4;
         v3 += 4;
         v4 += 4;
         if (v5 < 4)
             goto LABEL_4; // LABEL_4 doesn't exist in this scope.
    }
}
Quote Originally Posted by Killer Be Killed View Post
This is because LABEL_4 is defined within the scope of the (a3 < 4) if statement. the 'else' part is a different scope, so it doesn't know about LABEL_4.
Code:
if (a3 < 4)
{
    // Declaring LABEL_4 within this scope
    LABEL_4:
    if (v5 == 0)
        return 0;
}
else
{
    // This is a different scope.
    while (*(uint*)v4 == *(uint*)v3)
    {
         v5 -= 4;
         v3 += 4;
         v4 += 4;
         if (v5 < 4)
             goto LABEL_4; // LABEL_4 doesn't exist in this scope.
    }
}
Ah, thanks for your response.
@Hugo Boss , solved.
Terrible cleanup

Code:
        unsafe static int compareFunc(IntPtr _arg1, IntPtr _arg2, uint _nLength)
        {
            IntPtr arg2; // ecx@1
            IntPtr arg1; // edx@1
            uint nLength; // esi@1

            arg2 = _arg2;
            arg1 = _arg1;
            nLength = _nLength;

            if (nLength >= 4)
            { 
                while (*(uint*)arg1 == *(uint*)arg2)
                {
                    nLength -= 4;
                    arg2 += 4;
                    arg1 += 4;
                    if (nLength < 4)
                        break;
                }
            }

            if (nLength == 0)
                return 0;

            if (*(byte*)arg1 != *(byte*)arg2)
                return 1;

            if (nLength <= 1)
                return 0;

            if (*(byte*)(arg1 + 1) != *(byte*)(arg2 + 1))
                return 1;

            if (nLength <= 2)
                return 0;

            if (*(byte*)(arg1 + 2) != *(byte*)(arg2 + 2))
                return 1;

            return nLength > 3 ? 1 : 0;
        }
Simplified:
Code:
        bool compareFunc(String a, String b)
        {
            int shortLength = a.Length < b.Length ? a.Length : b.Length;
            return a.Substring(0, shortLength).CompareTo(b.Substring(0, shortLength)) != 0;
        }

It's basically just comparing 4 bytes at a time until the length left to check is <= 3 characters, in which case it checks per byte to see if they're equal. if all the bytes are equal it returns 0, if they're not it returns 1.

Feel free to PM me for my skype if you need anything else.
Posts 14 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?