home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / fst03f.zip / crc.c < prev    next >
C/C++ Source or Header  |  1996-01-09  |  2KB  |  62 lines

  1. /* crc.c -- Compute CRCs
  2.    Copyright (c) 1995-1996 by Eberhard Mattes
  3.  
  4. This file is part of fst.
  5.  
  6. fst is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. fst is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with fst; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include "crc.h"
  24.  
  25. #define CRC_POLYNOMIAL 0x4c11db7
  26.  
  27. static crc_t crc_table[256];
  28.  
  29. void crc_build_table (void)
  30. {
  31.   int i, j;
  32.   crc_t t;
  33.  
  34.   crc_table[0] = 0;
  35.   for (i = 0, j = 0; i < 128; ++i, j += 2)
  36.     {
  37.       t = crc_table[i] << 1;
  38.       if (crc_table[i] & 0x80000000)
  39.         {
  40.           crc_table[j+0] = t ^ CRC_POLYNOMIAL;
  41.           crc_table[j+1] = t;
  42.         }
  43.       else
  44.         {
  45.           crc_table[j+0] = t;
  46.           crc_table[j+1] = t ^ CRC_POLYNOMIAL;
  47.         }
  48.     }
  49. }
  50.  
  51.  
  52. crc_t crc_compute (const unsigned char *src, size_t size)
  53. {
  54.   size_t i;
  55.   crc_t crc;
  56.  
  57.   crc = ~0;
  58.   for (i = 0; i < size; ++i)
  59.     crc = (crc << 8) ^ crc_table[(crc >> 24) ^ src[i]];
  60.   return ~crc;
  61. }
  62.