This repository was archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CRC32 checksum for Yakuza: Like a Dragon
- Loading branch information
1 parent
3147fa6
commit 865688c
Showing
3 changed files
with
97 additions
and
174 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
static const char Key[] = "STarYZgr3DL11"; | ||
static const int KeyLen = 13; | ||
|
||
// https://stackoverflow.com/a/21001712 | ||
unsigned int crc32b(unsigned char *message) { | ||
int i, j; | ||
unsigned int byte, crc, mask; | ||
|
||
i = 0; | ||
crc = 0xFFFFFFFF; | ||
while (message[i] != 0) { | ||
byte = message[i]; // Get next byte. | ||
crc = crc ^ byte; | ||
for (j = 7; j >= 0; j--) { // Do eight times. | ||
mask = -(crc & 1); | ||
crc = (crc >> 1) ^ (0xEDB88320 & mask); | ||
} | ||
i = i + 1; | ||
} | ||
return ~crc; | ||
} | ||
|
||
uint32_t calculate(char *data, uint64_t sz) | ||
{ | ||
return crc32b(data); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
FILE *in, *out; | ||
unsigned char *data, *outname, *filename, *ext; | ||
size_t sz; | ||
uint32_t checksum; | ||
|
||
if (argc != 2) | ||
{ | ||
printf("Usage: %s in.json\n", argv[0]); | ||
return 0; | ||
} | ||
|
||
if ((in = fopen(argv[1], "rb")) == NULL) | ||
{ | ||
perror("Error"); | ||
return 1; | ||
} | ||
|
||
// get filename without extension | ||
filename = malloc(sizeof(argv[1])); | ||
strcpy(filename, argv[1]); | ||
ext = strchr(filename, '.'); | ||
filename[(int)(ext - filename)] = '\0'; | ||
|
||
outname = strcat(filename, ".sav"); | ||
|
||
if ((out = fopen(outname, "wb")) == NULL) | ||
{ | ||
perror("Error"); | ||
fclose(in); | ||
return 1; | ||
} | ||
|
||
printf("Writing %s to %s...\n", argv[1], outname); | ||
|
||
fseek(in, 0, SEEK_END); | ||
sz = ftell(in); | ||
data = malloc(sz + 4); | ||
rewind(in); | ||
fread(data, 1, sz, in); | ||
fclose(in); | ||
|
||
// calculate checksum | ||
checksum = calculate(data, (uint64_t)sz); | ||
|
||
// encrypt | ||
for (int i = 0; i < sz; i++) | ||
data[i] ^= Key[i % KeyLen]; | ||
|
||
// add checksum | ||
data[sz] = checksum; | ||
data[sz + 1] = checksum >> 8; | ||
data[sz + 2] = checksum >> 16; | ||
data[sz + 3] = checksum >> 24; | ||
|
||
fwrite(data, 1, sz + 4, out); | ||
|
||
free(data); | ||
fclose(out); | ||
|
||
puts("Done!"); | ||
|
||
return 0; | ||
} |