From cb7bf3e20839ac6e728ff377778859f0d6a715ec Mon Sep 17 00:00:00 2001
From: Bron Gondwana <brong@fastmail.fm>
Date: Mon, 24 Aug 2009 11:56:19 +1000
Subject: [PATCH] CRC32 functions

Add support for calculating the CRC32 of a buffer or an iovec
---
 lib/Makefile.in |    8 ++++----
 lib/crc32.c     |   43 +++++++++++++++++++++++++++++++++++++++++++
 lib/crc32.h     |   12 ++++++++++++
 3 files changed, 59 insertions(+), 4 deletions(-)
 create mode 100644 lib/crc32.c
 create mode 100644 lib/crc32.h

diff --git a/lib/Makefile.in b/lib/Makefile.in
index 091995c..ebe06ab 100644
--- a/lib/Makefile.in
+++ b/lib/Makefile.in
@@ -88,7 +88,7 @@ LIBCYR_HDRS = $(srcdir)/acl.h $(srcdir)/assert.h $(srcdir)/auth.h \
 	$(srcdir)/xmalloc.h $(srcdir)/imapurl.h \
 	$(srcdir)/cyrusdb.h $(srcdir)/iptostring.h $(srcdir)/rfc822date.h \
 	$(srcdir)/libcyr_cfg.h $(srcdir)/byteorder64.h \
-	$(srcdir)/md5.h $(srcdir)/hmac-md5.h
+	$(srcdir)/md5.h $(srcdir)/hmac-md5.h $(srcdir)/crc32.h
 
 LIBCYR_OBJS = acl.o bsearch.o charset.o glob.o retry.o util.o \
 	libcyr_cfg.o mkgmtime.o prot.o parseaddr.o imclient.o imparse.o \
@@ -97,14 +97,14 @@ LIBCYR_OBJS = acl.o bsearch.o charset.o glob.o retry.o util.o \
 	gmtoff_@WITH_GMTOFF@.o map_@WITH_MAP@.o $(ACL) $(AUTH) \
 	@LIBOBJS@ @CYRUSDB_OBJS@ @MD5OBJ@ \
 	iptostring.o xmalloc.o wildmat.o byteorder64.o \
-	xstrlcat.o xstrlcpy.o
+	xstrlcat.o xstrlcpy.o crc32.o
 
 LIBCYRM_HDRS = $(srcdir)/hash.h $(srcdir)/mpool.h $(srcdir)/xmalloc.h \
 	$(srcdir)/xstrlcat.h $(srcdir)/xstrlcpy.h $(srcdir)/util.h \
 	$(srcdir)/strhash.h $(srcdir)/libconfig.h $(srcdir)/assert.h \
-	imapopts.h signals.h
+	imapopts.h signals.h $(srcdir)/crc32.h
 LIBCYRM_OBJS = libconfig.o imapopts.o hash.o mpool.o xmalloc.o strhash.o \
-	xstrlcat.o xstrlcpy.o assert.o util.o signals.o @IPV6_OBJS@
+	xstrlcat.o xstrlcpy.o assert.o util.o signals.o @IPV6_OBJS@ crc32.o
 
 all: $(BUILTSOURCES) libcyrus_min.a libcyrus.a
 
diff --git a/lib/crc32.c b/lib/crc32.c
new file mode 100644
index 0000000..21adcf3
--- /dev/null
+++ b/lib/crc32.c
@@ -0,0 +1,43 @@
+/* crc32.h
+ */
+
+#include <config.h>
+#include "crc32.h"
+
+#ifdef HAVE_ZLIB
+
+#include <zlib.h>
+
+uint32_t crc32_buf(const char *buf, unsigned bytes)
+{
+    uint32_t crc = crc32(0L, Z_NULL, 0);
+    crc = crc32(crc, buf, bytes);
+    return crc;
+}
+
+uint32_t crc32_iovec(struct iovec *iov, int iovcnt)
+{
+    int n;
+    uint32_t crc = crc32(0L, Z_NULL, 0);
+    for (n = 0; n < iovcnt; n++) {
+	if (iov[n].iov_len)
+	    crc = crc32(crc, iov[n].iov_base, iov[n].iov_len);
+    }
+    return crc;
+}
+
+#else
+
+/* STUB */
+uint32_t crc32_buf(const char *buf, unsigned bytes)
+{
+    return 0;
+}
+
+/* STUB */
+uint32_t crc32_iovec(struct iovec *iov, int iovcnt)
+{
+    return 0;
+}
+
+#endif
diff --git a/lib/crc32.h b/lib/crc32.h
new file mode 100644
index 0000000..a810125
--- /dev/null
+++ b/lib/crc32.h
@@ -0,0 +1,12 @@
+/* crc32.h
+ */
+
+#ifndef CRC32_H
+#define CRC32_H
+#include <sys/uio.h>
+#include <stdint.h>
+
+uint32_t crc32_buf(const char *buf, unsigned bytes);
+uint32_t crc32_iovec(struct iovec *iov, int iovcnt);
+
+#endif
-- 
1.5.6.5

