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 <stdlib.h>
23 #endif
24 #ifdef NO_ERRNO_H
25 extern int errno;
26 #else
27 # include <errno.h>
28 #endif
29
30
31 #ifndef local
32 # define local static
33 #endif
34 /* compile with -Dlocal if your debugger can't find static symbols */
35
36 #ifndef VERSIONMADEBY
37 # define VERSIONMADEBY (0x0) /* platform depedent */
38 #endif
39
40 #ifndef Z_BUFSIZE
41 #define Z_BUFSIZE (16384)
42 #endif
43
44 #ifndef Z_MAXFILENAMEINZIP
45 #define Z_MAXFILENAMEINZIP (256)
46 #endif
47
48 #ifndef ALLOC
49 # define ALLOC(size) (malloc(size))
50 #endif
51 #ifndef TRYFREE
52 # define TRYFREE(p) {if (p) free(p);}
53 #endif
54
55 /*
56 #define SIZECENTRALDIRITEM (0x2e)
57 #define SIZEZIPLOCALHEADER (0x1e)
58 */
59
60 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
61
62 #ifndef SEEK_CUR
63 #define SEEK_CUR 1
64 #endif
65
66 #ifndef SEEK_END
67 #define SEEK_END 2
68 #endif
69
70 #ifndef SEEK_SET
71 #define SEEK_SET 0
72 #endif
73
74 #ifndef DEF_MEM_LEVEL
75 #if MAX_MEM_LEVEL >= 8
76 # define DEF_MEM_LEVEL 8
77 #else
78 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
79 #endif
80 #endif
81 const char zip_copyright[] =
82 " zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
83
84
85 #define SIZEDATA_INDATABLOCK (4096-(4*4))
86
87 #define LOCALHEADERMAGIC (0x04034b50)
88 #define CENTRALHEADERMAGIC (0x02014b50)
89 #define ENDHEADERMAGIC (0x06054b50)
90
91 #define FLAG_LOCALHEADER_OFFSET (0x06)
92 #define CRC_LOCALHEADER_OFFSET (0x0e)
93
94 #define SIZECENTRALHEADER (0x2e) /* 46 */
95
96 typedef struct linkedlist_datablock_internal_s
97 {
98 struct linkedlist_datablock_internal_s* next_datablock;
99 uLong avail_in_this_block;
100 uLong filled_in_this_block;
101 uLong unused; /* for future use and alignement */
102 unsigned char data[SIZEDATA_INDATABLOCK];
103 } linkedlist_datablock_internal;
104
105 typedef struct linkedlist_data_s
106 {
107 linkedlist_datablock_internal* first_block;
108 linkedlist_datablock_internal* last_block;
109 } linkedlist_data;
110
111
112 typedef struct
113 {
114 z_stream stream; /* zLib stream structure for inflate */
115 int stream_initialised; /* 1 is stream is initialised */
116 uInt pos_in_buffered_data; /* last written byte in buffered_data */
117
118 uLong pos_local_header; /* offset of the local header of the file
119 currenty writing */
120 char* central_header; /* central header data for the current file */
121 uLong size_centralheader; /* size of the central header for cur file */
122 uLong flag; /* flag of the file currently writing */
123
124 int method; /* compression method of file currenty wr.*/
125 int raw; /* 1 for directly writing raw data */
126 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
127 uLong dosDate;
128 uLong crc32;
129 int encrypt;
130 #ifndef NOCRYPT
131 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
132 const unsigned long* pcrc_32_tab;
133 int crypt_header_size;
134 #endif
135 } curfile_info;
136
137 typedef struct
138 {
139 zlib_filefunc_def z_filefunc;
140 voidpf filestream; /* io structore of the zipfile */
141 linkedlist_data central_dir;/* datablock with central dir in construction*/
142 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
143 curfile_info ci; /* info on the file curretly writing */
144
145 uLong begin_pos; /* position of the beginning of the zipfile */
146 uLong add_position_when_writting_offset;
147 uLong number_entry;
148 #ifndef NO_ADDFILEINEXISTINGZIP
149 char *globalcomment;
150 #endif
151 } zip_internal;
152
153
154 #ifndef NOCRYPT
155 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
156 #include "crypt.h"
157 #endif
158
allocate_new_datablock()159 local linkedlist_datablock_internal* allocate_new_datablock()
160 {
161 linkedlist_datablock_internal* ldi;
162 ldi = (linkedlist_datablock_internal*)
163 ALLOC(sizeof(linkedlist_datablock_internal));
164 if (ldi!=NULL)
165 {
166 ldi->next_datablock = NULL ;
167 ldi->filled_in_this_block = 0 ;
168 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
169 }
170 return ldi;
171 }
172
free_datablock(ldi)173 local void free_datablock(ldi)
174 linkedlist_datablock_internal* ldi;
175 {
176 while (ldi!=NULL)
177 {
178 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
179 TRYFREE(ldi);
180 ldi = ldinext;
181 }
182 }
183
init_linkedlist(ll)184 local void init_linkedlist(ll)
185 linkedlist_data* ll;
186 {
187 ll->first_block = ll->last_block = NULL;
188 }
189
190 #if 0 /* unused local function */
191 local void free_linkedlist(ll)
192 linkedlist_data* ll;
193 {
194 free_datablock(ll->first_block);
195 ll->first_block = ll->last_block = NULL;
196 }
197 #endif
198
add_data_in_datablock(ll,buf,len)199 local int add_data_in_datablock(ll,buf,len)
200 linkedlist_data* ll;
201 const void* buf;
202 uLong len;
203 {
204 linkedlist_datablock_internal* ldi;
205 const unsigned char* from_copy;
206
207 if (ll==NULL)
208 return ZIP_INTERNALERROR;
209
210 if (ll->last_block == NULL)
211 {
212 ll->first_block = ll->last_block = allocate_new_datablock();
213 if (ll->first_block == NULL)
214 return ZIP_INTERNALERROR;
215 }
216
217 ldi = ll->last_block;
218 from_copy = (unsigned char*)buf;
219
220 while (len>0)
221 {
222 uInt copy_this;
223 uInt i;
224 unsigned char* to_copy;
225
226 if (ldi->avail_in_this_block==0)
227 {
228 ldi->next_datablock = allocate_new_datablock();
229 if (ldi->next_datablock == NULL)
230 return ZIP_INTERNALERROR;
231 ldi = ldi->next_datablock ;
232 ll->last_block = ldi;
233 }
234
235 if (ldi->avail_in_this_block < len)
236 copy_this = (uInt)ldi->avail_in_this_block;
237 else
238 copy_this = (uInt)len;
239
240 to_copy = &(ldi->data[ldi->filled_in_this_block]);
241
242 for (i=0;i<copy_this;i++)
243 *(to_copy+i)=*(from_copy+i);
244
245 ldi->filled_in_this_block += copy_this;
246 ldi->avail_in_this_block -= copy_this;
247 from_copy += copy_this ;
248 len -= copy_this;
249 }
250 return ZIP_OK;
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(pzlib_filefunc_def,filestream,x,nbByte)264 local int ziplocal_putValue (pzlib_filefunc_def, filestream, x, nbByte)
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(dest,x,nbByte)292 local void ziplocal_putValue_inmemory (dest, x, nbByte)
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(ptm,dosDate)316 local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
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(pzlib_filefunc_def,filestream,pi)338 local int ziplocal_getByte(pzlib_filefunc_def,filestream,pi)
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(pzlib_filefunc_def,filestream,pX)368 local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX)
369 const zlib_filefunc_def* pzlib_filefunc_def;
370 voidpf filestream;
371 uLong *pX;
372 {
373 uLong x ;
374 int i=0;
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(pzlib_filefunc_def,filestream,pX)396 local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
397 const zlib_filefunc_def* pzlib_filefunc_def;
398 voidpf filestream;
399 uLong *pX;
400 {
401 uLong x ;
402 int i=0;
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(pzlib_filefunc_def,filestream)438 local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream)
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 /************************************************************/
497 extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc_def)
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 of 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 ziinit.globalcomment = ALLOC(size_comment+1);
617 if (ziinit.globalcomment)
618 {
619 size_comment = ZREAD(ziinit.z_filefunc, ziinit.filestream,ziinit.globalcomment,size_comment);
620 ziinit.globalcomment[size_comment]=0;
621 }
622 }
623
624 byte_before_the_zipfile = central_pos -
625 (offset_central_dir+size_central_dir);
626 ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
627
628 {
629 uLong size_central_dir_to_read = size_central_dir;
630 size_t buf_size = SIZEDATA_INDATABLOCK;
631 void* buf_read = (void*)ALLOC(buf_size);
632 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
633 offset_central_dir + byte_before_the_zipfile,
634 ZLIB_FILEFUNC_SEEK_SET) != 0)
635 err=ZIP_ERRNO;
636
637 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
638 {
639 uLong read_this = SIZEDATA_INDATABLOCK;
640 if (read_this > size_central_dir_to_read)
641 read_this = size_central_dir_to_read;
642 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
643 err=ZIP_ERRNO;
644
645 if (err==ZIP_OK)
646 err = add_data_in_datablock(&ziinit.central_dir,buf_read,
647 (uLong)read_this);
648 size_central_dir_to_read-=read_this;
649 }
650 TRYFREE(buf_read);
651 }
652 ziinit.begin_pos = byte_before_the_zipfile;
653 ziinit.number_entry = number_entry_CD;
654
655 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
656 offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
657 err=ZIP_ERRNO;
658 }
659
660 if (globalcomment)
661 {
662 *globalcomment = ziinit.globalcomment;
663 }
664 # endif /* !NO_ADDFILEINEXISTINGZIP*/
665
666 if (err != ZIP_OK)
667 {
668 # ifndef NO_ADDFILEINEXISTINGZIP
669 TRYFREE(ziinit.globalcomment);
670 # endif /* !NO_ADDFILEINEXISTINGZIP*/
671 TRYFREE(zi);
672 return NULL;
673 }
674 else
675 {
676 *zi = ziinit;
677 return (zipFile)zi;
678 }
679 }
680
681 extern zipFile ZEXPORT zipOpen (pathname, append)
682 const char *pathname;
683 int append;
684 {
685 return zipOpen2(pathname,append,NULL,NULL);
686 }
687
688 extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
689 extrafield_local, size_extrafield_local,
690 extrafield_global, size_extrafield_global,
691 comment, method, level, raw,
692 windowBits, memLevel, strategy,
693 password, crcForCrypting)
694 zipFile file;
695 const char* filename;
696 const zip_fileinfo* zipfi;
697 const void* extrafield_local;
698 uInt size_extrafield_local;
699 const void* extrafield_global;
700 uInt size_extrafield_global;
701 const char* comment;
702 int method;
703 int level;
704 int raw;
705 int windowBits;
706 int memLevel;
707 int strategy;
708 const char* password;
709 uLong crcForCrypting;
710 {
711 zip_internal* zi;
712 uInt size_filename;
713 uInt size_comment;
714 uInt i;
715 int err = ZIP_OK;
716
717 # ifdef NOCRYPT
718 if (password != NULL)
719 return ZIP_PARAMERROR;
720 # endif
721
722 if (file == NULL)
723 return ZIP_PARAMERROR;
724 if ((method!=0) && (method!=Z_DEFLATED))
725 return ZIP_PARAMERROR;
726
727 zi = (zip_internal*)file;
728
729 if (zi->in_opened_file_inzip == 1)
730 {
731 err = zipCloseFileInZip (file);
732 if (err != ZIP_OK)
733 return err;
734 }
735
736
737 if (filename==NULL)
738 filename="-";
739
740 if (comment==NULL)
741 size_comment = 0;
742 else
743 size_comment = (uInt)strlen(comment);
744
745 size_filename = (uInt)strlen(filename);
746
747 if (zipfi == NULL)
748 zi->ci.dosDate = 0;
749 else
750 {
751 if (zipfi->dosDate != 0)
752 zi->ci.dosDate = zipfi->dosDate;
753 else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
754 }
755
756 zi->ci.flag = 0;
757 if (level==8 || level==9)
758 zi->ci.flag |= 2;
759 if (level==2)
760 zi->ci.flag |= 4;
761 if (level==1)
762 zi->ci.flag |= 6;
763 if (password != NULL)
764 zi->ci.flag |= 1;
765
766 zi->ci.crc32 = 0;
767 zi->ci.method = method;
768 zi->ci.encrypt = 0;
769 zi->ci.stream_initialised = 0;
770 zi->ci.pos_in_buffered_data = 0;
771 zi->ci.raw = raw;
772 zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
773 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
774 size_extrafield_global + size_comment;
775 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
776
777 ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
778 /* version info */
779 ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
780 ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
781 ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
782 ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
783 ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
784 ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
785 ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
786 ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
787 ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
788 ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
789 ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
790 ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
791
792 if (zipfi==NULL)
793 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
794 else
795 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
796
797 if (zipfi==NULL)
798 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
799 else
800 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
801
802 ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
803
804 for (i=0;i<size_filename;i++)
805 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
806
807 for (i=0;i<size_extrafield_global;i++)
808 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
809 *(((const char*)extrafield_global)+i);
810
811 for (i=0;i<size_comment;i++)
812 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
813 size_extrafield_global+i) = *(comment+i);
814 if (zi->ci.central_header == NULL)
815 return ZIP_INTERNALERROR;
816
817 /* write the local header */
818 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
819
820 if (err==ZIP_OK)
821 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
822 if (err==ZIP_OK)
823 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
824
825 if (err==ZIP_OK)
826 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
827
828 if (err==ZIP_OK)
829 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
830
831 if (err==ZIP_OK)
832 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
833 if (err==ZIP_OK)
834 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
835 if (err==ZIP_OK)
836 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
837
838 if (err==ZIP_OK)
839 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
840
841 if (err==ZIP_OK)
842 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
843
844 if ((err==ZIP_OK) && (size_filename>0))
845 if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
846 err = ZIP_ERRNO;
847
848 if ((err==ZIP_OK) && (size_extrafield_local>0))
849 if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
850 !=size_extrafield_local)
851 err = ZIP_ERRNO;
852
853 zi->ci.stream.avail_in = (uInt)0;
854 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
855 zi->ci.stream.next_out = zi->ci.buffered_data;
856 zi->ci.stream.total_in = 0;
857 zi->ci.stream.total_out = 0;
858
859 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
860 {
861 zi->ci.stream.zalloc = (alloc_func)0;
862 zi->ci.stream.zfree = (free_func)0;
863 zi->ci.stream.opaque = (voidpf)0;
864
865 if (windowBits>0)
866 windowBits = -windowBits;
867
868 err = deflateInit2(&zi->ci.stream, level,
869 Z_DEFLATED, windowBits, memLevel, strategy);
870
871 if (err==Z_OK)
872 zi->ci.stream_initialised = 1;
873 }
874 # ifndef NOCRYPT
875 zi->ci.crypt_header_size = 0;
876 if ((err==Z_OK) && (password != NULL))
877 {
878 unsigned char bufHead[RAND_HEAD_LEN];
879 unsigned int sizeHead;
880 zi->ci.encrypt = 1;
881 zi->ci.pcrc_32_tab = get_crc_table();
882 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
883
884 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
885 zi->ci.crypt_header_size = sizeHead;
886
887 if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
888 err = ZIP_ERRNO;
889 }
890 # endif
891
892 if (err==Z_OK)
893 zi->in_opened_file_inzip = 1;
894 return err;
895 }
896
897 extern int ZEXPORT zipOpenNewFileInZip2(file, filename, zipfi,
898 extrafield_local, size_extrafield_local,
899 extrafield_global, size_extrafield_global,
900 comment, method, level, raw)
901 zipFile file;
902 const char* filename;
903 const zip_fileinfo* zipfi;
904 const void* extrafield_local;
905 uInt size_extrafield_local;
906 const void* extrafield_global;
907 uInt size_extrafield_global;
908 const char* comment;
909 int method;
910 int level;
911 int raw;
912 {
913 return zipOpenNewFileInZip3 (file, filename, zipfi,
914 extrafield_local, size_extrafield_local,
915 extrafield_global, size_extrafield_global,
916 comment, method, level, raw,
917 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
918 NULL, 0);
919 }
920
921 extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
922 extrafield_local, size_extrafield_local,
923 extrafield_global, size_extrafield_global,
924 comment, method, level)
925 zipFile file;
926 const char* filename;
927 const zip_fileinfo* zipfi;
928 const void* extrafield_local;
929 uInt size_extrafield_local;
930 const void* extrafield_global;
931 uInt size_extrafield_global;
932 const char* comment;
933 int method;
934 int level;
935 {
936 return zipOpenNewFileInZip2 (file, filename, zipfi,
937 extrafield_local, size_extrafield_local,
938 extrafield_global, size_extrafield_global,
939 comment, method, level, 0);
940 }
941
zipFlushWriteBuffer(zi)942 local int zipFlushWriteBuffer(zi)
943 zip_internal* zi;
944 {
945 int err=ZIP_OK;
946
947 if (zi->ci.encrypt != 0)
948 {
949 #ifndef NOCRYPT
950 uInt i;
951 int t;
952 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
953 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
954 zi->ci.buffered_data[i],t);
955 #endif
956 }
957 if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
958 !=zi->ci.pos_in_buffered_data)
959 err = ZIP_ERRNO;
960 zi->ci.pos_in_buffered_data = 0;
961 return err;
962 }
963
964 extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
965 zipFile file;
966 const void* buf;
967 unsigned len;
968 {
969 zip_internal* zi;
970 int err=ZIP_OK;
971
972 if (file == NULL)
973 return ZIP_PARAMERROR;
974 zi = (zip_internal*)file;
975
976 if (zi->in_opened_file_inzip == 0)
977 return ZIP_PARAMERROR;
978
979 zi->ci.stream.next_in = (void*)buf;
980 zi->ci.stream.avail_in = len;
981 zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
982
983 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
984 {
985 if (zi->ci.stream.avail_out == 0)
986 {
987 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
988 err = ZIP_ERRNO;
989 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
990 zi->ci.stream.next_out = zi->ci.buffered_data;
991 }
992
993
994 if (err != ZIP_OK)
995 break;
996
997 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
998 {
999 uLong uTotalOutBefore = zi->ci.stream.total_out;
1000 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
1001 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1002 }
1003 else
1004 {
1005 uInt copy_this,i;
1006 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
1007 copy_this = zi->ci.stream.avail_in;
1008 else
1009 copy_this = zi->ci.stream.avail_out;
1010 for (i=0;i<copy_this;i++)
1011 *(((char*)zi->ci.stream.next_out)+i) =
1012 *(((const char*)zi->ci.stream.next_in)+i);
1013 {
1014 zi->ci.stream.avail_in -= copy_this;
1015 zi->ci.stream.avail_out-= copy_this;
1016 zi->ci.stream.next_in+= copy_this;
1017 zi->ci.stream.next_out+= copy_this;
1018 zi->ci.stream.total_in+= copy_this;
1019 zi->ci.stream.total_out+= copy_this;
1020 zi->ci.pos_in_buffered_data += copy_this;
1021 }
1022 }
1023 }
1024
1025 return err;
1026 }
1027
1028 extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32)
1029 zipFile file;
1030 uLong uncompressed_size;
1031 uLong crc32;
1032 {
1033 zip_internal* zi;
1034 uLong compressed_size;
1035 int err=ZIP_OK;
1036
1037 if (file == NULL)
1038 return ZIP_PARAMERROR;
1039 zi = (zip_internal*)file;
1040
1041 if (zi->in_opened_file_inzip == 0)
1042 return ZIP_PARAMERROR;
1043 zi->ci.stream.avail_in = 0;
1044
1045 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1046 while (err==ZIP_OK)
1047 {
1048 uLong uTotalOutBefore;
1049 if (zi->ci.stream.avail_out == 0)
1050 {
1051 zipFlushWriteBuffer(zi);
1052 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1053 zi->ci.stream.next_out = zi->ci.buffered_data;
1054 }
1055 uTotalOutBefore = zi->ci.stream.total_out;
1056 err=deflate(&zi->ci.stream, Z_FINISH);
1057 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1058 }
1059
1060 if (err==Z_STREAM_END)
1061 err=ZIP_OK; /* this is normal */
1062
1063 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1064 if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
1065 err = ZIP_ERRNO;
1066
1067 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1068 {
1069 err=deflateEnd(&zi->ci.stream);
1070 zi->ci.stream_initialised = 0;
1071 }
1072
1073 if (!zi->ci.raw)
1074 {
1075 crc32 = (uLong)zi->ci.crc32;
1076 uncompressed_size = (uLong)zi->ci.stream.total_in;
1077 }
1078 compressed_size = (uLong)zi->ci.stream.total_out;
1079 # ifndef NOCRYPT
1080 compressed_size += zi->ci.crypt_header_size;
1081 # endif
1082
1083 ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1084 ziplocal_putValue_inmemory(zi->ci.central_header+20,
1085 compressed_size,4); /*compr size*/
1086 if (zi->ci.stream.data_type == Z_ASCII)
1087 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1088 ziplocal_putValue_inmemory(zi->ci.central_header+24,
1089 uncompressed_size,4); /*uncompr size*/
1090
1091 if (err==ZIP_OK)
1092 err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
1093 (uLong)zi->ci.size_centralheader);
1094 free(zi->ci.central_header);
1095
1096 if (err==ZIP_OK)
1097 {
1098 long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1099 if (ZSEEK(zi->z_filefunc,zi->filestream,
1100 zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1101 err = ZIP_ERRNO;
1102
1103 if (err==ZIP_OK)
1104 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1105
1106 if (err==ZIP_OK) /* compressed size, unknown */
1107 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1108
1109 if (err==ZIP_OK) /* uncompressed size, unknown */
1110 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1111
1112 if (ZSEEK(zi->z_filefunc,zi->filestream,
1113 cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1114 err = ZIP_ERRNO;
1115 }
1116
1117 zi->number_entry ++;
1118 zi->in_opened_file_inzip = 0;
1119
1120 return err;
1121 }
1122
1123 extern int ZEXPORT zipCloseFileInZip (file)
1124 zipFile file;
1125 {
1126 return zipCloseFileInZipRaw (file,0,0);
1127 }
1128
1129 extern int ZEXPORT zipClose (file, global_comment)
1130 zipFile file;
1131 const char* global_comment;
1132 {
1133 zip_internal* zi;
1134 int err = 0;
1135 uLong size_centraldir = 0;
1136 uLong centraldir_pos_inzip;
1137 uInt size_global_comment;
1138 if (file == NULL)
1139 return ZIP_PARAMERROR;
1140 zi = (zip_internal*)file;
1141
1142 if (zi->in_opened_file_inzip == 1)
1143 {
1144 err = zipCloseFileInZip (file);
1145 }
1146
1147 #ifndef NO_ADDFILEINEXISTINGZIP
1148 if (global_comment==NULL)
1149 global_comment = zi->globalcomment;
1150 #endif
1151 if (global_comment==NULL)
1152 size_global_comment = 0;
1153 else
1154 size_global_comment = (uInt)strlen(global_comment);
1155
1156 centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1157 if (err==ZIP_OK)
1158 {
1159 linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
1160 while (ldi!=NULL)
1161 {
1162 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1163 if (ZWRITE(zi->z_filefunc,zi->filestream,
1164 ldi->data,ldi->filled_in_this_block)
1165 !=ldi->filled_in_this_block )
1166 err = ZIP_ERRNO;
1167
1168 size_centraldir += ldi->filled_in_this_block;
1169 ldi = ldi->next_datablock;
1170 }
1171 }
1172 free_datablock(zi->central_dir.first_block);
1173
1174 if (err==ZIP_OK) /* Magic End */
1175 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1176
1177 if (err==ZIP_OK) /* number of this disk */
1178 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1179
1180 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1181 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1182
1183 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1184 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1185
1186 if (err==ZIP_OK) /* total number of entries in the central dir */
1187 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1188
1189 if (err==ZIP_OK) /* size of the central directory */
1190 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1191
1192 if (err==ZIP_OK) /* offset of start of central directory with respect to the
1193 starting disk number */
1194 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
1195 (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1196
1197 if (err==ZIP_OK) /* zipfile comment length */
1198 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1199
1200 if ((err==ZIP_OK) && (size_global_comment>0))
1201 if (ZWRITE(zi->z_filefunc,zi->filestream,
1202 global_comment,size_global_comment) != size_global_comment)
1203 err = ZIP_ERRNO;
1204
1205 if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)
1206 if (err == ZIP_OK)
1207 err = ZIP_ERRNO;
1208
1209 #ifndef NO_ADDFILEINEXISTINGZIP
1210 TRYFREE(zi->globalcomment);
1211 #endif
1212 TRYFREE(zi);
1213
1214 return err;
1215 }
1216