PowerBASIC Forums
  Programming
  ElfHash c translation

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone! next newest topic | next oldest topic
Author Topic:   ElfHash c translation
Brad McLane
Member
posted November 30, 2000 03:23 PM     Click Here to See the Profile for Brad McLane     Edit/Delete Message   Reply w/Quote
I am struggling with the translation of the ElfHash() function from c to PB.

Here's the c 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:


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

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

IP: Logged

Dave Navarro
Member
posted November 30, 2000 03:57 PM     Click Here to See the Profile for Dave Navarro     Edit/Delete Message   Reply w/Quote
quote:
Originally posted by Brad McLane:

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...


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

IP: Logged

Brad McLane
Member
posted December 01, 2000 12:07 AM     Click Here to See the Profile for Brad McLane     Edit/Delete Message   Reply w/Quote
That one works much better.

Thanks Dave!

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

IP: Logged

Wayne Diamond
Member
posted July 23, 2002 09:11 PM     Click Here to See the Profile for Wayne Diamond     Edit/Delete Message   Reply w/Quote
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:
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:
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/pbcrypto/view.php?algorithm=elf32

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

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

IP: Logged

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c