diff options
Diffstat (limited to 'contrib/minizip/zip.c')
-rw-r--r-- | contrib/minizip/zip.c | 662 |
1 files changed, 556 insertions, 106 deletions
diff --git a/contrib/minizip/zip.c b/contrib/minizip/zip.c index 0cae64a..29c17d8 100644 --- a/contrib/minizip/zip.c +++ b/contrib/minizip/zip.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* zip.c -- IO on .zip files using zlib | 1 | /* zip.c -- IO on .zip files using zlib |
2 | Version 0.15 beta, Mar 19th, 1998, | 2 | Version 0.21, March 10th, 2003 |
3 | 3 | ||
4 | Read zip.h for more info | 4 | Read zip.h for more info |
5 | */ | 5 | */ |
@@ -8,6 +8,7 @@ | |||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
10 | #include <string.h> | 10 | #include <string.h> |
11 | #include <time.h> | ||
11 | #include "zlib.h" | 12 | #include "zlib.h" |
12 | #include "zip.h" | 13 | #include "zip.h" |
13 | 14 | ||
@@ -66,8 +67,15 @@ | |||
66 | #define SEEK_SET 0 | 67 | #define SEEK_SET 0 |
67 | #endif | 68 | #endif |
68 | 69 | ||
70 | #ifndef DEF_MEM_LEVEL | ||
71 | #if MAX_MEM_LEVEL >= 8 | ||
72 | # define DEF_MEM_LEVEL 8 | ||
73 | #else | ||
74 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL | ||
75 | #endif | ||
76 | #endif | ||
69 | const char zip_copyright[] = | 77 | const char zip_copyright[] = |
70 | " zip 0.15 Copyright 1998 Gilles Vollant "; | 78 | " zip 0.21 Copyright 1998-2003 Gilles Vollant - http://www.winimage.com/zLibDll"; |
71 | 79 | ||
72 | 80 | ||
73 | #define SIZEDATA_INDATABLOCK (4096-(4*4)) | 81 | #define SIZEDATA_INDATABLOCK (4096-(4*4)) |
@@ -99,33 +107,49 @@ typedef struct linkedlist_data_s | |||
99 | 107 | ||
100 | typedef struct | 108 | typedef struct |
101 | { | 109 | { |
102 | z_stream stream; /* zLib stream structure for inflate */ | 110 | z_stream stream; /* zLib stream structure for inflate */ |
103 | int stream_initialised; /* 1 is stream is initialised */ | 111 | int stream_initialised; /* 1 is stream is initialised */ |
104 | uInt pos_in_buffered_data; /* last written byte in buffered_data */ | 112 | uInt pos_in_buffered_data; /* last written byte in buffered_data */ |
105 | 113 | ||
106 | uLong pos_local_header; /* offset of the local header of the file | 114 | uLong pos_local_header; /* offset of the local header of the file |
107 | currenty writing */ | 115 | currenty writing */ |
108 | char* central_header; /* central header data for the current file */ | 116 | char* central_header; /* central header data for the current file */ |
109 | uLong size_centralheader; /* size of the central header for cur file */ | 117 | uLong size_centralheader; /* size of the central header for cur file */ |
110 | uLong flag; /* flag of the file currently writing */ | 118 | uLong flag; /* flag of the file currently writing */ |
111 | 119 | ||
112 | int method; /* compression method of file currenty wr.*/ | 120 | int method; /* compression method of file currenty wr.*/ |
121 | int raw; /* 1 for directly writing raw data */ | ||
113 | Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/ | 122 | Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/ |
114 | uLong dosDate; | 123 | uLong dosDate; |
115 | uLong crc32; | 124 | uLong crc32; |
125 | int encrypt; | ||
126 | #ifndef NOCRPYT | ||
127 | unsigned long keys[3]; /* keys defining the pseudo-random sequence */ | ||
128 | const unsigned long* pcrc_32_tab; | ||
129 | int crypt_header_size; | ||
130 | #endif | ||
116 | } curfile_info; | 131 | } curfile_info; |
117 | 132 | ||
118 | typedef struct | 133 | typedef struct |
119 | { | 134 | { |
120 | FILE * filezip; | 135 | zlib_filefunc_def z_filefunc; |
136 | voidpf filestream; /* io structore of the zipfile */ | ||
121 | linkedlist_data central_dir;/* datablock with central dir in construction*/ | 137 | 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.*/ | 138 | 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 */ | 139 | curfile_info ci; /* info on the file curretly writing */ |
124 | 140 | ||
125 | uLong begin_pos; /* position of the beginning of the zipfile */ | 141 | uLong begin_pos; /* position of the beginning of the zipfile */ |
142 | uLong add_position_when_writting_offset; | ||
126 | uLong number_entry; | 143 | uLong number_entry; |
127 | } zip_internal; | 144 | } zip_internal; |
128 | 145 | ||
146 | |||
147 | |||
148 | #ifndef NOCRPYT | ||
149 | #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED | ||
150 | #include "crypt.h" | ||
151 | #endif | ||
152 | |||
129 | local linkedlist_datablock_internal* allocate_new_datablock() | 153 | local linkedlist_datablock_internal* allocate_new_datablock() |
130 | { | 154 | { |
131 | linkedlist_datablock_internal* ldi; | 155 | linkedlist_datablock_internal* ldi; |
@@ -166,7 +190,7 @@ local void free_linkedlist(ll) | |||
166 | 190 | ||
167 | 191 | ||
168 | local int add_data_in_datablock(ll,buf,len) | 192 | local int add_data_in_datablock(ll,buf,len) |
169 | linkedlist_data* ll; | 193 | linkedlist_data* ll; |
170 | const void* buf; | 194 | const void* buf; |
171 | uLong len; | 195 | uLong len; |
172 | { | 196 | { |
@@ -220,32 +244,20 @@ local int add_data_in_datablock(ll,buf,len) | |||
220 | } | 244 | } |
221 | 245 | ||
222 | 246 | ||
223 | local int write_datablock(fout,ll) | ||
224 | FILE * fout; | ||
225 | linkedlist_data* ll; | ||
226 | { | ||
227 | linkedlist_datablock_internal* ldi; | ||
228 | ldi = ll->first_block; | ||
229 | while (ldi!=NULL) | ||
230 | { | ||
231 | if (ldi->filled_in_this_block > 0) | ||
232 | if (fwrite(ldi->data,(uInt)ldi->filled_in_this_block,1,fout)!=1) | ||
233 | return ZIP_ERRNO; | ||
234 | ldi = ldi->next_datablock; | ||
235 | } | ||
236 | return ZIP_OK; | ||
237 | } | ||
238 | 247 | ||
239 | /****************************************************************************/ | 248 | /****************************************************************************/ |
240 | 249 | ||
250 | #ifndef NO_ADDFILEINEXISTINGZIP | ||
241 | /* =========================================================================== | 251 | /* =========================================================================== |
242 | Outputs a long in LSB order to the given file | 252 | Inputs a long in LSB order to the given file |
243 | nbByte == 1, 2 or 4 (byte, short or long) | 253 | nbByte == 1, 2 or 4 (byte, short or long) |
244 | */ | 254 | */ |
245 | 255 | ||
246 | local int ziplocal_putValue OF((FILE *file, uLong x, int nbByte)); | 256 | local int ziplocal_putValue OF((const zlib_filefunc_def* pzlib_filefunc_def, |
247 | local int ziplocal_putValue (file, x, nbByte) | 257 | voidpf filestream, uLong x, int nbByte)); |
248 | FILE *file; | 258 | local int ziplocal_putValue (pzlib_filefunc_def, filestream, x, nbByte) |
259 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
260 | voidpf filestream; | ||
249 | uLong x; | 261 | uLong x; |
250 | int nbByte; | 262 | int nbByte; |
251 | { | 263 | { |
@@ -255,7 +267,7 @@ local int ziplocal_putValue (file, x, nbByte) | |||
255 | buf[n] = (unsigned char)(x & 0xff); | 267 | buf[n] = (unsigned char)(x & 0xff); |
256 | x >>= 8; | 268 | x >>= 8; |
257 | } | 269 | } |
258 | if (fwrite(buf,nbByte,1,file)!=1) | 270 | if (ZWRITE(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte) |
259 | return ZIP_ERRNO; | 271 | return ZIP_ERRNO; |
260 | else | 272 | else |
261 | return ZIP_OK; | 273 | return ZIP_OK; |
@@ -278,7 +290,7 @@ local void ziplocal_putValue_inmemory (dest, x, nbByte) | |||
278 | 290 | ||
279 | 291 | ||
280 | local uLong ziplocal_TmzDateToDosDate(ptm,dosDate) | 292 | local uLong ziplocal_TmzDateToDosDate(ptm,dosDate) |
281 | tm_zip* ptm; | 293 | const tm_zip* ptm; |
282 | uLong dosDate; | 294 | uLong dosDate; |
283 | { | 295 | { |
284 | uLong year = (uLong)ptm->tm_year; | 296 | uLong year = (uLong)ptm->tm_year; |
@@ -294,38 +306,348 @@ local uLong ziplocal_TmzDateToDosDate(ptm,dosDate) | |||
294 | 306 | ||
295 | /****************************************************************************/ | 307 | /****************************************************************************/ |
296 | 308 | ||
297 | extern zipFile ZEXPORT zipOpen (pathname, append) | 309 | local int ziplocal_getByte OF(( |
310 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
311 | voidpf filestream, | ||
312 | int *pi)); | ||
313 | |||
314 | local int ziplocal_getByte(pzlib_filefunc_def,filestream,pi) | ||
315 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
316 | voidpf filestream; | ||
317 | int *pi; | ||
318 | { | ||
319 | unsigned char c; | ||
320 | int err = (int)ZREAD(*pzlib_filefunc_def,filestream,&c,1); | ||
321 | if (err==1) | ||
322 | { | ||
323 | *pi = (int)c; | ||
324 | return ZIP_OK; | ||
325 | } | ||
326 | else | ||
327 | { | ||
328 | if (ZERROR(*pzlib_filefunc_def,filestream)) | ||
329 | return ZIP_ERRNO; | ||
330 | else | ||
331 | return ZIP_EOF; | ||
332 | } | ||
333 | } | ||
334 | |||
335 | |||
336 | /* =========================================================================== | ||
337 | Reads a long in LSB order from the given gz_stream. Sets | ||
338 | */ | ||
339 | local int ziplocal_getShort OF(( | ||
340 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
341 | voidpf filestream, | ||
342 | uLong *pX)); | ||
343 | |||
344 | local int ziplocal_getShort (pzlib_filefunc_def,filestream,pX) | ||
345 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
346 | voidpf filestream; | ||
347 | uLong *pX; | ||
348 | { | ||
349 | uLong x ; | ||
350 | int i; | ||
351 | int err; | ||
352 | |||
353 | err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
354 | x = (uLong)i; | ||
355 | |||
356 | if (err==ZIP_OK) | ||
357 | err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
358 | x += ((uLong)i)<<8; | ||
359 | |||
360 | if (err==ZIP_OK) | ||
361 | *pX = x; | ||
362 | else | ||
363 | *pX = 0; | ||
364 | return err; | ||
365 | } | ||
366 | |||
367 | local int ziplocal_getLong OF(( | ||
368 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
369 | voidpf filestream, | ||
370 | uLong *pX)); | ||
371 | |||
372 | local int ziplocal_getLong (pzlib_filefunc_def,filestream,pX) | ||
373 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
374 | voidpf filestream; | ||
375 | uLong *pX; | ||
376 | { | ||
377 | uLong x ; | ||
378 | int i; | ||
379 | int err; | ||
380 | |||
381 | err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
382 | x = (uLong)i; | ||
383 | |||
384 | if (err==ZIP_OK) | ||
385 | err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
386 | x += ((uLong)i)<<8; | ||
387 | |||
388 | if (err==ZIP_OK) | ||
389 | err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
390 | x += ((uLong)i)<<16; | ||
391 | |||
392 | if (err==ZIP_OK) | ||
393 | err = ziplocal_getByte(pzlib_filefunc_def,filestream,&i); | ||
394 | x += ((uLong)i)<<24; | ||
395 | |||
396 | if (err==ZIP_OK) | ||
397 | *pX = x; | ||
398 | else | ||
399 | *pX = 0; | ||
400 | return err; | ||
401 | } | ||
402 | |||
403 | #ifndef BUFREADCOMMENT | ||
404 | #define BUFREADCOMMENT (0x400) | ||
405 | #endif | ||
406 | /* | ||
407 | Locate the Central directory of a zipfile (at the end, just before | ||
408 | the global comment) | ||
409 | */ | ||
410 | local uLong ziplocal_SearchCentralDir OF(( | ||
411 | const zlib_filefunc_def* pzlib_filefunc_def, | ||
412 | voidpf filestream)); | ||
413 | |||
414 | local uLong ziplocal_SearchCentralDir(pzlib_filefunc_def,filestream) | ||
415 | const zlib_filefunc_def* pzlib_filefunc_def; | ||
416 | voidpf filestream; | ||
417 | { | ||
418 | unsigned char* buf; | ||
419 | uLong uSizeFile; | ||
420 | uLong uBackRead; | ||
421 | uLong uMaxBack=0xffff; /* maximum size of global comment */ | ||
422 | uLong uPosFound=0; | ||
423 | |||
424 | if (ZSEEK(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0) | ||
425 | return 0; | ||
426 | |||
427 | |||
428 | uSizeFile = ZTELL(*pzlib_filefunc_def,filestream); | ||
429 | |||
430 | if (uMaxBack>uSizeFile) | ||
431 | uMaxBack = uSizeFile; | ||
432 | |||
433 | buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); | ||
434 | if (buf==NULL) | ||
435 | return 0; | ||
436 | |||
437 | uBackRead = 4; | ||
438 | while (uBackRead<uMaxBack) | ||
439 | { | ||
440 | uLong uReadSize,uReadPos ; | ||
441 | int i; | ||
442 | if (uBackRead+BUFREADCOMMENT>uMaxBack) | ||
443 | uBackRead = uMaxBack; | ||
444 | else | ||
445 | uBackRead+=BUFREADCOMMENT; | ||
446 | uReadPos = uSizeFile-uBackRead ; | ||
447 | |||
448 | uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? | ||
449 | (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); | ||
450 | if (ZSEEK(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
451 | break; | ||
452 | |||
453 | if (ZREAD(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize) | ||
454 | break; | ||
455 | |||
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)) | ||
459 | { | ||
460 | uPosFound = uReadPos+i; | ||
461 | break; | ||
462 | } | ||
463 | |||
464 | if (uPosFound!=0) | ||
465 | break; | ||
466 | } | ||
467 | TRYFREE(buf); | ||
468 | return uPosFound; | ||
469 | } | ||
470 | #endif /* !NO_ADDFILEINEXISTINGZIP*/ | ||
471 | |||
472 | /************************************************************/ | ||
473 | extern zipFile ZEXPORT zipOpen2 (pathname, append, globalcomment, pzlib_filefunc_def) | ||
298 | const char *pathname; | 474 | const char *pathname; |
299 | int append; | 475 | int append; |
476 | zipcharpc* globalcomment; | ||
477 | zlib_filefunc_def* pzlib_filefunc_def; | ||
300 | { | 478 | { |
301 | zip_internal ziinit; | 479 | zip_internal ziinit; |
302 | zip_internal* zi; | 480 | zip_internal* zi; |
481 | int err=ZIP_OK; | ||
482 | |||
303 | 483 | ||
304 | ziinit.filezip = fopen(pathname,(append == 0) ? "wb" : "ab"); | 484 | if (pzlib_filefunc_def==NULL) |
305 | if (ziinit.filezip == NULL) | 485 | fill_fopen_filefunc(&ziinit.z_filefunc); |
486 | else | ||
487 | ziinit.z_filefunc = *pzlib_filefunc_def; | ||
488 | |||
489 | ziinit.filestream = (*(ziinit.z_filefunc.zopen_file)) | ||
490 | (ziinit.z_filefunc.opaque, | ||
491 | pathname, | ||
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)); | ||
495 | |||
496 | if (ziinit.filestream == NULL) | ||
306 | return NULL; | 497 | return NULL; |
307 | ziinit.begin_pos = ftell(ziinit.filezip); | 498 | ziinit.begin_pos = ZTELL(ziinit.z_filefunc,ziinit.filestream); |
308 | ziinit.in_opened_file_inzip = 0; | 499 | ziinit.in_opened_file_inzip = 0; |
309 | ziinit.ci.stream_initialised = 0; | 500 | ziinit.ci.stream_initialised = 0; |
310 | ziinit.number_entry = 0; | 501 | ziinit.number_entry = 0; |
502 | ziinit.add_position_when_writting_offset = 0; | ||
311 | init_linkedlist(&(ziinit.central_dir)); | 503 | init_linkedlist(&(ziinit.central_dir)); |
312 | 504 | ||
313 | 505 | ||
314 | zi = (zip_internal*)ALLOC(sizeof(zip_internal)); | 506 | zi = (zip_internal*)ALLOC(sizeof(zip_internal)); |
315 | if (zi==NULL) | 507 | if (zi==NULL) |
316 | { | 508 | { |
317 | fclose(ziinit.filezip); | 509 | ZCLOSE(ziinit.z_filefunc,ziinit.filestream); |
318 | return NULL; | 510 | return NULL; |
319 | } | 511 | } |
320 | 512 | ||
321 | *zi = ziinit; | 513 | /* now we add file in a zipfile */ |
322 | return (zipFile)zi; | 514 | #ifndef NO_ADDFILEINEXISTINGZIP |
515 | if (append == APPEND_STATUS_ADDINZIP) | ||
516 | { | ||
517 | uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ | ||
518 | |||
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; | ||
522 | |||
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*/ | ||
527 | uLong number_entry; | ||
528 | uLong number_entry_CD; /* total number of entries in | ||
529 | the central dir | ||
530 | (same than number_entry on nospan) */ | ||
531 | uLong size_comment; | ||
532 | |||
533 | central_pos = ziplocal_SearchCentralDir(&ziinit.z_filefunc,ziinit.filestream); | ||
534 | if (central_pos==0) | ||
535 | err=ZIP_ERRNO; | ||
536 | |||
537 | if (ZSEEK(ziinit.z_filefunc, ziinit.filestream, | ||
538 | central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
539 | err=ZIP_ERRNO; | ||
540 | |||
541 | /* the signature, already checked */ | ||
542 | if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&uL)!=ZIP_OK) | ||
543 | err=ZIP_ERRNO; | ||
544 | |||
545 | /* number of this disk */ | ||
546 | if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_disk)!=ZIP_OK) | ||
547 | err=ZIP_ERRNO; | ||
548 | |||
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) | ||
551 | err=ZIP_ERRNO; | ||
552 | |||
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) | ||
555 | err=ZIP_ERRNO; | ||
556 | |||
557 | /* total number of entries in the central dir */ | ||
558 | if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&number_entry_CD)!=ZIP_OK) | ||
559 | err=ZIP_ERRNO; | ||
560 | |||
561 | if ((number_entry_CD!=number_entry) || | ||
562 | (number_disk_with_CD!=0) || | ||
563 | (number_disk!=0)) | ||
564 | err=ZIP_BADZIPFILE; | ||
565 | |||
566 | /* size of the central directory */ | ||
567 | if (ziplocal_getLong(&ziinit.z_filefunc, ziinit.filestream,&size_central_dir)!=ZIP_OK) | ||
568 | err=ZIP_ERRNO; | ||
569 | |||
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) | ||
573 | err=ZIP_ERRNO; | ||
574 | |||
575 | /* zipfile comment length */ | ||
576 | if (ziplocal_getShort(&ziinit.z_filefunc, ziinit.filestream,&size_comment)!=ZIP_OK) | ||
577 | err=ZIP_ERRNO; | ||
578 | |||
579 | if ((central_pos<offset_central_dir+size_central_dir) && | ||
580 | (err==ZIP_OK)) | ||
581 | err=ZIP_BADZIPFILE; | ||
582 | |||
583 | if (err!=ZIP_OK) | ||
584 | { | ||
585 | ZCLOSE(ziinit.z_filefunc, ziinit.filestream); | ||
586 | return NULL; | ||
587 | } | ||
588 | |||
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 ; | ||
592 | |||
593 | { | ||
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) | ||
600 | err=ZIP_ERRNO; | ||
601 | |||
602 | while ((size_central_dir_to_read>0) && (err==ZIP_OK)) | ||
603 | { | ||
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) | ||
608 | err=ZIP_ERRNO; | ||
609 | |||
610 | if (err==ZIP_OK) | ||
611 | err = add_data_in_datablock(&ziinit.central_dir,buf_read, | ||
612 | (uLong)read_this); | ||
613 | size_central_dir_to_read-=read_this; | ||
614 | } | ||
615 | TRYFREE(buf_read); | ||
616 | } | ||
617 | ziinit.begin_pos = byte_before_the_zipfile; | ||
618 | ziinit.number_entry = number_entry_CD; | ||
619 | |||
620 | if (ZSEEK(ziinit.z_filefunc, ziinit.filestream, | ||
621 | offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0) | ||
622 | err=ZIP_ERRNO; | ||
623 | } | ||
624 | #endif /* !NO_ADDFILEINEXISTINGZIP*/ | ||
625 | |||
626 | if (err != ZIP_OK) | ||
627 | { | ||
628 | TRYFREE(zi); | ||
629 | return NULL; | ||
630 | } | ||
631 | else | ||
632 | { | ||
633 | *zi = ziinit; | ||
634 | return (zipFile)zi; | ||
635 | } | ||
323 | } | 636 | } |
324 | 637 | ||
325 | extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | 638 | extern zipFile ZEXPORT zipOpen (pathname, append) |
326 | extrafield_local, size_extrafield_local, | 639 | const char *pathname; |
327 | extrafield_global, size_extrafield_global, | 640 | int append; |
328 | comment, method, level) | 641 | { |
642 | return zipOpen2(pathname,append,NULL,NULL); | ||
643 | } | ||
644 | |||
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) | ||
329 | zipFile file; | 651 | zipFile file; |
330 | const char* filename; | 652 | const char* filename; |
331 | const zip_fileinfo* zipfi; | 653 | const zip_fileinfo* zipfi; |
@@ -336,6 +658,12 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
336 | const char* comment; | 658 | const char* comment; |
337 | int method; | 659 | int method; |
338 | int level; | 660 | int level; |
661 | int raw; | ||
662 | int windowBits; | ||
663 | int memLevel; | ||
664 | int strategy; | ||
665 | const char* password; | ||
666 | uLong crcForCrypting; | ||
339 | { | 667 | { |
340 | zip_internal* zi; | 668 | zip_internal* zi; |
341 | uInt size_filename; | 669 | uInt size_filename; |
@@ -343,6 +671,11 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
343 | uInt i; | 671 | uInt i; |
344 | int err = ZIP_OK; | 672 | int err = ZIP_OK; |
345 | 673 | ||
674 | #ifdef NOCRPYT | ||
675 | if (password != NULL) | ||
676 | return ZIP_PARAMERROR; | ||
677 | #endif | ||
678 | |||
346 | if (file == NULL) | 679 | if (file == NULL) |
347 | return ZIP_PARAMERROR; | 680 | return ZIP_PARAMERROR; |
348 | if ((method!=0) && (method!=Z_DEFLATED)) | 681 | if ((method!=0) && (method!=Z_DEFLATED)) |
@@ -384,13 +717,17 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
384 | zi->ci.flag |= 4; | 717 | zi->ci.flag |= 4; |
385 | if ((level==1)) | 718 | if ((level==1)) |
386 | zi->ci.flag |= 6; | 719 | zi->ci.flag |= 6; |
720 | if (password != NULL) | ||
721 | zi->ci.flag |= 1; | ||
387 | 722 | ||
388 | zi->ci.crc32 = 0; | 723 | zi->ci.crc32 = 0; |
389 | zi->ci.method = method; | 724 | zi->ci.method = method; |
725 | zi->ci.encrypt = 0; | ||
390 | zi->ci.stream_initialised = 0; | 726 | zi->ci.stream_initialised = 0; |
391 | zi->ci.pos_in_buffered_data = 0; | 727 | zi->ci.pos_in_buffered_data = 0; |
392 | zi->ci.pos_local_header = ftell(zi->filezip); | 728 | zi->ci.raw = raw; |
393 | zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + | 729 | zi->ci.pos_local_header = ZTELL(zi->z_filefunc,zi->filestream) ; |
730 | zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + | ||
394 | size_extrafield_global + size_comment; | 731 | size_extrafield_global + size_comment; |
395 | zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader); | 732 | zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader); |
396 | 733 | ||
@@ -410,16 +747,16 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
410 | ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/ | 747 | ziplocal_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/ |
411 | 748 | ||
412 | if (zipfi==NULL) | 749 | if (zipfi==NULL) |
413 | ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2); | 750 | ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2); |
414 | else | 751 | else |
415 | ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2); | 752 | ziplocal_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2); |
416 | 753 | ||
417 | if (zipfi==NULL) | 754 | if (zipfi==NULL) |
418 | ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4); | 755 | ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4); |
419 | else | 756 | else |
420 | ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4); | 757 | ziplocal_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4); |
421 | 758 | ||
422 | ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header,4); | 759 | ziplocal_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header- zi->add_position_when_writting_offset,4); |
423 | 760 | ||
424 | for (i=0;i<size_filename;i++) | 761 | for (i=0;i<size_filename;i++) |
425 | *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i); | 762 | *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i); |
@@ -430,44 +767,44 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
430 | 767 | ||
431 | for (i=0;i<size_comment;i++) | 768 | for (i=0;i<size_comment;i++) |
432 | *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+ | 769 | *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+ |
433 | size_extrafield_global+i) = *(filename+i); | 770 | size_extrafield_global+i) = *(comment+i); |
434 | if (zi->ci.central_header == NULL) | 771 | if (zi->ci.central_header == NULL) |
435 | return ZIP_INTERNALERROR; | 772 | return ZIP_INTERNALERROR; |
436 | 773 | ||
437 | /* write the local header */ | 774 | /* write the local header */ |
438 | err = ziplocal_putValue(zi->filezip,(uLong)LOCALHEADERMAGIC,4); | 775 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC,4); |
439 | 776 | ||
440 | if (err==ZIP_OK) | 777 | if (err==ZIP_OK) |
441 | err = ziplocal_putValue(zi->filezip,(uLong)20,2);/* version needed to extract */ | 778 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */ |
442 | if (err==ZIP_OK) | 779 | if (err==ZIP_OK) |
443 | err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.flag,2); | 780 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2); |
444 | 781 | ||
445 | if (err==ZIP_OK) | 782 | if (err==ZIP_OK) |
446 | err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.method,2); | 783 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2); |
447 | 784 | ||
448 | if (err==ZIP_OK) | 785 | if (err==ZIP_OK) |
449 | err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.dosDate,4); | 786 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4); |
450 | 787 | ||
451 | if (err==ZIP_OK) | 788 | if (err==ZIP_OK) |
452 | err = ziplocal_putValue(zi->filezip,(uLong)0,4); /* crc 32, unknown */ | 789 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */ |
453 | if (err==ZIP_OK) | 790 | if (err==ZIP_OK) |
454 | err = ziplocal_putValue(zi->filezip,(uLong)0,4); /* compressed size, unknown */ | 791 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */ |
455 | if (err==ZIP_OK) | 792 | if (err==ZIP_OK) |
456 | err = ziplocal_putValue(zi->filezip,(uLong)0,4); /* uncompressed size, unknown */ | 793 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */ |
457 | 794 | ||
458 | if (err==ZIP_OK) | 795 | if (err==ZIP_OK) |
459 | err = ziplocal_putValue(zi->filezip,(uLong)size_filename,2); | 796 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2); |
460 | 797 | ||
461 | if (err==ZIP_OK) | 798 | if (err==ZIP_OK) |
462 | err = ziplocal_putValue(zi->filezip,(uLong)size_extrafield_local,2); | 799 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield_local,2); |
463 | 800 | ||
464 | if ((err==ZIP_OK) && (size_filename>0)) | 801 | if ((err==ZIP_OK) && (size_filename>0)) |
465 | if (fwrite(filename,(uInt)size_filename,1,zi->filezip)!=1) | 802 | if (ZWRITE(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename) |
466 | err = ZIP_ERRNO; | 803 | err = ZIP_ERRNO; |
467 | 804 | ||
468 | if ((err==ZIP_OK) && (size_extrafield_local>0)) | 805 | if ((err==ZIP_OK) && (size_extrafield_local>0)) |
469 | if (fwrite(extrafield_local,(uInt)size_extrafield_local,1,zi->filezip) | 806 | if (ZWRITE(zi->z_filefunc,zi->filestream,extrafield_local,size_extrafield_local) |
470 | !=1) | 807 | !=size_extrafield_local) |
471 | err = ZIP_ERRNO; | 808 | err = ZIP_ERRNO; |
472 | 809 | ||
473 | zi->ci.stream.avail_in = (uInt)0; | 810 | zi->ci.stream.avail_in = (uInt)0; |
@@ -476,17 +813,38 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
476 | zi->ci.stream.total_in = 0; | 813 | zi->ci.stream.total_in = 0; |
477 | zi->ci.stream.total_out = 0; | 814 | zi->ci.stream.total_out = 0; |
478 | 815 | ||
479 | if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED)) | 816 | if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) |
480 | { | 817 | { |
481 | zi->ci.stream.zalloc = (alloc_func)0; | 818 | zi->ci.stream.zalloc = (alloc_func)0; |
482 | zi->ci.stream.zfree = (free_func)0; | 819 | zi->ci.stream.zfree = (free_func)0; |
483 | zi->ci.stream.opaque = (voidpf)0; | 820 | zi->ci.stream.opaque = (voidpf)0; |
484 | 821 | ||
822 | if (windowBits>0) | ||
823 | windowBits = -windowBits; | ||
824 | |||
485 | err = deflateInit2(&zi->ci.stream, level, | 825 | err = deflateInit2(&zi->ci.stream, level, |
486 | Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0); | 826 | Z_DEFLATED, windowBits, memLevel, strategy); |
487 | 827 | ||
488 | if (err==Z_OK) | 828 | if (err==Z_OK) |
489 | zi->ci.stream_initialised = 1; | 829 | zi->ci.stream_initialised = 1; |
830 | |||
831 | #ifndef NOCRPYT | ||
832 | zi->ci.crypt_header_size = 0; | ||
833 | if ((err==Z_OK) && (password != NULL)) | ||
834 | { | ||
835 | unsigned char bufHead[RAND_HEAD_LEN]; | ||
836 | unsigned int sizeHead; | ||
837 | zi->ci.encrypt = 1; | ||
838 | zi->ci.pcrc_32_tab = get_crc_table(); | ||
839 | /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/ | ||
840 | |||
841 | sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting); | ||
842 | zi->ci.crypt_header_size = sizeHead; | ||
843 | |||
844 | if (ZWRITE(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead) | ||
845 | err = ZIP_ERRNO; | ||
846 | } | ||
847 | #endif | ||
490 | } | 848 | } |
491 | 849 | ||
492 | 850 | ||
@@ -495,9 +853,74 @@ extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | |||
495 | return err; | 853 | return err; |
496 | } | 854 | } |
497 | 855 | ||
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) | ||
860 | zipFile file; | ||
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; | ||
867 | const char* comment; | ||
868 | int method; | ||
869 | int level; | ||
870 | int raw; | ||
871 | { | ||
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, | ||
877 | NULL, 0); | ||
878 | } | ||
879 | |||
880 | extern int ZEXPORT zipOpenNewFileInZip (file, filename, zipfi, | ||
881 | extrafield_local, size_extrafield_local, | ||
882 | extrafield_global, size_extrafield_global, | ||
883 | comment, method, level) | ||
884 | zipFile file; | ||
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; | ||
891 | const char* comment; | ||
892 | int method; | ||
893 | int level; | ||
894 | { | ||
895 | return zipOpenNewFileInZip2 (file, filename, zipfi, | ||
896 | extrafield_local, size_extrafield_local, | ||
897 | extrafield_global, size_extrafield_global, | ||
898 | comment, method, level, 0); | ||
899 | } | ||
900 | |||
901 | local int zipFlushWriteBuffer(zi) | ||
902 | zip_internal* zi; | ||
903 | { | ||
904 | int err=ZIP_OK; | ||
905 | |||
906 | if (zi->ci.encrypt != 0) | ||
907 | { | ||
908 | uInt i; | ||
909 | int t; | ||
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); | ||
913 | } | ||
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) | ||
916 | err = ZIP_ERRNO; | ||
917 | zi->ci.pos_in_buffered_data = 0; | ||
918 | return err; | ||
919 | } | ||
920 | |||
498 | extern int ZEXPORT zipWriteInFileInZip (file, buf, len) | 921 | extern int ZEXPORT zipWriteInFileInZip (file, buf, len) |
499 | zipFile file; | 922 | zipFile file; |
500 | const voidp buf; | 923 | const void* buf; |
501 | unsigned len; | 924 | unsigned len; |
502 | { | 925 | { |
503 | zip_internal* zi; | 926 | zip_internal* zi; |
@@ -510,7 +933,7 @@ extern int ZEXPORT zipWriteInFileInZip (file, buf, len) | |||
510 | if (zi->in_opened_file_inzip == 0) | 933 | if (zi->in_opened_file_inzip == 0) |
511 | return ZIP_PARAMERROR; | 934 | return ZIP_PARAMERROR; |
512 | 935 | ||
513 | zi->ci.stream.next_in = buf; | 936 | zi->ci.stream.next_in = (void*)buf; |
514 | zi->ci.stream.avail_in = len; | 937 | zi->ci.stream.avail_in = len; |
515 | zi->ci.crc32 = crc32(zi->ci.crc32,buf,len); | 938 | zi->ci.crc32 = crc32(zi->ci.crc32,buf,len); |
516 | 939 | ||
@@ -518,15 +941,17 @@ extern int ZEXPORT zipWriteInFileInZip (file, buf, len) | |||
518 | { | 941 | { |
519 | if (zi->ci.stream.avail_out == 0) | 942 | if (zi->ci.stream.avail_out == 0) |
520 | { | 943 | { |
521 | if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip) | 944 | if (zipFlushWriteBuffer(zi) == ZIP_ERRNO) |
522 | !=1) | ||
523 | err = ZIP_ERRNO; | 945 | err = ZIP_ERRNO; |
524 | zi->ci.pos_in_buffered_data = 0; | ||
525 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; | 946 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; |
526 | zi->ci.stream.next_out = zi->ci.buffered_data; | 947 | zi->ci.stream.next_out = zi->ci.buffered_data; |
527 | } | 948 | } |
528 | 949 | ||
529 | if (zi->ci.method == Z_DEFLATED) | 950 | |
951 | if(err != ZIP_OK) | ||
952 | break; | ||
953 | |||
954 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) | ||
530 | { | 955 | { |
531 | uLong uTotalOutBefore = zi->ci.stream.total_out; | 956 | uLong uTotalOutBefore = zi->ci.stream.total_out; |
532 | err=deflate(&zi->ci.stream, Z_NO_FLUSH); | 957 | err=deflate(&zi->ci.stream, Z_NO_FLUSH); |
@@ -555,33 +980,34 @@ extern int ZEXPORT zipWriteInFileInZip (file, buf, len) | |||
555 | } | 980 | } |
556 | } | 981 | } |
557 | 982 | ||
558 | return 0; | 983 | return err; |
559 | } | 984 | } |
560 | 985 | ||
561 | extern int ZEXPORT zipCloseFileInZip (file) | 986 | extern int ZEXPORT zipCloseFileInZipRaw (file, uncompressed_size, crc32) |
562 | zipFile file; | 987 | zipFile file; |
988 | uLong uncompressed_size; | ||
989 | uLong crc32; | ||
563 | { | 990 | { |
564 | zip_internal* zi; | 991 | zip_internal* zi; |
992 | uLong compressed_size; | ||
565 | int err=ZIP_OK; | 993 | int err=ZIP_OK; |
566 | 994 | ||
567 | if (file == NULL) | 995 | if (file == NULL) |
568 | return ZIP_PARAMERROR; | 996 | return ZIP_PARAMERROR; |
569 | zi = (zip_internal*)file; | 997 | zi = (zip_internal*)file; |
570 | 998 | ||
571 | if (zi->in_opened_file_inzip == 0) | 999 | if (zi->in_opened_file_inzip == 0) |
572 | return ZIP_PARAMERROR; | 1000 | return ZIP_PARAMERROR; |
573 | zi->ci.stream.avail_in = 0; | 1001 | zi->ci.stream.avail_in = 0; |
574 | 1002 | ||
575 | if (zi->ci.method == Z_DEFLATED) | 1003 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) |
576 | while (err==ZIP_OK) | 1004 | while (err==ZIP_OK) |
577 | { | 1005 | { |
578 | uLong uTotalOutBefore; | 1006 | uLong uTotalOutBefore; |
579 | if (zi->ci.stream.avail_out == 0) | 1007 | if (zi->ci.stream.avail_out == 0) |
580 | { | 1008 | { |
581 | if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip) | 1009 | if (zipFlushWriteBuffer(zi) == ZIP_ERRNO) |
582 | !=1) | ||
583 | err = ZIP_ERRNO; | 1010 | err = ZIP_ERRNO; |
584 | zi->ci.pos_in_buffered_data = 0; | ||
585 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; | 1011 | zi->ci.stream.avail_out = (uInt)Z_BUFSIZE; |
586 | zi->ci.stream.next_out = zi->ci.buffered_data; | 1012 | zi->ci.stream.next_out = zi->ci.buffered_data; |
587 | } | 1013 | } |
@@ -594,21 +1020,32 @@ extern int ZEXPORT zipCloseFileInZip (file) | |||
594 | err=ZIP_OK; /* this is normal */ | 1020 | err=ZIP_OK; /* this is normal */ |
595 | 1021 | ||
596 | if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK)) | 1022 | if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK)) |
597 | if (fwrite(zi->ci.buffered_data,(uInt)zi->ci.pos_in_buffered_data,1,zi->filezip) | 1023 | if (zipFlushWriteBuffer(zi)==ZIP_ERRNO) |
598 | !=1) | ||
599 | err = ZIP_ERRNO; | 1024 | err = ZIP_ERRNO; |
600 | 1025 | ||
601 | if ((zi->ci.method == Z_DEFLATED) && (err==ZIP_OK)) | 1026 | if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) |
602 | { | 1027 | { |
603 | err=deflateEnd(&zi->ci.stream); | 1028 | err=deflateEnd(&zi->ci.stream); |
604 | zi->ci.stream_initialised = 0; | 1029 | zi->ci.stream_initialised = 0; |
605 | } | 1030 | } |
606 | 1031 | ||
607 | ziplocal_putValue_inmemory(zi->ci.central_header+16,(uLong)zi->ci.crc32,4); /*crc*/ | 1032 | if (!zi->ci.raw) |
1033 | { | ||
1034 | crc32 = (uLong)zi->ci.crc32; | ||
1035 | uncompressed_size = (uLong)zi->ci.stream.total_in; | ||
1036 | } | ||
1037 | compressed_size = (uLong)zi->ci.stream.total_out; | ||
1038 | #ifndef NOCRPYT | ||
1039 | compressed_size += zi->ci.crypt_header_size; | ||
1040 | #endif | ||
1041 | |||
1042 | ziplocal_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/ | ||
608 | ziplocal_putValue_inmemory(zi->ci.central_header+20, | 1043 | ziplocal_putValue_inmemory(zi->ci.central_header+20, |
609 | (uLong)zi->ci.stream.total_out,4); /*compr size*/ | 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); | ||
610 | ziplocal_putValue_inmemory(zi->ci.central_header+24, | 1047 | ziplocal_putValue_inmemory(zi->ci.central_header+24, |
611 | (uLong)zi->ci.stream.total_in,4); /*uncompr size*/ | 1048 | uncompressed_size,4); /*uncompr size*/ |
612 | 1049 | ||
613 | if (err==ZIP_OK) | 1050 | if (err==ZIP_OK) |
614 | err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header, | 1051 | err = add_data_in_datablock(&zi->central_dir,zi->ci.central_header, |
@@ -617,23 +1054,23 @@ extern int ZEXPORT zipCloseFileInZip (file) | |||
617 | 1054 | ||
618 | if (err==ZIP_OK) | 1055 | if (err==ZIP_OK) |
619 | { | 1056 | { |
620 | long cur_pos_inzip = ftell(zi->filezip); | 1057 | long cur_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream); |
621 | if (fseek(zi->filezip, | 1058 | if (ZSEEK(zi->z_filefunc,zi->filestream, |
622 | zi->ci.pos_local_header + 14,SEEK_SET)!=0) | 1059 | zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0) |
623 | err = ZIP_ERRNO; | 1060 | err = ZIP_ERRNO; |
624 | 1061 | ||
625 | if (err==ZIP_OK) | 1062 | if (err==ZIP_OK) |
626 | err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.crc32,4); /* crc 32, unknown */ | 1063 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */ |
627 | 1064 | ||
628 | if (err==ZIP_OK) /* compressed size, unknown */ | 1065 | if (err==ZIP_OK) /* compressed size, unknown */ |
629 | err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.stream.total_out,4); | 1066 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4); |
630 | 1067 | ||
631 | if (err==ZIP_OK) /* uncompressed size, unknown */ | 1068 | if (err==ZIP_OK) /* uncompressed size, unknown */ |
632 | err = ziplocal_putValue(zi->filezip,(uLong)zi->ci.stream.total_in,4); | 1069 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4); |
633 | 1070 | ||
634 | if (fseek(zi->filezip, | 1071 | if (ZSEEK(zi->z_filefunc,zi->filestream, |
635 | cur_pos_inzip,SEEK_SET)!=0) | 1072 | cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0) |
636 | err = ZIP_ERRNO; | 1073 | err = ZIP_ERRNO; |
637 | } | 1074 | } |
638 | 1075 | ||
639 | zi->number_entry ++; | 1076 | zi->number_entry ++; |
@@ -642,6 +1079,12 @@ extern int ZEXPORT zipCloseFileInZip (file) | |||
642 | return err; | 1079 | return err; |
643 | } | 1080 | } |
644 | 1081 | ||
1082 | extern int ZEXPORT zipCloseFileInZip (file) | ||
1083 | zipFile file; | ||
1084 | { | ||
1085 | return zipCloseFileInZipRaw (file,0,0); | ||
1086 | } | ||
1087 | |||
645 | extern int ZEXPORT zipClose (file, global_comment) | 1088 | extern int ZEXPORT zipClose (file, global_comment) |
646 | zipFile file; | 1089 | zipFile file; |
647 | const char* global_comment; | 1090 | const char* global_comment; |
@@ -666,15 +1109,16 @@ extern int ZEXPORT zipClose (file, global_comment) | |||
666 | size_global_comment = strlen(global_comment); | 1109 | size_global_comment = strlen(global_comment); |
667 | 1110 | ||
668 | 1111 | ||
669 | centraldir_pos_inzip = ftell(zi->filezip); | 1112 | centraldir_pos_inzip = ZTELL(zi->z_filefunc,zi->filestream); |
670 | if (err==ZIP_OK) | 1113 | if (err==ZIP_OK) |
671 | { | 1114 | { |
672 | linkedlist_datablock_internal* ldi = zi->central_dir.first_block ; | 1115 | linkedlist_datablock_internal* ldi = zi->central_dir.first_block ; |
673 | while (ldi!=NULL) | 1116 | while (ldi!=NULL) |
674 | { | 1117 | { |
675 | if ((err==ZIP_OK) && (ldi->filled_in_this_block>0)) | 1118 | if ((err==ZIP_OK) && (ldi->filled_in_this_block>0)) |
676 | if (fwrite(ldi->data,(uInt)ldi->filled_in_this_block, | 1119 | if (ZWRITE(zi->z_filefunc,zi->filestream, |
677 | 1,zi->filezip) !=1 ) | 1120 | ldi->data,ldi->filled_in_this_block) |
1121 | !=ldi->filled_in_this_block ) | ||
678 | err = ZIP_ERRNO; | 1122 | err = ZIP_ERRNO; |
679 | 1123 | ||
680 | size_centraldir += ldi->filled_in_this_block; | 1124 | size_centraldir += ldi->filled_in_this_block; |
@@ -684,34 +1128,40 @@ extern int ZEXPORT zipClose (file, global_comment) | |||
684 | free_datablock(zi->central_dir.first_block); | 1128 | free_datablock(zi->central_dir.first_block); |
685 | 1129 | ||
686 | if (err==ZIP_OK) /* Magic End */ | 1130 | if (err==ZIP_OK) /* Magic End */ |
687 | err = ziplocal_putValue(zi->filezip,(uLong)ENDHEADERMAGIC,4); | 1131 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4); |
688 | 1132 | ||
689 | if (err==ZIP_OK) /* number of this disk */ | 1133 | if (err==ZIP_OK) /* number of this disk */ |
690 | err = ziplocal_putValue(zi->filezip,(uLong)0,2); | 1134 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2); |
691 | 1135 | ||
692 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ | 1136 | if (err==ZIP_OK) /* number of the disk with the start of the central directory */ |
693 | err = ziplocal_putValue(zi->filezip,(uLong)0,2); | 1137 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2); |
694 | 1138 | ||
695 | if (err==ZIP_OK) /* total number of entries in the central dir on this disk */ | 1139 | if (err==ZIP_OK) /* total number of entries in the central dir on this disk */ |
696 | err = ziplocal_putValue(zi->filezip,(uLong)zi->number_entry,2); | 1140 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2); |
697 | 1141 | ||
698 | if (err==ZIP_OK) /* total number of entries in the central dir */ | 1142 | if (err==ZIP_OK) /* total number of entries in the central dir */ |
699 | err = ziplocal_putValue(zi->filezip,(uLong)zi->number_entry,2); | 1143 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2); |
700 | 1144 | ||
701 | if (err==ZIP_OK) /* size of the central directory */ | 1145 | if (err==ZIP_OK) /* size of the central directory */ |
702 | err = ziplocal_putValue(zi->filezip,(uLong)size_centraldir,4); | 1146 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4); |
703 | 1147 | ||
704 | if (err==ZIP_OK) /* offset of start of central directory with respect to the | 1148 | if (err==ZIP_OK) /* offset of start of central directory with respect to the |
705 | starting disk number */ | 1149 | starting disk number */ |
706 | err = ziplocal_putValue(zi->filezip,(uLong)centraldir_pos_inzip ,4); | 1150 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream, |
1151 | (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4); | ||
707 | 1152 | ||
708 | if (err==ZIP_OK) /* zipfile comment length */ | 1153 | if (err==ZIP_OK) /* zipfile comment length */ |
709 | err = ziplocal_putValue(zi->filezip,(uLong)size_global_comment,2); | 1154 | err = ziplocal_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2); |
710 | 1155 | ||
711 | if ((err==ZIP_OK) && (size_global_comment>0)) | 1156 | if ((err==ZIP_OK) && (size_global_comment>0)) |
712 | if (fwrite(global_comment,(uInt)size_global_comment,1,zi->filezip) !=1 ) | 1157 | if (ZWRITE(zi->z_filefunc,zi->filestream, |
1158 | global_comment,size_global_comment) != size_global_comment) | ||
713 | err = ZIP_ERRNO; | 1159 | err = ZIP_ERRNO; |
714 | fclose(zi->filezip); | 1160 | |
1161 | if (ZCLOSE(zi->z_filefunc,zi->filestream) != 0) | ||
1162 | if (err == ZIP_OK) | ||
1163 | err = ZIP_ERRNO; | ||
1164 | |||
715 | TRYFREE(zi); | 1165 | TRYFREE(zi); |
716 | 1166 | ||
717 | return err; | 1167 | return err; |