Revert of Recorded additional tough_energy_cases. (patchset #1 id:1 of https://codere...
[chromium-blink-merge.git] / third_party / zlib / contrib / minizip / miniunz.c
blob9ed009fbd9947b39212123c072c489983079dc36
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 )
6 Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
8 Modifications of Unzip for Zip64
9 Copyright (C) 2007-2008 Even Rouault
11 Modifications for Zip64 support on both zip and unzip
12 Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
15 #ifndef _WIN32
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
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <errno.h>
35 #include <fcntl.h>
37 #ifdef unix
38 # include <unistd.h>
39 # include <utime.h>
40 #else
41 # include <direct.h>
42 # include <io.h>
43 #endif
45 #include "unzip.h"
47 #define CASESENSITIVITY (0)
48 #define WRITEBUFFERSIZE (8192)
49 #define MAXFILENAME (256)
51 #ifdef _WIN32
52 #define USEWIN32IOAPI
53 #include "iowin32.h"
54 #endif
56 mini unzip, demo of unzip package
58 usage :
59 Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir]
61 list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT
62 if it exists
66 /* change_file_date : change the date/time of a file
67 filename : the filename of the file where date/time must be modified
68 dosdate : the new date at the MSDos format (4 bytes)
69 tmu_date : the SAME new date at the tm_unz format */
70 void change_file_date(filename,dosdate,tmu_date)
71 const char *filename;
72 uLong dosdate;
73 tm_unz tmu_date;
75 #ifdef _WIN32
76 HANDLE hFile;
77 FILETIME ftm,ftLocal,ftCreate,ftLastAcc,ftLastWrite;
79 hFile = CreateFileA(filename,GENERIC_READ | GENERIC_WRITE,
80 0,NULL,OPEN_EXISTING,0,NULL);
81 GetFileTime(hFile,&ftCreate,&ftLastAcc,&ftLastWrite);
82 DosDateTimeToFileTime((WORD)(dosdate>>16),(WORD)dosdate,&ftLocal);
83 LocalFileTimeToFileTime(&ftLocal,&ftm);
84 SetFileTime(hFile,&ftm,&ftLastAcc,&ftm);
85 CloseHandle(hFile);
86 #else
87 #ifdef unix
88 struct utimbuf ut;
89 struct tm newdate;
90 newdate.tm_sec = tmu_date.tm_sec;
91 newdate.tm_min=tmu_date.tm_min;
92 newdate.tm_hour=tmu_date.tm_hour;
93 newdate.tm_mday=tmu_date.tm_mday;
94 newdate.tm_mon=tmu_date.tm_mon;
95 if (tmu_date.tm_year > 1900)
96 newdate.tm_year=tmu_date.tm_year - 1900;
97 else
98 newdate.tm_year=tmu_date.tm_year ;
99 newdate.tm_isdst=-1;
101 ut.actime=ut.modtime=mktime(&newdate);
102 utime(filename,&ut);
103 #endif
104 #endif
108 /* mymkdir and change_file_date are not 100 % portable
109 As I don't know well Unix, I wait feedback for the unix portion */
111 int mymkdir(dirname)
112 const char* dirname;
114 int ret=0;
115 #ifdef _WIN32
116 ret = _mkdir(dirname);
117 #else
118 #ifdef unix
119 ret = mkdir (dirname,0775);
120 #endif
121 #endif
122 return ret;
125 int makedir (newdir)
126 char *newdir;
128 char *buffer ;
129 char *p;
130 int len = (int)strlen(newdir);
132 if (len <= 0)
133 return 0;
135 buffer = (char*)malloc(len+1);
136 if (buffer==NULL)
138 printf("Error allocating memory\n");
139 return UNZ_INTERNALERROR;
141 strcpy(buffer,newdir);
143 if (buffer[len-1] == '/') {
144 buffer[len-1] = '\0';
146 if (mymkdir(buffer) == 0)
148 free(buffer);
149 return 1;
152 p = buffer+1;
153 while (1)
155 char hold;
157 while(*p && *p != '\\' && *p != '/')
158 p++;
159 hold = *p;
160 *p = 0;
161 if ((mymkdir(buffer) == -1) && (errno == ENOENT))
163 printf("couldn't create directory %s\n",buffer);
164 free(buffer);
165 return 0;
167 if (hold == 0)
168 break;
169 *p++ = hold;
171 free(buffer);
172 return 1;
175 void do_banner()
177 printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n");
178 printf("more info at http://www.winimage.com/zLibDll/unzip.html\n\n");
181 void do_help()
183 printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
184 " -e Extract without pathname (junk paths)\n" \
185 " -x Extract with pathname\n" \
186 " -v list files\n" \
187 " -l list files\n" \
188 " -d directory to extract into\n" \
189 " -o overwrite files without prompting\n" \
190 " -p extract crypted file using password\n\n");
193 void Display64BitsSize(ZPOS64_T n, int size_char)
195 /* to avoid compatibility problem , we do here the conversion */
196 char number[21];
197 int offset=19;
198 int pos_string = 19;
199 number[20]=0;
200 for (;;) {
201 number[offset]=(char)((n%10)+'0');
202 if (number[offset] != '0')
203 pos_string=offset;
204 n/=10;
205 if (offset==0)
206 break;
207 offset--;
210 int size_display_string = 19-pos_string;
211 while (size_char > size_display_string)
213 size_char--;
214 printf(" ");
218 printf("%s",&number[pos_string]);
221 int do_list(uf)
222 unzFile uf;
224 uLong i;
225 unz_global_info64 gi;
226 int err;
228 err = unzGetGlobalInfo64(uf,&gi);
229 if (err!=UNZ_OK)
230 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
231 printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
232 printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
233 for (i=0;i<gi.number_entry;i++)
235 char filename_inzip[256];
236 unz_file_info64 file_info;
237 uLong ratio=0;
238 const char *string_method;
239 char charCrypt=' ';
240 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
241 if (err!=UNZ_OK)
243 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
244 break;
246 if (file_info.uncompressed_size>0)
247 ratio = (uLong)((file_info.compressed_size*100)/file_info.uncompressed_size);
249 /* display a '*' if the file is crypted */
250 if ((file_info.flag & 1) != 0)
251 charCrypt='*';
253 if (file_info.compression_method==0)
254 string_method="Stored";
255 else
256 if (file_info.compression_method==Z_DEFLATED)
258 uInt iLevel=(uInt)((file_info.flag & 0x6)/2);
259 if (iLevel==0)
260 string_method="Defl:N";
261 else if (iLevel==1)
262 string_method="Defl:X";
263 else if ((iLevel==2) || (iLevel==3))
264 string_method="Defl:F"; /* 2:fast , 3 : extra fast*/
266 else
267 if (file_info.compression_method==Z_BZIP2ED)
269 string_method="BZip2 ";
271 else
272 string_method="Unkn. ";
274 Display64BitsSize(file_info.uncompressed_size,7);
275 printf(" %6s%c",string_method,charCrypt);
276 Display64BitsSize(file_info.compressed_size,7);
277 printf(" %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n",
278 ratio,
279 (uLong)file_info.tmu_date.tm_mon + 1,
280 (uLong)file_info.tmu_date.tm_mday,
281 (uLong)file_info.tmu_date.tm_year % 100,
282 (uLong)file_info.tmu_date.tm_hour,(uLong)file_info.tmu_date.tm_min,
283 (uLong)file_info.crc,filename_inzip);
284 if ((i+1)<gi.number_entry)
286 err = unzGoToNextFile(uf);
287 if (err!=UNZ_OK)
289 printf("error %d with zipfile in unzGoToNextFile\n",err);
290 break;
295 return 0;
299 int do_extract_currentfile(uf,popt_extract_without_path,popt_overwrite,password)
300 unzFile uf;
301 const int* popt_extract_without_path;
302 int* popt_overwrite;
303 const char* password;
305 char filename_inzip[256];
306 char* filename_withoutpath;
307 char* p;
308 int err=UNZ_OK;
309 FILE *fout=NULL;
310 void* buf;
311 uInt size_buf;
313 unz_file_info64 file_info;
314 uLong ratio=0;
315 err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
317 if (err!=UNZ_OK)
319 printf("error %d with zipfile in unzGetCurrentFileInfo\n",err);
320 return err;
323 size_buf = WRITEBUFFERSIZE;
324 buf = (void*)malloc(size_buf);
325 if (buf==NULL)
327 printf("Error allocating memory\n");
328 return UNZ_INTERNALERROR;
331 p = filename_withoutpath = filename_inzip;
332 while ((*p) != '\0')
334 if (((*p)=='/') || ((*p)=='\\'))
335 filename_withoutpath = p+1;
336 p++;
339 if ((*filename_withoutpath)=='\0')
341 if ((*popt_extract_without_path)==0)
343 printf("creating directory: %s\n",filename_inzip);
344 mymkdir(filename_inzip);
347 else
349 const char* write_filename;
350 int skip=0;
352 if ((*popt_extract_without_path)==0)
353 write_filename = filename_inzip;
354 else
355 write_filename = filename_withoutpath;
357 err = unzOpenCurrentFilePassword(uf,password);
358 if (err!=UNZ_OK)
360 printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
363 if (((*popt_overwrite)==0) && (err==UNZ_OK))
365 char rep=0;
366 FILE* ftestexist;
367 ftestexist = fopen64(write_filename,"rb");
368 if (ftestexist!=NULL)
370 fclose(ftestexist);
373 char answer[128];
374 int ret;
376 printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename);
377 ret = scanf("%1s",answer);
378 if (ret != 1)
380 exit(EXIT_FAILURE);
382 rep = answer[0] ;
383 if ((rep>='a') && (rep<='z'))
384 rep -= 0x20;
386 while ((rep!='Y') && (rep!='N') && (rep!='A'));
389 if (rep == 'N')
390 skip = 1;
392 if (rep == 'A')
393 *popt_overwrite=1;
396 if ((skip==0) && (err==UNZ_OK))
398 fout=fopen64(write_filename,"wb");
400 /* some zipfile don't contain directory alone before file */
401 if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
402 (filename_withoutpath!=(char*)filename_inzip))
404 char c=*(filename_withoutpath-1);
405 *(filename_withoutpath-1)='\0';
406 makedir(write_filename);
407 *(filename_withoutpath-1)=c;
408 fout=fopen64(write_filename,"wb");
411 if (fout==NULL)
413 printf("error opening %s\n",write_filename);
417 if (fout!=NULL)
419 printf(" extracting: %s\n",write_filename);
423 err = unzReadCurrentFile(uf,buf,size_buf);
424 if (err<0)
426 printf("error %d with zipfile in unzReadCurrentFile\n",err);
427 break;
429 if (err>0)
430 if (fwrite(buf,err,1,fout)!=1)
432 printf("error in writing extracted file\n");
433 err=UNZ_ERRNO;
434 break;
437 while (err>0);
438 if (fout)
439 fclose(fout);
441 if (err==0)
442 change_file_date(write_filename,file_info.dosDate,
443 file_info.tmu_date);
446 if (err==UNZ_OK)
448 err = unzCloseCurrentFile (uf);
449 if (err!=UNZ_OK)
451 printf("error %d with zipfile in unzCloseCurrentFile\n",err);
454 else
455 unzCloseCurrentFile(uf); /* don't lose the error */
458 free(buf);
459 return err;
463 int do_extract(uf,opt_extract_without_path,opt_overwrite,password)
464 unzFile uf;
465 int opt_extract_without_path;
466 int opt_overwrite;
467 const char* password;
469 uLong i;
470 unz_global_info64 gi;
471 int err;
472 FILE* fout=NULL;
474 err = unzGetGlobalInfo64(uf,&gi);
475 if (err!=UNZ_OK)
476 printf("error %d with zipfile in unzGetGlobalInfo \n",err);
478 for (i=0;i<gi.number_entry;i++)
480 if (do_extract_currentfile(uf,&opt_extract_without_path,
481 &opt_overwrite,
482 password) != UNZ_OK)
483 break;
485 if ((i+1)<gi.number_entry)
487 err = unzGoToNextFile(uf);
488 if (err!=UNZ_OK)
490 printf("error %d with zipfile in unzGoToNextFile\n",err);
491 break;
496 return 0;
499 int do_extract_onefile(uf,filename,opt_extract_without_path,opt_overwrite,password)
500 unzFile uf;
501 const char* filename;
502 int opt_extract_without_path;
503 int opt_overwrite;
504 const char* password;
506 int err = UNZ_OK;
507 if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
509 printf("file %s not found in the zipfile\n",filename);
510 return 2;
513 if (do_extract_currentfile(uf,&opt_extract_without_path,
514 &opt_overwrite,
515 password) == UNZ_OK)
516 return 0;
517 else
518 return 1;
522 int main(argc,argv)
523 int argc;
524 char *argv[];
526 const char *zipfilename=NULL;
527 const char *filename_to_extract=NULL;
528 const char *password=NULL;
529 char filename_try[MAXFILENAME+16] = "";
530 int i;
531 int ret_value=0;
532 int opt_do_list=0;
533 int opt_do_extract=1;
534 int opt_do_extract_withoutpath=0;
535 int opt_overwrite=0;
536 int opt_extractdir=0;
537 const char *dirname=NULL;
538 unzFile uf=NULL;
540 do_banner();
541 if (argc==1)
543 do_help();
544 return 0;
546 else
548 for (i=1;i<argc;i++)
550 if ((*argv[i])=='-')
552 const char *p=argv[i]+1;
554 while ((*p)!='\0')
556 char c=*(p++);;
557 if ((c=='l') || (c=='L'))
558 opt_do_list = 1;
559 if ((c=='v') || (c=='V'))
560 opt_do_list = 1;
561 if ((c=='x') || (c=='X'))
562 opt_do_extract = 1;
563 if ((c=='e') || (c=='E'))
564 opt_do_extract = opt_do_extract_withoutpath = 1;
565 if ((c=='o') || (c=='O'))
566 opt_overwrite=1;
567 if ((c=='d') || (c=='D'))
569 opt_extractdir=1;
570 dirname=argv[i+1];
573 if (((c=='p') || (c=='P')) && (i+1<argc))
575 password=argv[i+1];
576 i++;
580 else
582 if (zipfilename == NULL)
583 zipfilename = argv[i];
584 else if ((filename_to_extract==NULL) && (!opt_extractdir))
585 filename_to_extract = argv[i] ;
590 if (zipfilename!=NULL)
593 # ifdef USEWIN32IOAPI
594 zlib_filefunc64_def ffunc;
595 # endif
597 strncpy(filename_try, zipfilename,MAXFILENAME-1);
598 /* strncpy doesnt append the trailing NULL, of the string is too long. */
599 filename_try[ MAXFILENAME ] = '\0';
601 # ifdef USEWIN32IOAPI
602 fill_win32_filefunc64A(&ffunc);
603 uf = unzOpen2_64(zipfilename,&ffunc);
604 # else
605 uf = unzOpen64(zipfilename);
606 # endif
607 if (uf==NULL)
609 strcat(filename_try,".zip");
610 # ifdef USEWIN32IOAPI
611 uf = unzOpen2_64(filename_try,&ffunc);
612 # else
613 uf = unzOpen64(filename_try);
614 # endif
618 if (uf==NULL)
620 printf("Cannot open %s or %s.zip\n",zipfilename,zipfilename);
621 return 1;
623 printf("%s opened\n",filename_try);
625 if (opt_do_list==1)
626 ret_value = do_list(uf);
627 else if (opt_do_extract==1)
629 #ifdef _WIN32
630 if (opt_extractdir && _chdir(dirname))
631 #else
632 if (opt_extractdir && chdir(dirname))
633 #endif
635 printf("Error changing into %s, aborting\n", dirname);
636 exit(-1);
639 if (filename_to_extract == NULL)
640 ret_value = do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password);
641 else
642 ret_value = do_extract_onefile(uf, filename_to_extract, opt_do_extract_withoutpath, opt_overwrite, password);
645 unzClose(uf);
647 return ret_value;