home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / crc32.c < prev    next >
C/C++ Source or Header  |  1997-01-24  |  1KB  |  55 lines

  1. /* crc32.c -- compute the CRC-32 of a data stream
  2.  * Copyright (C) 1995 Mark Adler
  3.  * For conditions of distribution and use, see copyright notice in zlib.h
  4.  */
  5.  
  6. /* $Id: crc32.c,v 1.5 1996/01/13 14:55:12 spc Exp $ */
  7.  
  8. #include "zip.h"
  9.  
  10. #ifndef USE_ZLIB
  11. #ifndef ASM_CRC
  12.  
  13. #ifndef ZCONST
  14. #  define ZCONST const
  15. #endif
  16.  
  17. #ifdef CRC32
  18. #  undef CRC32
  19. #endif
  20. #define CRC32(c, b) (crc_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
  21. #define DO1(buf)  crc = CRC32(crc, *buf++)
  22. #define DO2(buf)  DO1(buf); DO1(buf)
  23. #define DO4(buf)  DO2(buf); DO2(buf)
  24. #define DO8(buf)  DO4(buf); DO4(buf)
  25.  
  26. /* ========================================================================= */
  27. ulg crc32(crc, buf, len)
  28.     register ulg crc;           /* crc shift register */
  29.     register ZCONST uch *buf;   /* pointer to bytes to pump through */
  30.     extent len;                 /* number of bytes in buf[] */
  31. /* Run a set of bytes through the crc shift register.  If buf is a NULL
  32.    pointer, then initialize the crc shift register contents instead.
  33.    Return the current crc in either case. */
  34. {
  35.   register ulg near *crc_table;
  36.  
  37.   if (buf == NULL) return 0L;
  38.  
  39.   crc_table = get_crc_table();
  40.  
  41.   crc = crc ^ 0xffffffffL;
  42. #ifndef NO_UNROLLED_LOOPS
  43.   while (len >= 8) {
  44.     DO8(buf);
  45.     len -= 8;
  46.   }
  47. #endif
  48.   if (len) do {
  49.     DO1(buf);
  50.   } while (--len);
  51.   return crc ^ 0xffffffffL;     /* (instead of ~c for 64-bit machines) */
  52. }
  53. #endif /* !ASM_CRC */
  54. #endif /* !USE_ZLIB */
  55.