View Single Post
Old 08-22-2004, 03:06 PM   #160
barranquilla
Junior Member
barranquilla began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Aug 2004
Unhappy checksum on Java...

I tried to port the checksum algorithm to Java and I had some partial success. However, I think there's a problem with the way Java manages hex numbers (unsigned longs, maybe?). Here's the source:

import java.io.*;

public class GoogleRank {

public int GOOGLE_MAGIC = 0xE6359A60;

// Google Rank
private long zeroFill(long a, long b)
{
long z = 0x80000000;

if ((z & a) == z)
{

a = (a>>1);

a = a & (~z);

a = a | 0x40000000;
a = (a >> (b-1));
}
else
{
a = (a >> b);

}

return a;
}

public long[] mix(long a, long b, long c) {
a -= b; a -= c; a ^= (zeroFill(c,13));
b -= c; b -= a; b ^= (a<<8);
c -= a; c -= b; c ^= (zeroFill(b,13));
a -= b; a -= c; a ^= (zeroFill(c,12));
b -= c; b -= a; b ^= (a<<16);
c -= a; c -= b; c ^= (zeroFill(b,5));
a -= b; a -= c; a ^= (zeroFill(c,3));
b -= c; b -= a; b ^= (a<<10);
c -= a; c -= b; c ^= (zeroFill(b,15));
long[] mix = new long[3];
mix[0] = a;
mix[1] = b;
mix[2] = c;

return mix;
}

public long GoogleCH(String url) {

long a = 0x9E3779B9;
long b = 0x9E3779B9;
long c = GOOGLE_MAGIC;
int length = url.length();
int len = length;
int k = 0;


while(len >= 12) {
a += (url.charAt(k+0) +(url.charAt(k+1)<<8) +(url.charAt(k+2)<<16) +(url.charAt(k+3)<<24));

b += (url.charAt(k+4) +(url.charAt(k+5)<<8) +(url.charAt(k+6)<<16) +(url.charAt(k+7)<<24));
c += (url.charAt(k+8) +(url.charAt(k+9)<<8) +(url.charAt(k+10)<<16)+(url.charAt(k+11)<<24));

long [] mix = mix(a,b,c);
a = mix[0]; b = mix[1]; c = mix[2];
k += 12;
len -= 12;
}

c += length;
switch(len) /* all the case statements fall through */
{
case 11: c+=(url.charAt(k+10)<<24);
case 10: c+=(url.charAt(k+9)<<16);
case 9 : c+=(url.charAt(k+8)<<8);
/* the first byte of c is reserved for the length */
case 8 : b+=(url.charAt(k+7)<<24);
case 7 : b+=(url.charAt(k+6)<<16);
case 6 : b+=(url.charAt(k+5)<<8);
case 5 : b+=(url.charAt(k+4));
case 4 : a+=(url.charAt(k+3)<<24);
case 3 : a+=(url.charAt(k+2)<<16);
case 2 : a+=(url.charAt(k+1)<<8);
case 1 : a+=(url.charAt(k+0));
/* case 0: nothing left to add */
}
long[] mix = mix(a,b,c);
/*-------------------------------------------- report the result */
return mix[2];


}

public static void main(String[] args) {

try {
GoogleRank g = new GoogleRank();
long checksum = g.GoogleCH("info:http://www.example.com/");

// Anything go wrong?
} catch (Exception f) {
System.out.println("Exception: " + f.toString( ));
}
}
}


Any pointers on what may be wrong are very appreciated...
barranquilla is offline   Reply With Quote