30.11.2013 - New PICollection namespace, Android support, my own PIVector implementation
This commit is contained in:
25
picrc.h
25
picrc.h
@@ -165,12 +165,14 @@ inline std::ostream & operator <<(std::ostream & s, const uint_cl<L> & v) {std::
|
||||
template <uint L>
|
||||
class PIP_EXPORT PICRC {
|
||||
public:
|
||||
PICRC(const uint_cl<L> & poly) {poly_ = poly; reverse_poly = true; init_ = uint_cl<L>(0).inversed(); out_ = uint_cl<L>(0).inversed(); initTable();}
|
||||
PICRC(const uint_cl<L> & poly, bool reverse, const uint_cl<L> & initial, const uint_cl<L> & out_xor) {poly_ = poly; reverse_poly = reverse; init_ = initial; out_ = out_xor; initTable();}
|
||||
PICRC(const uint_cl<L> & poly) {poly_ = poly; reverse_poly = true; init_ = uint_cl<L>(0).inversed(); out_ = uint_cl<L>(0).inversed(); reverse_before_xor = reverse_data = false; initTable();}
|
||||
PICRC(const uint_cl<L> & poly, bool reverse_poly_, const uint_cl<L> & initial, const uint_cl<L> & out_xor) {poly_ = poly; reverse_poly = reverse_poly_; init_ = initial; out_ = out_xor; reverse_before_xor = reverse_data = false; initTable();}
|
||||
|
||||
void setInitial(const uint_cl<L> & v) {init_ = v;}
|
||||
void setOutXor(const uint_cl<L> & v) {out_ = v;}
|
||||
void setReversePolynome(bool yes) {reverse_poly = yes; initTable();}
|
||||
void setReverseOutBeforeXOR(bool yes) {reverse_before_xor = yes;}
|
||||
void setReverseDataBytes(bool yes) {reverse_data = yes;}
|
||||
|
||||
void initTable() {
|
||||
uint_cl<L> tmp, pol = reverse_poly ? poly_.reversed() : poly_;
|
||||
@@ -186,14 +188,17 @@ public:
|
||||
|
||||
uint_cl<L> calculate(const void * data, int size) {
|
||||
uint_cl<L> crc = init_;
|
||||
uchar * data_ = (uchar * )data;
|
||||
uchar * data_ = (uchar * )data, cb;
|
||||
//cout << "process " << size << endl;
|
||||
uchar nTemp;
|
||||
for (int i = 0; i < size; ++i) {
|
||||
nTemp = data_[i] ^ uchar(crc);
|
||||
cb = data_[i];
|
||||
if (reverse_data) cb = reverseByte(cb);
|
||||
nTemp = cb ^ uchar(crc);
|
||||
crc = crc >> 8;
|
||||
crc = crc ^ table[nTemp];
|
||||
}
|
||||
if (reverse_before_xor) crc = crc.reversed();
|
||||
return crc ^ out_;
|
||||
|
||||
}
|
||||
@@ -201,9 +206,19 @@ public:
|
||||
uint_cl<L> calculate(const char * str) {string s(str); return calculate((void * )s.data(), s.size());}
|
||||
|
||||
private:
|
||||
uchar reverseByte(uchar b) {
|
||||
uchar ret = 0;
|
||||
bool bit;
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
bit = 1 & (b >> (7 - i));
|
||||
if (bit) ret |= (1 << i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint_cl<L> table[256];
|
||||
uint_cl<L> poly_, init_, out_;
|
||||
bool reverse_poly;
|
||||
bool reverse_poly, reverse_before_xor, reverse_data;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user