ManaPlus
zipreader.cpp
Go to the documentation of this file.
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"
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  bool readArchiveInfo(ZipEntry *const entry)
76  {
77  if (entry == nullptr)
78  {
79  reportAlways("Entry is null.")
80  return false;
81  }
82  const std::string archiveName = entry->root;
83  STD_VECTOR<ZipLocalHeader*> &restrict headers = entry->mHeaders;
84  STD_VECTOR<std::string> &restrict dirs = entry->mDirs;
85  FILE *restrict const arcFile = fopen(archiveName.c_str(),
86  "rb");
87  if (arcFile == nullptr)
88  {
89  reportAlways("Can't open zip file %s",
90  archiveName.c_str())
91  return false;
92  }
93  uint8_t *const buf = new uint8_t[65535 + 10];
94  uint16_t val16 = 0U;
95  uint16_t method = 0U;
96  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  while (feof(arcFile) == 0)
104  {
105  size_t cnt = 0U;
106  // file header pointer on 0
107  // read file header signature
108  readVal(buf, 4, "zip file header") // + 4
109  // pointer on 4
110 
111  if (buf[0] == 0x50 &&
112  buf[1] == 0x4B &&
113  buf[2] == 0x03 &&
114  buf[3] == 0x04)
115  { // local file header
116  header = new ZipLocalHeader;
117  header->zipEntry = entry;
118  // skip useless fields
119  fseek(arcFile, 4, SEEK_CUR); // + 4
120  // file header pointer on 8
121  readVal(&method, 2, "compression method") // + 2
122  swapVal16(method)
123  header->compressed = (method != 0);
124  // file header pointer on 10
125  fseek(arcFile, 8, SEEK_CUR); // + 8
126  // file header pointer on 18
127  readVal(&header->compressSize, 4,
128  "zip compressed size") // + 4
129  swapVal32(header->compressSize)
130  // file header pointer on 22
131  readVal(&header->uncompressSize, 4,
132  "zip uncompressed size") // + 4
133  swapVal32(header->uncompressSize)
134  // file header pointer on 26
135  readVal(&val16, 2, "file name length") // + 2
136  swapVal16(val16)
137  // file header pointer on 28
138  const uint32_t fileNameLen = CAST_U32(val16);
139  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  readVal(&val16, 2, "extra field length") // + 2
149  swapVal16(val16)
150  // file header pointer on 30
151  const uint32_t extraFieldLen = CAST_U32(val16);
152  readVal(buf, fileNameLen, "file name")
153  // file header pointer on 30 + fileNameLen
154  buf[fileNameLen] = 0;
155  header->fileName = std::string(
156  reinterpret_cast<char*>(buf));
157  prepareFsPath(header->fileName);
158  header->dataOffset = CAST_S32(ftell(arcFile) + extraFieldLen);
159  fseek(arcFile, extraFieldLen + header->compressSize, SEEK_CUR);
160  // pointer on 30 + fileNameLen + extraFieldLen + compressSize
161  if (findLast(header->fileName, dirSeparator) == false)
162  {
163  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  dirs.push_back(header->fileName);
182  delete2(header)
183  }
184  }
185  else if (buf[0] == 0x50 &&
186  buf[1] == 0x4B &&
187  buf[2] == 0x01 &&
188  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  else if (buf[0] == 0x50 &&
196  buf[1] == 0x4B &&
197  buf[2] == 0x05 &&
198  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  delete [] buf;
220  fclose(arcFile);
221  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  uint8_t *readCompressedFile(const ZipLocalHeader *restrict const header)
244  {
245  if (header == nullptr)
246  {
247  reportAlways("ZipReader::readCompressedFile: header is null")
248  return nullptr;
249  }
250  FILE *restrict const arcFile = fopen(
251  header->zipEntry->root.c_str(),
252  "rb");
253  if (arcFile == nullptr)
254  {
255  reportAlways("Can't open zip file %s",
256  header->zipEntry->root.c_str())
257  return nullptr;
258  }
259 
260  fseek(arcFile, header->dataOffset, SEEK_SET);
261  const uint32_t compressSize = header->compressSize;
262  uint8_t *const buf = new uint8_t[compressSize];
263  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  fclose(arcFile);
273  return buf;
274  }
275 
276  const uint8_t *readFile(const ZipLocalHeader *restrict const header)
277  {
278  if (header == nullptr)
279  {
280  reportAlways("Open zip file error. header is null.")
281  return nullptr;
282  }
283  uint8_t *restrict const in = readCompressedFile(header);
284  if (in == nullptr)
285  return nullptr;
286  if (header->compressed == false)
287  return in; // return as is if data not compressed
288  const size_t outSize = header->uncompressSize;
289  uint8_t *restrict const out = new uint8_t[outSize];
290  if (outSize == 0)
291  {
292  delete [] in;
293  return out;
294  }
295 
296  z_stream strm;
297  strm.zalloc = nullptr;
298  strm.zfree = nullptr;
299  strm.opaque = nullptr;
300  strm.next_in = in;
301  strm.avail_in = header->compressSize;
302  strm.next_out = out;
303  strm.avail_out = CAST_U32(outSize);
304 
305 PRAGMACLANG6GCC(GCC diagnostic push)
306 PRAGMACLANG6GCC(GCC diagnostic ignored "-Wold-style-cast")
307  int ret = inflateInit2(&strm, -MAX_WBITS);
308 PRAGMACLANG6GCC(GCC diagnostic pop)
309 
310  if (ret != Z_OK)
311  {
312  reportZlibError(header->zipEntry->root, ret);
313  delete [] in;
314  delete [] out;
315  return nullptr;
316  }
317  ret = inflate(&strm, Z_FINISH);
318 // ret = inflate(&strm, Z_SYNC_FLUSH);
319  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  inflateEnd(&strm);
330  delete [] in;
331  return out;
332  }
333 } // namespace ZipReader
334 
335 } // namespace VirtFs
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
#define reportAlways(...)
Definition: checkutils.h:253
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define delete2(var)
Definition: delete2.h:25
#define restrict
Definition: localconsts.h:165
#define PRAGMA48(str)
Definition: localconsts.h:199
#define PRAGMACLANG6GCC(str)
Definition: localconsts.h:240
Logger * logger
Definition: logger.cpp:89
bool dirs(InputEvent &event)
Definition: actions.cpp:77
uint8_t * readFile(ZipLocalHeader *header)
Definition: zipreader.cpp:276
uint8_t * readCompressedFile(ZipLocalHeader *header)
Definition: zipreader.cpp:243
bool readArchiveInfo(ZipEntry *entry)
Definition: zipreader.cpp:75
void reportZlibError(std::string &text, int err)
Definition: zipreader.cpp:224
std::string getZlibError(int err)
Definition: zipreader.cpp:232
void prepareFsPath(std::string &path)
Definition: paths.cpp:132
bool findLast(const std::string &str1, const std::string &str2)
std::string root
Definition: fsentry.h:45
std::vector< std::string > mDirs
Definition: zipentry.h:47
std::vector< ZipLocalHeader * > mHeaders
Definition: zipentry.h:46
#define swapVal16(val)
Definition: zipreader.cpp:66
char * dirSeparator
Definition: fs.cpp:43
#define readVal(val, sz, msg)
Definition: zipreader.cpp:50
#define swapVal32(val)
Definition: zipreader.cpp:67