views:

576

answers:

7

I'm working with a small FAT16 filesystem, and I want to generate CRC values for indidual XML files which store configuration information. In case the data changes or is corrupted, I want to be able to check the CRC to determine that the file is still in it's original state.

The question is, how do I put the CRC value into the file, without changing the CRC value of the file itself? I can think of a couple solutions, but I think there must be a fairly standard solution for this issue.

+6  A: 

You could append the CRC value to the end of the file. Then, when computing the CRC value later for checking, omit the last four bytes.

Greg Hewgill
+3  A: 

Define a header, generate the CRC of everything except the header then put the value in the header.

PiedPiper
A: 

There is no way to do this. You could make the first x bytes (CRC uses a 32 bit integer, so 4 bytes) of the file contain the CRC, and then when calculating your CRC, you could only consider the bytes that come after that initial 4 bytes.

Another solution would be to include the CRC into the file name. So MyFile.Config would end up being MyFile.CRC1234567.Config.

Kibbee
+1  A: 

One solution would be to use dsofile.dll to add extended properties to your files. You could save the CRC value (converted to a string) as an extended file property. That way you don't change the structure of the file.

dsofile.dll is an ActiveX dll so it can be called from various languages, however it confines you to running on Windows. Here's more information on dsofile.dll: http://support.microsoft.com/kb/224351

Kluge
I believe that dsofile.dll only works with 'Structured Storage' files.
Michael Burr
+2  A: 

A common solution is to just use different files. Alongside each file simply have a file with the same file name with a different extension. For example: foobar.txt and foobar.txt.md5 (or .crc).

Wedge
+1  A: 

I wouldn't store the CRC in the file itself. I would have a single file ( I would use XML format ) that your program uses, with a list of filenames and their associated CRC values. No need to make it that complicated.

KPexEA
+2  A: 

The common solution which is widely used in communication protocols is to set the CRC field to 0, compute the CRC and then place it instead of the 0. The checking code should do the reverse process - read the CRC, zero the field, calculate the CRC and compare.

Also, for a file checksum I strongly recommend MD5 instead of CRC.

shoosh