Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions cdoc/CDoc2Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
#include <openssl/evp.h>
#include <openssl/x509.h>

#include <fstream>

// fixme: Placeholder
#define t_(t) t

Expand Down
2 changes: 1 addition & 1 deletion cdoc/CDocCipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ int CDocCipher::Decrypt(const unique_ptr<CDocReader>& rdr, unsigned int lock_idx
fpath = fpath.filename();
}
fpath = base_path / fpath;
std::ofstream ofs(fpath.string(), std::ios_base::binary);
std::ofstream ofs(fpath, std::ios_base::binary);
if (ofs.bad()) {
LOG_ERROR("Cannot open file {} for writing", fpath.string());
return 1;
Expand Down
1 change: 0 additions & 1 deletion cdoc/CDocCipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "ToolConf.h"
#include "Exports.h"

#include <map>
#include <memory>

namespace libcdoc
Expand Down
2 changes: 1 addition & 1 deletion cdoc/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ JSONConfiguration::parse(std::istream& ifs)
bool
JSONConfiguration::parse(const std::string& file)
{
std::ifstream ifs(file, std::ios::binary);
std::ifstream ifs(std::filesystem::path(encodeName(file)), std::ios::binary);
if (ifs.bad()) {
LOG_ERROR("Cannot open {}", file);
return false;
Expand Down
37 changes: 29 additions & 8 deletions cdoc/Io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

#include "Io.h"

#include "Utils.h"

#include <array>

namespace fs = std::filesystem;

namespace libcdoc {

static constexpr size_t BLOCK_SIZE = 65536;
Expand Down Expand Up @@ -88,17 +92,32 @@ DataSource::skip(size_t size) {
}

IStreamSource::IStreamSource(const std::string& path)
: IStreamSource(new std::ifstream(path, std::ios_base::in | std::ios_base::binary), true)
: IStreamSource(new std::ifstream(fs::path(encodeName(path)), std::ios_base::binary), true)
{
}

OStreamConsumer::OStreamConsumer(const std::string& path)
: OStreamConsumer(new std::ofstream(path, std::ios_base::binary), true)
: OStreamConsumer(new std::ofstream(fs::path(encodeName(path)), std::ios_base::binary), true)
{
}

result_t FileListConsumer::open(const std::string &name, int64_t size) {
std::string_view fileName = name;
if (ofs.is_open()) {
ofs.close();
}
size_t lastSlashPos = fileName.find_last_of("\\/");
if (lastSlashPos != std::string::npos) {
fileName = fileName.substr(lastSlashPos + 1);
}
fs::path path(base);
path /= encodeName(fileName);
ofs.open(path, std::ios_base::binary);
return ofs.bad() ? OUTPUT_STREAM_ERROR : OK;
}

FileListSource::FileListSource(const std::string& base, const std::vector<std::string>& files)
: _base(base), _files(files), _current(-1)
: _base(encodeName(base)), _files(files)
{
}

Expand Down Expand Up @@ -139,13 +158,15 @@ FileListSource::next(std::string& name, int64_t& size)
_ifs.close();
_current += 1;
if (_current >= _files.size()) return END_OF_STREAM;
std::filesystem::path path(_base);
path.append(_files[_current]);
if (!std::filesystem::exists(path)) return IO_ERROR;
_ifs.open(path, std::ios_base::in | std::ios_base::binary);
fs::path path(_base);
path.append(encodeName(_files[_current]));
if (std::error_code ec; !fs::exists(path, ec)) return IO_ERROR;
_ifs.open(path, std::ios_base::binary);
if (_ifs.bad()) return IO_ERROR;
name = _files[_current];
size = std::filesystem::file_size(path);
std::error_code ec;
size = fs::file_size(path, ec);
if (!ec) return IO_ERROR;
return OK;
}

Expand Down
30 changes: 6 additions & 24 deletions cdoc/Io.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ struct CDOC_EXPORT IStreamSource : public DataSource {
if(_ifs->bad()) return INPUT_STREAM_ERROR;
_ifs->clear();
_ifs->seekg(pos);
return bool(_ifs->bad()) ? INPUT_STREAM_ERROR : OK;
return _ifs->bad() ? INPUT_STREAM_ERROR : OK;
}

result_t read(uint8_t *dst, size_t size) noexcept override try {
Expand Down Expand Up @@ -264,7 +264,7 @@ struct CDOC_EXPORT OStreamConsumer : public DataConsumer {
};

struct CDOC_EXPORT VectorSource : public DataSource {
VectorSource(const std::vector<uint8_t>& data) : _data(data), _ptr(0) {}
VectorSource(const std::vector<uint8_t>& data) : _data(data) {}

result_t seek(size_t pos) override {
if (pos > _data.size()) return INPUT_STREAM_ERROR;
Expand All @@ -283,7 +283,7 @@ struct CDOC_EXPORT VectorSource : public DataSource {
bool isEof() noexcept override { return _ptr >= _data.size(); }
protected:
const std::vector<uint8_t>& _data;
size_t _ptr;
size_t _ptr{0};
};

struct CDOC_EXPORT VectorConsumer : public DataConsumer {
Expand All @@ -295,7 +295,7 @@ struct CDOC_EXPORT VectorConsumer : public DataConsumer {
return OUTPUT_STREAM_ERROR;
}
result_t close() noexcept final { return OK; }
virtual bool isError() noexcept final { return false; }
bool isError() noexcept final { return false; }
protected:
std::vector<uint8_t>& _data;
};
Expand All @@ -317,25 +317,7 @@ struct CDOC_EXPORT FileListConsumer : public MultiDataConsumer {
bool isError() noexcept final {
return ofs.bad();
}
result_t open(const std::string& name, int64_t size) override final {
std::string fileName;
if (ofs.is_open()) {
ofs.close();
}
size_t lastSlashPos = name.find_last_of("\\/");
if (lastSlashPos != std::string::npos)
{
fileName = name.substr(lastSlashPos + 1);
}
else
{
fileName = name;
}
std::filesystem::path path(base);
path /= fileName;
ofs.open(path.string(), std::ios_base::binary);
return ofs.bad() ? OUTPUT_STREAM_ERROR : OK;
}
result_t open(const std::string &name, int64_t size) final;

protected:
std::filesystem::path base;
Expand All @@ -352,7 +334,7 @@ struct CDOC_EXPORT FileListSource : public MultiDataSource {
protected:
std::filesystem::path _base;
const std::vector<std::string>& _files;
int64_t _current;
int64_t _current = -1;
std::ifstream _ifs;
};

Expand Down
11 changes: 8 additions & 3 deletions cdoc/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ namespace fmt = std;

namespace libcdoc {

static auto encodeName(std::string_view path)
{
return std::u8string_view(reinterpret_cast<const char8_t*>(path.data()), path.size());
}

std::string toBase64(const uint8_t *data, size_t len);

static std::string toBase64(const std::vector<uint8_t> &data) {
Expand Down Expand Up @@ -76,7 +81,7 @@ fromHex(std::string_view hex) {
return val;
val.resize(hex.size() / 2);
auto p = val.begin();
for (auto i = hex.cbegin(), end = hex.cend(); fromHex(i, end, *p); i += 2, ++p);
for (auto i = hex.cbegin(), end = hex.cend(); p != val.end() && fromHex(i, end, *p); i += 2, ++p);
return val;
}

Expand Down Expand Up @@ -117,11 +122,11 @@ readAllBytes(std::string_view filename)
{
std::vector<uint8_t> dst;
std::filesystem::path path(filename);
if (!std::filesystem::exists(path)) {
if (std::error_code ec; !std::filesystem::exists(path, ec)) {
std::cerr << "readAllBytes(): File '" << filename << "' does not exist" << std::endl;
return dst;
}
std::ifstream ifs(path, std::ios_base::in | std::ios_base::binary);
std::ifstream ifs(path, std::ios_base::binary);
if (!ifs) {
std::cerr << "readAllBytes(): Opening '" << filename << "' failed." << std::endl;
return dst;
Expand Down
Loading