D
D
Dmitry Alexandrov2016-09-14 12:18:31
Java
Dmitry Alexandrov, 2016-09-14 12:18:31

Help explaining the function's logic?

I'm trying to understand the logic of the hash getting code.

int __fastcall mt::String::getHashCode(const char *a1)
{
  const char *originalName; // [email protected]
  size_t nameLenght; // [email protected]
  int originalNamePtr; // [email protected]
  unsigned int nameLenghtTMP; // [email protected]
  int result; // [email protected]
  int v6; // [email protected]

  originalName = a1;
  nameLenght = strlen(a1);
  originalNamePtr = *originalName;
  if ( *originalName )
  {
    nameLenghtTMP = nameLenght;
    result = 0;
    do
    {
      result += originalNamePtr * nameLenghtTMP;
      v6 = (originalName++)[1];
      originalNamePtr = v6;
      nameLenghtTMP = (nameLenghtTMP >> 16) + 18000 * (unsigned __int16)nameLenghtTMP;
    }
    while ( v6 );
  }
  else
  {
    result = *originalName;
  }
  return result;
}

Given the not so deep knowledge of C ++, I do not fully understand all the magic of what is happening.
Specifically the following interests:
1) if ( *originalName ) under what conditions it works\does not work.
2) while ( v6 ) how does it think it's time to finish?
3) v6 = (originalName++)[1] I understand that the first element from originalName is taken here?
4) unsigned __int16 is there an analogue of this type in java?
PS: I am rewriting part of the code in Java, for many moments there is enough knowledge about C ++, but for such as above, alas =(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2016-09-14
@jamakasi666

about truth and falsity:
1 == true
100500 == true
0 == false
NULL == false
etc.
1) fires when the value in the cell pointed to by originalName is true
2) see about true and false
3) move the pointer to the next cell, take the offset +1, get the second element from the beginning. see address arithmetic
4) byte[2]?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question