ManaPlus
Functions
VirtFs::ZipReader Namespace Reference

Functions

bool readArchiveInfo (ZipEntry *entry)
 
void reportZlibError (std::string &text, int err)
 
std::string getZlibError (int err)
 
uint8_t * readCompressedFile (ZipLocalHeader *header)
 
uint8_t * readFile (ZipLocalHeader *header)
 
void reportZlibError (const std::string &text, const int err)
 
uint8_t * readCompressedFile (const ZipLocalHeader *const header)
 
const uint8_t * readFile (const ZipLocalHeader *const header)
 

Function Documentation

◆ getZlibError()

std::string VirtFs::ZipReader::getZlibError ( int  err)

Definition at line 232 of file zipreader.cpp.

233  {
234  switch (err)
235  {
236  case Z_OK:
237  return std::string();
238  default:
239  return "unknown zlib error";
240  }
241  }

Referenced by reportZlibError().

◆ readArchiveInfo()

bool VirtFs::ZipReader::readArchiveInfo ( ZipEntry entry)

Definition at line 75 of file zipreader.cpp.

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  }
#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 new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
if(!vert) return
#define restrict
Definition: localconsts.h:165
Logger * logger
Definition: logger.cpp:89
bool dirs(InputEvent &event)
Definition: actions.cpp:77
int size()
Definition: emotedb.cpp:306
void prepareFsPath(std::string &path)
Definition: paths.cpp:132
bool findLast(const std::string &str1, const std::string &str2)
std::string fileName
Definition: testmain.cpp:39
#define STD_VECTOR
Definition: vector.h:30
#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

References CAST_S32, CAST_U32, VirtFs::ZipLocalHeader::compressed, VirtFs::ZipLocalHeader::compressSize, VirtFs::ZipLocalHeader::dataOffset, delete2, Actions::dirs(), dirSeparator, VirtFs::ZipLocalHeader::fileName, findLast(), Logger::log(), logger, VirtFs::ZipEntry::mDirs, VirtFs::ZipEntry::mHeaders, prepareFsPath(), readVal, reportAlways, restrict, VirtFs::FsEntry::root, swapVal16, swapVal32, VirtFs::ZipLocalHeader::uncompressSize, and VirtFs::ZipLocalHeader::zipEntry.

Referenced by VirtFs::mountZip(), and VirtFs::mountZip2().

◆ readCompressedFile() [1/2]

uint8_t* VirtFs::ZipReader::readCompressedFile ( const ZipLocalHeader *const  header)

◆ readCompressedFile() [2/2]

uint8_t* VirtFs::ZipReader::readCompressedFile ( ZipLocalHeader header)

Definition at line 243 of file zipreader.cpp.

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  }

References reportAlways, and restrict.

Referenced by readFile().

◆ readFile() [1/2]

const uint8_t* VirtFs::ZipReader::readFile ( const ZipLocalHeader *const  header)

◆ readFile() [2/2]

uint8_t* VirtFs::ZipReader::readFile ( ZipLocalHeader header)

Definition at line 276 of file zipreader.cpp.

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  }
#define PRAGMACLANG6GCC(str)
Definition: localconsts.h:240
uint8_t * readCompressedFile(ZipLocalHeader *header)
Definition: zipreader.cpp:243
void reportZlibError(std::string &text, int err)
Definition: zipreader.cpp:224

References CAST_U32, PRAGMACLANG6GCC, readCompressedFile(), reportAlways, reportZlibError(), and restrict.

Referenced by VirtFs::FsZip::loadFile(), and VirtFs::FsZip::openRead().

◆ reportZlibError() [1/2]

void VirtFs::ZipReader::reportZlibError ( const std::string &  text,
const int  err 
)

◆ reportZlibError() [2/2]

void VirtFs::ZipReader::reportZlibError ( std::string &  text,
int  err 
)

Definition at line 224 of file zipreader.cpp.

226  {
227  reportAlways("Zlib error: '%s' in %s",
228  text.c_str(),
229  getZlibError(err).c_str())
230  }
std::string getZlibError(int err)
Definition: zipreader.cpp:232

References getZlibError(), and reportAlways.

Referenced by readFile().