zipruby 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of zipruby might be problematic. Click here for more details.
- data/README.txt +131 -0
- data/ext/extconf.rb +1 -1
- data/ext/libzip.mswin32.patch +113 -0
- data/ext/libzip.vcproj +308 -0
- data/ext/libzip.vcproj.IBM-94B792EFFD1.sugawara.user +37 -0
- data/ext/mkstemp.c +143 -0
- data/ext/zip.h +211 -0
- data/ext/zip_add.c +52 -0
- data/ext/zip_add_dir.c +83 -0
- data/ext/zip_close.c +566 -0
- data/ext/zip_delete.c +61 -0
- data/ext/zip_dirent.c +531 -0
- data/ext/zip_entry_free.c +55 -0
- data/ext/zip_entry_new.c +81 -0
- data/ext/zip_err_str.c +72 -0
- data/ext/zip_error.c +104 -0
- data/ext/zip_error_clear.c +47 -0
- data/ext/zip_error_get.c +47 -0
- data/ext/zip_error_get_sys_type.c +50 -0
- data/ext/zip_error_strerror.c +93 -0
- data/ext/zip_error_to_str.c +77 -0
- data/ext/zip_fclose.c +74 -0
- data/ext/zip_file_error_clear.c +47 -0
- data/ext/zip_file_error_get.c +47 -0
- data/ext/zip_file_get_offset.c +77 -0
- data/ext/zip_file_strerror.c +47 -0
- data/ext/zip_fopen.c +52 -0
- data/ext/zip_fopen_index.c +219 -0
- data/ext/zip_fread.c +125 -0
- data/ext/zip_free.c +83 -0
- data/ext/zip_get_archive_comment.c +63 -0
- data/ext/zip_get_file_comment.c +61 -0
- data/ext/zip_get_name.c +74 -0
- data/ext/zip_get_num_files.c +50 -0
- data/ext/zip_memdup.c +58 -0
- data/ext/zip_name_locate.c +95 -0
- data/ext/zip_new.c +71 -0
- data/ext/zip_open.c +520 -0
- data/ext/zip_rename.c +52 -0
- data/ext/zip_replace.c +81 -0
- data/ext/zip_set_archive_comment.c +68 -0
- data/ext/zip_set_file_comment.c +69 -0
- data/ext/zip_set_name.c +77 -0
- data/ext/zip_source_buffer.c +160 -0
- data/ext/zip_source_file.c +71 -0
- data/ext/zip_source_filep.c +176 -0
- data/ext/zip_source_free.c +54 -0
- data/ext/zip_source_function.c +62 -0
- data/ext/zip_source_zip.c +189 -0
- data/ext/zip_stat.c +52 -0
- data/ext/zip_stat_index.c +93 -0
- data/ext/zip_stat_init.c +53 -0
- data/ext/zip_strerror.c +47 -0
- data/ext/zip_unchange.c +84 -0
- data/ext/zip_unchange_all.c +56 -0
- data/ext/zip_unchange_archive.c +52 -0
- data/ext/zip_unchange_data.c +53 -0
- data/ext/zipint.h +240 -0
- data/ext/zipruby.h +1 -1
- metadata +63 -5
data/ext/zip_open.c
ADDED
@@ -0,0 +1,520 @@
|
|
1
|
+
/*
|
2
|
+
$NiH: zip_open.c,v 1.42 2007/02/28 18:08:39 wiz Exp $
|
3
|
+
|
4
|
+
zip_open.c -- open zip archive
|
5
|
+
Copyright (C) 1999-2007 Dieter Baron and Thomas Klausner
|
6
|
+
|
7
|
+
This file is part of libzip, a library to manipulate ZIP archives.
|
8
|
+
The authors can be contacted at <nih@giga.or.at>
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
1. Redistributions of source code must retain the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer.
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
17
|
+
the documentation and/or other materials provided with the
|
18
|
+
distribution.
|
19
|
+
3. The names of the authors may not be used to endorse or promote
|
20
|
+
products derived from this software without specific prior
|
21
|
+
written permission.
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
24
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
27
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
29
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
31
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
33
|
+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#include <sys/stat.h>
|
39
|
+
#include <errno.h>
|
40
|
+
#include <stdio.h>
|
41
|
+
#include <stdlib.h>
|
42
|
+
#include <string.h>
|
43
|
+
|
44
|
+
#include "zip.h"
|
45
|
+
#include "zipint.h"
|
46
|
+
|
47
|
+
static void set_error(int *, struct zip_error *, int);
|
48
|
+
static struct zip *_zip_allocate_new(const char *, int *);
|
49
|
+
static int _zip_checkcons(FILE *, struct zip_cdir *, struct zip_error *);
|
50
|
+
static struct zip_cdir *_zip_find_central_dir(FILE *, int, int *, off_t);
|
51
|
+
static int _zip_file_exists(const char *, int, int *);
|
52
|
+
static int _zip_headercomp(struct zip_dirent *, int,
|
53
|
+
struct zip_dirent *, int);
|
54
|
+
static unsigned char *_zip_memmem(const unsigned char *, int,
|
55
|
+
const unsigned char *, int);
|
56
|
+
static struct zip_cdir *_zip_readcdir(FILE *, unsigned char *, unsigned char *,
|
57
|
+
int, int, struct zip_error *);
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
struct zip *
|
62
|
+
zip_open(const char *fn, int flags, int *zep)
|
63
|
+
{
|
64
|
+
FILE *fp;
|
65
|
+
struct zip *za;
|
66
|
+
struct zip_cdir *cdir;
|
67
|
+
int i;
|
68
|
+
off_t len;
|
69
|
+
|
70
|
+
switch (_zip_file_exists(fn, flags, zep)) {
|
71
|
+
case -1:
|
72
|
+
return NULL;
|
73
|
+
case 0:
|
74
|
+
return _zip_allocate_new(fn, zep);
|
75
|
+
default:
|
76
|
+
break;
|
77
|
+
}
|
78
|
+
|
79
|
+
if ((fp=fopen(fn, "rb")) == NULL) {
|
80
|
+
set_error(zep, NULL, ZIP_ER_OPEN);
|
81
|
+
return NULL;
|
82
|
+
}
|
83
|
+
|
84
|
+
fseeko(fp, 0, SEEK_END);
|
85
|
+
len = ftello(fp);
|
86
|
+
|
87
|
+
/* treat empty files as empty archives */
|
88
|
+
if (len == 0) {
|
89
|
+
if ((za=_zip_allocate_new(fn, zep)) == NULL)
|
90
|
+
fclose(fp);
|
91
|
+
else
|
92
|
+
za->zp = fp;
|
93
|
+
return za;
|
94
|
+
}
|
95
|
+
|
96
|
+
cdir = _zip_find_central_dir(fp, flags, zep, len);
|
97
|
+
if (cdir == NULL) {
|
98
|
+
fclose(fp);
|
99
|
+
return NULL;
|
100
|
+
}
|
101
|
+
|
102
|
+
if ((za=_zip_allocate_new(fn, zep)) == NULL) {
|
103
|
+
_zip_cdir_free(cdir);
|
104
|
+
fclose(fp);
|
105
|
+
return NULL;
|
106
|
+
}
|
107
|
+
|
108
|
+
za->cdir = cdir;
|
109
|
+
za->zp = fp;
|
110
|
+
|
111
|
+
if ((za->entry=(struct zip_entry *)malloc(sizeof(*(za->entry))
|
112
|
+
* cdir->nentry)) == NULL) {
|
113
|
+
set_error(zep, NULL, ZIP_ER_MEMORY);
|
114
|
+
_zip_free(za);
|
115
|
+
return NULL;
|
116
|
+
}
|
117
|
+
for (i=0; i<cdir->nentry; i++)
|
118
|
+
_zip_entry_new(za);
|
119
|
+
|
120
|
+
return za;
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
static void
|
126
|
+
set_error(int *zep, struct zip_error *err, int ze)
|
127
|
+
{
|
128
|
+
int se;
|
129
|
+
|
130
|
+
if (err) {
|
131
|
+
_zip_error_get(err, &ze, &se);
|
132
|
+
if (zip_error_get_sys_type(ze) == ZIP_ET_SYS)
|
133
|
+
errno = se;
|
134
|
+
}
|
135
|
+
|
136
|
+
if (zep)
|
137
|
+
*zep = ze;
|
138
|
+
}
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
/* _zip_readcdir:
|
143
|
+
tries to find a valid end-of-central-directory at the beginning of
|
144
|
+
buf, and then the corresponding central directory entries.
|
145
|
+
Returns a struct zip_cdir which contains the central directory
|
146
|
+
entries, or NULL if unsuccessful. */
|
147
|
+
|
148
|
+
static struct zip_cdir *
|
149
|
+
_zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen,
|
150
|
+
int flags, struct zip_error *error)
|
151
|
+
{
|
152
|
+
struct zip_cdir *cd;
|
153
|
+
unsigned char *cdp, **bufp;
|
154
|
+
int i, comlen, nentry;
|
155
|
+
|
156
|
+
comlen = buf + buflen - eocd - EOCDLEN;
|
157
|
+
if (comlen < 0) {
|
158
|
+
/* not enough bytes left for comment */
|
159
|
+
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
160
|
+
return NULL;
|
161
|
+
}
|
162
|
+
|
163
|
+
/* check for end-of-central-dir magic */
|
164
|
+
if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
|
165
|
+
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
166
|
+
return NULL;
|
167
|
+
}
|
168
|
+
|
169
|
+
if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
|
170
|
+
_zip_error_set(error, ZIP_ER_MULTIDISK, 0);
|
171
|
+
return NULL;
|
172
|
+
}
|
173
|
+
|
174
|
+
cdp = eocd + 8;
|
175
|
+
/* number of cdir-entries on this disk */
|
176
|
+
i = _zip_read2(&cdp);
|
177
|
+
/* number of cdir-entries */
|
178
|
+
nentry = _zip_read2(&cdp);
|
179
|
+
|
180
|
+
if ((cd=_zip_cdir_new(nentry, error)) == NULL)
|
181
|
+
return NULL;
|
182
|
+
|
183
|
+
cd->size = _zip_read4(&cdp);
|
184
|
+
cd->offset = _zip_read4(&cdp);
|
185
|
+
cd->comment = NULL;
|
186
|
+
cd->comment_len = _zip_read2(&cdp);
|
187
|
+
|
188
|
+
if ((comlen < cd->comment_len) || (cd->nentry != i)) {
|
189
|
+
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
190
|
+
free(cd);
|
191
|
+
return NULL;
|
192
|
+
}
|
193
|
+
if ((flags & ZIP_CHECKCONS) && comlen != cd->comment_len) {
|
194
|
+
_zip_error_set(error, ZIP_ER_INCONS, 0);
|
195
|
+
free(cd);
|
196
|
+
return NULL;
|
197
|
+
}
|
198
|
+
|
199
|
+
if (cd->comment_len)
|
200
|
+
if ((cd->comment=(char *)_zip_memdup(eocd+EOCDLEN,
|
201
|
+
cd->comment_len, error))
|
202
|
+
== NULL) {
|
203
|
+
free(cd);
|
204
|
+
return NULL;
|
205
|
+
}
|
206
|
+
|
207
|
+
cdp = eocd;
|
208
|
+
if (cd->size < (unsigned int)(eocd-buf)) {
|
209
|
+
/* if buffer already read in, use it */
|
210
|
+
cdp = eocd - cd->size;
|
211
|
+
bufp = &cdp;
|
212
|
+
}
|
213
|
+
else {
|
214
|
+
/* go to start of cdir and read it entry by entry */
|
215
|
+
bufp = NULL;
|
216
|
+
clearerr(fp);
|
217
|
+
fseeko(fp, cd->offset, SEEK_SET);
|
218
|
+
/* possible consistency check: cd->offset =
|
219
|
+
len-(cd->size+cd->comment_len+EOCDLEN) ? */
|
220
|
+
if (ferror(fp) || ((unsigned long)ftello(fp) != cd->offset)) {
|
221
|
+
/* seek error or offset of cdir wrong */
|
222
|
+
if (ferror(fp))
|
223
|
+
_zip_error_set(error, ZIP_ER_SEEK, errno);
|
224
|
+
else
|
225
|
+
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
226
|
+
free(cd);
|
227
|
+
return NULL;
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
for (i=0; i<cd->nentry; i++) {
|
232
|
+
if ((_zip_dirent_read(cd->entry+i, fp, bufp, eocd-cdp, 0,
|
233
|
+
error)) < 0) {
|
234
|
+
cd->nentry = i;
|
235
|
+
_zip_cdir_free(cd);
|
236
|
+
return NULL;
|
237
|
+
}
|
238
|
+
}
|
239
|
+
|
240
|
+
return cd;
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
/* _zip_checkcons:
|
246
|
+
Checks the consistency of the central directory by comparing central
|
247
|
+
directory entries with local headers and checking for plausible
|
248
|
+
file and header offsets. Returns -1 if not plausible, else the
|
249
|
+
difference between the lowest and the highest fileposition reached */
|
250
|
+
|
251
|
+
static int
|
252
|
+
_zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error)
|
253
|
+
{
|
254
|
+
int i;
|
255
|
+
unsigned int min, max, j;
|
256
|
+
struct zip_dirent temp;
|
257
|
+
|
258
|
+
if (cd->nentry) {
|
259
|
+
max = cd->entry[0].offset;
|
260
|
+
min = cd->entry[0].offset;
|
261
|
+
}
|
262
|
+
else
|
263
|
+
min = max = 0;
|
264
|
+
|
265
|
+
for (i=0; i<cd->nentry; i++) {
|
266
|
+
if (cd->entry[i].offset < min)
|
267
|
+
min = cd->entry[i].offset;
|
268
|
+
if (min > cd->offset) {
|
269
|
+
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
270
|
+
return -1;
|
271
|
+
}
|
272
|
+
|
273
|
+
j = cd->entry[i].offset + cd->entry[i].comp_size
|
274
|
+
+ cd->entry[i].filename_len + LENTRYSIZE;
|
275
|
+
if (j > max)
|
276
|
+
max = j;
|
277
|
+
if (max > cd->offset) {
|
278
|
+
_zip_error_set(error, ZIP_ER_NOZIP, 0);
|
279
|
+
return -1;
|
280
|
+
}
|
281
|
+
|
282
|
+
if (fseeko(fp, cd->entry[i].offset, SEEK_SET) != 0) {
|
283
|
+
_zip_error_set(error, ZIP_ER_SEEK, 0);
|
284
|
+
return -1;
|
285
|
+
}
|
286
|
+
|
287
|
+
if (_zip_dirent_read(&temp, fp, NULL, 0, 1, error) == -1)
|
288
|
+
return -1;
|
289
|
+
|
290
|
+
if (_zip_headercomp(cd->entry+i, 0, &temp, 1) != 0) {
|
291
|
+
_zip_error_set(error, ZIP_ER_INCONS, 0);
|
292
|
+
_zip_dirent_finalize(&temp);
|
293
|
+
return -1;
|
294
|
+
}
|
295
|
+
_zip_dirent_finalize(&temp);
|
296
|
+
}
|
297
|
+
|
298
|
+
return max - min;
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
/* _zip_headercomp:
|
304
|
+
compares two headers h1 and h2; if they are local headers, set
|
305
|
+
local1p or local2p respectively to 1, else 0. Return 0 if they
|
306
|
+
are identical, -1 if not. */
|
307
|
+
|
308
|
+
static int
|
309
|
+
_zip_headercomp(struct zip_dirent *h1, int local1p, struct zip_dirent *h2,
|
310
|
+
int local2p)
|
311
|
+
{
|
312
|
+
if ((h1->version_needed != h2->version_needed)
|
313
|
+
#if 0
|
314
|
+
/* some zip-files have different values in local
|
315
|
+
and global headers for the bitflags */
|
316
|
+
|| (h1->bitflags != h2->bitflags)
|
317
|
+
#endif
|
318
|
+
|| (h1->comp_method != h2->comp_method)
|
319
|
+
|| (h1->last_mod != h2->last_mod)
|
320
|
+
|| (h1->filename_len != h2->filename_len)
|
321
|
+
|| !h1->filename || !h2->filename
|
322
|
+
|| strcmp(h1->filename, h2->filename))
|
323
|
+
return -1;
|
324
|
+
|
325
|
+
/* check that CRC and sizes are zero if data descriptor is used */
|
326
|
+
if ((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local1p
|
327
|
+
&& (h1->crc != 0
|
328
|
+
|| h1->comp_size != 0
|
329
|
+
|| h1->uncomp_size != 0))
|
330
|
+
return -1;
|
331
|
+
if ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) && local2p
|
332
|
+
&& (h2->crc != 0
|
333
|
+
|| h2->comp_size != 0
|
334
|
+
|| h2->uncomp_size != 0))
|
335
|
+
return -1;
|
336
|
+
|
337
|
+
/* check that CRC and sizes are equal if no data descriptor is used */
|
338
|
+
if (((h1->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local1p == 0)
|
339
|
+
&& ((h2->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0 || local2p == 0)) {
|
340
|
+
if ((h1->crc != h2->crc)
|
341
|
+
|| (h1->comp_size != h2->comp_size)
|
342
|
+
|| (h1->uncomp_size != h2->uncomp_size))
|
343
|
+
return -1;
|
344
|
+
}
|
345
|
+
|
346
|
+
if ((local1p == local2p)
|
347
|
+
&& ((h1->extrafield_len != h2->extrafield_len)
|
348
|
+
|| (h1->extrafield_len && h2->extrafield
|
349
|
+
&& memcmp(h1->extrafield, h2->extrafield,
|
350
|
+
h1->extrafield_len))))
|
351
|
+
return -1;
|
352
|
+
|
353
|
+
/* if either is local, nothing more to check */
|
354
|
+
if (local1p || local2p)
|
355
|
+
return 0;
|
356
|
+
|
357
|
+
if ((h1->version_madeby != h2->version_madeby)
|
358
|
+
|| (h1->disk_number != h2->disk_number)
|
359
|
+
|| (h1->int_attrib != h2->int_attrib)
|
360
|
+
|| (h1->ext_attrib != h2->ext_attrib)
|
361
|
+
|| (h1->offset != h2->offset)
|
362
|
+
|| (h1->comment_len != h2->comment_len)
|
363
|
+
|| (h1->comment_len && h2->comment
|
364
|
+
&& memcmp(h1->comment, h2->comment, h1->comment_len)))
|
365
|
+
return -1;
|
366
|
+
|
367
|
+
return 0;
|
368
|
+
}
|
369
|
+
|
370
|
+
|
371
|
+
|
372
|
+
static struct zip *
|
373
|
+
_zip_allocate_new(const char *fn, int *zep)
|
374
|
+
{
|
375
|
+
struct zip *za;
|
376
|
+
struct zip_error error;
|
377
|
+
|
378
|
+
if ((za=_zip_new(&error)) == NULL) {
|
379
|
+
set_error(zep, &error, 0);
|
380
|
+
return NULL;
|
381
|
+
}
|
382
|
+
|
383
|
+
za->zn = strdup(fn);
|
384
|
+
if (!za->zn) {
|
385
|
+
_zip_free(za);
|
386
|
+
set_error(zep, NULL, ZIP_ER_MEMORY);
|
387
|
+
return NULL;
|
388
|
+
}
|
389
|
+
return za;
|
390
|
+
}
|
391
|
+
|
392
|
+
|
393
|
+
|
394
|
+
static int
|
395
|
+
_zip_file_exists(const char *fn, int flags, int *zep)
|
396
|
+
{
|
397
|
+
struct stat st;
|
398
|
+
|
399
|
+
if (fn == NULL) {
|
400
|
+
set_error(zep, NULL, ZIP_ER_INVAL);
|
401
|
+
return -1;
|
402
|
+
}
|
403
|
+
|
404
|
+
if (stat(fn, &st) != 0) {
|
405
|
+
if (flags & ZIP_CREATE)
|
406
|
+
return 0;
|
407
|
+
else {
|
408
|
+
set_error(zep, NULL, ZIP_ER_OPEN);
|
409
|
+
return -1;
|
410
|
+
}
|
411
|
+
}
|
412
|
+
else if ((flags & ZIP_EXCL)) {
|
413
|
+
set_error(zep, NULL, ZIP_ER_EXISTS);
|
414
|
+
return -1;
|
415
|
+
}
|
416
|
+
/* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL,
|
417
|
+
just like open() */
|
418
|
+
|
419
|
+
return 1;
|
420
|
+
}
|
421
|
+
|
422
|
+
|
423
|
+
|
424
|
+
static struct zip_cdir *
|
425
|
+
_zip_find_central_dir(FILE *fp, int flags, int *zep, off_t len)
|
426
|
+
{
|
427
|
+
struct zip_cdir *cdir, *cdirnew;
|
428
|
+
unsigned char *buf, *match;
|
429
|
+
int a, best, buflen, i;
|
430
|
+
struct zip_error zerr;
|
431
|
+
|
432
|
+
i = fseeko(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END);
|
433
|
+
if (i == -1 && errno != EFBIG) {
|
434
|
+
/* seek before start of file on my machine */
|
435
|
+
set_error(zep, NULL, ZIP_ER_SEEK);
|
436
|
+
return NULL;
|
437
|
+
}
|
438
|
+
|
439
|
+
/* 64k is too much for stack */
|
440
|
+
if ((buf=(unsigned char *)malloc(CDBUFSIZE)) == NULL) {
|
441
|
+
set_error(zep, NULL, ZIP_ER_MEMORY);
|
442
|
+
return NULL;
|
443
|
+
}
|
444
|
+
|
445
|
+
clearerr(fp);
|
446
|
+
buflen = fread(buf, 1, CDBUFSIZE, fp);
|
447
|
+
|
448
|
+
if (ferror(fp)) {
|
449
|
+
set_error(zep, NULL, ZIP_ER_READ);
|
450
|
+
free(buf);
|
451
|
+
return NULL;
|
452
|
+
}
|
453
|
+
|
454
|
+
best = -1;
|
455
|
+
cdir = NULL;
|
456
|
+
match = buf;
|
457
|
+
_zip_error_set(&zerr, ZIP_ER_NOZIP, 0);
|
458
|
+
|
459
|
+
while ((match=_zip_memmem(match, buflen-(match-buf)-18,
|
460
|
+
(const unsigned char *)EOCD_MAGIC, 4))!=NULL) {
|
461
|
+
/* found match -- check, if good */
|
462
|
+
/* to avoid finding the same match all over again */
|
463
|
+
match++;
|
464
|
+
if ((cdirnew=_zip_readcdir(fp, buf, match-1, buflen, flags,
|
465
|
+
&zerr)) == NULL)
|
466
|
+
continue;
|
467
|
+
|
468
|
+
if (cdir) {
|
469
|
+
if (best <= 0)
|
470
|
+
best = _zip_checkcons(fp, cdir, &zerr);
|
471
|
+
a = _zip_checkcons(fp, cdirnew, &zerr);
|
472
|
+
if (best < a) {
|
473
|
+
_zip_cdir_free(cdir);
|
474
|
+
cdir = cdirnew;
|
475
|
+
best = a;
|
476
|
+
}
|
477
|
+
else
|
478
|
+
_zip_cdir_free(cdirnew);
|
479
|
+
}
|
480
|
+
else {
|
481
|
+
cdir = cdirnew;
|
482
|
+
if (flags & ZIP_CHECKCONS)
|
483
|
+
best = _zip_checkcons(fp, cdir, &zerr);
|
484
|
+
else
|
485
|
+
best = 0;
|
486
|
+
}
|
487
|
+
cdirnew = NULL;
|
488
|
+
}
|
489
|
+
|
490
|
+
free(buf);
|
491
|
+
|
492
|
+
if (best < 0) {
|
493
|
+
set_error(zep, &zerr, 0);
|
494
|
+
_zip_cdir_free(cdir);
|
495
|
+
return NULL;
|
496
|
+
}
|
497
|
+
|
498
|
+
return cdir;
|
499
|
+
}
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
static unsigned char *
|
504
|
+
_zip_memmem(const unsigned char *big, int biglen, const unsigned char *little,
|
505
|
+
int littlelen)
|
506
|
+
{
|
507
|
+
const unsigned char *p;
|
508
|
+
|
509
|
+
if ((biglen < littlelen) || (littlelen == 0))
|
510
|
+
return NULL;
|
511
|
+
p = big-1;
|
512
|
+
while ((p=(const unsigned char *)
|
513
|
+
memchr(p+1, little[0], (size_t)(big-(p+1)+biglen-littlelen+1)))
|
514
|
+
!= NULL) {
|
515
|
+
if (memcmp(p+1, little+1, littlelen-1)==0)
|
516
|
+
return (unsigned char *)p;
|
517
|
+
}
|
518
|
+
|
519
|
+
return NULL;
|
520
|
+
}
|
data/ext/zip_rename.c
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
/*
|
2
|
+
$NiH: zip_rename.c,v 1.14 2004/11/18 17:11:22 wiz Exp $
|
3
|
+
|
4
|
+
zip_rename.c -- rename file in zip archive
|
5
|
+
Copyright (C) 1999, 2003, 2004 Dieter Baron and Thomas Klausner
|
6
|
+
|
7
|
+
This file is part of libzip, a library to manipulate ZIP archives.
|
8
|
+
The authors can be contacted at <nih@giga.or.at>
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
1. Redistributions of source code must retain the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer.
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
17
|
+
the documentation and/or other materials provided with the
|
18
|
+
distribution.
|
19
|
+
3. The names of the authors may not be used to endorse or promote
|
20
|
+
products derived from this software without specific prior
|
21
|
+
written permission.
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
24
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
27
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
29
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
31
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
33
|
+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#include "zip.h"
|
39
|
+
#include "zipint.h"
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
int
|
44
|
+
zip_rename(struct zip *za, int idx, const char *name)
|
45
|
+
{
|
46
|
+
if (idx >= za->nentry || idx < 0) {
|
47
|
+
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
48
|
+
return -1;
|
49
|
+
}
|
50
|
+
|
51
|
+
return _zip_set_name(za, idx, name);
|
52
|
+
}
|
data/ext/zip_replace.c
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
/*
|
2
|
+
$NiH: zip_replace.c,v 1.19 2004/11/30 22:19:38 wiz Exp $
|
3
|
+
|
4
|
+
zip_replace.c -- replace file via callback function
|
5
|
+
Copyright (C) 1999, 2003, 2004, 2006 Dieter Baron and Thomas Klausner
|
6
|
+
|
7
|
+
This file is part of libzip, a library to manipulate ZIP archives.
|
8
|
+
The authors can be contacted at <nih@giga.or.at>
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
1. Redistributions of source code must retain the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer.
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
17
|
+
the documentation and/or other materials provided with the
|
18
|
+
distribution.
|
19
|
+
3. The names of the authors may not be used to endorse or promote
|
20
|
+
products derived from this software without specific prior
|
21
|
+
written permission.
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
24
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
27
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
29
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
31
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
33
|
+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#include "zip.h"
|
39
|
+
#include "zipint.h"
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
int
|
44
|
+
zip_replace(struct zip *za, int idx, struct zip_source *source)
|
45
|
+
{
|
46
|
+
if (idx < 0 || idx >= za->nentry || source == NULL) {
|
47
|
+
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
48
|
+
return -1;
|
49
|
+
}
|
50
|
+
|
51
|
+
if (_zip_replace(za, idx, NULL, source) == -1)
|
52
|
+
return -1;
|
53
|
+
|
54
|
+
return 0;
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
int
|
61
|
+
_zip_replace(struct zip *za, int idx, const char *name,
|
62
|
+
struct zip_source *source)
|
63
|
+
{
|
64
|
+
if (idx == -1) {
|
65
|
+
if (_zip_entry_new(za) == NULL)
|
66
|
+
return -1;
|
67
|
+
|
68
|
+
idx = za->nentry - 1;
|
69
|
+
}
|
70
|
+
|
71
|
+
_zip_unchange_data(za->entry+idx);
|
72
|
+
|
73
|
+
if (name && _zip_set_name(za, idx, name) != 0)
|
74
|
+
return -1;
|
75
|
+
|
76
|
+
za->entry[idx].state = ((za->cdir == NULL || idx >= za->cdir->nentry)
|
77
|
+
? ZIP_ST_ADDED : ZIP_ST_REPLACED);
|
78
|
+
za->entry[idx].source = source;
|
79
|
+
|
80
|
+
return idx;
|
81
|
+
}
|
@@ -0,0 +1,68 @@
|
|
1
|
+
/*
|
2
|
+
$NiH: zip_set_archive_comment.c,v 1.2 2006/04/23 15:26:30 dillo Exp $
|
3
|
+
|
4
|
+
zip_set_archive_comment.c -- set archive comment
|
5
|
+
Copyright (C) 2006 Dieter Baron and Thomas Klausner
|
6
|
+
|
7
|
+
This file is part of libzip, a library to manipulate ZIP archives.
|
8
|
+
The authors can be contacted at <nih@giga.or.at>
|
9
|
+
|
10
|
+
Redistribution and use in source and binary forms, with or without
|
11
|
+
modification, are permitted provided that the following conditions
|
12
|
+
are met:
|
13
|
+
1. Redistributions of source code must retain the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer.
|
15
|
+
2. Redistributions in binary form must reproduce the above copyright
|
16
|
+
notice, this list of conditions and the following disclaimer in
|
17
|
+
the documentation and/or other materials provided with the
|
18
|
+
distribution.
|
19
|
+
3. The names of the authors may not be used to endorse or promote
|
20
|
+
products derived from this software without specific prior
|
21
|
+
written permission.
|
22
|
+
|
23
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
|
24
|
+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
26
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
|
27
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
28
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
29
|
+
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
30
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
|
31
|
+
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
32
|
+
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
33
|
+
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
#include <stdlib.h>
|
39
|
+
|
40
|
+
#include "zip.h"
|
41
|
+
#include "zipint.h"
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
int
|
46
|
+
zip_set_archive_comment(struct zip *za, const char *comment, int len)
|
47
|
+
{
|
48
|
+
char *tmpcom;
|
49
|
+
|
50
|
+
if (len < 0 || len > MAXCOMLEN
|
51
|
+
|| (len > 0 && comment == NULL)) {
|
52
|
+
_zip_error_set(&za->error, ZIP_ER_INVAL, 0);
|
53
|
+
return -1;
|
54
|
+
}
|
55
|
+
|
56
|
+
if (len > 0) {
|
57
|
+
if ((tmpcom=(char *)_zip_memdup(comment, len, &za->error)) == NULL)
|
58
|
+
return -1;
|
59
|
+
}
|
60
|
+
else
|
61
|
+
tmpcom = NULL;
|
62
|
+
|
63
|
+
free(za->ch_comment);
|
64
|
+
za->ch_comment = tmpcom;
|
65
|
+
za->ch_comment_len = len;
|
66
|
+
|
67
|
+
return 0;
|
68
|
+
}
|