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

Last change on this file since 16863 was 16670, checked in by tbretz, 11 years ago
Removed the include for arpa/inet.h for CINT to make CINT be happy.
File size: 4.8 KB
Line 
1#ifndef MARS_checksum
2#define MARS_checksum
3
4#ifndef __CINT__
5#include <arpa/inet.h>
6#endif
7
8namespace std
9{
10
11class Checksum
12{
13public:
14 uint64_t buffer;
15
16 void reset() { buffer = 0; }
17 Checksum() : buffer(0) { }
18 Checksum(const Checksum &sum) : buffer(sum.buffer) { }
19 Checksum(uint64_t v) : buffer(((v>>16)&0xffff) | ((v&0xffff)<<32)) { }
20
21 uint32_t val() const { return (((buffer&0xffff)<<16) | ((buffer>>32)&0xffff)); }
22
23 bool valid() const { return buffer==0xffff0000ffff; }
24
25 void HandleCarryBits()
26 {
27 while (1)
28 {
29 const uint64_t carry = ((buffer>>48)&0xffff) | ((buffer&0xffff0000)<<16);
30 if (!carry)
31 break;
32
33 buffer = (buffer&0xffff0000ffff) + carry;
34 }
35 }
36
37 Checksum &operator+=(const Checksum &sum)
38 {
39 buffer += sum.buffer;
40 HandleCarryBits();
41 return *this;
42 }
43 Checksum operator+(Checksum sum) const
44 {
45 return (sum += *this);
46 }
47
48
49 bool add(const char *buf, size_t len)
50 {
51 // Avoid overflows in carry bits
52 if (len>262140) // 2^18-4
53 {
54 add(buf, 262140);
55 return add(buf+262140, len-262140);
56 }
57
58 if (len%4>0)
59 {
60 ostringstream sout;
61 sout << "Length " << len << " not dividable by 4." << endl;
62
63#ifdef __EXCEPTIONS
64 throw runtime_error(sout.str());
65#else
66 gLog << ___err___ << "ERROR - " << sout.str() << endl;
67 return false;
68#endif
69 }
70
71 const uint16_t *sbuf = reinterpret_cast<const uint16_t *>(buf);
72
73 uint32_t *hilo = reinterpret_cast<uint32_t*>(&buffer);
74
75 /*
76 for (size_t i = 0; i < len/2; i++)
77 {
78 //swap the bytes of the 32 bits value. but...
79 //the hi and lo values are stored in fits-like order. do not swap them
80 hilo[i%2] += ntohs(sbuf[i]); //(sbuf[i]&0xff00)>>8 | (sbuf[i]&0x00ff)<<8;
81 }*/
82
83 // This is about as twice as fast as the loop above
84 // ntohs is CPU optimized, i%2 doesn't need to be computed
85 const uint16_t *end = sbuf + len/2;
86 while (1)
87 {
88 if (sbuf==end)
89 break;
90
91 hilo[0] += ntohs(*sbuf++);
92
93 if (sbuf==end)
94 break;
95
96 hilo[1] += ntohs(*sbuf++);
97 }
98
99 HandleCarryBits();
100
101 return true;
102 }
103
104 bool add(const vector<char> &v)
105 {
106 return add(v.data(), v.size());
107 }
108
109 string str(bool complm=true) const
110 {
111 string rc(16,0);
112
113 const uint8_t exclude[13] =
114 {
115 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
116 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60
117 };
118
119 const uint32_t value = complm ? ~val() : val(); // complement each bit of the value
120
121 for (int ii = 0; ii < 4; ii++)
122 {
123 uint8_t byte = (value >> (24 - (8 * ii)));
124
125 const uint8_t quotient = byte / 4 + '0';
126 const uint8_t remainder = byte % 4;
127
128 uint32_t ch[4] = { quotient+remainder, quotient, quotient, quotient };
129
130 // avoid ASCII punctuation
131 while (1)
132 {
133 bool check = false;
134 for (int kk = 0; kk < 13; kk++)
135 {
136 for (int jj = 0; jj < 4; jj += 2)
137 {
138 if (ch[jj] != exclude[kk] && ch[jj+1] != exclude[kk])
139 continue;
140
141 ch[jj]++;
142 ch[jj+1]--;
143 check++;
144 }
145 }
146
147 if (!check)
148 break;
149 }
150
151 for (int jj = 0; jj < 4; jj++) // assign the bytes
152 rc[4*jj+ii] = ch[jj];
153 }
154
155 const char lastChar = rc[15];
156 for (int i=15;i>0;i--)
157 rc[i] = rc[i-1];
158 rc[0] = lastChar;
159 return rc;
160/*
161 uint8_t *p = reinterpret_cast<uint8_t*>(&value);
162 //swap the bytes of the value
163 uint8_t temp;
164 temp = p[0];
165 p[0] = p[3];
166 p[3] = temp;
167 temp = p[1];
168 p[1] = p[2];
169 p[2] = temp;
170
171 for (int i=0; i<4; i++)
172 {
173 rc[i+ 0] = '0' + p[i]/4 + p[i]%4;
174 rc[i+ 4] = '0' + p[i]/4;
175 rc[i+ 8] = '0' + p[i]/4;
176 rc[i+12] = '0' + p[i]/4;
177 }
178
179 while(1)
180 {
181 bool ok = true;
182 for (int i=0; i<16-4; i++)
183 {
184 for (int j=0; j<13; j++)
185 if (rc[i]==exclude[j] || rc[(i+4)%16]==exclude[j])
186 {
187 rc[i]++;
188 rc[(i+4)%16]--;
189 ok = false;
190 }
191 }
192
193 if (ok)
194 break;
195 }
196
197
198 return rc;
199 */
200 }
201};
202}
203
204#endif
Note: See TracBrowser for help on using the repository browser.