OXYGENHUB
◆ main openmsx / src / file / ZipFileAdapter.cc
1.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "ZipFileAdapter.hh"
2
3
#include "FileException.hh"
4
#include "MappedFile.hh"
5
#include "ZlibInflate.hh"
6
7
namespace openmsx {
8
9
ZipFileAdapter::ZipFileAdapter(std::unique_ptr<FileBase> file_, zstring_view filename_)
10
	: CompressedFileAdapter(std::move(file_), filename_)
11
{
12
}
13
14
void ZipFileAdapter::decompress(FileBase& f, Decompressed& d)
15
{
16
	auto mmap = MappedFile<const uint8_t>(f.mmap(0, true));
17
	ZlibInflate zlib(mmap);
18
19
	if (zlib.get32LE() != 0x04034B50) {
20
		throw FileException("Invalid ZIP file");
21
	}
22
23
	// skip "version needed to extract" and "general purpose bit flag"
24
	zlib.skip(2 + 2);
25
26
	// compression method
27
	if (zlib.get16LE() != 0x0008) {
28
		throw FileException("Unsupported zip compression method");
29
	}
30
31
	// skip "last mod file time", "last mod file data",
32
	//      "crc32",              "compressed size"
33
	zlib.skip(2 + 2 + 4 + 4);
34
35
	unsigned origSize = zlib.get32LE(); // uncompressed size
36
	unsigned filenameLen = zlib.get16LE(); // filename length
37
	unsigned extraFieldLen = zlib.get16LE(); // extra field length
38
	d.originalName = zlib.getString(filenameLen); // original filename
39
	zlib.skip(extraFieldLen); // skip "extra field"
40
41
	d.buf = zlib.inflate(origSize);
42
}
43
44
} // namespace openmsx