home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cuj9301.zip / 1101068A < prev    next >
Text File  |  1992-11-03  |  787b  |  43 lines

  1. int
  2. igchecksum (z, c)
  3.      register const char *z;
  4.      register int c;
  5. {
  6.   register unsigned int ichk1, ichk2;
  7.  
  8.   ichk1 = 0xffff;
  9.   ichk2 = 0;
  10.  
  11.   do
  12.     {
  13.       register unsigned int b;
  14.  
  15.       /* Rotate ichk1 left.  */
  16.       if ((ichk1 & 0x8000) == 0)
  17.     ichk1 <<= 1;
  18.       else
  19.     {
  20.       ichk1 <<= 1;
  21.       ++ichk1;
  22.     }
  23.  
  24.       /* Add the next character to ichk1.  */
  25.       b = *z++ & 0xff;
  26.       ichk1 += b;
  27.  
  28.       /* Add ichk1 xor the character position in the buffer counting from
  29.      the back to ichk2.  */
  30.       ichk2 += ichk1 ^ c;
  31.  
  32.       /* If the character was zero, or adding it to ichk1 caused an
  33.      overflow, xor ichk2 to ichk1.  */
  34.       if (b == 0 || (ichk1 & 0xffff) < b)
  35.     ichk1 ^= ichk2;
  36.     }
  37.   while (--c > 0);
  38.  
  39.   return ichk1 & 0xffff;
  40. }
  41.  
  42.  
  43.