OpenRCT2/src/openrct2/core/Zip.cpp

395 lines
9.5 KiB
C++
Raw Normal View History

2016-11-13 14:48:24 +01:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2016-11-13 14:48:24 +01:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-11-13 14:48:24 +01:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-11-13 14:48:24 +01:00
*****************************************************************************/
#include "Zip.h"
2016-11-13 14:48:24 +01:00
#include "IStream.hpp"
2018-06-22 22:58:39 +02:00
#include <algorithm>
#ifndef __ANDROID__
2018-07-21 16:17:06 +02:00
# include <zip.h>
#endif
2021-01-20 21:42:03 +01:00
using namespace OpenRCT2;
static std::string NormalisePath(std::string_view path)
{
std::string result;
if (!path.empty())
{
2021-01-20 22:04:49 +01:00
result.reserve(path.size());
for (auto ch : path)
{
2021-01-20 22:04:49 +01:00
if (ch == '\\')
{
2021-01-20 22:04:49 +01:00
result += '/';
}
else
{
result += ch;
}
}
}
return result;
}
/**
* Normalises both the given path and the stored paths and finds the first match.
*/
std::optional<size_t> IZipArchive::GetIndexFromPath(std::string_view path) const
{
auto normalisedPath = NormalisePath(path);
if (!normalisedPath.empty())
{
auto numFiles = GetNumFiles();
for (size_t i = 0; i < numFiles; i++)
{
auto normalisedZipPath = NormalisePath(GetFileName(i));
if (normalisedZipPath == normalisedPath)
{
return i;
}
}
}
2021-01-20 22:04:49 +01:00
return std::nullopt;
}
bool IZipArchive::Exists(std::string_view path) const
{
return GetIndexFromPath(path).has_value();
}
#ifndef __ANDROID__
2018-06-22 22:58:39 +02:00
class ZipArchive final : public IZipArchive
2016-11-13 14:48:24 +01:00
{
private:
2018-06-22 22:58:39 +02:00
zip_t* _zip;
ZIP_ACCESS _access;
std::vector<std::vector<uint8_t>> _writeBuffers;
2016-11-13 14:48:24 +01:00
public:
ZipArchive(std::string_view path, ZIP_ACCESS access)
2016-11-13 14:48:24 +01:00
{
auto zipOpenMode = ZIP_RDONLY;
if (access == ZIP_ACCESS::WRITE)
{
zipOpenMode = ZIP_CREATE;
}
int32_t error;
2021-01-06 20:28:19 +01:00
_zip = zip_open(std::string(path).c_str(), zipOpenMode, &error);
2016-11-13 14:48:24 +01:00
if (_zip == nullptr)
{
throw IOException("Unable to open zip file.");
}
_access = access;
2016-11-13 14:48:24 +01:00
}
~ZipArchive() override
{
zip_close(_zip);
}
size_t GetNumFiles() const override
{
2018-05-12 17:11:51 +02:00
return zip_get_num_entries(_zip, 0);
2016-11-13 14:48:24 +01:00
}
std::string GetFileName(size_t index) const override
2016-11-13 14:48:24 +01:00
{
std::string result;
auto name = zip_get_name(_zip, index, ZIP_FL_ENC_GUESS);
if (name != nullptr)
{
result = name;
}
return result;
2016-11-13 14:48:24 +01:00
}
uint64_t GetFileSize(size_t index) const override
2016-11-13 14:48:24 +01:00
{
zip_stat_t zipFileStat;
if (zip_stat_index(_zip, index, 0, &zipFileStat) == ZIP_ER_OK)
{
return zipFileStat.size;
}
else
{
return 0;
}
}
std::vector<uint8_t> GetFileData(std::string_view path) const override
2016-11-13 14:48:24 +01:00
{
std::vector<uint8_t> result;
2018-05-12 17:11:51 +02:00
auto index = GetIndexFromPath(path);
if (index)
2016-11-13 14:48:24 +01:00
{
auto dataSize = GetFileSize(*index);
if (dataSize > 0 && dataSize < SIZE_MAX)
2016-11-13 14:48:24 +01:00
{
auto zipFile = zip_fopen_index(_zip, *index, 0);
if (zipFile != nullptr)
2016-11-13 14:48:24 +01:00
{
result.resize(static_cast<size_t>(dataSize));
uint64_t readBytes = zip_fread(zipFile, result.data(), dataSize);
if (readBytes != dataSize)
{
2021-01-20 22:04:49 +01:00
result = {};
}
zip_fclose(zipFile);
2016-11-13 14:48:24 +01:00
}
}
}
return result;
2016-11-13 14:48:24 +01:00
}
2021-01-20 21:42:03 +01:00
std::unique_ptr<IStream> GetFileStream(std::string_view path) const override
{
auto index = GetIndexFromPath(path);
if (index)
{
2021-01-20 21:42:03 +01:00
return std::make_unique<ZipItemStream>(_zip, *index);
}
return {};
}
void SetFileData(std::string_view path, std::vector<uint8_t>&& data) override
{
// Push buffer to an internal list as libzip requires access to it until the zip
// handle is closed.
_writeBuffers.push_back(std::move(data));
const auto& writeBuffer = *_writeBuffers.rbegin();
auto source = zip_source_buffer(_zip, writeBuffer.data(), writeBuffer.size(), 0);
2018-05-12 17:11:51 +02:00
auto index = GetIndexFromPath(path);
if (index)
{
zip_replace(_zip, *index, source);
}
else
{
zip_add(_zip, path.data(), source);
}
}
2016-12-05 17:52:42 +01:00
void DeleteFile(std::string_view path) override
2016-12-05 17:52:42 +01:00
{
2018-05-12 17:11:51 +02:00
auto index = GetIndexFromPath(path);
if (index)
{
zip_delete(_zip, *index);
}
else
{
throw std::runtime_error("File does not exist.");
}
2016-12-05 17:52:42 +01:00
}
2016-12-07 13:00:02 +01:00
void RenameFile(std::string_view path, std::string_view newPath) override
2016-12-07 13:00:02 +01:00
{
2018-05-12 17:11:51 +02:00
auto index = GetIndexFromPath(path);
if (index)
{
zip_file_rename(_zip, *index, newPath.data(), ZIP_FL_ENC_GUESS);
}
else
{
throw std::runtime_error("File does not exist.");
}
2016-12-07 13:00:02 +01:00
}
2018-05-12 17:11:51 +02:00
private:
2021-01-20 21:42:03 +01:00
class ZipItemStream final : public IStream
2018-05-12 17:11:51 +02:00
{
private:
2021-01-20 21:42:03 +01:00
zip* _zip;
zip_int64_t _index;
zip_file_t* _zipFile{};
zip_uint64_t _len{};
zip_uint64_t _pos{};
2021-01-20 21:42:03 +01:00
public:
ZipItemStream(zip* zip, zip_int64_t index)
: _zip(zip)
, _index(index)
{
}
~ZipItemStream() override
{
Close();
}
bool CanRead() const override
{
return true;
}
bool CanWrite() const override
{
return false;
}
uint64_t GetLength() const override
{
return _len;
}
uint64_t GetPosition() const override
{
return _pos;
}
2021-01-20 21:42:03 +01:00
void SetPosition(uint64_t position) override
{
if (position > _pos)
{
2021-01-20 21:42:03 +01:00
// Read to seek forwards
Skip(position - _pos);
}
2021-01-20 21:42:03 +01:00
else if (position < _pos)
2018-05-12 17:11:51 +02:00
{
2021-01-20 21:42:03 +01:00
// Can not seek backwards, start from the beginning
Reset();
Skip(position);
2018-05-12 17:11:51 +02:00
}
2021-01-20 21:42:03 +01:00
}
2018-05-12 17:11:51 +02:00
2021-01-20 21:42:03 +01:00
void Seek(int64_t offset, int32_t origin) override
{
switch (origin)
{
2021-01-20 21:42:03 +01:00
case STREAM_SEEK_BEGIN:
SetPosition(offset);
break;
case STREAM_SEEK_CURRENT:
SetPosition(_pos + offset);
break;
case STREAM_SEEK_END:
SetPosition(_len - offset);
break;
}
}
2021-01-20 21:42:03 +01:00
void Read(void* buffer, uint64_t length) override
{
size_t readBytes = TryRead(buffer, length);
if (readBytes != length)
{
throw IOException("Attempted to read past end of file.");
}
}
2021-01-20 21:42:03 +01:00
void Write(const void* buffer, uint64_t length) override
{
throw IOException("Stream is read-only.");
}
2021-01-20 21:42:03 +01:00
uint64_t TryRead(void* buffer, uint64_t length) override
{
if (_zipFile == nullptr && !Reset())
{
return 0;
}
2021-01-20 21:42:03 +01:00
auto readBytes = zip_fread(_zipFile, buffer, length);
if (readBytes < 0)
{
2021-01-20 21:42:03 +01:00
return 0;
}
2021-01-20 21:42:03 +01:00
else
{
_pos += readBytes;
return static_cast<uint64_t>(readBytes);
}
}
2021-01-20 21:42:03 +01:00
const void* GetData() const override
{
return nullptr;
}
private:
void Close()
{
if (_zipFile != nullptr)
2018-05-12 17:11:51 +02:00
{
2021-01-20 21:42:03 +01:00
zip_fclose(_zipFile);
_zipFile = nullptr;
2018-05-12 17:11:51 +02:00
}
2021-01-20 21:42:03 +01:00
}
bool Reset()
{
Close();
2021-01-20 21:42:03 +01:00
_pos = 0;
_len = 0;
_zipFile = zip_fopen_index(_zip, _index, 0);
if (_zipFile == nullptr)
{
2021-01-20 21:42:03 +01:00
return false;
}
2021-01-20 21:42:03 +01:00
zip_stat_t zipFileStat{};
if (zip_stat_index(_zip, _index, 0, &zipFileStat) != ZIP_ER_OK)
{
2021-01-20 21:42:03 +01:00
return false;
}
2021-01-20 21:42:03 +01:00
_len = zipFileStat.size;
return true;
}
2021-01-20 21:42:03 +01:00
void Skip(zip_int64_t len)
{
2021-01-20 21:42:03 +01:00
// zip_fseek can not be used on compressed data, so skip bytes by
// reading into a temporary buffer
char buffer[2048]{};
while (len > 0)
{
auto readLen = std::min<zip_int64_t>(len, sizeof(buffer));
auto read = zip_fread(_zipFile, buffer, readLen);
if (read <= 0)
{
break;
}
_pos += read;
len -= read;
}
2018-05-12 17:11:51 +02:00
}
};
2016-11-13 14:48:24 +01:00
};
namespace Zip
{
std::unique_ptr<IZipArchive> Open(std::string_view path, ZIP_ACCESS access)
2016-11-13 14:48:24 +01:00
{
return std::make_unique<ZipArchive>(path, access);
2016-11-13 14:48:24 +01:00
}
std::unique_ptr<IZipArchive> TryOpen(std::string_view path, ZIP_ACCESS access)
2016-11-13 14:48:24 +01:00
{
std::unique_ptr<IZipArchive> result;
2016-11-13 14:48:24 +01:00
try
{
result = std::make_unique<ZipArchive>(path, access);
2016-11-13 14:48:24 +01:00
}
catch (const std::exception&)
2016-11-13 14:48:24 +01:00
{
}
return result;
}
2018-05-04 22:40:09 +02:00
} // namespace Zip
2017-06-15 14:22:15 +02:00
2018-06-22 22:58:39 +02:00
#endif