Shop OBEX P1 Docs P2 Docs Learn Events
Hash Code Help — Parallax Forums

Hash Code Help

Bill DrummondBill Drummond Posts: 54
edited 2009-04-03 17:12 in Propeller 1
I trying to port this pascal code to Spin
function ELFHash(const Str : String) : Int64;
var
  i    : integer;
  x : Cardinal;
begin
  Result := 0;
  for i := 1 to Length(Str) do
  begin
    Result := (Result shl 4) + Ord(Str[noparse][[/noparse]i]);
    x      := Result and $F0000000;
    if (x <> 0) then
    begin
      Result := Result xor (x shr 24);
    end;
    Result := Result and (not x);
  end;
end;

This doesn't work..
''***************************************
''*  From Keyboard Demo v1.0            *
''*  Author: Chip Gracey                *
''*  Copyright (c) 2006 Parallax, Inc.  *
''*  See end of file for terms of use.  *
''***************************************

 
CON

        _clkmode        = xtal1 + pll16x
        _xinfreq        = 5_000_000

VAR
 byte  j
 long  x
OBJ

        term    : "tv_terminal"
        kb      : "keyboard"

PUB start | i

  'start the tv terminal
  term.start(12)
  term.str(string("HashCode Demo...",13))

  'start the keyboard
  kb.start(26, 27)
  
  term.Hex(Hash(String("DIR")),8)
  term.out(13)
  term.Hex(Hash(String("TYPE")),8)
  term.out(13)
  term.Hex(Hash(String("MOUNT")),8)
  term.out(13)
  term.Hex(Hash(String("UNMOUNT")),8)
  term.out(13)
   
PUB Hash(string_ptr) : Result  
 result := 0
 repeat strsize(string_ptr)
   Result := (Result << 4) + byte[noparse][[/noparse]string_ptr++]
   x := Result and $F0_00_00_00
   if (x <> 0)
    Result := Result ^ (x >> 24)
   Result := result & (not x) 
  
