cordova-plugin-mserviceinterface-2.0 1.0.26 → 1.0.27

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 (57) hide show
  1. package/package.json +28 -28
  2. package/plugin.xml +153 -153
  3. package/src/android/CallbackProgressDialog.java +53 -53
  4. package/src/android/FileProvider.java +7 -7
  5. package/src/android/backgroundNotification.java +120 -120
  6. package/src/android/doubleogradle.gradle +5 -5
  7. package/src/android/mInterface.java +2123 -2087
  8. package/src/android/mInterfaceReceiver.java +59 -59
  9. package/src/android/mInterfaceService.java +749 -749
  10. package/src/android/mInterfaceUtil.java +1686 -1686
  11. package/src/android/res/xml/network_security_config.xml +8 -8
  12. package/src/android/res/xml/opener_paths.xml +16 -16
  13. package/src/ios/InternetConnection.h +64 -64
  14. package/src/ios/InternetConnection.m +242 -242
  15. package/src/ios/SSZipArchive.h +146 -146
  16. package/src/ios/SSZipArchive.m +1180 -1180
  17. package/src/ios/SSZipCommon.h +66 -66
  18. package/src/ios/UICKeyChainStore.h +64 -64
  19. package/src/ios/UICKeyChainStore.m +434 -434
  20. package/src/ios/XMLConverter.h +61 -61
  21. package/src/ios/XMLConverter.m +243 -243
  22. package/src/ios/ZipArchive.h +19 -19
  23. package/src/ios/aes/aes.h +270 -270
  24. package/src/ios/aes/aes_ni.c +687 -687
  25. package/src/ios/aes/aes_ni.h +59 -59
  26. package/src/ios/aes/aescrypt.c +301 -301
  27. package/src/ios/aes/aeskey.c +554 -554
  28. package/src/ios/aes/aesopt.h +776 -776
  29. package/src/ios/aes/aestab.c +418 -418
  30. package/src/ios/aes/aestab.h +173 -173
  31. package/src/ios/aes/brg_endian.h +127 -127
  32. package/src/ios/aes/brg_types.h +191 -191
  33. package/src/ios/aes/fileenc.c +145 -145
  34. package/src/ios/aes/fileenc.h +121 -121
  35. package/src/ios/aes/hmac.c +209 -209
  36. package/src/ios/aes/hmac.h +119 -119
  37. package/src/ios/aes/prng.c +155 -155
  38. package/src/ios/aes/prng.h +82 -82
  39. package/src/ios/aes/pwd2key.c +181 -181
  40. package/src/ios/aes/pwd2key.h +45 -45
  41. package/src/ios/aes/sha1.c +283 -283
  42. package/src/ios/aes/sha1.h +72 -72
  43. package/src/ios/minizip/crypt.c +146 -146
  44. package/src/ios/minizip/crypt.h +66 -66
  45. package/src/ios/minizip/ioapi.c +353 -353
  46. package/src/ios/minizip/ioapi.h +154 -154
  47. package/src/ios/minizip/ioapi_buf.c +452 -452
  48. package/src/ios/minizip/ioapi_buf.h +52 -52
  49. package/src/ios/minizip/ioapi_mem.c +167 -167
  50. package/src/ios/minizip/ioapi_mem.h +52 -52
  51. package/src/ios/minizip/minishared.c +292 -292
  52. package/src/ios/minizip/minishared.h +50 -50
  53. package/src/ios/minizip/unzip.c +2007 -2007
  54. package/src/ios/minizip/unzip.h +254 -254
  55. package/src/ios/minizip/zip.c +2007 -2007
  56. package/src/ios/minizip/zip.h +215 -215
  57. package/www/mInterface.js +3 -3
