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

Last change on this file since 14933 was 13988, checked in by tbretz, 12 years ago
Added two lost const qulifiers.
File size: 4.4 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 i = 0; i < len/2; i++)
72 {
73 //swap the bytes of the 32 bits value. but...
74 //the hi and lo values are stored in fits-like order. do not swap them
75 hilo[i%2] += (sbuf[i]&0xff00)>>8 | (sbuf[i]&0x00ff)<<8;
76 }
77 HandleCarryBits();
78
79 return true;
80 }
81
82 bool add(const vector<char> &v)
83 {
84 return add(v.data(), v.size());
85 }
86
87 string str(bool complm=true) const
88 {
89 string rc(16,0);
90
91 const uint8_t exclude[13] =
92 {
93 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
94 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60
95 };
96
97 const uint32_t value = complm ? ~val() : val(); // complement each bit of the value
98
99 for (int ii = 0; ii < 4; ii++)
100 {
101 uint8_t byte = (value >> (24 - (8 * ii)));
102
103 const uint8_t quotient = byte / 4 + '0';
104 const uint8_t remainder = byte % 4;
105
106 uint32_t ch[4] = { quotient+remainder, quotient, quotient, quotient };
107
108 // avoid ASCII punctuation
109 while (1)
110 {
111 bool check = false;
112 for (int kk = 0; kk < 13; kk++)
113 {
114 for (int jj = 0; jj < 4; jj += 2)
115 {
116 if (ch[jj] != exclude[kk] && ch[jj+1] != exclude[kk])
117 continue;
118
119 ch[jj]++;
120 ch[jj+1]--;
121 check++;
122 }
123 }
124
125 if (!check)
126 break;
127 }
128
129 for (int jj = 0; jj < 4; jj++) // assign the bytes
130 rc[4*jj+ii] = ch[jj];
131 }
132
133 const char lastChar = rc[15];
134 for (int i=15;i>0;i--)
135 rc[i] = rc[i-1];
136 rc[0] = lastChar;
137 return rc;
138/*
139 uint8_t *p = reinterpret_cast<uint8_t*>(&value);
140 //swap the bytes of the value
141 uint8_t temp;
142 temp = p[0];
143 p[0] = p[3];
144 p[3] = temp;
145 temp = p[1];
146 p[1] = p[2];
147 p[2] = temp;
148
149 for (int i=0; i<4; i++)
150 {
151 rc[i+ 0] = '0' + p[i]/4 + p[i]%4;
152 rc[i+ 4] = '0' + p[i]/4;
153 rc[i+ 8] = '0' + p[i]/4;
154 rc[i+12] = '0' + p[i]/4;
155 }
156
157 while(1)
158 {
159 bool ok = true;
160 for (int i=0; i<16-4; i++)
161 {
162 for (int j=0; j<13; j++)
163 if (rc[i]==exclude[j] || rc[(i+4)%16]==exclude[j])
164 {
165 rc[i]++;
166 rc[(i+4)%16]--;
167 ok = false;
168 }
169 }
170
171 if (ok)
172 break;
173 }
174
175
176 return rc;
177 */
178 }
179};
180}
181
182#endif
Note: See TracBrowser for help on using the repository browser.