New source that computes size/crc.  Use it in zip_fread to verify crc.

--HG--
branch : HEAD
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index e5f3ae9..60ca194 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -100,6 +100,7 @@
   zip_set_name.c
   zip_source_buffer.c
   zip_source_call.c
+  zip_source_crc.c
   zip_source_deflate.c
   zip_source_file.c
   zip_source_filep.c
diff --git a/lib/Makefile.am b/lib/Makefile.am
index e4baef8..7aa49ad 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -56,6 +56,7 @@
 	zip_set_file_comment.c \
 	zip_source_buffer.c \
 	zip_source_call.c \
+	zip_source_crc.c \
 	zip_source_deflate.c \
 	zip_source_file.c \
 	zip_source_filep.c \
diff --git a/lib/zip.h b/lib/zip.h
index 58b1c3e..9a44fdf 100644
--- a/lib/zip.h
+++ b/lib/zip.h
@@ -252,6 +252,7 @@
 						zip_uint64_t, int);
 ZIP_EXTERN zip_int64_t zip_source_call(struct zip_source *, void *,
 				       zip_uint64_t, enum zip_source_cmd);
+ZIP_EXTERN struct zip_source *zip_source_crc(struct zip *, struct zip_source *);
 ZIP_EXTERN struct zip_source *zip_source_deflate(struct zip *,
 						 struct zip_source *,
 						 zip_uint16_t, int);
diff --git a/lib/zip_error.c b/lib/zip_error.c
index aab7079..59aeea8 100644
--- a/lib/zip_error.c
+++ b/lib/zip_error.c
@@ -99,3 +99,14 @@
 	err->sys_err = se;
     }
 }
+
+
+
+void
+_zip_error_set_from_source(struct zip_error *err, struct zip_source *src)
+{
+    int e[2];
+    
+    zip_source_call(src, e, sizeof(e), ZIP_SOURCE_ERROR);
+    _zip_error_set(err, e[0], e[1]);
+}
diff --git a/lib/zip_fclose.c b/lib/zip_fclose.c
index 66d74e4..8a15feb 100644
--- a/lib/zip_fclose.c
+++ b/lib/zip_fclose.c
@@ -58,11 +58,6 @@
     ret = 0;
     if (zf->error.zip_err)
 	ret = zf->error.zip_err;
-    else if ((zf->flags & ZIP_ZF_CRC) && (zf->flags & ZIP_ZF_EOF)) {
-	/* if EOF, compare CRC */
-	if (zf->crc_orig != zf->crc)
-	    ret = ZIP_ER_CRC;
-    }
 
     free(zf);
     return ret;
diff --git a/lib/zip_fopen_index_encrypted.c b/lib/zip_fopen_index_encrypted.c
index 0abbcef..142976c 100644
--- a/lib/zip_fopen_index_encrypted.c
+++ b/lib/zip_fopen_index_encrypted.c
@@ -47,7 +47,6 @@
 zip_fopen_index_encrypted(struct zip *za, zip_uint64_t fileno, int flags,
 			  const char *password)
 {
-    int zfflags;
     struct zip_file *zf;
     zip_compression_implementation comp_impl;
     zip_encryption_implementation enc_impl;
@@ -76,10 +75,6 @@
 
     zip_stat_index(za, fileno, flags, &st);
 
-    zfflags = 0;
-    if (st.comp_method == ZIP_CM_STORE || (flags & ZIP_FL_COMPRESSED) == 0)
-	zfflags |= ZIP_ZF_CRC;
-
     enc_impl = NULL;
     if ((flags & ZIP_FL_ENCRYPTED) == 0) {
 	if (st.encryption_method != ZIP_EM_NONE) {
@@ -135,6 +130,15 @@
 	    }
 	    src = s2;
 	}
+	if ((flags & ZIP_FL_COMPRESSED) == 0
+	    || st.comp_method == ZIP_CM_STORE ) {
+	    if ((s2=zip_source_crc(za, src)) == NULL) {
+		zip_source_free(src);
+		/* XXX: set error (how?) */
+		return NULL;
+	    }
+	    src = s2;
+	}
     }
 
     if (zip_source_call(src, NULL, 0, ZIP_SOURCE_OPEN) < 0) {
@@ -150,9 +154,8 @@
 
     zf = _zip_file_new(za);
 
-    zf->flags = zfflags;
-    zf->bytes_left = za->cdir->entry[fileno].uncomp_size;
-    zf->crc_orig = za->cdir->entry[fileno].crc;
+    zf->size = za->cdir->entry[fileno].uncomp_size;
+    zf->crc = za->cdir->entry[fileno].crc;
     zf->src = src;
 
     return zf;
@@ -188,10 +191,9 @@
 
     zf->za = za;
     _zip_error_init(&zf->error);
-    zf->flags = 0;
-    zf->crc = crc32(0L, Z_NULL, 0);
-    zf->crc_orig = 0;
-    zf->bytes_left = 0;
+    zf->eof = 0;
+    zf->crc = 0;
+    zf->size = 0;
     zf->src = NULL;
 
     return zf;
diff --git a/lib/zip_fread.c b/lib/zip_fread.c
index ecda5b0..aab0097 100644
--- a/lib/zip_fread.c
+++ b/lib/zip_fread.c
@@ -53,35 +53,32 @@
 	return -1;
     }
 
-    if ((zf->flags & ZIP_ZF_EOF) || (toread == 0))
+    if ((zf->eof) || (toread == 0))
 	return 0;
 
     if ((n=zip_source_call(zf->src, outbuf, toread, ZIP_SOURCE_READ)) < 0) {
-	int e[2];
-	zip_source_call(zf->src, e, sizeof(e), ZIP_SOURCE_ERROR);
-	_zip_error_set(&zf->error, e[0], e[1]);
+	_zip_error_set_from_source(&zf->error, zf->src);
 	return -1;
     }
 
     if (n == 0) {
-	zf->flags |= ZIP_ZF_EOF;
+	struct zip_stat st;
 
-	if (zf->flags & ZIP_ZF_CRC) {
-	    if (zf->bytes_left != 0) {
-		_zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
-		return -1;
-	    }
-
-	    if (zf->crc != zf->crc_orig) {
-		_zip_error_set(&zf->error, ZIP_ER_CRC, 0);
-		return -1;
-	    }
+	if (zip_source_call(zf->src, &st, sizeof(st), ZIP_SOURCE_STAT) < 0) {
+	    _zip_error_set_from_source(&zf->error, zf->src);
+	    return -1;
 	}
-    }
-    else {
-	if (zf->flags & ZIP_ZF_CRC) {
-	    zf->bytes_left -= n;
-	    zf->crc = crc32(zf->crc, (Bytef *)outbuf, n);
+
+	zf->eof = 1;
+
+	if ((st.valid & ZIP_STAT_SIZE) && zf->size != st.size) {
+	    _zip_error_set(&zf->error, ZIP_ER_INCONS, 0);
+	    return -1;
+	}
+
+	if ((st.valid & ZIP_STAT_CRC) && zf->crc != st.crc) {
+	    _zip_error_set(&zf->error, ZIP_ER_CRC, 0);
+	    return -1;
 	}
     }
 
diff --git a/lib/zip_source_crc.c b/lib/zip_source_crc.c
new file mode 100644
index 0000000..e13141c
--- /dev/null
+++ b/lib/zip_source_crc.c
@@ -0,0 +1,142 @@
+/*
+  zip_source_crc.c -- pass-through source that calculates CRC32
+  Copyright (C) 2009 Dieter Baron and Thomas Klausner
+
+  This file is part of libzip, a library to manipulate ZIP archives.
+  The authors can be contacted at <libzip@nih.at>
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+  1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in
+     the documentation and/or other materials provided with the
+     distribution.
+  3. The names of the authors may not be used to endorse or promote
+     products derived from this software without specific prior
+     written permission.
+ 
+  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
+  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "zipint.h"
+
+struct crc {
+    struct zip_source *src;
+    int eof;
+    zip_uint64_t size;
+    zip_uint32_t crc;
+};
+
+static zip_int64_t crc_read(void *, void *, zip_uint64_t, enum zip_source_cmd);
+
+
+
+ZIP_EXTERN struct zip_source *
+zip_source_crc(struct zip *za, struct zip_source *src)
+{
+    struct crc *ctx;
+
+    if (za == NULL || src == NULL) {
+	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
+	return NULL;
+    }
+
+    if ((ctx=(struct crc *)malloc(sizeof(*ctx))) == NULL) {
+	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
+	return NULL;
+    }
+
+    ctx->eof = 0;
+    ctx->crc = crc32(0, NULL, 0);
+    ctx->size = 0;
+    ctx->src = src;
+
+
+    return zip_source_function(za, crc_read, ctx);
+}
+
+
+
+static zip_int64_t
+crc_read(void *_ctx, void *data, zip_uint64_t len, enum zip_source_cmd cmd)
+{
+    struct crc *ctx;
+    zip_int64_t n;
+
+    ctx = (struct crc *)_ctx;
+
+    switch (cmd) {
+    case ZIP_SOURCE_OPEN:
+	if (zip_source_call(ctx->src, data, len, cmd) < 0)
+	    return -1;
+
+	return 0;
+
+    case ZIP_SOURCE_READ:
+	if (ctx->eof || len == 0)
+	    return 0;
+
+	if ((n=zip_source_call(ctx->src, data, len, cmd)) < 0)
+	    return -1;
+
+	if (n == 0)
+	    ctx->eof = 1;
+	else {
+	    ctx->size += n;
+	    ctx->crc = crc32(ctx->crc, data, n);
+	}
+	return n;
+
+    case ZIP_SOURCE_CLOSE:
+	if (zip_source_call(ctx->src, data, len, cmd) < 0)
+	    return -1;
+	return 0;
+
+    case ZIP_SOURCE_STAT:
+	if (zip_source_call(ctx->src, data, len, cmd) < 0)
+	    return -1;
+	else {
+	    struct zip_stat *st;
+
+	    st = (struct zip_stat *)data;
+
+	    if (ctx->eof) {
+		/* XXX: error if mismatch with what src provided? */
+		st->size = ctx->size;
+		st->crc = ctx->crc;
+		st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC;
+	    }
+	}
+	return 0;
+	
+    case ZIP_SOURCE_ERROR:
+	return zip_source_call(ctx->src, data, len, cmd);
+
+    case ZIP_SOURCE_FREE:
+	zip_source_call(ctx->src, data, len, cmd);
+	free(ctx);
+	return 0;
+
+    default:
+	return zip_source_call(ctx->src, data, len, cmd);
+    }
+    
+}
diff --git a/lib/zipint.h b/lib/zipint.h
index 41636b4..decd334 100644
--- a/lib/zipint.h
+++ b/lib/zipint.h
@@ -136,11 +136,10 @@
 struct zip_file {
     struct zip *za;		/* zip archive containing this file */
     struct zip_error error;	/* error information */
-    int flags;			/* -1: eof, >0: error */
+    int eof;
 
-    unsigned long bytes_left;	/* number of bytes left to read */
-    unsigned long crc;		/* CRC so far */
-    unsigned long crc_orig;	/* CRC recorded in archive */
+    zip_uint64_t size;		/* uncompressed size recorded in archive */
+    zip_uint32_t crc;		/* CRC recorded in archive */
 
     struct zip_source *src;	/* data source */
 };
@@ -236,6 +235,7 @@
 void _zip_error_get(struct zip_error *, int *, int *);
 void _zip_error_init(struct zip_error *);
 void _zip_error_set(struct zip_error *, int, int);
+void _zip_error_set_from_source(struct zip_error *, struct zip_source *);
 const char *_zip_error_strerror(struct zip_error *);
 
 int _zip_file_fillbuf(void *, size_t, struct zip_file *);