Fellow Monks,

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 :-).

In C 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); }
and in Perl I have
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";
Running the two gives me different results.
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
Does anyone have any ideas??

In reply to computing CRC32 in Perl and C by gri6507

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.