home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / alt / cad / autocad / 1529 < prev    next >
Encoding:
Text File  |  1992-11-17  |  3.5 KB  |  122 lines

  1. Newsgroups: alt.cad.autocad
  2. Path: sparky!uunet!ornl!sunova!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!sgiblab!nec-gw!nec-tyo!wnoc-tyo-news!sh.wide!wnoc-kyo!kuis!kurims!masuda
  3. From: masuda@kurims.kyoto-u.ac.jp (Masuda Kazuyoshi)
  4. Subject: Re: CRC in AutoCAD .DWG files
  5. In-Reply-To: ieong@fraser.sfu.ca's message of Tue, 17 Nov 1992 01:04:40 GMT
  6. Message-ID: <MASUDA.92Nov18032311@hakobe.kurims.kyoto-u.ac.jp>
  7. Sender: news@kurims.kurims.kyoto-u.ac.jp
  8. Nntp-Posting-Host: hakobe
  9. Organization: Research Institute for Mathematical Sciences, Kyoto, Japan.
  10. References: <ieong.721962280@sfu.ca>
  11. Date: Tue, 17 Nov 1992 18:23:11 GMT
  12. Lines: 108
  13.  
  14. > Does anyone know what CRC algorithm is used in AutoCAD Release 11 and 12?
  15.  
  16.   There are many types of data --- entity, layer definition, ltype
  17. definition, etc... --- which consists of real data and 16-bit CRC.
  18.   For n-byte sequence of data (stored in buf[]), CRC is calculated by
  19. the following function calc_crc():
  20.  
  21.       1     unsigned calc_crc(buf, n)
  22.       2     unsigned char buf[];
  23.       3     int n;
  24.       4     {
  25.       5         int i, j;
  26.       6         unsigned crc = 0xc0c1;
  27.       7
  28.       8         for ( i = 0 ; i < n ; ++i ) {
  29.       9         crc ^= *buf++;
  30.      10         for ( j = 0 ; j < 8 ; ++j )
  31.      11             if ( crc & 1 )
  32.      12             crc = (crc >> 1) ^ 0xa001;
  33.      13             else
  34.      14             crc >>= 1;
  35.      15         }
  36.      16         return crc;
  37.      17     }
  38.  
  39.     NOTE:    We can do it more quickly by replacing line 10-14 with
  40.             crc = (crc >> 8) ^ crc_tab[crc & 0xff];
  41.         (of course we must initialize crc_tab[] in advance).
  42.  
  43. and (n+2)-byte sequence:
  44.  
  45.     n bytes of data
  46.     CRC & 0xff    (lower byte)
  47.     CRC >> 8    (upper byte)
  48.  
  49. makes up one record of data.
  50.  
  51.   For CRC-checking for a record of length n (n-2 bytes of data
  52. followed by 16-bit CRC), we have only to check whether the value of
  53. calc_crc(buf, n) equals to 0.  Because following conditions are
  54. equivalent:
  55.     (1) calc_crc(buf, n) == 0,
  56.     (2) calc_crc(buf, n-2) == buf[n-2] + (buf[n-1]<<8).
  57.  
  58.   Here is a sample program that tests CRC of all records of entity
  59. data in DWG file. (Each records corresponds to one entity in ENTITIES
  60. SECTION in DXF file.) It will show one example for how CRC is used in
  61. DWG file.
  62.  
  63.     #include <stdio.h>
  64.  
  65.     unsigned char buffer[1000];
  66.  
  67.     unsigned calc_crc(buf, n)
  68.     unsigned char buf[];
  69.     int n;
  70.     {
  71.         int i, j;
  72.         unsigned int crc = 0xc0c1;
  73.         for ( i = 0 ; i < n ; ++i ) {
  74.         crc ^= *buf++;
  75.         for ( j = 0 ; j < 8 ; ++j )
  76.             if ( crc & 1 )
  77.             crc = (crc >> 1) ^ 0xa001;
  78.             else
  79.             crc >>= 1;
  80.         }
  81.         return crc;
  82.     }
  83.  
  84.     main(argc, argv)    /* argv[1]: dwg file */
  85.     int argc;
  86.     char **argv;
  87.     {
  88.         FILE *fp;
  89.         long begin, end;
  90.         int n;
  91.         if ( argc == 1 ) exit(0);
  92.         if ( (fp = fopen(argv[1], "rb")) == NULL ) {
  93.         perror(argv[1]);
  94.         exit(1);
  95.         }
  96.         fseek(fp, 0x14L, 0);
  97.         fread(&begin, 4, 1, fp);    /* Beginning of ENTITIES data */
  98.         fread(&end, 4, 1, fp);    /* End of ENTITIES data */
  99.         fseek(fp, begin, 0);
  100.         while ( begin < end ) {
  101.         fread(buffer, 8, 1, fp);    /* Header of this record */
  102.         n = buffer[2] + (buffer[3]<<8);    /* Length of this record */
  103.         fread(buffer+8, n-8, 1, fp);    /* Rest of this record */
  104.         if ( calc_crc(buffer, (buffer[0] == 0x12 ? 8+2 : n)) != 0 ) {
  105.             /* NOTE:
  106.              * The record with buffer[0] == 0x12 is very exceptional.
  107.              * It rarely occurs. Don't pay too much attention for it.
  108.              */
  109.             printf("CRC error\n");
  110.             exit(1);
  111.         }
  112.         begin += n;
  113.         }
  114.         printf("done.\n");
  115.         exit(0);
  116.     }
  117.  
  118. ----------
  119. Kazuyoshi Masuda (masuda@kurims.kyoto-u.ac.jp)
  120. Kyoto University
  121. Japan
  122.