Created
January 1, 2019 06:19
-
-
Save pocc/c7284608903349d7a0240afa3aa60073 to your computer and use it in GitHub Desktop.
This function will compute the CRC of a hexstring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Using code from http://www.sunshine2k.de/articles/coding/crc/understanding_crc.html#ch5 | |
| // Using MBMS header bits from the pcap in wireshark bug#14875 | |
| // Expected output is 52 (0x34), which is the correct answer | |
| #include <stdio.h> | |
| int main(){ | |
| const int generator = 0x2f; | |
| int crc = 0; /* start with 0 so first byte can be 'xored' in */ | |
| // 0x10043b000000000000 aligned to 6 bits (from pcap) | |
| // 3 nibbles = 4x 6 bits | |
| int bytes[12] = {4,0,16,59, 0,0,0,0, 0,0,0,0}; | |
| for(int i=0; i < 12; i++) | |
| { | |
| int currByte = bytes[i]; | |
| crc ^= currByte; /* XOR-in the next input byte */ | |
| for (int i = 0; i < 6; i++) | |
| { | |
| if ((crc & 0x20) != 0) | |
| { | |
| crc = ((crc << 1) ^ generator) & 0x3f; | |
| } | |
| else | |
| { | |
| crc <<= 1; | |
| } | |
| } | |
| } | |
| printf("%i", crc); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment