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