Skip to main content
GameDev.net gamedev.net
🔒 Locked

Handling checksums

Started by Wooh Jul 29, 2011 at 9:36 AM 0 replies 800+ views
Original Post
Wooh
Wooh
I want to have a checksum in a savefile so that I can check if the savefile is not corrupted. I'm a bit unsure how to handle this the best way. At the moment I have a wrapper so that all data that is written is written to file using std::ofstream and also passed through the checksum calculation so I can later write the checksum at the end of the file.

When reading the data back I could use a similar approach letting all read data pass through the checksum calculation and when that is done I can compare the checksum. The problem with this approach is that the read routines are used while restoring the gamestate and at the end when it's time to validate the checksum I have already constructed the whole gamestate. To me this feels wrong. I feel I should validate the checksum first but then I will have to read through the file twice, once for calculating the checksum, and once when restoring the gamestate.

So, what is the best way of handling checksums, especially while reading?
Adam_42
Adam_42
You only really have two options:

1. Read the file twice. The first time to verify the checksum, the second time to load it. This has a small possibility of going wrong if the first read works correctly, and the second one doesn't.

2. Read the whole file into temporary memory, verify the checksum, and then load from that memory into your game state. std::stringstream should make this fairly simple.

In most cases there's not much performance difference between the two since the operating system disc cache should make #1 almost as quick as #2.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.