cronapp-cordova-plugin-contentsync 4.4.0-RC7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +11 -0
  3. package/README.md +372 -0
  4. package/package-lock.json +1545 -0
  5. package/package.json +58 -0
  6. package/plugin.xml +93 -0
  7. package/sample/css/index.css +128 -0
  8. package/sample/img/logo.png +0 -0
  9. package/sample/index.html +47 -0
  10. package/sample/js/index.js +152 -0
  11. package/spec/helper/cordova.js +83 -0
  12. package/spec/index.spec.js +334 -0
  13. package/src/android/Sync.java +1102 -0
  14. package/src/browser/Sync.js +20 -0
  15. package/src/ios/ContentSync.h +74 -0
  16. package/src/ios/ContentSync.m +1002 -0
  17. package/src/ios/SSZipArchive.h +52 -0
  18. package/src/ios/SSZipArchive.m +540 -0
  19. package/src/ios/minizip/crypt.h +131 -0
  20. package/src/ios/minizip/ioapi.c +239 -0
  21. package/src/ios/minizip/ioapi.h +201 -0
  22. package/src/ios/minizip/mztools.c +284 -0
  23. package/src/ios/minizip/mztools.h +31 -0
  24. package/src/ios/minizip/unzip.c +2153 -0
  25. package/src/ios/minizip/unzip.h +437 -0
  26. package/src/ios/minizip/zip.c +2022 -0
  27. package/src/ios/minizip/zip.h +362 -0
  28. package/src/windows/SyncProxy.js +279 -0
  29. package/src/windows/ZipWinProj/PGZipInflate.cs +94 -0
  30. package/src/windows/ZipWinProj/Properties/AssemblyInfo.cs +30 -0
  31. package/src/windows/ZipWinProj/ZipWinProj.csproj +57 -0
  32. package/src/wp8/Sync.cs +746 -0
  33. package/src/wp8/Unzip.cs +481 -0
  34. package/tests/anyfile.txt +1 -0
  35. package/tests/archives/www1.zip +0 -0
  36. package/tests/archives/www2.zip +0 -0
  37. package/tests/package.json +11 -0
  38. package/tests/plugin.xml +22 -0
  39. package/tests/scripts/start-server.sh +2 -0
  40. package/tests/scripts/stop-server.sh +1 -0
  41. package/tests/tests.js +255 -0
  42. package/www/index.js +285 -0
