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

CRC-32C incorrect #5

Closed nemequ closed 9 years ago

nemequ commented 9 years ago

I'm trying to use code generated by ./pycrc.py --model crc-32c --algorithm table-driven --generate c / ./pycrc.py --model crc-32c --algorithm table-driven --generate h to generate snappy-framed data, but the checksums don't match what other implementations expect.

Appendix B.4 of RFC 3720 includes some test vectors, so I threw together a quick program to verify pycrc's implementation and it fails:

#include "crc32.h"
#include <assert.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
  const uint8_t test_vectors[][32] = {
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
      0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
    { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
      0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
    { 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
      0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,
      0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
      0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }
  };

  assert (0xaa36918a == crc_finalize (crc_update(crc_init (), test_vectors[0], sizeof(test_vectors[0]))));
  assert (0x43aba862 == crc_finalize (crc_update(crc_init (), test_vectors[1], sizeof(test_vectors[1]))));
  assert (0x4e79dd46 == crc_finalize (crc_update(crc_init (), test_vectors[2], sizeof(test_vectors[2]))));
  assert (0x5cdb3f11 == crc_finalize (crc_update(crc_init (), test_vectors[3], sizeof(test_vectors[3]))));

  return 0;
}
tpircher-zz commented 9 years ago

Hi Evan,

I think the problem is just byte ordering. In your example I get the following checksums: 0xaa36918a =?= 0x8a9136aa 0x43aba862 =?= 0x62a8ab43 0x4e79dd46 =?= 0x46dd794e 0x5cdb3f11 =?= 0x113fdb5c

nemequ commented 9 years ago

Facepalm

Thanks, sorry for the noise.