1 /* zip.c -- IO on .zip files using zlib
2 Version 0.15 beta, Mar 19th, 1998,
4 Read zip.h for more info
29 /* compile with -Dlocal if your debugger can't find static symbols */
32 # define VERSIONMADEBY (0x0) /* platform depedent */
36 #define Z_BUFSIZE (16384)
39 #ifndef Z_MAXFILENAMEINZIP
40 #define Z_MAXFILENAMEINZIP (256)
44 # define ALLOC(size) (malloc(size))
47 # define TRYFREE(p) {if (p) free(p);}
51 #define SIZECENTRALDIRITEM (0x2e)
52 #define SIZEZIPLOCALHEADER (0x1e)
55 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
69 const char zip_copyright[] =
70 " zip 0.15 Copyright 1998 Gilles Vollant ";
73 #define SIZEDATA_INDATABLOCK (4096-(4*4))
75 #define LOCALHEADERMAGIC (0x04034b50)
76 #define CENTRALHEADERMAGIC (0x02014b50)
77 #define ENDHEADERMAGIC (0x06054b50)
79 #define FLAG_LOCALHEADER_OFFSET (0x06)
80 #define CRC_LOCALHEADER_OFFSET (0x0e)
82 #define SIZECENTRALHEADER (0x2e) /* 46 */
84 typedef struct linkedlist_datablock_internal_s
86 struct linkedlist_datablock_internal_s* next_datablock;
87 uLong avail_in_this_block;
88 uLong filled_in_this_block;
89 uLong unused; /* for future use and alignement */
90 unsigned char data[SIZEDATA_INDATABLOCK];
91 } linkedlist_datablock_internal;
93 typedef struct linkedlist_data_s
95 linkedlist_datablock_internal* first_block;
96 linkedlist_datablock_internal* last_block;
102 z_stream stream; /* zLib stream structure for inflate */
103 int stream_initialised; /* 1 is stream is initialised */
104 uInt pos_in_buffered_data; /* last written byte in buffered_data */
106 uLong pos_local_header; /* offset of the local header of the file
108 char* central_header; /* central header data for the current file */
109 uLong size_centralheader; /* size of the central header for cur file */
110 uLong flag; /* flag of the file currently writing */
112 int method; /* compression method of file currenty wr.*/
113 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
121 linkedlist_data central_dir;/* datablock with central dir in construction*/
122 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
123 curfile_info ci; /* info on the file curretly writing */
125 uLong begin_pos; /* position of the beginning of the zipfile */
129 local linkedlist_datablock_internal* allocate_new_datablock()
131 linkedlist_datablock_internal* ldi;
132 ldi = (linkedlist_datablock_internal*)
133 ALLOC(sizeof(linkedlist_datablock_internal));
136 ldi->next_datablock = NULL ;
137 ldi->filled_in_this_block = 0 ;
138 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
143 local void free_datablock(ldi)
144 linkedlist_datablock_internal* ldi;
148 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
154 local void init_linkedlist(ll)
157 ll->first_block = ll->last_block = NULL;
160 local void free_linkedlist(ll)
163 free_datablock(ll->first_block);
164 ll->first_block = ll->last_block = NULL;
168 local int add_data_in_datablock(ll,buf,len)
173 linkedlist_datablock_internal* ldi;
174 const unsigned char* from_copy;
177 return ZIP_INTERNALERROR;
179 if (ll->last_block == NULL)
181 ll->first_block = ll->last_block = allocate_new_datablock();
182 if (ll->first_block == NULL)
183 return ZIP_INTERNALERROR;
186 ldi = ll->last_block;
187 from_copy = (unsigned char*)buf;
193 unsigned char* to_copy;
195 if (ldi->avail_in_this_block==0)
197 ldi->next_datablock = allocate_new_datablock();
198 if (ldi->next_datablock == NULL)
199 return ZIP_INTERNALERROR;
200 ldi = ldi->next_datablock ;
201 ll->last_block = ldi;
204 if (ldi->avail_in_this_block < len)
205 copy_this = (uInt)ldi->avail_in_this_block;
207 copy_this = (uInt)len;
209 to_copy = &(ldi->data[ldi->filled_in_this_block]);
211 for (i=0;i<copy_this;i++)
212 *(to_copy+i)=*(from_copy+i);
214 ldi->filled_in_this_block += copy_this;
215 ldi->avail_in_this_block -= copy_this;
216 from_copy += copy_this ;
223 local int write_datablock(fout,ll)
227 linkedlist_datablock_internal* ldi;
228 ldi = ll->first_block;
231 if (ldi->filled_in_this_block > 0)
232 if (fwrite(ldi->data,(uInt)ldi->filled_in_this_block,1,fout)!=1)
234 ldi = ldi->next_datablock;
239 /****************************************************************************/
241 /* ===========================================================================
242 Outputs a long in LSB order to the given file
243 nbByte == 1, 2 or 4 (byte, short or long)
245 local int ziplocal_putValue (file, x, nbByte)
250 unsigned char buf[4];
252 for (n = 0; n < nbByte; n++) {
253 buf[n] = (unsigned char)(x & 0xff);
256 if (fwrite(buf,nbByte,1,file)!=1)
263 local void ziplocal_putValue_inmemory (dest, x, nbByte)
268 unsigned char* buf=(unsigned char*)dest;
270 for (n = 0; n < nbByte; n++) {
271 buf[n] = (unsigned char)(x & 0xff);
275 /****************************************************************************/
278 local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
282 uLong year = (uLong)ptm->tm_year;
288 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
289 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
293 /****************************************************************************/
295 extern zipFile ZEXPORT zipOpen (pathname, append)
296 const char *pathname;
302 ziinit.filezip = fopen(pathname,(append == 0) ? "wb" : "ab");
303 if (ziinit.filezip == NULL)
305 ziinit.begin_pos = ftell(ziinit.filezip);
306 ziinit.in_opened_file_inzip = 0;
307 ziinit.ci.stream_initialised = 0;
308 ziinit.number_entry = 0;
309 init_linkedlist(&(ziinit.central_dir));
312 zi = (zip_internal*)ALLOC(sizeof(zip_internal));
315 fclose(ziinit.filezip);
323 extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
324 extrafield_local, size_extrafield_local,
325 extrafield_global, size_extrafield_global,
326 comment, method, level)
328 const char* filename;
329 const zip_fileinfo* zipfi;
330 const void* extrafield_local;
331 uInt size_extrafield_local;
332 const void* extrafield_global;
333 uInt size_extrafield_global;
345 return ZIP_PARAMERROR;
346 if ((method!=0) && (method!=Z_DEFLATED))
347 return ZIP_PARAMERROR;
349 zi = (zip_internal*)file;
351 if (zi->in_opened_file_inzip == 1)
353 err = zipCloseFileInZip (file);
365 size_comment = strlen(comment);
367 size_filename = strlen(filename);
373 if (zipfi->dosDate != 0)
374 zi->ci.dosDate = zipfi->dosDate;
375 else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
379 if ((level==8) || (level==9))
387 zi->ci.method = method;
388 zi->ci.stream_initialised = 0;
389 zi->ci.pos_in_buffered_data = 0;
390 zi->ci.pos_local_header = ftell(zi->filezip);
391 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
392 size_extrafield_global + size_comment;
393 zi->ci.central_header = (char*)ALLOC(zi->ci.size_centralheader);
395 ziplocal_putValue_inmemory(zi->ci.central_header,CENTRALHEADERMAGIC,4);
397 ziplocal_putValue_inmemory(zi->ci.central_header+4,VERSIONMADEBY,2);
398 ziplocal_putValue_inmemory(zi->ci.central_header+6,20,2);
399 ziplocal_putValue_inmemory(zi->ci.central_header+8,zi->ci.flag,2);
400 ziplocal_putValue_inmemory(zi->ci.central_header+10,zi->ci.method,2);
401 ziplocal_putValue_inmemory(zi->ci.central_header+12,zi->ci.dosDate,4);
402 ziplocal_putValue_inmemory(zi->ci.central_header+16,0,4); /*crc*/
403 ziplocal_putValue_inmemory(zi->ci.central_header+20,0,4); /*compr size*/
404 ziplocal_putValue_inmemory(zi->ci.central_header+24,0,4); /*uncompr size*/
405 ziplocal_putValue_inmemory(zi->ci.central_header+28,size_filename,2);
406 ziplocal_putValue_inmemory(zi->ci.central_header+30,size_extrafield_global,2);
407 ziplocal_putValue_inmemory(zi->ci.central_header+32,size_comment,2);
408 ziplocal_putValue_inmemory(zi->ci.central_header+34,0,2); /*disk nm start*/
411 ziplocal_putValue_inmemory(zi->ci.central_header+36,0,2);
413 ziplocal_putValue_inmemory(zi->ci.central_header+36,zipfi->internal_fa,2);
416 ziplocal_putValue_inmemory(zi->ci.central_header+38,0,4);
418 ziplocal_putValue_inmemory(zi->ci.central_header+38,zipfi->external_fa,4);
420 ziplocal_putValue_inmemory(zi->ci.central_header+42,zi->ci.pos_local_header,4);
422 for (i=0;i<size_filename;i++)
423 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
425 for (i=0;i<size_extrafield_global;i++)
426 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
427 *(((const char*)extrafield_global)+i);
429 for (i=0;i<size_comment;i++)
430 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
431 size_extrafield_global+i) = *(filename+i);
432 if (zi->ci.central_header == NULL)
433 return ZIP_INTERNALERROR;
435 /* write the local header */
436 err = ziplocal_putValue(zi->filezip,LOCALHEADERMAGIC,4);
439 err = ziplocal_putValue(zi->filezip,20,2);/* version needed to extract */
441 err = ziplocal_putValue(zi->filezip,zi->ci.flag,2);
444 err = ziplocal_putValue(zi->filezip,zi->ci.method,2);
447 err = ziplocal_putValue(zi->filezip,zi->ci.dosDate,4);
450 err = ziplocal_putValue(zi->filezip,0,4); /* crc 32, unknown */
452 err = ziplocal_putValue(zi->filezip,0,4); /* compressed size, unknown */
454 err = ziplocal_putValue(zi->filezip,0,4); /* uncompressed size, unknown */
457 err = ziplocal_putValue(zi->filezip,(uLong)size_filename,2);
460 err = ziplocal_putValue(zi->filezip,(uLong)size_extrafield_local,2);
462 if ((err==ZIP_OK) && (size_filename>0))
463 if (fwrite(filename,(uInt)size_filename,1,zi->filezip)!=1)
466 if ((err==ZIP_OK) && (size_extrafield_local>0))
467 if (fwrite(extrafield_local,(uInt)size_extrafield_local,1,zi->filezip)
471 zi->ci.stream.avail_in = (uInt)0;
472 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
473 zi->ci.stream.next_out = zi->ci.buffered_data;
474 zi->ci.stream.total_in = 0;
475 zi->ci.stream.total_out = 0;
477 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED))
479 zi->ci.stream.zalloc = (alloc_func)0;
480 zi->ci.stream.zfree = (free_func)0;
481 zi->ci.stream.opaque = (voidpf)0;
483 err = deflateInit2(&zi->ci.stream, level,
484 Z_DEFLATED, -MAX_WBITS, /*DEF_MEM_LEVEL*/8, 0);
487 zi->ci.stream_initialised = 1;
492 zi->in_opened_file_inzip = 1;
496 extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
505 return ZIP_PARAMERROR;
506 zi = (zip_internal*)file;
508 if (zi->in_opened_file_inzip == 0)
509 return ZIP_PARAMERROR;
511 zi->ci.stream.next_in = buf;
512 zi->ci.stream.avail_in = len;
513 zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
515 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
517 if (zi->ci.stream.avail_out == 0)
519 if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
522 zi->ci.pos_in_buffered_data = 0;
523 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
524 zi->ci.stream.next_out = zi->ci.buffered_data;
527 if (zi->ci.method == Z_DEFLATED)
529 uLong uTotalOutBefore = zi->ci.stream.total_out;
530 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
531 zi->ci.pos_in_buffered_data += zi->ci.stream.total_out - uTotalOutBefore ;
537 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
538 copy_this = zi->ci.stream.avail_in;
540 copy_this = zi->ci.stream.avail_out;
541 for (i=0;i<copy_this;i++)
542 *(((char*)zi->ci.stream.next_out)+i) =
543 *(((const char*)zi->ci.stream.next_in)+i);
545 zi->ci.stream.avail_in -= copy_this;
546 zi->ci.stream.avail_out-= copy_this;
547 zi->ci.stream.next_in+= copy_this;
548 zi->ci.stream.next_out+= copy_this;
549 zi->ci.stream.total_in+= copy_this;
550 zi->ci.stream.total_out+= copy_this;
551 zi->ci.pos_in_buffered_data += copy_this;
559 extern int ZEXPORT zipCloseFileInZip (file)
566 return ZIP_PARAMERROR;
567 zi = (zip_internal*)file;
569 if (zi->in_opened_file_inzip == 0)
570 return ZIP_PARAMERROR;
571 zi->ci.stream.avail_in = 0;
573 if (zi->ci.method == Z_DEFLATED)
576 uLong uTotalOutBefore;
577 if (zi->ci.stream.avail_out == 0)
579 if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
582 zi->ci.pos_in_buffered_data = 0;
583 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
584 zi->ci.stream.next_out = zi->ci.buffered_data;
586 uTotalOutBefore = zi->ci.stream.total_out;
587 err=deflate(&zi->ci.stream, Z_FINISH);
588 zi->ci.pos_in_buffered_data += zi->ci.stream.total_out - uTotalOutBefore ;
591 if (err==Z_STREAM_END)
592 err=ZIP_OK; /* this is normal */
594 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
595 if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
599 if ((zi->ci.method == Z_DEFLATED) && (err==ZIP_OK))
601 err=deflateEnd(&zi->ci.stream);
602 zi->ci.stream_initialised = 0;
605 ziplocal_putValue_inmemory(zi->ci.central_header+16,zi->ci.crc32,4); /*crc*/
606 ziplocal_putValue_inmemory(zi->ci.central_header+20,
607 zi->ci.stream.total_out,4); /*compr size*/
608 ziplocal_putValue_inmemory(zi->ci.central_header+24,
609 zi->ci.stream.total_in,4); /*uncompr size*/
612 err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
613 (uLong)zi->ci.size_centralheader);
614 free(zi->ci.central_header);
618 long cur_pos_inzip = ftell(zi->filezip);
619 if (fseek(zi->filezip,
620 zi->ci.pos_local_header + 14,SEEK_SET)!=0)
624 err = ziplocal_putValue(zi->filezip,zi->ci.crc32,4); /* crc 32, unknown */
626 if (err==ZIP_OK) /* compressed size, unknown */
627 err = ziplocal_putValue(zi->filezip,zi->ci.stream.total_out,4);
629 if (err==ZIP_OK) /* uncompressed size, unknown */
630 err = ziplocal_putValue(zi->filezip,zi->ci.stream.total_in,4);
632 if (fseek(zi->filezip,
633 cur_pos_inzip,SEEK_SET)!=0)
638 zi->in_opened_file_inzip = 0;
643 extern int ZEXPORT zipClose (file, global_comment)
645 const char* global_comment;
649 uLong size_centraldir = 0;
650 uLong centraldir_pos_inzip ;
651 uInt size_global_comment;
653 return ZIP_PARAMERROR;
654 zi = (zip_internal*)file;
656 if (zi->in_opened_file_inzip == 1)
658 err = zipCloseFileInZip (file);
661 if (global_comment==NULL)
662 size_global_comment = 0;
664 size_global_comment = strlen(global_comment);
667 centraldir_pos_inzip = ftell(zi->filezip);
670 linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
673 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
674 if (fwrite(ldi->data,(uInt)ldi->filled_in_this_block,
678 size_centraldir += ldi->filled_in_this_block;
679 ldi = ldi->next_datablock;
682 free_datablock(zi->central_dir.first_block);
684 if (err==ZIP_OK) /* Magic End */
685 err = ziplocal_putValue(zi->filezip,ENDHEADERMAGIC,4);
687 if (err==ZIP_OK) /* number of this disk */
688 err = ziplocal_putValue(zi->filezip,0,2);
690 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
691 err = ziplocal_putValue(zi->filezip,0,2);
693 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
694 err = ziplocal_putValue(zi->filezip,zi->number_entry,2);
696 if (err==ZIP_OK) /* total number of entries in the central dir */
697 err = ziplocal_putValue(zi->filezip,zi->number_entry,2);
699 if (err==ZIP_OK) /* size of the central directory */
700 err = ziplocal_putValue(zi->filezip,size_centraldir,4);
702 if (err==ZIP_OK) /* offset of start of central directory with respect to the
703 starting disk number */
704 err = ziplocal_putValue(zi->filezip,centraldir_pos_inzip ,4);
706 if (err==ZIP_OK) /* zipfile comment length */
707 err = ziplocal_putValue(zi->filezip,(uLong)size_global_comment,2);
709 if ((err==ZIP_OK) && (size_global_comment>0))
710 if (fwrite(global_comment,(uInt)size_global_comment,1,zi->filezip) !=1 )