{{

Here is a windows program


                

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2009-04-03 07:33
    Change that (boolean AND)

    x := Result and $F0_00_00_00
    


    into that (bitwise AND)

    x := Result & $F0_00_00_00
    
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-04-03 08:42
    Thanks, I need to study the manual some more..
    any idea why it breaks with more than 6 characters?
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-04-03 09:51
    A Better Demo
    ''***************************************
    ''*  From Keyboard Demo v1.0            *
    ''*  Author: Chip Gracey                *
    ''*  Copyright (c) 2006 Parallax, Inc.  *
    ''*  See end of file for terms of use.  *
    ''***************************************
    
     
    CON
            _clkmode        = xtal1 + pll16x
            _xinfreq        = 5_000_000
    VAR
     long  x
     byte HashStr[noparse][[/noparse]8]
    OBJ
            term    : "tv_terminal"
            kb      : "keyboard"
     
    PUB start | i
      'start the tv terminal
      term.start(12)
      term.str(string("HashCode Demo...",13))
      'start the keyboard
      kb.start(26, 27)
      repeat
       term.str(string("Enter a word >")) 
        getstr
        term.str(string("Hash Code for "))
        term.str(@HashStr)
        term.str(string(" is $"))
        term.Hex(Hash(@HashStr),8)
          term.out(13)
     
    PUB GetStr | i,ch
        i := 0
        repeat until ch == 13
          ch  := kb.getkey
          HashStr[noparse][[/noparse]i++] := ch
          term.out(ch)
       HashStr[noparse][[/noparse]i-1] := 0
        
    PUB Hash(string_ptr) : Result  
     result := 0
     repeat strsize(string_ptr)
       Result := (Result << 4) + byte[noparse][[/noparse]string_ptr++]
       x := Result & $F0_00_00_00
       if (x <> 0)
        Result := Result ^ (x >> 24)
       Result := result & (not x) 
      
    {{
    &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9488;
    &#9474;                                                   TERMS OF USE: MIT License                                                  &#9474;                                                            
    &#9500;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9508;
    &#9474;Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    &#9474; 
    &#9474;files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    &#9474;
    &#9474;modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software&#9474;
    &#9474;is furnished to do so, subject to the following conditions:                                                                   &#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.&#9474;
    &#9474;                                                                                                                              &#9474;
    &#9474;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          &#9474;
    &#9474;WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         &#9474;
    &#9474;COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   &#9474;
    &#9474;ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         &#9474;
    &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9496;
    }}   
    

    Post Edited (Bill Drummond) : 4/3/2009 10:08:26 AM GMT
  • kuronekokuroneko Posts: 3,623
    edited 2009-04-03 10:01
    Bill Drummond said...
    any idea why it breaks with more than 6 characters?
    I'm just guessing here, maybe because your HashStr array only has space for 6 (now 8) bytes?

    Post Edited (kuroneko) : 4/3/2009 10:17:26 AM GMT
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-04-03 10:19
    The first try didn't have the HashStr Array. I increased it to 8 in the next one, but still won't do more than 6 chars correctly.
    I edited the Windows program so everybody uses hex.
  • kuronekokuroneko Posts: 3,623
    edited 2009-04-03 10:33
    So what happens after 6 characters? Having space for 8 bytes still lets you only safely enter 7 characters (+terminator). As for the latter, the terminator seems to override the last entered character (that is if SPIN uses post-increment, i++ vs i-1).

    And I'm also not too comfortable with repeat strsize(). Is this evaluated only at the beginning of the loop or everytime the loop is run? If you put some debug tracing in there, how far does it get?
  • Brian FairchildBrian Fairchild Posts: 549
    edited 2009-04-03 11:05
    My Pascal is very ropey but doesn't the 'int64' in the Pascal function definition imply that the result is a 64-bit value?
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-04-03 11:29
    So what happens after 6 characters? Having space for 8 bytes still lets you only safely enter 7 characters (+terminator). As for the latter, the terminator seems to override the last entered character (that is if SPIN uses post-increment, i++ vs i-1).
    I changed from:
    byte HashStr[noparse][[/noparse]8]  to byte HashStr[noparse][[/noparse]12]  
    and
     
    repeat strsize(string_ptr) to  repeat while byte[noparse][[/noparse]string_ptr] <> 0
    

    same result.
    My Pascal is very ropey but doesn't the 'int64' in the Pascal function definition imply that the result is a 64-bit value?
    That's true, but I changed it to Cardinal(unsigned 32 bit)·and it still works.

    I need to Learn how ViewPort works... got too many projects going at same time.
  • mparkmpark Posts: 1,305
    edited 2009-04-03 15:08
    What do you mean by "it breaks"? What results are you seeing and what are you expecting to see?
  • Brian FairchildBrian Fairchild Posts: 549
    edited 2009-04-03 15:47
    I've bashed the code into a proto board and when entering 7 characters the hash value is returned as '00000000'. An eight character string returns the hash value of just the 8th character.
  • Brian FairchildBrian Fairchild Posts: 549
    edited 2009-04-03 16:34
    OK, simple really when you spot it.

    Change...
    Result := result & (not x) 
    
    


    ...which is a boolean 'NOT' to...
    Result := result & (!x) 
    
    


    ...which is a bitwise 'NOT'.
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-04-03 16:50
    What do you mean by "it breaks"? What results are you seeing and what are you expecting to see?
    If I key in : 123456···· ·I see $03456786· - is good
    If I key in : 1234567·· · I see $00000000 should be $045678A7
    If I key in : 123456789 I see $000003B9 should be $0678AEE9
    If I key in :·MOUNT····· I see $00524A34··- is good
    If I key in : UNMOUNT· I see $00000000 should be $0A324A64

    Download the File in my first post to run the Hash Code generator on a PC.
  • Bill DrummondBill Drummond Posts: 54
    edited 2009-04-03 17:12
    OK, simple really when you spot it.
    Yea, I'm ! used to programming with funny paper cuss words yet.smhair.gif , but this is how you learn.
    ··
Sign In or Register to comment.