FITS Documents
Next: Alternate Checksum Algorithms
Up: Discussion
Previous: Checksum Keyword Alignment
The following is an example of how the CHECKSUM, DATASUM,
and CHECKVER keywords are used. For the default case of the
ASCII coded 32-bit 1's complement checksum, omitting the CHECKVER
keyword is preferred, but it is included here for the sake of completeness.
The example uses a primary FITS header, but the identical usage
applies for extension headers.
1 2 3 4 5 6
12345678901234567890123456789012345678901234567890123456789012345...
SIMPLE = T / standard FITS format
...
CHECKSUM= 'hcHjjc9ghcEghc9g' / ASCII 1's complement checksum
DATASUM = '2503531142' / checksum of data records
CHECKVER= 'COMPLEMENT' / checksum version ID
END
To calculate the two checksums, first accumulate the checksum of the
data records, format this unsigned integer into a character string (for
portability) and update the DATASUM keyword. Next, set the
CHECKSUM keyword value to '0000000000000000' and calculate
the checksum of the header records. Add the DATASUM to the header
checksum and bit complement the total. This is the 1's complement
additive inverse of the total. ASCII encode this value and write it
into the CHECKSUM keyword, replacing the 0's. The checksum of
the FITS HDU is now zeroed. A few points:
- If this same procedure is followed for all extensions in any
given FITS file as well as for the primary HDU, the
checksum of the entire file will be zeroed since each
extension's checksum is the additive identity element.
- To later verify a FITS file or an individual extension,
simply accumulate the checksum and compare it to negative zero
(all one's).
- To update the checksum after modifying some part of the
FITS file, subtract from the total checksum the
previous checksum of the changed FITS records and
add the new checksum of those records.
- To invert the ASCII encoding, permute the encoded string back
to an alignment of zero bytes, subtract the hex 30 offset
and calculate the checksum of the resulting string. This can
be used, for instance, to read the value of CHECKSUM into
the software when verifying or updating a file.
- Any ASCII encoding algorithm could potentially be used that
represents the pertinent unsigned integer as a 16 character
string with a per byte offset of hex 30 (for instance, not
excluding the punctuation). The encoding defined by example
in appendices A and B is recommended by this proposal, however.
Restraints on allowed encodings are discussed in §3.6.
FITS Documents
Next: Alternate Checksum Algorithms
Up: Discussion
Previous: Checksum Keyword Alignment