crc - OWI_ComputeCRC16 returning value 0 -
i checking out function owi_computecrc16() , when test function owi_computecrc16(0 >> 8, 0), return value 0.
/** * @brief compute crc16 value of data set. * @details function compute crc16 of indata using seed inital value crc. * @note setting seed 0 computes crc16 of indata. * @note passing return value of function seed argument computes crc16 value of longer string of data. * @bug n/a * @warning n/a * @param unsigned char indata: 1 byte of data compute crc from. * @param unsigned short seed: starting value of crc. * @return crc16 of indata seed initial value. */ unsigned short owi_computecrc16(unsigned char indata, unsigned short seed) { unsigned char bitsleft; unsigned char temp; (bitsleft = 8; bitsleft > 0; bitsleft--) { temp = ((seed ^ indata) & 0x01); if (temp == 0) { seed >>= 1; } else { seed ^= 0x4002; seed >>= 1; seed |= 0x8000; } indata >>= 1; } return seed; }
this making problems in legacy code have solve, function used parameters 0, 0 , sum of calculated crc values used.
example:
a) have data on example position = 0 key = 0 ... => owi_computecrc16(0 >> 8, 0) returns 0.
b) have data on example position = 0 key = 0 ... crc = 0. , have data record on example position = 1 key = 1 ... owi_computecrc16(1 >> 8, 1) returns 49345.
if sum on returned crc values, not know, if data position 0 included or not.
ok, think logical error , can solve it.
the question is: ok function, should calculate , return crc value, return value 0 (for possible given input values)?
p.s.: first question on stackoverflow, if did wrong or not understandable, please let me know. thanks.
the question is: ok function, should calculate , return crc value, return value 0 (for possible given input values)?
not any, sequences of bytes 0 return value. in fact 16-bit crc, should expect 0 crc for, on average, 1 out of every 65536 random sequences try.
this particular crc, no pre or post conditioning, give 0 crc empty sequence , sequence of zeros.
by way, odd implementation lot more operations needed. should replaced this:
unsigned short owi_computecrc16(unsigned char indata, unsigned short seed) { seed ^= indata; (int k = 0; k < 8; k++) seed = seed & 1 ? (seed >> 1) ^ 0xa001 : seed >> 1; return seed; }
that doesn't obfuscate crc polynomial, in version familiar (reflected) 0xa001 or (not reflected) polynomial 0x8005, x16+x15+x2+1. faster still byte-wise or word-wise table-driven version. see crcany.c examples.
Comments
Post a Comment