xref: /PHP-5.6/ext/zip/lib/zip_source_crc.c (revision 0a950a05)
1 /*
2   zip_source_crc.c -- pass-through source that calculates CRC32 and size
3   Copyright (C) 2009 Dieter Baron and Thomas Klausner
4 
5   This file is part of libzip, a library to manipulate ZIP archives.
6   The authors can be contacted at <libzip@nih.at>
7 
8   Redistribution and use in source and binary forms, with or without
9   modification, are permitted provided that the following conditions
10   are met:
11   1. Redistributions of source code must retain the above copyright
12      notice, this list of conditions and the following disclaimer.
13   2. Redistributions in binary form must reproduce the above copyright
14      notice, this list of conditions and the following disclaimer in
15      the documentation and/or other materials provided with the
16      distribution.
17   3. The names of the authors may not be used to endorse or promote
18      products derived from this software without specific prior
19      written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22   OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 
35 
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include "zipint.h"
40 
41 struct crc_context {
42     int eof;
43     int validate;
44     int e[2];
45     zip_uint64_t size;
46     zip_uint32_t crc;
47 };
48 
49 static zip_int64_t crc_read(struct zip_source *, void *, void *
50 			    , zip_uint64_t, enum zip_source_cmd);
51 
52 
53 
54 struct zip_source *
zip_source_crc(struct zip * za,struct zip_source * src,int validate)55 zip_source_crc(struct zip *za, struct zip_source *src, int validate)
56 {
57     struct crc_context *ctx;
58 
59     if (src == NULL) {
60 	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
61 	return NULL;
62     }
63 
64     if ((ctx=(struct crc_context *)malloc(sizeof(*ctx))) == NULL) {
65 	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
66 	return NULL;
67     }
68 
69     ctx->eof = 0;
70     ctx->validate = validate;
71     ctx->e[0] = ctx->e[1] = 0;
72     ctx->size = 0;
73     ctx->crc = 0;
74 
75     return zip_source_layered(za, src, crc_read, ctx);
76 }
77 
78 
79 
80 static zip_int64_t
crc_read(struct zip_source * src,void * _ctx,void * data,zip_uint64_t len,enum zip_source_cmd cmd)81 crc_read(struct zip_source *src, void *_ctx, void *data,
82 	 zip_uint64_t len, enum zip_source_cmd cmd)
83 {
84     struct crc_context *ctx;
85     zip_int64_t n;
86 
87     ctx = (struct crc_context *)_ctx;
88 
89     switch (cmd) {
90     case ZIP_SOURCE_OPEN:
91 	ctx->eof = 0;
92 	ctx->crc = (zip_uint32_t)crc32(0, NULL, 0);
93 	ctx->size = 0;
94 
95 	return 0;
96 
97     case ZIP_SOURCE_READ:
98 	if (ctx->eof || len == 0)
99 	    return 0;
100 
101 	if ((n=zip_source_read(src, data, len)) < 0)
102 	    return ZIP_SOURCE_ERR_LOWER;
103 
104 	if (n == 0) {
105 	    ctx->eof = 1;
106 	    if (ctx->validate) {
107 		struct zip_stat st;
108 
109 		if (zip_source_stat(src, &st) < 0)
110 		    return ZIP_SOURCE_ERR_LOWER;
111 
112 		if ((st.valid & ZIP_STAT_CRC) && st.crc != ctx->crc) {
113 		    ctx->e[0] = ZIP_ER_CRC;
114 		    ctx->e[1] = 0;
115 
116 		    return -1;
117 		}
118 		if ((st.valid & ZIP_STAT_SIZE) && st.size != ctx->size) {
119 		    ctx->e[0] = ZIP_ER_INCONS;
120 		    ctx->e[1] = 0;
121 
122 		    return -1;
123 		}
124 	    }
125 	}
126 	else {
127 	    ctx->size += (zip_uint64_t)n;
128 	    ctx->crc = (zip_uint32_t)crc32(ctx->crc, (const Bytef *)data, (uInt)n); /* TODO: check for overflow, use multiple crc calls if needed */
129 	}
130 	return n;
131 
132     case ZIP_SOURCE_CLOSE:
133 	return 0;
134 
135     case ZIP_SOURCE_STAT:
136 	{
137 	    struct zip_stat *st;
138 
139 	    st = (struct zip_stat *)data;
140 
141 	    if (ctx->eof) {
142 		/* TODO: Set comp_size, comp_method, encryption_method?
143 		        After all, this only works for uncompressed data. */
144 		st->size = ctx->size;
145 		st->crc = ctx->crc;
146 		st->comp_size = ctx->size;
147 		st->comp_method = ZIP_CM_STORE;
148 		st->encryption_method = ZIP_EM_NONE;
149 		st->valid |= ZIP_STAT_SIZE|ZIP_STAT_CRC|ZIP_STAT_COMP_SIZE|ZIP_STAT_COMP_METHOD|ZIP_STAT_ENCRYPTION_METHOD;;
150 	    }
151 	}
152 	return 0;
153 
154     case ZIP_SOURCE_ERROR:
155 	memcpy(data, ctx->e, sizeof(ctx->e));
156 	return 0;
157 
158     case ZIP_SOURCE_FREE:
159 	free(ctx);
160 	return 0;
161 
162     default:
163 	return -1;
164     }
165 
166 }
167