GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* |
||
2 |
* The ManaPlus Client |
||
3 |
* Copyright (C) 2017-2019 The ManaPlus Developers |
||
4 |
* Copyright (C) 2019-2021 Andrei Karas |
||
5 |
* |
||
6 |
* This file is part of The ManaPlus Client. |
||
7 |
* |
||
8 |
* This program is free software; you can redistribute it and/or modify |
||
9 |
* it under the terms of the GNU General Public License as published by |
||
10 |
* the Free Software Foundation; either version 2 of the License, or |
||
11 |
* any later version. |
||
12 |
* |
||
13 |
* This program is distributed in the hope that it will be useful, |
||
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 |
* GNU General Public License for more details. |
||
17 |
* |
||
18 |
* You should have received a copy of the GNU General Public License |
||
19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
20 |
*/ |
||
21 |
|||
22 |
#include "fs/virtfs/zipreader.h" |
||
23 |
|||
24 |
#include "fs/paths.h" |
||
25 |
|||
26 |
#include "fs/virtfs/zipentry.h" |
||
27 |
#include "fs/virtfs/ziplocalheader.h" |
||
28 |
|||
29 |
#include "utils/cast.h" |
||
30 |
#include "utils/checkutils.h" |
||
31 |
#include "utils/delete2.h" |
||
32 |
#include "utils/stringutils.h" |
||
33 |
|||
34 |
#include <zlib.h> |
||
35 |
PRAGMA48(GCC diagnostic push) |
||
36 |
PRAGMA48(GCC diagnostic ignored "-Wshadow") |
||
37 |
#include <SDL_endian.h> |
||
38 |
PRAGMA48(GCC diagnostic pop) |
||
39 |
|||
40 |
#include "debug.h" |
||
41 |
|||
42 |
#ifndef SDL_BIG_ENDIAN |
||
43 |
#error missing SDL_endian.h |
||
44 |
#endif // SDL_BYTEORDER |
||
45 |
|||
46 |
// #define DEBUG_ZIP |
||
47 |
|||
48 |
extern const char *dirSeparator; |
||
49 |
|||
50 |
#define readVal(val, sz, msg) \ |
||
51 |
cnt = fread(static_cast<void*>(val), 1, sz, arcFile); \ |
||
52 |
if (cnt != (sz)) \ |
||
53 |
{ \ |
||
54 |
reportAlways("Error reading " msg " in file %s", \ |
||
55 |
archiveName.c_str()) \ |
||
56 |
delete2(header) \ |
||
57 |
delete [] buf; \ |
||
58 |
fclose(arcFile); \ |
||
59 |
return false; \ |
||
60 |
} |
||
61 |
|||
62 |
#if SDL_BYTEORDER == SDL_BIG_ENDIAN |
||
63 |
#define swapVal16(val) val = __builtin_bswap16(val); |
||
64 |
#define swapVal32(val) val = __builtin_bswap32(val); |
||
65 |
#else // SDL_BYTEORDER == SDL_BIG_ENDIAN |
||
66 |
#define swapVal16(val) |
||
67 |
#define swapVal32(val) |
||
68 |
#endif // SDL_BYTEORDER == SDL_BIG_ENDIAN |
||
69 |
|||
70 |
namespace VirtFs |
||
71 |
{ |
||
72 |
|||
73 |
namespace ZipReader |
||
74 |
{ |
||
75 |
103 |
bool readArchiveInfo(ZipEntry *const entry) |
|
76 |
{ |
||
77 |
✗✓ | 103 |
if (entry == nullptr) |
78 |
{ |
||
79 |
reportAlways("Entry is null.") |
||
80 |
return false; |
||
81 |
} |
||
82 |
309 |
const std::string archiveName = entry->root; |
|
83 |
103 |
STD_VECTOR<ZipLocalHeader*> &restrict headers = entry->mHeaders; |
|
84 |
103 |
STD_VECTOR<std::string> &restrict dirs = entry->mDirs; |
|
85 |
103 |
FILE *restrict const arcFile = fopen(archiveName.c_str(), |
|
86 |
✓✗ | 103 |
"rb"); |
87 |
✗✓ | 103 |
if (arcFile == nullptr) |
88 |
{ |
||
89 |
reportAlways("Can't open zip file %s", |
||
90 |
archiveName.c_str()) |
||
91 |
return false; |
||
92 |
} |
||
93 |
✓✗ | 103 |
uint8_t *const buf = new uint8_t[65535 + 10]; |
94 |
103 |
uint16_t val16 = 0U; |
|
95 |
103 |
uint16_t method = 0U; |
|
96 |
103 |
ZipLocalHeader *header = nullptr; |
|
97 |
|||
98 |
#ifdef DEBUG_ZIP |
||
99 |
logger->log("Read archive: %s", archiveName.c_str()); |
||
100 |
#endif // DEBUG_ZIP |
||
101 |
|||
102 |
// format source https://en.wikipedia.org/wiki/Zip_%28file_format%29 |
||
103 |
✓✗ | 1149 |
while (feof(arcFile) == 0) |
104 |
{ |
||
105 |
1149 |
size_t cnt = 0U; |
|
106 |
// file header pointer on 0 |
||
107 |
// read file header signature |
||
108 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
1149 |
readVal(buf, 4, "zip file header") // + 4 |
109 |
// pointer on 4 |
||
110 |
|||
111 |
✓✗✓✗ |
2298 |
if (buf[0] == 0x50 && |
112 |
✓✓ | 2298 |
buf[1] == 0x4B && |
113 |
✓✗ | 2195 |
buf[2] == 0x03 && |
114 |
1046 |
buf[3] == 0x04) |
|
115 |
{ // local file header |
||
116 |
✓✗✓✗ |
1046 |
header = new ZipLocalHeader; |
117 |
1046 |
header->zipEntry = entry; |
|
118 |
// skip useless fields |
||
119 |
1046 |
fseek(arcFile, 4, SEEK_CUR); // + 4 |
|
120 |
// file header pointer on 8 |
||
121 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
1046 |
readVal(&method, 2, "compression method") // + 2 |
122 |
swapVal16(method) |
||
123 |
1046 |
header->compressed = (method != 0); |
|
124 |
// file header pointer on 10 |
||
125 |
1046 |
fseek(arcFile, 8, SEEK_CUR); // + 8 |
|
126 |
// file header pointer on 18 |
||
127 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
2092 |
readVal(&header->compressSize, 4, |
128 |
"zip compressed size") // + 4 |
||
129 |
swapVal32(header->compressSize) |
||
130 |
// file header pointer on 22 |
||
131 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
2092 |
readVal(&header->uncompressSize, 4, |
132 |
"zip uncompressed size") // + 4 |
||
133 |
swapVal32(header->uncompressSize) |
||
134 |
// file header pointer on 26 |
||
135 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
1046 |
readVal(&val16, 2, "file name length") // + 2 |
136 |
swapVal16(val16) |
||
137 |
// file header pointer on 28 |
||
138 |
1046 |
const uint32_t fileNameLen = CAST_U32(val16); |
|
139 |
✗✓ | 1046 |
if (fileNameLen > 1000) |
140 |
{ |
||
141 |
reportAlways("Error too long file name in file %s", |
||
142 |
archiveName.c_str()) |
||
143 |
delete header; |
||
144 |
delete [] buf; |
||
145 |
fclose(arcFile); |
||
146 |
return false; |
||
147 |
} |
||
148 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
1046 |
readVal(&val16, 2, "extra field length") // + 2 |
149 |
swapVal16(val16) |
||
150 |
// file header pointer on 30 |
||
151 |
1046 |
const uint32_t extraFieldLen = CAST_U32(val16); |
|
152 |
✗✓✗✗ ✗✗✗✗ ✗✗ |
2092 |
readVal(buf, fileNameLen, "file name") |
153 |
// file header pointer on 30 + fileNameLen |
||
154 |
1046 |
buf[fileNameLen] = 0; |
|
155 |
✓✗ | 4184 |
header->fileName = std::string( |
156 |
1046 |
reinterpret_cast<char*>(buf)); |
|
157 |
✓✗ | 1046 |
prepareFsPath(header->fileName); |
158 |
✓✗ | 1046 |
header->dataOffset = CAST_S32(ftell(arcFile) + extraFieldLen); |
159 |
1046 |
fseek(arcFile, extraFieldLen + header->compressSize, SEEK_CUR); |
|
160 |
// pointer on 30 + fileNameLen + extraFieldLen + compressSize |
||
161 |
✓✗✓✗ ✓✓ |
4184 |
if (findLast(header->fileName, dirSeparator) == false) |
162 |
{ |
||
163 |
✓✗ | 760 |
headers.push_back(header); |
164 |
#ifdef DEBUG_ZIP |
||
165 |
logger->log(" file name: %s", |
||
166 |
header->fileName.c_str()); |
||
167 |
logger->log(" compression method: %u", |
||
168 |
CAST_U32(method)); |
||
169 |
logger->log(" compressed size: %u", |
||
170 |
header->compressSize); |
||
171 |
logger->log(" uncompressed size: %u", |
||
172 |
header->uncompressSize); |
||
173 |
#endif // DEBUG_ZIP |
||
174 |
} |
||
175 |
else |
||
176 |
{ |
||
177 |
#ifdef DEBUG_ZIP |
||
178 |
logger->log(" dir name: %s", |
||
179 |
header->fileName.c_str()); |
||
180 |
#endif // DEBUG_ZIP |
||
181 |
✓✗ | 286 |
dirs.push_back(header->fileName); |
182 |
✓✗ | 572 |
delete2(header) |
183 |
} |
||
184 |
} |
||
185 |
✓✗✓✗ |
206 |
else if (buf[0] == 0x50 && |
186 |
✓✓ | 206 |
buf[1] == 0x4B && |
187 |
✗✓ | 205 |
buf[2] == 0x01 && |
188 |
102 |
buf[3] == 0x02) |
|
189 |
{ // central directory file header |
||
190 |
// !!! This is quick way for read zip archives. !!! |
||
191 |
// !!! It ignore modified files in archive. !!! |
||
192 |
// ignoring central directory entries |
||
193 |
break; |
||
194 |
} |
||
195 |
✓✗✓✗ |
2 |
else if (buf[0] == 0x50 && |
196 |
✓✗ | 2 |
buf[1] == 0x4B && |
197 |
✗✓ | 2 |
buf[2] == 0x05 && |
198 |
1 |
buf[3] == 0x06) |
|
199 |
{ // end of central directory |
||
200 |
// !!! This is quick way for read zip archives. !!! |
||
201 |
// !!! It ignore modified files in archive. !!! |
||
202 |
// ignoring end of central directory |
||
203 |
break; |
||
204 |
} |
||
205 |
else |
||
206 |
{ |
||
207 |
reportAlways("Error in header signature (0x%02x%02x%02x%02x)" |
||
208 |
" in file %s", |
||
209 |
buf[0], |
||
210 |
buf[1], |
||
211 |
buf[2], |
||
212 |
buf[3], |
||
213 |
archiveName.c_str()) |
||
214 |
delete [] buf; |
||
215 |
fclose(arcFile); |
||
216 |
return false; |
||
217 |
} |
||
218 |
} |
||
219 |
✓✗ | 103 |
delete [] buf; |
220 |
✓✗ | 103 |
fclose(arcFile); |
221 |
103 |
return true; |
|
222 |
} |
||
223 |
|||
224 |
void reportZlibError(const std::string &text, |
||
225 |
const int err) |
||
226 |
{ |
||
227 |
reportAlways("Zlib error: '%s' in %s", |
||
228 |
text.c_str(), |
||
229 |
getZlibError(err).c_str()) |
||
230 |
} |
||
231 |
|||
232 |
std::string getZlibError(const int err) |
||
233 |
{ |
||
234 |
switch (err) |
||
235 |
{ |
||
236 |
case Z_OK: |
||
237 |
return std::string(); |
||
238 |
default: |
||
239 |
return "unknown zlib error"; |
||
240 |
} |
||
241 |
} |
||
242 |
|||
243 |
66 |
uint8_t *readCompressedFile(const ZipLocalHeader *restrict const header) |
|
244 |
{ |
||
245 |
✓✓ | 66 |
if (header == nullptr) |
246 |
{ |
||
247 |
✓✗ | 2 |
reportAlways("ZipReader::readCompressedFile: header is null") |
248 |
return nullptr; |
||
249 |
} |
||
250 |
130 |
FILE *restrict const arcFile = fopen( |
|
251 |
65 |
header->zipEntry->root.c_str(), |
|
252 |
65 |
"rb"); |
|
253 |
✗✓ | 65 |
if (arcFile == nullptr) |
254 |
{ |
||
255 |
reportAlways("Can't open zip file %s", |
||
256 |
header->zipEntry->root.c_str()) |
||
257 |
return nullptr; |
||
258 |
} |
||
259 |
|||
260 |
65 |
fseek(arcFile, header->dataOffset, SEEK_SET); |
|
261 |
65 |
const uint32_t compressSize = header->compressSize; |
|
262 |
65 |
uint8_t *const buf = new uint8_t[compressSize]; |
|
263 |
✗✓ | 130 |
if (fread(static_cast<void*>(buf), 1, compressSize, arcFile) != |
264 |
compressSize) |
||
265 |
{ |
||
266 |
reportAlways("Read zip compressed file error from archive: %s", |
||
267 |
header->zipEntry->root.c_str()) |
||
268 |
fclose(arcFile); |
||
269 |
delete [] buf; |
||
270 |
return nullptr; |
||
271 |
} |
||
272 |
65 |
fclose(arcFile); |
|
273 |
return buf; |
||
274 |
} |
||
275 |
|||
276 |
65 |
const uint8_t *readFile(const ZipLocalHeader *restrict const header) |
|
277 |
{ |
||
278 |
✓✓ | 65 |
if (header == nullptr) |
279 |
{ |
||
280 |
✓✗ | 2 |
reportAlways("Open zip file error. header is null.") |
281 |
return nullptr; |
||
282 |
} |
||
283 |
64 |
uint8_t *restrict const in = readCompressedFile(header); |
|
284 |
✓✗ | 64 |
if (in == nullptr) |
285 |
return nullptr; |
||
286 |
✓✓ | 64 |
if (header->compressed == false) |
287 |
return in; // return as is if data not compressed |
||
288 |
55 |
const size_t outSize = header->uncompressSize; |
|
289 |
55 |
uint8_t *restrict const out = new uint8_t[outSize]; |
|
290 |
✗✓ | 55 |
if (outSize == 0) |
291 |
{ |
||
292 |
delete [] in; |
||
293 |
return out; |
||
294 |
} |
||
295 |
|||
296 |
z_stream strm; |
||
297 |
55 |
strm.zalloc = nullptr; |
|
298 |
55 |
strm.zfree = nullptr; |
|
299 |
55 |
strm.opaque = nullptr; |
|
300 |
55 |
strm.next_in = in; |
|
301 |
55 |
strm.avail_in = header->compressSize; |
|
302 |
55 |
strm.next_out = out; |
|
303 |
55 |
strm.avail_out = CAST_U32(outSize); |
|
304 |
|||
305 |
PRAGMACLANG6GCC(GCC diagnostic push) |
||
306 |
PRAGMACLANG6GCC(GCC diagnostic ignored "-Wold-style-cast") |
||
307 |
55 |
int ret = inflateInit2(&strm, -MAX_WBITS); |
|
308 |
PRAGMACLANG6GCC(GCC diagnostic pop) |
||
309 |
|||
310 |
✗✓ | 55 |
if (ret != Z_OK) |
311 |
{ |
||
312 |
reportZlibError(header->zipEntry->root, ret); |
||
313 |
delete [] in; |
||
314 |
delete [] out; |
||
315 |
return nullptr; |
||
316 |
} |
||
317 |
55 |
ret = inflate(&strm, Z_FINISH); |
|
318 |
// ret = inflate(&strm, Z_SYNC_FLUSH); |
||
319 |
✗✓ | 55 |
if (ret != Z_OK && |
320 |
ret != Z_STREAM_END) |
||
321 |
{ |
||
322 |
reportZlibError("file decompression error", |
||
323 |
ret); |
||
324 |
inflateEnd(&strm); |
||
325 |
delete [] in; |
||
326 |
delete [] out; |
||
327 |
return nullptr; |
||
328 |
} |
||
329 |
55 |
inflateEnd(&strm); |
|
330 |
55 |
delete [] in; |
|
331 |
55 |
return out; |
|
332 |
} |
||
333 |
} // namespace ZipReader |
||
334 |
|||
335 |
} // namespace VirtFs |
Generated by: GCOVR (Version 3.3) |