summaryrefslogtreecommitdiff
path: root/ext/zip/lib/zip_source_crc.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/zip/lib/zip_source_crc.c')
-rw-r--r--ext/zip/lib/zip_source_crc.c31
1 files changed, 19 insertions, 12 deletions
diff --git a/ext/zip/lib/zip_source_crc.c b/ext/zip/lib/zip_source_crc.c
index 7fd78f5697..7d6df6a385 100644
--- a/ext/zip/lib/zip_source_crc.c
+++ b/ext/zip/lib/zip_source_crc.c
@@ -38,7 +38,7 @@
#include "zipint.h"
-struct crc {
+struct crc_context {
int eof;
int validate;
int e[2];
@@ -51,23 +51,27 @@ static zip_int64_t crc_read(struct zip_source *, void *, void *
-ZIP_EXTERN(struct zip_source *)
+struct zip_source *
zip_source_crc(struct zip *za, struct zip_source *src, int validate)
{
- struct crc *ctx;
+ struct crc_context *ctx;
if (src == NULL) {
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
return NULL;
}
- if ((ctx=(struct crc *)malloc(sizeof(*ctx))) == NULL) {
+ if ((ctx=(struct crc_context *)malloc(sizeof(*ctx))) == NULL) {
_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
return NULL;
}
+ ctx->eof = 0;
ctx->validate = validate;
-
+ ctx->e[0] = ctx->e[1] = 0;
+ ctx->size = 0;
+ ctx->crc = 0;
+
return zip_source_layered(za, src, crc_read, ctx);
}
@@ -77,15 +81,15 @@ static zip_int64_t
crc_read(struct zip_source *src, void *_ctx, void *data,
zip_uint64_t len, enum zip_source_cmd cmd)
{
- struct crc *ctx;
+ struct crc_context *ctx;
zip_int64_t n;
- ctx = (struct crc *)_ctx;
+ ctx = (struct crc_context *)_ctx;
switch (cmd) {
case ZIP_SOURCE_OPEN:
ctx->eof = 0;
- ctx->crc = crc32(0, NULL, 0);
+ ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
ctx->size = 0;
return 0;
@@ -120,8 +124,8 @@ crc_read(struct zip_source *src, void *_ctx, void *data,
}
}
else {
- ctx->size += n;
- ctx->crc = crc32(ctx->crc, data, n);
+ ctx->size += (zip_uint64_t)n;
+ ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data, (uInt)n); /* TODO: check for overflow, use multiple crc calls if needed */
}
return n;
@@ -135,11 +139,14 @@ crc_read(struct zip_source *src, void *_ctx, void *data,
st = (struct zip_stat *)data;
if (ctx->eof) {
- /* XXX: Set comp_size, comp_method, encryption_method?
+ /* TODO: Set comp_size, comp_method, encryption_method?
After all, this only works for uncompressed data. */
st->size = ctx->size;
st->crc = ctx->crc;
- st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC;
+ st->comp_size = ctx->size;
+ st->comp_method = ZIP_CM_STORE;
+ st->encryption_method = ZIP_EM_NONE;
+ st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;;
}
}
return 0;