18 Checksum(uint64_t v) : buffer(((v>>16)&0xffff) | ((v&0xffff)<<32)) { }
20 uint32_t
val()
const {
return (((buffer&0xffff)<<16) | ((buffer>>32)&0xffff)); }
22 bool valid()
const {
return buffer==0xffff0000ffff; }
28 const uint64_t carry = ((buffer>>48)&0xffff) | ((buffer&0xffff0000)<<16);
32 buffer = (buffer&0xffff0000ffff) + carry;
45 return (sum += *
this);
49 bool add(
const char *buf,
size_t len,
bool big_endian =
true)
55 return add(buf+262140, len-262140);
60 std::ostringstream sout;
61 sout <<
"Length " << len <<
" not dividable by 4";
64 throw std::runtime_error(sout.str());
71 const uint16_t *sbuf =
reinterpret_cast<const uint16_t *
>(buf);
73 uint32_t *hilo =
reinterpret_cast<uint32_t*
>(&
buffer);
76 const uint16_t *
end = sbuf + len/2;
118 hilo[0] += ntohs(*sbuf++);
123 hilo[1] += ntohs(*sbuf++);
127 void addLoop(
const uint16_t *sbuf,
const uint16_t *
end, uint32_t* hilo)
134 hilo[0] += ntohs(*sbuf++);
139 hilo[1] += ntohs(*sbuf++);
143 bool add(
const std::vector<char> &v,
bool big_endian =
true)
145 return add(v.data(), v.size(), big_endian);
148 std::string
str(
bool complm=
true)
const 150 std::string rc(16,0);
152 const uint8_t exclude[13] =
154 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
155 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60
158 const uint32_t value = complm ? ~
val() :
val();
160 for (
int ii = 0; ii < 4; ii++)
162 uint8_t
byte = (value >> (24 - (8 * ii)));
164 const uint8_t quotient = byte / 4 +
'0';
165 const uint8_t remainder = byte % 4;
167 uint32_t ch[4] = { uint32_t(quotient+remainder), quotient, quotient, quotient };
173 for (
int kk = 0; kk < 13; kk++)
175 for (
int jj = 0; jj < 4; jj += 2)
177 if (ch[jj] != exclude[kk] && ch[jj+1] != exclude[kk])
190 for (
int jj = 0; jj < 4; jj++)
191 rc[4*jj+ii] = ch[jj];
194 const char lastChar = rc[15];
195 for (
int i=15;
i>0;
i--)
Checksum(const Checksum &sum)
void addLoop(const uint16_t *sbuf, const uint16_t *end, uint32_t *hilo)
Checksum & operator+=(const Checksum &sum)
Checksum operator+(Checksum sum) const
std::string str(bool complm=true) const
bool add(const std::vector< char > &v, bool big_endian=true)
void addLoopSwapping(const uint16_t *sbuf, const uint16_t *end, uint32_t *hilo)
bool add(const char *buf, size_t len, bool big_endian=true)