1 /* zip.c -- IO on .zip files using zlib
\r
2 Version 0.15 beta, Mar 19th, 1998,
\r
4 Read zip.h for more info
\r
16 # include <stddef.h>
\r
17 # include <string.h>
\r
18 # include <stdlib.h>
\r
28 # define local static
\r
30 /* compile with -Dlocal if your debugger can't find static symbols */
\r
32 #ifndef VERSIONMADEBY
\r
33 # define VERSIONMADEBY (0x0) /* platform depedent */
\r
37 #define Z_BUFSIZE (16384)
\r
40 #ifndef Z_MAXFILENAMEINZIP
\r
41 #define Z_MAXFILENAMEINZIP (256)
\r
45 # define ALLOC(size) (malloc(size))
\r
48 # define TRYFREE(p) {if (p) free(p);}
\r
52 #define SIZECENTRALDIRITEM (0x2e)
\r
53 #define SIZEZIPLOCALHEADER (0x1e)
\r
56 /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
\r
70 const char zip_copyright[] =
\r
71 " zip 0.15 Copyright 1998 Gilles Vollant ";
\r
74 #define SIZEDATA_INDATABLOCK (4096-(4*4))
\r
76 #define LOCALHEADERMAGIC (0x04034b50)
\r
77 #define CENTRALHEADERMAGIC (0x02014b50)
\r
78 #define ENDHEADERMAGIC (0x06054b50)
\r
80 #define FLAG_LOCALHEADER_OFFSET (0x06)
\r
81 #define CRC_LOCALHEADER_OFFSET (0x0e)
\r
83 #define SIZECENTRALHEADER (0x2e) /* 46 */
\r
85 typedef struct linkedlist_datablock_internal_s
\r
87 struct linkedlist_datablock_internal_s* next_datablock;
\r
88 uLong avail_in_this_block;
\r
89 uLong filled_in_this_block;
\r
90 uLong unused; /* for future use and alignement */
\r
91 unsigned char data[SIZEDATA_INDATABLOCK];
\r
92 } linkedlist_datablock_internal;
\r
94 typedef struct linkedlist_data_s
\r
96 linkedlist_datablock_internal* first_block;
\r
97 linkedlist_datablock_internal* last_block;
\r
103 z_stream stream; /* zLib stream structure for inflate */
\r
104 int stream_initialised; /* 1 is stream is initialised */
\r
105 uInt pos_in_buffered_data; /* last written byte in buffered_data */
\r
107 uLong pos_local_header; /* offset of the local header of the file
\r
108 currenty writing */
\r
109 char* central_header; /* central header data for the current file */
\r
110 uLong size_centralheader; /* size of the central header for cur file */
\r
111 uLong flag; /* flag of the file currently writing */
\r
113 int method; /* compression method of file currenty wr.*/
\r
114 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
\r
122 linkedlist_data central_dir;/* datablock with central dir in construction*/
\r
123 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
\r
124 curfile_info ci; /* info on the file curretly writing */
\r
126 uLong begin_pos; /* position of the beginning of the zipfile */
\r
127 uLong number_entry;
\r
130 local linkedlist_datablock_internal* allocate_new_datablock()
\r
132 linkedlist_datablock_internal* ldi;
\r
133 ldi = (linkedlist_datablock_internal*)
\r
134 ALLOC(sizeof(linkedlist_datablock_internal));
\r
137 ldi->next_datablock = NULL ;
\r
138 ldi->filled_in_this_block = 0 ;
\r
139 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
\r
144 local void free_datablock(ldi)
\r
145 linkedlist_datablock_internal* ldi;
\r
149 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
\r
155 local void init_linkedlist(ll)
\r
156 linkedlist_data* ll;
\r
158 ll->first_block = ll->last_block = NULL;
\r
161 local void free_linkedlist(ll)
\r
162 linkedlist_data* ll;
\r
164 free_datablock(ll->first_block);
\r
165 ll->first_block = ll->last_block = NULL;
\r
169 local int add_data_in_datablock(ll,buf,len)
\r
170 linkedlist_data* ll;
\r
174 linkedlist_datablock_internal* ldi;
\r
175 const unsigned char* from_copy;
\r
178 return ZIP_INTERNALERROR;
\r
180 if (ll->last_block == NULL)
\r
182 ll->first_block = ll->last_block = allocate_new_datablock();
\r
183 if (ll->first_block == NULL)
\r
184 return ZIP_INTERNALERROR;
\r
187 ldi = ll->last_block;
\r
188 from_copy = (unsigned char*)buf;
\r
194 unsigned char* to_copy;
\r
196 if (ldi->avail_in_this_block==0)
\r
198 ldi->next_datablock = allocate_new_datablock();
\r
199 if (ldi->next_datablock == NULL)
\r
200 return ZIP_INTERNALERROR;
\r
201 ldi = ldi->next_datablock ;
\r
202 ll->last_block = ldi;
\r
205 if (ldi->avail_in_this_block < len)
\r
206 copy_this = (uInt)ldi->avail_in_this_block;
\r
208 copy_this = (uInt)len;
\r
210 to_copy = &(ldi->data[ldi->filled_in_this_block]);
\r
212 for (i=0;i<copy_this;i++)
\r
213 *(to_copy+i)=*(from_copy+i);
\r
215 ldi->filled_in_this_block += copy_this;
\r
216 ldi->avail_in_this_block -= copy_this;
\r
217 from_copy += copy_this ;
\r
224 local int write_datablock(fout,ll)
\r
226 linkedlist_data* ll;
\r
228 linkedlist_datablock_internal* ldi;
\r
229 ldi = ll->first_block;
\r
232 if (ldi->filled_in_this_block > 0)
\r
233 if (fwrite(ldi->data,(uInt)ldi->filled_in_this_block,1,fout)!=1)
\r
235 ldi = ldi->next_datablock;
\r
240 /****************************************************************************/
\r
242 /* ===========================================================================
\r
243 Outputs a long in LSB order to the given file
\r
244 nbByte == 1, 2 or 4 (byte, short or long)
\r
247 local int ziplocal_putValue OF((FILE *file, uLong x, int nbByte));
\r
248 local int ziplocal_putValue (file, x, nbByte)
\r
253 unsigned char buf[4];
\r
255 for (n = 0; n < nbByte; n++) {
\r
256 buf[n] = (unsigned char)(x & 0xff);
\r
259 if (fwrite(buf,nbByte,1,file)!=1)
\r
265 local void ziplocal_putValue_inmemory OF((void* dest, uLong x, int nbByte));
\r
266 local void ziplocal_putValue_inmemory (dest, x, nbByte)
\r
271 unsigned char* buf=(unsigned char*)dest;
\r
273 for (n = 0; n < nbByte; n++) {
\r
274 buf[n] = (unsigned char)(x & 0xff);
\r
278 /****************************************************************************/
\r
281 local uLong ziplocal_TmzDateToDosDate(ptm,dosDate)
\r
285 uLong year = (uLong)ptm->tm_year;
\r
291 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
\r
292 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
\r
296 /****************************************************************************/
\r
298 extern zipFile ZEXPORT zipOpen (pathname, append)
\r
299 const char *pathname;
\r
302 zip_internal ziinit;
\r
305 ziinit.filezip = fopen(pathname,(append == 0) ? "wb" : "ab");
\r
306 if (ziinit.filezip == NULL)
\r
308 ziinit.begin_pos = ftell(ziinit.filezip);
\r
309 ziinit.in_opened_file_inzip = 0;
\r
310 ziinit.ci.stream_initialised = 0;
\r
311 ziinit.number_entry = 0;
\r
312 init_linkedlist(&(ziinit.central_dir));
\r
315 zi = (zip_internal*)ALLOC(sizeof(zip_internal));
\r
318 fclose(ziinit.filezip);
\r
323 return (zipFile)zi;
\r
326 extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi,
\r
327 extrafield_local, size_extrafield_local,
\r
328 extrafield_global, size_extrafield_global,
\r
329 comment, method, level)
\r
331 const char* filename;
\r
332 const zip_fileinfo* zipfi;
\r
333 const void* extrafield_local;
\r
334 uInt size_extrafield_local;
\r
335 const void* extrafield_global;
\r
336 uInt size_extrafield_global;
\r
337 const char* comment;
\r
342 uInt size_filename;
\r
348 return ZIP_PARAMERROR;
\r
349 if ((method!=0) && (method!=Z_DEFLATED))
\r
350 return ZIP_PARAMERROR;
\r
352 zi = (zip_internal*)file;
\r
354 if (zi->in_opened_file_inzip == 1)
\r
356 err = zipCloseFileInZip (file);
\r
362 if (filename==NULL)
\r
368 size_comment = strlen(comment);
\r
370 size_filename = strlen(filename);
\r
373 zi->ci.dosDate = 0;
\r
376 if (zipfi->dosDate != 0)
\r
377 zi->ci.dosDate = zipfi->dosDate;
\r
378 else zi->ci.dosDate = ziplocal_TmzDateToDosDate(&zipfi->tmz_date,zipfi->dosDate);
\r
382 if ((level==8) || (level==9))
\r
390 zi->ci.method = method;
\r
391 zi->ci.stream_initialised = 0;
\r
392 zi->ci.pos_in_buffered_data = 0;
\r
393 zi->ci.pos_local_header = ftell(zi->filezip);
\r
394 zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename +
\r
395 size_extrafield_global + size_comment;
\r
396 zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader);
\r
398 ziplocal_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
\r
400 ziplocal_putValue_inmemory(zi->ci.central_header+4,(uLong)VERSIONMADEBY,2);
\r
401 ziplocal_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
\r
402 ziplocal_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
\r
403 ziplocal_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
\r
404 ziplocal_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
\r
405 ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
\r
406 ziplocal_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
\r
407 ziplocal_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
\r
408 ziplocal_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
\r
409 ziplocal_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
\r
410 ziplocal_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
\r
411 ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
\r
414 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
\r
416 ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
\r
419 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
\r
421 ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
\r
423 ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header,4);
\r
425 for (i=0;i<size_filename;i++)
\r
426 *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
\r
428 for (i=0;i<size_extrafield_global;i++)
\r
429 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
\r
430 *(((const char*)extrafield_global)+i);
\r
432 for (i=0;i<size_comment;i++)
\r
433 *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
\r
434 size_extrafield_global+i) = *(filename+i);
\r
435 if (zi->ci.central_header == NULL)
\r
436 return ZIP_INTERNALERROR;
\r
438 /* write the local header */
\r
439 err = ziplocal_putValue(zi->filezip,(uLong)LOCALHEADERMAGIC,4);
\r
442 err = ziplocal_putValue(zi->filezip,(uLong)20,2);/* version needed to extract */
\r
444 err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.flag,2);
\r
447 err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.method,2);
\r
450 err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.dosDate,4);
\r
453 err = ziplocal_putValue(zi->filezip,(uLong)0,4); /* crc 32, unknown */
\r
455 err = ziplocal_putValue(zi->filezip,(uLong)0,4); /* compressed size, unknown */
\r
457 err = ziplocal_putValue(zi->filezip,(uLong)0,4); /* uncompressed size, unknown */
\r
460 err = ziplocal_putValue(zi->filezip,(uLong)size_filename,2);
\r
463 err = ziplocal_putValue(zi->filezip,(uLong)size_extrafield_local,2);
\r
465 if ((err==ZIP_OK) && (size_filename>0))
\r
466 if (fwrite(filename,(uInt)size_filename,1,zi->filezip)!=1)
\r
469 if ((err==ZIP_OK) && (size_extrafield_local>0))
\r
470 if (fwrite(extrafield_local,(uInt)size_extrafield_local,1,zi->filezip)
\r
474 zi->ci.stream.avail_in = (uInt)0;
\r
475 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
\r
476 zi->ci.stream.next_out = zi->ci.buffered_data;
\r
477 zi->ci.stream.total_in = 0;
\r
478 zi->ci.stream.total_out = 0;
\r
480 if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED))
\r
482 zi->ci.stream.zalloc = (alloc_func)0;
\r
483 zi->ci.stream.zfree = (free_func)0;
\r
484 zi->ci.stream.opaque = (voidpf)0;
\r
486 err = deflateInit2(&zi->ci.stream, level,
\r
487 Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0);
\r
490 zi->ci.stream_initialised = 1;
\r
495 zi->in_opened_file_inzip = 1;
\r
499 extern int ZEXPORT zipWriteInFileInZip (file, buf, len)
\r
508 return ZIP_PARAMERROR;
\r
509 zi = (zip_internal*)file;
\r
511 if (zi->in_opened_file_inzip == 0)
\r
512 return ZIP_PARAMERROR;
\r
514 zi->ci.stream.next_in = buf;
\r
515 zi->ci.stream.avail_in = len;
\r
516 zi->ci.crc32 = crc32(zi->ci.crc32,buf,len);
\r
518 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
\r
520 if (zi->ci.stream.avail_out == 0)
\r
522 if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
\r
525 zi->ci.pos_in_buffered_data = 0;
\r
526 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
\r
527 zi->ci.stream.next_out = zi->ci.buffered_data;
\r
530 if (zi->ci.method == Z_DEFLATED)
\r
532 uLong uTotalOutBefore = zi->ci.stream.total_out;
\r
533 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
\r
534 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
\r
540 if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
\r
541 copy_this = zi->ci.stream.avail_in;
\r
543 copy_this = zi->ci.stream.avail_out;
\r
544 for (i=0;i<copy_this;i++)
\r
545 *(((char*)zi->ci.stream.next_out)+i) =
\r
546 *(((const char*)zi->ci.stream.next_in)+i);
\r
548 zi->ci.stream.avail_in -= copy_this;
\r
549 zi->ci.stream.avail_out-= copy_this;
\r
550 zi->ci.stream.next_in+= copy_this;
\r
551 zi->ci.stream.next_out+= copy_this;
\r
552 zi->ci.stream.total_in+= copy_this;
\r
553 zi->ci.stream.total_out+= copy_this;
\r
554 zi->ci.pos_in_buffered_data += copy_this;
\r
562 extern int ZEXPORT zipCloseFileInZip (file)
\r
569 return ZIP_PARAMERROR;
\r
570 zi = (zip_internal*)file;
\r
572 if (zi->in_opened_file_inzip == 0)
\r
573 return ZIP_PARAMERROR;
\r
574 zi->ci.stream.avail_in = 0;
\r
576 if (zi->ci.method == Z_DEFLATED)
\r
577 while (err==ZIP_OK)
\r
579 uLong uTotalOutBefore;
\r
580 if (zi->ci.stream.avail_out == 0)
\r
582 if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
\r
585 zi->ci.pos_in_buffered_data = 0;
\r
586 zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
\r
587 zi->ci.stream.next_out = zi->ci.buffered_data;
\r
589 uTotalOutBefore = zi->ci.stream.total_out;
\r
590 err=deflate(&zi->ci.stream, Z_FINISH);
\r
591 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
\r
594 if (err==Z_STREAM_END)
\r
595 err=ZIP_OK; /* this is normal */
\r
597 if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
\r
598 if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip)
\r
602 if ((zi->ci.method == Z_DEFLATED) && (err==ZIP_OK))
\r
604 err=deflateEnd(&zi->ci.stream);
\r
605 zi->ci.stream_initialised = 0;
\r
608 ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)zi->ci.crc32,4); /*crc*/
\r
609 ziplocal_putValue_inmemory(zi->ci.central_header+20,
\r
610 (uLong)zi->ci.stream.total_out,4); /*compr size*/
\r
611 ziplocal_putValue_inmemory(zi->ci.central_header+24,
\r
612 (uLong)zi->ci.stream.total_in,4); /*uncompr size*/
\r
615 err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header,
\r
616 (uLong)zi->ci.size_centralheader);
\r
617 free(zi->ci.central_header);
\r
621 long cur_pos_inzip = ftell(zi->filezip);
\r
622 if (fseek(zi->filezip,
\r
623 zi->ci.pos_local_header + 14,SEEK_SET)!=0)
\r
627 err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.crc32,4); /* crc 32, unknown */
\r
629 if (err==ZIP_OK) /* compressed size, unknown */
\r
630 err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.stream.total_out,4);
\r
632 if (err==ZIP_OK) /* uncompressed size, unknown */
\r
633 err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.stream.total_in,4);
\r
635 if (fseek(zi->filezip,
\r
636 cur_pos_inzip,SEEK_SET)!=0)
\r
640 zi->number_entry ++;
\r
641 zi->in_opened_file_inzip = 0;
\r
646 extern int ZEXPORT zipClose (file, global_comment)
\r
648 const char* global_comment;
\r
652 uLong size_centraldir = 0;
\r
653 uLong centraldir_pos_inzip ;
\r
654 uInt size_global_comment;
\r
656 return ZIP_PARAMERROR;
\r
657 zi = (zip_internal*)file;
\r
659 if (zi->in_opened_file_inzip == 1)
\r
661 err = zipCloseFileInZip (file);
\r
664 if (global_comment==NULL)
\r
665 size_global_comment = 0;
\r
667 size_global_comment = strlen(global_comment);
\r
670 centraldir_pos_inzip = ftell(zi->filezip);
\r
673 linkedlist_datablock_internal* ldi = zi->central_dir.first_block ;
\r
676 if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
\r
677 if (fwrite(ldi->data,(uInt)ldi->filled_in_this_block,
\r
678 1,zi->filezip) !=1 )
\r
681 size_centraldir += ldi->filled_in_this_block;
\r
682 ldi = ldi->next_datablock;
\r
685 free_datablock(zi->central_dir.first_block);
\r
687 if (err==ZIP_OK) /* Magic End */
\r
688 err = ziplocal_putValue(zi->filezip,(uLong)ENDHEADERMAGIC,4);
\r
690 if (err==ZIP_OK) /* number of this disk */
\r
691 err = ziplocal_putValue(zi->filezip,(uLong)0,2);
\r
693 if (err==ZIP_OK) /* number of the disk with the start of the central directory */
\r
694 err = ziplocal_putValue(zi->filezip,(uLong)0,2);
\r
696 if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
\r
697 err = ziplocal_putValue(zi->filezip,(uLong)zi->number_entry,2);
\r
699 if (err==ZIP_OK) /* total number of entries in the central dir */
\r
700 err = ziplocal_putValue(zi->filezip,(uLong)zi->number_entry,2);
\r
702 if (err==ZIP_OK) /* size of the central directory */
\r
703 err = ziplocal_putValue(zi->filezip,(uLong)size_centraldir,4);
\r
705 if (err==ZIP_OK) /* offset of start of central directory with respect to the
\r
706 starting disk number */
\r
707 err = ziplocal_putValue(zi->filezip,(uLong)centraldir_pos_inzip ,4);
\r
709 if (err==ZIP_OK) /* zipfile comment length */
\r
710 err = ziplocal_putValue(zi->filezip,(uLong)size_global_comment,2);
\r
712 if ((err==ZIP_OK) && (size_global_comment>0))
\r
713 if (fwrite(global_comment,(uInt)size_global_comment,1,zi->filezip) !=1 )
\r
715 fclose(zi->filezip);
\r