Cyclic Redundancy Check 32 (CRC32) C Code

crc32 , labwindows cvi , cyclic redundancy check , c code Edinburgh, United Kingdom
  • 8 years ago
    I have code that can create a CRC32 checksum for a given section of an MPEG2 Transmission Stream packet, and also check the CRC32 checksum of a given section of an MPEG2 Transmission Stream Packet. The section of code that checks the CRC is working as expected - I have a valid MPEG2 TS Packet and have run it through the code yielding the expected result. The problem I have is that the section of code to re-create the CRC32 checksum yields the wrong answer. I have checked the code against various sources and it is correct as far as I can see - perhaps I'm not using it correctly? Anyone offer any assistance? The below code shows the CRC32 check and create code. The CRC_Calc function works as expected, but the CRC_Make function is giving me the incorrect CRC32 checksum for the given values. I am developing using LabWindows CVI, so the code is based around the use of panels and button callbacks, but is hopefully still readable - if not let me know. Note that the CRC32 checksum is not calculated using the whole MPEG2 TS but rather one section of the payload - this is highlighted in the code (stufftdt function on line beginning *array++ = 0x73;). The code that is calling these functions is below the CRC32 code. static ULONG crcTable[256]; // Lookup table array void CRC_Init(void) { ULONG crc, poly; int i, j; poly = (ULONG)0xEDB88320L; for (i = 0; i < 256; i++) { crc = i; for (j = 8; j > 0; j--) { if (crc & 1) { crc = (crc >> 1) ^ poly; } else { crc >>= 1; } } crcTable[i] = crc; } } DWORD CRC_Make(unsigned char *Memory, DWORD Length) { char msg[80]={0}; ULONG theCrc; theCrc = 0xffffffffL; while(Length--) { sprintf(msg,"Byte: 0x%02X",*Memory); printTexboxLine(msg); theCrc = (theCrc >> 8) ^ crcTable[(theCrc & 0xFF) ^ *Memory++];//((theCrc >> 8) & 0x00FFFFFFL) ^ crcTable[(theCrc ^* Memory++) & 0xFFL]; } return theCrc; } static const DWORD crc32tab[] = { (DWORD) 0x0L ,(DWORD) 0x04C11DB7L, (DWORD) 0x09823B6EL, (DWORD) 0x0D4326D9L, (DWORD) 0x130476DCL, (DWORD) 0x17C56B6BL ,(DWORD) 0x1A864DB2L, (DWORD) 0x1E475005L, (DWORD) 0x2608EDB8L, (DWORD) 0x22C9F00FL, (DWORD) 0x2F8AD6D6L ,(DWORD) 0x2B4BCB61L, (DWORD) 0x350C9B64L, (DWORD) 0x31CD86D3L, (DWORD) 0x3C8EA00AL, (DWORD) 0x384FBDBDL ,(DWORD) 0x4C11DB70L, (DWORD) 0x48D0C6C7L, (DWORD) 0x4593E01EL, (DWORD) 0x4152FDA9L, (DWORD) 0x5F15ADACL ,(DWORD) 0x5BD4B01BL, (DWORD) 0x569796C2L, (DWORD) 0x52568B75L, (DWORD) 0x6A1936C8L, (DWORD) 0x6ED82B7FL ,(DWORD) 0x639B0DA6L, (DWORD) 0x675A1011L, (DWORD) 0x791D4014L, (DWORD) 0x7DDC5DA3L, (DWORD) 0x709F7B7AL ,(DWORD) 0x745E66CDL, (DWORD) 0x9823B6E0L, (DWORD) 0x9CE2AB57L, (DWORD) 0x91A18D8EL, (DWORD) 0x95609039L ,(DWORD) 0x8B27C03CL, (DWORD) 0x8FE6DD8BL, (DWORD) 0x82A5FB52L, (DWORD) 0x8664E6E5L, (DWORD) 0xBE2B5B58L ,(DWORD) 0xBAEA46EFL, (DWORD) 0xB7A96036L, (DWORD) 0xB3687D81L, (DWORD) 0xAD2F2D84L, (DWORD) 0xA9EE3033L ,(DWORD) 0xA4AD16EAL, (DWORD) 0xA06C0B5DL, (DWORD) 0xD4326D90L, (DWORD) 0xD0F37027L, (DWORD) 0xDDB056FEL ,(DWORD) 0xD9714B49L, (DWORD) 0xC7361B4CL, (DWORD) 0xC3F706FBL, (DWORD) 0xCEB42022L, (DWORD) 0xCA753D95L ,(DWORD) 0xF23A8028L, (DWORD) 0xF6FB9D9FL, (DWORD) 0xFBB8BB46L, (DWORD) 0xFF79A6F1L, (DWORD) 0xE13EF6F4L ,(DWORD) 0xE5FFEB43L, (DWORD) 0xE8BCCD9AL, (DWORD) 0xEC7DD02DL, (DWORD) 0x34867077L, (DWORD) 0x30476DC0L ,(DWORD) 0x3D044B19L, (DWORD) 0x39C556AEL, (DWORD) 0x278206ABL, (DWORD) 0x23431B1CL, (DWORD) 0x2E003DC5L ,(DWORD) 0x2AC12072L, (DWORD) 0x128E9DCFL, (DWORD) 0x164F8078L, (DWORD) 0x1B0CA6A1L, (DWORD) 0x1FCDBB16L ,(DWORD) 0x018AEB13L, (DWORD) 0x054BF6A4L, (DWORD) 0x0808D07DL, (DWORD) 0x0CC9CDCAL, (DWORD) 0x7897AB07L ,(DWORD) 0x7C56B6B0L, (DWORD) 0x71159069L, (DWORD) 0x75D48DDEL, (DWORD) 0x6B93DDDBL, (DWORD) 0x6F52C06CL ,(DWORD) 0x6211E6B5L, (DWORD) 0x66D0FB02L, (DWORD) 0x5E9F46BFL, (DWORD) 0x5A5E5B08L, (DWORD) 0x571D7DD1L ,(DWORD) 0x53DC6066L, (DWORD) 0x4D9B3063L, (DWORD) 0x495A2DD4L, (DWORD) 0x44190B0DL, (DWORD) 0x40D816BAL ,(DWORD) 0xACA5C697L, (DWORD) 0xA864DB20L, (DWORD) 0xA527FDF9L, (DWORD) 0xA1E6E04EL, (DWORD) 0xBFA1B04BL ,(DWORD) 0xBB60ADFCL, (DWORD) 0xB6238B25L, (DWORD) 0xB2E29692L, (DWORD) 0x8AAD2B2FL, (DWORD) 0x8E6C3698L ,(DWORD) 0x832F1041L, (DWORD) 0x87EE0DF6L, (DWORD) 0x99A95DF3L, (DWORD) 0x9D684044L, (DWORD) 0x902B669DL ,(DWORD) 0x94EA7B2AL, (DWORD) 0xE0B41DE7L, (DWORD) 0xE4750050L, (DWORD) 0xE9362689L, (DWORD) 0xEDF73B3EL ,(DWORD) 0xF3B06B3BL, (DWORD) 0xF771768CL, (DWORD) 0xFA325055L, (DWORD) 0xFEF34DE2L, (DWORD) 0xC6BCF05FL ,(DWORD) 0xC27DEDE8L, (DWORD) 0xCF3ECB31L, (DWORD) 0xCBFFD686L, (DWORD) 0xD5B88683L, (DWORD) 0xD1799B34L ,(DWORD) 0xDC3ABDEDL, (DWORD) 0xD8FBA05AL, (DWORD) 0x690CE0EEL, (DWORD) 0x6DCDFD59L, (DWORD) 0x608EDB80L ,(DWORD) 0x644FC637L, (DWORD) 0x7A089632L, (DWORD) 0x7EC98B85L, (DWORD) 0x738AAD5CL, (DWORD) 0x774BB0EBL ,(DWORD) 0x4F040D56L, (DWORD) 0x4BC510E1L, (DWORD) 0x46863638L, (DWORD) 0x42472B8FL, (DWORD) 0x5C007B8AL ,(DWORD) 0x58C1663DL, (DWORD) 0x558240E4L, (DWORD) 0x51435D53L, (DWORD) 0x251D3B9EL, (DWORD) 0x21DC2629L ,(DWORD) 0x2C9F00F0L, (DWORD) 0x285E1D47L, (DWORD) 0x36194D42L, (DWORD) 0x32D850F5L, (DWORD) 0x3F9B762CL ,(DWORD) 0x3B5A6B9BL, (DWORD) 0x0315D626L, (DWORD) 0x07D4CB91L, (DWORD) 0x0A97ED48L, (DWORD) 0x0E56F0FFL ,(DWORD) 0x1011A0FAL, (DWORD) 0x14D0BD4DL, (DWORD) 0x19939B94L, (DWORD) 0x1D528623L, (DWORD) 0xF12F560EL ,(DWORD) 0xF5EE4BB9L, (DWORD) 0xF8AD6D60L, (DWORD) 0xFC6C70D7L, (DWORD) 0xE22B20D2L, (DWORD) 0xE6EA3D65L ,(DWORD) 0xEBA91BBCL, (DWORD) 0xEF68060BL, (DWORD) 0xD727BBB6L, (DWORD) 0xD3E6A601L, (DWORD) 0xDEA580D8L ,(DWORD) 0xDA649D6FL, (DWORD) 0xC423CD6AL, (DWORD) 0xC0E2D0DDL, (DWORD) 0xCDA1F604L, (DWORD) 0xC960EBB3L ,(DWORD) 0xBD3E8D7EL, (DWORD) 0xB9FF90C9L, (DWORD) 0xB4BCB610L, (DWORD) 0xB07DABA7L, (DWORD) 0xAE3AFBA2L ,(DWORD) 0xAAFBE615L, (DWORD) 0xA7B8C0CCL, (DWORD) 0xA379DD7BL, (DWORD) 0x9B3660C6L, (DWORD) 0x9FF77D71L ,(DWORD) 0x92B45BA8L, (DWORD) 0x9675461FL, (DWORD) 0x8832161AL, (DWORD) 0x8CF30BADL, (DWORD) 0x81B02D74L ,(DWORD) 0x857130C3L, (DWORD) 0x5D8A9099L, (DWORD) 0x594B8D2EL, (DWORD) 0x5408ABF7L, (DWORD) 0x50C9B640L ,(DWORD) 0x4E8EE645L, (DWORD) 0x4A4FFBF2L, (DWORD) 0x470CDD2BL, (DWORD) 0x43CDC09CL, (DWORD) 0x7B827D21L ,(DWORD) 0x7F436096L, (DWORD) 0x7200464FL, (DWORD) 0x76C15BF8L, (DWORD) 0x68860BFDL, (DWORD) 0x6C47164AL ,(DWORD) 0x61043093L, (DWORD) 0x65C52D24L, (DWORD) 0x119B4BE9L, (DWORD) 0x155A565EL, (DWORD) 0x18197087L ,(DWORD) 0x1CD86D30L, (DWORD) 0x029F3D35L, (DWORD) 0x065E2082L, (DWORD) 0x0B1D065BL, (DWORD) 0x0FDC1BECL ,(DWORD) 0x3793A651L, (DWORD) 0x3352BBE6L, (DWORD) 0x3E119D3FL, (DWORD) 0x3AD08088L, (DWORD) 0x2497D08DL ,(DWORD) 0x2056CD3AL, (DWORD) 0x2D15EBE3L, (DWORD) 0x29D4F654L, (DWORD) 0xC5A92679L, (DWORD) 0xC1683BCEL ,(DWORD) 0xCC2B1D17L, (DWORD) 0xC8EA00A0L, (DWORD) 0xD6AD50A5L, (DWORD) 0xD26C4D12L, (DWORD) 0xDF2F6BCBL ,(DWORD) 0xDBEE767CL, (DWORD) 0xE3A1CBC1L, (DWORD) 0xE760D676L, (DWORD) 0xEA23F0AFL, (DWORD) 0xEEE2ED18L ,(DWORD) 0xF0A5BD1DL, (DWORD) 0xF464A0AAL, (DWORD) 0xF9278673L, (DWORD) 0xFDE69BC4L, (DWORD) 0x89B8FD09L ,(DWORD) 0x8D79E0BEL, (DWORD) 0x803AC667L, (DWORD) 0x84FBDBD0L, (DWORD) 0x9ABC8BD5L, (DWORD) 0x9E7D9662L ,(DWORD) 0x933EB0BBL, (DWORD) 0x97FFAD0CL, (DWORD) 0xAFB010B1L, (DWORD) 0xAB710D06L, (DWORD) 0xA6322BDFL ,(DWORD) 0xA2F33668L, (DWORD) 0xBCB4666DL, (DWORD) 0xB8757BDAL, (DWORD) 0xB5365D03L, (DWORD) 0xB1F740B4L }; DWORD CRC_Calc(unsigned char * pU8section, DWORD U16length) { char msg[80]={0}; ULONG crc = 0xffffffff; U16length -= 4; while( U16length--) { sprintf(msg,"Byte: 0x%02X",*pU8section); printTexboxLine(msg); crc = (crc << 8) ^ crc32tab[((crc >> 24) ^ *(pU8section++)) & 0xff]; } printTexboxLine(""); U16length = 4; while( U16length--) { sprintf(msg,"Byte: 0x%02X",*pU8section); printTexboxLine(msg); crc = (crc << 8) ^ crc32tab[((crc >> 24) ^ *(pU8section++)) & 0xff]; } return (crc); } //The code calling the above functions follows below: int main (int argc, char *argv[]) { if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ CRC_Init(); //Need to be called before any other CRC functions!! gPh = LoadPanel (0, "test_crc.uir", PANEL); DisplayPanel (gPh); RunUserInterface (); /* Free resources and return */ DiscardPanel (gPh); CloseCVIRTE (); return 0; } /* *captured live date time packet with optional tot * * */ void stufftdt(unsigned char *array) { *array++ = 0x47; //ts header start *array++ = 0x40; // *array++ = 0x14; // *array++ = 0x1F; //ts header end *array++ = 0x00; //.... table start *array++ = 0x70; // tdt table_id *array++ = 0x70; *array++ = 0x05; *array++ = 0xD6; *array++ = 0x18; *array++ = 0x09; *array++ = 0x14; *array++ = 0x20; *array++ = 0x73; //tot table_id --CRC32 generated from this point until the byte before the CRC32 bytes *array++ = 0x70; //various bits, lower nibble plus next byte is section length *array++ = 0x27;// previous nibble + this is section length ie x027 = 39 bytes including crc at the end *array++ = 0xD6; *array++ = 0x18; *array++ = 0x09; *array++ = 0x14; *array++ = 0x20; *array++ = 0xF0; *array++ = 0x1C; *array++ = 0x58; *array++ = 0x1A; *array++ = 0x47; *array++ = 0x42; *array++ = 0x52; *array++ = 0x02; *array++ = 0x00; *array++ = 0x00; *array++ = 0xD6; *array++ = 0x87; *array++ = 0x00; *array++ = 0x59; *array++ = 0x59; *array++ = 0x01; *array++ = 0x00; *array++ = 0x49; *array++ = 0x52; *array++ = 0x4C; *array++ = 0x02; *array++ = 0x00; *array++ = 0x00; *array++ = 0xD6; *array++ = 0x87; *array++ = 0x00; *array++ = 0x59; *array++ = 0x59; *array++ = 0x01; *array++ = 0x00; *array++ = 0x4F; //CRC Bytes from here *array++ = 0x49; // | *array++ = 0x97; // | *array++ = 0xB8; // To here. } int CVICALLBACK cbtestcrc (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { unsigned char ts[188]={0}; char msg[80]={0}; DWORD crc; int n,l; switch (event) { case EVENT_COMMIT: memset((void*)&ts,0xFF,sizeof(ts)); stufftdt(ts); for(n=13;n<=13;n++) //Check CRC value is correct - should get 0x00 if CRC is correct { l=55-n; crc = CRC_Calc(&ts[n], l); //CRC_Make(&ts[n], l); sprintf(msg,"start: 0x%02X, end: 0x%02X, length: %d, crc: 0x%08X\n\n",ts[n],ts[n+l-1],l,crc); printTexboxLine(msg); } stufftdt(ts); for(n=13;n<=13;n++) //Calculate CRC value { l=51-n; crc = CRC_Make(&ts[n], l); sprintf(msg,"start: 0x%02X, end: 0x%02X, length: %d, crc: 0x%08X",ts[n],ts[n+l-1],l,crc); printTexboxLine(msg); } break; } return 0; }

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates