Announcement

Collapse
No announcement yet.

ElfHash c translation

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ElfHash c translation

    I am struggling with the translation of the ElfHash() function from c to PB.

    Here's the c code:

    Code:
    unsigned long ElfHash(const unsigned char *key)
    {
      unsigned long h = 0;
      while (*key) {
        h = (h << 4) + *key++;
        unsigned long g = h & 0xF0000000L;
        if (g) h ^= g >> 24;
        h &= ~g;
      }
      return h;
    }
    Now I know why I've always found excuses not to code in c: Too many cryptic operators!

    Here's my best hack at a PB translation:

    Code:
    Function ElfHash(sKey As String) As Long
    Local h As Long
    Local g As Long
    Local i As Long
    Local b As Byte
    Local lpsKey As Long
    Local lLen As Long
    h = 0
    
    
    lpsKey = StrPtr(sKey)
    
    
    For i = 0 To Len(sKey)
      Shift Left h, 4
      Incr lpsKey
      b = Peek(lpsKey)
      h = h + b
      g = h And &HF0000000&
      If IsTrue(g) Then
         Shift Right g, 24
         h = h Xor g
      End If
      h = h And -g
    
    Next
    
    Function = h
    
    End Function
    
    Function PbMain()
    
         Print Str$(ElfHash("test"))
    
    End Function
    The PB code runs but it always returns zero.

    Anyone have any suggestions?


    Brad



    ------------------

  • #2
    Originally posted by Brad McLane:
    Code:
    unsigned long ElfHash(const unsigned char *key)
    {
      unsigned long h = 0;
      while (*key) {
        h = (h << 4) + *key++;
        unsigned long g = h & 0xF0000000L;
        if (g) h ^= g >> 24;
        h &= ~g;
      }
      return h;
    }
    Here's my translation...
    Code:
    FUNCTION ElfHash(sKey AS STRING) AS LONG
     
      LOCAL key AS BYTE PTR
      LOCAL h AS DWORD
      LOCAL g AS DWORD
     
      key = STRPTR(sKey)
     
      h = 0
      WHILE @key
        SHIFT LEFT h, 4
        h = h + @key
        key = key + 1
        g = h AND &HF0000000???
        IF g THEN
          SHIFT RIGHT g, 24
          h = h XOR g  'h ^= g
        END IF
        h = h AND (NOT g)  'h &= ~g;
      WEND
     
      FUNCTION = h
     
    END FUNCTION
    --Dave

    ------------------
    Home of the BASIC Gurus
    www.basicguru.com
    Home of the BASIC Gurus
    www.basicguru.com

    Comment


    • #3
      That one works much better.

      Thanks Dave!

      ------------------

      Comment


      • #4
        Dave,
        Your port produces incorrect results, and unfortunately it has gone unnoticed for one and a half years
        But better late than never! Here's a fixed one:
        Code:
        FUNCTION Elf32(sKey AS STRING) AS LONG
          LOCAL key AS BYTE PTR
          LOCAL h AS LONG
          LOCAL g AS LONG
          LOCAL i AS LONG
          key = STRPTR(sKey)
          h = 0
          WHILE @key
            SHIFT LEFT h, 4
            h = h + @key
            key = key + 1
            g = h AND &HF0000000???
            IF g THEN
              i = g
              SHIFT RIGHT i, 24
              h = h XOR i
            END IF
            h = h AND (NOT g)
          WEND
          FUNCTION = h
        END FUNCTION
        And here's my inline port:
        Code:
        FUNCTION Elf32(sKey AS STRING) AS DWORD
        #REGISTER NONE
        LOCAL lLen AS DWORD, ptrStr AS DWORD, H AS DWORD
        lLen = LEN(sKey)
        ptrStr = STRPTR(sKey)
               ! xor ebx, ebx      ; ebx = result holder (H)
               ! mov edx, lLen     ; edx = Length
               ! mov ecx, ptrStr   ; ecx = Ptr to string
               ! xor esi, esi
            Elf1:
               ! xor eax, eax
               ! shl ebx, 4
               ! mov al, [ecx]
               ! add ebx, eax
               ! inc ecx
               ! mov eax, ebx
               ! and eax, &HF0000000???
               ! cmp eax, 0
               ! je Elf2
               ! mov esi, eax
               ! shr esi,24
               ! xor ebx, esi
            Elf2:
               ! not eax
               ! and ebx,eax
               ! dec edx
               ! cmp edx, 0
               ! jne Elf1
               ! mov H,ebx
            FUNCTION = H
        END FUNCTION
        See also http://www.wanet.com.au/~diamond/pbc...lgorithm=elf32


        ------------------
        The PowerBASIC Crypto Archives

        [This message has been edited by Wayne Diamond (edited July 24, 2002).]
        -

        Comment

        Working...
        X