#pragma once #include #include #include // A read-only ZIP reader, just enough for a plugin package (.fwspacm). // // Qt ships ZIP support only as private API (QtCore/private/qzipreader_p.h), // which is not something a project can rely on across toolchains. The package // format is fixed and small — a manifest plus one shared library — so the // format is read here instead: the end-of-central-directory record, the central // directory itself, and zlib for the deflated entries. // // Only what a package needs is supported: reading, with both the stored (0) and // deflated (8) methods. Writing is left to the packaging script at the repo // root (make-plugin-package.py), so nothing in the application has to create an // archive. class ZipArchive { public: // One file of the archive. `name` is the path as stored, always using '/' // as separator; entries ending in '/' are directories and are not read. struct Entry { QString name; quint32 compressedSize = 0; quint32 uncompressedSize = 0; quint32 localHeaderOffset = 0; quint16 method = 0; }; // Opening a package someone else wrote is untrusted input, so a single // entry claiming more than this is refused rather than allocating: a ZIP // bomb is the whole reason the limit exists. static constexpr quint32 kMaxEntrySize = 512u * 1024u * 1024u; ZipArchive() = default; // Reads an archive from disk. `error` is filled when the file cannot be // read or is not a ZIP; the archive then stays empty. bool open(const QString& path, QString* error); // Same, from an archive already in memory (the tests build one). bool openData(const QByteArray& data, QString* error); const std::vector& entries() const { return m_entries; } // File entries only, directories skipped. QStringList fileNames() const; bool contains(const QString& name) const; // Case-insensitive lookup, so a package built on Windows still installs // where a manifest says "Plugin.json" and the archive says "plugin.json". int indexOf(const QString& name) const; // Decompresses one entry. `error` is filled when the name is unknown, the // method is unsupported or the stream is corrupt. Returns an empty array // in all those cases, so a zero-length file and a failure are told apart by // the error, not by the result. QByteArray read(const QString& name, QString* error) const; // True when an entry name would write outside the directory it is // extracted to: absolute, empty, or climbing out with "..". A package is // never extracted to disk as a tree (the library is written to a single // known name), but the check is what keeps a hand-crafted archive from // smuggling a path through. static bool isSafeEntryName(const QString& name); private: bool parse(QString* error); const Entry* find(const QString& name) const; QByteArray m_data; std::vector m_entries; };