diff --git a/cdoc/CDoc2Reader.cpp b/cdoc/CDoc2Reader.cpp index 114baaf0..98db9ad8 100644 --- a/cdoc/CDoc2Reader.cpp +++ b/cdoc/CDoc2Reader.cpp @@ -37,8 +37,6 @@ #include #include -#include - // fixme: Placeholder #define t_(t) t diff --git a/cdoc/CDocCipher.cpp b/cdoc/CDocCipher.cpp index 0d49a38e..1ce552b8 100644 --- a/cdoc/CDocCipher.cpp +++ b/cdoc/CDocCipher.cpp @@ -468,7 +468,7 @@ int CDocCipher::Decrypt(const unique_ptr& 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; diff --git a/cdoc/CDocCipher.h b/cdoc/CDocCipher.h index 3138dc50..92bcc079 100644 --- a/cdoc/CDocCipher.h +++ b/cdoc/CDocCipher.h @@ -25,7 +25,6 @@ #include "ToolConf.h" #include "Exports.h" -#include #include namespace libcdoc diff --git a/cdoc/Configuration.cpp b/cdoc/Configuration.cpp index eee0586d..256172a8 100644 --- a/cdoc/Configuration.cpp +++ b/cdoc/Configuration.cpp @@ -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; diff --git a/cdoc/Io.cpp b/cdoc/Io.cpp index 2860f741..1d845b1d 100644 --- a/cdoc/Io.cpp +++ b/cdoc/Io.cpp @@ -18,8 +18,12 @@ #include "Io.h" +#include "Utils.h" + #include +namespace fs = std::filesystem; + namespace libcdoc { static constexpr size_t BLOCK_SIZE = 65536; @@ -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& files) - : _base(base), _files(files), _current(-1) + : _base(encodeName(base)), _files(files) { } @@ -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; } diff --git a/cdoc/Io.h b/cdoc/Io.h index bd4dcbdd..5bc5eecf 100644 --- a/cdoc/Io.h +++ b/cdoc/Io.h @@ -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 { @@ -264,7 +264,7 @@ struct CDOC_EXPORT OStreamConsumer : public DataConsumer { }; struct CDOC_EXPORT VectorSource : public DataSource { - VectorSource(const std::vector& data) : _data(data), _ptr(0) {} + VectorSource(const std::vector& data) : _data(data) {} result_t seek(size_t pos) override { if (pos > _data.size()) return INPUT_STREAM_ERROR; @@ -283,7 +283,7 @@ struct CDOC_EXPORT VectorSource : public DataSource { bool isEof() noexcept override { return _ptr >= _data.size(); } protected: const std::vector& _data; - size_t _ptr; + size_t _ptr{0}; }; struct CDOC_EXPORT VectorConsumer : public DataConsumer { @@ -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& _data; }; @@ -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; @@ -352,7 +334,7 @@ struct CDOC_EXPORT FileListSource : public MultiDataSource { protected: std::filesystem::path _base; const std::vector& _files; - int64_t _current; + int64_t _current = -1; std::ifstream _ifs; }; diff --git a/cdoc/Utils.h b/cdoc/Utils.h index 4e86400a..392b81b4 100644 --- a/cdoc/Utils.h +++ b/cdoc/Utils.h @@ -43,6 +43,11 @@ namespace fmt = std; namespace libcdoc { +static auto encodeName(std::string_view path) +{ + return std::u8string_view(reinterpret_cast(path.data()), path.size()); +} + std::string toBase64(const uint8_t *data, size_t len); static std::string toBase64(const std::vector &data) { @@ -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; } @@ -117,11 +122,11 @@ readAllBytes(std::string_view filename) { std::vector 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;