Files
FWS-PacManage/include/zip-archive.h
T
BlackAngelTV f6c8cedf93 feat(plugins): replace the shop with drag-and-drop .fwspacm packages
The shop was a catalogue served over HTTP that the application fetched and
listed, which put a network round trip between a user and a plugin that was
almost always one file they already had. It also had to keep its catalogue
URLs in step with every release, since an entry that pointed at a missing
asset was indistinguishable from one that never worked.

A plugin is now distributed as a .fwspacm package: a ZIP archive holding a
plugin.json manifest and the shared library, which the user drops anywhere
on the window. The file is the distribution, so there is no catalogue to
publish, no network, and no catalogue URL setting to explain. The manifest
tells the user what they are about to run, and its checksum is verified
before anything is written, so a package whose library is not the one it
declares never reaches the plugin directory.

Qt exposes no public ZIP API, so the archive is read directly (central
directory plus zlib for the deflated entries) rather than through a private
header that could move between toolchains. The reader stays in the logic
layer: app-core-test still links no QtWidgets, and a unit test builds its
own archives to check both compression methods and every refusal path.
2026-09-28 21:52:50 +02:00

73 lines
3.0 KiB
C++

#pragma once
#include <QByteArray>
#include <QString>
#include <vector>
// 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<Entry>& 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<Entry> m_entries;
};