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