Debian Codegraph @

compat/zlib/contrib/minizip/minizip.c

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