home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: alt.cad.autocad
- 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
- From: masuda@kurims.kyoto-u.ac.jp (Masuda Kazuyoshi)
- Subject: Re: CRC in AutoCAD .DWG files
- In-Reply-To: ieong@fraser.sfu.ca's message of Tue, 17 Nov 1992 01:04:40 GMT
- Message-ID: <MASUDA.92Nov18032311@hakobe.kurims.kyoto-u.ac.jp>
- Sender: news@kurims.kurims.kyoto-u.ac.jp
- Nntp-Posting-Host: hakobe
- Organization: Research Institute for Mathematical Sciences, Kyoto, Japan.
- References: <ieong.721962280@sfu.ca>
- Date: Tue, 17 Nov 1992 18:23:11 GMT
- Lines: 108
-
- > Does anyone know what CRC algorithm is used in AutoCAD Release 11 and 12?
-
- There are many types of data --- entity, layer definition, ltype
- definition, etc... --- which consists of real data and 16-bit CRC.
- For n-byte sequence of data (stored in buf[]), CRC is calculated by
- the following function calc_crc():
-
- 1 unsigned calc_crc(buf, n)
- 2 unsigned char buf[];
- 3 int n;
- 4 {
- 5 int i, j;
- 6 unsigned crc = 0xc0c1;
- 7
- 8 for ( i = 0 ; i < n ; ++i ) {
- 9 crc ^= *buf++;
- 10 for ( j = 0 ; j < 8 ; ++j )
- 11 if ( crc & 1 )
- 12 crc = (crc >> 1) ^ 0xa001;
- 13 else
- 14 crc >>= 1;
- 15 }
- 16 return crc;
- 17 }
-
- NOTE: We can do it more quickly by replacing line 10-14 with
- crc = (crc >> 8) ^ crc_tab[crc & 0xff];
- (of course we must initialize crc_tab[] in advance).
-
- and (n+2)-byte sequence:
-
- n bytes of data
- CRC & 0xff (lower byte)
- CRC >> 8 (upper byte)
-
- makes up one record of data.
-
- For CRC-checking for a record of length n (n-2 bytes of data
- followed by 16-bit CRC), we have only to check whether the value of
- calc_crc(buf, n) equals to 0. Because following conditions are
- equivalent:
- (1) calc_crc(buf, n) == 0,
- (2) calc_crc(buf, n-2) == buf[n-2] + (buf[n-1]<<8).
-
- Here is a sample program that tests CRC of all records of entity
- data in DWG file. (Each records corresponds to one entity in ENTITIES
- SECTION in DXF file.) It will show one example for how CRC is used in
- DWG file.
-
- #include <stdio.h>
-
- unsigned char buffer[1000];
-
- unsigned calc_crc(buf, n)
- unsigned char buf[];
- int n;
- {
- int i, j;
- unsigned int crc = 0xc0c1;
- for ( i = 0 ; i < n ; ++i ) {
- crc ^= *buf++;
- for ( j = 0 ; j < 8 ; ++j )
- if ( crc & 1 )
- crc = (crc >> 1) ^ 0xa001;
- else
- crc >>= 1;
- }
- return crc;
- }
-
- main(argc, argv) /* argv[1]: dwg file */
- int argc;
- char **argv;
- {
- FILE *fp;
- long begin, end;
- int n;
- if ( argc == 1 ) exit(0);
- if ( (fp = fopen(argv[1], "rb")) == NULL ) {
- perror(argv[1]);
- exit(1);
- }
- fseek(fp, 0x14L, 0);
- fread(&begin, 4, 1, fp); /* Beginning of ENTITIES data */
- fread(&end, 4, 1, fp); /* End of ENTITIES data */
- fseek(fp, begin, 0);
- while ( begin < end ) {
- fread(buffer, 8, 1, fp); /* Header of this record */
- n = buffer[2] + (buffer[3]<<8); /* Length of this record */
- fread(buffer+8, n-8, 1, fp); /* Rest of this record */
- if ( calc_crc(buffer, (buffer[0] == 0x12 ? 8+2 : n)) != 0 ) {
- /* NOTE:
- * The record with buffer[0] == 0x12 is very exceptional.
- * It rarely occurs. Don't pay too much attention for it.
- */
- printf("CRC error\n");
- exit(1);
- }
- begin += n;
- }
- printf("done.\n");
- exit(0);
- }
-
- ----------
- Kazuyoshi Masuda (masuda@kurims.kyoto-u.ac.jp)
- Kyoto University
- Japan
-