]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/ThirdParty/Minizip/minizip/miniunz.c
Add FMILibrary-2.0.3 to org.simantics.fmil.core\native.
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / ThirdParty / Minizip / minizip / miniunz.c
1 /*
2    miniunz.c
3    Version 1.1, February 14h, 2010
4    sample part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
5
6          Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
7
8          Modifications of Unzip for Zip64
9          Copyright (C) 2007-2008 Even Rouault
10
11          Modifications for Zip64 support on both zip and unzip
12          Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
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 #ifdef __APPLE__
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 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdarg.h>
49
50 #ifdef _WIN32
51 # include <direct.h>
52 # include <io.h>
53 #else
54 # include <unistd.h>
55 # include <utime.h>
56 #endif
57
58
59 #include "unzip.h"
60
61 #define CASESENSITIVITY (0)
62 #define WRITEBUFFERSIZE (8192)
63 #define MAXFILENAME (256)
64
65 #ifdef _WIN32
66 #define USEWIN32IOAPI
67 #include "iowin32.h"
68 #endif
69
70
71 /* MODIFICATION Replace all stdout prints with this function for better control */
72 static int minizip_printf( const char * format, ... )
73 {
74         return 1;
75
76
77
78 /*
79   mini unzip, demo of unzip package
80
81   usage :
82   Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
83
84   list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
85     if it exists
86 */
87
88
89 /* change_file_date : change the date/time of a file
90     filename : the filename of the file where date/time must be modified
91     dosdate : the new date at the MSDos format (4 bytes)
92     tmu_date : the SAME new date at the tm_unz format */
93 void change_file_date(filename,dosdate,tmu_date)
94     const char *filename;
95     uLong dosdate;
96     tm_unz tmu_date;
97 {
98 #ifdef _WIN32
99   HANDLE hFile;
100   FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
101
102   hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
103                       0,NULL,OPEN_EXISTING,0,NULL);
104   GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
105   DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
106   LocalFileTimeToFileTime(&ftLocal,&ftm);
107   SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
108   CloseHandle(hFile);
109 #else
110 #ifdef unix || __APPLE__
111   struct utimbuf ut;
112   struct tm newdate;
113   newdate.tm_sec = tmu_date.tm_sec;
114   newdate.tm_min=tmu_date.tm_min;
115   newdate.tm_hour=tmu_date.tm_hour;
116   newdate.tm_mday=tmu_date.tm_mday;
117   newdate.tm_mon=tmu_date.tm_mon;
118   if (tmu_date.tm_year > 1900)
119       newdate.tm_year=tmu_date.tm_year - 1900;
120   else
121       newdate.tm_year=tmu_date.tm_year ;
122   newdate.tm_isdst=-1;
123
124   ut.actime=ut.modtime=mktime(&newdate);
125   utime(filename,&ut);
126 #endif
127 #endif
128 }
129
130
131 /* mymkdir and change_file_date are not 100 % portable
132    As I don't know well Unix, I wait feedback for the unix portion */
133
134 int mymkdir(dirname)
135     const char* dirname;
136 {
137     int ret=0;
138 #ifdef _WIN32
139     ret = _mkdir(dirname);
140 #elif unix
141     ret = mkdir (dirname,0775);
142 #elif __APPLE__
143     ret = mkdir (dirname,0775);
144 #else\r
145 #error Unknown platform
146 #endif
147     return ret;
148 }
149
150 int makedir (newdir)
151     char *newdir;
152 {
153   char *buffer ;
154   char *p;
155   int  len = (int)strlen(newdir);
156
157   if (len <= 0)
158     return 0;
159
160   buffer = (char*)malloc(len+1);
161         if (buffer==NULL)
162         {
163                 minizip_printf("Error allocating memory\n");
164                 return UNZ_INTERNALERROR;
165         }
166   strcpy(buffer,newdir);
167
168   if (buffer[len-1] == '/') {
169     buffer[len-1] = '\0';
170   }
171   if (mymkdir(buffer) == 0)
172     {
173       free(buffer);
174       return 1;
175     }
176
177   p = buffer+1;
178   while (1)
179     {
180       char hold;
181
182       while(*p && *p != '\\' && *p != '/')
183         p++;
184       hold = *p;
185       *p = 0;
186       if ((mymkdir(buffer) == -1) && (errno == ENOENT))
187         {
188           minizip_printf("couldn't create directory %s\n",buffer);
189           free(buffer);
190           return 0;
191         }
192       if (hold == 0)
193         break;
194       *p++ = hold;
195     }
196   free(buffer);
197   return 1;
198 }
199
200 static void do_banner()
201 {
202         /*
203     minizip_printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
204     minizip_printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
205         */
206 }
207
208 static void do_help()
209 {
210     minizip_printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
211            "  -e  Extract without pathname (junk paths)\n" \
212            "  -x  Extract with pathname\n" \
213            "  -v  list files\n" \
214            "  -l  list files\n" \
215            "  -d  directory to extract into\n" \
216            "  -o  overwrite files without prompting\n" \
217            "  -p  extract crypted file using password\n\n");
218 }
219
220 void Display64BitsSize(ZPOS64_T n, int size_char)
221 {
222   /* to avoid compatibility problem , we do here the conversion */
223   char number[21];
224   int offset=19;
225   int pos_string = 19;
226   number[20]=0;
227   for (;;) {
228       number[offset]=(char)((n%10)+'0');
229       if (number[offset] != '0')
230           pos_string=offset;
231       n/=10;
232       if (offset==0)
233           break;
234       offset--;
235   }
236   {
237       int size_display_string = 19-pos_string;
238       while (size_char > size_display_string)
239       {
240           size_char--;
241           minizip_printf(" ");
242       }
243   }
244
245   minizip_printf("%s",&number[pos_string]);
246 }
247
248 int do_list(uf)
249     unzFile uf;
250 {
251     uLong i;
252     unz_global_info64 gi;
253     int err;
254
255     err = unzGetGlobalInfo64(uf,&gi);
256     if (err!=UNZ_OK)
257         minizip_printf("error %d with zipfile in unzGetGlobalInfo \n",err);
258     minizip_printf("  Length  Method     Size Ratio   Date    Time   CRC-32     Name\n");
259     minizip_printf("  ------  ------     ---- -----   ----    ----   ------     ----\n");
260     for (i=0;i<gi.number_entry;i++)
261     {
262         char filename_inzip[256];
263         unz_file_info64 file_info;
264         uLong ratio=0;
265         const char *string_method;
266         char charCrypt=' ';
267         err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
268         if (err!=UNZ_OK)
269         {
270             minizip_printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
271             break;
272         }
273         if (file_info.uncompressed_size>0)
274             ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
275
276         /* display a '*' if the file is crypted */
277         if ((file_info.flag & 1) != 0)
278             charCrypt='*';
279
280         if (file_info.compression_method==0)
281             string_method="Stored";
282         else
283         if (file_info.compression_method==Z_DEFLATED)
284         {
285             uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
286             if (iLevel==0)
287               string_method="Defl:N";
288             else if (iLevel==1)
289               string_method="Defl:X";
290             else if ((iLevel==2) || (iLevel==3))
291               string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
292         }
293         else
294         if (file_info.compression_method==Z_BZIP2ED)
295         {
296               string_method="BZip2 ";
297         }
298         else
299             string_method="Unkn. ";
300
301         Display64BitsSize(file_info.uncompressed_size,7);
302         minizip_printf("  %6s%c",string_method,charCrypt);
303         Display64BitsSize(file_info.compressed_size,7);
304         minizip_printf(" %3lu%%  %2.2lu-%2.2lu-%2.2lu  %2.2lu:%2.2lu  %8.8lx   %s\n",
305                 ratio,
306                 (uLong)file_info.tmu_date.tm_mon + 1,
307                 (uLong)file_info.tmu_date.tm_mday,
308                 (uLong)file_info.tmu_date.tm_year % 100,
309                 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
310                 (uLong)file_info.crc,filename_inzip);
311         if ((i+1)<gi.number_entry)
312         {
313             err = unzGoToNextFile(uf);
314             if (err!=UNZ_OK)
315             {
316                 minizip_printf("error %d with zipfile in unzGoToNextFile\n",err);
317                 break;
318             }
319         }
320     }
321
322     return 0;
323 }
324
325
326 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
327     unzFile uf;
328     const int* popt_extract_without_path;
329     int* popt_overwrite;
330     const char* password;
331 {
332     char filename_inzip[256];
333     char* filename_withoutpath;
334     char* p;
335     int err=UNZ_OK;
336     FILE *fout=NULL;
337     void* buf;
338     uInt size_buf;
339
340     unz_file_info64 file_info;
341     uLong ratio=0;
342     err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
343
344     if (err!=UNZ_OK)
345     {
346         minizip_printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
347         return err;
348     }
349
350     size_buf = WRITEBUFFERSIZE;
351     buf = (void*)malloc(size_buf);
352     if (buf==NULL)
353     {
354         minizip_printf("Error allocating memory\n");
355         return UNZ_INTERNALERROR;
356     }
357
358     p = filename_withoutpath = filename_inzip;
359     while ((*p) != '\0')
360     {
361         if (((*p)=='/') || ((*p)=='\\'))
362             filename_withoutpath = p+1;
363         p++;
364     }
365
366     if ((*filename_withoutpath)=='\0')
367     {
368         if ((*popt_extract_without_path)==0)
369         {
370             minizip_printf("creating directory: %s\n",filename_inzip);
371             mymkdir(filename_inzip);
372         }
373     }
374     else
375     {
376         const char* write_filename;
377         int skip=0;
378
379         if ((*popt_extract_without_path)==0)
380             write_filename = filename_inzip;
381         else
382             write_filename = filename_withoutpath;
383
384         err = unzOpenCurrentFilePassword(uf,password);
385         if (err!=UNZ_OK)
386         {
387             minizip_printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
388         }
389
390         if (((*popt_overwrite)==0) && (err==UNZ_OK))
391         {
392             char rep=0;
393             FILE* ftestexist;
394             ftestexist = FOPEN_FUNC(write_filename,"rb");
395             if (ftestexist!=NULL)
396             {
397                 fclose(ftestexist);
398                 do
399                 {
400                     char answer[128];
401                     int ret;
402
403                     minizip_printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
404                     ret = scanf("%1s",answer);
405                     if (ret != 1)
406                     {
407                        exit(EXIT_FAILURE);
408                     }
409                     rep = answer[0] ;
410                     if ((rep>='a') && (rep<='z'))
411                         rep -= 0x20;
412                 }
413                 while ((rep!='Y') && (rep!='N') && (rep!='A'));
414             }
415
416             if (rep == 'N')
417                 skip = 1;
418
419             if (rep == 'A')
420                 *popt_overwrite=1;
421         }
422
423         if ((skip==0) && (err==UNZ_OK))
424         {
425             fout=FOPEN_FUNC(write_filename,"wb");
426             /* some zipfile don't contain directory alone before file */
427             if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
428                                 (filename_withoutpath!=(char*)filename_inzip))
429             {
430                 char c=*(filename_withoutpath-1);
431                 *(filename_withoutpath-1)='\0';
432                 makedir(write_filename);
433                 *(filename_withoutpath-1)=c;
434                 fout=FOPEN_FUNC(write_filename,"wb");
435             }
436
437             if (fout==NULL)
438             {
439                 minizip_printf("error opening %s\n",write_filename);
440             }
441         }
442
443         if (fout!=NULL)
444         {
445             minizip_printf(" extracting: %s\n",write_filename);
446
447             do
448             {
449                 err = unzReadCurrentFile(uf,buf,size_buf);
450                 if (err<0)
451                 {
452                     minizip_printf("error %d with zipfile in unzReadCurrentFile\n",err);
453                     break;
454                 }
455                 if (err>0)
456                     if (fwrite(buf,err,1,fout)!=1)
457                     {
458                         minizip_printf("error in writing extracted file\n");
459                         err=UNZ_ERRNO;
460                         break;
461                     }
462             }
463             while (err>0);
464             if (fout)
465                     fclose(fout);
466
467             if (err==0)
468                 change_file_date(write_filename,file_info.dosDate,
469                                  file_info.tmu_date);
470         }
471
472         if (err==UNZ_OK)
473         {
474             err = unzCloseCurrentFile (uf);
475             if (err!=UNZ_OK)
476             {
477                 minizip_printf("error %d with zipfile in unzCloseCurrentFile\n",err);
478             }
479         }
480         else
481             unzCloseCurrentFile(uf); /* don't lose the error */
482     }
483
484     free(buf);
485     return err;
486 }
487
488
489 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
490     unzFile uf;
491     int opt_extract_without_path;
492     int opt_overwrite;
493     const char* password;
494 {
495     uLong i;
496     unz_global_info64 gi;
497     int err;
498     FILE* fout=NULL;
499
500     err = unzGetGlobalInfo64(uf,&gi);
501     if (err!=UNZ_OK)
502         minizip_printf("error %d with zipfile in unzGetGlobalInfo \n",err);
503
504     for (i=0;i<gi.number_entry;i++)
505     {
506         if (do_extract_currentfile(uf,&opt_extract_without_path,
507                                       &opt_overwrite,
508                                       password) != UNZ_OK)
509             break;
510
511         if ((i+1)<gi.number_entry)
512         {
513             err = unzGoToNextFile(uf);
514             if (err!=UNZ_OK)
515             {
516                 minizip_printf("error %d with zipfile in unzGoToNextFile\n",err);
517                 break;
518             }
519         }
520     }
521
522     return 0;
523 }
524
525 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
526     unzFile uf;
527     const char* filename;
528     int opt_extract_without_path;
529     int opt_overwrite;
530     const char* password;
531 {
532     int err = UNZ_OK;
533     if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
534     {
535         minizip_printf("file %s not found in the zipfile\n",filename);
536         return 2;
537     }
538
539     if (do_extract_currentfile(uf,&opt_extract_without_path,
540                                       &opt_overwrite,
541                                       password) == UNZ_OK)
542         return 0;
543     else
544         return 1;
545 }
546
547
548 int miniunz(argc,argv)
549     int argc;
550     char *argv[];
551 {
552     const char *zipfilename=NULL;
553     const char *filename_to_extract=NULL;
554     const char *password=NULL;
555     char filename_try[MAXFILENAME+16] = "";
556     int i;
557     int ret_value=0;
558     int opt_do_list=0;
559     int opt_do_extract=1;
560     int opt_do_extract_withoutpath=0;
561     int opt_overwrite=0;
562     int opt_extractdir=0;
563     const char *dirname=NULL;
564     unzFile uf=NULL;
565
566     do_banner();
567     if (argc==1)
568     {
569         do_help();
570         return 0;
571     }
572     else
573     {
574         for (i=1;i<argc;i++)
575         {
576             if ((*argv[i])=='-')
577             {
578                 const char *p=argv[i]+1;
579
580                 while ((*p)!='\0')
581                 {
582                     char c=*(p++);;
583                     if ((c=='l') || (c=='L'))
584                         opt_do_list = 1;
585                     if ((c=='v') || (c=='V'))
586                         opt_do_list = 1;
587                     if ((c=='x') || (c=='X'))
588                         opt_do_extract = 1;
589                     if ((c=='e') || (c=='E'))
590                         opt_do_extract = opt_do_extract_withoutpath = 1;
591                     if ((c=='o') || (c=='O'))
592                         opt_overwrite=1;
593                     if ((c=='d') || (c=='D'))
594                     {
595                         opt_extractdir=1;
596                         dirname=argv[i+1];
597                     }
598
599                     if (((c=='p') || (c=='P')) && (i+1<argc))
600                     {
601                         password=argv[i+1];
602                         i++;
603                     }
604                 }
605             }
606             else
607             {
608                 if (zipfilename == NULL)
609                     zipfilename = argv[i];
610                 else if ((filename_to_extract==NULL) && (!opt_extractdir))
611                         filename_to_extract = argv[i] ;
612             }
613         }
614     }
615
616     if (zipfilename!=NULL)
617     {
618
619 #        ifdef USEWIN32IOAPI
620         zlib_filefunc64_def ffunc;
621 #        endif
622
623         strncpy(filename_try, zipfilename,MAXFILENAME-1);
624         /* strncpy doesnt append the trailing NULL, of the string is too long. */
625         filename_try[ MAXFILENAME ] = '\0';
626
627 #        ifdef USEWIN32IOAPI
628         fill_win32_filefunc64A(&ffunc);
629         uf = unzOpen2_64(zipfilename,&ffunc);
630 #        else
631         uf = unzOpen64(zipfilename);
632 #        endif
633         if (uf==NULL)
634         {
635             strcat(filename_try,".zip");
636 #            ifdef USEWIN32IOAPI
637             uf = unzOpen2_64(filename_try,&ffunc);
638 #            else
639             uf = unzOpen64(filename_try);
640 #            endif
641         }
642     }
643
644     if (uf==NULL)
645     {
646         minizip_printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
647         return 1;
648     }
649     minizip_printf("%s opened\n",filename_try);
650
651     if (opt_do_list==1)
652         ret_value = do_list(uf);
653     else if (opt_do_extract==1)
654     {
655 #ifdef _WIN32
656         if (opt_extractdir && _chdir(dirname))
657 #else
658         if (opt_extractdir && chdir(dirname))
659 #endif
660         {
661           minizip_printf("Error changing into %s, aborting\n", dirname);
662           return -1; /*exit(-1); */
663         }
664
665         if (filename_to_extract == NULL)
666             ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
667         else
668             ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
669     }
670
671     unzClose(uf);
672
673     return ret_value;
674 }