Rizin
unix-like reverse engineering framework and cli tools
minizip.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "zip.h"

Go to the source code of this file.

Macros

#define __USE_FILE_OFFSET64
 
#define __USE_LARGEFILE64
 
#define _LARGEFILE64_SOURCE
 
#define _FILE_OFFSET_BIT   64
 
#define FOPEN_FUNC(filename, mode)   fopen64(filename, mode)
 
#define FTELLO_FUNC(stream)   ftello64(stream)
 
#define FSEEKO_FUNC(stream, offset, origin)   fseeko64(stream, offset, origin)
 
#define WRITEBUFFERSIZE   (16384)
 
#define MAXFILENAME   (256)
 

Functions

uLong filetime (char *f, tm_zip *tmzip, uLong *dt) const
 
static int check_exist_file (char *filename) const
 
static void do_banner ()
 
static void do_help ()
 
static int getFileCrc (const char *filenameinzip, void *buf, unsigned long size_buf, unsigned long *result_crc)
 
static int isLargeFile (const char *filename)
 
int main (int argc, argv)
 

Macro Definition Documentation

◆ __USE_FILE_OFFSET64

#define __USE_FILE_OFFSET64

Definition at line 18 of file minizip.c.

◆ __USE_LARGEFILE64

#define __USE_LARGEFILE64

Definition at line 21 of file minizip.c.

◆ _FILE_OFFSET_BIT

#define _FILE_OFFSET_BIT   64

Definition at line 27 of file minizip.c.

◆ _LARGEFILE64_SOURCE

#define _LARGEFILE64_SOURCE

Definition at line 24 of file minizip.c.

◆ FOPEN_FUNC

#define FOPEN_FUNC (   filename,
  mode 
)    fopen64(filename, mode)

Definition at line 37 of file minizip.c.

◆ FSEEKO_FUNC

#define FSEEKO_FUNC (   stream,
  offset,
  origin 
)    fseeko64(stream, offset, origin)

Definition at line 39 of file minizip.c.

◆ FTELLO_FUNC

#define FTELLO_FUNC (   stream)    ftello64(stream)

Definition at line 38 of file minizip.c.

◆ MAXFILENAME

#define MAXFILENAME   (256)

Definition at line 71 of file minizip.c.

◆ WRITEBUFFERSIZE

#define WRITEBUFFERSIZE   (16384)

Definition at line 70 of file minizip.c.

Function Documentation

◆ check_exist_file()

static int check_exist_file ( char*  filename) const
static

Definition at line 154 of file minizip.c.

156 {
157  FILE* ftestexist;
158  int ret = 1;
159  ftestexist = FOPEN_FUNC(filename,"rb");
160  if (ftestexist==NULL)
161  ret = 0;
162  else
163  fclose(ftestexist);
164  return ret;
165 }
#define NULL
Definition: cris-opc.c:27
const char * filename
Definition: ioapi.h:137
#define FOPEN_FUNC(filename, mode)
Definition: minizip.c:37
string FILE
Definition: benchmark.py:21

References benchmark::FILE, FOPEN_FUNC, and NULL.

Referenced by main().

◆ do_banner()

static void do_banner ( )
static

Definition at line 167 of file minizip.c.

168 {
169  printf("MiniZip 1.1, demo of zLib + MiniZip64 package, written by Gilles Vollant\n");
170  printf("more info on MiniZip at http://www.winimage.com/zLibDll/minizip.html\n\n");
171 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93

References printf().

Referenced by main().

◆ do_help()

static void do_help ( )
static

Definition at line 173 of file minizip.c.

174 {
175  printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] [-j] file.zip [files_to_add]\n\n" \
176  " -o Overwrite existing file.zip\n" \
177  " -a Append to existing file.zip\n" \
178  " -0 Store only\n" \
179  " -1 Compress faster\n" \
180  " -9 Compress better\n\n" \
181  " -j exclude path. store only the file name.\n\n");
182 }

References printf().

Referenced by main().

◆ filetime()

uLong filetime ( char *  f,
tm_zip tmzip,
uLong dt 
) const

Definition at line 141 of file minizip.c.

145 {
146  return 0;
147 }

Referenced by main(), and unzRepair().

◆ getFileCrc()

static int getFileCrc ( const char *  filenameinzip,
void *  buf,
unsigned long  size_buf,
unsigned long result_crc 
)
static

Definition at line 186 of file minizip.c.

187 {
188  unsigned long calculate_crc=0;
189  int err=ZIP_OK;
190  FILE * fin = FOPEN_FUNC(filenameinzip,"rb");
191 
192  unsigned long size_read = 0;
193  unsigned long total_read = 0;
194  if (fin==NULL)
195  {
196  err = ZIP_ERRNO;
197  }
198 
199  if (err == ZIP_OK)
200  do
201  {
202  err = ZIP_OK;
203  size_read = fread(buf,1,size_buf,fin);
204  if (size_read < size_buf)
205  if (feof(fin)==0)
206  {
207  printf("error in reading %s\n",filenameinzip);
208  err = ZIP_ERRNO;
209  }
210 
211  if (size_read>0)
212  calculate_crc = crc32_z(calculate_crc,buf,size_read);
213  total_read += size_read;
214 
215  } while ((err == ZIP_OK) && (size_read>0));
216 
217  if (fin)
218  fclose(fin);
219 
220  *result_crc=calculate_crc;
221  printf("file %s crc %lx\n", filenameinzip, calculate_crc);
222  return err;
223 }
static bool err
Definition: armass.c:435
voidpf void * buf
Definition: ioapi.h:138
#define ZIP_OK
Definition: zip.h:72
#define ZIP_ERRNO
Definition: zip.h:74
unsigned long ZEXPORT crc32_z(unsigned long crc, const unsigned char FAR *buf, z_size_t len)
Definition: crc32.c:739

References crc32_z(), err, benchmark::FILE, FOPEN_FUNC, NULL, printf(), ZIP_ERRNO, and ZIP_OK.

Referenced by main().

◆ isLargeFile()

static int isLargeFile ( const char *  filename)
static

Definition at line 225 of file minizip.c.

226 {
227  int largeFile = 0;
228  ZPOS64_T pos = 0;
229  FILE* pFile = FOPEN_FUNC(filename, "rb");
230 
231  if(pFile != NULL)
232  {
233  FSEEKO_FUNC(pFile, 0, SEEK_END);
234  pos = (ZPOS64_T)FTELLO_FUNC(pFile);
235 
236  printf("File : %s is %lld bytes\n", filename, pos);
237 
238  if(pos >= 0xffffffff)
239  largeFile = 1;
240 
241  fclose(pFile);
242  }
243 
244  return largeFile;
245 }
unsigned long long int ZPOS64_T
Definition: ioapi.h:99
#define FSEEKO_FUNC(stream, offset, origin)
Definition: minizip.c:39
#define FTELLO_FUNC(stream)
Definition: minizip.c:38
int pos
Definition: main.c:11
#define SEEK_END
Definition: zip.c:84

References benchmark::FILE, FOPEN_FUNC, FSEEKO_FUNC, FTELLO_FUNC, NULL, pos, printf(), and SEEK_END.

Referenced by main().

◆ main()

int main ( int  argc,
argv   
)

Definition at line 247 of file minizip.c.

