///////////////////////////////////////////////////////////////////////////////// // ---------------------------------------------------------------------------- // CRC tester v1.3 written on 4th of February 2003 by Sven Reifegerste (zorc/reflex) // This is the complete compilable C program, consisting only of this .c file. // No guarantee for any mistakes. // // changes to CRC tester v1.2: // // - remove unneccessary (!(polynom&1)) test for invalid polynoms // (now also XMODEM parameters 0x8408 work in c-code as they should) // // changes to CRC tester v1.1: // // - include an crc&0crcmask after converting non-direct to direct initial // value to avoid overflow // // changes to CRC tester v1.0: // // - most int's were replaced by unsigned long's to allow longer input strings // and avoid overflows and unnecessary type-casting's // ---------------------------------------------------------------------------- const int order = 16; const unsigned long polynom = 0x8005; const int direct = 1; const unsigned long crcinit = 0x0; const unsigned long crcxor = 0x0; const int refin = 1; const int refout = 1; // 'order' [1..32] is the CRC polynom order, counted without the leading '1' bit // 'polynom' is the CRC polynom without leading '1' bit // 'direct' [0,1] specifies the kind of algorithm: 1=direct, no augmented zero bits // 'crcinit' is the initial CRC value belonging to that algorithm // 'crcxor' is the final XOR value // 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not // 'refout' [0,1] specifies if the CRC will be reflected before XOR // internal global values: unsigned long crcmask; unsigned long crchighbit; unsigned long crcinit_direct; unsigned long crcinit_nondirect; unsigned long crctab[256]; // subroutines unsigned long reflect (unsigned long crc, int bitnum) { // reflects the lower 'bitnum' bits of 'crc' unsigned long i, j=1, crcout=0; for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) { if (crc & i) crcout|=j; j<<= 1; } return (crcout); } void generate_crc_table() { // make CRC lookup table used by table algorithms int i, j; unsigned long bit, crc; for (i=0; i<256; i++) { crc=(unsigned long)i; if (refin) crc=reflect(crc, 8); crc<<= order-8; for (j=0; j<8; j++) { bit = crc & crchighbit; crc<<= 1; if (bit) crc^= polynom; } if (refin) crc = reflect(crc, order); crc&= crcmask; crctab[i]= crc; } } unsigned long crctablefast (unsigned char* p, unsigned long len) { // fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip. // only usable with polynom orders of 8, 16, 24 or 32. unsigned long crc = crcinit_direct; if (refin) crc = reflect(crc, order); if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++]; else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++]; if (refout^refin) crc = reflect(crc, order); crc^= crcxor; crc&= crcmask; return(crc); } unsigned long crctable (unsigned char* p, unsigned long len) { // normal lookup table algorithm with augmented zero bytes. // only usable with polynom orders of 8, 16, 24 or 32. unsigned long crc = crcinit_nondirect; if (refin) crc = reflect(crc, order); if (!refin) while (len--) crc = ((crc << 8) | *p++) ^ crctab[ (crc >> (order-8)) & 0xff]; else while (len--) crc = ((crc >> 8) | (*p++ << (order-8))) ^ crctab[ crc & 0xff]; if (!refin) while (++len < order/8) crc = (crc << 8) ^ crctab[ (crc >> (order-8)) & 0xff]; else while (++len < order/8) crc = (crc >> 8) ^ crctab[crc & 0xff]; if (refout^refin) crc = reflect(crc, order); crc^= crcxor; crc&= crcmask; return(crc); } unsigned long crcbitbybit(unsigned char* p, unsigned long len) { // bit by bit algorithm with augmented zero bytes. // does not use lookup table, suited for polynom orders between 1...32. unsigned long i, j, c, bit; unsigned long crc = crcinit_nondirect; for (i=0; i>=1) { bit = crc & crchighbit; crc<<= 1; if (c & j) crc|= 1; if (bit) crc^= polynom; } } for (i=0; i>=1) { bit = crc & crchighbit; crc<<= 1; if (c & j) bit^= crchighbit; if (bit) crc^= polynom; } } if (refout) crc=reflect(crc, order); crc^= crcxor; crc&= crcmask; return(crc); } ///////////////////////////////////////////////////////////////////////////////// void __fastcall TForm1::Button19Click(TObject *Sender) { // генератор PIC // подготовить позывной AnsiString call=Edit7->Text; int len=call.Length(); if(len<3){MessageBox(NULL,"Unable generate PIC - to short CALLSIGN, must be 3...6 chars", "Unable generate PIC",MB_OK); return;} char *c; if(len<6){ c=call.c_str(); if(!isdigit(c[2])) call=" "+call; } len=call.Length(); switch(len) { case 3: call=call+" "; break; case 4: call=call+" "; break; case 5: call=call+" "; break; } c=call.c_str(); if(!isdigit(c[2])) {MessageBox(NULL,"Unable generate PIC - CALLSIGN is incorrect", "Incorrect CALLSIGN",MB_OK); return;} CALLSIGN=Edit7->Text; Form1->Caption=CALLSIGN+" DDS Control"; // сформировать encoding mapping // ( ( ( ( (1) * 36 + 0) * 10 + 1) * 27 + 1) * 27 + 1) * 27 + 0 if(c[0]==' ') c[0]=0; if(c[0]>47 && c[0]<58) c[0]=c[0]-21; // 0...9 if(c[0]>64 && c[0]<91) c[0]=c[0]-64; // A...Z if(c[1]>47 && c[1]<58) c[1]=c[1]-22; // 0...9 if(c[1]>64 && c[1]<91) c[1]=c[1]-65; // A...Z if(c[2]>47 && c[2]<58) c[2]=c[2]-48; // 0...9 if(c[3]==' ') c[3]=0; if(c[3]>64 && c[3]<91) c[3]=c[3]-64; // A...Z if(c[4]==' ') c[4]=0; if(c[4]>64 && c[4]<91) c[4]=c[4]-64; // A...Z if(c[5]==' ') c[5]=0; if(c[5]>64 && c[5]<91) c[5]=c[5]-64; // A...Z unsigned __int64 map=(((((c[0])*36 + c[1]) * 10 + c[2]) * 27 + c[3]) * 27 + c[4]) * 27 + c[5]; unsigned short crc; unsigned char msg51[4+28+16+3]; unsigned char arr[28+16+3]; for(int i=0; i<28; i++) {if(1 & (map>>i)) arr[27-i]='1'; else arr[27-i]='0';} crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1; crchighbit = (unsigned long)1<<(order-1); crc=crcbitbybitfast(arr, 28); unsigned short crc_low,crc_high; crc_low=crc&0xff; crc_high=(crc>>8)&0xff; if(crc_low==0)crc_low=0x1b; if(crc_high==0)crc_high=0x2b; crc=(crc_low<<8)^crc_high; for(int i=0; i<16; i++) {if(1 & (crc>>i)) arr[27+16-i]='1'; else arr[27+16-i]='0';} crc=crcbitbybitfast(arr, 28+16); crc_low=crc&0xff; crc_high=(crc>>8)&0xff; if(crc_low==0)crc_low=0x1b; if(crc_high==0)crc_high=0x2b; crc=(crc_low<<8)^crc_high; crc=crc&0x07; for(int i=0; i<3; i++) {if(1 & (crc>>i)) arr[27+16+3-i]='1'; else arr[27+16+3-i]='0';} unsigned char *nvect="111000010101011111100110110100000001100100010101011"; for(int i=0; i<28+16+3; i++) msg51[i+4]=arr[i]; for(int i=0; i<4; i++) msg51[i]='0'; for(int i=0; i<51; i++) { if(msg51[i]==nvect[i]) msg51[i]='0'; else msg51[i]='1'; } unsigned char wh_coded[17][8]; for(int i=0; i<51; i=i+3) { if(strncmp(&msg51[i],"000",3)==0)strncpy(&wh_coded[i/3][0],"0000000",7); if(strncmp(&msg51[i],"001",3)==0)strncpy(&wh_coded[i/3][0],"1010101",7); if(strncmp(&msg51[i],"010",3)==0)strncpy(&wh_coded[i/3][0],"0110011",7); if(strncmp(&msg51[i],"011",3)==0)strncpy(&wh_coded[i/3][0],"1100110",7); if(strncmp(&msg51[i],"100",3)==0)strncpy(&wh_coded[i/3][0],"0001111",7); if(strncmp(&msg51[i],"101",3)==0)strncpy(&wh_coded[i/3][0],"1011010",7); if(strncmp(&msg51[i],"110",3)==0)strncpy(&wh_coded[i/3][0],"0111100",7); if(strncmp(&msg51[i],"111",3)==0)strncpy(&wh_coded[i/3][0],"1101001",7); } unsigned char ilv[17*7]; int k=0; for(int i=0; i<7; i++) { for(int j=0; j<17; j++) {ilv[k]=wh_coded[j][i];k++;} } AnsiString operaPIC="11"; for(int i=0; i<17*7; i++) { if(ilv[i]=='0') operaPIC+="10"; else operaPIC+="01"; } operaPIC=operaPIC.SubString(1,239); Memo1->Text=operaPIC; } //--------------------------------------------------------------------------- unsigned short TForm1::crc16(unsigned char* crc_arr, unsigned int crc_num) { unsigned short crc = 0x0; unsigned char i; int b; for(int j=0; j>i); if(b) crc=crc^0x8005; // crc =(crc << 1) ^ 0x8005;// 0x1021; //else // crc=crc<< 1; } } return crc; } //----------------------------------------------------------------------------- unsigned short TForm1::crc16(unsigned __int64 data, unsigned int n) { const unsigned long crcinit = 0x0; unsigned long crcinit_direct; unsigned long crcinit_nondirect; unsigned short crcmask = ((((unsigned long)1<<(16-1))-1)<<1)|1 ; unsigned short crc=0x0; int b=0; data=data<<16; for(int i=0; i>(n+16-1-i));// if(b) crc=crc^0x8005; } const unsigned short crcxor = 0x0; crc^= crcxor; crc&= crcmask; return crc; } //-----------------------------------------------------------------------------