Skip to content

Instantly share code, notes, and snippets.

@CTurt

CTurt/checksum.c Secret

Created March 1, 2020 15:59
Show Gist options
  • Save CTurt/2344065fed820a19356dd3fa3cdb82d9 to your computer and use it in GitHub Desktop.
Save CTurt/2344065fed820a19356dd3fa3cdb82d9 to your computer and use it in GitHub Desktop.
Morita Shogi 64 checksum fixer
#include <stdio.h>
unsigned int checksum(unsigned char *data, int length) {
unsigned char bVar1;
unsigned int uVar2;
int iVar3;
uVar2 = 0xffff;
bVar1 = *data;
while(1) {
data = data + 1;
iVar3 = 0;
uVar2 = (unsigned int)bVar1 << 8 ^ uVar2;
do {
if ((uVar2 & 0x8000) == 0) {
uVar2 = uVar2 << 1;
}
else {
uVar2 = uVar2 << 1 ^ 0x1021;
}
if ((uVar2 & 0x8000) == 0) {
uVar2 = (uVar2 & 0xffff) << 1;
}
else {
uVar2 = (uVar2 & 0xffff) << 1 ^ 0x1021;
}
if ((uVar2 & 0x8000) == 0) {
uVar2 = (uVar2 & 0xffff) << 1;
}
else {
uVar2 = (uVar2 & 0xffff) << 1 ^ 0x1021;
}
if ((uVar2 & 0x8000) == 0) {
uVar2 = (uVar2 & 0xffff) << 1;
}
else {
uVar2 = (uVar2 & 0xffff) << 1 ^ 0x1021;
}
uVar2 = uVar2 & 0xffff;
iVar3 = iVar3 + 4;
} while (iVar3 != 8);
length -= 1;
if (length == 0) break;
bVar1 = *data;
}
return ~uVar2 & 0xffff;
}
int main(int argc, char **argv) {
char save[0x200];
if(argc < 2) return 1;
printf("%s\n", argv[1]);
FILE *f = fopen(argv[1], "rb");
if(!f) return 1;
if(fread(save, 0x100, 2, f) != 2) return 1;
unsigned short c1 = checksum(save, 0xef);
unsigned short c2 = checksum(save + 0x100, 0xef);
printf("%04x\n%04x\n", c1, c2);
fclose(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment