52
52
53
53
// minizip 1.2.0 is same with other platforms
54
54
#define unzGoToFirstFile64 (A, B, C, D ) unzGoToFirstFile2(A, B, C, D, NULL , 0 , NULL , 0 )
55
- #define unzGoToNextFile64 (A, B, C, D ) unzGoToNextFile2(A, B, C, D, NULL , 0 , NULL , 0 )
55
+ #define unzGoToNextFile64 (A, B, C, D ) unzGoToNextFile2(A, B, C, D, NULL , 0 , NULL , 0 )
56
56
57
57
namespace ax
58
58
{
@@ -93,32 +93,22 @@ yasio::byte_buffer ZipUtils::compressGZ(const void* in, size_t inlen, int level)
93
93
break ;
94
94
}
95
95
96
- switch (err)
97
- {
98
- case Z_NEED_DICT:
99
- err = Z_DATA_ERROR;
100
- case Z_DATA_ERROR:
101
- case Z_MEM_ERROR:
102
- goto _L_end;
103
- }
104
-
105
- // not enough buffer ?
106
- if (err != Z_STREAM_END)
96
+ if (!err)
107
97
{
108
98
output.insert (output.end (), buffer, buffer + sizeof (buffer));
109
99
110
100
d_stream.next_out = buffer;
111
101
d_stream.avail_out = sizeof (buffer);
112
102
}
103
+ else
104
+ {
105
+ output.clear ();
106
+ output.shrink_to_fit ();
107
+ break ;
108
+ }
113
109
}
114
110
115
- _L_end:
116
111
deflateEnd (&d_stream);
117
- if (err != Z_STREAM_END)
118
- {
119
- output.clear ();
120
- }
121
-
122
112
return output;
123
113
}
124
114
@@ -141,7 +131,21 @@ yasio::byte_buffer ZipUtils::decompressGZ(const void* in, size_t inlen, int expe
141
131
if (err != Z_OK) //
142
132
return output;
143
133
144
- output.reserve (expected_size != -1 ? expected_size : (inlen << 2 ));
134
+ // GZ contains extra data: crc32(4bytes) + uncompress size(4bytes)
135
+ // so at least 8bytes
136
+ if (inlen < 8 )
137
+ {
138
+ AXLOGW (" ZipUtils: Invalid gz compress data!" );
139
+ return output;
140
+ }
141
+
142
+ if (expected_size < 1 )
143
+ {
144
+ // gzip: extra data contains crc32(4bytes), uncompress size(4bytes)
145
+ memcpy (&expected_size, reinterpret_cast <const uint8_t *>(in) + inlen - 4 , 4 );
146
+ expected_size = std::clamp (expected_size, 1 , 20 * 1024 * 1024 );
147
+ }
148
+ output.reserve (expected_size);
145
149
146
150
for (;;)
147
151
{
@@ -153,52 +157,39 @@ yasio::byte_buffer ZipUtils::decompressGZ(const void* in, size_t inlen, int expe
153
157
break ;
154
158
}
155
159
156
- switch ( err)
160
+ if (! err)
157
161
{
158
- case Z_NEED_DICT:
159
- err = Z_DATA_ERROR;
160
- case Z_DATA_ERROR:
161
- case Z_MEM_ERROR:
162
- goto _L_end;
163
- }
164
-
165
- // not enough memory ?
166
- if (err != Z_STREAM_END)
167
- {
168
- // *out = (unsigned char*)realloc(*out, bufferSize * BUFFER_INC_FACTOR);
169
162
output.insert (output.end (), buffer, buffer + sizeof (buffer));
170
-
171
163
d_stream.next_out = buffer;
172
164
d_stream.avail_out = sizeof (buffer);
173
165
}
174
- }
175
-
176
- _L_end:
177
- inflateEnd (&d_stream);
178
- if (err != Z_STREAM_END)
179
- {
180
- switch (err)
166
+ else
181
167
{
182
- case Z_MEM_ERROR:
183
- AXLOGW (" ZipUtils: Out of memory while decompressing map data!" );
184
- break ;
185
- case Z_VERSION_ERROR:
186
- AXLOGW (" ZipUtils: Incompatible zlib version!" );
187
- break ;
188
- case Z_DATA_ERROR:
189
- AXLOGW (" ZipUtils: Incorrect zlib compressed data!" );
168
+ switch (err)
169
+ {
170
+ case Z_MEM_ERROR:
171
+ AXLOGW (" ZipUtils: Out of memory while decompressing map data!" );
172
+ break ;
173
+ case Z_VERSION_ERROR:
174
+ AXLOGW (" ZipUtils: Incompatible zlib version!" );
175
+ break ;
176
+ case Z_DATA_ERROR:
177
+ AXLOGW (" ZipUtils: Incorrect zlib compressed data!" );
178
+ break ;
179
+ default :
180
+ AXLOGW (" ZipUtils: Decompress data fail, err:{}" , err);
181
+ }
182
+ output.clear ();
183
+ output.shrink_to_fit ();
190
184
break ;
191
- default :
192
- AXLOGW (" ZipUtils: Unknown error while decompressing map data!" );
193
185
}
194
- output.clear ();
195
- output.shrink_to_fit ();
196
186
}
197
187
188
+ inflateEnd (&d_stream);
198
189
return output;
199
190
}
200
191
201
- inline void ZipUtils::decodeEncodedPvr (unsigned int * data, ssize_t len)
192
+ void ZipUtils::decodeEncodedPvr (unsigned int * data, ssize_t len)
202
193
{
203
194
const int enclen = 1024 ;
204
195
const int securelen = 512 ;
@@ -230,7 +221,7 @@ inline void ZipUtils::decodeEncodedPvr(unsigned int* data, ssize_t len)
230
221
do
231
222
{
232
223
#define DELTA 0x9e3779b9
233
- #define MX (((z >> 5 ^ y << 2 ) + (y >> 3 ^ z << 4 )) ^ ((sum ^ y) + (s_uEncryptedPvrKeyParts[(p & 3 ) ^ e] ^ z)))
224
+ #define MX (((z >> 5 ^ y << 2 ) + (y >> 3 ^ z << 4 )) ^ ((sum ^ y) + (s_uEncryptedPvrKeyParts[(p & 3 ) ^ e] ^ z)))
234
225
235
226
sum += DELTA;
236
227
e = (sum >> 2 ) & 3 ;
@@ -275,7 +266,7 @@ inline void ZipUtils::decodeEncodedPvr(unsigned int* data, ssize_t len)
275
266
}
276
267
}
277
268
278
- inline unsigned int ZipUtils::checksumPvr (const unsigned int * data, ssize_t len)
269
+ unsigned int ZipUtils::checksumPvr (const unsigned int * data, ssize_t len)
279
270
{
280
271
unsigned int cs = 0 ;
281
272
const int cslen = 128 ;
@@ -295,7 +286,7 @@ ssize_t ZipUtils::inflateMemoryWithHint(unsigned char* in, ssize_t inLength, uns
295
286
auto outBuffer = decompressGZ (std::span{in, in + inLength}, static_cast <int >(outLengthHint));
296
287
auto outLen = outBuffer.size ();
297
288
if (out)
298
- *out = outBuffer.release_pointer ();
289
+ *out = outBuffer.release_pointer ();
299
290
return outLen;
300
291
}
301
292
@@ -347,7 +338,6 @@ int ZipUtils::inflateGZipFile(const char* path, unsigned char** out)
347
338
AXLOGW (" ZipUtils: gzclose failed" );
348
339
}
349
340
350
-
351
341
auto totalSize = buffer.size ();
352
342
if (out)
353
343
*out = buffer.release_pointer ();
@@ -548,15 +538,15 @@ struct ZipFilePrivate
548
538
{
549
539
ZipFilePrivate ()
550
540
{
551
- functionOverrides.zopen64_file = ZipFile_open_file_func;
541
+ functionOverrides.zopen64_file = ZipFile_open_file_func;
552
542
functionOverrides.zopendisk64_file = ZipFile_opendisk_file_func;
553
- functionOverrides.zread_file = ZipFile_read_file_func;
554
- functionOverrides.zwrite_file = ZipFile_write_file_func;
543
+ functionOverrides.zread_file = ZipFile_read_file_func;
544
+ functionOverrides.zwrite_file = ZipFile_write_file_func;
555
545
functionOverrides.ztell64_file = ZipFile_tell_file_func;
556
546
functionOverrides.zseek64_file = ZipFile_seek_file_func;
557
- functionOverrides.zclose_file = ZipFile_close_file_func;
558
- functionOverrides.zerror_file = ZipFile_error_file_func;
559
- functionOverrides.opaque = this ;
547
+ functionOverrides.zclose_file = ZipFile_close_file_func;
548
+ functionOverrides.zerror_file = ZipFile_error_file_func;
549
+ functionOverrides.opaque = this ;
560
550
}
561
551
562
552
// unzip overrides to support IFileStream
@@ -685,12 +675,15 @@ ZipFile* ZipFile::createFromFile(std::string_view zipFile, std::string_view filt
685
675
return nullptr ;
686
676
}
687
677
688
- ZipFile * ZipFile::createWithBuffer (const void * buffer, uLong size)
678
+ ZipFile* ZipFile::createWithBuffer (const void * buffer, uLong size)
689
679
{
690
- ZipFile *zip = new ZipFile ();
691
- if (zip->initWithBuffer (buffer, size)) {
680
+ ZipFile* zip = new ZipFile ();
681
+ if (zip->initWithBuffer (buffer, size))
682
+ {
692
683
return zip;
693
- } else {
684
+ }
685
+ else
686
+ {
694
687
delete zip;
695
688
return nullptr ;
696
689
}
@@ -872,17 +865,21 @@ int ZipFile::getCurrentFileInfo(std::string* filename, unz_file_info_s* info)
872
865
return ret;
873
866
}
874
867
875
- bool ZipFile::initWithBuffer (const void * buffer, uLong size)
868
+ bool ZipFile::initWithBuffer (const void * buffer, uLong size)
876
869
{
877
- if (!buffer || size == 0 ) return false ;
878
- zlib_filefunc_def memory_file = { 0 };
879
-
880
- std::unique_ptr<ourmemory_t > memfs (new ourmemory_t { (char *)const_cast <void *>(buffer), static_cast <uint32_t >(size), 0 , 0 , 0 });
881
- if (!memfs) return false ;
870
+ if (!buffer || size == 0 )
871
+ return false ;
872
+ zlib_filefunc_def memory_file = {0 };
873
+
874
+ std::unique_ptr<ourmemory_t > memfs (
875
+ new ourmemory_t {(char *)const_cast <void *>(buffer), static_cast <uint32_t >(size), 0 , 0 , 0 });
876
+ if (!memfs)
877
+ return false ;
882
878
fill_memory_filefunc (&memory_file, memfs.get ());
883
-
879
+
884
880
_data->zipFile = unzOpen2 (nullptr , &memory_file);
885
- if (!_data->zipFile ) return false ;
881
+ if (!_data->zipFile )
882
+ return false ;
886
883
_data->memfs = std::move (memfs);
887
884
setFilter (emptyFilename);
888
885
return true ;
@@ -964,4 +961,4 @@ int64_t ZipFile::vsize(ZipEntryInfo* entry)
964
961
return 0 ;
965
962
}
966
963
967
- }
964
+ } // namespace ax
0 commit comments