source: trunk/Mars/mcore/checksum.h@ 12995

Last change on this file since 12995 was 12995, checked in by tbretz, 13 years ago
First version of a fits writer.
File size: 3.9 KB
Line 
1#ifndef MARS_checksum
2#define MARS_checksum
3
4namespace std
5{
6
7class Checksum
8{
9public:
10 uint64_t buffer;
11
12 void reset() { buffer = 0; }
13 Checksum() : buffer(0) { }
14 Checksum(const Checksum &sum) : buffer(sum.buffer) { }
15 Checksum(uint64_t v) : buffer(((v>>16)&0xffff) | ((v&0xffff)<<32)) { }
16
17 uint32_t val() const { return (((buffer&0xffff)<<16) | ((buffer>>32)&0xffff)); }
18
19 bool valid() const { return buffer==0xffff0000ffff; }
20
21 void HandleCarryBits()
22 {
23 while (1)
24 {
25 const uint64_t carry = ((buffer>>48)&0xffff) | ((buffer&0xffff0000)<<16);
26 if (!carry)
27 break;
28
29 buffer = (buffer&0xffff0000ffff) + carry;
30 }
31 }
32
33 Checksum &operator+=(const Checksum &sum)
34 {
35 buffer += sum.buffer;
36 HandleCarryBits();
37 return *this;
38 }
39 Checksum operator+(Checksum sum) const
40 {
41 return (sum += *this);
42 }
43
44
45 bool add(const char *buf, size_t len)
46 {
47 // Avoid overflows in carry bits
48 if (len>262140) // 2^18-4
49 {
50 add(buf, 262140);
51 return add(buf+262140, len-262140);
52 }
53
54 if (len%4>0)
55 {
56 ostringstream sout;
57 sout << "Length " << len << " not dividable by 4." << endl;
58
59#ifdef __EXCEPTIONS
60 throw runtime_error(sout.str());
61#else
62 gLog << ___err___ << "ERROR - " << sout.str() << endl;
63 return false;
64#endif
65 }
66
67 const unsigned short *sbuf = reinterpret_cast<const unsigned short *>(buf);
68
69 uint32_t *hilo = reinterpret_cast<uint32_t*>(&buffer);
70
71 for (size_t ii = 0; ii < len/2; ii++)
72 hilo[(ii+1)%2] += sbuf[ii];
73
74 HandleCarryBits();
75
76 return true;
77 }
78
79 bool add(const vector<char> &v)
80 {
81 return add(v.data(), v.size());
82 }
83
84 string str(bool complm=true) const
85 {
86 string rc(16,0);
87
88 const uint8_t exclude[13] =
89 {
90 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
91 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60
92 };
93
94 const uint32_t value = complm ? ~val() : val(); // complement each bit of the value
95/*
96 for (int ii = 0; ii < 4; ii++)
97 {
98 uint8_t byte = (value >> (24 - (8 * ii)));
99
100 const uint8_t quotient = byte / 4 + offset;
101 const uint8_t remainder = byte % 4;
102
103 uint32_t ch[4] = { quotient+remainder, quotient, quotient, quotient };
104
105 // avoid ASCII punctuation
106 while (1)
107 {
108 bool check = false;
109 for (int kk = 0; kk < 13; kk++)
110 {
111 for (int jj = 0; jj < 4; jj += 2)
112 {
113 if (ch[jj] != exclude[kk] && ch[jj+1] != exclude[kk])
114 continue;
115
116 ch[jj]++;
117 ch[jj+1]--;
118 check++;
119 }
120 }
121
122 if (!check)
123 break;
124 }
125
126 for (int jj = 0; jj < 4; jj++) // assign the bytes
127 rc[4*jj+ii] = ch[jj];
128 }
129
130 return rc;
131 */
132 const uint8_t *p = reinterpret_cast<const uint8_t*>(&value);
133
134 for (int i=0; i<4; i++)
135 {
136 rc[i+ 0] = '0' + p[i]/4 + p[i]%4;
137 rc[i+ 4] = '0' + p[i]/4;
138 rc[i+ 8] = '0' + p[i]/4;
139 rc[i+12] = '0' + p[i]/4;
140 }
141
142 while(1)
143 {
144 bool ok = true;
145 for (int i=0; i<16-4; i++)
146 {
147 for (int j=0; j<13; j++)
148 if (rc[i]==exclude[j] || rc[(i+4)%16]==exclude[j])
149 {
150 rc[i]++;
151 rc[(i+4)%16]--;
152 ok = false;
153 }
154 }
155
156 if (ok)
157 break;
158 }
159
160
161 return rc;
162 }
163};
164}
165
166#endif
Note: See TracBrowser for help on using the repository browser.