home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / protocol / crc.info < prev    next >
Text File  |  2020-01-01  |  3KB  |  115 lines

  1. Andy - Here is the CRC generation function from the version of C-Kermit
  2. I'm working on now (5A).
  3.  
  4. /* CRC generation tables */
  5.  
  6. /* LONG means "unsigned long" */
  7.  
  8. static LONG crcta[16] = { 0L, 010201L, 020402L, 030603L, 041004L,
  9.   051205L, 061406L, 071607L, 0102010L, 0112211L, 0122412L, 0132613L, 0143014L,
  10.   0153215L, 0163416L, 0173617L };
  11.  
  12. static LONG crctb[16] = { 0L, 010611L, 021422L, 031233L, 043044L,
  13.   053655L, 062466L, 072277L, 0106110L, 0116701L, 0127532L, 0137323L, 0145154L,
  14.   0155745L, 0164576L, 0174367L };
  15.  
  16.  
  17. /*  C H K 3  --  Compute a type-3 Kermit block check.  */
  18. /*
  19.  Calculate the 16-bit CRC-CCITT of a null-terminated string using a lookup 
  20.  table.  Assumes the argument string contains no embedded nulls.
  21. */
  22. unsigned int
  23. chk3(pkt) register CHAR *pkt; {
  24.     register LONG c, crc;
  25.     register unsigned int m;
  26.     m = (parity) ? 0177 : 0377;
  27.     for (crc = 0; *pkt != '\0'; pkt++) {
  28.     c = crc ^ (LONG)(*pkt & m);
  29.     crc = (crc >> 8) ^ (crcta[(c & 0xF0) >> 4] ^ crctb[c & 0x0F]);
  30.     }
  31.     return((unsigned int) (crc & 0xFFFF));
  32. }
  33.  
  34. And here is a similar routine from DEC-20 Kermit (circa 1985) that shows
  35. the tables in octal, in case that's any help.
  36.  
  37. ; This routine calculates the CRC for a string, using the
  38. ; CRC-CCITT polynomial x^16+x^12+x^5+1.
  39. ; The string should be the fields of the packet between but not including
  40. ; the <mark> and the block check, which is treated as a string of bits with
  41. ; the low order bit of the first character first and the high order bit of the
  42. ; last character last -- this is how the bits arrive on the transmission line.
  43. ; The bit string is divided by the polynomial.
  44. ;
  45. ; The initial value of the CRC is 0.  The result is the remainder of this
  46. ; division, used as-is (i.e. not complemented).
  47. ;
  48. ; Call with
  49. ;  t1/ length of string
  50. ;  t2/ 8-bit byte pointer to string
  51. ; Returns +1 always, with t1/ 16-bit CRC, t2 unchanged.
  52. ;
  53. ; AC usage:
  54. ;    t1/ Accumulated CRC
  55. ;    q4/ Remaining length
  56. ;    q3/ Byte pointer to string
  57. ;    q2/ temp
  58. ;    q1/ temp
  59. ;
  60. crcclc:    saveac <q1,q2,q3,q4,t2>    ; Save q1-q4, and t2.
  61.     dmove q3,t1        ; Get arguments.
  62.     setz t1,        ; Initial CRC is 0.
  63.     move t2, parity        ;[136] Get parity.
  64.  
  65. crcc.1:    ildb q1, q4        ; Get a character.
  66.     caie t2, none        ;[136] Parity = NONE?
  67.      andi q1, ^o177        ;[136] No, doing parity, strip parity bit.
  68.     xori q1, (t1)        ; Add in with current CRC.
  69.     ldb q2, [point 4,q1,31]    ; Get high 4 bits.
  70.     andi q1, ^o17        ; And low 4 bits.
  71.     move q1, crctb2(q1)    ; Get low portion of CRC factor.
  72.     xor q1, crctab(q2)    ; Plus high portion.
  73.     lsh t1, -^d8        ; Shift off a byte from previous CRC.
  74.     xor t1, q1        ; Add in new value.
  75.     sojg q3, crcc.1        ; Loop for all characters.
  76.     
  77.     ret            ; Done, return +1 with CRC in t1.
  78.  
  79. ; Data tables for CRC-CCITT generation
  80.  
  81. crctab:    oct    0
  82.     oct    10201
  83.     oct    20402
  84.     oct    30603
  85.     oct    41004
  86.     oct    51205
  87.     oct    61406
  88.     oct    71607
  89.     oct    102010
  90.     oct    112211
  91.     oct    122412
  92.     oct    132613
  93.     oct    143014
  94.     oct    153215
  95.     oct    163416
  96.     oct    173617
  97.  
  98. crctb2:    oct    0
  99.     oct    10611
  100.     oct    21422
  101.     oct    31233
  102.     oct    43044
  103.     oct    53655
  104.     oct    62466
  105.     oct    72277
  106.     oct    106110
  107.     oct    116701
  108.     oct    127532
  109.     oct    137323
  110.     oct    145154
  111.     oct    155745
  112.     oct    164576
  113.     oct    174367
  114.