Rewrite of IDA decompiled function

627 Views Asked by At

I'm looking for help to rewrite this IDA decompiled function to C code.

int random_generated_number;
sub_8049A96(&random_generated_number, 11); //Passes address of random_generated_number and int 11

int __cdecl sub_8049A96(int a1, int a2)
{
  int result;
  int i;

  for ( i = 0; i < a2; ++i ) // loop 11 times
    *(i + a1) = byte_8049C4E[rand() % 10u]; // Cast byte pointer (i + a1) = select rand number between 0 to 9. I don't really understand what *(i + a1) is doing. could you explain?
  result = a2 + a1; Set result = 11? is this correct?
  *(a2 + a1) = 0; // What is this doing?
  return result; Returns 11?
}

So far, I have come up with this,

int test(int a1, int a2)
{
    int result;
    int i;

    char byte_8049C4E[48];

    for (i = 0; i < a2; ++i)
        *(i + a1) = byte_8049C4E[rand() % 10u];
    result = a2 + a1;
    *(a2 + a1) = 0;
    return result;
}

But upon compilation I get illegal indirection. Can anyone help? Also if anyone is able to give a detailed explanation of what the function is doing it would be much appreciated? I have in-lined my comments, would like to know if they are right or not.

1

There are 1 best solutions below

2
On

I suspect the function generates a string of random digits, and equates to something like this:

/* write a string of len digits to s, and return a pointer to
   the end of the string (for further appending) */
char *test(char *s, int len)
{
    char *digits = "0123456789";

    for (i = 0; i < len; i++)
        s[i] = digits[rand() % 10];
    s[i] = '\0';
    return &s[i];
}

..though digits could contain something else, depeding on what's stored in byte_08049C4E[]. If it really was passed a random address in the first parameter, it may crash the program and certainly wouldn't do anything useful.