0

I have this piece of code generated from Ghidra

void __cdecl FUNCTION(uint *key,uint *text)

{
  undefined uVar1;
  ushort uVar2;
  uint *puVar3;
  uint *puVar4;
  uint *puVar5;
  int iVar6;
  uint uVar7;
  uint *puVar8;
  uint uVar9;
  uint local_18c [11];
  uint *local_14;
  uint *local_10;
  uint local_c;

  puVar3 = key;
  uVar2 = 0;
  local_10 = (uint *)key[4];
  local_14 = (uint *)key[3];
  puVar5 = local_18c;
  uVar9 = key[1];
  puVar8 = (uint *)key[2];
  uVar7 = *key;
  do {
    local_c = uVar7;
    key = puVar8;
    puVar4 = local_14;
    if (uVar2 < 0x10) {
      puVar5[0xe] = *text;
      uVar1 = *(undefined *)((int)puVar5 + 0x3b);
      *(undefined *)((int)puVar5 + 0x3b) = *(undefined *)(puVar5 + 0xe);
      *(undefined *)(puVar5 + 0xe) = uVar1;
      uVar1 = *(undefined *)((int)puVar5 + 0x3a);
      *(undefined *)((int)puVar5 + 0x3a) = *(undefined *)((int)puVar5 + 0x39);
      *(undefined *)((int)puVar5 + 0x39) = uVar1;
    }
    else {
      uVar7 = puVar5[0xb] ^ puVar5[6] ^ puVar5[-2] ^ *puVar5;
      puVar5[0xe] = uVar7 >> 0x1f | uVar7 * 2;
    }
    if (uVar2 < 0x14) {
      iVar6 = (~uVar9 & (uint)local_14 | (uint)key & uVar9) + 0x5a827999;
    }
    else {
      if (uVar2 < 0x28) {
        iVar6 = ((uint)local_14 ^ (uint)key ^ uVar9) + 0x6ed9eba1;
      }
      else {
        if (uVar2 < 0x3c) {
          iVar6 = (((uint)key | uVar9) & (uint)local_14 | (uint)key & uVar9) + 0x8f1bbcdc;
        }
        else {
          iVar6 = ((uint)local_14 ^ (uint)key ^ uVar9) + 0xca62c1d6;
        }
      }
    }
    text = text + 1;
    puVar8 = puVar5 + 0xe;
    puVar5 = puVar5 + 1;
    uVar7 = (int)local_10 + iVar6 + (local_c >> 0x1b | local_c << 5) + *puVar8;
    puVar8 = (uint *)(uVar9 << 0x1e | uVar9 >> 2);
    uVar2 = uVar2 + 1;
    local_10 = local_14;
    local_14 = key;
    uVar9 = local_c;
  } while (uVar2 < 0x50);
  *puVar3 = *puVar3 + uVar7;
  puVar3[1] = puVar3[1] + local_c;
  puVar3[2] = puVar3[2] + (int)puVar8;
  puVar3[3] = puVar3[3] + (int)key;
  puVar3[4] = puVar3[4] + (int)puVar4;
  return;
}

I'm trying to translate it in Java or C code, but i don't understand how. there are some obscure points.

All variable are typed correctly, but the first uVar1 is "undefined", why?

The operator ~ how is it implemented?

1

From Ghidra help:

By default, the variables data type will be UndefinedN where N is the size (in bytes) of the stack reference.

undefined stands for undefined1 and it's a type of size 1(byte) in Ghidra.

If some variable is of this type, it basically means that decompiler didn't infer any "better" type for this variable (which could be bool or char for example), but it knows that it occupies 1 byte.

Regarding your second question, ~ is a bitwise NOT operation, just like in C.

Note1: If you are unsure what a particular operator means, just select it in decompiler view and it will show you the corresponding disassembly instruction.

Note2: The code you are analysing may be easier to read when you disable printing of type casts (Edit->Tool options...->Decompiler->Display->Disable printing of type casts).

Note3: Another option to consider is Use inplace assignment operations (under Decompiler->Analysis in options) to have uVar2 += 1 instead of uVar2 = uVar2 + 1 for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.