home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / CRC / makecrc.c < prev    next >
C/C++ Source or Header  |  1996-02-01  |  4KB  |  156 lines

  1. /* This program will write six C routines for the calculation of
  2.  * the following CRC's. */
  3.  
  4. /* The CRC polynomial.
  5.  * These 4 values define the crc-polynomial.
  6.  * If you change them, you must change crctab[]'s initial value to what is
  7.  * printed by initcrctab() [see 'compile with -DMAKETAB' above].
  8.  */
  9.  
  10. /* This tables assumes CCITT is MSB first.  Swapped means LSB first.   In that
  11.  * case the polynomial is also swapped
  12.  */
  13.  
  14. /* 16 bit crc's */
  15. /* Value used by:            CCITT    KERMIT    ARC    BINHEX    */
  16. /* the poly:                0x1021    0x8408    0xA001    0x1021    */
  17. /* original:                0x1021    0x1021    0x8005    0x1021    */
  18. /* init value:                -1    0    0    0    */
  19. /* swapped:                no    yes    yes    no    */
  20. /* bits in CRC:                16    16    16    16    */
  21. /* ARC used by LHARC, ZOO, STUFFIT                    */
  22. /* BINHEX used by XMODEM, PACKIT                    */
  23.  
  24. /* 32 bit crc's */
  25. /* Value used by:            CCITT32        ZIP        */
  26. /* the poly:                0x04c11db7    0xedb88320    */
  27. /* original:                0x04c11db7    0x04c11db7    */
  28. /* init value:                -1        -1        */
  29. /* swapped                no        yes        */
  30. /* bits in CRC:                32        32        */
  31. /* ZIP used by COMPACTOR                        */
  32.  
  33. #include <stdio.h>
  34.  
  35. extern void exit();
  36. extern char *strcat();
  37.  
  38. static void initcrctab();
  39.  
  40. main()
  41. {
  42.     initcrctab("ccitt", 0x1021, 0xffff, 0, 16);
  43.     initcrctab("kermit", 0x8408, 0, 1, 16);
  44.     initcrctab("arc", 0xa001, 0, 1, 16);
  45.     initcrctab("binhex", 0x1021, 0, 0, 16);
  46.     initcrctab("ccitt32",0x04c11db7,0xffffffff,0,32);
  47.     initcrctab("zip",0xedb88320,0xffffffff,1,32);
  48.     exit(0);
  49.     /*NOTREACHED*/
  50. }
  51.  
  52. static void initcrctab(name, poly, init, swapped, bits)
  53. char *name;
  54. int poly, init, swapped, bits;
  55. {
  56.     register  int b, i;
  57.     unsigned short v;
  58.     unsigned long vv;
  59.     FILE *fd;
  60.     char buf[20];
  61.     
  62.     buf[0] = 0;
  63.     (void)strcat(buf, name);
  64.     (void)strcat(buf, ".c");
  65.     if((fd = fopen(buf, "w")) == NULL) {
  66.     (void)fprintf(stderr, "Cannot open %s for writing\n", buf);
  67.     exit(1);
  68.     }
  69.     (void)fprintf(fd, "unsigned long %s_crcinit = %d;\n", name, init);
  70.     (void)fprintf(fd, "\n");
  71.     if(bits == 16) {
  72.     (void)fprintf(fd, "static unsigned short crctab[256] = {\n");
  73.     } else {
  74.     (void)fprintf(fd, "static unsigned long crctab[256] = {\n");
  75.     }
  76.     (void)fprintf(fd, "    ");
  77.     if(bits == 16) {
  78.     for(b = 0; b < 256; ++b) {
  79.         if(swapped) {
  80.         for(v = b, i = 8; --i >= 0;)
  81.             v = v & 1 ? (v>>1)^poly : v>>1;
  82.         } else {
  83.         for(v = b<<8, i = 8; --i >= 0;)
  84.             v = v & 0x8000 ? (v<<1)^poly : v<<1;
  85.         }
  86.         (void)fprintf(fd, "0x%.4x,", v & 0xffff);
  87.         if((b&7) == 7) {
  88.         (void)fprintf(fd, "\n");
  89.         if(b != 255) (void)fprintf(fd, "    ");
  90.         } else {
  91.         (void)fprintf(fd, " ");
  92.         }
  93.     }
  94.     } else {
  95.     for(b = 0; b < 256; ++b) {
  96.         if(swapped) {
  97.         for(vv = b, i = 8; --i >= 0;)
  98.             vv = vv & 1 ? (vv>>1)^poly : vv>>1;
  99.         } else {
  100.         for(vv = b<<24, i = 8; --i >= 0;)
  101.             vv = vv & 0x80000000 ? (vv<<1)^poly : vv<<1;
  102.         }
  103.         (void)fprintf(fd, "0x%.8x,", vv & 0xffffffff);
  104.         if((b&3) == 3) {
  105.         (void)fprintf(fd, "\n");
  106.         if(b != 255) (void)fprintf(fd, "    ");
  107.         } else {
  108.         (void)fprintf(fd, " ");
  109.         }
  110.     }
  111.     }
  112.     (void)fprintf(fd, "};\n");
  113.     (void)fprintf(fd, "\n");
  114.     (void)fprintf(fd, "unsigned long %s_updcrc(icrc, icp, icnt)\n", name);
  115.     (void)fprintf(fd, "    unsigned long icrc;\n");
  116.     (void)fprintf(fd, "    unsigned char *icp;\n");
  117.     (void)fprintf(fd, "    int icnt;\n");
  118.     (void)fprintf(fd, "{\n");
  119.     if(bits == 16) {
  120.     (void)fprintf(fd, "#define M1 0xff\n");
  121.     (void)fprintf(fd, "#define M2 0xff00\n");
  122.     } else {
  123.     (void)fprintf(fd, "#define M1 0xffffff\n");
  124.     (void)fprintf(fd, "#define M2 0xffffff00\n");
  125.     }
  126.     (void)fprintf(fd, "    register unsigned long _crc = icrc;\n");
  127.     (void)fprintf(fd, "    register unsigned char *cp = icp;\n");
  128.     (void)fprintf(fd, "    register int cnt = icnt;\n");
  129.     (void)fprintf(fd, "\n");
  130.     (void)fprintf(fd, "    while(cnt--) {\n");
  131.     if(bits == 16) {
  132.     if (swapped) {
  133.         (void)fprintf(fd,
  134.             "\t_crc=((_crc>>8)&M1)^crctab[(_crc&0xff)^*cp++];\n");
  135.     } else {
  136.         (void)fprintf(fd,
  137.             "\t_crc=((_crc<<8)&M2)^crctab[((_crc>>8)&0xff)^*cp++];\n");
  138.     }
  139.     } else {
  140.     if(swapped) {
  141.         (void)fprintf(fd,
  142.             "\t_crc=((_crc>>8)&M1)^crctab[(_crc&0xff)^*cp++];\n");
  143.     } else {
  144.         (void)fprintf(fd,
  145.             "\t_crc=((_crc<<8)&M2)^crctab[((_crc>>24)&0xff)^*cp++];\n");
  146.     }
  147.     }
  148.     (void)fprintf(fd, "    }\n");
  149.     (void)fprintf(fd, "\n");
  150.     (void)fprintf(fd, "    return(_crc);\n");
  151.     (void)fprintf(fd, "}\n");
  152.     (void)fprintf(fd, "\n");
  153.     (void)fclose(fd);
  154. }
  155.  
  156.