Index: trunk/Mars/mcore/fits.h
===================================================================
--- trunk/Mars/mcore/fits.h	(revision 12995)
+++ trunk/Mars/mcore/fits.h	(revision 12996)
@@ -41,4 +41,6 @@
 #endif
 
+#include "checksum.h"
+
 namespace std
 {
@@ -88,4 +90,6 @@
         Columns cols;
         Keys    keys;
+
+        int64_t datasum;
 
         string Trim(const string &str, char c=' ') const
@@ -218,4 +222,5 @@
             num_rows      = Get<size_t>("NAXIS2");
             num_cols      = Get<size_t>("TFIELDS");
+            datasum       = Get<int64_t>("DATASUM", -1);
 
             size_t bytes = 0;
@@ -382,5 +387,5 @@
         // Values of keys are always signed
         template<typename T>
-            T Get(const string &key, const string &deflt) const
+            T Get(const string &key, const T &deflt) const
         {
             const map<string,Entry>::const_iterator it = keys.find(key);
@@ -416,4 +421,7 @@
 
     size_t fRow;
+
+    Checksum fChkHeader;
+    Checksum fChkData;
 
     bool ReadBlock(vector<string> &vec)
@@ -428,4 +436,6 @@
                 break;
 
+            fChkHeader.add(c, 80);
+
 //            if (c[0]==0)
 //                return vector<string>();
@@ -527,5 +537,8 @@
 
             if (block[0].substr(0, 9)=="SIMPLE  =")
+            {
+                fChkHeader.reset();
                 continue;
+            }
 
             if (block[0].substr(0, 9)=="XTENSION=")
@@ -544,5 +557,5 @@
                 }
 
-                fBufferRow.resize(fTable.bytes_per_row);
+                fBufferRow.resize(fTable.bytes_per_row + 4-fTable.bytes_per_row%4);
                 fBufferDat.resize(fTable.bytes_per_row);
 
@@ -565,13 +578,35 @@
     }
 
-    void ReadRow(size_t row)
+    uint8_t ReadRow(size_t row)
     {
         // if (row!=fRow+1) // Fast seeking is ensured by izstream
         seekg(fTable.offset+row*fTable.bytes_per_row);
 
+        // For the checksum we need everything to be correctly aligned
+        const uint8_t offset = (row*fTable.bytes_per_row)%4;
+
+        auto ib = fBufferRow.begin();
+        auto ie = fBufferRow.end();
+        *ib++ = 0;
+        *ib++ = 0;
+        *ib++ = 0;
+        *ib   = 0;
+
+        *--ie = 0;
+        *--ie = 0;
+        *--ie = 0;
+        *--ie = 0;
+
+        fBufferRow.assign(fBufferRow.size(), 0);
+
+        read(fBufferRow.data()+offset, fTable.bytes_per_row);
+        //fin.clear(fin.rdstate()&~ios::eofbit);
+
+        if (row==fRow+1)
+            fChkData.add(fBufferRow);
+
         fRow = row;
 
-        read(fBufferRow.data(), fBufferRow.size());
-        //fin.clear(fin.rdstate()&~ios::eofbit);
+        return offset;
     }
 
@@ -586,13 +621,15 @@
     bool GetRow(size_t row)
     {
-        ReadRow(row);
+        const uint8_t offset = ReadRow(row);
         if (!good())
             return good();
 
+        const char *ptr = fBufferRow.data() + offset;
+
         for (Addresses::const_iterator it=fAddresses.begin(); it!=fAddresses.end(); it++)
         {
             const Table::Column &c = it->second;
 
-            const char *src = fBufferRow.data() + c.offset;
+            const char *src = ptr + c.offset;
             char *dest = reinterpret_cast<char*>(it->first);
 
@@ -782,4 +819,8 @@
     void PrintKeys() const { fTable.PrintKeys(); }
     void PrintColumns() const { fTable.PrintColumns(); }
+
+    bool IsHeaderOk() const { return fTable.datasum<0?false:(fChkHeader+Checksum(fTable.datasum)).valid(); }
+    bool IsFileOk() const { return (fChkHeader+fChkData).valid(); }
+
 };
 
