Fossil SCM

fossil-scm / compat / zlib / contrib / minizip / minizip.c
Blame History Raw 512 lines
1
/*
2
minizip.c
3
sample part of the MiniZip project - ( https://www.winimage.com/zLibDll/minizip.html )
4
5
Copyright (C) 1998-2026 Gilles Vollant (minizip) ( https://www.winimage.com/zLibDll/minizip.html )
6
7
Modifications of Unzip for Zip64
8
Copyright (C) 2007-2008 Even Rouault
9
10
Modifications for Zip64 support on both zip and unzip
11
Copyright (C) 2009-2010 Mathias Svensson ( https://result42.com )
12
*/
13
14
15
#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
16
#ifndef __USE_FILE_OFFSET64
17
#define __USE_FILE_OFFSET64
18
#endif
19
#ifndef __USE_LARGEFILE64
20
#define __USE_LARGEFILE64
21
#endif
22
#ifndef _LARGEFILE64_SOURCE
23
#define _LARGEFILE64_SOURCE
24
#endif
25
#ifndef _FILE_OFFSET_BIT
26
#define _FILE_OFFSET_BIT 64
27
#endif
28
#endif
29
30
#if defined(__APPLE__) || defined(__HAIKU__) || defined(MINIZIP_FOPEN_NO_64)
31
// In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions
32
#define FOPEN_FUNC(filename, mode) fopen(filename, mode)
33
#define FTELLO_FUNC(stream) ftello(stream)
34
#define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
35
#else
36
#define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
37
#define FTELLO_FUNC(stream) ftello64(stream)
38
#define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
39
#endif
40
41
42
43
#ifndef _CRT_SECURE_NO_WARNINGS
44
# define _CRT_SECURE_NO_WARNINGS
45
#endif
46
#include <stdio.h>
47
#include <stdlib.h>
48
#include <string.h>
49
#include <time.h>
50
#include <errno.h>
51
#include <fcntl.h>
52
53
#ifdef _WIN32
54
# include <direct.h>
55
# include <io.h>
56
#else
57
# include <unistd.h>
58
# include <utime.h>
59
# include <sys/types.h>
60
# include <sys/stat.h>
61
#endif
62
63
#include "zip.h"
64
#include "ints.h"
65
66
#ifdef _WIN32
67
#define USEWIN32IOAPI
68
#include "iowin32.h"
69
#endif
70
71
72
73
#define WRITEBUFFERSIZE (16384)
74
#define MAXFILENAME (256)
75
76
#ifdef _WIN32
77
/* f: name of file to get info on, tmzip: return value: access,
78
modification and creation times, dt: dostime */
79
static int filetime(const char *f, tm_zip *tmzip, uLong *dt) {
80
(void)tmzip;
81
int ret = 0;
82
{
83
FILETIME ftLocal;
84
HANDLE hFind;
85
WIN32_FIND_DATAA ff32;
86
87
hFind = FindFirstFileA(f,&ff32);
88
if (hFind != INVALID_HANDLE_VALUE)
89
{
90
FileTimeToLocalFileTime(&(ff32.ftLastWriteTime),&ftLocal);
91
FileTimeToDosDateTime(&ftLocal,((LPWORD)dt)+1,((LPWORD)dt)+0);
92
FindClose(hFind);
93
ret = 1;
94
}
95
}
96
return ret;
97
}
98
#elif defined(__unix__) || defined(__unix) || defined(__APPLE__)
99
/* f: name of file to get info on, tmzip: return value: access,
100
modification and creation times, dt: dostime */
101
static int filetime(const char *f, tm_zip *tmzip, uLong *dt) {
102
(void)dt;
103
int ret=0;
104
struct stat s; /* results of stat() */
105
struct tm* filedate;
106
time_t tm_t=0;
107
108
if (strcmp(f,"-")!=0)
109
{
110
char name[MAXFILENAME+1];
111
size_t len = strlen(f);
112
if (len > MAXFILENAME)
113
len = MAXFILENAME;
114
115
strncpy(name, f,MAXFILENAME-1);
116
/* strncpy doesn't append the trailing NULL, of the string is too long. */
117
name[ MAXFILENAME ] = '\0';
118
119
if (name[len - 1] == '/')
120
name[len - 1] = '\0';
121
/* not all systems allow stat'ing a file with / appended */
122
if (stat(name,&s)==0)
123
{
124
tm_t = s.st_mtime;
125
ret = 1;
126
}
127
}
128
filedate = localtime(&tm_t);
129
130
tmzip->tm_sec = filedate->tm_sec;
131
tmzip->tm_min = filedate->tm_min;
132
tmzip->tm_hour = filedate->tm_hour;
133
tmzip->tm_mday = filedate->tm_mday;
134
tmzip->tm_mon = filedate->tm_mon ;
135
tmzip->tm_year = filedate->tm_year;
136
137
return ret;
138
}
139
#else
140
/* f: name of file to get info on, tmzip: return value: access,
141
modification and creation times, dt: dostime */
142
static int filetime(const char *f, tm_zip *tmzip, uLong *dt) {
143
(void)f;
144
(void)tmzip;
145
(void)dt;
146
return 0;
147
}
148
#endif
149
150
151
152
153
static int check_exist_file(const char* filename) {
154
FILE* ftestexist;
155
int ret = 1;
156
ftestexist = FOPEN_FUNC(filename,"rb");
157
if (ftestexist==NULL)
158
ret = 0;
159
else
160
fclose(ftestexist);
161
return ret;
162
}
163
164
static void do_banner(void) {
165
printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
166
printf("more info on MiniZip at https://www.winimage.com/zLibDll/minizip.html\n\n");
167
}
168
169
static void do_help(void) {
170
printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
171
" -o Overwrite existing file.zip\n" \
172
" -a Append to existing file.zip\n" \
173
" -0 Store only\n" \
174
" -1 Compress faster\n" \
175
" -9 Compress better\n\n" \
176
" -j exclude path. store only the file name.\n\n");
177
}
178
179
/* calculate the CRC32 of a file,
180
because to encrypt a file, we need known the CRC32 of the file before */
181
static int getFileCrc(const char* filenameinzip, void* buf, unsigned long size_buf, unsigned long* result_crc) {
182
unsigned long calculate_crc=0;
183
int err=ZIP_OK;
184
FILE * fin = FOPEN_FUNC(filenameinzip,"rb");
185
186
unsigned long size_read = 0;
187
/* unsigned long total_read = 0; */
188
if (fin==NULL)
189
{
190
err = ZIP_ERRNO;
191
}
192
193
if (err == ZIP_OK)
194
do
195
{
196
err = ZIP_OK;
197
size_read = (unsigned long)fread(buf,1,size_buf,fin);
198
if (size_read < size_buf)
199
if (feof(fin)==0)
200
{
201
printf("error in reading %s\n",filenameinzip);
202
err = ZIP_ERRNO;
203
}
204
205
if (size_read>0)
206
calculate_crc = crc32_z(calculate_crc,buf,size_read);
207
/* total_read += size_read; */
208
209
} while ((err == ZIP_OK) && (size_read>0));
210
211
if (fin)
212
fclose(fin);
213
214
*result_crc=calculate_crc;
215
printf("file %s crc %lx\n", filenameinzip, calculate_crc);
216
return err;
217
}
218
219
static int isLargeFile(const char* filename) {
220
int largeFile = 0;
221
ZPOS64_T pos = 0;
222
FILE* pFile = FOPEN_FUNC(filename, "rb");
223
224
if(pFile != NULL)
225
{
226
FSEEKO_FUNC(pFile, 0, SEEK_END);
227
pos = (ZPOS64_T)FTELLO_FUNC(pFile);
228
229
printf("File : %s is %"PUI64" bytes\n", filename, pos);
230
231
if(pos >= 0xffffffff)
232
largeFile = 1;
233
234
fclose(pFile);
235
}
236
237
return largeFile;
238
}
239
240
int main(int argc, char *argv[]) {
241
int i;
242
int opt_overwrite=0;
243
int opt_compress_level=Z_DEFAULT_COMPRESSION;
244
int opt_exclude_path=0;
245
int zipfilenamearg = 0;
246
char filename_try[MAXFILENAME+16];
247
int zipok;
248
int err=0;
249
unsigned long size_buf=0;
250
void* buf=NULL;
251
const char* password=NULL;
252
253
254
do_banner();
255
if (argc==1)
256
{
257
do_help();
258
return 0;
259
}
260
else
261
{
262
for (i=1;i<argc;i++)
263
{
264
if ((*argv[i])=='-')
265
{
266
const char *p=argv[i]+1;
267
268
while ((*p)!='\0')
269
{
270
char c=*(p++);
271
if ((c=='o') || (c=='O'))
272
opt_overwrite = 1;
273
if ((c=='a') || (c=='A'))
274
opt_overwrite = 2;
275
if ((c>='0') && (c<='9'))
276
opt_compress_level = c-'0';
277
if ((c=='j') || (c=='J'))
278
opt_exclude_path = 1;
279
280
if (((c=='p') || (c=='P')) && (i+1<argc))
281
{
282
password=argv[i+1];
283
i++;
284
}
285
}
286
}
287
else
288
{
289
if (zipfilenamearg == 0)
290
{
291
zipfilenamearg = i ;
292
}
293
}
294
}
295
}
296
297
size_buf = WRITEBUFFERSIZE;
298
buf = (void*)malloc(size_buf);
299
if (buf==NULL)
300
{
301
printf("Error allocating memory\n");
302
return ZIP_INTERNALERROR;
303
}
304
305
if (zipfilenamearg==0)
306
{
307
zipok=0;
308
}
309
else
310
{
311
int len;
312
int dot_found=0;
313
314
zipok = 1 ;
315
strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
316
/* strncpy doesn't append the trailing NULL, of the string is too long. */
317
filename_try[ MAXFILENAME ] = '\0';
318
319
len=(int)strlen(filename_try);
320
for (i=0;i<len;i++)
321
if (filename_try[i]=='.')
322
dot_found=1;
323
324
if (dot_found==0)
325
strcat(filename_try,".zip");
326
327
if (opt_overwrite==2)
328
{
329
/* if the file don't exist, we not append file */
330
if (check_exist_file(filename_try)==0)
331
opt_overwrite=1;
332
}
333
else
334
if (opt_overwrite==0)
335
if (check_exist_file(filename_try)!=0)
336
{
337
char rep=0;
338
do
339
{
340
char answer[128];
341
int ret;
342
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
343
ret = scanf("%1s",answer);
344
if (ret != 1)
345
{
346
exit(EXIT_FAILURE);
347
}
348
rep = answer[0] ;
349
if ((rep>='a') && (rep<='z'))
350
rep -= 0x20;
351
}
352
while ((rep!='Y') && (rep!='N') && (rep!='A'));
353
if (rep=='N')
354
zipok = 0;
355
if (rep=='A')
356
opt_overwrite = 2;
357
}
358
}
359
360
if (zipok==1)
361
{
362
zipFile zf;
363
int errclose;
364
# ifdef USEWIN32IOAPI
365
zlib_filefunc64_def ffunc;
366
fill_win32_filefunc64A(&ffunc);
367
zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
368
# else
369
zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
370
# endif
371
372
if (zf == NULL)
373
{
374
printf("error opening %s\n",filename_try);
375
err= ZIP_ERRNO;
376
}
377
else
378
printf("creating %s\n",filename_try);
379
380
for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
381
{
382
if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
383
((argv[i][1]=='o') || (argv[i][1]=='O') ||
384
(argv[i][1]=='a') || (argv[i][1]=='A') ||
385
(argv[i][1]=='p') || (argv[i][1]=='P') ||
386
((argv[i][1]>='0') && (argv[i][1]<='9'))) &&
387
(strlen(argv[i]) == 2)))
388
{
389
FILE * fin = NULL;
390
size_t size_read;
391
const char* filenameinzip = argv[i];
392
const char *savefilenameinzip;
393
zip_fileinfo zi;
394
unsigned long crcFile=0;
395
int zip64 = 0;
396
397
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
398
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
399
zi.dosDate = 0;
400
zi.internal_fa = 0;
401
zi.external_fa = 0;
402
filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
403
404
/*
405
err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
406
NULL,0,NULL,0,NULL / * comment * /,
407
(opt_compress_level != 0) ? Z_DEFLATED : 0,
408
opt_compress_level);
409
*/
410
if ((password != NULL) && (err==ZIP_OK))
411
err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
412
413
zip64 = isLargeFile(filenameinzip);
414
415
/* The path name saved, should not include a leading slash. */
416
/*if it did, windows/xp and dynazip couldn't read the zip file. */
417
savefilenameinzip = filenameinzip;
418
while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
419
{
420
savefilenameinzip++;
421
}
422
423
/*should the zip file contain any path at all?*/
424
if( opt_exclude_path )
425
{
426
const char *tmpptr;
427
const char *lastslash = 0;
428
for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
429
{
430
if( *tmpptr == '\\' || *tmpptr == '/')
431
{
432
lastslash = tmpptr;
433
}
434
}
435
if( lastslash != NULL )
436
{
437
savefilenameinzip = lastslash+1; /* base filename follows last slash. */
438
}
439
}
440
441
/**/
442
err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
443
NULL,0,NULL,0,NULL /* comment*/,
444
(opt_compress_level != 0) ? Z_DEFLATED : 0,
445
opt_compress_level,0,
446
/* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
447
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
448
password,crcFile, zip64);
449
450
if (err != ZIP_OK)
451
printf("error in opening %s in zipfile\n",filenameinzip);
452
else
453
{
454
fin = FOPEN_FUNC(filenameinzip,"rb");
455
if (fin==NULL)
456
{
457
err=ZIP_ERRNO;
458
printf("error in opening %s for reading\n",filenameinzip);
459
}
460
}
461
462
if (err == ZIP_OK)
463
do
464
{
465
err = ZIP_OK;
466
size_read = fread(buf,1,size_buf,fin);
467
if (size_read < size_buf)
468
if (feof(fin)==0)
469
{
470
printf("error in reading %s\n",filenameinzip);
471
err = ZIP_ERRNO;
472
}
473
474
if (size_read>0)
475
{
476
err = zipWriteInFileInZip (zf,buf,(unsigned)size_read);
477
if (err<0)
478
{
479
printf("error in writing %s in the zipfile\n",
480
filenameinzip);
481
}
482
483
}
484
} while ((err == ZIP_OK) && (size_read>0));
485
486
if (fin)
487
fclose(fin);
488
489
if (err<0)
490
err=ZIP_ERRNO;
491
else
492
{
493
err = zipCloseFileInZip(zf);
494
if (err!=ZIP_OK)
495
printf("error in closing %s in the zipfile\n",
496
filenameinzip);
497
}
498
}
499
}
500
errclose = zipClose(zf,NULL);
501
if (errclose != ZIP_OK)
502
printf("error in closing %s\n",filename_try);
503
}
504
else
505
{
506
do_help();
507
}
508
509
free(buf);
510
return err;
511
}
512

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button