]> git.lyx.org Git - features.git/blob - src/support/minizip/zip.c
nuke compiler warnings in minizip
[features.git] / src / support / minizip / zip.c
1 /* zip.c -- IO on .zip files using zlib
2    Version 1.01e, February 12th, 2005
3
4    27 Dec 2004 Rolf Kalbermatter
5    Modification to zipOpen2 to support globalComment retrieval.
6
7    Copyright (C) 1998-2005 Gilles Vollant
8
9    Read zip.h for more info
10 */
11
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 #include "zlib.h"
18 #include "zip.h"
19
20 #ifdef STDC
21 #  include <stddef.h>
22 #  include <string.h>
23 #  include <stdlib.h>
24 #endif
25 #ifdef NO_ERRNO_H
26     extern int errno;
27 #else
28 #   include <errno.h>
29 #endif
30
31
32 #ifndef local
33 #  define local static
34 #endif
35 /* compile with -Dlocal if your debugger can't find static symbols */
36
37 #ifndef VERSIONMADEBY
38 # define VERSIONMADEBY   (0x0) /* platform depedent */
39 #endif
40
41 #ifndef Z_BUFSIZE
42 #define Z_BUFSIZE (16384)
43 #endif
44
45 #ifndef Z_MAXFILENAMEINZIP
46 #define Z_MAXFILENAMEINZIP (256)
47 #endif
48
49 #ifndef ALLOC
50 # define ALLOC(size) (malloc(size))
51 #endif
52 #ifndef TRYFREE
53 # define TRYFREE(p) {if (p) free(p);}
54 #endif
55
56 /*
57 #define SIZECENTRALDIRITEM (0x2e)
58 #define SIZEZIPLOCALHEADER (0x1e)
59 */
60
61 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
62
63 #ifndef SEEK_CUR
64 #define SEEK_CUR    1
65 #endif
66
67 #ifndef SEEK_END
68 #define SEEK_END    2
69 #endif
70
71 #ifndef SEEK_SET
72 #define SEEK_SET    0
73 #endif
74
75 #ifndef DEF_MEM_LEVEL
76 #if MAX_MEM_LEVEL >= 8
77 #  define DEF_MEM_LEVEL 8
78 #else
79 #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
80 #endif
81 #endif
82 const char zip_copyright[] =
83    " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
84
85
86 #define SIZEDATA_INDATABLOCK (4096-(4*4))
87
88 #define LOCALHEADERMAGIC    (0x04034b50)
89 #define CENTRALHEADERMAGIC  (0x02014b50)
90 #define ENDHEADERMAGIC      (0x06054b50)
91
92 #define FLAG_LOCALHEADER_OFFSET (0x06)
93 #define CRC_LOCALHEADER_OFFSET  (0x0e)
94
95 #define SIZECENTRALHEADER (0x2e) /* 46 */
96
97 typedef struct linkedlist_datablock_internal_s
98 {
99   struct linkedlist_datablock_internal_s* next_datablock;
100   uLong  avail_in_this_block;
101   uLong  filled_in_this_block;
102   uLong  unused; /* for future use and alignement */
103   unsigned char data[SIZEDATA_INDATABLOCK];
104 } linkedlist_datablock_internal;
105
106 typedef struct linkedlist_data_s
107 {
108     linkedlist_datablock_internal* first_block;
109     linkedlist_datablock_internal* last_block;
110 } linkedlist_data;
111
112
113 typedef struct
114 {
115     z_stream stream;            /* zLib stream structure for inflate */
116     int  stream_initialised;    /* 1 is stream is initialised */
117     uInt pos_in_buffered_data;  /* last written byte in buffered_data */
118
119     uLong pos_local_header;     /* offset of the local header of the file
120                                      currenty writing */
121     char* central_header;       /* central header data for the current file */
122     uLong size_centralheader;   /* size of the central header for cur file */
123     uLong flag;                 /* flag of the file currently writing */
124
125     int  method;                /* compression method of file currenty wr.*/
126     int  raw;                   /* 1 for directly writing raw data */
127     Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
128     uLong dosDate;
129     uLong crc32;
130     int  encrypt;
131 #ifndef NOCRYPT
132     unsigned long keys[3];     /* keys defining the pseudo-random sequence */
133     const unsigned long* pcrc_32_tab;
134     int crypt_header_size;
135 #endif
136 } curfile_info;
137
138 typedef struct
139 {
140     zlib_filefunc_def z_filefunc;
141     voidpf filestream;        /* io structore of the zipfile */
142     linkedlist_data central_dir;/* datablock with central dir in construction*/
143     int  in_opened_file_inzip;  /* 1 if a file in the zip is currently writ.*/
144     curfile_info ci;            /* info on the file curretly writing */
145
146     uLong begin_pos;            /* position of the beginning of the zipfile */
147     uLong add_position_when_writting_offset;
148     uLong number_entry;
149 #ifndef NO_ADDFILEINEXISTINGZIP
150     char *globalcomment;
151 #endif
152 } zip_internal;
153
154
155
156 #ifndef NOCRYPT
157 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
158 #include "crypt.h"
159 #endif
160
161 local linkedlist_datablock_internal* allocate_new_datablock()
162 {
163     linkedlist_datablock_internal* ldi;
164     ldi = (linkedlist_datablock_internal*)
165                  ALLOC(sizeof(linkedlist_datablock_internal));
166     if (ldi!=NULL)
167     {
168         ldi->next_datablock = NULL ;
169         ldi->filled_in_this_block = 0 ;
170         ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
171     }
172     return ldi;
173 }
174
175 local void free_datablock(linkedlist_datablock_internal*ldi)
176 {
177     while (ldi!=NULL)
178     {
179         linkedlist_datablock_internal* ldinext = ldi->next_datablock;
180         TRYFREE(ldi);
181         ldi = ldinext;
182     }
183 }
184
185 local void init_linkedlist(linkedlist_data* ll)
186 {
187     ll->first_block = ll->last_block = NULL;
188 }
189
190 #if 0
191 local void free_linkedlist(linkedlist_data* ll)
192 {
193     free_datablock(ll->first_block);
194     ll->first_block = ll->last_block = NULL;
195 }
196 #endif
197
198 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
199 {
200     linkedlist_datablock_internal* ldi;
201     const unsigned char* from_copy;
202
203     if (ll==NULL)
204         return ZIP_INTERNALERROR;
205
206     if (ll->last_block == NULL)
207     {
208         ll->first_block = ll->last_block = allocate_new_datablock();
209         if (ll->first_block == NULL)
210             return ZIP_INTERNALERROR;
211     }
212
213     ldi = ll->last_block;
214     from_copy = (unsigned char*)buf;
215
216     while (len>0)
217     {
218         uInt copy_this;
219         uInt i;
220         unsigned char* to_copy;
221
222         if (ldi->avail_in_this_block==0)
223         {
224             ldi->next_datablock = allocate_new_datablock();
225             if (ldi->next_datablock == NULL)
226                 return ZIP_INTERNALERROR;
227             ldi = ldi->next_datablock ;
228             ll->last_block = ldi;
229         }
230
231         if (ldi->avail_in_this_block < len)
232             copy_this = (uInt)ldi->avail_in_this_block;
233         else
234             copy_this = (uInt)len;
235
236         to_copy = &(ldi->data[ldi->filled_in_this_block]);
237
238         for (i=0;i<copy_this;i++)
239             *(to_copy+i)=*(from_copy+i);
240
241         ldi->filled_in_this_block += copy_this;
242         ldi->avail_in_this_block -= copy_this;
243         from_copy += copy_this ;
244         len -= copy_this;
245     }
246     return ZIP_OK;
247 }
248
249
250
251 /****************************************************************************/
252
253 #ifndef NO_ADDFILEINEXISTINGZIP
254 /* ===========================================================================
255    Inputs a long in LSB order to the given file
256    nbByte == 1, 2 or 4 (byte, short or long)
257 */
258
259 local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
260                                 voidpf filestream, uLong x, int nbByte));
261 local int ziplocal_putValue (const zlib_filefunc_def* pzlib_filefunc_def, 
262         voidpf filestream, uLong x, int nbByte)
263 {
264     unsigned char buf[4];
265     int n;
266     for (n = 0; n < nbByte; n++)
267     {
268         buf[n] = (unsigned char)(x & 0xff);
269         x >>= 8;
270     }
271     if (x != 0)
272       {     /* data overflow - hack for ZIP64 (X Roche) */
273       for (n = 0; n < nbByte; n++)
274         {
275           buf[n] = 0xff;
276         }
277       }
278
279     if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
280         return ZIP_ERRNO;
281     else
282         return ZIP_OK;
283 }
284
285 local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
286 local void ziplocal_putValue_inmemory (void* dest, uLong x, int nbByte)
287 {
288     unsigned char* buf=(unsigned char*)dest;
289     int n;
290     for (n = 0; n < nbByte; n++) {
291         buf[n] = (unsigned char)(x & 0xff);
292         x >>= 8;
293     }
294
295     if (x != 0)
296     {     /* data overflow - hack for ZIP64 */
297        for (n = 0; n < nbByte; n++)
298        {
299           buf[n] = 0xff;
300        }
301     }
302 }
303
304 /****************************************************************************/
305
306
307 local uLong ziplocal_TmzDateToDosDate(const tm_zip* ptm, uLong /* dosDate */)
308 {
309     uLong year = (uLong)ptm->tm_year;
310     if (year>1980)
311         year-=1980;
312     else if (year>80)
313         year-=80;
314     return
315       (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
316         ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
317 }
318
319
320 /****************************************************************************/
321
322 local int ziplocal_getByte OF((
323     const zlib_filefunc_def* pzlib_filefunc_def,
324     voidpf filestream,
325     int *pi));
326
327 local int ziplocal_getByte(const zlib_filefunc_def* pzlib_filefunc_def, 
328     voidpf filestream, int *pi)
329 {
330     unsigned char c;
331     int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
332     if (err==1)
333     {
334         *pi = (int)c;
335         return ZIP_OK;
336     }
337     else
338     {
339         if (ZERROR(*pzlib_filefunc_def,filestream))
340             return ZIP_ERRNO;
341         else
342             return ZIP_EOF;
343     }
344 }
345
346
347 /* ===========================================================================
348    Reads a long in LSB order from the given gz_stream. Sets
349 */
350 local int ziplocal_getShort OF((
351     const zlib_filefunc_def* pzlib_filefunc_def,
352     voidpf filestream,
353     uLong *pX));
354
355 local int ziplocal_getShort ( const zlib_filefunc_def* pzlib_filefunc_def, voidpf filestream, uLong * pX)
356 {
357     uLong x ;
358     int i;
359     int err;
360
361     err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
362     x = (uLong)i;
363
364     if (err==ZIP_OK)
365         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
366     x += ((uLong)i)<<8;
367
368     if (err==ZIP_OK)
369         *pX = x;
370     else
371         *pX = 0;
372     return err;
373 }
374
375 local int ziplocal_getLong OF((
376     const zlib_filefunc_def* pzlib_filefunc_def,
377     voidpf filestream,
378     uLong *pX));
379
380 local int ziplocal_getLong (const zlib_filefunc_def* pzlib_filefunc_def, 
381         voidpf filestream, uLong * pX)
382 {
383     uLong x ;
384     int i;
385     int err;
386
387     err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
388     x = (uLong)i;
389
390     if (err==ZIP_OK)
391         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
392     x += ((uLong)i)<<8;
393
394     if (err==ZIP_OK)
395         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
396     x += ((uLong)i)<<16;
397
398     if (err==ZIP_OK)
399         err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
400     x += ((uLong)i)<<24;
401
402     if (err==ZIP_OK)
403         *pX = x;
404     else
405         *pX = 0;
406     return err;
407 }
408
409 #ifndef BUFREADCOMMENT
410 #define BUFREADCOMMENT (0x400)
411 #endif
412 /*
413   Locate the Central directory of a zipfile (at the end, just before
414     the global comment)
415 */
416 local uLong ziplocal_SearchCentralDir OF((
417     const zlib_filefunc_def* pzlib_filefunc_def,
418     voidpf filestream));
419
420 local uLong ziplocal_SearchCentralDir(const zlib_filefunc_def* pzlib_filefunc_def,
421     voidpf filestream)
422 {
423     unsigned char* buf;
424     uLong uSizeFile;
425     uLong uBackRead;
426     uLong uMaxBack=0xffff; /* maximum size of global comment */
427     uLong uPosFound=0;
428
429     if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
430         return 0;
431
432
433     uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
434
435     if (uMaxBack>uSizeFile)
436         uMaxBack = uSizeFile;
437
438     buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
439     if (buf==NULL)
440         return 0;
441
442     uBackRead = 4;
443     while (uBackRead<uMaxBack)
444     {
445         uLong uReadSize,uReadPos ;
446         int i;
447         if (uBackRead+BUFREADCOMMENT>uMaxBack)
448             uBackRead = uMaxBack;
449         else
450             uBackRead+=BUFREADCOMMENT;
451         uReadPos = uSizeFile-uBackRead ;
452
453         uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
454                      (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
455         if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
456             break;
457
458         if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
459             break;
460
461         for (i=(int)uReadSize-3; (i--)>0;)
462             if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
463                 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
464             {
465                 uPosFound = uReadPos+i;
466                 break;
467             }
468
469         if (uPosFound!=0)
470             break;
471     }
472     TRYFREE(buf);
473     return uPosFound;
474 }
475 #endif /* !NO_ADDFILEINEXISTINGZIP*/
476
477 /************************************************************/
478 extern zipFile ZEXPORT zipOpen2 (const char * pathname,  int  append, 
479     zipcharpc* globalcomment,  zlib_filefunc_def* pzlib_filefunc_def)
480 {
481     zip_internal ziinit;
482     zip_internal* zi;
483     int err=ZIP_OK;
484
485
486     if (pzlib_filefunc_def==NULL)
487         fill_fopen_filefunc(&ziinit.z_filefunc);
488     else
489         ziinit.z_filefunc = *pzlib_filefunc_def;
490
491     ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
492                  (ziinit.z_filefunc.opaque,
493                   pathname,
494                   (append == APPEND_STATUS_CREATE) ?
495                   (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
496                     (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
497
498     if (ziinit.filestream == NULL)
499         return NULL;
500     ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
501     ziinit.in_opened_file_inzip = 0;
502     ziinit.ci.stream_initialised = 0;
503     ziinit.number_entry = 0;
504     ziinit.add_position_when_writting_offset = 0;
505     init_linkedlist(&(ziinit.central_dir));
506
507
508     zi = (zip_internal*)ALLOC(sizeof(zip_internal));
509     if (zi==NULL)
510     {
511         ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
512         return NULL;
513     }
514
515     /* now we add file in a zipfile */
516 #    ifndef NO_ADDFILEINEXISTINGZIP
517     ziinit.globalcomment = NULL;
518     if (append == APPEND_STATUS_ADDINZIP)
519     {
520         uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
521
522         uLong size_central_dir;     /* size of the central directory  */
523         uLong offset_central_dir;   /* offset of start of central directory */
524         uLong central_pos,uL;
525
526         uLong number_disk;          /* number of the current dist, used for
527                                     spaning ZIP, unsupported, always 0*/
528         uLong number_disk_with_CD;  /* number the the disk with central dir, used
529                                     for spaning ZIP, unsupported, always 0*/
530         uLong number_entry;
531         uLong number_entry_CD;      /* total number of entries in
532                                     the central dir
533                                     (same than number_entry on nospan) */
534         uLong size_comment;
535
536         central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
537         if (central_pos==0)
538             err=ZIP_ERRNO;
539
540         if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
541                                         central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
542             err=ZIP_ERRNO;
543
544         /* the signature, already checked */
545         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
546             err=ZIP_ERRNO;
547
548         /* number of this disk */
549         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
550             err=ZIP_ERRNO;
551
552         /* number of the disk with the start of the central directory */
553         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
554             err=ZIP_ERRNO;
555
556         /* total number of entries in the central dir on this disk */
557         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
558             err=ZIP_ERRNO;
559
560         /* total number of entries in the central dir */
561         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
562             err=ZIP_ERRNO;
563
564         if ((number_entry_CD!=number_entry) ||
565             (number_disk_with_CD!=0) ||
566             (number_disk!=0))
567             err=ZIP_BADZIPFILE;
568
569         /* size of the central directory */
570         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
571             err=ZIP_ERRNO;
572
573         /* offset of start of central directory with respect to the
574             starting disk number */
575         if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
576             err=ZIP_ERRNO;
577
578         /* zipfile global comment length */
579         if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
580             err=ZIP_ERRNO;
581
582         if ((central_pos<offset_central_dir+size_central_dir) &&
583             (err==ZIP_OK))
584             err=ZIP_BADZIPFILE;
585
586         if (err!=ZIP_OK)
587         {
588             ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
589             return NULL;
590         }
591
592         if (size_comment>0)
593         {
594             ziinit.globalcomment = (char*)ALLOC(size_comment+1);
595             if (ziinit.globalcomment)
596             {
597                size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
598                ziinit.globalcomment[size_comment]=0;
599             }
600         }
601
602         byte_before_the_zipfile = central_pos -
603                                 (offset_central_dir+size_central_dir);
604         ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
605
606         {
607             uLong size_central_dir_to_read = size_central_dir;
608             size_t buf_size = SIZEDATA_INDATABLOCK;
609             void* buf_read = (void*)ALLOC(buf_size);
610             if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
611                   offset_central_dir + byte_before_the_zipfile,
612                   ZLIB_FILEFUNC_SEEK_SET) != 0)
613                   err=ZIP_ERRNO;
614
615             while ((size_central_dir_to_read>0) && (err==ZIP_OK))
616             {
617                 uLong read_this = SIZEDATA_INDATABLOCK;
618                 if (read_this > size_central_dir_to_read)
619                     read_this = size_central_dir_to_read;
620                 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
621                     err=ZIP_ERRNO;
622
623                 if (err==ZIP_OK)
624                     err = add_data_in_datablock(&ziinit.central_dir,buf_read,
625                                                 (uLong)read_this);
626                 size_central_dir_to_read-=read_this;
627             }
628             TRYFREE(buf_read);
629         }
630         ziinit.begin_pos = byte_before_the_zipfile;
631         ziinit.number_entry = number_entry_CD;
632
633         if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
634                   offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
635             err=ZIP_ERRNO;
636     }
637
638     if (globalcomment)
639     {
640       *globalcomment = ziinit.globalcomment;
641     }
642 #    endif /* !NO_ADDFILEINEXISTINGZIP*/
643
644     if (err != ZIP_OK)
645     {
646 #    ifndef NO_ADDFILEINEXISTINGZIP
647         TRYFREE(ziinit.globalcomment);
648 #    endif /* !NO_ADDFILEINEXISTINGZIP*/
649         TRYFREE(zi);
650         return NULL;
651     }
652     else
653     {
654         *zi = ziinit;
655         return (zipFile)zi;
656     }
657 }
658
659 extern zipFile ZEXPORT zipOpen (const char * pathname,  int append)
660 {
661     return zipOpen2(pathname,append,NULL,NULL);
662 }
663
664 extern int ZEXPORT zipOpenNewFileInZip3 (
665     zipFile file,
666     const char* filename,
667     const zip_fileinfo* zipfi,
668     const void* extrafield_local,
669     uInt size_extrafield_local,
670     const void* extrafield_global,
671     uInt size_extrafield_global,
672     const char* comment,
673     int method,
674     int level,
675     int raw,
676     int windowBits,
677     int memLevel,
678     int strategy,
679     const char* password,
680     uLong crcForCrypting)
681 {
682     zip_internal* zi;
683     uInt size_filename;
684     uInt size_comment;
685     uInt i;
686     int err = ZIP_OK;
687
688 #    ifdef NOCRYPT
689     if (password != NULL)
690         return ZIP_PARAMERROR;
691 #    endif
692
693     if (file == NULL)
694         return ZIP_PARAMERROR;
695     if ((method!=0) && (method!=Z_DEFLATED))
696         return ZIP_PARAMERROR;
697
698     zi = (zip_internal*)file;
699
700     if (zi->in_opened_file_inzip == 1)
701     {
702         err = zipCloseFileInZip (file);
703         if (err != ZIP_OK)
704             return err;
705     }
706
707
708     if (filename==NULL)
709         filename="-";
710
711     if (comment==NULL)
712         size_comment = 0;
713     else
714         size_comment = (uInt)strlen(comment);
715
716     size_filename = (uInt)strlen(filename);
717
718     if (zipfi == NULL)
719         zi->ci.dosDate = 0;
720     else
721     {
722         if (zipfi->dosDate != 0)
723             zi->ci.dosDate = zipfi->dosDate;
724         else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
725     }
726
727     zi->ci.flag = 0;
728     if ((level==8) || (level==9))
729       zi->ci.flag |= 2;
730     if ((level==2))
731       zi->ci.flag |= 4;
732     if ((level==1))
733       zi->ci.flag |= 6;
734     if (password != NULL)
735       zi->ci.flag |= 1;
736
737     zi->ci.crc32 = 0;
738     zi->ci.method = method;
739     zi->ci.encrypt = 0;
740     zi->ci.stream_initialised = 0;
741     zi->ci.pos_in_buffered_data = 0;
742     zi->ci.raw = raw;
743     zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
744     zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
745                                       size_extrafield_global + size_comment;
746     zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
747
748     ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
749     /* version info */
750     ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
751     ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
752     ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
753     ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
754     ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
755     ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
756     ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
757     ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
758     ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
759     ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
760     ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
761     ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
762
763     if (zipfi==NULL)
764         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
765     else
766         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
767
768     if (zipfi==NULL)
769         ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
770     else
771         ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
772
773     ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
774
775     for (i=0;i<size_filename;i++)
776         *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
777
778     for (i=0;i<size_extrafield_global;i++)
779         *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
780               *(((const char*)extrafield_global)+i);
781
782     for (i=0;i<size_comment;i++)
783         *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
784               size_extrafield_global+i) = *(comment+i);
785     if (zi->ci.central_header == NULL)
786         return ZIP_INTERNALERROR;
787
788     /* write the local header */
789     err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
790
791     if (err==ZIP_OK)
792         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
793     if (err==ZIP_OK)
794         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
795
796     if (err==ZIP_OK)
797         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
798
799     if (err==ZIP_OK)
800         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
801
802     if (err==ZIP_OK)
803         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
804     if (err==ZIP_OK)
805         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
806     if (err==ZIP_OK)
807         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
808
809     if (err==ZIP_OK)
810         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
811
812     if (err==ZIP_OK)
813         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
814
815     if ((err==ZIP_OK) && (size_filename>0))
816         if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
817                 err = ZIP_ERRNO;
818
819     if ((err==ZIP_OK) && (size_extrafield_local>0))
820         if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
821                                                                            !=size_extrafield_local)
822                 err = ZIP_ERRNO;
823
824     zi->ci.stream.avail_in = (uInt)0;
825     zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
826     zi->ci.stream.next_out = zi->ci.buffered_data;
827     zi->ci.stream.total_in = 0;
828     zi->ci.stream.total_out = 0;
829
830     if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
831     {
832         zi->ci.stream.zalloc = (alloc_func)0;
833         zi->ci.stream.zfree = (free_func)0;
834         zi->ci.stream.opaque = (voidpf)0;
835
836         if (windowBits>0)
837             windowBits = -windowBits;
838
839         err = deflateInit2(&zi->ci.stream, level,
840                Z_DEFLATED, windowBits, memLevel, strategy);
841
842         if (err==Z_OK)
843             zi->ci.stream_initialised = 1;
844     }
845 #    ifndef NOCRYPT
846     zi->ci.crypt_header_size = 0;
847     if ((err==Z_OK) && (password != NULL))
848     {
849         unsigned char bufHead[RAND_HEAD_LEN];
850         unsigned int sizeHead;
851         zi->ci.encrypt = 1;
852         zi->ci.pcrc_32_tab = get_crc_table();
853         /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
854
855         sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
856         zi->ci.crypt_header_size = sizeHead;
857
858         if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
859                 err = ZIP_ERRNO;
860     }
861 #    endif
862
863     if (err==Z_OK)
864         zi->in_opened_file_inzip = 1;
865     return err;
866 }
867
868 extern int ZEXPORT zipOpenNewFileInZip2(
869     zipFile file,
870     const char* filename,
871     const zip_fileinfo* zipfi,
872     const void* extrafield_local,
873     uInt size_extrafield_local,
874     const void* extrafield_global,
875     uInt size_extrafield_global,
876     const char* comment,
877     int method,
878     int level,
879     int raw)
880 {
881     return zipOpenNewFileInZip3 (file, filename, zipfi,
882                                  extrafield_local, size_extrafield_local,
883                                  extrafield_global, size_extrafield_global,
884                                  comment, method, level, raw,
885                                  -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
886                                  NULL, 0);
887 }
888
889 extern int ZEXPORT zipOpenNewFileInZip (
890     zipFile file,
891     const char* filename,
892     const zip_fileinfo* zipfi,
893     const void* extrafield_local,
894     uInt size_extrafield_local,
895     const void* extrafield_global,
896     uInt size_extrafield_global,
897     const char* comment,
898     int method,
899     int level)
900 {
901     return zipOpenNewFileInZip2 (file, filename, zipfi,
902                                  extrafield_local, size_extrafield_local,
903                                  extrafield_global, size_extrafield_global,
904                                  comment, method, level, 0);
905 }
906
907 local int zipFlushWriteBuffer(
908   zip_internal* zi)
909 {
910     int err=ZIP_OK;
911
912     if (zi->ci.encrypt != 0)
913     {
914 #ifndef NOCRYPT
915         uInt i;
916         int t;
917         for (i=0;i<zi->ci.pos_in_buffered_data;i++)
918             zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
919                                        zi->ci.buffered_data[i],t);
920 #endif
921     }
922     if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
923                                                                     !=zi->ci.pos_in_buffered_data)
924       err = ZIP_ERRNO;
925     zi->ci.pos_in_buffered_data = 0;
926     return err;
927 }
928
929 extern int ZEXPORT zipWriteInFileInZip (
930     zipFile file,
931     const void* buf,
932     unsigned len)
933 {
934     zip_internal* zi;
935     int err=ZIP_OK;
936
937     if (file == NULL)
938         return ZIP_PARAMERROR;
939     zi = (zip_internal*)file;
940
941     if (zi->in_opened_file_inzip == 0)
942         return ZIP_PARAMERROR;
943
944     zi->ci.stream.next_in = (Bytef*)buf;
945     zi->ci.stream.avail_in = len;
946     zi->ci.crc32 = crc32(zi->ci.crc32,(Bytef*)buf,len);
947
948     while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
949     {
950         if (zi->ci.stream.avail_out == 0)
951         {
952             if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
953                 err = ZIP_ERRNO;
954             zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
955             zi->ci.stream.next_out = zi->ci.buffered_data;
956         }
957
958
959         if(err != ZIP_OK)
960             break;
961
962         if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
963         {
964             uLong uTotalOutBefore = zi->ci.stream.total_out;
965             err=deflate(&zi->ci.stream,  Z_NO_FLUSH);
966             zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
967
968         }
969         else
970         {
971             uInt copy_this,i;
972             if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
973                 copy_this = zi->ci.stream.avail_in;
974             else
975                 copy_this = zi->ci.stream.avail_out;
976             for (i=0;i<copy_this;i++)
977                 *(((char*)zi->ci.stream.next_out)+i) =
978                     *(((const char*)zi->ci.stream.next_in)+i);
979             {
980                 zi->ci.stream.avail_in -= copy_this;
981                 zi->ci.stream.avail_out-= copy_this;
982                 zi->ci.stream.next_in+= copy_this;
983                 zi->ci.stream.next_out+= copy_this;
984                 zi->ci.stream.total_in+= copy_this;
985                 zi->ci.stream.total_out+= copy_this;
986                 zi->ci.pos_in_buffered_data += copy_this;
987             }
988         }
989     }
990
991     return err;
992 }
993
994 extern int ZEXPORT zipCloseFileInZipRaw (
995     zipFile file,
996     uLong uncompressed_size,
997     uLong crc32)
998 {
999     zip_internal* zi;
1000     uLong compressed_size;
1001     int err=ZIP_OK;
1002
1003     if (file == NULL)
1004         return ZIP_PARAMERROR;
1005     zi = (zip_internal*)file;
1006
1007     if (zi->in_opened_file_inzip == 0)
1008         return ZIP_PARAMERROR;
1009     zi->ci.stream.avail_in = 0;
1010
1011     if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1012         while (err==ZIP_OK)
1013     {
1014         uLong uTotalOutBefore;
1015         if (zi->ci.stream.avail_out == 0)
1016         {
1017             if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
1018                 err = ZIP_ERRNO;
1019             zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1020             zi->ci.stream.next_out = zi->ci.buffered_data;
1021         }
1022         uTotalOutBefore = zi->ci.stream.total_out;
1023         err=deflate(&zi->ci.stream,  Z_FINISH);
1024         zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1025     }
1026
1027     if (err==Z_STREAM_END)
1028         err=ZIP_OK; /* this is normal */
1029
1030     if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1031         if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
1032             err = ZIP_ERRNO;
1033
1034     if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1035     {
1036         err=deflateEnd(&zi->ci.stream);
1037         zi->ci.stream_initialised = 0;
1038     }
1039
1040     if (!zi->ci.raw)
1041     {
1042         crc32 = (uLong)zi->ci.crc32;
1043         uncompressed_size = (uLong)zi->ci.stream.total_in;
1044     }
1045     compressed_size = (uLong)zi->ci.stream.total_out;
1046 #    ifndef NOCRYPT
1047     compressed_size += zi->ci.crypt_header_size;
1048 #    endif
1049
1050     ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1051     ziplocal_putValue_inmemory(zi->ci.central_header+20,
1052                                 compressed_size,4); /*compr size*/
1053     if (zi->ci.stream.data_type == Z_ASCII)
1054         ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1055     ziplocal_putValue_inmemory(zi->ci.central_header+24,
1056                                 uncompressed_size,4); /*uncompr size*/
1057
1058     if (err==ZIP_OK)
1059         err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
1060                                        (uLong)zi->ci.size_centralheader);
1061     free(zi->ci.central_header);
1062
1063     if (err==ZIP_OK)
1064     {
1065         long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1066         if (ZSEEK(zi->z_filefunc,zi->filestream,
1067                   zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1068             err = ZIP_ERRNO;
1069
1070         if (err==ZIP_OK)
1071             err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1072
1073         if (err==ZIP_OK) /* compressed size, unknown */
1074             err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1075
1076         if (err==ZIP_OK) /* uncompressed size, unknown */
1077             err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1078
1079         if (ZSEEK(zi->z_filefunc,zi->filestream,
1080                   cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1081             err = ZIP_ERRNO;
1082     }
1083
1084     zi->number_entry ++;
1085     zi->in_opened_file_inzip = 0;
1086
1087     return err;
1088 }
1089
1090 extern int ZEXPORT zipCloseFileInZip (zipFile file)
1091 {
1092     return zipCloseFileInZipRaw (file,0,0);
1093 }
1094
1095 extern int ZEXPORT zipClose (zipFile file,
1096     const char* global_comment)
1097 {
1098     zip_internal* zi;
1099     int err = 0;
1100     uLong size_centraldir = 0;
1101     uLong centraldir_pos_inzip;
1102     uInt size_global_comment;
1103     if (file == NULL)
1104         return ZIP_PARAMERROR;
1105     zi = (zip_internal*)file;
1106
1107     if (zi->in_opened_file_inzip == 1)
1108     {
1109         err = zipCloseFileInZip (file);
1110     }
1111
1112 #ifndef NO_ADDFILEINEXISTINGZIP
1113     if (global_comment==NULL)
1114         global_comment = zi->globalcomment;
1115 #endif
1116     if (global_comment==NULL)
1117         size_global_comment = 0;
1118     else
1119         size_global_comment = (uInt)strlen(global_comment);
1120
1121     centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1122     if (err==ZIP_OK)
1123     {
1124         linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
1125         while (ldi!=NULL)
1126         {
1127             if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1128                 if (ZWRITE(zi->z_filefunc,zi->filestream,
1129                            ldi->data,ldi->filled_in_this_block)
1130                               !=ldi->filled_in_this_block )
1131                     err = ZIP_ERRNO;
1132
1133             size_centraldir += ldi->filled_in_this_block;
1134             ldi = ldi->next_datablock;
1135         }
1136     }
1137     free_datablock(zi->central_dir.first_block);
1138
1139     if (err==ZIP_OK) /* Magic End */
1140         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1141
1142     if (err==ZIP_OK) /* number of this disk */
1143         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1144
1145     if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1146         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1147
1148     if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1149         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1150
1151     if (err==ZIP_OK) /* total number of entries in the central dir */
1152         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1153
1154     if (err==ZIP_OK) /* size of the central directory */
1155         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1156
1157     if (err==ZIP_OK) /* offset of start of central directory with respect to the
1158                             starting disk number */
1159         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
1160                                 (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1161
1162     if (err==ZIP_OK) /* zipfile comment length */
1163         err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1164
1165     if ((err==ZIP_OK) && (size_global_comment>0))
1166         if (ZWRITE(zi->z_filefunc,zi->filestream,
1167                    global_comment,size_global_comment) != size_global_comment)
1168                 err = ZIP_ERRNO;
1169
1170     if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
1171         if (err == ZIP_OK)
1172             err = ZIP_ERRNO;
1173
1174 #ifndef NO_ADDFILEINEXISTINGZIP
1175     TRYFREE(zi->globalcomment);
1176 #endif
1177     TRYFREE(zi);
1178
1179     return err;
1180 }