Source file: /~heha/mb-iwp/DDS130/DDS130.zip/CrcCrack.cpp

#include <windows.h>
#include <stdio.h>

#define elemof(x) (sizeof(x)/sizeof(*(x)))

struct Pattern{
 BYTE len;
 BYTE data[9];
 WORD crc;
};

// Prüf-Strukturen
static const Pattern pat[]={
 {7, {0x02, 0x00, 0x10, 0x82, 0x00, 0x78, 0x01}, 0x6D93},
 {5, {0x02, 0x00, 0x01, 0x00, 0x50}, 0xD904},
 {9, {0x02, 0x00, 0x05, 0x00, 0x66, 0x00, 0x00, 0x64, 0x00}, 0xA1A2},
 {9, {0x02, 0x00, 0x05, 0x00, 0x66, 0x00, 0x00, 0x06, 0x40}, 0x6C27},
 {9, {0x02, 0x00, 0x05, 0x00, 0x66, 0x00, 0x00, 0x00, 0x64}, 0x78FF},
 {7, {0x02, 0x00, 0x10, 0x82, 0x00, 0x78, 0x00}, 0xED96}};

struct CrcParam{
 WORD poly;
 WORD init;
 WORD xor;
 BYTE flags;
	// bool revin;
	// bool revout;
	// bool xordata;	wie USB, kein Quell-Daten-Schieben!
	// bool shift1;		wie USB
	// bool crcrev;		Low- und High-Byte andersherum?
 BYTE start;
};

bool check(const Pattern*p, const CrcParam *cp) {
 WORD crc=cp->init;
 BYTE bit;
// main loop, algorithm is fast bit by bit type
 for (int i=cp->start; i<p->len; i++) {
  BYTE b = p->data[i];
  for (int j=0; j<8; j++) {
   if (cp->flags&4) {
    crc^=b;
    bit=0;
   }else{
    if (cp->flags&1) {
     bit=b&1; b>>=1;		// rechtsschieben der Daten
    }else{
     bit=b>>7&1; b<<=1;
    }
   }
   if (cp->flags&2) {
    bit^=crc&1; crc>>=1;	// rechtsschieben der CRC
    if (cp->flags&8) crc|=1<<15;
   }else{
    bit^=crc>>15&1; crc<<=1;
    if (cp->flags&8) crc++;
   }
   if (bit) crc^=cp->poly;
  }
 }
// perform xor value
 crc^=cp->xor;
 if (cp->flags&16) crc=MAKEWORD(HIWORD(crc),LOWORD(crc));
 return crc==p->crc;
}

void checkallpoly(CrcParam*cp) {
// for (cp->poly=1; cp->poly; cp->poly++) {
  for (int j=0; j<elemof(pat); j++) {
   if (!check(&pat[j],cp)) break;
  }
  if (j==elemof(pat)) printf("Found: %X(%X/%X/%d/%d)\n",cp->poly,cp->init,cp->xor,cp->flags,cp->start);
// }
}

void checkallp(CrcParam*cp) {
 do{
  checkallpoly(cp);
//  cp->xor=0xFFFF;
//  checkallpoly(cp);
//  cp->xor=0;
 }while (++cp->init);
}

void checkall() {
 CrcParam cp;
 memset(&cp,0,sizeof(cp));
 cp.poly=0x8005;
 for (cp.flags=0;cp.flags<32;cp.flags++) checkallp(&cp);
 cp.start++;
 for (cp.flags=0;cp.flags<32;cp.flags++) checkallp(&cp);
 cp.start++;
 for (cp.flags=0;cp.flags<32;cp.flags++) checkallp(&cp);
}
 
/*
Tx: 02 00 10 82 00 78 01 93 6D
Rx: 02 00 10 82 00 78 06 82 ED
Tx: 02 00 01 00 50 04 D9
Rx: 02 00 35 00 50 00 6E 00 64 00 00 00 00 00 19 B2 D0 5E 00 00 00 00 00 01 90 00 00 00 19 B2 D0 5E 00 00 64 00 00 00 00 00 00 00 00 00 00 00 01 64 27 10 90 03 00 01 00 00 00 00 84 DF
Tx: 02 00 05 00 66 00 00 64 00 A2 A1
Rx: 02 00 10 82 00 66 06 82 A9
Tx: 02 00 05 00 66 00 00 06 40 27 6C
Rx: 02 00 10 82 00 66 06 82 A9
Tx: 02 00 05 00 66 00 00 00 64 FF 78
Rx: 02 00 10 82 00 66 06 82 A9
Tx: 02 00 10 82 00 78 00 96 ED
Rx: 02 00 10 82 00 78 06 82 ED
*/

int CALLBACK mainCRTStartup() {
 printf("Hallo\n");
 checkall();
 return 0;
}

/*
Heraus kommt:
Poly = 0x8005
Init = 0x800D
Xor  = 0
linksschieben
Erst LSB, dann MSB (im Gegensatz zur eingestellten Frequenz)

Damit CRC-Routine:
*/

WORD GenCrc(const BYTE*data, int len) {
 WORD crc=0x800D;
 for (; len; len--) {
  BYTE b = *data++;
  for (int j=0; j<8; j++) {
   BYTE bit=b; b<<=1;
   bit^=crc>>7; crc<<=1;
   if (bit&0x80) crc^=0x8005;
  }
 }
 return crc;
}

void AppendCrc(BYTE*data, int&len) {
 *(WORD*)(data+len)=GenCrc(data,len);
 len+=2;
}

bool CheckCrc(const BYTE*data, int&len) {
 len-=2;
 return GenCrc(data,len)==*(WORD*)(data+len);
}
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded