tpircher-zz / pycrc

Free, easy to use Cyclic Redundancy Check (CRC) calculator and source code generator
https://pycrc.org
MIT License
169 stars 36 forks source link

Model CRC-5 generates a left shift by 8 bits which is too big for data type #6

Closed RedX2501 closed 9 years ago

RedX2501 commented 9 years ago

When using the following command line:

/pycrc.py --model crc-5 --algorithm table-driven --std=c89 --generate c -o source.c

a source.c is generated with following crc_update:

 crc_t crc_update(crc_t crc, const unsigned char *data, size_t data_len)
 {
     unsigned int tbl_idx;

     while (data_len--) {
         tbl_idx = ((crc >> 3) ^ *data) & 0xff;
         crc = (crc_table[tbl_idx] ^ (crc >> 8)) & (0x1f << 3);

         data++;
     }
     return crc & (0x1f << 3);

with crc_t typedef'ed to unsigned char.

This means that the crc >> 8 is always 0. Is this intended?

pycrc.py --version
pycrc v0.8.2
tpircher-zz commented 9 years ago

Thanks for the report and sorry for the late response. This is/was a bug and should be fixed with 61f397b.

The update function now is:

crc_t crc_update(crc_t crc, const void *data, size_t data_len)
{
    const unsigned char *d = (const unsigned char *)data;
    unsigned int tbl_idx;

    while (data_len--) {
        tbl_idx = ((crc >> 3) ^ *d);
        crc = (crc_table[tbl_idx]) & (0x1f << 3);

        d++;
    }
    return crc & (0x1f << 3);
}