c++ - Check function Compare Issue -
im writing function compares 32bit crc extracted buffer (uint32_t lbuffer[10]
) first ten entries contain randomly generated data, calculated crc computed within checkcrc
function. can see why doesn't want give true result? appreciated!
here function code:
bool crc32::checkcrc(const uint32_t* pldata , uint32_t llength, uint32_t previouscrc32) { bool flagpass; uint32_t lcalccrc,lmsgcrc; //msg crc needs extracted first lmsgcrc = pldata[llength-1]; //newly calculated crc //lcalccrc = calculate_crc32(pldata,llength-1,linitcrc); lcalccrc = ~previouscrc32; unsigned char* current = (unsigned char*) pldata; while (llength--) { lcalccrc = (lcalccrc >> 8) ^ crc_table[(lcalccrc & 0xff) ^ *current++]; } lcalccrc = ~lcalccrc; if (lcalccrc == lmsgcrc) { flagpass = true; } else { flagpass = false; } return flagpass; }
the problem in how hare handling length of data buffer. when retrieve crc of buffer pldata[llength-1]
length interpreted number of elements in array. later when iterate on buffer length assumed number of bytes in buffer.
if llength
represents number of bytes need compensate when retrieving existing crc value buffer. need adjust size of buffer existing crc not included when iterate on buffer.
uint32_t lmsgcrc = *((const uint32_t*)((const char*)pldata + llength) - 1); llength -= sizeof(pldata[0]);
if llength
represents number of elements in array rather number of bytes need adjust size. again need take consideration existing crc @ end of buffer.
// adjust length size of elements llength = (llength - sizeof(pldata[0])) * sizeof(pldata[0]); while (llength--) { lcalccrc = (lcalccrc >> 8) ^ crc_table[(lcalccrc & 0xff) ^ *current++]; }
Comments
Post a Comment