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