home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip21.zip / crc32.c < prev    next >
C/C++ Source or Header  |  1996-02-16  |  1KB  |  51 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. #ifdef CRC32
  14. # undef CRC32
  15. #endif
  16. #define CRC32(c, b) (crc_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
  17. #define DO1(buf)  crc = CRC32(crc, *buf++)
  18. #define DO2(buf)  DO1(buf); DO1(buf)
  19. #define DO4(buf)  DO2(buf); DO2(buf)
  20. #define DO8(buf)  DO4(buf); DO4(buf)
  21.  
  22. /* ========================================================================= */
  23. ulg crc32(crc, buf, len)
  24.     register ulg crc;           /* crc shift register */
  25.     register const uch *buf;    /* pointer to bytes to pump through */
  26.     extent len;                 /* number of bytes in buf[] */
  27. /* Run a set of bytes through the crc shift register.  If buf is a NULL
  28.    pointer, then initialize the crc shift register contents instead.
  29.    Return the current crc in either case. */
  30. {
  31.   register ulg near *crc_table;
  32.  
  33.   if (buf == NULL) return 0L;
  34.  
  35.   crc_table = get_crc_table();
  36.  
  37.   crc = crc ^ 0xffffffffL;
  38. #ifndef NO_UNROLLED_LOOPS
  39.   while (len >= 8) {
  40.     DO8(buf);
  41.     len -= 8;
  42.   }
  43. #endif
  44.   if (len) do {
  45.     DO1(buf);
  46.   } while (--len);
  47.   return crc ^ 0xffffffffL;     /* (instead of ~c for 64-bit machines) */
  48. }
  49. #endif /* !ASM_CRC */
  50. #endif /* !USE_ZLIB */
  51.