순환 중복 검사
소프트웨어 구현 > CRC 테이블을 위한 코드 예

우선 모든 OCT 단위로 입력을 미리 계산한다.
//#define CRC_SHIFT_5

static unsigned char crctable[256];

/// CRC 테이블 만들기 /////////////////////////////

//   Generate a table for a byte-wise 3-bit CRC calculation on the polynomial:
//   x^3 + x + 1

void make_crc_table( void )
{
    int cnt, bcnt;
    unsigned short poly, c;

    // terms of polynomial defining this crc (except x^3):
    static const char p[] = {0,1};

    // make exclusive-or pattern from polynomial
    poly = 0;
    for ( cnt = 0; cnt < sizeof(p)/sizeof(p[0]); cnt++ ) {
        poly |= 1 << p[cnt];
    }
    poly <<= 5;

    for ( cnt = 0; cnt < 256; cnt++ ) {
        c = cnt;
        for ( bcnt = 0; bcnt < 8; bcnt++ ) {
            c = ( c & 0x80 ) ? poly ^ ( c << 1 ) : ( c << 1 );
        }
       #ifdef CRC_SHIFT_5
	crctable[cnt] = (unsigned char) (c>>5) & 0x07;
       #else
        crctable[cnt] = (unsigned char) (c & 0xE0);
       #endif
    }
}

int main(int argc, char* argv[])
{
    int cnt;
    unsigned char crc;

    make_crc_table();

    FILE *fout;

    if ( (fout = fopen("crc3table.h", "wt")) == NULL)
       return -1;

    fprintf(fout,
      "#ifndef _CRC3TABLE_H\n"
      "#define _CRC3TABLE_H\n"
      "\nextern const unsigned char crctable[];\n"
    );

   #ifdef CRC_SHIFT_5
    fprintf(fout, "\n#define CRC_SHIFT_5\n");
   #endif

    fprintf(fout, "\n#endif\n");
    fclose(fout);

    if ( (fout = fopen("crc3table.c", "wt")) == NULL)
       return -1;

    fprintf(fout, "\nconst unsigned char crctable[256] = {\n   ");
    for ( cnt = 0; cnt < 256; cnt++ ) {
        if (cnt == 255) {
            fprintf(fout, "0x%02X\n", crctable[cnt] );
            break;
        } else
            fprintf(fout,"0x%02X,", crctable[cnt] );
            if ( (cnt % 8) == 7)
                fprintf(fout,"\n   ");
            else
                fprintf(fout," ");
    }
    fprintf(fout,"};\n");
    fclose(fout);

    return 0;
}
실행결과 :
const unsigned char crctable[256] = {
   0x00, 0x60, 0xC0, 0xA0, 0xE0, 0x80, 0x20, 0x40,
   0xA0, 0xC0, 0x60, 0x00, 0x40, 0x20, 0x80, 0xE0,
   0x20, 0x40, 0xE0, 0x80, 0xC0, 0xA0, 0x00, 0x60,
   0x80, 0xE0, 0x40, 0x20, 0x60, 0x00, 0xA0, 0xC0,
   0x40, 0x20, 0x80, 0xE0, 0xA0, 0xC0, 0x60, 0x00,
   0xE0, 0x80, 0x20, 0x40, 0x00, 0x60, 0xC0, 0xA0,
   0x60, 0x00, 0xA0, 0xC0, 0x80, 0xE0, 0x40, 0x20,
   0xC0, 0xA0, 0x00, 0x60, 0x20, 0x40, 0xE0, 0x80,
   0x80, 0xE0, 0x40, 0x20, 0x60, 0x00, 0xA0, 0xC0,
   0x20, 0x40, 0xE0, 0x80, 0xC0, 0xA0, 0x00, 0x60,
   0xA0, 0xC0, 0x60, 0x00, 0x40, 0x20, 0x80, 0xE0,
   0x00, 0x60, 0xC0, 0xA0, 0xE0, 0x80, 0x20, 0x40,
   0xC0, 0xA0, 0x00, 0x60, 0x20, 0x40, 0xE0, 0x80,
   0x60, 0x00, 0xA0, 0xC0, 0x80, 0xE0, 0x40, 0x20,
   0xE0, 0x80, 0x20, 0x40, 0x00, 0x60, 0xC0, 0xA0,
   0x40, 0x20, 0x80, 0xE0, 0xA0, 0xC0, 0x60, 0x00,
   0x60, 0x00, 0xA0, 0xC0, 0x80, 0xE0, 0x40, 0x20,
   0xC0, 0xA0, 0x00, 0x60, 0x20, 0x40, 0xE0, 0x80,
   0x40, 0x20, 0x80, 0xE0, 0xA0, 0xC0, 0x60, 0x00,
   0xE0, 0x80, 0x20, 0x40, 0x00, 0x60, 0xC0, 0xA0,
   0x20, 0x40, 0xE0, 0x80, 0xC0, 0xA0, 0x00, 0x60,
   0x80, 0xE0, 0x40, 0x20, 0x60, 0x00, 0xA0, 0xC0,
   0x00, 0x60, 0xC0, 0xA0, 0xE0, 0x80, 0x20, 0x40,
   0xA0, 0xC0, 0x60, 0x00, 0x40, 0x20, 0x80, 0xE0,
   0xE0, 0x80, 0x20, 0x40, 0x00, 0x60, 0xC0, 0xA0,
   0x40, 0x20, 0x80, 0xE0, 0xA0, 0xC0, 0x60, 0x00,
   0xC0, 0xA0, 0x00, 0x60, 0x20, 0x40, 0xE0, 0x80,
   0x60, 0x00, 0xA0, 0xC0, 0x80, 0xE0, 0x40, 0x20,
   0xA0, 0xC0, 0x60, 0x00, 0x40, 0x20, 0x80, 0xE0,
   0x00, 0x60, 0xC0, 0xA0, 0xE0, 0x80, 0x20, 0x40,
   0x80, 0xE0, 0x40, 0x20, 0x60, 0x00, 0xA0, 0xC0,
   0x20, 0x40, 0xE0, 0x80, 0xC0, 0xA0, 0x00, 0x60
};
변수 crctable[]이 바이트 단위로 모두 계산한다. 이것을 프로그램 파일로 만들어 CRC 계산하는데 사용한다.
이 파일이름을 crc3table.c라고 하면 다음에 이것을 사용하여 코딩한다.