have problem convert javascript to c
-
hiho@ll i need to convert a simple javascript i've found which generates a checksum to C because i want to use it in a c prog btw. i want to use this exact checksum code, so trieing another checksum algorithm isn't an option here is only one line of code where i think the problem occures i'll post the whole code as a reply so you can test it if you want javascript:
c[r(i,3)]=(c[r(i,3)]-c[r(i+1,3)]-j)^(r(i,3)==1?j<>>s[i]);
C:c[r(i,3.0)]=(c[r(i,3.0)]-c[r(i+1,3.0)]-j)^(r(i,3.0)==1?j<>s[i]);
r is defined as this:#define r(x,y) (long long int)(floor(((x)/(y)-floor((x)/(y)))*(y)+0.1))
and for testing you can use c as the following array:long long int c[]={0x9E3779B9,0x9E3779B9,0xE6359A60};
the problem is that i get wrong values within the C code but the javascript code works another problem was Visual Studio!! i got complete different values for the whole code using visual studio using the same code on Linux (i used cygwin) got me right values (for some part of the whole code) don't know what the problem is/was but neither Visual Studio nor Linux got the one line described above work i get wrong values anybody knows the right conversion of the javascript code to C code? thx@ll -
hiho@ll i need to convert a simple javascript i've found which generates a checksum to C because i want to use it in a c prog btw. i want to use this exact checksum code, so trieing another checksum algorithm isn't an option here is only one line of code where i think the problem occures i'll post the whole code as a reply so you can test it if you want javascript:
c[r(i,3)]=(c[r(i,3)]-c[r(i+1,3)]-j)^(r(i,3)==1?j<>>s[i]);
C:c[r(i,3.0)]=(c[r(i,3.0)]-c[r(i+1,3.0)]-j)^(r(i,3.0)==1?j<>s[i]);
r is defined as this:#define r(x,y) (long long int)(floor(((x)/(y)-floor((x)/(y)))*(y)+0.1))
and for testing you can use c as the following array:long long int c[]={0x9E3779B9,0x9E3779B9,0xE6359A60};
the problem is that i get wrong values within the C code but the javascript code works another problem was Visual Studio!! i got complete different values for the whole code using visual studio using the same code on Linux (i used cygwin) got me right values (for some part of the whole code) don't know what the problem is/was but neither Visual Studio nor Linux got the one line described above work i get wrong values anybody knows the right conversion of the javascript code to C code? thx@llthe javascript:
function r(x,y){ return Math.floor((x/y-Math.floor(x/y))*y+.1); } function m(c){ var i,j,s=[13,8,13,12,16,5,3,10,15]; for(i=0;i<9;i+=1){ j=c[r(i+2,3)]; c[r(i,3)]=(c[r(i,3)]-c[r(i+1,3)]-j)^(r(i,3)==1?j<<s[i]:j>>>s[i]); } } function getURL(url){ url='info:'+url; var c=[0x9E3779B9,0x9E3779B9,0xE6359A60],i,j,k=0,l; for(l=url.length;l>=12;l-=12){ for(i=0;i<16;i++){ j=k+i; c[Math.floor(i/4)]+=url.charCodeAt(j)<<(r(j,4)*8); } m(c); k+=12; } c[2]+=url.length; for(i=l;i>0;i--) c[Math.floor((i-1)/4)]+=url.charCodeAt(k+i-1)<<(r(i-1,4)+(i>8?1:0))*8; m(c); return'6'+c[2]; } String: Checksum the C code i already have: `#include #include #include #include #define r(x,y) (long long int)(floor(((x)/(y)-floor((x)/(y)))*(y)+0.1)) void m(long long int *c); void getUrl(char *tmp); void printc(long long int *c); void printc(long long int *c){ for(long long int i=0;i<3;i++) printf("%lld ",c[i]); printf("\n"); } int main(int argc, char **argv) { getUrl("http://fabmail.de/"); fgetc(stdin); return 0; } void m(long long int *c){ long long int i,j,s[]={13,8,13,12,16,5,3,10,15}; printc(c); for(i=0;i<9;i++){ j=c[r(i+2,3.0)]; c[r(i,3.0)]=(c[r(i,3.0)]-c[r(i+1,3.0)]-j)^(r(i,3.0)==1?j<>s[i]); } } void getUrl(char *tmp){ char url[2048]={0}; char ergebnis[1024]={0}; if(tmp==NULL || strlen(tmp)>2084-6){ return; } sprintf(url,"info:%s",tmp); long long int c[]={0x9E3779B9,0x9E3779B9,0xE6359A60}; long long int i,j,k=0,l; for(l=(long long int)strlen(url);l>=12;l-=12){ for(i=0;i<16;i++){ j=k+i; c[(int)floor(i/4.0)]+=url[j]<<(r(j,4.0)*8); } m(c); k+=12; } c[2]+=(long long int)strlen(url); for(i=l;i>0;i--) c[(int)floor((i-1)/4.0)]+=url[(k+i-1)]<<(r(i-1,4.0)+(i>8?1:0))*8; m(c); sprintf(ergebnis,"6%lld",c[2]); printf("%s\n",ergebnis); }` int the C code the problem is in the m(c); function but i don't know what's wrong -- modified at 9:10 Sunday 2nd October, 2005