1 /* zip.c -- IO on .zip files using zlib
2 Version 0.21, March 10th, 2003
4 Read zip.h for more info
30 /* compile with -Dlocal if your debugger can't find static symbols */
33 # define VERSIONMADEBY (0x0) /* platform depedent */
37 #define Z_BUFSIZE (16384)
40 #ifndef Z_MAXFILENAMEINZIP
41 #define Z_MAXFILENAMEINZIP (256)
45 # define ALLOC(size) (malloc(size))
48 # define TRYFREE(p) {if (p) free(p);}
52 #define SIZECENTRALDIRITEM (0x2e)
53 #define SIZEZIPLOCALHEADER (0x1e)
56 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
71 #if MAX_MEM_LEVEL >= 8
72 # define DEF_MEM_LEVEL 8
74 # define DEF_MEM_LEVEL MAX_MEM_LEVEL
77 const char zip_copyright[] =
78 " zip 0.21 Copyright 1998-2003 Gilles Vollant - http://www.winimage.com/zLibDll";
81 #define SIZEDATA_INDATABLOCK (4096-(4*4))
83 #define LOCALHEADERMAGIC (0x04034b50)
84 #define CENTRALHEADERMAGIC (0x02014b50)
85 #define ENDHEADERMAGIC (0x06054b50)
87 #define FLAG_LOCALHEADER_OFFSET (0x06)
88 #define CRC_LOCALHEADER_OFFSET (0x0e)
90 #define SIZECENTRALHEADER (0x2e) /* 46 */
92 typedef struct linkedlist_datablock_internal_s
94 struct linkedlist_datablock_internal_s* next_datablock;
95 uLong avail_in_this_block;
96 uLong filled_in_this_block;
97 uLong unused; /* for future use and alignement */
98 unsigned char data[SIZEDATA_INDATABLOCK];
99 } linkedlist_datablock_internal;
101 typedef struct linkedlist_data_s
103 linkedlist_datablock_internal* first_block;
104 linkedlist_datablock_internal* last_block;
110 z_stream stream; /* zLib stream structure for inflate */
111 int stream_initialised; /* 1 is stream is initialised */
112 uInt pos_in_buffered_data; /* last written byte in buffered_data */
114 uLong pos_local_header; /* offset of the local header of the file
116 char* central_header; /* central header data for the current file */
117 uLong size_centralheader; /* size of the central header for cur file */
118 uLong flag; /* flag of the file currently writing */
120 int method; /* compression method of file currenty wr.*/
121 int raw; /* 1 for directly writing raw data */
122 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
127 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
128 const unsigned long* pcrc_32_tab;
129 int crypt_header_size;
135 zlib_filefunc_def z_filefunc;
136 voidpf filestream; /* io structore of the zipfile */
137 linkedlist_data central_dir;/* datablock with central dir in construction*/
138 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
139 curfile_info ci; /* info on the file curretly writing */
141 uLong begin_pos; /* position of the beginning of the zipfile */
142 uLong add_position_when_writting_offset;
149 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
153 local linkedlist_datablock_internal* allocate_new_datablock()
155 linkedlist_datablock_internal* ldi;
156 ldi = (linkedlist_datablock_internal*)
157 ALLOC(sizeof(linkedlist_datablock_internal));
160 ldi->next_datablock = NULL ;
161 ldi->filled_in_this_block = 0 ;
162 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
167 local void free_datablock(ldi)
168 linkedlist_datablock_internal* ldi;
172 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
178 local void init_linkedlist(ll)
181 ll->first_block = ll->last_block = NULL;
184 local void free_linkedlist(ll)
187 free_datablock(ll->first_block);
188 ll->first_block = ll->last_block = NULL;
192 local int add_data_in_datablock(ll,buf,len)
197 linkedlist_datablock_internal* ldi;
198 const unsigned char* from_copy;
201 return ZIP_INTERNALERROR;
203 if (ll->last_block == NULL)
205 ll->first_block = ll->last_block = allocate_new_datablock();
206 if (ll->first_block == NULL)
207 return ZIP_INTERNALERROR;
210 ldi = ll->last_block;
211 from_copy = (unsigned char*)buf;
217 unsigned char* to_copy;
219 if (ldi->avail_in_this_block==0)
221 ldi->next_datablock = allocate_new_datablock();
222 if (ldi->next_datablock == NULL)
223 return ZIP_INTERNALERROR;
224 ldi = ldi->next_datablock ;
225 ll->last_block = ldi;
228 if (ldi->avail_in_this_block < len)
229 copy_this = (uInt)ldi->avail_in_this_block;
231 copy_this = (uInt)len;
233 to_copy = &(ldi->data[ldi->filled_in_this_block]);
235 for (i=0;i<copy_this;i++)
236 *(to_copy+i)=*(from_copy+i);
238 ldi->filled_in_this_block += copy_this;
239 ldi->avail_in_this_block -= copy_this;
240 from_copy += copy_this ;
248 /****************************************************************************/
250 #ifndef NO_ADDFILEINEXISTINGZIP
251 /* ===========================================================================
252 Inputs a long in LSB order to the given file
253 nbByte == 1, 2 or 4 (byte, short or long)
256 local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def,
257 voidpf filestream, uLong x, int nbByte));
258 local int ziplocal_putValue (pzlib_filefunc_def, filestream, x, nbByte)
259 const zlib_filefunc_def* pzlib_filefunc_def;
264 unsigned char buf[4];
266 for (n = 0; n < nbByte; n++) {
267 buf[n] = (unsigned char)(x & 0xff);
270 if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
276 local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
277 local void ziplocal_putValue_inmemory (dest, x, nbByte)
282 unsigned char* buf=(unsigned char*)dest;
284 for (n = 0; n < nbByte; n++) {
285 buf[n] = (unsigned char)(x & 0xff);
289 /****************************************************************************/
292 local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
296 uLong year = (uLong)ptm->tm_year;
302 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
303 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
307 /****************************************************************************/
309 local int ziplocal_getByte OF((
310 const zlib_filefunc_def* pzlib_filefunc_def,
314 local int ziplocal_getByte(pzlib_filefunc_def,filestream,pi)
315 const zlib_filefunc_def* pzlib_filefunc_def;
320 int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1);
328 if (ZERROR(*pzlib_filefunc_def,filestream))
336 /* ===========================================================================
337 Reads a long in LSB order from the given gz_stream. Sets
339 local int ziplocal_getShort OF((
340 const zlib_filefunc_def* pzlib_filefunc_def,
344 local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX)
345 const zlib_filefunc_def* pzlib_filefunc_def;
353 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
357 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
367 local int ziplocal_getLong OF((
368 const zlib_filefunc_def* pzlib_filefunc_def,
372 local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX)
373 const zlib_filefunc_def* pzlib_filefunc_def;
381 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
385 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
389 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
393 err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i);
403 #ifndef BUFREADCOMMENT
404 #define BUFREADCOMMENT (0x400)
407 Locate the Central directory of a zipfile (at the end, just before
410 local uLong ziplocal_SearchCentralDir OF((
411 const zlib_filefunc_def* pzlib_filefunc_def,
414 local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream)
415 const zlib_filefunc_def* pzlib_filefunc_def;
421 uLong uMaxBack=0xffff; /* maximum size of global comment */
424 if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
428 uSizeFile = ZTELL(*pzlib_filefunc_def,filestream);
430 if (uMaxBack>uSizeFile)
431 uMaxBack = uSizeFile;
433 buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
438 while (uBackRead<uMaxBack)
440 uLong uReadSize,uReadPos ;
442 if (uBackRead+BUFREADCOMMENT>uMaxBack)
443 uBackRead = uMaxBack;
445 uBackRead+=BUFREADCOMMENT;
446 uReadPos = uSizeFile-uBackRead ;
448 uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
449 (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
450 if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
453 if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
456 for (i=(int)uReadSize-3; (i--)>0;)
457 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
458 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
460 uPosFound = uReadPos+i;
470 #endif /* !NO_ADDFILEINEXISTINGZIP*/
472 /************************************************************/
473 extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc_def)
474 const char *pathname;
476 zipcharpc* globalcomment;
477 zlib_filefunc_def* pzlib_filefunc_def;
484 if (pzlib_filefunc_def==NULL)
485 fill_fopen_filefunc(&ziinit.z_filefunc);
487 ziinit.z_filefunc = *pzlib_filefunc_def;
489 ziinit.filestream = (*(ziinit.z_filefunc.zopen_file))
490 (ziinit.z_filefunc.opaque,
492 (append == APPEND_STATUS_CREATE) ?
493 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
494 (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
496 if (ziinit.filestream == NULL)
498 ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream);
499 ziinit.in_opened_file_inzip = 0;
500 ziinit.ci.stream_initialised = 0;
501 ziinit.number_entry = 0;
502 ziinit.add_position_when_writting_offset = 0;
503 init_linkedlist(&(ziinit.central_dir));
506 zi = (zip_internal*)ALLOC(sizeof(zip_internal));
509 ZCLOSE(ziinit.z_filefunc,ziinit.filestream);
513 /* now we add file in a zipfile */
514 #ifndef NO_ADDFILEINEXISTINGZIP
515 if (append == APPEND_STATUS_ADDINZIP)
517 uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
519 uLong size_central_dir; /* size of the central directory */
520 uLong offset_central_dir; /* offset of start of central directory */
521 uLong central_pos,uL;
523 uLong number_disk; /* number of the current dist, used for
524 spaning ZIP, unsupported, always 0*/
525 uLong number_disk_with_CD; /* number the the disk with central dir, used
526 for spaning ZIP, unsupported, always 0*/
528 uLong number_entry_CD; /* total number of entries in
530 (same than number_entry on nospan) */
533 central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
537 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
538 central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
541 /* the signature, already checked */
542 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK)
545 /* number of this disk */
546 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK)
549 /* number of the disk with the start of the central directory */
550 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk_with_CD)!=ZIP_OK)
553 /* total number of entries in the central dir on this disk */
554 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry)!=ZIP_OK)
557 /* total number of entries in the central dir */
558 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK)
561 if ((number_entry_CD!=number_entry) ||
562 (number_disk_with_CD!=0) ||
566 /* size of the central directory */
567 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK)
570 /* offset of start of central directory with respect to the
571 starting disk number */
572 if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&offset_central_dir)!=ZIP_OK)
575 /* zipfile comment length */
576 if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK)
579 if ((central_pos<offset_central_dir+size_central_dir) &&
585 ZCLOSE(ziinit.z_filefunc, ziinit.filestream);
589 byte_before_the_zipfile = central_pos -
590 (offset_central_dir+size_central_dir);
591 ziinit.add_position_when_writting_offset = byte_before_the_zipfile ;
594 uLong size_central_dir_to_read = size_central_dir;
595 size_t buf_size = SIZEDATA_INDATABLOCK;
596 void* buf_read = (void*)ALLOC(buf_size);
597 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
598 offset_central_dir + byte_before_the_zipfile,
599 ZLIB_FILEFUNC_SEEK_SET) != 0)
602 while ((size_central_dir_to_read>0) && (err==ZIP_OK))
604 uLong read_this = SIZEDATA_INDATABLOCK;
605 if (read_this > size_central_dir_to_read)
606 read_this = size_central_dir_to_read;
607 if (ZREAD(ziinit.z_filefunc, ziinit.filestream,buf_read,read_this) != read_this)
611 err = add_data_in_datablock(&ziinit.central_dir,buf_read,
613 size_central_dir_to_read-=read_this;
617 ziinit.begin_pos = byte_before_the_zipfile;
618 ziinit.number_entry = number_entry_CD;
620 if (ZSEEK(ziinit.z_filefunc, ziinit.filestream,
621 offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
624 #endif /* !NO_ADDFILEINEXISTINGZIP*/
638 extern zipFile ZEXPORT zipOpen (pathname, append)
639 const char *pathname;
642 return zipOpen2(pathname,append,NULL,NULL);
645 extern int ZEXPORT zipOpenNewFileInZip3 (file, filename, zipfi,
646 extrafield_local, size_extrafield_local,
647 extrafield_global, size_extrafield_global,
648 comment, method, level, raw,
649 windowBits, memLevel, strategy,
650 password, crcForCrypting)
652 const char* filename;
653 const zip_fileinfo* zipfi;
654 const void* extrafield_local;
655 uInt size_extrafield_local;
656 const void* extrafield_global;
657 uInt size_extrafield_global;
665 const char* password;
666 uLong crcForCrypting;
675 if (password != NULL)
676 return ZIP_PARAMERROR;
680 return ZIP_PARAMERROR;
681 if ((method!=0) && (method!=Z_DEFLATED))
682 return ZIP_PARAMERROR;
684 zi = (zip_internal*)file;
686 if (zi->in_opened_file_inzip == 1)
688 err = zipCloseFileInZip (file);
700 size_comment = strlen(comment);
702 size_filename = strlen(filename);
708 if (zipfi->dosDate != 0)
709 zi->ci.dosDate = zipfi->dosDate;
710 else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
714 if ((level==8) || (level==9))
720 if (password != NULL)
724 zi->ci.method = method;
726 zi->ci.stream_initialised = 0;
727 zi->ci.pos_in_buffered_data = 0;
729 zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ;
730 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
731 size_extrafield_global + size_comment;
732 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
734 ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
736 ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
737 ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
738 ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
739 ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
740 ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
741 ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
742 ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
743 ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
744 ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
745 ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
746 ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
747 ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
750 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
752 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
755 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
757 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
759 ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4);
761 for (i=0;i<size_filename;i++)
762 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
764 for (i=0;i<size_extrafield_global;i++)
765 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
766 *(((const char*)extrafield_global)+i);
768 for (i=0;i<size_comment;i++)
769 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
770 size_extrafield_global+i) = *(comment+i);
771 if (zi->ci.central_header == NULL)
772 return ZIP_INTERNALERROR;
774 /* write the local header */
775 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4);
778 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
780 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
783 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
786 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
789 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
791 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
793 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
796 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
799 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2);
801 if ((err==ZIP_OK) && (size_filename>0))
802 if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
805 if ((err==ZIP_OK) && (size_extrafield_local>0))
806 if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local)
807 !=size_extrafield_local)
810 zi->ci.stream.avail_in = (uInt)0;
811 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
812 zi->ci.stream.next_out = zi->ci.buffered_data;
813 zi->ci.stream.total_in = 0;
814 zi->ci.stream.total_out = 0;
816 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
818 zi->ci.stream.zalloc = (alloc_func)0;
819 zi->ci.stream.zfree = (free_func)0;
820 zi->ci.stream.opaque = (voidpf)0;
823 windowBits = -windowBits;
825 err = deflateInit2(&zi->ci.stream, level,
826 Z_DEFLATED, windowBits, memLevel, strategy);
829 zi->ci.stream_initialised = 1;
832 zi->ci.crypt_header_size = 0;
833 if ((err==Z_OK) && (password != NULL))
835 unsigned char bufHead[RAND_HEAD_LEN];
836 unsigned int sizeHead;
838 zi->ci.pcrc_32_tab = get_crc_table();
839 /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
841 sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
842 zi->ci.crypt_header_size = sizeHead;
844 if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
852 zi->in_opened_file_inzip = 1;
856 extern int ZEXPORT zipOpenNewFileInZip2(file, filename, zipfi,
857 extrafield_local, size_extrafield_local,
858 extrafield_global, size_extrafield_global,
859 comment, method, level, raw)
861 const char* filename;
862 const zip_fileinfo* zipfi;
863 const void* extrafield_local;
864 uInt size_extrafield_local;
865 const void* extrafield_global;
866 uInt size_extrafield_global;
872 return zipOpenNewFileInZip3 (file, filename, zipfi,
873 extrafield_local, size_extrafield_local,
874 extrafield_global, size_extrafield_global,
875 comment, method, level, raw,
876 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
880 extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
881 extrafield_local, size_extrafield_local,
882 extrafield_global, size_extrafield_global,
883 comment, method, level)
885 const char* filename;
886 const zip_fileinfo* zipfi;
887 const void* extrafield_local;
888 uInt size_extrafield_local;
889 const void* extrafield_global;
890 uInt size_extrafield_global;
895 return zipOpenNewFileInZip2 (file, filename, zipfi,
896 extrafield_local, size_extrafield_local,
897 extrafield_global, size_extrafield_global,
898 comment, method, level, 0);
901 local int zipFlushWriteBuffer(zi)
906 if (zi->ci.encrypt != 0)
910 for (i=0;i<zi->ci.pos_in_buffered_data;i++)
911 zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab,
912 zi->ci.buffered_data[i],t);
914 if (ZWRITE(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data)
915 !=zi->ci.pos_in_buffered_data)
917 zi->ci.pos_in_buffered_data = 0;
921 extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
930 return ZIP_PARAMERROR;
931 zi = (zip_internal*)file;
933 if (zi->in_opened_file_inzip == 0)
934 return ZIP_PARAMERROR;
936 zi->ci.stream.next_in = (void*)buf;
937 zi->ci.stream.avail_in = len;
938 zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
940 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
942 if (zi->ci.stream.avail_out == 0)
944 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
946 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
947 zi->ci.stream.next_out = zi->ci.buffered_data;
954 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
956 uLong uTotalOutBefore = zi->ci.stream.total_out;
957 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
958 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
964 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
965 copy_this = zi->ci.stream.avail_in;
967 copy_this = zi->ci.stream.avail_out;
968 for (i=0;i<copy_this;i++)
969 *(((char*)zi->ci.stream.next_out)+i) =
970 *(((const char*)zi->ci.stream.next_in)+i);
972 zi->ci.stream.avail_in -= copy_this;
973 zi->ci.stream.avail_out-= copy_this;
974 zi->ci.stream.next_in+= copy_this;
975 zi->ci.stream.next_out+= copy_this;
976 zi->ci.stream.total_in+= copy_this;
977 zi->ci.stream.total_out+= copy_this;
978 zi->ci.pos_in_buffered_data += copy_this;
986 extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32)
988 uLong uncompressed_size;
992 uLong compressed_size;
996 return ZIP_PARAMERROR;
997 zi = (zip_internal*)file;
999 if (zi->in_opened_file_inzip == 0)
1000 return ZIP_PARAMERROR;
1001 zi->ci.stream.avail_in = 0;
1003 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1006 uLong uTotalOutBefore;
1007 if (zi->ci.stream.avail_out == 0)
1009 if (zipFlushWriteBuffer(zi) == ZIP_ERRNO)
1011 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1012 zi->ci.stream.next_out = zi->ci.buffered_data;
1014 uTotalOutBefore = zi->ci.stream.total_out;
1015 err=deflate(&zi->ci.stream, Z_FINISH);
1016 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1019 if (err==Z_STREAM_END)
1020 err=ZIP_OK; /* this is normal */
1022 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1023 if (zipFlushWriteBuffer(zi)==ZIP_ERRNO)
1026 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1028 err=deflateEnd(&zi->ci.stream);
1029 zi->ci.stream_initialised = 0;
1034 crc32 = (uLong)zi->ci.crc32;
1035 uncompressed_size = (uLong)zi->ci.stream.total_in;
1037 compressed_size = (uLong)zi->ci.stream.total_out;
1039 compressed_size += zi->ci.crypt_header_size;
1042 ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1043 ziplocal_putValue_inmemory(zi->ci.central_header+20,
1044 compressed_size,4); /*compr size*/
1045 if (zi->ci.stream.data_type == Z_ASCII)
1046 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1047 ziplocal_putValue_inmemory(zi->ci.central_header+24,
1048 uncompressed_size,4); /*uncompr size*/
1051 err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
1052 (uLong)zi->ci.size_centralheader);
1053 free(zi->ci.central_header);
1057 long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1058 if (ZSEEK(zi->z_filefunc,zi->filestream,
1059 zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1063 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1065 if (err==ZIP_OK) /* compressed size, unknown */
1066 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1068 if (err==ZIP_OK) /* uncompressed size, unknown */
1069 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1071 if (ZSEEK(zi->z_filefunc,zi->filestream,
1072 cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1076 zi->number_entry ++;
1077 zi->in_opened_file_inzip = 0;
1082 extern int ZEXPORT zipCloseFileInZip (file)
1085 return zipCloseFileInZipRaw (file,0,0);
1088 extern int ZEXPORT zipClose (file, global_comment)
1090 const char* global_comment;
1094 uLong size_centraldir = 0;
1095 uLong centraldir_pos_inzip ;
1096 uInt size_global_comment;
1098 return ZIP_PARAMERROR;
1099 zi = (zip_internal*)file;
1101 if (zi->in_opened_file_inzip == 1)
1103 err = zipCloseFileInZip (file);
1106 if (global_comment==NULL)
1107 size_global_comment = 0;
1109 size_global_comment = strlen(global_comment);
1112 centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream);
1115 linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
1118 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1119 if (ZWRITE(zi->z_filefunc,zi->filestream,
1120 ldi->data,ldi->filled_in_this_block)
1121 !=ldi->filled_in_this_block )
1124 size_centraldir += ldi->filled_in_this_block;
1125 ldi = ldi->next_datablock;
1128 free_datablock(zi->central_dir.first_block);
1130 if (err==ZIP_OK) /* Magic End */
1131 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1133 if (err==ZIP_OK) /* number of this disk */
1134 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1136 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1137 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1139 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1140 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1142 if (err==ZIP_OK) /* total number of entries in the central dir */
1143 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1145 if (err==ZIP_OK) /* size of the central directory */
1146 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1148 if (err==ZIP_OK) /* offset of start of central directory with respect to the
1149 starting disk number */
1150 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,
1151 (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1153 if (err==ZIP_OK) /* zipfile comment length */
1154 err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1156 if ((err==ZIP_OK) && (size_global_comment>0))
1157 if (ZWRITE(zi->z_filefunc,zi->filestream,
1158 global_comment,size_global_comment) != size_global_comment)
1161 if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0)