home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume28 / crc16 / part01 < prev    next >
Encoding:
Text File  |  1992-03-14  |  4.9 KB  |  172 lines

  1. Newsgroups: comp.sources.misc
  2. From: hendrik@vedge.UUCP (Hendrik Boom)
  3. Subject:  v28i102:  crc16 - Generate 16-bit CRC checksums, Part01/01
  4. Message-ID: <1992Mar14.223929.5153@sparky.imd.sterling.com>
  5. X-Md4-Signature: 1d1f174f0fd424312de82f1fa3e9d55f
  6. Date: Sat, 14 Mar 1992 22:39:29 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: hendrik@vedge.UUCP (Hendrik Boom)
  10. Posting-number: Volume 28, Issue 102
  11. Archive-name: crc16/part01
  12. Environment: UNIX, Amiga
  13.  
  14. I wrote this a few years ago. It's useful for checking that file transfers
  15. work properly.
  16.  
  17. How to use it?
  18. Say
  19.     crc  filenames
  20. and get crc check values for the files on standard output, in hex and decimal.
  21.  
  22. It uses the CCITT 16-bit CRC polynomial.
  23.  
  24.     -- hendrik.
  25. ----
  26. #! /bin/sh
  27. # This is a shell archive.  Remove anything before this line, then feed it
  28. # into a shell via "sh file" or similar.  To overwrite existing files,
  29. # type "sh file -c".
  30. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  31. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  32. # Contents:  crc.c crc.makefile make_crc_tab.c
  33. # Wrapped by kent@sparky on Sat Mar 14 16:32:35 1992
  34. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  35. echo If this archive is complete, you will see the following message:
  36. echo '          "shar: End of archive 1 (of 1)."'
  37. if test -f 'crc.c' -a "${1}" != "-c" ; then 
  38.   echo shar: Will not clobber existing file \"'crc.c'\"
  39. else
  40.   echo shar: Extracting \"'crc.c'\" \(1087 characters\)
  41.   sed "s/^X//" >'crc.c' <<'END_OF_FILE'
  42. X/* The following routines can be used to interpret the crc_table.
  43. X  First use init_crc; then add_crc for each character, from
  44. X  first to last; and finally, as report_crc to yield the
  45. X  result. It is probably much more convenient to code these
  46. X  routines inline; this file serves primarily as documentation. */
  47. X#include <stdio.h>
  48. X#include "crc_tab.h"
  49. X
  50. Xstatic unsigned short crc;
  51. X
  52. Xvoid init_crc(){crc = 0;}
  53. X
  54. Xvoid add_crc(c) unsigned char c;
  55. X  {crc = crc_table[(crc & 0x00ff) ^ c] ^ (crc >> 8);
  56. X  }
  57. X
  58. Xunsigned short report_crc(){return crc;}
  59. X
  60. Xvoid crcfile(name)char *name;
  61. X {FILE *f; int c; unsigned short ck;
  62. X  printf("crc %s ", name);
  63. X  f = fopen(name, "rb");
  64. X  if(! f){printf("cannot find %s\n", name); return;}
  65. X  init_crc();
  66. X  while((c = getc(f)) >= 0){add_crc(c);}
  67. X  ck =  report_crc();
  68. X#ifdef AMIGA
  69. X  printf("0x%x %d\n", ck, ck);
  70. X#else
  71. X  printf("%#.4x %d\n", ck, ck);
  72. X#endif
  73. X  fclose(f);
  74. X  return;
  75. X }
  76. X
  77. Xvoid main(argc, argv)int argc; char *argv[];
  78. X {int i; char **c;
  79. X  if(argc < 2){printf("crc filenames\n"); exit(1);}
  80. X  c = &(argv[1]); i=argc;
  81. X  while(--i)crcfile(*c++);
  82. X  exit(0);
  83. X }
  84. END_OF_FILE
  85.   if test 1087 -ne `wc -c <'crc.c'`; then
  86.     echo shar: \"'crc.c'\" unpacked with wrong size!
  87.   fi
  88.   # end of 'crc.c'
  89. fi
  90. if test -f 'crc.makefile' -a "${1}" != "-c" ; then 
  91.   echo shar: Will not clobber existing file \"'crc.makefile'\"
  92. else
  93.   echo shar: Extracting \"'crc.makefile'\" \(211 characters\)
  94.   sed "s/^X//" >'crc.makefile' <<'END_OF_FILE'
  95. X$(SYSTYPE)/make_crc_tab : make_crc_tab.c
  96. X    cc make_crc_tab.c -o $(SYSTYPE)/make_crc_tab
  97. Xcrc_tab.h : make_crc_tab
  98. X    ./$(SYSTYPE)/make_crc_tab >crc_tab.h
  99. X$(SYSTYPE)/crc : crc.c crc_tab.h
  100. X    cc crc.c -o $(SYSTYPE)/crc
  101. END_OF_FILE
  102.   if test 211 -ne `wc -c <'crc.makefile'`; then
  103.     echo shar: \"'crc.makefile'\" unpacked with wrong size!
  104.   fi
  105.   # end of 'crc.makefile'
  106. fi
  107. if test -f 'make_crc_tab.c' -a "${1}" != "-c" ; then 
  108.   echo shar: Will not clobber existing file \"'make_crc_tab.c'\"
  109. else
  110.   echo shar: Extracting \"'make_crc_tab.c'\" \(809 characters\)
  111.   sed "s/^X//" >'make_crc_tab.c' <<'END_OF_FILE'
  112. X/* The CRC polynomial is represented in binary; each bit is the
  113. Xcoefficient of a term. The low-order bit corresponds to the
  114. Xhighest-order coefficient (yes, this does seem backwards).
  115. X
  116. XThe bit sequences for some common CRC polynomials:
  117. X
  118. XCRC-16:     16    15    2
  119. X    x   + x   + x  + 1    0x14003
  120. X
  121. XCCITT:     16    12    5
  122. X    x   + x   + x  + 1    0x10811
  123. X
  124. XThe CCITT polynomial is used for Kermit.
  125. X
  126. X*/
  127. X
  128. X#define CCITT 0x10811
  129. X#define CRC_16 0x14003
  130. X
  131. Xvoid make_16_bit_crc_tab(poly) unsigned int poly;
  132. X  {unsigned int i,j, M;
  133. X  printf("unsigned short crc_table[256] = {\n");
  134. X  for (i=0; i<256; i++){
  135. X    M = i;
  136. X    for (j=8;j>0;j--){
  137. X      if (M & 1){M ^= poly;}
  138. X      M >>= 1;}
  139. X#ifdef AMIGA
  140. X    printf("0x%x,\n", M);
  141. X#else
  142. X    printf("%#x,\n", M);
  143. X#endif
  144. X    }
  145. X  printf("};\n");
  146. X}
  147. X
  148. Xvoid main(){make_16_bit_crc_tab(CCITT);}
  149. END_OF_FILE
  150.   if test 809 -ne `wc -c <'make_crc_tab.c'`; then
  151.     echo shar: \"'make_crc_tab.c'\" unpacked with wrong size!
  152.   fi
  153.   # end of 'make_crc_tab.c'
  154. fi
  155. echo shar: End of archive 1 \(of 1\).
  156. cp /dev/null ark1isdone
  157. MISSING=""
  158. for I in 1 ; do
  159.     if test ! -f ark${I}isdone ; then
  160.     MISSING="${MISSING} ${I}"
  161.     fi
  162. done
  163. if test "${MISSING}" = "" ; then
  164.     echo You have the archive.
  165.     rm -f ark[1-9]isdone
  166. else
  167.     echo You still must unpack the following archives:
  168.     echo "        " ${MISSING}
  169. fi
  170. exit 0
  171. exit 0 # Just in case...
  172.