openMSX
GZFileAdapter.cc
Go to the documentation of this file.
1#include "GZFileAdapter.hh"
2#include "ZlibInflate.hh"
3#include "FileException.hh"
4
5namespace openmsx {
6
7static constexpr uint8_t ASCII_FLAG = 0x01; // bit 0 set: file probably ascii text
8static constexpr uint8_t HEAD_CRC = 0x02; // bit 1 set: header CRC present
9static constexpr uint8_t EXTRA_FIELD = 0x04; // bit 2 set: extra field present
10static constexpr uint8_t ORIG_NAME = 0x08; // bit 3 set: original file name present
11static constexpr uint8_t COMMENT = 0x10; // bit 4 set: file comment present
12static constexpr uint8_t RESERVED = 0xE0; // bits 5..7: reserved
13
14
15GZFileAdapter::GZFileAdapter(std::unique_ptr<FileBase> file_)
16 : CompressedFileAdapter(std::move(file_))
17{
18}
19
20[[nodiscard]] static bool skipHeader(ZlibInflate& zlib, std::string& originalName)
21{
22 // check magic bytes
23 if (zlib.get16LE() != 0x8B1F) {
24 return false;
25 }
26
27 uint8_t method = zlib.getByte();
28 uint8_t flags = zlib.getByte();
29 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
30 return false;
31 }
32
33 // Discard time, xflags and OS code:
34 zlib.skip(6);
35
36 if ((flags & EXTRA_FIELD) != 0) {
37 // skip the extra field
38 zlib.skip(zlib.get16LE());
39 }
40 if ((flags & ORIG_NAME) != 0) {
41 // get the original file name
42 originalName = zlib.getCString();
43 }
44 if ((flags & COMMENT) != 0) {
45 // skip the .gz file comment
46 (void)zlib.getCString();
47 }
48 if ((flags & HEAD_CRC) != 0) {
49 // skip the header crc
50 zlib.skip(2);
51 }
52 return true;
53}
54
55void GZFileAdapter::decompress(FileBase& f, Decompressed& d)
56{
57 ZlibInflate zlib(f.mmap());
58 if (!skipHeader(zlib, d.originalName)) {
59 throw FileException("Not a gzip header");
60 }
61 d.size = zlib.inflate(d.buf);
62}
63
64} // namespace openmsx
GZFileAdapter(std::unique_ptr< FileBase > file)
std::string getCString()
void skip(size_t num)
size_t inflate(MemBuffer< uint8_t > &output, size_t sizeHint=65536)
This file implemented 3 utility functions:
Definition Autofire.cc:11
STL namespace.