Advertisement
Reisyukaku

Triforce Heroes SaveData checksum fixer

Oct 23rd, 2015
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. const unsigned int poly = 0x04C11DB7;
  5.  
  6. unsigned int reflect(unsigned int data, int bits) {
  7.     unsigned int ret = 0;
  8.     int i;
  9.     for(i = bits-1; i>=0; i--) {
  10.         if(data & 1) ret |= (1 << i);
  11.         data >>= 1;
  12.     }
  13.     return ret;
  14. }
  15.  
  16. unsigned int crc32(char* msg, int len) {
  17.     unsigned int crc = 0xffffffff;
  18.     int i, j;
  19.     for(i = 0; i < len; i++) {
  20.         crc ^= ((char)reflect(msg[i], 8) << 24);
  21.         for(j = 8; j; j--) {
  22.             crc = (crc << 1) ^ ((crc & 0x80000000) ? poly : 0x0);
  23.         }
  24.     }
  25.     return reflect(crc, 32) ^ 0xffffffff;
  26. }
  27.  
  28. int main(int argc, char **argv){
  29.     if(argc < 2){
  30.         printf("Usage: %s [SaveData.bin]\n", argv[0]);
  31.         return -1;
  32.     }
  33.    
  34.     FILE *fp;
  35.     fp = fopen(argv[1], "rb+");
  36.     if(!fp) return -1;
  37.     int crc1 = 0, crc2 = 0, crc3 = 0;
  38.     unsigned char *firstChunk = malloc(0x1FC);
  39.     unsigned char *secondChunk = malloc(0x17FC);
  40.     unsigned char *thirdChunk = malloc(0x11FC);
  41.     //Get chunks
  42.     fread(firstChunk, 1, 0x1FC, fp);
  43.     fseek(fp, 0x200, SEEK_SET);
  44.     fread(secondChunk, 1, 0x17FC, fp);
  45.     fseek(fp, 0x1A00, SEEK_SET);
  46.     fread(thirdChunk, 1, 0x11FC, fp);
  47.     //Get CRCs
  48.     crc1 = crc32(firstChunk, 0x1FC);
  49.     crc2 = crc32(secondChunk, 0x17FC);
  50.     crc3 = crc32(thirdChunk, 0x11FC);
  51.     //Write CRCs
  52.     fseek(fp, 0x1FC, SEEK_SET);
  53.     fwrite(&crc1, sizeof(int),1, fp);
  54.     fseek(fp, 0x19FC, SEEK_SET);
  55.     fwrite(&crc2, sizeof(int),1, fp);
  56.     fseek(fp, 0x2BFC, SEEK_SET);
  57.     fwrite(&crc3, sizeof(int),1, fp);
  58.     fclose(fp);
  59.     printf("First check = 0x%08X\n", crc1);
  60.     printf("Second check = 0x%08X\n", crc2);
  61.     printf("Third check = 0x%08X\n", crc3);
  62.     free(firstChunk);
  63.     free(secondChunk);
  64.     free(thirdChunk);
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement