Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- const unsigned int poly = 0x04C11DB7;
- unsigned int reflect(unsigned int data, int bits) {
- unsigned int ret = 0;
- int i;
- for(i = bits-1; i>=0; i--) {
- if(data & 1) ret |= (1 << i);
- data >>= 1;
- }
- return ret;
- }
- unsigned int crc32(char* msg, int len) {
- unsigned int crc = 0xffffffff;
- int i, j;
- for(i = 0; i < len; i++) {
- crc ^= ((char)reflect(msg[i], 8) << 24);
- for(j = 8; j; j--) {
- crc = (crc << 1) ^ ((crc & 0x80000000) ? poly : 0x0);
- }
- }
- return reflect(crc, 32) ^ 0xffffffff;
- }
- int main(int argc, char **argv){
- if(argc < 2){
- printf("Usage: %s [SaveData.bin]\n", argv[0]);
- return -1;
- }
- FILE *fp;
- fp = fopen(argv[1], "rb+");
- if(!fp) return -1;
- int crc1 = 0, crc2 = 0, crc3 = 0;
- unsigned char *firstChunk = malloc(0x1FC);
- unsigned char *secondChunk = malloc(0x17FC);
- unsigned char *thirdChunk = malloc(0x11FC);
- //Get chunks
- fread(firstChunk, 1, 0x1FC, fp);
- fseek(fp, 0x200, SEEK_SET);
- fread(secondChunk, 1, 0x17FC, fp);
- fseek(fp, 0x1A00, SEEK_SET);
- fread(thirdChunk, 1, 0x11FC, fp);
- //Get CRCs
- crc1 = crc32(firstChunk, 0x1FC);
- crc2 = crc32(secondChunk, 0x17FC);
- crc3 = crc32(thirdChunk, 0x11FC);
- //Write CRCs
- fseek(fp, 0x1FC, SEEK_SET);
- fwrite(&crc1, sizeof(int),1, fp);
- fseek(fp, 0x19FC, SEEK_SET);
- fwrite(&crc2, sizeof(int),1, fp);
- fseek(fp, 0x2BFC, SEEK_SET);
- fwrite(&crc3, sizeof(int),1, fp);
- fclose(fp);
- printf("First check = 0x%08X\n", crc1);
- printf("Second check = 0x%08X\n", crc2);
- printf("Third check = 0x%08X\n", crc3);
- free(firstChunk);
- free(secondChunk);
- free(thirdChunk);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement