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
allocate_new_datablock()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
free_datablock(linkedlist_datablock_internal * ldi)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
init_linkedlist(linkedlist_data * ll)185 local void init_linkedlist(linkedlist_data* ll)
186 {
187 ll->first_block = ll->last_block = NULL;
188 }
189
190 /* AKT: not used
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 */
197
add_data_in_datablock(linkedlist_data * ll,const void * buf,uLong len)198 local int add_data_in_datablock(
199 linkedlist_data* ll,
200 const void* buf,
201 uLong len)
202 {
203 linkedlist_datablock_internal* ldi;
204 const unsigned char* from_copy;
205
206 if (ll==NULL)
207 return ZIP_INTERNALERROR;
208
209 if (ll->last_block == NULL)
210 {
211 ll->first_block = ll->last_block = allocate_new_datablock();
212 if (ll->first_block == NULL)
213 return ZIP_INTERNALERROR;
214 }
215
216 ldi = ll->last_block;
217 from_copy = (unsigned char*)buf;
218
219 while (len>0)
220 {
221 uInt copy_this;
222 uInt i;
223 unsigned char* to_copy;
224
225 if (ldi->avail_in_this_block==0)
226 {
227 ldi->next_datablock = allocate_new_datablock();
228 if (ldi->next_datablock == NULL)
229 return ZIP_INTERNALERROR;
230 ldi = ldi->next_datablock ;
231 ll->last_block = ldi;
232 }
233
234 if (ldi->avail_in_this_block < len)
235 copy_this = (uInt)ldi->avail_in_this_block;
236 else
237 copy_this = (uInt)len;
238
239 to_copy = &(ldi->data[ldi->filled_in_this_block]);
240
241 for (i=0;i<copy_this;i++)
242 *(to_copy+i)=*(from_copy+i);
243
244 ldi->filled_in_this_block += copy_this;
245 ldi->avail_in_this_block -= copy_this;
246 from_copy += copy_this ;
247 len -= copy_this;
248 }
249 return ZIP_OK;
250 }
251
252
253
254 /****************************************************************************/
255
256 #ifndef NO_ADDFILEINEXISTINGZIP
257 /* ===========================================================================
258 Inputs a long in LSB order to the given file
259 nbByte == 1, 2 or 4 (byte, short or long)
260 */
261
262 local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
263 voidpf filestream, uLong x, int nbByte));
ziplocal_putValue(const zlib_filefunc_def * pzlib_filefunc_def,voidpf filestream,uLong x,int nbByte)264 local int ziplocal_putValue (
265 const zlib_filefunc_def* pzlib_filefunc_def,
266 voidpf filestream,
267 uLong x,
268 int nbByte)
269 {
270 unsigned char buf[4];
271 int n;
272 for (n = 0; n < nbByte; n++)
273 {
274 buf[n] = (unsigned char)(x & 0xff);
275 x >>= 8;
276 }
277 if (x != 0)
278 { /* data overflow - hack for ZIP64 (X Roche) */
279 for (n = 0; n < nbByte; n++)
280 {
281 buf[n] = 0xff;
282 }
283 }
284
285 if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
286 return ZIP_ERRNO;
287 else
288 return ZIP_OK;
289 }
290
291 local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
ziplocal_putValue_inmemory(void * dest,uLong x,int nbByte)292 local void ziplocal_putValue_inmemory (
293 void* dest,
294 uLong x,
295 int nbByte)
296 {
297 unsigned char* buf=(unsigned char*)dest;
298 int n;
299 for (n = 0; n < nbByte; n++) {
300 buf[n] = (unsigned char)(x & 0xff);
301 x >>= 8;
302 }
303
304 if (x != 0)
305 { /* data overflow - hack for ZIP64 */
306 for (n = 0; n < nbByte; n++)
307 {
308 buf[n] = 0xff;
309 }
310 }
311 }
312
313 /****************************************************************************/
314
315
ziplocal_TmzDateToDosDate(const tm_zip * ptm,uLong dosDate)316 local uLong ziplocal_TmzDateToDosDate(
317 const tm_zip* ptm,
318 uLong dosDate)
319 {
320 uLong year = (uLong)ptm->tm_year;
321 if (year>1980)
322 year-=1980;
323 else if (year>80)
324 year-=80;
325 return
326 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
327 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
328 }
329
330
331 /****************************************************************************/
332
333 local int ziplocal_getByte OF((
334 const zlib_filefunc_def* pzlib_filefunc_def,
335 voidpf filestream,
336 int *pi));
337
ziplocal_getByte(const zlib_filefunc_def * pzlib_filefunc_def,voidpf filestream,int * pi)338 local int ziplocal_getByte(
339 const zlib_filefunc_def* pzlib_filefunc_def,
340 voidpf filestream,
341 int *pi)
342 {
343 unsigned char c;
344 int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
345 if (err==1)
346 {
347 *pi = (int)c;
348 return ZIP_OK;
349 }
350 else
351 {
352 if (ZERROR(*pzlib_filefunc_def,filestream))
353 return ZIP_ERRNO;
354 else
355 return ZIP_EOF;
356 }
357 }
358
359
360 /* ===========================================================================
361 Reads a long in LSB order from the given gz_stream. Sets
362 */
363 local int ziplocal_getShort OF((
364 const zlib_filefunc_def* pzlib_filefunc_def,
365 voidpf filestream,
366 uLong *pX));
367
ziplocal_getShort(const zlib_filefunc_def * pzlib_filefunc_def,voidpf filestream,uLong * pX)368 local int ziplocal_getShort (
369 const zlib_filefunc_def* pzlib_filefunc_def,
370 voidpf filestream,
371 uLong *pX)
372 {
373 uLong x ;
374 int i;
375 int err;
376
377 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
378 x = (uLong)i;
379
380 if (err==ZIP_OK)
381 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
382 x += ((uLong)i)<<8;
383
384 if (err==ZIP_OK)
385 *pX = x;
386 else
387 *pX = 0;
388 return err;
389 }
390
391 local int ziplocal_getLong OF((
392 const zlib_filefunc_def* pzlib_filefunc_def,
393 voidpf filestream,
394 uLong *pX));
395
ziplocal_getLong(const zlib_filefunc_def * pzlib_filefunc_def,voidpf filestream,uLong * pX)396 local int ziplocal_getLong (
397 const zlib_filefunc_def* pzlib_filefunc_def,
398 voidpf filestream,
399 uLong *pX)
400 {
401 uLong x ;
402 int i;
403 int err;
404
405 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
406 x = (uLong)i;
407
408 if (err==ZIP_OK)
409 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
410 x += ((uLong)i)<<8;
411
412 if (err==ZIP_OK)
413 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
414 x += ((uLong)i)<<16;
415
416 if (err==ZIP_OK)
417 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
418 x += ((uLong)i)<<24;
419
420 if (err==ZIP_OK)
421 *pX = x;
422 else
423 *pX = 0;
424 return err;
425 }
426
427 #ifndef BUFREADCOMMENT
428 #define BUFREADCOMMENT (0x400)
429 #endif
430 /*
431 Locate the Central directory of a zipfile (at the end, just before
432 the global comment)
433 */
434 local uLong ziplocal_SearchCentralDir OF((
435 const zlib_filefunc_def* pzlib_filefunc_def,
436 voidpf filestream));
437
ziplocal_SearchCentralDir(const zlib_filefunc_def * pzlib_filefunc_def,voidpf filestream)438 local uLong ziplocal_SearchCentralDir(
439 const zlib_filefunc_def* pzlib_filefunc_def,
440 voidpf filestream)
441 {
442 unsigned char* buf;
443 uLong uSizeFile;
444 uLong uBackRead;
445 uLong uMaxBack=0xffff; /* maximum size of global comment */
446 uLong uPosFound=0;
447
448 if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
449 return 0;
450
451
452 uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
453
454 if (uMaxBack>uSizeFile)
455 uMaxBack = uSizeFile;
456
457 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
458 if (buf==NULL)
459 return 0;
460
461 uBackRead = 4;
462 while (uBackRead<uMaxBack)
463 {
464 uLong uReadSize,uReadPos ;
465 int i;
466 if (uBackRead+BUFREADCOMMENT>uMaxBack)
467 uBackRead = uMaxBack;
468 else
469 uBackRead+=BUFREADCOMMENT;
470 uReadPos = uSizeFile-uBackRead ;
471
472 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
473 (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
474 if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
475 break;
476
477 if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
478 break;
479
480 for (i=(int)uReadSize-3; (i--)>0;)
481 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
482 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
483 {
484 uPosFound = uReadPos+i;
485 break;
486 }
487
488 if (uPosFound!=0)
489 break;
490 }
491 TRYFREE(buf);
492 return uPosFound;
493 }
494 #endif /* !NO_ADDFILEINEXISTINGZIP*/
495
496 /************************************************************/
zipOpen2(const char * pathname,int append,zipcharpc * globalcomment,zlib_filefunc_def * pzlib_filefunc_def)497 extern zipFile ZEXPORT zipOpen2 (
498 const char *pathname,
499 int append,
500 zipcharpc* globalcomment,
501 zlib_filefunc_def* pzlib_filefunc_def)
502 {
503 zip_internal ziinit;
504 zip_internal* zi;
505 int err=ZIP_OK;
506
507
508 if (pzlib_filefunc_def==NULL)
509 fill_fopen_filefunc(&ziinit.z_filefunc);
510 else
511 ziinit.z_filefunc = *pzlib_filefunc_def;
512
513 ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
514 (ziinit.z_filefunc.opaque,
515 pathname,
516 (append == APPEND_STATUS_CREATE) ?
517 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
518 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
519
520 if (ziinit.filestream == NULL)
521 return NULL;
522 ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
523 ziinit.in_opened_file_inzip = 0;
524 ziinit.ci.stream_initialised = 0;
525 ziinit.number_entry = 0;
526 ziinit.add_position_when_writting_offset = 0;
527 init_linkedlist(&(ziinit.central_dir));
528
529
530 zi = (zip_internal*)ALLOC(sizeof(zip_internal));
531 if (zi==NULL)
532 {
533 ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
534 return NULL;
535 }
536
537 /* now we add file in a zipfile */
538 # ifndef NO_ADDFILEINEXISTINGZIP
539 ziinit.globalcomment = NULL;
540 if (append == APPEND_STATUS_ADDINZIP)
541 {
542 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
543
544 uLong size_central_dir; /* size of the central directory */
545 uLong offset_central_dir; /* offset of start of central directory */
546 uLong central_pos,uL;
547
548 uLong number_disk; /* number of the current dist, used for
549 spaning ZIP, unsupported, always 0*/
550 uLong number_disk_with_CD; /* number the the disk with central dir, used
551 for spaning ZIP, unsupported, always 0*/
552 uLong number_entry;
553 uLong number_entry_CD; /* total number of entries in
554 the central dir
555 (same than number_entry on nospan) */
556 uLong size_comment;
557
558 central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
559 if (central_pos==0)
560 err=ZIP_ERRNO;
561
562 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
563 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
564 err=ZIP_ERRNO;
565
566 /* the signature, already checked */
567 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
568 err=ZIP_ERRNO;
569
570 /* number of this disk */
571 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
572 err=ZIP_ERRNO;
573
574 /* number of the disk with the start of the central directory */
575 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
576 err=ZIP_ERRNO;
577
578 /* total number of entries in the central dir on this disk */
579 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
580 err=ZIP_ERRNO;
581
582 /* total number of entries in the central dir */
583 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
584 err=ZIP_ERRNO;
585
586 if ((number_entry_CD!=number_entry) ||
587 (number_disk_with_CD!=0) ||
588 (number_disk!=0))
589 err=ZIP_BADZIPFILE;
590
591 /* size of the central directory */
592 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
593 err=ZIP_ERRNO;
594
595 /* offset of start of central directory with respect to the
596 starting disk number */
597 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
598 err=ZIP_ERRNO;
599
600 /* zipfile global comment length */
601 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
602 err=ZIP_ERRNO;
603
604 if ((central_pos<offset_central_dir+size_central_dir) &&
605 (err==ZIP_OK))
606 err=ZIP_BADZIPFILE;
607
608 if (err!=ZIP_OK)
609 {
610 ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
611 return NULL;
612 }
613
614 if (size_comment>0)
615 {
616 // AKT added (char*)
617 ziinit.globalcomment = (char*) ALLOC(size_comment+1);
618 if (ziinit.globalcomment)
619 {
620 size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
621 ziinit.globalcomment[size_comment]=0;
622 }
623 }
624
625 byte_before_the_zipfile = central_pos -
626 (offset_central_dir+size_central_dir);
627 ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
628
629 {
630 uLong size_central_dir_to_read = size_central_dir;
631 size_t buf_size = SIZEDATA_INDATABLOCK;
632 void* buf_read = (void*)ALLOC(buf_size);
633 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
634 offset_central_dir + byte_before_the_zipfile,
635 ZLIB_FILEFUNC_SEEK_SET) != 0)
636 err=ZIP_ERRNO;
637
638 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
639 {
640 uLong read_this = SIZEDATA_INDATABLOCK;
641 if (read_this > size_central_dir_to_read)
642 read_this = size_central_dir_to_read;
643 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
644 err=ZIP_ERRNO;
645
646 if (err==ZIP_OK)
647 err = add_data_in_datablock(&ziinit.central_dir,buf_read,
648 (uLong)read_this);
649 size_central_dir_to_read-=read_this;
650 }
651 TRYFREE(buf_read);
652 }
653 ziinit.begin_pos = byte_before_the_zipfile;
654 ziinit.number_entry = number_entry_CD;
655
656 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
657 offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
658 err=ZIP_ERRNO;
659 }
660
661 if (globalcomment)
662 {
663 *globalcomment = ziinit.globalcomment;
664 }
665 # endif /* !NO_ADDFILEINEXISTINGZIP*/
666
667 if (err != ZIP_OK)
668 {
669 # ifndef NO_ADDFILEINEXISTINGZIP
670 TRYFREE(ziinit.globalcomment);
671 # endif /* !NO_ADDFILEINEXISTINGZIP*/
672 TRYFREE(zi);
673 return NULL;
674 }
675 else
676 {
677 *zi = ziinit;
678 return (zipFile)zi;
679 }
680 }
681
zipOpen(const char * pathname,int append)682 extern zipFile ZEXPORT zipOpen (
683 const char *pathname,
684 int append)
685 {
686 return zipOpen2(pathname,append,NULL,NULL);
687 }
688
zipOpenNewFileInZip3(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)689 extern int ZEXPORT zipOpenNewFileInZip3 (
690 zipFile file,
691 const char* filename,
692 const zip_fileinfo* zipfi,
693 const void* extrafield_local,
694 uInt size_extrafield_local,
695 const void* extrafield_global,
696 uInt size_extrafield_global,
697 const char* comment,
698 int method,
699 int level,
700 int raw,
701 int windowBits,
702 int memLevel,
703 int strategy,
704 const char* password,
705 uLong crcForCrypting)
706 {
707 zip_internal* zi;
708 uInt size_filename;
709 uInt size_comment;
710 uInt i;
711 int err = ZIP_OK;
712
713 # ifdef NOCRYPT
714 if (password != NULL)
715 return ZIP_PARAMERROR;
716 # endif
717
718 if (file == NULL)
719 return ZIP_PARAMERROR;
720 if ((method!=0) && (method!=Z_DEFLATED))
721 return ZIP_PARAMERROR;
722
723 zi = (zip_internal*)file;
724
725 if (zi->in_opened_file_inzip == 1)
726 {
727 err = zipCloseFileInZip (file);
728 if (err != ZIP_OK)
729 return err;
730 }
731
732
733 if (filename==NULL)
734 filename="-";
735
736 if (comment==NULL)
737 size_comment = 0;
738 else
739 size_comment = (uInt)strlen(comment);
740
741 size_filename = (uInt)strlen(filename);
742
743 if (zipfi == NULL)
744 zi->ci.dosDate = 0;
745 else
746 {
747 if (zipfi->dosDate != 0)
748 zi->ci.dosDate = zipfi->dosDate;
749 else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
750 }
751
752 zi->ci.flag = 0;
753 if ((level==8) || (level==9))
754 zi->ci.flag |= 2;
755 if (level==2)
756 zi->ci.flag |= 4;
757 if (level==1)
758 zi->ci.flag |= 6;
759 if (password != NULL)
760 zi->ci.flag |= 1;
761
762 zi->ci.crc32 = 0;
763 zi->ci.method = method;
764 zi->ci.encrypt = 0;
765 zi->ci.stream_initialised = 0;
766 zi->ci.pos_in_buffered_data = 0;
767 zi->ci.raw = raw;
768 zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
769 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
770 size_extrafield_global + size_comment;
771 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
772
773 ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
774 /* version info */
775 ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
776 ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
777 ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
778 ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
779 ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
780 ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
781 ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
782 ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
783 ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
784 ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
785 ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
786 ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
787
788 if (zipfi==NULL)
789 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
790 else
791 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
792
793 if (zipfi==NULL)
794 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
795 else
796 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
797
798 ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
799
800 for (i=0;i<size_filename;i++)
801 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
802
803 for (i=0;i<size_extrafield_global;i++)
804 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
805 *(((const char*)extrafield_global)+i);
806
807 for (i=0;i<size_comment;i++)
808 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
809 size_extrafield_global+i) = *(comment+i);
810 if (zi->ci.central_header == NULL)
811 return ZIP_INTERNALERROR;
812
813 /* write the local header */
814 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
815
816 if (err==ZIP_OK)
817 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
818 if (err==ZIP_OK)
819 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
820
821 if (err==ZIP_OK)
822 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
823
824 if (err==ZIP_OK)
825 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
826
827 if (err==ZIP_OK)
828 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
829 if (err==ZIP_OK)
830 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
831 if (err==ZIP_OK)
832 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
833
834 if (err==ZIP_OK)
835 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
836
837 if (err==ZIP_OK)
838 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
839
840 if ((err==ZIP_OK) && (size_filename>0))
841 if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
842 err = ZIP_ERRNO;
843
844 if ((err==ZIP_OK) && (size_extrafield_local>0))
845 if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
846 !=size_extrafield_local)
847 err = ZIP_ERRNO;
848
849 zi->ci.stream.avail_in = (uInt)0;
850 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
851 zi->ci.stream.next_out = zi->ci.buffered_data;
852 zi->ci.stream.total_in = 0;
853 zi->ci.stream.total_out = 0;
854
855 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
856 {
857 zi->ci.stream.zalloc = (alloc_func)0;
858 zi->ci.stream.zfree = (free_func)0;
859 zi->ci.stream.opaque = (voidpf)0;
860
861 if (windowBits>0)
862 windowBits = -windowBits;
863
864 err = deflateInit2(&zi->ci.stream, level,
865 Z_DEFLATED, windowBits, memLevel, strategy);
866
867 if (err==Z_OK)
868 zi->ci.stream_initialised = 1;
869 }
870 # ifndef NOCRYPT
871 zi->ci.crypt_header_size = 0;
872 if ((err==Z_OK) && (password != NULL))
873 {
874 unsigned char bufHead[RAND_HEAD_LEN];
875 unsigned int sizeHead;
876 zi->ci.encrypt = 1;
877 zi->ci.pcrc_32_tab = (const unsigned long *)get_crc_table();
878 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
879
880 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
881 zi->ci.crypt_header_size = sizeHead;
882
883 if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
884 err = ZIP_ERRNO;
885 }
886 # endif
887
888 if (err==Z_OK)
889 zi->in_opened_file_inzip = 1;
890 return err;
891 }
892
zipOpenNewFileInZip2(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)893 extern int ZEXPORT zipOpenNewFileInZip2(
894 zipFile file,
895 const char* filename,
896 const zip_fileinfo* zipfi,
897 const void* extrafield_local,
898 uInt size_extrafield_local,
899 const void* extrafield_global,
900 uInt size_extrafield_global,
901 const char* comment,
902 int method,
903 int level,
904 int raw)
905 {
906 return zipOpenNewFileInZip3 (file, filename, zipfi,
907 extrafield_local, size_extrafield_local,
908 extrafield_global, size_extrafield_global,
909 comment, method, level, raw,
910 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
911 NULL, 0);
912 }
913
zipOpenNewFileInZip(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)914 extern int ZEXPORT zipOpenNewFileInZip (
915 zipFile file,
916 const char* filename,
917 const zip_fileinfo* zipfi,
918 const void* extrafield_local,
919 uInt size_extrafield_local,
920 const void* extrafield_global,
921 uInt size_extrafield_global,
922 const char* comment,
923 int method,
924 int level)
925 {
926 return zipOpenNewFileInZip2 (file, filename, zipfi,
927 extrafield_local, size_extrafield_local,
928 extrafield_global, size_extrafield_global,
929 comment, method, level, 0);
930 }
931
zipFlushWriteBuffer(zip_internal * zi)932 local int zipFlushWriteBuffer(zip_internal* zi)
933 {
934 int err=ZIP_OK;
935
936 if (zi->ci.encrypt != 0)
937 {
938 #ifndef NOCRYPT
939 uInt i;
940 int t;
941 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
942 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
943 zi->ci.buffered_data[i],t);
944 #endif
945 }
946 if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
947 !=zi->ci.pos_in_buffered_data)
948 err = ZIP_ERRNO;
949 zi->ci.pos_in_buffered_data = 0;
950 return err;
951 }
952
zipWriteInFileInZip(zipFile file,const void * buf,unsigned len)953 extern int ZEXPORT zipWriteInFileInZip (
954 zipFile file,
955 const void* buf,
956 unsigned len)
957 {
958 zip_internal* zi;
959 int err=ZIP_OK;
960
961 if (file == NULL)
962 return ZIP_PARAMERROR;
963 zi = (zip_internal*)file;
964
965 if (zi->in_opened_file_inzip == 0)
966 return ZIP_PARAMERROR;
967
968 zi->ci.stream.next_in = (Bytef*)buf; // AKT was (void*)buf;
969 zi->ci.stream.avail_in = len;
970 zi->ci.crc32 = crc32(zi->ci.crc32, (Bytef*)buf, len); // AKT added (Bytef*)
971
972 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
973 {
974 if (zi->ci.stream.avail_out == 0)
975 {
976 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
977 err = ZIP_ERRNO;
978 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
979 zi->ci.stream.next_out = zi->ci.buffered_data;
980 }
981
982
983 if(err != ZIP_OK)
984 break;
985
986 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
987 {
988 uLong uTotalOutBefore = zi->ci.stream.total_out;
989 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
990 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
991
992 }
993 else
994 {
995 uInt copy_this,i;
996 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
997 copy_this = zi->ci.stream.avail_in;
998 else
999 copy_this = zi->ci.stream.avail_out;
1000 for (i=0;i<copy_this;i++)
1001 *(((char*)zi->ci.stream.next_out)+i) =
1002 *(((const char*)zi->ci.stream.next_in)+i);
1003 {
1004 zi->ci.stream.avail_in -= copy_this;
1005 zi->ci.stream.avail_out-= copy_this;
1006 zi->ci.stream.next_in+= copy_this;
1007 zi->ci.stream.next_out+= copy_this;
1008 zi->ci.stream.total_in+= copy_this;
1009 zi->ci.stream.total_out+= copy_this;
1010 zi->ci.pos_in_buffered_data += copy_this;
1011 }
1012 }
1013 }
1014
1015 return err;
1016 }
1017
zipCloseFileInZipRaw(zipFile file,uLong uncompressed_size,uLong crc32)1018 extern int ZEXPORT zipCloseFileInZipRaw (
1019 zipFile file,
1020 uLong uncompressed_size,
1021 uLong crc32)
1022 {
1023 zip_internal* zi;
1024 uLong compressed_size;
1025 int err=ZIP_OK;
1026
1027 if (file == NULL)
1028 return ZIP_PARAMERROR;
1029 zi = (zip_internal*)file;
1030
1031 if (zi->in_opened_file_inzip == 0)
1032 return ZIP_PARAMERROR;
1033 zi->ci.stream.avail_in = 0;
1034
1035 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1036 while (err==ZIP_OK)
1037 {
1038 uLong uTotalOutBefore;
1039 if (zi->ci.stream.avail_out == 0)
1040 {
1041 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
1042 err = ZIP_ERRNO;
1043 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1044 zi->ci.stream.next_out = zi->ci.buffered_data;
1045 }
1046 uTotalOutBefore = zi->ci.stream.total_out;
1047 err=deflate(&zi->ci.stream, Z_FINISH);
1048 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1049 }
1050
1051 if (err==Z_STREAM_END)
1052 err=ZIP_OK; /* this is normal */
1053
1054 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1055 if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
1056 err = ZIP_ERRNO;
1057
1058 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1059 {
1060 err=deflateEnd(&zi->ci.stream);
1061 zi->ci.stream_initialised = 0;
1062 }
1063
1064 if (!zi->ci.raw)
1065 {
1066 crc32 = (uLong)zi->ci.crc32;
1067 uncompressed_size = (uLong)zi->ci.stream.total_in;
1068 }
1069 compressed_size = (uLong)zi->ci.stream.total_out;
1070 # ifndef NOCRYPT
1071 compressed_size += zi->ci.crypt_header_size;
1072 # endif
1073
1074 ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1075 ziplocal_putValue_inmemory(zi->ci.central_header+20,
1076 compressed_size,4); /*compr size*/
1077 if (zi->ci.stream.data_type == Z_ASCII)
1078 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1079 ziplocal_putValue_inmemory(zi->ci.central_header+24,
1080 uncompressed_size,4); /*uncompr size*/
1081
1082 if (err==ZIP_OK)
1083 err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
1084 (uLong)zi->ci.size_centralheader);
1085 free(zi->ci.central_header);
1086
1087 if (err==ZIP_OK)
1088 {
1089 long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1090 if (ZSEEK(zi->z_filefunc,zi->filestream,
1091 zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1092 err = ZIP_ERRNO;
1093
1094 if (err==ZIP_OK)
1095 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1096
1097 if (err==ZIP_OK) /* compressed size, unknown */
1098 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1099
1100 if (err==ZIP_OK) /* uncompressed size, unknown */
1101 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1102
1103 if (ZSEEK(zi->z_filefunc,zi->filestream,
1104 cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1105 err = ZIP_ERRNO;
1106 }
1107
1108 zi->number_entry ++;
1109 zi->in_opened_file_inzip = 0;
1110
1111 return err;
1112 }
1113
zipCloseFileInZip(zipFile file)1114 extern int ZEXPORT zipCloseFileInZip (zipFile file)
1115 {
1116 return zipCloseFileInZipRaw (file,0,0);
1117 }
1118
zipClose(zipFile file,const char * global_comment)1119 extern int ZEXPORT zipClose (
1120 zipFile file,
1121 const char* global_comment)
1122 {
1123 zip_internal* zi;
1124 int err = 0;
1125 uLong size_centraldir = 0;
1126 uLong centraldir_pos_inzip;
1127 uInt size_global_comment;
1128 if (file == NULL)
1129 return ZIP_PARAMERROR;
1130 zi = (zip_internal*)file;
1131
1132 if (zi->in_opened_file_inzip == 1)
1133 {
1134 err = zipCloseFileInZip (file);
1135 }
1136
1137 #ifndef NO_ADDFILEINEXISTINGZIP
1138 if (global_comment==NULL)
1139 global_comment = zi->globalcomment;
1140 #endif
1141 if (global_comment==NULL)
1142 size_global_comment = 0;
1143 else
1144 size_global_comment = (uInt)strlen(global_comment);
1145
1146 centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1147 if (err==ZIP_OK)
1148 {
1149 linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
1150 while (ldi!=NULL)
1151 {
1152 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1153 if (ZWRITE(zi->z_filefunc,zi->filestream,
1154 ldi->data,ldi->filled_in_this_block)
1155 !=ldi->filled_in_this_block )
1156 err = ZIP_ERRNO;
1157
1158 size_centraldir += ldi->filled_in_this_block;
1159 ldi = ldi->next_datablock;
1160 }
1161 }
1162 free_datablock(zi->central_dir.first_block);
1163
1164 if (err==ZIP_OK) /* Magic End */
1165 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1166
1167 if (err==ZIP_OK) /* number of this disk */
1168 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1169
1170 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1171 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1172
1173 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1174 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1175
1176 if (err==ZIP_OK) /* total number of entries in the central dir */
1177 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1178
1179 if (err==ZIP_OK) /* size of the central directory */
1180 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1181
1182 if (err==ZIP_OK) /* offset of start of central directory with respect to the
1183 starting disk number */
1184 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
1185 (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1186
1187 if (err==ZIP_OK) /* zipfile comment length */
1188 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1189
1190 if ((err==ZIP_OK) && (size_global_comment>0))
1191 if (ZWRITE(zi->z_filefunc,zi->filestream,
1192 global_comment,size_global_comment) != size_global_comment)
1193 err = ZIP_ERRNO;
1194
1195 if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
1196 if (err == ZIP_OK)
1197 err = ZIP_ERRNO;
1198
1199 #ifndef NO_ADDFILEINEXISTINGZIP
1200 TRYFREE(zi->globalcomment);
1201 #endif
1202 TRYFREE(zi);
1203
1204 return err;
1205 }
1206