openMSX
ZipFileAdapter.cc
Go to the documentation of this file.
1 #include "ZipFileAdapter.hh"
2 #include "ZlibInflate.hh"
3 #include "FileException.hh"
4 
5 namespace openmsx {
6 
7 ZipFileAdapter::ZipFileAdapter(std::unique_ptr<FileBase> file_)
8  : CompressedFileAdapter(std::move(file_))
9 {
10 }
11 
12 void ZipFileAdapter::decompress(FileBase& f, Decompressed& d)
13 {
14  ZlibInflate zlib(f.mmap());
15 
16  if (zlib.get32LE() != 0x04034B50) {
17  throw FileException("Invalid ZIP file");
18  }
19 
20  // skip "version needed to extract" and "general purpose bit flag"
21  zlib.skip(2 + 2);
22 
23  // compression method
24  if (zlib.get16LE() != 0x0008) {
25  throw FileException("Unsupported zip compression method");
26  }
27 
28  // skip "last mod file time", "last mod file data",
29  // "crc32", "compressed size"
30  zlib.skip(2 + 2 + 4 + 4);
31 
32  unsigned origSize = zlib.get32LE(); // uncompressed size
33  unsigned filenameLen = zlib.get16LE(); // filename length
34  unsigned extraFieldLen = zlib.get16LE(); // extra field length
35  d.originalName = zlib.getString(filenameLen); // original filename
36  zlib.skip(extraFieldLen); // skip "extra field"
37 
38  d.size = zlib.inflate(d.buf, origSize);
39 }
40 
41 } // namespace openmsx
ZipFileAdapter(std::unique_ptr< FileBase > file)
STL namespace.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
virtual span< uint8_t > mmap()
Definition: FileBase.cc:10