Add password support for archive reader

This commit is contained in:
Georg Hagen
2025-02-06 17:15:43 +01:00
parent 07b5883620
commit 7a965a7767
3 changed files with 19 additions and 3 deletions

View File

@@ -18,6 +18,12 @@ namespace OpenVulkano
namespace
{
constexpr int BUFFER_SIZE = 16384;
const char* ArchivePasswordCallbackHandler(struct archive *, void *_client_data)
{
ArchiveReader* reader = static_cast<ArchiveReader*>(_client_data);
return reader->GetPasswordCallback()();
}
}
ArchiveReader::ArchiveReader(const std::shared_ptr<spdlog::logger>& logger, std::optional<ArchiveType::Type> archiveType)
@@ -42,6 +48,11 @@ namespace OpenVulkano
ArchiveReader::~ArchiveReader() = default;
void ArchiveReader::SetPasswordCallback(const decltype(m_passwordCallback)& callback)
{
archive_read_set_passphrase_callback(m_archive, this, &ArchivePasswordCallbackHandler);
}
void ArchiveReader::PrepOpen()
{
if (m_open)

View File

@@ -19,10 +19,11 @@ namespace OpenVulkano
{
class ArchiveReader final : public ArchiveBase
{
bool m_open = false;
bool m_eof = false;
std::optional<ArchiveType::Type> m_archiveType;
std::queue<std::string> m_archivesToRead;
std::function<const char*()> m_passwordCallback;
bool m_open = false;
bool m_eof = false;
public:
explicit ArchiveReader(const std::shared_ptr<spdlog::logger>& logger = nullptr, std::optional<ArchiveType::Type> archiveType = std::nullopt);
@@ -35,6 +36,8 @@ namespace OpenVulkano
~ArchiveReader() override;
void SetPasswordCallback(const decltype(m_passwordCallback)& callback);
bool Open(const char* archiveFile);
bool Open(const std::string& archiveFile);
@@ -72,6 +75,8 @@ namespace OpenVulkano
bool GetNextFileAsStream(const std::function<void(const FileDescription&, std::istream&)>& streamReader);
const decltype(m_passwordCallback)& GetPasswordCallback() const { return m_passwordCallback; }
private:
void ReadNextHeader();
std::function<int(archive*)> GetCurrentFormatReadFunc() const;

View File

@@ -64,7 +64,7 @@ namespace OpenVulkano
if (buffer.first == nullptr) continue;
size += buffer.second;
}
FileDescription description = FileDescription::MakeDescriptionForFile(fileName, size);
const FileDescription description = FileDescription::MakeDescriptionForFile(fileName, size);
return AddFile(description, buffers);
}