Calculate CRC for strings [solved]

Programming related discussions related to game research
StreamThread
Posts: 54
Joined: Fri May 27, 2016 2:28 pm

Calculate CRC for strings [solved]

Post by StreamThread »

Hello

How to use Encryption command for calculate CRC32 of string?

I tried next code:
...


Well, i solved my objective with the next method:

Code: Select all

set MEMORY_FILE binary "
// Reverses (reflects) bits in a 32-bit word.
unsigned reverse(unsigned x) {
   x = ((x & 0x55555555) <<  1) | ((x >>  1) & 0x55555555);
   x = ((x & 0x33333333) <<  2) | ((x >>  2) & 0x33333333);
   x = ((x & 0x0F0F0F0F) <<  4) | ((x >>  4) & 0x0F0F0F0F);
   x = (x << 24) | ((x & 0xFF00) << 8) |
       ((x >> 8) & 0xFF00) | (x >> 24);
   return x;
}

unsigned int crc32a(unsigned char *message) {
   int i, j;
   unsigned int byte, crc;

   i = 0;
   crc = 0xFFFFFFFF;
   while (message[i] != 0) {
      byte = message[i];            // Get next byte.
      byte = reverse(byte);         // 32-bit reversal.
      for (j = 0; j <= 7; j++) {    // Do eight times.
         if ((int)(crc ^ byte) < 0)
              crc = (crc << 1) ^ 0x04C11DB7;
         else crc = crc << 1;
         byte = byte << 1;          // Ready next msg bit.
      }
      i = i + 1;
   }
   return reverse(~crc);
}
"

callDLL MEMORY_FILE "crc32a" "tcc" RET "my_message"

print "My CRC: %RET|h%"


But still interesting, maybe there's a way do same even easier with QuckBMS commands like Encryption or String 'e' operator? )
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Calculate CRC for strings [solved]

Post by aluigi »

If you want to calculate the CRC on a variable (NULL delimited string):

Code: Select all

encryption crc 0x04c11db7 "32 -1 -1 0 1 0"
string DUMMY E "my_message"
encryption "" ""
print "My CRC: %QUICKBMS_CRC|h%"

If you want to calculate it on a file just replace the second line with:

Code: Select all

get SIZE asize
log MEMORY_FILE 0 SIZE


Finding the parameters for the CRC method of the Encryption command is really a joke, just dump the string in a new file and use the following script on it:
http://aluigi.org/bms/crc_scan.bms

quickbms.exe crc_scan.bms input_string.txt > output.txt

In your case we had the following results:

Code: Select all

...
0x3c449c53 0x539c443c - CONFIG 0x04c11db7  32 -1 -1 0 1 0
...
0x3c449c53 0x539c443c - CONFIG 0xedb88320  32 -1 -1 0 0 1

The KEY argument of Encryption must be the polynomial while the IVEC field is the rest of the configuration.

As you can see the result obtained from crc_scan.bms has the crc in both endianess so you don't have to care about "swapping" your expected result for finding it :)
StreamThread
Posts: 54
Joined: Fri May 27, 2016 2:28 pm

Re: Calculate CRC for strings [solved]

Post by StreamThread »

Sometimes are differences in crc generation. First column - previous function, Second - QUICKBMS_SRC with Poly 0x04C11DB7:


Code: Select all

[Fn CRC32]      [QUICKBMS_CRC]

0x82e7bb5f      0x82e7bb5f      \animations\characters\branch\branch.mac.mrg.bin
0x6795d953      0xe6694177**      \animations\characters\branch\branch.xba
0x8ef67c66      0x8ef67c66      \animations\characters\branch\branch.xbg
0xfd44eff1      0xfd44eff1         \animations\characters\branch\branchtrap_nt_start_c.man.mrg.bin
0xd747174a      0xd747174a      \animations\characters\branch\branchtrap_nt_start_c.mcl.mrg.bin
0xe82d9056      0x4fba9ba2**      \animations\characters\branch\branchtrap_pull.man.mrg.bin
0xc22e68ed      0xc22e68ed      \animations\characters\branch\branchtrap_pull.mcl.mrg.bin
0xd7a4242e      0xd7a4242e      \animations\characters\branch\branchtrap_pull_nt.man.mrg.bin
0xfda7dc95      0xfda7dc95      \animations\characters\branch\branchtrap_pull_nt.mcl.mrg.bin
0xd150fb65      0xdf42fb0a**      \animations\characters\branch\branchtrap_wip.man.mrg.bin
0xfb5303de      0xfb5303de      \animations\characters\branch\branchtrap_wip.mcl.mrg.bin
0xcc62b3d9      0x04344ce8**      \animations\characters\branch\trp_bwhip00_df.xbt
0x01729feb      0x01729feb      \animations\characters\carver_3rd\carver_3rd_armsonly_human.mac.mrg.bin
0xff4dddd6      0x16e61c49**      \animations\characters\carver_3rd\carver_3rd_armsonly_human.xbg
0xed818071      0xefd134aa**      \animations\characters\carver_3rd\carver_shadow.mac.mrg.bin
0x3bc300eb      0xfa1503d3**      \animations\characters\carver_3rd\carver_shadow.xbg
0x89e2b043      0x89e2b043      \animations\characters\carver_first\carver_first_hands_human_df.xbt
0x325957c7      0x325957c7      \animations\characters\carver_first\carver_first_hands_human_nt.xbt
0x6ade1f4b      0x6ade1f4b      \animations\characters\carver_first\carver_first_hands_mutant_df.xbt
0xd165f8cf      0xd165f8cf      \animations\characters\carver_first\carver_first_hands_mutant_nt.xbt
0x16075c5a      0x1600f599**      \animations\characters\carver_first\cf_assault_getgun.man.mrg.bin
0x3c04a4e1      0x3c04a4e1      \animations\characters\carver_first\cf_assault_getgun.mcl.mrg.bin


Online CRC with poly 0x04C11DB7 give results simular to function.
Now i guess, the problem with nameCRC command, about i posted in "QuickBMS errors" topic, also linked with that.
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Calculate CRC for strings [solved]

Post by aluigi »

Here everything works perfectly and \animations\characters\branch\branch.xba returns 0x6795d953 using both Encryption and Namecrc.
beedy
Posts: 81
Joined: Sat Aug 26, 2017 8:09 am

Re: Calculate CRC for strings [solved]

Post by beedy »

Hello,
I want to know how these crc values are calculated? I have tried online crc calculator and quickbms crc-scanner with no luck.
It's strings from EA's NHL .big archive and I can't add some new files in the archive if the crc is wrong. There are also same named files in Fifa bh-files with same crc.

Here is some examples:
attribdb/renddb.bin should be 0x3C12BC16
attribdb/renddb.opt should be 0x3C12F450
rendering/effects/powerring_0_0.rx2 should be 0xE91BE626
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Calculate CRC for strings [solved]

Post by aluigi »

There is a detailed explanation here:
viewtopic.php?p=40031#p40031

Regarding your examples there is no matching result for the provided strings, maybe they are calculated on all toupper strings or with / converted to \ or probably it's just a custom crc algorithm.
beedy
Posts: 81
Joined: Sat Aug 26, 2017 8:09 am

Re: Calculate CRC for strings [solved]

Post by beedy »

I have tried do this for strings but did not match. I have tried to find info about crc in .xex-file but all I found is something about EASTL hash, but it don’t know anything about it. Yes It’s probably a EA’s custom crc or something and maybe It will too hard to try to reverse-engineer it?