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