• Uncategorized

About linux : Checksum-calculator-in-C-gives-wrong-checksum-in-Windows-correct-in-Linux

Question Detail

I am using the checksum calculator from: https://github.com/Vi1i/OcarinaChecksumChecker

I downloaded the ocarina.h and checksumCalc.c files and was able to compile them using gcc checksumCalc.c in WSL for Windows (Debian Linux shell in Windows 10). It then outputs the file “a.out” which I am able to run using ./a.out "THE LEGEND OF ZELDA.sra" 0020 and which calculates the correct checksum for my Legend of Zelda save file that matches what I see in my hex editor.

Then I decided to play around and try compiling it using Windows. I’ve set up my Powershell so I can compile C programs simply using “cl” in Powershell. First, I tried doing cl checksumCalc.c and I got the following errors:

checksumCalc.c(49): error C2065: 'ushort': undeclared identifier

checksumCalc.c(49): error C2146: syntax error: missing ')' before identifier 'data'

checksumCalc.c(49): error C2059: syntax error: ')'

After Googling the first error, I changed “ushort” in the checksumCalc.c file to USHORT, and added #include <windows.h> at the top of the file. I tried compiling again and it actually compiled. However, when I try running the resulting exe in Windows, it gives an incorrect checksum. I used the same syntax as the a.out file, but replaced a.out with checksumCalc.exe. checksumCalc.exe "THE LEGEND OF ZELDA.sra" 0020

Does anyone have an idea of why the program is giving me a completely different result in Windows versus Linux? Here is a direct link to the checksumCalc.c code: https://github.com/Vi1i/OcarinaChecksumChecker/blob/master/checksumCalc.c

As mentioned above, the only thing I changed when trying in Windows was I added #include <windows.h>, and I made the ushort at the bottom all caps. I was hoping someone that understands the quirks of C programming and the differences between compiling in Windows and Linux could help me understand what might be going wrong.

For the record, I can just as easily accomplish what I’m trying to do by just getting the checksum from the Linux shell but I just want to understand what might be going on from a learning perspective. I’m a C noob, and to me I figured C was C, but apparently there must be commands that are exclusive to Linux/Windows where the code needs to be adapted to work properly.

Question Answer

Got this working properly thanks to this tip by user Maxim Sagaydachny in the comments:

replace FILE *file = fopen(argv[1], "r"); with FILE *file = fopen(argv[1], "rb");

It looks like something to do with the way Windows was reading the file as a text file, the “b” seems to indicate that the file should be read as binary. Now the Windows program is generating the correct checksum.

Thanks to Maxim Sagaydachny for this tip which ended up solving the issue. Also, thank you Joseph Sible-Reinstate Monica for the suggestions!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.