Guest User

Untitled

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