Files
asteria-qt/src/backend/formats/IByteSource.h
T
2026-10-25 15:43:51 +01:00

39 lines
1.2 KiB
C++

#pragma once
#include <QByteArray>
#include <QCryptographicHash>
#include <QFile>
#include <QString>
#include <QStringList>
// Abstracts how raw image bytes are obtained from a named entry.
// Implementations: ZipByteSource (minizip), RarByteSource (libarchive), DirByteSource (QDir/QFile).
class IByteSource {
public:
virtual ~IByteSource() = default;
// Opens the source. After returning true, listEntries/readEntry/contentHash are valid.
virtual bool open(const QString &path) = 0;
// All entry names in the source's natural order. Caller filters and sorts.
virtual QStringList listEntries() const = 0;
// Full bytes of one named entry. Returns empty on miss.
virtual QByteArray readEntry(const QString &entryName) const = 0;
// MD5 of the source. For archives: MD5 of file bytes.
// For directories: MD5 of sorted "name:size\n" lines.
virtual QByteArray contentHash() const = 0;
// Shared MD5 calculation for files on disk.
static QByteArray hashFileMd5(const QString &filePath)
{
if (filePath.isEmpty())
return {};
QFile f(filePath);
if (f.open(QIODevice::ReadOnly))
return QCryptographicHash::hash(f.readAll(), QCryptographicHash::Md5);
return {};
}
};