250 {
251  int i;
252  int opt_overwrite=0;
253  int opt_compress_level=Z_DEFAULT_COMPRESSION;
254  int opt_exclude_path=0;
255  int zipfilenamearg = 0;
256  char filename_try[MAXFILENAME+16];
257  int zipok;
258  int err=0;
259  size_t size_buf=0;
260  void* buf=NULL;
261  const char* password=NULL;
262 
263 
264  do_banner();
265  if (argc==1)
266  {
267  do_help();
268  return 0;
269  }
270  else
271  {
272  for (i=1;i<argc;i++)
273  {
274  if ((*argv[i])=='-')
275  {
276  const char *p=argv[i]+1;
277 
278  while ((*p)!='\0')
279  {
280  char c=*(p++);;
281  if ((c=='o') || (c=='O'))
282  opt_overwrite = 1;
283  if ((c=='a') || (c=='A'))
284  opt_overwrite = 2;
285  if ((c>='0') && (c<='9'))
286  opt_compress_level = c-'0';
287  if ((c=='j') || (c=='J'))
288  opt_exclude_path = 1;
289 
290  if (((c=='p') || (c=='P')) && (i+1<argc))
291  {
292  password=argv[i+1];
293  i++;
294  }
295  }
296  }
297  else
298  {
299  if (zipfilenamearg == 0)
300  {
301  zipfilenamearg = i ;
302  }
303  }
304  }
305  }
306 
307  size_buf = WRITEBUFFERSIZE;
308  buf = (void*)malloc(size_buf);
309  if (buf==NULL)
310  {
311  printf("Error allocating memory\n");
312  return ZIP_INTERNALERROR;
313  }
314 
315  if (zipfilenamearg==0)
316  {
317  zipok=0;
318  }
319  else
320  {
321  int i,len;
322  int dot_found=0;
323 
324  zipok = 1 ;
325  strncpy(filename_try, argv[zipfilenamearg],MAXFILENAME-1);
326  /* strncpy doesnt append the trailing NULL, of the string is too long. */
327  filename_try[ MAXFILENAME ] = '\0';
328 
329  len=(int)strlen(filename_try);
330  for (i=0;i<len;i++)
331  if (filename_try[i]=='.')
332  dot_found=1;
333 
334  if (dot_found==0)
335  strcat(filename_try,".zip");
336 
337  if (opt_overwrite==2)
338  {
339  /* if the file don't exist, we not append file */
340  if (check_exist_file(filename_try)==0)
341  opt_overwrite=1;
342  }
343  else
344  if (opt_overwrite==0)
345  if (check_exist_file(filename_try)!=0)
346  {
347  char rep=0;
348  do
349  {
350  char answer[128];
351  int ret;
352  printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ",filename_try);
353  ret = scanf("%1s",answer);
354  if (ret != 1)
355  {
356  exit(EXIT_FAILURE);
357  }
358  rep = answer[0] ;
359  if ((rep>='a') && (rep<='z'))
360  rep -= 0x20;
361  }
362  while ((rep!='Y') && (rep!='N') && (rep!='A'));
363  if (rep=='N')
364  zipok = 0;
365  if (rep=='A')
366  opt_overwrite = 2;
367  }
368  }
369 
370  if (zipok==1)
371  {
372  zipFile zf;
373  int errclose;
374 # ifdef USEWIN32IOAPI
375  zlib_filefunc64_def ffunc;
376  fill_win32_filefunc64A(&ffunc);
377  zf = zipOpen2_64(filename_try,(opt_overwrite==2) ? 2 : 0,NULL,&ffunc);
378 # else
379  zf = zipOpen64(filename_try,(opt_overwrite==2) ? 2 : 0);
380 # endif
381 
382  if (zf == NULL)
383  {
384  printf("error opening %s\n",filename_try);
385  err= ZIP_ERRNO;
386  }
387  else
388  printf("creating %s\n",filename_try);
389 
390  for (i=zipfilenamearg+1;(i<argc) && (err==ZIP_OK);i++)
391  {
392  if (!((((*(argv[i]))=='-') || ((*(argv[i]))=='/')) &&
393  ((argv[i][1]=='o') || (argv[i][1]=='O') ||
394  (argv[i][1]=='a') || (argv[i][1]=='A') ||
395  (argv[i][1]=='p') || (argv[i][1]=='P') ||
396  ((argv[i][1]>='0') || (argv[i][1]<='9'))) &&
397  (strlen(argv[i]) == 2)))
398  {
399  FILE * fin;
400  size_t size_read;
401  const char* filenameinzip = argv[i];
402  const char *savefilenameinzip;
403  zip_fileinfo zi;
404  unsigned long crcFile=0;
405  int zip64 = 0;
406 
408  zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
409  zi.dosDate = 0;
410  zi.internal_fa = 0;
411  zi.external_fa = 0;
412  filetime(filenameinzip,&zi.tmz_date,&zi.dosDate);
413 
414 /*
415  err = zipOpenNewFileInZip(zf,filenameinzip,&zi,
416  NULL,0,NULL,0,NULL / * comment * /,
417  (opt_compress_level != 0) ? Z_DEFLATED : 0,
418  opt_compress_level);
419 */
420  if ((password != NULL) && (err==ZIP_OK))
421  err = getFileCrc(filenameinzip,buf,size_buf,&crcFile);
422 
423  zip64 = isLargeFile(filenameinzip);
424 
425  /* The path name saved, should not include a leading slash. */
426  /*if it did, windows/xp and dynazip couldn't read the zip file. */
427  savefilenameinzip = filenameinzip;
428  while( savefilenameinzip[0] == '\\' || savefilenameinzip[0] == '/' )
429  {
430  savefilenameinzip++;
431  }
432 
433  /*should the zip file contain any path at all?*/
434  if( opt_exclude_path )
435  {
436  const char *tmpptr;
437  const char *lastslash = 0;
438  for( tmpptr = savefilenameinzip; *tmpptr; tmpptr++)
439  {
440  if( *tmpptr == '\\' || *tmpptr == '/')
441  {
442  lastslash = tmpptr;
443  }
444  }
445  if( lastslash != NULL )
446  {
447  savefilenameinzip = lastslash+1; // base filename follows last slash.
448  }
449  }
450 
451 
452  err = zipOpenNewFileInZip3_64(zf,savefilenameinzip,&zi,
453  NULL,0,NULL,0,NULL /* comment*/,
454  (opt_compress_level != 0) ? Z_DEFLATED : 0,
455  opt_compress_level,0,
456  /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */
458  password,crcFile, zip64);
459 
460  if (err != ZIP_OK)
461  printf("error in opening %s in zipfile\n",filenameinzip);
462  else
463  {
464  fin = FOPEN_FUNC(filenameinzip,"rb");
465  if (fin==NULL)
466  {
467  err=ZIP_ERRNO;
468  printf("error in opening %s for reading\n",filenameinzip);
469  }
470  }
471 
472  if (err == ZIP_OK)
473  do
474  {
475  err = ZIP_OK;
476  size_read = fread(buf,1,size_buf,fin);
477  if (size_read < size_buf)
478  if (feof(fin)==0)
479  {
480  printf("error in reading %s\n",filenameinzip);
481  err = ZIP_ERRNO;
482  }
483 
484  if (size_read>0)
485  {
486  err = zipWriteInFileInZip (zf,buf,(unsigned)size_read);
487  if (err<0)
488  {
489  printf("error in writing %s in the zipfile\n",
490  filenameinzip);
491  }
492 
493  }
494  } while ((err == ZIP_OK) && (size_read>0));
495 
496  if (fin)
497  fclose(fin);
498 
499  if (err<0)
500  err=ZIP_ERRNO;
501  else
502  {
503  err = zipCloseFileInZip(zf);
504  if (err!=ZIP_OK)
505  printf("error in closing %s in the zipfile\n",
506  filenameinzip);
507  }
508  }
509  }
510  errclose = zipClose(zf,NULL);
511  if (errclose != ZIP_OK)
512  printf("error in closing %s\n",filename_try);
513  }
514  else
515  {
516  do_help();
517  }
518 
519  free(buf);
520  return 0;
521 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define MAX_WBITS
Definition: flirt.c:105
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def)
Definition: iowin32.c:439
void * p
Definition: libc.cpp:67
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static void do_banner()
Definition: minizip.c:167
#define MAXFILENAME
Definition: minizip.c:71
static int isLargeFile(const char *filename)
Definition: minizip.c:225
static int check_exist_file(char *filename) const
Definition: minizip.c:154
static int getFileCrc(const char *filenameinzip, void *buf, unsigned long size_buf, unsigned long *result_crc)
Definition: minizip.c:186
static void do_help()
Definition: minizip.c:173
#define WRITEBUFFERSIZE
Definition: minizip.c:70
uLong filetime(char *f, tm_zip *tmzip, uLong *dt) const
Definition: minizip.c:141
static int
Definition: sfsocketcall.h:114
#define c(i)
Definition: sha256.c:43
int tm_hour
Definition: zip.h:93
int tm_min
Definition: zip.h:92
int tm_sec
Definition: zip.h:91
int tm_mon
Definition: zip.h:95
int tm_year
Definition: zip.h:96
int tm_mday
Definition: zip.h:94
uLong dosDate
Definition: zip.h:102
uLong internal_fa
Definition: zip.h:105
uLong external_fa
Definition: zip.h:106
tm_zip tmz_date
Definition: zip.h:101
int ZEXPORT zipClose(zipFile file, const char *global_comment)
Definition: zip.c:1882
zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc *globalcomment, zlib_filefunc64_def *pzlib_filefunc_def)
Definition: zip.c:932
int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, unsigned int len)
Definition: zip.c:1402
int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi, const void *extrafield_local, uInt size_extrafield_local, const void *extrafield_global, uInt size_extrafield_global, const char *comment, int method, int level, int raw, int windowBits, int memLevel, int strategy, const char *password, uLong crcForCrypting, int zip64)
Definition: zip.c:1296
int ZEXPORT zipCloseFileInZip(zipFile file)
Definition: zip.c:1750
zipFile ZEXPORT zipOpen64(const void *pathname, int append)
Definition: zip.c:953
#define DEF_MEM_LEVEL
Definition: zip.h:83
#define ZIP_INTERNALERROR
Definition: zip.h:77
voidp zipFile
Definition: zip.h:69
#define Z_DEFLATED
Definition: zlib.h:209
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:193

References argv, c, check_exist_file(), DEF_MEM_LEVEL, do_banner(), do_help(), zip_fileinfo::dosDate, err, test-lz4-list::exit, zip_fileinfo::external_fa, benchmark::FILE, filetime(), fill_win32_filefunc64A(), FOPEN_FUNC, free(), getFileCrc(), i, int, zip_fileinfo::internal_fa, isLargeFile(), len, malloc(), MAX_WBITS, MAXFILENAME, NULL, p, printf(), tm_zip_s::tm_hour, tm_zip_s::tm_mday, tm_zip_s::tm_min, tm_zip_s::tm_mon, tm_zip_s::tm_sec, tm_zip_s::tm_year, zip_fileinfo::tmz_date, WRITEBUFFERSIZE, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY, Z_DEFLATED, ZIP_ERRNO, ZIP_INTERNALERROR, ZIP_OK, zipClose(), zipCloseFileInZip(), zipOpen2_64(), zipOpen64(), zipOpenNewFileInZip3_64(), and zipWriteInFileInZip().