gri6507 has asked for the wisdom of the Perl Monks concerning the following question:
I have a situation where the CRC32 computed in my C program does not agree with the CRC32 computed in Perl. I've looked high and low but cannot find the problem. I was hoping that some of you can shed some light on this issue. I'm honestly not sure at this point whether it is a Perl or a C issue :-).
and in Perl I have#include <stdio.h> #include <stdlib.h> #define CCITT_4 0x0000000C // 4-bit #define CRC_4 0x0000000F // 4-bit #define CRC_5 0x0000001A // 5-bit #define USB_5 0x00000014 // 5-bit #define CCITT_5 0x0000001A // 5-bit (same as CRC-5) #define CRC_6 0x00000030 // 6-bit #define DARC_6 0x00000026 // 6-bit #define CRC_7 0x00000044 // 7-bit #define FT2 0x00000053 // 7-bit #define CRC_8 0x000000AB // 8-bit #define DARC_8 0x0000009C // 8-bit #define ATM_8 0x000000E0 // 8-bit #define C2 0x000000F4 // 8-bit #define DOWCRC 0x0000008C // 8-bit #define WCDMA_8 0x000000D9 // 8-bit #define CRC_10 0x00000331 // 10-bit #define CRC_12 0x00000F08 // 12-bit #define CRC_12B 0x00000F01 // 12-bit alternative CRC +-12 #define DARC_14 0x00002804 // 14-bit #define CAN 0x00004CD1 // 15-bit #define CRC_16 0x0000C002 // 16-bit #define ANSI_16 0x0000A001 // 16-bit #define C3 0x0000AC9A // 16-bit #define EIC_16 0x0000C9DA // 16-bit #define CCITT_16 0x00008408 // 16-bit #define CRC_CCITT CCITT_16 #define CRC_32 0xEDB88320 // 32-bit void generator(unsigned int lPolynomial, unsigned int *mLookup, unsign +ed int *mMask) { unsigned int lLookupIndex; unsigned int lBit; unsigned int lCrc; // Set the mask to the size of the generator polynomial. *mMask = 0xffffffff; while ((*mMask >> 1) > lPolynomial) *mMask >>= 1; // Generate the lookup table from the given polynomial. for (lLookupIndex = 0; lLookupIndex < 256; ++lLookupIndex) { lCrc = lLookupIndex; for (lBit = 0; lBit < 8; ++lBit) { if (lCrc & 1) lCrc = (lCrc >> 1) ^ lPolynomial; else lCrc >>= 1; } mLookup[lLookupIndex] = lCrc; } } unsigned int crc(const char *lBuffer, unsigned int lLength, unsigned i +nt *mLookup, unsigned int *mMask) { const char *lEndOfBuffer = lBuffer + lLength; unsigned int lCrc = *mMask; while (lBuffer < lEndOfBuffer) { lCrc = (lCrc >> 8) ^ mLookup[(lCrc ^ *lBuffer++) & 0xff]; } return lCrc; } int main(void) { unsigned int mLookup[256]; unsigned int mMask; unsigned int mCrc; char s[] = "Hello Word. How are you?"; generator(CRC_32, mLookup, &mMask); mCrc = crc(s, sizeof(s), mLookup, &mMask); printf("CRC for '%s' is 0x%08x\n", s, mCrc); }
Running the two gives me different results.use strict; use warnings; use Digest::CRC; my $string = "Hello Word. How are you?"; my $ctx = Digest::CRC->new(type=>"crc32"); $ctx->add($string); print "CRC for '$string' is 0x" . $ctx->hexdigest . "\n";
gcc try.c; ./a.out CRC for 'Hello Word. How are you?' is 0x17ccc647 perl try.pl CRC for 'Hello Word. How are you?' is 0x8487fb61
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: computing CRC32 in Perl and C
by ikegami (Patriarch) on Dec 14, 2007 at 03:42 UTC | |
|
Re: computing CRC32 in Perl and C
by FunkyMonk (Bishop) on Dec 13, 2007 at 22:47 UTC | |
|
Re: computing CRC32 in Perl and C
by roboticus (Chancellor) on Dec 13, 2007 at 20:21 UTC | |
by gri6507 (Deacon) on Dec 13, 2007 at 20:31 UTC | |
by roboticus (Chancellor) on Dec 13, 2007 at 20:43 UTC |