comparison OrthancFramework/Resources/ThirdParty/minizip/zip.c @ 5428:8174e45f48d8

Upgraded minizip library to stay away from CVE-2023-45853
author Alain Mazy <am@osimis.io>
date Fri, 17 Nov 2023 08:58:40 +0100
parents d25f4c0fa160
children
comparison
equal deleted inserted replaced
5427:111e21b4f8bc 5428:8174e45f48d8
12 Changes 12 Changes
13 Oct-2009 - Mathias Svensson - Remove old C style function prototypes 13 Oct-2009 - Mathias Svensson - Remove old C style function prototypes
14 Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives 14 Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives
15 Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions. 15 Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions.
16 Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data 16 Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data
17 It is used when recreting zip archive with RAW when deleting items from a zip. 17 It is used when recreating zip archive with RAW when deleting items from a zip.
18 ZIP64 data is automatically added to items that needs it, and existing ZIP64 data need to be removed. 18 ZIP64 data is automatically added to items that needs it, and existing ZIP64 data need to be removed.
19 Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required) 19 Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required)
20 Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer 20 Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
21 21
22 */ 22 */
23 23
24 24
25 #include <stdio.h> 25 #include <stdio.h>
26 #include <stdlib.h> 26 #include <stdlib.h>
27 #include <string.h> 27 #include <string.h>
28 #include <stdint.h>
28 #include <time.h> 29 #include <time.h>
29 #include "zlib.h" 30 #include "zlib.h"
30 #include "zip.h" 31 #include "zip.h"
31 32
32 #ifdef STDC 33 #ifdef STDC
33 # include <stddef.h> 34 # include <stddef.h>
34 # include <string.h>
35 # include <stdlib.h>
36 #endif 35 #endif
37 #ifdef NO_ERRNO_H 36 #ifdef NO_ERRNO_H
38 extern int errno; 37 extern int errno;
39 #else 38 #else
40 # include <errno.h> 39 # include <errno.h>
45 # define local static 44 # define local static
46 #endif 45 #endif
47 /* compile with -Dlocal if your debugger can't find static symbols */ 46 /* compile with -Dlocal if your debugger can't find static symbols */
48 47
49 #ifndef VERSIONMADEBY 48 #ifndef VERSIONMADEBY
50 # define VERSIONMADEBY (0x0) /* platform depedent */ 49 # define VERSIONMADEBY (0x0) /* platform dependent */
51 #endif 50 #endif
52 51
53 #ifndef Z_BUFSIZE 52 #ifndef Z_BUFSIZE
54 #define Z_BUFSIZE (64*1024) //(16384) 53 #define Z_BUFSIZE (64*1024) //(16384)
55 #endif 54 #endif
58 #define Z_MAXFILENAMEINZIP (256) 57 #define Z_MAXFILENAMEINZIP (256)
59 #endif 58 #endif
60 59
61 #ifndef ALLOC 60 #ifndef ALLOC
62 # define ALLOC(size) (malloc(size)) 61 # define ALLOC(size) (malloc(size))
63 #endif
64 #ifndef TRYFREE
65 # define TRYFREE(p) {if (p) free(p);}
66 #endif 62 #endif
67 63
68 /* 64 /*
69 #define SIZECENTRALDIRITEM (0x2e) 65 #define SIZECENTRALDIRITEM (0x2e)
70 #define SIZEZIPLOCALHEADER (0x1e) 66 #define SIZEZIPLOCALHEADER (0x1e)
136 132
137 int stream_initialised; /* 1 is stream is initialised */ 133 int stream_initialised; /* 1 is stream is initialised */
138 uInt pos_in_buffered_data; /* last written byte in buffered_data */ 134 uInt pos_in_buffered_data; /* last written byte in buffered_data */
139 135
140 ZPOS64_T pos_local_header; /* offset of the local header of the file 136 ZPOS64_T pos_local_header; /* offset of the local header of the file
141 currenty writing */ 137 currently writing */
142 char* central_header; /* central header data for the current file */ 138 char* central_header; /* central header data for the current file */
143 uLong size_centralExtra; 139 uLong size_centralExtra;
144 uLong size_centralheader; /* size of the central header for cur file */ 140 uLong size_centralheader; /* size of the central header for cur file */
145 uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */ 141 uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
146 uLong flag; /* flag of the file currently writing */ 142 uLong flag; /* flag of the file currently writing */
147 143
148 int method; /* compression method of file currenty wr.*/ 144 int method; /* compression method of file currently wr.*/
149 int raw; /* 1 for directly writing raw data */ 145 int raw; /* 1 for directly writing raw data */
150 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/ 146 Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
151 uLong dosDate; 147 uLong dosDate;
152 uLong crc32; 148 uLong crc32;
153 int encrypt; 149 int encrypt;
154 int zip64; /* Add ZIP64 extened information in the extra field */ 150 int zip64; /* Add ZIP64 extended information in the extra field */
155 ZPOS64_T pos_zip64extrainfo; 151 ZPOS64_T pos_zip64extrainfo;
156 ZPOS64_T totalCompressedData; 152 ZPOS64_T totalCompressedData;
157 ZPOS64_T totalUncompressedData; 153 ZPOS64_T totalUncompressedData;
158 #ifndef NOCRYPT 154 #ifndef NOCRYPT
159 unsigned long keys[3]; /* keys defining the pseudo-random sequence */ 155 unsigned long keys[3]; /* keys defining the pseudo-random sequence */
160 const z_crc_t* pcrc_32_tab; 156 const z_crc_t* pcrc_32_tab;
161 int crypt_header_size; 157 unsigned crypt_header_size;
162 #endif 158 #endif
163 } curfile64_info; 159 } curfile64_info;
164 160
165 typedef struct 161 typedef struct
166 { 162 {
167 zlib_filefunc64_32_def z_filefunc; 163 zlib_filefunc64_32_def z_filefunc;
168 voidpf filestream; /* io structore of the zipfile */ 164 voidpf filestream; /* io structure of the zipfile */
169 linkedlist_data central_dir;/* datablock with central dir in construction*/ 165 linkedlist_data central_dir;/* datablock with central dir in construction*/
170 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/ 166 int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
171 curfile64_info ci; /* info on the file curretly writing */ 167 curfile64_info ci; /* info on the file currently writing */
172 168
173 ZPOS64_T begin_pos; /* position of the beginning of the zipfile */ 169 ZPOS64_T begin_pos; /* position of the beginning of the zipfile */
174 ZPOS64_T add_position_when_writing_offset; 170 ZPOS64_T add_position_when_writing_offset;
175 ZPOS64_T number_entry; 171 ZPOS64_T number_entry;
176 172
184 #ifndef NOCRYPT 180 #ifndef NOCRYPT
185 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED 181 #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
186 #include "crypt.h" 182 #include "crypt.h"
187 #endif 183 #endif
188 184
189 local linkedlist_datablock_internal* allocate_new_datablock() 185 local linkedlist_datablock_internal* allocate_new_datablock(void) {
190 {
191 linkedlist_datablock_internal* ldi; 186 linkedlist_datablock_internal* ldi;
192 ldi = (linkedlist_datablock_internal*) 187 ldi = (linkedlist_datablock_internal*)
193 ALLOC(sizeof(linkedlist_datablock_internal)); 188 ALLOC(sizeof(linkedlist_datablock_internal));
194 if (ldi!=NULL) 189 if (ldi!=NULL)
195 { 190 {
198 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ; 193 ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
199 } 194 }
200 return ldi; 195 return ldi;
201 } 196 }
202 197
203 local void free_datablock(linkedlist_datablock_internal* ldi) 198 local void free_datablock(linkedlist_datablock_internal* ldi) {
204 {
205 while (ldi!=NULL) 199 while (ldi!=NULL)
206 { 200 {
207 linkedlist_datablock_internal* ldinext = ldi->next_datablock; 201 linkedlist_datablock_internal* ldinext = ldi->next_datablock;
208 TRYFREE(ldi); 202 free(ldi);
209 ldi = ldinext; 203 ldi = ldinext;
210 } 204 }
211 } 205 }
212 206
213 local void init_linkedlist(linkedlist_data* ll) 207 local void init_linkedlist(linkedlist_data* ll) {
214 {
215 ll->first_block = ll->last_block = NULL; 208 ll->first_block = ll->last_block = NULL;
216 } 209 }
217 210
218 local void free_linkedlist(linkedlist_data* ll) 211 local void free_linkedlist(linkedlist_data* ll) {
219 {
220 free_datablock(ll->first_block); 212 free_datablock(ll->first_block);
221 ll->first_block = ll->last_block = NULL; 213 ll->first_block = ll->last_block = NULL;
222 } 214 }
223 215
224 216
225 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) 217 local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) {
226 {
227 linkedlist_datablock_internal* ldi; 218 linkedlist_datablock_internal* ldi;
228 const unsigned char* from_copy; 219 const unsigned char* from_copy;
229 220
230 if (ll==NULL) 221 if (ll==NULL)
231 return ZIP_INTERNALERROR; 222 return ZIP_INTERNALERROR;
236 if (ll->first_block == NULL) 227 if (ll->first_block == NULL)
237 return ZIP_INTERNALERROR; 228 return ZIP_INTERNALERROR;
238 } 229 }
239 230
240 ldi = ll->last_block; 231 ldi = ll->last_block;
241 from_copy = (unsigned char*)buf; 232 from_copy = (const unsigned char*)buf;
242 233
243 while (len>0) 234 while (len>0)
244 { 235 {
245 uInt copy_this; 236 uInt copy_this;
246 uInt i; 237 uInt i;
281 /* =========================================================================== 272 /* ===========================================================================
282 Inputs a long in LSB order to the given file 273 Inputs a long in LSB order to the given file
283 nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T) 274 nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
284 */ 275 */
285 276
286 local int zip64local_putValue OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)); 277 local int zip64local_putValue(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte) {
287 local int zip64local_putValue (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte)
288 {
289 unsigned char buf[8]; 278 unsigned char buf[8];
290 int n; 279 int n;
291 for (n = 0; n < nbByte; n++) 280 for (n = 0; n < nbByte; n++)
292 { 281 {
293 buf[n] = (unsigned char)(x & 0xff); 282 buf[n] = (unsigned char)(x & 0xff);
299 { 288 {
300 buf[n] = 0xff; 289 buf[n] = 0xff;
301 } 290 }
302 } 291 }
303 292
304 if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte) 293 if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,(uLong)nbByte)!=(uLong)nbByte)
305 return ZIP_ERRNO; 294 return ZIP_ERRNO;
306 else 295 else
307 return ZIP_OK; 296 return ZIP_OK;
308 } 297 }
309 298
310 local void zip64local_putValue_inmemory OF((void* dest, ZPOS64_T x, int nbByte)); 299 local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte) {
311 local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte)
312 {
313 unsigned char* buf=(unsigned char*)dest; 300 unsigned char* buf=(unsigned char*)dest;
314 int n; 301 int n;
315 for (n = 0; n < nbByte; n++) { 302 for (n = 0; n < nbByte; n++) {
316 buf[n] = (unsigned char)(x & 0xff); 303 buf[n] = (unsigned char)(x & 0xff);
317 x >>= 8; 304 x >>= 8;
327 } 314 }
328 315
329 /****************************************************************************/ 316 /****************************************************************************/
330 317
331 318
332 local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) 319 local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) {
333 {
334 uLong year = (uLong)ptm->tm_year; 320 uLong year = (uLong)ptm->tm_year;
335 if (year>=1980) 321 if (year>=1980)
336 year-=1980; 322 year-=1980;
337 else if (year>=80) 323 else if (year>=80)
338 year-=80; 324 year-=80;
339 return 325 return
340 (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) | 326 (uLong) (((uLong)(ptm->tm_mday) + (32 * (uLong)(ptm->tm_mon+1)) + (512 * year)) << 16) |
341 ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour)); 327 (((uLong)ptm->tm_sec/2) + (32 * (uLong)ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
342 } 328 }
343 329
344 330
345 /****************************************************************************/ 331 /****************************************************************************/
346 332
347 local int zip64local_getByte OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)); 333 local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int* pi) {
348
349 local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def,voidpf filestream,int* pi)
350 {
351 unsigned char c; 334 unsigned char c;
352 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1); 335 int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
353 if (err==1) 336 if (err==1)
354 { 337 {
355 *pi = (int)c; 338 *pi = (int)c;
366 349
367 350
368 /* =========================================================================== 351 /* ===========================================================================
369 Reads a long in LSB order from the given gz_stream. Sets 352 Reads a long in LSB order from the given gz_stream. Sets
370 */ 353 */
371 local int zip64local_getShort OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); 354 local int zip64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) {
372
373 local int zip64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
374 {
375 uLong x ; 355 uLong x ;
376 int i = 0; 356 int i = 0;
377 int err; 357 int err;
378 358
379 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 359 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
388 else 368 else
389 *pX = 0; 369 *pX = 0;
390 return err; 370 return err;
391 } 371 }
392 372
393 local int zip64local_getLong OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong *pX)); 373 local int zip64local_getLong(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) {
394
395 local int zip64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX)
396 {
397 uLong x ; 374 uLong x ;
398 int i = 0; 375 int i = 0;
399 int err; 376 int err;
400 377
401 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 378 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
418 else 395 else
419 *pX = 0; 396 *pX = 0;
420 return err; 397 return err;
421 } 398 }
422 399
423 local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)); 400
424 401 local int zip64local_getLong64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) {
425
426 local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
427 {
428 ZPOS64_T x; 402 ZPOS64_T x;
429 int i = 0; 403 int i = 0;
430 int err; 404 int err;
431 405
432 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i); 406 err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
473 #endif 447 #endif
474 /* 448 /*
475 Locate the Central directory of a zipfile (at the end, just before 449 Locate the Central directory of a zipfile (at the end, just before
476 the global comment) 450 the global comment)
477 */ 451 */
478 local ZPOS64_T zip64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); 452 local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
479
480 local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
481 {
482 unsigned char* buf; 453 unsigned char* buf;
483 ZPOS64_T uSizeFile; 454 ZPOS64_T uSizeFile;
484 ZPOS64_T uBackRead; 455 ZPOS64_T uBackRead;
485 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ 456 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
486 ZPOS64_T uPosFound=0; 457 ZPOS64_T uPosFound=0;
520 491
521 for (i=(int)uReadSize-3; (i--)>0;) 492 for (i=(int)uReadSize-3; (i--)>0;)
522 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && 493 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
523 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) 494 ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
524 { 495 {
525 uPosFound = uReadPos+i; 496 uPosFound = uReadPos+(unsigned)i;
526 break; 497 break;
527 } 498 }
528 499
529 if (uPosFound!=0) 500 if (uPosFound!=0)
530 break; 501 break;
531 } 502 }
532 TRYFREE(buf); 503 free(buf);
533 return uPosFound; 504 return uPosFound;
534 } 505 }
535 506
536 /* 507 /*
537 Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before 508 Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
538 the global comment) 509 the global comment)
539 */ 510 */
540 local ZPOS64_T zip64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)); 511 local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
541
542 local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
543 {
544 unsigned char* buf; 512 unsigned char* buf;
545 ZPOS64_T uSizeFile; 513 ZPOS64_T uSizeFile;
546 ZPOS64_T uBackRead; 514 ZPOS64_T uBackRead;
547 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */ 515 ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
548 ZPOS64_T uPosFound=0; 516 ZPOS64_T uPosFound=0;
584 for (i=(int)uReadSize-3; (i--)>0;) 552 for (i=(int)uReadSize-3; (i--)>0;)
585 { 553 {
586 // Signature "0x07064b50" Zip64 end of central directory locater 554 // Signature "0x07064b50" Zip64 end of central directory locater
587 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07)) 555 if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
588 { 556 {
589 uPosFound = uReadPos+i; 557 uPosFound = uReadPos+(unsigned)i;
590 break; 558 break;
591 } 559 }
592 } 560 }
593 561
594 if (uPosFound!=0) 562 if (uPosFound!=0)
595 break; 563 break;
596 } 564 }
597 565
598 TRYFREE(buf); 566 free(buf);
599 if (uPosFound == 0) 567 if (uPosFound == 0)
600 return 0; 568 return 0;
601 569
602 /* Zip64 end of central directory locator */ 570 /* Zip64 end of central directory locator */
603 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0) 571 if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
635 return 0; 603 return 0;
636 604
637 return relativeOffset; 605 return relativeOffset;
638 } 606 }
639 607
640 int LoadCentralDirectoryRecord(zip64_internal* pziinit) 608 local int LoadCentralDirectoryRecord(zip64_internal* pziinit) {
641 {
642 int err=ZIP_OK; 609 int err=ZIP_OK;
643 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ 610 ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
644 611
645 ZPOS64_T size_central_dir; /* size of the central directory */ 612 ZPOS64_T size_central_dir; /* size of the central directory */
646 ZPOS64_T offset_central_dir; /* offset of start of central directory */ 613 ZPOS64_T offset_central_dir; /* offset of start of central directory */
647 ZPOS64_T central_pos; 614 ZPOS64_T central_pos;
648 uLong uL; 615 uLong uL;
649 616
650 uLong number_disk; /* number of the current dist, used for 617 uLong number_disk; /* number of the current disk, used for
651 spaning ZIP, unsupported, always 0*/ 618 spanning ZIP, unsupported, always 0*/
652 uLong number_disk_with_CD; /* number the the disk with central dir, used 619 uLong number_disk_with_CD; /* number of the disk with central dir, used
653 for spaning ZIP, unsupported, always 0*/ 620 for spanning ZIP, unsupported, always 0*/
654 ZPOS64_T number_entry; 621 ZPOS64_T number_entry;
655 ZPOS64_T number_entry_CD; /* total number of entries in 622 ZPOS64_T number_entry_CD; /* total number of entries in
656 the central dir 623 the central dir
657 (same than number_entry on nospan) */ 624 (same than number_entry on nospan) */
658 uLong VersionMadeBy; 625 uLong VersionMadeBy;
828 if (err==ZIP_OK) 795 if (err==ZIP_OK)
829 err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this); 796 err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
830 797
831 size_central_dir_to_read-=read_this; 798 size_central_dir_to_read-=read_this;
832 } 799 }
833 TRYFREE(buf_read); 800 free(buf_read);
834 } 801 }
835 pziinit->begin_pos = byte_before_the_zipfile; 802 pziinit->begin_pos = byte_before_the_zipfile;
836 pziinit->number_entry = number_entry_CD; 803 pziinit->number_entry = number_entry_CD;
837 804
838 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0) 805 if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
844 811
845 #endif /* !NO_ADDFILEINEXISTINGZIP*/ 812 #endif /* !NO_ADDFILEINEXISTINGZIP*/
846 813
847 814
848 /************************************************************/ 815 /************************************************************/
849 extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) 816 extern zipFile ZEXPORT zipOpen3(const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) {
850 {
851 zip64_internal ziinit; 817 zip64_internal ziinit;
852 zip64_internal* zi; 818 zip64_internal* zi;
853 int err=ZIP_OK; 819 int err=ZIP_OK;
854 820
855 ziinit.z_filefunc.zseek32_file = NULL; 821 ziinit.z_filefunc.zseek32_file = NULL;
903 # endif /* !NO_ADDFILEINEXISTINGZIP*/ 869 # endif /* !NO_ADDFILEINEXISTINGZIP*/
904 870
905 if (err != ZIP_OK) 871 if (err != ZIP_OK)
906 { 872 {
907 # ifndef NO_ADDFILEINEXISTINGZIP 873 # ifndef NO_ADDFILEINEXISTINGZIP
908 TRYFREE(ziinit.globalcomment); 874 free(ziinit.globalcomment);
909 # endif /* !NO_ADDFILEINEXISTINGZIP*/ 875 # endif /* !NO_ADDFILEINEXISTINGZIP*/
910 TRYFREE(zi); 876 free(zi);
911 return NULL; 877 return NULL;
912 } 878 }
913 else 879 else
914 { 880 {
915 *zi = ziinit; 881 *zi = ziinit;
916 return (zipFile)zi; 882 return (zipFile)zi;
917 } 883 }
918 } 884 }
919 885
920 extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def) 886 extern zipFile ZEXPORT zipOpen2(const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def) {
921 {
922 if (pzlib_filefunc32_def != NULL) 887 if (pzlib_filefunc32_def != NULL)
923 { 888 {
924 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; 889 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
925 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def); 890 fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
926 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill); 891 return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
927 } 892 }
928 else 893 else
929 return zipOpen3(pathname, append, globalcomment, NULL); 894 return zipOpen3(pathname, append, globalcomment, NULL);
930 } 895 }
931 896
932 extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def) 897 extern zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def) {
933 {
934 if (pzlib_filefunc_def != NULL) 898 if (pzlib_filefunc_def != NULL)
935 { 899 {
936 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill; 900 zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
937 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def; 901 zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
938 zlib_filefunc64_32_def_fill.ztell32_file = NULL; 902 zlib_filefunc64_32_def_fill.ztell32_file = NULL;
943 return zipOpen3(pathname, append, globalcomment, NULL); 907 return zipOpen3(pathname, append, globalcomment, NULL);
944 } 908 }
945 909
946 910
947 911
948 extern zipFile ZEXPORT zipOpen (const char* pathname, int append) 912 extern zipFile ZEXPORT zipOpen(const char* pathname, int append) {
949 {
950 return zipOpen3((const void*)pathname,append,NULL,NULL); 913 return zipOpen3((const void*)pathname,append,NULL,NULL);
951 } 914 }
952 915
953 extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append) 916 extern zipFile ZEXPORT zipOpen64(const void* pathname, int append) {
954 {
955 return zipOpen3(pathname,append,NULL,NULL); 917 return zipOpen3(pathname,append,NULL,NULL);
956 } 918 }
957 919
958 int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local) 920 local int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local) {
959 {
960 /* write the local header */ 921 /* write the local header */
961 int err; 922 int err;
962 uInt size_filename = (uInt)strlen(filename); 923 uInt size_filename = (uInt)strlen(filename);
963 uInt size_extrafield = size_extrafield_local; 924 uInt size_extrafield = size_extrafield_local;
964 925
1032 ZPOS64_T UncompressedSize = 0; 993 ZPOS64_T UncompressedSize = 0;
1033 994
1034 // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file) 995 // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
1035 zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream); 996 zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
1036 997
1037 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2); 998 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)HeaderID,2);
1038 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2); 999 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)DataSize,2);
1039 1000
1040 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8); 1001 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
1041 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8); 1002 err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
1042 } 1003 }
1043 1004
1050 before calling this function it can be done with zipRemoveExtraInfoBlock 1011 before calling this function it can be done with zipRemoveExtraInfoBlock
1051 1012
1052 It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize 1013 It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
1053 unnecessary allocations. 1014 unnecessary allocations.
1054 */ 1015 */
1055 extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1016 extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1056 const void* extrafield_local, uInt size_extrafield_local, 1017 const void* extrafield_local, uInt size_extrafield_local,
1057 const void* extrafield_global, uInt size_extrafield_global, 1018 const void* extrafield_global, uInt size_extrafield_global,
1058 const char* comment, int method, int level, int raw, 1019 const char* comment, int method, int level, int raw,
1059 int windowBits,int memLevel, int strategy, 1020 int windowBits,int memLevel, int strategy,
1060 const char* password, uLong crcForCrypting, 1021 const char* password, uLong crcForCrypting,
1061 uLong versionMadeBy, uLong flagBase, int zip64) 1022 uLong versionMadeBy, uLong flagBase, int zip64) {
1062 {
1063 zip64_internal* zi; 1023 zip64_internal* zi;
1064 uInt size_filename; 1024 uInt size_filename;
1065 uInt size_comment; 1025 uInt size_comment;
1066 uInt i; 1026 uInt i;
1067 int err = ZIP_OK; 1027 int err = ZIP_OK;
1080 return ZIP_PARAMERROR; 1040 return ZIP_PARAMERROR;
1081 #else 1041 #else
1082 if ((method!=0) && (method!=Z_DEFLATED)) 1042 if ((method!=0) && (method!=Z_DEFLATED))
1083 return ZIP_PARAMERROR; 1043 return ZIP_PARAMERROR;
1084 #endif 1044 #endif
1045
1046 // The filename and comment length must fit in 16 bits.
1047 if ((filename!=NULL) && (strlen(filename)>0xffff))
1048 return ZIP_PARAMERROR;
1049 if ((comment!=NULL) && (strlen(comment)>0xffff))
1050 return ZIP_PARAMERROR;
1051 // The extra field length must fit in 16 bits. If the member also requires
1052 // a Zip64 extra block, that will also need to fit within that 16-bit
1053 // length, but that will be checked for later.
1054 if ((size_extrafield_local>0xffff) || (size_extrafield_global>0xffff))
1055 return ZIP_PARAMERROR;
1085 1056
1086 zi = (zip64_internal*)file; 1057 zi = (zip64_internal*)file;
1087 1058
1088 if (zi->in_opened_file_inzip == 1) 1059 if (zi->in_opened_file_inzip == 1)
1089 { 1060 {
1260 if (err==Z_OK) 1231 if (err==Z_OK)
1261 zi->in_opened_file_inzip = 1; 1232 zi->in_opened_file_inzip = 1;
1262 return err; 1233 return err;
1263 } 1234 }
1264 1235
1265 extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1236 extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1266 const void* extrafield_local, uInt size_extrafield_local, 1237 const void* extrafield_local, uInt size_extrafield_local,
1267 const void* extrafield_global, uInt size_extrafield_global, 1238 const void* extrafield_global, uInt size_extrafield_global,
1268 const char* comment, int method, int level, int raw, 1239 const char* comment, int method, int level, int raw,
1269 int windowBits,int memLevel, int strategy, 1240 int windowBits,int memLevel, int strategy,
1270 const char* password, uLong crcForCrypting, 1241 const char* password, uLong crcForCrypting,
1271 uLong versionMadeBy, uLong flagBase) 1242 uLong versionMadeBy, uLong flagBase) {
1272 { 1243 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1273 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1244 extrafield_local, size_extrafield_local,
1274 extrafield_local, size_extrafield_local, 1245 extrafield_global, size_extrafield_global,
1275 extrafield_global, size_extrafield_global, 1246 comment, method, level, raw,
1276 comment, method, level, raw, 1247 windowBits, memLevel, strategy,
1277 windowBits, memLevel, strategy, 1248 password, crcForCrypting, versionMadeBy, flagBase, 0);
1278 password, crcForCrypting, versionMadeBy, flagBase, 0); 1249 }
1279 } 1250
1280 1251 extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1281 extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1252 const void* extrafield_local, uInt size_extrafield_local,
1282 const void* extrafield_local, uInt size_extrafield_local, 1253 const void* extrafield_global, uInt size_extrafield_global,
1283 const void* extrafield_global, uInt size_extrafield_global, 1254 const char* comment, int method, int level, int raw,
1284 const char* comment, int method, int level, int raw, 1255 int windowBits,int memLevel, int strategy,
1285 int windowBits,int memLevel, int strategy, 1256 const char* password, uLong crcForCrypting) {
1286 const char* password, uLong crcForCrypting) 1257 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1287 { 1258 extrafield_local, size_extrafield_local,
1288 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1259 extrafield_global, size_extrafield_global,
1289 extrafield_local, size_extrafield_local, 1260 comment, method, level, raw,
1290 extrafield_global, size_extrafield_global, 1261 windowBits, memLevel, strategy,
1291 comment, method, level, raw, 1262 password, crcForCrypting, VERSIONMADEBY, 0, 0);
1292 windowBits, memLevel, strategy,
1293 password, crcForCrypting, VERSIONMADEBY, 0, 0);
1294 } 1263 }
1295 1264
1296 extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1265 extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1297 const void* extrafield_local, uInt size_extrafield_local, 1266 const void* extrafield_local, uInt size_extrafield_local,
1298 const void* extrafield_global, uInt size_extrafield_global, 1267 const void* extrafield_global, uInt size_extrafield_global,
1299 const char* comment, int method, int level, int raw, 1268 const char* comment, int method, int level, int raw,
1300 int windowBits,int memLevel, int strategy, 1269 int windowBits,int memLevel, int strategy,
1301 const char* password, uLong crcForCrypting, int zip64) 1270 const char* password, uLong crcForCrypting, int zip64) {
1302 { 1271 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1303 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1272 extrafield_local, size_extrafield_local,
1304 extrafield_local, size_extrafield_local, 1273 extrafield_global, size_extrafield_global,
1305 extrafield_global, size_extrafield_global, 1274 comment, method, level, raw,
1306 comment, method, level, raw, 1275 windowBits, memLevel, strategy,
1307 windowBits, memLevel, strategy, 1276 password, crcForCrypting, VERSIONMADEBY, 0, zip64);
1308 password, crcForCrypting, VERSIONMADEBY, 0, zip64);
1309 } 1277 }
1310 1278
1311 extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1279 extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1312 const void* extrafield_local, uInt size_extrafield_local, 1280 const void* extrafield_local, uInt size_extrafield_local,
1313 const void* extrafield_global, uInt size_extrafield_global, 1281 const void* extrafield_global, uInt size_extrafield_global,
1314 const char* comment, int method, int level, int raw) 1282 const char* comment, int method, int level, int raw) {
1315 { 1283 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1316 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1284 extrafield_local, size_extrafield_local,
1317 extrafield_local, size_extrafield_local, 1285 extrafield_global, size_extrafield_global,
1318 extrafield_global, size_extrafield_global, 1286 comment, method, level, raw,
1319 comment, method, level, raw, 1287 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1320 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1288 NULL, 0, VERSIONMADEBY, 0, 0);
1321 NULL, 0, VERSIONMADEBY, 0, 0);
1322 } 1289 }
1323 1290
1324 extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi, 1291 extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1325 const void* extrafield_local, uInt size_extrafield_local, 1292 const void* extrafield_local, uInt size_extrafield_local,
1326 const void* extrafield_global, uInt size_extrafield_global, 1293 const void* extrafield_global, uInt size_extrafield_global,
1327 const char* comment, int method, int level, int raw, int zip64) 1294 const char* comment, int method, int level, int raw, int zip64) {
1328 { 1295 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1329 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1296 extrafield_local, size_extrafield_local,
1330 extrafield_local, size_extrafield_local, 1297 extrafield_global, size_extrafield_global,
1331 extrafield_global, size_extrafield_global, 1298 comment, method, level, raw,
1332 comment, method, level, raw, 1299 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1333 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1300 NULL, 0, VERSIONMADEBY, 0, zip64);
1334 NULL, 0, VERSIONMADEBY, 0, zip64); 1301 }
1335 } 1302
1336 1303 extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1337 extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1304 const void* extrafield_local, uInt size_extrafield_local,
1338 const void* extrafield_local, uInt size_extrafield_local, 1305 const void*extrafield_global, uInt size_extrafield_global,
1339 const void*extrafield_global, uInt size_extrafield_global, 1306 const char* comment, int method, int level, int zip64) {
1340 const char* comment, int method, int level, int zip64) 1307 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1341 { 1308 extrafield_local, size_extrafield_local,
1342 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1309 extrafield_global, size_extrafield_global,
1343 extrafield_local, size_extrafield_local, 1310 comment, method, level, 0,
1344 extrafield_global, size_extrafield_global, 1311 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1345 comment, method, level, 0, 1312 NULL, 0, VERSIONMADEBY, 0, zip64);
1346 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1313 }
1347 NULL, 0, VERSIONMADEBY, 0, zip64); 1314
1348 } 1315 extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1349 1316 const void* extrafield_local, uInt size_extrafield_local,
1350 extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi, 1317 const void*extrafield_global, uInt size_extrafield_global,
1351 const void* extrafield_local, uInt size_extrafield_local, 1318 const char* comment, int method, int level) {
1352 const void*extrafield_global, uInt size_extrafield_global, 1319 return zipOpenNewFileInZip4_64(file, filename, zipfi,
1353 const char* comment, int method, int level) 1320 extrafield_local, size_extrafield_local,
1354 { 1321 extrafield_global, size_extrafield_global,
1355 return zipOpenNewFileInZip4_64 (file, filename, zipfi, 1322 comment, method, level, 0,
1356 extrafield_local, size_extrafield_local, 1323 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1357 extrafield_global, size_extrafield_global, 1324 NULL, 0, VERSIONMADEBY, 0, 0);
1358 comment, method, level, 0, 1325 }
1359 -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, 1326
1360 NULL, 0, VERSIONMADEBY, 0, 0); 1327 local int zip64FlushWriteBuffer(zip64_internal* zi) {
1361 }
1362
1363 local int zip64FlushWriteBuffer(zip64_internal* zi)
1364 {
1365 int err=ZIP_OK; 1328 int err=ZIP_OK;
1366 1329
1367 if (zi->ci.encrypt != 0) 1330 if (zi->ci.encrypt != 0)
1368 { 1331 {
1369 #ifndef NOCRYPT 1332 #ifndef NOCRYPT
1397 zi->ci.pos_in_buffered_data = 0; 1360 zi->ci.pos_in_buffered_data = 0;
1398 1361
1399 return err; 1362 return err;
1400 } 1363 }
1401 1364
1402 extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len) 1365 extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void* buf, unsigned int len) {
1403 {
1404 zip64_internal* zi; 1366 zip64_internal* zi;
1405 int err=ZIP_OK; 1367 int err=ZIP_OK;
1406 1368
1407 if (file == NULL) 1369 if (file == NULL)
1408 return ZIP_PARAMERROR; 1370 return ZIP_PARAMERROR;
1448 err = ZIP_OK; 1410 err = ZIP_OK;
1449 } 1411 }
1450 else 1412 else
1451 #endif 1413 #endif
1452 { 1414 {
1453 zi->ci.stream.next_in = (Bytef*)buf; 1415 zi->ci.stream.next_in = (Bytef*)(uintptr_t)buf;
1454 zi->ci.stream.avail_in = len; 1416 zi->ci.stream.avail_in = len;
1455 1417
1456 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0)) 1418 while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
1457 { 1419 {
1458 if (zi->ci.stream.avail_out == 0) 1420 if (zi->ci.stream.avail_out == 0)
1469 1431
1470 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw)) 1432 if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1471 { 1433 {
1472 uLong uTotalOutBefore = zi->ci.stream.total_out; 1434 uLong uTotalOutBefore = zi->ci.stream.total_out;
1473 err=deflate(&zi->ci.stream, Z_NO_FLUSH); 1435 err=deflate(&zi->ci.stream, Z_NO_FLUSH);
1474 if(uTotalOutBefore > zi->ci.stream.total_out)
1475 {
1476 int bBreak = 0;
1477 bBreak++;
1478 }
1479 1436
1480 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ; 1437 zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1481 } 1438 }
1482 else 1439 else
1483 { 1440 {
1504 } 1461 }
1505 1462
1506 return err; 1463 return err;
1507 } 1464 }
1508 1465
1509 extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32) 1466 extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uLong uncompressed_size, uLong crc32) {
1510 {
1511 return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32); 1467 return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
1512 } 1468 }
1513 1469
1514 extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32) 1470 extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_size, uLong crc32) {
1515 {
1516 zip64_internal* zi; 1471 zip64_internal* zi;
1517 ZPOS64_T compressed_size; 1472 ZPOS64_T compressed_size;
1518 uLong invalidValue = 0xffffffff; 1473 uLong invalidValue = 0xffffffff;
1519 short datasize = 0; 1474 unsigned datasize = 0;
1520 int err=ZIP_OK; 1475 int err=ZIP_OK;
1521 1476
1522 if (file == NULL) 1477 if (file == NULL)
1523 return ZIP_PARAMERROR; 1478 return ZIP_PARAMERROR;
1524 zi = (zip64_internal*)file; 1479 zi = (zip64_internal*)file;
1745 zi->in_opened_file_inzip = 0; 1700 zi->in_opened_file_inzip = 0;
1746 1701
1747 return err; 1702 return err;
1748 } 1703 }
1749 1704
1750 extern int ZEXPORT zipCloseFileInZip (zipFile file) 1705 extern int ZEXPORT zipCloseFileInZip(zipFile file) {
1751 {
1752 return zipCloseFileInZipRaw (file,0,0); 1706 return zipCloseFileInZipRaw (file,0,0);
1753 } 1707 }
1754 1708
1755 int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip) 1709 local int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip) {
1756 {
1757 int err = ZIP_OK; 1710 int err = ZIP_OK;
1758 ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writing_offset; 1711 ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writing_offset;
1759 1712
1760 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4); 1713 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
1761 1714
1772 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4); 1725 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
1773 1726
1774 return err; 1727 return err;
1775 } 1728 }
1776 1729
1777 int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) 1730 local int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) {
1778 {
1779 int err = ZIP_OK; 1731 int err = ZIP_OK;
1780 1732
1781 uLong Zip64DataSize = 44; 1733 uLong Zip64DataSize = 44;
1782 1734
1783 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4); 1735 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
1811 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writing_offset; 1763 ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
1812 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8); 1764 err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
1813 } 1765 }
1814 return err; 1766 return err;
1815 } 1767 }
1816 int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) 1768
1817 { 1769 local int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) {
1818 int err = ZIP_OK; 1770 int err = ZIP_OK;
1819 1771
1820 /*signature*/ 1772 /*signature*/
1821 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4); 1773 err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1822 1774
1859 } 1811 }
1860 1812
1861 return err; 1813 return err;
1862 } 1814 }
1863 1815
1864 int Write_GlobalComment(zip64_internal* zi, const char* global_comment) 1816 local int Write_GlobalComment(zip64_internal* zi, const char* global_comment) {
1865 {
1866 int err = ZIP_OK; 1817 int err = ZIP_OK;
1867 uInt size_global_comment = 0; 1818 uInt size_global_comment = 0;
1868 1819
1869 if(global_comment != NULL) 1820 if(global_comment != NULL)
1870 size_global_comment = (uInt)strlen(global_comment); 1821 size_global_comment = (uInt)strlen(global_comment);
1877 err = ZIP_ERRNO; 1828 err = ZIP_ERRNO;
1878 } 1829 }
1879 return err; 1830 return err;
1880 } 1831 }
1881 1832
1882 extern int ZEXPORT zipClose (zipFile file, const char* global_comment) 1833 extern int ZEXPORT zipClose(zipFile file, const char* global_comment) {
1883 {
1884 zip64_internal* zi; 1834 zip64_internal* zi;
1885 int err = 0; 1835 int err = 0;
1886 uLong size_centraldir = 0; 1836 uLong size_centraldir = 0;
1887 ZPOS64_T centraldir_pos_inzip; 1837 ZPOS64_T centraldir_pos_inzip;
1888 ZPOS64_T pos; 1838 ZPOS64_T pos;
1920 } 1870 }
1921 } 1871 }
1922 free_linkedlist(&(zi->central_dir)); 1872 free_linkedlist(&(zi->central_dir));
1923 1873
1924 pos = centraldir_pos_inzip - zi->add_position_when_writing_offset; 1874 pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
1925 if(pos >= 0xffffffff || zi->number_entry > 0xFFFF) 1875 if(pos >= 0xffffffff || zi->number_entry >= 0xFFFF)
1926 { 1876 {
1927 ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream); 1877 ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream);
1928 Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip); 1878 Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1929 1879
1930 Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos); 1880 Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos);
1939 if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0) 1889 if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0)
1940 if (err == ZIP_OK) 1890 if (err == ZIP_OK)
1941 err = ZIP_ERRNO; 1891 err = ZIP_ERRNO;
1942 1892
1943 #ifndef NO_ADDFILEINEXISTINGZIP 1893 #ifndef NO_ADDFILEINEXISTINGZIP
1944 TRYFREE(zi->globalcomment); 1894 free(zi->globalcomment);
1945 #endif 1895 #endif
1946 TRYFREE(zi); 1896 free(zi);
1947 1897
1948 return err; 1898 return err;
1949 } 1899 }
1950 1900
1951 extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHeader) 1901 extern int ZEXPORT zipRemoveExtraInfoBlock(char* pData, int* dataLen, short sHeader) {
1952 {
1953 char* p = pData; 1902 char* p = pData;
1954 int size = 0; 1903 int size = 0;
1955 char* pNewHeader; 1904 char* pNewHeader;
1956 char* pTmp; 1905 char* pTmp;
1957 short header; 1906 short header;
1958 short dataSize; 1907 short dataSize;
1959 1908
1960 int retVal = ZIP_OK; 1909 int retVal = ZIP_OK;
1961 1910
1962 if(pData == NULL || *dataLen < 4) 1911 if(pData == NULL || dataLen == NULL || *dataLen < 4)
1963 return ZIP_PARAMERROR; 1912 return ZIP_PARAMERROR;
1964 1913
1965 pNewHeader = (char*)ALLOC(*dataLen); 1914 pNewHeader = (char*)ALLOC((unsigned)*dataLen);
1966 pTmp = pNewHeader; 1915 pTmp = pNewHeader;
1967 1916
1968 while(p < (pData + *dataLen)) 1917 while(p < (pData + *dataLen))
1969 { 1918 {
1970 header = *(short*)p; 1919 header = *(short*)p;
1999 retVal = ZIP_OK; 1948 retVal = ZIP_OK;
2000 } 1949 }
2001 else 1950 else
2002 retVal = ZIP_ERRNO; 1951 retVal = ZIP_ERRNO;
2003 1952
2004 TRYFREE(pNewHeader); 1953 free(pNewHeader);
2005 1954
2006 return retVal; 1955 return retVal;
2007 } 1956 }