| 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 |
|
| 24 |
zlib.skip(2 + 2); |
| 25 |
|
| 26 |
|
| 27 |
if (zlib.get16LE() != 0x0008) { |
| 28 |
throw FileException("Unsupported zip compression method"); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
zlib.skip(2 + 2 + 4 + 4); |
| 34 |
|
| 35 |
unsigned origSize = zlib.get32LE(); |
| 36 |
unsigned filenameLen = zlib.get16LE(); |
| 37 |
unsigned extraFieldLen = zlib.get16LE(); |
| 38 |
d.originalName = zlib.getString(filenameLen); |
| 39 |
zlib.skip(extraFieldLen); |
| 40 |
|
| 41 |
d.buf = zlib.inflate(origSize); |
| 42 |
} |
| 43 |
|
| 44 |
} |