xref: /PHP-5.3/ext/zip/lib/zip_source_zip.c (revision 335a11b1)
1 /*
2   zip_source_zip.c -- create data source from zip file
3   Copyright (C) 1999-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 read_zip {
42     struct zip_file *zf;
43     struct zip_stat st;
44     zip_uint64_t off;
45     zip_int64_t len;
46 };
47 
48 static zip_int64_t read_zip(void *st, void *data, zip_uint64_t len,
49 			enum zip_source_cmd cmd);
50 
51 
52 
53 ZIP_EXTERN(struct zip_source *)
zip_source_zip(struct zip * za,struct zip * srcza,zip_uint64_t srcidx,int flags,zip_uint64_t start,zip_int64_t len)54 zip_source_zip(struct zip *za, struct zip *srcza, zip_uint64_t srcidx,
55 	       int flags, zip_uint64_t start, zip_int64_t len)
56 {
57     struct zip_error error;
58     struct zip_source *zs;
59     struct read_zip *p;
60 
61     /* XXX: ZIP_FL_RECOMPRESS */
62 
63     if (za == NULL)
64 	return NULL;
65 
66     if (srcza == NULL || len < -1 || srcidx < 0 || srcidx >= srcza->nentry) {
67 	_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
68 	return NULL;
69     }
70 
71     if ((flags & ZIP_FL_UNCHANGED) == 0
72 	&& ZIP_ENTRY_DATA_CHANGED(srcza->entry+srcidx)) {
73 	_zip_error_set(&za->error, ZIP_ER_CHANGED, 0);
74 	return NULL;
75     }
76 
77     if (len == 0)
78 	len = -1;
79 
80     if (start == 0 && len == -1 && (flags & ZIP_FL_RECOMPRESS) == 0)
81 	flags |= ZIP_FL_COMPRESSED;
82     else
83 	flags &= ~ZIP_FL_COMPRESSED;
84 
85     if ((p=(struct read_zip *)malloc(sizeof(*p))) == NULL) {
86 	_zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
87 	return NULL;
88     }
89 
90     _zip_error_copy(&error, &srcza->error);
91 
92     if (zip_stat_index(srcza, srcidx, flags, &p->st) < 0
93 	|| (p->zf=zip_fopen_index(srcza, srcidx, flags)) == NULL) {
94 	free(p);
95 	_zip_error_copy(&za->error, &srcza->error);
96 	_zip_error_copy(&srcza->error, &error);
97 
98 	return NULL;
99     }
100     p->off = start;
101     p->len = len;
102 
103     if ((flags & ZIP_FL_COMPRESSED) == 0) {
104 	p->st.size = p->st.comp_size = len;
105 	p->st.comp_method = ZIP_CM_STORE;
106 	p->st.crc = 0;
107     }
108 
109     if ((zs=zip_source_function(za, read_zip, p)) == NULL) {
110 	free(p);
111 	return NULL;
112     }
113 
114     return zs;
115 }
116 
117 
118 
119 static zip_int64_t
read_zip(void * state,void * data,zip_uint64_t len,enum zip_source_cmd cmd)120 read_zip(void *state, void *data, zip_uint64_t len, enum zip_source_cmd cmd)
121 {
122     struct read_zip *z;
123     char b[8192], *buf;
124     int i;
125     zip_uint64_t n;
126 
127     z = (struct read_zip *)state;
128     buf = (char *)data;
129 
130     switch (cmd) {
131     case ZIP_SOURCE_OPEN:
132 	for (n=0; n<z->off; n+= i) {
133 	    i = (z->off-n > sizeof(b) ? sizeof(b) : z->off-n);
134 	    if ((i=zip_fread(z->zf, b, i)) < 0) {
135 		zip_fclose(z->zf);
136 		z->zf = NULL;
137 		return -1;
138 	    }
139 	}
140 	return 0;
141 
142     case ZIP_SOURCE_READ:
143 	if (z->len != -1)
144 	    n = len > z->len ? z->len : len;
145 	else
146 	    n = len;
147 
148 
149 	if ((i=zip_fread(z->zf, buf, n)) < 0)
150 	    return -1;
151 
152 	if (z->len != -1)
153 	    z->len -= i;
154 
155 	return i;
156 
157     case ZIP_SOURCE_CLOSE:
158 	return 0;
159 
160     case ZIP_SOURCE_STAT:
161 	if (len < sizeof(z->st))
162 	    return -1;
163 	len = sizeof(z->st);
164 
165 	memcpy(data, &z->st, len);
166 	return len;
167 
168     case ZIP_SOURCE_ERROR:
169 	{
170 	    int *e;
171 
172 	    if (len < sizeof(int)*2)
173 		return -1;
174 
175 	    e = (int *)data;
176 	    zip_file_error_get(z->zf, e, e+1);
177 	}
178 	return sizeof(int)*2;
179 
180     case ZIP_SOURCE_FREE:
181 	zip_fclose(z->zf);
182 	free(z);
183 	return 0;
184 
185     default:
186 	;
187     }
188 
189     return -1;
190 }
191