@@ -0,0 +1,2022 @@
1
+ /* zip.c -- IO on .zip files using zlib
2
+ Version 1.1, February 14h, 2010
3
+ part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
4
+
5
+ Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
6
+
7
+ Modifications for Zip64 support
8
+ Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
9
+
10
+ For more info read MiniZip_info.txt
11
+
12
+ Changes
13
+ Oct-2009 - Mathias Svensson - Remove old C style function prototypes
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.
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.
18
+ ZIP64 data is automaticly 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)
20
+ Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
21
+
22
+ */
23
+
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include <time.h>
29
+ #include "zlib.h"
30
+ #include "zip.h"
31
+
32
+ #ifdef STDC
33
+ # include <stddef.h>
34
+ # include <string.h>
35
+ # include <stdlib.h>
36
+ #endif
37
+ #ifdef NO_ERRNO_H
38
+ extern int errno;
39
+ #else
40
+ # include <errno.h>
41
+ #endif
42
+
43
+
44
+ #ifndef local
45
+ # define local static
46
+ #endif
47
+ /* compile with -Dlocal if your debugger can't find static symbols */
48
+
49
+ #ifndef VERSIONMADEBY
50
+ # define VERSIONMADEBY (0x0) /* platform depedent */
51
+ #endif
52
+
53
+ #ifndef Z_BUFSIZE
54
+ #define Z_BUFSIZE (64*1024) //(16384)
55
+ #endif
56
+
57
+ #ifndef Z_MAXFILENAMEINZIP
58
+ #define Z_MAXFILENAMEINZIP (256)
59
+ #endif
60
+
61
+ #ifndef ALLOC
62
+ # define ALLOC(size) (malloc(size))
63
+ #endif
64
+ #ifndef TRYFREE
65
+ # define TRYFREE(p) {if (p) free(p);}
66
+ #endif
67
+
68
+ /*
69
+ #define SIZECENTRALDIRITEM (0x2e)
70
+ #define SIZEZIPLOCALHEADER (0x1e)
71
+ */
72
+
73
+ /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
74
+
75
+
76
+ // NOT sure that this work on ALL platform
77
+ #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
78
+
79
+ #ifndef SEEK_CUR
80
+ #define SEEK_CUR 1
81
+ #endif
82
+
83
+ #ifndef SEEK_END
84
+ #define SEEK_END 2
85
+ #endif
86
+
87
+ #ifndef SEEK_SET
88
+ #define SEEK_SET 0
89
+ #endif
90
+
91
+ #ifndef DEF_MEM_LEVEL
92
+ #if MAX_MEM_LEVEL >= 8
93
+ # define DEF_MEM_LEVEL 8
94
+ #else
95
+ # define DEF_MEM_LEVEL MAX_MEM_LEVEL
96
+ #endif
97
+ #endif
98
+ const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
99
+
100
+
101
+ #define SIZEDATA_INDATABLOCK (4096-(4*4))
102
+
103
+ #define LOCALHEADERMAGIC (0x04034b50)
104
+ #define CENTRALHEADERMAGIC (0x02014b50)
105
+ #define ENDHEADERMAGIC (0x06054b50)
106
+ #define ZIP64ENDHEADERMAGIC (0x6064b50)
107
+ #define ZIP64ENDLOCHEADERMAGIC (0x7064b50)
108
+
109
+ #define FLAG_LOCALHEADER_OFFSET (0x06)
110
+ #define CRC_LOCALHEADER_OFFSET (0x0e)
111
+
112
+ #define SIZECENTRALHEADER (0x2e) /* 46 */
113
+
114
+ typedef struct linkedlist_datablock_internal_s
115
+ {
116
+ struct linkedlist_datablock_internal_s* next_datablock;
117
+ uLong avail_in_this_block;
118
+ uLong filled_in_this_block;
119
+ uLong unused; /* for future use and alignement */
120
+ unsigned char data[SIZEDATA_INDATABLOCK];
121
+ } linkedlist_datablock_internal;
122
+
123
+ typedef struct linkedlist_data_s
124
+ {
125
+ linkedlist_datablock_internal* first_block;
126
+ linkedlist_datablock_internal* last_block;
127
+ } linkedlist_data;
128
+
129
+
130
+ typedef struct
131
+ {
132
+ z_stream stream; /* zLib stream structure for inflate */
133
+ #ifdef HAVE_BZIP2
134
+ bz_stream bstream; /* bzLib stream structure for bziped */
135
+ #endif
136
+
137
+ int stream_initialised; /* 1 is stream is initialised */
138
+ uInt pos_in_buffered_data; /* last written byte in buffered_data */
139
+
140
+ ZPOS64_T pos_local_header; /* offset of the local header of the file
141
+ currenty writing */
142
+ char* central_header; /* central header data for the current file */
143
+ uLong size_centralExtra;
144
+ 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 */
146
+ uLong flag; /* flag of the file currently writing */
147
+
148
+ int method; /* compression method of file currenty wr.*/
149
+ int raw; /* 1 for directly writing raw data */
150
+ Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
151
+ uLong dosDate;
152
+ uLong crc32;
153
+ int encrypt;
154
+ int zip64; /* Add ZIP64 extened information in the extra field */
155
+ ZPOS64_T pos_zip64extrainfo;
156
+ ZPOS64_T totalCompressedData;
157
+ ZPOS64_T totalUncompressedData;
158
+ #ifndef NOCRYPT
159
+ unsigned long keys[3]; /* keys defining the pseudo-random sequence */
160
+ const unsigned long* pcrc_32_tab;
161
+ int crypt_header_size;
162
+ #endif
163
+ } curfile64_info;
164
+
165
+ typedef struct
166
+ {
167
+ zlib_filefunc64_32_def z_filefunc;
168
+ voidpf filestream; /* io structore of the zipfile */
169
+ 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.*/
171
+ curfile64_info ci; /* info on the file curretly writing */
172
+
173
+ ZPOS64_T begin_pos; /* position of the beginning of the zipfile */
174
+ ZPOS64_T add_position_when_writting_offset;
175
+ ZPOS64_T number_entry;
176
+
177
+ #ifndef NO_ADDFILEINEXISTINGZIP
178
+ char *globalcomment;
179
+ #endif
180
+
181
+ } zip64_internal;
182
+
183
+
184
+ #ifndef NOCRYPT
185
+ #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
186
+ #include "crypt.h"
187
+ #endif
188
+
189
+ local linkedlist_datablock_internal* allocate_new_datablock()
190
+ {
191
+ linkedlist_datablock_internal* ldi;
192
+ ldi = (linkedlist_datablock_internal*)
193
+ ALLOC(sizeof(linkedlist_datablock_internal));
194
+ if (ldi!=NULL)
195
+ {
196
+ ldi->next_datablock = NULL ;
197
+ ldi->filled_in_this_block = 0 ;
198
+ ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
199
+ }
200
+ return ldi;
201
+ }
202
+
203
+ local void free_datablock(linkedlist_datablock_internal* ldi)
204
+ {
205
+ while (ldi!=NULL)
206
+ {
207
+ linkedlist_datablock_internal* ldinext = ldi->next_datablock;
208
+ TRYFREE(ldi);
209
+ ldi = ldinext;
210
+ }
211
+ }
212
+
213
+ local void init_linkedlist(linkedlist_data* ll)
214
+ {
215
+ ll->first_block = ll->last_block = NULL;
216
+ }
217
+
218
+ local void free_linkedlist(linkedlist_data* ll)
219
+ {
220
+ free_datablock(ll->first_block);
221
+ ll->first_block = ll->last_block = NULL;
222
+ }
223
+
224
+
225
+ local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len)
226
+ {
227
+ linkedlist_datablock_internal* ldi;
228
+ const unsigned char* from_copy;
229
+
230
+ if (ll==NULL)
231
+ return ZIP_INTERNALERROR;
232
+
233
+ if (ll->last_block == NULL)
234
+ {
235
+ ll->first_block = ll->last_block = allocate_new_datablock();
236
+ if (ll->first_block == NULL)
237
+ return ZIP_INTERNALERROR;
238
+ }
239
+
240
+ ldi = ll->last_block;
241
+ from_copy = (unsigned char*)buf;
242
+
243
+ while (len>0)
244
+ {
245
+ uInt copy_this;
246
+ uInt i;
247
+ unsigned char* to_copy;
248
+
249
+ if (ldi->avail_in_this_block==0)
250
+ {
251
+ ldi->next_datablock = allocate_new_datablock();
252
+ if (ldi->next_datablock == NULL)
253
+ return ZIP_INTERNALERROR;
254
+ ldi = ldi->next_datablock ;
255
+ ll->last_block = ldi;
256
+ }
257
+
258
+ if (ldi->avail_in_this_block < len)
259
+ copy_this = (uInt)ldi->avail_in_this_block;
260
+ else
261
+ copy_this = (uInt)len;
262
+
263
+ to_copy = &(ldi->data[ldi->filled_in_this_block]);
264
+
265
+ for (i=0;i<copy_this;i++)
266
+ *(to_copy+i)=*(from_copy+i);
267
+
268
+ ldi->filled_in_this_block += copy_this;
269
+ ldi->avail_in_this_block -= copy_this;
270
+ from_copy += copy_this ;
271
+ len -= copy_this;
272
+ }
273
+ return ZIP_OK;
274
+ }
275
+
276
+
277
+
278
+ /****************************************************************************/
279
+
280
+ #ifndef NO_ADDFILEINEXISTINGZIP
281
+ /* ===========================================================================
282
+ Inputs a long in LSB order to the given file
283
+ nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
284
+ */
285
+
286
+ local int zip64local_putValue OF((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];
290
+ int n;
291
+ for (n = 0; n < nbByte; n++)
292
+ {
293
+ buf[n] = (unsigned char)(x & 0xff);
294
+ x >>= 8;
295
+ }
296
+ if (x != 0)
297
+ { /* data overflow - hack for ZIP64 (X Roche) */
298
+ for (n = 0; n < nbByte; n++)
299
+ {
300
+ buf[n] = 0xff;
301
+ }
302
+ }
303
+
304
+ if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,nbByte)!=(uLong)nbByte)
305
+ return ZIP_ERRNO;
306
+ else
307
+ return ZIP_OK;
308
+ }
309
+
310
+ local void zip64local_putValue_inmemory OF((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;
314
+ int n;
315
+ for (n = 0; n < nbByte; n++) {
316
+ buf[n] = (unsigned char)(x & 0xff);
317
+ x >>= 8;
318
+ }
319
+
320
+ if (x != 0)
321
+ { /* data overflow - hack for ZIP64 */
322
+ for (n = 0; n < nbByte; n++)
323
+ {
324
+ buf[n] = 0xff;
325
+ }
326
+ }
327
+ }
328
+
329
+ /****************************************************************************/
330
+
331
+
332
+ local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm)
333
+ {
334
+ uLong year = (uLong)ptm->tm_year;
335
+ if (year>=1980)
336
+ year-=1980;
337
+ else if (year>=80)
338
+ year-=80;
339
+ return
340
+ (uLong) (((ptm->tm_mday) + (32 * (ptm->tm_mon+1)) + (512 * year)) << 16) |
341
+ ((ptm->tm_sec/2) + (32* ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
342
+ }
343
+
344
+
345
+ /****************************************************************************/
346
+
347
+ local int zip64local_getByte OF((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;
352
+ int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
353
+ if (err==1)
354
+ {
355
+ *pi = (int)c;
356
+ return ZIP_OK;
357
+ }
358
+ else
359
+ {
360
+ if (ZERROR64(*pzlib_filefunc_def,filestream))
361
+ return ZIP_ERRNO;
362
+ else
363
+ return ZIP_EOF;
364
+ }
365
+ }
366
+
367
+
368
+ /* ===========================================================================
369
+ Reads a long in LSB order from the given gz_stream. Sets
370
+ */
371
+ local int zip64local_getShort OF((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 ;
376
+ int i = 0;
377
+ int err;
378
+
379
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
380
+ x = (uLong)i;
381
+
382
+ if (err==ZIP_OK)
383
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
384
+ x += ((uLong)i)<<8;
385
+
386
+ if (err==ZIP_OK)
387
+ *pX = x;
388
+ else
389
+ *pX = 0;
390
+ return err;
391
+ }
392
+
393
+ local int zip64local_getLong OF((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 ;
398
+ int i = 0;
399
+ int err;
400
+
401
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
402
+ x = (uLong)i;
403
+
404
+ if (err==ZIP_OK)
405
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
406
+ x += ((uLong)i)<<8;
407
+
408
+ if (err==ZIP_OK)
409
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
410
+ x += ((uLong)i)<<16;
411
+
412
+ if (err==ZIP_OK)
413
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
414
+ x += ((uLong)i)<<24;
415
+
416
+ if (err==ZIP_OK)
417
+ *pX = x;
418
+ else
419
+ *pX = 0;
420
+ return err;
421
+ }
422
+
423
+ local int zip64local_getLong64 OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX));
424
+
425
+
426
+ local int zip64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
427
+ {
428
+ ZPOS64_T x;
429
+ int i = 0;
430
+ int err;
431
+
432
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
433
+ x = (ZPOS64_T)i;
434
+
435
+ if (err==ZIP_OK)
436
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
437
+ x += ((ZPOS64_T)i)<<8;
438
+
439
+ if (err==ZIP_OK)
440
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
441
+ x += ((ZPOS64_T)i)<<16;
442
+
443
+ if (err==ZIP_OK)
444
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
445
+ x += ((ZPOS64_T)i)<<24;
446
+
447
+ if (err==ZIP_OK)
448
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
449
+ x += ((ZPOS64_T)i)<<32;
450
+
451
+ if (err==ZIP_OK)
452
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
453
+ x += ((ZPOS64_T)i)<<40;
454
+
455
+ if (err==ZIP_OK)
456
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
457
+ x += ((ZPOS64_T)i)<<48;
458
+
459
+ if (err==ZIP_OK)
460
+ err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
461
+ x += ((ZPOS64_T)i)<<56;
462
+
463
+ if (err==ZIP_OK)
464
+ *pX = x;
465
+ else
466
+ *pX = 0;
467
+
468
+ return err;
469
+ }
470
+
471
+ #ifndef BUFREADCOMMENT
472
+ #define BUFREADCOMMENT (0x400)
473
+ #endif
474
+ /*
475
+ Locate the Central directory of a zipfile (at the end, just before
476
+ the global comment)
477
+ */
478
+ local ZPOS64_T zip64local_SearchCentralDir OF((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;
483
+ ZPOS64_T uSizeFile;
484
+ ZPOS64_T uBackRead;
485
+ ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
486
+ ZPOS64_T uPosFound=0;
487
+
488
+ if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
489
+ return 0;
490
+
491
+
492
+ uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
493
+
494
+ if (uMaxBack>uSizeFile)
495
+ uMaxBack = uSizeFile;
496
+
497
+ buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
498
+ if (buf==NULL)
499
+ return 0;
500
+
501
+ uBackRead = 4;
502
+ while (uBackRead<uMaxBack)
503
+ {
504
+ uLong uReadSize;
505
+ ZPOS64_T uReadPos ;
506
+ int i;
507
+ if (uBackRead+BUFREADCOMMENT>uMaxBack)
508
+ uBackRead = uMaxBack;
509
+ else
510
+ uBackRead+=BUFREADCOMMENT;
511
+ uReadPos = uSizeFile-uBackRead ;
512
+
513
+ uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
514
+ (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
515
+ if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
516
+ break;
517
+
518
+ if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
519
+ break;
520
+
521
+ for (i=(int)uReadSize-3; (i--)>0;)
522
+ if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
523
+ ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
524
+ {
525
+ uPosFound = uReadPos+i;
526
+ break;
527
+ }
528
+
529
+ if (uPosFound!=0)
530
+ break;
531
+ }
532
+ TRYFREE(buf);
533
+ return uPosFound;
534
+ }
535
+
536
+ /*
537
+ 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)
539
+ */
540
+ local ZPOS64_T zip64local_SearchCentralDir64 OF((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;
545
+ ZPOS64_T uSizeFile;
546
+ ZPOS64_T uBackRead;
547
+ ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
548
+ ZPOS64_T uPosFound=0;
549
+ uLong uL;
550
+ ZPOS64_T relativeOffset;
551
+
552
+ if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
553
+ return 0;
554
+
555
+ uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
556
+
557
+ if (uMaxBack>uSizeFile)
558
+ uMaxBack = uSizeFile;
559
+
560
+ buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
561
+ if (buf==NULL)
562
+ return 0;
563
+
564
+ uBackRead = 4;
565
+ while (uBackRead<uMaxBack)
566
+ {
567
+ uLong uReadSize;
568
+ ZPOS64_T uReadPos;
569
+ int i;
570
+ if (uBackRead+BUFREADCOMMENT>uMaxBack)
571
+ uBackRead = uMaxBack;
572
+ else
573
+ uBackRead+=BUFREADCOMMENT;
574
+ uReadPos = uSizeFile-uBackRead ;
575
+
576
+ uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
577
+ (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
578
+ if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
579
+ break;
580
+
581
+ if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
582
+ break;
583
+
584
+ for (i=(int)uReadSize-3; (i--)>0;)
585
+ {
586
+ // 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))
588
+ {
589
+ uPosFound = uReadPos+i;
590
+ break;
591
+ }
592
+ }
593
+
594
+ if (uPosFound!=0)
595
+ break;
596
+ }
597
+
598
+ TRYFREE(buf);
599
+ if (uPosFound == 0)
600
+ return 0;
601
+
602
+ /* Zip64 end of central directory locator */
603
+ if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
604
+ return 0;
605
+
606
+ /* the signature, already checked */
607
+ if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
608
+ return 0;
609
+
610
+ /* number of the disk with the start of the zip64 end of central directory */
611
+ if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
612
+ return 0;
613
+ if (uL != 0)
614
+ return 0;
615
+
616
+ /* relative offset of the zip64 end of central directory record */
617
+ if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
618
+ return 0;
619
+
620
+ /* total number of disks */
621
+ if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
622
+ return 0;
623
+ if (uL != 1)
624
+ return 0;
625
+
626
+ /* Goto Zip64 end of central directory record */
627
+ if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
628
+ return 0;
629
+
630
+ /* the signature */
631
+ if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
632
+ return 0;
633
+
634
+ if (uL != 0x06064b50) // signature of 'Zip64 end of central directory'
635
+ return 0;
636
+
637
+ return relativeOffset;
638
+ }
639
+
640
+ int LoadCentralDirectoryRecord(zip64_internal* pziinit);
641
+ int LoadCentralDirectoryRecord(zip64_internal* pziinit)
642
+ {
643
+ int err=ZIP_OK;
644
+ ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
645
+
646
+ ZPOS64_T size_central_dir; /* size of the central directory */
647
+ ZPOS64_T offset_central_dir; /* offset of start of central directory */
648
+ ZPOS64_T central_pos;
649
+ uLong uL;
650
+
651
+ uLong number_disk; /* number of the current dist, used for
652
+ spaning ZIP, unsupported, always 0*/
653
+ uLong number_disk_with_CD; /* number the the disk with central dir, used
654
+ for spaning ZIP, unsupported, always 0*/
655
+ ZPOS64_T number_entry;
656
+ ZPOS64_T number_entry_CD; /* total number of entries in
657
+ the central dir
658
+ (same than number_entry on nospan) */
659
+ uLong VersionMadeBy;
660
+ uLong VersionNeeded;
661
+ uLong size_comment;
662
+
663
+ int hasZIP64Record = 0;
664
+
665
+ // check first if we find a ZIP64 record
666
+ central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
667
+ if(central_pos > 0)
668
+ {
669
+ hasZIP64Record = 1;
670
+ }
671
+ else if(central_pos == 0)
672
+ {
673
+ central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
674
+ }
675
+
676
+ /* disable to allow appending to empty ZIP archive
677
+ if (central_pos==0)
678
+ err=ZIP_ERRNO;
679
+ */
680
+
681
+ if(hasZIP64Record)
682
+ {
683
+ ZPOS64_T sizeEndOfCentralDirectory;
684
+ if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
685
+ err=ZIP_ERRNO;
686
+
687
+ /* the signature, already checked */
688
+ if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
689
+ err=ZIP_ERRNO;
690
+
691
+ /* size of zip64 end of central directory record */
692
+ if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
693
+ err=ZIP_ERRNO;
694
+
695
+ /* version made by */
696
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
697
+ err=ZIP_ERRNO;
698
+
699
+ /* version needed to extract */
700
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
701
+ err=ZIP_ERRNO;
702
+
703
+ /* number of this disk */
704
+ if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
705
+ err=ZIP_ERRNO;
706
+
707
+ /* number of the disk with the start of the central directory */
708
+ if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
709
+ err=ZIP_ERRNO;
710
+
711
+ /* total number of entries in the central directory on this disk */
712
+ if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
713
+ err=ZIP_ERRNO;
714
+
715
+ /* total number of entries in the central directory */
716
+ if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
717
+ err=ZIP_ERRNO;
718
+
719
+ if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
720
+ err=ZIP_BADZIPFILE;
721
+
722
+ /* size of the central directory */
723
+ if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
724
+ err=ZIP_ERRNO;
725
+
726
+ /* offset of start of central directory with respect to the
727
+ starting disk number */
728
+ if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
729
+ err=ZIP_ERRNO;
730
+
731
+ // TODO..
732
+ // read the comment from the standard central header.
733
+ size_comment = 0;
734
+ }
735
+ else
736
+ {
737
+ // Read End of central Directory info
738
+ if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
739
+ err=ZIP_ERRNO;
740
+
741
+ /* the signature, already checked */
742
+ if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
743
+ err=ZIP_ERRNO;
744
+
745
+ /* number of this disk */
746
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
747
+ err=ZIP_ERRNO;
748
+
749
+ /* number of the disk with the start of the central directory */
750
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
751
+ err=ZIP_ERRNO;
752
+
753
+ /* total number of entries in the central dir on this disk */
754
+ number_entry = 0;
755
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
756
+ err=ZIP_ERRNO;
757
+ else
758
+ number_entry = uL;
759
+
760
+ /* total number of entries in the central dir */
761
+ number_entry_CD = 0;
762
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
763
+ err=ZIP_ERRNO;
764
+ else
765
+ number_entry_CD = uL;
766
+
767
+ if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
768
+ err=ZIP_BADZIPFILE;
769
+
770
+ /* size of the central directory */
771
+ size_central_dir = 0;
772
+ if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
773
+ err=ZIP_ERRNO;
774
+ else
775
+ size_central_dir = uL;
776
+
777
+ /* offset of start of central directory with respect to the starting disk number */
778
+ offset_central_dir = 0;
779
+ if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
780
+ err=ZIP_ERRNO;
781
+ else
782
+ offset_central_dir = uL;
783
+
784
+
785
+ /* zipfile global comment length */
786
+ if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
787
+ err=ZIP_ERRNO;
788
+ }
789
+
790
+ if ((central_pos<offset_central_dir+size_central_dir) &&
791
+ (err==ZIP_OK))
792
+ err=ZIP_BADZIPFILE;
793
+
794
+ if (err!=ZIP_OK)
795
+ {
796
+ ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
797
+ return ZIP_ERRNO;
798
+ }
799
+
800
+ if (size_comment>0)
801
+ {
802
+ pziinit->globalcomment = (char*)ALLOC(size_comment+1);
803
+ if (pziinit->globalcomment)
804
+ {
805
+ size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
806
+ pziinit->globalcomment[size_comment]=0;
807
+ }
808
+ }
809
+
810
+ byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
811
+ pziinit->add_position_when_writting_offset = byte_before_the_zipfile;
812
+
813
+ {
814
+ ZPOS64_T size_central_dir_to_read = size_central_dir;
815
+ size_t buf_size = SIZEDATA_INDATABLOCK;
816
+ void* buf_read = (void*)ALLOC(buf_size);
817
+ if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
818
+ err=ZIP_ERRNO;
819
+
820
+ while ((size_central_dir_to_read>0) && (err==ZIP_OK))
821
+ {
822
+ ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
823
+ if (read_this > size_central_dir_to_read)
824
+ read_this = size_central_dir_to_read;
825
+
826
+ if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
827
+ err=ZIP_ERRNO;
828
+
829
+ if (err==ZIP_OK)
830
+ err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
831
+
832
+ size_central_dir_to_read-=read_this;
833
+ }
834
+ TRYFREE(buf_read);
835
+ }
836
+ pziinit->begin_pos = byte_before_the_zipfile;
837
+ pziinit->number_entry = number_entry_CD;
838
+
839
+ if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
840
+ err=ZIP_ERRNO;
841
+
842
+ return err;
843
+ }
844
+
845
+
846
+ #endif /* !NO_ADDFILEINEXISTINGZIP*/
847
+
848
+
849
+ /************************************************************/
850
+ extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def);
851
+ extern zipFile ZEXPORT zipOpen3 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def)
852
+ {
853
+ zip64_internal ziinit;
854
+ zip64_internal* zi;
855
+ int err=ZIP_OK;
856
+
857
+ ziinit.z_filefunc.zseek32_file = NULL;
858
+ ziinit.z_filefunc.ztell32_file = NULL;
859
+ if (pzlib_filefunc64_32_def==NULL)
860
+ fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
861
+ else
862
+ ziinit.z_filefunc = *pzlib_filefunc64_32_def;
863
+
864
+ ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
865
+ pathname,
866
+ (append == APPEND_STATUS_CREATE) ?
867
+ (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
868
+ (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
869
+
870
+ if (ziinit.filestream == NULL)
871
+ return NULL;
872
+
873
+ if (append == APPEND_STATUS_CREATEAFTER)
874
+ ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
875
+
876
+ ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
877
+ ziinit.in_opened_file_inzip = 0;
878
+ ziinit.ci.stream_initialised = 0;
879
+ ziinit.number_entry = 0;
880
+ ziinit.add_position_when_writting_offset = 0;
881
+ init_linkedlist(&(ziinit.central_dir));
882
+
883
+
884
+
885
+ zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
886
+ if (zi==NULL)
887
+ {
888
+ ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
889
+ return NULL;
890
+ }
891
+
892
+ /* now we add file in a zipfile */
893
+ # ifndef NO_ADDFILEINEXISTINGZIP
894
+ ziinit.globalcomment = NULL;
895
+ if (append == APPEND_STATUS_ADDINZIP)
896
+ {
897
+ // Read and Cache Central Directory Records
898
+ err = LoadCentralDirectoryRecord(&ziinit);
899
+ }
900
+
901
+ if (globalcomment)
902
+ {
903
+ *globalcomment = ziinit.globalcomment;
904
+ }
905
+ # endif /* !NO_ADDFILEINEXISTINGZIP*/
906
+
907
+ if (err != ZIP_OK)
908
+ {
909
+ # ifndef NO_ADDFILEINEXISTINGZIP
910
+ TRYFREE(ziinit.globalcomment);
911
+ # endif /* !NO_ADDFILEINEXISTINGZIP*/
912
+ TRYFREE(zi);
913
+ return NULL;
914
+ }
915
+ else
916
+ {
917
+ *zi = ziinit;
918
+ return (zipFile)zi;
919
+ }
920
+ }
921
+
922
+ extern zipFile ZEXPORT zipOpen2 (const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def)
923
+ {
924
+ if (pzlib_filefunc32_def != NULL)
925
+ {
926
+ zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
927
+ fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
928
+ return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
929
+ }
930
+ else
931
+ return zipOpen3(pathname, append, globalcomment, NULL);
932
+ }
933
+
934
+ extern zipFile ZEXPORT zipOpen2_64 (const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def)
935
+ {
936
+ if (pzlib_filefunc_def != NULL)
937
+ {
938
+ zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
939
+ zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
940
+ zlib_filefunc64_32_def_fill.ztell32_file = NULL;
941
+ zlib_filefunc64_32_def_fill.zseek32_file = NULL;
942
+ return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
943
+ }
944
+ else
945
+ return zipOpen3(pathname, append, globalcomment, NULL);
946
+ }
947
+
948
+
949
+
950
+ extern zipFile ZEXPORT zipOpen (const char* pathname, int append)
951
+ {
952
+ return zipOpen3((const void*)pathname,append,NULL,NULL);
953
+ }
954
+
955
+ extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append)
956
+ {
957
+ return zipOpen3(pathname,append,NULL,NULL);
958
+ }
959
+
960
+ int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local);
961
+ int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local)
962
+ {
963
+ /* write the local header */
964
+ int err;
965
+ uInt size_filename = (uInt)strlen(filename);
966
+ uInt size_extrafield = size_extrafield_local;
967
+
968
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
969
+
970
+ if (err==ZIP_OK)
971
+ {
972
+ if(zi->ci.zip64)
973
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
974
+ else
975
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
976
+ }
977
+
978
+ if (err==ZIP_OK)
979
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
980
+
981
+ if (err==ZIP_OK)
982
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
983
+
984
+ if (err==ZIP_OK)
985
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
986
+
987
+ // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later
988
+ if (err==ZIP_OK)
989
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
990
+ if (err==ZIP_OK)
991
+ {
992
+ if(zi->ci.zip64)
993
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */
994
+ else
995
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
996
+ }
997
+ if (err==ZIP_OK)
998
+ {
999
+ if(zi->ci.zip64)
1000
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */
1001
+ else
1002
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
1003
+ }
1004
+
1005
+ if (err==ZIP_OK)
1006
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
1007
+
1008
+ if(zi->ci.zip64)
1009
+ {
1010
+ size_extrafield += 20;
1011
+ }
1012
+
1013
+ if (err==ZIP_OK)
1014
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2);
1015
+
1016
+ if ((err==ZIP_OK) && (size_filename > 0))
1017
+ {
1018
+ if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
1019
+ err = ZIP_ERRNO;
1020
+ }
1021
+
1022
+ if ((err==ZIP_OK) && (size_extrafield_local > 0))
1023
+ {
1024
+ if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
1025
+ err = ZIP_ERRNO;
1026
+ }
1027
+
1028
+
1029
+ if ((err==ZIP_OK) && (zi->ci.zip64))
1030
+ {
1031
+ // write the Zip64 extended info
1032
+ short HeaderID = 1;
1033
+ short DataSize = 16;
1034
+ ZPOS64_T CompressedSize = 0;
1035
+ ZPOS64_T UncompressedSize = 0;
1036
+
1037
+ // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
1038
+ zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
1039
+
1040
+ #ifndef __clang_analyzer__
1041
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)HeaderID,2);
1042
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (short)DataSize,2);
1043
+
1044
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
1045
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
1046
+ #endif
1047
+ }
1048
+
1049
+ return err;
1050
+ }
1051
+
1052
+ /*
1053
+ NOTE.
1054
+ When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped
1055
+ before calling this function it can be done with zipRemoveExtraInfoBlock
1056
+
1057
+ It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
1058
+ unnecessary allocations.
1059
+ */
1060
+ extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1061
+ const void* extrafield_local, uInt size_extrafield_local,
1062
+ const void* extrafield_global, uInt size_extrafield_global,
1063
+ const char* comment, int method, int level, int raw,
1064
+ int windowBits,int memLevel, int strategy,
1065
+ const char* password, uLong crcForCrypting,
1066
+ uLong versionMadeBy, uLong flagBase, int zip64)
1067
+ {
1068
+ zip64_internal* zi;
1069
+ uInt size_filename;
1070
+ uInt size_comment;
1071
+ uInt i;
1072
+ int err = ZIP_OK;
1073
+
1074
+ # ifdef NOCRYPT
1075
+ if (password != NULL)
1076
+ return ZIP_PARAMERROR;
1077
+ # endif
1078
+
1079
+ if (file == NULL)
1080
+ return ZIP_PARAMERROR;
1081
+
1082
+ #ifdef HAVE_BZIP2
1083
+ if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
1084
+ return ZIP_PARAMERROR;
1085
+ #else
1086
+ if ((method!=0) && (method!=Z_DEFLATED))
1087
+ return ZIP_PARAMERROR;
1088
+ #endif
1089
+
1090
+ zi = (zip64_internal*)file;
1091
+
1092
+ if (zi->in_opened_file_inzip == 1)
1093
+ {
1094
+ err = zipCloseFileInZip (file);
1095
+ if (err != ZIP_OK)
1096
+ return err;
1097
+ }
1098
+
1099
+ if (filename==NULL)
1100
+ filename="-";
1101
+
1102
+ if (comment==NULL)
1103
+ size_comment = 0;
1104
+ else
1105
+ size_comment = (uInt)strlen(comment);
1106
+
1107
+ size_filename = (uInt)strlen(filename);
1108
+
1109
+ if (zipfi == NULL)
1110
+ zi->ci.dosDate = 0;
1111
+ else
1112
+ {
1113
+ if (zipfi->dosDate != 0)
1114
+ zi->ci.dosDate = zipfi->dosDate;
1115
+ else
1116
+ zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
1117
+ }
1118
+
1119
+ zi->ci.flag = flagBase;
1120
+ if (level==8 || level==9)
1121
+ zi->ci.flag |= 2;
1122
+ if (level==2)
1123
+ zi->ci.flag |= 4;
1124
+ if (level==1)
1125
+ zi->ci.flag |= 6;
1126
+ if (password != NULL)
1127
+ zi->ci.flag |= 1;
1128
+
1129
+ zi->ci.crc32 = 0;
1130
+ zi->ci.method = method;
1131
+ zi->ci.encrypt = 0;
1132
+ zi->ci.stream_initialised = 0;
1133
+ zi->ci.pos_in_buffered_data = 0;
1134
+ zi->ci.raw = raw;
1135
+ zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
1136
+
1137
+ zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
1138
+ zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
1139
+
1140
+ zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
1141
+
1142
+ zi->ci.size_centralExtra = size_extrafield_global;
1143
+ zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
1144
+ /* version info */
1145
+ zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
1146
+ zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
1147
+ zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
1148
+ zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
1149
+ zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
1150
+ zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
1151
+ zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
1152
+ zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
1153
+ zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
1154
+ zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
1155
+ zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
1156
+ zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
1157
+
1158
+ if (zipfi==NULL)
1159
+ zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
1160
+ else
1161
+ zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
1162
+
1163
+ if (zipfi==NULL)
1164
+ zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
1165
+ else
1166
+ zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
1167
+
1168
+ if(zi->ci.pos_local_header >= 0xffffffff)
1169
+ zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
1170
+ else
1171
+ zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writting_offset,4);
1172
+
1173
+ for (i=0;i<size_filename;i++)
1174
+ *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
1175
+
1176
+ for (i=0;i<size_extrafield_global;i++)
1177
+ *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
1178
+ *(((const char*)extrafield_global)+i);
1179
+
1180
+ for (i=0;i<size_comment;i++)
1181
+ *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
1182
+ size_extrafield_global+i) = *(comment+i);
1183
+ if (zi->ci.central_header == NULL)
1184
+ return ZIP_INTERNALERROR;
1185
+
1186
+ zi->ci.zip64 = zip64;
1187
+ zi->ci.totalCompressedData = 0;
1188
+ zi->ci.totalUncompressedData = 0;
1189
+ zi->ci.pos_zip64extrainfo = 0;
1190
+
1191
+ err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
1192
+
1193
+ #ifdef HAVE_BZIP2
1194
+ zi->ci.bstream.avail_in = (uInt)0;
1195
+ zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1196
+ zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1197
+ zi->ci.bstream.total_in_hi32 = 0;
1198
+ zi->ci.bstream.total_in_lo32 = 0;
1199
+ zi->ci.bstream.total_out_hi32 = 0;
1200
+ zi->ci.bstream.total_out_lo32 = 0;
1201
+ #endif
1202
+
1203
+ zi->ci.stream.avail_in = (uInt)0;
1204
+ zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1205
+ zi->ci.stream.next_out = zi->ci.buffered_data;
1206
+ zi->ci.stream.total_in = 0;
1207
+ zi->ci.stream.total_out = 0;
1208
+ zi->ci.stream.data_type = Z_BINARY;
1209
+
1210
+ #ifdef HAVE_BZIP2
1211
+ if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1212
+ #else
1213
+ if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1214
+ #endif
1215
+ {
1216
+ if(zi->ci.method == Z_DEFLATED)
1217
+ {
1218
+ zi->ci.stream.zalloc = (alloc_func)0;
1219
+ zi->ci.stream.zfree = (free_func)0;
1220
+ zi->ci.stream.opaque = (voidpf)0;
1221
+
1222
+ if (windowBits>0)
1223
+ windowBits = -windowBits;
1224
+
1225
+ err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
1226
+
1227
+ if (err==Z_OK)
1228
+ zi->ci.stream_initialised = Z_DEFLATED;
1229
+ }
1230
+ else if(zi->ci.method == Z_BZIP2ED)
1231
+ {
1232
+ #ifdef HAVE_BZIP2
1233
+ // Init BZip stuff here
1234
+ zi->ci.bstream.bzalloc = 0;
1235
+ zi->ci.bstream.bzfree = 0;
1236
+ zi->ci.bstream.opaque = (voidpf)0;
1237
+
1238
+ err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
1239
+ if(err == BZ_OK)
1240
+ zi->ci.stream_initialised = Z_BZIP2ED;
1241
+ #endif
1242
+ }
1243
+
1244
+ }
1245
+
1246
+ # ifndef NOCRYPT
1247
+ zi->ci.crypt_header_size = 0;
1248
+ if ((err==Z_OK) && (password != NULL))
1249
+ {
1250
+ unsigned char bufHead[RAND_HEAD_LEN];
1251
+ unsigned int sizeHead;
1252
+ zi->ci.encrypt = 1;
1253
+ zi->ci.pcrc_32_tab = (const unsigned long*)get_crc_table();
1254
+ /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
1255
+
1256
+ sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
1257
+ zi->ci.crypt_header_size = sizeHead;
1258
+
1259
+ if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
1260
+ err = ZIP_ERRNO;
1261
+ }
1262
+ # endif
1263
+
1264
+ if (err==Z_OK)
1265
+ zi->in_opened_file_inzip = 1;
1266
+ return err;
1267
+ }
1268
+
1269
+ extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1270
+ const void* extrafield_local, uInt size_extrafield_local,
1271
+ const void* extrafield_global, uInt size_extrafield_global,
1272
+ const char* comment, int method, int level, int raw,
1273
+ int windowBits,int memLevel, int strategy,
1274
+ const char* password, uLong crcForCrypting,
1275
+ uLong versionMadeBy, uLong flagBase)
1276
+ {
1277
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1278
+ extrafield_local, size_extrafield_local,
1279
+ extrafield_global, size_extrafield_global,
1280
+ comment, method, level, raw,
1281
+ windowBits, memLevel, strategy,
1282
+ password, crcForCrypting, versionMadeBy, flagBase, 0);
1283
+ }
1284
+
1285
+ extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1286
+ const void* extrafield_local, uInt size_extrafield_local,
1287
+ const void* extrafield_global, uInt size_extrafield_global,
1288
+ const char* comment, int method, int level, int raw,
1289
+ int windowBits,int memLevel, int strategy,
1290
+ const char* password, uLong crcForCrypting)
1291
+ {
1292
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1293
+ extrafield_local, size_extrafield_local,
1294
+ extrafield_global, size_extrafield_global,
1295
+ comment, method, level, raw,
1296
+ windowBits, memLevel, strategy,
1297
+ password, crcForCrypting, VERSIONMADEBY, 0, 0);
1298
+ }
1299
+
1300
+ extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1301
+ const void* extrafield_local, uInt size_extrafield_local,
1302
+ const void* extrafield_global, uInt size_extrafield_global,
1303
+ const char* comment, int method, int level, int raw,
1304
+ int windowBits,int memLevel, int strategy,
1305
+ const char* password, uLong crcForCrypting, int zip64)
1306
+ {
1307
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1308
+ extrafield_local, size_extrafield_local,
1309
+ extrafield_global, size_extrafield_global,
1310
+ comment, method, level, raw,
1311
+ windowBits, memLevel, strategy,
1312
+ password, crcForCrypting, VERSIONMADEBY, 0, zip64);
1313
+ }
1314
+
1315
+ extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1316
+ const void* extrafield_local, uInt size_extrafield_local,
1317
+ const void* extrafield_global, uInt size_extrafield_global,
1318
+ const char* comment, int method, int level, int raw)
1319
+ {
1320
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1321
+ extrafield_local, size_extrafield_local,
1322
+ extrafield_global, size_extrafield_global,
1323
+ comment, method, level, raw,
1324
+ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1325
+ NULL, 0, VERSIONMADEBY, 0, 0);
1326
+ }
1327
+
1328
+ extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
1329
+ const void* extrafield_local, uInt size_extrafield_local,
1330
+ const void* extrafield_global, uInt size_extrafield_global,
1331
+ const char* comment, int method, int level, int raw, int zip64)
1332
+ {
1333
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1334
+ extrafield_local, size_extrafield_local,
1335
+ extrafield_global, size_extrafield_global,
1336
+ comment, method, level, raw,
1337
+ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1338
+ NULL, 0, VERSIONMADEBY, 0, zip64);
1339
+ }
1340
+
1341
+ extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1342
+ const void* extrafield_local, uInt size_extrafield_local,
1343
+ const void*extrafield_global, uInt size_extrafield_global,
1344
+ const char* comment, int method, int level, int zip64)
1345
+ {
1346
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1347
+ extrafield_local, size_extrafield_local,
1348
+ extrafield_global, size_extrafield_global,
1349
+ comment, method, level, 0,
1350
+ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1351
+ NULL, 0, VERSIONMADEBY, 0, zip64);
1352
+ }
1353
+
1354
+ extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi,
1355
+ const void* extrafield_local, uInt size_extrafield_local,
1356
+ const void*extrafield_global, uInt size_extrafield_global,
1357
+ const char* comment, int method, int level)
1358
+ {
1359
+ return zipOpenNewFileInZip4_64 (file, filename, zipfi,
1360
+ extrafield_local, size_extrafield_local,
1361
+ extrafield_global, size_extrafield_global,
1362
+ comment, method, level, 0,
1363
+ -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
1364
+ NULL, 0, VERSIONMADEBY, 0, 0);
1365
+ }
1366
+
1367
+ local int zip64FlushWriteBuffer(zip64_internal* zi)
1368
+ {
1369
+ int err=ZIP_OK;
1370
+
1371
+ if (zi->ci.encrypt != 0)
1372
+ {
1373
+ #ifndef NOCRYPT
1374
+ uInt i;
1375
+ int t;
1376
+ for (i=0;i<zi->ci.pos_in_buffered_data;i++)
1377
+ zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t);
1378
+ #endif
1379
+ }
1380
+
1381
+ if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
1382
+ err = ZIP_ERRNO;
1383
+
1384
+ zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
1385
+
1386
+ #ifdef HAVE_BZIP2
1387
+ if(zi->ci.method == Z_BZIP2ED)
1388
+ {
1389
+ zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32;
1390
+ zi->ci.bstream.total_in_lo32 = 0;
1391
+ zi->ci.bstream.total_in_hi32 = 0;
1392
+ }
1393
+ else
1394
+ #endif
1395
+ {
1396
+ zi->ci.totalUncompressedData += zi->ci.stream.total_in;
1397
+ zi->ci.stream.total_in = 0;
1398
+ }
1399
+
1400
+
1401
+ zi->ci.pos_in_buffered_data = 0;
1402
+
1403
+ return err;
1404
+ }
1405
+
1406
+ extern int ZEXPORT zipWriteInFileInZip (zipFile file,const void* buf,unsigned int len)
1407
+ {
1408
+ zip64_internal* zi;
1409
+ int err=ZIP_OK;
1410
+
1411
+ if (file == NULL)
1412
+ return ZIP_PARAMERROR;
1413
+ zi = (zip64_internal*)file;
1414
+
1415
+ if (zi->in_opened_file_inzip == 0)
1416
+ return ZIP_PARAMERROR;
1417
+
1418
+ zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
1419
+
1420
+ #ifdef HAVE_BZIP2
1421
+ if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw))
1422
+ {
1423
+ zi->ci.bstream.next_in = (void*)buf;
1424
+ zi->ci.bstream.avail_in = len;
1425
+ err = BZ_RUN_OK;
1426
+
1427
+ while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0))
1428
+ {
1429
+ if (zi->ci.bstream.avail_out == 0)
1430
+ {
1431
+ if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1432
+ err = ZIP_ERRNO;
1433
+ zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1434
+ zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1435
+ }
1436
+
1437
+
1438
+ if(err != BZ_RUN_OK)
1439
+ break;
1440
+
1441
+ if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1442
+ {
1443
+ uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32;
1444
+ // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32;
1445
+ err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN);
1446
+
1447
+ zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ;
1448
+ }
1449
+ }
1450
+
1451
+ if(err == BZ_RUN_OK)
1452
+ err = ZIP_OK;
1453
+ }
1454
+ else
1455
+ #endif
1456
+ {
1457
+ zi->ci.stream.next_in = (Bytef*)buf;
1458
+ zi->ci.stream.avail_in = len;
1459
+
1460
+ while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
1461
+ {
1462
+ if (zi->ci.stream.avail_out == 0)
1463
+ {
1464
+ if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1465
+ err = ZIP_ERRNO;
1466
+ zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1467
+ zi->ci.stream.next_out = zi->ci.buffered_data;
1468
+ }
1469
+
1470
+
1471
+ if(err != ZIP_OK)
1472
+ break;
1473
+
1474
+ if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1475
+ {
1476
+ uLong uTotalOutBefore = zi->ci.stream.total_out;
1477
+ err=deflate(&zi->ci.stream, Z_NO_FLUSH);
1478
+ if(uTotalOutBefore > zi->ci.stream.total_out)
1479
+ {
1480
+ int bBreak = 0;
1481
+ bBreak++;
1482
+ }
1483
+
1484
+ zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1485
+ }
1486
+ else
1487
+ {
1488
+ uInt copy_this,i;
1489
+ if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
1490
+ copy_this = zi->ci.stream.avail_in;
1491
+ else
1492
+ copy_this = zi->ci.stream.avail_out;
1493
+
1494
+ for (i = 0; i < copy_this; i++)
1495
+ *(((char*)zi->ci.stream.next_out)+i) =
1496
+ *(((const char*)zi->ci.stream.next_in)+i);
1497
+ {
1498
+ zi->ci.stream.avail_in -= copy_this;
1499
+ zi->ci.stream.avail_out-= copy_this;
1500
+ zi->ci.stream.next_in+= copy_this;
1501
+ zi->ci.stream.next_out+= copy_this;
1502
+ zi->ci.stream.total_in+= copy_this;
1503
+ zi->ci.stream.total_out+= copy_this;
1504
+ zi->ci.pos_in_buffered_data += copy_this;
1505
+ }
1506
+ }
1507
+ }// while(...)
1508
+ }
1509
+
1510
+ return err;
1511
+ }
1512
+
1513
+ extern int ZEXPORT zipCloseFileInZipRaw (zipFile file, uLong uncompressed_size, uLong crc32)
1514
+ {
1515
+ return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
1516
+ }
1517
+
1518
+ extern int ZEXPORT zipCloseFileInZipRaw64 (zipFile file, ZPOS64_T uncompressed_size, uLong crc32)
1519
+ {
1520
+ zip64_internal* zi;
1521
+ ZPOS64_T compressed_size;
1522
+ uLong invalidValue = 0xffffffff;
1523
+ short datasize = 0;
1524
+ int err=ZIP_OK;
1525
+
1526
+ if (file == NULL)
1527
+ return ZIP_PARAMERROR;
1528
+ zi = (zip64_internal*)file;
1529
+
1530
+ if (zi->in_opened_file_inzip == 0)
1531
+ return ZIP_PARAMERROR;
1532
+ zi->ci.stream.avail_in = 0;
1533
+
1534
+ if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1535
+ {
1536
+ while (err==ZIP_OK)
1537
+ {
1538
+ uLong uTotalOutBefore;
1539
+ if (zi->ci.stream.avail_out == 0)
1540
+ {
1541
+ if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1542
+ {
1543
+ #ifndef __clang_analyzer__
1544
+ err = ZIP_ERRNO;
1545
+ #endif
1546
+ }
1547
+ zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
1548
+ #ifndef __clang_analyzer__
1549
+ zi->ci.stream.next_out = zi->ci.buffered_data;
1550
+ #endif
1551
+ }
1552
+ uTotalOutBefore = zi->ci.stream.total_out;
1553
+ err=deflate(&zi->ci.stream, Z_FINISH);
1554
+ zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
1555
+ }
1556
+ }
1557
+ else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1558
+ {
1559
+ #ifdef HAVE_BZIP2
1560
+ err = BZ_FINISH_OK;
1561
+ while (err==BZ_FINISH_OK)
1562
+ {
1563
+ uLong uTotalOutBefore;
1564
+ if (zi->ci.bstream.avail_out == 0)
1565
+ {
1566
+ if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
1567
+ err = ZIP_ERRNO;
1568
+ zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
1569
+ zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
1570
+ }
1571
+ uTotalOutBefore = zi->ci.bstream.total_out_lo32;
1572
+ err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH);
1573
+ if(err == BZ_STREAM_END)
1574
+ err = Z_STREAM_END;
1575
+
1576
+ zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
1577
+ }
1578
+
1579
+ if(err == BZ_FINISH_OK)
1580
+ err = ZIP_OK;
1581
+ #endif
1582
+ }
1583
+
1584
+ if (err==Z_STREAM_END)
1585
+ err=ZIP_OK; /* this is normal */
1586
+
1587
+ if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
1588
+ {
1589
+ if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
1590
+ err = ZIP_ERRNO;
1591
+ }
1592
+
1593
+ if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
1594
+ {
1595
+ int tmp_err = deflateEnd(&zi->ci.stream);
1596
+ if (err == ZIP_OK)
1597
+ err = tmp_err;
1598
+ zi->ci.stream_initialised = 0;
1599
+ }
1600
+ #ifdef HAVE_BZIP2
1601
+ else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
1602
+ {
1603
+ int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
1604
+ if (err==ZIP_OK)
1605
+ err = tmperr;
1606
+ zi->ci.stream_initialised = 0;
1607
+ }
1608
+ #endif
1609
+
1610
+ if (!zi->ci.raw)
1611
+ {
1612
+ crc32 = (uLong)zi->ci.crc32;
1613
+ uncompressed_size = zi->ci.totalUncompressedData;
1614
+ }
1615
+ compressed_size = zi->ci.totalCompressedData;
1616
+
1617
+ # ifndef NOCRYPT
1618
+ compressed_size += zi->ci.crypt_header_size;
1619
+ # endif
1620
+
1621
+ // update Current Item crc and sizes,
1622
+ if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
1623
+ {
1624
+ /*version Made by*/
1625
+ zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
1626
+ /*version needed*/
1627
+ zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);
1628
+
1629
+ }
1630
+
1631
+ zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
1632
+
1633
+
1634
+ if(compressed_size >= 0xffffffff)
1635
+ zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
1636
+ else
1637
+ zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/
1638
+
1639
+ /// set internal file attributes field
1640
+ if (zi->ci.stream.data_type == Z_ASCII)
1641
+ zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
1642
+
1643
+ if(uncompressed_size >= 0xffffffff)
1644
+ zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
1645
+ else
1646
+ zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/
1647
+
1648
+ // Add ZIP64 extra info field for uncompressed size
1649
+ if(uncompressed_size >= 0xffffffff)
1650
+ datasize += 8;
1651
+
1652
+ // Add ZIP64 extra info field for compressed size
1653
+ if(compressed_size >= 0xffffffff)
1654
+ datasize += 8;
1655
+
1656
+ // Add ZIP64 extra info field for relative offset to local file header of current file
1657
+ if(zi->ci.pos_local_header >= 0xffffffff)
1658
+ datasize += 8;
1659
+
1660
+ if(datasize > 0)
1661
+ {
1662
+ char* p = NULL;
1663
+
1664
+ if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
1665
+ {
1666
+ // we can not write more data to the buffer that we have room for.
1667
+ return ZIP_BADZIPFILE;
1668
+ }
1669
+
1670
+ p = zi->ci.central_header + zi->ci.size_centralheader;
1671
+
1672
+ // Add Extra Information Header for 'ZIP64 information'
1673
+ zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
1674
+ p += 2;
1675
+ zip64local_putValue_inmemory(p, datasize, 2); // DataSize
1676
+ p += 2;
1677
+
1678
+ if(uncompressed_size >= 0xffffffff)
1679
+ {
1680
+ zip64local_putValue_inmemory(p, uncompressed_size, 8);
1681
+ p += 8;
1682
+ }
1683
+
1684
+ if(compressed_size >= 0xffffffff)
1685
+ {
1686
+ zip64local_putValue_inmemory(p, compressed_size, 8);
1687
+ p += 8;
1688
+ }
1689
+
1690
+ if(zi->ci.pos_local_header >= 0xffffffff)
1691
+ {
1692
+ zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
1693
+ #ifndef __clang_analyzer__
1694
+ p += 8;
1695
+ #endif
1696
+ }
1697
+
1698
+ // Update how much extra free space we got in the memory buffer
1699
+ // and increase the centralheader size so the new ZIP64 fields are included
1700
+ // ( 4 below is the size of HeaderID and DataSize field )
1701
+ zi->ci.size_centralExtraFree -= datasize + 4;
1702
+ zi->ci.size_centralheader += datasize + 4;
1703
+
1704
+ // Update the extra info size field
1705
+ zi->ci.size_centralExtra += datasize + 4;
1706
+ zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
1707
+ }
1708
+
1709
+ if (err==ZIP_OK)
1710
+ err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
1711
+
1712
+ free(zi->ci.central_header);
1713
+
1714
+ if (err==ZIP_OK)
1715
+ {
1716
+ // Update the LocalFileHeader with the new values.
1717
+
1718
+ ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
1719
+
1720
+ if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
1721
+ err = ZIP_ERRNO;
1722
+
1723
+ if (err==ZIP_OK)
1724
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
1725
+
1726
+ if(uncompressed_size >= 0xffffffff)
1727
+ {
1728
+ if(zi->ci.pos_zip64extrainfo > 0)
1729
+ {
1730
+ // Update the size in the ZIP64 extended field.
1731
+ if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
1732
+ err = ZIP_ERRNO;
1733
+
1734
+ if (err==ZIP_OK) /* compressed size, unknown */
1735
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
1736
+
1737
+ if (err==ZIP_OK) /* uncompressed size, unknown */
1738
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
1739
+ }
1740
+ }
1741
+ else
1742
+ {
1743
+ if (err==ZIP_OK) /* compressed size, unknown */
1744
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
1745
+
1746
+ if (err==ZIP_OK) /* uncompressed size, unknown */
1747
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
1748
+ }
1749
+
1750
+ if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
1751
+ err = ZIP_ERRNO;
1752
+ }
1753
+
1754
+ zi->number_entry ++;
1755
+ zi->in_opened_file_inzip = 0;
1756
+
1757
+ return err;
1758
+ }
1759
+
1760
+ extern int ZEXPORT zipCloseFileInZip (zipFile file)
1761
+ {
1762
+ return zipCloseFileInZipRaw (file,0,0);
1763
+ }
1764
+
1765
+ int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip);
1766
+ int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip)
1767
+ {
1768
+ int err = ZIP_OK;
1769
+ ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writting_offset;
1770
+
1771
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
1772
+
1773
+ /*num disks*/
1774
+ if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1775
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1776
+
1777
+ /*relative offset*/
1778
+ if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */
1779
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8);
1780
+
1781
+ /*total disks*/ /* Do not support spawning of disk so always say 1 here*/
1782
+ if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1783
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
1784
+
1785
+ return err;
1786
+ }
1787
+
1788
+ int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip);
1789
+ int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
1790
+ {
1791
+ int err = ZIP_OK;
1792
+
1793
+ uLong Zip64DataSize = 44;
1794
+
1795
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
1796
+
1797
+ if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */
1798
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ?
1799
+
1800
+ if (err==ZIP_OK) /* version made by */
1801
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
1802
+
1803
+ if (err==ZIP_OK) /* version needed */
1804
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
1805
+
1806
+ if (err==ZIP_OK) /* number of this disk */
1807
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1808
+
1809
+ if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1810
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
1811
+
1812
+ if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1813
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
1814
+
1815
+ if (err==ZIP_OK) /* total number of entries in the central dir */
1816
+ err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
1817
+
1818
+ if (err==ZIP_OK) /* size of the central directory */
1819
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8);
1820
+
1821
+ if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
1822
+ {
1823
+ ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
1824
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
1825
+ }
1826
+ return err;
1827
+ }
1828
+
1829
+ int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip);
1830
+ int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip)
1831
+ {
1832
+ int err = ZIP_OK;
1833
+
1834
+ /*signature*/
1835
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
1836
+
1837
+ if (err==ZIP_OK) /* number of this disk */
1838
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1839
+
1840
+ if (err==ZIP_OK) /* number of the disk with the start of the central directory */
1841
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
1842
+
1843
+ if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
1844
+ {
1845
+ {
1846
+ if(zi->number_entry >= 0xFFFF)
1847
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
1848
+ else
1849
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1850
+ }
1851
+ }
1852
+
1853
+ if (err==ZIP_OK) /* total number of entries in the central dir */
1854
+ {
1855
+ if(zi->number_entry >= 0xFFFF)
1856
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
1857
+ else
1858
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
1859
+ }
1860
+
1861
+ if (err==ZIP_OK) /* size of the central directory */
1862
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
1863
+
1864
+ if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
1865
+ {
1866
+ ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
1867
+ if(pos >= 0xffffffff)
1868
+ {
1869
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4);
1870
+ }
1871
+ else
1872
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writting_offset),4);
1873
+ }
1874
+
1875
+ return err;
1876
+ }
1877
+
1878
+ int Write_GlobalComment(zip64_internal* zi, const char* global_comment);
1879
+ int Write_GlobalComment(zip64_internal* zi, const char* global_comment)
1880
+ {
1881
+ int err = ZIP_OK;
1882
+ uInt size_global_comment = 0;
1883
+
1884
+ if(global_comment != NULL)
1885
+ size_global_comment = (uInt)strlen(global_comment);
1886
+
1887
+ err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
1888
+
1889
+ if (err == ZIP_OK && size_global_comment > 0)
1890
+ {
1891
+ if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment)
1892
+ err = ZIP_ERRNO;
1893
+ }
1894
+ return err;
1895
+ }
1896
+
1897
+ extern int ZEXPORT zipClose (zipFile file, const char* global_comment)
1898
+ {
1899
+ zip64_internal* zi;
1900
+ int err = 0;
1901
+ uLong size_centraldir = 0;
1902
+ ZPOS64_T centraldir_pos_inzip;
1903
+ ZPOS64_T pos;
1904
+
1905
+ if (file == NULL)
1906
+ return ZIP_PARAMERROR;
1907
+
1908
+ zi = (zip64_internal*)file;
1909
+
1910
+ if (zi->in_opened_file_inzip == 1)
1911
+ {
1912
+ err = zipCloseFileInZip (file);
1913
+ }
1914
+
1915
+ #ifndef NO_ADDFILEINEXISTINGZIP
1916
+ if (global_comment==NULL)
1917
+ global_comment = zi->globalcomment;
1918
+ #endif
1919
+
1920
+ centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
1921
+
1922
+ if (err==ZIP_OK)
1923
+ {
1924
+ linkedlist_datablock_internal* ldi = zi->central_dir.first_block;
1925
+ while (ldi!=NULL)
1926
+ {
1927
+ if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
1928
+ {
1929
+ if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block)
1930
+ err = ZIP_ERRNO;
1931
+ }
1932
+
1933
+ size_centraldir += ldi->filled_in_this_block;
1934
+ ldi = ldi->next_datablock;
1935
+ }
1936
+ }
1937
+ free_linkedlist(&(zi->central_dir));
1938
+
1939
+ pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
1940
+ if(pos >= 0xffffffff)
1941
+ {
1942
+ ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream);
1943
+ Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1944
+
1945
+ Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos);
1946
+ }
1947
+
1948
+ if (err==ZIP_OK)
1949
+ err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
1950
+
1951
+ if(err == ZIP_OK)
1952
+ err = Write_GlobalComment(zi, global_comment);
1953
+
1954
+ if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0)
1955
+ if (err == ZIP_OK)
1956
+ err = ZIP_ERRNO;
1957
+
1958
+ #ifndef NO_ADDFILEINEXISTINGZIP
1959
+ TRYFREE(zi->globalcomment);
1960
+ #endif
1961
+ TRYFREE(zi);
1962
+
1963
+ return err;
1964
+ }
1965
+
1966
+ extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHeader)
1967
+ {
1968
+ char* p = pData;
1969
+ int size = 0;
1970
+ char* pNewHeader;
1971
+ char* pTmp;
1972
+ short header;
1973
+ short dataSize;
1974
+
1975
+ int retVal = ZIP_OK;
1976
+
1977
+ if(pData == NULL || *dataLen < 4)
1978
+ return ZIP_PARAMERROR;
1979
+
1980
+ pNewHeader = (char*)ALLOC(*dataLen);
1981
+ pTmp = pNewHeader;
1982
+
1983
+ while(p < (pData + *dataLen))
1984
+ {
1985
+ header = *(short*)p;
1986
+ dataSize = *(((short*)p)+1);
1987
+
1988
+ if( header == sHeader ) // Header found.
1989
+ {
1990
+ p += dataSize + 4; // skip it. do not copy to temp buffer
1991
+ }
1992
+ else
1993
+ {
1994
+ // Extra Info block should not be removed, So copy it to the temp buffer.
1995
+ memcpy(pTmp, p, dataSize + 4);
1996
+ p += dataSize + 4;
1997
+ size += dataSize + 4;
1998
+ }
1999
+
2000
+ }
2001
+
2002
+ if(size < *dataLen)
2003
+ {
2004
+ // clean old extra info block.
2005
+ memset(pData,0, *dataLen);
2006
+
2007
+ // copy the new extra info block over the old
2008
+ if(size > 0)
2009
+ memcpy(pData, pNewHeader, size);
2010
+
2011
+ // set the new extra info size
2012
+ *dataLen = size;
2013
+
2014
+ retVal = ZIP_OK;
2015
+ }
2016
+ else
2017
+ retVal = ZIP_ERRNO;
2018
+
2019
+ TRYFREE(pNewHeader);
2020
+
2021
+ return retVal;
2022
+ }