@@ -1,2007 +1,2007 @@
1
- /* unzip.c -- IO for uncompress .zip files using zlib
2
- Version 1.2.0, September 16th, 2017
3
- part of the MiniZip project
4
-
5
- Copyright (C) 2010-2017 Nathan Moinvaziri
6
- Modifications for AES, PKWARE disk spanning
7
- https://github.com/nmoinvaz/minizip
8
- Copyright (C) 2009-2010 Mathias Svensson
9
- Modifications for Zip64 support on both zip and unzip
10
- http://result42.com
11
- Copyright (C) 2007-2008 Even Rouault
12
- Modifications of Unzip for Zip64
13
- Copyright (C) 1998-2010 Gilles Vollant
14
- http://www.winimage.com/zLibDll/minizip.html
15
-
16
- This program is distributed under the terms of the same license as zlib.
17
- See the accompanying LICENSE file for the full text of the license.
18
- */
19
-
20
- #include <stdio.h>
21
- #include <stdlib.h>
22
- #include <stdint.h>
23
- #include <string.h>
24
- #include <errno.h>
25
- #include <assert.h>
26
-
27
- #include "zlib.h"
28
- #include "unzip.h"
29
-
30
- #ifdef HAVE_AES
31
- # define AES_METHOD (99)
32
- # define AES_PWVERIFYSIZE (2)
33
- # define AES_MAXSALTLENGTH (16)
34
- # define AES_AUTHCODESIZE (10)
35
- # define AES_HEADERSIZE (11)
36
- # define AES_KEYSIZE(mode) (64 + (mode * 64))
37
-
38
- # include "aes.h"
39
- # include "fileenc.h"
40
- #endif
41
- #ifdef HAVE_APPLE_COMPRESSION
42
- # include <compression.h>
43
- #endif
44
-
45
- #ifndef NOUNCRYPT
46
- # include "crypt.h"
47
- #endif
48
-
49
- #define DISKHEADERMAGIC (0x08074b50)
50
- #define LOCALHEADERMAGIC (0x04034b50)
51
- #define CENTRALHEADERMAGIC (0x02014b50)
52
- #define ENDHEADERMAGIC (0x06054b50)
53
- #define ZIP64ENDHEADERMAGIC (0x06064b50)
54
- #define ZIP64ENDLOCHEADERMAGIC (0x07064b50)
55
-
56
- #define SIZECENTRALDIRITEM (0x2e)
57
- #define SIZECENTRALHEADERLOCATOR (0x14)
58
- #define SIZEZIPLOCALHEADER (0x1e)
59
-
60
- #ifndef BUFREADCOMMENT
61
- # define BUFREADCOMMENT (0x400)
62
- #endif
63
-
64
- #ifndef UNZ_BUFSIZE
65
- # define UNZ_BUFSIZE (UINT16_MAX)
66
- #endif
67
- #ifndef UNZ_MAXFILENAMEINZIP
68
- # define UNZ_MAXFILENAMEINZIP (256)
69
- #endif
70
-
71
- #ifndef ALLOC
72
- # define ALLOC(size) (malloc(size))
73
- #endif
74
- #ifndef TRYFREE
75
- # define TRYFREE(p) {if (p) free(p);}
76
- #endif
77
-
78
- const char unz_copyright[] = " unzip 1.2.0 Copyright 1998-2017 - https://github.com/nmoinvaz/minizip";
79
-
80
- /* unz_file_info_internal contain internal info about a file in zipfile*/
81
- typedef struct unz_file_info64_internal_s
82
- {
83
- uint64_t offset_curfile; /* relative offset of local header 8 bytes */
84
- uint64_t byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
85
- #ifdef HAVE_AES
86
- uint8_t aes_encryption_mode;
87
- uint16_t aes_compression_method;
88
- uint16_t aes_version;
89
- #endif
90
- } unz_file_info64_internal;
91
-
92
- /* file_in_zip_read_info_s contain internal information about a file in zipfile */
93
- typedef struct
94
- {
95
- uint8_t *read_buffer; /* internal buffer for compressed data */
96
- z_stream stream; /* zLib stream structure for inflate */
97
- #ifdef HAVE_BZIP2
98
- bz_stream bstream; /* bzLib stream structure for bziped */
99
- #endif
100
- #ifdef HAVE_APPLE_COMPRESSION
101
- compression_stream astream; /* libcompression stream structure */
102
- #endif
103
- #ifdef HAVE_AES
104
- fcrypt_ctx aes_ctx;
105
- #endif
106
- uint64_t pos_in_zipfile; /* position in byte on the zipfile, for fseek */
107
- uint8_t stream_initialised; /* flag set if stream structure is initialised */
108
-
109
- uint64_t offset_local_extrafield; /* offset of the local extra field */
110
- uint16_t size_local_extrafield; /* size of the local extra field */
111
- uint64_t pos_local_extrafield; /* position in the local extra field in read */
112
- uint64_t total_out_64;
113
-
114
- uint32_t crc32; /* crc32 of all data uncompressed */
115
- uint32_t crc32_expected; /* crc32 we must obtain after decompress all */
116
- uint64_t rest_read_compressed; /* number of byte to be decompressed */
117
- uint64_t rest_read_uncompressed; /* number of byte to be obtained after decomp */
118
-
119
- zlib_filefunc64_32_def z_filefunc;
120
-
121
- voidpf filestream; /* io structore of the zipfile */
122
- uint16_t compression_method; /* compression method (0==store) */
123
- uint64_t byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
124
- int raw;
125
- } file_in_zip64_read_info_s;
126
-
127
- /* unz64_s contain internal information about the zipfile */
128
- typedef struct
129
- {
130
- zlib_filefunc64_32_def z_filefunc;
131
-
132
- voidpf filestream; /* io structure of the current zipfile */
133
- voidpf filestream_with_CD; /* io structure of the disk with the central directory */
134
-
135
- unz_global_info64 gi; /* public global information */
136
-
137
- uint64_t byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
138
- uint64_t num_file; /* number of the current file in the zipfile */
139
- uint64_t pos_in_central_dir; /* pos of the current file in the central dir */
140
- uint64_t current_file_ok; /* flag about the usability of the current file */
141
- uint64_t central_pos; /* position of the beginning of the central dir */
142
- uint32_t number_disk; /* number of the current disk, used for spanning ZIP */
143
- uint64_t size_central_dir; /* size of the central directory */
144
- uint64_t offset_central_dir; /* offset of start of central directory with
145
- respect to the starting disk number */
146
-
147
- unz_file_info64 cur_file_info; /* public info about the current file in zip*/
148
- unz_file_info64_internal cur_file_info_internal;
149
- /* private info about it*/
150
- file_in_zip64_read_info_s *pfile_in_zip_read;
151
- /* structure about the current file if we are decompressing it */
152
- int is_zip64; /* is the current file zip64 */
153
- #ifndef NOUNCRYPT
154
- uint32_t keys[3]; /* keys defining the pseudo-random sequence */
155
- const z_crc_t *pcrc_32_tab;
156
- #endif
157
- } unz64_internal;
158
-
159
- /* Read a byte from a gz_stream; Return EOF for end of file. */
160
- static int unzReadUInt8(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint8_t *value)
161
- {
162
- uint8_t c = 0;
163
- if (ZREAD64(*pzlib_filefunc_def, filestream, &c, 1) == 1)
164
- {
165
- *value = (uint8_t)c;
166
- return UNZ_OK;
167
- }
168
- *value = 0;
169
- if (ZERROR64(*pzlib_filefunc_def, filestream))
170
- return UNZ_ERRNO;
171
- return UNZ_EOF;
172
- }
173
-
174
- static int unzReadUInt16(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint16_t *value)
175
- {
176
- uint16_t x;
177
- uint8_t c = 0;
178
- int err = UNZ_OK;
179
-
180
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
181
- x = (uint16_t)c;
182
- if (err == UNZ_OK)
183
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
184
- x |= ((uint16_t)c) << 8;
185
-
186
- if (err == UNZ_OK)
187
- *value = x;
188
- else
189
- *value = 0;
190
- return err;
191
- }
192
-
193
- static int unzReadUInt32(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint32_t *value)
194
- {
195
- uint32_t x = 0;
196
- uint8_t c = 0;
197
- int err = UNZ_OK;
198
-
199
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
200
- x = (uint32_t)c;
201
- if (err == UNZ_OK)
202
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
203
- x |= ((uint32_t)c) << 8;
204
- if (err == UNZ_OK)
205
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
206
- x |= ((uint32_t)c) << 16;
207
- if (err == UNZ_OK)
208
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
209
- x += ((uint32_t)c) << 24;
210
-
211
- if (err == UNZ_OK)
212
- *value = x;
213
- else
214
- *value = 0;
215
- return err;
216
- }
217
-
218
- static int unzReadUInt64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint64_t *value)
219
- {
220
- uint64_t x = 0;
221
- uint8_t i = 0;
222
- int err = UNZ_OK;
223
-
224
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
225
- x = (uint64_t)i;
226
- if (err == UNZ_OK)
227
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
228
- x |= ((uint64_t)i) << 8;
229
- if (err == UNZ_OK)
230
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
231
- x |= ((uint64_t)i) << 16;
232
- if (err == UNZ_OK)
233
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
234
- x |= ((uint64_t)i) << 24;
235
- if (err == UNZ_OK)
236
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
237
- x |= ((uint64_t)i) << 32;
238
- if (err == UNZ_OK)
239
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
240
- x |= ((uint64_t)i) << 40;
241
- if (err == UNZ_OK)
242
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
243
- x |= ((uint64_t)i) << 48;
244
- if (err == UNZ_OK)
245
- err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
246
- x |= ((uint64_t)i) << 56;
247
-
248
- if (err == UNZ_OK)
249
- *value = x;
250
- else
251
- *value = 0;
252
- return err;
253
- }
254
-
255
- /* Locate the Central directory of a zip file (at the end, just before the global comment) */
256
- static int unzSearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, uint64_t *pos_found, voidpf filestream)
257
- {
258
- uint8_t buf[BUFREADCOMMENT + 4];
259
- uint64_t file_size = 0;
260
- uint64_t back_read = 4;
261
- uint64_t max_back = UINT16_MAX; /* maximum size of global comment */
262
- uint32_t read_size = 0;
263
- uint64_t read_pos = 0;
264
- uint32_t i = 0;
265
- *pos_found = 0;
266
-
267
- if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0)
268
- return UNZ_ERRNO;
269
-
270
- file_size = ZTELL64(*pzlib_filefunc_def, filestream);
271
-
272
- if (max_back > file_size)
273
- max_back = file_size;
274
-
275
- while (back_read < max_back)
276
- {
277
- if (back_read + BUFREADCOMMENT > max_back)
278
- back_read = max_back;
279
- else
280
- back_read += BUFREADCOMMENT;
281
-
282
- read_pos = file_size - back_read;
283
- read_size = ((BUFREADCOMMENT + 4) < (file_size - read_pos)) ?
284
- (BUFREADCOMMENT + 4) : (uint32_t)(file_size - read_pos);
285
-
286
- if (ZSEEK64(*pzlib_filefunc_def, filestream, read_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
287
- break;
288
- if (ZREAD64(*pzlib_filefunc_def, filestream, buf, read_size) != read_size)
289
- break;
290
-
291
- for (i = read_size - 3; (i--) > 0;)
292
- if (((*(buf+i)) == (ENDHEADERMAGIC & 0xff)) &&
293
- ((*(buf+i+1)) == (ENDHEADERMAGIC >> 8 & 0xff)) &&
294
- ((*(buf+i+2)) == (ENDHEADERMAGIC >> 16 & 0xff)) &&
295
- ((*(buf+i+3)) == (ENDHEADERMAGIC >> 24 & 0xff)))
296
- {
297
- *pos_found = read_pos+i;
298
- return UNZ_OK;
299
- }
300
- }
301
-
302
- return UNZ_ERRNO;
303
- }
304
-
305
- /* Locate the Central directory 64 of a zipfile (at the end, just before the global comment) */
306
- static int unzSearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, uint64_t *offset, voidpf filestream,
307
- const uint64_t endcentraloffset)
308
- {
309
- uint32_t value32 = 0;
310
- *offset = 0;
311
-
312
- /* Zip64 end of central directory locator */
313
- if (ZSEEK64(*pzlib_filefunc_def, filestream, endcentraloffset - SIZECENTRALHEADERLOCATOR, ZLIB_FILEFUNC_SEEK_SET) != 0)
314
- return UNZ_ERRNO;
315
-
316
- /* Read locator signature */
317
- if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
318
- return UNZ_ERRNO;
319
- if (value32 != ZIP64ENDLOCHEADERMAGIC)
320
- return UNZ_ERRNO;
321
- /* Number of the disk with the start of the zip64 end of central directory */
322
- if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
323
- return UNZ_ERRNO;
324
- /* Relative offset of the zip64 end of central directory record */
325
- if (unzReadUInt64(pzlib_filefunc_def, filestream, offset) != UNZ_OK)
326
- return UNZ_ERRNO;
327
- /* Total number of disks */
328
- if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
329
- return UNZ_ERRNO;
330
- /* Goto end of central directory record */
331
- if (ZSEEK64(*pzlib_filefunc_def, filestream, *offset, ZLIB_FILEFUNC_SEEK_SET) != 0)
332
- return UNZ_ERRNO;
333
- /* The signature */
334
- if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
335
- return UNZ_ERRNO;
336
- if (value32 != ZIP64ENDHEADERMAGIC)
337
- return UNZ_ERRNO;
338
-
339
- return UNZ_OK;
340
- }
341
-
342
- static unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_filefunc64_32_def)
343
- {
344
- unz64_internal us = { 0 };
345
- unz64_internal *s = NULL;
346
- uint64_t central_pos = 0;
347
- uint64_t central_pos64 = 0;
348
- uint64_t number_entry_CD = 0;
349
- uint16_t value16 = 0;
350
- uint32_t value32 = 0;
351
- uint64_t value64 = 0;
352
- voidpf filestream = NULL;
353
- int err = UNZ_OK;
354
- int err64 = UNZ_OK;
355
-
356
- if (pzlib_filefunc64_32_def == NULL)
357
- fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
358
- else
359
- {
360
- assert((pzlib_filefunc64_32_def->zopen32_file || pzlib_filefunc64_32_def->zfile_func64.zopen64_file) &&
361
- (pzlib_filefunc64_32_def->zopendisk32_file || pzlib_filefunc64_32_def->zfile_func64.zopendisk64_file) &&
362
- (pzlib_filefunc64_32_def->ztell32_file || pzlib_filefunc64_32_def->zfile_func64.ztell64_file) &&
363
- (pzlib_filefunc64_32_def->zseek32_file || pzlib_filefunc64_32_def->zfile_func64.zseek64_file));
364
- us.z_filefunc = *pzlib_filefunc64_32_def;
365
- }
366
-
367
- us.filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
368
-
369
- if (us.filestream == NULL)
370
- return NULL;
371
-
372
- us.filestream_with_CD = us.filestream;
373
-
374
- /* Search for end of central directory header */
375
- err = unzSearchCentralDir(&us.z_filefunc, &central_pos, us.filestream);
376
- if (err == UNZ_OK)
377
- {
378
- if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
379
- err = UNZ_ERRNO;
380
-
381
- /* The signature, already checked */
382
- if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
383
- err = UNZ_ERRNO;
384
- /* Number of this disk */
385
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
386
- err = UNZ_ERRNO;
387
- us.number_disk = value16;
388
- /* Number of the disk with the start of the central directory */
389
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
390
- err = UNZ_ERRNO;
391
- us.gi.number_disk_with_CD = value16;
392
- /* Total number of entries in the central directory on this disk */
393
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
394
- err = UNZ_ERRNO;
395
- us.gi.number_entry = value16;
396
- /* Total number of entries in the central directory */
397
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
398
- err = UNZ_ERRNO;
399
- number_entry_CD = value16;
400
- if (number_entry_CD != us.gi.number_entry)
401
- err = UNZ_BADZIPFILE;
402
- /* Size of the central directory */
403
- if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
404
- err = UNZ_ERRNO;
405
- us.size_central_dir = value32;
406
- /* Offset of start of central directory with respect to the starting disk number */
407
- if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
408
- err = UNZ_ERRNO;
409
- us.offset_central_dir = value32;
410
- /* Zipfile comment length */
411
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &us.gi.size_comment) != UNZ_OK)
412
- err = UNZ_ERRNO;
413
-
414
- if (err == UNZ_OK)
415
- {
416
- /* Search for Zip64 end of central directory header */
417
- err64 = unzSearchCentralDir64(&us.z_filefunc, &central_pos64, us.filestream, central_pos);
418
- if (err64 == UNZ_OK)
419
- {
420
- central_pos = central_pos64;
421
- us.is_zip64 = 1;
422
-
423
- if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
424
- err = UNZ_ERRNO;
425
-
426
- /* the signature, already checked */
427
- if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
428
- err = UNZ_ERRNO;
429
- /* size of zip64 end of central directory record */
430
- if (unzReadUInt64(&us.z_filefunc, us.filestream, &value64) != UNZ_OK)
431
- err = UNZ_ERRNO;
432
- /* version made by */
433
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
434
- err = UNZ_ERRNO;
435
- /* version needed to extract */
436
- if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
437
- err = UNZ_ERRNO;
438
- /* number of this disk */
439
- if (unzReadUInt32(&us.z_filefunc, us.filestream, &us.number_disk) != UNZ_OK)
440
- err = UNZ_ERRNO;
441
- /* number of the disk with the start of the central directory */
442
- if (unzReadUInt32(&us.z_filefunc, us.filestream, &us.gi.number_disk_with_CD) != UNZ_OK)
443
- err = UNZ_ERRNO;
444
- /* total number of entries in the central directory on this disk */
445
- if (unzReadUInt64(&us.z_filefunc, us.filestream, &us.gi.number_entry) != UNZ_OK)
446
- err = UNZ_ERRNO;
447
- /* total number of entries in the central directory */
448
- if (unzReadUInt64(&us.z_filefunc, us.filestream, &number_entry_CD) != UNZ_OK)
449
- err = UNZ_ERRNO;
450
- if (number_entry_CD != us.gi.number_entry)
451
- err = UNZ_BADZIPFILE;
452
- /* size of the central directory */
453
- if (unzReadUInt64(&us.z_filefunc, us.filestream, &us.size_central_dir) != UNZ_OK)
454
- err = UNZ_ERRNO;
455
- /* offset of start of central directory with respect to the starting disk number */
456
- if (unzReadUInt64(&us.z_filefunc, us.filestream, &us.offset_central_dir) != UNZ_OK)
457
- err = UNZ_ERRNO;
458
- }
459
- else if ((us.size_central_dir == UINT16_MAX) || (us.offset_central_dir == UINT32_MAX))
460
- err = UNZ_BADZIPFILE;
461
- }
462
- }
463
- else
464
- err = UNZ_ERRNO;
465
-
466
- if ((err == UNZ_OK) && (central_pos < us.offset_central_dir + us.size_central_dir))
467
- err = UNZ_BADZIPFILE;
468
-
469
- if (err != UNZ_OK)
470
- {
471
- ZCLOSE64(us.z_filefunc, us.filestream);
472
- return NULL;
473
- }
474
-
475
- if (us.gi.number_disk_with_CD == 0)
476
- {
477
- /* If there is only one disk open another stream so we don't have to seek between the CD
478
- and the file headers constantly */
479
- filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
480
- if (filestream != NULL)
481
- us.filestream = filestream;
482
- }
483
-
484
- /* Hack for zip files that have no respect for zip64
485
- if ((central_pos > 0xffffffff) && (us.offset_central_dir < 0xffffffff))
486
- us.offset_central_dir = central_pos - us.size_central_dir;*/
487
-
488
- us.byte_before_the_zipfile = central_pos - (us.offset_central_dir + us.size_central_dir);
489
- us.central_pos = central_pos;
490
-
491
- s = (unz64_internal*)ALLOC(sizeof(unz64_internal));
492
- if (s != NULL)
493
- {
494
- *s = us;
495
- if (err64 != UNZ_OK)
496
- // workaround incorrect count #184
497
- s->gi.number_entry = unzCountEntries(s);
498
-
499
- unzGoToFirstFile((unzFile)s);
500
- }
501
- return (unzFile)s;
502
- }
503
-
504
- extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc32_def)
505
- {
506
- if (pzlib_filefunc32_def != NULL)
507
- {
508
- zlib_filefunc64_32_def zlib_filefunc64_32_def_fill = { 0 };
509
- fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill, pzlib_filefunc32_def);
510
- return unzOpenInternal(path, &zlib_filefunc64_32_def_fill);
511
- }
512
- return unzOpenInternal(path, NULL);
513
- }
514
-
515
- extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
516
- {
517
- if (pzlib_filefunc_def != NULL)
518
- {
519
- zlib_filefunc64_32_def zlib_filefunc64_32_def_fill = { 0 };
520
- zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
521
- return unzOpenInternal(path, &zlib_filefunc64_32_def_fill);
522
- }
523
- return unzOpenInternal(path, NULL);
524
- }
525
-
526
- extern unzFile ZEXPORT unzOpen(const char *path)
527
- {
528
- return unzOpenInternal(path, NULL);
529
- }
530
-
531
- extern unzFile ZEXPORT unzOpen64(const void *path)
532
- {
533
- return unzOpenInternal(path, NULL);
534
- }
535
-
536
- extern int ZEXPORT unzClose(unzFile file)
537
- {
538
- unz64_internal *s;
539
- if (file == NULL)
540
- return UNZ_PARAMERROR;
541
- s = (unz64_internal*)file;
542
-
543
- if (s->pfile_in_zip_read != NULL)
544
- unzCloseCurrentFile(file);
545
-
546
- if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD))
547
- ZCLOSE64(s->z_filefunc, s->filestream);
548
- if (s->filestream_with_CD != NULL)
549
- ZCLOSE64(s->z_filefunc, s->filestream_with_CD);
550
-
551
- s->filestream = NULL;
552
- s->filestream_with_CD = NULL;
553
- TRYFREE(s);
554
- return UNZ_OK;
555
- }
556
-
557
- /* Goto to the next available disk for spanned archives */
558
- static int unzGoToNextDisk(unzFile file)
559
- {
560
- unz64_internal *s;
561
- uint32_t number_disk_next = 0;
562
-
563
- s = (unz64_internal*)file;
564
- if (s == NULL)
565
- return UNZ_PARAMERROR;
566
- number_disk_next = s->number_disk;
567
-
568
- if ((s->pfile_in_zip_read != NULL) && (s->pfile_in_zip_read->rest_read_uncompressed > 0))
569
- /* We are currently reading a file and we need the next sequential disk */
570
- number_disk_next += 1;
571
- else
572
- /* Goto the disk for the current file */
573
- number_disk_next = s->cur_file_info.disk_num_start;
574
-
575
- if (number_disk_next != s->number_disk)
576
- {
577
- /* Switch disks */
578
- if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD))
579
- ZCLOSE64(s->z_filefunc, s->filestream);
580
-
581
- if (number_disk_next == s->gi.number_disk_with_CD)
582
- {
583
- s->filestream = s->filestream_with_CD;
584
- }
585
- else
586
- {
587
- s->filestream = ZOPENDISK64(s->z_filefunc, s->filestream_with_CD, number_disk_next,
588
- ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
589
- }
590
-
591
- if (s->filestream == NULL)
592
- return UNZ_ERRNO;
593
-
594
- s->number_disk = number_disk_next;
595
- }
596
-
597
- return UNZ_OK;
598
- }
599
-
600
- extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
601
- {
602
- unz64_internal *s = NULL;
603
- if (file == NULL)
604
- return UNZ_PARAMERROR;
605
- s = (unz64_internal*)file;
606
-
607
- pglobal_info32->number_entry = (uint32_t)s->gi.number_entry;
608
- pglobal_info32->size_comment = s->gi.size_comment;
609
- pglobal_info32->number_disk_with_CD = s->gi.number_disk_with_CD;
610
- return UNZ_OK;
611
- }
612
-
613
- extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
614
- {
615
- unz64_internal *s = NULL;
616
- if (file == NULL)
617
- return UNZ_PARAMERROR;
618
- s = (unz64_internal*)file;
619
- *pglobal_info = s->gi;
620
- return UNZ_OK;
621
- }
622
-
623
- extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
624
- {
625
- unz64_internal *s = NULL;
626
- uint16_t bytes_to_read = comment_size;
627
- if (file == NULL)
628
- return (int)UNZ_PARAMERROR;
629
- s = (unz64_internal*)file;
630
-
631
- if (bytes_to_read > s->gi.size_comment)
632
- bytes_to_read = s->gi.size_comment;
633
-
634
- if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, s->central_pos + 22, ZLIB_FILEFUNC_SEEK_SET) != 0)
635
- return UNZ_ERRNO;
636
-
637
- if (bytes_to_read > 0)
638
- {
639
- *comment = 0;
640
- if (ZREAD64(s->z_filefunc, s->filestream_with_CD, comment, bytes_to_read) != bytes_to_read)
641
- return UNZ_ERRNO;
642
- }
643
-
644
- if ((comment != NULL) && (comment_size > s->gi.size_comment))
645
- *(comment + s->gi.size_comment) = 0;
646
-
647
- return (int)bytes_to_read;
648
- }
649
-
650
- static int unzGetCurrentFileInfoField(unzFile file, uint32_t *seek, void *field, uint16_t field_size, uint16_t size_file_field, int null_terminated_field)
651
- {
652
- unz64_internal *s = NULL;
653
- uint32_t bytes_to_read = 0;
654
- int err = UNZ_OK;
655
-
656
- if (file == NULL)
657
- return (int)UNZ_PARAMERROR;
658
- s = (unz64_internal*)file;
659
-
660
- /* Read field */
661
- if (field != NULL)
662
- {
663
- if (size_file_field < field_size)
664
- {
665
- if (null_terminated_field)
666
- *((char *)field+size_file_field) = 0;
667
-
668
- bytes_to_read = size_file_field;
669
- }
670
- else
671
- bytes_to_read = field_size;
672
-
673
- if (*seek != 0)
674
- {
675
- if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, *seek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
676
- *seek = 0;
677
- else
678
- err = UNZ_ERRNO;
679
- }
680
-
681
- if ((size_file_field > 0) && (field_size > 0))
682
- {
683
- if (ZREAD64(s->z_filefunc, s->filestream_with_CD, field, bytes_to_read) != bytes_to_read)
684
- err = UNZ_ERRNO;
685
- }
686
- *seek += size_file_field - bytes_to_read;
687
- }
688
- else
689
- {
690
- *seek += size_file_field;
691
- }
692
-
693
- return err;
694
- }
695
-
696
- /* Get info about the current file in the zipfile, with internal only info */
697
- static int unzGetCurrentFileInfoInternal(unzFile file, unz_file_info64 *pfile_info,
698
- unz_file_info64_internal *pfile_info_internal, char *filename, uint16_t filename_size, void *extrafield,
699
- uint16_t extrafield_size, char *comment, uint16_t comment_size)
700
- {
701
- unz64_internal *s = NULL;
702
- unz_file_info64 file_info;
703
- unz_file_info64_internal file_info_internal;
704
- uint32_t magic = 0;
705
- uint64_t current_pos = 0;
706
- uint32_t seek = 0;
707
- uint32_t extra_pos = 0;
708
- uint16_t extra_header_id = 0;
709
- uint16_t extra_data_size = 0;
710
- uint16_t value16 = 0;
711
- uint32_t value32 = 0;
712
- uint64_t value64 = 0;
713
- int err = UNZ_OK;
714
-
715
- if (file == NULL)
716
- return UNZ_PARAMERROR;
717
- s = (unz64_internal*)file;
718
-
719
- if (ZSEEK64(s->z_filefunc, s->filestream_with_CD,
720
- s->pos_in_central_dir + s->byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
721
- err = UNZ_ERRNO;
722
-
723
- /* Check the magic */
724
- if (err == UNZ_OK)
725
- {
726
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &magic) != UNZ_OK)
727
- err = UNZ_ERRNO;
728
- else if (magic != CENTRALHEADERMAGIC)
729
- err = UNZ_BADZIPFILE;
730
- }
731
-
732
- /* Read central directory header */
733
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.version) != UNZ_OK)
734
- err = UNZ_ERRNO;
735
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.version_needed) != UNZ_OK)
736
- err = UNZ_ERRNO;
737
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.flag) != UNZ_OK)
738
- err = UNZ_ERRNO;
739
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.compression_method) != UNZ_OK)
740
- err = UNZ_ERRNO;
741
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.dos_date) != UNZ_OK)
742
- err = UNZ_ERRNO;
743
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.crc) != UNZ_OK)
744
- err = UNZ_ERRNO;
745
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &value32) != UNZ_OK)
746
- err = UNZ_ERRNO;
747
- file_info.compressed_size = value32;
748
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &value32) != UNZ_OK)
749
- err = UNZ_ERRNO;
750
- file_info.uncompressed_size = value32;
751
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.size_filename) != UNZ_OK)
752
- err = UNZ_ERRNO;
753
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.size_file_extra) != UNZ_OK)
754
- err = UNZ_ERRNO;
755
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.size_file_comment) != UNZ_OK)
756
- err = UNZ_ERRNO;
757
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &value16) != UNZ_OK)
758
- err = UNZ_ERRNO;
759
- file_info.disk_num_start = value16;
760
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.internal_fa) != UNZ_OK)
761
- err = UNZ_ERRNO;
762
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.external_fa) != UNZ_OK)
763
- err = UNZ_ERRNO;
764
- /* Relative offset of local header */
765
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &value32) != UNZ_OK)
766
- err = UNZ_ERRNO;
767
-
768
- file_info.size_file_extra_internal = 0;
769
- file_info.disk_offset = value32;
770
- file_info_internal.offset_curfile = value32;
771
- #ifdef HAVE_AES
772
- file_info_internal.aes_compression_method = 0;
773
- file_info_internal.aes_encryption_mode = 0;
774
- file_info_internal.aes_version = 0;
775
- #endif
776
-
777
- if (err == UNZ_OK)
778
- err = unzGetCurrentFileInfoField(file, &seek, filename, filename_size, file_info.size_filename, 1);
779
-
780
- /* Read extrafield */
781
- if (err == UNZ_OK)
782
- err = unzGetCurrentFileInfoField(file, &seek, extrafield, extrafield_size, file_info.size_file_extra, 0);
783
-
784
- if ((err == UNZ_OK) && (file_info.size_file_extra != 0))
785
- {
786
- if (seek != 0)
787
- {
788
- if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, seek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
789
- seek = 0;
790
- else
791
- err = UNZ_ERRNO;
792
- }
793
-
794
- /* We are going to parse the extra field so we need to move back */
795
- current_pos = ZTELL64(s->z_filefunc, s->filestream_with_CD);
796
- if (current_pos < file_info.size_file_extra)
797
- err = UNZ_ERRNO;
798
- current_pos -= file_info.size_file_extra;
799
- if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, current_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
800
- err = UNZ_ERRNO;
801
-
802
- while ((err != UNZ_ERRNO) && (extra_pos < file_info.size_file_extra))
803
- {
804
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &extra_header_id) != UNZ_OK)
805
- err = UNZ_ERRNO;
806
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &extra_data_size) != UNZ_OK)
807
- err = UNZ_ERRNO;
808
-
809
- /* ZIP64 extra fields */
810
- if (extra_header_id == 0x0001)
811
- {
812
- /* Subtract size of ZIP64 field, since ZIP64 is handled internally */
813
- file_info.size_file_extra_internal += 2 + 2 + extra_data_size;
814
-
815
- if (file_info.uncompressed_size == UINT32_MAX)
816
- {
817
- if (unzReadUInt64(&s->z_filefunc, s->filestream_with_CD, &file_info.uncompressed_size) != UNZ_OK)
818
- err = UNZ_ERRNO;
819
- }
820
- if (file_info.compressed_size == UINT32_MAX)
821
- {
822
- if (unzReadUInt64(&s->z_filefunc, s->filestream_with_CD, &file_info.compressed_size) != UNZ_OK)
823
- err = UNZ_ERRNO;
824
- }
825
- if (file_info_internal.offset_curfile == UINT32_MAX)
826
- {
827
- /* Relative Header offset */
828
- if (unzReadUInt64(&s->z_filefunc, s->filestream_with_CD, &value64) != UNZ_OK)
829
- err = UNZ_ERRNO;
830
- file_info_internal.offset_curfile = value64;
831
- file_info.disk_offset = value64;
832
- }
833
- if (file_info.disk_num_start == UINT32_MAX)
834
- {
835
- /* Disk Start Number */
836
- if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.disk_num_start) != UNZ_OK)
837
- err = UNZ_ERRNO;
838
- }
839
- }
840
- #ifdef HAVE_AES
841
- /* AES header */
842
- else if (extra_header_id == 0x9901)
843
- {
844
- uint8_t value8 = 0;
845
-
846
- /* Subtract size of AES field, since AES is handled internally */
847
- file_info.size_file_extra_internal += 2 + 2 + extra_data_size;
848
-
849
- /* Verify version info */
850
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &value16) != UNZ_OK)
851
- err = UNZ_ERRNO;
852
- /* Support AE-1 and AE-2 */
853
- if (value16 != 1 && value16 != 2)
854
- err = UNZ_ERRNO;
855
- file_info_internal.aes_version = value16;
856
- if (unzReadUInt8(&s->z_filefunc, s->filestream_with_CD, &value8) != UNZ_OK)
857
- err = UNZ_ERRNO;
858
- if ((char)value8 != 'A')
859
- err = UNZ_ERRNO;
860
- if (unzReadUInt8(&s->z_filefunc, s->filestream_with_CD, &value8) != UNZ_OK)
861
- err = UNZ_ERRNO;
862
- if ((char)value8 != 'E')
863
- err = UNZ_ERRNO;
864
- /* Get AES encryption strength and actual compression method */
865
- if (unzReadUInt8(&s->z_filefunc, s->filestream_with_CD, &value8) != UNZ_OK)
866
- err = UNZ_ERRNO;
867
- file_info_internal.aes_encryption_mode = value8;
868
- if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &value16) != UNZ_OK)
869
- err = UNZ_ERRNO;
870
- file_info_internal.aes_compression_method = value16;
871
- }
872
- #endif
873
- else
874
- {
875
- if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, extra_data_size, ZLIB_FILEFUNC_SEEK_CUR) != 0)
876
- err = UNZ_ERRNO;
877
- }
878
-
879
- extra_pos += 2 + 2 + extra_data_size;
880
- }
881
- }
882
-
883
- if (file_info.disk_num_start == s->gi.number_disk_with_CD)
884
- file_info_internal.byte_before_the_zipfile = s->byte_before_the_zipfile;
885
- else
886
- file_info_internal.byte_before_the_zipfile = 0;
887
-
888
- if (err == UNZ_OK)
889
- err = unzGetCurrentFileInfoField(file, &seek, comment, comment_size, file_info.size_file_comment, 1);
890
-
891
- if ((err == UNZ_OK) && (pfile_info != NULL))
892
- *pfile_info = file_info;
893
- if ((err == UNZ_OK) && (pfile_info_internal != NULL))
894
- *pfile_info_internal = file_info_internal;
895
-
896
- return err;
897
- }
898
-
899
- extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
900
- uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
901
- {
902
- unz_file_info64 file_info64;
903
- int err = UNZ_OK;
904
-
905
- err = unzGetCurrentFileInfoInternal(file, &file_info64, NULL, filename, filename_size,
906
- extrafield, extrafield_size, comment, comment_size);
907
-
908
- if ((err == UNZ_OK) && (pfile_info != NULL))
909
- {
910
- pfile_info->version = file_info64.version;
911
- pfile_info->version_needed = file_info64.version_needed;
912
- pfile_info->flag = file_info64.flag;
913
- pfile_info->compression_method = file_info64.compression_method;
914
- pfile_info->dos_date = file_info64.dos_date;
915
- pfile_info->crc = file_info64.crc;
916
-
917
- pfile_info->size_filename = file_info64.size_filename;
918
- pfile_info->size_file_extra = file_info64.size_file_extra - file_info64.size_file_extra_internal;
919
- pfile_info->size_file_comment = file_info64.size_file_comment;
920
-
921
- pfile_info->disk_num_start = (uint16_t)file_info64.disk_num_start;
922
- pfile_info->internal_fa = file_info64.internal_fa;
923
- pfile_info->external_fa = file_info64.external_fa;
924
-
925
- pfile_info->compressed_size = (uint32_t)file_info64.compressed_size;
926
- pfile_info->uncompressed_size = (uint32_t)file_info64.uncompressed_size;
927
- }
928
- return err;
929
- }
930
-
931
- extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
932
- uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
933
- {
934
- return unzGetCurrentFileInfoInternal(file, pfile_info, NULL, filename, filename_size,
935
- extrafield, extrafield_size, comment, comment_size);
936
- }
937
-
938
- /* Read the local header of the current zipfile. Check the coherency of the local header and info in the
939
- end of central directory about this file store in *piSizeVar the size of extra info in local header
940
- (filename and size of extra field data) */
941
- static int unzCheckCurrentFileCoherencyHeader(unz64_internal *s, uint32_t *psize_variable, uint64_t *poffset_local_extrafield,
942
- uint16_t *psize_local_extrafield)
943
- {
944
- uint32_t magic = 0;
945
- uint16_t value16 = 0;
946
- uint32_t value32 = 0;
947
- uint32_t flags = 0;
948
- uint16_t size_filename = 0;
949
- uint16_t size_extra_field = 0;
950
- uint16_t compression_method = 0;
951
- int err = UNZ_OK;
952
-
953
- if (psize_variable == NULL)
954
- return UNZ_PARAMERROR;
955
- *psize_variable = 0;
956
- if (poffset_local_extrafield == NULL)
957
- return UNZ_PARAMERROR;
958
- *poffset_local_extrafield = 0;
959
- if (psize_local_extrafield == NULL)
960
- return UNZ_PARAMERROR;
961
- *psize_local_extrafield = 0;
962
-
963
- err = unzGoToNextDisk((unzFile)s);
964
- if (err != UNZ_OK)
965
- return err;
966
-
967
- if (ZSEEK64(s->z_filefunc, s->filestream, s->cur_file_info_internal.offset_curfile +
968
- s->cur_file_info_internal.byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
969
- return UNZ_ERRNO;
970
-
971
- if (err == UNZ_OK)
972
- {
973
- if (unzReadUInt32(&s->z_filefunc, s->filestream, &magic) != UNZ_OK)
974
- err = UNZ_ERRNO;
975
- else if (magic != LOCALHEADERMAGIC)
976
- err = UNZ_BADZIPFILE;
977
- }
978
-
979
- if (unzReadUInt16(&s->z_filefunc, s->filestream, &value16) != UNZ_OK)
980
- err = UNZ_ERRNO;
981
- if (unzReadUInt16(&s->z_filefunc, s->filestream, &value16) != UNZ_OK)
982
- err = UNZ_ERRNO;
983
- flags = value16;
984
- if (unzReadUInt16(&s->z_filefunc, s->filestream, &value16) != UNZ_OK)
985
- err = UNZ_ERRNO;
986
- else if ((err == UNZ_OK) && (value16 != s->cur_file_info.compression_method))
987
- err = UNZ_BADZIPFILE;
988
-
989
- compression_method = s->cur_file_info.compression_method;
990
- #ifdef HAVE_AES
991
- if (compression_method == AES_METHOD)
992
- compression_method = s->cur_file_info_internal.aes_compression_method;
993
- #endif
994
-
995
- if ((err == UNZ_OK) && (compression_method != 0) && (compression_method != Z_DEFLATED))
996
- {
997
- #ifdef HAVE_BZIP2
998
- if (compression_method != Z_BZIP2ED)
999
- #endif
1000
- err = UNZ_BADZIPFILE;
1001
- }
1002
-
1003
- if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* date/time */
1004
- err = UNZ_ERRNO;
1005
- if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* crc */
1006
- err = UNZ_ERRNO;
1007
- else if ((err == UNZ_OK) && (value32 != s->cur_file_info.crc) && ((flags & 8) == 0))
1008
- err = UNZ_BADZIPFILE;
1009
- if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* size compr */
1010
- err = UNZ_ERRNO;
1011
- else if ((value32 != UINT32_MAX) && (err == UNZ_OK) && (value32 != s->cur_file_info.compressed_size) && ((flags & 8) == 0))
1012
- err = UNZ_BADZIPFILE;
1013
- if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* size uncompr */
1014
- err = UNZ_ERRNO;
1015
- else if ((value32 != UINT32_MAX) && (err == UNZ_OK) && (value32 != s->cur_file_info.uncompressed_size) && ((flags & 8) == 0))
1016
- err = UNZ_BADZIPFILE;
1017
- if (unzReadUInt16(&s->z_filefunc, s->filestream, &size_filename) != UNZ_OK)
1018
- err = UNZ_ERRNO;
1019
-
1020
- *psize_variable += size_filename;
1021
-
1022
- if (unzReadUInt16(&s->z_filefunc, s->filestream, &size_extra_field) != UNZ_OK)
1023
- err = UNZ_ERRNO;
1024
-
1025
- *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + size_filename;
1026
- *psize_local_extrafield = size_extra_field;
1027
- *psize_variable += size_extra_field;
1028
-
1029
- return err;
1030
- }
1031
-
1032
- extern uint64_t ZEXPORT unzCountEntries(const unzFile file)
1033
- {
1034
- if (file == NULL)
1035
- return 0;
1036
-
1037
- unz64_internal s = *(unz64_internal*)file;
1038
-
1039
- s.pos_in_central_dir = s.offset_central_dir;
1040
- s.num_file = 0;
1041
- while (UNZ_OK == unzGetCurrentFileInfoInternal(&s,
1042
- &s.cur_file_info,
1043
- &s.cur_file_info_internal,
1044
- NULL, 0, NULL, 0, NULL, 0))
1045
- {
1046
- s.pos_in_central_dir += SIZECENTRALDIRITEM
1047
- + s.cur_file_info.size_filename
1048
- + s.cur_file_info.size_file_extra
1049
- + s.cur_file_info.size_file_comment;
1050
- s.num_file += 1;
1051
- }
1052
- return s.num_file;
1053
- }
1054
-
1055
- /*
1056
- Open for reading data the current file in the zipfile.
1057
- If there is no error and the file is opened, the return value is UNZ_OK.
1058
- */
1059
- extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
1060
- {
1061
- unz64_internal *s = NULL;
1062
- file_in_zip64_read_info_s *pfile_in_zip_read_info = NULL;
1063
- uint16_t compression_method = 0;
1064
- uint64_t offset_local_extrafield = 0;
1065
- uint16_t size_local_extrafield = 0;
1066
- uint32_t size_variable = 0;
1067
- int err = UNZ_OK;
1068
- #ifndef NOUNCRYPT
1069
- char source[12];
1070
- #else
1071
- if (password != NULL)
1072
- return UNZ_PARAMERROR;
1073
- #endif
1074
- if (file == NULL)
1075
- return UNZ_PARAMERROR;
1076
- s = (unz64_internal*)file;
1077
- if (!s->current_file_ok)
1078
- return UNZ_PARAMERROR;
1079
-
1080
- if (s->pfile_in_zip_read != NULL)
1081
- unzCloseCurrentFile(file);
1082
-
1083
- if (unzCheckCurrentFileCoherencyHeader(s, &size_variable, &offset_local_extrafield, &size_local_extrafield) != UNZ_OK)
1084
- return UNZ_BADZIPFILE;
1085
-
1086
- compression_method = s->cur_file_info.compression_method;
1087
- #ifdef HAVE_AES
1088
- if (compression_method == AES_METHOD)
1089
- {
1090
- compression_method = s->cur_file_info_internal.aes_compression_method;
1091
- if (password == NULL)
1092
- {
1093
- return UNZ_PARAMERROR;
1094
- }
1095
- }
1096
- #endif
1097
-
1098
- if (method != NULL)
1099
- *method = compression_method;
1100
-
1101
- if (level != NULL)
1102
- {
1103
- *level = 6;
1104
- switch (s->cur_file_info.flag & 0x06)
1105
- {
1106
- case 6 : *level = 1; break;
1107
- case 4 : *level = 2; break;
1108
- case 2 : *level = 9; break;
1109
- }
1110
- }
1111
-
1112
- if ((compression_method != 0) && (compression_method != Z_DEFLATED))
1113
- {
1114
- #ifdef HAVE_BZIP2
1115
- if (compression_method != Z_BZIP2ED)
1116
- #endif
1117
- {
1118
- return UNZ_BADZIPFILE;
1119
- }
1120
- }
1121
-
1122
- pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
1123
- if (pfile_in_zip_read_info == NULL)
1124
- return UNZ_INTERNALERROR;
1125
-
1126
- pfile_in_zip_read_info->read_buffer = (uint8_t*)ALLOC(UNZ_BUFSIZE);
1127
- if (pfile_in_zip_read_info->read_buffer == NULL)
1128
- {
1129
- TRYFREE(pfile_in_zip_read_info);
1130
- return UNZ_INTERNALERROR;
1131
- }
1132
-
1133
- pfile_in_zip_read_info->stream_initialised = 0;
1134
-
1135
- pfile_in_zip_read_info->filestream = s->filestream;
1136
- pfile_in_zip_read_info->z_filefunc = s->z_filefunc;
1137
-
1138
- pfile_in_zip_read_info->raw = raw;
1139
- pfile_in_zip_read_info->crc32 = 0;
1140
- pfile_in_zip_read_info->crc32_expected = s->cur_file_info.crc;
1141
- pfile_in_zip_read_info->total_out_64 = 0;
1142
- pfile_in_zip_read_info->compression_method = compression_method;
1143
-
1144
- pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
1145
- pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
1146
- pfile_in_zip_read_info->pos_local_extrafield = 0;
1147
-
1148
- pfile_in_zip_read_info->rest_read_compressed = s->cur_file_info.compressed_size;
1149
- pfile_in_zip_read_info->rest_read_uncompressed = s->cur_file_info.uncompressed_size;
1150
- pfile_in_zip_read_info->byte_before_the_zipfile = 0;
1151
-
1152
- if (s->number_disk == s->gi.number_disk_with_CD)
1153
- pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile;
1154
-
1155
- pfile_in_zip_read_info->pos_in_zipfile = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + size_variable;
1156
-
1157
- pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1158
- pfile_in_zip_read_info->stream.zfree = (free_func)0;
1159
- pfile_in_zip_read_info->stream.opaque = (voidpf)s;
1160
- pfile_in_zip_read_info->stream.total_out = 0;
1161
- pfile_in_zip_read_info->stream.total_in = 0;
1162
- pfile_in_zip_read_info->stream.next_in = NULL;
1163
- pfile_in_zip_read_info->stream.avail_in = 0;
1164
-
1165
- if (!raw)
1166
- {
1167
- if (compression_method == Z_BZIP2ED)
1168
- {
1169
- #ifdef HAVE_BZIP2
1170
- pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
1171
- pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
1172
- pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
1173
- pfile_in_zip_read_info->bstream.state = (voidpf)0;
1174
-
1175
- err = BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
1176
- if (err == Z_OK)
1177
- {
1178
- pfile_in_zip_read_info->stream_initialised = Z_BZIP2ED;
1179
- }
1180
- else
1181
- {
1182
- TRYFREE(pfile_in_zip_read_info);
1183
- return err;
1184
- }
1185
- #else
1186
- pfile_in_zip_read_info->raw = 1;
1187
- #endif
1188
- }
1189
- else if (compression_method == Z_DEFLATED)
1190
- {
1191
- #ifdef HAVE_APPLE_COMPRESSION
1192
- err = compression_stream_init(&pfile_in_zip_read_info->astream, COMPRESSION_STREAM_DECODE, COMPRESSION_ZLIB);
1193
- if (err == COMPRESSION_STATUS_ERROR)
1194
- err = UNZ_INTERNALERROR;
1195
- else
1196
- err = Z_OK;
1197
- #else
1198
- err = inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
1199
- #endif
1200
- if (err == Z_OK)
1201
- {
1202
- pfile_in_zip_read_info->stream_initialised = Z_DEFLATED;
1203
- }
1204
- else
1205
- {
1206
- TRYFREE(pfile_in_zip_read_info);
1207
- return err;
1208
- }
1209
- /* windowBits is passed < 0 to tell that there is no zlib header.
1210
- * Note that in this case inflate *requires* an extra "dummy" byte
1211
- * after the compressed stream in order to complete decompression and
1212
- * return Z_STREAM_END.
1213
- * In unzip, i don't wait absolutely Z_STREAM_END because I known the
1214
- * size of both compressed and uncompressed data
1215
- */
1216
- }
1217
- }
1218
-
1219
- s->pfile_in_zip_read = pfile_in_zip_read_info;
1220
-
1221
- #ifndef NOUNCRYPT
1222
- s->pcrc_32_tab = NULL;
1223
-
1224
- if ((password != NULL) && ((s->cur_file_info.flag & 1) != 0))
1225
- {
1226
- if (ZSEEK64(s->z_filefunc, s->filestream,
1227
- s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile,
1228
- ZLIB_FILEFUNC_SEEK_SET) != 0)
1229
- return UNZ_INTERNALERROR;
1230
- #ifdef HAVE_AES
1231
- if (s->cur_file_info.compression_method == AES_METHOD)
1232
- {
1233
- unsigned char passverify_archive[AES_PWVERIFYSIZE];
1234
- unsigned char passverify_password[AES_PWVERIFYSIZE];
1235
- unsigned char salt_value[AES_MAXSALTLENGTH];
1236
- uint32_t salt_length = 0;
1237
-
1238
- if ((s->cur_file_info_internal.aes_encryption_mode < 1) ||
1239
- (s->cur_file_info_internal.aes_encryption_mode > 3))
1240
- return UNZ_INTERNALERROR;
1241
-
1242
- salt_length = SALT_LENGTH(s->cur_file_info_internal.aes_encryption_mode);
1243
-
1244
- if (ZREAD64(s->z_filefunc, s->filestream, salt_value, salt_length) != salt_length)
1245
- return UNZ_INTERNALERROR;
1246
- if (ZREAD64(s->z_filefunc, s->filestream, passverify_archive, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
1247
- return UNZ_INTERNALERROR;
1248
-
1249
- fcrypt_init(s->cur_file_info_internal.aes_encryption_mode, (uint8_t *)password,
1250
- (uint32_t)strlen(password), salt_value, passverify_password, &s->pfile_in_zip_read->aes_ctx);
1251
-
1252
- if (memcmp(passverify_archive, passverify_password, AES_PWVERIFYSIZE) != 0)
1253
- return UNZ_BADPASSWORD;
1254
-
1255
- s->pfile_in_zip_read->rest_read_compressed -= salt_length + AES_PWVERIFYSIZE;
1256
- s->pfile_in_zip_read->rest_read_compressed -= AES_AUTHCODESIZE;
1257
-
1258
- s->pfile_in_zip_read->pos_in_zipfile += salt_length + AES_PWVERIFYSIZE;
1259
- }
1260
- else
1261
- #endif
1262
- {
1263
- int i;
1264
- s->pcrc_32_tab = (const z_crc_t*)get_crc_table();
1265
- init_keys(password, s->keys, s->pcrc_32_tab);
1266
-
1267
- if (ZREAD64(s->z_filefunc, s->filestream, source, 12) < 12)
1268
- return UNZ_INTERNALERROR;
1269
-
1270
- for (i = 0; i < 12; i++)
1271
- zdecode(s->keys, s->pcrc_32_tab, source[i]);
1272
- uint8_t expected = (s->cur_file_info.flag & (1 << 3)) ?
1273
- s->cur_file_info.dos_date >> 8 :
1274
- s->cur_file_info.crc >> 24;
1275
- uint8_t actual = (uint8_t)source[11];
1276
- if (expected != actual) {
1277
- return UNZ_BADPASSWORD;
1278
- }
1279
-
1280
- s->pfile_in_zip_read->rest_read_compressed -= 12;
1281
- s->pfile_in_zip_read->pos_in_zipfile += 12;
1282
- }
1283
- }
1284
- #endif
1285
-
1286
- return UNZ_OK;
1287
- }
1288
-
1289
- extern int ZEXPORT unzOpenCurrentFile(unzFile file)
1290
- {
1291
- return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
1292
- }
1293
-
1294
- extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
1295
- {
1296
- return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
1297
- }
1298
-
1299
- extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
1300
- {
1301
- return unzOpenCurrentFile3(file, method, level, raw, NULL);
1302
- }
1303
-
1304
- /* Read bytes from the current file.
1305
- buf contain buffer where data must be copied
1306
- len the size of buf.
1307
-
1308
- return the number of byte copied if some bytes are copied
1309
- return 0 if the end of file was reached
1310
- return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
1311
- extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len)
1312
- {
1313
- unz64_internal *s = NULL;
1314
- uint32_t read = 0;
1315
- int err = UNZ_OK;
1316
-
1317
- if (file == NULL)
1318
- return UNZ_PARAMERROR;
1319
- s = (unz64_internal*)file;
1320
-
1321
- if (s->pfile_in_zip_read == NULL)
1322
- return UNZ_PARAMERROR;
1323
- if (s->pfile_in_zip_read->read_buffer == NULL)
1324
- return UNZ_END_OF_LIST_OF_FILE;
1325
- if (len == 0)
1326
- return 0;
1327
- // avail_out is uInt, so uint32_t len might allow requesting a larger buffer than zlib can support
1328
- if (len > UINT_MAX)
1329
- return UNZ_PARAMERROR;
1330
-
1331
- s->pfile_in_zip_read->stream.next_out = (uint8_t*)buf;
1332
- s->pfile_in_zip_read->stream.avail_out = (uInt)len;
1333
-
1334
- if ((s->pfile_in_zip_read->compression_method == 0) || (s->pfile_in_zip_read->raw))
1335
- {
1336
- if (len > s->pfile_in_zip_read->rest_read_compressed + s->pfile_in_zip_read->stream.avail_in)
1337
- s->pfile_in_zip_read->stream.avail_out = (uInt)s->pfile_in_zip_read->rest_read_compressed +
1338
- s->pfile_in_zip_read->stream.avail_in;
1339
- }
1340
-
1341
- do
1342
- {
1343
- if (s->pfile_in_zip_read->stream.avail_in == 0)
1344
- {
1345
- uint32_t bytes_to_read = UNZ_BUFSIZE;
1346
- uint32_t bytes_not_read = 0;
1347
- uint32_t bytes_read = 0;
1348
- uint32_t total_bytes_read = 0;
1349
-
1350
- if (s->pfile_in_zip_read->stream.next_in != NULL)
1351
- bytes_not_read = (uint32_t)(s->pfile_in_zip_read->read_buffer + UNZ_BUFSIZE -
1352
- s->pfile_in_zip_read->stream.next_in);
1353
- bytes_to_read -= bytes_not_read;
1354
- if (bytes_not_read > 0)
1355
- memmove(s->pfile_in_zip_read->read_buffer, s->pfile_in_zip_read->stream.next_in, bytes_not_read);
1356
- if (s->pfile_in_zip_read->rest_read_compressed < bytes_to_read)
1357
- bytes_to_read = (uint32_t)s->pfile_in_zip_read->rest_read_compressed;
1358
-
1359
- while (total_bytes_read != bytes_to_read)
1360
- {
1361
- if (ZSEEK64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream,
1362
- s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile,
1363
- ZLIB_FILEFUNC_SEEK_SET) != 0)
1364
- return UNZ_ERRNO;
1365
-
1366
- bytes_read = ZREAD64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream,
1367
- s->pfile_in_zip_read->read_buffer + bytes_not_read + total_bytes_read,
1368
- bytes_to_read - total_bytes_read);
1369
-
1370
- total_bytes_read += bytes_read;
1371
- s->pfile_in_zip_read->pos_in_zipfile += bytes_read;
1372
-
1373
- if (bytes_read == 0)
1374
- {
1375
- if (ZERROR64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream))
1376
- return UNZ_ERRNO;
1377
-
1378
- err = unzGoToNextDisk(file);
1379
- if (err != UNZ_OK)
1380
- return err;
1381
-
1382
- s->pfile_in_zip_read->pos_in_zipfile = 0;
1383
- s->pfile_in_zip_read->filestream = s->filestream;
1384
- }
1385
- }
1386
-
1387
- #ifndef NOUNCRYPT
1388
- if ((s->cur_file_info.flag & 1) != 0)
1389
- {
1390
- #ifdef HAVE_AES
1391
- if (s->cur_file_info.compression_method == AES_METHOD)
1392
- {
1393
- fcrypt_decrypt(s->pfile_in_zip_read->read_buffer, bytes_to_read, &s->pfile_in_zip_read->aes_ctx);
1394
- }
1395
- else
1396
- #endif
1397
- if (s->pcrc_32_tab != NULL)
1398
- {
1399
- uint32_t i = 0;
1400
-
1401
- for (i = 0; i < total_bytes_read; i++)
1402
- s->pfile_in_zip_read->read_buffer[i] =
1403
- zdecode(s->keys, s->pcrc_32_tab, s->pfile_in_zip_read->read_buffer[i]);
1404
- }
1405
- }
1406
- #endif
1407
-
1408
- s->pfile_in_zip_read->rest_read_compressed -= total_bytes_read;
1409
- s->pfile_in_zip_read->stream.next_in = (uint8_t*)s->pfile_in_zip_read->read_buffer;
1410
- s->pfile_in_zip_read->stream.avail_in = (uInt)(bytes_not_read + total_bytes_read);
1411
- }
1412
-
1413
- if ((s->pfile_in_zip_read->compression_method == 0) || (s->pfile_in_zip_read->raw))
1414
- {
1415
- uint32_t i = 0;
1416
- uint32_t copy = 0;
1417
-
1418
- if ((s->pfile_in_zip_read->stream.avail_in == 0) &&
1419
- (s->pfile_in_zip_read->rest_read_compressed == 0))
1420
- return (read == 0) ? UNZ_EOF : read;
1421
-
1422
- if (s->pfile_in_zip_read->stream.avail_out < s->pfile_in_zip_read->stream.avail_in)
1423
- copy = s->pfile_in_zip_read->stream.avail_out;
1424
- else
1425
- copy = s->pfile_in_zip_read->stream.avail_in;
1426
-
1427
- for (i = 0; i < copy; i++)
1428
- *(s->pfile_in_zip_read->stream.next_out + i) =
1429
- *(s->pfile_in_zip_read->stream.next_in + i);
1430
-
1431
- s->pfile_in_zip_read->total_out_64 = s->pfile_in_zip_read->total_out_64 + copy;
1432
- s->pfile_in_zip_read->rest_read_uncompressed -= copy;
1433
- s->pfile_in_zip_read->crc32 = (uint32_t)crc32(s->pfile_in_zip_read->crc32,
1434
- s->pfile_in_zip_read->stream.next_out, copy);
1435
-
1436
- s->pfile_in_zip_read->stream.avail_in -= copy;
1437
- s->pfile_in_zip_read->stream.avail_out -= copy;
1438
- s->pfile_in_zip_read->stream.next_out += copy;
1439
- s->pfile_in_zip_read->stream.next_in += copy;
1440
- s->pfile_in_zip_read->stream.total_out += copy;
1441
-
1442
- read += copy;
1443
- }
1444
- else if (s->pfile_in_zip_read->compression_method == Z_BZIP2ED)
1445
- {
1446
- #ifdef HAVE_BZIP2
1447
- uint64_t total_out_before = 0;
1448
- uint64_t total_out_after = 0;
1449
- uint64_t out_bytes = 0;
1450
- const uint8_t *buf_before = NULL;
1451
-
1452
- s->pfile_in_zip_read->bstream.next_in = (char*)s->pfile_in_zip_read->stream.next_in;
1453
- s->pfile_in_zip_read->bstream.avail_in = s->pfile_in_zip_read->stream.avail_in;
1454
- s->pfile_in_zip_read->bstream.total_in_lo32 = (uint32_t)s->pfile_in_zip_read->stream.total_in;
1455
- s->pfile_in_zip_read->bstream.total_in_hi32 = s->pfile_in_zip_read->stream.total_in >> 32;
1456
-
1457
- s->pfile_in_zip_read->bstream.next_out = (char*)s->pfile_in_zip_read->stream.next_out;
1458
- s->pfile_in_zip_read->bstream.avail_out = s->pfile_in_zip_read->stream.avail_out;
1459
- s->pfile_in_zip_read->bstream.total_out_lo32 = (uint32_t)s->pfile_in_zip_read->stream.total_out;
1460
- s->pfile_in_zip_read->bstream.total_out_hi32 = s->pfile_in_zip_read->stream.total_out >> 32;
1461
-
1462
- total_out_before = s->pfile_in_zip_read->bstream.total_out_lo32 +
1463
- (((uint32_t)s->pfile_in_zip_read->bstream.total_out_hi32) << 32);
1464
- buf_before = (const uint8_t*)s->pfile_in_zip_read->bstream.next_out;
1465
-
1466
- err = BZ2_bzDecompress(&s->pfile_in_zip_read->bstream);
1467
-
1468
- total_out_after = s->pfile_in_zip_read->bstream.total_out_lo32 +
1469
- (((uint32_t)s->pfile_in_zip_read->bstream.total_out_hi32) << 32);
1470
-
1471
- out_bytes = total_out_after - total_out_before;
1472
-
1473
- s->pfile_in_zip_read->total_out_64 = s->pfile_in_zip_read->total_out_64 + out_bytes;
1474
- s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes;
1475
- s->pfile_in_zip_read->crc32 = crc32(s->pfile_in_zip_read->crc32, buf_before, (uint32_t)out_bytes);
1476
-
1477
- read += (uint32_t)out_bytes;
1478
-
1479
- s->pfile_in_zip_read->stream.next_in = (uint8_t*)s->pfile_in_zip_read->bstream.next_in;
1480
- s->pfile_in_zip_read->stream.avail_in = s->pfile_in_zip_read->bstream.avail_in;
1481
- s->pfile_in_zip_read->stream.total_in = s->pfile_in_zip_read->bstream.total_in_lo32;
1482
- s->pfile_in_zip_read->stream.next_out = (uint8_t*)s->pfile_in_zip_read->bstream.next_out;
1483
- s->pfile_in_zip_read->stream.avail_out = s->pfile_in_zip_read->bstream.avail_out;
1484
- s->pfile_in_zip_read->stream.total_out = s->pfile_in_zip_read->bstream.total_out_lo32;
1485
-
1486
- if (err == BZ_STREAM_END)
1487
- return (read == 0) ? UNZ_EOF : read;
1488
- if (err != BZ_OK)
1489
- break;
1490
- #endif
1491
- }
1492
- #ifdef HAVE_APPLE_COMPRESSION
1493
- else
1494
- {
1495
- uint64_t total_out_before = 0;
1496
- uint64_t total_out_after = 0;
1497
- uint64_t out_bytes = 0;
1498
- const uint8_t *buf_before = NULL;
1499
-
1500
- s->pfile_in_zip_read->astream.src_ptr = s->pfile_in_zip_read->stream.next_in;
1501
- s->pfile_in_zip_read->astream.src_size = s->pfile_in_zip_read->stream.avail_in;
1502
- s->pfile_in_zip_read->astream.dst_ptr = s->pfile_in_zip_read->stream.next_out;
1503
- s->pfile_in_zip_read->astream.dst_size = len;
1504
-
1505
- total_out_before = s->pfile_in_zip_read->stream.total_out;
1506
- buf_before = s->pfile_in_zip_read->stream.next_out;
1507
-
1508
- compression_status status;
1509
- compression_stream_flags flags;
1510
-
1511
- if (s->pfile_in_zip_read->stream.avail_in == 0)
1512
- {
1513
- flags = COMPRESSION_STREAM_FINALIZE;
1514
- }
1515
-
1516
- status = compression_stream_process(&s->pfile_in_zip_read->astream, flags);
1517
-
1518
- total_out_after = len - s->pfile_in_zip_read->astream.dst_size;
1519
- out_bytes = total_out_after - total_out_before;
1520
-
1521
- s->pfile_in_zip_read->total_out_64 += out_bytes;
1522
- s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes;
1523
- s->pfile_in_zip_read->crc32 =
1524
- crc32(s->pfile_in_zip_read->crc32, buf_before, (uint32_t)out_bytes);
1525
-
1526
- read += (uint32_t)out_bytes;
1527
-
1528
- s->pfile_in_zip_read->stream.next_in = s->pfile_in_zip_read->astream.src_ptr;
1529
- s->pfile_in_zip_read->stream.avail_in = s->pfile_in_zip_read->astream.src_size;
1530
- s->pfile_in_zip_read->stream.next_out = s->pfile_in_zip_read->astream.dst_ptr;
1531
- s->pfile_in_zip_read->stream.avail_out = s->pfile_in_zip_read->astream.dst_size;
1532
-
1533
- if (status == COMPRESSION_STATUS_END)
1534
- return (read == 0) ? UNZ_EOF : read;
1535
- if (status == COMPRESSION_STATUS_ERROR)
1536
- return Z_DATA_ERROR;
1537
- return read;
1538
- }
1539
- #else
1540
- else
1541
- {
1542
- uint64_t total_out_before = 0;
1543
- uint64_t total_out_after = 0;
1544
- uint64_t out_bytes = 0;
1545
- const uint8_t *buf_before = NULL;
1546
- int flush = Z_SYNC_FLUSH;
1547
-
1548
- total_out_before = s->pfile_in_zip_read->stream.total_out;
1549
- buf_before = s->pfile_in_zip_read->stream.next_out;
1550
-
1551
- /*
1552
- if ((pfile_in_zip_read_info->rest_read_uncompressed ==
1553
- pfile_in_zip_read_info->stream.avail_out) &&
1554
- (pfile_in_zip_read_info->rest_read_compressed == 0))
1555
- flush = Z_FINISH;
1556
- */
1557
- err = inflate(&s->pfile_in_zip_read->stream, flush);
1558
-
1559
- if ((err >= 0) && (s->pfile_in_zip_read->stream.msg != NULL))
1560
- err = Z_DATA_ERROR;
1561
-
1562
- total_out_after = s->pfile_in_zip_read->stream.total_out;
1563
- out_bytes = total_out_after - total_out_before;
1564
-
1565
- s->pfile_in_zip_read->total_out_64 += out_bytes;
1566
- s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes;
1567
- s->pfile_in_zip_read->crc32 =
1568
- (uint32_t)crc32(s->pfile_in_zip_read->crc32, buf_before, (uint32_t)out_bytes);
1569
-
1570
- read += (uint32_t)out_bytes;
1571
-
1572
- if (err == Z_STREAM_END)
1573
- return (read == 0) ? UNZ_EOF : read;
1574
- if (err != Z_OK)
1575
- break;
1576
- }
1577
- #endif
1578
- }
1579
- while (s->pfile_in_zip_read->stream.avail_out > 0);
1580
-
1581
- if (err == Z_OK)
1582
- return read;
1583
- return err;
1584
- }
1585
-
1586
- extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, uint32_t len)
1587
- {
1588
- unz64_internal *s = NULL;
1589
- uint64_t size_to_read = 0;
1590
- uint32_t read_now = 0;
1591
-
1592
- if (file == NULL)
1593
- return UNZ_PARAMERROR;
1594
- s = (unz64_internal*)file;
1595
- if (s->pfile_in_zip_read == NULL)
1596
- return UNZ_PARAMERROR;
1597
-
1598
- size_to_read = s->pfile_in_zip_read->size_local_extrafield - s->pfile_in_zip_read->pos_local_extrafield;
1599
-
1600
- if (buf == NULL)
1601
- return (int)size_to_read;
1602
-
1603
- if (len > size_to_read)
1604
- read_now = (uint32_t)size_to_read;
1605
- else
1606
- read_now = len;
1607
-
1608
- if (read_now == 0)
1609
- return 0;
1610
-
1611
- if (ZSEEK64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream,
1612
- s->pfile_in_zip_read->offset_local_extrafield + s->pfile_in_zip_read->pos_local_extrafield,
1613
- ZLIB_FILEFUNC_SEEK_SET) != 0)
1614
- return UNZ_ERRNO;
1615
-
1616
- if (ZREAD64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream, buf, read_now) != read_now)
1617
- return UNZ_ERRNO;
1618
-
1619
- return (int)read_now;
1620
- }
1621
-
1622
- extern int ZEXPORT unzCloseCurrentFile(unzFile file)
1623
- {
1624
- unz64_internal *s = NULL;
1625
- file_in_zip64_read_info_s *pfile_in_zip_read_info = NULL;
1626
- int err = UNZ_OK;
1627
-
1628
- if (file == NULL)
1629
- return UNZ_PARAMERROR;
1630
- s = (unz64_internal*)file;
1631
- pfile_in_zip_read_info = s->pfile_in_zip_read;
1632
- if (pfile_in_zip_read_info == NULL)
1633
- return UNZ_PARAMERROR;
1634
-
1635
- #ifdef HAVE_AES
1636
- if (s->cur_file_info.compression_method == AES_METHOD)
1637
- {
1638
- unsigned char authcode[AES_AUTHCODESIZE];
1639
- unsigned char rauthcode[AES_AUTHCODESIZE];
1640
-
1641
- if (ZREAD64(s->z_filefunc, s->filestream, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
1642
- return UNZ_ERRNO;
1643
-
1644
- if (fcrypt_end(rauthcode, &s->pfile_in_zip_read->aes_ctx) != AES_AUTHCODESIZE)
1645
- err = UNZ_CRCERROR;
1646
- if (memcmp(authcode, rauthcode, AES_AUTHCODESIZE) != 0)
1647
- err = UNZ_CRCERROR;
1648
- }
1649
- /* AES zip version AE-1 will expect a valid crc as well */
1650
- if ((s->cur_file_info.compression_method != AES_METHOD) ||
1651
- (s->cur_file_info_internal.aes_version == 0x0001))
1652
- #endif
1653
- {
1654
- if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
1655
- (!pfile_in_zip_read_info->raw))
1656
- {
1657
- if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_expected)
1658
- err = UNZ_CRCERROR;
1659
- }
1660
- }
1661
-
1662
- TRYFREE(pfile_in_zip_read_info->read_buffer);
1663
- pfile_in_zip_read_info->read_buffer = NULL;
1664
- if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
1665
- {
1666
- #ifdef HAVE_APPLE_COMPRESSION
1667
- if (compression_stream_destroy)
1668
- compression_stream_destroy(&pfile_in_zip_read_info->astream);
1669
- #else
1670
- inflateEnd(&pfile_in_zip_read_info->stream);
1671
- #endif
1672
-
1673
- }
1674
- #ifdef HAVE_BZIP2
1675
- else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
1676
- BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
1677
- #endif
1678
-
1679
- pfile_in_zip_read_info->stream_initialised = 0;
1680
- TRYFREE(pfile_in_zip_read_info);
1681
-
1682
- s->pfile_in_zip_read = NULL;
1683
-
1684
- return err;
1685
- }
1686
-
1687
- extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
1688
- uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
1689
- {
1690
- unz64_internal *s = NULL;
1691
- int err = UNZ_OK;
1692
-
1693
- if (file == NULL)
1694
- return UNZ_PARAMERROR;
1695
- s = (unz64_internal*)file;
1696
-
1697
- if (s->gi.number_entry == 0)
1698
- return UNZ_END_OF_LIST_OF_FILE;
1699
-
1700
- s->pos_in_central_dir = s->offset_central_dir;
1701
- s->num_file = 0;
1702
-
1703
- err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal,
1704
- filename, filename_size, extrafield, extrafield_size, comment, comment_size);
1705
-
1706
- s->current_file_ok = (err == UNZ_OK);
1707
- if ((err == UNZ_OK) && (pfile_info != NULL))
1708
- memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64));
1709
-
1710
- return err;
1711
- }
1712
-
1713
- extern int ZEXPORT unzGoToFirstFile(unzFile file)
1714
- {
1715
- return unzGoToFirstFile2(file, NULL, NULL, 0, NULL, 0, NULL, 0);
1716
- }
1717
-
1718
- extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
1719
- uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
1720
- {
1721
- unz64_internal *s = NULL;
1722
- int err = UNZ_OK;
1723
-
1724
- if (file == NULL)
1725
- return UNZ_PARAMERROR;
1726
- s = (unz64_internal*)file;
1727
-
1728
- if (!s->current_file_ok)
1729
- return UNZ_END_OF_LIST_OF_FILE;
1730
- if (s->gi.number_entry != UINT16_MAX) /* 2^16 files overflow hack */
1731
- {
1732
- if (s->num_file+1 == s->gi.number_entry)
1733
- return UNZ_END_OF_LIST_OF_FILE;
1734
- }
1735
-
1736
- s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
1737
- s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment;
1738
- s->num_file += 1;
1739
-
1740
- err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal,
1741
- filename, filename_size, extrafield, extrafield_size, comment, comment_size);
1742
-
1743
- s->current_file_ok = (err == UNZ_OK);
1744
- if ((err == UNZ_OK) && (pfile_info != NULL))
1745
- memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64));
1746
-
1747
- return err;
1748
- }
1749
-
1750
- extern int ZEXPORT unzGoToNextFile(unzFile file)
1751
- {
1752
- return unzGoToNextFile2(file, NULL, NULL, 0, NULL, 0, NULL, 0);
1753
- }
1754
-
1755
- extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
1756
- {
1757
- unz64_internal *s = NULL;
1758
- unz_file_info64 cur_file_info_saved;
1759
- unz_file_info64_internal cur_file_info_internal_saved;
1760
- uint64_t num_file_saved = 0;
1761
- uint64_t pos_in_central_dir_saved = 0;
1762
- char current_filename[UNZ_MAXFILENAMEINZIP+1];
1763
- int err = UNZ_OK;
1764
-
1765
- if (file == NULL)
1766
- return UNZ_PARAMERROR;
1767
- if (strlen(filename) >= UNZ_MAXFILENAMEINZIP)
1768
- return UNZ_PARAMERROR;
1769
- s = (unz64_internal*)file;
1770
- if (!s->current_file_ok)
1771
- return UNZ_END_OF_LIST_OF_FILE;
1772
-
1773
- /* Save the current state */
1774
- num_file_saved = s->num_file;
1775
- pos_in_central_dir_saved = s->pos_in_central_dir;
1776
- cur_file_info_saved = s->cur_file_info;
1777
- cur_file_info_internal_saved = s->cur_file_info_internal;
1778
-
1779
- err = unzGoToFirstFile2(file, NULL, current_filename, sizeof(current_filename)-1, NULL, 0, NULL, 0);
1780
-
1781
- while (err == UNZ_OK)
1782
- {
1783
- if (filename_compare_func != NULL)
1784
- err = filename_compare_func(file, current_filename, filename);
1785
- else
1786
- err = strcmp(current_filename, filename);
1787
- if (err == 0)
1788
- return UNZ_OK;
1789
- err = unzGoToNextFile2(file, NULL, current_filename, sizeof(current_filename)-1, NULL, 0, NULL, 0);
1790
- }
1791
-
1792
- /* We failed, so restore the state of the 'current file' to where we were. */
1793
- s->num_file = num_file_saved;
1794
- s->pos_in_central_dir = pos_in_central_dir_saved;
1795
- s->cur_file_info = cur_file_info_saved;
1796
- s->cur_file_info_internal = cur_file_info_internal_saved;
1797
- return err;
1798
- }
1799
-
1800
- extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos)
1801
- {
1802
- unz64_file_pos file_pos64;
1803
- int err = unzGetFilePos64(file, &file_pos64);
1804
- if (err == UNZ_OK)
1805
- {
1806
- file_pos->pos_in_zip_directory = (uint32_t)file_pos64.pos_in_zip_directory;
1807
- file_pos->num_of_file = (uint32_t)file_pos64.num_of_file;
1808
- }
1809
- return err;
1810
- }
1811
-
1812
- extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos)
1813
- {
1814
- unz64_file_pos file_pos64;
1815
- if (file_pos == NULL)
1816
- return UNZ_PARAMERROR;
1817
- file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
1818
- file_pos64.num_of_file = file_pos->num_of_file;
1819
- return unzGoToFilePos64(file, &file_pos64);
1820
- }
1821
-
1822
- extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos)
1823
- {
1824
- unz64_internal *s = NULL;
1825
-
1826
- if (file == NULL || file_pos == NULL)
1827
- return UNZ_PARAMERROR;
1828
- s = (unz64_internal*)file;
1829
- if (!s->current_file_ok)
1830
- return UNZ_END_OF_LIST_OF_FILE;
1831
-
1832
- file_pos->pos_in_zip_directory = s->pos_in_central_dir;
1833
- file_pos->num_of_file = s->num_file;
1834
- return UNZ_OK;
1835
- }
1836
-
1837
- extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos)
1838
- {
1839
- unz64_internal *s = NULL;
1840
- int err = UNZ_OK;
1841
-
1842
- if (file == NULL || file_pos == NULL)
1843
- return UNZ_PARAMERROR;
1844
- s = (unz64_internal*)file;
1845
-
1846
- /* Jump to the right spot */
1847
- s->pos_in_central_dir = file_pos->pos_in_zip_directory;
1848
- s->num_file = file_pos->num_of_file;
1849
-
1850
- /* Set the current file */
1851
- err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0);
1852
- /* Return results */
1853
- s->current_file_ok = (err == UNZ_OK);
1854
- return err;
1855
- }
1856
-
1857
- extern int32_t ZEXPORT unzGetOffset(unzFile file)
1858
- {
1859
- uint64_t offset64 = 0;
1860
-
1861
- if (file == NULL)
1862
- return UNZ_PARAMERROR;
1863
- offset64 = unzGetOffset64(file);
1864
- return (int32_t)offset64;
1865
- }
1866
-
1867
- extern int64_t ZEXPORT unzGetOffset64(unzFile file)
1868
- {
1869
- unz64_internal *s = NULL;
1870
-
1871
- if (file == NULL)
1872
- return UNZ_PARAMERROR;
1873
- s = (unz64_internal*)file;
1874
- if (!s->current_file_ok)
1875
- return 0;
1876
- if (s->gi.number_entry != 0 && s->gi.number_entry != UINT16_MAX)
1877
- {
1878
- if (s->num_file == s->gi.number_entry)
1879
- return 0;
1880
- }
1881
- return s->pos_in_central_dir;
1882
- }
1883
-
1884
- extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos)
1885
- {
1886
- return unzSetOffset64(file, pos);
1887
- }
1888
-
1889
- extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
1890
- {
1891
- unz64_internal *s = NULL;
1892
- int err = UNZ_OK;
1893
-
1894
- if (file == NULL)
1895
- return UNZ_PARAMERROR;
1896
- s = (unz64_internal*)file;
1897
- s->pos_in_central_dir = pos;
1898
- s->num_file = s->gi.number_entry; /* hack */
1899
-
1900
- err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0);
1901
-
1902
- s->current_file_ok = (err == UNZ_OK);
1903
- return err;
1904
- }
1905
-
1906
- extern int32_t ZEXPORT unzTell(unzFile file)
1907
- {
1908
- unz64_internal *s = NULL;
1909
- if (file == NULL)
1910
- return UNZ_PARAMERROR;
1911
- s = (unz64_internal*)file;
1912
- if (s->pfile_in_zip_read == NULL)
1913
- return UNZ_PARAMERROR;
1914
- return (int32_t)s->pfile_in_zip_read->stream.total_out;
1915
- }
1916
-
1917
- extern int64_t ZEXPORT unzTell64(unzFile file)
1918
- {
1919
- unz64_internal *s = NULL;
1920
- if (file == NULL)
1921
- return UNZ_PARAMERROR;
1922
- s = (unz64_internal*)file;
1923
- if (s->pfile_in_zip_read == NULL)
1924
- return UNZ_PARAMERROR;
1925
- return s->pfile_in_zip_read->total_out_64;
1926
- }
1927
-
1928
- extern int ZEXPORT unzSeek(unzFile file, uint32_t offset, int origin)
1929
- {
1930
- return unzSeek64(file, offset, origin);
1931
- }
1932
-
1933
- extern int ZEXPORT unzSeek64(unzFile file, uint64_t offset, int origin)
1934
- {
1935
- unz64_internal *s = NULL;
1936
- uint64_t stream_pos_begin = 0;
1937
- uint64_t stream_pos_end = 0;
1938
- uint64_t position = 0;
1939
- int is_within_buffer = 0;
1940
-
1941
- if (file == NULL)
1942
- return UNZ_PARAMERROR;
1943
- s = (unz64_internal*)file;
1944
-
1945
- if (s->pfile_in_zip_read == NULL)
1946
- return UNZ_ERRNO;
1947
- if (s->pfile_in_zip_read->compression_method != 0)
1948
- return UNZ_ERRNO;
1949
-
1950
- if (origin == SEEK_SET)
1951
- position = offset;
1952
- else if (origin == SEEK_CUR)
1953
- position = s->pfile_in_zip_read->total_out_64 + offset;
1954
- else if (origin == SEEK_END)
1955
- position = s->cur_file_info.compressed_size + offset;
1956
- else
1957
- return UNZ_PARAMERROR;
1958
-
1959
- if (position > s->cur_file_info.compressed_size)
1960
- return UNZ_PARAMERROR;
1961
-
1962
- stream_pos_end = s->pfile_in_zip_read->pos_in_zipfile;
1963
- stream_pos_begin = stream_pos_end;
1964
-
1965
- if (stream_pos_begin > UNZ_BUFSIZE)
1966
- stream_pos_begin -= UNZ_BUFSIZE;
1967
- else
1968
- stream_pos_begin = 0;
1969
-
1970
- is_within_buffer =
1971
- (s->pfile_in_zip_read->stream.avail_in != 0) &&
1972
- (s->pfile_in_zip_read->rest_read_compressed != 0 || s->cur_file_info.compressed_size < UNZ_BUFSIZE) &&
1973
- (position >= stream_pos_begin && position < stream_pos_end);
1974
-
1975
- if (is_within_buffer)
1976
- {
1977
- s->pfile_in_zip_read->stream.next_in += position - s->pfile_in_zip_read->total_out_64;
1978
- s->pfile_in_zip_read->stream.avail_in = (uInt)(stream_pos_end - position);
1979
- }
1980
- else
1981
- {
1982
- s->pfile_in_zip_read->stream.avail_in = 0;
1983
- s->pfile_in_zip_read->stream.next_in = 0;
1984
-
1985
- s->pfile_in_zip_read->pos_in_zipfile = s->pfile_in_zip_read->offset_local_extrafield + position;
1986
- s->pfile_in_zip_read->rest_read_compressed = s->cur_file_info.compressed_size - position;
1987
- }
1988
-
1989
- s->pfile_in_zip_read->rest_read_uncompressed -= (position - s->pfile_in_zip_read->total_out_64);
1990
- s->pfile_in_zip_read->stream.total_out = (uint32_t)position;
1991
- s->pfile_in_zip_read->total_out_64 = position;
1992
-
1993
- return UNZ_OK;
1994
- }
1995
-
1996
- extern int ZEXPORT unzEndOfFile(unzFile file)
1997
- {
1998
- unz64_internal *s = NULL;
1999
- if (file == NULL)
2000
- return UNZ_PARAMERROR;
2001
- s = (unz64_internal*)file;
2002
- if (s->pfile_in_zip_read == NULL)
2003
- return UNZ_PARAMERROR;
2004
- if (s->pfile_in_zip_read->rest_read_uncompressed == 0)
2005
- return 1;
2006
- return 0;
2007
- }
1
+ /* unzip.c -- IO for uncompress .zip files using zlib
2
+ Version 1.2.0, September 16th, 2017
3
+ part of the MiniZip project
4
+
5
+ Copyright (C) 2010-2017 Nathan Moinvaziri
6
+ Modifications for AES, PKWARE disk spanning
7
+ https://github.com/nmoinvaz/minizip
8
+ Copyright (C) 2009-2010 Mathias Svensson
9
+ Modifications for Zip64 support on both zip and unzip
10
+ http://result42.com
11
+ Copyright (C) 2007-2008 Even Rouault
12
+ Modifications of Unzip for Zip64
13
+ Copyright (C) 1998-2010 Gilles Vollant
14
+ http://www.winimage.com/zLibDll/minizip.html
15
+
16
+ This program is distributed under the terms of the same license as zlib.
17
+ See the accompanying LICENSE file for the full text of the license.
18
+ */
19
+
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+ #include <stdint.h>
23
+ #include <string.h>
24
+ #include <errno.h>
25
+ #include <assert.h>
26
+
27
+ #include "zlib.h"
28
+ #include "unzip.h"
29
+
30
+ #ifdef HAVE_AES
31
+ # define AES_METHOD (99)
32
+ # define AES_PWVERIFYSIZE (2)
33
+ # define AES_MAXSALTLENGTH (16)
34
+ # define AES_AUTHCODESIZE (10)
35
+ # define AES_HEADERSIZE (11)
36
+ # define AES_KEYSIZE(mode) (64 + (mode * 64))
37
+
38
+ # include "aes.h"
39
+ # include "fileenc.h"
40
+ #endif
41
+ #ifdef HAVE_APPLE_COMPRESSION
42
+ # include <compression.h>
43
+ #endif
44
+
45
+ #ifndef NOUNCRYPT
46
+ # include "crypt.h"
47
+ #endif
48
+
49
+ #define DISKHEADERMAGIC (0x08074b50)
50
+ #define LOCALHEADERMAGIC (0x04034b50)
51
+ #define CENTRALHEADERMAGIC (0x02014b50)
52
+ #define ENDHEADERMAGIC (0x06054b50)
53
+ #define ZIP64ENDHEADERMAGIC (0x06064b50)
54
+ #define ZIP64ENDLOCHEADERMAGIC (0x07064b50)
55
+
56
+ #define SIZECENTRALDIRITEM (0x2e)
57
+ #define SIZECENTRALHEADERLOCATOR (0x14)
58
+ #define SIZEZIPLOCALHEADER (0x1e)
59
+
60
+ #ifndef BUFREADCOMMENT
61
+ # define BUFREADCOMMENT (0x400)
62
+ #endif
63
+
64
+ #ifndef UNZ_BUFSIZE
65
+ # define UNZ_BUFSIZE (UINT16_MAX)
66
+ #endif
67
+ #ifndef UNZ_MAXFILENAMEINZIP
68
+ # define UNZ_MAXFILENAMEINZIP (256)
69
+ #endif
70
+
71
+ #ifndef ALLOC
72
+ # define ALLOC(size) (malloc(size))
73
+ #endif
74
+ #ifndef TRYFREE
75
+ # define TRYFREE(p) {if (p) free(p);}
76
+ #endif
77
+
78
+ const char unz_copyright[] = " unzip 1.2.0 Copyright 1998-2017 - https://github.com/nmoinvaz/minizip";
79
+
80
+ /* unz_file_info_internal contain internal info about a file in zipfile*/
81
+ typedef struct unz_file_info64_internal_s
82
+ {
83
+ uint64_t offset_curfile; /* relative offset of local header 8 bytes */
84
+ uint64_t byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
85
+ #ifdef HAVE_AES
86
+ uint8_t aes_encryption_mode;
87
+ uint16_t aes_compression_method;
88
+ uint16_t aes_version;
89
+ #endif
90
+ } unz_file_info64_internal;
91
+
92
+ /* file_in_zip_read_info_s contain internal information about a file in zipfile */
93
+ typedef struct
94
+ {
95
+ uint8_t *read_buffer; /* internal buffer for compressed data */
96
+ z_stream stream; /* zLib stream structure for inflate */
97
+ #ifdef HAVE_BZIP2
98
+ bz_stream bstream; /* bzLib stream structure for bziped */
99
+ #endif
100
+ #ifdef HAVE_APPLE_COMPRESSION
101
+ compression_stream astream; /* libcompression stream structure */
102
+ #endif
103
+ #ifdef HAVE_AES
104
+ fcrypt_ctx aes_ctx;
105
+ #endif
106
+ uint64_t pos_in_zipfile; /* position in byte on the zipfile, for fseek */
107
+ uint8_t stream_initialised; /* flag set if stream structure is initialised */
108
+
109
+ uint64_t offset_local_extrafield; /* offset of the local extra field */
110
+ uint16_t size_local_extrafield; /* size of the local extra field */
111
+ uint64_t pos_local_extrafield; /* position in the local extra field in read */
112
+ uint64_t total_out_64;
113
+
114
+ uint32_t crc32; /* crc32 of all data uncompressed */
115
+ uint32_t crc32_expected; /* crc32 we must obtain after decompress all */
116
+ uint64_t rest_read_compressed; /* number of byte to be decompressed */
117
+ uint64_t rest_read_uncompressed; /* number of byte to be obtained after decomp */
118
+
119
+ zlib_filefunc64_32_def z_filefunc;
120
+
121
+ voidpf filestream; /* io structore of the zipfile */
122
+ uint16_t compression_method; /* compression method (0==store) */
123
+ uint64_t byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
124
+ int raw;
125
+ } file_in_zip64_read_info_s;
126
+
127
+ /* unz64_s contain internal information about the zipfile */
128
+ typedef struct
129
+ {
130
+ zlib_filefunc64_32_def z_filefunc;
131
+
132
+ voidpf filestream; /* io structure of the current zipfile */
133
+ voidpf filestream_with_CD; /* io structure of the disk with the central directory */
134
+
135
+ unz_global_info64 gi; /* public global information */
136
+
137
+ uint64_t byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
138
+ uint64_t num_file; /* number of the current file in the zipfile */
139
+ uint64_t pos_in_central_dir; /* pos of the current file in the central dir */
140
+ uint64_t current_file_ok; /* flag about the usability of the current file */
141
+ uint64_t central_pos; /* position of the beginning of the central dir */
142
+ uint32_t number_disk; /* number of the current disk, used for spanning ZIP */
143
+ uint64_t size_central_dir; /* size of the central directory */
144
+ uint64_t offset_central_dir; /* offset of start of central directory with
145
+ respect to the starting disk number */
146
+
147
+ unz_file_info64 cur_file_info; /* public info about the current file in zip*/
148
+ unz_file_info64_internal cur_file_info_internal;
149
+ /* private info about it*/
150
+ file_in_zip64_read_info_s *pfile_in_zip_read;
151
+ /* structure about the current file if we are decompressing it */
152
+ int is_zip64; /* is the current file zip64 */
153
+ #ifndef NOUNCRYPT
154
+ uint32_t keys[3]; /* keys defining the pseudo-random sequence */
155
+ const z_crc_t *pcrc_32_tab;
156
+ #endif
157
+ } unz64_internal;
158
+
159
+ /* Read a byte from a gz_stream; Return EOF for end of file. */
160
+ static int unzReadUInt8(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint8_t *value)
161
+ {
162
+ uint8_t c = 0;
163
+ if (ZREAD64(*pzlib_filefunc_def, filestream, &c, 1) == 1)
164
+ {
165
+ *value = (uint8_t)c;
166
+ return UNZ_OK;
167
+ }
168
+ *value = 0;
169
+ if (ZERROR64(*pzlib_filefunc_def, filestream))
170
+ return UNZ_ERRNO;
171
+ return UNZ_EOF;
172
+ }
173
+
174
+ static int unzReadUInt16(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint16_t *value)
175
+ {
176
+ uint16_t x;
177
+ uint8_t c = 0;
178
+ int err = UNZ_OK;
179
+
180
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
181
+ x = (uint16_t)c;
182
+ if (err == UNZ_OK)
183
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
184
+ x |= ((uint16_t)c) << 8;
185
+
186
+ if (err == UNZ_OK)
187
+ *value = x;
188
+ else
189
+ *value = 0;
190
+ return err;
191
+ }
192
+
193
+ static int unzReadUInt32(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint32_t *value)
194
+ {
195
+ uint32_t x = 0;
196
+ uint8_t c = 0;
197
+ int err = UNZ_OK;
198
+
199
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
200
+ x = (uint32_t)c;
201
+ if (err == UNZ_OK)
202
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
203
+ x |= ((uint32_t)c) << 8;
204
+ if (err == UNZ_OK)
205
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
206
+ x |= ((uint32_t)c) << 16;
207
+ if (err == UNZ_OK)
208
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &c);
209
+ x += ((uint32_t)c) << 24;
210
+
211
+ if (err == UNZ_OK)
212
+ *value = x;
213
+ else
214
+ *value = 0;
215
+ return err;
216
+ }
217
+
218
+ static int unzReadUInt64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint64_t *value)
219
+ {
220
+ uint64_t x = 0;
221
+ uint8_t i = 0;
222
+ int err = UNZ_OK;
223
+
224
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
225
+ x = (uint64_t)i;
226
+ if (err == UNZ_OK)
227
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
228
+ x |= ((uint64_t)i) << 8;
229
+ if (err == UNZ_OK)
230
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
231
+ x |= ((uint64_t)i) << 16;
232
+ if (err == UNZ_OK)
233
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
234
+ x |= ((uint64_t)i) << 24;
235
+ if (err == UNZ_OK)
236
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
237
+ x |= ((uint64_t)i) << 32;
238
+ if (err == UNZ_OK)
239
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
240
+ x |= ((uint64_t)i) << 40;
241
+ if (err == UNZ_OK)
242
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
243
+ x |= ((uint64_t)i) << 48;
244
+ if (err == UNZ_OK)
245
+ err = unzReadUInt8(pzlib_filefunc_def, filestream, &i);
246
+ x |= ((uint64_t)i) << 56;
247
+
248
+ if (err == UNZ_OK)
249
+ *value = x;
250
+ else
251
+ *value = 0;
252
+ return err;
253
+ }
254
+
255
+ /* Locate the Central directory of a zip file (at the end, just before the global comment) */
256
+ static int unzSearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, uint64_t *pos_found, voidpf filestream)
257
+ {
258
+ uint8_t buf[BUFREADCOMMENT + 4];
259
+ uint64_t file_size = 0;
260
+ uint64_t back_read = 4;
261
+ uint64_t max_back = UINT16_MAX; /* maximum size of global comment */
262
+ uint32_t read_size = 0;
263
+ uint64_t read_pos = 0;
264
+ uint32_t i = 0;
265
+ *pos_found = 0;
266
+
267
+ if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0)
268
+ return UNZ_ERRNO;
269
+
270
+ file_size = ZTELL64(*pzlib_filefunc_def, filestream);
271
+
272
+ if (max_back > file_size)
273
+ max_back = file_size;
274
+
275
+ while (back_read < max_back)
276
+ {
277
+ if (back_read + BUFREADCOMMENT > max_back)
278
+ back_read = max_back;
279
+ else
280
+ back_read += BUFREADCOMMENT;
281
+
282
+ read_pos = file_size - back_read;
283
+ read_size = ((BUFREADCOMMENT + 4) < (file_size - read_pos)) ?
284
+ (BUFREADCOMMENT + 4) : (uint32_t)(file_size - read_pos);
285
+
286
+ if (ZSEEK64(*pzlib_filefunc_def, filestream, read_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
287
+ break;
288
+ if (ZREAD64(*pzlib_filefunc_def, filestream, buf, read_size) != read_size)
289
+ break;
290
+
291
+ for (i = read_size - 3; (i--) > 0;)
292
+ if (((*(buf+i)) == (ENDHEADERMAGIC & 0xff)) &&
293
+ ((*(buf+i+1)) == (ENDHEADERMAGIC >> 8 & 0xff)) &&
294
+ ((*(buf+i+2)) == (ENDHEADERMAGIC >> 16 & 0xff)) &&
295
+ ((*(buf+i+3)) == (ENDHEADERMAGIC >> 24 & 0xff)))
296
+ {
297
+ *pos_found = read_pos+i;
298
+ return UNZ_OK;
299
+ }
300
+ }
301
+
302
+ return UNZ_ERRNO;
303
+ }
304
+
305
+ /* Locate the Central directory 64 of a zipfile (at the end, just before the global comment) */
306
+ static int unzSearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, uint64_t *offset, voidpf filestream,
307
+ const uint64_t endcentraloffset)
308
+ {
309
+ uint32_t value32 = 0;
310
+ *offset = 0;
311
+
312
+ /* Zip64 end of central directory locator */
313
+ if (ZSEEK64(*pzlib_filefunc_def, filestream, endcentraloffset - SIZECENTRALHEADERLOCATOR, ZLIB_FILEFUNC_SEEK_SET) != 0)
314
+ return UNZ_ERRNO;
315
+
316
+ /* Read locator signature */
317
+ if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
318
+ return UNZ_ERRNO;
319
+ if (value32 != ZIP64ENDLOCHEADERMAGIC)
320
+ return UNZ_ERRNO;
321
+ /* Number of the disk with the start of the zip64 end of central directory */
322
+ if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
323
+ return UNZ_ERRNO;
324
+ /* Relative offset of the zip64 end of central directory record */
325
+ if (unzReadUInt64(pzlib_filefunc_def, filestream, offset) != UNZ_OK)
326
+ return UNZ_ERRNO;
327
+ /* Total number of disks */
328
+ if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
329
+ return UNZ_ERRNO;
330
+ /* Goto end of central directory record */
331
+ if (ZSEEK64(*pzlib_filefunc_def, filestream, *offset, ZLIB_FILEFUNC_SEEK_SET) != 0)
332
+ return UNZ_ERRNO;
333
+ /* The signature */
334
+ if (unzReadUInt32(pzlib_filefunc_def, filestream, &value32) != UNZ_OK)
335
+ return UNZ_ERRNO;
336
+ if (value32 != ZIP64ENDHEADERMAGIC)
337
+ return UNZ_ERRNO;
338
+
339
+ return UNZ_OK;
340
+ }
341
+
342
+ static unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_filefunc64_32_def)
343
+ {
344
+ unz64_internal us = { 0 };
345
+ unz64_internal *s = NULL;
346
+ uint64_t central_pos = 0;
347
+ uint64_t central_pos64 = 0;
348
+ uint64_t number_entry_CD = 0;
349
+ uint16_t value16 = 0;
350
+ uint32_t value32 = 0;
351
+ uint64_t value64 = 0;
352
+ voidpf filestream = NULL;
353
+ int err = UNZ_OK;
354
+ int err64 = UNZ_OK;
355
+
356
+ if (pzlib_filefunc64_32_def == NULL)
357
+ fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
358
+ else
359
+ {
360
+ assert((pzlib_filefunc64_32_def->zopen32_file || pzlib_filefunc64_32_def->zfile_func64.zopen64_file) &&
361
+ (pzlib_filefunc64_32_def->zopendisk32_file || pzlib_filefunc64_32_def->zfile_func64.zopendisk64_file) &&
362
+ (pzlib_filefunc64_32_def->ztell32_file || pzlib_filefunc64_32_def->zfile_func64.ztell64_file) &&
363
+ (pzlib_filefunc64_32_def->zseek32_file || pzlib_filefunc64_32_def->zfile_func64.zseek64_file));
364
+ us.z_filefunc = *pzlib_filefunc64_32_def;
365
+ }
366
+
367
+ us.filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
368
+
369
+ if (us.filestream == NULL)
370
+ return NULL;
371
+
372
+ us.filestream_with_CD = us.filestream;
373
+
374
+ /* Search for end of central directory header */
375
+ err = unzSearchCentralDir(&us.z_filefunc, &central_pos, us.filestream);
376
+ if (err == UNZ_OK)
377
+ {
378
+ if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
379
+ err = UNZ_ERRNO;
380
+
381
+ /* The signature, already checked */
382
+ if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
383
+ err = UNZ_ERRNO;
384
+ /* Number of this disk */
385
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
386
+ err = UNZ_ERRNO;
387
+ us.number_disk = value16;
388
+ /* Number of the disk with the start of the central directory */
389
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
390
+ err = UNZ_ERRNO;
391
+ us.gi.number_disk_with_CD = value16;
392
+ /* Total number of entries in the central directory on this disk */
393
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
394
+ err = UNZ_ERRNO;
395
+ us.gi.number_entry = value16;
396
+ /* Total number of entries in the central directory */
397
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
398
+ err = UNZ_ERRNO;
399
+ number_entry_CD = value16;
400
+ if (number_entry_CD != us.gi.number_entry)
401
+ err = UNZ_BADZIPFILE;
402
+ /* Size of the central directory */
403
+ if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
404
+ err = UNZ_ERRNO;
405
+ us.size_central_dir = value32;
406
+ /* Offset of start of central directory with respect to the starting disk number */
407
+ if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
408
+ err = UNZ_ERRNO;
409
+ us.offset_central_dir = value32;
410
+ /* Zipfile comment length */
411
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &us.gi.size_comment) != UNZ_OK)
412
+ err = UNZ_ERRNO;
413
+
414
+ if (err == UNZ_OK)
415
+ {
416
+ /* Search for Zip64 end of central directory header */
417
+ err64 = unzSearchCentralDir64(&us.z_filefunc, &central_pos64, us.filestream, central_pos);
418
+ if (err64 == UNZ_OK)
419
+ {
420
+ central_pos = central_pos64;
421
+ us.is_zip64 = 1;
422
+
423
+ if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
424
+ err = UNZ_ERRNO;
425
+
426
+ /* the signature, already checked */
427
+ if (unzReadUInt32(&us.z_filefunc, us.filestream, &value32) != UNZ_OK)
428
+ err = UNZ_ERRNO;
429
+ /* size of zip64 end of central directory record */
430
+ if (unzReadUInt64(&us.z_filefunc, us.filestream, &value64) != UNZ_OK)
431
+ err = UNZ_ERRNO;
432
+ /* version made by */
433
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
434
+ err = UNZ_ERRNO;
435
+ /* version needed to extract */
436
+ if (unzReadUInt16(&us.z_filefunc, us.filestream, &value16) != UNZ_OK)
437
+ err = UNZ_ERRNO;
438
+ /* number of this disk */
439
+ if (unzReadUInt32(&us.z_filefunc, us.filestream, &us.number_disk) != UNZ_OK)
440
+ err = UNZ_ERRNO;
441
+ /* number of the disk with the start of the central directory */
442
+ if (unzReadUInt32(&us.z_filefunc, us.filestream, &us.gi.number_disk_with_CD) != UNZ_OK)
443
+ err = UNZ_ERRNO;
444
+ /* total number of entries in the central directory on this disk */
445
+ if (unzReadUInt64(&us.z_filefunc, us.filestream, &us.gi.number_entry) != UNZ_OK)
446
+ err = UNZ_ERRNO;
447
+ /* total number of entries in the central directory */
448
+ if (unzReadUInt64(&us.z_filefunc, us.filestream, &number_entry_CD) != UNZ_OK)
449
+ err = UNZ_ERRNO;
450
+ if (number_entry_CD != us.gi.number_entry)
451
+ err = UNZ_BADZIPFILE;
452
+ /* size of the central directory */
453
+ if (unzReadUInt64(&us.z_filefunc, us.filestream, &us.size_central_dir) != UNZ_OK)
454
+ err = UNZ_ERRNO;
455
+ /* offset of start of central directory with respect to the starting disk number */
456
+ if (unzReadUInt64(&us.z_filefunc, us.filestream, &us.offset_central_dir) != UNZ_OK)
457
+ err = UNZ_ERRNO;
458
+ }
459
+ else if ((us.size_central_dir == UINT16_MAX) || (us.offset_central_dir == UINT32_MAX))
460
+ err = UNZ_BADZIPFILE;
461
+ }
462
+ }
463
+ else
464
+ err = UNZ_ERRNO;
465
+
466
+ if ((err == UNZ_OK) && (central_pos < us.offset_central_dir + us.size_central_dir))
467
+ err = UNZ_BADZIPFILE;
468
+
469
+ if (err != UNZ_OK)
470
+ {
471
+ ZCLOSE64(us.z_filefunc, us.filestream);
472
+ return NULL;
473
+ }
474
+
475
+ if (us.gi.number_disk_with_CD == 0)
476
+ {
477
+ /* If there is only one disk open another stream so we don't have to seek between the CD
478
+ and the file headers constantly */
479
+ filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
480
+ if (filestream != NULL)
481
+ us.filestream = filestream;
482
+ }
483
+
484
+ /* Hack for zip files that have no respect for zip64
485
+ if ((central_pos > 0xffffffff) && (us.offset_central_dir < 0xffffffff))
486
+ us.offset_central_dir = central_pos - us.size_central_dir;*/
487
+
488
+ us.byte_before_the_zipfile = central_pos - (us.offset_central_dir + us.size_central_dir);
489
+ us.central_pos = central_pos;
490
+
491
+ s = (unz64_internal*)ALLOC(sizeof(unz64_internal));
492
+ if (s != NULL)
493
+ {
494
+ *s = us;
495
+ if (err64 != UNZ_OK)
496
+ // workaround incorrect count #184
497
+ s->gi.number_entry = unzCountEntries(s);
498
+
499
+ unzGoToFirstFile((unzFile)s);
500
+ }
501
+ return (unzFile)s;
502
+ }
503
+
504
+ extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc32_def)
505
+ {
506
+ if (pzlib_filefunc32_def != NULL)
507
+ {
508
+ zlib_filefunc64_32_def zlib_filefunc64_32_def_fill = { 0 };
509
+ fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill, pzlib_filefunc32_def);
510
+ return unzOpenInternal(path, &zlib_filefunc64_32_def_fill);
511
+ }
512
+ return unzOpenInternal(path, NULL);
513
+ }
514
+
515
+ extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
516
+ {
517
+ if (pzlib_filefunc_def != NULL)
518
+ {
519
+ zlib_filefunc64_32_def zlib_filefunc64_32_def_fill = { 0 };
520
+ zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
521
+ return unzOpenInternal(path, &zlib_filefunc64_32_def_fill);
522
+ }
523
+ return unzOpenInternal(path, NULL);
524
+ }
525
+
526
+ extern unzFile ZEXPORT unzOpen(const char *path)
527
+ {
528
+ return unzOpenInternal(path, NULL);
529
+ }
530
+
531
+ extern unzFile ZEXPORT unzOpen64(const void *path)
532
+ {
533
+ return unzOpenInternal(path, NULL);
534
+ }
535
+
536
+ extern int ZEXPORT unzClose(unzFile file)
537
+ {
538
+ unz64_internal *s;
539
+ if (file == NULL)
540
+ return UNZ_PARAMERROR;
541
+ s = (unz64_internal*)file;
542
+
543
+ if (s->pfile_in_zip_read != NULL)
544
+ unzCloseCurrentFile(file);
545
+
546
+ if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD))
547
+ ZCLOSE64(s->z_filefunc, s->filestream);
548
+ if (s->filestream_with_CD != NULL)
549
+ ZCLOSE64(s->z_filefunc, s->filestream_with_CD);
550
+
551
+ s->filestream = NULL;
552
+ s->filestream_with_CD = NULL;
553
+ TRYFREE(s);
554
+ return UNZ_OK;
555
+ }
556
+
557
+ /* Goto to the next available disk for spanned archives */
558
+ static int unzGoToNextDisk(unzFile file)
559
+ {
560
+ unz64_internal *s;
561
+ uint32_t number_disk_next = 0;
562
+
563
+ s = (unz64_internal*)file;
564
+ if (s == NULL)
565
+ return UNZ_PARAMERROR;
566
+ number_disk_next = s->number_disk;
567
+
568
+ if ((s->pfile_in_zip_read != NULL) && (s->pfile_in_zip_read->rest_read_uncompressed > 0))
569
+ /* We are currently reading a file and we need the next sequential disk */
570
+ number_disk_next += 1;
571
+ else
572
+ /* Goto the disk for the current file */
573
+ number_disk_next = s->cur_file_info.disk_num_start;
574
+
575
+ if (number_disk_next != s->number_disk)
576
+ {
577
+ /* Switch disks */
578
+ if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD))
579
+ ZCLOSE64(s->z_filefunc, s->filestream);
580
+
581
+ if (number_disk_next == s->gi.number_disk_with_CD)
582
+ {
583
+ s->filestream = s->filestream_with_CD;
584
+ }
585
+ else
586
+ {
587
+ s->filestream = ZOPENDISK64(s->z_filefunc, s->filestream_with_CD, number_disk_next,
588
+ ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
589
+ }
590
+
591
+ if (s->filestream == NULL)
592
+ return UNZ_ERRNO;
593
+
594
+ s->number_disk = number_disk_next;
595
+ }
596
+
597
+ return UNZ_OK;
598
+ }
599
+
600
+ extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
601
+ {
602
+ unz64_internal *s = NULL;
603
+ if (file == NULL)
604
+ return UNZ_PARAMERROR;
605
+ s = (unz64_internal*)file;
606
+
607
+ pglobal_info32->number_entry = (uint32_t)s->gi.number_entry;
608
+ pglobal_info32->size_comment = s->gi.size_comment;
609
+ pglobal_info32->number_disk_with_CD = s->gi.number_disk_with_CD;
610
+ return UNZ_OK;
611
+ }
612
+
613
+ extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
614
+ {
615
+ unz64_internal *s = NULL;
616
+ if (file == NULL)
617
+ return UNZ_PARAMERROR;
618
+ s = (unz64_internal*)file;
619
+ *pglobal_info = s->gi;
620
+ return UNZ_OK;
621
+ }
622
+
623
+ extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
624
+ {
625
+ unz64_internal *s = NULL;
626
+ uint16_t bytes_to_read = comment_size;
627
+ if (file == NULL)
628
+ return (int)UNZ_PARAMERROR;
629
+ s = (unz64_internal*)file;
630
+
631
+ if (bytes_to_read > s->gi.size_comment)
632
+ bytes_to_read = s->gi.size_comment;
633
+
634
+ if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, s->central_pos + 22, ZLIB_FILEFUNC_SEEK_SET) != 0)
635
+ return UNZ_ERRNO;
636
+
637
+ if (bytes_to_read > 0)
638
+ {
639
+ *comment = 0;
640
+ if (ZREAD64(s->z_filefunc, s->filestream_with_CD, comment, bytes_to_read) != bytes_to_read)
641
+ return UNZ_ERRNO;
642
+ }
643
+
644
+ if ((comment != NULL) && (comment_size > s->gi.size_comment))
645
+ *(comment + s->gi.size_comment) = 0;
646
+
647
+ return (int)bytes_to_read;
648
+ }
649
+
650
+ static int unzGetCurrentFileInfoField(unzFile file, uint32_t *seek, void *field, uint16_t field_size, uint16_t size_file_field, int null_terminated_field)
651
+ {
652
+ unz64_internal *s = NULL;
653
+ uint32_t bytes_to_read = 0;
654
+ int err = UNZ_OK;
655
+
656
+ if (file == NULL)
657
+ return (int)UNZ_PARAMERROR;
658
+ s = (unz64_internal*)file;
659
+
660
+ /* Read field */
661
+ if (field != NULL)
662
+ {
663
+ if (size_file_field < field_size)
664
+ {
665
+ if (null_terminated_field)
666
+ *((char *)field+size_file_field) = 0;
667
+
668
+ bytes_to_read = size_file_field;
669
+ }
670
+ else
671
+ bytes_to_read = field_size;
672
+
673
+ if (*seek != 0)
674
+ {
675
+ if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, *seek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
676
+ *seek = 0;
677
+ else
678
+ err = UNZ_ERRNO;
679
+ }
680
+
681
+ if ((size_file_field > 0) && (field_size > 0))
682
+ {
683
+ if (ZREAD64(s->z_filefunc, s->filestream_with_CD, field, bytes_to_read) != bytes_to_read)
684
+ err = UNZ_ERRNO;
685
+ }
686
+ *seek += size_file_field - bytes_to_read;
687
+ }
688
+ else
689
+ {
690
+ *seek += size_file_field;
691
+ }
692
+
693
+ return err;
694
+ }
695
+
696
+ /* Get info about the current file in the zipfile, with internal only info */
697
+ static int unzGetCurrentFileInfoInternal(unzFile file, unz_file_info64 *pfile_info,
698
+ unz_file_info64_internal *pfile_info_internal, char *filename, uint16_t filename_size, void *extrafield,
699
+ uint16_t extrafield_size, char *comment, uint16_t comment_size)
700
+ {
701
+ unz64_internal *s = NULL;
702
+ unz_file_info64 file_info;
703
+ unz_file_info64_internal file_info_internal;
704
+ uint32_t magic = 0;
705
+ uint64_t current_pos = 0;
706
+ uint32_t seek = 0;
707
+ uint32_t extra_pos = 0;
708
+ uint16_t extra_header_id = 0;
709
+ uint16_t extra_data_size = 0;
710
+ uint16_t value16 = 0;
711
+ uint32_t value32 = 0;
712
+ uint64_t value64 = 0;
713
+ int err = UNZ_OK;
714
+
715
+ if (file == NULL)
716
+ return UNZ_PARAMERROR;
717
+ s = (unz64_internal*)file;
718
+
719
+ if (ZSEEK64(s->z_filefunc, s->filestream_with_CD,
720
+ s->pos_in_central_dir + s->byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
721
+ err = UNZ_ERRNO;
722
+
723
+ /* Check the magic */
724
+ if (err == UNZ_OK)
725
+ {
726
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &magic) != UNZ_OK)
727
+ err = UNZ_ERRNO;
728
+ else if (magic != CENTRALHEADERMAGIC)
729
+ err = UNZ_BADZIPFILE;
730
+ }
731
+
732
+ /* Read central directory header */
733
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.version) != UNZ_OK)
734
+ err = UNZ_ERRNO;
735
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.version_needed) != UNZ_OK)
736
+ err = UNZ_ERRNO;
737
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.flag) != UNZ_OK)
738
+ err = UNZ_ERRNO;
739
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.compression_method) != UNZ_OK)
740
+ err = UNZ_ERRNO;
741
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.dos_date) != UNZ_OK)
742
+ err = UNZ_ERRNO;
743
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.crc) != UNZ_OK)
744
+ err = UNZ_ERRNO;
745
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &value32) != UNZ_OK)
746
+ err = UNZ_ERRNO;
747
+ file_info.compressed_size = value32;
748
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &value32) != UNZ_OK)
749
+ err = UNZ_ERRNO;
750
+ file_info.uncompressed_size = value32;
751
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.size_filename) != UNZ_OK)
752
+ err = UNZ_ERRNO;
753
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.size_file_extra) != UNZ_OK)
754
+ err = UNZ_ERRNO;
755
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.size_file_comment) != UNZ_OK)
756
+ err = UNZ_ERRNO;
757
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &value16) != UNZ_OK)
758
+ err = UNZ_ERRNO;
759
+ file_info.disk_num_start = value16;
760
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &file_info.internal_fa) != UNZ_OK)
761
+ err = UNZ_ERRNO;
762
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.external_fa) != UNZ_OK)
763
+ err = UNZ_ERRNO;
764
+ /* Relative offset of local header */
765
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &value32) != UNZ_OK)
766
+ err = UNZ_ERRNO;
767
+
768
+ file_info.size_file_extra_internal = 0;
769
+ file_info.disk_offset = value32;
770
+ file_info_internal.offset_curfile = value32;
771
+ #ifdef HAVE_AES
772
+ file_info_internal.aes_compression_method = 0;
773
+ file_info_internal.aes_encryption_mode = 0;
774
+ file_info_internal.aes_version = 0;
775
+ #endif
776
+
777
+ if (err == UNZ_OK)
778
+ err = unzGetCurrentFileInfoField(file, &seek, filename, filename_size, file_info.size_filename, 1);
779
+
780
+ /* Read extrafield */
781
+ if (err == UNZ_OK)
782
+ err = unzGetCurrentFileInfoField(file, &seek, extrafield, extrafield_size, file_info.size_file_extra, 0);
783
+
784
+ if ((err == UNZ_OK) && (file_info.size_file_extra != 0))
785
+ {
786
+ if (seek != 0)
787
+ {
788
+ if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, seek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
789
+ seek = 0;
790
+ else
791
+ err = UNZ_ERRNO;
792
+ }
793
+
794
+ /* We are going to parse the extra field so we need to move back */
795
+ current_pos = ZTELL64(s->z_filefunc, s->filestream_with_CD);
796
+ if (current_pos < file_info.size_file_extra)
797
+ err = UNZ_ERRNO;
798
+ current_pos -= file_info.size_file_extra;
799
+ if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, current_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
800
+ err = UNZ_ERRNO;
801
+
802
+ while ((err != UNZ_ERRNO) && (extra_pos < file_info.size_file_extra))
803
+ {
804
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &extra_header_id) != UNZ_OK)
805
+ err = UNZ_ERRNO;
806
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &extra_data_size) != UNZ_OK)
807
+ err = UNZ_ERRNO;
808
+
809
+ /* ZIP64 extra fields */
810
+ if (extra_header_id == 0x0001)
811
+ {
812
+ /* Subtract size of ZIP64 field, since ZIP64 is handled internally */
813
+ file_info.size_file_extra_internal += 2 + 2 + extra_data_size;
814
+
815
+ if (file_info.uncompressed_size == UINT32_MAX)
816
+ {
817
+ if (unzReadUInt64(&s->z_filefunc, s->filestream_with_CD, &file_info.uncompressed_size) != UNZ_OK)
818
+ err = UNZ_ERRNO;
819
+ }
820
+ if (file_info.compressed_size == UINT32_MAX)
821
+ {
822
+ if (unzReadUInt64(&s->z_filefunc, s->filestream_with_CD, &file_info.compressed_size) != UNZ_OK)
823
+ err = UNZ_ERRNO;
824
+ }
825
+ if (file_info_internal.offset_curfile == UINT32_MAX)
826
+ {
827
+ /* Relative Header offset */
828
+ if (unzReadUInt64(&s->z_filefunc, s->filestream_with_CD, &value64) != UNZ_OK)
829
+ err = UNZ_ERRNO;
830
+ file_info_internal.offset_curfile = value64;
831
+ file_info.disk_offset = value64;
832
+ }
833
+ if (file_info.disk_num_start == UINT32_MAX)
834
+ {
835
+ /* Disk Start Number */
836
+ if (unzReadUInt32(&s->z_filefunc, s->filestream_with_CD, &file_info.disk_num_start) != UNZ_OK)
837
+ err = UNZ_ERRNO;
838
+ }
839
+ }
840
+ #ifdef HAVE_AES
841
+ /* AES header */
842
+ else if (extra_header_id == 0x9901)
843
+ {
844
+ uint8_t value8 = 0;
845
+
846
+ /* Subtract size of AES field, since AES is handled internally */
847
+ file_info.size_file_extra_internal += 2 + 2 + extra_data_size;
848
+
849
+ /* Verify version info */
850
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &value16) != UNZ_OK)
851
+ err = UNZ_ERRNO;
852
+ /* Support AE-1 and AE-2 */
853
+ if (value16 != 1 && value16 != 2)
854
+ err = UNZ_ERRNO;
855
+ file_info_internal.aes_version = value16;
856
+ if (unzReadUInt8(&s->z_filefunc, s->filestream_with_CD, &value8) != UNZ_OK)
857
+ err = UNZ_ERRNO;
858
+ if ((char)value8 != 'A')
859
+ err = UNZ_ERRNO;
860
+ if (unzReadUInt8(&s->z_filefunc, s->filestream_with_CD, &value8) != UNZ_OK)
861
+ err = UNZ_ERRNO;
862
+ if ((char)value8 != 'E')
863
+ err = UNZ_ERRNO;
864
+ /* Get AES encryption strength and actual compression method */
865
+ if (unzReadUInt8(&s->z_filefunc, s->filestream_with_CD, &value8) != UNZ_OK)
866
+ err = UNZ_ERRNO;
867
+ file_info_internal.aes_encryption_mode = value8;
868
+ if (unzReadUInt16(&s->z_filefunc, s->filestream_with_CD, &value16) != UNZ_OK)
869
+ err = UNZ_ERRNO;
870
+ file_info_internal.aes_compression_method = value16;
871
+ }
872
+ #endif
873
+ else
874
+ {
875
+ if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, extra_data_size, ZLIB_FILEFUNC_SEEK_CUR) != 0)
876
+ err = UNZ_ERRNO;
877
+ }
878
+
879
+ extra_pos += 2 + 2 + extra_data_size;
880
+ }
881
+ }
882
+
883
+ if (file_info.disk_num_start == s->gi.number_disk_with_CD)
884
+ file_info_internal.byte_before_the_zipfile = s->byte_before_the_zipfile;
885
+ else
886
+ file_info_internal.byte_before_the_zipfile = 0;
887
+
888
+ if (err == UNZ_OK)
889
+ err = unzGetCurrentFileInfoField(file, &seek, comment, comment_size, file_info.size_file_comment, 1);
890
+
891
+ if ((err == UNZ_OK) && (pfile_info != NULL))
892
+ *pfile_info = file_info;
893
+ if ((err == UNZ_OK) && (pfile_info_internal != NULL))
894
+ *pfile_info_internal = file_info_internal;
895
+
896
+ return err;
897
+ }
898
+
899
+ extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
900
+ uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
901
+ {
902
+ unz_file_info64 file_info64;
903
+ int err = UNZ_OK;
904
+
905
+ err = unzGetCurrentFileInfoInternal(file, &file_info64, NULL, filename, filename_size,
906
+ extrafield, extrafield_size, comment, comment_size);
907
+
908
+ if ((err == UNZ_OK) && (pfile_info != NULL))
909
+ {
910
+ pfile_info->version = file_info64.version;
911
+ pfile_info->version_needed = file_info64.version_needed;
912
+ pfile_info->flag = file_info64.flag;
913
+ pfile_info->compression_method = file_info64.compression_method;
914
+ pfile_info->dos_date = file_info64.dos_date;
915
+ pfile_info->crc = file_info64.crc;
916
+
917
+ pfile_info->size_filename = file_info64.size_filename;
918
+ pfile_info->size_file_extra = file_info64.size_file_extra - file_info64.size_file_extra_internal;
919
+ pfile_info->size_file_comment = file_info64.size_file_comment;
920
+
921
+ pfile_info->disk_num_start = (uint16_t)file_info64.disk_num_start;
922
+ pfile_info->internal_fa = file_info64.internal_fa;
923
+ pfile_info->external_fa = file_info64.external_fa;
924
+
925
+ pfile_info->compressed_size = (uint32_t)file_info64.compressed_size;
926
+ pfile_info->uncompressed_size = (uint32_t)file_info64.uncompressed_size;
927
+ }
928
+ return err;
929
+ }
930
+
931
+ extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
932
+ uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
933
+ {
934
+ return unzGetCurrentFileInfoInternal(file, pfile_info, NULL, filename, filename_size,
935
+ extrafield, extrafield_size, comment, comment_size);
936
+ }
937
+
938
+ /* Read the local header of the current zipfile. Check the coherency of the local header and info in the
939
+ end of central directory about this file store in *piSizeVar the size of extra info in local header
940
+ (filename and size of extra field data) */
941
+ static int unzCheckCurrentFileCoherencyHeader(unz64_internal *s, uint32_t *psize_variable, uint64_t *poffset_local_extrafield,
942
+ uint16_t *psize_local_extrafield)
943
+ {
944
+ uint32_t magic = 0;
945
+ uint16_t value16 = 0;
946
+ uint32_t value32 = 0;
947
+ uint32_t flags = 0;
948
+ uint16_t size_filename = 0;
949
+ uint16_t size_extra_field = 0;
950
+ uint16_t compression_method = 0;
951
+ int err = UNZ_OK;
952
+
953
+ if (psize_variable == NULL)
954
+ return UNZ_PARAMERROR;
955
+ *psize_variable = 0;
956
+ if (poffset_local_extrafield == NULL)
957
+ return UNZ_PARAMERROR;
958
+ *poffset_local_extrafield = 0;
959
+ if (psize_local_extrafield == NULL)
960
+ return UNZ_PARAMERROR;
961
+ *psize_local_extrafield = 0;
962
+
963
+ err = unzGoToNextDisk((unzFile)s);
964
+ if (err != UNZ_OK)
965
+ return err;
966
+
967
+ if (ZSEEK64(s->z_filefunc, s->filestream, s->cur_file_info_internal.offset_curfile +
968
+ s->cur_file_info_internal.byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
969
+ return UNZ_ERRNO;
970
+
971
+ if (err == UNZ_OK)
972
+ {
973
+ if (unzReadUInt32(&s->z_filefunc, s->filestream, &magic) != UNZ_OK)
974
+ err = UNZ_ERRNO;
975
+ else if (magic != LOCALHEADERMAGIC)
976
+ err = UNZ_BADZIPFILE;
977
+ }
978
+
979
+ if (unzReadUInt16(&s->z_filefunc, s->filestream, &value16) != UNZ_OK)
980
+ err = UNZ_ERRNO;
981
+ if (unzReadUInt16(&s->z_filefunc, s->filestream, &value16) != UNZ_OK)
982
+ err = UNZ_ERRNO;
983
+ flags = value16;
984
+ if (unzReadUInt16(&s->z_filefunc, s->filestream, &value16) != UNZ_OK)
985
+ err = UNZ_ERRNO;
986
+ else if ((err == UNZ_OK) && (value16 != s->cur_file_info.compression_method))
987
+ err = UNZ_BADZIPFILE;
988
+
989
+ compression_method = s->cur_file_info.compression_method;
990
+ #ifdef HAVE_AES
991
+ if (compression_method == AES_METHOD)
992
+ compression_method = s->cur_file_info_internal.aes_compression_method;
993
+ #endif
994
+
995
+ if ((err == UNZ_OK) && (compression_method != 0) && (compression_method != Z_DEFLATED))
996
+ {
997
+ #ifdef HAVE_BZIP2
998
+ if (compression_method != Z_BZIP2ED)
999
+ #endif
1000
+ err = UNZ_BADZIPFILE;
1001
+ }
1002
+
1003
+ if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* date/time */
1004
+ err = UNZ_ERRNO;
1005
+ if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* crc */
1006
+ err = UNZ_ERRNO;
1007
+ else if ((err == UNZ_OK) && (value32 != s->cur_file_info.crc) && ((flags & 8) == 0))
1008
+ err = UNZ_BADZIPFILE;
1009
+ if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* size compr */
1010
+ err = UNZ_ERRNO;
1011
+ else if ((value32 != UINT32_MAX) && (err == UNZ_OK) && (value32 != s->cur_file_info.compressed_size) && ((flags & 8) == 0))
1012
+ err = UNZ_BADZIPFILE;
1013
+ if (unzReadUInt32(&s->z_filefunc, s->filestream, &value32) != UNZ_OK) /* size uncompr */
1014
+ err = UNZ_ERRNO;
1015
+ else if ((value32 != UINT32_MAX) && (err == UNZ_OK) && (value32 != s->cur_file_info.uncompressed_size) && ((flags & 8) == 0))
1016
+ err = UNZ_BADZIPFILE;
1017
+ if (unzReadUInt16(&s->z_filefunc, s->filestream, &size_filename) != UNZ_OK)
1018
+ err = UNZ_ERRNO;
1019
+
1020
+ *psize_variable += size_filename;
1021
+
1022
+ if (unzReadUInt16(&s->z_filefunc, s->filestream, &size_extra_field) != UNZ_OK)
1023
+ err = UNZ_ERRNO;
1024
+
1025
+ *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + size_filename;
1026
+ *psize_local_extrafield = size_extra_field;
1027
+ *psize_variable += size_extra_field;
1028
+
1029
+ return err;
1030
+ }
1031
+
1032
+ extern uint64_t ZEXPORT unzCountEntries(const unzFile file)
1033
+ {
1034
+ if (file == NULL)
1035
+ return 0;
1036
+
1037
+ unz64_internal s = *(unz64_internal*)file;
1038
+
1039
+ s.pos_in_central_dir = s.offset_central_dir;
1040
+ s.num_file = 0;
1041
+ while (UNZ_OK == unzGetCurrentFileInfoInternal(&s,
1042
+ &s.cur_file_info,
1043
+ &s.cur_file_info_internal,
1044
+ NULL, 0, NULL, 0, NULL, 0))
1045
+ {
1046
+ s.pos_in_central_dir += SIZECENTRALDIRITEM
1047
+ + s.cur_file_info.size_filename
1048
+ + s.cur_file_info.size_file_extra
1049
+ + s.cur_file_info.size_file_comment;
1050
+ s.num_file += 1;
1051
+ }
1052
+ return s.num_file;
1053
+ }
1054
+
1055
+ /*
1056
+ Open for reading data the current file in the zipfile.
1057
+ If there is no error and the file is opened, the return value is UNZ_OK.
1058
+ */
1059
+ extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
1060
+ {
1061
+ unz64_internal *s = NULL;
1062
+ file_in_zip64_read_info_s *pfile_in_zip_read_info = NULL;
1063
+ uint16_t compression_method = 0;
1064
+ uint64_t offset_local_extrafield = 0;
1065
+ uint16_t size_local_extrafield = 0;
1066
+ uint32_t size_variable = 0;
1067
+ int err = UNZ_OK;
1068
+ #ifndef NOUNCRYPT
1069
+ char source[12];
1070
+ #else
1071
+ if (password != NULL)
1072
+ return UNZ_PARAMERROR;
1073
+ #endif
1074
+ if (file == NULL)
1075
+ return UNZ_PARAMERROR;
1076
+ s = (unz64_internal*)file;
1077
+ if (!s->current_file_ok)
1078
+ return UNZ_PARAMERROR;
1079
+
1080
+ if (s->pfile_in_zip_read != NULL)
1081
+ unzCloseCurrentFile(file);
1082
+
1083
+ if (unzCheckCurrentFileCoherencyHeader(s, &size_variable, &offset_local_extrafield, &size_local_extrafield) != UNZ_OK)
1084
+ return UNZ_BADZIPFILE;
1085
+
1086
+ compression_method = s->cur_file_info.compression_method;
1087
+ #ifdef HAVE_AES
1088
+ if (compression_method == AES_METHOD)
1089
+ {
1090
+ compression_method = s->cur_file_info_internal.aes_compression_method;
1091
+ if (password == NULL)
1092
+ {
1093
+ return UNZ_PARAMERROR;
1094
+ }
1095
+ }
1096
+ #endif
1097
+
1098
+ if (method != NULL)
1099
+ *method = compression_method;
1100
+
1101
+ if (level != NULL)
1102
+ {
1103
+ *level = 6;
1104
+ switch (s->cur_file_info.flag & 0x06)
1105
+ {
1106
+ case 6 : *level = 1; break;
1107
+ case 4 : *level = 2; break;
1108
+ case 2 : *level = 9; break;
1109
+ }
1110
+ }
1111
+
1112
+ if ((compression_method != 0) && (compression_method != Z_DEFLATED))
1113
+ {
1114
+ #ifdef HAVE_BZIP2
1115
+ if (compression_method != Z_BZIP2ED)
1116
+ #endif
1117
+ {
1118
+ return UNZ_BADZIPFILE;
1119
+ }
1120
+ }
1121
+
1122
+ pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
1123
+ if (pfile_in_zip_read_info == NULL)
1124
+ return UNZ_INTERNALERROR;
1125
+
1126
+ pfile_in_zip_read_info->read_buffer = (uint8_t*)ALLOC(UNZ_BUFSIZE);
1127
+ if (pfile_in_zip_read_info->read_buffer == NULL)
1128
+ {
1129
+ TRYFREE(pfile_in_zip_read_info);
1130
+ return UNZ_INTERNALERROR;
1131
+ }
1132
+
1133
+ pfile_in_zip_read_info->stream_initialised = 0;
1134
+
1135
+ pfile_in_zip_read_info->filestream = s->filestream;
1136
+ pfile_in_zip_read_info->z_filefunc = s->z_filefunc;
1137
+
1138
+ pfile_in_zip_read_info->raw = raw;
1139
+ pfile_in_zip_read_info->crc32 = 0;
1140
+ pfile_in_zip_read_info->crc32_expected = s->cur_file_info.crc;
1141
+ pfile_in_zip_read_info->total_out_64 = 0;
1142
+ pfile_in_zip_read_info->compression_method = compression_method;
1143
+
1144
+ pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
1145
+ pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
1146
+ pfile_in_zip_read_info->pos_local_extrafield = 0;
1147
+
1148
+ pfile_in_zip_read_info->rest_read_compressed = s->cur_file_info.compressed_size;
1149
+ pfile_in_zip_read_info->rest_read_uncompressed = s->cur_file_info.uncompressed_size;
1150
+ pfile_in_zip_read_info->byte_before_the_zipfile = 0;
1151
+
1152
+ if (s->number_disk == s->gi.number_disk_with_CD)
1153
+ pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile;
1154
+
1155
+ pfile_in_zip_read_info->pos_in_zipfile = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + size_variable;
1156
+
1157
+ pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
1158
+ pfile_in_zip_read_info->stream.zfree = (free_func)0;
1159
+ pfile_in_zip_read_info->stream.opaque = (voidpf)s;
1160
+ pfile_in_zip_read_info->stream.total_out = 0;
1161
+ pfile_in_zip_read_info->stream.total_in = 0;
1162
+ pfile_in_zip_read_info->stream.next_in = NULL;
1163
+ pfile_in_zip_read_info->stream.avail_in = 0;
1164
+
1165
+ if (!raw)
1166
+ {
1167
+ if (compression_method == Z_BZIP2ED)
1168
+ {
1169
+ #ifdef HAVE_BZIP2
1170
+ pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
1171
+ pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
1172
+ pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
1173
+ pfile_in_zip_read_info->bstream.state = (voidpf)0;
1174
+
1175
+ err = BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
1176
+ if (err == Z_OK)
1177
+ {
1178
+ pfile_in_zip_read_info->stream_initialised = Z_BZIP2ED;
1179
+ }
1180
+ else
1181
+ {
1182
+ TRYFREE(pfile_in_zip_read_info);
1183
+ return err;
1184
+ }
1185
+ #else
1186
+ pfile_in_zip_read_info->raw = 1;
1187
+ #endif
1188
+ }
1189
+ else if (compression_method == Z_DEFLATED)
1190
+ {
1191
+ #ifdef HAVE_APPLE_COMPRESSION
1192
+ err = compression_stream_init(&pfile_in_zip_read_info->astream, COMPRESSION_STREAM_DECODE, COMPRESSION_ZLIB);
1193
+ if (err == COMPRESSION_STATUS_ERROR)
1194
+ err = UNZ_INTERNALERROR;
1195
+ else
1196
+ err = Z_OK;
1197
+ #else
1198
+ err = inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
1199
+ #endif
1200
+ if (err == Z_OK)
1201
+ {
1202
+ pfile_in_zip_read_info->stream_initialised = Z_DEFLATED;
1203
+ }
1204
+ else
1205
+ {
1206
+ TRYFREE(pfile_in_zip_read_info);
1207
+ return err;
1208
+ }
1209
+ /* windowBits is passed < 0 to tell that there is no zlib header.
1210
+ * Note that in this case inflate *requires* an extra "dummy" byte
1211
+ * after the compressed stream in order to complete decompression and
1212
+ * return Z_STREAM_END.
1213
+ * In unzip, i don't wait absolutely Z_STREAM_END because I known the
1214
+ * size of both compressed and uncompressed data
1215
+ */
1216
+ }
1217
+ }
1218
+
1219
+ s->pfile_in_zip_read = pfile_in_zip_read_info;
1220
+
1221
+ #ifndef NOUNCRYPT
1222
+ s->pcrc_32_tab = NULL;
1223
+
1224
+ if ((password != NULL) && ((s->cur_file_info.flag & 1) != 0))
1225
+ {
1226
+ if (ZSEEK64(s->z_filefunc, s->filestream,
1227
+ s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile,
1228
+ ZLIB_FILEFUNC_SEEK_SET) != 0)
1229
+ return UNZ_INTERNALERROR;
1230
+ #ifdef HAVE_AES
1231
+ if (s->cur_file_info.compression_method == AES_METHOD)
1232
+ {
1233
+ unsigned char passverify_archive[AES_PWVERIFYSIZE];
1234
+ unsigned char passverify_password[AES_PWVERIFYSIZE];
1235
+ unsigned char salt_value[AES_MAXSALTLENGTH];
1236
+ uint32_t salt_length = 0;
1237
+
1238
+ if ((s->cur_file_info_internal.aes_encryption_mode < 1) ||
1239
+ (s->cur_file_info_internal.aes_encryption_mode > 3))
1240
+ return UNZ_INTERNALERROR;
1241
+
1242
+ salt_length = SALT_LENGTH(s->cur_file_info_internal.aes_encryption_mode);
1243
+
1244
+ if (ZREAD64(s->z_filefunc, s->filestream, salt_value, salt_length) != salt_length)
1245
+ return UNZ_INTERNALERROR;
1246
+ if (ZREAD64(s->z_filefunc, s->filestream, passverify_archive, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
1247
+ return UNZ_INTERNALERROR;
1248
+
1249
+ fcrypt_init(s->cur_file_info_internal.aes_encryption_mode, (uint8_t *)password,
1250
+ (uint32_t)strlen(password), salt_value, passverify_password, &s->pfile_in_zip_read->aes_ctx);
1251
+
1252
+ if (memcmp(passverify_archive, passverify_password, AES_PWVERIFYSIZE) != 0)
1253
+ return UNZ_BADPASSWORD;
1254
+
1255
+ s->pfile_in_zip_read->rest_read_compressed -= salt_length + AES_PWVERIFYSIZE;
1256
+ s->pfile_in_zip_read->rest_read_compressed -= AES_AUTHCODESIZE;
1257
+
1258
+ s->pfile_in_zip_read->pos_in_zipfile += salt_length + AES_PWVERIFYSIZE;
1259
+ }
1260
+ else
1261
+ #endif
1262
+ {
1263
+ int i;
1264
+ s->pcrc_32_tab = (const z_crc_t*)get_crc_table();
1265
+ init_keys(password, s->keys, s->pcrc_32_tab);
1266
+
1267
+ if (ZREAD64(s->z_filefunc, s->filestream, source, 12) < 12)
1268
+ return UNZ_INTERNALERROR;
1269
+
1270
+ for (i = 0; i < 12; i++)
1271
+ zdecode(s->keys, s->pcrc_32_tab, source[i]);
1272
+ uint8_t expected = (s->cur_file_info.flag & (1 << 3)) ?
1273
+ s->cur_file_info.dos_date >> 8 :
1274
+ s->cur_file_info.crc >> 24;
1275
+ uint8_t actual = (uint8_t)source[11];
1276
+ if (expected != actual) {
1277
+ return UNZ_BADPASSWORD;
1278
+ }
1279
+
1280
+ s->pfile_in_zip_read->rest_read_compressed -= 12;
1281
+ s->pfile_in_zip_read->pos_in_zipfile += 12;
1282
+ }
1283
+ }
1284
+ #endif
1285
+
1286
+ return UNZ_OK;
1287
+ }
1288
+
1289
+ extern int ZEXPORT unzOpenCurrentFile(unzFile file)
1290
+ {
1291
+ return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
1292
+ }
1293
+
1294
+ extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
1295
+ {
1296
+ return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
1297
+ }
1298
+
1299
+ extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
1300
+ {
1301
+ return unzOpenCurrentFile3(file, method, level, raw, NULL);
1302
+ }
1303
+
1304
+ /* Read bytes from the current file.
1305
+ buf contain buffer where data must be copied
1306
+ len the size of buf.
1307
+
1308
+ return the number of byte copied if some bytes are copied
1309
+ return 0 if the end of file was reached
1310
+ return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
1311
+ extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, uint32_t len)
1312
+ {
1313
+ unz64_internal *s = NULL;
1314
+ uint32_t read = 0;
1315
+ int err = UNZ_OK;
1316
+
1317
+ if (file == NULL)
1318
+ return UNZ_PARAMERROR;
1319
+ s = (unz64_internal*)file;
1320
+
1321
+ if (s->pfile_in_zip_read == NULL)
1322
+ return UNZ_PARAMERROR;
1323
+ if (s->pfile_in_zip_read->read_buffer == NULL)
1324
+ return UNZ_END_OF_LIST_OF_FILE;
1325
+ if (len == 0)
1326
+ return 0;
1327
+ // avail_out is uInt, so uint32_t len might allow requesting a larger buffer than zlib can support
1328
+ if (len > UINT_MAX)
1329
+ return UNZ_PARAMERROR;
1330
+
1331
+ s->pfile_in_zip_read->stream.next_out = (uint8_t*)buf;
1332
+ s->pfile_in_zip_read->stream.avail_out = (uInt)len;
1333
+
1334
+ if ((s->pfile_in_zip_read->compression_method == 0) || (s->pfile_in_zip_read->raw))
1335
+ {
1336
+ if (len > s->pfile_in_zip_read->rest_read_compressed + s->pfile_in_zip_read->stream.avail_in)
1337
+ s->pfile_in_zip_read->stream.avail_out = (uInt)s->pfile_in_zip_read->rest_read_compressed +
1338
+ s->pfile_in_zip_read->stream.avail_in;
1339
+ }
1340
+
1341
+ do
1342
+ {
1343
+ if (s->pfile_in_zip_read->stream.avail_in == 0)
1344
+ {
1345
+ uint32_t bytes_to_read = UNZ_BUFSIZE;
1346
+ uint32_t bytes_not_read = 0;
1347
+ uint32_t bytes_read = 0;
1348
+ uint32_t total_bytes_read = 0;
1349
+
1350
+ if (s->pfile_in_zip_read->stream.next_in != NULL)
1351
+ bytes_not_read = (uint32_t)(s->pfile_in_zip_read->read_buffer + UNZ_BUFSIZE -
1352
+ s->pfile_in_zip_read->stream.next_in);
1353
+ bytes_to_read -= bytes_not_read;
1354
+ if (bytes_not_read > 0)
1355
+ memmove(s->pfile_in_zip_read->read_buffer, s->pfile_in_zip_read->stream.next_in, bytes_not_read);
1356
+ if (s->pfile_in_zip_read->rest_read_compressed < bytes_to_read)
1357
+ bytes_to_read = (uint32_t)s->pfile_in_zip_read->rest_read_compressed;
1358
+
1359
+ while (total_bytes_read != bytes_to_read)
1360
+ {
1361
+ if (ZSEEK64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream,
1362
+ s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile,
1363
+ ZLIB_FILEFUNC_SEEK_SET) != 0)
1364
+ return UNZ_ERRNO;
1365
+
1366
+ bytes_read = ZREAD64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream,
1367
+ s->pfile_in_zip_read->read_buffer + bytes_not_read + total_bytes_read,
1368
+ bytes_to_read - total_bytes_read);
1369
+
1370
+ total_bytes_read += bytes_read;
1371
+ s->pfile_in_zip_read->pos_in_zipfile += bytes_read;
1372
+
1373
+ if (bytes_read == 0)
1374
+ {
1375
+ if (ZERROR64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream))
1376
+ return UNZ_ERRNO;
1377
+
1378
+ err = unzGoToNextDisk(file);
1379
+ if (err != UNZ_OK)
1380
+ return err;
1381
+
1382
+ s->pfile_in_zip_read->pos_in_zipfile = 0;
1383
+ s->pfile_in_zip_read->filestream = s->filestream;
1384
+ }
1385
+ }
1386
+
1387
+ #ifndef NOUNCRYPT
1388
+ if ((s->cur_file_info.flag & 1) != 0)
1389
+ {
1390
+ #ifdef HAVE_AES
1391
+ if (s->cur_file_info.compression_method == AES_METHOD)
1392
+ {
1393
+ fcrypt_decrypt(s->pfile_in_zip_read->read_buffer, bytes_to_read, &s->pfile_in_zip_read->aes_ctx);
1394
+ }
1395
+ else
1396
+ #endif
1397
+ if (s->pcrc_32_tab != NULL)
1398
+ {
1399
+ uint32_t i = 0;
1400
+
1401
+ for (i = 0; i < total_bytes_read; i++)
1402
+ s->pfile_in_zip_read->read_buffer[i] =
1403
+ zdecode(s->keys, s->pcrc_32_tab, s->pfile_in_zip_read->read_buffer[i]);
1404
+ }
1405
+ }
1406
+ #endif
1407
+
1408
+ s->pfile_in_zip_read->rest_read_compressed -= total_bytes_read;
1409
+ s->pfile_in_zip_read->stream.next_in = (uint8_t*)s->pfile_in_zip_read->read_buffer;
1410
+ s->pfile_in_zip_read->stream.avail_in = (uInt)(bytes_not_read + total_bytes_read);
1411
+ }
1412
+
1413
+ if ((s->pfile_in_zip_read->compression_method == 0) || (s->pfile_in_zip_read->raw))
1414
+ {
1415
+ uint32_t i = 0;
1416
+ uint32_t copy = 0;
1417
+
1418
+ if ((s->pfile_in_zip_read->stream.avail_in == 0) &&
1419
+ (s->pfile_in_zip_read->rest_read_compressed == 0))
1420
+ return (read == 0) ? UNZ_EOF : read;
1421
+
1422
+ if (s->pfile_in_zip_read->stream.avail_out < s->pfile_in_zip_read->stream.avail_in)
1423
+ copy = s->pfile_in_zip_read->stream.avail_out;
1424
+ else
1425
+ copy = s->pfile_in_zip_read->stream.avail_in;
1426
+
1427
+ for (i = 0; i < copy; i++)
1428
+ *(s->pfile_in_zip_read->stream.next_out + i) =
1429
+ *(s->pfile_in_zip_read->stream.next_in + i);
1430
+
1431
+ s->pfile_in_zip_read->total_out_64 = s->pfile_in_zip_read->total_out_64 + copy;
1432
+ s->pfile_in_zip_read->rest_read_uncompressed -= copy;
1433
+ s->pfile_in_zip_read->crc32 = (uint32_t)crc32(s->pfile_in_zip_read->crc32,
1434
+ s->pfile_in_zip_read->stream.next_out, copy);
1435
+
1436
+ s->pfile_in_zip_read->stream.avail_in -= copy;
1437
+ s->pfile_in_zip_read->stream.avail_out -= copy;
1438
+ s->pfile_in_zip_read->stream.next_out += copy;
1439
+ s->pfile_in_zip_read->stream.next_in += copy;
1440
+ s->pfile_in_zip_read->stream.total_out += copy;
1441
+
1442
+ read += copy;
1443
+ }
1444
+ else if (s->pfile_in_zip_read->compression_method == Z_BZIP2ED)
1445
+ {
1446
+ #ifdef HAVE_BZIP2
1447
+ uint64_t total_out_before = 0;
1448
+ uint64_t total_out_after = 0;
1449
+ uint64_t out_bytes = 0;
1450
+ const uint8_t *buf_before = NULL;
1451
+
1452
+ s->pfile_in_zip_read->bstream.next_in = (char*)s->pfile_in_zip_read->stream.next_in;
1453
+ s->pfile_in_zip_read->bstream.avail_in = s->pfile_in_zip_read->stream.avail_in;
1454
+ s->pfile_in_zip_read->bstream.total_in_lo32 = (uint32_t)s->pfile_in_zip_read->stream.total_in;
1455
+ s->pfile_in_zip_read->bstream.total_in_hi32 = s->pfile_in_zip_read->stream.total_in >> 32;
1456
+
1457
+ s->pfile_in_zip_read->bstream.next_out = (char*)s->pfile_in_zip_read->stream.next_out;
1458
+ s->pfile_in_zip_read->bstream.avail_out = s->pfile_in_zip_read->stream.avail_out;
1459
+ s->pfile_in_zip_read->bstream.total_out_lo32 = (uint32_t)s->pfile_in_zip_read->stream.total_out;
1460
+ s->pfile_in_zip_read->bstream.total_out_hi32 = s->pfile_in_zip_read->stream.total_out >> 32;
1461
+
1462
+ total_out_before = s->pfile_in_zip_read->bstream.total_out_lo32 +
1463
+ (((uint32_t)s->pfile_in_zip_read->bstream.total_out_hi32) << 32);
1464
+ buf_before = (const uint8_t*)s->pfile_in_zip_read->bstream.next_out;
1465
+
1466
+ err = BZ2_bzDecompress(&s->pfile_in_zip_read->bstream);
1467
+
1468
+ total_out_after = s->pfile_in_zip_read->bstream.total_out_lo32 +
1469
+ (((uint32_t)s->pfile_in_zip_read->bstream.total_out_hi32) << 32);
1470
+
1471
+ out_bytes = total_out_after - total_out_before;
1472
+
1473
+ s->pfile_in_zip_read->total_out_64 = s->pfile_in_zip_read->total_out_64 + out_bytes;
1474
+ s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes;
1475
+ s->pfile_in_zip_read->crc32 = crc32(s->pfile_in_zip_read->crc32, buf_before, (uint32_t)out_bytes);
1476
+
1477
+ read += (uint32_t)out_bytes;
1478
+
1479
+ s->pfile_in_zip_read->stream.next_in = (uint8_t*)s->pfile_in_zip_read->bstream.next_in;
1480
+ s->pfile_in_zip_read->stream.avail_in = s->pfile_in_zip_read->bstream.avail_in;
1481
+ s->pfile_in_zip_read->stream.total_in = s->pfile_in_zip_read->bstream.total_in_lo32;
1482
+ s->pfile_in_zip_read->stream.next_out = (uint8_t*)s->pfile_in_zip_read->bstream.next_out;
1483
+ s->pfile_in_zip_read->stream.avail_out = s->pfile_in_zip_read->bstream.avail_out;
1484
+ s->pfile_in_zip_read->stream.total_out = s->pfile_in_zip_read->bstream.total_out_lo32;
1485
+
1486
+ if (err == BZ_STREAM_END)
1487
+ return (read == 0) ? UNZ_EOF : read;
1488
+ if (err != BZ_OK)
1489
+ break;
1490
+ #endif
1491
+ }
1492
+ #ifdef HAVE_APPLE_COMPRESSION
1493
+ else
1494
+ {
1495
+ uint64_t total_out_before = 0;
1496
+ uint64_t total_out_after = 0;
1497
+ uint64_t out_bytes = 0;
1498
+ const uint8_t *buf_before = NULL;
1499
+
1500
+ s->pfile_in_zip_read->astream.src_ptr = s->pfile_in_zip_read->stream.next_in;
1501
+ s->pfile_in_zip_read->astream.src_size = s->pfile_in_zip_read->stream.avail_in;
1502
+ s->pfile_in_zip_read->astream.dst_ptr = s->pfile_in_zip_read->stream.next_out;
1503
+ s->pfile_in_zip_read->astream.dst_size = len;
1504
+
1505
+ total_out_before = s->pfile_in_zip_read->stream.total_out;
1506
+ buf_before = s->pfile_in_zip_read->stream.next_out;
1507
+
1508
+ compression_status status;
1509
+ compression_stream_flags flags;
1510
+
1511
+ if (s->pfile_in_zip_read->stream.avail_in == 0)
1512
+ {
1513
+ flags = COMPRESSION_STREAM_FINALIZE;
1514
+ }
1515
+
1516
+ status = compression_stream_process(&s->pfile_in_zip_read->astream, flags);
1517
+
1518
+ total_out_after = len - s->pfile_in_zip_read->astream.dst_size;
1519
+ out_bytes = total_out_after - total_out_before;
1520
+
1521
+ s->pfile_in_zip_read->total_out_64 += out_bytes;
1522
+ s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes;
1523
+ s->pfile_in_zip_read->crc32 =
1524
+ crc32(s->pfile_in_zip_read->crc32, buf_before, (uint32_t)out_bytes);
1525
+
1526
+ read += (uint32_t)out_bytes;
1527
+
1528
+ s->pfile_in_zip_read->stream.next_in = s->pfile_in_zip_read->astream.src_ptr;
1529
+ s->pfile_in_zip_read->stream.avail_in = s->pfile_in_zip_read->astream.src_size;
1530
+ s->pfile_in_zip_read->stream.next_out = s->pfile_in_zip_read->astream.dst_ptr;
1531
+ s->pfile_in_zip_read->stream.avail_out = s->pfile_in_zip_read->astream.dst_size;
1532
+
1533
+ if (status == COMPRESSION_STATUS_END)
1534
+ return (read == 0) ? UNZ_EOF : read;
1535
+ if (status == COMPRESSION_STATUS_ERROR)
1536
+ return Z_DATA_ERROR;
1537
+ return read;
1538
+ }
1539
+ #else
1540
+ else
1541
+ {
1542
+ uint64_t total_out_before = 0;
1543
+ uint64_t total_out_after = 0;
1544
+ uint64_t out_bytes = 0;
1545
+ const uint8_t *buf_before = NULL;
1546
+ int flush = Z_SYNC_FLUSH;
1547
+
1548
+ total_out_before = s->pfile_in_zip_read->stream.total_out;
1549
+ buf_before = s->pfile_in_zip_read->stream.next_out;
1550
+
1551
+ /*
1552
+ if ((pfile_in_zip_read_info->rest_read_uncompressed ==
1553
+ pfile_in_zip_read_info->stream.avail_out) &&
1554
+ (pfile_in_zip_read_info->rest_read_compressed == 0))
1555
+ flush = Z_FINISH;
1556
+ */
1557
+ err = inflate(&s->pfile_in_zip_read->stream, flush);
1558
+
1559
+ if ((err >= 0) && (s->pfile_in_zip_read->stream.msg != NULL))
1560
+ err = Z_DATA_ERROR;
1561
+
1562
+ total_out_after = s->pfile_in_zip_read->stream.total_out;
1563
+ out_bytes = total_out_after - total_out_before;
1564
+
1565
+ s->pfile_in_zip_read->total_out_64 += out_bytes;
1566
+ s->pfile_in_zip_read->rest_read_uncompressed -= out_bytes;
1567
+ s->pfile_in_zip_read->crc32 =
1568
+ (uint32_t)crc32(s->pfile_in_zip_read->crc32, buf_before, (uint32_t)out_bytes);
1569
+
1570
+ read += (uint32_t)out_bytes;
1571
+
1572
+ if (err == Z_STREAM_END)
1573
+ return (read == 0) ? UNZ_EOF : read;
1574
+ if (err != Z_OK)
1575
+ break;
1576
+ }
1577
+ #endif
1578
+ }
1579
+ while (s->pfile_in_zip_read->stream.avail_out > 0);
1580
+
1581
+ if (err == Z_OK)
1582
+ return read;
1583
+ return err;
1584
+ }
1585
+
1586
+ extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, uint32_t len)
1587
+ {
1588
+ unz64_internal *s = NULL;
1589
+ uint64_t size_to_read = 0;
1590
+ uint32_t read_now = 0;
1591
+
1592
+ if (file == NULL)
1593
+ return UNZ_PARAMERROR;
1594
+ s = (unz64_internal*)file;
1595
+ if (s->pfile_in_zip_read == NULL)
1596
+ return UNZ_PARAMERROR;
1597
+
1598
+ size_to_read = s->pfile_in_zip_read->size_local_extrafield - s->pfile_in_zip_read->pos_local_extrafield;
1599
+
1600
+ if (buf == NULL)
1601
+ return (int)size_to_read;
1602
+
1603
+ if (len > size_to_read)
1604
+ read_now = (uint32_t)size_to_read;
1605
+ else
1606
+ read_now = len;
1607
+
1608
+ if (read_now == 0)
1609
+ return 0;
1610
+
1611
+ if (ZSEEK64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream,
1612
+ s->pfile_in_zip_read->offset_local_extrafield + s->pfile_in_zip_read->pos_local_extrafield,
1613
+ ZLIB_FILEFUNC_SEEK_SET) != 0)
1614
+ return UNZ_ERRNO;
1615
+
1616
+ if (ZREAD64(s->pfile_in_zip_read->z_filefunc, s->pfile_in_zip_read->filestream, buf, read_now) != read_now)
1617
+ return UNZ_ERRNO;
1618
+
1619
+ return (int)read_now;
1620
+ }
1621
+
1622
+ extern int ZEXPORT unzCloseCurrentFile(unzFile file)
1623
+ {
1624
+ unz64_internal *s = NULL;
1625
+ file_in_zip64_read_info_s *pfile_in_zip_read_info = NULL;
1626
+ int err = UNZ_OK;
1627
+
1628
+ if (file == NULL)
1629
+ return UNZ_PARAMERROR;
1630
+ s = (unz64_internal*)file;
1631
+ pfile_in_zip_read_info = s->pfile_in_zip_read;
1632
+ if (pfile_in_zip_read_info == NULL)
1633
+ return UNZ_PARAMERROR;
1634
+
1635
+ #ifdef HAVE_AES
1636
+ if (s->cur_file_info.compression_method == AES_METHOD)
1637
+ {
1638
+ unsigned char authcode[AES_AUTHCODESIZE];
1639
+ unsigned char rauthcode[AES_AUTHCODESIZE];
1640
+
1641
+ if (ZREAD64(s->z_filefunc, s->filestream, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
1642
+ return UNZ_ERRNO;
1643
+
1644
+ if (fcrypt_end(rauthcode, &s->pfile_in_zip_read->aes_ctx) != AES_AUTHCODESIZE)
1645
+ err = UNZ_CRCERROR;
1646
+ if (memcmp(authcode, rauthcode, AES_AUTHCODESIZE) != 0)
1647
+ err = UNZ_CRCERROR;
1648
+ }
1649
+ /* AES zip version AE-1 will expect a valid crc as well */
1650
+ if ((s->cur_file_info.compression_method != AES_METHOD) ||
1651
+ (s->cur_file_info_internal.aes_version == 0x0001))
1652
+ #endif
1653
+ {
1654
+ if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
1655
+ (!pfile_in_zip_read_info->raw))
1656
+ {
1657
+ if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_expected)
1658
+ err = UNZ_CRCERROR;
1659
+ }
1660
+ }
1661
+
1662
+ TRYFREE(pfile_in_zip_read_info->read_buffer);
1663
+ pfile_in_zip_read_info->read_buffer = NULL;
1664
+ if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
1665
+ {
1666
+ #ifdef HAVE_APPLE_COMPRESSION
1667
+ if (compression_stream_destroy)
1668
+ compression_stream_destroy(&pfile_in_zip_read_info->astream);
1669
+ #else
1670
+ inflateEnd(&pfile_in_zip_read_info->stream);
1671
+ #endif
1672
+
1673
+ }
1674
+ #ifdef HAVE_BZIP2
1675
+ else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
1676
+ BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
1677
+ #endif
1678
+
1679
+ pfile_in_zip_read_info->stream_initialised = 0;
1680
+ TRYFREE(pfile_in_zip_read_info);
1681
+
1682
+ s->pfile_in_zip_read = NULL;
1683
+
1684
+ return err;
1685
+ }
1686
+
1687
+ extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
1688
+ uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
1689
+ {
1690
+ unz64_internal *s = NULL;
1691
+ int err = UNZ_OK;
1692
+
1693
+ if (file == NULL)
1694
+ return UNZ_PARAMERROR;
1695
+ s = (unz64_internal*)file;
1696
+
1697
+ if (s->gi.number_entry == 0)
1698
+ return UNZ_END_OF_LIST_OF_FILE;
1699
+
1700
+ s->pos_in_central_dir = s->offset_central_dir;
1701
+ s->num_file = 0;
1702
+
1703
+ err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal,
1704
+ filename, filename_size, extrafield, extrafield_size, comment, comment_size);
1705
+
1706
+ s->current_file_ok = (err == UNZ_OK);
1707
+ if ((err == UNZ_OK) && (pfile_info != NULL))
1708
+ memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64));
1709
+
1710
+ return err;
1711
+ }
1712
+
1713
+ extern int ZEXPORT unzGoToFirstFile(unzFile file)
1714
+ {
1715
+ return unzGoToFirstFile2(file, NULL, NULL, 0, NULL, 0, NULL, 0);
1716
+ }
1717
+
1718
+ extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
1719
+ uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
1720
+ {
1721
+ unz64_internal *s = NULL;
1722
+ int err = UNZ_OK;
1723
+
1724
+ if (file == NULL)
1725
+ return UNZ_PARAMERROR;
1726
+ s = (unz64_internal*)file;
1727
+
1728
+ if (!s->current_file_ok)
1729
+ return UNZ_END_OF_LIST_OF_FILE;
1730
+ if (s->gi.number_entry != UINT16_MAX) /* 2^16 files overflow hack */
1731
+ {
1732
+ if (s->num_file+1 == s->gi.number_entry)
1733
+ return UNZ_END_OF_LIST_OF_FILE;
1734
+ }
1735
+
1736
+ s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
1737
+ s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment;
1738
+ s->num_file += 1;
1739
+
1740
+ err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal,
1741
+ filename, filename_size, extrafield, extrafield_size, comment, comment_size);
1742
+
1743
+ s->current_file_ok = (err == UNZ_OK);
1744
+ if ((err == UNZ_OK) && (pfile_info != NULL))
1745
+ memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64));
1746
+
1747
+ return err;
1748
+ }
1749
+
1750
+ extern int ZEXPORT unzGoToNextFile(unzFile file)
1751
+ {
1752
+ return unzGoToNextFile2(file, NULL, NULL, 0, NULL, 0, NULL, 0);
1753
+ }
1754
+
1755
+ extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
1756
+ {
1757
+ unz64_internal *s = NULL;
1758
+ unz_file_info64 cur_file_info_saved;
1759
+ unz_file_info64_internal cur_file_info_internal_saved;
1760
+ uint64_t num_file_saved = 0;
1761
+ uint64_t pos_in_central_dir_saved = 0;
1762
+ char current_filename[UNZ_MAXFILENAMEINZIP+1];
1763
+ int err = UNZ_OK;
1764
+
1765
+ if (file == NULL)
1766
+ return UNZ_PARAMERROR;
1767
+ if (strlen(filename) >= UNZ_MAXFILENAMEINZIP)
1768
+ return UNZ_PARAMERROR;
1769
+ s = (unz64_internal*)file;
1770
+ if (!s->current_file_ok)
1771
+ return UNZ_END_OF_LIST_OF_FILE;
1772
+
1773
+ /* Save the current state */
1774
+ num_file_saved = s->num_file;
1775
+ pos_in_central_dir_saved = s->pos_in_central_dir;
1776
+ cur_file_info_saved = s->cur_file_info;
1777
+ cur_file_info_internal_saved = s->cur_file_info_internal;
1778
+
1779
+ err = unzGoToFirstFile2(file, NULL, current_filename, sizeof(current_filename)-1, NULL, 0, NULL, 0);
1780
+
1781
+ while (err == UNZ_OK)
1782
+ {
1783
+ if (filename_compare_func != NULL)
1784
+ err = filename_compare_func(file, current_filename, filename);
1785
+ else
1786
+ err = strcmp(current_filename, filename);
1787
+ if (err == 0)
1788
+ return UNZ_OK;
1789
+ err = unzGoToNextFile2(file, NULL, current_filename, sizeof(current_filename)-1, NULL, 0, NULL, 0);
1790
+ }
1791
+
1792
+ /* We failed, so restore the state of the 'current file' to where we were. */
1793
+ s->num_file = num_file_saved;
1794
+ s->pos_in_central_dir = pos_in_central_dir_saved;
1795
+ s->cur_file_info = cur_file_info_saved;
1796
+ s->cur_file_info_internal = cur_file_info_internal_saved;
1797
+ return err;
1798
+ }
1799
+
1800
+ extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos)
1801
+ {
1802
+ unz64_file_pos file_pos64;
1803
+ int err = unzGetFilePos64(file, &file_pos64);
1804
+ if (err == UNZ_OK)
1805
+ {
1806
+ file_pos->pos_in_zip_directory = (uint32_t)file_pos64.pos_in_zip_directory;
1807
+ file_pos->num_of_file = (uint32_t)file_pos64.num_of_file;
1808
+ }
1809
+ return err;
1810
+ }
1811
+
1812
+ extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos)
1813
+ {
1814
+ unz64_file_pos file_pos64;
1815
+ if (file_pos == NULL)
1816
+ return UNZ_PARAMERROR;
1817
+ file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
1818
+ file_pos64.num_of_file = file_pos->num_of_file;
1819
+ return unzGoToFilePos64(file, &file_pos64);
1820
+ }
1821
+
1822
+ extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos)
1823
+ {
1824
+ unz64_internal *s = NULL;
1825
+
1826
+ if (file == NULL || file_pos == NULL)
1827
+ return UNZ_PARAMERROR;
1828
+ s = (unz64_internal*)file;
1829
+ if (!s->current_file_ok)
1830
+ return UNZ_END_OF_LIST_OF_FILE;
1831
+
1832
+ file_pos->pos_in_zip_directory = s->pos_in_central_dir;
1833
+ file_pos->num_of_file = s->num_file;
1834
+ return UNZ_OK;
1835
+ }
1836
+
1837
+ extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos)
1838
+ {
1839
+ unz64_internal *s = NULL;
1840
+ int err = UNZ_OK;
1841
+
1842
+ if (file == NULL || file_pos == NULL)
1843
+ return UNZ_PARAMERROR;
1844
+ s = (unz64_internal*)file;
1845
+
1846
+ /* Jump to the right spot */
1847
+ s->pos_in_central_dir = file_pos->pos_in_zip_directory;
1848
+ s->num_file = file_pos->num_of_file;
1849
+
1850
+ /* Set the current file */
1851
+ err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0);
1852
+ /* Return results */
1853
+ s->current_file_ok = (err == UNZ_OK);
1854
+ return err;
1855
+ }
1856
+
1857
+ extern int32_t ZEXPORT unzGetOffset(unzFile file)
1858
+ {
1859
+ uint64_t offset64 = 0;
1860
+
1861
+ if (file == NULL)
1862
+ return UNZ_PARAMERROR;
1863
+ offset64 = unzGetOffset64(file);
1864
+ return (int32_t)offset64;
1865
+ }
1866
+
1867
+ extern int64_t ZEXPORT unzGetOffset64(unzFile file)
1868
+ {
1869
+ unz64_internal *s = NULL;
1870
+
1871
+ if (file == NULL)
1872
+ return UNZ_PARAMERROR;
1873
+ s = (unz64_internal*)file;
1874
+ if (!s->current_file_ok)
1875
+ return 0;
1876
+ if (s->gi.number_entry != 0 && s->gi.number_entry != UINT16_MAX)
1877
+ {
1878
+ if (s->num_file == s->gi.number_entry)
1879
+ return 0;
1880
+ }
1881
+ return s->pos_in_central_dir;
1882
+ }
1883
+
1884
+ extern int ZEXPORT unzSetOffset(unzFile file, uint32_t pos)
1885
+ {
1886
+ return unzSetOffset64(file, pos);
1887
+ }
1888
+
1889
+ extern int ZEXPORT unzSetOffset64(unzFile file, uint64_t pos)
1890
+ {
1891
+ unz64_internal *s = NULL;
1892
+ int err = UNZ_OK;
1893
+
1894
+ if (file == NULL)
1895
+ return UNZ_PARAMERROR;
1896
+ s = (unz64_internal*)file;
1897
+ s->pos_in_central_dir = pos;
1898
+ s->num_file = s->gi.number_entry; /* hack */
1899
+
1900
+ err = unzGetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0);
1901
+
1902
+ s->current_file_ok = (err == UNZ_OK);
1903
+ return err;
1904
+ }
1905
+
1906
+ extern int32_t ZEXPORT unzTell(unzFile file)
1907
+ {
1908
+ unz64_internal *s = NULL;
1909
+ if (file == NULL)
1910
+ return UNZ_PARAMERROR;
1911
+ s = (unz64_internal*)file;
1912
+ if (s->pfile_in_zip_read == NULL)
1913
+ return UNZ_PARAMERROR;
1914
+ return (int32_t)s->pfile_in_zip_read->stream.total_out;
1915
+ }
1916
+
1917
+ extern int64_t ZEXPORT unzTell64(unzFile file)
1918
+ {
1919
+ unz64_internal *s = NULL;
1920
+ if (file == NULL)
1921
+ return UNZ_PARAMERROR;
1922
+ s = (unz64_internal*)file;
1923
+ if (s->pfile_in_zip_read == NULL)
1924
+ return UNZ_PARAMERROR;
1925
+ return s->pfile_in_zip_read->total_out_64;
1926
+ }
1927
+
1928
+ extern int ZEXPORT unzSeek(unzFile file, uint32_t offset, int origin)
1929
+ {
1930
+ return unzSeek64(file, offset, origin);
1931
+ }
1932
+
1933
+ extern int ZEXPORT unzSeek64(unzFile file, uint64_t offset, int origin)
1934
+ {
1935
+ unz64_internal *s = NULL;
1936
+ uint64_t stream_pos_begin = 0;
1937
+ uint64_t stream_pos_end = 0;
1938
+ uint64_t position = 0;
1939
+ int is_within_buffer = 0;
1940
+
1941
+ if (file == NULL)
1942
+ return UNZ_PARAMERROR;
1943
+ s = (unz64_internal*)file;
1944
+
1945
+ if (s->pfile_in_zip_read == NULL)
1946
+ return UNZ_ERRNO;
1947
+ if (s->pfile_in_zip_read->compression_method != 0)
1948
+ return UNZ_ERRNO;
1949
+
1950
+ if (origin == SEEK_SET)
1951
+ position = offset;
1952
+ else if (origin == SEEK_CUR)
1953
+ position = s->pfile_in_zip_read->total_out_64 + offset;
1954
+ else if (origin == SEEK_END)
1955
+ position = s->cur_file_info.compressed_size + offset;
1956
+ else
1957
+ return UNZ_PARAMERROR;
1958
+
1959
+ if (position > s->cur_file_info.compressed_size)
1960
+ return UNZ_PARAMERROR;
1961
+
1962
+ stream_pos_end = s->pfile_in_zip_read->pos_in_zipfile;
1963
+ stream_pos_begin = stream_pos_end;
1964
+
1965
+ if (stream_pos_begin > UNZ_BUFSIZE)
1966
+ stream_pos_begin -= UNZ_BUFSIZE;
1967
+ else
1968
+ stream_pos_begin = 0;
1969
+
1970
+ is_within_buffer =
1971
+ (s->pfile_in_zip_read->stream.avail_in != 0) &&
1972
+ (s->pfile_in_zip_read->rest_read_compressed != 0 || s->cur_file_info.compressed_size < UNZ_BUFSIZE) &&
1973
+ (position >= stream_pos_begin && position < stream_pos_end);
1974
+
1975
+ if (is_within_buffer)
1976
+ {
1977
+ s->pfile_in_zip_read->stream.next_in += position - s->pfile_in_zip_read->total_out_64;
1978
+ s->pfile_in_zip_read->stream.avail_in = (uInt)(stream_pos_end - position);
1979
+ }
1980
+ else
1981
+ {
1982
+ s->pfile_in_zip_read->stream.avail_in = 0;
1983
+ s->pfile_in_zip_read->stream.next_in = 0;
1984
+
1985
+ s->pfile_in_zip_read->pos_in_zipfile = s->pfile_in_zip_read->offset_local_extrafield + position;
1986
+ s->pfile_in_zip_read->rest_read_compressed = s->cur_file_info.compressed_size - position;
1987
+ }
1988
+
1989
+ s->pfile_in_zip_read->rest_read_uncompressed -= (position - s->pfile_in_zip_read->total_out_64);
1990
+ s->pfile_in_zip_read->stream.total_out = (uint32_t)position;
1991
+ s->pfile_in_zip_read->total_out_64 = position;
1992
+
1993
+ return UNZ_OK;
1994
+ }
1995
+
1996
+ extern int ZEXPORT unzEndOfFile(unzFile file)
1997
+ {
1998
+ unz64_internal *s = NULL;
1999
+ if (file == NULL)
2000
+ return UNZ_PARAMERROR;
2001
+ s = (unz64_internal*)file;
2002
+ if (s->pfile_in_zip_read == NULL)
2003
+ return UNZ_PARAMERROR;
2004
+ if (s->pfile_in_zip_read->rest_read_uncompressed == 0)
2005
+ return 1;
2006
+ return 0;
2007
+ }