Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- using namespace std;
- unsigned char n64_get_crc(unsigned char *data)
- {
- unsigned char crc = 0;
- for(int i = 0; i <= 32; i++ ){
- for(int j = 7; j >= 0; j-- ){
- unsigned char tmp = 0;
- if(crc & 0x80){
- tmp = 0x85;
- }
- crc <<= 1;
- if(i < 32){
- if(data[i] & (0x01 << j)){
- crc |= 0x1;
- }
- }
- crc ^= tmp;
- }
- }
- return crc; //Returns the non-inverted CRC of a 32-byte datastring
- }
- unsigned char n64_get_crc_quick(unsigned char *data)
- {
- static const unsigned char crctable[256] = {143,133,128,64,32,16,8,4,2,1,194,97,242,121,254,127,253,188,94,47,213,
- 168,84,42,21,200,100,50,25,206,103,241,186,93,236,118,59,223,173,148,74,
- 37,208,104,52,26,13,196,98,49,218,109,244,122,61,220,110,55,217,174,87,
- 233,182,91,239,181,152,76,38,19,203,167,145,138,69,224,112,56,28,14,7,
- 193,162,81,234,117,248,124,62,31,205,164,82,41,214,107,247,185,158,79,
- 229,176,88,44,22,11,199,161,146,73,230,115,251,191,157,140,70,35,211,
- 171,151,137,134,67,227,179,155,143,133,128,64,32,16,8,4,2,1,194,97,242,
- 121,254,127,253,188,94,47,213,168,84,42,21,200,100,50,25,206,103,241,
- 186,93,236,118,59,223,173,148,74,37,208,104,52,26,13,196,98,49,218,109,
- 244,122,61,220,110,55,217,174,87,233,182,91,239,181,152,76,38,19,203,167,
- 145,138,69,224,112,56,28,14,7,193,162,81,234,117,248,124,62,31,205,164,82,
- 41,214,107,247,185,158,79,229,176,88,44,22,11,199,161,146,73,230,115,251,
- 191,157,140,70,35,211,171,151,137,134,67,227,179,155,143,133};
- unsigned char crc = 0;
- for(int byte=0; byte<=32; byte++){
- for (int bit=0; bit<8; bit++){
- if(data[byte] & 1<<(7-bit)){
- crc ^= crctable[byte*8 + bit];
- }
- }
- }
- return crc; //Returns the non-inverted CRC of a 32-byte datastring
- }
- int main() {
- //Change this packet to whatever you're testing
- unsigned char packet[32]={0xFF,0x00,0x3b,0x00,0x00,0x00,0x01,0xFF,0xFF,0x00,0x00,0x00,0x77,0x00,0x00,0x00\
- ,0x00,0x92,0xC0,0x00,0xB1,0x4d,0x00,0x00,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0xFF};
- printf("crc: 0x%.2x, crc_quick: 0x%.2x\n",n64_get_crc(packet), n64_get_crc_quick(packet));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment