More Awesome Than You!

TS2: Burnination => The Podium => Topic started by: wes_h on 2007 April 11, 03:29:00



Title: Need an algorithm to generate CRC32 hash
Post by: wes_h on 2007 April 11, 03:29:00
A long time ago, I had a copy of an algorithm to generate the CRC32 value used as the InstanceHi (or Subtype) field in the TGI. Of course, I lost it, since it was always so easy to go to SimPE and use the hash generator. Now, I have a project I am working on that needs to have this hard-coded in.

The only real meaty resource specfically related to Maxis' usage I located states it is generated using the parameters:

Resource ID: CRC-32 (Poly=0x04C11DB7, Seed=0xFFFFFFFF, reflection=false, XOR Output=0x00000000)

I found plenty of CRC32 examples using that poly value, being the algorithm used for WinZIP and others. However, they all had a reflection part in the routine that I did not see as being any kind of boolean operation.

So what I am asking for is a pointer to a working routine for this... any programming language is fine.

<* Wes *>


Title: Re: Need an algorithm to generate CRC32 hash
Post by: dizzy on 2007 April 11, 11:34:09
I did something like this before...

http://www.moreawesomethanyou.com/smf/index.php?topic=6650.msg187881#msg187881


Title: Re: Need an algorithm to generate CRC32 hash
Post by: wes_h on 2007 April 12, 02:22:15
But I USED search! :)

I was hoping YOU had done that before.
I have copied the routine and will work with it.
I appreciate the help.

<* Wes *>


Title: Re: Need an algorithm to generate CRC32 hash
Post by: wes_h on 2007 May 22, 04:11:21
OK, I was unable to make the algorithm for CRC32 you posted work.

In case you, or anyone else, is in burning need of this routine, I am posting the code I found posted on the internet (as modified by me) that will calculate the CRC32 the same way that the game and SimPE calculate it. It ain't pretty, and I didn't worry about optimizing it because I use it once per bone for exporting animation data. The two lines commented out are for a bit reflection routine I didn't include, or need:

Code:
unsigned long crcbitbybitfast(unsigned char* p) {

// fast bit by bit algorithm without augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.

unsigned long i, j, c, bit;
unsigned long crc = 0xffffffff;
unsigned long crchighbit = 0x80000000;
unsigned long polynom = 0x04c11db7;
unsigned long crcxor = 0;
unsigned long crcmask = 0xFFFFFFFF;
unsigned long order = 32;
unsigned long len;

len=strlen(p);

for (i=0; i<len; i++) {
c = (unsigned long)*p++;
// if (refin) c = reflect(c, 8);
for (j=0x80; j; j>>=1) {
bit = crc & crchighbit;
crc<<= 1;
if (c & j) bit^= crchighbit;
if (bit) crc^= polynom;
}
}
// if (refout) crc=reflect(crc, order);
crc^= crcxor;
crc&= crcmask;
return(crc);
}